1// Copyright 2014 the V8 project authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5#include <stdlib.h> 6 7#include "src/v8.h" 8#include "test/cctest/cctest.h" 9 10namespace { 11 12 13static void Cleanup() { 14 CompileRun( 15 "delete object.x;" 16 "delete hidden_prototype.x;" 17 "delete object[Symbol.unscopables];" 18 "delete hidden_prototype[Symbol.unscopables];"); 19} 20 21 22TEST(Unscopables) { 23 LocalContext context; 24 v8::Isolate* isolate = context->GetIsolate(); 25 v8::HandleScope handle_scope(isolate); 26 27 v8::Local<v8::FunctionTemplate> t0 = v8::FunctionTemplate::New(isolate); 28 v8::Local<v8::FunctionTemplate> t1 = v8::FunctionTemplate::New(isolate); 29 30 t1->SetHiddenPrototype(true); 31 32 v8::Local<v8::Object> object = t0->GetFunction()->NewInstance(); 33 v8::Local<v8::Object> hidden_prototype = t1->GetFunction()->NewInstance(); 34 35 object->SetPrototype(hidden_prototype); 36 37 context->Global()->Set(v8_str("object"), object); 38 context->Global()->Set(v8_str("hidden_prototype"), hidden_prototype); 39 40 CHECK_EQ(1, CompileRun( 41 "var result;" 42 "var x = 0;" 43 "object.x = 1;" 44 "with (object) {" 45 " result = x;" 46 "}" 47 "result")->Int32Value()); 48 49 Cleanup(); 50 CHECK_EQ(2, CompileRun( 51 "var result;" 52 "var x = 0;" 53 "hidden_prototype.x = 2;" 54 "with (object) {" 55 " result = x;" 56 "}" 57 "result")->Int32Value()); 58 59 Cleanup(); 60 CHECK_EQ(0, CompileRun( 61 "var result;" 62 "var x = 0;" 63 "object.x = 3;" 64 "object[Symbol.unscopables] = {x: true};" 65 "with (object) {" 66 " result = x;" 67 "}" 68 "result")->Int32Value()); 69 70 Cleanup(); 71 CHECK_EQ(0, CompileRun( 72 "var result;" 73 "var x = 0;" 74 "hidden_prototype.x = 4;" 75 "hidden_prototype[Symbol.unscopables] = {x: true};" 76 "with (object) {" 77 " result = x;" 78 "}" 79 "result")->Int32Value()); 80 81 Cleanup(); 82 CHECK_EQ(0, CompileRun( 83 "var result;" 84 "var x = 0;" 85 "object.x = 5;" 86 "hidden_prototype[Symbol.unscopables] = {x: true};" 87 "with (object) {" 88 " result = x;" 89 "}" 90 "result;")->Int32Value()); 91 92 Cleanup(); 93 CHECK_EQ(0, CompileRun( 94 "var result;" 95 "var x = 0;" 96 "hidden_prototype.x = 6;" 97 "object[Symbol.unscopables] = {x: true};" 98 "with (object) {" 99 " result = x;" 100 "}" 101 "result")->Int32Value()); 102} 103} 104