aboutsummaryrefslogtreecommitdiff
path: root/samples
diff options
context:
space:
mode:
authorBen Murdoch <benm@google.com>2016-03-22 12:00:34 +0000
committerBen Murdoch <benm@google.com>2016-04-05 15:27:36 +0100
commit014dc512cdd3e367bee49a713fdc5ed92584a3e5 (patch)
tree742b8bb81c9998b13f6a801f8e0bec6ae9a568c1 /samples
parent094c92c64194bd11593e915f372914dcfccf9dd2 (diff)
downloadv8-014dc512cdd3e367bee49a713fdc5ed92584a3e5.tar.gz
Upgrade V8 to version 4.9.385.28
https://chromium.googlesource.com/v8/v8/+/4.9.385.28 Change-Id: I4b2e74289d4bf3667f2f3dc8aa2e541f63e26eb4
Diffstat (limited to 'samples')
-rw-r--r--samples/SConscript38
-rw-r--r--samples/hello-world.cc72
-rw-r--r--samples/lineprocessor.cc385
-rw-r--r--samples/process.cc243
-rw-r--r--samples/samples.gyp6
-rw-r--r--samples/shell.cc195
6 files changed, 339 insertions, 600 deletions
diff --git a/samples/SConscript b/samples/SConscript
deleted file mode 100644
index 84c48c90..00000000
--- a/samples/SConscript
+++ /dev/null
@@ -1,38 +0,0 @@
-# Copyright 2008 the V8 project authors. All rights reserved.
-# Redistribution and use in source and binary forms, with or without
-# modification, are permitted provided that the following conditions are
-# met:
-#
-# * Redistributions of source code must retain the above copyright
-# notice, this list of conditions and the following disclaimer.
-# * Redistributions in binary form must reproduce the above
-# copyright notice, this list of conditions and the following
-# disclaimer in the documentation and/or other materials provided
-# with the distribution.
-# * Neither the name of Google Inc. nor the names of its
-# contributors may be used to endorse or promote products derived
-# from this software without specific prior written permission.
-#
-# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
-# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
-# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
-# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
-# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
-# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
-# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
-# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
-from os.path import join
-Import('sample context tools')
-
-def ConfigureObjectFiles():
- env = Environment(tools=tools)
- env.Replace(**context.flags['sample'])
- context.ApplyEnvOverrides(env)
- return env.Object(sample + '.cc')
-
-sample_object = ConfigureObjectFiles()
-Return('sample_object')
diff --git a/samples/hello-world.cc b/samples/hello-world.cc
new file mode 100644
index 00000000..3b952d81
--- /dev/null
+++ b/samples/hello-world.cc
@@ -0,0 +1,72 @@
+// Copyright 2015 the V8 project authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "include/libplatform/libplatform.h"
+#include "include/v8.h"
+
+using namespace v8;
+
+class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
+ public:
+ virtual void* Allocate(size_t length) {
+ void* data = AllocateUninitialized(length);
+ return data == NULL ? data : memset(data, 0, length);
+ }
+ virtual void* AllocateUninitialized(size_t length) { return malloc(length); }
+ virtual void Free(void* data, size_t) { free(data); }
+};
+
+
+int main(int argc, char* argv[]) {
+ // Initialize V8.
+ V8::InitializeICU();
+ V8::InitializeExternalStartupData(argv[0]);
+ Platform* platform = platform::CreateDefaultPlatform();
+ V8::InitializePlatform(platform);
+ V8::Initialize();
+
+ // Create a new Isolate and make it the current one.
+ ArrayBufferAllocator allocator;
+ Isolate::CreateParams create_params;
+ create_params.array_buffer_allocator = &allocator;
+ Isolate* isolate = Isolate::New(create_params);
+ {
+ Isolate::Scope isolate_scope(isolate);
+
+ // Create a stack-allocated handle scope.
+ HandleScope handle_scope(isolate);
+
+ // Create a new context.
+ Local<Context> context = Context::New(isolate);
+
+ // Enter the context for compiling and running the hello world script.
+ Context::Scope context_scope(context);
+
+ // Create a string containing the JavaScript source code.
+ Local<String> source =
+ String::NewFromUtf8(isolate, "'Hello' + ', World!'",
+ NewStringType::kNormal).ToLocalChecked();
+
+ // Compile the source code.
+ Local<Script> script = Script::Compile(context, source).ToLocalChecked();
+
+ // Run the script to get the result.
+ Local<Value> result = script->Run(context).ToLocalChecked();
+
+ // Convert the result to an UTF8 string and print it.
+ String::Utf8Value utf8(result);
+ printf("%s\n", *utf8);
+ }
+
+ // Dispose the isolate and tear down V8.
+ isolate->Dispose();
+ V8::Dispose();
+ V8::ShutdownPlatform();
+ delete platform;
+ return 0;
+}
diff --git a/samples/lineprocessor.cc b/samples/lineprocessor.cc
deleted file mode 100644
index 69bfab49..00000000
--- a/samples/lineprocessor.cc
+++ /dev/null
@@ -1,385 +0,0 @@
-// Copyright 2012 the V8 project authors. All rights reserved.
-// Redistribution and use in source and binary forms, with or without
-// modification, are permitted provided that the following conditions are
-// met:
-//
-// * Redistributions of source code must retain the above copyright
-// notice, this list of conditions and the following disclaimer.
-// * Redistributions in binary form must reproduce the above
-// copyright notice, this list of conditions and the following
-// disclaimer in the documentation and/or other materials provided
-// with the distribution.
-// * Neither the name of Google Inc. nor the names of its
-// contributors may be used to endorse or promote products derived
-// from this software without specific prior written permission.
-//
-// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
-// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
-// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
-// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
-// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
-// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
-// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
-// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
-#include <include/v8.h>
-
-#include <include/libplatform/libplatform.h>
-#include <include/v8-debug.h>
-
-#include <fcntl.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-/**
- * This sample program should demonstrate certain aspects of debugging
- * standalone V8-based application.
- *
- * The program reads input stream, processes it line by line and print
- * the result to output. The actual processing is done by custom JavaScript
- * script. The script is specified with command line parameters.
- *
- * The main cycle of the program will sequentially read lines from standard
- * input, process them and print to standard output until input closes.
- * There are 2 possible configuration in regard to main cycle.
- *
- * 1. The main cycle is on C++ side. Program should be run with
- * --main-cycle-in-cpp option. Script must declare a function named
- * "ProcessLine". The main cycle in C++ reads lines and calls this function
- * for processing every time. This is a sample script:
-
-function ProcessLine(input_line) {
- return ">>>" + input_line + "<<<";
-}
-
- *
- * 2. The main cycle is in JavaScript. Program should be run with
- * --main-cycle-in-js option. Script gets run one time at all and gets
- * API of 2 global functions: "read_line" and "print". It should read input
- * and print converted lines to output itself. This a sample script:
-
-while (true) {
- var line = read_line();
- if (!line) {
- break;
- }
- var res = line + " | " + line;
- print(res);
-}
- */
-
-enum MainCycleType {
- CycleInCpp,
- CycleInJs
-};
-
-const char* ToCString(const v8::String::Utf8Value& value);
-void ReportException(v8::Isolate* isolate, v8::TryCatch* handler);
-v8::Handle<v8::String> ReadFile(v8::Isolate* isolate, const char* name);
-v8::Handle<v8::String> ReadLine();
-
-void Print(const v8::FunctionCallbackInfo<v8::Value>& args);
-void ReadLine(const v8::FunctionCallbackInfo<v8::Value>& args);
-bool RunCppCycle(v8::Handle<v8::Script> script,
- v8::Local<v8::Context> context,
- bool report_exceptions);
-
-
-v8::Persistent<v8::Context> debug_message_context;
-
-int RunMain(int argc, char* argv[]) {
- v8::V8::SetFlagsFromCommandLine(&argc, argv, true);
- v8::Isolate* isolate = v8::Isolate::New();
- v8::Isolate::Scope isolate_scope(isolate);
- v8::HandleScope handle_scope(isolate);
-
- v8::Handle<v8::String> script_source;
- v8::Handle<v8::Value> script_name;
- int script_param_counter = 0;
-
- MainCycleType cycle_type = CycleInCpp;
-
- for (int i = 1; i < argc; i++) {
- const char* str = argv[i];
- if (strcmp(str, "-f") == 0) {
- // Ignore any -f flags for compatibility with the other stand-
- // alone JavaScript engines.
- continue;
- } else if (strcmp(str, "--main-cycle-in-cpp") == 0) {
- cycle_type = CycleInCpp;
- } else if (strcmp(str, "--main-cycle-in-js") == 0) {
- cycle_type = CycleInJs;
- } else if (strncmp(str, "--", 2) == 0) {
- printf("Warning: unknown flag %s.\nTry --help for options\n", str);
- } else if (strcmp(str, "-e") == 0 && i + 1 < argc) {
- script_source = v8::String::NewFromUtf8(isolate, argv[i + 1]);
- script_name = v8::String::NewFromUtf8(isolate, "unnamed");
- i++;
- script_param_counter++;
- } else {
- // Use argument as a name of file to load.
- script_source = ReadFile(isolate, str);
- script_name = v8::String::NewFromUtf8(isolate, str);
- if (script_source.IsEmpty()) {
- printf("Error reading '%s'\n", str);
- return 1;
- }
- script_param_counter++;
- }
- }
-
- if (script_param_counter == 0) {
- printf("Script is not specified\n");
- return 1;
- }
- if (script_param_counter != 1) {
- printf("Only one script may be specified\n");
- return 1;
- }
-
- // Create a template for the global object.
- v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(isolate);
-
- // Bind the global 'print' function to the C++ Print callback.
- global->Set(v8::String::NewFromUtf8(isolate, "print"),
- v8::FunctionTemplate::New(isolate, Print));
-
- if (cycle_type == CycleInJs) {
- // Bind the global 'read_line' function to the C++ Print callback.
- global->Set(v8::String::NewFromUtf8(isolate, "read_line"),
- v8::FunctionTemplate::New(isolate, ReadLine));
- }
-
- // Create a new execution environment containing the built-in
- // functions
- v8::Handle<v8::Context> context = v8::Context::New(isolate, NULL, global);
- // Enter the newly created execution environment.
- v8::Context::Scope context_scope(context);
-
- debug_message_context.Reset(isolate, context);
-
- bool report_exceptions = true;
-
- v8::Handle<v8::Script> script;
- {
- // Compile script in try/catch context.
- v8::TryCatch try_catch;
- v8::ScriptOrigin origin(script_name);
- script = v8::Script::Compile(script_source, &origin);
- if (script.IsEmpty()) {
- // Print errors that happened during compilation.
- if (report_exceptions)
- ReportException(isolate, &try_catch);
- return 1;
- }
- }
-
- {
- v8::TryCatch try_catch;
-
- script->Run();
- if (try_catch.HasCaught()) {
- if (report_exceptions)
- ReportException(isolate, &try_catch);
- return 1;
- }
- }
-
- if (cycle_type == CycleInCpp) {
- bool res = RunCppCycle(script,
- isolate->GetCurrentContext(),
- report_exceptions);
- return !res;
- } else {
- // All is already done.
- }
- return 0;
-}
-
-
-bool RunCppCycle(v8::Handle<v8::Script> script,
- v8::Local<v8::Context> context,
- bool report_exceptions) {
- v8::Isolate* isolate = context->GetIsolate();
-
- v8::Handle<v8::String> fun_name =
- v8::String::NewFromUtf8(isolate, "ProcessLine");
- v8::Handle<v8::Value> process_val = context->Global()->Get(fun_name);
-
- // If there is no Process function, or if it is not a function,
- // bail out
- if (!process_val->IsFunction()) {
- printf("Error: Script does not declare 'ProcessLine' global function.\n");
- return 1;
- }
-
- // It is a function; cast it to a Function
- v8::Handle<v8::Function> process_fun =
- v8::Handle<v8::Function>::Cast(process_val);
-
-
- while (!feof(stdin)) {
- v8::HandleScope handle_scope(isolate);
-
- v8::Handle<v8::String> input_line = ReadLine();
- if (input_line == v8::Undefined(isolate)) {
- continue;
- }
-
- const int argc = 1;
- v8::Handle<v8::Value> argv[argc] = { input_line };
-
- v8::Handle<v8::Value> result;
- {
- v8::TryCatch try_catch;
- result = process_fun->Call(isolate->GetCurrentContext()->Global(),
- argc, argv);
- if (try_catch.HasCaught()) {
- if (report_exceptions)
- ReportException(isolate, &try_catch);
- return false;
- }
- }
- v8::String::Utf8Value str(result);
- const char* cstr = ToCString(str);
- printf("%s\n", cstr);
- }
-
- return true;
-}
-
-
-int main(int argc, char* argv[]) {
- v8::V8::InitializeICU();
- v8::Platform* platform = v8::platform::CreateDefaultPlatform();
- v8::V8::InitializePlatform(platform);
- v8::V8::Initialize();
- int result = RunMain(argc, argv);
- v8::V8::Dispose();
- v8::V8::ShutdownPlatform();
- delete platform;
- return result;
-}
-
-
-// Extracts a C string from a V8 Utf8Value.
-const char* ToCString(const v8::String::Utf8Value& value) {
- return *value ? *value : "<string conversion failed>";
-}
-
-
-// Reads a file into a v8 string.
-v8::Handle<v8::String> ReadFile(v8::Isolate* isolate, const char* name) {
- FILE* file = fopen(name, "rb");
- if (file == NULL) return v8::Handle<v8::String>();
-
- fseek(file, 0, SEEK_END);
- int size = ftell(file);
- rewind(file);
-
- char* chars = new char[size + 1];
- chars[size] = '\0';
- for (int i = 0; i < size;) {
- int read = static_cast<int>(fread(&chars[i], 1, size - i, file));
- i += read;
- }
- fclose(file);
- v8::Handle<v8::String> result =
- v8::String::NewFromUtf8(isolate, chars, v8::String::kNormalString, size);
- delete[] chars;
- return result;
-}
-
-
-void ReportException(v8::Isolate* isolate, v8::TryCatch* try_catch) {
- v8::HandleScope handle_scope(isolate);
- v8::String::Utf8Value exception(try_catch->Exception());
- const char* exception_string = ToCString(exception);
- v8::Handle<v8::Message> message = try_catch->Message();
- if (message.IsEmpty()) {
- // V8 didn't provide any extra information about this error; just
- // print the exception.
- printf("%s\n", exception_string);
- } else {
- // Print (filename):(line number): (message).
- v8::String::Utf8Value filename(message->GetScriptOrigin().ResourceName());
- const char* filename_string = ToCString(filename);
- int linenum = message->GetLineNumber();
- printf("%s:%i: %s\n", filename_string, linenum, exception_string);
- // Print line of source code.
- v8::String::Utf8Value sourceline(message->GetSourceLine());
- const char* sourceline_string = ToCString(sourceline);
- printf("%s\n", sourceline_string);
- // Print wavy underline (GetUnderline is deprecated).
- int start = message->GetStartColumn();
- for (int i = 0; i < start; i++) {
- printf(" ");
- }
- int end = message->GetEndColumn();
- for (int i = start; i < end; i++) {
- printf("^");
- }
- printf("\n");
- }
-}
-
-
-// The callback that is invoked by v8 whenever the JavaScript 'print'
-// function is called. Prints its arguments on stdout separated by
-// spaces and ending with a newline.
-void Print(const v8::FunctionCallbackInfo<v8::Value>& args) {
- bool first = true;
- for (int i = 0; i < args.Length(); i++) {
- v8::HandleScope handle_scope(args.GetIsolate());
- if (first) {
- first = false;
- } else {
- printf(" ");
- }
- v8::String::Utf8Value str(args[i]);
- const char* cstr = ToCString(str);
- printf("%s", cstr);
- }
- printf("\n");
- fflush(stdout);
-}
-
-
-// The callback that is invoked by v8 whenever the JavaScript 'read_line'
-// function is called. Reads a string from standard input and returns.
-void ReadLine(const v8::FunctionCallbackInfo<v8::Value>& args) {
- if (args.Length() > 0) {
- args.GetIsolate()->ThrowException(
- v8::String::NewFromUtf8(args.GetIsolate(), "Unexpected arguments"));
- return;
- }
- args.GetReturnValue().Set(ReadLine());
-}
-
-
-v8::Handle<v8::String> ReadLine() {
- const int kBufferSize = 1024 + 1;
- char buffer[kBufferSize];
-
- char* res;
- {
- res = fgets(buffer, kBufferSize, stdin);
- }
- v8::Isolate* isolate = v8::Isolate::GetCurrent();
- if (res == NULL) {
- v8::Handle<v8::Primitive> t = v8::Undefined(isolate);
- return v8::Handle<v8::String>::Cast(t);
- }
- // Remove newline char
- for (char* pos = buffer; *pos != '\0'; pos++) {
- if (*pos == '\n') {
- *pos = '\0';
- break;
- }
- }
- return v8::String::NewFromUtf8(isolate, buffer);
-}
diff --git a/samples/process.cc b/samples/process.cc
index f4479706..cfbd054c 100644
--- a/samples/process.cc
+++ b/samples/process.cc
@@ -29,12 +29,26 @@
#include <include/libplatform/libplatform.h>
+#include <stdlib.h>
+#include <string.h>
+
#include <map>
#include <string>
using namespace std;
using namespace v8;
+class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
+ public:
+ virtual void* Allocate(size_t length) {
+ void* data = AllocateUninitialized(length);
+ return data == NULL ? data : memset(data, 0, length);
+ }
+ virtual void* AllocateUninitialized(size_t length) { return malloc(length); }
+ virtual void Free(void* data, size_t) { free(data); }
+};
+
+
// These interfaces represent an existing request processing interface.
// The idea is to imagine a real application that uses these interfaces
// and then add scripting capabilities that allow you to interact with
@@ -79,8 +93,8 @@ class JsHttpRequestProcessor : public HttpRequestProcessor {
public:
// Creates a new processor that processes requests by invoking the
// Process function of the JavaScript script given as an argument.
- JsHttpRequestProcessor(Isolate* isolate, Handle<String> script)
- : isolate_(isolate), script_(script) { }
+ JsHttpRequestProcessor(Isolate* isolate, Local<String> script)
+ : isolate_(isolate), script_(script) {}
virtual ~JsHttpRequestProcessor();
virtual bool Initialize(map<string, string>* opts,
@@ -90,7 +104,7 @@ class JsHttpRequestProcessor : public HttpRequestProcessor {
private:
// Execute the script associated with this processor and extract the
// Process function. Returns true if this succeeded, otherwise false.
- bool ExecuteScript(Handle<String> script);
+ bool ExecuteScript(Local<String> script);
// Wrap the options and output map in a JavaScript objects and
// install it in the global namespace as 'options' and 'output'.
@@ -98,8 +112,8 @@ class JsHttpRequestProcessor : public HttpRequestProcessor {
// Constructs the template that describes the JavaScript wrapper
// type for requests.
- static Handle<ObjectTemplate> MakeRequestTemplate(Isolate* isolate);
- static Handle<ObjectTemplate> MakeMapTemplate(Isolate* isolate);
+ static Local<ObjectTemplate> MakeRequestTemplate(Isolate* isolate);
+ static Local<ObjectTemplate> MakeMapTemplate(Isolate* isolate);
// Callbacks that access the individual fields of request objects.
static void GetPath(Local<String> name,
@@ -118,19 +132,19 @@ class JsHttpRequestProcessor : public HttpRequestProcessor {
// Utility methods for wrapping C++ objects as JavaScript objects,
// and going back again.
- Handle<Object> WrapMap(map<string, string>* obj);
- static map<string, string>* UnwrapMap(Handle<Object> obj);
- Handle<Object> WrapRequest(HttpRequest* obj);
- static HttpRequest* UnwrapRequest(Handle<Object> obj);
+ Local<Object> WrapMap(map<string, string>* obj);
+ static map<string, string>* UnwrapMap(Local<Object> obj);
+ Local<Object> WrapRequest(HttpRequest* obj);
+ static HttpRequest* UnwrapRequest(Local<Object> obj);
Isolate* GetIsolate() { return isolate_; }
Isolate* isolate_;
- Handle<String> script_;
- Persistent<Context> context_;
- Persistent<Function> process_;
- static Persistent<ObjectTemplate> request_template_;
- static Persistent<ObjectTemplate> map_template_;
+ Local<String> script_;
+ Global<Context> context_;
+ Global<Function> process_;
+ static Global<ObjectTemplate> request_template_;
+ static Global<ObjectTemplate> map_template_;
};
@@ -142,7 +156,7 @@ class JsHttpRequestProcessor : public HttpRequestProcessor {
static void LogCallback(const v8::FunctionCallbackInfo<v8::Value>& args) {
if (args.Length() < 1) return;
HandleScope scope(args.GetIsolate());
- Handle<Value> arg = args[0];
+ Local<Value> arg = args[0];
String::Utf8Value value(arg);
HttpRequestProcessor::Log(*value);
}
@@ -156,8 +170,9 @@ bool JsHttpRequestProcessor::Initialize(map<string, string>* opts,
// Create a template for the global object where we set the
// built-in global functions.
- Handle<ObjectTemplate> global = ObjectTemplate::New(GetIsolate());
- global->Set(String::NewFromUtf8(GetIsolate(), "log"),
+ Local<ObjectTemplate> global = ObjectTemplate::New(GetIsolate());
+ global->Set(String::NewFromUtf8(GetIsolate(), "log", NewStringType::kNormal)
+ .ToLocalChecked(),
FunctionTemplate::New(GetIsolate(), LogCallback));
// Each processor gets its own context so different processors don't
@@ -165,7 +180,7 @@ bool JsHttpRequestProcessor::Initialize(map<string, string>* opts,
// is what we need for the reference to remain after we return from
// this method. That persistent handle has to be disposed in the
// destructor.
- v8::Handle<v8::Context> context = Context::New(GetIsolate(), NULL, global);
+ v8::Local<v8::Context> context = Context::New(GetIsolate(), NULL, global);
context_.Reset(GetIsolate(), context);
// Enter the new context so all the following operations take place
@@ -182,17 +197,21 @@ bool JsHttpRequestProcessor::Initialize(map<string, string>* opts,
// The script compiled and ran correctly. Now we fetch out the
// Process function from the global object.
- Handle<String> process_name = String::NewFromUtf8(GetIsolate(), "Process");
- Handle<Value> process_val = context->Global()->Get(process_name);
-
+ Local<String> process_name =
+ String::NewFromUtf8(GetIsolate(), "Process", NewStringType::kNormal)
+ .ToLocalChecked();
+ Local<Value> process_val;
// If there is no Process function, or if it is not a function,
// bail out
- if (!process_val->IsFunction()) return false;
+ if (!context->Global()->Get(context, process_name).ToLocal(&process_val) ||
+ !process_val->IsFunction()) {
+ return false;
+ }
// It is a function; cast it to a Function
- Handle<Function> process_fun = Handle<Function>::Cast(process_val);
+ Local<Function> process_fun = Local<Function>::Cast(process_val);
- // Store the function in a Persistent handle, since we also want
+ // Store the function in a Global handle, since we also want
// that to remain after this call returns
process_.Reset(GetIsolate(), process_fun);
@@ -201,16 +220,18 @@ bool JsHttpRequestProcessor::Initialize(map<string, string>* opts,
}
-bool JsHttpRequestProcessor::ExecuteScript(Handle<String> script) {
+bool JsHttpRequestProcessor::ExecuteScript(Local<String> script) {
HandleScope handle_scope(GetIsolate());
// We're just about to compile the script; set up an error handler to
// catch any exceptions the script might throw.
- TryCatch try_catch;
+ TryCatch try_catch(GetIsolate());
+
+ Local<Context> context(GetIsolate()->GetCurrentContext());
// Compile the script and check for errors.
- Handle<Script> compiled_script = Script::Compile(script);
- if (compiled_script.IsEmpty()) {
+ Local<Script> compiled_script;
+ if (!Script::Compile(context, script).ToLocal(&compiled_script)) {
String::Utf8Value error(try_catch.Exception());
Log(*error);
// The script failed to compile; bail out.
@@ -218,8 +239,8 @@ bool JsHttpRequestProcessor::ExecuteScript(Handle<String> script) {
}
// Run the script!
- Handle<Value> result = compiled_script->Run();
- if (result.IsEmpty()) {
+ Local<Value> result;
+ if (!compiled_script->Run(context).ToLocal(&result)) {
// The TryCatch above is still in effect and will have caught the error.
String::Utf8Value error(try_catch.Exception());
Log(*error);
@@ -235,18 +256,26 @@ bool JsHttpRequestProcessor::InstallMaps(map<string, string>* opts,
HandleScope handle_scope(GetIsolate());
// Wrap the map object in a JavaScript wrapper
- Handle<Object> opts_obj = WrapMap(opts);
+ Local<Object> opts_obj = WrapMap(opts);
v8::Local<v8::Context> context =
v8::Local<v8::Context>::New(GetIsolate(), context_);
// Set the options object as a property on the global object.
- context->Global()->Set(String::NewFromUtf8(GetIsolate(), "options"),
- opts_obj);
-
- Handle<Object> output_obj = WrapMap(output);
- context->Global()->Set(String::NewFromUtf8(GetIsolate(), "output"),
- output_obj);
+ context->Global()
+ ->Set(context,
+ String::NewFromUtf8(GetIsolate(), "options", NewStringType::kNormal)
+ .ToLocalChecked(),
+ opts_obj)
+ .FromJust();
+
+ Local<Object> output_obj = WrapMap(output);
+ context->Global()
+ ->Set(context,
+ String::NewFromUtf8(GetIsolate(), "output", NewStringType::kNormal)
+ .ToLocalChecked(),
+ output_obj)
+ .FromJust();
return true;
}
@@ -264,19 +293,19 @@ bool JsHttpRequestProcessor::Process(HttpRequest* request) {
Context::Scope context_scope(context);
// Wrap the C++ request object in a JavaScript wrapper
- Handle<Object> request_obj = WrapRequest(request);
+ Local<Object> request_obj = WrapRequest(request);
// Set up an exception handler before calling the Process function
- TryCatch try_catch;
+ TryCatch try_catch(GetIsolate());
// Invoke the process function, giving the global object as 'this'
// and one argument, the request.
const int argc = 1;
- Handle<Value> argv[argc] = { request_obj };
+ Local<Value> argv[argc] = {request_obj};
v8::Local<v8::Function> process =
v8::Local<v8::Function>::New(GetIsolate(), process_);
- Handle<Value> result = process->Call(context->Global(), argc, argv);
- if (result.IsEmpty()) {
+ Local<Value> result;
+ if (!process->Call(context, context->Global(), argc, argv).ToLocal(&result)) {
String::Utf8Value error(try_catch.Exception());
Log(*error);
return false;
@@ -295,8 +324,8 @@ JsHttpRequestProcessor::~JsHttpRequestProcessor() {
}
-Persistent<ObjectTemplate> JsHttpRequestProcessor::request_template_;
-Persistent<ObjectTemplate> JsHttpRequestProcessor::map_template_;
+Global<ObjectTemplate> JsHttpRequestProcessor::request_template_;
+Global<ObjectTemplate> JsHttpRequestProcessor::map_template_;
// -----------------------------------
@@ -305,25 +334,26 @@ Persistent<ObjectTemplate> JsHttpRequestProcessor::map_template_;
// Utility function that wraps a C++ http request object in a
// JavaScript object.
-Handle<Object> JsHttpRequestProcessor::WrapMap(map<string, string>* obj) {
- // Handle scope for temporary handles.
+Local<Object> JsHttpRequestProcessor::WrapMap(map<string, string>* obj) {
+ // Local scope for temporary handles.
EscapableHandleScope handle_scope(GetIsolate());
// Fetch the template for creating JavaScript map wrappers.
// It only has to be created once, which we do on demand.
if (map_template_.IsEmpty()) {
- Handle<ObjectTemplate> raw_template = MakeMapTemplate(GetIsolate());
+ Local<ObjectTemplate> raw_template = MakeMapTemplate(GetIsolate());
map_template_.Reset(GetIsolate(), raw_template);
}
- Handle<ObjectTemplate> templ =
+ Local<ObjectTemplate> templ =
Local<ObjectTemplate>::New(GetIsolate(), map_template_);
// Create an empty map wrapper.
- Local<Object> result = templ->NewInstance();
+ Local<Object> result =
+ templ->NewInstance(GetIsolate()->GetCurrentContext()).ToLocalChecked();
// Wrap the raw C++ pointer in an External so it can be referenced
// from within JavaScript.
- Handle<External> map_ptr = External::New(GetIsolate(), obj);
+ Local<External> map_ptr = External::New(GetIsolate(), obj);
// Store the map pointer in the JavaScript wrapper.
result->SetInternalField(0, map_ptr);
@@ -338,8 +368,8 @@ Handle<Object> JsHttpRequestProcessor::WrapMap(map<string, string>* obj) {
// Utility function that extracts the C++ map pointer from a wrapper
// object.
-map<string, string>* JsHttpRequestProcessor::UnwrapMap(Handle<Object> obj) {
- Handle<External> field = Handle<External>::Cast(obj->GetInternalField(0));
+map<string, string>* JsHttpRequestProcessor::UnwrapMap(Local<Object> obj) {
+ Local<External> field = Local<External>::Cast(obj->GetInternalField(0));
void* ptr = field->Value();
return static_cast<map<string, string>*>(ptr);
}
@@ -371,9 +401,10 @@ void JsHttpRequestProcessor::MapGet(Local<Name> name,
// Otherwise fetch the value and wrap it in a JavaScript string
const string& value = (*iter).second;
- info.GetReturnValue().Set(String::NewFromUtf8(
- info.GetIsolate(), value.c_str(), String::kNormalString,
- static_cast<int>(value.length())));
+ info.GetReturnValue().Set(
+ String::NewFromUtf8(info.GetIsolate(), value.c_str(),
+ NewStringType::kNormal,
+ static_cast<int>(value.length())).ToLocalChecked());
}
@@ -396,7 +427,7 @@ void JsHttpRequestProcessor::MapSet(Local<Name> name, Local<Value> value_obj,
}
-Handle<ObjectTemplate> JsHttpRequestProcessor::MakeMapTemplate(
+Local<ObjectTemplate> JsHttpRequestProcessor::MakeMapTemplate(
Isolate* isolate) {
EscapableHandleScope handle_scope(isolate);
@@ -417,25 +448,26 @@ Handle<ObjectTemplate> JsHttpRequestProcessor::MakeMapTemplate(
* Utility function that wraps a C++ http request object in a
* JavaScript object.
*/
-Handle<Object> JsHttpRequestProcessor::WrapRequest(HttpRequest* request) {
- // Handle scope for temporary handles.
+Local<Object> JsHttpRequestProcessor::WrapRequest(HttpRequest* request) {
+ // Local scope for temporary handles.
EscapableHandleScope handle_scope(GetIsolate());
// Fetch the template for creating JavaScript http request wrappers.
// It only has to be created once, which we do on demand.
if (request_template_.IsEmpty()) {
- Handle<ObjectTemplate> raw_template = MakeRequestTemplate(GetIsolate());
+ Local<ObjectTemplate> raw_template = MakeRequestTemplate(GetIsolate());
request_template_.Reset(GetIsolate(), raw_template);
}
- Handle<ObjectTemplate> templ =
+ Local<ObjectTemplate> templ =
Local<ObjectTemplate>::New(GetIsolate(), request_template_);
// Create an empty http request wrapper.
- Local<Object> result = templ->NewInstance();
+ Local<Object> result =
+ templ->NewInstance(GetIsolate()->GetCurrentContext()).ToLocalChecked();
// Wrap the raw C++ pointer in an External so it can be referenced
// from within JavaScript.
- Handle<External> request_ptr = External::New(GetIsolate(), request);
+ Local<External> request_ptr = External::New(GetIsolate(), request);
// Store the request pointer in the JavaScript wrapper.
result->SetInternalField(0, request_ptr);
@@ -452,8 +484,8 @@ Handle<Object> JsHttpRequestProcessor::WrapRequest(HttpRequest* request) {
* Utility function that extracts the C++ http request object from a
* wrapper object.
*/
-HttpRequest* JsHttpRequestProcessor::UnwrapRequest(Handle<Object> obj) {
- Handle<External> field = Handle<External>::Cast(obj->GetInternalField(0));
+HttpRequest* JsHttpRequestProcessor::UnwrapRequest(Local<Object> obj) {
+ Local<External> field = Local<External>::Cast(obj->GetInternalField(0));
void* ptr = field->Value();
return static_cast<HttpRequest*>(ptr);
}
@@ -468,9 +500,10 @@ void JsHttpRequestProcessor::GetPath(Local<String> name,
const string& path = request->Path();
// Wrap the result in a JavaScript string and return it.
- info.GetReturnValue().Set(String::NewFromUtf8(
- info.GetIsolate(), path.c_str(), String::kNormalString,
- static_cast<int>(path.length())));
+ info.GetReturnValue().Set(
+ String::NewFromUtf8(info.GetIsolate(), path.c_str(),
+ NewStringType::kNormal,
+ static_cast<int>(path.length())).ToLocalChecked());
}
@@ -479,9 +512,10 @@ void JsHttpRequestProcessor::GetReferrer(
const PropertyCallbackInfo<Value>& info) {
HttpRequest* request = UnwrapRequest(info.Holder());
const string& path = request->Referrer();
- info.GetReturnValue().Set(String::NewFromUtf8(
- info.GetIsolate(), path.c_str(), String::kNormalString,
- static_cast<int>(path.length())));
+ info.GetReturnValue().Set(
+ String::NewFromUtf8(info.GetIsolate(), path.c_str(),
+ NewStringType::kNormal,
+ static_cast<int>(path.length())).ToLocalChecked());
}
@@ -489,9 +523,10 @@ void JsHttpRequestProcessor::GetHost(Local<String> name,
const PropertyCallbackInfo<Value>& info) {
HttpRequest* request = UnwrapRequest(info.Holder());
const string& path = request->Host();
- info.GetReturnValue().Set(String::NewFromUtf8(
- info.GetIsolate(), path.c_str(), String::kNormalString,
- static_cast<int>(path.length())));
+ info.GetReturnValue().Set(
+ String::NewFromUtf8(info.GetIsolate(), path.c_str(),
+ NewStringType::kNormal,
+ static_cast<int>(path.length())).ToLocalChecked());
}
@@ -500,13 +535,14 @@ void JsHttpRequestProcessor::GetUserAgent(
const PropertyCallbackInfo<Value>& info) {
HttpRequest* request = UnwrapRequest(info.Holder());
const string& path = request->UserAgent();
- info.GetReturnValue().Set(String::NewFromUtf8(
- info.GetIsolate(), path.c_str(), String::kNormalString,
- static_cast<int>(path.length())));
+ info.GetReturnValue().Set(
+ String::NewFromUtf8(info.GetIsolate(), path.c_str(),
+ NewStringType::kNormal,
+ static_cast<int>(path.length())).ToLocalChecked());
}
-Handle<ObjectTemplate> JsHttpRequestProcessor::MakeRequestTemplate(
+Local<ObjectTemplate> JsHttpRequestProcessor::MakeRequestTemplate(
Isolate* isolate) {
EscapableHandleScope handle_scope(isolate);
@@ -515,16 +551,20 @@ Handle<ObjectTemplate> JsHttpRequestProcessor::MakeRequestTemplate(
// Add accessors for each of the fields of the request.
result->SetAccessor(
- String::NewFromUtf8(isolate, "path", String::kInternalizedString),
+ String::NewFromUtf8(isolate, "path", NewStringType::kInternalized)
+ .ToLocalChecked(),
GetPath);
result->SetAccessor(
- String::NewFromUtf8(isolate, "referrer", String::kInternalizedString),
+ String::NewFromUtf8(isolate, "referrer", NewStringType::kInternalized)
+ .ToLocalChecked(),
GetReferrer);
result->SetAccessor(
- String::NewFromUtf8(isolate, "host", String::kInternalizedString),
+ String::NewFromUtf8(isolate, "host", NewStringType::kInternalized)
+ .ToLocalChecked(),
GetHost);
result->SetAccessor(
- String::NewFromUtf8(isolate, "userAgent", String::kInternalizedString),
+ String::NewFromUtf8(isolate, "userAgent", NewStringType::kInternalized)
+ .ToLocalChecked(),
GetUserAgent);
// Again, return the result through the current handle scope.
@@ -590,23 +630,26 @@ void ParseOptions(int argc,
// Reads a file into a v8 string.
-Handle<String> ReadFile(Isolate* isolate, const string& name) {
+MaybeLocal<String> ReadFile(Isolate* isolate, const string& name) {
FILE* file = fopen(name.c_str(), "rb");
- if (file == NULL) return Handle<String>();
+ if (file == NULL) return MaybeLocal<String>();
fseek(file, 0, SEEK_END);
- int size = ftell(file);
+ size_t size = ftell(file);
rewind(file);
char* chars = new char[size + 1];
chars[size] = '\0';
- for (int i = 0; i < size;) {
- int read = static_cast<int>(fread(&chars[i], 1, size - i, file));
- i += read;
+ for (size_t i = 0; i < size;) {
+ i += fread(&chars[i], 1, size - i, file);
+ if (ferror(file)) {
+ fclose(file);
+ return MaybeLocal<String>();
+ }
}
fclose(file);
- Handle<String> result =
- String::NewFromUtf8(isolate, chars, String::kNormalString, size);
+ MaybeLocal<String> result = String::NewFromUtf8(
+ isolate, chars, NewStringType::kNormal, static_cast<int>(size));
delete[] chars;
return result;
}
@@ -623,11 +666,13 @@ StringHttpRequest kSampleRequests[kSampleSize] = {
};
-bool ProcessEntries(HttpRequestProcessor* processor, int count,
- StringHttpRequest* reqs) {
+bool ProcessEntries(v8::Platform* platform, HttpRequestProcessor* processor,
+ int count, StringHttpRequest* reqs) {
for (int i = 0; i < count; i++) {
- if (!processor->Process(&reqs[i]))
- return false;
+ bool result = processor->Process(&reqs[i]);
+ while (v8::platform::PumpMessageLoop(platform, Isolate::GetCurrent()))
+ continue;
+ if (!result) return false;
}
return true;
}
@@ -643,6 +688,7 @@ void PrintMap(map<string, string>* m) {
int main(int argc, char* argv[]) {
v8::V8::InitializeICU();
+ v8::V8::InitializeExternalStartupData(argv[0]);
v8::Platform* platform = v8::platform::CreateDefaultPlatform();
v8::V8::InitializePlatform(platform);
v8::V8::Initialize();
@@ -653,11 +699,14 @@ int main(int argc, char* argv[]) {
fprintf(stderr, "No script was specified.\n");
return 1;
}
- Isolate* isolate = Isolate::New();
+ ArrayBufferAllocator array_buffer_allocator;
+ Isolate::CreateParams create_params;
+ create_params.array_buffer_allocator = &array_buffer_allocator;
+ Isolate* isolate = Isolate::New(create_params);
Isolate::Scope isolate_scope(isolate);
HandleScope scope(isolate);
- Handle<String> source = ReadFile(isolate, file);
- if (source.IsEmpty()) {
+ Local<String> source;
+ if (!ReadFile(isolate, file).ToLocal(&source)) {
fprintf(stderr, "Error reading '%s'.\n", file.c_str());
return 1;
}
@@ -667,7 +716,7 @@ int main(int argc, char* argv[]) {
fprintf(stderr, "Error initializing processor.\n");
return 1;
}
- if (!ProcessEntries(&processor, kSampleSize, kSampleRequests))
+ if (!ProcessEntries(platform, &processor, kSampleSize, kSampleRequests))
return 1;
PrintMap(&output);
}
diff --git a/samples/samples.gyp b/samples/samples.gyp
index 31e96f4c..7e0608b2 100644
--- a/samples/samples.gyp
+++ b/samples/samples.gyp
@@ -62,6 +62,12 @@
],
},
{
+ 'target_name': 'hello-world',
+ 'sources': [
+ 'hello-world.cc',
+ ],
+ },
+ {
'target_name': 'process',
'sources': [
'process.cc',
diff --git a/samples/shell.cc b/samples/shell.cc
index 1a08afff..b89ffdd1 100644
--- a/samples/shell.cc
+++ b/samples/shell.cc
@@ -44,20 +44,19 @@
*/
-v8::Handle<v8::Context> CreateShellContext(v8::Isolate* isolate);
-void RunShell(v8::Handle<v8::Context> context);
-int RunMain(v8::Isolate* isolate, int argc, char* argv[]);
-bool ExecuteString(v8::Isolate* isolate,
- v8::Handle<v8::String> source,
- v8::Handle<v8::Value> name,
- bool print_result,
+v8::Local<v8::Context> CreateShellContext(v8::Isolate* isolate);
+void RunShell(v8::Local<v8::Context> context, v8::Platform* platform);
+int RunMain(v8::Isolate* isolate, v8::Platform* platform, int argc,
+ char* argv[]);
+bool ExecuteString(v8::Isolate* isolate, v8::Local<v8::String> source,
+ v8::Local<v8::Value> name, bool print_result,
bool report_exceptions);
void Print(const v8::FunctionCallbackInfo<v8::Value>& args);
void Read(const v8::FunctionCallbackInfo<v8::Value>& args);
void Load(const v8::FunctionCallbackInfo<v8::Value>& args);
void Quit(const v8::FunctionCallbackInfo<v8::Value>& args);
void Version(const v8::FunctionCallbackInfo<v8::Value>& args);
-v8::Handle<v8::String> ReadFile(v8::Isolate* isolate, const char* name);
+v8::MaybeLocal<v8::String> ReadFile(v8::Isolate* isolate, const char* name);
void ReportException(v8::Isolate* isolate, v8::TryCatch* handler);
@@ -77,27 +76,30 @@ class ShellArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
int main(int argc, char* argv[]) {
v8::V8::InitializeICU();
+ v8::V8::InitializeExternalStartupData(argv[0]);
v8::Platform* platform = v8::platform::CreateDefaultPlatform();
v8::V8::InitializePlatform(platform);
v8::V8::Initialize();
v8::V8::SetFlagsFromCommandLine(&argc, argv, true);
ShellArrayBufferAllocator array_buffer_allocator;
- v8::V8::SetArrayBufferAllocator(&array_buffer_allocator);
- v8::Isolate* isolate = v8::Isolate::New();
+ v8::Isolate::CreateParams create_params;
+ create_params.array_buffer_allocator = &array_buffer_allocator;
+ v8::Isolate* isolate = v8::Isolate::New(create_params);
run_shell = (argc == 1);
int result;
{
v8::Isolate::Scope isolate_scope(isolate);
v8::HandleScope handle_scope(isolate);
- v8::Handle<v8::Context> context = CreateShellContext(isolate);
+ v8::Local<v8::Context> context = CreateShellContext(isolate);
if (context.IsEmpty()) {
fprintf(stderr, "Error creating context\n");
return 1;
}
v8::Context::Scope context_scope(context);
- result = RunMain(isolate, argc, argv);
- if (run_shell) RunShell(context);
+ result = RunMain(isolate, platform, argc, argv);
+ if (run_shell) RunShell(context, platform);
}
+ isolate->Dispose();
v8::V8::Dispose();
v8::V8::ShutdownPlatform();
delete platform;
@@ -113,24 +115,31 @@ const char* ToCString(const v8::String::Utf8Value& value) {
// Creates a new execution environment containing the built-in
// functions.
-v8::Handle<v8::Context> CreateShellContext(v8::Isolate* isolate) {
+v8::Local<v8::Context> CreateShellContext(v8::Isolate* isolate) {
// Create a template for the global object.
- v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(isolate);
+ v8::Local<v8::ObjectTemplate> global = v8::ObjectTemplate::New(isolate);
// Bind the global 'print' function to the C++ Print callback.
- global->Set(v8::String::NewFromUtf8(isolate, "print"),
- v8::FunctionTemplate::New(isolate, Print));
+ global->Set(
+ v8::String::NewFromUtf8(isolate, "print", v8::NewStringType::kNormal)
+ .ToLocalChecked(),
+ v8::FunctionTemplate::New(isolate, Print));
// Bind the global 'read' function to the C++ Read callback.
- global->Set(v8::String::NewFromUtf8(isolate, "read"),
+ global->Set(v8::String::NewFromUtf8(
+ isolate, "read", v8::NewStringType::kNormal).ToLocalChecked(),
v8::FunctionTemplate::New(isolate, Read));
// Bind the global 'load' function to the C++ Load callback.
- global->Set(v8::String::NewFromUtf8(isolate, "load"),
+ global->Set(v8::String::NewFromUtf8(
+ isolate, "load", v8::NewStringType::kNormal).ToLocalChecked(),
v8::FunctionTemplate::New(isolate, Load));
// Bind the 'quit' function
- global->Set(v8::String::NewFromUtf8(isolate, "quit"),
+ global->Set(v8::String::NewFromUtf8(
+ isolate, "quit", v8::NewStringType::kNormal).ToLocalChecked(),
v8::FunctionTemplate::New(isolate, Quit));
// Bind the 'version' function
- global->Set(v8::String::NewFromUtf8(isolate, "version"),
- v8::FunctionTemplate::New(isolate, Version));
+ global->Set(
+ v8::String::NewFromUtf8(isolate, "version", v8::NewStringType::kNormal)
+ .ToLocalChecked(),
+ v8::FunctionTemplate::New(isolate, Version));
return v8::Context::New(isolate, NULL, global);
}
@@ -163,19 +172,22 @@ void Print(const v8::FunctionCallbackInfo<v8::Value>& args) {
void Read(const v8::FunctionCallbackInfo<v8::Value>& args) {
if (args.Length() != 1) {
args.GetIsolate()->ThrowException(
- v8::String::NewFromUtf8(args.GetIsolate(), "Bad parameters"));
+ v8::String::NewFromUtf8(args.GetIsolate(), "Bad parameters",
+ v8::NewStringType::kNormal).ToLocalChecked());
return;
}
v8::String::Utf8Value file(args[0]);
if (*file == NULL) {
args.GetIsolate()->ThrowException(
- v8::String::NewFromUtf8(args.GetIsolate(), "Error loading file"));
+ v8::String::NewFromUtf8(args.GetIsolate(), "Error loading file",
+ v8::NewStringType::kNormal).ToLocalChecked());
return;
}
- v8::Handle<v8::String> source = ReadFile(args.GetIsolate(), *file);
- if (source.IsEmpty()) {
+ v8::Local<v8::String> source;
+ if (!ReadFile(args.GetIsolate(), *file).ToLocal(&source)) {
args.GetIsolate()->ThrowException(
- v8::String::NewFromUtf8(args.GetIsolate(), "Error loading file"));
+ v8::String::NewFromUtf8(args.GetIsolate(), "Error loading file",
+ v8::NewStringType::kNormal).ToLocalChecked());
return;
}
args.GetReturnValue().Set(source);
@@ -191,22 +203,21 @@ void Load(const v8::FunctionCallbackInfo<v8::Value>& args) {
v8::String::Utf8Value file(args[i]);
if (*file == NULL) {
args.GetIsolate()->ThrowException(
- v8::String::NewFromUtf8(args.GetIsolate(), "Error loading file"));
+ v8::String::NewFromUtf8(args.GetIsolate(), "Error loading file",
+ v8::NewStringType::kNormal).ToLocalChecked());
return;
}
- v8::Handle<v8::String> source = ReadFile(args.GetIsolate(), *file);
- if (source.IsEmpty()) {
+ v8::Local<v8::String> source;
+ if (!ReadFile(args.GetIsolate(), *file).ToLocal(&source)) {
args.GetIsolate()->ThrowException(
- v8::String::NewFromUtf8(args.GetIsolate(), "Error loading file"));
+ v8::String::NewFromUtf8(args.GetIsolate(), "Error loading file",
+ v8::NewStringType::kNormal).ToLocalChecked());
return;
}
- if (!ExecuteString(args.GetIsolate(),
- source,
- v8::String::NewFromUtf8(args.GetIsolate(), *file),
- false,
- false)) {
+ if (!ExecuteString(args.GetIsolate(), source, args[i], false, false)) {
args.GetIsolate()->ThrowException(
- v8::String::NewFromUtf8(args.GetIsolate(), "Error executing file"));
+ v8::String::NewFromUtf8(args.GetIsolate(), "Error executing file",
+ v8::NewStringType::kNormal).ToLocalChecked());
return;
}
}
@@ -218,7 +229,8 @@ void Load(const v8::FunctionCallbackInfo<v8::Value>& args) {
void Quit(const v8::FunctionCallbackInfo<v8::Value>& args) {
// If not arguments are given args[0] will yield undefined which
// converts to the integer value 0.
- int exit_code = args[0]->Int32Value();
+ int exit_code =
+ args[0]->Int32Value(args.GetIsolate()->GetCurrentContext()).FromMaybe(0);
fflush(stdout);
fflush(stderr);
exit(exit_code);
@@ -227,35 +239,40 @@ void Quit(const v8::FunctionCallbackInfo<v8::Value>& args) {
void Version(const v8::FunctionCallbackInfo<v8::Value>& args) {
args.GetReturnValue().Set(
- v8::String::NewFromUtf8(args.GetIsolate(), v8::V8::GetVersion()));
+ v8::String::NewFromUtf8(args.GetIsolate(), v8::V8::GetVersion(),
+ v8::NewStringType::kNormal).ToLocalChecked());
}
// Reads a file into a v8 string.
-v8::Handle<v8::String> ReadFile(v8::Isolate* isolate, const char* name) {
+v8::MaybeLocal<v8::String> ReadFile(v8::Isolate* isolate, const char* name) {
FILE* file = fopen(name, "rb");
- if (file == NULL) return v8::Handle<v8::String>();
+ if (file == NULL) return v8::MaybeLocal<v8::String>();
fseek(file, 0, SEEK_END);
- int size = ftell(file);
+ size_t size = ftell(file);
rewind(file);
char* chars = new char[size + 1];
chars[size] = '\0';
- for (int i = 0; i < size;) {
- int read = static_cast<int>(fread(&chars[i], 1, size - i, file));
- i += read;
+ for (size_t i = 0; i < size;) {
+ i += fread(&chars[i], 1, size - i, file);
+ if (ferror(file)) {
+ fclose(file);
+ return v8::MaybeLocal<v8::String>();
+ }
}
fclose(file);
- v8::Handle<v8::String> result =
- v8::String::NewFromUtf8(isolate, chars, v8::String::kNormalString, size);
+ v8::MaybeLocal<v8::String> result = v8::String::NewFromUtf8(
+ isolate, chars, v8::NewStringType::kNormal, static_cast<int>(size));
delete[] chars;
return result;
}
// Process remaining command line arguments and execute files
-int RunMain(v8::Isolate* isolate, int argc, char* argv[]) {
+int RunMain(v8::Isolate* isolate, v8::Platform* platform, int argc,
+ char* argv[]) {
for (int i = 1; i < argc; i++) {
const char* str = argv[i];
if (strcmp(str, "--shell") == 0) {
@@ -269,20 +286,31 @@ int RunMain(v8::Isolate* isolate, int argc, char* argv[]) {
"Warning: unknown flag %s.\nTry --help for options\n", str);
} else if (strcmp(str, "-e") == 0 && i + 1 < argc) {
// Execute argument given to -e option directly.
- v8::Handle<v8::String> file_name =
- v8::String::NewFromUtf8(isolate, "unnamed");
- v8::Handle<v8::String> source =
- v8::String::NewFromUtf8(isolate, argv[++i]);
- if (!ExecuteString(isolate, source, file_name, false, true)) return 1;
+ v8::Local<v8::String> file_name =
+ v8::String::NewFromUtf8(isolate, "unnamed",
+ v8::NewStringType::kNormal).ToLocalChecked();
+ v8::Local<v8::String> source;
+ if (!v8::String::NewFromUtf8(isolate, argv[++i],
+ v8::NewStringType::kNormal)
+ .ToLocal(&source)) {
+ return 1;
+ }
+ bool success = ExecuteString(isolate, source, file_name, false, true);
+ while (v8::platform::PumpMessageLoop(platform, isolate)) continue;
+ if (!success) return 1;
} else {
// Use all other arguments as names of files to load and run.
- v8::Handle<v8::String> file_name = v8::String::NewFromUtf8(isolate, str);
- v8::Handle<v8::String> source = ReadFile(isolate, str);
- if (source.IsEmpty()) {
+ v8::Local<v8::String> file_name =
+ v8::String::NewFromUtf8(isolate, str, v8::NewStringType::kNormal)
+ .ToLocalChecked();
+ v8::Local<v8::String> source;
+ if (!ReadFile(isolate, str).ToLocal(&source)) {
fprintf(stderr, "Error reading '%s'\n", str);
continue;
}
- if (!ExecuteString(isolate, source, file_name, false, true)) return 1;
+ bool success = ExecuteString(isolate, source, file_name, false, true);
+ while (v8::platform::PumpMessageLoop(platform, isolate)) continue;
+ if (!success) return 1;
}
}
return 0;
@@ -290,47 +318,49 @@ int RunMain(v8::Isolate* isolate, int argc, char* argv[]) {
// The read-eval-execute loop of the shell.
-void RunShell(v8::Handle<v8::Context> context) {
+void RunShell(v8::Local<v8::Context> context, v8::Platform* platform) {
fprintf(stderr, "V8 version %s [sample shell]\n", v8::V8::GetVersion());
static const int kBufferSize = 256;
// Enter the execution environment before evaluating any code.
v8::Context::Scope context_scope(context);
v8::Local<v8::String> name(
- v8::String::NewFromUtf8(context->GetIsolate(), "(shell)"));
+ v8::String::NewFromUtf8(context->GetIsolate(), "(shell)",
+ v8::NewStringType::kNormal).ToLocalChecked());
while (true) {
char buffer[kBufferSize];
fprintf(stderr, "> ");
char* str = fgets(buffer, kBufferSize, stdin);
if (str == NULL) break;
v8::HandleScope handle_scope(context->GetIsolate());
- ExecuteString(context->GetIsolate(),
- v8::String::NewFromUtf8(context->GetIsolate(), str),
- name,
- true,
- true);
+ ExecuteString(
+ context->GetIsolate(),
+ v8::String::NewFromUtf8(context->GetIsolate(), str,
+ v8::NewStringType::kNormal).ToLocalChecked(),
+ name, true, true);
+ while (v8::platform::PumpMessageLoop(platform, context->GetIsolate()))
+ continue;
}
fprintf(stderr, "\n");
}
// Executes a string within the current v8 context.
-bool ExecuteString(v8::Isolate* isolate,
- v8::Handle<v8::String> source,
- v8::Handle<v8::Value> name,
- bool print_result,
+bool ExecuteString(v8::Isolate* isolate, v8::Local<v8::String> source,
+ v8::Local<v8::Value> name, bool print_result,
bool report_exceptions) {
v8::HandleScope handle_scope(isolate);
- v8::TryCatch try_catch;
+ v8::TryCatch try_catch(isolate);
v8::ScriptOrigin origin(name);
- v8::Handle<v8::Script> script = v8::Script::Compile(source, &origin);
- if (script.IsEmpty()) {
+ v8::Local<v8::Context> context(isolate->GetCurrentContext());
+ v8::Local<v8::Script> script;
+ if (!v8::Script::Compile(context, source, &origin).ToLocal(&script)) {
// Print errors that happened during compilation.
if (report_exceptions)
ReportException(isolate, &try_catch);
return false;
} else {
- v8::Handle<v8::Value> result = script->Run();
- if (result.IsEmpty()) {
+ v8::Local<v8::Value> result;
+ if (!script->Run(context).ToLocal(&result)) {
assert(try_catch.HasCaught());
// Print errors that happened during execution.
if (report_exceptions)
@@ -355,7 +385,7 @@ void ReportException(v8::Isolate* isolate, v8::TryCatch* try_catch) {
v8::HandleScope handle_scope(isolate);
v8::String::Utf8Value exception(try_catch->Exception());
const char* exception_string = ToCString(exception);
- v8::Handle<v8::Message> message = try_catch->Message();
+ v8::Local<v8::Message> message = try_catch->Message();
if (message.IsEmpty()) {
// V8 didn't provide any extra information about this error; just
// print the exception.
@@ -363,25 +393,30 @@ void ReportException(v8::Isolate* isolate, v8::TryCatch* try_catch) {
} else {
// Print (filename):(line number): (message).
v8::String::Utf8Value filename(message->GetScriptOrigin().ResourceName());
+ v8::Local<v8::Context> context(isolate->GetCurrentContext());
const char* filename_string = ToCString(filename);
- int linenum = message->GetLineNumber();
+ int linenum = message->GetLineNumber(context).FromJust();
fprintf(stderr, "%s:%i: %s\n", filename_string, linenum, exception_string);
// Print line of source code.
- v8::String::Utf8Value sourceline(message->GetSourceLine());
+ v8::String::Utf8Value sourceline(
+ message->GetSourceLine(context).ToLocalChecked());
const char* sourceline_string = ToCString(sourceline);
fprintf(stderr, "%s\n", sourceline_string);
// Print wavy underline (GetUnderline is deprecated).
- int start = message->GetStartColumn();
+ int start = message->GetStartColumn(context).FromJust();
for (int i = 0; i < start; i++) {
fprintf(stderr, " ");
}
- int end = message->GetEndColumn();
+ int end = message->GetEndColumn(context).FromJust();
for (int i = start; i < end; i++) {
fprintf(stderr, "^");
}
fprintf(stderr, "\n");
- v8::String::Utf8Value stack_trace(try_catch->StackTrace());
- if (stack_trace.length() > 0) {
+ v8::Local<v8::Value> stack_trace_string;
+ if (try_catch->StackTrace(context).ToLocal(&stack_trace_string) &&
+ stack_trace_string->IsString() &&
+ v8::Local<v8::String>::Cast(stack_trace_string)->Length() > 0) {
+ v8::String::Utf8Value stack_trace(stack_trace_string);
const char* stack_trace_string = ToCString(stack_trace);
fprintf(stderr, "%s\n", stack_trace_string);
}