OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 | 5 |
6 // Defined when linking against shared lib on Windows. | 6 // Defined when linking against shared lib on Windows. |
7 #if defined(USING_V8_SHARED) && !defined(V8_SHARED) | 7 #if defined(USING_V8_SHARED) && !defined(V8_SHARED) |
8 #define V8_SHARED | 8 #define V8_SHARED |
9 #endif | 9 #endif |
10 | 10 |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
101 return length > 10 * MB ? malloc(1) : malloc(length); | 101 return length > 10 * MB ? malloc(1) : malloc(length); |
102 } | 102 } |
103 void Free(void* p, size_t) override { free(p); } | 103 void Free(void* p, size_t) override { free(p); } |
104 }; | 104 }; |
105 | 105 |
106 | 106 |
107 v8::Platform* g_platform = NULL; | 107 v8::Platform* g_platform = NULL; |
108 | 108 |
109 | 109 |
110 #ifndef V8_SHARED | 110 #ifndef V8_SHARED |
111 bool FindInObjectList(Local<Object> object, const Shell::ObjectList& list) { | 111 bool FindInObjectList(Handle<Object> object, const Shell::ObjectList& list) { |
112 for (int i = 0; i < list.length(); ++i) { | 112 for (int i = 0; i < list.length(); ++i) { |
113 if (list[i]->StrictEquals(object)) { | 113 if (list[i]->StrictEquals(object)) { |
114 return true; | 114 return true; |
115 } | 115 } |
116 } | 116 } |
117 return false; | 117 return false; |
118 } | 118 } |
119 #endif // !V8_SHARED | 119 #endif // !V8_SHARED |
120 | 120 |
121 | 121 |
122 } // namespace | 122 } // namespace |
123 | 123 |
124 | 124 |
125 static Local<Value> Throw(Isolate* isolate, const char* message) { | 125 static Handle<Value> Throw(Isolate* isolate, const char* message) { |
126 return isolate->ThrowException( | 126 return isolate->ThrowException(String::NewFromUtf8(isolate, message)); |
127 String::NewFromUtf8(isolate, message, NewStringType::kNormal) | |
128 .ToLocalChecked()); | |
129 } | 127 } |
130 | 128 |
131 | 129 |
132 | 130 |
133 class PerIsolateData { | 131 class PerIsolateData { |
134 public: | 132 public: |
135 explicit PerIsolateData(Isolate* isolate) : isolate_(isolate), realms_(NULL) { | 133 explicit PerIsolateData(Isolate* isolate) : isolate_(isolate), realms_(NULL) { |
136 HandleScope scope(isolate); | 134 HandleScope scope(isolate); |
137 isolate->SetData(0, this); | 135 isolate->SetData(0, this); |
138 } | 136 } |
(...skipping 14 matching lines...) Expand all Loading... |
153 PerIsolateData* data_; | 151 PerIsolateData* data_; |
154 }; | 152 }; |
155 | 153 |
156 private: | 154 private: |
157 friend class Shell; | 155 friend class Shell; |
158 friend class RealmScope; | 156 friend class RealmScope; |
159 Isolate* isolate_; | 157 Isolate* isolate_; |
160 int realm_count_; | 158 int realm_count_; |
161 int realm_current_; | 159 int realm_current_; |
162 int realm_switch_; | 160 int realm_switch_; |
163 Global<Context>* realms_; | 161 Persistent<Context>* realms_; |
164 Global<Value> realm_shared_; | 162 Persistent<Value> realm_shared_; |
165 | 163 |
166 int RealmIndexOrThrow(const v8::FunctionCallbackInfo<v8::Value>& args, | 164 int RealmIndexOrThrow(const v8::FunctionCallbackInfo<v8::Value>& args, |
167 int arg_offset); | 165 int arg_offset); |
168 int RealmFind(Local<Context> context); | 166 int RealmFind(Handle<Context> context); |
169 }; | 167 }; |
170 | 168 |
171 | 169 |
172 LineEditor *LineEditor::current_ = NULL; | 170 LineEditor *LineEditor::current_ = NULL; |
173 | 171 |
174 | 172 |
175 LineEditor::LineEditor(Type type, const char* name) | 173 LineEditor::LineEditor(Type type, const char* name) |
176 : type_(type), name_(name) { | 174 : type_(type), name_(name) { |
177 if (current_ == NULL || current_->type_ < type) current_ = this; | 175 if (current_ == NULL || current_->type_ < type) current_ = this; |
178 } | 176 } |
179 | 177 |
180 | 178 |
181 class DumbLineEditor: public LineEditor { | 179 class DumbLineEditor: public LineEditor { |
182 public: | 180 public: |
183 explicit DumbLineEditor(Isolate* isolate) | 181 explicit DumbLineEditor(Isolate* isolate) |
184 : LineEditor(LineEditor::DUMB, "dumb"), isolate_(isolate) { } | 182 : LineEditor(LineEditor::DUMB, "dumb"), isolate_(isolate) { } |
185 virtual Local<String> Prompt(const char* prompt); | 183 virtual Handle<String> Prompt(const char* prompt); |
186 | |
187 private: | 184 private: |
188 Isolate* isolate_; | 185 Isolate* isolate_; |
189 }; | 186 }; |
190 | 187 |
191 | 188 |
192 Local<String> DumbLineEditor::Prompt(const char* prompt) { | 189 Handle<String> DumbLineEditor::Prompt(const char* prompt) { |
193 printf("%s", prompt); | 190 printf("%s", prompt); |
194 #if defined(__native_client__) | 191 #if defined(__native_client__) |
195 // Native Client libc is used to being embedded in Chrome and | 192 // Native Client libc is used to being embedded in Chrome and |
196 // has trouble recognizing when to flush. | 193 // has trouble recognizing when to flush. |
197 fflush(stdout); | 194 fflush(stdout); |
198 #endif | 195 #endif |
199 return Shell::ReadFromStdin(isolate_); | 196 return Shell::ReadFromStdin(isolate_); |
200 } | 197 } |
201 | 198 |
202 | 199 |
203 #ifndef V8_SHARED | 200 #ifndef V8_SHARED |
204 CounterMap* Shell::counter_map_; | 201 CounterMap* Shell::counter_map_; |
205 base::OS::MemoryMappedFile* Shell::counters_file_ = NULL; | 202 base::OS::MemoryMappedFile* Shell::counters_file_ = NULL; |
206 CounterCollection Shell::local_counters_; | 203 CounterCollection Shell::local_counters_; |
207 CounterCollection* Shell::counters_ = &local_counters_; | 204 CounterCollection* Shell::counters_ = &local_counters_; |
208 base::LazyMutex Shell::context_mutex_; | 205 base::LazyMutex Shell::context_mutex_; |
209 const base::TimeTicks Shell::kInitialTicks = | 206 const base::TimeTicks Shell::kInitialTicks = |
210 base::TimeTicks::HighResolutionNow(); | 207 base::TimeTicks::HighResolutionNow(); |
211 Global<Context> Shell::utility_context_; | 208 Persistent<Context> Shell::utility_context_; |
212 base::LazyMutex Shell::workers_mutex_; | 209 base::LazyMutex Shell::workers_mutex_; |
213 bool Shell::allow_new_workers_ = true; | 210 bool Shell::allow_new_workers_ = true; |
214 i::List<Worker*> Shell::workers_; | 211 i::List<Worker*> Shell::workers_; |
215 i::List<SharedArrayBuffer::Contents> Shell::externalized_shared_contents_; | 212 i::List<SharedArrayBuffer::Contents> Shell::externalized_shared_contents_; |
216 #endif // !V8_SHARED | 213 #endif // !V8_SHARED |
217 | 214 |
218 Global<Context> Shell::evaluation_context_; | 215 Persistent<Context> Shell::evaluation_context_; |
219 ArrayBuffer::Allocator* Shell::array_buffer_allocator; | 216 ArrayBuffer::Allocator* Shell::array_buffer_allocator; |
220 ShellOptions Shell::options; | 217 ShellOptions Shell::options; |
221 const char* Shell::kPrompt = "d8> "; | 218 const char* Shell::kPrompt = "d8> "; |
222 base::OnceType Shell::quit_once_ = V8_ONCE_INIT; | 219 base::OnceType Shell::quit_once_ = V8_ONCE_INIT; |
223 | 220 |
224 #ifndef V8_SHARED | 221 #ifndef V8_SHARED |
225 bool CounterMap::Match(void* key1, void* key2) { | 222 bool CounterMap::Match(void* key1, void* key2) { |
226 const char* name1 = reinterpret_cast<const char*>(key1); | 223 const char* name1 = reinterpret_cast<const char*>(key1); |
227 const char* name2 = reinterpret_cast<const char*>(key2); | 224 const char* name2 = reinterpret_cast<const char*>(key2); |
228 return strcmp(name1, name2) == 0; | 225 return strcmp(name1, name2) == 0; |
(...skipping 22 matching lines...) Expand all Loading... |
251 name_string->Write(name_buffer, 0, name_length); | 248 name_string->Write(name_buffer, 0, name_length); |
252 } | 249 } |
253 Isolate::CreateParams create_params; | 250 Isolate::CreateParams create_params; |
254 create_params.array_buffer_allocator = Shell::array_buffer_allocator; | 251 create_params.array_buffer_allocator = Shell::array_buffer_allocator; |
255 Isolate* temp_isolate = Isolate::New(create_params); | 252 Isolate* temp_isolate = Isolate::New(create_params); |
256 ScriptCompiler::CachedData* result = NULL; | 253 ScriptCompiler::CachedData* result = NULL; |
257 { | 254 { |
258 Isolate::Scope isolate_scope(temp_isolate); | 255 Isolate::Scope isolate_scope(temp_isolate); |
259 HandleScope handle_scope(temp_isolate); | 256 HandleScope handle_scope(temp_isolate); |
260 Context::Scope context_scope(Context::New(temp_isolate)); | 257 Context::Scope context_scope(Context::New(temp_isolate)); |
261 Local<String> source_copy = | 258 Local<String> source_copy = v8::String::NewFromTwoByte( |
262 v8::String::NewFromTwoByte(temp_isolate, source_buffer, | 259 temp_isolate, source_buffer, v8::String::kNormalString, source_length); |
263 v8::NewStringType::kNormal, | |
264 source_length).ToLocalChecked(); | |
265 Local<Value> name_copy; | 260 Local<Value> name_copy; |
266 if (name_buffer) { | 261 if (name_buffer) { |
267 name_copy = v8::String::NewFromTwoByte(temp_isolate, name_buffer, | 262 name_copy = v8::String::NewFromTwoByte( |
268 v8::NewStringType::kNormal, | 263 temp_isolate, name_buffer, v8::String::kNormalString, name_length); |
269 name_length).ToLocalChecked(); | |
270 } else { | 264 } else { |
271 name_copy = v8::Undefined(temp_isolate); | 265 name_copy = v8::Undefined(temp_isolate); |
272 } | 266 } |
273 ScriptCompiler::Source script_source(source_copy, ScriptOrigin(name_copy)); | 267 ScriptCompiler::Source script_source(source_copy, ScriptOrigin(name_copy)); |
274 ScriptCompiler::CompileUnboundScript(temp_isolate, &script_source, | 268 ScriptCompiler::CompileUnbound(temp_isolate, &script_source, |
275 compile_options).ToLocalChecked(); | 269 compile_options); |
276 if (script_source.GetCachedData()) { | 270 if (script_source.GetCachedData()) { |
277 int length = script_source.GetCachedData()->length; | 271 int length = script_source.GetCachedData()->length; |
278 uint8_t* cache = new uint8_t[length]; | 272 uint8_t* cache = new uint8_t[length]; |
279 memcpy(cache, script_source.GetCachedData()->data, length); | 273 memcpy(cache, script_source.GetCachedData()->data, length); |
280 result = new ScriptCompiler::CachedData( | 274 result = new ScriptCompiler::CachedData( |
281 cache, length, ScriptCompiler::CachedData::BufferOwned); | 275 cache, length, ScriptCompiler::CachedData::BufferOwned); |
282 } | 276 } |
283 } | 277 } |
284 temp_isolate->Dispose(); | 278 temp_isolate->Dispose(); |
285 delete[] source_buffer; | 279 delete[] source_buffer; |
286 delete[] name_buffer; | 280 delete[] name_buffer; |
287 return result; | 281 return result; |
288 } | 282 } |
289 | 283 |
290 | 284 |
291 // Compile a string within the current v8 context. | 285 // Compile a string within the current v8 context. |
292 MaybeLocal<Script> Shell::CompileString( | 286 Local<Script> Shell::CompileString( |
293 Isolate* isolate, Local<String> source, Local<Value> name, | 287 Isolate* isolate, Local<String> source, Local<Value> name, |
294 ScriptCompiler::CompileOptions compile_options, SourceType source_type) { | 288 ScriptCompiler::CompileOptions compile_options, SourceType source_type) { |
295 Local<Context> context(isolate->GetCurrentContext()); | |
296 ScriptOrigin origin(name); | 289 ScriptOrigin origin(name); |
297 if (compile_options == ScriptCompiler::kNoCompileOptions) { | 290 if (compile_options == ScriptCompiler::kNoCompileOptions) { |
298 ScriptCompiler::Source script_source(source, origin); | 291 ScriptCompiler::Source script_source(source, origin); |
299 return source_type == SCRIPT | 292 return source_type == SCRIPT |
300 ? ScriptCompiler::Compile(context, &script_source, | 293 ? ScriptCompiler::Compile(isolate, &script_source, |
301 compile_options) | 294 compile_options) |
302 : ScriptCompiler::CompileModule(context, &script_source, | 295 : ScriptCompiler::CompileModule(isolate, &script_source, |
303 compile_options); | 296 compile_options); |
304 } | 297 } |
305 | 298 |
306 ScriptCompiler::CachedData* data = | 299 ScriptCompiler::CachedData* data = |
307 CompileForCachedData(source, name, compile_options); | 300 CompileForCachedData(source, name, compile_options); |
308 ScriptCompiler::Source cached_source(source, origin, data); | 301 ScriptCompiler::Source cached_source(source, origin, data); |
309 if (compile_options == ScriptCompiler::kProduceCodeCache) { | 302 if (compile_options == ScriptCompiler::kProduceCodeCache) { |
310 compile_options = ScriptCompiler::kConsumeCodeCache; | 303 compile_options = ScriptCompiler::kConsumeCodeCache; |
311 } else if (compile_options == ScriptCompiler::kProduceParserCache) { | 304 } else if (compile_options == ScriptCompiler::kProduceParserCache) { |
312 compile_options = ScriptCompiler::kConsumeParserCache; | 305 compile_options = ScriptCompiler::kConsumeParserCache; |
313 } else { | 306 } else { |
314 DCHECK(false); // A new compile option? | 307 DCHECK(false); // A new compile option? |
315 } | 308 } |
316 if (data == NULL) compile_options = ScriptCompiler::kNoCompileOptions; | 309 if (data == NULL) compile_options = ScriptCompiler::kNoCompileOptions; |
317 MaybeLocal<Script> result = | 310 Local<Script> result = |
318 source_type == SCRIPT | 311 source_type == SCRIPT |
319 ? ScriptCompiler::Compile(context, &cached_source, compile_options) | 312 ? ScriptCompiler::Compile(isolate, &cached_source, compile_options) |
320 : ScriptCompiler::CompileModule(context, &cached_source, | 313 : ScriptCompiler::CompileModule(isolate, &cached_source, |
321 compile_options); | 314 compile_options); |
322 CHECK(data == NULL || !data->rejected); | 315 CHECK(data == NULL || !data->rejected); |
323 return result; | 316 return result; |
324 } | 317 } |
325 | 318 |
326 | 319 |
327 // Executes a string within the current v8 context. | 320 // Executes a string within the current v8 context. |
328 bool Shell::ExecuteString(Isolate* isolate, Local<String> source, | 321 bool Shell::ExecuteString(Isolate* isolate, Handle<String> source, |
329 Local<Value> name, bool print_result, | 322 Handle<Value> name, bool print_result, |
330 bool report_exceptions, SourceType source_type) { | 323 bool report_exceptions, SourceType source_type) { |
331 #ifndef V8_SHARED | 324 #ifndef V8_SHARED |
332 bool FLAG_debugger = i::FLAG_debugger; | 325 bool FLAG_debugger = i::FLAG_debugger; |
333 #else | 326 #else |
334 bool FLAG_debugger = false; | 327 bool FLAG_debugger = false; |
335 #endif // !V8_SHARED | 328 #endif // !V8_SHARED |
336 HandleScope handle_scope(isolate); | 329 HandleScope handle_scope(isolate); |
337 TryCatch try_catch(isolate); | 330 TryCatch try_catch(isolate); |
338 options.script_executed = true; | 331 options.script_executed = true; |
339 if (FLAG_debugger) { | 332 if (FLAG_debugger) { |
340 // When debugging make exceptions appear to be uncaught. | 333 // When debugging make exceptions appear to be uncaught. |
341 try_catch.SetVerbose(true); | 334 try_catch.SetVerbose(true); |
342 } | 335 } |
343 | 336 |
344 MaybeLocal<Value> maybe_result; | 337 Handle<Value> result; |
345 { | 338 { |
346 PerIsolateData* data = PerIsolateData::Get(isolate); | 339 PerIsolateData* data = PerIsolateData::Get(isolate); |
347 Local<Context> realm = | 340 Local<Context> realm = |
348 Local<Context>::New(isolate, data->realms_[data->realm_current_]); | 341 Local<Context>::New(isolate, data->realms_[data->realm_current_]); |
349 Context::Scope context_scope(realm); | 342 Context::Scope context_scope(realm); |
350 Local<Script> script; | 343 Handle<Script> script = Shell::CompileString( |
351 if (!Shell::CompileString(isolate, source, name, options.compile_options, | 344 isolate, source, name, options.compile_options, source_type); |
352 source_type).ToLocal(&script)) { | 345 if (script.IsEmpty()) { |
353 // Print errors that happened during compilation. | 346 // Print errors that happened during compilation. |
354 if (report_exceptions && !FLAG_debugger) | 347 if (report_exceptions && !FLAG_debugger) |
355 ReportException(isolate, &try_catch); | 348 ReportException(isolate, &try_catch); |
356 return false; | 349 return false; |
357 } | 350 } |
358 maybe_result = script->Run(realm); | 351 result = script->Run(); |
359 EmptyMessageQueues(isolate); | 352 EmptyMessageQueues(isolate); |
360 data->realm_current_ = data->realm_switch_; | 353 data->realm_current_ = data->realm_switch_; |
361 } | 354 } |
362 Local<Value> result; | 355 if (result.IsEmpty()) { |
363 if (!maybe_result.ToLocal(&result)) { | |
364 DCHECK(try_catch.HasCaught()); | 356 DCHECK(try_catch.HasCaught()); |
365 // Print errors that happened during execution. | 357 // Print errors that happened during execution. |
366 if (report_exceptions && !FLAG_debugger) | 358 if (report_exceptions && !FLAG_debugger) |
367 ReportException(isolate, &try_catch); | 359 ReportException(isolate, &try_catch); |
368 return false; | 360 return false; |
369 } | 361 } |
370 DCHECK(!try_catch.HasCaught()); | 362 DCHECK(!try_catch.HasCaught()); |
371 if (print_result) { | 363 if (print_result) { |
372 #if !defined(V8_SHARED) | 364 #if !defined(V8_SHARED) |
373 if (options.test_shell) { | 365 if (options.test_shell) { |
374 #endif | 366 #endif |
375 if (!result->IsUndefined()) { | 367 if (!result->IsUndefined()) { |
376 // If all went well and the result wasn't undefined then print | 368 // If all went well and the result wasn't undefined then print |
377 // the returned value. | 369 // the returned value. |
378 v8::String::Utf8Value str(result); | 370 v8::String::Utf8Value str(result); |
379 fwrite(*str, sizeof(**str), str.length(), stdout); | 371 fwrite(*str, sizeof(**str), str.length(), stdout); |
380 printf("\n"); | 372 printf("\n"); |
381 } | 373 } |
382 #if !defined(V8_SHARED) | 374 #if !defined(V8_SHARED) |
383 } else { | 375 } else { |
384 v8::TryCatch try_catch(isolate); | 376 v8::TryCatch try_catch(isolate); |
385 v8::Local<v8::Context> context = | 377 v8::Local<v8::Context> context = |
386 v8::Local<v8::Context>::New(isolate, utility_context_); | 378 v8::Local<v8::Context>::New(isolate, utility_context_); |
387 v8::Context::Scope context_scope(context); | 379 v8::Context::Scope context_scope(context); |
388 Local<Object> global = context->Global(); | 380 Handle<Object> global = context->Global(); |
389 Local<Value> fun = | 381 Handle<Value> fun = |
390 global->Get(context, String::NewFromUtf8(isolate, "Stringify", | 382 global->Get(String::NewFromUtf8(isolate, "Stringify")); |
391 v8::NewStringType::kNormal) | 383 Handle<Value> argv[1] = {result}; |
392 .ToLocalChecked()).ToLocalChecked(); | 384 Handle<Value> s = Handle<Function>::Cast(fun)->Call(global, 1, argv); |
393 Local<Value> argv[1] = {result}; | 385 if (try_catch.HasCaught()) return true; |
394 Local<Value> s; | |
395 if (!Local<Function>::Cast(fun) | |
396 ->Call(context, global, 1, argv) | |
397 .ToLocal(&s)) { | |
398 return true; | |
399 } | |
400 DCHECK(!try_catch.HasCaught()); | |
401 v8::String::Utf8Value str(s); | 386 v8::String::Utf8Value str(s); |
402 fwrite(*str, sizeof(**str), str.length(), stdout); | 387 fwrite(*str, sizeof(**str), str.length(), stdout); |
403 printf("\n"); | 388 printf("\n"); |
404 } | 389 } |
405 #endif | 390 #endif |
406 } | 391 } |
407 return true; | 392 return true; |
408 } | 393 } |
409 | 394 |
410 | 395 |
411 PerIsolateData::RealmScope::RealmScope(PerIsolateData* data) : data_(data) { | 396 PerIsolateData::RealmScope::RealmScope(PerIsolateData* data) : data_(data) { |
412 data_->realm_count_ = 1; | 397 data_->realm_count_ = 1; |
413 data_->realm_current_ = 0; | 398 data_->realm_current_ = 0; |
414 data_->realm_switch_ = 0; | 399 data_->realm_switch_ = 0; |
415 data_->realms_ = new Global<Context>[1]; | 400 data_->realms_ = new Persistent<Context>[1]; |
416 data_->realms_[0].Reset(data_->isolate_, | 401 data_->realms_[0].Reset(data_->isolate_, |
417 data_->isolate_->GetEnteredContext()); | 402 data_->isolate_->GetEnteredContext()); |
418 } | 403 } |
419 | 404 |
420 | 405 |
421 PerIsolateData::RealmScope::~RealmScope() { | 406 PerIsolateData::RealmScope::~RealmScope() { |
422 // Drop realms to avoid keeping them alive. | 407 // Drop realms to avoid keeping them alive. |
423 for (int i = 0; i < data_->realm_count_; ++i) | 408 for (int i = 0; i < data_->realm_count_; ++i) |
424 data_->realms_[i].Reset(); | 409 data_->realms_[i].Reset(); |
425 delete[] data_->realms_; | 410 delete[] data_->realms_; |
426 if (!data_->realm_shared_.IsEmpty()) | 411 if (!data_->realm_shared_.IsEmpty()) |
427 data_->realm_shared_.Reset(); | 412 data_->realm_shared_.Reset(); |
428 } | 413 } |
429 | 414 |
430 | 415 |
431 int PerIsolateData::RealmFind(Local<Context> context) { | 416 int PerIsolateData::RealmFind(Handle<Context> context) { |
432 for (int i = 0; i < realm_count_; ++i) { | 417 for (int i = 0; i < realm_count_; ++i) { |
433 if (realms_[i] == context) return i; | 418 if (realms_[i] == context) return i; |
434 } | 419 } |
435 return -1; | 420 return -1; |
436 } | 421 } |
437 | 422 |
438 | 423 |
439 int PerIsolateData::RealmIndexOrThrow( | 424 int PerIsolateData::RealmIndexOrThrow( |
440 const v8::FunctionCallbackInfo<v8::Value>& args, | 425 const v8::FunctionCallbackInfo<v8::Value>& args, |
441 int arg_offset) { | 426 int arg_offset) { |
442 if (args.Length() < arg_offset || !args[arg_offset]->IsNumber()) { | 427 if (args.Length() < arg_offset || !args[arg_offset]->IsNumber()) { |
443 Throw(args.GetIsolate(), "Invalid argument"); | 428 Throw(args.GetIsolate(), "Invalid argument"); |
444 return -1; | 429 return -1; |
445 } | 430 } |
446 int index = args[arg_offset] | 431 int index = args[arg_offset]->Int32Value(); |
447 ->Int32Value(args.GetIsolate()->GetCurrentContext()) | 432 if (index < 0 || |
448 .FromMaybe(-1); | 433 index >= realm_count_ || |
449 if (index < 0 || index >= realm_count_ || realms_[index].IsEmpty()) { | 434 realms_[index].IsEmpty()) { |
450 Throw(args.GetIsolate(), "Invalid realm index"); | 435 Throw(args.GetIsolate(), "Invalid realm index"); |
451 return -1; | 436 return -1; |
452 } | 437 } |
453 return index; | 438 return index; |
454 } | 439 } |
455 | 440 |
456 | 441 |
457 #ifndef V8_SHARED | 442 #ifndef V8_SHARED |
458 // performance.now() returns a time stamp as double, measured in milliseconds. | 443 // performance.now() returns a time stamp as double, measured in milliseconds. |
459 // When FLAG_verify_predictable mode is enabled it returns current value | 444 // When FLAG_verify_predictable mode is enabled it returns current value |
(...skipping 23 matching lines...) Expand all Loading... |
483 | 468 |
484 | 469 |
485 // Realm.owner(o) returns the index of the realm that created o. | 470 // Realm.owner(o) returns the index of the realm that created o. |
486 void Shell::RealmOwner(const v8::FunctionCallbackInfo<v8::Value>& args) { | 471 void Shell::RealmOwner(const v8::FunctionCallbackInfo<v8::Value>& args) { |
487 Isolate* isolate = args.GetIsolate(); | 472 Isolate* isolate = args.GetIsolate(); |
488 PerIsolateData* data = PerIsolateData::Get(isolate); | 473 PerIsolateData* data = PerIsolateData::Get(isolate); |
489 if (args.Length() < 1 || !args[0]->IsObject()) { | 474 if (args.Length() < 1 || !args[0]->IsObject()) { |
490 Throw(args.GetIsolate(), "Invalid argument"); | 475 Throw(args.GetIsolate(), "Invalid argument"); |
491 return; | 476 return; |
492 } | 477 } |
493 int index = data->RealmFind(args[0] | 478 int index = data->RealmFind(args[0]->ToObject(isolate)->CreationContext()); |
494 ->ToObject(isolate->GetCurrentContext()) | |
495 .ToLocalChecked() | |
496 ->CreationContext()); | |
497 if (index == -1) return; | 479 if (index == -1) return; |
498 args.GetReturnValue().Set(index); | 480 args.GetReturnValue().Set(index); |
499 } | 481 } |
500 | 482 |
501 | 483 |
502 // Realm.global(i) returns the global object of realm i. | 484 // Realm.global(i) returns the global object of realm i. |
503 // (Note that properties of global objects cannot be read/written cross-realm.) | 485 // (Note that properties of global objects cannot be read/written cross-realm.) |
504 void Shell::RealmGlobal(const v8::FunctionCallbackInfo<v8::Value>& args) { | 486 void Shell::RealmGlobal(const v8::FunctionCallbackInfo<v8::Value>& args) { |
505 PerIsolateData* data = PerIsolateData::Get(args.GetIsolate()); | 487 PerIsolateData* data = PerIsolateData::Get(args.GetIsolate()); |
506 int index = data->RealmIndexOrThrow(args, 0); | 488 int index = data->RealmIndexOrThrow(args, 0); |
507 if (index == -1) return; | 489 if (index == -1) return; |
508 args.GetReturnValue().Set( | 490 args.GetReturnValue().Set( |
509 Local<Context>::New(args.GetIsolate(), data->realms_[index])->Global()); | 491 Local<Context>::New(args.GetIsolate(), data->realms_[index])->Global()); |
510 } | 492 } |
511 | 493 |
512 | 494 |
513 // Realm.create() creates a new realm and returns its index. | 495 // Realm.create() creates a new realm and returns its index. |
514 void Shell::RealmCreate(const v8::FunctionCallbackInfo<v8::Value>& args) { | 496 void Shell::RealmCreate(const v8::FunctionCallbackInfo<v8::Value>& args) { |
515 Isolate* isolate = args.GetIsolate(); | 497 Isolate* isolate = args.GetIsolate(); |
516 TryCatch try_catch(isolate); | 498 TryCatch try_catch(isolate); |
517 PerIsolateData* data = PerIsolateData::Get(isolate); | 499 PerIsolateData* data = PerIsolateData::Get(isolate); |
518 Global<Context>* old_realms = data->realms_; | 500 Persistent<Context>* old_realms = data->realms_; |
519 int index = data->realm_count_; | 501 int index = data->realm_count_; |
520 data->realms_ = new Global<Context>[++data->realm_count_]; | 502 data->realms_ = new Persistent<Context>[++data->realm_count_]; |
521 for (int i = 0; i < index; ++i) { | 503 for (int i = 0; i < index; ++i) { |
522 data->realms_[i].Reset(isolate, old_realms[i]); | 504 data->realms_[i].Reset(isolate, old_realms[i]); |
523 old_realms[i].Reset(); | 505 old_realms[i].Reset(); |
524 } | 506 } |
525 delete[] old_realms; | 507 delete[] old_realms; |
526 Local<ObjectTemplate> global_template = CreateGlobalTemplate(isolate); | 508 Handle<ObjectTemplate> global_template = CreateGlobalTemplate(isolate); |
527 Local<Context> context = Context::New(isolate, NULL, global_template); | 509 Local<Context> context = Context::New(isolate, NULL, global_template); |
528 if (context.IsEmpty()) { | 510 if (context.IsEmpty()) { |
529 DCHECK(try_catch.HasCaught()); | 511 DCHECK(try_catch.HasCaught()); |
530 try_catch.ReThrow(); | 512 try_catch.ReThrow(); |
531 return; | 513 return; |
532 } | 514 } |
533 data->realms_[index].Reset(isolate, context); | 515 data->realms_[index].Reset(isolate, context); |
534 args.GetReturnValue().Set(index); | 516 args.GetReturnValue().Set(index); |
535 } | 517 } |
536 | 518 |
(...skipping 28 matching lines...) Expand all Loading... |
565 // Realm.eval(i, s) evaluates s in realm i and returns the result. | 547 // Realm.eval(i, s) evaluates s in realm i and returns the result. |
566 void Shell::RealmEval(const v8::FunctionCallbackInfo<v8::Value>& args) { | 548 void Shell::RealmEval(const v8::FunctionCallbackInfo<v8::Value>& args) { |
567 Isolate* isolate = args.GetIsolate(); | 549 Isolate* isolate = args.GetIsolate(); |
568 PerIsolateData* data = PerIsolateData::Get(isolate); | 550 PerIsolateData* data = PerIsolateData::Get(isolate); |
569 int index = data->RealmIndexOrThrow(args, 0); | 551 int index = data->RealmIndexOrThrow(args, 0); |
570 if (index == -1) return; | 552 if (index == -1) return; |
571 if (args.Length() < 2 || !args[1]->IsString()) { | 553 if (args.Length() < 2 || !args[1]->IsString()) { |
572 Throw(args.GetIsolate(), "Invalid argument"); | 554 Throw(args.GetIsolate(), "Invalid argument"); |
573 return; | 555 return; |
574 } | 556 } |
575 ScriptCompiler::Source script_source( | 557 ScriptCompiler::Source script_source(args[1]->ToString(isolate)); |
576 args[1]->ToString(isolate->GetCurrentContext()).ToLocalChecked()); | 558 Handle<UnboundScript> script = ScriptCompiler::CompileUnbound( |
577 Local<UnboundScript> script; | 559 isolate, &script_source); |
578 if (!ScriptCompiler::CompileUnboundScript(isolate, &script_source) | 560 if (script.IsEmpty()) return; |
579 .ToLocal(&script)) { | |
580 return; | |
581 } | |
582 Local<Context> realm = Local<Context>::New(isolate, data->realms_[index]); | 561 Local<Context> realm = Local<Context>::New(isolate, data->realms_[index]); |
583 realm->Enter(); | 562 realm->Enter(); |
584 Local<Value> result; | 563 Handle<Value> result = script->BindToCurrentContext()->Run(); |
585 if (!script->BindToCurrentContext()->Run(realm).ToLocal(&result)) { | |
586 realm->Exit(); | |
587 return; | |
588 } | |
589 realm->Exit(); | 564 realm->Exit(); |
590 args.GetReturnValue().Set(result); | 565 args.GetReturnValue().Set(result); |
591 } | 566 } |
592 | 567 |
593 | 568 |
594 // Realm.shared is an accessor for a single shared value across realms. | 569 // Realm.shared is an accessor for a single shared value across realms. |
595 void Shell::RealmSharedGet(Local<String> property, | 570 void Shell::RealmSharedGet(Local<String> property, |
596 const PropertyCallbackInfo<Value>& info) { | 571 const PropertyCallbackInfo<Value>& info) { |
597 Isolate* isolate = info.GetIsolate(); | 572 Isolate* isolate = info.GetIsolate(); |
598 PerIsolateData* data = PerIsolateData::Get(isolate); | 573 PerIsolateData* data = PerIsolateData::Get(isolate); |
(...skipping 19 matching lines...) Expand all Loading... |
618 | 593 |
619 void Shell::Write(const v8::FunctionCallbackInfo<v8::Value>& args) { | 594 void Shell::Write(const v8::FunctionCallbackInfo<v8::Value>& args) { |
620 for (int i = 0; i < args.Length(); i++) { | 595 for (int i = 0; i < args.Length(); i++) { |
621 HandleScope handle_scope(args.GetIsolate()); | 596 HandleScope handle_scope(args.GetIsolate()); |
622 if (i != 0) { | 597 if (i != 0) { |
623 printf(" "); | 598 printf(" "); |
624 } | 599 } |
625 | 600 |
626 // Explicitly catch potential exceptions in toString(). | 601 // Explicitly catch potential exceptions in toString(). |
627 v8::TryCatch try_catch(args.GetIsolate()); | 602 v8::TryCatch try_catch(args.GetIsolate()); |
628 Local<String> str_obj; | 603 Handle<String> str_obj = args[i]->ToString(args.GetIsolate()); |
629 if (!args[i] | 604 if (try_catch.HasCaught()) { |
630 ->ToString(args.GetIsolate()->GetCurrentContext()) | |
631 .ToLocal(&str_obj)) { | |
632 try_catch.ReThrow(); | 605 try_catch.ReThrow(); |
633 return; | 606 return; |
634 } | 607 } |
635 | 608 |
636 v8::String::Utf8Value str(str_obj); | 609 v8::String::Utf8Value str(str_obj); |
637 int n = static_cast<int>(fwrite(*str, sizeof(**str), str.length(), stdout)); | 610 int n = static_cast<int>(fwrite(*str, sizeof(**str), str.length(), stdout)); |
638 if (n != str.length()) { | 611 if (n != str.length()) { |
639 printf("Error in fwrite\n"); | 612 printf("Error in fwrite\n"); |
640 Exit(1); | 613 Exit(1); |
641 } | 614 } |
642 } | 615 } |
643 } | 616 } |
644 | 617 |
645 | 618 |
646 void Shell::Read(const v8::FunctionCallbackInfo<v8::Value>& args) { | 619 void Shell::Read(const v8::FunctionCallbackInfo<v8::Value>& args) { |
647 String::Utf8Value file(args[0]); | 620 String::Utf8Value file(args[0]); |
648 if (*file == NULL) { | 621 if (*file == NULL) { |
649 Throw(args.GetIsolate(), "Error loading file"); | 622 Throw(args.GetIsolate(), "Error loading file"); |
650 return; | 623 return; |
651 } | 624 } |
652 Local<String> source = ReadFile(args.GetIsolate(), *file); | 625 Handle<String> source = ReadFile(args.GetIsolate(), *file); |
653 if (source.IsEmpty()) { | 626 if (source.IsEmpty()) { |
654 Throw(args.GetIsolate(), "Error loading file"); | 627 Throw(args.GetIsolate(), "Error loading file"); |
655 return; | 628 return; |
656 } | 629 } |
657 args.GetReturnValue().Set(source); | 630 args.GetReturnValue().Set(source); |
658 } | 631 } |
659 | 632 |
660 | 633 |
661 Local<String> Shell::ReadFromStdin(Isolate* isolate) { | 634 Handle<String> Shell::ReadFromStdin(Isolate* isolate) { |
662 static const int kBufferSize = 256; | 635 static const int kBufferSize = 256; |
663 char buffer[kBufferSize]; | 636 char buffer[kBufferSize]; |
664 Local<String> accumulator = | 637 Handle<String> accumulator = String::NewFromUtf8(isolate, ""); |
665 String::NewFromUtf8(isolate, "", NewStringType::kNormal).ToLocalChecked(); | |
666 int length; | 638 int length; |
667 while (true) { | 639 while (true) { |
668 // Continue reading if the line ends with an escape '\\' or the line has | 640 // Continue reading if the line ends with an escape '\\' or the line has |
669 // not been fully read into the buffer yet (does not end with '\n'). | 641 // not been fully read into the buffer yet (does not end with '\n'). |
670 // If fgets gets an error, just give up. | 642 // If fgets gets an error, just give up. |
671 char* input = NULL; | 643 char* input = NULL; |
672 input = fgets(buffer, kBufferSize, stdin); | 644 input = fgets(buffer, kBufferSize, stdin); |
673 if (input == NULL) return Local<String>(); | 645 if (input == NULL) return Handle<String>(); |
674 length = static_cast<int>(strlen(buffer)); | 646 length = static_cast<int>(strlen(buffer)); |
675 if (length == 0) { | 647 if (length == 0) { |
676 return accumulator; | 648 return accumulator; |
677 } else if (buffer[length-1] != '\n') { | 649 } else if (buffer[length-1] != '\n') { |
678 accumulator = String::Concat( | 650 accumulator = String::Concat( |
679 accumulator, | 651 accumulator, |
680 String::NewFromUtf8(isolate, buffer, NewStringType::kNormal, length) | 652 String::NewFromUtf8(isolate, buffer, String::kNormalString, length)); |
681 .ToLocalChecked()); | |
682 } else if (length > 1 && buffer[length-2] == '\\') { | 653 } else if (length > 1 && buffer[length-2] == '\\') { |
683 buffer[length-2] = '\n'; | 654 buffer[length-2] = '\n'; |
684 accumulator = String::Concat( | 655 accumulator = String::Concat( |
685 accumulator, | 656 accumulator, String::NewFromUtf8(isolate, buffer, |
686 String::NewFromUtf8(isolate, buffer, NewStringType::kNormal, | 657 String::kNormalString, length - 1)); |
687 length - 1).ToLocalChecked()); | |
688 } else { | 658 } else { |
689 return String::Concat( | 659 return String::Concat( |
690 accumulator, | 660 accumulator, String::NewFromUtf8(isolate, buffer, |
691 String::NewFromUtf8(isolate, buffer, NewStringType::kNormal, | 661 String::kNormalString, length - 1)); |
692 length - 1).ToLocalChecked()); | |
693 } | 662 } |
694 } | 663 } |
695 } | 664 } |
696 | 665 |
697 | 666 |
698 void Shell::Load(const v8::FunctionCallbackInfo<v8::Value>& args) { | 667 void Shell::Load(const v8::FunctionCallbackInfo<v8::Value>& args) { |
699 for (int i = 0; i < args.Length(); i++) { | 668 for (int i = 0; i < args.Length(); i++) { |
700 HandleScope handle_scope(args.GetIsolate()); | 669 HandleScope handle_scope(args.GetIsolate()); |
701 String::Utf8Value file(args[i]); | 670 String::Utf8Value file(args[i]); |
702 if (*file == NULL) { | 671 if (*file == NULL) { |
703 Throw(args.GetIsolate(), "Error loading file"); | 672 Throw(args.GetIsolate(), "Error loading file"); |
704 return; | 673 return; |
705 } | 674 } |
706 Local<String> source = ReadFile(args.GetIsolate(), *file); | 675 Handle<String> source = ReadFile(args.GetIsolate(), *file); |
707 if (source.IsEmpty()) { | 676 if (source.IsEmpty()) { |
708 Throw(args.GetIsolate(), "Error loading file"); | 677 Throw(args.GetIsolate(), "Error loading file"); |
709 return; | 678 return; |
710 } | 679 } |
711 if (!ExecuteString( | 680 if (!ExecuteString(args.GetIsolate(), |
712 args.GetIsolate(), source, | 681 source, |
713 String::NewFromUtf8(args.GetIsolate(), *file, | 682 String::NewFromUtf8(args.GetIsolate(), *file), |
714 NewStringType::kNormal).ToLocalChecked(), | 683 false, |
715 false, true)) { | 684 true)) { |
716 Throw(args.GetIsolate(), "Error executing file"); | 685 Throw(args.GetIsolate(), "Error executing file"); |
717 return; | 686 return; |
718 } | 687 } |
719 } | 688 } |
720 } | 689 } |
721 | 690 |
722 | 691 |
723 #ifndef V8_SHARED | 692 #ifndef V8_SHARED |
724 void Shell::WorkerNew(const v8::FunctionCallbackInfo<v8::Value>& args) { | 693 void Shell::WorkerNew(const v8::FunctionCallbackInfo<v8::Value>& args) { |
725 Isolate* isolate = args.GetIsolate(); | 694 Isolate* isolate = args.GetIsolate(); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
759 | 728 |
760 Local<Value> this_value = args.This()->GetInternalField(0); | 729 Local<Value> this_value = args.This()->GetInternalField(0); |
761 if (!this_value->IsExternal()) { | 730 if (!this_value->IsExternal()) { |
762 Throw(isolate, "this is not a Worker"); | 731 Throw(isolate, "this is not a Worker"); |
763 return; | 732 return; |
764 } | 733 } |
765 | 734 |
766 Worker* worker = | 735 Worker* worker = |
767 static_cast<Worker*>(Local<External>::Cast(this_value)->Value()); | 736 static_cast<Worker*>(Local<External>::Cast(this_value)->Value()); |
768 | 737 |
769 Local<Value> message = args[0]; | 738 Handle<Value> message = args[0]; |
770 ObjectList to_transfer; | 739 ObjectList to_transfer; |
771 if (args.Length() >= 2) { | 740 if (args.Length() >= 2) { |
772 if (!args[1]->IsArray()) { | 741 if (!args[1]->IsArray()) { |
773 Throw(isolate, "Transfer list must be an Array"); | 742 Throw(isolate, "Transfer list must be an Array"); |
774 return; | 743 return; |
775 } | 744 } |
776 | 745 |
777 Local<Array> transfer = Local<Array>::Cast(args[1]); | 746 Handle<Array> transfer = Handle<Array>::Cast(args[1]); |
778 uint32_t length = transfer->Length(); | 747 uint32_t length = transfer->Length(); |
779 for (uint32_t i = 0; i < length; ++i) { | 748 for (uint32_t i = 0; i < length; ++i) { |
780 Local<Value> element; | 749 Handle<Value> element; |
781 if (transfer->Get(context, i).ToLocal(&element)) { | 750 if (transfer->Get(context, i).ToLocal(&element)) { |
782 if (!element->IsArrayBuffer() && !element->IsSharedArrayBuffer()) { | 751 if (!element->IsArrayBuffer() && !element->IsSharedArrayBuffer()) { |
783 Throw(isolate, | 752 Throw(isolate, |
784 "Transfer array elements must be an ArrayBuffer or " | 753 "Transfer array elements must be an ArrayBuffer or " |
785 "SharedArrayBuffer."); | 754 "SharedArrayBuffer."); |
786 break; | 755 break; |
787 } | 756 } |
788 | 757 |
789 to_transfer.Add(Local<Object>::Cast(element)); | 758 to_transfer.Add(Handle<Object>::Cast(element)); |
790 } | 759 } |
791 } | 760 } |
792 } | 761 } |
793 | 762 |
794 ObjectList seen_objects; | 763 ObjectList seen_objects; |
795 SerializationData* data = new SerializationData; | 764 SerializationData* data = new SerializationData; |
796 if (SerializeValue(isolate, message, to_transfer, &seen_objects, data)) { | 765 if (SerializeValue(isolate, message, to_transfer, &seen_objects, data)) { |
797 worker->PostMessage(data); | 766 worker->PostMessage(data); |
798 } else { | 767 } else { |
799 delete data; | 768 delete data; |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
836 } | 805 } |
837 | 806 |
838 Worker* worker = | 807 Worker* worker = |
839 static_cast<Worker*>(Local<External>::Cast(this_value)->Value()); | 808 static_cast<Worker*>(Local<External>::Cast(this_value)->Value()); |
840 worker->Terminate(); | 809 worker->Terminate(); |
841 } | 810 } |
842 #endif // !V8_SHARED | 811 #endif // !V8_SHARED |
843 | 812 |
844 | 813 |
845 void Shell::QuitOnce(v8::FunctionCallbackInfo<v8::Value>* args) { | 814 void Shell::QuitOnce(v8::FunctionCallbackInfo<v8::Value>* args) { |
846 int exit_code = (*args)[0] | 815 int exit_code = (*args)[0]->Int32Value(); |
847 ->Int32Value(args->GetIsolate()->GetCurrentContext()) | |
848 .FromMaybe(0); | |
849 #ifndef V8_SHARED | 816 #ifndef V8_SHARED |
850 CleanupWorkers(); | 817 CleanupWorkers(); |
851 #endif // !V8_SHARED | 818 #endif // !V8_SHARED |
852 OnExit(args->GetIsolate()); | 819 OnExit(args->GetIsolate()); |
853 exit(exit_code); | 820 exit(exit_code); |
854 } | 821 } |
855 | 822 |
856 | 823 |
857 void Shell::Quit(const v8::FunctionCallbackInfo<v8::Value>& args) { | 824 void Shell::Quit(const v8::FunctionCallbackInfo<v8::Value>& args) { |
858 base::CallOnce(&quit_once_, &QuitOnce, | 825 base::CallOnce(&quit_once_, &QuitOnce, |
859 const_cast<v8::FunctionCallbackInfo<v8::Value>*>(&args)); | 826 const_cast<v8::FunctionCallbackInfo<v8::Value>*>(&args)); |
860 } | 827 } |
861 | 828 |
862 | 829 |
863 void Shell::Version(const v8::FunctionCallbackInfo<v8::Value>& args) { | 830 void Shell::Version(const v8::FunctionCallbackInfo<v8::Value>& args) { |
864 args.GetReturnValue().Set( | 831 args.GetReturnValue().Set( |
865 String::NewFromUtf8(args.GetIsolate(), V8::GetVersion(), | 832 String::NewFromUtf8(args.GetIsolate(), V8::GetVersion())); |
866 NewStringType::kNormal).ToLocalChecked()); | |
867 } | 833 } |
868 | 834 |
869 | 835 |
870 void Shell::ReportException(Isolate* isolate, v8::TryCatch* try_catch) { | 836 void Shell::ReportException(Isolate* isolate, v8::TryCatch* try_catch) { |
871 HandleScope handle_scope(isolate); | 837 HandleScope handle_scope(isolate); |
872 #ifndef V8_SHARED | 838 #ifndef V8_SHARED |
873 Local<Context> utility_context; | 839 Handle<Context> utility_context; |
874 bool enter_context = !isolate->InContext(); | 840 bool enter_context = !isolate->InContext(); |
875 if (enter_context) { | 841 if (enter_context) { |
876 utility_context = Local<Context>::New(isolate, utility_context_); | 842 utility_context = Local<Context>::New(isolate, utility_context_); |
877 utility_context->Enter(); | 843 utility_context->Enter(); |
878 } | 844 } |
879 #endif // !V8_SHARED | 845 #endif // !V8_SHARED |
880 v8::String::Utf8Value exception(try_catch->Exception()); | 846 v8::String::Utf8Value exception(try_catch->Exception()); |
881 const char* exception_string = ToCString(exception); | 847 const char* exception_string = ToCString(exception); |
882 Local<Message> message = try_catch->Message(); | 848 Handle<Message> message = try_catch->Message(); |
883 if (message.IsEmpty()) { | 849 if (message.IsEmpty()) { |
884 // V8 didn't provide any extra information about this error; just | 850 // V8 didn't provide any extra information about this error; just |
885 // print the exception. | 851 // print the exception. |
886 printf("%s\n", exception_string); | 852 printf("%s\n", exception_string); |
887 } else { | 853 } else { |
888 // Print (filename):(line number): (message). | 854 // Print (filename):(line number): (message). |
889 v8::String::Utf8Value filename(message->GetScriptOrigin().ResourceName()); | 855 v8::String::Utf8Value filename(message->GetScriptOrigin().ResourceName()); |
890 const char* filename_string = ToCString(filename); | 856 const char* filename_string = ToCString(filename); |
891 int linenum = | 857 int linenum = message->GetLineNumber(); |
892 message->GetLineNumber(isolate->GetCurrentContext()).FromJust(); | |
893 printf("%s:%i: %s\n", filename_string, linenum, exception_string); | 858 printf("%s:%i: %s\n", filename_string, linenum, exception_string); |
894 // Print line of source code. | 859 // Print line of source code. |
895 v8::String::Utf8Value sourceline( | 860 v8::String::Utf8Value sourceline(message->GetSourceLine()); |
896 message->GetSourceLine(isolate->GetCurrentContext()).ToLocalChecked()); | |
897 const char* sourceline_string = ToCString(sourceline); | 861 const char* sourceline_string = ToCString(sourceline); |
898 printf("%s\n", sourceline_string); | 862 printf("%s\n", sourceline_string); |
899 // Print wavy underline (GetUnderline is deprecated). | 863 // Print wavy underline (GetUnderline is deprecated). |
900 int start = | 864 int start = message->GetStartColumn(); |
901 message->GetStartColumn(isolate->GetCurrentContext()).FromJust(); | |
902 for (int i = 0; i < start; i++) { | 865 for (int i = 0; i < start; i++) { |
903 printf(" "); | 866 printf(" "); |
904 } | 867 } |
905 int end = message->GetEndColumn(isolate->GetCurrentContext()).FromJust(); | 868 int end = message->GetEndColumn(); |
906 for (int i = start; i < end; i++) { | 869 for (int i = start; i < end; i++) { |
907 printf("^"); | 870 printf("^"); |
908 } | 871 } |
909 printf("\n"); | 872 printf("\n"); |
910 Local<Value> stack_trace_string; | 873 v8::String::Utf8Value stack_trace(try_catch->StackTrace()); |
911 if (try_catch->StackTrace(isolate->GetCurrentContext()) | 874 if (stack_trace.length() > 0) { |
912 .ToLocal(&stack_trace_string)) { | 875 const char* stack_trace_string = ToCString(stack_trace); |
913 v8::String::Utf8Value stack_trace( | 876 printf("%s\n", stack_trace_string); |
914 Local<String>::Cast(stack_trace_string)); | |
915 printf("%s\n", ToCString(stack_trace)); | |
916 } | 877 } |
917 } | 878 } |
918 printf("\n"); | 879 printf("\n"); |
919 #ifndef V8_SHARED | 880 #ifndef V8_SHARED |
920 if (enter_context) utility_context->Exit(); | 881 if (enter_context) utility_context->Exit(); |
921 #endif // !V8_SHARED | 882 #endif // !V8_SHARED |
922 } | 883 } |
923 | 884 |
924 | 885 |
925 #ifndef V8_SHARED | 886 #ifndef V8_SHARED |
926 Local<Array> Shell::GetCompletions(Isolate* isolate, Local<String> text, | 887 Handle<Array> Shell::GetCompletions(Isolate* isolate, |
927 Local<String> full) { | 888 Handle<String> text, |
| 889 Handle<String> full) { |
928 EscapableHandleScope handle_scope(isolate); | 890 EscapableHandleScope handle_scope(isolate); |
929 v8::Local<v8::Context> utility_context = | 891 v8::Local<v8::Context> utility_context = |
930 v8::Local<v8::Context>::New(isolate, utility_context_); | 892 v8::Local<v8::Context>::New(isolate, utility_context_); |
931 v8::Context::Scope context_scope(utility_context); | 893 v8::Context::Scope context_scope(utility_context); |
932 Local<Object> global = utility_context->Global(); | 894 Handle<Object> global = utility_context->Global(); |
933 Local<Value> fun = global->Get(utility_context, | 895 Local<Value> fun = |
934 String::NewFromUtf8(isolate, "GetCompletions", | 896 global->Get(String::NewFromUtf8(isolate, "GetCompletions")); |
935 NewStringType::kNormal) | |
936 .ToLocalChecked()).ToLocalChecked(); | |
937 static const int kArgc = 3; | 897 static const int kArgc = 3; |
938 v8::Local<v8::Context> evaluation_context = | 898 v8::Local<v8::Context> evaluation_context = |
939 v8::Local<v8::Context>::New(isolate, evaluation_context_); | 899 v8::Local<v8::Context>::New(isolate, evaluation_context_); |
940 Local<Value> argv[kArgc] = {evaluation_context->Global(), text, full}; | 900 Handle<Value> argv[kArgc] = { evaluation_context->Global(), text, full }; |
941 Local<Value> val = Local<Function>::Cast(fun) | 901 Local<Value> val = Local<Function>::Cast(fun)->Call(global, kArgc, argv); |
942 ->Call(utility_context, global, kArgc, argv) | |
943 .ToLocalChecked(); | |
944 return handle_scope.Escape(Local<Array>::Cast(val)); | 902 return handle_scope.Escape(Local<Array>::Cast(val)); |
945 } | 903 } |
946 | 904 |
947 | 905 |
948 Local<Object> Shell::DebugMessageDetails(Isolate* isolate, | 906 Local<Object> Shell::DebugMessageDetails(Isolate* isolate, |
949 Local<String> message) { | 907 Handle<String> message) { |
950 EscapableHandleScope handle_scope(isolate); | 908 EscapableHandleScope handle_scope(isolate); |
951 v8::Local<v8::Context> context = | 909 v8::Local<v8::Context> context = |
952 v8::Local<v8::Context>::New(isolate, utility_context_); | 910 v8::Local<v8::Context>::New(isolate, utility_context_); |
953 v8::Context::Scope context_scope(context); | 911 v8::Context::Scope context_scope(context); |
954 Local<Object> global = context->Global(); | 912 Handle<Object> global = context->Global(); |
955 Local<Value> fun = | 913 Handle<Value> fun = |
956 global->Get(context, String::NewFromUtf8(isolate, "DebugMessageDetails", | 914 global->Get(String::NewFromUtf8(isolate, "DebugMessageDetails")); |
957 NewStringType::kNormal) | |
958 .ToLocalChecked()).ToLocalChecked(); | |
959 static const int kArgc = 1; | 915 static const int kArgc = 1; |
960 Local<Value> argv[kArgc] = {message}; | 916 Handle<Value> argv[kArgc] = { message }; |
961 Local<Value> val = Local<Function>::Cast(fun) | 917 Handle<Value> val = Handle<Function>::Cast(fun)->Call(global, kArgc, argv); |
962 ->Call(context, global, kArgc, argv) | 918 return handle_scope.Escape(Local<Object>(Handle<Object>::Cast(val))); |
963 .ToLocalChecked(); | |
964 return handle_scope.Escape(Local<Object>(Local<Object>::Cast(val))); | |
965 } | 919 } |
966 | 920 |
967 | 921 |
968 Local<Value> Shell::DebugCommandToJSONRequest(Isolate* isolate, | 922 Local<Value> Shell::DebugCommandToJSONRequest(Isolate* isolate, |
969 Local<String> command) { | 923 Handle<String> command) { |
970 EscapableHandleScope handle_scope(isolate); | 924 EscapableHandleScope handle_scope(isolate); |
971 v8::Local<v8::Context> context = | 925 v8::Local<v8::Context> context = |
972 v8::Local<v8::Context>::New(isolate, utility_context_); | 926 v8::Local<v8::Context>::New(isolate, utility_context_); |
973 v8::Context::Scope context_scope(context); | 927 v8::Context::Scope context_scope(context); |
974 Local<Object> global = context->Global(); | 928 Handle<Object> global = context->Global(); |
975 Local<Value> fun = | 929 Handle<Value> fun = |
976 global->Get(context, | 930 global->Get(String::NewFromUtf8(isolate, "DebugCommandToJSONRequest")); |
977 String::NewFromUtf8(isolate, "DebugCommandToJSONRequest", | |
978 NewStringType::kNormal).ToLocalChecked()) | |
979 .ToLocalChecked(); | |
980 static const int kArgc = 1; | 931 static const int kArgc = 1; |
981 Local<Value> argv[kArgc] = {command}; | 932 Handle<Value> argv[kArgc] = { command }; |
982 Local<Value> val = Local<Function>::Cast(fun) | 933 Handle<Value> val = Handle<Function>::Cast(fun)->Call(global, kArgc, argv); |
983 ->Call(context, global, kArgc, argv) | |
984 .ToLocalChecked(); | |
985 return handle_scope.Escape(Local<Value>(val)); | 934 return handle_scope.Escape(Local<Value>(val)); |
986 } | 935 } |
987 | 936 |
988 | 937 |
989 int32_t* Counter::Bind(const char* name, bool is_histogram) { | 938 int32_t* Counter::Bind(const char* name, bool is_histogram) { |
990 int i; | 939 int i; |
991 for (i = 0; i < kMaxNameSize - 1 && name[i]; i++) | 940 for (i = 0; i < kMaxNameSize - 1 && name[i]; i++) |
992 name_[i] = static_cast<char>(name[i]); | 941 name_[i] = static_cast<char>(name[i]); |
993 name_[i] = '\0'; | 942 name_[i] = '\0'; |
994 is_histogram_ = is_histogram; | 943 is_histogram_ = is_histogram; |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1109 evaluation_context->SetSecurityToken(Undefined(isolate)); | 1058 evaluation_context->SetSecurityToken(Undefined(isolate)); |
1110 v8::Context::Scope context_scope(utility_context); | 1059 v8::Context::Scope context_scope(utility_context); |
1111 | 1060 |
1112 if (i::FLAG_debugger) printf("JavaScript debugger enabled\n"); | 1061 if (i::FLAG_debugger) printf("JavaScript debugger enabled\n"); |
1113 // Install the debugger object in the utility scope | 1062 // Install the debugger object in the utility scope |
1114 i::Debug* debug = reinterpret_cast<i::Isolate*>(isolate)->debug(); | 1063 i::Debug* debug = reinterpret_cast<i::Isolate*>(isolate)->debug(); |
1115 debug->Load(); | 1064 debug->Load(); |
1116 i::Handle<i::Context> debug_context = debug->debug_context(); | 1065 i::Handle<i::Context> debug_context = debug->debug_context(); |
1117 i::Handle<i::JSObject> js_debug | 1066 i::Handle<i::JSObject> js_debug |
1118 = i::Handle<i::JSObject>(debug_context->global_object()); | 1067 = i::Handle<i::JSObject>(debug_context->global_object()); |
1119 utility_context->Global() | 1068 utility_context->Global()->Set(String::NewFromUtf8(isolate, "$debug"), |
1120 ->Set(utility_context, | 1069 Utils::ToLocal(js_debug)); |
1121 String::NewFromUtf8(isolate, "$debug", NewStringType::kNormal) | |
1122 .ToLocalChecked(), | |
1123 Utils::ToLocal(js_debug)) | |
1124 .FromJust(); | |
1125 debug_context->set_security_token( | 1070 debug_context->set_security_token( |
1126 reinterpret_cast<i::Isolate*>(isolate)->heap()->undefined_value()); | 1071 reinterpret_cast<i::Isolate*>(isolate)->heap()->undefined_value()); |
1127 | 1072 |
1128 // Run the d8 shell utility script in the utility context | 1073 // Run the d8 shell utility script in the utility context |
1129 int source_index = i::NativesCollection<i::D8>::GetIndex("d8"); | 1074 int source_index = i::NativesCollection<i::D8>::GetIndex("d8"); |
1130 i::Vector<const char> shell_source = | 1075 i::Vector<const char> shell_source = |
1131 i::NativesCollection<i::D8>::GetScriptSource(source_index); | 1076 i::NativesCollection<i::D8>::GetScriptSource(source_index); |
1132 i::Vector<const char> shell_source_name = | 1077 i::Vector<const char> shell_source_name = |
1133 i::NativesCollection<i::D8>::GetScriptName(source_index); | 1078 i::NativesCollection<i::D8>::GetScriptName(source_index); |
1134 Local<String> source = | 1079 Handle<String> source = |
1135 String::NewFromUtf8(isolate, shell_source.start(), NewStringType::kNormal, | 1080 String::NewFromUtf8(isolate, shell_source.start(), String::kNormalString, |
1136 shell_source.length()).ToLocalChecked(); | 1081 shell_source.length()); |
1137 Local<String> name = | 1082 Handle<String> name = |
1138 String::NewFromUtf8(isolate, shell_source_name.start(), | 1083 String::NewFromUtf8(isolate, shell_source_name.start(), |
1139 NewStringType::kNormal, | 1084 String::kNormalString, shell_source_name.length()); |
1140 shell_source_name.length()).ToLocalChecked(); | |
1141 ScriptOrigin origin(name); | 1085 ScriptOrigin origin(name); |
1142 Local<Script> script = | 1086 Handle<Script> script = Script::Compile(source, &origin); |
1143 Script::Compile(utility_context, source, &origin).ToLocalChecked(); | 1087 script->Run(); |
1144 script->Run(utility_context).ToLocalChecked(); | |
1145 // Mark the d8 shell script as native to avoid it showing up as normal source | 1088 // Mark the d8 shell script as native to avoid it showing up as normal source |
1146 // in the debugger. | 1089 // in the debugger. |
1147 i::Handle<i::Object> compiled_script = Utils::OpenHandle(*script); | 1090 i::Handle<i::Object> compiled_script = Utils::OpenHandle(*script); |
1148 i::Handle<i::Script> script_object = compiled_script->IsJSFunction() | 1091 i::Handle<i::Script> script_object = compiled_script->IsJSFunction() |
1149 ? i::Handle<i::Script>(i::Script::cast( | 1092 ? i::Handle<i::Script>(i::Script::cast( |
1150 i::JSFunction::cast(*compiled_script)->shared()->script())) | 1093 i::JSFunction::cast(*compiled_script)->shared()->script())) |
1151 : i::Handle<i::Script>(i::Script::cast( | 1094 : i::Handle<i::Script>(i::Script::cast( |
1152 i::SharedFunctionInfo::cast(*compiled_script)->script())); | 1095 i::SharedFunctionInfo::cast(*compiled_script)->script())); |
1153 script_object->set_type(i::Smi::FromInt(i::Script::TYPE_NATIVE)); | 1096 script_object->set_type(i::Smi::FromInt(i::Script::TYPE_NATIVE)); |
1154 | 1097 |
1155 // Start the in-process debugger if requested. | 1098 // Start the in-process debugger if requested. |
1156 if (i::FLAG_debugger) v8::Debug::SetDebugEventListener(HandleDebugEvent); | 1099 if (i::FLAG_debugger) v8::Debug::SetDebugEventListener(HandleDebugEvent); |
1157 } | 1100 } |
1158 #endif // !V8_SHARED | 1101 #endif // !V8_SHARED |
1159 | 1102 |
1160 | 1103 |
1161 Local<ObjectTemplate> Shell::CreateGlobalTemplate(Isolate* isolate) { | 1104 Handle<ObjectTemplate> Shell::CreateGlobalTemplate(Isolate* isolate) { |
1162 Local<ObjectTemplate> global_template = ObjectTemplate::New(isolate); | 1105 Handle<ObjectTemplate> global_template = ObjectTemplate::New(isolate); |
1163 global_template->Set( | 1106 global_template->Set(String::NewFromUtf8(isolate, "print"), |
1164 String::NewFromUtf8(isolate, "print", NewStringType::kNormal) | 1107 FunctionTemplate::New(isolate, Print)); |
1165 .ToLocalChecked(), | 1108 global_template->Set(String::NewFromUtf8(isolate, "write"), |
1166 FunctionTemplate::New(isolate, Print)); | 1109 FunctionTemplate::New(isolate, Write)); |
1167 global_template->Set( | 1110 global_template->Set(String::NewFromUtf8(isolate, "read"), |
1168 String::NewFromUtf8(isolate, "write", NewStringType::kNormal) | 1111 FunctionTemplate::New(isolate, Read)); |
1169 .ToLocalChecked(), | 1112 global_template->Set(String::NewFromUtf8(isolate, "readbuffer"), |
1170 FunctionTemplate::New(isolate, Write)); | 1113 FunctionTemplate::New(isolate, ReadBuffer)); |
1171 global_template->Set( | 1114 global_template->Set(String::NewFromUtf8(isolate, "readline"), |
1172 String::NewFromUtf8(isolate, "read", NewStringType::kNormal) | 1115 FunctionTemplate::New(isolate, ReadLine)); |
1173 .ToLocalChecked(), | 1116 global_template->Set(String::NewFromUtf8(isolate, "load"), |
1174 FunctionTemplate::New(isolate, Read)); | 1117 FunctionTemplate::New(isolate, Load)); |
1175 global_template->Set( | |
1176 String::NewFromUtf8(isolate, "readbuffer", NewStringType::kNormal) | |
1177 .ToLocalChecked(), | |
1178 FunctionTemplate::New(isolate, ReadBuffer)); | |
1179 global_template->Set( | |
1180 String::NewFromUtf8(isolate, "readline", NewStringType::kNormal) | |
1181 .ToLocalChecked(), | |
1182 FunctionTemplate::New(isolate, ReadLine)); | |
1183 global_template->Set( | |
1184 String::NewFromUtf8(isolate, "load", NewStringType::kNormal) | |
1185 .ToLocalChecked(), | |
1186 FunctionTemplate::New(isolate, Load)); | |
1187 // Some Emscripten-generated code tries to call 'quit', which in turn would | 1118 // Some Emscripten-generated code tries to call 'quit', which in turn would |
1188 // call C's exit(). This would lead to memory leaks, because there is no way | 1119 // call C's exit(). This would lead to memory leaks, because there is no way |
1189 // we can terminate cleanly then, so we need a way to hide 'quit'. | 1120 // we can terminate cleanly then, so we need a way to hide 'quit'. |
1190 if (!options.omit_quit) { | 1121 if (!options.omit_quit) { |
1191 global_template->Set( | 1122 global_template->Set(String::NewFromUtf8(isolate, "quit"), |
1192 String::NewFromUtf8(isolate, "quit", NewStringType::kNormal) | 1123 FunctionTemplate::New(isolate, Quit)); |
1193 .ToLocalChecked(), | |
1194 FunctionTemplate::New(isolate, Quit)); | |
1195 } | 1124 } |
1196 global_template->Set( | 1125 global_template->Set(String::NewFromUtf8(isolate, "version"), |
1197 String::NewFromUtf8(isolate, "version", NewStringType::kNormal) | 1126 FunctionTemplate::New(isolate, Version)); |
1198 .ToLocalChecked(), | |
1199 FunctionTemplate::New(isolate, Version)); | |
1200 | 1127 |
1201 // Bind the Realm object. | 1128 // Bind the Realm object. |
1202 Local<ObjectTemplate> realm_template = ObjectTemplate::New(isolate); | 1129 Handle<ObjectTemplate> realm_template = ObjectTemplate::New(isolate); |
1203 realm_template->Set( | 1130 realm_template->Set(String::NewFromUtf8(isolate, "current"), |
1204 String::NewFromUtf8(isolate, "current", NewStringType::kNormal) | 1131 FunctionTemplate::New(isolate, RealmCurrent)); |
1205 .ToLocalChecked(), | 1132 realm_template->Set(String::NewFromUtf8(isolate, "owner"), |
1206 FunctionTemplate::New(isolate, RealmCurrent)); | 1133 FunctionTemplate::New(isolate, RealmOwner)); |
1207 realm_template->Set( | 1134 realm_template->Set(String::NewFromUtf8(isolate, "global"), |
1208 String::NewFromUtf8(isolate, "owner", NewStringType::kNormal) | 1135 FunctionTemplate::New(isolate, RealmGlobal)); |
1209 .ToLocalChecked(), | 1136 realm_template->Set(String::NewFromUtf8(isolate, "create"), |
1210 FunctionTemplate::New(isolate, RealmOwner)); | 1137 FunctionTemplate::New(isolate, RealmCreate)); |
1211 realm_template->Set( | 1138 realm_template->Set(String::NewFromUtf8(isolate, "dispose"), |
1212 String::NewFromUtf8(isolate, "global", NewStringType::kNormal) | 1139 FunctionTemplate::New(isolate, RealmDispose)); |
1213 .ToLocalChecked(), | 1140 realm_template->Set(String::NewFromUtf8(isolate, "switch"), |
1214 FunctionTemplate::New(isolate, RealmGlobal)); | 1141 FunctionTemplate::New(isolate, RealmSwitch)); |
1215 realm_template->Set( | 1142 realm_template->Set(String::NewFromUtf8(isolate, "eval"), |
1216 String::NewFromUtf8(isolate, "create", NewStringType::kNormal) | 1143 FunctionTemplate::New(isolate, RealmEval)); |
1217 .ToLocalChecked(), | 1144 realm_template->SetAccessor(String::NewFromUtf8(isolate, "shared"), |
1218 FunctionTemplate::New(isolate, RealmCreate)); | 1145 RealmSharedGet, RealmSharedSet); |
1219 realm_template->Set( | 1146 global_template->Set(String::NewFromUtf8(isolate, "Realm"), realm_template); |
1220 String::NewFromUtf8(isolate, "dispose", NewStringType::kNormal) | |
1221 .ToLocalChecked(), | |
1222 FunctionTemplate::New(isolate, RealmDispose)); | |
1223 realm_template->Set( | |
1224 String::NewFromUtf8(isolate, "switch", NewStringType::kNormal) | |
1225 .ToLocalChecked(), | |
1226 FunctionTemplate::New(isolate, RealmSwitch)); | |
1227 realm_template->Set( | |
1228 String::NewFromUtf8(isolate, "eval", NewStringType::kNormal) | |
1229 .ToLocalChecked(), | |
1230 FunctionTemplate::New(isolate, RealmEval)); | |
1231 realm_template->SetAccessor( | |
1232 String::NewFromUtf8(isolate, "shared", NewStringType::kNormal) | |
1233 .ToLocalChecked(), | |
1234 RealmSharedGet, RealmSharedSet); | |
1235 global_template->Set( | |
1236 String::NewFromUtf8(isolate, "Realm", NewStringType::kNormal) | |
1237 .ToLocalChecked(), | |
1238 realm_template); | |
1239 | 1147 |
1240 #ifndef V8_SHARED | 1148 #ifndef V8_SHARED |
1241 Local<ObjectTemplate> performance_template = ObjectTemplate::New(isolate); | 1149 Handle<ObjectTemplate> performance_template = ObjectTemplate::New(isolate); |
1242 performance_template->Set( | 1150 performance_template->Set(String::NewFromUtf8(isolate, "now"), |
1243 String::NewFromUtf8(isolate, "now", NewStringType::kNormal) | 1151 FunctionTemplate::New(isolate, PerformanceNow)); |
1244 .ToLocalChecked(), | 1152 global_template->Set(String::NewFromUtf8(isolate, "performance"), |
1245 FunctionTemplate::New(isolate, PerformanceNow)); | 1153 performance_template); |
1246 global_template->Set( | |
1247 String::NewFromUtf8(isolate, "performance", NewStringType::kNormal) | |
1248 .ToLocalChecked(), | |
1249 performance_template); | |
1250 | 1154 |
1251 Local<FunctionTemplate> worker_fun_template = | 1155 Handle<FunctionTemplate> worker_fun_template = |
1252 FunctionTemplate::New(isolate, WorkerNew); | 1156 FunctionTemplate::New(isolate, WorkerNew); |
1253 worker_fun_template->PrototypeTemplate()->Set( | 1157 worker_fun_template->PrototypeTemplate()->Set( |
1254 String::NewFromUtf8(isolate, "terminate", NewStringType::kNormal) | 1158 String::NewFromUtf8(isolate, "terminate"), |
1255 .ToLocalChecked(), | |
1256 FunctionTemplate::New(isolate, WorkerTerminate)); | 1159 FunctionTemplate::New(isolate, WorkerTerminate)); |
1257 worker_fun_template->PrototypeTemplate()->Set( | 1160 worker_fun_template->PrototypeTemplate()->Set( |
1258 String::NewFromUtf8(isolate, "postMessage", NewStringType::kNormal) | 1161 String::NewFromUtf8(isolate, "postMessage"), |
1259 .ToLocalChecked(), | |
1260 FunctionTemplate::New(isolate, WorkerPostMessage)); | 1162 FunctionTemplate::New(isolate, WorkerPostMessage)); |
1261 worker_fun_template->PrototypeTemplate()->Set( | 1163 worker_fun_template->PrototypeTemplate()->Set( |
1262 String::NewFromUtf8(isolate, "getMessage", NewStringType::kNormal) | 1164 String::NewFromUtf8(isolate, "getMessage"), |
1263 .ToLocalChecked(), | |
1264 FunctionTemplate::New(isolate, WorkerGetMessage)); | 1165 FunctionTemplate::New(isolate, WorkerGetMessage)); |
1265 worker_fun_template->InstanceTemplate()->SetInternalFieldCount(1); | 1166 worker_fun_template->InstanceTemplate()->SetInternalFieldCount(1); |
1266 global_template->Set( | 1167 global_template->Set(String::NewFromUtf8(isolate, "Worker"), |
1267 String::NewFromUtf8(isolate, "Worker", NewStringType::kNormal) | 1168 worker_fun_template); |
1268 .ToLocalChecked(), | |
1269 worker_fun_template); | |
1270 #endif // !V8_SHARED | 1169 #endif // !V8_SHARED |
1271 | 1170 |
1272 Local<ObjectTemplate> os_templ = ObjectTemplate::New(isolate); | 1171 Handle<ObjectTemplate> os_templ = ObjectTemplate::New(isolate); |
1273 AddOSMethods(isolate, os_templ); | 1172 AddOSMethods(isolate, os_templ); |
1274 global_template->Set( | 1173 global_template->Set(String::NewFromUtf8(isolate, "os"), os_templ); |
1275 String::NewFromUtf8(isolate, "os", NewStringType::kNormal) | |
1276 .ToLocalChecked(), | |
1277 os_templ); | |
1278 | 1174 |
1279 return global_template; | 1175 return global_template; |
1280 } | 1176 } |
1281 | 1177 |
1282 | 1178 |
1283 void Shell::Initialize(Isolate* isolate) { | 1179 void Shell::Initialize(Isolate* isolate) { |
1284 #ifndef V8_SHARED | 1180 #ifndef V8_SHARED |
1285 // Set up counters | 1181 // Set up counters |
1286 if (i::StrLength(i::FLAG_map_counters) != 0) | 1182 if (i::StrLength(i::FLAG_map_counters) != 0) |
1287 MapCounters(isolate, i::FLAG_map_counters); | 1183 MapCounters(isolate, i::FLAG_map_counters); |
1288 #endif // !V8_SHARED | 1184 #endif // !V8_SHARED |
1289 } | 1185 } |
1290 | 1186 |
1291 | 1187 |
1292 void Shell::InitializeDebugger(Isolate* isolate) { | 1188 void Shell::InitializeDebugger(Isolate* isolate) { |
1293 if (options.test_shell) return; | 1189 if (options.test_shell) return; |
1294 #ifndef V8_SHARED | 1190 #ifndef V8_SHARED |
1295 HandleScope scope(isolate); | 1191 HandleScope scope(isolate); |
1296 Local<ObjectTemplate> global_template = CreateGlobalTemplate(isolate); | 1192 Handle<ObjectTemplate> global_template = CreateGlobalTemplate(isolate); |
1297 utility_context_.Reset(isolate, | 1193 utility_context_.Reset(isolate, |
1298 Context::New(isolate, NULL, global_template)); | 1194 Context::New(isolate, NULL, global_template)); |
1299 if (utility_context_.IsEmpty()) { | 1195 if (utility_context_.IsEmpty()) { |
1300 printf("Failed to initialize debugger\n"); | 1196 printf("Failed to initialize debugger\n"); |
1301 Shell::Exit(1); | 1197 Shell::Exit(1); |
1302 } | 1198 } |
1303 #endif // !V8_SHARED | 1199 #endif // !V8_SHARED |
1304 } | 1200 } |
1305 | 1201 |
1306 | 1202 |
1307 Local<Context> Shell::CreateEvaluationContext(Isolate* isolate) { | 1203 Local<Context> Shell::CreateEvaluationContext(Isolate* isolate) { |
1308 #ifndef V8_SHARED | 1204 #ifndef V8_SHARED |
1309 // This needs to be a critical section since this is not thread-safe | 1205 // This needs to be a critical section since this is not thread-safe |
1310 base::LockGuard<base::Mutex> lock_guard(context_mutex_.Pointer()); | 1206 base::LockGuard<base::Mutex> lock_guard(context_mutex_.Pointer()); |
1311 #endif // !V8_SHARED | 1207 #endif // !V8_SHARED |
1312 // Initialize the global objects | 1208 // Initialize the global objects |
1313 Local<ObjectTemplate> global_template = CreateGlobalTemplate(isolate); | 1209 Handle<ObjectTemplate> global_template = CreateGlobalTemplate(isolate); |
1314 EscapableHandleScope handle_scope(isolate); | 1210 EscapableHandleScope handle_scope(isolate); |
1315 Local<Context> context = Context::New(isolate, NULL, global_template); | 1211 Local<Context> context = Context::New(isolate, NULL, global_template); |
1316 DCHECK(!context.IsEmpty()); | 1212 DCHECK(!context.IsEmpty()); |
1317 Context::Scope scope(context); | 1213 Context::Scope scope(context); |
1318 | 1214 |
1319 #ifndef V8_SHARED | 1215 #ifndef V8_SHARED |
1320 i::Factory* factory = reinterpret_cast<i::Isolate*>(isolate)->factory(); | 1216 i::Factory* factory = reinterpret_cast<i::Isolate*>(isolate)->factory(); |
1321 i::JSArguments js_args = i::FLAG_js_arguments; | 1217 i::JSArguments js_args = i::FLAG_js_arguments; |
1322 i::Handle<i::FixedArray> arguments_array = | 1218 i::Handle<i::FixedArray> arguments_array = |
1323 factory->NewFixedArray(js_args.argc); | 1219 factory->NewFixedArray(js_args.argc); |
1324 for (int j = 0; j < js_args.argc; j++) { | 1220 for (int j = 0; j < js_args.argc; j++) { |
1325 i::Handle<i::String> arg = | 1221 i::Handle<i::String> arg = |
1326 factory->NewStringFromUtf8(i::CStrVector(js_args[j])).ToHandleChecked(); | 1222 factory->NewStringFromUtf8(i::CStrVector(js_args[j])).ToHandleChecked(); |
1327 arguments_array->set(j, *arg); | 1223 arguments_array->set(j, *arg); |
1328 } | 1224 } |
1329 i::Handle<i::JSArray> arguments_jsarray = | 1225 i::Handle<i::JSArray> arguments_jsarray = |
1330 factory->NewJSArrayWithElements(arguments_array); | 1226 factory->NewJSArrayWithElements(arguments_array); |
1331 context->Global() | 1227 context->Global()->Set(String::NewFromUtf8(isolate, "arguments"), |
1332 ->Set(context, | 1228 Utils::ToLocal(arguments_jsarray)); |
1333 String::NewFromUtf8(isolate, "arguments", NewStringType::kNormal) | |
1334 .ToLocalChecked(), | |
1335 Utils::ToLocal(arguments_jsarray)) | |
1336 .FromJust(); | |
1337 #endif // !V8_SHARED | 1229 #endif // !V8_SHARED |
1338 return handle_scope.Escape(context); | 1230 return handle_scope.Escape(context); |
1339 } | 1231 } |
1340 | 1232 |
1341 | 1233 |
1342 void Shell::Exit(int exit_code) { | 1234 void Shell::Exit(int exit_code) { |
1343 // Use _exit instead of exit to avoid races between isolate | 1235 // Use _exit instead of exit to avoid races between isolate |
1344 // threads and static destructors. | 1236 // threads and static destructors. |
1345 fflush(stdout); | 1237 fflush(stdout); |
1346 fflush(stderr); | 1238 fflush(stderr); |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1481 Isolate* isolate = args.GetIsolate(); | 1373 Isolate* isolate = args.GetIsolate(); |
1482 DataAndPersistent* data = new DataAndPersistent; | 1374 DataAndPersistent* data = new DataAndPersistent; |
1483 data->data = reinterpret_cast<uint8_t*>( | 1375 data->data = reinterpret_cast<uint8_t*>( |
1484 ReadChars(args.GetIsolate(), *filename, &length)); | 1376 ReadChars(args.GetIsolate(), *filename, &length)); |
1485 if (data->data == NULL) { | 1377 if (data->data == NULL) { |
1486 delete data; | 1378 delete data; |
1487 Throw(args.GetIsolate(), "Error reading file"); | 1379 Throw(args.GetIsolate(), "Error reading file"); |
1488 return; | 1380 return; |
1489 } | 1381 } |
1490 data->byte_length = length; | 1382 data->byte_length = length; |
1491 Local<v8::ArrayBuffer> buffer = ArrayBuffer::New(isolate, data->data, length); | 1383 Handle<v8::ArrayBuffer> buffer = |
| 1384 ArrayBuffer::New(isolate, data->data, length); |
1492 data->handle.Reset(isolate, buffer); | 1385 data->handle.Reset(isolate, buffer); |
1493 data->handle.SetWeak(data, ReadBufferWeakCallback, | 1386 data->handle.SetWeak(data, ReadBufferWeakCallback, |
1494 v8::WeakCallbackType::kParameter); | 1387 v8::WeakCallbackType::kParameter); |
1495 data->handle.MarkIndependent(); | 1388 data->handle.MarkIndependent(); |
1496 isolate->AdjustAmountOfExternalAllocatedMemory(length); | 1389 isolate->AdjustAmountOfExternalAllocatedMemory(length); |
1497 | 1390 |
1498 args.GetReturnValue().Set(buffer); | 1391 args.GetReturnValue().Set(buffer); |
1499 } | 1392 } |
1500 | 1393 |
1501 | 1394 |
1502 // Reads a file into a v8 string. | 1395 // Reads a file into a v8 string. |
1503 Local<String> Shell::ReadFile(Isolate* isolate, const char* name) { | 1396 Handle<String> Shell::ReadFile(Isolate* isolate, const char* name) { |
1504 int size = 0; | 1397 int size = 0; |
1505 char* chars = ReadChars(isolate, name, &size); | 1398 char* chars = ReadChars(isolate, name, &size); |
1506 if (chars == NULL) return Local<String>(); | 1399 if (chars == NULL) return Handle<String>(); |
1507 Local<String> result = | 1400 Handle<String> result = |
1508 String::NewFromUtf8(isolate, chars, NewStringType::kNormal, size) | 1401 String::NewFromUtf8(isolate, chars, String::kNormalString, size); |
1509 .ToLocalChecked(); | |
1510 delete[] chars; | 1402 delete[] chars; |
1511 return result; | 1403 return result; |
1512 } | 1404 } |
1513 | 1405 |
1514 | 1406 |
1515 void Shell::RunShell(Isolate* isolate) { | 1407 void Shell::RunShell(Isolate* isolate) { |
1516 HandleScope outer_scope(isolate); | 1408 HandleScope outer_scope(isolate); |
1517 v8::Local<v8::Context> context = | 1409 v8::Local<v8::Context> context = |
1518 v8::Local<v8::Context>::New(isolate, evaluation_context_); | 1410 v8::Local<v8::Context>::New(isolate, evaluation_context_); |
1519 v8::Context::Scope context_scope(context); | 1411 v8::Context::Scope context_scope(context); |
1520 PerIsolateData::RealmScope realm_scope(PerIsolateData::Get(isolate)); | 1412 PerIsolateData::RealmScope realm_scope(PerIsolateData::Get(isolate)); |
1521 Local<String> name = | 1413 Handle<String> name = String::NewFromUtf8(isolate, "(d8)"); |
1522 String::NewFromUtf8(isolate, "(d8)", NewStringType::kNormal) | |
1523 .ToLocalChecked(); | |
1524 LineEditor* console = LineEditor::Get(); | 1414 LineEditor* console = LineEditor::Get(); |
1525 printf("V8 version %s [console: %s]\n", V8::GetVersion(), console->name()); | 1415 printf("V8 version %s [console: %s]\n", V8::GetVersion(), console->name()); |
1526 console->Open(isolate); | 1416 console->Open(isolate); |
1527 while (true) { | 1417 while (true) { |
1528 HandleScope inner_scope(isolate); | 1418 HandleScope inner_scope(isolate); |
1529 Local<String> input = console->Prompt(Shell::kPrompt); | 1419 Handle<String> input = console->Prompt(Shell::kPrompt); |
1530 if (input.IsEmpty()) break; | 1420 if (input.IsEmpty()) break; |
1531 ExecuteString(isolate, input, name, true, true); | 1421 ExecuteString(isolate, input, name, true, true); |
1532 } | 1422 } |
1533 printf("\n"); | 1423 printf("\n"); |
1534 } | 1424 } |
1535 | 1425 |
1536 | 1426 |
1537 SourceGroup::~SourceGroup() { | 1427 SourceGroup::~SourceGroup() { |
1538 #ifndef V8_SHARED | 1428 #ifndef V8_SHARED |
1539 delete thread_; | 1429 delete thread_; |
1540 thread_ = NULL; | 1430 thread_ = NULL; |
1541 #endif // !V8_SHARED | 1431 #endif // !V8_SHARED |
1542 } | 1432 } |
1543 | 1433 |
1544 | 1434 |
1545 void SourceGroup::Execute(Isolate* isolate) { | 1435 void SourceGroup::Execute(Isolate* isolate) { |
1546 bool exception_was_thrown = false; | 1436 bool exception_was_thrown = false; |
1547 for (int i = begin_offset_; i < end_offset_; ++i) { | 1437 for (int i = begin_offset_; i < end_offset_; ++i) { |
1548 const char* arg = argv_[i]; | 1438 const char* arg = argv_[i]; |
1549 Shell::SourceType source_type = Shell::SCRIPT; | 1439 Shell::SourceType source_type = Shell::SCRIPT; |
1550 if (strcmp(arg, "-e") == 0 && i + 1 < end_offset_) { | 1440 if (strcmp(arg, "-e") == 0 && i + 1 < end_offset_) { |
1551 // Execute argument given to -e option directly. | 1441 // Execute argument given to -e option directly. |
1552 HandleScope handle_scope(isolate); | 1442 HandleScope handle_scope(isolate); |
1553 Local<String> file_name = | 1443 Handle<String> file_name = String::NewFromUtf8(isolate, "unnamed"); |
1554 String::NewFromUtf8(isolate, "unnamed", NewStringType::kNormal) | 1444 Handle<String> source = String::NewFromUtf8(isolate, argv_[i + 1]); |
1555 .ToLocalChecked(); | |
1556 Local<String> source = | |
1557 String::NewFromUtf8(isolate, argv_[i + 1], NewStringType::kNormal) | |
1558 .ToLocalChecked(); | |
1559 if (!Shell::ExecuteString(isolate, source, file_name, false, true)) { | 1445 if (!Shell::ExecuteString(isolate, source, file_name, false, true)) { |
1560 exception_was_thrown = true; | 1446 exception_was_thrown = true; |
1561 break; | 1447 break; |
1562 } | 1448 } |
1563 ++i; | 1449 ++i; |
1564 continue; | 1450 continue; |
1565 } else if (strcmp(arg, "--module") == 0 && i + 1 < end_offset_) { | 1451 } else if (strcmp(arg, "--module") == 0 && i + 1 < end_offset_) { |
1566 // Treat the next file as a module. | 1452 // Treat the next file as a module. |
1567 source_type = Shell::MODULE; | 1453 source_type = Shell::MODULE; |
1568 arg = argv_[++i]; | 1454 arg = argv_[++i]; |
1569 } else if (arg[0] == '-') { | 1455 } else if (arg[0] == '-') { |
1570 // Ignore other options. They have been parsed already. | 1456 // Ignore other options. They have been parsed already. |
1571 continue; | 1457 continue; |
1572 } | 1458 } |
1573 | 1459 |
1574 // Use all other arguments as names of files to load and run. | 1460 // Use all other arguments as names of files to load and run. |
1575 HandleScope handle_scope(isolate); | 1461 HandleScope handle_scope(isolate); |
1576 Local<String> file_name = | 1462 Handle<String> file_name = String::NewFromUtf8(isolate, arg); |
1577 String::NewFromUtf8(isolate, arg, NewStringType::kNormal) | 1463 Handle<String> source = ReadFile(isolate, arg); |
1578 .ToLocalChecked(); | |
1579 Local<String> source = ReadFile(isolate, arg); | |
1580 if (source.IsEmpty()) { | 1464 if (source.IsEmpty()) { |
1581 printf("Error reading '%s'\n", arg); | 1465 printf("Error reading '%s'\n", arg); |
1582 Shell::Exit(1); | 1466 Shell::Exit(1); |
1583 } | 1467 } |
1584 if (!Shell::ExecuteString(isolate, source, file_name, false, true, | 1468 if (!Shell::ExecuteString(isolate, source, file_name, false, true, |
1585 source_type)) { | 1469 source_type)) { |
1586 exception_was_thrown = true; | 1470 exception_was_thrown = true; |
1587 break; | 1471 break; |
1588 } | 1472 } |
1589 } | 1473 } |
1590 if (exception_was_thrown != Shell::options.expected_to_throw) { | 1474 if (exception_was_thrown != Shell::options.expected_to_throw) { |
1591 Shell::Exit(1); | 1475 Shell::Exit(1); |
1592 } | 1476 } |
1593 } | 1477 } |
1594 | 1478 |
1595 | 1479 |
1596 Local<String> SourceGroup::ReadFile(Isolate* isolate, const char* name) { | 1480 Handle<String> SourceGroup::ReadFile(Isolate* isolate, const char* name) { |
1597 int size; | 1481 int size; |
1598 char* chars = ReadChars(isolate, name, &size); | 1482 char* chars = ReadChars(isolate, name, &size); |
1599 if (chars == NULL) return Local<String>(); | 1483 if (chars == NULL) return Handle<String>(); |
1600 Local<String> result = | 1484 Handle<String> result = |
1601 String::NewFromUtf8(isolate, chars, NewStringType::kNormal, size) | 1485 String::NewFromUtf8(isolate, chars, String::kNormalString, size); |
1602 .ToLocalChecked(); | |
1603 delete[] chars; | 1486 delete[] chars; |
1604 return result; | 1487 return result; |
1605 } | 1488 } |
1606 | 1489 |
1607 | 1490 |
1608 #ifndef V8_SHARED | 1491 #ifndef V8_SHARED |
1609 base::Thread::Options SourceGroup::GetThreadOptions() { | 1492 base::Thread::Options SourceGroup::GetThreadOptions() { |
1610 // On some systems (OSX 10.6) the stack size default is 0.5Mb or less | 1493 // On some systems (OSX 10.6) the stack size default is 0.5Mb or less |
1611 // which is not enough to parse the big literal expressions used in tests. | 1494 // which is not enough to parse the big literal expressions used in tests. |
1612 // The stack size should be at least StackGuard::kLimitSize + some | 1495 // The stack size should be at least StackGuard::kLimitSize + some |
(...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1845 { | 1728 { |
1846 Isolate::Scope iscope(isolate); | 1729 Isolate::Scope iscope(isolate); |
1847 { | 1730 { |
1848 HandleScope scope(isolate); | 1731 HandleScope scope(isolate); |
1849 PerIsolateData data(isolate); | 1732 PerIsolateData data(isolate); |
1850 Local<Context> context = Shell::CreateEvaluationContext(isolate); | 1733 Local<Context> context = Shell::CreateEvaluationContext(isolate); |
1851 { | 1734 { |
1852 Context::Scope cscope(context); | 1735 Context::Scope cscope(context); |
1853 PerIsolateData::RealmScope realm_scope(PerIsolateData::Get(isolate)); | 1736 PerIsolateData::RealmScope realm_scope(PerIsolateData::Get(isolate)); |
1854 | 1737 |
1855 Local<Object> global = context->Global(); | 1738 Handle<Object> global = context->Global(); |
1856 Local<Value> this_value = External::New(isolate, this); | 1739 Handle<Value> this_value = External::New(isolate, this); |
1857 Local<FunctionTemplate> postmessage_fun_template = | 1740 Handle<FunctionTemplate> postmessage_fun_template = |
1858 FunctionTemplate::New(isolate, PostMessageOut, this_value); | 1741 FunctionTemplate::New(isolate, PostMessageOut, this_value); |
1859 | 1742 |
1860 Local<Function> postmessage_fun; | 1743 Handle<Function> postmessage_fun; |
1861 if (postmessage_fun_template->GetFunction(context) | 1744 if (postmessage_fun_template->GetFunction(context) |
1862 .ToLocal(&postmessage_fun)) { | 1745 .ToLocal(&postmessage_fun)) { |
1863 global->Set(context, String::NewFromUtf8(isolate, "postMessage", | 1746 global->Set(String::NewFromUtf8(isolate, "postMessage"), |
1864 NewStringType::kNormal) | 1747 postmessage_fun); |
1865 .ToLocalChecked(), | |
1866 postmessage_fun).FromJust(); | |
1867 } | 1748 } |
1868 | 1749 |
1869 // First run the script | 1750 // First run the script |
1870 Local<String> file_name = | 1751 Handle<String> file_name = String::NewFromUtf8(isolate, "unnamed"); |
1871 String::NewFromUtf8(isolate, "unnamed", NewStringType::kNormal) | 1752 Handle<String> source = String::NewFromUtf8(isolate, script_); |
1872 .ToLocalChecked(); | |
1873 Local<String> source = | |
1874 String::NewFromUtf8(isolate, script_, NewStringType::kNormal) | |
1875 .ToLocalChecked(); | |
1876 if (Shell::ExecuteString(isolate, source, file_name, false, true)) { | 1753 if (Shell::ExecuteString(isolate, source, file_name, false, true)) { |
1877 // Get the message handler | 1754 // Get the message handler |
1878 Local<Value> onmessage = | 1755 Handle<Value> onmessage = |
1879 global->Get(context, String::NewFromUtf8(isolate, "onmessage", | 1756 global->Get(String::NewFromUtf8(isolate, "onmessage")); |
1880 NewStringType::kNormal) | |
1881 .ToLocalChecked()).ToLocalChecked(); | |
1882 if (onmessage->IsFunction()) { | 1757 if (onmessage->IsFunction()) { |
1883 Local<Function> onmessage_fun = Local<Function>::Cast(onmessage); | 1758 Handle<Function> onmessage_fun = Handle<Function>::Cast(onmessage); |
1884 // Now wait for messages | 1759 // Now wait for messages |
1885 while (true) { | 1760 while (true) { |
1886 in_semaphore_.Wait(); | 1761 in_semaphore_.Wait(); |
1887 SerializationData* data; | 1762 SerializationData* data; |
1888 if (!in_queue_.Dequeue(&data)) continue; | 1763 if (!in_queue_.Dequeue(&data)) continue; |
1889 if (data == NULL) { | 1764 if (data == NULL) { |
1890 break; | 1765 break; |
1891 } | 1766 } |
1892 int offset = 0; | 1767 int offset = 0; |
1893 Local<Value> data_value; | 1768 Local<Value> data_value; |
1894 if (Shell::DeserializeValue(isolate, *data, &offset) | 1769 if (Shell::DeserializeValue(isolate, *data, &offset) |
1895 .ToLocal(&data_value)) { | 1770 .ToLocal(&data_value)) { |
1896 Local<Value> argv[] = {data_value}; | 1771 Handle<Value> argv[] = {data_value}; |
1897 (void)onmessage_fun->Call(context, global, 1, argv); | 1772 (void)onmessage_fun->Call(context, global, 1, argv); |
1898 } | 1773 } |
1899 delete data; | 1774 delete data; |
1900 } | 1775 } |
1901 } | 1776 } |
1902 } | 1777 } |
1903 } | 1778 } |
1904 } | 1779 } |
1905 Shell::CollectGarbage(isolate); | 1780 Shell::CollectGarbage(isolate); |
1906 } | 1781 } |
1907 isolate->Dispose(); | 1782 isolate->Dispose(); |
1908 | 1783 |
1909 // Post NULL to wake the thread waiting on GetMessage() if there is one. | 1784 // Post NULL to wake the thread waiting on GetMessage() if there is one. |
1910 out_queue_.Enqueue(NULL); | 1785 out_queue_.Enqueue(NULL); |
1911 out_semaphore_.Signal(); | 1786 out_semaphore_.Signal(); |
1912 } | 1787 } |
1913 | 1788 |
1914 | 1789 |
1915 void Worker::PostMessageOut(const v8::FunctionCallbackInfo<v8::Value>& args) { | 1790 void Worker::PostMessageOut(const v8::FunctionCallbackInfo<v8::Value>& args) { |
1916 Isolate* isolate = args.GetIsolate(); | 1791 Isolate* isolate = args.GetIsolate(); |
1917 HandleScope handle_scope(isolate); | 1792 HandleScope handle_scope(isolate); |
1918 | 1793 |
1919 if (args.Length() < 1) { | 1794 if (args.Length() < 1) { |
1920 Throw(isolate, "Invalid argument"); | 1795 Throw(isolate, "Invalid argument"); |
1921 return; | 1796 return; |
1922 } | 1797 } |
1923 | 1798 |
1924 Local<Value> message = args[0]; | 1799 Handle<Value> message = args[0]; |
1925 | 1800 |
1926 // TODO(binji): Allow transferring from worker to main thread? | 1801 // TODO(binji): Allow transferring from worker to main thread? |
1927 Shell::ObjectList to_transfer; | 1802 Shell::ObjectList to_transfer; |
1928 | 1803 |
1929 Shell::ObjectList seen_objects; | 1804 Shell::ObjectList seen_objects; |
1930 SerializationData* data = new SerializationData; | 1805 SerializationData* data = new SerializationData; |
1931 if (Shell::SerializeValue(isolate, message, to_transfer, &seen_objects, | 1806 if (Shell::SerializeValue(isolate, message, to_transfer, &seen_objects, |
1932 data)) { | 1807 data)) { |
1933 DCHECK(args.Data()->IsExternal()); | 1808 DCHECK(args.Data()->IsExternal()); |
1934 Local<External> this_value = Local<External>::Cast(args.Data()); | 1809 Handle<External> this_value = Handle<External>::Cast(args.Data()); |
1935 Worker* worker = static_cast<Worker*>(this_value->Value()); | 1810 Worker* worker = static_cast<Worker*>(this_value->Value()); |
1936 worker->out_queue_.Enqueue(data); | 1811 worker->out_queue_.Enqueue(data); |
1937 worker->out_semaphore_.Signal(); | 1812 worker->out_semaphore_.Signal(); |
1938 } else { | 1813 } else { |
1939 delete data; | 1814 delete data; |
1940 } | 1815 } |
1941 } | 1816 } |
1942 #endif // !V8_SHARED | 1817 #endif // !V8_SHARED |
1943 | 1818 |
1944 | 1819 |
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2134 } | 2009 } |
2135 } | 2010 } |
2136 | 2011 |
2137 | 2012 |
2138 void Shell::EmptyMessageQueues(Isolate* isolate) { | 2013 void Shell::EmptyMessageQueues(Isolate* isolate) { |
2139 while (v8::platform::PumpMessageLoop(g_platform, isolate)) continue; | 2014 while (v8::platform::PumpMessageLoop(g_platform, isolate)) continue; |
2140 } | 2015 } |
2141 | 2016 |
2142 | 2017 |
2143 #ifndef V8_SHARED | 2018 #ifndef V8_SHARED |
2144 bool Shell::SerializeValue(Isolate* isolate, Local<Value> value, | 2019 bool Shell::SerializeValue(Isolate* isolate, Handle<Value> value, |
2145 const ObjectList& to_transfer, | 2020 const ObjectList& to_transfer, |
2146 ObjectList* seen_objects, | 2021 ObjectList* seen_objects, |
2147 SerializationData* out_data) { | 2022 SerializationData* out_data) { |
2148 DCHECK(out_data); | 2023 DCHECK(out_data); |
2149 Local<Context> context = isolate->GetCurrentContext(); | 2024 Local<Context> context = isolate->GetCurrentContext(); |
2150 | 2025 |
2151 if (value->IsUndefined()) { | 2026 if (value->IsUndefined()) { |
2152 out_data->WriteTag(kSerializationTagUndefined); | 2027 out_data->WriteTag(kSerializationTagUndefined); |
2153 } else if (value->IsNull()) { | 2028 } else if (value->IsNull()) { |
2154 out_data->WriteTag(kSerializationTagNull); | 2029 out_data->WriteTag(kSerializationTagNull); |
2155 } else if (value->IsTrue()) { | 2030 } else if (value->IsTrue()) { |
2156 out_data->WriteTag(kSerializationTagTrue); | 2031 out_data->WriteTag(kSerializationTagTrue); |
2157 } else if (value->IsFalse()) { | 2032 } else if (value->IsFalse()) { |
2158 out_data->WriteTag(kSerializationTagFalse); | 2033 out_data->WriteTag(kSerializationTagFalse); |
2159 } else if (value->IsNumber()) { | 2034 } else if (value->IsNumber()) { |
2160 Local<Number> num = Local<Number>::Cast(value); | 2035 Handle<Number> num = Handle<Number>::Cast(value); |
2161 double value = num->Value(); | 2036 double value = num->Value(); |
2162 out_data->WriteTag(kSerializationTagNumber); | 2037 out_data->WriteTag(kSerializationTagNumber); |
2163 out_data->Write(value); | 2038 out_data->Write(value); |
2164 } else if (value->IsString()) { | 2039 } else if (value->IsString()) { |
2165 v8::String::Utf8Value str(value); | 2040 v8::String::Utf8Value str(value); |
2166 out_data->WriteTag(kSerializationTagString); | 2041 out_data->WriteTag(kSerializationTagString); |
2167 out_data->Write(str.length()); | 2042 out_data->Write(str.length()); |
2168 out_data->WriteMemory(*str, str.length()); | 2043 out_data->WriteMemory(*str, str.length()); |
2169 } else if (value->IsArray()) { | 2044 } else if (value->IsArray()) { |
2170 Local<Array> array = Local<Array>::Cast(value); | 2045 Handle<Array> array = Handle<Array>::Cast(value); |
2171 if (FindInObjectList(array, *seen_objects)) { | 2046 if (FindInObjectList(array, *seen_objects)) { |
2172 Throw(isolate, "Duplicated arrays not supported"); | 2047 Throw(isolate, "Duplicated arrays not supported"); |
2173 return false; | 2048 return false; |
2174 } | 2049 } |
2175 seen_objects->Add(array); | 2050 seen_objects->Add(array); |
2176 out_data->WriteTag(kSerializationTagArray); | 2051 out_data->WriteTag(kSerializationTagArray); |
2177 uint32_t length = array->Length(); | 2052 uint32_t length = array->Length(); |
2178 out_data->Write(length); | 2053 out_data->Write(length); |
2179 for (uint32_t i = 0; i < length; ++i) { | 2054 for (uint32_t i = 0; i < length; ++i) { |
2180 Local<Value> element_value; | 2055 Local<Value> element_value; |
2181 if (array->Get(context, i).ToLocal(&element_value)) { | 2056 if (array->Get(context, i).ToLocal(&element_value)) { |
2182 if (!SerializeValue(isolate, element_value, to_transfer, seen_objects, | 2057 if (!SerializeValue(isolate, element_value, to_transfer, seen_objects, |
2183 out_data)) | 2058 out_data)) |
2184 return false; | 2059 return false; |
2185 } else { | 2060 } else { |
2186 Throw(isolate, "Failed to serialize array element."); | 2061 Throw(isolate, "Failed to serialize array element."); |
2187 return false; | 2062 return false; |
2188 } | 2063 } |
2189 } | 2064 } |
2190 } else if (value->IsArrayBuffer()) { | 2065 } else if (value->IsArrayBuffer()) { |
2191 Local<ArrayBuffer> array_buffer = Local<ArrayBuffer>::Cast(value); | 2066 Handle<ArrayBuffer> array_buffer = Handle<ArrayBuffer>::Cast(value); |
2192 if (FindInObjectList(array_buffer, *seen_objects)) { | 2067 if (FindInObjectList(array_buffer, *seen_objects)) { |
2193 Throw(isolate, "Duplicated array buffers not supported"); | 2068 Throw(isolate, "Duplicated array buffers not supported"); |
2194 return false; | 2069 return false; |
2195 } | 2070 } |
2196 seen_objects->Add(array_buffer); | 2071 seen_objects->Add(array_buffer); |
2197 if (FindInObjectList(array_buffer, to_transfer)) { | 2072 if (FindInObjectList(array_buffer, to_transfer)) { |
2198 // Transfer ArrayBuffer | 2073 // Transfer ArrayBuffer |
2199 if (!array_buffer->IsNeuterable()) { | 2074 if (!array_buffer->IsNeuterable()) { |
2200 Throw(isolate, "Attempting to transfer an un-neuterable ArrayBuffer"); | 2075 Throw(isolate, "Attempting to transfer an un-neuterable ArrayBuffer"); |
2201 return false; | 2076 return false; |
(...skipping 12 matching lines...) Expand all Loading... |
2214 return false; | 2089 return false; |
2215 } | 2090 } |
2216 | 2091 |
2217 int byte_length = static_cast<int>(contents.ByteLength()); | 2092 int byte_length = static_cast<int>(contents.ByteLength()); |
2218 out_data->WriteTag(kSerializationTagArrayBuffer); | 2093 out_data->WriteTag(kSerializationTagArrayBuffer); |
2219 out_data->Write(byte_length); | 2094 out_data->Write(byte_length); |
2220 out_data->WriteMemory(contents.Data(), | 2095 out_data->WriteMemory(contents.Data(), |
2221 static_cast<int>(contents.ByteLength())); | 2096 static_cast<int>(contents.ByteLength())); |
2222 } | 2097 } |
2223 } else if (value->IsSharedArrayBuffer()) { | 2098 } else if (value->IsSharedArrayBuffer()) { |
2224 Local<SharedArrayBuffer> sab = Local<SharedArrayBuffer>::Cast(value); | 2099 Handle<SharedArrayBuffer> sab = Handle<SharedArrayBuffer>::Cast(value); |
2225 if (FindInObjectList(sab, *seen_objects)) { | 2100 if (FindInObjectList(sab, *seen_objects)) { |
2226 Throw(isolate, "Duplicated shared array buffers not supported"); | 2101 Throw(isolate, "Duplicated shared array buffers not supported"); |
2227 return false; | 2102 return false; |
2228 } | 2103 } |
2229 seen_objects->Add(sab); | 2104 seen_objects->Add(sab); |
2230 if (!FindInObjectList(sab, to_transfer)) { | 2105 if (!FindInObjectList(sab, to_transfer)) { |
2231 Throw(isolate, "SharedArrayBuffer must be transferred"); | 2106 Throw(isolate, "SharedArrayBuffer must be transferred"); |
2232 return false; | 2107 return false; |
2233 } | 2108 } |
2234 | 2109 |
2235 SharedArrayBuffer::Contents contents; | 2110 SharedArrayBuffer::Contents contents; |
2236 if (sab->IsExternal()) { | 2111 if (sab->IsExternal()) { |
2237 contents = sab->GetContents(); | 2112 contents = sab->GetContents(); |
2238 } else { | 2113 } else { |
2239 contents = sab->Externalize(); | 2114 contents = sab->Externalize(); |
2240 externalized_shared_contents_.Add(contents); | 2115 externalized_shared_contents_.Add(contents); |
2241 } | 2116 } |
2242 out_data->WriteSharedArrayBufferContents(contents); | 2117 out_data->WriteSharedArrayBufferContents(contents); |
2243 } else if (value->IsObject()) { | 2118 } else if (value->IsObject()) { |
2244 Local<Object> object = Local<Object>::Cast(value); | 2119 Handle<Object> object = Handle<Object>::Cast(value); |
2245 if (FindInObjectList(object, *seen_objects)) { | 2120 if (FindInObjectList(object, *seen_objects)) { |
2246 Throw(isolate, "Duplicated objects not supported"); | 2121 Throw(isolate, "Duplicated objects not supported"); |
2247 return false; | 2122 return false; |
2248 } | 2123 } |
2249 seen_objects->Add(object); | 2124 seen_objects->Add(object); |
2250 Local<Array> property_names; | 2125 Local<Array> property_names; |
2251 if (!object->GetOwnPropertyNames(context).ToLocal(&property_names)) { | 2126 if (!object->GetOwnPropertyNames(context).ToLocal(&property_names)) { |
2252 Throw(isolate, "Unable to get property names"); | 2127 Throw(isolate, "Unable to get property names"); |
2253 return false; | 2128 return false; |
2254 } | 2129 } |
2255 | 2130 |
2256 uint32_t length = property_names->Length(); | 2131 uint32_t length = property_names->Length(); |
2257 out_data->WriteTag(kSerializationTagObject); | 2132 out_data->WriteTag(kSerializationTagObject); |
2258 out_data->Write(length); | 2133 out_data->Write(length); |
2259 for (uint32_t i = 0; i < length; ++i) { | 2134 for (uint32_t i = 0; i < length; ++i) { |
2260 Local<Value> name; | 2135 Handle<Value> name; |
2261 Local<Value> property_value; | 2136 Handle<Value> property_value; |
2262 if (property_names->Get(context, i).ToLocal(&name) && | 2137 if (property_names->Get(context, i).ToLocal(&name) && |
2263 object->Get(context, name).ToLocal(&property_value)) { | 2138 object->Get(context, name).ToLocal(&property_value)) { |
2264 if (!SerializeValue(isolate, name, to_transfer, seen_objects, out_data)) | 2139 if (!SerializeValue(isolate, name, to_transfer, seen_objects, out_data)) |
2265 return false; | 2140 return false; |
2266 if (!SerializeValue(isolate, property_value, to_transfer, seen_objects, | 2141 if (!SerializeValue(isolate, property_value, to_transfer, seen_objects, |
2267 out_data)) | 2142 out_data)) |
2268 return false; | 2143 return false; |
2269 } else { | 2144 } else { |
2270 Throw(isolate, "Failed to serialize property."); | 2145 Throw(isolate, "Failed to serialize property."); |
2271 return false; | 2146 return false; |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2304 result = False(isolate); | 2179 result = False(isolate); |
2305 break; | 2180 break; |
2306 case kSerializationTagNumber: | 2181 case kSerializationTagNumber: |
2307 result = Number::New(isolate, data.Read<double>(offset)); | 2182 result = Number::New(isolate, data.Read<double>(offset)); |
2308 break; | 2183 break; |
2309 case kSerializationTagString: { | 2184 case kSerializationTagString: { |
2310 int length = data.Read<int>(offset); | 2185 int length = data.Read<int>(offset); |
2311 CHECK(length >= 0); | 2186 CHECK(length >= 0); |
2312 std::vector<char> buffer(length + 1); // + 1 so it is never empty. | 2187 std::vector<char> buffer(length + 1); // + 1 so it is never empty. |
2313 data.ReadMemory(&buffer[0], length, offset); | 2188 data.ReadMemory(&buffer[0], length, offset); |
2314 MaybeLocal<String> str = | 2189 MaybeLocal<String> str = String::NewFromUtf8( |
2315 String::NewFromUtf8(isolate, &buffer[0], NewStringType::kNormal, | 2190 isolate, &buffer[0], String::kNormalString, length); |
2316 length).ToLocalChecked(); | |
2317 if (!str.IsEmpty()) result = str.ToLocalChecked(); | 2191 if (!str.IsEmpty()) result = str.ToLocalChecked(); |
2318 break; | 2192 break; |
2319 } | 2193 } |
2320 case kSerializationTagArray: { | 2194 case kSerializationTagArray: { |
2321 uint32_t length = data.Read<uint32_t>(offset); | 2195 uint32_t length = data.Read<uint32_t>(offset); |
2322 Local<Array> array = Array::New(isolate, length); | 2196 Handle<Array> array = Array::New(isolate, length); |
2323 for (uint32_t i = 0; i < length; ++i) { | 2197 for (uint32_t i = 0; i < length; ++i) { |
2324 Local<Value> element_value; | 2198 Local<Value> element_value; |
2325 CHECK(DeserializeValue(isolate, data, offset).ToLocal(&element_value)); | 2199 CHECK(DeserializeValue(isolate, data, offset).ToLocal(&element_value)); |
2326 array->Set(isolate->GetCurrentContext(), i, element_value).FromJust(); | 2200 array->Set(i, element_value); |
2327 } | 2201 } |
2328 result = array; | 2202 result = array; |
2329 break; | 2203 break; |
2330 } | 2204 } |
2331 case kSerializationTagObject: { | 2205 case kSerializationTagObject: { |
2332 int length = data.Read<int>(offset); | 2206 int length = data.Read<int>(offset); |
2333 Local<Object> object = Object::New(isolate); | 2207 Handle<Object> object = Object::New(isolate); |
2334 for (int i = 0; i < length; ++i) { | 2208 for (int i = 0; i < length; ++i) { |
2335 Local<Value> property_name; | 2209 Local<Value> property_name; |
2336 CHECK(DeserializeValue(isolate, data, offset).ToLocal(&property_name)); | 2210 CHECK(DeserializeValue(isolate, data, offset).ToLocal(&property_name)); |
2337 Local<Value> property_value; | 2211 Local<Value> property_value; |
2338 CHECK(DeserializeValue(isolate, data, offset).ToLocal(&property_value)); | 2212 CHECK(DeserializeValue(isolate, data, offset).ToLocal(&property_value)); |
2339 object->Set(isolate->GetCurrentContext(), property_name, property_value) | 2213 object->Set(property_name, property_value); |
2340 .FromJust(); | |
2341 } | 2214 } |
2342 result = object; | 2215 result = object; |
2343 break; | 2216 break; |
2344 } | 2217 } |
2345 case kSerializationTagArrayBuffer: { | 2218 case kSerializationTagArrayBuffer: { |
2346 int byte_length = data.Read<int>(offset); | 2219 int byte_length = data.Read<int>(offset); |
2347 Local<ArrayBuffer> array_buffer = ArrayBuffer::New(isolate, byte_length); | 2220 Handle<ArrayBuffer> array_buffer = ArrayBuffer::New(isolate, byte_length); |
2348 ArrayBuffer::Contents contents = array_buffer->GetContents(); | 2221 ArrayBuffer::Contents contents = array_buffer->GetContents(); |
2349 DCHECK(static_cast<size_t>(byte_length) == contents.ByteLength()); | 2222 DCHECK(static_cast<size_t>(byte_length) == contents.ByteLength()); |
2350 data.ReadMemory(contents.Data(), byte_length, offset); | 2223 data.ReadMemory(contents.Data(), byte_length, offset); |
2351 result = array_buffer; | 2224 result = array_buffer; |
2352 break; | 2225 break; |
2353 } | 2226 } |
2354 case kSerializationTagTransferredArrayBuffer: { | 2227 case kSerializationTagTransferredArrayBuffer: { |
2355 ArrayBuffer::Contents contents; | 2228 ArrayBuffer::Contents contents; |
2356 data.ReadArrayBufferContents(&contents, offset); | 2229 data.ReadArrayBufferContents(&contents, offset); |
2357 result = ArrayBuffer::New(isolate, contents.Data(), contents.ByteLength(), | 2230 result = ArrayBuffer::New(isolate, contents.Data(), contents.ByteLength(), |
(...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2597 } | 2470 } |
2598 | 2471 |
2599 } // namespace v8 | 2472 } // namespace v8 |
2600 | 2473 |
2601 | 2474 |
2602 #ifndef GOOGLE3 | 2475 #ifndef GOOGLE3 |
2603 int main(int argc, char* argv[]) { | 2476 int main(int argc, char* argv[]) { |
2604 return v8::Shell::Main(argc, argv); | 2477 return v8::Shell::Main(argc, argv); |
2605 } | 2478 } |
2606 #endif | 2479 #endif |
OLD | NEW |