Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1 | // Copyright 2012 the V8 project authors. All rights reserved. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 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. |
| 27 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 28 | #include <include/v8.h> |
| 29 | |
| 30 | #include <include/libplatform/libplatform.h> |
| 31 | |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 32 | #include <assert.h> |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 33 | #include <fcntl.h> |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 34 | #include <stdio.h> |
| 35 | #include <stdlib.h> |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 36 | #include <string.h> |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 37 | |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 38 | /** |
| 39 | * This sample program shows how to implement a simple javascript shell |
| 40 | * based on V8. This includes initializing V8 with command line options, |
| 41 | * creating global functions, compiling and executing strings. |
| 42 | * |
| 43 | * For a more sophisticated shell, consider using the debug shell D8. |
| 44 | */ |
| 45 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 46 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 47 | v8::Local<v8::Context> CreateShellContext(v8::Isolate* isolate); |
| 48 | void RunShell(v8::Local<v8::Context> context, v8::Platform* platform); |
| 49 | int RunMain(v8::Isolate* isolate, v8::Platform* platform, int argc, |
| 50 | char* argv[]); |
| 51 | bool ExecuteString(v8::Isolate* isolate, v8::Local<v8::String> source, |
| 52 | v8::Local<v8::Value> name, bool print_result, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 53 | bool report_exceptions); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 54 | void Print(const v8::FunctionCallbackInfo<v8::Value>& args); |
| 55 | void Read(const v8::FunctionCallbackInfo<v8::Value>& args); |
| 56 | void Load(const v8::FunctionCallbackInfo<v8::Value>& args); |
| 57 | void Quit(const v8::FunctionCallbackInfo<v8::Value>& args); |
| 58 | void Version(const v8::FunctionCallbackInfo<v8::Value>& args); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 59 | v8::MaybeLocal<v8::String> ReadFile(v8::Isolate* isolate, const char* name); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 60 | void ReportException(v8::Isolate* isolate, v8::TryCatch* handler); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 61 | |
| 62 | |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 63 | static bool run_shell; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 64 | |
| 65 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 66 | class ShellArrayBufferAllocator : public v8::ArrayBuffer::Allocator { |
| 67 | public: |
| 68 | virtual void* Allocate(size_t length) { |
| 69 | void* data = AllocateUninitialized(length); |
| 70 | return data == NULL ? data : memset(data, 0, length); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 71 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 72 | virtual void* AllocateUninitialized(size_t length) { return malloc(length); } |
| 73 | virtual void Free(void* data, size_t) { free(data); } |
| 74 | }; |
| 75 | |
| 76 | |
| 77 | int main(int argc, char* argv[]) { |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 78 | v8::V8::InitializeICUDefaultLocation(argv[0]); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 79 | v8::V8::InitializeExternalStartupData(argv[0]); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 80 | v8::Platform* platform = v8::platform::CreateDefaultPlatform(); |
| 81 | v8::V8::InitializePlatform(platform); |
| 82 | v8::V8::Initialize(); |
| 83 | v8::V8::SetFlagsFromCommandLine(&argc, argv, true); |
| 84 | ShellArrayBufferAllocator array_buffer_allocator; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 85 | v8::Isolate::CreateParams create_params; |
| 86 | create_params.array_buffer_allocator = &array_buffer_allocator; |
| 87 | v8::Isolate* isolate = v8::Isolate::New(create_params); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 88 | run_shell = (argc == 1); |
| 89 | int result; |
| 90 | { |
| 91 | v8::Isolate::Scope isolate_scope(isolate); |
| 92 | v8::HandleScope handle_scope(isolate); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 93 | v8::Local<v8::Context> context = CreateShellContext(isolate); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 94 | if (context.IsEmpty()) { |
| 95 | fprintf(stderr, "Error creating context\n"); |
| 96 | return 1; |
| 97 | } |
| 98 | v8::Context::Scope context_scope(context); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 99 | result = RunMain(isolate, platform, argc, argv); |
| 100 | if (run_shell) RunShell(context, platform); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 101 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 102 | isolate->Dispose(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 103 | v8::V8::Dispose(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 104 | v8::V8::ShutdownPlatform(); |
| 105 | delete platform; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 106 | return result; |
| 107 | } |
| 108 | |
| 109 | |
| 110 | // Extracts a C string from a V8 Utf8Value. |
| 111 | const char* ToCString(const v8::String::Utf8Value& value) { |
| 112 | return *value ? *value : "<string conversion failed>"; |
| 113 | } |
| 114 | |
| 115 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 116 | // Creates a new execution environment containing the built-in |
| 117 | // functions. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 118 | v8::Local<v8::Context> CreateShellContext(v8::Isolate* isolate) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 119 | // Create a template for the global object. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 120 | v8::Local<v8::ObjectTemplate> global = v8::ObjectTemplate::New(isolate); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 121 | // Bind the global 'print' function to the C++ Print callback. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 122 | global->Set( |
| 123 | v8::String::NewFromUtf8(isolate, "print", v8::NewStringType::kNormal) |
| 124 | .ToLocalChecked(), |
| 125 | v8::FunctionTemplate::New(isolate, Print)); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 126 | // Bind the global 'read' function to the C++ Read callback. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 127 | global->Set(v8::String::NewFromUtf8( |
| 128 | isolate, "read", v8::NewStringType::kNormal).ToLocalChecked(), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 129 | v8::FunctionTemplate::New(isolate, Read)); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 130 | // Bind the global 'load' function to the C++ Load callback. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 131 | global->Set(v8::String::NewFromUtf8( |
| 132 | isolate, "load", v8::NewStringType::kNormal).ToLocalChecked(), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 133 | v8::FunctionTemplate::New(isolate, Load)); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 134 | // Bind the 'quit' function |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 135 | global->Set(v8::String::NewFromUtf8( |
| 136 | isolate, "quit", v8::NewStringType::kNormal).ToLocalChecked(), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 137 | v8::FunctionTemplate::New(isolate, Quit)); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 138 | // Bind the 'version' function |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 139 | global->Set( |
| 140 | v8::String::NewFromUtf8(isolate, "version", v8::NewStringType::kNormal) |
| 141 | .ToLocalChecked(), |
| 142 | v8::FunctionTemplate::New(isolate, Version)); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 143 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 144 | return v8::Context::New(isolate, NULL, global); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 148 | // The callback that is invoked by v8 whenever the JavaScript 'print' |
| 149 | // function is called. Prints its arguments on stdout separated by |
| 150 | // spaces and ending with a newline. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 151 | void Print(const v8::FunctionCallbackInfo<v8::Value>& args) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 152 | bool first = true; |
| 153 | for (int i = 0; i < args.Length(); i++) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 154 | v8::HandleScope handle_scope(args.GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 155 | if (first) { |
| 156 | first = false; |
| 157 | } else { |
| 158 | printf(" "); |
| 159 | } |
| 160 | v8::String::Utf8Value str(args[i]); |
| 161 | const char* cstr = ToCString(str); |
| 162 | printf("%s", cstr); |
| 163 | } |
| 164 | printf("\n"); |
| 165 | fflush(stdout); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | |
| 169 | // The callback that is invoked by v8 whenever the JavaScript 'read' |
| 170 | // function is called. This function loads the content of the file named in |
| 171 | // the argument into a JavaScript string. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 172 | void Read(const v8::FunctionCallbackInfo<v8::Value>& args) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 173 | if (args.Length() != 1) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 174 | args.GetIsolate()->ThrowException( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 175 | v8::String::NewFromUtf8(args.GetIsolate(), "Bad parameters", |
| 176 | v8::NewStringType::kNormal).ToLocalChecked()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 177 | return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 178 | } |
| 179 | v8::String::Utf8Value file(args[0]); |
| 180 | if (*file == NULL) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 181 | args.GetIsolate()->ThrowException( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 182 | v8::String::NewFromUtf8(args.GetIsolate(), "Error loading file", |
| 183 | v8::NewStringType::kNormal).ToLocalChecked()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 184 | return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 185 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 186 | v8::Local<v8::String> source; |
| 187 | if (!ReadFile(args.GetIsolate(), *file).ToLocal(&source)) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 188 | args.GetIsolate()->ThrowException( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 189 | v8::String::NewFromUtf8(args.GetIsolate(), "Error loading file", |
| 190 | v8::NewStringType::kNormal).ToLocalChecked()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 191 | return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 192 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 193 | args.GetReturnValue().Set(source); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | |
| 197 | // The callback that is invoked by v8 whenever the JavaScript 'load' |
| 198 | // function is called. Loads, compiles and executes its argument |
| 199 | // JavaScript file. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 200 | void Load(const v8::FunctionCallbackInfo<v8::Value>& args) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 201 | for (int i = 0; i < args.Length(); i++) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 202 | v8::HandleScope handle_scope(args.GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 203 | v8::String::Utf8Value file(args[i]); |
| 204 | if (*file == NULL) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 205 | args.GetIsolate()->ThrowException( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 206 | v8::String::NewFromUtf8(args.GetIsolate(), "Error loading file", |
| 207 | v8::NewStringType::kNormal).ToLocalChecked()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 208 | return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 209 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 210 | v8::Local<v8::String> source; |
| 211 | if (!ReadFile(args.GetIsolate(), *file).ToLocal(&source)) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 212 | args.GetIsolate()->ThrowException( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 213 | v8::String::NewFromUtf8(args.GetIsolate(), "Error loading file", |
| 214 | v8::NewStringType::kNormal).ToLocalChecked()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 215 | return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 216 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 217 | if (!ExecuteString(args.GetIsolate(), source, args[i], false, false)) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 218 | args.GetIsolate()->ThrowException( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 219 | v8::String::NewFromUtf8(args.GetIsolate(), "Error executing file", |
| 220 | v8::NewStringType::kNormal).ToLocalChecked()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 221 | return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 222 | } |
| 223 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | |
| 227 | // The callback that is invoked by v8 whenever the JavaScript 'quit' |
| 228 | // function is called. Quits. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 229 | void Quit(const v8::FunctionCallbackInfo<v8::Value>& args) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 230 | // If not arguments are given args[0] will yield undefined which |
| 231 | // converts to the integer value 0. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 232 | int exit_code = |
| 233 | args[0]->Int32Value(args.GetIsolate()->GetCurrentContext()).FromMaybe(0); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 234 | fflush(stdout); |
| 235 | fflush(stderr); |
| 236 | exit(exit_code); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 240 | void Version(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 241 | args.GetReturnValue().Set( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 242 | v8::String::NewFromUtf8(args.GetIsolate(), v8::V8::GetVersion(), |
| 243 | v8::NewStringType::kNormal).ToLocalChecked()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | |
| 247 | // Reads a file into a v8 string. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 248 | v8::MaybeLocal<v8::String> ReadFile(v8::Isolate* isolate, const char* name) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 249 | FILE* file = fopen(name, "rb"); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 250 | if (file == NULL) return v8::MaybeLocal<v8::String>(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 251 | |
| 252 | fseek(file, 0, SEEK_END); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 253 | size_t size = ftell(file); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 254 | rewind(file); |
| 255 | |
| 256 | char* chars = new char[size + 1]; |
| 257 | chars[size] = '\0'; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 258 | for (size_t i = 0; i < size;) { |
| 259 | i += fread(&chars[i], 1, size - i, file); |
| 260 | if (ferror(file)) { |
| 261 | fclose(file); |
| 262 | return v8::MaybeLocal<v8::String>(); |
| 263 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 264 | } |
| 265 | fclose(file); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 266 | v8::MaybeLocal<v8::String> result = v8::String::NewFromUtf8( |
| 267 | isolate, chars, v8::NewStringType::kNormal, static_cast<int>(size)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 268 | delete[] chars; |
| 269 | return result; |
| 270 | } |
| 271 | |
| 272 | |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 273 | // Process remaining command line arguments and execute files |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 274 | int RunMain(v8::Isolate* isolate, v8::Platform* platform, int argc, |
| 275 | char* argv[]) { |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 276 | for (int i = 1; i < argc; i++) { |
| 277 | const char* str = argv[i]; |
| 278 | if (strcmp(str, "--shell") == 0) { |
| 279 | run_shell = true; |
| 280 | } else if (strcmp(str, "-f") == 0) { |
| 281 | // Ignore any -f flags for compatibility with the other stand- |
| 282 | // alone JavaScript engines. |
| 283 | continue; |
| 284 | } else if (strncmp(str, "--", 2) == 0) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 285 | fprintf(stderr, |
| 286 | "Warning: unknown flag %s.\nTry --help for options\n", str); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 287 | } else if (strcmp(str, "-e") == 0 && i + 1 < argc) { |
| 288 | // Execute argument given to -e option directly. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 289 | v8::Local<v8::String> file_name = |
| 290 | v8::String::NewFromUtf8(isolate, "unnamed", |
| 291 | v8::NewStringType::kNormal).ToLocalChecked(); |
| 292 | v8::Local<v8::String> source; |
| 293 | if (!v8::String::NewFromUtf8(isolate, argv[++i], |
| 294 | v8::NewStringType::kNormal) |
| 295 | .ToLocal(&source)) { |
| 296 | return 1; |
| 297 | } |
| 298 | bool success = ExecuteString(isolate, source, file_name, false, true); |
| 299 | while (v8::platform::PumpMessageLoop(platform, isolate)) continue; |
| 300 | if (!success) return 1; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 301 | } else { |
| 302 | // Use all other arguments as names of files to load and run. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 303 | v8::Local<v8::String> file_name = |
| 304 | v8::String::NewFromUtf8(isolate, str, v8::NewStringType::kNormal) |
| 305 | .ToLocalChecked(); |
| 306 | v8::Local<v8::String> source; |
| 307 | if (!ReadFile(isolate, str).ToLocal(&source)) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 308 | fprintf(stderr, "Error reading '%s'\n", str); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 309 | continue; |
| 310 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 311 | bool success = ExecuteString(isolate, source, file_name, false, true); |
| 312 | while (v8::platform::PumpMessageLoop(platform, isolate)) continue; |
| 313 | if (!success) return 1; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 314 | } |
| 315 | } |
| 316 | return 0; |
| 317 | } |
| 318 | |
| 319 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 320 | // The read-eval-execute loop of the shell. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 321 | void RunShell(v8::Local<v8::Context> context, v8::Platform* platform) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 322 | fprintf(stderr, "V8 version %s [sample shell]\n", v8::V8::GetVersion()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 323 | static const int kBufferSize = 256; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 324 | // Enter the execution environment before evaluating any code. |
| 325 | v8::Context::Scope context_scope(context); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 326 | v8::Local<v8::String> name( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 327 | v8::String::NewFromUtf8(context->GetIsolate(), "(shell)", |
| 328 | v8::NewStringType::kNormal).ToLocalChecked()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 329 | while (true) { |
| 330 | char buffer[kBufferSize]; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 331 | fprintf(stderr, "> "); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 332 | char* str = fgets(buffer, kBufferSize, stdin); |
| 333 | if (str == NULL) break; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 334 | v8::HandleScope handle_scope(context->GetIsolate()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 335 | ExecuteString( |
| 336 | context->GetIsolate(), |
| 337 | v8::String::NewFromUtf8(context->GetIsolate(), str, |
| 338 | v8::NewStringType::kNormal).ToLocalChecked(), |
| 339 | name, true, true); |
| 340 | while (v8::platform::PumpMessageLoop(platform, context->GetIsolate())) |
| 341 | continue; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 342 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 343 | fprintf(stderr, "\n"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | |
| 347 | // Executes a string within the current v8 context. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 348 | bool ExecuteString(v8::Isolate* isolate, v8::Local<v8::String> source, |
| 349 | v8::Local<v8::Value> name, bool print_result, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 350 | bool report_exceptions) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 351 | v8::HandleScope handle_scope(isolate); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 352 | v8::TryCatch try_catch(isolate); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 353 | v8::ScriptOrigin origin(name); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 354 | v8::Local<v8::Context> context(isolate->GetCurrentContext()); |
| 355 | v8::Local<v8::Script> script; |
| 356 | if (!v8::Script::Compile(context, source, &origin).ToLocal(&script)) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 357 | // Print errors that happened during compilation. |
| 358 | if (report_exceptions) |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 359 | ReportException(isolate, &try_catch); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 360 | return false; |
| 361 | } else { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 362 | v8::Local<v8::Value> result; |
| 363 | if (!script->Run(context).ToLocal(&result)) { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 364 | assert(try_catch.HasCaught()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 365 | // Print errors that happened during execution. |
| 366 | if (report_exceptions) |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 367 | ReportException(isolate, &try_catch); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 368 | return false; |
| 369 | } else { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 370 | assert(!try_catch.HasCaught()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 371 | if (print_result && !result->IsUndefined()) { |
| 372 | // If all went well and the result wasn't undefined then print |
| 373 | // the returned value. |
| 374 | v8::String::Utf8Value str(result); |
| 375 | const char* cstr = ToCString(str); |
| 376 | printf("%s\n", cstr); |
| 377 | } |
| 378 | return true; |
| 379 | } |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 384 | void ReportException(v8::Isolate* isolate, v8::TryCatch* try_catch) { |
| 385 | v8::HandleScope handle_scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 386 | v8::String::Utf8Value exception(try_catch->Exception()); |
| 387 | const char* exception_string = ToCString(exception); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 388 | v8::Local<v8::Message> message = try_catch->Message(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 389 | if (message.IsEmpty()) { |
| 390 | // V8 didn't provide any extra information about this error; just |
| 391 | // print the exception. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 392 | fprintf(stderr, "%s\n", exception_string); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 393 | } else { |
| 394 | // Print (filename):(line number): (message). |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 395 | v8::String::Utf8Value filename(message->GetScriptOrigin().ResourceName()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 396 | v8::Local<v8::Context> context(isolate->GetCurrentContext()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 397 | const char* filename_string = ToCString(filename); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 398 | int linenum = message->GetLineNumber(context).FromJust(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 399 | fprintf(stderr, "%s:%i: %s\n", filename_string, linenum, exception_string); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 400 | // Print line of source code. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 401 | v8::String::Utf8Value sourceline( |
| 402 | message->GetSourceLine(context).ToLocalChecked()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 403 | const char* sourceline_string = ToCString(sourceline); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 404 | fprintf(stderr, "%s\n", sourceline_string); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 405 | // Print wavy underline (GetUnderline is deprecated). |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 406 | int start = message->GetStartColumn(context).FromJust(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 407 | for (int i = 0; i < start; i++) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 408 | fprintf(stderr, " "); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 409 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 410 | int end = message->GetEndColumn(context).FromJust(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 411 | for (int i = start; i < end; i++) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 412 | fprintf(stderr, "^"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 413 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 414 | fprintf(stderr, "\n"); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 415 | v8::Local<v8::Value> stack_trace_string; |
| 416 | if (try_catch->StackTrace(context).ToLocal(&stack_trace_string) && |
| 417 | stack_trace_string->IsString() && |
| 418 | v8::Local<v8::String>::Cast(stack_trace_string)->Length() > 0) { |
| 419 | v8::String::Utf8Value stack_trace(stack_trace_string); |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 420 | const char* stack_trace_string = ToCString(stack_trace); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 421 | fprintf(stderr, "%s\n", stack_trace_string); |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 422 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 423 | } |
| 424 | } |