2018-05-13 10:10:44 +00:00
|
|
|
// Copyright 2018 the V8 project authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
module test {
|
2018-05-16 09:45:07 +00:00
|
|
|
macro ElementsKindTestHelper1(kind: constexpr ElementsKind): bool {
|
2018-07-17 16:20:53 +00:00
|
|
|
if constexpr((kind == UINT8_ELEMENTS) || (kind == UINT16_ELEMENTS)) {
|
|
|
|
return true;
|
|
|
|
}
|
2018-05-16 09:45:07 +00:00
|
|
|
else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2018-05-13 10:10:44 +00:00
|
|
|
|
2018-05-16 09:45:07 +00:00
|
|
|
macro ElementsKindTestHelper2(kind: constexpr ElementsKind): bool {
|
|
|
|
return ((kind == UINT8_ELEMENTS) || (kind == UINT16_ELEMENTS));
|
2018-05-13 10:10:44 +00:00
|
|
|
}
|
|
|
|
|
2018-05-16 09:45:07 +00:00
|
|
|
macro ElementsKindTestHelper3(kind: constexpr ElementsKind): constexpr bool {
|
|
|
|
return ((kind == UINT8_ELEMENTS) || (kind == UINT16_ELEMENTS));
|
|
|
|
}
|
2018-05-13 10:10:44 +00:00
|
|
|
|
2018-05-16 09:45:07 +00:00
|
|
|
macro LabelTestHelper1(): never
|
|
|
|
labels Label1 {
|
|
|
|
goto Label1;
|
|
|
|
}
|
2018-05-13 10:10:44 +00:00
|
|
|
|
2018-05-16 09:45:07 +00:00
|
|
|
macro LabelTestHelper2(): never
|
|
|
|
labels Label2(Smi) {
|
|
|
|
goto Label2(42);
|
|
|
|
}
|
2018-05-13 14:24:08 +00:00
|
|
|
|
2018-05-16 09:45:07 +00:00
|
|
|
macro LabelTestHelper3(): never
|
|
|
|
labels Label3(String, Smi) {
|
|
|
|
goto Label3('foo', 7);
|
|
|
|
}
|
2018-05-13 14:24:08 +00:00
|
|
|
|
2018-05-16 09:45:07 +00:00
|
|
|
macro TestConstexpr1() {
|
2018-06-12 11:11:44 +00:00
|
|
|
check(from_constexpr<bool>(IsFastElementsKind(PACKED_SMI_ELEMENTS)));
|
2018-05-16 09:45:07 +00:00
|
|
|
}
|
2018-05-13 14:24:08 +00:00
|
|
|
|
2018-05-16 09:45:07 +00:00
|
|
|
macro TestConstexprIf() {
|
2018-05-24 14:09:12 +00:00
|
|
|
check(ElementsKindTestHelper1(UINT8_ELEMENTS));
|
|
|
|
check(ElementsKindTestHelper1(UINT16_ELEMENTS));
|
|
|
|
check(!ElementsKindTestHelper1(UINT32_ELEMENTS));
|
2018-05-16 09:45:07 +00:00
|
|
|
}
|
2018-05-13 10:10:44 +00:00
|
|
|
|
2018-05-16 09:45:07 +00:00
|
|
|
macro TestConstexprReturn() {
|
2018-06-12 11:11:44 +00:00
|
|
|
check(from_constexpr<bool>(ElementsKindTestHelper3(UINT8_ELEMENTS)));
|
|
|
|
check(from_constexpr<bool>(ElementsKindTestHelper3(UINT16_ELEMENTS)));
|
|
|
|
check(!from_constexpr<bool>(ElementsKindTestHelper3(UINT32_ELEMENTS)));
|
|
|
|
check(from_constexpr<bool>(!ElementsKindTestHelper3(UINT32_ELEMENTS)));
|
2018-05-16 09:45:07 +00:00
|
|
|
}
|
2018-05-13 10:10:44 +00:00
|
|
|
|
2018-05-16 09:45:07 +00:00
|
|
|
macro TestGotoLabel(): Boolean {
|
|
|
|
try {
|
|
|
|
LabelTestHelper1() otherwise Label1;
|
|
|
|
}
|
|
|
|
label Label1 {
|
|
|
|
return True;
|
|
|
|
}
|
|
|
|
}
|
2018-05-13 10:10:44 +00:00
|
|
|
|
2018-05-16 09:45:07 +00:00
|
|
|
macro TestGotoLabelWithOneParameter(): Boolean {
|
|
|
|
try {
|
|
|
|
LabelTestHelper2() otherwise Label2;
|
|
|
|
}
|
|
|
|
label Label2(smi: Smi) {
|
2018-05-24 14:09:12 +00:00
|
|
|
check(smi == 42);
|
2018-05-16 09:45:07 +00:00
|
|
|
return True;
|
|
|
|
}
|
2018-05-13 14:24:08 +00:00
|
|
|
}
|
2018-05-16 09:45:07 +00:00
|
|
|
|
|
|
|
macro TestGotoLabelWithTwoParameters(): Boolean {
|
|
|
|
try {
|
|
|
|
LabelTestHelper3() otherwise Label3;
|
|
|
|
}
|
|
|
|
label Label3(str: String, smi: Smi) {
|
2018-05-24 14:09:12 +00:00
|
|
|
check(str == 'foo');
|
|
|
|
check(smi == 7);
|
2018-05-16 09:45:07 +00:00
|
|
|
return True;
|
|
|
|
}
|
2018-05-13 14:24:08 +00:00
|
|
|
}
|
|
|
|
|
2018-07-17 16:20:53 +00:00
|
|
|
builtin GenericBuiltinTest<T : type>(c: Context, param: T): Object {
|
2018-05-16 09:45:07 +00:00
|
|
|
return Null;
|
2018-05-13 14:24:08 +00:00
|
|
|
}
|
2018-05-16 09:45:07 +00:00
|
|
|
|
|
|
|
GenericBuiltinTest<Object>(c: Context, param: Object): Object {
|
|
|
|
return param;
|
2018-05-13 14:24:08 +00:00
|
|
|
}
|
|
|
|
|
2018-05-16 09:45:07 +00:00
|
|
|
macro TestBuiltinSpecialization(c: Context) {
|
2018-05-24 14:09:12 +00:00
|
|
|
check(GenericBuiltinTest<Smi>(c, 0) == Null);
|
|
|
|
check(GenericBuiltinTest<Smi>(c, 1) == Null);
|
|
|
|
check(GenericBuiltinTest<Object>(c, Undefined) == Undefined);
|
|
|
|
check(GenericBuiltinTest<Object>(c, Undefined) == Undefined);
|
2018-05-13 14:24:08 +00:00
|
|
|
}
|
2018-05-16 09:45:07 +00:00
|
|
|
|
|
|
|
macro LabelTestHelper4(flag: constexpr bool): never labels Label4, Label5 {
|
2018-07-17 16:20:53 +00:00
|
|
|
if
|
|
|
|
constexpr(flag) goto Label4;
|
2018-05-16 09:45:07 +00:00
|
|
|
else
|
|
|
|
goto Label5;
|
2018-05-13 14:24:08 +00:00
|
|
|
}
|
|
|
|
|
2018-05-16 09:45:07 +00:00
|
|
|
macro CallLabelTestHelper4(flag: constexpr bool): bool {
|
|
|
|
try {
|
|
|
|
LabelTestHelper4(flag) otherwise Label4, Label5;
|
|
|
|
}
|
|
|
|
label Label4 {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
label Label5 {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2018-05-14 10:53:04 +00:00
|
|
|
|
2018-05-16 09:45:07 +00:00
|
|
|
macro TestPartiallyUnusedLabel(): Boolean {
|
|
|
|
let r1: bool = CallLabelTestHelper4(true);
|
|
|
|
let r2: bool = CallLabelTestHelper4(false);
|
|
|
|
|
|
|
|
if (r1 && !r2)
|
|
|
|
return True;
|
|
|
|
else
|
|
|
|
return False;
|
2018-05-14 10:53:04 +00:00
|
|
|
}
|
2018-05-16 09:45:07 +00:00
|
|
|
|
2018-07-17 16:20:53 +00:00
|
|
|
macro GenericMacroTest<T : type>(param: T): Object {
|
2018-05-16 09:45:07 +00:00
|
|
|
return Undefined;
|
2018-05-14 10:53:04 +00:00
|
|
|
}
|
2018-05-16 09:45:07 +00:00
|
|
|
|
|
|
|
GenericMacroTest<Object>(param2: Object): Object {
|
|
|
|
return param2;
|
2018-05-14 10:53:04 +00:00
|
|
|
}
|
|
|
|
|
2018-07-17 16:20:53 +00:00
|
|
|
macro GenericMacroTestWithLabels<T : type>(param: T): Object labels X {
|
2018-05-16 09:45:07 +00:00
|
|
|
return Undefined;
|
|
|
|
}
|
2018-05-14 10:53:04 +00:00
|
|
|
|
2018-05-16 09:45:07 +00:00
|
|
|
GenericMacroTestWithLabels<Object>(param2: Object): Object labels Y {
|
|
|
|
return param2;
|
|
|
|
}
|
2018-05-14 10:53:04 +00:00
|
|
|
|
2018-05-16 09:45:07 +00:00
|
|
|
macro TestMacroSpecialization() {
|
|
|
|
try {
|
2018-05-24 14:09:12 +00:00
|
|
|
check(GenericMacroTest<Smi>(0) == Undefined);
|
|
|
|
check(GenericMacroTest<Smi>(1) == Undefined);
|
|
|
|
check(GenericMacroTest<Object>(Null) == Null);
|
|
|
|
check(GenericMacroTest<Object>(False) == False);
|
|
|
|
check(GenericMacroTest<Object>(True) == True);
|
|
|
|
check(GenericMacroTestWithLabels<Smi>(0) otherwise Fail == Undefined);
|
|
|
|
check(GenericMacroTestWithLabels<Smi>(0) otherwise Fail == Undefined);
|
|
|
|
check(GenericMacroTestWithLabels<Object>(Null) otherwise Fail == Null);
|
|
|
|
check(GenericMacroTestWithLabels<Object>(False) otherwise Fail == False);
|
2018-05-16 09:45:07 +00:00
|
|
|
}
|
|
|
|
label Fail {
|
|
|
|
unreachable;
|
|
|
|
}
|
|
|
|
}
|
2018-05-16 14:00:35 +00:00
|
|
|
|
2018-07-17 16:20:53 +00:00
|
|
|
builtin TestHelperPlus1(context: Context, x: Smi): Smi {
|
2018-05-16 14:00:35 +00:00
|
|
|
return x + 1;
|
|
|
|
}
|
2018-07-17 16:20:53 +00:00
|
|
|
builtin TestHelperPlus2(context: Context, x: Smi): Smi {
|
2018-05-16 14:00:35 +00:00
|
|
|
return x + 2;
|
|
|
|
}
|
|
|
|
|
2018-07-17 16:20:53 +00:00
|
|
|
macro TestFunctionPointers(context: Context): Boolean {
|
|
|
|
let fptr: builtin(Context, Smi) => Smi = TestHelperPlus1;
|
2018-05-24 14:09:12 +00:00
|
|
|
check(fptr(context, 42) == 43);
|
2018-05-16 14:00:35 +00:00
|
|
|
fptr = TestHelperPlus2;
|
2018-05-24 14:09:12 +00:00
|
|
|
check(fptr(context, 42) == 44);
|
2018-05-16 14:00:35 +00:00
|
|
|
return True;
|
|
|
|
}
|
2018-05-18 08:33:36 +00:00
|
|
|
|
2018-07-17 16:20:53 +00:00
|
|
|
macro TestVariableRedeclaration(context: Context): Boolean {
|
|
|
|
let var1: int31 = from_constexpr<bool>(42 == 0) ? 0 : 1;
|
|
|
|
let var2: int31 = from_constexpr<bool>(42 == 0) ? 1 : 0;
|
2018-05-18 08:33:36 +00:00
|
|
|
return True;
|
|
|
|
}
|
2018-05-22 12:48:29 +00:00
|
|
|
|
2018-07-17 16:20:53 +00:00
|
|
|
macro TestTernaryOperator(x: Smi): Smi {
|
|
|
|
let b: bool = x < 0 ? true : false;
|
2018-07-03 12:02:05 +00:00
|
|
|
return b ? x - 10 : x + 100;
|
|
|
|
}
|
|
|
|
|
2018-05-22 12:48:29 +00:00
|
|
|
macro TestFunctionPointerToGeneric(c: Context) {
|
|
|
|
let fptr1: builtin(Context, Smi) => Object = GenericBuiltinTest<Smi>;
|
|
|
|
let fptr2: builtin(Context, Object) => Object = GenericBuiltinTest<Object>;
|
|
|
|
|
2018-05-24 14:09:12 +00:00
|
|
|
check(fptr1(c, 0) == Null);
|
|
|
|
check(fptr1(c, 1) == Null);
|
|
|
|
check(fptr2(c, Undefined) == Undefined);
|
|
|
|
check(fptr2(c, Undefined) == Undefined);
|
2018-05-22 12:48:29 +00:00
|
|
|
}
|
2018-05-22 21:11:39 +00:00
|
|
|
|
|
|
|
type SmiToSmi = builtin(Smi) => Smi;
|
2018-07-17 16:20:53 +00:00
|
|
|
macro TestTypeAlias(x: SmiToSmi): Code {
|
2018-05-22 21:11:39 +00:00
|
|
|
return x;
|
|
|
|
}
|
2018-05-29 14:18:39 +00:00
|
|
|
|
|
|
|
macro TestUnsafeCast(c: Context, n: Number): Boolean {
|
|
|
|
if (TaggedIsSmi(n)) {
|
|
|
|
let m: Smi = unsafe_cast<Smi>(n);
|
|
|
|
|
|
|
|
check(TestHelperPlus1(c, m) == 11);
|
|
|
|
return True;
|
|
|
|
}
|
|
|
|
return False;
|
|
|
|
}
|
2018-06-05 08:49:05 +00:00
|
|
|
|
|
|
|
macro TestHexLiteral() {
|
|
|
|
check(convert<intptr>(0xffff) + 1 == 0x10000);
|
|
|
|
check(convert<intptr>(-0xffff) == -65535);
|
|
|
|
}
|
2018-06-05 11:54:38 +00:00
|
|
|
|
|
|
|
macro TestLargeIntegerLiterals(c: Context) {
|
|
|
|
let x: int32 = 0x40000000;
|
|
|
|
let y: int32 = 0x7fffffff;
|
|
|
|
}
|
2018-06-13 08:25:17 +00:00
|
|
|
|
|
|
|
macro TestMultilineAssert() {
|
|
|
|
let someVeryLongVariableNameThatWillCauseLineBreaks: Smi = 5;
|
2018-07-17 16:20:53 +00:00
|
|
|
check(
|
|
|
|
someVeryLongVariableNameThatWillCauseLineBreaks > 0 &&
|
|
|
|
someVeryLongVariableNameThatWillCauseLineBreaks < 10);
|
2018-06-13 08:25:17 +00:00
|
|
|
}
|
2018-06-28 12:15:37 +00:00
|
|
|
|
|
|
|
macro TestNewlineInString() {
|
|
|
|
Print('Hello, World!\n');
|
|
|
|
}
|
2018-07-13 08:50:22 +00:00
|
|
|
|
|
|
|
const kConstexprConst: constexpr int31 = 5;
|
|
|
|
const kIntptrConst: intptr = 4;
|
|
|
|
const kSmiConst: Smi = 3;
|
|
|
|
|
|
|
|
macro TestModuleConstBindings() {
|
|
|
|
check(kConstexprConst == Int32Constant(5));
|
|
|
|
check(kIntptrConst == 4);
|
|
|
|
check(kSmiConst == 3);
|
|
|
|
}
|
2018-07-17 06:39:07 +00:00
|
|
|
|
|
|
|
macro TestLocalConstBindings() {
|
2018-07-27 13:56:57 +00:00
|
|
|
const x : constexpr int31 = 3;
|
|
|
|
const x_smi : Smi = x;
|
|
|
|
{
|
|
|
|
const x : Smi = x + from_constexpr<Smi>(1);
|
|
|
|
check(x == x_smi + 1);
|
|
|
|
const x_smi : Smi = x;
|
|
|
|
check(x == x_smi);
|
|
|
|
check(x == 4);
|
|
|
|
}
|
|
|
|
check(x_smi == 3);
|
|
|
|
check(x == x_smi);
|
2018-07-17 06:39:07 +00:00
|
|
|
}
|
2018-07-17 16:20:53 +00:00
|
|
|
|
|
|
|
struct TestStructA {
|
|
|
|
indexes: FixedArray;
|
|
|
|
i: Smi;
|
|
|
|
k: Number;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct TestStructB {
|
|
|
|
x: TestStructA;
|
|
|
|
y: Smi;
|
|
|
|
}
|
|
|
|
|
|
|
|
macro TestStruct1(i: TestStructA): Smi {
|
|
|
|
return i.i;
|
|
|
|
}
|
|
|
|
|
|
|
|
macro TestStruct2(): TestStructA {
|
|
|
|
return TestStructA{unsafe_cast<FixedArray>(kEmptyFixedArray), 27, 31};
|
|
|
|
}
|
|
|
|
|
|
|
|
macro TestStruct3(): TestStructA {
|
|
|
|
let a: TestStructA =
|
|
|
|
TestStructA{unsafe_cast<FixedArray>(kEmptyFixedArray), 13, 5};
|
|
|
|
let b: TestStructA = a;
|
|
|
|
let c: TestStructA = TestStruct2();
|
|
|
|
a.i = TestStruct1(c);
|
|
|
|
a.k = a.i;
|
|
|
|
let d: TestStructB;
|
|
|
|
d.x = a;
|
|
|
|
d = TestStructB{a, 7};
|
|
|
|
let e: TestStructA = d.x;
|
|
|
|
let f: Smi = TestStructA{unsafe_cast<FixedArray>(kEmptyFixedArray), 27, 31}.i;
|
|
|
|
f = TestStruct2().i;
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct TestStructC {
|
|
|
|
x : TestStructA;
|
|
|
|
y : TestStructA;
|
|
|
|
}
|
|
|
|
|
|
|
|
macro TestStruct4(): TestStructC {
|
|
|
|
return TestStructC{TestStruct2(), TestStruct2()};
|
|
|
|
}
|
2018-07-27 10:32:14 +00:00
|
|
|
|
|
|
|
// This macro tests different versions of the for-loop where some parts
|
|
|
|
// are (not) present.
|
|
|
|
macro TestForLoop() {
|
|
|
|
let sum: Smi = 0;
|
|
|
|
for (let i: Smi = 0; i < 5; ++i) sum = sum + i;
|
|
|
|
check(sum == 10);
|
|
|
|
|
|
|
|
sum = 0;
|
|
|
|
let j: Smi = 0;
|
|
|
|
for (; j < 5; ++j) sum = sum + j;
|
|
|
|
check(sum == 10);
|
|
|
|
|
|
|
|
sum = 0;
|
|
|
|
j = 0;
|
|
|
|
for (; j < 5;) sum = sum + j++;
|
|
|
|
check(sum == 10);
|
|
|
|
|
|
|
|
// Check that break works. No test expression.
|
|
|
|
sum = 0;
|
|
|
|
for (let i: Smi = 0;; ++i) {
|
|
|
|
if (i == 5) break;
|
|
|
|
sum = sum + i;
|
|
|
|
}
|
|
|
|
check(sum == 10);
|
|
|
|
|
|
|
|
sum = 0;
|
|
|
|
j = 0;
|
|
|
|
for (;;) {
|
|
|
|
if (j == 5) break;
|
|
|
|
sum = sum + j;
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
check(sum == 10);
|
|
|
|
|
|
|
|
// The following tests are the same as above, but use continue to skip
|
|
|
|
// index 3.
|
|
|
|
sum = 0;
|
|
|
|
for (let i: Smi = 0; i < 5; ++i) {
|
|
|
|
if (i == 3) continue;
|
|
|
|
sum = sum + i;
|
|
|
|
}
|
|
|
|
check(sum == 7);
|
|
|
|
|
|
|
|
sum = 0;
|
|
|
|
j = 0;
|
|
|
|
for (; j < 5; ++j) {
|
|
|
|
if (j == 3) continue;
|
|
|
|
sum = sum + j;
|
|
|
|
}
|
|
|
|
check(sum == 7);
|
|
|
|
|
|
|
|
sum = 0;
|
|
|
|
j = 0;
|
|
|
|
for (; j < 5;) {
|
|
|
|
if (j == 3) {
|
|
|
|
j++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
sum = sum + j;
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
check(sum == 7);
|
|
|
|
|
|
|
|
sum = 0;
|
|
|
|
for (let i: Smi = 0;; ++i) {
|
|
|
|
if (i == 3) continue;
|
|
|
|
if (i == 5) break;
|
|
|
|
sum = sum + i;
|
|
|
|
}
|
|
|
|
check(sum == 7);
|
|
|
|
|
|
|
|
sum = 0;
|
|
|
|
j = 0;
|
|
|
|
for (;;) {
|
|
|
|
if (j == 3) {
|
|
|
|
j++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (j == 5) break;
|
|
|
|
sum = sum + j;
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
check(sum == 7);
|
|
|
|
}
|
2018-05-13 10:10:44 +00:00
|
|
|
}
|