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
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 #include <limits.h>
29
30 #include "src/init/v8.h"
31
32 #include "src/api/api.h"
33 #include "src/base/platform/platform.h"
34 #include "src/codegen/compilation-cache.h"
35 #include "src/execution/execution.h"
36 #include "src/execution/isolate.h"
37 #include "src/objects/objects-inl.h"
38 #include "src/strings/unicode-inl.h"
39 #include "src/utils/utils.h"
40 #include "test/cctest/cctest.h"
41
42 namespace v8 {
43 namespace internal {
44 namespace test_javascript_arm64 {
45
ExpectBoolean(Local<v8::Context> context,bool expected,Local<Value> result)46 static void ExpectBoolean(Local<v8::Context> context, bool expected,
47 Local<Value> result) {
48 CHECK(result->IsBoolean());
49 CHECK_EQ(expected, result->BooleanValue(context->GetIsolate()));
50 }
51
ExpectInt32(Local<v8::Context> context,int32_t expected,Local<Value> result)52 static void ExpectInt32(Local<v8::Context> context, int32_t expected,
53 Local<Value> result) {
54 CHECK(result->IsInt32());
55 CHECK_EQ(expected, result->Int32Value(context).FromJust());
56 }
57
ExpectNumber(Local<v8::Context> context,double expected,Local<Value> result)58 static void ExpectNumber(Local<v8::Context> context, double expected,
59 Local<Value> result) {
60 CHECK(result->IsNumber());
61 CHECK_EQ(expected, result->NumberValue(context).FromJust());
62 }
63
64
ExpectUndefined(Local<Value> result)65 static void ExpectUndefined(Local<Value> result) {
66 CHECK(result->IsUndefined());
67 }
68
69
70 // Tests are sorted by order of implementation.
71
TEST(simple_value)72 TEST(simple_value) {
73 LocalContext env;
74 v8::HandleScope scope(env->GetIsolate());
75 Local<Value> result = CompileRun("0x271828;");
76 ExpectInt32(env.local(), 0x271828, result);
77 }
78
79
TEST(global_variable)80 TEST(global_variable) {
81 LocalContext env;
82 v8::HandleScope scope(env->GetIsolate());
83 Local<Value> result = CompileRun("var my_global_var = 0x123; my_global_var;");
84 ExpectInt32(env.local(), 0x123, result);
85 }
86
87
TEST(simple_function_call)88 TEST(simple_function_call) {
89 LocalContext env;
90 v8::HandleScope scope(env->GetIsolate());
91 Local<Value> result = CompileRun(
92 "function foo() { return 0x314; }"
93 "foo();");
94 ExpectInt32(env.local(), 0x314, result);
95 }
96
97
TEST(binary_op)98 TEST(binary_op) {
99 LocalContext env;
100 v8::HandleScope scope(env->GetIsolate());
101 Local<Value> result = CompileRun(
102 "function foo() {"
103 " var a = 0x1200;"
104 " var b = 0x0035;"
105 " return 2 * (a + b - 1);"
106 "}"
107 "foo();");
108 ExpectInt32(env.local(), 0x2468, result);
109 }
110
if_comparison_testcontext_helper(Local<v8::Context> context,char const * op,char const * lhs,char const * rhs,int expect)111 static void if_comparison_testcontext_helper(Local<v8::Context> context,
112 char const* op, char const* lhs,
113 char const* rhs, int expect) {
114 char buffer[256];
115 snprintf(buffer, sizeof(buffer),
116 "var lhs = %s;"
117 "var rhs = %s;"
118 "if ( lhs %s rhs ) { 1; }"
119 "else { 0; }",
120 lhs, rhs, op);
121 Local<Value> result = CompileRun(buffer);
122 ExpectInt32(context, expect, result);
123 }
124
if_comparison_effectcontext_helper(Local<v8::Context> context,char const * op,char const * lhs,char const * rhs,int expect)125 static void if_comparison_effectcontext_helper(Local<v8::Context> context,
126 char const* op, char const* lhs,
127 char const* rhs, int expect) {
128 char buffer[256];
129 snprintf(buffer, sizeof(buffer),
130 "var lhs = %s;"
131 "var rhs = %s;"
132 "var test = lhs %s rhs;"
133 "if ( test ) { 1; }"
134 "else { 0; }",
135 lhs, rhs, op);
136 Local<Value> result = CompileRun(buffer);
137 ExpectInt32(context, expect, result);
138 }
139
if_comparison_helper(Local<v8::Context> context,char const * op,int expect_when_lt,int expect_when_eq,int expect_when_gt)140 static void if_comparison_helper(Local<v8::Context> context, char const* op,
141 int expect_when_lt, int expect_when_eq,
142 int expect_when_gt) {
143 // TODO(all): Non-SMI tests.
144
145 if_comparison_testcontext_helper(context, op, "1", "3", expect_when_lt);
146 if_comparison_testcontext_helper(context, op, "5", "5", expect_when_eq);
147 if_comparison_testcontext_helper(context, op, "9", "7", expect_when_gt);
148
149 if_comparison_effectcontext_helper(context, op, "1", "3", expect_when_lt);
150 if_comparison_effectcontext_helper(context, op, "5", "5", expect_when_eq);
151 if_comparison_effectcontext_helper(context, op, "9", "7", expect_when_gt);
152 }
153
154
TEST(if_comparison)155 TEST(if_comparison) {
156 LocalContext env;
157 v8::HandleScope scope(env->GetIsolate());
158
159 if_comparison_helper(env.local(), "<", 1, 0, 0);
160 if_comparison_helper(env.local(), "<=", 1, 1, 0);
161 if_comparison_helper(env.local(), "==", 0, 1, 0);
162 if_comparison_helper(env.local(), "===", 0, 1, 0);
163 if_comparison_helper(env.local(), ">=", 0, 1, 1);
164 if_comparison_helper(env.local(), ">", 0, 0, 1);
165 if_comparison_helper(env.local(), "!=", 1, 0, 1);
166 if_comparison_helper(env.local(), "!==", 1, 0, 1);
167 }
168
169
TEST(unary_plus)170 TEST(unary_plus) {
171 LocalContext env;
172 v8::HandleScope scope(env->GetIsolate());
173 Local<Value> result;
174 // SMI
175 result = CompileRun("var a = 1234; +a");
176 ExpectInt32(env.local(), 1234, result);
177 // Number
178 result = CompileRun("var a = 1234.5; +a");
179 ExpectNumber(env.local(), 1234.5, result);
180 // String (SMI)
181 result = CompileRun("var a = '1234'; +a");
182 ExpectInt32(env.local(), 1234, result);
183 // String (Number)
184 result = CompileRun("var a = '1234.5'; +a");
185 ExpectNumber(env.local(), 1234.5, result);
186 // Check side effects.
187 result = CompileRun("var a = 1234; +(a = 4321); a");
188 ExpectInt32(env.local(), 4321, result);
189 }
190
191
TEST(unary_minus)192 TEST(unary_minus) {
193 LocalContext env;
194 v8::HandleScope scope(env->GetIsolate());
195 Local<Value> result;
196 result = CompileRun("var a = 1234; -a");
197 ExpectInt32(env.local(), -1234, result);
198 result = CompileRun("var a = 1234.5; -a");
199 ExpectNumber(env.local(), -1234.5, result);
200 result = CompileRun("var a = 1234; -(a = 4321); a");
201 ExpectInt32(env.local(), 4321, result);
202 result = CompileRun("var a = '1234'; -a");
203 ExpectInt32(env.local(), -1234, result);
204 result = CompileRun("var a = '1234.5'; -a");
205 ExpectNumber(env.local(), -1234.5, result);
206 }
207
208
TEST(unary_void)209 TEST(unary_void) {
210 LocalContext env;
211 v8::HandleScope scope(env->GetIsolate());
212 Local<Value> result;
213 result = CompileRun("var a = 1234; void (a);");
214 ExpectUndefined(result);
215 result = CompileRun("var a = 0; void (a = 42); a");
216 ExpectInt32(env.local(), 42, result);
217 result = CompileRun("var a = 0; void (a = 42);");
218 ExpectUndefined(result);
219 }
220
221
TEST(unary_not)222 TEST(unary_not) {
223 LocalContext env;
224 v8::HandleScope scope(env->GetIsolate());
225 Local<Value> result;
226 result = CompileRun("var a = 1234; !a");
227 ExpectBoolean(env.local(), false, result);
228 result = CompileRun("var a = 0; !a");
229 ExpectBoolean(env.local(), true, result);
230 result = CompileRun("var a = 0; !(a = 1234); a");
231 ExpectInt32(env.local(), 1234, result);
232 result = CompileRun("var a = '1234'; !a");
233 ExpectBoolean(env.local(), false, result);
234 result = CompileRun("var a = ''; !a");
235 ExpectBoolean(env.local(), true, result);
236 result = CompileRun("var a = 1234; !!a");
237 ExpectBoolean(env.local(), true, result);
238 result = CompileRun("var a = 0; !!a");
239 ExpectBoolean(env.local(), false, result);
240 result = CompileRun("var a = 0; if ( !a ) { 1; } else { 0; }");
241 ExpectInt32(env.local(), 1, result);
242 result = CompileRun("var a = 1; if ( !a ) { 1; } else { 0; }");
243 ExpectInt32(env.local(), 0, result);
244 }
245
246 } // namespace test_javascript_arm64
247 } // namespace internal
248 } // namespace v8
249