test-javascript-arm64.cc 8.56 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
// Copyright 2013 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
//       copyright notice, this list of conditions and the following
//       disclaimer in the documentation and/or other materials provided
//       with the distribution.
//     * Neither the name of Google Inc. nor the names of its
//       contributors may be used to endorse or promote products derived
//       from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include <limits.h>

30
#include "src/v8.h"
31

32
#include "src/api.h"
33
#include "src/base/platform/platform.h"
34 35 36
#include "src/compilation-cache.h"
#include "src/execution.h"
#include "src/isolate.h"
37
#include "src/objects-inl.h"
38 39 40
#include "src/unicode-inl.h"
#include "src/utils.h"
#include "test/cctest/cctest.h"
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60

using ::v8::Context;
using ::v8::Extension;
using ::v8::Function;
using ::v8::FunctionTemplate;
using ::v8::HandleScope;
using ::v8::Local;
using ::v8::Message;
using ::v8::MessageCallback;
using ::v8::Object;
using ::v8::ObjectTemplate;
using ::v8::Persistent;
using ::v8::Script;
using ::v8::StackTrace;
using ::v8::String;
using ::v8::TryCatch;
using ::v8::Undefined;
using ::v8::V8;
using ::v8::Value;

61 62
static void ExpectBoolean(Local<Context> context, bool expected,
                          Local<Value> result) {
63
  CHECK(result->IsBoolean());
64
  CHECK_EQ(expected, result->BooleanValue(context).FromJust());
65 66 67
}


68 69
static void ExpectInt32(Local<Context> context, int32_t expected,
                        Local<Value> result) {
70
  CHECK(result->IsInt32());
71
  CHECK_EQ(expected, result->Int32Value(context).FromJust());
72 73 74
}


75 76
static void ExpectNumber(Local<Context> context, double expected,
                         Local<Value> result) {
77
  CHECK(result->IsNumber());
78
  CHECK_EQ(expected, result->NumberValue(context).FromJust());
79 80 81 82 83 84 85 86 87 88 89 90 91 92
}


static void ExpectUndefined(Local<Value> result) {
  CHECK(result->IsUndefined());
}


// Tests are sorted by order of implementation.

TEST(simple_value) {
  LocalContext env;
  v8::HandleScope scope(env->GetIsolate());
  Local<Value> result = CompileRun("0x271828;");
93
  ExpectInt32(env.local(), 0x271828, result);
94 95 96 97 98 99 100
}


TEST(global_variable) {
  LocalContext env;
  v8::HandleScope scope(env->GetIsolate());
  Local<Value> result = CompileRun("var my_global_var = 0x123; my_global_var;");
101
  ExpectInt32(env.local(), 0x123, result);
102 103 104 105 106 107 108 109 110
}


TEST(simple_function_call) {
  LocalContext env;
  v8::HandleScope scope(env->GetIsolate());
  Local<Value> result = CompileRun(
      "function foo() { return 0x314; }"
      "foo();");
111
  ExpectInt32(env.local(), 0x314, result);
112 113 114 115 116 117 118 119 120 121 122 123 124
}


TEST(binary_op) {
  LocalContext env;
  v8::HandleScope scope(env->GetIsolate());
  Local<Value> result = CompileRun(
      "function foo() {"
      "  var a = 0x1200;"
      "  var b = 0x0035;"
      "  return 2 * (a + b - 1);"
      "}"
      "foo();");
125
  ExpectInt32(env.local(), 0x2468, result);
126 127
}

128 129 130
static void if_comparison_testcontext_helper(Local<Context> context,
                                             char const* op, char const* lhs,
                                             char const* rhs, int expect) {
131 132 133 134 135 136 137 138
  char buffer[256];
  snprintf(buffer, sizeof(buffer),
           "var lhs = %s;"
           "var rhs = %s;"
           "if ( lhs %s rhs ) { 1; }"
           "else { 0; }",
           lhs, rhs, op);
  Local<Value> result = CompileRun(buffer);
139
  ExpectInt32(context, expect, result);
140 141
}

142 143 144
static void if_comparison_effectcontext_helper(Local<Context> context,
                                               char const* op, char const* lhs,
                                               char const* rhs, int expect) {
145 146 147 148 149 150 151 152 153
  char buffer[256];
  snprintf(buffer, sizeof(buffer),
           "var lhs = %s;"
           "var rhs = %s;"
           "var test = lhs %s rhs;"
           "if ( test ) { 1; }"
           "else { 0; }",
           lhs, rhs, op);
  Local<Value> result = CompileRun(buffer);
154
  ExpectInt32(context, expect, result);
155 156
}

157 158 159
static void if_comparison_helper(Local<Context> context, char const* op,
                                 int expect_when_lt, int expect_when_eq,
                                 int expect_when_gt) {
160 161
  // TODO(all): Non-SMI tests.

162 163 164
  if_comparison_testcontext_helper(context, op, "1", "3", expect_when_lt);
  if_comparison_testcontext_helper(context, op, "5", "5", expect_when_eq);
  if_comparison_testcontext_helper(context, op, "9", "7", expect_when_gt);
165

166 167 168
  if_comparison_effectcontext_helper(context, op, "1", "3", expect_when_lt);
  if_comparison_effectcontext_helper(context, op, "5", "5", expect_when_eq);
  if_comparison_effectcontext_helper(context, op, "9", "7", expect_when_gt);
169 170 171 172 173 174 175
}


TEST(if_comparison) {
  LocalContext env;
  v8::HandleScope scope(env->GetIsolate());

176 177 178 179 180 181 182 183
  if_comparison_helper(env.local(), "<", 1, 0, 0);
  if_comparison_helper(env.local(), "<=", 1, 1, 0);
  if_comparison_helper(env.local(), "==", 0, 1, 0);
  if_comparison_helper(env.local(), "===", 0, 1, 0);
  if_comparison_helper(env.local(), ">=", 0, 1, 1);
  if_comparison_helper(env.local(), ">", 0, 0, 1);
  if_comparison_helper(env.local(), "!=", 1, 0, 1);
  if_comparison_helper(env.local(), "!==", 1, 0, 1);
184 185 186 187 188 189 190 191 192
}


TEST(unary_plus) {
  LocalContext env;
  v8::HandleScope scope(env->GetIsolate());
  Local<Value> result;
  // SMI
  result = CompileRun("var a = 1234; +a");
193
  ExpectInt32(env.local(), 1234, result);
194 195
  // Number
  result = CompileRun("var a = 1234.5; +a");
196
  ExpectNumber(env.local(), 1234.5, result);
197 198
  // String (SMI)
  result = CompileRun("var a = '1234'; +a");
199
  ExpectInt32(env.local(), 1234, result);
200 201
  // String (Number)
  result = CompileRun("var a = '1234.5'; +a");
202
  ExpectNumber(env.local(), 1234.5, result);
203 204
  // Check side effects.
  result = CompileRun("var a = 1234; +(a = 4321); a");
205
  ExpectInt32(env.local(), 4321, result);
206 207 208 209 210 211 212 213
}


TEST(unary_minus) {
  LocalContext env;
  v8::HandleScope scope(env->GetIsolate());
  Local<Value> result;
  result = CompileRun("var a = 1234; -a");
214
  ExpectInt32(env.local(), -1234, result);
215
  result = CompileRun("var a = 1234.5; -a");
216
  ExpectNumber(env.local(), -1234.5, result);
217
  result = CompileRun("var a = 1234; -(a = 4321); a");
218
  ExpectInt32(env.local(), 4321, result);
219
  result = CompileRun("var a = '1234'; -a");
220
  ExpectInt32(env.local(), -1234, result);
221
  result = CompileRun("var a = '1234.5'; -a");
222
  ExpectNumber(env.local(), -1234.5, result);
223 224 225 226 227 228 229 230 231 232
}


TEST(unary_void) {
  LocalContext env;
  v8::HandleScope scope(env->GetIsolate());
  Local<Value> result;
  result = CompileRun("var a = 1234; void (a);");
  ExpectUndefined(result);
  result = CompileRun("var a = 0; void (a = 42); a");
233
  ExpectInt32(env.local(), 42, result);
234 235 236 237 238 239 240 241 242 243
  result = CompileRun("var a = 0; void (a = 42);");
  ExpectUndefined(result);
}


TEST(unary_not) {
  LocalContext env;
  v8::HandleScope scope(env->GetIsolate());
  Local<Value> result;
  result = CompileRun("var a = 1234; !a");
244
  ExpectBoolean(env.local(), false, result);
245
  result = CompileRun("var a = 0; !a");
246
  ExpectBoolean(env.local(), true, result);
247
  result = CompileRun("var a = 0; !(a = 1234); a");
248
  ExpectInt32(env.local(), 1234, result);
249
  result = CompileRun("var a = '1234'; !a");
250
  ExpectBoolean(env.local(), false, result);
251
  result = CompileRun("var a = ''; !a");
252
  ExpectBoolean(env.local(), true, result);
253
  result = CompileRun("var a = 1234; !!a");
254
  ExpectBoolean(env.local(), true, result);
255
  result = CompileRun("var a = 0; !!a");
256
  ExpectBoolean(env.local(), false, result);
257
  result = CompileRun("var a = 0; if ( !a ) { 1; } else { 0; }");
258
  ExpectInt32(env.local(), 1, result);
259
  result = CompileRun("var a = 1; if ( !a ) { 1; } else { 0; }");
260
  ExpectInt32(env.local(), 0, result);
261
}