OLD | NEW |
| (Empty) |
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 | |
4 // met: | |
5 // | |
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. | |
15 // | |
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. | |
27 | |
28 // Adapted from test/mjsunit/compiler/variables.js | |
29 | |
30 #include <limits.h> | |
31 | |
32 #include "v8.h" | |
33 | |
34 #include "api.h" | |
35 #include "isolate.h" | |
36 #include "compilation-cache.h" | |
37 #include "execution.h" | |
38 #include "snapshot.h" | |
39 #include "platform.h" | |
40 #include "utils.h" | |
41 #include "cctest.h" | |
42 #include "parser.h" | |
43 #include "unicode-inl.h" | |
44 | |
45 using ::v8::Context; | |
46 using ::v8::Extension; | |
47 using ::v8::Function; | |
48 using ::v8::FunctionTemplate; | |
49 using ::v8::Handle; | |
50 using ::v8::HandleScope; | |
51 using ::v8::Local; | |
52 using ::v8::Message; | |
53 using ::v8::MessageCallback; | |
54 using ::v8::Object; | |
55 using ::v8::ObjectTemplate; | |
56 using ::v8::Persistent; | |
57 using ::v8::Script; | |
58 using ::v8::StackTrace; | |
59 using ::v8::String; | |
60 using ::v8::TryCatch; | |
61 using ::v8::Undefined; | |
62 using ::v8::V8; | |
63 using ::v8::Value; | |
64 | |
65 static void ExpectInt32(int32_t expected, Local<Value> result) { | |
66 CHECK(result->IsInt32()); | |
67 CHECK_EQ(expected, result->Int32Value()); | |
68 } | |
69 | |
70 | |
71 // Global variables. | |
72 TEST(global_variables) { | |
73 LocalContext env; | |
74 v8::HandleScope scope(env->GetIsolate()); | |
75 Local<Value> result = CompileRun( | |
76 "var x = 0;" | |
77 "function f0() { return x; }" | |
78 "f0();"); | |
79 ExpectInt32(0, result); | |
80 } | |
81 | |
82 | |
83 // Parameters. | |
84 TEST(parameters) { | |
85 LocalContext env; | |
86 v8::HandleScope scope(env->GetIsolate()); | |
87 Local<Value> result = CompileRun( | |
88 "function f1(x) { return x; }" | |
89 "f1(1);"); | |
90 ExpectInt32(1, result); | |
91 } | |
92 | |
93 | |
94 // Stack-allocated locals. | |
95 TEST(stack_allocated_locals) { | |
96 LocalContext env; | |
97 v8::HandleScope scope(env->GetIsolate()); | |
98 Local<Value> result = CompileRun( | |
99 "function f2() { var x = 2; return x; }" | |
100 "f2();"); | |
101 ExpectInt32(2, result); | |
102 } | |
103 | |
104 | |
105 // Context-allocated locals. Local function forces x into f3's context. | |
106 TEST(context_allocated_locals) { | |
107 LocalContext env; | |
108 v8::HandleScope scope(env->GetIsolate()); | |
109 Local<Value> result = CompileRun( | |
110 "function f3(x) {" | |
111 " function g() { return x; }" | |
112 " return x;" | |
113 "}" | |
114 "f3(3);"); | |
115 ExpectInt32(3, result); | |
116 } | |
117 | |
118 | |
119 // Local function reads x from an outer context. | |
120 TEST(read_from_outer_context) { | |
121 LocalContext env; | |
122 v8::HandleScope scope(env->GetIsolate()); | |
123 Local<Value> result = CompileRun( | |
124 "function f4(x) {" | |
125 " function g() { return x; }" | |
126 " return g();" | |
127 "}" | |
128 "f4(4);"); | |
129 ExpectInt32(4, result); | |
130 } | |
131 | |
132 | |
133 // Local function reads x from an outer context. | |
134 TEST(lookup_slots) { | |
135 LocalContext env; | |
136 v8::HandleScope scope(env->GetIsolate()); | |
137 Local<Value> result = CompileRun( | |
138 "function f5(x) {" | |
139 " with ({}) return x;" | |
140 "}" | |
141 "f5(5);"); | |
142 ExpectInt32(5, result); | |
143 } | |
OLD | NEW |