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 3414 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3425 CHECK(is_maybe_assigned == expected || | 3425 CHECK(is_maybe_assigned == expected || |
3426 (is_maybe_assigned && inners[j].allow_error_in_inner_function)); | 3426 (is_maybe_assigned && inners[j].allow_error_in_inner_function)); |
3427 } else { | 3427 } else { |
3428 CHECK_EQ(is_maybe_assigned, expected); | 3428 CHECK_EQ(is_maybe_assigned, expected); |
3429 } | 3429 } |
3430 } | 3430 } |
3431 } | 3431 } |
3432 } | 3432 } |
3433 } | 3433 } |
3434 | 3434 |
| 3435 TEST(MaybeAssignedParameters) { |
| 3436 i::Isolate* isolate = CcTest::i_isolate(); |
| 3437 i::HandleScope scope(isolate); |
| 3438 LocalContext env; |
| 3439 |
| 3440 struct { |
| 3441 bool arg_assigned; |
| 3442 const char* source; |
| 3443 } tests[] = { |
| 3444 {false, "function f(arg) {}"}, |
| 3445 {false, "function f(arg) {g(arg)}"}, |
| 3446 {false, "function f(arg) {function h() { g(arg) }; h()}"}, |
| 3447 {false, "function f(arg) {function h() { g(arg) }; return h}"}, |
| 3448 {false, "function f(arg=1) {}"}, |
| 3449 {false, "function f(arg=1) {g(arg)}"}, |
| 3450 {false, "function f(arg, arguments) {g(arg); arguments[0] = 42; g(arg)}"}, |
| 3451 {false, |
| 3452 "function f(arg, ...arguments) {g(arg); arguments[0] = 42; g(arg)}"}, |
| 3453 {false, |
| 3454 "function f(arg, arguments=[]) {g(arg); arguments[0] = 42; g(arg)}"}, |
| 3455 {false, "function f(...arg) {g(arg); arguments[0] = 42; g(arg)}"}, |
| 3456 |
| 3457 // strict arguments object |
| 3458 {false, "function f(arg, x=1) {g(arg); arguments[0] = 42; g(arg)}"}, |
| 3459 {false, "function f(arg, ...x) {g(arg); arguments[0] = 42; g(arg)}"}, |
| 3460 {false, "function f(arg=1) {g(arg); arguments[0] = 42; g(arg)}"}, |
| 3461 {false, |
| 3462 "function f(arg) {'use strict'; g(arg); arguments[0] = 42; g(arg)}"}, |
| 3463 {false, "function f(arg) {g(arg); f.arguments[0] = 42; g(arg)}"}, |
| 3464 {false, "function f(arg, args=arguments) {g(arg); args[0] = 42; g(arg)}"}, |
| 3465 |
| 3466 {true, "function f(arg) {g(arg); arg = 42; g(arg)}"}, |
| 3467 {true, "function f(arg) {g(arg); eval('arg = 42'); g(arg)}"}, |
| 3468 {true, "function f(arg) {g(arg); var arg = 42; g(arg)}"}, |
| 3469 {true, "function f(arg, x=1) {g(arg); arg = 42; g(arg)}"}, |
| 3470 {true, "function f(arg, ...x) {g(arg); arg = 42; g(arg)}"}, |
| 3471 {true, "function f(arg=1) {g(arg); arg = 42; g(arg)}"}, |
| 3472 {true, "function f(arg) {'use strict'; g(arg); arg = 42; g(arg)}"}, |
| 3473 {true, "function f(arg, {a=(g(arg), arg=42)}) {g(arg)}"}, |
| 3474 {true, "function f(arg) {g(arg); g(() => arg = 42); g(arg)}"}, |
| 3475 {true, "function f(arg) {g(arg); g(() => eval('arg = 42')); g(arg)}"}, |
| 3476 {true, "function f(arg) {g(arg); g(() => arguments[0] = 42); g(arg)}"}, |
| 3477 {true, "function f(...arg) {g(arg); eval('arg = 42'); g(arg)}"}, |
| 3478 |
| 3479 // sloppy arguments object |
| 3480 {true, "function f(arg) {g(arg); arguments[0] = 42; g(arg)}"}, |
| 3481 {true, "function f(arg) {g(arg); h(arguments); g(arg)}"}, |
| 3482 {true, |
| 3483 "function f(arg) {((args) => {arguments[0] = 42})(arguments); " |
| 3484 "g(arg)}"}, |
| 3485 {true, "function f(arg) {g(arg); eval('arguments[0] = 42'); g(arg)}"}, |
| 3486 }; |
| 3487 |
| 3488 const char* suffix = "; f"; |
| 3489 |
| 3490 for (unsigned i = 0; i < arraysize(tests); ++i) { |
| 3491 bool assigned = tests[i].arg_assigned; |
| 3492 const char* source = tests[i].source; |
| 3493 for (unsigned allow_lazy = 0; allow_lazy < 2; ++allow_lazy) { |
| 3494 i::ScopedVector<char> program(Utf8LengthHelper(source) + |
| 3495 Utf8LengthHelper(suffix) + 1); |
| 3496 i::SNPrintF(program, "%s%s", source, suffix); |
| 3497 i::Zone zone(isolate->allocator(), ZONE_NAME); |
| 3498 std::unique_ptr<i::ParseInfo> info; |
| 3499 printf("%s\n", program.start()); |
| 3500 v8::Local<v8::Value> v = CompileRun(program.start()); |
| 3501 i::Handle<i::Object> o = v8::Utils::OpenHandle(*v); |
| 3502 i::Handle<i::JSFunction> f = i::Handle<i::JSFunction>::cast(o); |
| 3503 i::Handle<i::SharedFunctionInfo> shared = i::handle(f->shared()); |
| 3504 info = std::unique_ptr<i::ParseInfo>(new i::ParseInfo(&zone, shared)); |
| 3505 info->set_allow_lazy_parsing(allow_lazy); |
| 3506 CHECK(i::parsing::ParseFunction(info.get())); |
| 3507 CHECK(i::Compiler::Analyze(info.get())); |
| 3508 CHECK_NOT_NULL(info->literal()); |
| 3509 |
| 3510 i::Scope* scope = info->literal()->scope(); |
| 3511 CHECK(!scope->AsDeclarationScope()->was_lazily_parsed()); |
| 3512 CHECK_NULL(scope->sibling()); |
| 3513 CHECK(scope->is_function_scope()); |
| 3514 const i::AstRawString* var_name = |
| 3515 info->ast_value_factory()->GetOneByteString("arg"); |
| 3516 i::Variable* var = scope->Lookup(var_name); |
| 3517 CHECK(var->is_used() || !assigned); |
| 3518 bool is_maybe_assigned = var->maybe_assigned() == i::kMaybeAssigned; |
| 3519 CHECK_EQ(is_maybe_assigned, assigned); |
| 3520 } |
| 3521 } |
| 3522 } |
| 3523 |
3435 namespace { | 3524 namespace { |
3436 | 3525 |
3437 i::Scope* DeserializeFunctionScope(i::Isolate* isolate, i::Zone* zone, | 3526 i::Scope* DeserializeFunctionScope(i::Isolate* isolate, i::Zone* zone, |
3438 i::Handle<i::JSObject> m, const char* name) { | 3527 i::Handle<i::JSObject> m, const char* name) { |
3439 i::AstValueFactory avf(zone, isolate->heap()->HashSeed()); | 3528 i::AstValueFactory avf(zone, isolate->heap()->HashSeed()); |
3440 i::Handle<i::JSFunction> f = i::Handle<i::JSFunction>::cast( | 3529 i::Handle<i::JSFunction> f = i::Handle<i::JSFunction>::cast( |
3441 i::JSReceiver::GetProperty(isolate, m, name).ToHandleChecked()); | 3530 i::JSReceiver::GetProperty(isolate, m, name).ToHandleChecked()); |
3442 i::DeclarationScope* script_scope = | 3531 i::DeclarationScope* script_scope = |
3443 new (zone) i::DeclarationScope(zone, &avf); | 3532 new (zone) i::DeclarationScope(zone, &avf); |
3444 i::Scope* s = i::Scope::DeserializeScopeChain( | 3533 i::Scope* s = i::Scope::DeserializeScopeChain( |
(...skipping 5161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
8606 DCHECK_NOT_NULL(scope); | 8695 DCHECK_NOT_NULL(scope); |
8607 DCHECK_NULL(scope->sibling()); | 8696 DCHECK_NULL(scope->sibling()); |
8608 DCHECK(scope->is_function_scope()); | 8697 DCHECK(scope->is_function_scope()); |
8609 const i::AstRawString* var_name = | 8698 const i::AstRawString* var_name = |
8610 info.ast_value_factory()->GetOneByteString("my_var"); | 8699 info.ast_value_factory()->GetOneByteString("my_var"); |
8611 i::Variable* var = scope->Lookup(var_name); | 8700 i::Variable* var = scope->Lookup(var_name); |
8612 CHECK_EQ(inners[i].ctxt_allocate, | 8701 CHECK_EQ(inners[i].ctxt_allocate, |
8613 i::ScopeTestHelper::MustAllocateInContext(var)); | 8702 i::ScopeTestHelper::MustAllocateInContext(var)); |
8614 } | 8703 } |
8615 } | 8704 } |
OLD | NEW |