blob: de0976825dd04f048131f40f1f0f5bd867837336 [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// 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);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000026 v8::Local<v8::Context> current_context = isolate->GetCurrentContext();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000027
28 v8::Local<v8::FunctionTemplate> t0 = v8::FunctionTemplate::New(isolate);
29 v8::Local<v8::FunctionTemplate> t1 = v8::FunctionTemplate::New(isolate);
30
31 t1->SetHiddenPrototype(true);
32
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000033 v8::Local<v8::Object> object = t0->GetFunction(current_context)
34 .ToLocalChecked()
35 ->NewInstance(current_context)
36 .ToLocalChecked();
37 v8::Local<v8::Object> hidden_prototype = t1->GetFunction(current_context)
38 .ToLocalChecked()
39 ->NewInstance(current_context)
40 .ToLocalChecked();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000041
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000042 CHECK(object->SetPrototype(current_context, hidden_prototype).FromJust());
Ben Murdochb8a8cc12014-11-26 15:28:44 +000043
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000044 context->Global()
45 ->Set(current_context, v8_str("object"), object)
46 .FromMaybe(false);
47 context->Global()
48 ->Set(current_context, v8_str("hidden_prototype"), hidden_prototype)
49 .FromMaybe(false);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000050
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000051 CHECK_EQ(1, CompileRun("var result;"
52 "var x = 0;"
53 "object.x = 1;"
54 "with (object) {"
55 " result = x;"
56 "}"
57 "result")
58 ->Int32Value(current_context)
59 .FromJust());
Ben Murdochb8a8cc12014-11-26 15:28:44 +000060
61 Cleanup();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000062 CHECK_EQ(2, CompileRun("var result;"
63 "var x = 0;"
64 "hidden_prototype.x = 2;"
65 "with (object) {"
66 " result = x;"
67 "}"
68 "result")
69 ->Int32Value(current_context)
70 .FromJust());
Ben Murdochb8a8cc12014-11-26 15:28:44 +000071
72 Cleanup();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000073 CHECK_EQ(0, CompileRun("var result;"
74 "var x = 0;"
75 "object.x = 3;"
76 "object[Symbol.unscopables] = {x: true};"
77 "with (object) {"
78 " result = x;"
79 "}"
80 "result")
81 ->Int32Value(current_context)
82 .FromJust());
Ben Murdochb8a8cc12014-11-26 15:28:44 +000083
84 Cleanup();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000085 CHECK_EQ(0, CompileRun("var result;"
86 "var x = 0;"
87 "hidden_prototype.x = 4;"
88 "hidden_prototype[Symbol.unscopables] = {x: true};"
89 "with (object) {"
90 " result = x;"
91 "}"
92 "result")
93 ->Int32Value(current_context)
94 .FromJust());
Ben Murdochb8a8cc12014-11-26 15:28:44 +000095
96 Cleanup();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000097 CHECK_EQ(0, CompileRun("var result;"
98 "var x = 0;"
99 "object.x = 5;"
100 "hidden_prototype[Symbol.unscopables] = {x: true};"
101 "with (object) {"
102 " result = x;"
103 "}"
104 "result;")
105 ->Int32Value(current_context)
106 .FromJust());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000107
108 Cleanup();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000109 CHECK_EQ(0, CompileRun("var result;"
110 "var x = 0;"
111 "hidden_prototype.x = 6;"
112 "object[Symbol.unscopables] = {x: true};"
113 "with (object) {"
114 " result = x;"
115 "}"
116 "result")
117 ->Int32Value(current_context)
118 .FromJust());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000119}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000120
121} // namespace