Chromium Code Reviews| 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); | |
|
Yang
2015/02/09 10:21:19
Would be nicer to have more than one extension obj
| |
| 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 | |
| 407 #ifdef ENABLE_DISASSEMBLER | 437 #ifdef ENABLE_DISASSEMBLER |
| 408 static Handle<JSFunction> GetJSFunction(v8::Handle<v8::Object> obj, | 438 static Handle<JSFunction> GetJSFunction(v8::Handle<v8::Object> obj, |
| 409 const char* property_name) { | 439 const char* property_name) { |
| 410 v8::Local<v8::Function> fun = | 440 v8::Local<v8::Function> fun = |
| 411 v8::Local<v8::Function>::Cast(obj->Get(v8_str(property_name))); | 441 v8::Local<v8::Function>::Cast(obj->Get(v8_str(property_name))); |
| 412 return v8::Utils::OpenHandle(*fun); | 442 return v8::Utils::OpenHandle(*fun); |
| 413 } | 443 } |
| 414 | 444 |
| 415 | 445 |
| 416 static void CheckCodeForUnsafeLiteral(Handle<JSFunction> f) { | 446 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();"); | 479 CompileRun("function f() { a = 12345678 }; f();"); |
| 450 CheckCodeForUnsafeLiteral(GetJSFunction(context->Global(), "f")); | 480 CheckCodeForUnsafeLiteral(GetJSFunction(context->Global(), "f")); |
| 451 CompileRun("function f(x) { a = 12345678 + x}; f(1);"); | 481 CompileRun("function f(x) { a = 12345678 + x}; f(1);"); |
| 452 CheckCodeForUnsafeLiteral(GetJSFunction(context->Global(), "f")); | 482 CheckCodeForUnsafeLiteral(GetJSFunction(context->Global(), "f")); |
| 453 CompileRun("function f(x) { var arguments = 1; x += 12345678}; f(1);"); | 483 CompileRun("function f(x) { var arguments = 1; x += 12345678}; f(1);"); |
| 454 CheckCodeForUnsafeLiteral(GetJSFunction(context->Global(), "f")); | 484 CheckCodeForUnsafeLiteral(GetJSFunction(context->Global(), "f")); |
| 455 CompileRun("function f(x) { var arguments = 1; x = 12345678}; f(1);"); | 485 CompileRun("function f(x) { var arguments = 1; x = 12345678}; f(1);"); |
| 456 CheckCodeForUnsafeLiteral(GetJSFunction(context->Global(), "f")); | 486 CheckCodeForUnsafeLiteral(GetJSFunction(context->Global(), "f")); |
| 457 } | 487 } |
| 458 #endif | 488 #endif |
| OLD | NEW |