OLD | NEW |
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 386 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
397 *v8::Local<v8::Function>::Cast(env->Global()->Get(v8_str("closure2")))); | 397 *v8::Local<v8::Function>::Cast(env->Global()->Get(v8_str("closure2")))); |
398 CHECK(fun1->IsOptimized() | 398 CHECK(fun1->IsOptimized() |
399 || !CcTest::i_isolate()->use_crankshaft() || !fun1->IsOptimizable()); | 399 || !CcTest::i_isolate()->use_crankshaft() || !fun1->IsOptimizable()); |
400 CHECK(fun2->IsOptimized() | 400 CHECK(fun2->IsOptimized() |
401 || !CcTest::i_isolate()->use_crankshaft() || !fun2->IsOptimizable()); | 401 || !CcTest::i_isolate()->use_crankshaft() || !fun2->IsOptimizable()); |
402 CHECK_EQ(fun1->code(), fun2->code()); | 402 CHECK_EQ(fun1->code(), fun2->code()); |
403 } | 403 } |
404 } | 404 } |
405 | 405 |
406 | 406 |
| 407 TEST(CompileFunctionInContext) { |
| 408 CcTest::InitializeVM(); |
| 409 v8::HandleScope scope(CcTest::isolate()); |
| 410 LocalContext env; |
| 411 CompileRun("var r = 10;"); |
| 412 v8::Local<v8::Object> math = |
| 413 v8::Local<v8::Object>::Cast(env->Global()->Get(v8_str("Math"))); |
| 414 v8::ScriptCompiler::Source script_source(v8_str( |
| 415 "a = PI * r * r;" |
| 416 "x = r * cos(PI);" |
| 417 "y = r * sin(PI / 2);")); |
| 418 v8::Local<v8::Function> fun = v8::ScriptCompiler::CompileFunctionInContext( |
| 419 CcTest::isolate(), &script_source, env.local(), 1, &math); |
| 420 CHECK(!fun.IsEmpty()); |
| 421 fun->Call(env->Global(), 0, NULL); |
| 422 CHECK(env->Global()->Has(v8_str("a"))); |
| 423 v8::Local<v8::Value> a = env->Global()->Get(v8_str("a")); |
| 424 CHECK(a->IsNumber()); |
| 425 CHECK(env->Global()->Has(v8_str("x"))); |
| 426 v8::Local<v8::Value> x = env->Global()->Get(v8_str("x")); |
| 427 CHECK(x->IsNumber()); |
| 428 CHECK(env->Global()->Has(v8_str("y"))); |
| 429 v8::Local<v8::Value> y = env->Global()->Get(v8_str("y")); |
| 430 CHECK(y->IsNumber()); |
| 431 CHECK_EQ(314.1592653589793, a->NumberValue()); |
| 432 CHECK_EQ(-10.0, x->NumberValue()); |
| 433 CHECK_EQ(10.0, y->NumberValue()); |
| 434 } |
| 435 |
| 436 |
| 437 TEST(CompileFunctionInContextComplex) { |
| 438 CcTest::InitializeVM(); |
| 439 v8::HandleScope scope(CcTest::isolate()); |
| 440 LocalContext env; |
| 441 CompileRun( |
| 442 "var x = 1;" |
| 443 "var y = 2;" |
| 444 "var z = 4;" |
| 445 "var a = {x: 8, y: 16};" |
| 446 "var b = {x: 32};"); |
| 447 v8::Local<v8::Object> ext[2]; |
| 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"))); |
| 450 v8::ScriptCompiler::Source script_source(v8_str("result = x + y + z")); |
| 451 v8::Local<v8::Function> fun = v8::ScriptCompiler::CompileFunctionInContext( |
| 452 CcTest::isolate(), &script_source, env.local(), 2, ext); |
| 453 CHECK(!fun.IsEmpty()); |
| 454 fun->Call(env->Global(), 0, NULL); |
| 455 CHECK(env->Global()->Has(v8_str("result"))); |
| 456 v8::Local<v8::Value> result = env->Global()->Get(v8_str("result")); |
| 457 CHECK(result->IsNumber()); |
| 458 CHECK_EQ(52.0, result->NumberValue()); |
| 459 } |
| 460 |
| 461 |
407 #ifdef ENABLE_DISASSEMBLER | 462 #ifdef ENABLE_DISASSEMBLER |
408 static Handle<JSFunction> GetJSFunction(v8::Handle<v8::Object> obj, | 463 static Handle<JSFunction> GetJSFunction(v8::Handle<v8::Object> obj, |
409 const char* property_name) { | 464 const char* property_name) { |
410 v8::Local<v8::Function> fun = | 465 v8::Local<v8::Function> fun = |
411 v8::Local<v8::Function>::Cast(obj->Get(v8_str(property_name))); | 466 v8::Local<v8::Function>::Cast(obj->Get(v8_str(property_name))); |
412 return v8::Utils::OpenHandle(*fun); | 467 return v8::Utils::OpenHandle(*fun); |
413 } | 468 } |
414 | 469 |
415 | 470 |
416 static void CheckCodeForUnsafeLiteral(Handle<JSFunction> f) { | 471 static void CheckCodeForUnsafeLiteral(Handle<JSFunction> f) { |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
449 CompileRun("function f() { a = 12345678 }; f();"); | 504 CompileRun("function f() { a = 12345678 }; f();"); |
450 CheckCodeForUnsafeLiteral(GetJSFunction(context->Global(), "f")); | 505 CheckCodeForUnsafeLiteral(GetJSFunction(context->Global(), "f")); |
451 CompileRun("function f(x) { a = 12345678 + x}; f(1);"); | 506 CompileRun("function f(x) { a = 12345678 + x}; f(1);"); |
452 CheckCodeForUnsafeLiteral(GetJSFunction(context->Global(), "f")); | 507 CheckCodeForUnsafeLiteral(GetJSFunction(context->Global(), "f")); |
453 CompileRun("function f(x) { var arguments = 1; x += 12345678}; f(1);"); | 508 CompileRun("function f(x) { var arguments = 1; x += 12345678}; f(1);"); |
454 CheckCodeForUnsafeLiteral(GetJSFunction(context->Global(), "f")); | 509 CheckCodeForUnsafeLiteral(GetJSFunction(context->Global(), "f")); |
455 CompileRun("function f(x) { var arguments = 1; x = 12345678}; f(1);"); | 510 CompileRun("function f(x) { var arguments = 1; x = 12345678}; f(1);"); |
456 CheckCodeForUnsafeLiteral(GetJSFunction(context->Global(), "f")); | 511 CheckCodeForUnsafeLiteral(GetJSFunction(context->Global(), "f")); |
457 } | 512 } |
458 #endif | 513 #endif |
OLD | NEW |