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