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 | #ifdef COMPRESS_STARTUP_DATA_BZ2 |
| 39 | #error Using compressed startup data is not supported for this sample |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 40 | #endif |
| 41 | |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 42 | /** |
| 43 | * This sample program shows how to implement a simple javascript shell |
| 44 | * based on V8. This includes initializing V8 with command line options, |
| 45 | * creating global functions, compiling and executing strings. |
| 46 | * |
| 47 | * For a more sophisticated shell, consider using the debug shell D8. |
| 48 | */ |
| 49 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 50 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 51 | v8::Handle<v8::Context> CreateShellContext(v8::Isolate* isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 52 | void RunShell(v8::Handle<v8::Context> context); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 53 | int RunMain(v8::Isolate* isolate, int argc, char* argv[]); |
| 54 | bool ExecuteString(v8::Isolate* isolate, |
| 55 | v8::Handle<v8::String> source, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 56 | v8::Handle<v8::Value> name, |
| 57 | bool print_result, |
| 58 | bool report_exceptions); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 59 | void Print(const v8::FunctionCallbackInfo<v8::Value>& args); |
| 60 | void Read(const v8::FunctionCallbackInfo<v8::Value>& args); |
| 61 | void Load(const v8::FunctionCallbackInfo<v8::Value>& args); |
| 62 | void Quit(const v8::FunctionCallbackInfo<v8::Value>& args); |
| 63 | void Version(const v8::FunctionCallbackInfo<v8::Value>& args); |
| 64 | v8::Handle<v8::String> ReadFile(v8::Isolate* isolate, const char* name); |
| 65 | void ReportException(v8::Isolate* isolate, v8::TryCatch* handler); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 66 | |
| 67 | |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 68 | static bool run_shell; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 69 | |
| 70 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 71 | class ShellArrayBufferAllocator : public v8::ArrayBuffer::Allocator { |
| 72 | public: |
| 73 | virtual void* Allocate(size_t length) { |
| 74 | void* data = AllocateUninitialized(length); |
| 75 | return data == NULL ? data : memset(data, 0, length); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 76 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 77 | virtual void* AllocateUninitialized(size_t length) { return malloc(length); } |
| 78 | virtual void Free(void* data, size_t) { free(data); } |
| 79 | }; |
| 80 | |
| 81 | |
| 82 | int main(int argc, char* argv[]) { |
| 83 | v8::V8::InitializeICU(); |
| 84 | v8::Platform* platform = v8::platform::CreateDefaultPlatform(); |
| 85 | v8::V8::InitializePlatform(platform); |
| 86 | v8::V8::Initialize(); |
| 87 | v8::V8::SetFlagsFromCommandLine(&argc, argv, true); |
| 88 | ShellArrayBufferAllocator array_buffer_allocator; |
| 89 | v8::V8::SetArrayBufferAllocator(&array_buffer_allocator); |
| 90 | v8::Isolate* isolate = v8::Isolate::New(); |
| 91 | run_shell = (argc == 1); |
| 92 | int result; |
| 93 | { |
| 94 | v8::Isolate::Scope isolate_scope(isolate); |
| 95 | v8::HandleScope handle_scope(isolate); |
| 96 | v8::Handle<v8::Context> context = CreateShellContext(isolate); |
| 97 | if (context.IsEmpty()) { |
| 98 | fprintf(stderr, "Error creating context\n"); |
| 99 | return 1; |
| 100 | } |
| 101 | v8::Context::Scope context_scope(context); |
| 102 | result = RunMain(isolate, argc, argv); |
| 103 | if (run_shell) RunShell(context); |
| 104 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 105 | v8::V8::Dispose(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 106 | v8::V8::ShutdownPlatform(); |
| 107 | delete platform; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 108 | return result; |
| 109 | } |
| 110 | |
| 111 | |
| 112 | // Extracts a C string from a V8 Utf8Value. |
| 113 | const char* ToCString(const v8::String::Utf8Value& value) { |
| 114 | return *value ? *value : "<string conversion failed>"; |
| 115 | } |
| 116 | |
| 117 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 118 | // Creates a new execution environment containing the built-in |
| 119 | // functions. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 120 | v8::Handle<v8::Context> CreateShellContext(v8::Isolate* isolate) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 121 | // Create a template for the global object. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 122 | v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(isolate); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 123 | // Bind the global 'print' function to the C++ Print callback. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 124 | global->Set(v8::String::NewFromUtf8(isolate, "print"), |
| 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 | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 127 | global->Set(v8::String::NewFromUtf8(isolate, "read"), |
| 128 | v8::FunctionTemplate::New(isolate, Read)); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 129 | // Bind the global 'load' function to the C++ Load callback. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 130 | global->Set(v8::String::NewFromUtf8(isolate, "load"), |
| 131 | v8::FunctionTemplate::New(isolate, Load)); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 132 | // Bind the 'quit' function |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 133 | global->Set(v8::String::NewFromUtf8(isolate, "quit"), |
| 134 | v8::FunctionTemplate::New(isolate, Quit)); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 135 | // Bind the 'version' function |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 136 | global->Set(v8::String::NewFromUtf8(isolate, "version"), |
| 137 | v8::FunctionTemplate::New(isolate, Version)); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 138 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 139 | return v8::Context::New(isolate, NULL, global); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 143 | // The callback that is invoked by v8 whenever the JavaScript 'print' |
| 144 | // function is called. Prints its arguments on stdout separated by |
| 145 | // spaces and ending with a newline. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 146 | void Print(const v8::FunctionCallbackInfo<v8::Value>& args) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 147 | bool first = true; |
| 148 | for (int i = 0; i < args.Length(); i++) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 149 | v8::HandleScope handle_scope(args.GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 150 | if (first) { |
| 151 | first = false; |
| 152 | } else { |
| 153 | printf(" "); |
| 154 | } |
| 155 | v8::String::Utf8Value str(args[i]); |
| 156 | const char* cstr = ToCString(str); |
| 157 | printf("%s", cstr); |
| 158 | } |
| 159 | printf("\n"); |
| 160 | fflush(stdout); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | |
| 164 | // The callback that is invoked by v8 whenever the JavaScript 'read' |
| 165 | // function is called. This function loads the content of the file named in |
| 166 | // the argument into a JavaScript string. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 167 | void Read(const v8::FunctionCallbackInfo<v8::Value>& args) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 168 | if (args.Length() != 1) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 169 | args.GetIsolate()->ThrowException( |
| 170 | v8::String::NewFromUtf8(args.GetIsolate(), "Bad parameters")); |
| 171 | return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 172 | } |
| 173 | v8::String::Utf8Value file(args[0]); |
| 174 | if (*file == NULL) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 175 | args.GetIsolate()->ThrowException( |
| 176 | v8::String::NewFromUtf8(args.GetIsolate(), "Error loading file")); |
| 177 | return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 178 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 179 | v8::Handle<v8::String> source = ReadFile(args.GetIsolate(), *file); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 180 | if (source.IsEmpty()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 181 | args.GetIsolate()->ThrowException( |
| 182 | v8::String::NewFromUtf8(args.GetIsolate(), "Error loading file")); |
| 183 | return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 184 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 185 | args.GetReturnValue().Set(source); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | |
| 189 | // The callback that is invoked by v8 whenever the JavaScript 'load' |
| 190 | // function is called. Loads, compiles and executes its argument |
| 191 | // JavaScript file. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 192 | void Load(const v8::FunctionCallbackInfo<v8::Value>& args) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 193 | for (int i = 0; i < args.Length(); i++) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 194 | v8::HandleScope handle_scope(args.GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 195 | v8::String::Utf8Value file(args[i]); |
| 196 | if (*file == NULL) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 197 | args.GetIsolate()->ThrowException( |
| 198 | v8::String::NewFromUtf8(args.GetIsolate(), "Error loading file")); |
| 199 | return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 200 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 201 | v8::Handle<v8::String> source = ReadFile(args.GetIsolate(), *file); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 202 | if (source.IsEmpty()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 203 | args.GetIsolate()->ThrowException( |
| 204 | v8::String::NewFromUtf8(args.GetIsolate(), "Error loading file")); |
| 205 | return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 206 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 207 | if (!ExecuteString(args.GetIsolate(), |
| 208 | source, |
| 209 | v8::String::NewFromUtf8(args.GetIsolate(), *file), |
| 210 | false, |
| 211 | false)) { |
| 212 | args.GetIsolate()->ThrowException( |
| 213 | v8::String::NewFromUtf8(args.GetIsolate(), "Error executing file")); |
| 214 | return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 215 | } |
| 216 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | |
| 220 | // The callback that is invoked by v8 whenever the JavaScript 'quit' |
| 221 | // function is called. Quits. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 222 | void Quit(const v8::FunctionCallbackInfo<v8::Value>& args) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 223 | // If not arguments are given args[0] will yield undefined which |
| 224 | // converts to the integer value 0. |
| 225 | int exit_code = args[0]->Int32Value(); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 226 | fflush(stdout); |
| 227 | fflush(stderr); |
| 228 | exit(exit_code); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 232 | void Version(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 233 | args.GetReturnValue().Set( |
| 234 | v8::String::NewFromUtf8(args.GetIsolate(), v8::V8::GetVersion())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | |
| 238 | // Reads a file into a v8 string. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 239 | v8::Handle<v8::String> ReadFile(v8::Isolate* isolate, const char* name) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 240 | FILE* file = fopen(name, "rb"); |
| 241 | if (file == NULL) return v8::Handle<v8::String>(); |
| 242 | |
| 243 | fseek(file, 0, SEEK_END); |
| 244 | int size = ftell(file); |
| 245 | rewind(file); |
| 246 | |
| 247 | char* chars = new char[size + 1]; |
| 248 | chars[size] = '\0'; |
| 249 | for (int i = 0; i < size;) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 250 | int read = static_cast<int>(fread(&chars[i], 1, size - i, file)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 251 | i += read; |
| 252 | } |
| 253 | fclose(file); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 254 | v8::Handle<v8::String> result = |
| 255 | v8::String::NewFromUtf8(isolate, chars, v8::String::kNormalString, size); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 256 | delete[] chars; |
| 257 | return result; |
| 258 | } |
| 259 | |
| 260 | |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 261 | // Process remaining command line arguments and execute files |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 262 | int RunMain(v8::Isolate* isolate, int argc, char* argv[]) { |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 263 | for (int i = 1; i < argc; i++) { |
| 264 | const char* str = argv[i]; |
| 265 | if (strcmp(str, "--shell") == 0) { |
| 266 | run_shell = true; |
| 267 | } else if (strcmp(str, "-f") == 0) { |
| 268 | // Ignore any -f flags for compatibility with the other stand- |
| 269 | // alone JavaScript engines. |
| 270 | continue; |
| 271 | } else if (strncmp(str, "--", 2) == 0) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 272 | fprintf(stderr, |
| 273 | "Warning: unknown flag %s.\nTry --help for options\n", str); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 274 | } else if (strcmp(str, "-e") == 0 && i + 1 < argc) { |
| 275 | // Execute argument given to -e option directly. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 276 | v8::Handle<v8::String> file_name = |
| 277 | v8::String::NewFromUtf8(isolate, "unnamed"); |
| 278 | v8::Handle<v8::String> source = |
| 279 | v8::String::NewFromUtf8(isolate, argv[++i]); |
| 280 | if (!ExecuteString(isolate, source, file_name, false, true)) return 1; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 281 | } else { |
| 282 | // Use all other arguments as names of files to load and run. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 283 | v8::Handle<v8::String> file_name = v8::String::NewFromUtf8(isolate, str); |
| 284 | v8::Handle<v8::String> source = ReadFile(isolate, str); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 285 | if (source.IsEmpty()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 286 | fprintf(stderr, "Error reading '%s'\n", str); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 287 | continue; |
| 288 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 289 | if (!ExecuteString(isolate, source, file_name, false, true)) return 1; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 290 | } |
| 291 | } |
| 292 | return 0; |
| 293 | } |
| 294 | |
| 295 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 296 | // The read-eval-execute loop of the shell. |
| 297 | void RunShell(v8::Handle<v8::Context> context) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 298 | fprintf(stderr, "V8 version %s [sample shell]\n", v8::V8::GetVersion()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 299 | static const int kBufferSize = 256; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 300 | // Enter the execution environment before evaluating any code. |
| 301 | v8::Context::Scope context_scope(context); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 302 | v8::Local<v8::String> name( |
| 303 | v8::String::NewFromUtf8(context->GetIsolate(), "(shell)")); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 304 | while (true) { |
| 305 | char buffer[kBufferSize]; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 306 | fprintf(stderr, "> "); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 307 | char* str = fgets(buffer, kBufferSize, stdin); |
| 308 | if (str == NULL) break; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 309 | v8::HandleScope handle_scope(context->GetIsolate()); |
| 310 | ExecuteString(context->GetIsolate(), |
| 311 | v8::String::NewFromUtf8(context->GetIsolate(), str), |
| 312 | name, |
| 313 | true, |
| 314 | true); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 315 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 316 | fprintf(stderr, "\n"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | |
| 320 | // Executes a string within the current v8 context. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 321 | bool ExecuteString(v8::Isolate* isolate, |
| 322 | v8::Handle<v8::String> source, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 323 | v8::Handle<v8::Value> name, |
| 324 | bool print_result, |
| 325 | bool report_exceptions) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 326 | v8::HandleScope handle_scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 327 | v8::TryCatch try_catch; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 328 | v8::ScriptOrigin origin(name); |
| 329 | v8::Handle<v8::Script> script = v8::Script::Compile(source, &origin); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 330 | if (script.IsEmpty()) { |
| 331 | // Print errors that happened during compilation. |
| 332 | if (report_exceptions) |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 333 | ReportException(isolate, &try_catch); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 334 | return false; |
| 335 | } else { |
| 336 | v8::Handle<v8::Value> result = script->Run(); |
| 337 | if (result.IsEmpty()) { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 338 | assert(try_catch.HasCaught()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 339 | // Print errors that happened during execution. |
| 340 | if (report_exceptions) |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 341 | ReportException(isolate, &try_catch); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 342 | return false; |
| 343 | } else { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 344 | assert(!try_catch.HasCaught()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 345 | if (print_result && !result->IsUndefined()) { |
| 346 | // If all went well and the result wasn't undefined then print |
| 347 | // the returned value. |
| 348 | v8::String::Utf8Value str(result); |
| 349 | const char* cstr = ToCString(str); |
| 350 | printf("%s\n", cstr); |
| 351 | } |
| 352 | return true; |
| 353 | } |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 358 | void ReportException(v8::Isolate* isolate, v8::TryCatch* try_catch) { |
| 359 | v8::HandleScope handle_scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 360 | v8::String::Utf8Value exception(try_catch->Exception()); |
| 361 | const char* exception_string = ToCString(exception); |
| 362 | v8::Handle<v8::Message> message = try_catch->Message(); |
| 363 | if (message.IsEmpty()) { |
| 364 | // V8 didn't provide any extra information about this error; just |
| 365 | // print the exception. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 366 | fprintf(stderr, "%s\n", exception_string); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 367 | } else { |
| 368 | // Print (filename):(line number): (message). |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 369 | v8::String::Utf8Value filename(message->GetScriptOrigin().ResourceName()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 370 | const char* filename_string = ToCString(filename); |
| 371 | int linenum = message->GetLineNumber(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 372 | fprintf(stderr, "%s:%i: %s\n", filename_string, linenum, exception_string); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 373 | // Print line of source code. |
| 374 | v8::String::Utf8Value sourceline(message->GetSourceLine()); |
| 375 | const char* sourceline_string = ToCString(sourceline); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 376 | fprintf(stderr, "%s\n", sourceline_string); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 377 | // Print wavy underline (GetUnderline is deprecated). |
| 378 | int start = message->GetStartColumn(); |
| 379 | for (int i = 0; i < start; i++) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 380 | fprintf(stderr, " "); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 381 | } |
| 382 | int end = message->GetEndColumn(); |
| 383 | for (int i = start; i < end; i++) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 384 | fprintf(stderr, "^"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 385 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 386 | fprintf(stderr, "\n"); |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 387 | v8::String::Utf8Value stack_trace(try_catch->StackTrace()); |
| 388 | if (stack_trace.length() > 0) { |
| 389 | const char* stack_trace_string = ToCString(stack_trace); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 390 | fprintf(stderr, "%s\n", stack_trace_string); |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 391 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 392 | } |
| 393 | } |