Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(336)

Side by Side Diff: test/cctest/test-compiler.cc

Issue 925433002: Make it possible to define arguments for CompileFunctionInContext (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: updates Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/api.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 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 398 matching lines...) Expand 10 before | Expand all | Expand 10 after
409 v8::HandleScope scope(CcTest::isolate()); 409 v8::HandleScope scope(CcTest::isolate());
410 LocalContext env; 410 LocalContext env;
411 CompileRun("var r = 10;"); 411 CompileRun("var r = 10;");
412 v8::Local<v8::Object> math = 412 v8::Local<v8::Object> math =
413 v8::Local<v8::Object>::Cast(env->Global()->Get(v8_str("Math"))); 413 v8::Local<v8::Object>::Cast(env->Global()->Get(v8_str("Math")));
414 v8::ScriptCompiler::Source script_source(v8_str( 414 v8::ScriptCompiler::Source script_source(v8_str(
415 "a = PI * r * r;" 415 "a = PI * r * r;"
416 "x = r * cos(PI);" 416 "x = r * cos(PI);"
417 "y = r * sin(PI / 2);")); 417 "y = r * sin(PI / 2);"));
418 v8::Local<v8::Function> fun = v8::ScriptCompiler::CompileFunctionInContext( 418 v8::Local<v8::Function> fun = v8::ScriptCompiler::CompileFunctionInContext(
419 CcTest::isolate(), &script_source, env.local(), 1, &math); 419 CcTest::isolate(), &script_source, env.local(), 0, NULL, 1, &math);
420 CHECK(!fun.IsEmpty()); 420 CHECK(!fun.IsEmpty());
421 fun->Call(env->Global(), 0, NULL); 421 fun->Call(env->Global(), 0, NULL);
422 CHECK(env->Global()->Has(v8_str("a"))); 422 CHECK(env->Global()->Has(v8_str("a")));
423 v8::Local<v8::Value> a = env->Global()->Get(v8_str("a")); 423 v8::Local<v8::Value> a = env->Global()->Get(v8_str("a"));
424 CHECK(a->IsNumber()); 424 CHECK(a->IsNumber());
425 CHECK(env->Global()->Has(v8_str("x"))); 425 CHECK(env->Global()->Has(v8_str("x")));
426 v8::Local<v8::Value> x = env->Global()->Get(v8_str("x")); 426 v8::Local<v8::Value> x = env->Global()->Get(v8_str("x"));
427 CHECK(x->IsNumber()); 427 CHECK(x->IsNumber());
428 CHECK(env->Global()->Has(v8_str("y"))); 428 CHECK(env->Global()->Has(v8_str("y")));
429 v8::Local<v8::Value> y = env->Global()->Get(v8_str("y")); 429 v8::Local<v8::Value> y = env->Global()->Get(v8_str("y"));
(...skipping 12 matching lines...) Expand all
442 "var x = 1;" 442 "var x = 1;"
443 "var y = 2;" 443 "var y = 2;"
444 "var z = 4;" 444 "var z = 4;"
445 "var a = {x: 8, y: 16};" 445 "var a = {x: 8, y: 16};"
446 "var b = {x: 32};"); 446 "var b = {x: 32};");
447 v8::Local<v8::Object> ext[2]; 447 v8::Local<v8::Object> ext[2];
448 ext[0] = v8::Local<v8::Object>::Cast(env->Global()->Get(v8_str("a"))); 448 ext[0] = v8::Local<v8::Object>::Cast(env->Global()->Get(v8_str("a")));
449 ext[1] = v8::Local<v8::Object>::Cast(env->Global()->Get(v8_str("b"))); 449 ext[1] = v8::Local<v8::Object>::Cast(env->Global()->Get(v8_str("b")));
450 v8::ScriptCompiler::Source script_source(v8_str("result = x + y + z")); 450 v8::ScriptCompiler::Source script_source(v8_str("result = x + y + z"));
451 v8::Local<v8::Function> fun = v8::ScriptCompiler::CompileFunctionInContext( 451 v8::Local<v8::Function> fun = v8::ScriptCompiler::CompileFunctionInContext(
452 CcTest::isolate(), &script_source, env.local(), 2, ext); 452 CcTest::isolate(), &script_source, env.local(), 0, NULL, 2, ext);
453 CHECK(!fun.IsEmpty()); 453 CHECK(!fun.IsEmpty());
454 fun->Call(env->Global(), 0, NULL); 454 fun->Call(env->Global(), 0, NULL);
455 CHECK(env->Global()->Has(v8_str("result"))); 455 CHECK(env->Global()->Has(v8_str("result")));
456 v8::Local<v8::Value> result = env->Global()->Get(v8_str("result")); 456 v8::Local<v8::Value> result = env->Global()->Get(v8_str("result"));
457 CHECK(result->IsNumber()); 457 CHECK(result->IsNumber());
458 CHECK_EQ(52.0, result->NumberValue()); 458 CHECK_EQ(52.0, result->NumberValue());
459 } 459 }
460 460
461 461
462 TEST(CompileFunctionInContextArgs) {
463 CcTest::InitializeVM();
464 v8::HandleScope scope(CcTest::isolate());
465 LocalContext env;
466 CompileRun("var a = {x: 23};");
467 v8::Local<v8::Object> ext[1];
468 ext[0] = v8::Local<v8::Object>::Cast(env->Global()->Get(v8_str("a")));
469 v8::ScriptCompiler::Source script_source(v8_str("result = x + b"));
470 v8::Local<v8::String> arg = v8_str("b");
471 v8::Local<v8::Function> fun = v8::ScriptCompiler::CompileFunctionInContext(
472 CcTest::isolate(), &script_source, env.local(), 1, &arg, 1, ext);
473 CHECK(!fun.IsEmpty());
474 v8::Local<v8::Value> b_value = v8::Number::New(CcTest::isolate(), 42.0);
475 fun->Call(env->Global(), 1, &b_value);
476 CHECK(env->Global()->Has(v8_str("result")));
477 v8::Local<v8::Value> result = env->Global()->Get(v8_str("result"));
478 CHECK(result->IsNumber());
479 CHECK_EQ(65.0, result->NumberValue());
480 }
481
482
483 TEST(CompileFunctionInContextComments) {
484 CcTest::InitializeVM();
485 v8::HandleScope scope(CcTest::isolate());
486 LocalContext env;
487 CompileRun("var a = {x: 23, y: 1, z: 2};");
488 v8::Local<v8::Object> ext[1];
489 ext[0] = v8::Local<v8::Object>::Cast(env->Global()->Get(v8_str("a")));
490 v8::ScriptCompiler::Source script_source(
491 v8_str("result = /* y + */ x + b // + z"));
492 v8::Local<v8::String> arg = v8_str("b");
493 v8::Local<v8::Function> fun = v8::ScriptCompiler::CompileFunctionInContext(
494 CcTest::isolate(), &script_source, env.local(), 1, &arg, 1, ext);
495 CHECK(!fun.IsEmpty());
496 v8::Local<v8::Value> b_value = v8::Number::New(CcTest::isolate(), 42.0);
497 fun->Call(env->Global(), 1, &b_value);
498 CHECK(env->Global()->Has(v8_str("result")));
499 v8::Local<v8::Value> result = env->Global()->Get(v8_str("result"));
500 CHECK(result->IsNumber());
501 CHECK_EQ(65.0, result->NumberValue());
502 }
503
504
505 TEST(CompileFunctionInContextNonIdentifierArgs) {
506 CcTest::InitializeVM();
507 v8::HandleScope scope(CcTest::isolate());
508 LocalContext env;
509 v8::ScriptCompiler::Source script_source(v8_str("result = 1"));
510 v8::Local<v8::String> arg = v8_str("b }");
511 v8::Local<v8::Function> fun = v8::ScriptCompiler::CompileFunctionInContext(
512 CcTest::isolate(), &script_source, env.local(), 1, &arg, 0, NULL);
513 CHECK(fun.IsEmpty());
514 }
515
516
462 #ifdef ENABLE_DISASSEMBLER 517 #ifdef ENABLE_DISASSEMBLER
463 static Handle<JSFunction> GetJSFunction(v8::Handle<v8::Object> obj, 518 static Handle<JSFunction> GetJSFunction(v8::Handle<v8::Object> obj,
464 const char* property_name) { 519 const char* property_name) {
465 v8::Local<v8::Function> fun = 520 v8::Local<v8::Function> fun =
466 v8::Local<v8::Function>::Cast(obj->Get(v8_str(property_name))); 521 v8::Local<v8::Function>::Cast(obj->Get(v8_str(property_name)));
467 return v8::Utils::OpenHandle(*fun); 522 return v8::Utils::OpenHandle(*fun);
468 } 523 }
469 524
470 525
471 static void CheckCodeForUnsafeLiteral(Handle<JSFunction> f) { 526 static void CheckCodeForUnsafeLiteral(Handle<JSFunction> f) {
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
504 CompileRun("function f() { a = 12345678 }; f();"); 559 CompileRun("function f() { a = 12345678 }; f();");
505 CheckCodeForUnsafeLiteral(GetJSFunction(context->Global(), "f")); 560 CheckCodeForUnsafeLiteral(GetJSFunction(context->Global(), "f"));
506 CompileRun("function f(x) { a = 12345678 + x}; f(1);"); 561 CompileRun("function f(x) { a = 12345678 + x}; f(1);");
507 CheckCodeForUnsafeLiteral(GetJSFunction(context->Global(), "f")); 562 CheckCodeForUnsafeLiteral(GetJSFunction(context->Global(), "f"));
508 CompileRun("function f(x) { var arguments = 1; x += 12345678}; f(1);"); 563 CompileRun("function f(x) { var arguments = 1; x += 12345678}; f(1);");
509 CheckCodeForUnsafeLiteral(GetJSFunction(context->Global(), "f")); 564 CheckCodeForUnsafeLiteral(GetJSFunction(context->Global(), "f"));
510 CompileRun("function f(x) { var arguments = 1; x = 12345678}; f(1);"); 565 CompileRun("function f(x) { var arguments = 1; x = 12345678}; f(1);");
511 CheckCodeForUnsafeLiteral(GetJSFunction(context->Global(), "f")); 566 CheckCodeForUnsafeLiteral(GetJSFunction(context->Global(), "f"));
512 } 567 }
513 #endif 568 #endif
OLDNEW
« no previous file with comments | « src/api.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698