OLD | NEW |
---|---|
1 // Copyright 2008 the V8 project authors. All rights reserved. | 1 // Copyright 2008 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 253 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
264 static RegisterThreadedTest* first_; | 264 static RegisterThreadedTest* first_; |
265 static int count_; | 265 static int count_; |
266 CcTest::TestFunction* callback_; | 266 CcTest::TestFunction* callback_; |
267 RegisterThreadedTest* prev_; | 267 RegisterThreadedTest* prev_; |
268 const char* name_; | 268 const char* name_; |
269 }; | 269 }; |
270 | 270 |
271 // A LocalContext holds a reference to a v8::Context. | 271 // A LocalContext holds a reference to a v8::Context. |
272 class LocalContext { | 272 class LocalContext { |
273 public: | 273 public: |
274 LocalContext(v8::Isolate* isolate, | 274 LocalContext(v8::Isolate* isolate, v8::ExtensionConfiguration* extensions = 0, |
275 v8::ExtensionConfiguration* extensions = 0, | 275 v8::Local<v8::ObjectTemplate> global_template = |
276 v8::Handle<v8::ObjectTemplate> global_template = | 276 v8::Local<v8::ObjectTemplate>(), |
277 v8::Handle<v8::ObjectTemplate>(), | 277 v8::Local<v8::Value> global_object = v8::Local<v8::Value>()) { |
278 v8::Handle<v8::Value> global_object = v8::Handle<v8::Value>()) { | |
279 Initialize(isolate, extensions, global_template, global_object); | 278 Initialize(isolate, extensions, global_template, global_object); |
280 } | 279 } |
281 | 280 |
282 LocalContext(v8::ExtensionConfiguration* extensions = 0, | 281 LocalContext(v8::ExtensionConfiguration* extensions = 0, |
283 v8::Handle<v8::ObjectTemplate> global_template = | 282 v8::Local<v8::ObjectTemplate> global_template = |
284 v8::Handle<v8::ObjectTemplate>(), | 283 v8::Local<v8::ObjectTemplate>(), |
285 v8::Handle<v8::Value> global_object = v8::Handle<v8::Value>()) { | 284 v8::Local<v8::Value> global_object = v8::Local<v8::Value>()) { |
286 Initialize(CcTest::isolate(), extensions, global_template, global_object); | 285 Initialize(CcTest::isolate(), extensions, global_template, global_object); |
287 } | 286 } |
288 | 287 |
289 virtual ~LocalContext() { | 288 virtual ~LocalContext() { |
290 v8::HandleScope scope(isolate_); | 289 v8::HandleScope scope(isolate_); |
291 v8::Local<v8::Context>::New(isolate_, context_)->Exit(); | 290 v8::Local<v8::Context>::New(isolate_, context_)->Exit(); |
292 context_.Reset(); | 291 context_.Reset(); |
293 } | 292 } |
294 | 293 |
295 v8::Context* operator->() { | 294 v8::Context* operator->() { |
296 return *reinterpret_cast<v8::Context**>(&context_); | 295 return *reinterpret_cast<v8::Context**>(&context_); |
297 } | 296 } |
298 v8::Context* operator*() { return operator->(); } | 297 v8::Context* operator*() { return operator->(); } |
299 bool IsReady() { return !context_.IsEmpty(); } | 298 bool IsReady() { return !context_.IsEmpty(); } |
300 | 299 |
301 v8::Local<v8::Context> local() { | 300 v8::Local<v8::Context> local() { |
302 return v8::Local<v8::Context>::New(isolate_, context_); | 301 return v8::Local<v8::Context>::New(isolate_, context_); |
303 } | 302 } |
304 | 303 |
305 private: | 304 private: |
306 void Initialize(v8::Isolate* isolate, | 305 void Initialize(v8::Isolate* isolate, v8::ExtensionConfiguration* extensions, |
307 v8::ExtensionConfiguration* extensions, | 306 v8::Local<v8::ObjectTemplate> global_template, |
308 v8::Handle<v8::ObjectTemplate> global_template, | 307 v8::Local<v8::Value> global_object) { |
309 v8::Handle<v8::Value> global_object) { | |
310 v8::HandleScope scope(isolate); | 308 v8::HandleScope scope(isolate); |
311 v8::Local<v8::Context> context = v8::Context::New(isolate, | 309 v8::Local<v8::Context> context = v8::Context::New(isolate, |
312 extensions, | 310 extensions, |
313 global_template, | 311 global_template, |
314 global_object); | 312 global_object); |
315 context_.Reset(isolate, context); | 313 context_.Reset(isolate, context); |
316 context->Enter(); | 314 context->Enter(); |
317 // We can't do this later perhaps because of a fatal error. | 315 // We can't do this later perhaps because of a fatal error. |
318 isolate_ = isolate; | 316 isolate_ = isolate; |
319 } | 317 } |
(...skipping 10 matching lines...) Expand all Loading... | |
330 return converted; | 328 return converted; |
331 } | 329 } |
332 | 330 |
333 | 331 |
334 static inline v8::Local<v8::Value> v8_num(double x) { | 332 static inline v8::Local<v8::Value> v8_num(double x) { |
335 return v8::Number::New(v8::Isolate::GetCurrent(), x); | 333 return v8::Number::New(v8::Isolate::GetCurrent(), x); |
336 } | 334 } |
337 | 335 |
338 | 336 |
339 static inline v8::Local<v8::String> v8_str(const char* x) { | 337 static inline v8::Local<v8::String> v8_str(const char* x) { |
340 return v8::String::NewFromUtf8(v8::Isolate::GetCurrent(), x); | 338 return v8::String::NewFromUtf8(v8::Isolate::GetCurrent(), x, |
339 v8::NewStringType::kNormal) | |
340 .ToLocalChecked(); | |
341 } | 341 } |
342 | 342 |
343 | 343 |
344 static inline v8::Local<v8::Symbol> v8_symbol(const char* name) { | 344 static inline v8::Local<v8::Symbol> v8_symbol(const char* name) { |
345 return v8::Symbol::New(v8::Isolate::GetCurrent(), v8_str(name)); | 345 return v8::Symbol::New(v8::Isolate::GetCurrent(), v8_str(name)); |
346 } | 346 } |
347 | 347 |
348 | 348 |
349 static inline v8::Local<v8::Script> v8_compile(v8::Local<v8::String> x) { | |
350 v8::Local<v8::Script> result; | |
351 if (v8::Script::Compile(v8::Isolate::GetCurrent()->GetCurrentContext(), x) | |
352 .ToLocal(&result)) { | |
353 return result; | |
354 } | |
355 return v8::Local<v8::Script>(); | |
356 } | |
357 | |
358 | |
349 static inline v8::Local<v8::Script> v8_compile(const char* x) { | 359 static inline v8::Local<v8::Script> v8_compile(const char* x) { |
350 return v8::Script::Compile(v8_str(x)); | 360 return v8_compile(v8_str(x)); |
351 } | 361 } |
352 | 362 |
353 | 363 |
354 static inline v8::Local<v8::Script> v8_compile(v8::Local<v8::String> x) { | |
355 return v8::Script::Compile(x); | |
356 } | |
357 | |
358 | |
359 static inline v8::Local<v8::Script> CompileWithOrigin( | 364 static inline v8::Local<v8::Script> CompileWithOrigin( |
360 v8::Local<v8::String> source, v8::Local<v8::String> origin_url) { | 365 v8::Local<v8::String> source, v8::Local<v8::String> origin_url) { |
361 v8::ScriptOrigin origin(origin_url); | 366 v8::ScriptOrigin origin(origin_url); |
362 v8::ScriptCompiler::Source script_source(source, origin); | 367 v8::ScriptCompiler::Source script_source(source, origin); |
363 return v8::ScriptCompiler::Compile( | 368 return v8::ScriptCompiler::Compile( |
364 v8::Isolate::GetCurrent(), &script_source); | 369 v8::Isolate::GetCurrent()->GetCurrentContext(), &script_source) |
370 .ToLocalChecked(); | |
365 } | 371 } |
366 | 372 |
367 | 373 |
368 static inline v8::Local<v8::Script> CompileWithOrigin( | 374 static inline v8::Local<v8::Script> CompileWithOrigin( |
369 v8::Local<v8::String> source, const char* origin_url) { | 375 v8::Local<v8::String> source, const char* origin_url) { |
370 return CompileWithOrigin(source, v8_str(origin_url)); | 376 return CompileWithOrigin(source, v8_str(origin_url)); |
371 } | 377 } |
372 | 378 |
373 | 379 |
374 static inline v8::Local<v8::Script> CompileWithOrigin(const char* source, | 380 static inline v8::Local<v8::Script> CompileWithOrigin(const char* source, |
375 const char* origin_url) { | 381 const char* origin_url) { |
376 return CompileWithOrigin(v8_str(source), v8_str(origin_url)); | 382 return CompileWithOrigin(v8_str(source), v8_str(origin_url)); |
377 } | 383 } |
378 | 384 |
379 | 385 |
380 // Helper functions that compile and run the source. | 386 // Helper functions that compile and run the source. |
381 static inline v8::Local<v8::Value> CompileRun(const char* source) { | |
382 return v8::Script::Compile(v8_str(source))->Run(); | |
383 } | |
384 | |
385 | |
386 // Helper functions that compile and run the source. | |
387 static inline v8::MaybeLocal<v8::Value> CompileRun( | 387 static inline v8::MaybeLocal<v8::Value> CompileRun( |
388 v8::Local<v8::Context> context, const char* source) { | 388 v8::Local<v8::Context> context, const char* source) { |
389 return v8::Script::Compile(v8_str(source))->Run(context); | 389 return v8::Script::Compile(context, v8_str(source)) |
390 .ToLocalChecked() | |
391 ->Run(context); | |
390 } | 392 } |
391 | 393 |
392 | 394 |
393 static inline v8::Local<v8::Value> CompileRun(v8::Local<v8::String> source) { | 395 static inline v8::Local<v8::Value> CompileRun(v8::Local<v8::String> source) { |
394 return v8::Script::Compile(source)->Run(); | 396 v8::Local<v8::Value> result; |
397 if (v8_compile(source) | |
398 ->Run(v8::Isolate::GetCurrent()->GetCurrentContext()) | |
399 .ToLocal(&result)) { | |
400 return result; | |
401 } | |
402 return v8::Local<v8::Value>(); | |
395 } | 403 } |
396 | 404 |
397 | 405 |
406 // Helper functions that compile and run the source. | |
407 static inline v8::Local<v8::Value> CompileRun(const char* source) { | |
408 return CompileRun(v8_str(source)); | |
409 } | |
410 | |
411 | |
398 static inline v8::Local<v8::Value> ParserCacheCompileRun(const char* source) { | 412 static inline v8::Local<v8::Value> ParserCacheCompileRun(const char* source) { |
399 // Compile once just to get the preparse data, then compile the second time | 413 // Compile once just to get the preparse data, then compile the second time |
400 // using the data. | 414 // using the data. |
401 v8::Isolate* isolate = v8::Isolate::GetCurrent(); | 415 v8::Isolate* isolate = v8::Isolate::GetCurrent(); |
416 v8::Local<v8::Context> context = isolate->GetCurrentContext(); | |
402 v8::ScriptCompiler::Source script_source(v8_str(source)); | 417 v8::ScriptCompiler::Source script_source(v8_str(source)); |
403 v8::ScriptCompiler::Compile(isolate, &script_source, | 418 v8::ScriptCompiler::Compile(context, &script_source, |
404 v8::ScriptCompiler::kProduceParserCache); | 419 v8::ScriptCompiler::kProduceParserCache) |
420 .ToLocalChecked(); | |
405 | 421 |
406 // Check whether we received cached data, and if so use it. | 422 // Check whether we received cached data, and if so use it. |
407 v8::ScriptCompiler::CompileOptions options = | 423 v8::ScriptCompiler::CompileOptions options = |
408 script_source.GetCachedData() ? v8::ScriptCompiler::kConsumeParserCache | 424 script_source.GetCachedData() ? v8::ScriptCompiler::kConsumeParserCache |
409 : v8::ScriptCompiler::kNoCompileOptions; | 425 : v8::ScriptCompiler::kNoCompileOptions; |
410 | 426 |
411 return v8::ScriptCompiler::Compile(isolate, &script_source, options)->Run(); | 427 return v8::ScriptCompiler::Compile(context, &script_source, options) |
428 .ToLocalChecked() | |
429 ->Run(context) | |
430 .ToLocalChecked(); | |
412 } | 431 } |
413 | 432 |
414 | 433 |
415 // Helper functions that compile and run the source with given origin. | 434 // Helper functions that compile and run the source with given origin. |
416 static inline v8::Local<v8::Value> CompileRunWithOrigin(const char* source, | 435 static inline v8::Local<v8::Value> CompileRunWithOrigin(const char* source, |
417 const char* origin_url, | 436 const char* origin_url, |
418 int line_number, | 437 int line_number, |
419 int column_number) { | 438 int column_number) { |
420 v8::Isolate* isolate = v8::Isolate::GetCurrent(); | 439 v8::Isolate* isolate = v8::Isolate::GetCurrent(); |
440 v8::Local<v8::Context> context = isolate->GetCurrentContext(); | |
421 v8::ScriptOrigin origin(v8_str(origin_url), | 441 v8::ScriptOrigin origin(v8_str(origin_url), |
422 v8::Integer::New(isolate, line_number), | 442 v8::Integer::New(isolate, line_number), |
423 v8::Integer::New(isolate, column_number)); | 443 v8::Integer::New(isolate, column_number)); |
424 v8::ScriptCompiler::Source script_source(v8_str(source), origin); | 444 v8::ScriptCompiler::Source script_source(v8_str(source), origin); |
425 return v8::ScriptCompiler::Compile(isolate, &script_source)->Run(); | 445 v8::Local<v8::Value> result; |
446 if (v8::ScriptCompiler::Compile(context, &script_source) | |
447 .ToLocalChecked() | |
448 ->Run(context) | |
449 .ToLocal(&result)) { | |
450 return result; | |
451 } | |
452 return v8::Local<v8::Value>(); | |
rmcilroy
2015/09/07 14:01:44
nit - could you create a helper function:
static
| |
426 } | 453 } |
427 | 454 |
428 | 455 |
429 static inline v8::Local<v8::Value> CompileRunWithOrigin( | 456 static inline v8::Local<v8::Value> CompileRunWithOrigin( |
430 v8::Local<v8::String> source, const char* origin_url) { | 457 v8::Local<v8::String> source, const char* origin_url) { |
458 v8::Isolate* isolate = v8::Isolate::GetCurrent(); | |
459 v8::Local<v8::Context> context = isolate->GetCurrentContext(); | |
431 v8::ScriptCompiler::Source script_source( | 460 v8::ScriptCompiler::Source script_source( |
432 source, v8::ScriptOrigin(v8_str(origin_url))); | 461 source, v8::ScriptOrigin(v8_str(origin_url))); |
433 return v8::ScriptCompiler::Compile(v8::Isolate::GetCurrent(), &script_source) | 462 v8::Local<v8::Value> result; |
434 ->Run(); | 463 if (v8::ScriptCompiler::Compile(context, &script_source) |
464 .ToLocalChecked() | |
465 ->Run(context) | |
466 .ToLocal(&result)) { | |
467 return result; | |
468 } | |
469 return v8::Local<v8::Value>(); | |
435 } | 470 } |
436 | 471 |
437 | 472 |
438 static inline v8::Local<v8::Value> CompileRunWithOrigin( | 473 static inline v8::Local<v8::Value> CompileRunWithOrigin( |
439 const char* source, const char* origin_url) { | 474 const char* source, const char* origin_url) { |
440 return CompileRunWithOrigin(v8_str(source), origin_url); | 475 return CompileRunWithOrigin(v8_str(source), origin_url); |
441 } | 476 } |
442 | 477 |
443 | 478 |
444 | 479 |
445 static inline void ExpectString(const char* code, const char* expected) { | 480 static inline void ExpectString(const char* code, const char* expected) { |
446 v8::Local<v8::Value> result = CompileRun(code); | 481 v8::Local<v8::Value> result = CompileRun(code); |
447 CHECK(result->IsString()); | 482 CHECK(result->IsString()); |
448 v8::String::Utf8Value utf8(result); | 483 v8::String::Utf8Value utf8(result); |
449 CHECK_EQ(0, strcmp(expected, *utf8)); | 484 CHECK_EQ(0, strcmp(expected, *utf8)); |
450 } | 485 } |
451 | 486 |
452 | 487 |
453 static inline void ExpectInt32(const char* code, int expected) { | 488 static inline void ExpectInt32(const char* code, int expected) { |
454 v8::Local<v8::Value> result = CompileRun(code); | 489 v8::Local<v8::Value> result = CompileRun(code); |
455 CHECK(result->IsInt32()); | 490 CHECK(result->IsInt32()); |
456 CHECK_EQ(expected, result->Int32Value()); | 491 CHECK_EQ(expected, |
492 result->Int32Value(v8::Isolate::GetCurrent()->GetCurrentContext()) | |
493 .FromJust()); | |
457 } | 494 } |
458 | 495 |
459 | 496 |
460 static inline void ExpectBoolean(const char* code, bool expected) { | 497 static inline void ExpectBoolean(const char* code, bool expected) { |
461 v8::Local<v8::Value> result = CompileRun(code); | 498 v8::Local<v8::Value> result = CompileRun(code); |
462 CHECK(result->IsBoolean()); | 499 CHECK(result->IsBoolean()); |
463 CHECK_EQ(expected, result->BooleanValue()); | 500 CHECK_EQ(expected, |
501 result->BooleanValue(v8::Isolate::GetCurrent()->GetCurrentContext()) | |
502 .FromJust()); | |
464 } | 503 } |
465 | 504 |
466 | 505 |
467 static inline void ExpectTrue(const char* code) { | 506 static inline void ExpectTrue(const char* code) { |
468 ExpectBoolean(code, true); | 507 ExpectBoolean(code, true); |
469 } | 508 } |
470 | 509 |
471 | 510 |
472 static inline void ExpectFalse(const char* code) { | 511 static inline void ExpectFalse(const char* code) { |
473 ExpectBoolean(code, false); | 512 ExpectBoolean(code, false); |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
601 HandleAndZoneScope() {} | 640 HandleAndZoneScope() {} |
602 | 641 |
603 // Prefixing the below with main_ reduces a lot of naming clashes. | 642 // Prefixing the below with main_ reduces a lot of naming clashes. |
604 i::Zone* main_zone() { return &main_zone_; } | 643 i::Zone* main_zone() { return &main_zone_; } |
605 | 644 |
606 private: | 645 private: |
607 i::Zone main_zone_; | 646 i::Zone main_zone_; |
608 }; | 647 }; |
609 | 648 |
610 #endif // ifndef CCTEST_H_ | 649 #endif // ifndef CCTEST_H_ |
OLD | NEW |