OLD | NEW |
1 // Copyright 2013 the V8 project authors. All rights reserved. | 1 // Copyright 2013 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
11 // with the distribution. | 11 // with the distribution. |
12 // * Neither the name of Google Inc. nor the names of its | 12 // * Neither the name of Google Inc. nor the names of its |
13 // contributors may be used to endorse or promote products derived | 13 // contributors may be used to endorse or promote products derived |
14 // from this software without specific prior written permission. | 14 // from this software without specific prior written permission. |
15 // | 15 // |
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | 27 |
| 28 // TODO(jochen): Remove this after the setting is turned on globally. |
| 29 #define V8_IMMINENT_DEPRECATION_WARNINGS |
| 30 |
28 #include <limits.h> | 31 #include <limits.h> |
29 | 32 |
30 #include "src/v8.h" | 33 #include "src/v8.h" |
31 | 34 |
32 #include "src/api.h" | 35 #include "src/api.h" |
33 #include "src/base/platform/platform.h" | 36 #include "src/base/platform/platform.h" |
34 #include "src/compilation-cache.h" | 37 #include "src/compilation-cache.h" |
35 #include "src/execution.h" | 38 #include "src/execution.h" |
36 #include "src/isolate.h" | 39 #include "src/isolate.h" |
37 #include "src/parser.h" | 40 #include "src/parser.h" |
38 #include "src/unicode-inl.h" | 41 #include "src/unicode-inl.h" |
39 #include "src/utils.h" | 42 #include "src/utils.h" |
40 #include "test/cctest/cctest.h" | 43 #include "test/cctest/cctest.h" |
41 | 44 |
42 using ::v8::Context; | 45 using ::v8::Context; |
43 using ::v8::Extension; | 46 using ::v8::Extension; |
44 using ::v8::Function; | 47 using ::v8::Function; |
45 using ::v8::FunctionTemplate; | 48 using ::v8::FunctionTemplate; |
46 using ::v8::Handle; | |
47 using ::v8::HandleScope; | 49 using ::v8::HandleScope; |
48 using ::v8::Local; | 50 using ::v8::Local; |
49 using ::v8::Message; | 51 using ::v8::Message; |
50 using ::v8::MessageCallback; | 52 using ::v8::MessageCallback; |
51 using ::v8::Object; | 53 using ::v8::Object; |
52 using ::v8::ObjectTemplate; | 54 using ::v8::ObjectTemplate; |
53 using ::v8::Persistent; | 55 using ::v8::Persistent; |
54 using ::v8::Script; | 56 using ::v8::Script; |
55 using ::v8::StackTrace; | 57 using ::v8::StackTrace; |
56 using ::v8::String; | 58 using ::v8::String; |
57 using ::v8::TryCatch; | 59 using ::v8::TryCatch; |
58 using ::v8::Undefined; | 60 using ::v8::Undefined; |
59 using ::v8::V8; | 61 using ::v8::V8; |
60 using ::v8::Value; | 62 using ::v8::Value; |
61 | 63 |
62 static void ExpectBoolean(bool expected, Local<Value> result) { | 64 static void ExpectBoolean(Local<Context> context, bool expected, |
| 65 Local<Value> result) { |
63 CHECK(result->IsBoolean()); | 66 CHECK(result->IsBoolean()); |
64 CHECK_EQ(expected, result->BooleanValue()); | 67 CHECK_EQ(expected, result->BooleanValue(context).FromJust()); |
65 } | 68 } |
66 | 69 |
67 | 70 |
68 static void ExpectInt32(int32_t expected, Local<Value> result) { | 71 static void ExpectInt32(Local<Context> context, int32_t expected, |
| 72 Local<Value> result) { |
69 CHECK(result->IsInt32()); | 73 CHECK(result->IsInt32()); |
70 CHECK_EQ(expected, result->Int32Value()); | 74 CHECK_EQ(expected, result->Int32Value(context).FromJust()); |
71 } | 75 } |
72 | 76 |
73 | 77 |
74 static void ExpectNumber(double expected, Local<Value> result) { | 78 static void ExpectNumber(Local<Context> context, double expected, |
| 79 Local<Value> result) { |
75 CHECK(result->IsNumber()); | 80 CHECK(result->IsNumber()); |
76 CHECK_EQ(expected, result->NumberValue()); | 81 CHECK_EQ(expected, result->NumberValue(context).FromJust()); |
77 } | 82 } |
78 | 83 |
79 | 84 |
80 static void ExpectUndefined(Local<Value> result) { | 85 static void ExpectUndefined(Local<Value> result) { |
81 CHECK(result->IsUndefined()); | 86 CHECK(result->IsUndefined()); |
82 } | 87 } |
83 | 88 |
84 | 89 |
85 // Tests are sorted by order of implementation. | 90 // Tests are sorted by order of implementation. |
86 | 91 |
87 TEST(simple_value) { | 92 TEST(simple_value) { |
88 LocalContext env; | 93 LocalContext env; |
89 v8::HandleScope scope(env->GetIsolate()); | 94 v8::HandleScope scope(env->GetIsolate()); |
90 Local<Value> result = CompileRun("0x271828;"); | 95 Local<Value> result = CompileRun("0x271828;"); |
91 ExpectInt32(0x271828, result); | 96 ExpectInt32(env.local(), 0x271828, result); |
92 } | 97 } |
93 | 98 |
94 | 99 |
95 TEST(global_variable) { | 100 TEST(global_variable) { |
96 LocalContext env; | 101 LocalContext env; |
97 v8::HandleScope scope(env->GetIsolate()); | 102 v8::HandleScope scope(env->GetIsolate()); |
98 Local<Value> result = CompileRun("var my_global_var = 0x123; my_global_var;"); | 103 Local<Value> result = CompileRun("var my_global_var = 0x123; my_global_var;"); |
99 ExpectInt32(0x123, result); | 104 ExpectInt32(env.local(), 0x123, result); |
100 } | 105 } |
101 | 106 |
102 | 107 |
103 TEST(simple_function_call) { | 108 TEST(simple_function_call) { |
104 LocalContext env; | 109 LocalContext env; |
105 v8::HandleScope scope(env->GetIsolate()); | 110 v8::HandleScope scope(env->GetIsolate()); |
106 Local<Value> result = CompileRun( | 111 Local<Value> result = CompileRun( |
107 "function foo() { return 0x314; }" | 112 "function foo() { return 0x314; }" |
108 "foo();"); | 113 "foo();"); |
109 ExpectInt32(0x314, result); | 114 ExpectInt32(env.local(), 0x314, result); |
110 } | 115 } |
111 | 116 |
112 | 117 |
113 TEST(binary_op) { | 118 TEST(binary_op) { |
114 LocalContext env; | 119 LocalContext env; |
115 v8::HandleScope scope(env->GetIsolate()); | 120 v8::HandleScope scope(env->GetIsolate()); |
116 Local<Value> result = CompileRun( | 121 Local<Value> result = CompileRun( |
117 "function foo() {" | 122 "function foo() {" |
118 " var a = 0x1200;" | 123 " var a = 0x1200;" |
119 " var b = 0x0035;" | 124 " var b = 0x0035;" |
120 " return 2 * (a + b - 1);" | 125 " return 2 * (a + b - 1);" |
121 "}" | 126 "}" |
122 "foo();"); | 127 "foo();"); |
123 ExpectInt32(0x2468, result); | 128 ExpectInt32(env.local(), 0x2468, result); |
124 } | 129 } |
125 | 130 |
126 static void if_comparison_testcontext_helper( | 131 static void if_comparison_testcontext_helper(Local<Context> context, |
127 char const * op, | 132 char const* op, char const* lhs, |
128 char const * lhs, | 133 char const* rhs, int expect) { |
129 char const * rhs, | |
130 int expect) { | |
131 char buffer[256]; | 134 char buffer[256]; |
132 snprintf(buffer, sizeof(buffer), | 135 snprintf(buffer, sizeof(buffer), |
133 "var lhs = %s;" | 136 "var lhs = %s;" |
134 "var rhs = %s;" | 137 "var rhs = %s;" |
135 "if ( lhs %s rhs ) { 1; }" | 138 "if ( lhs %s rhs ) { 1; }" |
136 "else { 0; }", | 139 "else { 0; }", |
137 lhs, rhs, op); | 140 lhs, rhs, op); |
138 Local<Value> result = CompileRun(buffer); | 141 Local<Value> result = CompileRun(buffer); |
139 ExpectInt32(expect, result); | 142 ExpectInt32(context, expect, result); |
140 } | 143 } |
141 | 144 |
142 static void if_comparison_effectcontext_helper( | 145 static void if_comparison_effectcontext_helper(Local<Context> context, |
143 char const * op, | 146 char const* op, char const* lhs, |
144 char const * lhs, | 147 char const* rhs, int expect) { |
145 char const * rhs, | |
146 int expect) { | |
147 char buffer[256]; | 148 char buffer[256]; |
148 snprintf(buffer, sizeof(buffer), | 149 snprintf(buffer, sizeof(buffer), |
149 "var lhs = %s;" | 150 "var lhs = %s;" |
150 "var rhs = %s;" | 151 "var rhs = %s;" |
151 "var test = lhs %s rhs;" | 152 "var test = lhs %s rhs;" |
152 "if ( test ) { 1; }" | 153 "if ( test ) { 1; }" |
153 "else { 0; }", | 154 "else { 0; }", |
154 lhs, rhs, op); | 155 lhs, rhs, op); |
155 Local<Value> result = CompileRun(buffer); | 156 Local<Value> result = CompileRun(buffer); |
156 ExpectInt32(expect, result); | 157 ExpectInt32(context, expect, result); |
157 } | 158 } |
158 | 159 |
159 static void if_comparison_helper( | 160 static void if_comparison_helper(Local<Context> context, char const* op, |
160 char const * op, | 161 int expect_when_lt, int expect_when_eq, |
161 int expect_when_lt, | 162 int expect_when_gt) { |
162 int expect_when_eq, | |
163 int expect_when_gt) { | |
164 // TODO(all): Non-SMI tests. | 163 // TODO(all): Non-SMI tests. |
165 | 164 |
166 if_comparison_testcontext_helper(op, "1", "3", expect_when_lt); | 165 if_comparison_testcontext_helper(context, op, "1", "3", expect_when_lt); |
167 if_comparison_testcontext_helper(op, "5", "5", expect_when_eq); | 166 if_comparison_testcontext_helper(context, op, "5", "5", expect_when_eq); |
168 if_comparison_testcontext_helper(op, "9", "7", expect_when_gt); | 167 if_comparison_testcontext_helper(context, op, "9", "7", expect_when_gt); |
169 | 168 |
170 if_comparison_effectcontext_helper(op, "1", "3", expect_when_lt); | 169 if_comparison_effectcontext_helper(context, op, "1", "3", expect_when_lt); |
171 if_comparison_effectcontext_helper(op, "5", "5", expect_when_eq); | 170 if_comparison_effectcontext_helper(context, op, "5", "5", expect_when_eq); |
172 if_comparison_effectcontext_helper(op, "9", "7", expect_when_gt); | 171 if_comparison_effectcontext_helper(context, op, "9", "7", expect_when_gt); |
173 } | 172 } |
174 | 173 |
175 | 174 |
176 TEST(if_comparison) { | 175 TEST(if_comparison) { |
177 LocalContext env; | 176 LocalContext env; |
178 v8::HandleScope scope(env->GetIsolate()); | 177 v8::HandleScope scope(env->GetIsolate()); |
179 | 178 |
180 if_comparison_helper("<", 1, 0, 0); | 179 if_comparison_helper(env.local(), "<", 1, 0, 0); |
181 if_comparison_helper("<=", 1, 1, 0); | 180 if_comparison_helper(env.local(), "<=", 1, 1, 0); |
182 if_comparison_helper("==", 0, 1, 0); | 181 if_comparison_helper(env.local(), "==", 0, 1, 0); |
183 if_comparison_helper("===", 0, 1, 0); | 182 if_comparison_helper(env.local(), "===", 0, 1, 0); |
184 if_comparison_helper(">=", 0, 1, 1); | 183 if_comparison_helper(env.local(), ">=", 0, 1, 1); |
185 if_comparison_helper(">", 0, 0, 1); | 184 if_comparison_helper(env.local(), ">", 0, 0, 1); |
186 if_comparison_helper("!=", 1, 0, 1); | 185 if_comparison_helper(env.local(), "!=", 1, 0, 1); |
187 if_comparison_helper("!==", 1, 0, 1); | 186 if_comparison_helper(env.local(), "!==", 1, 0, 1); |
188 } | 187 } |
189 | 188 |
190 | 189 |
191 TEST(unary_plus) { | 190 TEST(unary_plus) { |
192 LocalContext env; | 191 LocalContext env; |
193 v8::HandleScope scope(env->GetIsolate()); | 192 v8::HandleScope scope(env->GetIsolate()); |
194 Local<Value> result; | 193 Local<Value> result; |
195 // SMI | 194 // SMI |
196 result = CompileRun("var a = 1234; +a"); | 195 result = CompileRun("var a = 1234; +a"); |
197 ExpectInt32(1234, result); | 196 ExpectInt32(env.local(), 1234, result); |
198 // Number | 197 // Number |
199 result = CompileRun("var a = 1234.5; +a"); | 198 result = CompileRun("var a = 1234.5; +a"); |
200 ExpectNumber(1234.5, result); | 199 ExpectNumber(env.local(), 1234.5, result); |
201 // String (SMI) | 200 // String (SMI) |
202 result = CompileRun("var a = '1234'; +a"); | 201 result = CompileRun("var a = '1234'; +a"); |
203 ExpectInt32(1234, result); | 202 ExpectInt32(env.local(), 1234, result); |
204 // String (Number) | 203 // String (Number) |
205 result = CompileRun("var a = '1234.5'; +a"); | 204 result = CompileRun("var a = '1234.5'; +a"); |
206 ExpectNumber(1234.5, result); | 205 ExpectNumber(env.local(), 1234.5, result); |
207 // Check side effects. | 206 // Check side effects. |
208 result = CompileRun("var a = 1234; +(a = 4321); a"); | 207 result = CompileRun("var a = 1234; +(a = 4321); a"); |
209 ExpectInt32(4321, result); | 208 ExpectInt32(env.local(), 4321, result); |
210 } | 209 } |
211 | 210 |
212 | 211 |
213 TEST(unary_minus) { | 212 TEST(unary_minus) { |
214 LocalContext env; | 213 LocalContext env; |
215 v8::HandleScope scope(env->GetIsolate()); | 214 v8::HandleScope scope(env->GetIsolate()); |
216 Local<Value> result; | 215 Local<Value> result; |
217 result = CompileRun("var a = 1234; -a"); | 216 result = CompileRun("var a = 1234; -a"); |
218 ExpectInt32(-1234, result); | 217 ExpectInt32(env.local(), -1234, result); |
219 result = CompileRun("var a = 1234.5; -a"); | 218 result = CompileRun("var a = 1234.5; -a"); |
220 ExpectNumber(-1234.5, result); | 219 ExpectNumber(env.local(), -1234.5, result); |
221 result = CompileRun("var a = 1234; -(a = 4321); a"); | 220 result = CompileRun("var a = 1234; -(a = 4321); a"); |
222 ExpectInt32(4321, result); | 221 ExpectInt32(env.local(), 4321, result); |
223 result = CompileRun("var a = '1234'; -a"); | 222 result = CompileRun("var a = '1234'; -a"); |
224 ExpectInt32(-1234, result); | 223 ExpectInt32(env.local(), -1234, result); |
225 result = CompileRun("var a = '1234.5'; -a"); | 224 result = CompileRun("var a = '1234.5'; -a"); |
226 ExpectNumber(-1234.5, result); | 225 ExpectNumber(env.local(), -1234.5, result); |
227 } | 226 } |
228 | 227 |
229 | 228 |
230 TEST(unary_void) { | 229 TEST(unary_void) { |
231 LocalContext env; | 230 LocalContext env; |
232 v8::HandleScope scope(env->GetIsolate()); | 231 v8::HandleScope scope(env->GetIsolate()); |
233 Local<Value> result; | 232 Local<Value> result; |
234 result = CompileRun("var a = 1234; void (a);"); | 233 result = CompileRun("var a = 1234; void (a);"); |
235 ExpectUndefined(result); | 234 ExpectUndefined(result); |
236 result = CompileRun("var a = 0; void (a = 42); a"); | 235 result = CompileRun("var a = 0; void (a = 42); a"); |
237 ExpectInt32(42, result); | 236 ExpectInt32(env.local(), 42, result); |
238 result = CompileRun("var a = 0; void (a = 42);"); | 237 result = CompileRun("var a = 0; void (a = 42);"); |
239 ExpectUndefined(result); | 238 ExpectUndefined(result); |
240 } | 239 } |
241 | 240 |
242 | 241 |
243 TEST(unary_not) { | 242 TEST(unary_not) { |
244 LocalContext env; | 243 LocalContext env; |
245 v8::HandleScope scope(env->GetIsolate()); | 244 v8::HandleScope scope(env->GetIsolate()); |
246 Local<Value> result; | 245 Local<Value> result; |
247 result = CompileRun("var a = 1234; !a"); | 246 result = CompileRun("var a = 1234; !a"); |
248 ExpectBoolean(false, result); | 247 ExpectBoolean(env.local(), false, result); |
249 result = CompileRun("var a = 0; !a"); | 248 result = CompileRun("var a = 0; !a"); |
250 ExpectBoolean(true, result); | 249 ExpectBoolean(env.local(), true, result); |
251 result = CompileRun("var a = 0; !(a = 1234); a"); | 250 result = CompileRun("var a = 0; !(a = 1234); a"); |
252 ExpectInt32(1234, result); | 251 ExpectInt32(env.local(), 1234, result); |
253 result = CompileRun("var a = '1234'; !a"); | 252 result = CompileRun("var a = '1234'; !a"); |
254 ExpectBoolean(false, result); | 253 ExpectBoolean(env.local(), false, result); |
255 result = CompileRun("var a = ''; !a"); | 254 result = CompileRun("var a = ''; !a"); |
256 ExpectBoolean(true, result); | 255 ExpectBoolean(env.local(), true, result); |
257 result = CompileRun("var a = 1234; !!a"); | 256 result = CompileRun("var a = 1234; !!a"); |
258 ExpectBoolean(true, result); | 257 ExpectBoolean(env.local(), true, result); |
259 result = CompileRun("var a = 0; !!a"); | 258 result = CompileRun("var a = 0; !!a"); |
260 ExpectBoolean(false, result); | 259 ExpectBoolean(env.local(), false, result); |
261 result = CompileRun("var a = 0; if ( !a ) { 1; } else { 0; }"); | 260 result = CompileRun("var a = 0; if ( !a ) { 1; } else { 0; }"); |
262 ExpectInt32(1, result); | 261 ExpectInt32(env.local(), 1, result); |
263 result = CompileRun("var a = 1; if ( !a ) { 1; } else { 0; }"); | 262 result = CompileRun("var a = 1; if ( !a ) { 1; } else { 0; }"); |
264 ExpectInt32(0, result); | 263 ExpectInt32(env.local(), 0, result); |
265 } | 264 } |
OLD | NEW |