Line data Source code
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 :
10 : namespace {
11 :
12 :
13 25 : static 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 25 : }
20 :
21 :
22 26644 : TEST(Unscopables) {
23 5 : LocalContext context;
24 5 : v8::Isolate* isolate = context->GetIsolate();
25 10 : v8::HandleScope handle_scope(isolate);
26 5 : v8::Local<v8::Context> current_context = isolate->GetCurrentContext();
27 :
28 5 : v8::Local<v8::FunctionTemplate> t0 = v8::FunctionTemplate::New(isolate);
29 5 : v8::Local<v8::FunctionTemplate> t1 = v8::FunctionTemplate::New(isolate);
30 :
31 5 : v8::Local<v8::Object> object = t0->GetFunction(current_context)
32 : .ToLocalChecked()
33 : ->NewInstance(current_context)
34 : .ToLocalChecked();
35 5 : v8::Local<v8::Object> hidden_prototype = t1->GetFunction(current_context)
36 : .ToLocalChecked()
37 : ->NewInstance(current_context)
38 : .ToLocalChecked();
39 :
40 10 : CHECK(object->SetPrototype(current_context, hidden_prototype).FromJust());
41 :
42 10 : context->Global()
43 15 : ->Set(current_context, v8_str("object"), object)
44 : .FromMaybe(false);
45 10 : context->Global()
46 15 : ->Set(current_context, v8_str("hidden_prototype"), hidden_prototype)
47 : .FromMaybe(false);
48 :
49 10 : CHECK_EQ(1, CompileRun("var result;"
50 : "var x = 0;"
51 : "object.x = 1;"
52 : "with (object) {"
53 : " result = x;"
54 : "}"
55 : "result")
56 : ->Int32Value(current_context)
57 : .FromJust());
58 :
59 5 : Cleanup();
60 10 : CHECK_EQ(2, CompileRun("var result;"
61 : "var x = 0;"
62 : "hidden_prototype.x = 2;"
63 : "with (object) {"
64 : " result = x;"
65 : "}"
66 : "result")
67 : ->Int32Value(current_context)
68 : .FromJust());
69 :
70 5 : Cleanup();
71 10 : CHECK_EQ(0, CompileRun("var result;"
72 : "var x = 0;"
73 : "object.x = 3;"
74 : "object[Symbol.unscopables] = {x: true};"
75 : "with (object) {"
76 : " result = x;"
77 : "}"
78 : "result")
79 : ->Int32Value(current_context)
80 : .FromJust());
81 :
82 5 : Cleanup();
83 10 : CHECK_EQ(0, CompileRun("var result;"
84 : "var x = 0;"
85 : "hidden_prototype.x = 4;"
86 : "hidden_prototype[Symbol.unscopables] = {x: true};"
87 : "with (object) {"
88 : " result = x;"
89 : "}"
90 : "result")
91 : ->Int32Value(current_context)
92 : .FromJust());
93 :
94 5 : Cleanup();
95 10 : CHECK_EQ(0, CompileRun("var result;"
96 : "var x = 0;"
97 : "object.x = 5;"
98 : "hidden_prototype[Symbol.unscopables] = {x: true};"
99 : "with (object) {"
100 : " result = x;"
101 : "}"
102 : "result;")
103 : ->Int32Value(current_context)
104 : .FromJust());
105 :
106 5 : Cleanup();
107 10 : CHECK_EQ(0, CompileRun("var result;"
108 : "var x = 0;"
109 : "hidden_prototype.x = 6;"
110 : "object[Symbol.unscopables] = {x: true};"
111 : "with (object) {"
112 : " result = x;"
113 : "}"
114 : "result")
115 : ->Int32Value(current_context)
116 : .FromJust());
117 5 : }
118 :
119 79917 : } // namespace
|