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/snapshot.h"
41 #include "src/unicode-inl.h"
42 #include "src/utils.h"
43 #include "test/cctest/cctest.h"
46 using ::v8::Extension;
48 using ::v8::FunctionTemplate;
50 using ::v8::HandleScope;
53 using ::v8::MessageCallback;
55 using ::v8::ObjectTemplate;
56 using ::v8::Persistent;
58 using ::v8::StackTrace;
61 using ::v8::Undefined;
65 static void ExpectInt32(int32_t expected, Local<Value> result) {
66 CHECK(result->IsInt32());
67 CHECK_EQ(expected, result->Int32Value());
72 TEST(global_variables) {
74 v8::HandleScope scope(env->GetIsolate());
75 Local<Value> result = CompileRun(
77 "function f0() { return x; }"
79 ExpectInt32(0, result);
86 v8::HandleScope scope(env->GetIsolate());
87 Local<Value> result = CompileRun(
88 "function f1(x) { return x; }"
90 ExpectInt32(1, result);
94 // Stack-allocated locals.
95 TEST(stack_allocated_locals) {
97 v8::HandleScope scope(env->GetIsolate());
98 Local<Value> result = CompileRun(
99 "function f2() { var x = 2; return x; }"
101 ExpectInt32(2, result);
105 // Context-allocated locals. Local function forces x into f3's context.
106 TEST(context_allocated_locals) {
108 v8::HandleScope scope(env->GetIsolate());
109 Local<Value> result = CompileRun(
111 " function g() { return x; }"
115 ExpectInt32(3, result);
119 // Local function reads x from an outer context.
120 TEST(read_from_outer_context) {
122 v8::HandleScope scope(env->GetIsolate());
123 Local<Value> result = CompileRun(
125 " function g() { return x; }"
129 ExpectInt32(4, result);
133 // Local function reads x from an outer context.
136 v8::HandleScope scope(env->GetIsolate());
137 Local<Value> result = CompileRun(
139 " with ({}) return x;"
142 ExpectInt32(5, result);