Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| 11 // with the distribution. | 11 // with the distribution. |
| 12 // * Neither the name of Google Inc. nor the names of its | 12 // * Neither the name of Google Inc. nor the names of its |
| 13 // contributors may be used to endorse or promote products derived | 13 // contributors may be used to endorse or promote products derived |
| 14 // from this software without specific prior written permission. | 14 // from this software without specific prior written permission. |
| 15 // | 15 // |
| 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | 27 |
| 28 // TODO(dcarney): remove this | |
| 29 #define V8_ALLOW_ACCESS_TO_RAW_HANDLE_CONSTRUCTOR | |
| 30 #define V8_ALLOW_ACCESS_TO_PERSISTENT_IMPLICIT | |
| 31 #define V8_ALLOW_ACCESS_TO_PERSISTENT_ARROW | |
| 32 | |
| 28 #include <v8.h> | 33 #include <v8.h> |
| 29 #include <assert.h> | 34 #include <assert.h> |
| 30 #include <fcntl.h> | 35 #include <fcntl.h> |
| 31 #include <string.h> | 36 #include <string.h> |
| 32 #include <stdio.h> | 37 #include <stdio.h> |
| 33 #include <stdlib.h> | 38 #include <stdlib.h> |
| 34 | 39 |
| 35 #ifdef COMPRESS_STARTUP_DATA_BZ2 | 40 #ifdef COMPRESS_STARTUP_DATA_BZ2 |
| 36 #error Using compressed startup data is not supported for this sample | 41 #error Using compressed startup data is not supported for this sample |
| 37 #endif | 42 #endif |
| 38 | 43 |
| 39 /** | 44 /** |
| 40 * This sample program shows how to implement a simple javascript shell | 45 * This sample program shows how to implement a simple javascript shell |
| 41 * based on V8. This includes initializing V8 with command line options, | 46 * based on V8. This includes initializing V8 with command line options, |
| 42 * creating global functions, compiling and executing strings. | 47 * creating global functions, compiling and executing strings. |
| 43 * | 48 * |
| 44 * For a more sophisticated shell, consider using the debug shell D8. | 49 * For a more sophisticated shell, consider using the debug shell D8. |
| 45 */ | 50 */ |
| 46 | 51 |
| 47 | 52 |
| 48 v8::Persistent<v8::Context> CreateShellContext(); | 53 v8::Handle<v8::Context> CreateShellContext(); |
| 49 void RunShell(v8::Handle<v8::Context> context); | 54 void RunShell(v8::Handle<v8::Context> context); |
| 50 int RunMain(v8::Isolate* isolate, int argc, char* argv[]); | 55 int RunMain(v8::Isolate* isolate, int argc, char* argv[]); |
| 51 bool ExecuteString(v8::Isolate* isolate, | 56 bool ExecuteString(v8::Isolate* isolate, |
| 52 v8::Handle<v8::String> source, | 57 v8::Handle<v8::String> source, |
| 53 v8::Handle<v8::Value> name, | 58 v8::Handle<v8::Value> name, |
| 54 bool print_result, | 59 bool print_result, |
| 55 bool report_exceptions); | 60 bool report_exceptions); |
| 56 v8::Handle<v8::Value> Print(const v8::Arguments& args); | 61 v8::Handle<v8::Value> Print(const v8::Arguments& args); |
| 57 v8::Handle<v8::Value> Read(const v8::Arguments& args); | 62 v8::Handle<v8::Value> Read(const v8::Arguments& args); |
| 58 v8::Handle<v8::Value> Load(const v8::Arguments& args); | 63 v8::Handle<v8::Value> Load(const v8::Arguments& args); |
| 59 v8::Handle<v8::Value> Quit(const v8::Arguments& args); | 64 v8::Handle<v8::Value> Quit(const v8::Arguments& args); |
| 60 v8::Handle<v8::Value> Version(const v8::Arguments& args); | 65 v8::Handle<v8::Value> Version(const v8::Arguments& args); |
| 61 v8::Handle<v8::String> ReadFile(const char* name); | 66 v8::Handle<v8::String> ReadFile(const char* name); |
| 62 void ReportException(v8::Isolate* isolate, v8::TryCatch* handler); | 67 void ReportException(v8::Isolate* isolate, v8::TryCatch* handler); |
| 63 | 68 |
| 64 | 69 |
| 65 static bool run_shell; | 70 static bool run_shell; |
| 66 | 71 |
| 67 | 72 |
| 68 int main(int argc, char* argv[]) { | 73 int main(int argc, char* argv[]) { |
| 69 v8::V8::SetFlagsFromCommandLine(&argc, argv, true); | 74 v8::V8::SetFlagsFromCommandLine(&argc, argv, true); |
| 70 v8::Isolate* isolate = v8::Isolate::GetCurrent(); | 75 v8::Isolate* isolate = v8::Isolate::GetCurrent(); |
| 71 run_shell = (argc == 1); | 76 run_shell = (argc == 1); |
| 72 int result; | 77 int result; |
| 73 { | 78 { |
| 74 v8::HandleScope handle_scope(isolate); | 79 v8::HandleScope handle_scope(isolate); |
| 75 v8::Persistent<v8::Context> context = CreateShellContext(); | 80 v8::Handle<v8::Context> context = CreateShellContext(); |
| 76 if (context.IsEmpty()) { | 81 if (context.IsEmpty()) { |
| 77 fprintf(stderr, "Error creating context\n"); | 82 fprintf(stderr, "Error creating context\n"); |
| 78 return 1; | 83 return 1; |
| 79 } | 84 } |
| 80 context->Enter(); | 85 context->Enter(); |
| 81 result = RunMain(isolate, argc, argv); | 86 result = RunMain(isolate, argc, argv); |
| 82 if (run_shell) RunShell(context); | 87 if (run_shell) RunShell(context); |
| 83 context->Exit(); | 88 context->Exit(); |
| 84 context.Dispose(isolate); | |
| 85 } | 89 } |
| 86 v8::V8::Dispose(); | 90 v8::V8::Dispose(); |
| 87 return result; | 91 return result; |
| 88 } | 92 } |
| 89 | 93 |
| 90 | 94 |
| 91 // Extracts a C string from a V8 Utf8Value. | 95 // Extracts a C string from a V8 Utf8Value. |
| 92 const char* ToCString(const v8::String::Utf8Value& value) { | 96 const char* ToCString(const v8::String::Utf8Value& value) { |
| 93 return *value ? *value : "<string conversion failed>"; | 97 return *value ? *value : "<string conversion failed>"; |
| 94 } | 98 } |
| 95 | 99 |
| 96 | 100 |
| 97 // Creates a new execution environment containing the built-in | 101 // Creates a new execution environment containing the built-in |
| 98 // functions. | 102 // functions. |
| 99 v8::Persistent<v8::Context> CreateShellContext() { | 103 v8::Handle<v8::Context> CreateShellContext() { |
| 100 // Create a template for the global object. | 104 // Create a template for the global object. |
| 101 v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(); | 105 v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(); |
| 102 // Bind the global 'print' function to the C++ Print callback. | 106 // Bind the global 'print' function to the C++ Print callback. |
| 103 global->Set(v8::String::New("print"), v8::FunctionTemplate::New(Print)); | 107 global->Set(v8::String::New("print"), v8::FunctionTemplate::New(Print)); |
| 104 // Bind the global 'read' function to the C++ Read callback. | 108 // Bind the global 'read' function to the C++ Read callback. |
| 105 global->Set(v8::String::New("read"), v8::FunctionTemplate::New(Read)); | 109 global->Set(v8::String::New("read"), v8::FunctionTemplate::New(Read)); |
| 106 // Bind the global 'load' function to the C++ Load callback. | 110 // Bind the global 'load' function to the C++ Load callback. |
| 107 global->Set(v8::String::New("load"), v8::FunctionTemplate::New(Load)); | 111 global->Set(v8::String::New("load"), v8::FunctionTemplate::New(Load)); |
| 108 // Bind the 'quit' function | 112 // Bind the 'quit' function |
| 109 global->Set(v8::String::New("quit"), v8::FunctionTemplate::New(Quit)); | 113 global->Set(v8::String::New("quit"), v8::FunctionTemplate::New(Quit)); |
| 110 // Bind the 'version' function | 114 // Bind the 'version' function |
| 111 global->Set(v8::String::New("version"), v8::FunctionTemplate::New(Version)); | 115 global->Set(v8::String::New("version"), v8::FunctionTemplate::New(Version)); |
| 112 | 116 |
| 113 return v8::Context::New(NULL, global); | 117 return v8::Context::New(v8::Isolate::GetCurrent(), NULL, global); |
|
Sven Panne
2013/04/30 07:31:22
While we're here, can we make this an argument? Th
| |
| 114 } | 118 } |
| 115 | 119 |
| 116 | 120 |
| 117 // The callback that is invoked by v8 whenever the JavaScript 'print' | 121 // The callback that is invoked by v8 whenever the JavaScript 'print' |
| 118 // function is called. Prints its arguments on stdout separated by | 122 // function is called. Prints its arguments on stdout separated by |
| 119 // spaces and ending with a newline. | 123 // spaces and ending with a newline. |
| 120 v8::Handle<v8::Value> Print(const v8::Arguments& args) { | 124 v8::Handle<v8::Value> Print(const v8::Arguments& args) { |
| 121 bool first = true; | 125 bool first = true; |
| 122 for (int i = 0; i < args.Length(); i++) { | 126 for (int i = 0; i < args.Length(); i++) { |
| 123 v8::HandleScope handle_scope(args.GetIsolate()); | 127 v8::HandleScope handle_scope(args.GetIsolate()); |
| (...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 343 fprintf(stderr, "^"); | 347 fprintf(stderr, "^"); |
| 344 } | 348 } |
| 345 fprintf(stderr, "\n"); | 349 fprintf(stderr, "\n"); |
| 346 v8::String::Utf8Value stack_trace(try_catch->StackTrace()); | 350 v8::String::Utf8Value stack_trace(try_catch->StackTrace()); |
| 347 if (stack_trace.length() > 0) { | 351 if (stack_trace.length() > 0) { |
| 348 const char* stack_trace_string = ToCString(stack_trace); | 352 const char* stack_trace_string = ToCString(stack_trace); |
| 349 fprintf(stderr, "%s\n", stack_trace_string); | 353 fprintf(stderr, "%s\n", stack_trace_string); |
| 350 } | 354 } |
| 351 } | 355 } |
| 352 } | 356 } |
| OLD | NEW |