OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
102 | 102 |
103 void Print(const v8::FunctionCallbackInfo<v8::Value>& args); | 103 void Print(const v8::FunctionCallbackInfo<v8::Value>& args); |
104 void ReadLine(const v8::FunctionCallbackInfo<v8::Value>& args); | 104 void ReadLine(const v8::FunctionCallbackInfo<v8::Value>& args); |
105 bool RunCppCycle(v8::Handle<v8::Script> script, | 105 bool RunCppCycle(v8::Handle<v8::Script> script, |
106 v8::Local<v8::Context> context, | 106 v8::Local<v8::Context> context, |
107 bool report_exceptions); | 107 bool report_exceptions); |
108 | 108 |
109 | 109 |
110 v8::Persistent<v8::Context> debug_message_context; | 110 v8::Persistent<v8::Context> debug_message_context; |
111 | 111 |
112 void DispatchDebugMessages() { | |
113 // We are in some random thread. We should already have v8::Locker acquired | |
114 // (we requested this when registered this callback). We was called | |
115 // because new debug messages arrived; they may have already been processed, | |
116 // but we shouldn't worry about this. | |
117 // | |
118 // All we have to do is to set context and call ProcessDebugMessages. | |
119 // | |
120 // We should decide which V8 context to use here. This is important for | |
121 // "evaluate" command, because it must be executed some context. | |
122 // In our sample we have only one context, so there is nothing really to | |
123 // think about. | |
124 v8::Isolate* isolate = v8::Isolate::GetCurrent(); | |
125 v8::HandleScope handle_scope(isolate); | |
126 v8::Local<v8::Context> context = | |
127 v8::Local<v8::Context>::New(isolate, debug_message_context); | |
128 v8::Context::Scope scope(context); | |
129 | |
130 v8::Debug::ProcessDebugMessages(); | |
131 } | |
132 | |
133 | |
134 int RunMain(int argc, char* argv[]) { | 112 int RunMain(int argc, char* argv[]) { |
135 v8::V8::SetFlagsFromCommandLine(&argc, argv, true); | 113 v8::V8::SetFlagsFromCommandLine(&argc, argv, true); |
136 v8::Isolate* isolate = v8::Isolate::New(); | 114 v8::Isolate* isolate = v8::Isolate::New(); |
137 v8::Isolate::Scope isolate_scope(isolate); | 115 v8::Isolate::Scope isolate_scope(isolate); |
138 v8::Locker locker(isolate); | 116 v8::Locker locker(isolate); |
139 v8::HandleScope handle_scope(isolate); | 117 v8::HandleScope handle_scope(isolate); |
140 | 118 |
141 v8::Handle<v8::String> script_source; | 119 v8::Handle<v8::String> script_source; |
142 v8::Handle<v8::Value> script_name; | 120 v8::Handle<v8::Value> script_name; |
143 int script_param_counter = 0; | 121 int script_param_counter = 0; |
144 | 122 |
145 int port_number = -1; | |
146 bool wait_for_connection = false; | |
147 bool support_callback = false; | |
148 | |
149 MainCycleType cycle_type = CycleInCpp; | 123 MainCycleType cycle_type = CycleInCpp; |
150 | 124 |
151 for (int i = 1; i < argc; i++) { | 125 for (int i = 1; i < argc; i++) { |
152 const char* str = argv[i]; | 126 const char* str = argv[i]; |
153 if (strcmp(str, "-f") == 0) { | 127 if (strcmp(str, "-f") == 0) { |
154 // Ignore any -f flags for compatibility with the other stand- | 128 // Ignore any -f flags for compatibility with the other stand- |
155 // alone JavaScript engines. | 129 // alone JavaScript engines. |
156 continue; | 130 continue; |
157 } else if (strcmp(str, "--main-cycle-in-cpp") == 0) { | 131 } else if (strcmp(str, "--main-cycle-in-cpp") == 0) { |
158 cycle_type = CycleInCpp; | 132 cycle_type = CycleInCpp; |
159 } else if (strcmp(str, "--main-cycle-in-js") == 0) { | 133 } else if (strcmp(str, "--main-cycle-in-js") == 0) { |
160 cycle_type = CycleInJs; | 134 cycle_type = CycleInJs; |
161 } else if (strcmp(str, "--callback") == 0) { | |
162 support_callback = true; | |
163 } else if (strcmp(str, "--wait-for-connection") == 0) { | |
164 wait_for_connection = true; | |
165 } else if (strcmp(str, "-p") == 0 && i + 1 < argc) { | |
166 port_number = atoi(argv[i + 1]); // NOLINT | |
167 i++; | |
168 } else if (strncmp(str, "--", 2) == 0) { | 135 } else if (strncmp(str, "--", 2) == 0) { |
169 printf("Warning: unknown flag %s.\nTry --help for options\n", str); | 136 printf("Warning: unknown flag %s.\nTry --help for options\n", str); |
170 } else if (strcmp(str, "-e") == 0 && i + 1 < argc) { | 137 } else if (strcmp(str, "-e") == 0 && i + 1 < argc) { |
171 script_source = v8::String::NewFromUtf8(isolate, argv[i + 1]); | 138 script_source = v8::String::NewFromUtf8(isolate, argv[i + 1]); |
172 script_name = v8::String::NewFromUtf8(isolate, "unnamed"); | 139 script_name = v8::String::NewFromUtf8(isolate, "unnamed"); |
173 i++; | 140 i++; |
174 script_param_counter++; | 141 script_param_counter++; |
175 } else { | 142 } else { |
176 // Use argument as a name of file to load. | 143 // Use argument as a name of file to load. |
177 script_source = ReadFile(isolate, str); | 144 script_source = ReadFile(isolate, str); |
(...skipping 29 matching lines...) Expand all Loading... |
207 } | 174 } |
208 | 175 |
209 // Create a new execution environment containing the built-in | 176 // Create a new execution environment containing the built-in |
210 // functions | 177 // functions |
211 v8::Handle<v8::Context> context = v8::Context::New(isolate, NULL, global); | 178 v8::Handle<v8::Context> context = v8::Context::New(isolate, NULL, global); |
212 // Enter the newly created execution environment. | 179 // Enter the newly created execution environment. |
213 v8::Context::Scope context_scope(context); | 180 v8::Context::Scope context_scope(context); |
214 | 181 |
215 debug_message_context.Reset(isolate, context); | 182 debug_message_context.Reset(isolate, context); |
216 | 183 |
217 if (support_callback) { | |
218 v8::Debug::SetDebugMessageDispatchHandler(DispatchDebugMessages, true); | |
219 } | |
220 | |
221 if (port_number != -1) { | |
222 v8::Debug::EnableAgent("lineprocessor", port_number, wait_for_connection); | |
223 } | |
224 | |
225 bool report_exceptions = true; | 184 bool report_exceptions = true; |
226 | 185 |
227 v8::Handle<v8::Script> script; | 186 v8::Handle<v8::Script> script; |
228 { | 187 { |
229 // Compile script in try/catch context. | 188 // Compile script in try/catch context. |
230 v8::TryCatch try_catch; | 189 v8::TryCatch try_catch; |
231 v8::ScriptOrigin origin(script_name); | 190 v8::ScriptOrigin origin(script_name); |
232 script = v8::Script::Compile(script_source, &origin); | 191 script = v8::Script::Compile(script_source, &origin); |
233 if (script.IsEmpty()) { | 192 if (script.IsEmpty()) { |
234 // Print errors that happened during compilation. | 193 // Print errors that happened during compilation. |
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
433 } | 392 } |
434 // Remove newline char | 393 // Remove newline char |
435 for (char* pos = buffer; *pos != '\0'; pos++) { | 394 for (char* pos = buffer; *pos != '\0'; pos++) { |
436 if (*pos == '\n') { | 395 if (*pos == '\n') { |
437 *pos = '\0'; | 396 *pos = '\0'; |
438 break; | 397 break; |
439 } | 398 } |
440 } | 399 } |
441 return v8::String::NewFromUtf8(isolate, buffer); | 400 return v8::String::NewFromUtf8(isolate, buffer); |
442 } | 401 } |
OLD | NEW |