1 // Copyright 2013 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.
33 #include "src/base/platform/platform.h"
34 #include "src/compilation-cache.h"
35 #include "src/execution.h"
36 #include "src/isolate.h"
37 #include "src/parser.h"
38 #include "src/unicode-inl.h"
39 #include "src/utils.h"
40 #include "test/cctest/cctest.h"
43 using ::v8::Extension;
45 using ::v8::FunctionTemplate;
47 using ::v8::HandleScope;
50 using ::v8::MessageCallback;
52 using ::v8::ObjectTemplate;
53 using ::v8::Persistent;
55 using ::v8::StackTrace;
58 using ::v8::Undefined;
62 static void ExpectBoolean(bool expected, Local<Value> result) {
63 CHECK(result->IsBoolean());
64 CHECK_EQ(expected, result->BooleanValue());
68 static void ExpectInt32(int32_t expected, Local<Value> result) {
69 CHECK(result->IsInt32());
70 CHECK_EQ(expected, result->Int32Value());
74 static void ExpectNumber(double expected, Local<Value> result) {
75 CHECK(result->IsNumber());
76 CHECK_EQ(expected, result->NumberValue());
80 static void ExpectUndefined(Local<Value> result) {
81 CHECK(result->IsUndefined());
85 // Tests are sorted by order of implementation.
89 v8::HandleScope scope(env->GetIsolate());
90 Local<Value> result = CompileRun("0x271828;");
91 ExpectInt32(0x271828, result);
95 TEST(global_variable) {
97 v8::HandleScope scope(env->GetIsolate());
98 Local<Value> result = CompileRun("var my_global_var = 0x123; my_global_var;");
99 ExpectInt32(0x123, result);
103 TEST(simple_function_call) {
105 v8::HandleScope scope(env->GetIsolate());
106 Local<Value> result = CompileRun(
107 "function foo() { return 0x314; }"
109 ExpectInt32(0x314, result);
115 v8::HandleScope scope(env->GetIsolate());
116 Local<Value> result = CompileRun(
120 " return 2 * (a + b - 1);"
123 ExpectInt32(0x2468, result);
126 static void if_comparison_testcontext_helper(
132 snprintf(buffer, sizeof(buffer),
135 "if ( lhs %s rhs ) { 1; }"
138 Local<Value> result = CompileRun(buffer);
139 ExpectInt32(expect, result);
142 static void if_comparison_effectcontext_helper(
148 snprintf(buffer, sizeof(buffer),
151 "var test = lhs %s rhs;"
155 Local<Value> result = CompileRun(buffer);
156 ExpectInt32(expect, result);
159 static void if_comparison_helper(
163 int expect_when_gt) {
164 // TODO(all): Non-SMI tests.
166 if_comparison_testcontext_helper(op, "1", "3", expect_when_lt);
167 if_comparison_testcontext_helper(op, "5", "5", expect_when_eq);
168 if_comparison_testcontext_helper(op, "9", "7", expect_when_gt);
170 if_comparison_effectcontext_helper(op, "1", "3", expect_when_lt);
171 if_comparison_effectcontext_helper(op, "5", "5", expect_when_eq);
172 if_comparison_effectcontext_helper(op, "9", "7", expect_when_gt);
176 TEST(if_comparison) {
178 v8::HandleScope scope(env->GetIsolate());
180 if_comparison_helper("<", 1, 0, 0);
181 if_comparison_helper("<=", 1, 1, 0);
182 if_comparison_helper("==", 0, 1, 0);
183 if_comparison_helper("===", 0, 1, 0);
184 if_comparison_helper(">=", 0, 1, 1);
185 if_comparison_helper(">", 0, 0, 1);
186 if_comparison_helper("!=", 1, 0, 1);
187 if_comparison_helper("!==", 1, 0, 1);
193 v8::HandleScope scope(env->GetIsolate());
196 result = CompileRun("var a = 1234; +a");
197 ExpectInt32(1234, result);
199 result = CompileRun("var a = 1234.5; +a");
200 ExpectNumber(1234.5, result);
202 result = CompileRun("var a = '1234'; +a");
203 ExpectInt32(1234, result);
205 result = CompileRun("var a = '1234.5'; +a");
206 ExpectNumber(1234.5, result);
207 // Check side effects.
208 result = CompileRun("var a = 1234; +(a = 4321); a");
209 ExpectInt32(4321, result);
215 v8::HandleScope scope(env->GetIsolate());
217 result = CompileRun("var a = 1234; -a");
218 ExpectInt32(-1234, result);
219 result = CompileRun("var a = 1234.5; -a");
220 ExpectNumber(-1234.5, result);
221 result = CompileRun("var a = 1234; -(a = 4321); a");
222 ExpectInt32(4321, result);
223 result = CompileRun("var a = '1234'; -a");
224 ExpectInt32(-1234, result);
225 result = CompileRun("var a = '1234.5'; -a");
226 ExpectNumber(-1234.5, result);
232 v8::HandleScope scope(env->GetIsolate());
234 result = CompileRun("var a = 1234; void (a);");
235 ExpectUndefined(result);
236 result = CompileRun("var a = 0; void (a = 42); a");
237 ExpectInt32(42, result);
238 result = CompileRun("var a = 0; void (a = 42);");
239 ExpectUndefined(result);
245 v8::HandleScope scope(env->GetIsolate());
247 result = CompileRun("var a = 1234; !a");
248 ExpectBoolean(false, result);
249 result = CompileRun("var a = 0; !a");
250 ExpectBoolean(true, result);
251 result = CompileRun("var a = 0; !(a = 1234); a");
252 ExpectInt32(1234, result);
253 result = CompileRun("var a = '1234'; !a");
254 ExpectBoolean(false, result);
255 result = CompileRun("var a = ''; !a");
256 ExpectBoolean(true, result);
257 result = CompileRun("var a = 1234; !!a");
258 ExpectBoolean(true, result);
259 result = CompileRun("var a = 0; !!a");
260 ExpectBoolean(false, result);
261 result = CompileRun("var a = 0; if ( !a ) { 1; } else { 0; }");
262 ExpectInt32(1, result);
263 result = CompileRun("var a = 1; if ( !a ) { 1; } else { 0; }");
264 ExpectInt32(0, result);