[presubmit] Enable readability/namespace linter checking.
[platform/upstream/v8.git] / test / cctest / test-unscopables-hidden-prototype.cc
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 // TODO(mythria): Remove this after this flag is turned on globally
6 #define V8_IMMINENT_DEPRECATION_WARNINGS
7
8 #include <stdlib.h>
9
10 #include "src/v8.h"
11 #include "test/cctest/cctest.h"
12
13 namespace {
14
15
16 static void Cleanup() {
17   CompileRun(
18       "delete object.x;"
19       "delete hidden_prototype.x;"
20       "delete object[Symbol.unscopables];"
21       "delete hidden_prototype[Symbol.unscopables];");
22 }
23
24
25 TEST(Unscopables) {
26   LocalContext context;
27   v8::Isolate* isolate = context->GetIsolate();
28   v8::HandleScope handle_scope(isolate);
29   v8::Local<v8::Context> current_context = isolate->GetCurrentContext();
30
31   v8::Local<v8::FunctionTemplate> t0 = v8::FunctionTemplate::New(isolate);
32   v8::Local<v8::FunctionTemplate> t1 = v8::FunctionTemplate::New(isolate);
33
34   t1->SetHiddenPrototype(true);
35
36   v8::Local<v8::Object> object = t0->GetFunction(current_context)
37                                      .ToLocalChecked()
38                                      ->NewInstance(current_context)
39                                      .ToLocalChecked();
40   v8::Local<v8::Object> hidden_prototype = t1->GetFunction(current_context)
41                                                .ToLocalChecked()
42                                                ->NewInstance(current_context)
43                                                .ToLocalChecked();
44
45   CHECK(object->SetPrototype(current_context, hidden_prototype).FromJust());
46
47   context->Global()
48       ->Set(current_context, v8_str("object"), object)
49       .FromMaybe(false);
50   context->Global()
51       ->Set(current_context, v8_str("hidden_prototype"), hidden_prototype)
52       .FromMaybe(false);
53
54   CHECK_EQ(1, CompileRun("var result;"
55                          "var x = 0;"
56                          "object.x = 1;"
57                          "with (object) {"
58                          "  result = x;"
59                          "}"
60                          "result")
61                   ->Int32Value(current_context)
62                   .FromJust());
63
64   Cleanup();
65   CHECK_EQ(2, CompileRun("var result;"
66                          "var x = 0;"
67                          "hidden_prototype.x = 2;"
68                          "with (object) {"
69                          "  result = x;"
70                          "}"
71                          "result")
72                   ->Int32Value(current_context)
73                   .FromJust());
74
75   Cleanup();
76   CHECK_EQ(0, CompileRun("var result;"
77                          "var x = 0;"
78                          "object.x = 3;"
79                          "object[Symbol.unscopables] = {x: true};"
80                          "with (object) {"
81                          "  result = x;"
82                          "}"
83                          "result")
84                   ->Int32Value(current_context)
85                   .FromJust());
86
87   Cleanup();
88   CHECK_EQ(0, CompileRun("var result;"
89                          "var x = 0;"
90                          "hidden_prototype.x = 4;"
91                          "hidden_prototype[Symbol.unscopables] = {x: true};"
92                          "with (object) {"
93                          "  result = x;"
94                          "}"
95                          "result")
96                   ->Int32Value(current_context)
97                   .FromJust());
98
99   Cleanup();
100   CHECK_EQ(0, CompileRun("var result;"
101                          "var x = 0;"
102                          "object.x = 5;"
103                          "hidden_prototype[Symbol.unscopables] = {x: true};"
104                          "with (object) {"
105                          "  result = x;"
106                          "}"
107                          "result;")
108                   ->Int32Value(current_context)
109                   .FromJust());
110
111   Cleanup();
112   CHECK_EQ(0, CompileRun("var result;"
113                          "var x = 0;"
114                          "hidden_prototype.x = 6;"
115                          "object[Symbol.unscopables] = {x: true};"
116                          "with (object) {"
117                          "  result = x;"
118                          "}"
119                          "result")
120                   ->Int32Value(current_context)
121                   .FromJust());
122 }
123
124 }  // namespace