blob: 2ce31b47c00720c441a67115d60abbbe9f6d6ff8 [file] [log] [blame]
erik.corry@gmail.comed49e962012-04-17 11:57:53 +00001// Copyright 2012 the V8 project authors. All rights reserved.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +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
ulan@chromium.org57ff8812013-05-10 08:16:55 +000028// TODO(dcarney): remove
29#define V8_ALLOW_ACCESS_TO_RAW_HANDLE_CONSTRUCTOR
30#define V8_ALLOW_ACCESS_TO_PERSISTENT_IMPLICIT
31
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000032#include <v8.h>
ager@chromium.org357bf652010-04-12 11:30:10 +000033
yangguo@chromium.org99aa4902012-07-06 16:21:55 +000034#ifdef ENABLE_DEBUGGER_SUPPORT
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000035#include <v8-debug.h>
yangguo@chromium.org99aa4902012-07-06 16:21:55 +000036#endif // ENABLE_DEBUGGER_SUPPORT
ager@chromium.org357bf652010-04-12 11:30:10 +000037
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000038#include <fcntl.h>
39#include <string.h>
40#include <stdio.h>
41#include <stdlib.h>
42
43/**
44 * This sample program should demonstrate certain aspects of debugging
45 * standalone V8-based application.
46 *
47 * The program reads input stream, processes it line by line and print
48 * the result to output. The actual processing is done by custom JavaScript
49 * script. The script is specified with command line parameters.
50 *
51 * The main cycle of the program will sequentially read lines from standard
52 * input, process them and print to standard output until input closes.
53 * There are 2 possible configuration in regard to main cycle.
54 *
55 * 1. The main cycle is on C++ side. Program should be run with
56 * --main-cycle-in-cpp option. Script must declare a function named
57 * "ProcessLine". The main cycle in C++ reads lines and calls this function
58 * for processing every time. This is a sample script:
59
60function ProcessLine(input_line) {
61 return ">>>" + input_line + "<<<";
62}
63
64 *
65 * 2. The main cycle is in JavaScript. Program should be run with
66 * --main-cycle-in-js option. Script gets run one time at all and gets
67 * API of 2 global functions: "read_line" and "print". It should read input
68 * and print converted lines to output itself. This a sample script:
69
70while (true) {
71 var line = read_line();
72 if (!line) {
73 break;
74 }
75 var res = line + " | " + line;
76 print(res);
77}
78
79 *
80 * When run with "-p" argument, the program starts V8 Debugger Agent and
81 * allows remote debugger to attach and debug JavaScript code.
82 *
83 * Interesting aspects:
84 * 1. Wait for remote debugger to attach
85 * Normally the program compiles custom script and immediately runs it.
86 * If programmer needs to debug script from the very beginning, he should
87 * run this sample program with "--wait-for-connection" command line parameter.
88 * This way V8 will suspend on the first statement and wait for
89 * debugger to attach.
90 *
91 * 2. Unresponsive V8
92 * V8 Debugger Agent holds a connection with remote debugger, but it does
93 * respond only when V8 is running some script. In particular, when this program
94 * is waiting for input, all requests from debugger get deferred until V8
95 * is called again. See how "--callback" command-line parameter in this sample
96 * fixes this issue.
97 */
98
99enum MainCycleType {
100 CycleInCpp,
101 CycleInJs
102};
103
104const char* ToCString(const v8::String::Utf8Value& value);
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +0000105void ReportException(v8::Isolate* isolate, v8::TryCatch* handler);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000106v8::Handle<v8::String> ReadFile(const char* name);
107v8::Handle<v8::String> ReadLine();
108
109v8::Handle<v8::Value> Print(const v8::Arguments& args);
110v8::Handle<v8::Value> ReadLine(const v8::Arguments& args);
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +0000111bool RunCppCycle(v8::Handle<v8::Script> script,
112 v8::Local<v8::Context> context,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000113 bool report_exceptions);
114
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000115
yangguo@chromium.org99aa4902012-07-06 16:21:55 +0000116#ifdef ENABLE_DEBUGGER_SUPPORT
ager@chromium.org357bf652010-04-12 11:30:10 +0000117v8::Persistent<v8::Context> debug_message_context;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000118
119void DispatchDebugMessages() {
120 // We are in some random thread. We should already have v8::Locker acquired
121 // (we requested this when registered this callback). We was called
122 // because new debug messages arrived; they may have already been processed,
123 // but we shouldn't worry about this.
124 //
125 // All we have to do is to set context and call ProcessDebugMessages.
126 //
127 // We should decide which V8 context to use here. This is important for
128 // "evaluate" command, because it must be executed some context.
129 // In our sample we have only one context, so there is nothing really to
130 // think about.
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000131 v8::Isolate* isolate = v8::Isolate::GetCurrent();
132 v8::HandleScope handle_scope(isolate);
133 v8::Context::Scope scope(isolate, debug_message_context);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000134
135 v8::Debug::ProcessDebugMessages();
136}
yangguo@chromium.org99aa4902012-07-06 16:21:55 +0000137#endif // ENABLE_DEBUGGER_SUPPORT
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000138
139
140int RunMain(int argc, char* argv[]) {
141 v8::V8::SetFlagsFromCommandLine(&argc, argv, true);
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +0000142 v8::Isolate* isolate = v8::Isolate::GetCurrent();
143 v8::HandleScope handle_scope(isolate);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000144
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000145 v8::Handle<v8::String> script_source;
146 v8::Handle<v8::Value> script_name;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000147 int script_param_counter = 0;
148
yangguo@chromium.org99aa4902012-07-06 16:21:55 +0000149#ifdef ENABLE_DEBUGGER_SUPPORT
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000150 int port_number = -1;
151 bool wait_for_connection = false;
152 bool support_callback = false;
yangguo@chromium.org99aa4902012-07-06 16:21:55 +0000153#endif // ENABLE_DEBUGGER_SUPPORT
ager@chromium.org357bf652010-04-12 11:30:10 +0000154
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000155 MainCycleType cycle_type = CycleInCpp;
156
157 for (int i = 1; i < argc; i++) {
158 const char* str = argv[i];
159 if (strcmp(str, "-f") == 0) {
160 // Ignore any -f flags for compatibility with the other stand-
161 // alone JavaScript engines.
162 continue;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000163 } else if (strcmp(str, "--main-cycle-in-cpp") == 0) {
164 cycle_type = CycleInCpp;
165 } else if (strcmp(str, "--main-cycle-in-js") == 0) {
166 cycle_type = CycleInJs;
yangguo@chromium.org99aa4902012-07-06 16:21:55 +0000167#ifdef ENABLE_DEBUGGER_SUPPORT
ager@chromium.org357bf652010-04-12 11:30:10 +0000168 } else if (strcmp(str, "--callback") == 0) {
169 support_callback = true;
170 } else if (strcmp(str, "--wait-for-connection") == 0) {
171 wait_for_connection = true;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000172 } else if (strcmp(str, "-p") == 0 && i + 1 < argc) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000173 port_number = atoi(argv[i + 1]); // NOLINT
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000174 i++;
yangguo@chromium.org99aa4902012-07-06 16:21:55 +0000175#endif // ENABLE_DEBUGGER_SUPPORT
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000176 } else if (strncmp(str, "--", 2) == 0) {
177 printf("Warning: unknown flag %s.\nTry --help for options\n", str);
178 } else if (strcmp(str, "-e") == 0 && i + 1 < argc) {
179 script_source = v8::String::New(argv[i + 1]);
180 script_name = v8::String::New("unnamed");
181 i++;
182 script_param_counter++;
183 } else {
184 // Use argument as a name of file to load.
185 script_source = ReadFile(str);
186 script_name = v8::String::New(str);
187 if (script_source.IsEmpty()) {
188 printf("Error reading '%s'\n", str);
189 return 1;
190 }
191 script_param_counter++;
192 }
193 }
194
195 if (script_param_counter == 0) {
196 printf("Script is not specified\n");
197 return 1;
198 }
199 if (script_param_counter != 1) {
200 printf("Only one script may be specified\n");
201 return 1;
202 }
203
204 // Create a template for the global object.
205 v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New();
206
207 // Bind the global 'print' function to the C++ Print callback.
208 global->Set(v8::String::New("print"), v8::FunctionTemplate::New(Print));
209
210 if (cycle_type == CycleInJs) {
211 // Bind the global 'read_line' function to the C++ Print callback.
212 global->Set(v8::String::New("read_line"),
213 v8::FunctionTemplate::New(ReadLine));
214 }
215
216 // Create a new execution environment containing the built-in
217 // functions
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000218 v8::Handle<v8::Context> context = v8::Context::New(isolate, NULL, global);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000219 // Enter the newly created execution environment.
220 v8::Context::Scope context_scope(context);
221
yangguo@chromium.org99aa4902012-07-06 16:21:55 +0000222#ifdef ENABLE_DEBUGGER_SUPPORT
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000223 debug_message_context =
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +0000224 v8::Persistent<v8::Context>::New(isolate, context);
ager@chromium.org357bf652010-04-12 11:30:10 +0000225
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +0000226 v8::Locker locker(isolate);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000227
228 if (support_callback) {
229 v8::Debug::SetDebugMessageDispatchHandler(DispatchDebugMessages, true);
230 }
231
232 if (port_number != -1) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000233 v8::Debug::EnableAgent("lineprocessor", port_number, wait_for_connection);
234 }
yangguo@chromium.org99aa4902012-07-06 16:21:55 +0000235#endif // ENABLE_DEBUGGER_SUPPORT
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000236
237 bool report_exceptions = true;
238
239 v8::Handle<v8::Script> script;
240 {
241 // Compile script in try/catch context.
242 v8::TryCatch try_catch;
243 script = v8::Script::Compile(script_source, script_name);
244 if (script.IsEmpty()) {
245 // Print errors that happened during compilation.
246 if (report_exceptions)
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +0000247 ReportException(isolate, &try_catch);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000248 return 1;
249 }
250 }
251
252 {
253 v8::TryCatch try_catch;
254
255 script->Run();
256 if (try_catch.HasCaught()) {
257 if (report_exceptions)
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +0000258 ReportException(isolate, &try_catch);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000259 return 1;
260 }
261 }
262
263 if (cycle_type == CycleInCpp) {
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +0000264 bool res = RunCppCycle(script,
265 v8::Context::GetCurrent(),
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000266 report_exceptions);
267 return !res;
268 } else {
269 // All is already done.
270 }
271 return 0;
272}
273
274
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +0000275bool RunCppCycle(v8::Handle<v8::Script> script,
276 v8::Local<v8::Context> context,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000277 bool report_exceptions) {
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +0000278 v8::Isolate* isolate = context->GetIsolate();
yangguo@chromium.org99aa4902012-07-06 16:21:55 +0000279#ifdef ENABLE_DEBUGGER_SUPPORT
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +0000280 v8::Locker lock(isolate);
yangguo@chromium.org99aa4902012-07-06 16:21:55 +0000281#endif // ENABLE_DEBUGGER_SUPPORT
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000282
283 v8::Handle<v8::String> fun_name = v8::String::New("ProcessLine");
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +0000284 v8::Handle<v8::Value> process_val = context->Global()->Get(fun_name);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000285
286 // If there is no Process function, or if it is not a function,
287 // bail out
288 if (!process_val->IsFunction()) {
289 printf("Error: Script does not declare 'ProcessLine' global function.\n");
290 return 1;
291 }
292
293 // It is a function; cast it to a Function
294 v8::Handle<v8::Function> process_fun =
295 v8::Handle<v8::Function>::Cast(process_val);
296
297
298 while (!feof(stdin)) {
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +0000299 v8::HandleScope handle_scope(isolate);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000300
301 v8::Handle<v8::String> input_line = ReadLine();
302 if (input_line == v8::Undefined()) {
303 continue;
304 }
305
306 const int argc = 1;
307 v8::Handle<v8::Value> argv[argc] = { input_line };
308
309 v8::Handle<v8::Value> result;
310 {
311 v8::TryCatch try_catch;
312 result = process_fun->Call(v8::Context::GetCurrent()->Global(),
313 argc, argv);
314 if (try_catch.HasCaught()) {
315 if (report_exceptions)
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +0000316 ReportException(isolate, &try_catch);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000317 return false;
318 }
319 }
320 v8::String::Utf8Value str(result);
321 const char* cstr = ToCString(str);
322 printf("%s\n", cstr);
323 }
324
325 return true;
326}
327
328int main(int argc, char* argv[]) {
329 int result = RunMain(argc, argv);
330 v8::V8::Dispose();
331 return result;
332}
333
334
335// Extracts a C string from a V8 Utf8Value.
336const char* ToCString(const v8::String::Utf8Value& value) {
337 return *value ? *value : "<string conversion failed>";
338}
339
340
341// Reads a file into a v8 string.
342v8::Handle<v8::String> ReadFile(const char* name) {
343 FILE* file = fopen(name, "rb");
344 if (file == NULL) return v8::Handle<v8::String>();
345
346 fseek(file, 0, SEEK_END);
347 int size = ftell(file);
348 rewind(file);
349
350 char* chars = new char[size + 1];
351 chars[size] = '\0';
352 for (int i = 0; i < size;) {
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000353 int read = static_cast<int>(fread(&chars[i], 1, size - i, file));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000354 i += read;
355 }
356 fclose(file);
357 v8::Handle<v8::String> result = v8::String::New(chars, size);
358 delete[] chars;
359 return result;
360}
361
362
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +0000363void ReportException(v8::Isolate* isolate, v8::TryCatch* try_catch) {
364 v8::HandleScope handle_scope(isolate);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000365 v8::String::Utf8Value exception(try_catch->Exception());
366 const char* exception_string = ToCString(exception);
367 v8::Handle<v8::Message> message = try_catch->Message();
368 if (message.IsEmpty()) {
369 // V8 didn't provide any extra information about this error; just
370 // print the exception.
371 printf("%s\n", exception_string);
372 } else {
373 // Print (filename):(line number): (message).
374 v8::String::Utf8Value filename(message->GetScriptResourceName());
375 const char* filename_string = ToCString(filename);
376 int linenum = message->GetLineNumber();
377 printf("%s:%i: %s\n", filename_string, linenum, exception_string);
378 // Print line of source code.
379 v8::String::Utf8Value sourceline(message->GetSourceLine());
380 const char* sourceline_string = ToCString(sourceline);
381 printf("%s\n", sourceline_string);
382 // Print wavy underline (GetUnderline is deprecated).
383 int start = message->GetStartColumn();
384 for (int i = 0; i < start; i++) {
385 printf(" ");
386 }
387 int end = message->GetEndColumn();
388 for (int i = start; i < end; i++) {
389 printf("^");
390 }
391 printf("\n");
392 }
393}
394
395
396// The callback that is invoked by v8 whenever the JavaScript 'print'
397// function is called. Prints its arguments on stdout separated by
398// spaces and ending with a newline.
399v8::Handle<v8::Value> Print(const v8::Arguments& args) {
400 bool first = true;
401 for (int i = 0; i < args.Length(); i++) {
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +0000402 v8::HandleScope handle_scope(args.GetIsolate());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000403 if (first) {
404 first = false;
405 } else {
406 printf(" ");
407 }
408 v8::String::Utf8Value str(args[i]);
409 const char* cstr = ToCString(str);
410 printf("%s", cstr);
411 }
412 printf("\n");
413 fflush(stdout);
414 return v8::Undefined();
415}
416
417
418// The callback that is invoked by v8 whenever the JavaScript 'read_line'
419// function is called. Reads a string from standard input and returns.
420v8::Handle<v8::Value> ReadLine(const v8::Arguments& args) {
421 if (args.Length() > 0) {
422 return v8::ThrowException(v8::String::New("Unexpected arguments"));
423 }
424 return ReadLine();
425}
426
427v8::Handle<v8::String> ReadLine() {
428 const int kBufferSize = 1024 + 1;
429 char buffer[kBufferSize];
430
431 char* res;
432 {
yangguo@chromium.org99aa4902012-07-06 16:21:55 +0000433#ifdef ENABLE_DEBUGGER_SUPPORT
yangguo@chromium.org46a2a512013-01-18 16:29:40 +0000434 v8::Unlocker unlocker(v8::Isolate::GetCurrent());
yangguo@chromium.org99aa4902012-07-06 16:21:55 +0000435#endif // ENABLE_DEBUGGER_SUPPORT
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000436 res = fgets(buffer, kBufferSize, stdin);
437 }
438 if (res == NULL) {
439 v8::Handle<v8::Primitive> t = v8::Undefined();
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000440 return v8::Handle<v8::String>(v8::String::Cast(*t));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000441 }
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000442 // Remove newline char
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000443 for (char* pos = buffer; *pos != '\0'; pos++) {
444 if (*pos == '\n') {
445 *pos = '\0';
446 break;
447 }
448 }
449 return v8::String::New(buffer);
450}