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.
|
|
|
|
|
2020-04-07 11:20:38 +00:00
|
|
|
// Test line comment
|
|
|
|
/* Test mulitline
|
|
|
|
comment
|
|
|
|
*/
|
2020-07-16 09:35:18 +00:00
|
|
|
/*multiline_without_whitespace*/
|
2020-04-07 11:20:38 +00:00
|
|
|
|
2018-11-13 13:00:34 +00:00
|
|
|
namespace test {
|
2020-05-12 13:28:29 +00:00
|
|
|
macro ElementsKindTestHelper1(kind: constexpr ElementsKind): bool {
|
|
|
|
if constexpr (
|
2020-07-16 09:35:18 +00:00
|
|
|
kind == ElementsKind::UINT8_ELEMENTS ||
|
|
|
|
kind == ElementsKind::UINT16_ELEMENTS) {
|
2020-05-12 13:28:29 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
2018-05-16 09:45:07 +00:00
|
|
|
}
|
2020-05-12 13:28:29 +00:00
|
|
|
}
|
2018-05-13 10:10:44 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
macro ElementsKindTestHelper2(kind: constexpr ElementsKind): constexpr bool {
|
2020-07-16 09:35:18 +00:00
|
|
|
return kind == ElementsKind::UINT8_ELEMENTS ||
|
|
|
|
kind == ElementsKind::UINT16_ELEMENTS;
|
2020-05-12 13:28:29 +00:00
|
|
|
}
|
2018-05-13 10:10:44 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
macro LabelTestHelper1(): never
|
|
|
|
labels Label1 {
|
|
|
|
goto Label1;
|
|
|
|
}
|
2018-05-13 10:10:44 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
macro LabelTestHelper2(): never
|
|
|
|
labels Label2(Smi) {
|
|
|
|
goto Label2(42);
|
|
|
|
}
|
2018-05-13 14:24:08 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
macro LabelTestHelper3(): never
|
|
|
|
labels Label3(Oddball, Smi) {
|
|
|
|
goto Label3(Null, 7);
|
|
|
|
}
|
2018-05-13 14:24:08 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
@export
|
2021-09-30 15:12:46 +00:00
|
|
|
macro TestConstexpr1(): void {
|
2020-05-12 13:28:29 +00:00
|
|
|
check(FromConstexpr<bool>(
|
|
|
|
IsFastElementsKind(ElementsKind::PACKED_SMI_ELEMENTS)));
|
|
|
|
}
|
2018-05-13 14:24:08 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
@export
|
2021-09-30 15:12:46 +00:00
|
|
|
macro TestConstexprIf(): void {
|
2020-05-12 13:28:29 +00:00
|
|
|
check(ElementsKindTestHelper1(ElementsKind::UINT8_ELEMENTS));
|
|
|
|
check(ElementsKindTestHelper1(ElementsKind::UINT16_ELEMENTS));
|
|
|
|
check(!ElementsKindTestHelper1(ElementsKind::UINT32_ELEMENTS));
|
|
|
|
}
|
2018-05-13 10:10:44 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
@export
|
2021-09-30 15:12:46 +00:00
|
|
|
macro TestConstexprReturn(): void {
|
2020-05-12 13:28:29 +00:00
|
|
|
check(FromConstexpr<bool>(
|
|
|
|
ElementsKindTestHelper2(ElementsKind::UINT8_ELEMENTS)));
|
|
|
|
check(FromConstexpr<bool>(
|
|
|
|
ElementsKindTestHelper2(ElementsKind::UINT16_ELEMENTS)));
|
|
|
|
check(!FromConstexpr<bool>(
|
|
|
|
ElementsKindTestHelper2(ElementsKind::UINT32_ELEMENTS)));
|
|
|
|
check(FromConstexpr<bool>(
|
|
|
|
!ElementsKindTestHelper2(ElementsKind::UINT32_ELEMENTS)));
|
|
|
|
}
|
2018-05-13 10:10:44 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
@export
|
|
|
|
macro TestGotoLabel(): Boolean {
|
|
|
|
try {
|
|
|
|
LabelTestHelper1() otherwise Label1;
|
|
|
|
} label Label1 {
|
|
|
|
return True;
|
2018-05-16 09:45:07 +00:00
|
|
|
}
|
2020-05-12 13:28:29 +00:00
|
|
|
}
|
2018-05-13 10:10:44 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
@export
|
|
|
|
macro TestGotoLabelWithOneParameter(): Boolean {
|
|
|
|
try {
|
|
|
|
LabelTestHelper2() otherwise Label2;
|
|
|
|
} label Label2(smi: Smi) {
|
|
|
|
check(smi == 42);
|
|
|
|
return True;
|
2018-05-13 14:24:08 +00:00
|
|
|
}
|
2020-05-12 13:28:29 +00:00
|
|
|
}
|
2018-05-16 09:45:07 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
@export
|
|
|
|
macro TestGotoLabelWithTwoParameters(): Boolean {
|
|
|
|
try {
|
|
|
|
LabelTestHelper3() otherwise Label3;
|
|
|
|
} label Label3(o: Oddball, smi: Smi) {
|
|
|
|
check(o == Null);
|
|
|
|
check(smi == 7);
|
|
|
|
return True;
|
2018-05-13 14:24:08 +00:00
|
|
|
}
|
2020-05-12 13:28:29 +00:00
|
|
|
}
|
2018-05-13 14:24:08 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
builtin GenericBuiltinTest<T: type>(_param: T): JSAny {
|
|
|
|
return Null;
|
|
|
|
}
|
2018-05-16 09:45:07 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
GenericBuiltinTest<JSAny>(param: JSAny): JSAny {
|
|
|
|
return param;
|
|
|
|
}
|
2018-05-13 14:24:08 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
@export
|
2021-09-30 15:12:46 +00:00
|
|
|
macro TestBuiltinSpecialization(): void {
|
2020-05-12 13:28:29 +00:00
|
|
|
check(GenericBuiltinTest<Smi>(0) == Null);
|
|
|
|
check(GenericBuiltinTest<Smi>(1) == Null);
|
|
|
|
check(GenericBuiltinTest<JSAny>(Undefined) == Undefined);
|
|
|
|
check(GenericBuiltinTest<JSAny>(Undefined) == Undefined);
|
|
|
|
}
|
2018-05-16 09:45:07 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
macro LabelTestHelper4(flag: constexpr bool): never
|
|
|
|
labels Label4, Label5 {
|
|
|
|
if constexpr (flag) {
|
|
|
|
goto Label4;
|
|
|
|
} else {
|
|
|
|
goto Label5;
|
2018-05-13 14:24:08 +00:00
|
|
|
}
|
2020-05-12 13:28:29 +00:00
|
|
|
}
|
2018-05-13 14:24:08 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
macro CallLabelTestHelper4(flag: constexpr bool): bool {
|
|
|
|
try {
|
|
|
|
LabelTestHelper4(flag) otherwise Label4, Label5;
|
|
|
|
} label Label4 {
|
|
|
|
return true;
|
|
|
|
} label Label5 {
|
|
|
|
return false;
|
2018-05-16 09:45:07 +00:00
|
|
|
}
|
2020-05-12 13:28:29 +00:00
|
|
|
}
|
2018-05-14 10:53:04 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
@export
|
|
|
|
macro TestPartiallyUnusedLabel(): Boolean {
|
|
|
|
const r1: bool = CallLabelTestHelper4(true);
|
|
|
|
const r2: bool = CallLabelTestHelper4(false);
|
2018-05-16 09:45:07 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
if (r1 && !r2) {
|
|
|
|
return True;
|
|
|
|
} else {
|
|
|
|
return False;
|
2018-05-14 10:53:04 +00:00
|
|
|
}
|
2020-05-12 13:28:29 +00:00
|
|
|
}
|
2018-05-16 09:45:07 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
macro GenericMacroTest<T: type>(_param: T): Object {
|
|
|
|
return Undefined;
|
|
|
|
}
|
2018-05-16 09:45:07 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
GenericMacroTest<Object>(param2: Object): Object {
|
|
|
|
return param2;
|
|
|
|
}
|
2018-05-14 10:53:04 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
macro GenericMacroTestWithLabels<T: type>(_param: T): Object
|
|
|
|
labels _X {
|
|
|
|
return Undefined;
|
|
|
|
}
|
2018-05-14 10:53:04 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
GenericMacroTestWithLabels<Object>(param2: Object): Object
|
|
|
|
labels Y {
|
|
|
|
return Cast<Smi>(param2) otherwise Y;
|
|
|
|
}
|
2018-05-14 10:53:04 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
@export
|
2021-09-30 15:12:46 +00:00
|
|
|
macro TestMacroSpecialization(): void {
|
2020-05-12 13:28:29 +00:00
|
|
|
try {
|
|
|
|
const _smi0: Smi = 0;
|
|
|
|
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);
|
2018-05-16 09:45:07 +00:00
|
|
|
try {
|
2020-05-12 13:28:29 +00:00
|
|
|
GenericMacroTestWithLabels<Object>(False) otherwise Expected;
|
|
|
|
} label Expected {}
|
|
|
|
} label Fail {
|
|
|
|
unreachable;
|
2018-05-16 09:45:07 +00:00
|
|
|
}
|
2020-05-12 13:28:29 +00:00
|
|
|
}
|
2018-05-16 14:00:35 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
builtin TestHelperPlus1(x: Smi): Smi {
|
|
|
|
return x + 1;
|
|
|
|
}
|
|
|
|
builtin TestHelperPlus2(x: Smi): Smi {
|
|
|
|
return x + 2;
|
|
|
|
}
|
2018-05-16 14:00:35 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
@export
|
|
|
|
macro TestFunctionPointers(implicit context: Context)(): Boolean {
|
|
|
|
let fptr: builtin(Smi) => Smi = TestHelperPlus1;
|
|
|
|
check(fptr(42) == 43);
|
|
|
|
fptr = TestHelperPlus2;
|
|
|
|
check(fptr(42) == 44);
|
|
|
|
return True;
|
|
|
|
}
|
2018-05-18 08:33:36 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
@export
|
|
|
|
macro TestVariableRedeclaration(implicit context: Context)(): Boolean {
|
2022-01-27 12:48:40 +00:00
|
|
|
let _var1: int31 = FromConstexpr<bool>(42 == 0) ? FromConstexpr<int31>(0) : 1;
|
|
|
|
let _var2: int31 = FromConstexpr<bool>(42 == 0) ? FromConstexpr<int31>(1) : 0;
|
2020-05-12 13:28:29 +00:00
|
|
|
return True;
|
|
|
|
}
|
2018-05-22 12:48:29 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
@export
|
|
|
|
macro TestTernaryOperator(x: Smi): Smi {
|
|
|
|
const b: bool = x < 0 ? true : false;
|
|
|
|
return b ? x - 10 : x + 100;
|
|
|
|
}
|
2018-07-03 12:02:05 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
@export
|
2021-09-30 15:12:46 +00:00
|
|
|
macro TestFunctionPointerToGeneric(): void {
|
2020-05-12 13:28:29 +00:00
|
|
|
const fptr1: builtin(Smi) => JSAny = GenericBuiltinTest<Smi>;
|
|
|
|
const fptr2: builtin(JSAny) => JSAny = GenericBuiltinTest<JSAny>;
|
2018-05-22 12:48:29 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
check(fptr1(0) == Null);
|
|
|
|
check(fptr1(1) == Null);
|
|
|
|
check(fptr2(Undefined) == Undefined);
|
|
|
|
check(fptr2(Undefined) == Undefined);
|
|
|
|
}
|
2018-05-22 21:11:39 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
type ObjectToObject = builtin(Context, JSAny) => JSAny;
|
|
|
|
@export
|
|
|
|
macro TestTypeAlias(x: ObjectToObject): BuiltinPtr {
|
|
|
|
return x;
|
|
|
|
}
|
2018-05-29 14:18:39 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
@export
|
|
|
|
macro TestUnsafeCast(implicit context: Context)(n: Number): Boolean {
|
|
|
|
if (TaggedIsSmi(n)) {
|
|
|
|
const m: Smi = UnsafeCast<Smi>(n);
|
2018-05-29 14:18:39 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
check(TestHelperPlus1(m) == 11);
|
|
|
|
return True;
|
2018-05-29 14:18:39 +00:00
|
|
|
}
|
2020-05-12 13:28:29 +00:00
|
|
|
return False;
|
|
|
|
}
|
2018-06-05 08:49:05 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
@export
|
2021-09-30 15:12:46 +00:00
|
|
|
macro TestHexLiteral(): void {
|
2020-05-12 13:28:29 +00:00
|
|
|
check(Convert<intptr>(0xffff) + 1 == 0x10000);
|
|
|
|
check(Convert<intptr>(-0xffff) == -65535);
|
|
|
|
}
|
2018-06-05 11:54:38 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
@export
|
2021-09-30 15:12:46 +00:00
|
|
|
macro TestLargeIntegerLiterals(implicit c: Context)(): void {
|
2020-05-12 13:28:29 +00:00
|
|
|
let _x: int32 = 0x40000000;
|
|
|
|
let _y: int32 = 0x7fffffff;
|
|
|
|
}
|
2018-06-13 08:25:17 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
@export
|
2021-09-30 15:12:46 +00:00
|
|
|
macro TestMultilineAssert(): void {
|
2020-05-12 13:28:29 +00:00
|
|
|
const someVeryLongVariableNameThatWillCauseLineBreaks: Smi = 5;
|
|
|
|
check(
|
|
|
|
someVeryLongVariableNameThatWillCauseLineBreaks > 0 &&
|
|
|
|
someVeryLongVariableNameThatWillCauseLineBreaks < 10);
|
|
|
|
}
|
2018-06-28 12:15:37 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
@export
|
2021-09-30 15:12:46 +00:00
|
|
|
macro TestNewlineInString(): void {
|
2020-05-12 13:28:29 +00:00
|
|
|
Print('Hello, World!\n');
|
|
|
|
}
|
2018-07-13 08:50:22 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
const kConstexprConst: constexpr int31 = 5;
|
|
|
|
const kIntptrConst: intptr = 4;
|
|
|
|
const kSmiConst: Smi = 3;
|
2018-07-13 08:50:22 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
@export
|
2021-09-30 15:12:46 +00:00
|
|
|
macro TestModuleConstBindings(): void {
|
2020-05-12 13:28:29 +00:00
|
|
|
check(kConstexprConst == Int32Constant(5));
|
|
|
|
check(kIntptrConst == 4);
|
|
|
|
check(kSmiConst == 3);
|
|
|
|
}
|
2018-07-17 06:39:07 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
@export
|
2021-09-30 15:12:46 +00:00
|
|
|
macro TestLocalConstBindings(): void {
|
2020-05-12 13:28:29 +00:00
|
|
|
const x: constexpr int31 = 3;
|
|
|
|
const xSmi: Smi = x;
|
|
|
|
{
|
|
|
|
const x: Smi = x + FromConstexpr<Smi>(1);
|
|
|
|
check(x == xSmi + 1);
|
2018-09-24 09:28:48 +00:00
|
|
|
const xSmi: Smi = x;
|
2018-09-11 13:15:02 +00:00
|
|
|
check(x == xSmi);
|
2020-05-12 13:28:29 +00:00
|
|
|
check(x == 4);
|
2018-07-17 06:39:07 +00:00
|
|
|
}
|
2020-05-12 13:28:29 +00:00
|
|
|
check(xSmi == 3);
|
|
|
|
check(x == xSmi);
|
|
|
|
}
|
2018-07-17 16:20:53 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
struct TestStructA {
|
|
|
|
indexes: FixedArray;
|
|
|
|
i: Smi;
|
|
|
|
k: Number;
|
|
|
|
}
|
2018-07-17 16:20:53 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
struct TestStructB {
|
|
|
|
x: TestStructA;
|
|
|
|
y: Smi;
|
|
|
|
}
|
2018-07-17 16:20:53 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
@export
|
|
|
|
macro TestStruct1(i: TestStructA): Smi {
|
|
|
|
return i.i;
|
|
|
|
}
|
2018-07-17 16:20:53 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
@export
|
|
|
|
macro TestStruct2(implicit context: Context)(): TestStructA {
|
|
|
|
return TestStructA{
|
|
|
|
indexes: UnsafeCast<FixedArray>(kEmptyFixedArray),
|
|
|
|
i: 27,
|
|
|
|
k: 31
|
|
|
|
};
|
|
|
|
}
|
2018-07-17 16:20:53 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
@export
|
|
|
|
macro TestStruct3(implicit context: Context)(): TestStructA {
|
|
|
|
let a: TestStructA =
|
|
|
|
TestStructA{indexes: UnsafeCast<FixedArray>(kEmptyFixedArray), i: 13, k: 5};
|
|
|
|
let _b: TestStructA = a;
|
|
|
|
const c: TestStructA = TestStruct2();
|
|
|
|
a.i = TestStruct1(c);
|
|
|
|
a.k = a.i;
|
|
|
|
let d: TestStructB;
|
|
|
|
d.x = a;
|
|
|
|
d = TestStructB{x: a, y: 7};
|
|
|
|
let _e: TestStructA = d.x;
|
|
|
|
let f: Smi = TestStructA{
|
|
|
|
indexes: UnsafeCast<FixedArray>(kEmptyFixedArray),
|
|
|
|
i: 27,
|
|
|
|
k: 31
|
|
|
|
}.i;
|
|
|
|
f = TestStruct2().i;
|
|
|
|
return a;
|
|
|
|
}
|
2018-07-27 10:32:14 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
struct TestStructC {
|
|
|
|
x: TestStructA;
|
|
|
|
y: TestStructA;
|
|
|
|
}
|
2019-01-17 13:25:11 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
@export
|
|
|
|
macro TestStruct4(implicit context: Context)(): TestStructC {
|
|
|
|
return TestStructC{x: TestStruct2(), y: TestStruct2()};
|
|
|
|
}
|
2018-07-27 10:32:14 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
macro TestStructInLabel(implicit context: Context)(): never labels
|
|
|
|
Foo(TestStructA) {
|
|
|
|
goto Foo(TestStruct2());
|
|
|
|
}
|
|
|
|
@export // Silence unused warning.
|
2021-09-30 15:12:46 +00:00
|
|
|
macro CallTestStructInLabel(implicit context: Context)(): void {
|
2020-05-12 13:28:29 +00:00
|
|
|
try {
|
|
|
|
TestStructInLabel() otherwise Foo;
|
|
|
|
} label Foo(_s: TestStructA) {}
|
|
|
|
}
|
2018-07-27 10:32:14 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
// This macro tests different versions of the for-loop where some parts
|
|
|
|
// are (not) present.
|
|
|
|
@export
|
2021-09-30 15:12:46 +00:00
|
|
|
macro TestForLoop(): void {
|
2020-05-12 13:28:29 +00:00
|
|
|
let sum: Smi = 0;
|
|
|
|
for (let i: Smi = 0; i < 5; ++i) sum += i;
|
|
|
|
check(sum == 10);
|
|
|
|
|
|
|
|
sum = 0;
|
|
|
|
let j: Smi = 0;
|
|
|
|
for (; j < 5; ++j) sum += j;
|
|
|
|
check(sum == 10);
|
|
|
|
|
|
|
|
sum = 0;
|
|
|
|
j = 0;
|
|
|
|
for (; j < 5;) 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 += i;
|
|
|
|
}
|
|
|
|
check(sum == 10);
|
|
|
|
|
|
|
|
sum = 0;
|
|
|
|
j = 0;
|
|
|
|
for (;;) {
|
|
|
|
if (j == 5) break;
|
|
|
|
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 += i;
|
|
|
|
}
|
|
|
|
check(sum == 7);
|
|
|
|
|
|
|
|
sum = 0;
|
|
|
|
j = 0;
|
|
|
|
for (; j < 5; ++j) {
|
|
|
|
if (j == 3) continue;
|
|
|
|
sum += j;
|
|
|
|
}
|
|
|
|
check(sum == 7);
|
|
|
|
|
|
|
|
sum = 0;
|
|
|
|
j = 0;
|
|
|
|
for (; j < 5;) {
|
|
|
|
if (j == 3) {
|
2018-07-27 10:32:14 +00:00
|
|
|
j++;
|
2020-05-12 13:28:29 +00:00
|
|
|
continue;
|
2018-07-27 10:32:14 +00:00
|
|
|
}
|
2020-05-12 13:28:29 +00:00
|
|
|
sum += j;
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
check(sum == 7);
|
2018-07-27 10:32:14 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
sum = 0;
|
|
|
|
for (let i: Smi = 0;; ++i) {
|
|
|
|
if (i == 3) continue;
|
|
|
|
if (i == 5) break;
|
|
|
|
sum += i;
|
|
|
|
}
|
|
|
|
check(sum == 7);
|
2018-07-27 10:32:14 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
sum = 0;
|
|
|
|
j = 0;
|
|
|
|
for (;;) {
|
|
|
|
if (j == 3) {
|
2018-07-27 10:32:14 +00:00
|
|
|
j++;
|
2020-05-12 13:28:29 +00:00
|
|
|
continue;
|
2018-07-27 10:32:14 +00:00
|
|
|
}
|
2018-10-24 09:18:30 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
if (j == 5) break;
|
|
|
|
sum += j;
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
check(sum == 7);
|
2019-02-06 12:35:58 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
j = 0;
|
|
|
|
try {
|
|
|
|
for (;;) {
|
|
|
|
if (++j == 10) goto Exit;
|
2019-02-06 12:35:58 +00:00
|
|
|
}
|
2020-05-12 13:28:29 +00:00
|
|
|
} label Exit {
|
|
|
|
check(j == 10);
|
2018-07-27 10:32:14 +00:00
|
|
|
}
|
2018-08-07 14:06:18 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
// Test if we can handle uninitialized values on the stack.
|
|
|
|
let _i: Smi;
|
|
|
|
for (let j: Smi = 0; j < 10; ++j) {
|
2018-08-07 21:57:19 +00:00
|
|
|
}
|
2020-05-12 13:28:29 +00:00
|
|
|
}
|
2018-08-07 21:57:19 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
@export
|
2021-09-30 15:12:46 +00:00
|
|
|
macro TestSubtyping(x: Smi): void {
|
2020-05-12 13:28:29 +00:00
|
|
|
const _foo: JSAny = x;
|
|
|
|
}
|
|
|
|
|
|
|
|
macro IncrementIfSmi<A: type>(x: A): A {
|
|
|
|
typeswitch (x) {
|
|
|
|
case (x: Smi): {
|
|
|
|
return x + 1;
|
|
|
|
}
|
|
|
|
case (o: A): {
|
|
|
|
return o;
|
2018-08-07 21:57:19 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-12 13:28:29 +00:00
|
|
|
}
|
2018-08-07 21:57:19 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
type NumberOrFixedArray = Number|FixedArray;
|
|
|
|
macro TypeswitchExample(implicit context: Context)(x: NumberOrFixedArray):
|
|
|
|
int32 {
|
|
|
|
let result: int32 = 0;
|
|
|
|
typeswitch (IncrementIfSmi(x)) {
|
|
|
|
case (_x: FixedArray): {
|
|
|
|
result = result + 1;
|
2018-08-07 21:57:19 +00:00
|
|
|
}
|
2020-05-12 13:28:29 +00:00
|
|
|
case (Number): {
|
|
|
|
result = result + 2;
|
2018-08-07 21:57:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
result = result * 10;
|
2018-08-07 21:57:19 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
typeswitch (IncrementIfSmi(x)) {
|
|
|
|
case (x: Smi): {
|
|
|
|
result = result + Convert<int32>(x);
|
|
|
|
}
|
|
|
|
case (a: FixedArray): {
|
|
|
|
result = result + Convert<int32>(a.length);
|
|
|
|
}
|
|
|
|
case (_x: HeapNumber): {
|
|
|
|
result = result + 7;
|
2019-01-17 13:58:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
return result;
|
|
|
|
}
|
2018-08-07 14:06:18 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
@export
|
2021-09-30 15:12:46 +00:00
|
|
|
macro TestTypeswitch(implicit context: Context)(): void {
|
2020-05-12 13:28:29 +00:00
|
|
|
check(TypeswitchExample(FromConstexpr<Smi>(5)) == 26);
|
|
|
|
const a: FixedArray = AllocateZeroedFixedArray(3);
|
|
|
|
check(TypeswitchExample(a) == 13);
|
|
|
|
check(TypeswitchExample(FromConstexpr<Number>(0.5)) == 27);
|
|
|
|
}
|
[torque] disallow using logical operators in value contexts
This CL makes sure, that logical operators (||, &&) always have return
type never. Together with a check that never is never passed as a
function argument, this prevents faulty evaluation as in !(x || y).
Before, the logical operators had a behavior similar to
(bool labels Taken, NotTaken), with a fast exit if the left-hand side
allowed shor-circuit evaluation, but returning the right-hand side
otherwise. Since we want to allow existing (a || b || c) patterns in
the codebase, this requires weakening the restriction that the left-
and right-hand side need to have the same type. Now the possibilites
are:
bool, never
never, bool
never, never
bool, bool
constexpr bool, constexpr bool
Bug: v8:8137
Change-Id: I9576b337dc4008ac58b4625e77fef4e73bcdd6e3
Reviewed-on: https://chromium-review.googlesource.com/1215162
Reviewed-by: Daniel Clifford <danno@chromium.org>
Commit-Queue: Tobias Tebbi <tebbi@chromium.org>
Cr-Commit-Position: refs/heads/master@{#55750}
2018-09-08 16:16:08 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
@export
|
2021-09-30 15:12:46 +00:00
|
|
|
macro TestTypeswitchAsanLsanFailure(implicit context: Context)(obj: Object):
|
|
|
|
void {
|
2020-05-12 13:28:29 +00:00
|
|
|
typeswitch (obj) {
|
|
|
|
case (_o: Smi): {
|
|
|
|
}
|
|
|
|
case (_o: JSTypedArray): {
|
|
|
|
}
|
|
|
|
case (_o: JSReceiver): {
|
|
|
|
}
|
|
|
|
case (_o: HeapObject): {
|
|
|
|
}
|
2019-02-06 15:17:35 +00:00
|
|
|
}
|
2020-05-12 13:28:29 +00:00
|
|
|
}
|
2019-02-06 15:17:35 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
macro ExampleGenericOverload<A: type>(o: Object): A {
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
macro ExampleGenericOverload<A: type>(o: Smi): A {
|
|
|
|
return o + 1;
|
|
|
|
}
|
[torque] disallow using logical operators in value contexts
This CL makes sure, that logical operators (||, &&) always have return
type never. Together with a check that never is never passed as a
function argument, this prevents faulty evaluation as in !(x || y).
Before, the logical operators had a behavior similar to
(bool labels Taken, NotTaken), with a fast exit if the left-hand side
allowed shor-circuit evaluation, but returning the right-hand side
otherwise. Since we want to allow existing (a || b || c) patterns in
the codebase, this requires weakening the restriction that the left-
and right-hand side need to have the same type. Now the possibilites
are:
bool, never
never, bool
never, never
bool, bool
constexpr bool, constexpr bool
Bug: v8:8137
Change-Id: I9576b337dc4008ac58b4625e77fef4e73bcdd6e3
Reviewed-on: https://chromium-review.googlesource.com/1215162
Reviewed-by: Daniel Clifford <danno@chromium.org>
Commit-Queue: Tobias Tebbi <tebbi@chromium.org>
Cr-Commit-Position: refs/heads/master@{#55750}
2018-09-08 16:16:08 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
@export
|
2021-09-30 15:12:46 +00:00
|
|
|
macro TestGenericOverload(implicit context: Context)(): void {
|
2020-05-12 13:28:29 +00:00
|
|
|
const xSmi: Smi = 5;
|
|
|
|
const xObject: Object = xSmi;
|
|
|
|
check(ExampleGenericOverload<Smi>(xSmi) == 6);
|
|
|
|
check(UnsafeCast<Smi>(ExampleGenericOverload<Object>(xObject)) == 5);
|
|
|
|
}
|
[torque] disallow using logical operators in value contexts
This CL makes sure, that logical operators (||, &&) always have return
type never. Together with a check that never is never passed as a
function argument, this prevents faulty evaluation as in !(x || y).
Before, the logical operators had a behavior similar to
(bool labels Taken, NotTaken), with a fast exit if the left-hand side
allowed shor-circuit evaluation, but returning the right-hand side
otherwise. Since we want to allow existing (a || b || c) patterns in
the codebase, this requires weakening the restriction that the left-
and right-hand side need to have the same type. Now the possibilites
are:
bool, never
never, bool
never, never
bool, bool
constexpr bool, constexpr bool
Bug: v8:8137
Change-Id: I9576b337dc4008ac58b4625e77fef4e73bcdd6e3
Reviewed-on: https://chromium-review.googlesource.com/1215162
Reviewed-by: Daniel Clifford <danno@chromium.org>
Commit-Queue: Tobias Tebbi <tebbi@chromium.org>
Cr-Commit-Position: refs/heads/master@{#55750}
2018-09-08 16:16:08 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
@export
|
2021-09-30 15:12:46 +00:00
|
|
|
macro TestEquality(implicit context: Context)(): void {
|
2020-05-12 13:28:29 +00:00
|
|
|
const notEqual: bool =
|
|
|
|
AllocateHeapNumberWithValue(0.5) != AllocateHeapNumberWithValue(0.5);
|
|
|
|
check(!notEqual);
|
|
|
|
const equal: bool =
|
|
|
|
AllocateHeapNumberWithValue(0.5) == AllocateHeapNumberWithValue(0.5);
|
|
|
|
check(equal);
|
|
|
|
}
|
2018-10-08 13:50:46 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
@export
|
|
|
|
macro TestOrAnd(x: bool, y: bool, z: bool): bool {
|
|
|
|
return x || y && z ? true : false;
|
|
|
|
}
|
2018-10-08 13:50:46 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
@export
|
|
|
|
macro TestAndOr(x: bool, y: bool, z: bool): bool {
|
|
|
|
return x && y || z ? true : false;
|
|
|
|
}
|
2018-10-08 13:50:46 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
@export
|
2021-09-30 15:12:46 +00:00
|
|
|
macro TestLogicalOperators(): void {
|
2020-05-12 13:28:29 +00:00
|
|
|
check(TestAndOr(true, true, true));
|
|
|
|
check(TestAndOr(true, true, false));
|
|
|
|
check(TestAndOr(true, false, true));
|
|
|
|
check(!TestAndOr(true, false, false));
|
|
|
|
check(TestAndOr(false, true, true));
|
|
|
|
check(!TestAndOr(false, true, false));
|
|
|
|
check(TestAndOr(false, false, true));
|
|
|
|
check(!TestAndOr(false, false, false));
|
|
|
|
check(TestOrAnd(true, true, true));
|
|
|
|
check(TestOrAnd(true, true, false));
|
|
|
|
check(TestOrAnd(true, false, true));
|
|
|
|
check(TestOrAnd(true, false, false));
|
|
|
|
check(TestOrAnd(false, true, true));
|
|
|
|
check(!TestOrAnd(false, true, false));
|
|
|
|
check(!TestOrAnd(false, false, true));
|
|
|
|
check(!TestOrAnd(false, false, false));
|
|
|
|
}
|
2018-10-08 13:50:46 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
@export
|
|
|
|
macro TestCall(i: Smi): Smi labels A {
|
|
|
|
if (i < 5) return i;
|
|
|
|
goto A;
|
|
|
|
}
|
2018-10-08 13:50:46 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
@export
|
2021-09-30 15:12:46 +00:00
|
|
|
macro TestOtherwiseWithCode1(): void {
|
2020-05-12 13:28:29 +00:00
|
|
|
let v: Smi = 0;
|
|
|
|
let s: Smi = 1;
|
|
|
|
try {
|
|
|
|
TestCall(10) otherwise goto B(++s);
|
|
|
|
} label B(v1: Smi) {
|
|
|
|
v = v1;
|
2018-10-08 13:50:46 +00:00
|
|
|
}
|
2021-09-30 07:26:47 +00:00
|
|
|
dcheck(v == 2);
|
2020-05-12 13:28:29 +00:00
|
|
|
}
|
2018-10-31 13:00:51 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
@export
|
2021-09-30 15:12:46 +00:00
|
|
|
macro TestOtherwiseWithCode2(): void {
|
2020-05-12 13:28:29 +00:00
|
|
|
let s: Smi = 0;
|
|
|
|
for (let i: Smi = 0; i < 10; ++i) {
|
|
|
|
TestCall(i) otherwise break;
|
|
|
|
++s;
|
2018-11-02 12:40:31 +00:00
|
|
|
}
|
2021-09-30 07:26:47 +00:00
|
|
|
dcheck(s == 5);
|
2020-05-12 13:28:29 +00:00
|
|
|
}
|
2018-11-02 12:40:31 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
@export
|
2021-09-30 15:12:46 +00:00
|
|
|
macro TestOtherwiseWithCode3(): void {
|
2020-05-12 13:28:29 +00:00
|
|
|
let s: Smi = 0;
|
|
|
|
for (let i: Smi = 0; i < 10; ++i) {
|
|
|
|
s += TestCall(i) otherwise break;
|
2018-10-31 13:00:51 +00:00
|
|
|
}
|
2021-09-30 07:26:47 +00:00
|
|
|
dcheck(s == 10);
|
2020-05-12 13:28:29 +00:00
|
|
|
}
|
2018-10-31 13:00:51 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
@export
|
2021-09-30 15:12:46 +00:00
|
|
|
macro TestForwardLabel(): void {
|
2020-05-12 13:28:29 +00:00
|
|
|
try {
|
|
|
|
goto A;
|
|
|
|
} label A {
|
|
|
|
goto B(5);
|
|
|
|
} label B(b: Smi) {
|
2021-09-30 07:26:47 +00:00
|
|
|
dcheck(b == 5);
|
2018-10-31 13:00:51 +00:00
|
|
|
}
|
2020-05-12 13:28:29 +00:00
|
|
|
}
|
2018-10-31 13:00:51 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
@export
|
2021-09-30 15:12:46 +00:00
|
|
|
macro TestQualifiedAccess(implicit context: Context)(): void {
|
2020-05-12 13:28:29 +00:00
|
|
|
const s: Smi = 0;
|
|
|
|
check(!Is<JSArray>(s));
|
|
|
|
}
|
2018-10-31 13:00:51 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
@export
|
|
|
|
macro TestCatch1(implicit context: Context)(): Smi {
|
|
|
|
let r: Smi = 0;
|
|
|
|
try {
|
2019-12-18 15:33:16 +00:00
|
|
|
ThrowTypeError(MessageTemplate::kInvalidArrayLength);
|
2021-12-06 21:32:23 +00:00
|
|
|
} catch (_e, _message) {
|
2020-05-12 13:28:29 +00:00
|
|
|
r = 1;
|
|
|
|
return r;
|
2018-10-31 13:00:51 +00:00
|
|
|
}
|
2020-05-12 13:28:29 +00:00
|
|
|
}
|
2018-10-31 13:00:51 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
@export
|
|
|
|
macro TestCatch2Wrapper(implicit context: Context)(): never {
|
|
|
|
ThrowTypeError(MessageTemplate::kInvalidArrayLength);
|
|
|
|
}
|
|
|
|
|
|
|
|
@export
|
|
|
|
macro TestCatch2(implicit context: Context)(): Smi {
|
|
|
|
let r: Smi = 0;
|
|
|
|
try {
|
|
|
|
TestCatch2Wrapper();
|
2021-12-06 21:32:23 +00:00
|
|
|
} catch (_e, _message) {
|
2020-05-12 13:28:29 +00:00
|
|
|
r = 2;
|
|
|
|
return r;
|
2018-10-31 13:00:51 +00:00
|
|
|
}
|
2020-05-12 13:28:29 +00:00
|
|
|
}
|
2018-10-31 14:59:13 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
@export
|
|
|
|
macro TestCatch3WrapperWithLabel(implicit context: Context)():
|
|
|
|
never labels _Abort {
|
|
|
|
ThrowTypeError(MessageTemplate::kInvalidArrayLength);
|
|
|
|
}
|
2018-10-31 14:59:13 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
@export
|
|
|
|
macro TestCatch3(implicit context: Context)(): Smi {
|
|
|
|
let r: Smi = 0;
|
|
|
|
try {
|
|
|
|
TestCatch3WrapperWithLabel() otherwise Abort;
|
2021-12-06 21:32:23 +00:00
|
|
|
} catch (_e, _message) {
|
2020-05-12 13:28:29 +00:00
|
|
|
r = 2;
|
|
|
|
return r;
|
|
|
|
} label Abort {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2018-10-31 14:59:13 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
// This test doesn't actually test the functionality of iterators,
|
|
|
|
// it's only purpose is to make sure tha the CSA macros in the
|
|
|
|
// IteratorBuiltinsAssembler match the signatures provided in
|
|
|
|
// iterator.tq.
|
|
|
|
@export
|
|
|
|
transitioning macro TestIterator(implicit context: Context)(
|
2021-09-30 15:12:46 +00:00
|
|
|
o: JSReceiver, map: Map): void {
|
2020-05-12 13:28:29 +00:00
|
|
|
try {
|
|
|
|
const t1: JSAny = iterator::GetIteratorMethod(o);
|
|
|
|
const t2: iterator::IteratorRecord = iterator::GetIterator(o);
|
2018-10-31 14:59:13 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
const _t3: JSAny = iterator::IteratorStep(t2) otherwise Fail;
|
|
|
|
const _t4: JSAny = iterator::IteratorStep(t2, map) otherwise Fail;
|
2018-11-09 14:34:54 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
const _t5: JSAny = iterator::IteratorValue(o);
|
|
|
|
const _t6: JSAny = iterator::IteratorValue(o, map);
|
2018-12-07 16:29:49 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
const _t7: JSArray = iterator::IterableToList(t1, t1);
|
2018-12-07 16:29:49 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
iterator::IteratorCloseOnException(t2);
|
|
|
|
} label Fail {}
|
|
|
|
}
|
[torque] Implement methods and constructors for structs and classes
With the changes in this patch, it is now possible to add methods to
both Torque's class and struct types. As a special case, "constructor"
methods are used to initialize the values of classes and structs when
they are constructed.
The functionality in this patch includes:
- The refactoring of class- and struct-handling code to share field
and method declaration code between both.
- Addition of the "%Allocate" intrinsic that allocates raw bytes to be
allocated from the V8 GC's NewSpace heap as the basis for freshly
created, initialized class objects.
- An implementation of a CallMethodExpression AST node that enables
calling methods and constructors, including special handling of
passing through the "this" pointer for method calls on structs by
reference. The syntax for struct construction using "{}" remains as
before, but now calls the struct's matching constructor rather than
implicitly initializing the struct fields with the initialization
arguments. A new syntax for allocation classes is introduced: "new
ClassName{constructor_param1, constructor_param1, ...}", which
de-sugars to an %Allocate call followed by a call to the matching
constructor.
- class constructors can use the "super" keyword to initialize their
super class.
- If classes and struct do not have a constructor, Torque creates a
default constructor for them based on their field declarations,
where each field's initial value is assigned to a same-typed
parameter to the the default constructor. The default constructor's
parameters are in field-declaration order, and for derived classes,
the default constructor automatically uses a "super" initialization
call to initialize inherited fields.
- Class field declarations now automatically create ".field" and
".field=" operators that create CSA-compatible object accessors.
- Addition of a no-argument constructor for JSArrays that creates an
empty, PACKED_SMI_ELEMENTS JSArray using the machinery added
elsewhere in this patch.
Bug: v8:7793
Change-Id: I31ce5f4b444656ab999555d780aeeba605666bfa
Reviewed-on: https://chromium-review.googlesource.com/c/1392192
Commit-Queue: Daniel Clifford <danno@chromium.org>
Reviewed-by: Tobias Tebbi <tebbi@chromium.org>
Cr-Commit-Position: refs/heads/master@{#58860}
2019-01-16 16:25:29 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
@export
|
2021-09-30 15:12:46 +00:00
|
|
|
macro TestFrame1(implicit context: Context)(): void {
|
2020-05-12 13:28:29 +00:00
|
|
|
const f: Frame = LoadFramePointer();
|
|
|
|
const frameType: FrameType =
|
|
|
|
Cast<FrameType>(f.context_or_frame_type) otherwise unreachable;
|
2021-09-30 07:26:47 +00:00
|
|
|
dcheck(frameType == STUB_FRAME);
|
|
|
|
dcheck(f.caller == LoadParentFramePointer());
|
2020-05-12 13:28:29 +00:00
|
|
|
typeswitch (f) {
|
|
|
|
case (_f: StandardFrame): {
|
|
|
|
unreachable;
|
|
|
|
}
|
|
|
|
case (_f: StubFrame): {
|
[torque] Implement methods and constructors for structs and classes
With the changes in this patch, it is now possible to add methods to
both Torque's class and struct types. As a special case, "constructor"
methods are used to initialize the values of classes and structs when
they are constructed.
The functionality in this patch includes:
- The refactoring of class- and struct-handling code to share field
and method declaration code between both.
- Addition of the "%Allocate" intrinsic that allocates raw bytes to be
allocated from the V8 GC's NewSpace heap as the basis for freshly
created, initialized class objects.
- An implementation of a CallMethodExpression AST node that enables
calling methods and constructors, including special handling of
passing through the "this" pointer for method calls on structs by
reference. The syntax for struct construction using "{}" remains as
before, but now calls the struct's matching constructor rather than
implicitly initializing the struct fields with the initialization
arguments. A new syntax for allocation classes is introduced: "new
ClassName{constructor_param1, constructor_param1, ...}", which
de-sugars to an %Allocate call followed by a call to the matching
constructor.
- class constructors can use the "super" keyword to initialize their
super class.
- If classes and struct do not have a constructor, Torque creates a
default constructor for them based on their field declarations,
where each field's initial value is assigned to a same-typed
parameter to the the default constructor. The default constructor's
parameters are in field-declaration order, and for derived classes,
the default constructor automatically uses a "super" initialization
call to initialize inherited fields.
- Class field declarations now automatically create ".field" and
".field=" operators that create CSA-compatible object accessors.
- Addition of a no-argument constructor for JSArrays that creates an
empty, PACKED_SMI_ELEMENTS JSArray using the machinery added
elsewhere in this patch.
Bug: v8:7793
Change-Id: I31ce5f4b444656ab999555d780aeeba605666bfa
Reviewed-on: https://chromium-review.googlesource.com/c/1392192
Commit-Queue: Daniel Clifford <danno@chromium.org>
Reviewed-by: Tobias Tebbi <tebbi@chromium.org>
Cr-Commit-Position: refs/heads/master@{#58860}
2019-01-16 16:25:29 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-12 13:28:29 +00:00
|
|
|
}
|
[torque] Implement methods and constructors for structs and classes
With the changes in this patch, it is now possible to add methods to
both Torque's class and struct types. As a special case, "constructor"
methods are used to initialize the values of classes and structs when
they are constructed.
The functionality in this patch includes:
- The refactoring of class- and struct-handling code to share field
and method declaration code between both.
- Addition of the "%Allocate" intrinsic that allocates raw bytes to be
allocated from the V8 GC's NewSpace heap as the basis for freshly
created, initialized class objects.
- An implementation of a CallMethodExpression AST node that enables
calling methods and constructors, including special handling of
passing through the "this" pointer for method calls on structs by
reference. The syntax for struct construction using "{}" remains as
before, but now calls the struct's matching constructor rather than
implicitly initializing the struct fields with the initialization
arguments. A new syntax for allocation classes is introduced: "new
ClassName{constructor_param1, constructor_param1, ...}", which
de-sugars to an %Allocate call followed by a call to the matching
constructor.
- class constructors can use the "super" keyword to initialize their
super class.
- If classes and struct do not have a constructor, Torque creates a
default constructor for them based on their field declarations,
where each field's initial value is assigned to a same-typed
parameter to the the default constructor. The default constructor's
parameters are in field-declaration order, and for derived classes,
the default constructor automatically uses a "super" initialization
call to initialize inherited fields.
- Class field declarations now automatically create ".field" and
".field=" operators that create CSA-compatible object accessors.
- Addition of a no-argument constructor for JSArrays that creates an
empty, PACKED_SMI_ELEMENTS JSArray using the machinery added
elsewhere in this patch.
Bug: v8:7793
Change-Id: I31ce5f4b444656ab999555d780aeeba605666bfa
Reviewed-on: https://chromium-review.googlesource.com/c/1392192
Commit-Queue: Daniel Clifford <danno@chromium.org>
Reviewed-by: Tobias Tebbi <tebbi@chromium.org>
Cr-Commit-Position: refs/heads/master@{#58860}
2019-01-16 16:25:29 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
@export
|
2021-09-30 15:12:46 +00:00
|
|
|
macro TestNew(implicit context: Context)(): void {
|
2020-05-12 13:28:29 +00:00
|
|
|
const f: JSArray = NewJSArray();
|
|
|
|
check(f.IsEmpty());
|
|
|
|
f.length = 0;
|
|
|
|
}
|
[torque] Implement methods and constructors for structs and classes
With the changes in this patch, it is now possible to add methods to
both Torque's class and struct types. As a special case, "constructor"
methods are used to initialize the values of classes and structs when
they are constructed.
The functionality in this patch includes:
- The refactoring of class- and struct-handling code to share field
and method declaration code between both.
- Addition of the "%Allocate" intrinsic that allocates raw bytes to be
allocated from the V8 GC's NewSpace heap as the basis for freshly
created, initialized class objects.
- An implementation of a CallMethodExpression AST node that enables
calling methods and constructors, including special handling of
passing through the "this" pointer for method calls on structs by
reference. The syntax for struct construction using "{}" remains as
before, but now calls the struct's matching constructor rather than
implicitly initializing the struct fields with the initialization
arguments. A new syntax for allocation classes is introduced: "new
ClassName{constructor_param1, constructor_param1, ...}", which
de-sugars to an %Allocate call followed by a call to the matching
constructor.
- class constructors can use the "super" keyword to initialize their
super class.
- If classes and struct do not have a constructor, Torque creates a
default constructor for them based on their field declarations,
where each field's initial value is assigned to a same-typed
parameter to the the default constructor. The default constructor's
parameters are in field-declaration order, and for derived classes,
the default constructor automatically uses a "super" initialization
call to initialize inherited fields.
- Class field declarations now automatically create ".field" and
".field=" operators that create CSA-compatible object accessors.
- Addition of a no-argument constructor for JSArrays that creates an
empty, PACKED_SMI_ELEMENTS JSArray using the machinery added
elsewhere in this patch.
Bug: v8:7793
Change-Id: I31ce5f4b444656ab999555d780aeeba605666bfa
Reviewed-on: https://chromium-review.googlesource.com/c/1392192
Commit-Queue: Daniel Clifford <danno@chromium.org>
Reviewed-by: Tobias Tebbi <tebbi@chromium.org>
Cr-Commit-Position: refs/heads/master@{#58860}
2019-01-16 16:25:29 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
struct TestInner {
|
2021-09-30 15:12:46 +00:00
|
|
|
macro SetX(newValue: int32): void {
|
2020-05-12 13:28:29 +00:00
|
|
|
this.x = newValue;
|
[torque] Implement methods and constructors for structs and classes
With the changes in this patch, it is now possible to add methods to
both Torque's class and struct types. As a special case, "constructor"
methods are used to initialize the values of classes and structs when
they are constructed.
The functionality in this patch includes:
- The refactoring of class- and struct-handling code to share field
and method declaration code between both.
- Addition of the "%Allocate" intrinsic that allocates raw bytes to be
allocated from the V8 GC's NewSpace heap as the basis for freshly
created, initialized class objects.
- An implementation of a CallMethodExpression AST node that enables
calling methods and constructors, including special handling of
passing through the "this" pointer for method calls on structs by
reference. The syntax for struct construction using "{}" remains as
before, but now calls the struct's matching constructor rather than
implicitly initializing the struct fields with the initialization
arguments. A new syntax for allocation classes is introduced: "new
ClassName{constructor_param1, constructor_param1, ...}", which
de-sugars to an %Allocate call followed by a call to the matching
constructor.
- class constructors can use the "super" keyword to initialize their
super class.
- If classes and struct do not have a constructor, Torque creates a
default constructor for them based on their field declarations,
where each field's initial value is assigned to a same-typed
parameter to the the default constructor. The default constructor's
parameters are in field-declaration order, and for derived classes,
the default constructor automatically uses a "super" initialization
call to initialize inherited fields.
- Class field declarations now automatically create ".field" and
".field=" operators that create CSA-compatible object accessors.
- Addition of a no-argument constructor for JSArrays that creates an
empty, PACKED_SMI_ELEMENTS JSArray using the machinery added
elsewhere in this patch.
Bug: v8:7793
Change-Id: I31ce5f4b444656ab999555d780aeeba605666bfa
Reviewed-on: https://chromium-review.googlesource.com/c/1392192
Commit-Queue: Daniel Clifford <danno@chromium.org>
Reviewed-by: Tobias Tebbi <tebbi@chromium.org>
Cr-Commit-Position: refs/heads/master@{#58860}
2019-01-16 16:25:29 +00:00
|
|
|
}
|
2020-05-12 13:28:29 +00:00
|
|
|
macro GetX(): int32 {
|
|
|
|
return this.x;
|
2019-02-05 12:39:44 +00:00
|
|
|
}
|
2020-05-12 13:28:29 +00:00
|
|
|
x: int32;
|
|
|
|
y: int32;
|
|
|
|
}
|
2019-02-05 12:39:44 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
struct TestOuter {
|
|
|
|
a: int32;
|
|
|
|
b: TestInner;
|
|
|
|
c: int32;
|
|
|
|
}
|
2019-02-27 16:42:45 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
@export
|
2021-09-30 15:12:46 +00:00
|
|
|
macro TestStructConstructor(implicit context: Context)(): void {
|
2020-05-12 13:28:29 +00:00
|
|
|
// Test default constructor
|
|
|
|
let a: TestOuter = TestOuter{a: 5, b: TestInner{x: 6, y: 7}, c: 8};
|
|
|
|
check(a.a == 5);
|
|
|
|
check(a.b.x == 6);
|
|
|
|
check(a.b.y == 7);
|
|
|
|
check(a.c == 8);
|
|
|
|
a.b.x = 1;
|
|
|
|
check(a.b.x == 1);
|
|
|
|
a.b.SetX(2);
|
|
|
|
check(a.b.x == 2);
|
|
|
|
check(a.b.GetX() == 2);
|
|
|
|
}
|
2019-04-03 10:22:54 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
class InternalClass extends HeapObject {
|
2021-09-30 15:12:46 +00:00
|
|
|
macro Flip(): void labels NotASmi {
|
2020-05-12 13:28:29 +00:00
|
|
|
const tmp = Cast<Smi>(this.b) otherwise NotASmi;
|
|
|
|
this.b = this.a;
|
|
|
|
this.a = tmp;
|
2019-04-03 10:22:54 +00:00
|
|
|
}
|
2020-05-12 13:28:29 +00:00
|
|
|
a: Smi;
|
|
|
|
b: Number;
|
|
|
|
}
|
2019-04-03 10:22:54 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
macro NewInternalClass(x: Smi): InternalClass {
|
|
|
|
return new InternalClass{a: x, b: x + 1};
|
|
|
|
}
|
2019-06-11 12:26:32 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
@export
|
2021-09-30 15:12:46 +00:00
|
|
|
macro TestInternalClass(implicit context: Context)(): void {
|
2020-05-12 13:28:29 +00:00
|
|
|
const o = NewInternalClass(5);
|
|
|
|
o.Flip() otherwise unreachable;
|
|
|
|
check(o.a == 6);
|
|
|
|
check(o.b == 5);
|
|
|
|
}
|
2019-04-04 17:23:05 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
struct StructWithConst {
|
|
|
|
macro TestMethod1(): int32 {
|
|
|
|
return this.b;
|
2019-12-19 08:55:02 +00:00
|
|
|
}
|
2020-05-12 13:28:29 +00:00
|
|
|
macro TestMethod2(): Object {
|
|
|
|
return this.a;
|
2019-04-04 17:23:05 +00:00
|
|
|
}
|
2020-05-12 13:28:29 +00:00
|
|
|
a: Object;
|
|
|
|
const b: int32;
|
|
|
|
}
|
2019-04-04 17:23:05 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
@export
|
2021-09-30 15:12:46 +00:00
|
|
|
macro TestConstInStructs(): void {
|
2020-05-12 13:28:29 +00:00
|
|
|
const x = StructWithConst{a: Null, b: 1};
|
|
|
|
let y = StructWithConst{a: Null, b: 1};
|
|
|
|
y.a = Undefined;
|
|
|
|
const _copy = x;
|
2019-04-11 13:36:05 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
check(x.TestMethod1() == 1);
|
|
|
|
check(x.TestMethod2() == Null);
|
|
|
|
}
|
2019-04-11 13:36:05 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
@export
|
2021-09-30 15:12:46 +00:00
|
|
|
macro TestParentFrameArguments(implicit context: Context)(): void {
|
2020-05-12 13:28:29 +00:00
|
|
|
const parentFrame = LoadParentFramePointer();
|
|
|
|
const castFrame = Cast<StandardFrame>(parentFrame) otherwise unreachable;
|
|
|
|
const arguments = GetFrameArguments(castFrame, 1);
|
|
|
|
ArgumentsIterator{arguments, current: 0};
|
|
|
|
}
|
2019-04-11 13:36:05 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
struct TestIterator {
|
|
|
|
macro Next(): Object labels NoMore {
|
|
|
|
if (this.count-- == 0) goto NoMore;
|
|
|
|
return TheHole;
|
2019-04-11 13:36:05 +00:00
|
|
|
}
|
2020-05-12 13:28:29 +00:00
|
|
|
count: Smi;
|
|
|
|
}
|
2019-05-07 08:38:44 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
@export
|
|
|
|
macro TestNewFixedArrayFromSpread(implicit context: Context)(): Object {
|
|
|
|
let i = TestIterator{count: 5};
|
|
|
|
return new FixedArray{map: kFixedArrayMap, length: 5, objects: ...i};
|
|
|
|
}
|
2019-08-29 15:30:47 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
class SmiPair extends HeapObject {
|
|
|
|
macro GetA():&Smi {
|
2020-07-16 09:35:18 +00:00
|
|
|
return &this.a;
|
2020-05-12 13:28:29 +00:00
|
|
|
}
|
|
|
|
a: Smi;
|
|
|
|
b: Smi;
|
|
|
|
}
|
2019-08-29 15:30:47 +00:00
|
|
|
|
2021-09-30 15:12:46 +00:00
|
|
|
macro Swap<T: type>(a:&T, b:&T): void {
|
2020-07-16 09:35:18 +00:00
|
|
|
const tmp = *a;
|
|
|
|
*a = *b;
|
|
|
|
*b = tmp;
|
2020-05-12 13:28:29 +00:00
|
|
|
}
|
2019-08-29 15:30:47 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
@export
|
2021-09-30 15:12:46 +00:00
|
|
|
macro TestReferences(): void {
|
2020-05-12 13:28:29 +00:00
|
|
|
const array = new SmiPair{a: 7, b: 2};
|
2020-07-16 09:35:18 +00:00
|
|
|
const ref:&Smi = &array.a;
|
|
|
|
*ref = 3 + *ref;
|
|
|
|
-- *ref;
|
|
|
|
Swap(&array.b, array.GetA());
|
2020-05-12 13:28:29 +00:00
|
|
|
check(array.a == 2);
|
|
|
|
check(array.b == 9);
|
|
|
|
}
|
2019-08-29 15:30:47 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
@export
|
2021-09-30 15:12:46 +00:00
|
|
|
macro TestSlices(): void {
|
2020-05-12 13:28:29 +00:00
|
|
|
const it = TestIterator{count: 3};
|
|
|
|
const a = new FixedArray{map: kFixedArrayMap, length: 3, objects: ...it};
|
|
|
|
check(a.length == 3);
|
2019-08-29 15:30:47 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
const oneTwoThree = Convert<Smi>(123);
|
|
|
|
a.objects[0] = oneTwoThree;
|
2020-07-16 09:35:18 +00:00
|
|
|
const firstRef:&Object = &a.objects[0];
|
|
|
|
check(TaggedEqual(*firstRef, oneTwoThree));
|
2019-08-29 15:30:47 +00:00
|
|
|
|
2020-11-25 09:04:52 +00:00
|
|
|
const slice: MutableSlice<Object> = &a.objects;
|
2020-05-12 13:28:29 +00:00
|
|
|
const firstRefAgain:&Object = slice.TryAtIndex(0) otherwise unreachable;
|
2020-07-16 09:35:18 +00:00
|
|
|
check(TaggedEqual(*firstRefAgain, oneTwoThree));
|
[torque] Add user-defined Slice struct
This CL consists of several preparatory steps for slices in Torque. Above all, it introduces a user-defined struct, torque_internal::Slice<T>, that performs bounds checking and returns references to elements in arrays. To enable this, several smaller changes were also made:
- Constructors of internal classes such as torque_internal::Reference<T> now require a special 'Unsafe' argument, making it clear that there be dragons.
- Struct methods are now declared during finalization. This allows instances of generic structs to have methods referring to the same struct. Previously, methods would be declared before the instance had been fully registered, leading to errors during type resolution. Furthermore, such methods were declared in a temporary namespace, that would then erroneously escape and lead to use-after-free issues.
- Instances of TypeArgumentInference were not running in the correct (Torque) scopes, leading to type resolution errors.
- The chain of ContextualVariable::Scope for any given ContextualVariable (such as CurrentScope) can now be walked, simplifying debugging.
R=jgruber@chromium.org, tebbi@chromium.org
Bug: v8:7793
Change-Id: I36f808f63cc3ce441062dfc56f511f24f1e3121e
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1758322
Commit-Queue: Georg Schmid <gsps@google.com>
Reviewed-by: Jakob Gruber <jgruber@chromium.org>
Reviewed-by: Tobias Tebbi <tebbi@chromium.org>
Cr-Commit-Position: refs/heads/master@{#63314}
2019-08-21 11:47:44 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
const threeTwoOne = Convert<Smi>(321);
|
2020-07-16 09:35:18 +00:00
|
|
|
*firstRefAgain = threeTwoOne;
|
2020-05-12 13:28:29 +00:00
|
|
|
check(TaggedEqual(a.objects[0], threeTwoOne));
|
[torque] Add user-defined Slice struct
This CL consists of several preparatory steps for slices in Torque. Above all, it introduces a user-defined struct, torque_internal::Slice<T>, that performs bounds checking and returns references to elements in arrays. To enable this, several smaller changes were also made:
- Constructors of internal classes such as torque_internal::Reference<T> now require a special 'Unsafe' argument, making it clear that there be dragons.
- Struct methods are now declared during finalization. This allows instances of generic structs to have methods referring to the same struct. Previously, methods would be declared before the instance had been fully registered, leading to errors during type resolution. Furthermore, such methods were declared in a temporary namespace, that would then erroneously escape and lead to use-after-free issues.
- Instances of TypeArgumentInference were not running in the correct (Torque) scopes, leading to type resolution errors.
- The chain of ContextualVariable::Scope for any given ContextualVariable (such as CurrentScope) can now be walked, simplifying debugging.
R=jgruber@chromium.org, tebbi@chromium.org
Bug: v8:7793
Change-Id: I36f808f63cc3ce441062dfc56f511f24f1e3121e
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1758322
Commit-Queue: Georg Schmid <gsps@google.com>
Reviewed-by: Jakob Gruber <jgruber@chromium.org>
Reviewed-by: Tobias Tebbi <tebbi@chromium.org>
Cr-Commit-Position: refs/heads/master@{#63314}
2019-08-21 11:47:44 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
// *slice; // error, not allowed
|
|
|
|
// a.objects; // error, not allowed
|
|
|
|
// a.objects = slice; // error, not allowed
|
2019-08-29 15:30:47 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
// TODO(gsps): Currently errors, but should be allowed:
|
2020-11-25 09:04:52 +00:00
|
|
|
// const _sameSlice: MutableSlice<Object> = &(*slice);
|
2020-05-12 13:28:29 +00:00
|
|
|
// (*slice)[0] : Smi
|
|
|
|
}
|
[torque] Add user-defined Slice struct
This CL consists of several preparatory steps for slices in Torque. Above all, it introduces a user-defined struct, torque_internal::Slice<T>, that performs bounds checking and returns references to elements in arrays. To enable this, several smaller changes were also made:
- Constructors of internal classes such as torque_internal::Reference<T> now require a special 'Unsafe' argument, making it clear that there be dragons.
- Struct methods are now declared during finalization. This allows instances of generic structs to have methods referring to the same struct. Previously, methods would be declared before the instance had been fully registered, leading to errors during type resolution. Furthermore, such methods were declared in a temporary namespace, that would then erroneously escape and lead to use-after-free issues.
- Instances of TypeArgumentInference were not running in the correct (Torque) scopes, leading to type resolution errors.
- The chain of ContextualVariable::Scope for any given ContextualVariable (such as CurrentScope) can now be walked, simplifying debugging.
R=jgruber@chromium.org, tebbi@chromium.org
Bug: v8:7793
Change-Id: I36f808f63cc3ce441062dfc56f511f24f1e3121e
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1758322
Commit-Queue: Georg Schmid <gsps@google.com>
Reviewed-by: Jakob Gruber <jgruber@chromium.org>
Reviewed-by: Tobias Tebbi <tebbi@chromium.org>
Cr-Commit-Position: refs/heads/master@{#63314}
2019-08-21 11:47:44 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
@export
|
|
|
|
macro TestSliceEnumeration(implicit context: Context)(): Undefined {
|
|
|
|
const fixedArray: FixedArray = AllocateZeroedFixedArray(3);
|
|
|
|
for (let i: intptr = 0; i < 3; i++) {
|
|
|
|
check(UnsafeCast<Smi>(fixedArray.objects[i]) == 0);
|
|
|
|
fixedArray.objects[i] = Convert<Smi>(i) + 3;
|
2019-05-21 14:59:31 +00:00
|
|
|
}
|
|
|
|
|
2020-07-16 09:35:18 +00:00
|
|
|
let slice = &fixedArray.objects;
|
2020-05-12 13:28:29 +00:00
|
|
|
for (let i: intptr = 0; i < slice.length; i++) {
|
|
|
|
let ref = slice.TryAtIndex(i) otherwise unreachable;
|
2020-07-16 09:35:18 +00:00
|
|
|
const value = UnsafeCast<Smi>(*ref);
|
2020-05-12 13:28:29 +00:00
|
|
|
check(value == Convert<Smi>(i) + 3);
|
2020-07-16 09:35:18 +00:00
|
|
|
*ref = value + 4;
|
2019-06-04 14:19:40 +00:00
|
|
|
}
|
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
let it = slice.Iterator();
|
|
|
|
let count: Smi = 0;
|
|
|
|
while (true) {
|
|
|
|
const value = UnsafeCast<Smi>(it.Next() otherwise break);
|
|
|
|
check(value == count + 7);
|
|
|
|
count++;
|
2019-06-04 14:19:40 +00:00
|
|
|
}
|
2020-05-12 13:28:29 +00:00
|
|
|
check(count == 3);
|
|
|
|
check(it.Empty());
|
2019-06-04 14:19:40 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
return Undefined;
|
|
|
|
}
|
2019-06-12 11:30:05 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
@export
|
2021-09-30 15:12:46 +00:00
|
|
|
macro TestStaticAssert(): void {
|
2020-07-27 10:35:36 +00:00
|
|
|
static_assert(1 + 2 == 3);
|
2020-07-31 16:41:39 +00:00
|
|
|
|
|
|
|
static_assert(Convert<uintptr>(5) < Convert<uintptr>(6));
|
|
|
|
static_assert(!(Convert<uintptr>(5) < Convert<uintptr>(5)));
|
|
|
|
static_assert(!(Convert<uintptr>(6) < Convert<uintptr>(5)));
|
|
|
|
static_assert(Convert<uintptr>(5) <= Convert<uintptr>(5));
|
|
|
|
static_assert(Convert<uintptr>(5) <= Convert<uintptr>(6));
|
|
|
|
static_assert(!(Convert<uintptr>(6) <= Convert<uintptr>(5)));
|
|
|
|
|
|
|
|
static_assert(Convert<intptr>(-6) < Convert<intptr>(-5));
|
|
|
|
static_assert(!(Convert<intptr>(-5) < Convert<intptr>(-5)));
|
|
|
|
static_assert(!(Convert<intptr>(-5) < Convert<intptr>(-6)));
|
|
|
|
static_assert(Convert<intptr>(-5) <= Convert<intptr>(-5));
|
|
|
|
static_assert(Convert<intptr>(-6) <= Convert<intptr>(-5));
|
|
|
|
static_assert(!(Convert<intptr>(-5) <= Convert<intptr>(-6)));
|
2020-05-12 13:28:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class SmiBox extends HeapObject {
|
|
|
|
value: Smi;
|
|
|
|
unrelated: Smi;
|
|
|
|
}
|
|
|
|
|
|
|
|
builtin NewSmiBox(implicit context: Context)(value: Smi): SmiBox {
|
|
|
|
return new SmiBox{value, unrelated: 0};
|
|
|
|
}
|
|
|
|
|
|
|
|
@export
|
2021-09-30 15:12:46 +00:00
|
|
|
macro TestLoadEliminationFixed(implicit context: Context)(): void {
|
2020-05-12 13:28:29 +00:00
|
|
|
const box = NewSmiBox(123);
|
|
|
|
const v1 = box.value;
|
|
|
|
box.unrelated = 999;
|
|
|
|
const v2 = (box.unrelated == 0) ? box.value : box.value;
|
2020-07-27 10:35:36 +00:00
|
|
|
static_assert(TaggedEqual(v1, v2));
|
2020-05-12 13:28:29 +00:00
|
|
|
|
|
|
|
box.value = 11;
|
|
|
|
const v3 = box.value;
|
|
|
|
const eleven: Smi = 11;
|
2020-07-27 10:35:36 +00:00
|
|
|
static_assert(TaggedEqual(v3, eleven));
|
2020-05-12 13:28:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@export
|
2021-09-30 15:12:46 +00:00
|
|
|
macro TestLoadEliminationVariable(implicit context: Context)(): void {
|
2020-05-12 13:28:29 +00:00
|
|
|
const a = UnsafeCast<FixedArray>(kEmptyFixedArray);
|
|
|
|
const box = NewSmiBox(1);
|
|
|
|
const v1 = a.objects[box.value];
|
|
|
|
const u1 = a.objects[box.value + 2];
|
|
|
|
const v2 = a.objects[box.value];
|
|
|
|
const u2 = a.objects[box.value + 2];
|
2020-07-27 10:35:36 +00:00
|
|
|
static_assert(TaggedEqual(v1, v2));
|
|
|
|
static_assert(TaggedEqual(u1, u2));
|
2020-05-12 13:28:29 +00:00
|
|
|
}
|
2019-06-12 11:30:05 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
@export
|
|
|
|
macro TestRedundantArrayElementCheck(implicit context: Context)(): Smi {
|
|
|
|
const a = kEmptyFixedArray;
|
|
|
|
for (let i: Smi = 0; i < a.length; i++) {
|
|
|
|
if (a.objects[i] == TheHole) {
|
2019-06-17 19:37:46 +00:00
|
|
|
if (a.objects[i] == TheHole) {
|
2020-05-12 13:28:29 +00:00
|
|
|
return -1;
|
|
|
|
} else {
|
2020-07-27 10:35:36 +00:00
|
|
|
static_assert(false);
|
2019-06-17 08:18:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-12 13:28:29 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2019-06-17 08:18:27 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
@export
|
|
|
|
macro TestRedundantSmiCheck(implicit context: Context)(): Smi {
|
|
|
|
const a = kEmptyFixedArray;
|
|
|
|
const x = a.objects[1];
|
|
|
|
typeswitch (x) {
|
|
|
|
case (Smi): {
|
|
|
|
Cast<Smi>(x) otherwise VerifiedUnreachable();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
case (Object): {
|
2019-06-17 08:18:27 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-12 13:28:29 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2019-06-17 08:18:27 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
struct SBox<T: type> {
|
|
|
|
value: T;
|
|
|
|
}
|
[torque] Add Generic Structs
This CL introduces generic Torque structs. Generics are grounded early in the Torque compilation pipeline, meaning that every instantiation of a generic struct with concrete types will be turned into a distinct StructType.
As an example, consider a Tuple of types T1, T2:
struct Tuple<T1: type, T2: type> {
const fst: T1;
const snd: T2;
}
which can be manipulated using generic macros, such as
macro Swap<T1: type, T2: type>(tuple: Tuple<T1, T2>): Tuple<T2, T1> {
return Tuple<T2, T1>{fst: tuple.snd, snd: tuple.fst};
}
Currently there is no type inference for struct instantiation sites, so type arguments have to be provided explicitly:
const intptrAndSmi = Tuple<intptr, Smi>{fst: 1, snd: 2};
R=sigurds@chromium.org, tebbi@chromium.org
Change-Id: I43111561cbe53144db473dc844a478045644ef6c
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1714868
Commit-Queue: Georg Schmid <gsps@google.com>
Reviewed-by: Tobias Tebbi <tebbi@chromium.org>
Reviewed-by: Sigurd Schneider <sigurds@chromium.org>
Cr-Commit-Position: refs/heads/master@{#62878}
2019-07-23 14:55:01 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
@export
|
|
|
|
macro TestGenericStruct1(): intptr {
|
|
|
|
const i: intptr = 123;
|
|
|
|
let box = SBox{value: i};
|
|
|
|
let boxbox: SBox<SBox<intptr>> = SBox{value: box};
|
|
|
|
check(box.value == 123);
|
|
|
|
boxbox.value.value *= 2;
|
|
|
|
check(boxbox.value.value == 246);
|
|
|
|
return boxbox.value.value;
|
|
|
|
}
|
[torque] Add Generic Structs
This CL introduces generic Torque structs. Generics are grounded early in the Torque compilation pipeline, meaning that every instantiation of a generic struct with concrete types will be turned into a distinct StructType.
As an example, consider a Tuple of types T1, T2:
struct Tuple<T1: type, T2: type> {
const fst: T1;
const snd: T2;
}
which can be manipulated using generic macros, such as
macro Swap<T1: type, T2: type>(tuple: Tuple<T1, T2>): Tuple<T2, T1> {
return Tuple<T2, T1>{fst: tuple.snd, snd: tuple.fst};
}
Currently there is no type inference for struct instantiation sites, so type arguments have to be provided explicitly:
const intptrAndSmi = Tuple<intptr, Smi>{fst: 1, snd: 2};
R=sigurds@chromium.org, tebbi@chromium.org
Change-Id: I43111561cbe53144db473dc844a478045644ef6c
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1714868
Commit-Queue: Georg Schmid <gsps@google.com>
Reviewed-by: Tobias Tebbi <tebbi@chromium.org>
Reviewed-by: Sigurd Schneider <sigurds@chromium.org>
Cr-Commit-Position: refs/heads/master@{#62878}
2019-07-23 14:55:01 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
struct TestTuple<T1: type, T2: type> {
|
|
|
|
const fst: T1;
|
|
|
|
const snd: T2;
|
|
|
|
}
|
[torque] Add Generic Structs
This CL introduces generic Torque structs. Generics are grounded early in the Torque compilation pipeline, meaning that every instantiation of a generic struct with concrete types will be turned into a distinct StructType.
As an example, consider a Tuple of types T1, T2:
struct Tuple<T1: type, T2: type> {
const fst: T1;
const snd: T2;
}
which can be manipulated using generic macros, such as
macro Swap<T1: type, T2: type>(tuple: Tuple<T1, T2>): Tuple<T2, T1> {
return Tuple<T2, T1>{fst: tuple.snd, snd: tuple.fst};
}
Currently there is no type inference for struct instantiation sites, so type arguments have to be provided explicitly:
const intptrAndSmi = Tuple<intptr, Smi>{fst: 1, snd: 2};
R=sigurds@chromium.org, tebbi@chromium.org
Change-Id: I43111561cbe53144db473dc844a478045644ef6c
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1714868
Commit-Queue: Georg Schmid <gsps@google.com>
Reviewed-by: Tobias Tebbi <tebbi@chromium.org>
Reviewed-by: Sigurd Schneider <sigurds@chromium.org>
Cr-Commit-Position: refs/heads/master@{#62878}
2019-07-23 14:55:01 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
macro TupleSwap<T1: type, T2: type>(tuple: TestTuple<T1, T2>):
|
|
|
|
TestTuple<T2, T1> {
|
|
|
|
return TestTuple{fst: tuple.snd, snd: tuple.fst};
|
|
|
|
}
|
[torque] Add Generic Structs
This CL introduces generic Torque structs. Generics are grounded early in the Torque compilation pipeline, meaning that every instantiation of a generic struct with concrete types will be turned into a distinct StructType.
As an example, consider a Tuple of types T1, T2:
struct Tuple<T1: type, T2: type> {
const fst: T1;
const snd: T2;
}
which can be manipulated using generic macros, such as
macro Swap<T1: type, T2: type>(tuple: Tuple<T1, T2>): Tuple<T2, T1> {
return Tuple<T2, T1>{fst: tuple.snd, snd: tuple.fst};
}
Currently there is no type inference for struct instantiation sites, so type arguments have to be provided explicitly:
const intptrAndSmi = Tuple<intptr, Smi>{fst: 1, snd: 2};
R=sigurds@chromium.org, tebbi@chromium.org
Change-Id: I43111561cbe53144db473dc844a478045644ef6c
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1714868
Commit-Queue: Georg Schmid <gsps@google.com>
Reviewed-by: Tobias Tebbi <tebbi@chromium.org>
Reviewed-by: Sigurd Schneider <sigurds@chromium.org>
Cr-Commit-Position: refs/heads/master@{#62878}
2019-07-23 14:55:01 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
@export
|
|
|
|
macro TestGenericStruct2():
|
|
|
|
TestTuple<TestTuple<intptr, Smi>, TestTuple<Smi, intptr>> {
|
|
|
|
const intptrAndSmi = TestTuple<intptr, Smi>{fst: 1, snd: 2};
|
|
|
|
const smiAndIntptr = TupleSwap(intptrAndSmi);
|
|
|
|
check(intptrAndSmi.fst == smiAndIntptr.snd);
|
|
|
|
check(intptrAndSmi.snd == smiAndIntptr.fst);
|
|
|
|
const tupleTuple =
|
|
|
|
TestTuple<TestTuple<intptr, Smi>>{fst: intptrAndSmi, snd: smiAndIntptr};
|
|
|
|
return tupleTuple;
|
|
|
|
}
|
[torque] Add Generic Structs
This CL introduces generic Torque structs. Generics are grounded early in the Torque compilation pipeline, meaning that every instantiation of a generic struct with concrete types will be turned into a distinct StructType.
As an example, consider a Tuple of types T1, T2:
struct Tuple<T1: type, T2: type> {
const fst: T1;
const snd: T2;
}
which can be manipulated using generic macros, such as
macro Swap<T1: type, T2: type>(tuple: Tuple<T1, T2>): Tuple<T2, T1> {
return Tuple<T2, T1>{fst: tuple.snd, snd: tuple.fst};
}
Currently there is no type inference for struct instantiation sites, so type arguments have to be provided explicitly:
const intptrAndSmi = Tuple<intptr, Smi>{fst: 1, snd: 2};
R=sigurds@chromium.org, tebbi@chromium.org
Change-Id: I43111561cbe53144db473dc844a478045644ef6c
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1714868
Commit-Queue: Georg Schmid <gsps@google.com>
Reviewed-by: Tobias Tebbi <tebbi@chromium.org>
Reviewed-by: Sigurd Schneider <sigurds@chromium.org>
Cr-Commit-Position: refs/heads/master@{#62878}
2019-07-23 14:55:01 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
macro BranchAndWriteResult(x: Smi, box: SmiBox): bool {
|
|
|
|
if (x > 5 || x < 0) {
|
|
|
|
box.value = 1;
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
box.value = 2;
|
|
|
|
return false;
|
2019-07-29 17:29:29 +00:00
|
|
|
}
|
2020-05-12 13:28:29 +00:00
|
|
|
}
|
2019-07-29 17:29:29 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
@export
|
2021-09-30 15:12:46 +00:00
|
|
|
macro TestBranchOnBoolOptimization(implicit context: Context)(input: Smi):
|
|
|
|
void {
|
2020-05-12 13:28:29 +00:00
|
|
|
const box = NewSmiBox(1);
|
|
|
|
// If the two branches get combined into one, we should be able to determine
|
|
|
|
// the value of {box} statically.
|
|
|
|
if (BranchAndWriteResult(input, box)) {
|
2020-07-27 10:35:36 +00:00
|
|
|
static_assert(box.value == 1);
|
2020-05-12 13:28:29 +00:00
|
|
|
} else {
|
2020-07-27 10:35:36 +00:00
|
|
|
static_assert(box.value == 2);
|
2019-07-29 17:29:29 +00:00
|
|
|
}
|
2020-05-12 13:28:29 +00:00
|
|
|
}
|
2019-07-29 17:29:29 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
bitfield struct TestBitFieldStruct extends uint8 {
|
|
|
|
a: bool: 1 bit;
|
|
|
|
b: uint16: 3 bit;
|
|
|
|
c: uint32: 3 bit;
|
|
|
|
d: bool: 1 bit;
|
|
|
|
}
|
2019-12-12 20:49:36 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
@export
|
|
|
|
macro TestBitFieldLoad(
|
|
|
|
val: TestBitFieldStruct, expectedA: bool, expectedB: uint16,
|
2021-09-30 15:12:46 +00:00
|
|
|
expectedC: uint32, expectedD: bool): void {
|
2020-05-12 13:28:29 +00:00
|
|
|
check(val.a == expectedA);
|
|
|
|
check(val.b == expectedB);
|
|
|
|
check(val.c == expectedC);
|
|
|
|
check(val.d == expectedD);
|
|
|
|
}
|
2019-12-12 20:49:36 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
@export
|
2021-09-30 15:12:46 +00:00
|
|
|
macro TestBitFieldStore(val: TestBitFieldStruct): void {
|
2020-05-12 13:28:29 +00:00
|
|
|
let val: TestBitFieldStruct = val; // Get a mutable local copy.
|
|
|
|
const a: bool = val.a;
|
|
|
|
const b: uint16 = val.b;
|
|
|
|
let c: uint32 = val.c;
|
|
|
|
const d: bool = val.d;
|
2019-12-12 20:49:36 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
val.a = !a;
|
|
|
|
TestBitFieldLoad(val, !a, b, c, d);
|
2019-12-12 20:49:36 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
c = Unsigned(7 - Signed(val.c));
|
|
|
|
val.c = c;
|
|
|
|
TestBitFieldLoad(val, !a, b, c, d);
|
2019-12-12 20:49:36 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
val.d = val.b == val.c;
|
|
|
|
TestBitFieldLoad(val, !a, b, c, b == c);
|
|
|
|
}
|
2019-12-12 20:49:36 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
@export
|
2021-09-30 15:12:46 +00:00
|
|
|
macro TestBitFieldInit(a: bool, b: uint16, c: uint32, d: bool): void {
|
2020-05-12 13:28:29 +00:00
|
|
|
const val: TestBitFieldStruct = TestBitFieldStruct{a: a, b: b, c: c, d: d};
|
|
|
|
TestBitFieldLoad(val, a, b, c, d);
|
|
|
|
}
|
2020-04-22 17:05:44 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
// Some other bitfield structs, to verify getting uintptr values out of word32
|
|
|
|
// structs and vice versa.
|
|
|
|
bitfield struct TestBitFieldStruct2 extends uint32 {
|
|
|
|
a: uintptr: 5 bit;
|
|
|
|
b: uintptr: 6 bit;
|
|
|
|
}
|
|
|
|
bitfield struct TestBitFieldStruct3 extends uintptr {
|
|
|
|
c: bool: 1 bit;
|
|
|
|
d: uint32: 9 bit;
|
|
|
|
e: uintptr: 17 bit;
|
|
|
|
}
|
2019-12-12 20:49:36 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
@export
|
|
|
|
macro TestBitFieldUintptrOps(
|
2021-09-30 15:12:46 +00:00
|
|
|
val2: TestBitFieldStruct2, val3: TestBitFieldStruct3): void {
|
2020-05-12 13:28:29 +00:00
|
|
|
let val2: TestBitFieldStruct2 = val2; // Get a mutable local copy.
|
|
|
|
let val3: TestBitFieldStruct3 = val3; // Get a mutable local copy.
|
|
|
|
|
|
|
|
// Caller is expected to provide these exact values, so we can verify
|
|
|
|
// reading values before starting to write anything.
|
|
|
|
check(val2.a == 3);
|
|
|
|
check(val2.b == 61);
|
|
|
|
check(val3.c);
|
|
|
|
check(val3.d == 500);
|
|
|
|
check(val3.e == 0x1cc);
|
|
|
|
|
|
|
|
val2.b = 16;
|
|
|
|
check(val2.a == 3);
|
|
|
|
check(val2.b == 16);
|
|
|
|
|
|
|
|
val2.b++;
|
|
|
|
check(val2.a == 3);
|
|
|
|
check(val2.b == 17);
|
|
|
|
|
|
|
|
val3.d = 99;
|
|
|
|
val3.e = 1234;
|
|
|
|
check(val3.c);
|
|
|
|
check(val3.d == 99);
|
|
|
|
check(val3.e == 1234);
|
|
|
|
}
|
2020-03-03 15:53:40 +00:00
|
|
|
|
2020-05-19 17:58:18 +00:00
|
|
|
bitfield struct TestBitFieldStruct4 extends uint31 {
|
|
|
|
a: bool: 1 bit;
|
|
|
|
b: int32: 3 bit;
|
|
|
|
c: bool: 1 bit;
|
|
|
|
}
|
|
|
|
|
|
|
|
bitfield struct TestBitFieldStruct5 extends uint31 {
|
|
|
|
b: int32: 19 bit;
|
|
|
|
a: bool: 1 bit;
|
|
|
|
c: bool: 1 bit;
|
|
|
|
}
|
|
|
|
|
|
|
|
@export
|
2021-09-30 15:12:46 +00:00
|
|
|
macro TestBitFieldMultipleFlags(a: bool, b: int32, c: bool): void {
|
2020-05-19 17:58:18 +00:00
|
|
|
const f = TestBitFieldStruct4{a: a, b: b, c: c};
|
|
|
|
let simpleExpression = f.a & f.b == 3 & !f.c;
|
|
|
|
let expectedReduction = (Signed(f) & 0x1f) == Convert<int32>(1 | 3 << 1);
|
2020-07-27 10:35:36 +00:00
|
|
|
static_assert(simpleExpression == expectedReduction);
|
2020-05-19 17:58:18 +00:00
|
|
|
simpleExpression = !f.a & f.b == 4 & f.c;
|
|
|
|
expectedReduction = (Signed(f) & 0x1f) == Convert<int32>(4 << 1 | 1 << 4);
|
2020-07-27 10:35:36 +00:00
|
|
|
static_assert(simpleExpression == expectedReduction);
|
2020-05-19 17:58:18 +00:00
|
|
|
simpleExpression = f.b == 0 & f.c;
|
|
|
|
expectedReduction = (Signed(f) & 0x1e) == Convert<int32>(1 << 4);
|
2020-07-27 10:35:36 +00:00
|
|
|
static_assert(simpleExpression == expectedReduction);
|
2020-05-19 17:58:18 +00:00
|
|
|
simpleExpression = f.a & f.c;
|
|
|
|
expectedReduction = (Signed(f) & 0x11) == Convert<int32>(1 | 1 << 4);
|
2020-07-27 10:35:36 +00:00
|
|
|
static_assert(simpleExpression == expectedReduction);
|
2020-05-19 17:58:18 +00:00
|
|
|
const f2 = TestBitFieldStruct5{b: b, a: a, c: c};
|
|
|
|
simpleExpression = !f2.a & f2.b == 1234 & f2.c;
|
|
|
|
expectedReduction = (Signed(f2) & 0x1fffff) == Convert<int32>(1234 | 1 << 20);
|
2020-07-27 10:35:36 +00:00
|
|
|
static_assert(simpleExpression == expectedReduction);
|
2020-05-19 17:58:18 +00:00
|
|
|
simpleExpression = !f2.a & !f2.c;
|
|
|
|
expectedReduction = (Signed(f2) & 0x180000) == Convert<int32>(0);
|
2020-07-27 10:35:36 +00:00
|
|
|
static_assert(simpleExpression == expectedReduction);
|
2020-05-19 17:58:18 +00:00
|
|
|
}
|
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
@export
|
|
|
|
class ExportedSubClass extends ExportedSubClassBase {
|
|
|
|
c_field: int32;
|
|
|
|
d_field: int32;
|
|
|
|
e_field: Smi;
|
|
|
|
}
|
2020-03-03 15:53:40 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
@export
|
|
|
|
class ExportedSubClassBase extends HeapObject {
|
|
|
|
a: HeapObject;
|
|
|
|
b: HeapObject;
|
|
|
|
}
|
2020-03-03 15:53:40 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
@abstract
|
|
|
|
class AbstractInternalClass extends HeapObject {
|
|
|
|
}
|
2020-05-04 12:50:30 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
class AbstractInternalClassSubclass1 extends AbstractInternalClass {}
|
2020-05-04 12:50:30 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
class AbstractInternalClassSubclass2 extends AbstractInternalClass {}
|
2020-05-04 12:50:30 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
class InternalClassWithSmiElements extends FixedArrayBase {
|
|
|
|
data: Smi;
|
|
|
|
object: Oddball;
|
|
|
|
entries[length]: Smi;
|
|
|
|
}
|
2020-03-03 15:53:40 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
struct InternalClassStructElement {
|
|
|
|
a: Smi;
|
|
|
|
b: Smi;
|
|
|
|
}
|
2020-03-03 15:53:40 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
class InternalClassWithStructElements extends HeapObject {
|
|
|
|
dummy1: int32;
|
|
|
|
dummy2: int32;
|
|
|
|
const count: Smi;
|
|
|
|
data: Smi;
|
|
|
|
object: Object;
|
|
|
|
entries[count]: Smi;
|
|
|
|
more_entries[count]: InternalClassStructElement;
|
|
|
|
}
|
2020-03-03 15:53:40 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
struct SmiGeneratorIterator {
|
|
|
|
macro Next(): Smi labels _NoMore {
|
|
|
|
return this.value++;
|
2020-03-03 15:53:40 +00:00
|
|
|
}
|
2020-05-12 13:28:29 +00:00
|
|
|
value: Smi;
|
|
|
|
}
|
2020-03-03 15:53:40 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
struct InternalClassStructElementGeneratorIterator {
|
|
|
|
macro Next(): InternalClassStructElement labels _NoMore {
|
|
|
|
return InternalClassStructElement{a: this.value++, b: this.value++};
|
2020-03-03 15:53:40 +00:00
|
|
|
}
|
2020-05-12 13:28:29 +00:00
|
|
|
value: Smi;
|
|
|
|
}
|
2020-03-03 15:53:40 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
@export
|
2021-09-30 15:12:46 +00:00
|
|
|
macro TestFullyGeneratedClassWithElements(): void {
|
2020-05-12 13:28:29 +00:00
|
|
|
// Test creation, initialization and access of a fully generated class with
|
|
|
|
// simple (Smi) elements
|
|
|
|
const length: Smi = Convert<Smi>(3);
|
|
|
|
const object1 = new InternalClassWithSmiElements{
|
|
|
|
length,
|
|
|
|
data: 0,
|
|
|
|
object: Undefined,
|
|
|
|
entries: ...SmiGeneratorIterator {
|
|
|
|
value: 11
|
|
|
|
}
|
|
|
|
};
|
2021-09-30 07:26:47 +00:00
|
|
|
dcheck(object1.length == 3);
|
|
|
|
dcheck(object1.data == 0);
|
|
|
|
dcheck(object1.object == Undefined);
|
|
|
|
dcheck(object1.entries[0] == 11);
|
|
|
|
dcheck(object1.entries[1] == 12);
|
|
|
|
dcheck(object1.entries[2] == 13);
|
2020-05-12 13:28:29 +00:00
|
|
|
|
|
|
|
// Test creation, initialization and access of a fully generated class
|
|
|
|
// with elements that are a struct.
|
|
|
|
const object2 = new InternalClassWithStructElements{
|
|
|
|
dummy1: 44,
|
|
|
|
dummy2: 45,
|
|
|
|
count: length,
|
|
|
|
data: 55,
|
|
|
|
object: Undefined,
|
|
|
|
entries: ...SmiGeneratorIterator{value: 3},
|
|
|
|
more_entries: ...InternalClassStructElementGeneratorIterator {
|
|
|
|
value: 1
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-09-30 07:26:47 +00:00
|
|
|
dcheck(object2.dummy1 == 44);
|
|
|
|
dcheck(object2.dummy2 == 45);
|
|
|
|
dcheck(object2.count == 3);
|
|
|
|
dcheck(object2.data == 55);
|
|
|
|
dcheck(object2.object == Undefined);
|
|
|
|
dcheck(object2.entries[0] == 3);
|
|
|
|
dcheck(object2.entries[1] == 4);
|
|
|
|
dcheck(object2.entries[2] == 5);
|
|
|
|
dcheck(object2.more_entries[0].a == 1);
|
|
|
|
dcheck(object2.more_entries[0].b == 2);
|
|
|
|
dcheck(object2.more_entries[1].a == 3);
|
|
|
|
dcheck(object2.more_entries[1].b == 4);
|
|
|
|
dcheck(object2.more_entries[2].a == 5);
|
|
|
|
dcheck(object2.more_entries[2].b == 6);
|
2020-05-12 13:28:29 +00:00
|
|
|
}
|
2020-03-03 15:53:40 +00:00
|
|
|
|
2020-05-12 13:28:29 +00:00
|
|
|
@export
|
|
|
|
macro TestFullyGeneratedClassFromCpp(): ExportedSubClass {
|
|
|
|
return new
|
|
|
|
ExportedSubClass{a: Null, b: Null, c_field: 7, d_field: 8, e_field: 9};
|
|
|
|
}
|
2020-06-23 07:08:51 +00:00
|
|
|
|
|
|
|
@export
|
|
|
|
class ExportedSubClass2 extends ExportedSubClassBase {
|
|
|
|
x_field: int32;
|
|
|
|
y_field: int32;
|
|
|
|
z_field: Smi;
|
|
|
|
}
|
|
|
|
|
|
|
|
@export
|
2021-09-30 15:12:46 +00:00
|
|
|
macro TestGeneratedCastOperators(implicit context: Context)(): void {
|
2020-06-23 07:08:51 +00:00
|
|
|
const a = new
|
|
|
|
ExportedSubClass{a: Null, b: Null, c_field: 3, d_field: 4, e_field: 5};
|
|
|
|
const b = new ExportedSubClassBase{a: Undefined, b: Null};
|
|
|
|
const c = new
|
|
|
|
ExportedSubClass2{a: Null, b: Null, x_field: 3, y_field: 4, z_field: 5};
|
|
|
|
const aO: Object = a;
|
|
|
|
const bO: Object = b;
|
|
|
|
const cO: Object = c;
|
2021-09-30 07:26:47 +00:00
|
|
|
dcheck(Is<ExportedSubClassBase>(aO));
|
|
|
|
dcheck(Is<ExportedSubClass>(aO));
|
|
|
|
dcheck(!Is<ExportedSubClass2>(aO));
|
|
|
|
dcheck(Is<ExportedSubClassBase>(bO));
|
|
|
|
dcheck(!Is<ExportedSubClass>(bO));
|
|
|
|
dcheck(Is<ExportedSubClassBase>(cO));
|
|
|
|
dcheck(!Is<ExportedSubClass>(cO));
|
|
|
|
dcheck(Is<ExportedSubClass2>(cO));
|
2020-06-23 07:08:51 +00:00
|
|
|
|
2020-08-06 10:06:53 +00:00
|
|
|
const jsf: JSFunction =
|
|
|
|
*NativeContextSlot(ContextSlot::REGEXP_FUNCTION_INDEX);
|
2021-09-30 07:26:47 +00:00
|
|
|
dcheck(!Is<JSSloppyArgumentsObject>(jsf));
|
2020-06-23 07:08:51 +00:00
|
|
|
|
|
|
|
const parameterValues = NewFixedArray(0, ConstantIterator(TheHole));
|
|
|
|
const elements = NewSloppyArgumentsElements(
|
|
|
|
0, context, parameterValues, ConstantIterator(TheHole));
|
|
|
|
const fastArgs = arguments::NewJSFastAliasedArgumentsObject(
|
|
|
|
elements, Convert<Smi>(0), jsf);
|
2021-09-30 07:26:47 +00:00
|
|
|
dcheck(Is<JSArgumentsObject>(fastArgs));
|
2020-06-23 07:08:51 +00:00
|
|
|
}
|
2020-07-10 14:36:04 +00:00
|
|
|
|
|
|
|
extern runtime InYoungGeneration(implicit context: Context)(HeapObject):
|
|
|
|
Boolean;
|
|
|
|
|
|
|
|
@export
|
2021-09-30 15:12:46 +00:00
|
|
|
macro TestNewPretenured(implicit context: Context)(): void {
|
2020-07-10 14:36:04 +00:00
|
|
|
const obj = new (Pretenured) ExportedSubClassBase{a: Undefined, b: Null};
|
2021-09-30 07:26:47 +00:00
|
|
|
dcheck(Is<ExportedSubClassBase>(obj));
|
|
|
|
dcheck(InYoungGeneration(obj) == False);
|
2020-07-10 14:36:04 +00:00
|
|
|
}
|
2020-08-05 13:43:04 +00:00
|
|
|
|
|
|
|
@export
|
2021-09-30 15:12:46 +00:00
|
|
|
macro TestWord8Phi(): void {
|
2020-08-05 13:43:04 +00:00
|
|
|
for (let i: intptr = -5; i < 5; ++i) {
|
|
|
|
let x: int8;
|
|
|
|
if (i == -1) {
|
|
|
|
x = -1;
|
|
|
|
} else {
|
|
|
|
x = Convert<int8>(i);
|
|
|
|
}
|
|
|
|
check(x == Convert<int8>(i));
|
|
|
|
}
|
|
|
|
}
|
2020-11-13 10:43:07 +00:00
|
|
|
|
|
|
|
@export
|
2021-09-30 15:12:46 +00:00
|
|
|
macro TestOffHeapSlice(ptr: RawPtr<char8>, length: intptr): void {
|
2020-11-13 10:43:07 +00:00
|
|
|
const string = UnsafeCast<SeqOneByteString>(Convert<String>('Hello World!'));
|
|
|
|
|
|
|
|
check(*torque_internal::unsafe::NewOffHeapReference(ptr) == string.chars[0]);
|
|
|
|
|
2020-11-25 09:04:52 +00:00
|
|
|
let offHeapSlice = torque_internal::unsafe::NewOffHeapConstSlice(ptr, length);
|
2020-11-13 10:43:07 +00:00
|
|
|
let onHeapSlice = &string.chars;
|
|
|
|
for (let i: intptr = 0; i < onHeapSlice.length; ++i) {
|
|
|
|
check(*onHeapSlice.AtIndex(i) == *offHeapSlice.AtIndex(i));
|
|
|
|
}
|
|
|
|
}
|
2021-01-15 18:23:49 +00:00
|
|
|
|
|
|
|
struct TwoValues {
|
|
|
|
a: Smi;
|
|
|
|
b: Map;
|
|
|
|
}
|
|
|
|
|
|
|
|
builtin ReturnTwoValues(implicit context: Context)(
|
|
|
|
value: Smi, obj: HeapObject): TwoValues {
|
|
|
|
return TwoValues{a: value + 1, b: obj.map};
|
|
|
|
}
|
|
|
|
|
|
|
|
@export
|
2021-09-30 15:12:46 +00:00
|
|
|
macro TestCallMultiReturnBuiltin(implicit context: Context)(): void {
|
2021-01-15 18:23:49 +00:00
|
|
|
const result = ReturnTwoValues(444, FromConstexpr<String>('hi'));
|
|
|
|
check(result.a == 445);
|
|
|
|
check(result.b == FromConstexpr<String>('hi').map);
|
|
|
|
}
|
2021-02-23 15:58:06 +00:00
|
|
|
|
|
|
|
@export
|
|
|
|
macro TestRunLazyTwice(lazySmi: Lazy<Smi>): Smi {
|
|
|
|
const firstResult = RunLazy(lazySmi);
|
|
|
|
const secondResult = RunLazy(lazySmi);
|
|
|
|
return firstResult + secondResult;
|
|
|
|
}
|
|
|
|
|
|
|
|
macro GetLazySmi(): Smi {
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
macro AddTwoSmiValues(a: Smi, b: Smi): Smi {
|
|
|
|
return a + b;
|
|
|
|
}
|
|
|
|
|
|
|
|
macro AddSmiAndConstexprValues(a: Smi, b: constexpr int31): Smi {
|
|
|
|
return a + b;
|
|
|
|
}
|
|
|
|
|
|
|
|
@export
|
2021-09-30 15:12:46 +00:00
|
|
|
macro TestCreateLazyNodeFromTorque(): void {
|
2021-02-23 15:58:06 +00:00
|
|
|
const lazy = %MakeLazy<Smi>('GetLazySmi');
|
|
|
|
const result = TestRunLazyTwice(lazy);
|
|
|
|
check(result == 6);
|
|
|
|
|
|
|
|
// The macro can also be referred to using namespace qualifications.
|
|
|
|
const lazy2 = %MakeLazy<Smi>('test::GetLazySmi');
|
|
|
|
const result2 = TestRunLazyTwice(lazy2);
|
|
|
|
check(result2 == 6);
|
|
|
|
|
|
|
|
// We can save params to the macro. The most common usage is likely a
|
|
|
|
// single-arg macro that just returns the arg, but we can use any number of
|
|
|
|
// params.
|
|
|
|
const lazy3 = %MakeLazy<Smi>('AddTwoSmiValues', 5, 6);
|
|
|
|
const result3 = TestRunLazyTwice(lazy3);
|
|
|
|
check(result3 == 22);
|
|
|
|
|
|
|
|
// It's okay if some of the params are constexpr and some aren't.
|
|
|
|
const lazy4 = %MakeLazy<Smi>('AddSmiAndConstexprValues', 7, 8);
|
|
|
|
const result4 = TestRunLazyTwice(lazy4);
|
|
|
|
check(result4 == 30);
|
|
|
|
}
|
2018-10-31 14:59:13 +00:00
|
|
|
}
|