blob: ad9b1aba8bfd8b30be150bccb489f2ee31c4240f [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// Copyright 2012 the V8 project authors. All rights reserved.
Steve Blocka7e24c12009-10-30 11:49:00 +00002// 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 Murdochb8a8cc12014-11-26 15:28:44 +000028#include <include/v8.h>
29
30#include <include/libplatform/libplatform.h>
31
Ben Murdoche0cee9b2011-05-25 10:26:03 +010032#include <assert.h>
Steve Blocka7e24c12009-10-30 11:49:00 +000033#include <fcntl.h>
Steve Blocka7e24c12009-10-30 11:49:00 +000034#include <stdio.h>
35#include <stdlib.h>
Ben Murdochb8a8cc12014-11-26 15:28:44 +000036#include <string.h>
Steve Blocka7e24c12009-10-30 11:49:00 +000037
Ben Murdoch69a99ed2011-11-30 16:03:39 +000038/**
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 Block44f0eee2011-05-26 01:26:41 +010046
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000047v8::Local<v8::Context> CreateShellContext(v8::Isolate* isolate);
48void RunShell(v8::Local<v8::Context> context, v8::Platform* platform);
49int RunMain(v8::Isolate* isolate, v8::Platform* platform, int argc,
50 char* argv[]);
51bool ExecuteString(v8::Isolate* isolate, v8::Local<v8::String> source,
52 v8::Local<v8::Value> name, bool print_result,
Steve Blocka7e24c12009-10-30 11:49:00 +000053 bool report_exceptions);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000054void Print(const v8::FunctionCallbackInfo<v8::Value>& args);
55void Read(const v8::FunctionCallbackInfo<v8::Value>& args);
56void Load(const v8::FunctionCallbackInfo<v8::Value>& args);
57void Quit(const v8::FunctionCallbackInfo<v8::Value>& args);
58void Version(const v8::FunctionCallbackInfo<v8::Value>& args);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000059v8::MaybeLocal<v8::String> ReadFile(v8::Isolate* isolate, const char* name);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000060void ReportException(v8::Isolate* isolate, v8::TryCatch* handler);
Steve Blocka7e24c12009-10-30 11:49:00 +000061
62
Ben Murdoch69a99ed2011-11-30 16:03:39 +000063static bool run_shell;
Steve Block44f0eee2011-05-26 01:26:41 +010064
65
Ben Murdochb8a8cc12014-11-26 15:28:44 +000066class 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 Block44f0eee2011-05-26 01:26:41 +010071 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000072 virtual void* AllocateUninitialized(size_t length) { return malloc(length); }
73 virtual void Free(void* data, size_t) { free(data); }
74};
75
76
77int main(int argc, char* argv[]) {
Ben Murdoch61f157c2016-09-16 13:49:30 +010078 v8::V8::InitializeICUDefaultLocation(argv[0]);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000079 v8::V8::InitializeExternalStartupData(argv[0]);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000080 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 Murdoch4a90d5f2016-03-22 12:00:34 +000085 v8::Isolate::CreateParams create_params;
86 create_params.array_buffer_allocator = &array_buffer_allocator;
87 v8::Isolate* isolate = v8::Isolate::New(create_params);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000088 run_shell = (argc == 1);
89 int result;
90 {
91 v8::Isolate::Scope isolate_scope(isolate);
92 v8::HandleScope handle_scope(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000093 v8::Local<v8::Context> context = CreateShellContext(isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000094 if (context.IsEmpty()) {
95 fprintf(stderr, "Error creating context\n");
96 return 1;
97 }
98 v8::Context::Scope context_scope(context);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000099 result = RunMain(isolate, platform, argc, argv);
100 if (run_shell) RunShell(context, platform);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000101 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000102 isolate->Dispose();
Steve Blocka7e24c12009-10-30 11:49:00 +0000103 v8::V8::Dispose();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000104 v8::V8::ShutdownPlatform();
105 delete platform;
Steve Blocka7e24c12009-10-30 11:49:00 +0000106 return result;
107}
108
109
110// Extracts a C string from a V8 Utf8Value.
111const char* ToCString(const v8::String::Utf8Value& value) {
112 return *value ? *value : "<string conversion failed>";
113}
114
115
Steve Block44f0eee2011-05-26 01:26:41 +0100116// Creates a new execution environment containing the built-in
117// functions.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000118v8::Local<v8::Context> CreateShellContext(v8::Isolate* isolate) {
Steve Block44f0eee2011-05-26 01:26:41 +0100119 // Create a template for the global object.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000120 v8::Local<v8::ObjectTemplate> global = v8::ObjectTemplate::New(isolate);
Steve Block44f0eee2011-05-26 01:26:41 +0100121 // Bind the global 'print' function to the C++ Print callback.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000122 global->Set(
123 v8::String::NewFromUtf8(isolate, "print", v8::NewStringType::kNormal)
124 .ToLocalChecked(),
125 v8::FunctionTemplate::New(isolate, Print));
Steve Block44f0eee2011-05-26 01:26:41 +0100126 // Bind the global 'read' function to the C++ Read callback.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000127 global->Set(v8::String::NewFromUtf8(
128 isolate, "read", v8::NewStringType::kNormal).ToLocalChecked(),
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000129 v8::FunctionTemplate::New(isolate, Read));
Steve Block44f0eee2011-05-26 01:26:41 +0100130 // Bind the global 'load' function to the C++ Load callback.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000131 global->Set(v8::String::NewFromUtf8(
132 isolate, "load", v8::NewStringType::kNormal).ToLocalChecked(),
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000133 v8::FunctionTemplate::New(isolate, Load));
Steve Block44f0eee2011-05-26 01:26:41 +0100134 // Bind the 'quit' function
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000135 global->Set(v8::String::NewFromUtf8(
136 isolate, "quit", v8::NewStringType::kNormal).ToLocalChecked(),
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000137 v8::FunctionTemplate::New(isolate, Quit));
Steve Block44f0eee2011-05-26 01:26:41 +0100138 // Bind the 'version' function
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000139 global->Set(
140 v8::String::NewFromUtf8(isolate, "version", v8::NewStringType::kNormal)
141 .ToLocalChecked(),
142 v8::FunctionTemplate::New(isolate, Version));
Ben Murdoch8b112d22011-06-08 16:22:53 +0100143
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000144 return v8::Context::New(isolate, NULL, global);
Steve Block44f0eee2011-05-26 01:26:41 +0100145}
146
147
Steve Blocka7e24c12009-10-30 11:49:00 +0000148// 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 Murdochb8a8cc12014-11-26 15:28:44 +0000151void Print(const v8::FunctionCallbackInfo<v8::Value>& args) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000152 bool first = true;
153 for (int i = 0; i < args.Length(); i++) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000154 v8::HandleScope handle_scope(args.GetIsolate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000155 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 Blocka7e24c12009-10-30 11:49:00 +0000166}
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 Murdochb8a8cc12014-11-26 15:28:44 +0000172void Read(const v8::FunctionCallbackInfo<v8::Value>& args) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000173 if (args.Length() != 1) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000174 args.GetIsolate()->ThrowException(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000175 v8::String::NewFromUtf8(args.GetIsolate(), "Bad parameters",
176 v8::NewStringType::kNormal).ToLocalChecked());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000177 return;
Steve Blocka7e24c12009-10-30 11:49:00 +0000178 }
179 v8::String::Utf8Value file(args[0]);
180 if (*file == NULL) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000181 args.GetIsolate()->ThrowException(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000182 v8::String::NewFromUtf8(args.GetIsolate(), "Error loading file",
183 v8::NewStringType::kNormal).ToLocalChecked());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000184 return;
Steve Blocka7e24c12009-10-30 11:49:00 +0000185 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000186 v8::Local<v8::String> source;
187 if (!ReadFile(args.GetIsolate(), *file).ToLocal(&source)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000188 args.GetIsolate()->ThrowException(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000189 v8::String::NewFromUtf8(args.GetIsolate(), "Error loading file",
190 v8::NewStringType::kNormal).ToLocalChecked());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000191 return;
Steve Blocka7e24c12009-10-30 11:49:00 +0000192 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000193 args.GetReturnValue().Set(source);
Steve Blocka7e24c12009-10-30 11:49:00 +0000194}
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 Murdochb8a8cc12014-11-26 15:28:44 +0000200void Load(const v8::FunctionCallbackInfo<v8::Value>& args) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000201 for (int i = 0; i < args.Length(); i++) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000202 v8::HandleScope handle_scope(args.GetIsolate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000203 v8::String::Utf8Value file(args[i]);
204 if (*file == NULL) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000205 args.GetIsolate()->ThrowException(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000206 v8::String::NewFromUtf8(args.GetIsolate(), "Error loading file",
207 v8::NewStringType::kNormal).ToLocalChecked());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000208 return;
Steve Blocka7e24c12009-10-30 11:49:00 +0000209 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000210 v8::Local<v8::String> source;
211 if (!ReadFile(args.GetIsolate(), *file).ToLocal(&source)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000212 args.GetIsolate()->ThrowException(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000213 v8::String::NewFromUtf8(args.GetIsolate(), "Error loading file",
214 v8::NewStringType::kNormal).ToLocalChecked());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000215 return;
Steve Blocka7e24c12009-10-30 11:49:00 +0000216 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000217 if (!ExecuteString(args.GetIsolate(), source, args[i], false, false)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000218 args.GetIsolate()->ThrowException(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000219 v8::String::NewFromUtf8(args.GetIsolate(), "Error executing file",
220 v8::NewStringType::kNormal).ToLocalChecked());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000221 return;
Steve Blocka7e24c12009-10-30 11:49:00 +0000222 }
223 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000224}
225
226
227// The callback that is invoked by v8 whenever the JavaScript 'quit'
228// function is called. Quits.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000229void Quit(const v8::FunctionCallbackInfo<v8::Value>& args) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000230 // If not arguments are given args[0] will yield undefined which
231 // converts to the integer value 0.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000232 int exit_code =
233 args[0]->Int32Value(args.GetIsolate()->GetCurrentContext()).FromMaybe(0);
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000234 fflush(stdout);
235 fflush(stderr);
236 exit(exit_code);
Steve Blocka7e24c12009-10-30 11:49:00 +0000237}
238
239
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000240void Version(const v8::FunctionCallbackInfo<v8::Value>& args) {
241 args.GetReturnValue().Set(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000242 v8::String::NewFromUtf8(args.GetIsolate(), v8::V8::GetVersion(),
243 v8::NewStringType::kNormal).ToLocalChecked());
Steve Blocka7e24c12009-10-30 11:49:00 +0000244}
245
246
247// Reads a file into a v8 string.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000248v8::MaybeLocal<v8::String> ReadFile(v8::Isolate* isolate, const char* name) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000249 FILE* file = fopen(name, "rb");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000250 if (file == NULL) return v8::MaybeLocal<v8::String>();
Steve Blocka7e24c12009-10-30 11:49:00 +0000251
252 fseek(file, 0, SEEK_END);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000253 size_t size = ftell(file);
Steve Blocka7e24c12009-10-30 11:49:00 +0000254 rewind(file);
255
256 char* chars = new char[size + 1];
257 chars[size] = '\0';
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000258 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 Blocka7e24c12009-10-30 11:49:00 +0000264 }
265 fclose(file);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000266 v8::MaybeLocal<v8::String> result = v8::String::NewFromUtf8(
267 isolate, chars, v8::NewStringType::kNormal, static_cast<int>(size));
Steve Blocka7e24c12009-10-30 11:49:00 +0000268 delete[] chars;
269 return result;
270}
271
272
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000273// Process remaining command line arguments and execute files
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000274int RunMain(v8::Isolate* isolate, v8::Platform* platform, int argc,
275 char* argv[]) {
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000276 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 Murdochb8a8cc12014-11-26 15:28:44 +0000285 fprintf(stderr,
286 "Warning: unknown flag %s.\nTry --help for options\n", str);
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000287 } else if (strcmp(str, "-e") == 0 && i + 1 < argc) {
288 // Execute argument given to -e option directly.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000289 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 Murdoch69a99ed2011-11-30 16:03:39 +0000301 } else {
302 // Use all other arguments as names of files to load and run.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000303 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 Murdochb8a8cc12014-11-26 15:28:44 +0000308 fprintf(stderr, "Error reading '%s'\n", str);
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000309 continue;
310 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000311 bool success = ExecuteString(isolate, source, file_name, false, true);
312 while (v8::platform::PumpMessageLoop(platform, isolate)) continue;
313 if (!success) return 1;
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000314 }
315 }
316 return 0;
317}
318
319
Steve Blocka7e24c12009-10-30 11:49:00 +0000320// The read-eval-execute loop of the shell.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000321void RunShell(v8::Local<v8::Context> context, v8::Platform* platform) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000322 fprintf(stderr, "V8 version %s [sample shell]\n", v8::V8::GetVersion());
Steve Blocka7e24c12009-10-30 11:49:00 +0000323 static const int kBufferSize = 256;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100324 // Enter the execution environment before evaluating any code.
325 v8::Context::Scope context_scope(context);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000326 v8::Local<v8::String> name(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000327 v8::String::NewFromUtf8(context->GetIsolate(), "(shell)",
328 v8::NewStringType::kNormal).ToLocalChecked());
Steve Blocka7e24c12009-10-30 11:49:00 +0000329 while (true) {
330 char buffer[kBufferSize];
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000331 fprintf(stderr, "> ");
Steve Blocka7e24c12009-10-30 11:49:00 +0000332 char* str = fgets(buffer, kBufferSize, stdin);
333 if (str == NULL) break;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000334 v8::HandleScope handle_scope(context->GetIsolate());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000335 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 Blocka7e24c12009-10-30 11:49:00 +0000342 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000343 fprintf(stderr, "\n");
Steve Blocka7e24c12009-10-30 11:49:00 +0000344}
345
346
347// Executes a string within the current v8 context.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000348bool ExecuteString(v8::Isolate* isolate, v8::Local<v8::String> source,
349 v8::Local<v8::Value> name, bool print_result,
Steve Blocka7e24c12009-10-30 11:49:00 +0000350 bool report_exceptions) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000351 v8::HandleScope handle_scope(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000352 v8::TryCatch try_catch(isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000353 v8::ScriptOrigin origin(name);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000354 v8::Local<v8::Context> context(isolate->GetCurrentContext());
355 v8::Local<v8::Script> script;
356 if (!v8::Script::Compile(context, source, &origin).ToLocal(&script)) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000357 // Print errors that happened during compilation.
358 if (report_exceptions)
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000359 ReportException(isolate, &try_catch);
Steve Blocka7e24c12009-10-30 11:49:00 +0000360 return false;
361 } else {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000362 v8::Local<v8::Value> result;
363 if (!script->Run(context).ToLocal(&result)) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100364 assert(try_catch.HasCaught());
Steve Blocka7e24c12009-10-30 11:49:00 +0000365 // Print errors that happened during execution.
366 if (report_exceptions)
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000367 ReportException(isolate, &try_catch);
Steve Blocka7e24c12009-10-30 11:49:00 +0000368 return false;
369 } else {
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100370 assert(!try_catch.HasCaught());
Steve Blocka7e24c12009-10-30 11:49:00 +0000371 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 Murdochb8a8cc12014-11-26 15:28:44 +0000384void ReportException(v8::Isolate* isolate, v8::TryCatch* try_catch) {
385 v8::HandleScope handle_scope(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000386 v8::String::Utf8Value exception(try_catch->Exception());
387 const char* exception_string = ToCString(exception);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000388 v8::Local<v8::Message> message = try_catch->Message();
Steve Blocka7e24c12009-10-30 11:49:00 +0000389 if (message.IsEmpty()) {
390 // V8 didn't provide any extra information about this error; just
391 // print the exception.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000392 fprintf(stderr, "%s\n", exception_string);
Steve Blocka7e24c12009-10-30 11:49:00 +0000393 } else {
394 // Print (filename):(line number): (message).
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000395 v8::String::Utf8Value filename(message->GetScriptOrigin().ResourceName());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000396 v8::Local<v8::Context> context(isolate->GetCurrentContext());
Steve Blocka7e24c12009-10-30 11:49:00 +0000397 const char* filename_string = ToCString(filename);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000398 int linenum = message->GetLineNumber(context).FromJust();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000399 fprintf(stderr, "%s:%i: %s\n", filename_string, linenum, exception_string);
Steve Blocka7e24c12009-10-30 11:49:00 +0000400 // Print line of source code.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000401 v8::String::Utf8Value sourceline(
402 message->GetSourceLine(context).ToLocalChecked());
Steve Blocka7e24c12009-10-30 11:49:00 +0000403 const char* sourceline_string = ToCString(sourceline);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000404 fprintf(stderr, "%s\n", sourceline_string);
Steve Blocka7e24c12009-10-30 11:49:00 +0000405 // Print wavy underline (GetUnderline is deprecated).
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000406 int start = message->GetStartColumn(context).FromJust();
Steve Blocka7e24c12009-10-30 11:49:00 +0000407 for (int i = 0; i < start; i++) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000408 fprintf(stderr, " ");
Steve Blocka7e24c12009-10-30 11:49:00 +0000409 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000410 int end = message->GetEndColumn(context).FromJust();
Steve Blocka7e24c12009-10-30 11:49:00 +0000411 for (int i = start; i < end; i++) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000412 fprintf(stderr, "^");
Steve Blocka7e24c12009-10-30 11:49:00 +0000413 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000414 fprintf(stderr, "\n");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000415 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 Monsen25f61362010-05-21 11:50:48 +0100420 const char* stack_trace_string = ToCString(stack_trace);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000421 fprintf(stderr, "%s\n", stack_trace_string);
Kristian Monsen25f61362010-05-21 11:50:48 +0100422 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000423 }
424}