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 |
| 412 static inline v8::Local<v8::Value> CompileRun( |
| 413 v8::Local<v8::Context> context, v8::ScriptCompiler::Source* script_source, |
| 414 v8::ScriptCompiler::CompileOptions options) { |
| 415 v8::Local<v8::Value> result; |
| 416 if (v8::ScriptCompiler::Compile(context, script_source, options) |
| 417 .ToLocalChecked() |
| 418 ->Run(context) |
| 419 .ToLocal(&result)) { |
| 420 return result; |
| 421 } |
| 422 return v8::Local<v8::Value>(); |
| 423 } |
| 424 |
| 425 |
398 static inline v8::Local<v8::Value> ParserCacheCompileRun(const char* source) { | 426 static inline v8::Local<v8::Value> ParserCacheCompileRun(const char* source) { |
399 // Compile once just to get the preparse data, then compile the second time | 427 // Compile once just to get the preparse data, then compile the second time |
400 // using the data. | 428 // using the data. |
401 v8::Isolate* isolate = v8::Isolate::GetCurrent(); | 429 v8::Isolate* isolate = v8::Isolate::GetCurrent(); |
| 430 v8::Local<v8::Context> context = isolate->GetCurrentContext(); |
402 v8::ScriptCompiler::Source script_source(v8_str(source)); | 431 v8::ScriptCompiler::Source script_source(v8_str(source)); |
403 v8::ScriptCompiler::Compile(isolate, &script_source, | 432 v8::ScriptCompiler::Compile(context, &script_source, |
404 v8::ScriptCompiler::kProduceParserCache); | 433 v8::ScriptCompiler::kProduceParserCache) |
| 434 .ToLocalChecked(); |
405 | 435 |
406 // Check whether we received cached data, and if so use it. | 436 // Check whether we received cached data, and if so use it. |
407 v8::ScriptCompiler::CompileOptions options = | 437 v8::ScriptCompiler::CompileOptions options = |
408 script_source.GetCachedData() ? v8::ScriptCompiler::kConsumeParserCache | 438 script_source.GetCachedData() ? v8::ScriptCompiler::kConsumeParserCache |
409 : v8::ScriptCompiler::kNoCompileOptions; | 439 : v8::ScriptCompiler::kNoCompileOptions; |
410 | 440 |
411 return v8::ScriptCompiler::Compile(isolate, &script_source, options)->Run(); | 441 return CompileRun(context, &script_source, options); |
412 } | 442 } |
413 | 443 |
414 | 444 |
415 // Helper functions that compile and run the source with given origin. | 445 // Helper functions that compile and run the source with given origin. |
416 static inline v8::Local<v8::Value> CompileRunWithOrigin(const char* source, | 446 static inline v8::Local<v8::Value> CompileRunWithOrigin(const char* source, |
417 const char* origin_url, | 447 const char* origin_url, |
418 int line_number, | 448 int line_number, |
419 int column_number) { | 449 int column_number) { |
420 v8::Isolate* isolate = v8::Isolate::GetCurrent(); | 450 v8::Isolate* isolate = v8::Isolate::GetCurrent(); |
| 451 v8::Local<v8::Context> context = isolate->GetCurrentContext(); |
421 v8::ScriptOrigin origin(v8_str(origin_url), | 452 v8::ScriptOrigin origin(v8_str(origin_url), |
422 v8::Integer::New(isolate, line_number), | 453 v8::Integer::New(isolate, line_number), |
423 v8::Integer::New(isolate, column_number)); | 454 v8::Integer::New(isolate, column_number)); |
424 v8::ScriptCompiler::Source script_source(v8_str(source), origin); | 455 v8::ScriptCompiler::Source script_source(v8_str(source), origin); |
425 return v8::ScriptCompiler::Compile(isolate, &script_source)->Run(); | 456 return CompileRun(context, &script_source, |
| 457 v8::ScriptCompiler::CompileOptions()); |
426 } | 458 } |
427 | 459 |
428 | 460 |
429 static inline v8::Local<v8::Value> CompileRunWithOrigin( | 461 static inline v8::Local<v8::Value> CompileRunWithOrigin( |
430 v8::Local<v8::String> source, const char* origin_url) { | 462 v8::Local<v8::String> source, const char* origin_url) { |
| 463 v8::Isolate* isolate = v8::Isolate::GetCurrent(); |
| 464 v8::Local<v8::Context> context = isolate->GetCurrentContext(); |
431 v8::ScriptCompiler::Source script_source( | 465 v8::ScriptCompiler::Source script_source( |
432 source, v8::ScriptOrigin(v8_str(origin_url))); | 466 source, v8::ScriptOrigin(v8_str(origin_url))); |
433 return v8::ScriptCompiler::Compile(v8::Isolate::GetCurrent(), &script_source) | 467 return CompileRun(context, &script_source, |
434 ->Run(); | 468 v8::ScriptCompiler::CompileOptions()); |
435 } | 469 } |
436 | 470 |
437 | 471 |
438 static inline v8::Local<v8::Value> CompileRunWithOrigin( | 472 static inline v8::Local<v8::Value> CompileRunWithOrigin( |
439 const char* source, const char* origin_url) { | 473 const char* source, const char* origin_url) { |
440 return CompileRunWithOrigin(v8_str(source), origin_url); | 474 return CompileRunWithOrigin(v8_str(source), origin_url); |
441 } | 475 } |
442 | 476 |
443 | 477 |
444 | 478 |
445 static inline void ExpectString(const char* code, const char* expected) { | 479 static inline void ExpectString(const char* code, const char* expected) { |
446 v8::Local<v8::Value> result = CompileRun(code); | 480 v8::Local<v8::Value> result = CompileRun(code); |
447 CHECK(result->IsString()); | 481 CHECK(result->IsString()); |
448 v8::String::Utf8Value utf8(result); | 482 v8::String::Utf8Value utf8(result); |
449 CHECK_EQ(0, strcmp(expected, *utf8)); | 483 CHECK_EQ(0, strcmp(expected, *utf8)); |
450 } | 484 } |
451 | 485 |
452 | 486 |
453 static inline void ExpectInt32(const char* code, int expected) { | 487 static inline void ExpectInt32(const char* code, int expected) { |
454 v8::Local<v8::Value> result = CompileRun(code); | 488 v8::Local<v8::Value> result = CompileRun(code); |
455 CHECK(result->IsInt32()); | 489 CHECK(result->IsInt32()); |
456 CHECK_EQ(expected, result->Int32Value()); | 490 CHECK_EQ(expected, |
| 491 result->Int32Value(v8::Isolate::GetCurrent()->GetCurrentContext()) |
| 492 .FromJust()); |
457 } | 493 } |
458 | 494 |
459 | 495 |
460 static inline void ExpectBoolean(const char* code, bool expected) { | 496 static inline void ExpectBoolean(const char* code, bool expected) { |
461 v8::Local<v8::Value> result = CompileRun(code); | 497 v8::Local<v8::Value> result = CompileRun(code); |
462 CHECK(result->IsBoolean()); | 498 CHECK(result->IsBoolean()); |
463 CHECK_EQ(expected, result->BooleanValue()); | 499 CHECK_EQ(expected, |
| 500 result->BooleanValue(v8::Isolate::GetCurrent()->GetCurrentContext()) |
| 501 .FromJust()); |
464 } | 502 } |
465 | 503 |
466 | 504 |
467 static inline void ExpectTrue(const char* code) { | 505 static inline void ExpectTrue(const char* code) { |
468 ExpectBoolean(code, true); | 506 ExpectBoolean(code, true); |
469 } | 507 } |
470 | 508 |
471 | 509 |
472 static inline void ExpectFalse(const char* code) { | 510 static inline void ExpectFalse(const char* code) { |
473 ExpectBoolean(code, false); | 511 ExpectBoolean(code, false); |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
601 HandleAndZoneScope() {} | 639 HandleAndZoneScope() {} |
602 | 640 |
603 // Prefixing the below with main_ reduces a lot of naming clashes. | 641 // Prefixing the below with main_ reduces a lot of naming clashes. |
604 i::Zone* main_zone() { return &main_zone_; } | 642 i::Zone* main_zone() { return &main_zone_; } |
605 | 643 |
606 private: | 644 private: |
607 i::Zone main_zone_; | 645 i::Zone main_zone_; |
608 }; | 646 }; |
609 | 647 |
610 #endif // ifndef CCTEST_H_ | 648 #endif // ifndef CCTEST_H_ |
OLD | NEW |