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 ScriptCompiler::CompileUnboundScript(temp_isolate, &script_source, |
269 compile_options); | 275 compile_options).ToLocalChecked(); |
270 if (script_source.GetCachedData()) { | 276 if (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 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
728 | 759 |
729 Local<Value> this_value = args.This()->GetInternalField(0); | 760 Local<Value> this_value = args.This()->GetInternalField(0); |
730 if (!this_value->IsExternal()) { | 761 if (!this_value->IsExternal()) { |
731 Throw(isolate, "this is not a Worker"); | 762 Throw(isolate, "this is not a Worker"); |
732 return; | 763 return; |
733 } | 764 } |
734 | 765 |
735 Worker* worker = | 766 Worker* worker = |
736 static_cast<Worker*>(Local<External>::Cast(this_value)->Value()); | 767 static_cast<Worker*>(Local<External>::Cast(this_value)->Value()); |
737 | 768 |
738 Handle<Value> message = args[0]; | 769 Local<Value> message = args[0]; |
739 ObjectList to_transfer; | 770 ObjectList to_transfer; |
740 if (args.Length() >= 2) { | 771 if (args.Length() >= 2) { |
741 if (!args[1]->IsArray()) { | 772 if (!args[1]->IsArray()) { |
742 Throw(isolate, "Transfer list must be an Array"); | 773 Throw(isolate, "Transfer list must be an Array"); |
743 return; | 774 return; |
744 } | 775 } |
745 | 776 |
746 Handle<Array> transfer = Handle<Array>::Cast(args[1]); | 777 Local<Array> transfer = Local<Array>::Cast(args[1]); |
747 uint32_t length = transfer->Length(); | 778 uint32_t length = transfer->Length(); |
748 for (uint32_t i = 0; i < length; ++i) { | 779 for (uint32_t i = 0; i < length; ++i) { |
749 Handle<Value> element; | 780 Local<Value> element; |
750 if (transfer->Get(context, i).ToLocal(&element)) { | 781 if (transfer->Get(context, i).ToLocal(&element)) { |
751 if (!element->IsArrayBuffer() && !element->IsSharedArrayBuffer()) { | 782 if (!element->IsArrayBuffer() && !element->IsSharedArrayBuffer()) { |
752 Throw(isolate, | 783 Throw(isolate, |
753 "Transfer array elements must be an ArrayBuffer or " | 784 "Transfer array elements must be an ArrayBuffer or " |
754 "SharedArrayBuffer."); | 785 "SharedArrayBuffer."); |
755 break; | 786 break; |
756 } | 787 } |
757 | 788 |
758 to_transfer.Add(Handle<Object>::Cast(element)); | 789 to_transfer.Add(Local<Object>::Cast(element)); |
759 } | 790 } |
760 } | 791 } |
761 } | 792 } |
762 | 793 |
763 ObjectList seen_objects; | 794 ObjectList seen_objects; |
764 SerializationData* data = new SerializationData; | 795 SerializationData* data = new SerializationData; |
765 if (SerializeValue(isolate, message, to_transfer, &seen_objects, data)) { | 796 if (SerializeValue(isolate, message, to_transfer, &seen_objects, data)) { |
766 worker->PostMessage(data); | 797 worker->PostMessage(data); |
767 } else { | 798 } else { |
768 delete data; | 799 delete data; |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
805 } | 836 } |
806 | 837 |
807 Worker* worker = | 838 Worker* worker = |
808 static_cast<Worker*>(Local<External>::Cast(this_value)->Value()); | 839 static_cast<Worker*>(Local<External>::Cast(this_value)->Value()); |
809 worker->Terminate(); | 840 worker->Terminate(); |
810 } | 841 } |
811 #endif // !V8_SHARED | 842 #endif // !V8_SHARED |
812 | 843 |
813 | 844 |
814 void Shell::QuitOnce(v8::FunctionCallbackInfo<v8::Value>* args) { | 845 void Shell::QuitOnce(v8::FunctionCallbackInfo<v8::Value>* args) { |
815 int exit_code = (*args)[0]->Int32Value(); | 846 int exit_code = (*args)[0] |
| 847 ->Int32Value(args->GetIsolate()->GetCurrentContext()) |
| 848 .FromMaybe(0); |
816 #ifndef V8_SHARED | 849 #ifndef V8_SHARED |
817 CleanupWorkers(); | 850 CleanupWorkers(); |
818 #endif // !V8_SHARED | 851 #endif // !V8_SHARED |
819 OnExit(args->GetIsolate()); | 852 OnExit(args->GetIsolate()); |
820 exit(exit_code); | 853 exit(exit_code); |
821 } | 854 } |
822 | 855 |
823 | 856 |
824 void Shell::Quit(const v8::FunctionCallbackInfo<v8::Value>& args) { | 857 void Shell::Quit(const v8::FunctionCallbackInfo<v8::Value>& args) { |
825 base::CallOnce(&quit_once_, &QuitOnce, | 858 base::CallOnce(&quit_once_, &QuitOnce, |
826 const_cast<v8::FunctionCallbackInfo<v8::Value>*>(&args)); | 859 const_cast<v8::FunctionCallbackInfo<v8::Value>*>(&args)); |
827 } | 860 } |
828 | 861 |
829 | 862 |
830 void Shell::Version(const v8::FunctionCallbackInfo<v8::Value>& args) { | 863 void Shell::Version(const v8::FunctionCallbackInfo<v8::Value>& args) { |
831 args.GetReturnValue().Set( | 864 args.GetReturnValue().Set( |
832 String::NewFromUtf8(args.GetIsolate(), V8::GetVersion())); | 865 String::NewFromUtf8(args.GetIsolate(), V8::GetVersion(), |
| 866 NewStringType::kNormal).ToLocalChecked()); |
833 } | 867 } |
834 | 868 |
835 | 869 |
836 void Shell::ReportException(Isolate* isolate, v8::TryCatch* try_catch) { | 870 void Shell::ReportException(Isolate* isolate, v8::TryCatch* try_catch) { |
837 HandleScope handle_scope(isolate); | 871 HandleScope handle_scope(isolate); |
838 #ifndef V8_SHARED | 872 #ifndef V8_SHARED |
839 Handle<Context> utility_context; | 873 Local<Context> utility_context; |
840 bool enter_context = !isolate->InContext(); | 874 bool enter_context = !isolate->InContext(); |
841 if (enter_context) { | 875 if (enter_context) { |
842 utility_context = Local<Context>::New(isolate, utility_context_); | 876 utility_context = Local<Context>::New(isolate, utility_context_); |
843 utility_context->Enter(); | 877 utility_context->Enter(); |
844 } | 878 } |
845 #endif // !V8_SHARED | 879 #endif // !V8_SHARED |
846 v8::String::Utf8Value exception(try_catch->Exception()); | 880 v8::String::Utf8Value exception(try_catch->Exception()); |
847 const char* exception_string = ToCString(exception); | 881 const char* exception_string = ToCString(exception); |
848 Handle<Message> message = try_catch->Message(); | 882 Local<Message> message = try_catch->Message(); |
849 if (message.IsEmpty()) { | 883 if (message.IsEmpty()) { |
850 // V8 didn't provide any extra information about this error; just | 884 // V8 didn't provide any extra information about this error; just |
851 // print the exception. | 885 // print the exception. |
852 printf("%s\n", exception_string); | 886 printf("%s\n", exception_string); |
853 } else { | 887 } else { |
854 // Print (filename):(line number): (message). | 888 // Print (filename):(line number): (message). |
855 v8::String::Utf8Value filename(message->GetScriptOrigin().ResourceName()); | 889 v8::String::Utf8Value filename(message->GetScriptOrigin().ResourceName()); |
856 const char* filename_string = ToCString(filename); | 890 const char* filename_string = ToCString(filename); |
857 int linenum = message->GetLineNumber(); | 891 int linenum = |
| 892 message->GetLineNumber(isolate->GetCurrentContext()).FromJust(); |
858 printf("%s:%i: %s\n", filename_string, linenum, exception_string); | 893 printf("%s:%i: %s\n", filename_string, linenum, exception_string); |
859 // Print line of source code. | 894 // Print line of source code. |
860 v8::String::Utf8Value sourceline(message->GetSourceLine()); | 895 v8::String::Utf8Value sourceline( |
| 896 message->GetSourceLine(isolate->GetCurrentContext()).ToLocalChecked()); |
861 const char* sourceline_string = ToCString(sourceline); | 897 const char* sourceline_string = ToCString(sourceline); |
862 printf("%s\n", sourceline_string); | 898 printf("%s\n", sourceline_string); |
863 // Print wavy underline (GetUnderline is deprecated). | 899 // Print wavy underline (GetUnderline is deprecated). |
864 int start = message->GetStartColumn(); | 900 int start = |
| 901 message->GetStartColumn(isolate->GetCurrentContext()).FromJust(); |
865 for (int i = 0; i < start; i++) { | 902 for (int i = 0; i < start; i++) { |
866 printf(" "); | 903 printf(" "); |
867 } | 904 } |
868 int end = message->GetEndColumn(); | 905 int end = message->GetEndColumn(isolate->GetCurrentContext()).FromJust(); |
869 for (int i = start; i < end; i++) { | 906 for (int i = start; i < end; i++) { |
870 printf("^"); | 907 printf("^"); |
871 } | 908 } |
872 printf("\n"); | 909 printf("\n"); |
873 v8::String::Utf8Value stack_trace(try_catch->StackTrace()); | 910 Local<Value> stack_trace_string; |
874 if (stack_trace.length() > 0) { | 911 if (try_catch->StackTrace(isolate->GetCurrentContext()) |
875 const char* stack_trace_string = ToCString(stack_trace); | 912 .ToLocal(&stack_trace_string)) { |
876 printf("%s\n", stack_trace_string); | 913 v8::String::Utf8Value stack_trace( |
| 914 Local<String>::Cast(stack_trace_string)); |
| 915 printf("%s\n", ToCString(stack_trace)); |
877 } | 916 } |
878 } | 917 } |
879 printf("\n"); | 918 printf("\n"); |
880 #ifndef V8_SHARED | 919 #ifndef V8_SHARED |
881 if (enter_context) utility_context->Exit(); | 920 if (enter_context) utility_context->Exit(); |
882 #endif // !V8_SHARED | 921 #endif // !V8_SHARED |
883 } | 922 } |
884 | 923 |
885 | 924 |
886 #ifndef V8_SHARED | 925 #ifndef V8_SHARED |
887 Handle<Array> Shell::GetCompletions(Isolate* isolate, | 926 Local<Array> Shell::GetCompletions(Isolate* isolate, Local<String> text, |
888 Handle<String> text, | 927 Local<String> full) { |
889 Handle<String> full) { | |
890 EscapableHandleScope handle_scope(isolate); | 928 EscapableHandleScope handle_scope(isolate); |
891 v8::Local<v8::Context> utility_context = | 929 v8::Local<v8::Context> utility_context = |
892 v8::Local<v8::Context>::New(isolate, utility_context_); | 930 v8::Local<v8::Context>::New(isolate, utility_context_); |
893 v8::Context::Scope context_scope(utility_context); | 931 v8::Context::Scope context_scope(utility_context); |
894 Handle<Object> global = utility_context->Global(); | 932 Local<Object> global = utility_context->Global(); |
895 Local<Value> fun = | 933 Local<Value> fun = global->Get(utility_context, |
896 global->Get(String::NewFromUtf8(isolate, "GetCompletions")); | 934 String::NewFromUtf8(isolate, "GetCompletions", |
| 935 NewStringType::kNormal) |
| 936 .ToLocalChecked()).ToLocalChecked(); |
897 static const int kArgc = 3; | 937 static const int kArgc = 3; |
898 v8::Local<v8::Context> evaluation_context = | 938 v8::Local<v8::Context> evaluation_context = |
899 v8::Local<v8::Context>::New(isolate, evaluation_context_); | 939 v8::Local<v8::Context>::New(isolate, evaluation_context_); |
900 Handle<Value> argv[kArgc] = { evaluation_context->Global(), text, full }; | 940 Local<Value> argv[kArgc] = {evaluation_context->Global(), text, full}; |
901 Local<Value> val = Local<Function>::Cast(fun)->Call(global, kArgc, argv); | 941 Local<Value> val = Local<Function>::Cast(fun) |
| 942 ->Call(utility_context, global, kArgc, argv) |
| 943 .ToLocalChecked(); |
902 return handle_scope.Escape(Local<Array>::Cast(val)); | 944 return handle_scope.Escape(Local<Array>::Cast(val)); |
903 } | 945 } |
904 | 946 |
905 | 947 |
906 Local<Object> Shell::DebugMessageDetails(Isolate* isolate, | 948 Local<Object> Shell::DebugMessageDetails(Isolate* isolate, |
907 Handle<String> message) { | 949 Local<String> message) { |
908 EscapableHandleScope handle_scope(isolate); | 950 EscapableHandleScope handle_scope(isolate); |
909 v8::Local<v8::Context> context = | 951 v8::Local<v8::Context> context = |
910 v8::Local<v8::Context>::New(isolate, utility_context_); | 952 v8::Local<v8::Context>::New(isolate, utility_context_); |
911 v8::Context::Scope context_scope(context); | 953 v8::Context::Scope context_scope(context); |
912 Handle<Object> global = context->Global(); | 954 Local<Object> global = context->Global(); |
913 Handle<Value> fun = | 955 Local<Value> fun = |
914 global->Get(String::NewFromUtf8(isolate, "DebugMessageDetails")); | 956 global->Get(context, String::NewFromUtf8(isolate, "DebugMessageDetails", |
| 957 NewStringType::kNormal) |
| 958 .ToLocalChecked()).ToLocalChecked(); |
915 static const int kArgc = 1; | 959 static const int kArgc = 1; |
916 Handle<Value> argv[kArgc] = { message }; | 960 Local<Value> argv[kArgc] = {message}; |
917 Handle<Value> val = Handle<Function>::Cast(fun)->Call(global, kArgc, argv); | 961 Local<Value> val = Local<Function>::Cast(fun) |
918 return handle_scope.Escape(Local<Object>(Handle<Object>::Cast(val))); | 962 ->Call(context, global, kArgc, argv) |
| 963 .ToLocalChecked(); |
| 964 return handle_scope.Escape(Local<Object>(Local<Object>::Cast(val))); |
919 } | 965 } |
920 | 966 |
921 | 967 |
922 Local<Value> Shell::DebugCommandToJSONRequest(Isolate* isolate, | 968 Local<Value> Shell::DebugCommandToJSONRequest(Isolate* isolate, |
923 Handle<String> command) { | 969 Local<String> command) { |
924 EscapableHandleScope handle_scope(isolate); | 970 EscapableHandleScope handle_scope(isolate); |
925 v8::Local<v8::Context> context = | 971 v8::Local<v8::Context> context = |
926 v8::Local<v8::Context>::New(isolate, utility_context_); | 972 v8::Local<v8::Context>::New(isolate, utility_context_); |
927 v8::Context::Scope context_scope(context); | 973 v8::Context::Scope context_scope(context); |
928 Handle<Object> global = context->Global(); | 974 Local<Object> global = context->Global(); |
929 Handle<Value> fun = | 975 Local<Value> fun = |
930 global->Get(String::NewFromUtf8(isolate, "DebugCommandToJSONRequest")); | 976 global->Get(context, |
| 977 String::NewFromUtf8(isolate, "DebugCommandToJSONRequest", |
| 978 NewStringType::kNormal).ToLocalChecked()) |
| 979 .ToLocalChecked(); |
931 static const int kArgc = 1; | 980 static const int kArgc = 1; |
932 Handle<Value> argv[kArgc] = { command }; | 981 Local<Value> argv[kArgc] = {command}; |
933 Handle<Value> val = Handle<Function>::Cast(fun)->Call(global, kArgc, argv); | 982 Local<Value> val = Local<Function>::Cast(fun) |
| 983 ->Call(context, global, kArgc, argv) |
| 984 .ToLocalChecked(); |
934 return handle_scope.Escape(Local<Value>(val)); | 985 return handle_scope.Escape(Local<Value>(val)); |
935 } | 986 } |
936 | 987 |
937 | 988 |
938 int32_t* Counter::Bind(const char* name, bool is_histogram) { | 989 int32_t* Counter::Bind(const char* name, bool is_histogram) { |
939 int i; | 990 int i; |
940 for (i = 0; i < kMaxNameSize - 1 && name[i]; i++) | 991 for (i = 0; i < kMaxNameSize - 1 && name[i]; i++) |
941 name_[i] = static_cast<char>(name[i]); | 992 name_[i] = static_cast<char>(name[i]); |
942 name_[i] = '\0'; | 993 name_[i] = '\0'; |
943 is_histogram_ = is_histogram; | 994 is_histogram_ = is_histogram; |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1058 evaluation_context->SetSecurityToken(Undefined(isolate)); | 1109 evaluation_context->SetSecurityToken(Undefined(isolate)); |
1059 v8::Context::Scope context_scope(utility_context); | 1110 v8::Context::Scope context_scope(utility_context); |
1060 | 1111 |
1061 if (i::FLAG_debugger) printf("JavaScript debugger enabled\n"); | 1112 if (i::FLAG_debugger) printf("JavaScript debugger enabled\n"); |
1062 // Install the debugger object in the utility scope | 1113 // Install the debugger object in the utility scope |
1063 i::Debug* debug = reinterpret_cast<i::Isolate*>(isolate)->debug(); | 1114 i::Debug* debug = reinterpret_cast<i::Isolate*>(isolate)->debug(); |
1064 debug->Load(); | 1115 debug->Load(); |
1065 i::Handle<i::Context> debug_context = debug->debug_context(); | 1116 i::Handle<i::Context> debug_context = debug->debug_context(); |
1066 i::Handle<i::JSObject> js_debug | 1117 i::Handle<i::JSObject> js_debug |
1067 = i::Handle<i::JSObject>(debug_context->global_object()); | 1118 = i::Handle<i::JSObject>(debug_context->global_object()); |
1068 utility_context->Global()->Set(String::NewFromUtf8(isolate, "$debug"), | 1119 utility_context->Global() |
1069 Utils::ToLocal(js_debug)); | 1120 ->Set(utility_context, |
| 1121 String::NewFromUtf8(isolate, "$debug", NewStringType::kNormal) |
| 1122 .ToLocalChecked(), |
| 1123 Utils::ToLocal(js_debug)) |
| 1124 .FromJust(); |
1070 debug_context->set_security_token( | 1125 debug_context->set_security_token( |
1071 reinterpret_cast<i::Isolate*>(isolate)->heap()->undefined_value()); | 1126 reinterpret_cast<i::Isolate*>(isolate)->heap()->undefined_value()); |
1072 | 1127 |
1073 // Run the d8 shell utility script in the utility context | 1128 // Run the d8 shell utility script in the utility context |
1074 int source_index = i::NativesCollection<i::D8>::GetIndex("d8"); | 1129 int source_index = i::NativesCollection<i::D8>::GetIndex("d8"); |
1075 i::Vector<const char> shell_source = | 1130 i::Vector<const char> shell_source = |
1076 i::NativesCollection<i::D8>::GetScriptSource(source_index); | 1131 i::NativesCollection<i::D8>::GetScriptSource(source_index); |
1077 i::Vector<const char> shell_source_name = | 1132 i::Vector<const char> shell_source_name = |
1078 i::NativesCollection<i::D8>::GetScriptName(source_index); | 1133 i::NativesCollection<i::D8>::GetScriptName(source_index); |
1079 Handle<String> source = | 1134 Local<String> source = |
1080 String::NewFromUtf8(isolate, shell_source.start(), String::kNormalString, | 1135 String::NewFromUtf8(isolate, shell_source.start(), NewStringType::kNormal, |
1081 shell_source.length()); | 1136 shell_source.length()).ToLocalChecked(); |
1082 Handle<String> name = | 1137 Local<String> name = |
1083 String::NewFromUtf8(isolate, shell_source_name.start(), | 1138 String::NewFromUtf8(isolate, shell_source_name.start(), |
1084 String::kNormalString, shell_source_name.length()); | 1139 NewStringType::kNormal, |
| 1140 shell_source_name.length()).ToLocalChecked(); |
1085 ScriptOrigin origin(name); | 1141 ScriptOrigin origin(name); |
1086 Handle<Script> script = Script::Compile(source, &origin); | 1142 Local<Script> script = |
1087 script->Run(); | 1143 Script::Compile(utility_context, source, &origin).ToLocalChecked(); |
| 1144 script->Run(utility_context).ToLocalChecked(); |
1088 // Mark the d8 shell script as native to avoid it showing up as normal source | 1145 // Mark the d8 shell script as native to avoid it showing up as normal source |
1089 // in the debugger. | 1146 // in the debugger. |
1090 i::Handle<i::Object> compiled_script = Utils::OpenHandle(*script); | 1147 i::Handle<i::Object> compiled_script = Utils::OpenHandle(*script); |
1091 i::Handle<i::Script> script_object = compiled_script->IsJSFunction() | 1148 i::Handle<i::Script> script_object = compiled_script->IsJSFunction() |
1092 ? i::Handle<i::Script>(i::Script::cast( | 1149 ? i::Handle<i::Script>(i::Script::cast( |
1093 i::JSFunction::cast(*compiled_script)->shared()->script())) | 1150 i::JSFunction::cast(*compiled_script)->shared()->script())) |
1094 : i::Handle<i::Script>(i::Script::cast( | 1151 : i::Handle<i::Script>(i::Script::cast( |
1095 i::SharedFunctionInfo::cast(*compiled_script)->script())); | 1152 i::SharedFunctionInfo::cast(*compiled_script)->script())); |
1096 script_object->set_type(i::Smi::FromInt(i::Script::TYPE_NATIVE)); | 1153 script_object->set_type(i::Smi::FromInt(i::Script::TYPE_NATIVE)); |
1097 | 1154 |
1098 // Start the in-process debugger if requested. | 1155 // Start the in-process debugger if requested. |
1099 if (i::FLAG_debugger) v8::Debug::SetDebugEventListener(HandleDebugEvent); | 1156 if (i::FLAG_debugger) v8::Debug::SetDebugEventListener(HandleDebugEvent); |
1100 } | 1157 } |
1101 #endif // !V8_SHARED | 1158 #endif // !V8_SHARED |
1102 | 1159 |
1103 | 1160 |
1104 Handle<ObjectTemplate> Shell::CreateGlobalTemplate(Isolate* isolate) { | 1161 Local<ObjectTemplate> Shell::CreateGlobalTemplate(Isolate* isolate) { |
1105 Handle<ObjectTemplate> global_template = ObjectTemplate::New(isolate); | 1162 Local<ObjectTemplate> global_template = ObjectTemplate::New(isolate); |
1106 global_template->Set(String::NewFromUtf8(isolate, "print"), | 1163 global_template->Set( |
1107 FunctionTemplate::New(isolate, Print)); | 1164 String::NewFromUtf8(isolate, "print", NewStringType::kNormal) |
1108 global_template->Set(String::NewFromUtf8(isolate, "write"), | 1165 .ToLocalChecked(), |
1109 FunctionTemplate::New(isolate, Write)); | 1166 FunctionTemplate::New(isolate, Print)); |
1110 global_template->Set(String::NewFromUtf8(isolate, "read"), | 1167 global_template->Set( |
1111 FunctionTemplate::New(isolate, Read)); | 1168 String::NewFromUtf8(isolate, "write", NewStringType::kNormal) |
1112 global_template->Set(String::NewFromUtf8(isolate, "readbuffer"), | 1169 .ToLocalChecked(), |
1113 FunctionTemplate::New(isolate, ReadBuffer)); | 1170 FunctionTemplate::New(isolate, Write)); |
1114 global_template->Set(String::NewFromUtf8(isolate, "readline"), | 1171 global_template->Set( |
1115 FunctionTemplate::New(isolate, ReadLine)); | 1172 String::NewFromUtf8(isolate, "read", NewStringType::kNormal) |
1116 global_template->Set(String::NewFromUtf8(isolate, "load"), | 1173 .ToLocalChecked(), |
1117 FunctionTemplate::New(isolate, Load)); | 1174 FunctionTemplate::New(isolate, Read)); |
| 1175 global_template->Set( |
| 1176 String::NewFromUtf8(isolate, "readbuffer", NewStringType::kNormal) |
| 1177 .ToLocalChecked(), |
| 1178 FunctionTemplate::New(isolate, ReadBuffer)); |
| 1179 global_template->Set( |
| 1180 String::NewFromUtf8(isolate, "readline", NewStringType::kNormal) |
| 1181 .ToLocalChecked(), |
| 1182 FunctionTemplate::New(isolate, ReadLine)); |
| 1183 global_template->Set( |
| 1184 String::NewFromUtf8(isolate, "load", NewStringType::kNormal) |
| 1185 .ToLocalChecked(), |
| 1186 FunctionTemplate::New(isolate, Load)); |
1118 // Some Emscripten-generated code tries to call 'quit', which in turn would | 1187 // Some Emscripten-generated code tries to call 'quit', which in turn would |
1119 // call C's exit(). This would lead to memory leaks, because there is no way | 1188 // call C's exit(). This would lead to memory leaks, because there is no way |
1120 // we can terminate cleanly then, so we need a way to hide 'quit'. | 1189 // we can terminate cleanly then, so we need a way to hide 'quit'. |
1121 if (!options.omit_quit) { | 1190 if (!options.omit_quit) { |
1122 global_template->Set(String::NewFromUtf8(isolate, "quit"), | 1191 global_template->Set( |
1123 FunctionTemplate::New(isolate, Quit)); | 1192 String::NewFromUtf8(isolate, "quit", NewStringType::kNormal) |
| 1193 .ToLocalChecked(), |
| 1194 FunctionTemplate::New(isolate, Quit)); |
1124 } | 1195 } |
1125 global_template->Set(String::NewFromUtf8(isolate, "version"), | 1196 global_template->Set( |
1126 FunctionTemplate::New(isolate, Version)); | 1197 String::NewFromUtf8(isolate, "version", NewStringType::kNormal) |
| 1198 .ToLocalChecked(), |
| 1199 FunctionTemplate::New(isolate, Version)); |
1127 | 1200 |
1128 // Bind the Realm object. | 1201 // Bind the Realm object. |
1129 Handle<ObjectTemplate> realm_template = ObjectTemplate::New(isolate); | 1202 Local<ObjectTemplate> realm_template = ObjectTemplate::New(isolate); |
1130 realm_template->Set(String::NewFromUtf8(isolate, "current"), | 1203 realm_template->Set( |
1131 FunctionTemplate::New(isolate, RealmCurrent)); | 1204 String::NewFromUtf8(isolate, "current", NewStringType::kNormal) |
1132 realm_template->Set(String::NewFromUtf8(isolate, "owner"), | 1205 .ToLocalChecked(), |
1133 FunctionTemplate::New(isolate, RealmOwner)); | 1206 FunctionTemplate::New(isolate, RealmCurrent)); |
1134 realm_template->Set(String::NewFromUtf8(isolate, "global"), | 1207 realm_template->Set( |
1135 FunctionTemplate::New(isolate, RealmGlobal)); | 1208 String::NewFromUtf8(isolate, "owner", NewStringType::kNormal) |
1136 realm_template->Set(String::NewFromUtf8(isolate, "create"), | 1209 .ToLocalChecked(), |
1137 FunctionTemplate::New(isolate, RealmCreate)); | 1210 FunctionTemplate::New(isolate, RealmOwner)); |
1138 realm_template->Set(String::NewFromUtf8(isolate, "dispose"), | 1211 realm_template->Set( |
1139 FunctionTemplate::New(isolate, RealmDispose)); | 1212 String::NewFromUtf8(isolate, "global", NewStringType::kNormal) |
1140 realm_template->Set(String::NewFromUtf8(isolate, "switch"), | 1213 .ToLocalChecked(), |
1141 FunctionTemplate::New(isolate, RealmSwitch)); | 1214 FunctionTemplate::New(isolate, RealmGlobal)); |
1142 realm_template->Set(String::NewFromUtf8(isolate, "eval"), | 1215 realm_template->Set( |
1143 FunctionTemplate::New(isolate, RealmEval)); | 1216 String::NewFromUtf8(isolate, "create", NewStringType::kNormal) |
1144 realm_template->SetAccessor(String::NewFromUtf8(isolate, "shared"), | 1217 .ToLocalChecked(), |
1145 RealmSharedGet, RealmSharedSet); | 1218 FunctionTemplate::New(isolate, RealmCreate)); |
1146 global_template->Set(String::NewFromUtf8(isolate, "Realm"), realm_template); | 1219 realm_template->Set( |
| 1220 String::NewFromUtf8(isolate, "dispose", NewStringType::kNormal) |
| 1221 .ToLocalChecked(), |
| 1222 FunctionTemplate::New(isolate, RealmDispose)); |
| 1223 realm_template->Set( |
| 1224 String::NewFromUtf8(isolate, "switch", NewStringType::kNormal) |
| 1225 .ToLocalChecked(), |
| 1226 FunctionTemplate::New(isolate, RealmSwitch)); |
| 1227 realm_template->Set( |
| 1228 String::NewFromUtf8(isolate, "eval", NewStringType::kNormal) |
| 1229 .ToLocalChecked(), |
| 1230 FunctionTemplate::New(isolate, RealmEval)); |
| 1231 realm_template->SetAccessor( |
| 1232 String::NewFromUtf8(isolate, "shared", NewStringType::kNormal) |
| 1233 .ToLocalChecked(), |
| 1234 RealmSharedGet, RealmSharedSet); |
| 1235 global_template->Set( |
| 1236 String::NewFromUtf8(isolate, "Realm", NewStringType::kNormal) |
| 1237 .ToLocalChecked(), |
| 1238 realm_template); |
1147 | 1239 |
1148 #ifndef V8_SHARED | 1240 #ifndef V8_SHARED |
1149 Handle<ObjectTemplate> performance_template = ObjectTemplate::New(isolate); | 1241 Local<ObjectTemplate> performance_template = ObjectTemplate::New(isolate); |
1150 performance_template->Set(String::NewFromUtf8(isolate, "now"), | 1242 performance_template->Set( |
1151 FunctionTemplate::New(isolate, PerformanceNow)); | 1243 String::NewFromUtf8(isolate, "now", NewStringType::kNormal) |
1152 global_template->Set(String::NewFromUtf8(isolate, "performance"), | 1244 .ToLocalChecked(), |
1153 performance_template); | 1245 FunctionTemplate::New(isolate, PerformanceNow)); |
| 1246 global_template->Set( |
| 1247 String::NewFromUtf8(isolate, "performance", NewStringType::kNormal) |
| 1248 .ToLocalChecked(), |
| 1249 performance_template); |
1154 | 1250 |
1155 Handle<FunctionTemplate> worker_fun_template = | 1251 Local<FunctionTemplate> worker_fun_template = |
1156 FunctionTemplate::New(isolate, WorkerNew); | 1252 FunctionTemplate::New(isolate, WorkerNew); |
1157 worker_fun_template->PrototypeTemplate()->Set( | 1253 worker_fun_template->PrototypeTemplate()->Set( |
1158 String::NewFromUtf8(isolate, "terminate"), | 1254 String::NewFromUtf8(isolate, "terminate", NewStringType::kNormal) |
| 1255 .ToLocalChecked(), |
1159 FunctionTemplate::New(isolate, WorkerTerminate)); | 1256 FunctionTemplate::New(isolate, WorkerTerminate)); |
1160 worker_fun_template->PrototypeTemplate()->Set( | 1257 worker_fun_template->PrototypeTemplate()->Set( |
1161 String::NewFromUtf8(isolate, "postMessage"), | 1258 String::NewFromUtf8(isolate, "postMessage", NewStringType::kNormal) |
| 1259 .ToLocalChecked(), |
1162 FunctionTemplate::New(isolate, WorkerPostMessage)); | 1260 FunctionTemplate::New(isolate, WorkerPostMessage)); |
1163 worker_fun_template->PrototypeTemplate()->Set( | 1261 worker_fun_template->PrototypeTemplate()->Set( |
1164 String::NewFromUtf8(isolate, "getMessage"), | 1262 String::NewFromUtf8(isolate, "getMessage", NewStringType::kNormal) |
| 1263 .ToLocalChecked(), |
1165 FunctionTemplate::New(isolate, WorkerGetMessage)); | 1264 FunctionTemplate::New(isolate, WorkerGetMessage)); |
1166 worker_fun_template->InstanceTemplate()->SetInternalFieldCount(1); | 1265 worker_fun_template->InstanceTemplate()->SetInternalFieldCount(1); |
1167 global_template->Set(String::NewFromUtf8(isolate, "Worker"), | 1266 global_template->Set( |
1168 worker_fun_template); | 1267 String::NewFromUtf8(isolate, "Worker", NewStringType::kNormal) |
| 1268 .ToLocalChecked(), |
| 1269 worker_fun_template); |
1169 #endif // !V8_SHARED | 1270 #endif // !V8_SHARED |
1170 | 1271 |
1171 Handle<ObjectTemplate> os_templ = ObjectTemplate::New(isolate); | 1272 Local<ObjectTemplate> os_templ = ObjectTemplate::New(isolate); |
1172 AddOSMethods(isolate, os_templ); | 1273 AddOSMethods(isolate, os_templ); |
1173 global_template->Set(String::NewFromUtf8(isolate, "os"), os_templ); | 1274 global_template->Set( |
| 1275 String::NewFromUtf8(isolate, "os", NewStringType::kNormal) |
| 1276 .ToLocalChecked(), |
| 1277 os_templ); |
1174 | 1278 |
1175 return global_template; | 1279 return global_template; |
1176 } | 1280 } |
1177 | 1281 |
1178 | 1282 |
1179 void Shell::Initialize(Isolate* isolate) { | 1283 void Shell::Initialize(Isolate* isolate) { |
1180 #ifndef V8_SHARED | 1284 #ifndef V8_SHARED |
1181 // Set up counters | 1285 // Set up counters |
1182 if (i::StrLength(i::FLAG_map_counters) != 0) | 1286 if (i::StrLength(i::FLAG_map_counters) != 0) |
1183 MapCounters(isolate, i::FLAG_map_counters); | 1287 MapCounters(isolate, i::FLAG_map_counters); |
1184 #endif // !V8_SHARED | 1288 #endif // !V8_SHARED |
1185 } | 1289 } |
1186 | 1290 |
1187 | 1291 |
1188 void Shell::InitializeDebugger(Isolate* isolate) { | 1292 void Shell::InitializeDebugger(Isolate* isolate) { |
1189 if (options.test_shell) return; | 1293 if (options.test_shell) return; |
1190 #ifndef V8_SHARED | 1294 #ifndef V8_SHARED |
1191 HandleScope scope(isolate); | 1295 HandleScope scope(isolate); |
1192 Handle<ObjectTemplate> global_template = CreateGlobalTemplate(isolate); | 1296 Local<ObjectTemplate> global_template = CreateGlobalTemplate(isolate); |
1193 utility_context_.Reset(isolate, | 1297 utility_context_.Reset(isolate, |
1194 Context::New(isolate, NULL, global_template)); | 1298 Context::New(isolate, NULL, global_template)); |
1195 if (utility_context_.IsEmpty()) { | 1299 if (utility_context_.IsEmpty()) { |
1196 printf("Failed to initialize debugger\n"); | 1300 printf("Failed to initialize debugger\n"); |
1197 Shell::Exit(1); | 1301 Shell::Exit(1); |
1198 } | 1302 } |
1199 #endif // !V8_SHARED | 1303 #endif // !V8_SHARED |
1200 } | 1304 } |
1201 | 1305 |
1202 | 1306 |
1203 Local<Context> Shell::CreateEvaluationContext(Isolate* isolate) { | 1307 Local<Context> Shell::CreateEvaluationContext(Isolate* isolate) { |
1204 #ifndef V8_SHARED | 1308 #ifndef V8_SHARED |
1205 // This needs to be a critical section since this is not thread-safe | 1309 // This needs to be a critical section since this is not thread-safe |
1206 base::LockGuard<base::Mutex> lock_guard(context_mutex_.Pointer()); | 1310 base::LockGuard<base::Mutex> lock_guard(context_mutex_.Pointer()); |
1207 #endif // !V8_SHARED | 1311 #endif // !V8_SHARED |
1208 // Initialize the global objects | 1312 // Initialize the global objects |
1209 Handle<ObjectTemplate> global_template = CreateGlobalTemplate(isolate); | 1313 Local<ObjectTemplate> global_template = CreateGlobalTemplate(isolate); |
1210 EscapableHandleScope handle_scope(isolate); | 1314 EscapableHandleScope handle_scope(isolate); |
1211 Local<Context> context = Context::New(isolate, NULL, global_template); | 1315 Local<Context> context = Context::New(isolate, NULL, global_template); |
1212 DCHECK(!context.IsEmpty()); | 1316 DCHECK(!context.IsEmpty()); |
1213 Context::Scope scope(context); | 1317 Context::Scope scope(context); |
1214 | 1318 |
1215 #ifndef V8_SHARED | 1319 #ifndef V8_SHARED |
1216 i::Factory* factory = reinterpret_cast<i::Isolate*>(isolate)->factory(); | 1320 i::Factory* factory = reinterpret_cast<i::Isolate*>(isolate)->factory(); |
1217 i::JSArguments js_args = i::FLAG_js_arguments; | 1321 i::JSArguments js_args = i::FLAG_js_arguments; |
1218 i::Handle<i::FixedArray> arguments_array = | 1322 i::Handle<i::FixedArray> arguments_array = |
1219 factory->NewFixedArray(js_args.argc); | 1323 factory->NewFixedArray(js_args.argc); |
1220 for (int j = 0; j < js_args.argc; j++) { | 1324 for (int j = 0; j < js_args.argc; j++) { |
1221 i::Handle<i::String> arg = | 1325 i::Handle<i::String> arg = |
1222 factory->NewStringFromUtf8(i::CStrVector(js_args[j])).ToHandleChecked(); | 1326 factory->NewStringFromUtf8(i::CStrVector(js_args[j])).ToHandleChecked(); |
1223 arguments_array->set(j, *arg); | 1327 arguments_array->set(j, *arg); |
1224 } | 1328 } |
1225 i::Handle<i::JSArray> arguments_jsarray = | 1329 i::Handle<i::JSArray> arguments_jsarray = |
1226 factory->NewJSArrayWithElements(arguments_array); | 1330 factory->NewJSArrayWithElements(arguments_array); |
1227 context->Global()->Set(String::NewFromUtf8(isolate, "arguments"), | 1331 context->Global() |
1228 Utils::ToLocal(arguments_jsarray)); | 1332 ->Set(context, |
| 1333 String::NewFromUtf8(isolate, "arguments", NewStringType::kNormal) |
| 1334 .ToLocalChecked(), |
| 1335 Utils::ToLocal(arguments_jsarray)) |
| 1336 .FromJust(); |
1229 #endif // !V8_SHARED | 1337 #endif // !V8_SHARED |
1230 return handle_scope.Escape(context); | 1338 return handle_scope.Escape(context); |
1231 } | 1339 } |
1232 | 1340 |
1233 | 1341 |
1234 void Shell::Exit(int exit_code) { | 1342 void Shell::Exit(int exit_code) { |
1235 // Use _exit instead of exit to avoid races between isolate | 1343 // Use _exit instead of exit to avoid races between isolate |
1236 // threads and static destructors. | 1344 // threads and static destructors. |
1237 fflush(stdout); | 1345 fflush(stdout); |
1238 fflush(stderr); | 1346 fflush(stderr); |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1373 Isolate* isolate = args.GetIsolate(); | 1481 Isolate* isolate = args.GetIsolate(); |
1374 DataAndPersistent* data = new DataAndPersistent; | 1482 DataAndPersistent* data = new DataAndPersistent; |
1375 data->data = reinterpret_cast<uint8_t*>( | 1483 data->data = reinterpret_cast<uint8_t*>( |
1376 ReadChars(args.GetIsolate(), *filename, &length)); | 1484 ReadChars(args.GetIsolate(), *filename, &length)); |
1377 if (data->data == NULL) { | 1485 if (data->data == NULL) { |
1378 delete data; | 1486 delete data; |
1379 Throw(args.GetIsolate(), "Error reading file"); | 1487 Throw(args.GetIsolate(), "Error reading file"); |
1380 return; | 1488 return; |
1381 } | 1489 } |
1382 data->byte_length = length; | 1490 data->byte_length = length; |
1383 Handle<v8::ArrayBuffer> buffer = | 1491 Local<v8::ArrayBuffer> buffer = ArrayBuffer::New(isolate, data->data, length); |
1384 ArrayBuffer::New(isolate, data->data, length); | |
1385 data->handle.Reset(isolate, buffer); | 1492 data->handle.Reset(isolate, buffer); |
1386 data->handle.SetWeak(data, ReadBufferWeakCallback, | 1493 data->handle.SetWeak(data, ReadBufferWeakCallback, |
1387 v8::WeakCallbackType::kParameter); | 1494 v8::WeakCallbackType::kParameter); |
1388 data->handle.MarkIndependent(); | 1495 data->handle.MarkIndependent(); |
1389 isolate->AdjustAmountOfExternalAllocatedMemory(length); | 1496 isolate->AdjustAmountOfExternalAllocatedMemory(length); |
1390 | 1497 |
1391 args.GetReturnValue().Set(buffer); | 1498 args.GetReturnValue().Set(buffer); |
1392 } | 1499 } |
1393 | 1500 |
1394 | 1501 |
1395 // Reads a file into a v8 string. | 1502 // Reads a file into a v8 string. |
1396 Handle<String> Shell::ReadFile(Isolate* isolate, const char* name) { | 1503 Local<String> Shell::ReadFile(Isolate* isolate, const char* name) { |
1397 int size = 0; | 1504 int size = 0; |
1398 char* chars = ReadChars(isolate, name, &size); | 1505 char* chars = ReadChars(isolate, name, &size); |
1399 if (chars == NULL) return Handle<String>(); | 1506 if (chars == NULL) return Local<String>(); |
1400 Handle<String> result = | 1507 Local<String> result = |
1401 String::NewFromUtf8(isolate, chars, String::kNormalString, size); | 1508 String::NewFromUtf8(isolate, chars, NewStringType::kNormal, size) |
| 1509 .ToLocalChecked(); |
1402 delete[] chars; | 1510 delete[] chars; |
1403 return result; | 1511 return result; |
1404 } | 1512 } |
1405 | 1513 |
1406 | 1514 |
1407 void Shell::RunShell(Isolate* isolate) { | 1515 void Shell::RunShell(Isolate* isolate) { |
1408 HandleScope outer_scope(isolate); | 1516 HandleScope outer_scope(isolate); |
1409 v8::Local<v8::Context> context = | 1517 v8::Local<v8::Context> context = |
1410 v8::Local<v8::Context>::New(isolate, evaluation_context_); | 1518 v8::Local<v8::Context>::New(isolate, evaluation_context_); |
1411 v8::Context::Scope context_scope(context); | 1519 v8::Context::Scope context_scope(context); |
1412 PerIsolateData::RealmScope realm_scope(PerIsolateData::Get(isolate)); | 1520 PerIsolateData::RealmScope realm_scope(PerIsolateData::Get(isolate)); |
1413 Handle<String> name = String::NewFromUtf8(isolate, "(d8)"); | 1521 Local<String> name = |
| 1522 String::NewFromUtf8(isolate, "(d8)", NewStringType::kNormal) |
| 1523 .ToLocalChecked(); |
1414 LineEditor* console = LineEditor::Get(); | 1524 LineEditor* console = LineEditor::Get(); |
1415 printf("V8 version %s [console: %s]\n", V8::GetVersion(), console->name()); | 1525 printf("V8 version %s [console: %s]\n", V8::GetVersion(), console->name()); |
1416 console->Open(isolate); | 1526 console->Open(isolate); |
1417 while (true) { | 1527 while (true) { |
1418 HandleScope inner_scope(isolate); | 1528 HandleScope inner_scope(isolate); |
1419 Handle<String> input = console->Prompt(Shell::kPrompt); | 1529 Local<String> input = console->Prompt(Shell::kPrompt); |
1420 if (input.IsEmpty()) break; | 1530 if (input.IsEmpty()) break; |
1421 ExecuteString(isolate, input, name, true, true); | 1531 ExecuteString(isolate, input, name, true, true); |
1422 } | 1532 } |
1423 printf("\n"); | 1533 printf("\n"); |
1424 } | 1534 } |
1425 | 1535 |
1426 | 1536 |
1427 SourceGroup::~SourceGroup() { | 1537 SourceGroup::~SourceGroup() { |
1428 #ifndef V8_SHARED | 1538 #ifndef V8_SHARED |
1429 delete thread_; | 1539 delete thread_; |
1430 thread_ = NULL; | 1540 thread_ = NULL; |
1431 #endif // !V8_SHARED | 1541 #endif // !V8_SHARED |
1432 } | 1542 } |
1433 | 1543 |
1434 | 1544 |
1435 void SourceGroup::Execute(Isolate* isolate) { | 1545 void SourceGroup::Execute(Isolate* isolate) { |
1436 bool exception_was_thrown = false; | 1546 bool exception_was_thrown = false; |
1437 for (int i = begin_offset_; i < end_offset_; ++i) { | 1547 for (int i = begin_offset_; i < end_offset_; ++i) { |
1438 const char* arg = argv_[i]; | 1548 const char* arg = argv_[i]; |
1439 Shell::SourceType source_type = Shell::SCRIPT; | 1549 Shell::SourceType source_type = Shell::SCRIPT; |
1440 if (strcmp(arg, "-e") == 0 && i + 1 < end_offset_) { | 1550 if (strcmp(arg, "-e") == 0 && i + 1 < end_offset_) { |
1441 // Execute argument given to -e option directly. | 1551 // Execute argument given to -e option directly. |
1442 HandleScope handle_scope(isolate); | 1552 HandleScope handle_scope(isolate); |
1443 Handle<String> file_name = String::NewFromUtf8(isolate, "unnamed"); | 1553 Local<String> file_name = |
1444 Handle<String> source = String::NewFromUtf8(isolate, argv_[i + 1]); | 1554 String::NewFromUtf8(isolate, "unnamed", NewStringType::kNormal) |
| 1555 .ToLocalChecked(); |
| 1556 Local<String> source = |
| 1557 String::NewFromUtf8(isolate, argv_[i + 1], NewStringType::kNormal) |
| 1558 .ToLocalChecked(); |
1445 if (!Shell::ExecuteString(isolate, source, file_name, false, true)) { | 1559 if (!Shell::ExecuteString(isolate, source, file_name, false, true)) { |
1446 exception_was_thrown = true; | 1560 exception_was_thrown = true; |
1447 break; | 1561 break; |
1448 } | 1562 } |
1449 ++i; | 1563 ++i; |
1450 continue; | 1564 continue; |
1451 } else if (strcmp(arg, "--module") == 0 && i + 1 < end_offset_) { | 1565 } else if (strcmp(arg, "--module") == 0 && i + 1 < end_offset_) { |
1452 // Treat the next file as a module. | 1566 // Treat the next file as a module. |
1453 source_type = Shell::MODULE; | 1567 source_type = Shell::MODULE; |
1454 arg = argv_[++i]; | 1568 arg = argv_[++i]; |
1455 } else if (arg[0] == '-') { | 1569 } else if (arg[0] == '-') { |
1456 // Ignore other options. They have been parsed already. | 1570 // Ignore other options. They have been parsed already. |
1457 continue; | 1571 continue; |
1458 } | 1572 } |
1459 | 1573 |
1460 // Use all other arguments as names of files to load and run. | 1574 // Use all other arguments as names of files to load and run. |
1461 HandleScope handle_scope(isolate); | 1575 HandleScope handle_scope(isolate); |
1462 Handle<String> file_name = String::NewFromUtf8(isolate, arg); | 1576 Local<String> file_name = |
1463 Handle<String> source = ReadFile(isolate, arg); | 1577 String::NewFromUtf8(isolate, arg, NewStringType::kNormal) |
| 1578 .ToLocalChecked(); |
| 1579 Local<String> source = ReadFile(isolate, arg); |
1464 if (source.IsEmpty()) { | 1580 if (source.IsEmpty()) { |
1465 printf("Error reading '%s'\n", arg); | 1581 printf("Error reading '%s'\n", arg); |
1466 Shell::Exit(1); | 1582 Shell::Exit(1); |
1467 } | 1583 } |
1468 if (!Shell::ExecuteString(isolate, source, file_name, false, true, | 1584 if (!Shell::ExecuteString(isolate, source, file_name, false, true, |
1469 source_type)) { | 1585 source_type)) { |
1470 exception_was_thrown = true; | 1586 exception_was_thrown = true; |
1471 break; | 1587 break; |
1472 } | 1588 } |
1473 } | 1589 } |
1474 if (exception_was_thrown != Shell::options.expected_to_throw) { | 1590 if (exception_was_thrown != Shell::options.expected_to_throw) { |
1475 Shell::Exit(1); | 1591 Shell::Exit(1); |
1476 } | 1592 } |
1477 } | 1593 } |
1478 | 1594 |
1479 | 1595 |
1480 Handle<String> SourceGroup::ReadFile(Isolate* isolate, const char* name) { | 1596 Local<String> SourceGroup::ReadFile(Isolate* isolate, const char* name) { |
1481 int size; | 1597 int size; |
1482 char* chars = ReadChars(isolate, name, &size); | 1598 char* chars = ReadChars(isolate, name, &size); |
1483 if (chars == NULL) return Handle<String>(); | 1599 if (chars == NULL) return Local<String>(); |
1484 Handle<String> result = | 1600 Local<String> result = |
1485 String::NewFromUtf8(isolate, chars, String::kNormalString, size); | 1601 String::NewFromUtf8(isolate, chars, NewStringType::kNormal, size) |
| 1602 .ToLocalChecked(); |
1486 delete[] chars; | 1603 delete[] chars; |
1487 return result; | 1604 return result; |
1488 } | 1605 } |
1489 | 1606 |
1490 | 1607 |
1491 #ifndef V8_SHARED | 1608 #ifndef V8_SHARED |
1492 base::Thread::Options SourceGroup::GetThreadOptions() { | 1609 base::Thread::Options SourceGroup::GetThreadOptions() { |
1493 // On some systems (OSX 10.6) the stack size default is 0.5Mb or less | 1610 // On some systems (OSX 10.6) the stack size default is 0.5Mb or less |
1494 // which is not enough to parse the big literal expressions used in tests. | 1611 // which is not enough to parse the big literal expressions used in tests. |
1495 // The stack size should be at least StackGuard::kLimitSize + some | 1612 // The stack size should be at least StackGuard::kLimitSize + some |
(...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1728 { | 1845 { |
1729 Isolate::Scope iscope(isolate); | 1846 Isolate::Scope iscope(isolate); |
1730 { | 1847 { |
1731 HandleScope scope(isolate); | 1848 HandleScope scope(isolate); |
1732 PerIsolateData data(isolate); | 1849 PerIsolateData data(isolate); |
1733 Local<Context> context = Shell::CreateEvaluationContext(isolate); | 1850 Local<Context> context = Shell::CreateEvaluationContext(isolate); |
1734 { | 1851 { |
1735 Context::Scope cscope(context); | 1852 Context::Scope cscope(context); |
1736 PerIsolateData::RealmScope realm_scope(PerIsolateData::Get(isolate)); | 1853 PerIsolateData::RealmScope realm_scope(PerIsolateData::Get(isolate)); |
1737 | 1854 |
1738 Handle<Object> global = context->Global(); | 1855 Local<Object> global = context->Global(); |
1739 Handle<Value> this_value = External::New(isolate, this); | 1856 Local<Value> this_value = External::New(isolate, this); |
1740 Handle<FunctionTemplate> postmessage_fun_template = | 1857 Local<FunctionTemplate> postmessage_fun_template = |
1741 FunctionTemplate::New(isolate, PostMessageOut, this_value); | 1858 FunctionTemplate::New(isolate, PostMessageOut, this_value); |
1742 | 1859 |
1743 Handle<Function> postmessage_fun; | 1860 Local<Function> postmessage_fun; |
1744 if (postmessage_fun_template->GetFunction(context) | 1861 if (postmessage_fun_template->GetFunction(context) |
1745 .ToLocal(&postmessage_fun)) { | 1862 .ToLocal(&postmessage_fun)) { |
1746 global->Set(String::NewFromUtf8(isolate, "postMessage"), | 1863 global->Set(context, String::NewFromUtf8(isolate, "postMessage", |
1747 postmessage_fun); | 1864 NewStringType::kNormal) |
| 1865 .ToLocalChecked(), |
| 1866 postmessage_fun).FromJust(); |
1748 } | 1867 } |
1749 | 1868 |
1750 // First run the script | 1869 // First run the script |
1751 Handle<String> file_name = String::NewFromUtf8(isolate, "unnamed"); | 1870 Local<String> file_name = |
1752 Handle<String> source = String::NewFromUtf8(isolate, script_); | 1871 String::NewFromUtf8(isolate, "unnamed", NewStringType::kNormal) |
| 1872 .ToLocalChecked(); |
| 1873 Local<String> source = |
| 1874 String::NewFromUtf8(isolate, script_, NewStringType::kNormal) |
| 1875 .ToLocalChecked(); |
1753 if (Shell::ExecuteString(isolate, source, file_name, false, true)) { | 1876 if (Shell::ExecuteString(isolate, source, file_name, false, true)) { |
1754 // Get the message handler | 1877 // Get the message handler |
1755 Handle<Value> onmessage = | 1878 Local<Value> onmessage = |
1756 global->Get(String::NewFromUtf8(isolate, "onmessage")); | 1879 global->Get(context, String::NewFromUtf8(isolate, "onmessage", |
| 1880 NewStringType::kNormal) |
| 1881 .ToLocalChecked()).ToLocalChecked(); |
1757 if (onmessage->IsFunction()) { | 1882 if (onmessage->IsFunction()) { |
1758 Handle<Function> onmessage_fun = Handle<Function>::Cast(onmessage); | 1883 Local<Function> onmessage_fun = Local<Function>::Cast(onmessage); |
1759 // Now wait for messages | 1884 // Now wait for messages |
1760 while (true) { | 1885 while (true) { |
1761 in_semaphore_.Wait(); | 1886 in_semaphore_.Wait(); |
1762 SerializationData* data; | 1887 SerializationData* data; |
1763 if (!in_queue_.Dequeue(&data)) continue; | 1888 if (!in_queue_.Dequeue(&data)) continue; |
1764 if (data == NULL) { | 1889 if (data == NULL) { |
1765 break; | 1890 break; |
1766 } | 1891 } |
1767 int offset = 0; | 1892 int offset = 0; |
1768 Local<Value> data_value; | 1893 Local<Value> data_value; |
1769 if (Shell::DeserializeValue(isolate, *data, &offset) | 1894 if (Shell::DeserializeValue(isolate, *data, &offset) |
1770 .ToLocal(&data_value)) { | 1895 .ToLocal(&data_value)) { |
1771 Handle<Value> argv[] = {data_value}; | 1896 Local<Value> argv[] = {data_value}; |
1772 (void)onmessage_fun->Call(context, global, 1, argv); | 1897 (void)onmessage_fun->Call(context, global, 1, argv); |
1773 } | 1898 } |
1774 delete data; | 1899 delete data; |
1775 } | 1900 } |
1776 } | 1901 } |
1777 } | 1902 } |
1778 } | 1903 } |
1779 } | 1904 } |
1780 Shell::CollectGarbage(isolate); | 1905 Shell::CollectGarbage(isolate); |
1781 } | 1906 } |
1782 isolate->Dispose(); | 1907 isolate->Dispose(); |
1783 | 1908 |
1784 // Post NULL to wake the thread waiting on GetMessage() if there is one. | 1909 // Post NULL to wake the thread waiting on GetMessage() if there is one. |
1785 out_queue_.Enqueue(NULL); | 1910 out_queue_.Enqueue(NULL); |
1786 out_semaphore_.Signal(); | 1911 out_semaphore_.Signal(); |
1787 } | 1912 } |
1788 | 1913 |
1789 | 1914 |
1790 void Worker::PostMessageOut(const v8::FunctionCallbackInfo<v8::Value>& args) { | 1915 void Worker::PostMessageOut(const v8::FunctionCallbackInfo<v8::Value>& args) { |
1791 Isolate* isolate = args.GetIsolate(); | 1916 Isolate* isolate = args.GetIsolate(); |
1792 HandleScope handle_scope(isolate); | 1917 HandleScope handle_scope(isolate); |
1793 | 1918 |
1794 if (args.Length() < 1) { | 1919 if (args.Length() < 1) { |
1795 Throw(isolate, "Invalid argument"); | 1920 Throw(isolate, "Invalid argument"); |
1796 return; | 1921 return; |
1797 } | 1922 } |
1798 | 1923 |
1799 Handle<Value> message = args[0]; | 1924 Local<Value> message = args[0]; |
1800 | 1925 |
1801 // TODO(binji): Allow transferring from worker to main thread? | 1926 // TODO(binji): Allow transferring from worker to main thread? |
1802 Shell::ObjectList to_transfer; | 1927 Shell::ObjectList to_transfer; |
1803 | 1928 |
1804 Shell::ObjectList seen_objects; | 1929 Shell::ObjectList seen_objects; |
1805 SerializationData* data = new SerializationData; | 1930 SerializationData* data = new SerializationData; |
1806 if (Shell::SerializeValue(isolate, message, to_transfer, &seen_objects, | 1931 if (Shell::SerializeValue(isolate, message, to_transfer, &seen_objects, |
1807 data)) { | 1932 data)) { |
1808 DCHECK(args.Data()->IsExternal()); | 1933 DCHECK(args.Data()->IsExternal()); |
1809 Handle<External> this_value = Handle<External>::Cast(args.Data()); | 1934 Local<External> this_value = Local<External>::Cast(args.Data()); |
1810 Worker* worker = static_cast<Worker*>(this_value->Value()); | 1935 Worker* worker = static_cast<Worker*>(this_value->Value()); |
1811 worker->out_queue_.Enqueue(data); | 1936 worker->out_queue_.Enqueue(data); |
1812 worker->out_semaphore_.Signal(); | 1937 worker->out_semaphore_.Signal(); |
1813 } else { | 1938 } else { |
1814 delete data; | 1939 delete data; |
1815 } | 1940 } |
1816 } | 1941 } |
1817 #endif // !V8_SHARED | 1942 #endif // !V8_SHARED |
1818 | 1943 |
1819 | 1944 |
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2009 } | 2134 } |
2010 } | 2135 } |
2011 | 2136 |
2012 | 2137 |
2013 void Shell::EmptyMessageQueues(Isolate* isolate) { | 2138 void Shell::EmptyMessageQueues(Isolate* isolate) { |
2014 while (v8::platform::PumpMessageLoop(g_platform, isolate)) continue; | 2139 while (v8::platform::PumpMessageLoop(g_platform, isolate)) continue; |
2015 } | 2140 } |
2016 | 2141 |
2017 | 2142 |
2018 #ifndef V8_SHARED | 2143 #ifndef V8_SHARED |
2019 bool Shell::SerializeValue(Isolate* isolate, Handle<Value> value, | 2144 bool Shell::SerializeValue(Isolate* isolate, Local<Value> value, |
2020 const ObjectList& to_transfer, | 2145 const ObjectList& to_transfer, |
2021 ObjectList* seen_objects, | 2146 ObjectList* seen_objects, |
2022 SerializationData* out_data) { | 2147 SerializationData* out_data) { |
2023 DCHECK(out_data); | 2148 DCHECK(out_data); |
2024 Local<Context> context = isolate->GetCurrentContext(); | 2149 Local<Context> context = isolate->GetCurrentContext(); |
2025 | 2150 |
2026 if (value->IsUndefined()) { | 2151 if (value->IsUndefined()) { |
2027 out_data->WriteTag(kSerializationTagUndefined); | 2152 out_data->WriteTag(kSerializationTagUndefined); |
2028 } else if (value->IsNull()) { | 2153 } else if (value->IsNull()) { |
2029 out_data->WriteTag(kSerializationTagNull); | 2154 out_data->WriteTag(kSerializationTagNull); |
2030 } else if (value->IsTrue()) { | 2155 } else if (value->IsTrue()) { |
2031 out_data->WriteTag(kSerializationTagTrue); | 2156 out_data->WriteTag(kSerializationTagTrue); |
2032 } else if (value->IsFalse()) { | 2157 } else if (value->IsFalse()) { |
2033 out_data->WriteTag(kSerializationTagFalse); | 2158 out_data->WriteTag(kSerializationTagFalse); |
2034 } else if (value->IsNumber()) { | 2159 } else if (value->IsNumber()) { |
2035 Handle<Number> num = Handle<Number>::Cast(value); | 2160 Local<Number> num = Local<Number>::Cast(value); |
2036 double value = num->Value(); | 2161 double value = num->Value(); |
2037 out_data->WriteTag(kSerializationTagNumber); | 2162 out_data->WriteTag(kSerializationTagNumber); |
2038 out_data->Write(value); | 2163 out_data->Write(value); |
2039 } else if (value->IsString()) { | 2164 } else if (value->IsString()) { |
2040 v8::String::Utf8Value str(value); | 2165 v8::String::Utf8Value str(value); |
2041 out_data->WriteTag(kSerializationTagString); | 2166 out_data->WriteTag(kSerializationTagString); |
2042 out_data->Write(str.length()); | 2167 out_data->Write(str.length()); |
2043 out_data->WriteMemory(*str, str.length()); | 2168 out_data->WriteMemory(*str, str.length()); |
2044 } else if (value->IsArray()) { | 2169 } else if (value->IsArray()) { |
2045 Handle<Array> array = Handle<Array>::Cast(value); | 2170 Local<Array> array = Local<Array>::Cast(value); |
2046 if (FindInObjectList(array, *seen_objects)) { | 2171 if (FindInObjectList(array, *seen_objects)) { |
2047 Throw(isolate, "Duplicated arrays not supported"); | 2172 Throw(isolate, "Duplicated arrays not supported"); |
2048 return false; | 2173 return false; |
2049 } | 2174 } |
2050 seen_objects->Add(array); | 2175 seen_objects->Add(array); |
2051 out_data->WriteTag(kSerializationTagArray); | 2176 out_data->WriteTag(kSerializationTagArray); |
2052 uint32_t length = array->Length(); | 2177 uint32_t length = array->Length(); |
2053 out_data->Write(length); | 2178 out_data->Write(length); |
2054 for (uint32_t i = 0; i < length; ++i) { | 2179 for (uint32_t i = 0; i < length; ++i) { |
2055 Local<Value> element_value; | 2180 Local<Value> element_value; |
2056 if (array->Get(context, i).ToLocal(&element_value)) { | 2181 if (array->Get(context, i).ToLocal(&element_value)) { |
2057 if (!SerializeValue(isolate, element_value, to_transfer, seen_objects, | 2182 if (!SerializeValue(isolate, element_value, to_transfer, seen_objects, |
2058 out_data)) | 2183 out_data)) |
2059 return false; | 2184 return false; |
2060 } else { | 2185 } else { |
2061 Throw(isolate, "Failed to serialize array element."); | 2186 Throw(isolate, "Failed to serialize array element."); |
2062 return false; | 2187 return false; |
2063 } | 2188 } |
2064 } | 2189 } |
2065 } else if (value->IsArrayBuffer()) { | 2190 } else if (value->IsArrayBuffer()) { |
2066 Handle<ArrayBuffer> array_buffer = Handle<ArrayBuffer>::Cast(value); | 2191 Local<ArrayBuffer> array_buffer = Local<ArrayBuffer>::Cast(value); |
2067 if (FindInObjectList(array_buffer, *seen_objects)) { | 2192 if (FindInObjectList(array_buffer, *seen_objects)) { |
2068 Throw(isolate, "Duplicated array buffers not supported"); | 2193 Throw(isolate, "Duplicated array buffers not supported"); |
2069 return false; | 2194 return false; |
2070 } | 2195 } |
2071 seen_objects->Add(array_buffer); | 2196 seen_objects->Add(array_buffer); |
2072 if (FindInObjectList(array_buffer, to_transfer)) { | 2197 if (FindInObjectList(array_buffer, to_transfer)) { |
2073 // Transfer ArrayBuffer | 2198 // Transfer ArrayBuffer |
2074 if (!array_buffer->IsNeuterable()) { | 2199 if (!array_buffer->IsNeuterable()) { |
2075 Throw(isolate, "Attempting to transfer an un-neuterable ArrayBuffer"); | 2200 Throw(isolate, "Attempting to transfer an un-neuterable ArrayBuffer"); |
2076 return false; | 2201 return false; |
(...skipping 12 matching lines...) Expand all Loading... |
2089 return false; | 2214 return false; |
2090 } | 2215 } |
2091 | 2216 |
2092 int byte_length = static_cast<int>(contents.ByteLength()); | 2217 int byte_length = static_cast<int>(contents.ByteLength()); |
2093 out_data->WriteTag(kSerializationTagArrayBuffer); | 2218 out_data->WriteTag(kSerializationTagArrayBuffer); |
2094 out_data->Write(byte_length); | 2219 out_data->Write(byte_length); |
2095 out_data->WriteMemory(contents.Data(), | 2220 out_data->WriteMemory(contents.Data(), |
2096 static_cast<int>(contents.ByteLength())); | 2221 static_cast<int>(contents.ByteLength())); |
2097 } | 2222 } |
2098 } else if (value->IsSharedArrayBuffer()) { | 2223 } else if (value->IsSharedArrayBuffer()) { |
2099 Handle<SharedArrayBuffer> sab = Handle<SharedArrayBuffer>::Cast(value); | 2224 Local<SharedArrayBuffer> sab = Local<SharedArrayBuffer>::Cast(value); |
2100 if (FindInObjectList(sab, *seen_objects)) { | 2225 if (FindInObjectList(sab, *seen_objects)) { |
2101 Throw(isolate, "Duplicated shared array buffers not supported"); | 2226 Throw(isolate, "Duplicated shared array buffers not supported"); |
2102 return false; | 2227 return false; |
2103 } | 2228 } |
2104 seen_objects->Add(sab); | 2229 seen_objects->Add(sab); |
2105 if (!FindInObjectList(sab, to_transfer)) { | 2230 if (!FindInObjectList(sab, to_transfer)) { |
2106 Throw(isolate, "SharedArrayBuffer must be transferred"); | 2231 Throw(isolate, "SharedArrayBuffer must be transferred"); |
2107 return false; | 2232 return false; |
2108 } | 2233 } |
2109 | 2234 |
2110 SharedArrayBuffer::Contents contents; | 2235 SharedArrayBuffer::Contents contents; |
2111 if (sab->IsExternal()) { | 2236 if (sab->IsExternal()) { |
2112 contents = sab->GetContents(); | 2237 contents = sab->GetContents(); |
2113 } else { | 2238 } else { |
2114 contents = sab->Externalize(); | 2239 contents = sab->Externalize(); |
2115 externalized_shared_contents_.Add(contents); | 2240 externalized_shared_contents_.Add(contents); |
2116 } | 2241 } |
2117 out_data->WriteSharedArrayBufferContents(contents); | 2242 out_data->WriteSharedArrayBufferContents(contents); |
2118 } else if (value->IsObject()) { | 2243 } else if (value->IsObject()) { |
2119 Handle<Object> object = Handle<Object>::Cast(value); | 2244 Local<Object> object = Local<Object>::Cast(value); |
2120 if (FindInObjectList(object, *seen_objects)) { | 2245 if (FindInObjectList(object, *seen_objects)) { |
2121 Throw(isolate, "Duplicated objects not supported"); | 2246 Throw(isolate, "Duplicated objects not supported"); |
2122 return false; | 2247 return false; |
2123 } | 2248 } |
2124 seen_objects->Add(object); | 2249 seen_objects->Add(object); |
2125 Local<Array> property_names; | 2250 Local<Array> property_names; |
2126 if (!object->GetOwnPropertyNames(context).ToLocal(&property_names)) { | 2251 if (!object->GetOwnPropertyNames(context).ToLocal(&property_names)) { |
2127 Throw(isolate, "Unable to get property names"); | 2252 Throw(isolate, "Unable to get property names"); |
2128 return false; | 2253 return false; |
2129 } | 2254 } |
2130 | 2255 |
2131 uint32_t length = property_names->Length(); | 2256 uint32_t length = property_names->Length(); |
2132 out_data->WriteTag(kSerializationTagObject); | 2257 out_data->WriteTag(kSerializationTagObject); |
2133 out_data->Write(length); | 2258 out_data->Write(length); |
2134 for (uint32_t i = 0; i < length; ++i) { | 2259 for (uint32_t i = 0; i < length; ++i) { |
2135 Handle<Value> name; | 2260 Local<Value> name; |
2136 Handle<Value> property_value; | 2261 Local<Value> property_value; |
2137 if (property_names->Get(context, i).ToLocal(&name) && | 2262 if (property_names->Get(context, i).ToLocal(&name) && |
2138 object->Get(context, name).ToLocal(&property_value)) { | 2263 object->Get(context, name).ToLocal(&property_value)) { |
2139 if (!SerializeValue(isolate, name, to_transfer, seen_objects, out_data)) | 2264 if (!SerializeValue(isolate, name, to_transfer, seen_objects, out_data)) |
2140 return false; | 2265 return false; |
2141 if (!SerializeValue(isolate, property_value, to_transfer, seen_objects, | 2266 if (!SerializeValue(isolate, property_value, to_transfer, seen_objects, |
2142 out_data)) | 2267 out_data)) |
2143 return false; | 2268 return false; |
2144 } else { | 2269 } else { |
2145 Throw(isolate, "Failed to serialize property."); | 2270 Throw(isolate, "Failed to serialize property."); |
2146 return false; | 2271 return false; |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2179 result = False(isolate); | 2304 result = False(isolate); |
2180 break; | 2305 break; |
2181 case kSerializationTagNumber: | 2306 case kSerializationTagNumber: |
2182 result = Number::New(isolate, data.Read<double>(offset)); | 2307 result = Number::New(isolate, data.Read<double>(offset)); |
2183 break; | 2308 break; |
2184 case kSerializationTagString: { | 2309 case kSerializationTagString: { |
2185 int length = data.Read<int>(offset); | 2310 int length = data.Read<int>(offset); |
2186 CHECK(length >= 0); | 2311 CHECK(length >= 0); |
2187 std::vector<char> buffer(length + 1); // + 1 so it is never empty. | 2312 std::vector<char> buffer(length + 1); // + 1 so it is never empty. |
2188 data.ReadMemory(&buffer[0], length, offset); | 2313 data.ReadMemory(&buffer[0], length, offset); |
2189 MaybeLocal<String> str = String::NewFromUtf8( | 2314 MaybeLocal<String> str = |
2190 isolate, &buffer[0], String::kNormalString, length); | 2315 String::NewFromUtf8(isolate, &buffer[0], NewStringType::kNormal, |
| 2316 length).ToLocalChecked(); |
2191 if (!str.IsEmpty()) result = str.ToLocalChecked(); | 2317 if (!str.IsEmpty()) result = str.ToLocalChecked(); |
2192 break; | 2318 break; |
2193 } | 2319 } |
2194 case kSerializationTagArray: { | 2320 case kSerializationTagArray: { |
2195 uint32_t length = data.Read<uint32_t>(offset); | 2321 uint32_t length = data.Read<uint32_t>(offset); |
2196 Handle<Array> array = Array::New(isolate, length); | 2322 Local<Array> array = Array::New(isolate, length); |
2197 for (uint32_t i = 0; i < length; ++i) { | 2323 for (uint32_t i = 0; i < length; ++i) { |
2198 Local<Value> element_value; | 2324 Local<Value> element_value; |
2199 CHECK(DeserializeValue(isolate, data, offset).ToLocal(&element_value)); | 2325 CHECK(DeserializeValue(isolate, data, offset).ToLocal(&element_value)); |
2200 array->Set(i, element_value); | 2326 array->Set(isolate->GetCurrentContext(), i, element_value).FromJust(); |
2201 } | 2327 } |
2202 result = array; | 2328 result = array; |
2203 break; | 2329 break; |
2204 } | 2330 } |
2205 case kSerializationTagObject: { | 2331 case kSerializationTagObject: { |
2206 int length = data.Read<int>(offset); | 2332 int length = data.Read<int>(offset); |
2207 Handle<Object> object = Object::New(isolate); | 2333 Local<Object> object = Object::New(isolate); |
2208 for (int i = 0; i < length; ++i) { | 2334 for (int i = 0; i < length; ++i) { |
2209 Local<Value> property_name; | 2335 Local<Value> property_name; |
2210 CHECK(DeserializeValue(isolate, data, offset).ToLocal(&property_name)); | 2336 CHECK(DeserializeValue(isolate, data, offset).ToLocal(&property_name)); |
2211 Local<Value> property_value; | 2337 Local<Value> property_value; |
2212 CHECK(DeserializeValue(isolate, data, offset).ToLocal(&property_value)); | 2338 CHECK(DeserializeValue(isolate, data, offset).ToLocal(&property_value)); |
2213 object->Set(property_name, property_value); | 2339 object->Set(isolate->GetCurrentContext(), property_name, property_value) |
| 2340 .FromJust(); |
2214 } | 2341 } |
2215 result = object; | 2342 result = object; |
2216 break; | 2343 break; |
2217 } | 2344 } |
2218 case kSerializationTagArrayBuffer: { | 2345 case kSerializationTagArrayBuffer: { |
2219 int byte_length = data.Read<int>(offset); | 2346 int byte_length = data.Read<int>(offset); |
2220 Handle<ArrayBuffer> array_buffer = ArrayBuffer::New(isolate, byte_length); | 2347 Local<ArrayBuffer> array_buffer = ArrayBuffer::New(isolate, byte_length); |
2221 ArrayBuffer::Contents contents = array_buffer->GetContents(); | 2348 ArrayBuffer::Contents contents = array_buffer->GetContents(); |
2222 DCHECK(static_cast<size_t>(byte_length) == contents.ByteLength()); | 2349 DCHECK(static_cast<size_t>(byte_length) == contents.ByteLength()); |
2223 data.ReadMemory(contents.Data(), byte_length, offset); | 2350 data.ReadMemory(contents.Data(), byte_length, offset); |
2224 result = array_buffer; | 2351 result = array_buffer; |
2225 break; | 2352 break; |
2226 } | 2353 } |
2227 case kSerializationTagTransferredArrayBuffer: { | 2354 case kSerializationTagTransferredArrayBuffer: { |
2228 ArrayBuffer::Contents contents; | 2355 ArrayBuffer::Contents contents; |
2229 data.ReadArrayBufferContents(&contents, offset); | 2356 data.ReadArrayBufferContents(&contents, offset); |
2230 result = ArrayBuffer::New(isolate, contents.Data(), contents.ByteLength(), | 2357 result = ArrayBuffer::New(isolate, contents.Data(), contents.ByteLength(), |
(...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2470 } | 2597 } |
2471 | 2598 |
2472 } // namespace v8 | 2599 } // namespace v8 |
2473 | 2600 |
2474 | 2601 |
2475 #ifndef GOOGLE3 | 2602 #ifndef GOOGLE3 |
2476 int main(int argc, char* argv[]) { | 2603 int main(int argc, char* argv[]) { |
2477 return v8::Shell::Main(argc, argv); | 2604 return v8::Shell::Main(argc, argv); |
2478 } | 2605 } |
2479 #endif | 2606 #endif |
OLD | NEW |