diff options
Diffstat (limited to 'deps/v8/samples')
-rw-r--r-- | deps/v8/samples/lineprocessor.cc | 41 | ||||
-rw-r--r-- | deps/v8/samples/process.cc | 63 | ||||
-rw-r--r-- | deps/v8/samples/shell.cc | 78 |
3 files changed, 104 insertions, 78 deletions
diff --git a/deps/v8/samples/lineprocessor.cc b/deps/v8/samples/lineprocessor.cc index 26e787f2b7..b5b63675e5 100644 --- a/deps/v8/samples/lineprocessor.cc +++ b/deps/v8/samples/lineprocessor.cc @@ -98,13 +98,14 @@ enum MainCycleType { }; const char* ToCString(const v8::String::Utf8Value& value); -void ReportException(v8::TryCatch* handler); +void ReportException(v8::Isolate* isolate, v8::TryCatch* handler); v8::Handle<v8::String> ReadFile(const char* name); v8::Handle<v8::String> ReadLine(); v8::Handle<v8::Value> Print(const v8::Arguments& args); v8::Handle<v8::Value> ReadLine(const v8::Arguments& args); -bool RunCppCycle(v8::Handle<v8::Script> script, v8::Local<v8::Context> context, +bool RunCppCycle(v8::Handle<v8::Script> script, + v8::Local<v8::Context> context, bool report_exceptions); @@ -132,7 +133,8 @@ void DispatchDebugMessages() { int RunMain(int argc, char* argv[]) { v8::V8::SetFlagsFromCommandLine(&argc, argv, true); - v8::HandleScope handle_scope; + v8::Isolate* isolate = v8::Isolate::GetCurrent(); + v8::HandleScope handle_scope(isolate); v8::Handle<v8::String> script_source(NULL); v8::Handle<v8::Value> script_name(NULL); @@ -212,9 +214,10 @@ int RunMain(int argc, char* argv[]) { v8::Context::Scope context_scope(context); #ifdef ENABLE_DEBUGGER_SUPPORT - debug_message_context = v8::Persistent<v8::Context>::New(context); + debug_message_context = + v8::Persistent<v8::Context>::New(isolate, context); - v8::Locker locker; + v8::Locker locker(isolate); if (support_callback) { v8::Debug::SetDebugMessageDispatchHandler(DispatchDebugMessages, true); @@ -235,7 +238,7 @@ int RunMain(int argc, char* argv[]) { if (script.IsEmpty()) { // Print errors that happened during compilation. if (report_exceptions) - ReportException(&try_catch); + ReportException(isolate, &try_catch); return 1; } } @@ -246,13 +249,14 @@ int RunMain(int argc, char* argv[]) { script->Run(); if (try_catch.HasCaught()) { if (report_exceptions) - ReportException(&try_catch); + ReportException(isolate, &try_catch); return 1; } } if (cycle_type == CycleInCpp) { - bool res = RunCppCycle(script, v8::Context::GetCurrent(), + bool res = RunCppCycle(script, + v8::Context::GetCurrent(), report_exceptions); return !res; } else { @@ -262,15 +266,16 @@ int RunMain(int argc, char* argv[]) { } -bool RunCppCycle(v8::Handle<v8::Script> script, v8::Local<v8::Context> context, +bool RunCppCycle(v8::Handle<v8::Script> script, + v8::Local<v8::Context> context, bool report_exceptions) { + v8::Isolate* isolate = context->GetIsolate(); #ifdef ENABLE_DEBUGGER_SUPPORT - v8::Locker lock; + v8::Locker lock(isolate); #endif // ENABLE_DEBUGGER_SUPPORT v8::Handle<v8::String> fun_name = v8::String::New("ProcessLine"); - v8::Handle<v8::Value> process_val = - v8::Context::GetCurrent()->Global()->Get(fun_name); + 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 @@ -285,7 +290,7 @@ bool RunCppCycle(v8::Handle<v8::Script> script, v8::Local<v8::Context> context, while (!feof(stdin)) { - v8::HandleScope handle_scope; + v8::HandleScope handle_scope(isolate); v8::Handle<v8::String> input_line = ReadLine(); if (input_line == v8::Undefined()) { @@ -302,7 +307,7 @@ bool RunCppCycle(v8::Handle<v8::Script> script, v8::Local<v8::Context> context, argc, argv); if (try_catch.HasCaught()) { if (report_exceptions) - ReportException(&try_catch); + ReportException(isolate, &try_catch); return false; } } @@ -349,8 +354,8 @@ v8::Handle<v8::String> ReadFile(const char* name) { } -void ReportException(v8::TryCatch* try_catch) { - v8::HandleScope handle_scope; +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(); @@ -388,7 +393,7 @@ void ReportException(v8::TryCatch* try_catch) { v8::Handle<v8::Value> Print(const v8::Arguments& args) { bool first = true; for (int i = 0; i < args.Length(); i++) { - v8::HandleScope handle_scope; + v8::HandleScope handle_scope(args.GetIsolate()); if (first) { first = false; } else { @@ -420,7 +425,7 @@ v8::Handle<v8::String> ReadLine() { char* res; { #ifdef ENABLE_DEBUGGER_SUPPORT - v8::Unlocker unlocker; + v8::Unlocker unlocker(v8::Isolate::GetCurrent()); #endif // ENABLE_DEBUGGER_SUPPORT res = fgets(buffer, kBufferSize, stdin); } diff --git a/deps/v8/samples/process.cc b/deps/v8/samples/process.cc index ae6a5500cd..4dcc09a56b 100644 --- a/deps/v8/samples/process.cc +++ b/deps/v8/samples/process.cc @@ -79,7 +79,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. - explicit JsHttpRequestProcessor(Handle<String> script) : script_(script) { } + JsHttpRequestProcessor(Isolate* isolate, Handle<String> script) + : isolate_(isolate), script_(script) { } virtual ~JsHttpRequestProcessor(); virtual bool Initialize(map<string, string>* opts, @@ -97,8 +98,8 @@ class JsHttpRequestProcessor : public HttpRequestProcessor { // Constructs the template that describes the JavaScript wrapper // type for requests. - static Handle<ObjectTemplate> MakeRequestTemplate(); - static Handle<ObjectTemplate> MakeMapTemplate(); + static Handle<ObjectTemplate> MakeRequestTemplate(Isolate* isolate); + static Handle<ObjectTemplate> MakeMapTemplate(Isolate* isolate); // Callbacks that access the individual fields of request objects. static Handle<Value> GetPath(Local<String> name, const AccessorInfo& info); @@ -116,11 +117,14 @@ class JsHttpRequestProcessor : public HttpRequestProcessor { // Utility methods for wrapping C++ objects as JavaScript objects, // and going back again. - static Handle<Object> WrapMap(map<string, string>* obj); + Handle<Object> WrapMap(map<string, string>* obj); static map<string, string>* UnwrapMap(Handle<Object> obj); - static Handle<Object> WrapRequest(HttpRequest* obj); + Handle<Object> WrapRequest(HttpRequest* obj); static HttpRequest* UnwrapRequest(Handle<Object> obj); + Isolate* GetIsolate() { return isolate_; } + + Isolate* isolate_; Handle<String> script_; Persistent<Context> context_; Persistent<Function> process_; @@ -134,12 +138,12 @@ class JsHttpRequestProcessor : public HttpRequestProcessor { static Handle<Value> LogCallback(const Arguments& args) { - if (args.Length() < 1) return v8::Undefined(); - HandleScope scope; + if (args.Length() < 1) return Undefined(); + HandleScope scope(args.GetIsolate()); Handle<Value> arg = args[0]; String::Utf8Value value(arg); HttpRequestProcessor::Log(*value); - return v8::Undefined(); + return Undefined(); } @@ -147,7 +151,7 @@ static Handle<Value> LogCallback(const Arguments& args) { bool JsHttpRequestProcessor::Initialize(map<string, string>* opts, map<string, string>* output) { // Create a handle scope to hold the temporary references. - HandleScope handle_scope; + HandleScope handle_scope(GetIsolate()); // Create a template for the global object where we set the // built-in global functions. @@ -187,7 +191,7 @@ bool JsHttpRequestProcessor::Initialize(map<string, string>* opts, // Store the function in a Persistent handle, since we also want // that to remain after this call returns - process_ = Persistent<Function>::New(process_fun); + process_ = Persistent<Function>::New(GetIsolate(), process_fun); // All done; all went well return true; @@ -195,7 +199,7 @@ bool JsHttpRequestProcessor::Initialize(map<string, string>* opts, bool JsHttpRequestProcessor::ExecuteScript(Handle<String> script) { - HandleScope handle_scope; + 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. @@ -225,7 +229,7 @@ bool JsHttpRequestProcessor::ExecuteScript(Handle<String> script) { bool JsHttpRequestProcessor::InstallMaps(map<string, string>* opts, map<string, string>* output) { - HandleScope handle_scope; + HandleScope handle_scope(GetIsolate()); // Wrap the map object in a JavaScript wrapper Handle<Object> opts_obj = WrapMap(opts); @@ -242,7 +246,7 @@ bool JsHttpRequestProcessor::InstallMaps(map<string, string>* opts, bool JsHttpRequestProcessor::Process(HttpRequest* request) { // Create a handle scope to keep the temporary object references. - HandleScope handle_scope; + HandleScope handle_scope(GetIsolate()); // Enter this processor's context so all the remaining operations // take place there @@ -273,8 +277,9 @@ JsHttpRequestProcessor::~JsHttpRequestProcessor() { // Dispose the persistent handles. When noone else has any // references to the objects stored in the handles they will be // automatically reclaimed. - context_.Dispose(); - process_.Dispose(); + Isolate* isolate = GetIsolate(); + context_.Dispose(isolate); + process_.Dispose(isolate); } @@ -290,13 +295,13 @@ Persistent<ObjectTemplate> JsHttpRequestProcessor::map_template_; // JavaScript object. Handle<Object> JsHttpRequestProcessor::WrapMap(map<string, string>* obj) { // Handle scope for temporary handles. - HandleScope handle_scope; + HandleScope 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(); - map_template_ = Persistent<ObjectTemplate>::New(raw_template); + Handle<ObjectTemplate> raw_template = MakeMapTemplate(GetIsolate()); + map_template_ = Persistent<ObjectTemplate>::New(GetIsolate(), raw_template); } Handle<ObjectTemplate> templ = map_template_; @@ -373,8 +378,9 @@ Handle<Value> JsHttpRequestProcessor::MapSet(Local<String> name, } -Handle<ObjectTemplate> JsHttpRequestProcessor::MakeMapTemplate() { - HandleScope handle_scope; +Handle<ObjectTemplate> JsHttpRequestProcessor::MakeMapTemplate( + Isolate* isolate) { + HandleScope handle_scope(isolate); Handle<ObjectTemplate> result = ObjectTemplate::New(); result->SetInternalFieldCount(1); @@ -395,13 +401,14 @@ Handle<ObjectTemplate> JsHttpRequestProcessor::MakeMapTemplate() { */ Handle<Object> JsHttpRequestProcessor::WrapRequest(HttpRequest* request) { // Handle scope for temporary handles. - HandleScope handle_scope; + HandleScope 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(); - request_template_ = Persistent<ObjectTemplate>::New(raw_template); + Handle<ObjectTemplate> raw_template = MakeRequestTemplate(GetIsolate()); + request_template_ = + Persistent<ObjectTemplate>::New(GetIsolate(), raw_template); } Handle<ObjectTemplate> templ = request_template_; @@ -471,8 +478,9 @@ Handle<Value> JsHttpRequestProcessor::GetUserAgent(Local<String> name, } -Handle<ObjectTemplate> JsHttpRequestProcessor::MakeRequestTemplate() { - HandleScope handle_scope; +Handle<ObjectTemplate> JsHttpRequestProcessor::MakeRequestTemplate( + Isolate* isolate) { + HandleScope handle_scope(isolate); Handle<ObjectTemplate> result = ObjectTemplate::New(); result->SetInternalFieldCount(1); @@ -604,13 +612,14 @@ int main(int argc, char* argv[]) { fprintf(stderr, "No script was specified.\n"); return 1; } - HandleScope scope; + Isolate* isolate = Isolate::GetCurrent(); + HandleScope scope(isolate); Handle<String> source = ReadFile(file); if (source.IsEmpty()) { fprintf(stderr, "Error reading '%s'.\n", file.c_str()); return 1; } - JsHttpRequestProcessor processor(source); + JsHttpRequestProcessor processor(isolate, source); map<string, string> output; if (!processor.Initialize(&options, &output)) { fprintf(stderr, "Error initializing processor.\n"); diff --git a/deps/v8/samples/shell.cc b/deps/v8/samples/shell.cc index 821ef75a76..0b71c2c6dc 100644 --- a/deps/v8/samples/shell.cc +++ b/deps/v8/samples/shell.cc @@ -47,8 +47,9 @@ v8::Persistent<v8::Context> CreateShellContext(); void RunShell(v8::Handle<v8::Context> context); -int RunMain(int argc, char* argv[]); -bool ExecuteString(v8::Handle<v8::String> source, +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, bool report_exceptions); @@ -58,7 +59,7 @@ v8::Handle<v8::Value> Load(const v8::Arguments& args); v8::Handle<v8::Value> Quit(const v8::Arguments& args); v8::Handle<v8::Value> Version(const v8::Arguments& args); v8::Handle<v8::String> ReadFile(const char* name); -void ReportException(v8::TryCatch* handler); +void ReportException(v8::Isolate* isolate, v8::TryCatch* handler); static bool run_shell; @@ -66,20 +67,21 @@ static bool run_shell; int main(int argc, char* argv[]) { v8::V8::SetFlagsFromCommandLine(&argc, argv, true); + v8::Isolate* isolate = v8::Isolate::GetCurrent(); run_shell = (argc == 1); int result; { - v8::HandleScope handle_scope; + v8::HandleScope handle_scope(isolate); v8::Persistent<v8::Context> context = CreateShellContext(); if (context.IsEmpty()) { - printf("Error creating context\n"); + fprintf(stderr, "Error creating context\n"); return 1; } context->Enter(); - result = RunMain(argc, argv); + result = RunMain(isolate, argc, argv); if (run_shell) RunShell(context); context->Exit(); - context.Dispose(); + context.Dispose(isolate); } v8::V8::Dispose(); return result; @@ -118,7 +120,7 @@ v8::Persistent<v8::Context> CreateShellContext() { v8::Handle<v8::Value> Print(const v8::Arguments& args) { bool first = true; for (int i = 0; i < args.Length(); i++) { - v8::HandleScope handle_scope; + v8::HandleScope handle_scope(args.GetIsolate()); if (first) { first = false; } else { @@ -158,7 +160,7 @@ v8::Handle<v8::Value> Read(const v8::Arguments& args) { // JavaScript file. v8::Handle<v8::Value> Load(const v8::Arguments& args) { for (int i = 0; i < args.Length(); i++) { - v8::HandleScope handle_scope; + v8::HandleScope handle_scope(args.GetIsolate()); v8::String::Utf8Value file(args[i]); if (*file == NULL) { return v8::ThrowException(v8::String::New("Error loading file")); @@ -167,7 +169,11 @@ v8::Handle<v8::Value> Load(const v8::Arguments& args) { if (source.IsEmpty()) { return v8::ThrowException(v8::String::New("Error loading file")); } - if (!ExecuteString(source, v8::String::New(*file), false, false)) { + if (!ExecuteString(args.GetIsolate(), + source, + v8::String::New(*file), + false, + false)) { return v8::ThrowException(v8::String::New("Error executing file")); } } @@ -216,7 +222,7 @@ v8::Handle<v8::String> ReadFile(const char* name) { // Process remaining command line arguments and execute files -int RunMain(int argc, char* argv[]) { +int RunMain(v8::Isolate* isolate, int argc, char* argv[]) { for (int i = 1; i < argc; i++) { const char* str = argv[i]; if (strcmp(str, "--shell") == 0) { @@ -226,21 +232,22 @@ int RunMain(int argc, char* argv[]) { // alone JavaScript engines. continue; } else if (strncmp(str, "--", 2) == 0) { - printf("Warning: unknown flag %s.\nTry --help for options\n", str); + fprintf(stderr, + "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::New("unnamed"); v8::Handle<v8::String> source = v8::String::New(argv[++i]); - if (!ExecuteString(source, file_name, false, true)) return 1; + if (!ExecuteString(isolate, source, file_name, false, true)) return 1; } else { // Use all other arguments as names of files to load and run. v8::Handle<v8::String> file_name = v8::String::New(str); v8::Handle<v8::String> source = ReadFile(str); if (source.IsEmpty()) { - printf("Error reading '%s'\n", str); + fprintf(stderr, "Error reading '%s'\n", str); continue; } - if (!ExecuteString(source, file_name, false, true)) return 1; + if (!ExecuteString(isolate, source, file_name, false, true)) return 1; } } return 0; @@ -249,35 +256,40 @@ int RunMain(int argc, char* argv[]) { // The read-eval-execute loop of the shell. void RunShell(v8::Handle<v8::Context> context) { - printf("V8 version %s [sample shell]\n", v8::V8::GetVersion()); + 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::New("(shell)")); while (true) { char buffer[kBufferSize]; - printf("> "); + fprintf(stderr, "> "); char* str = fgets(buffer, kBufferSize, stdin); if (str == NULL) break; - v8::HandleScope handle_scope; - ExecuteString(v8::String::New(str), name, true, true); + v8::HandleScope handle_scope(context->GetIsolate()); + ExecuteString(context->GetIsolate(), + v8::String::New(str), + name, + true, + true); } - printf("\n"); + fprintf(stderr, "\n"); } // Executes a string within the current v8 context. -bool ExecuteString(v8::Handle<v8::String> source, +bool ExecuteString(v8::Isolate* isolate, + v8::Handle<v8::String> source, v8::Handle<v8::Value> name, bool print_result, bool report_exceptions) { - v8::HandleScope handle_scope; + v8::HandleScope handle_scope(isolate); v8::TryCatch try_catch; v8::Handle<v8::Script> script = v8::Script::Compile(source, name); if (script.IsEmpty()) { // Print errors that happened during compilation. if (report_exceptions) - ReportException(&try_catch); + ReportException(isolate, &try_catch); return false; } else { v8::Handle<v8::Value> result = script->Run(); @@ -285,7 +297,7 @@ bool ExecuteString(v8::Handle<v8::String> source, assert(try_catch.HasCaught()); // Print errors that happened during execution. if (report_exceptions) - ReportException(&try_catch); + ReportException(isolate, &try_catch); return false; } else { assert(!try_catch.HasCaught()); @@ -302,39 +314,39 @@ bool ExecuteString(v8::Handle<v8::String> source, } -void ReportException(v8::TryCatch* try_catch) { - v8::HandleScope handle_scope; +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); + fprintf(stderr, "%s\n", exception_string); } else { // Print (filename):(line number): (message). v8::String::Utf8Value filename(message->GetScriptResourceName()); const char* filename_string = ToCString(filename); int linenum = message->GetLineNumber(); - printf("%s:%i: %s\n", filename_string, linenum, exception_string); + fprintf(stderr, "%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); + fprintf(stderr, "%s\n", sourceline_string); // Print wavy underline (GetUnderline is deprecated). int start = message->GetStartColumn(); for (int i = 0; i < start; i++) { - printf(" "); + fprintf(stderr, " "); } int end = message->GetEndColumn(); for (int i = start; i < end; i++) { - printf("^"); + fprintf(stderr, "^"); } - printf("\n"); + fprintf(stderr, "\n"); v8::String::Utf8Value stack_trace(try_catch->StackTrace()); if (stack_trace.length() > 0) { const char* stack_trace_string = ToCString(stack_trace); - printf("%s\n", stack_trace_string); + fprintf(stderr, "%s\n", stack_trace_string); } } } |