1 // Copyright 2010 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 // Adapted from test/mjsunit/compiler/variables.js
35 #include "src/base/platform/platform.h"
36 #include "src/compilation-cache.h"
37 #include "src/execution.h"
38 #include "src/isolate.h"
39 #include "src/parser.h"
40 #include "src/unicode-inl.h"
41 #include "src/utils.h"
42 #include "test/cctest/cctest.h"
45 using ::v8::Extension;
47 using ::v8::FunctionTemplate;
49 using ::v8::HandleScope;
52 using ::v8::MessageCallback;
54 using ::v8::ObjectTemplate;
55 using ::v8::Persistent;
57 using ::v8::StackTrace;
60 using ::v8::Undefined;
64 static void ExpectInt32(int32_t expected, Local<Value> result) {
65 CHECK(result->IsInt32());
66 CHECK_EQ(expected, result->Int32Value());
71 TEST(global_variables) {
73 v8::HandleScope scope(env->GetIsolate());
74 Local<Value> result = CompileRun(
76 "function f0() { return x; }"
78 ExpectInt32(0, result);
85 v8::HandleScope scope(env->GetIsolate());
86 Local<Value> result = CompileRun(
87 "function f1(x) { return x; }"
89 ExpectInt32(1, result);
93 // Stack-allocated locals.
94 TEST(stack_allocated_locals) {
96 v8::HandleScope scope(env->GetIsolate());
97 Local<Value> result = CompileRun(
98 "function f2() { var x = 2; return x; }"
100 ExpectInt32(2, result);
104 // Context-allocated locals. Local function forces x into f3's context.
105 TEST(context_allocated_locals) {
107 v8::HandleScope scope(env->GetIsolate());
108 Local<Value> result = CompileRun(
110 " function g() { return x; }"
114 ExpectInt32(3, result);
118 // Local function reads x from an outer context.
119 TEST(read_from_outer_context) {
121 v8::HandleScope scope(env->GetIsolate());
122 Local<Value> result = CompileRun(
124 " function g() { return x; }"
128 ExpectInt32(4, result);
132 // Local function reads x from an outer context.
135 v8::HandleScope scope(env->GetIsolate());
136 Local<Value> result = CompileRun(
138 " with ({}) return x;"
141 ExpectInt32(5, result);