Skip to content

Commit

Permalink
use wasm basic type as array length
Browse files Browse the repository at this point in the history
Signed-off-by: Su Yihan <[email protected]>
  • Loading branch information
yviansu committed Jan 8, 2024
1 parent b8e87d8 commit 70f08fa
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/backend/binaryen/wasm_expr_gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4030,6 +4030,12 @@ export class WASMExpressionGen {
);
}
}
case ValueTypeKind.WASM_I64: {
return module.i64.const(0, 0);
}
case ValueTypeKind.WASM_F32: {
return module.f32.const(0);
}
case ValueTypeKind.INT:
case ValueTypeKind.BOOLEAN: {
return module.i32.const(0);
Expand Down
8 changes: 7 additions & 1 deletion src/expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -851,7 +851,13 @@ export default class ExpressionProcessor {
} else if (argLen === 1) {
const elem = newExprNode.arguments[0];
const elemExpr = this.visitNode(elem);
if (elemExpr.exprType.kind !== TypeKind.NUMBER) {
if (
elemExpr.exprType.kind !== TypeKind.NUMBER &&
elemExpr.exprType.kind !== TypeKind.WASM_I32 &&
elemExpr.exprType.kind !== TypeKind.WASM_I64 &&
elemExpr.exprType.kind !== TypeKind.WASM_F32 &&
elemExpr.exprType.kind !== TypeKind.WASM_F64
) {
isLiteral = true;
}
}
Expand Down
38 changes: 38 additions & 0 deletions tests/samples/array_wasm_basic_type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright (C) 2023 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/

export function arrayLength() {
const a1: i32 = 10;
const arr1_i32: i32[] = new Array(a1);
const arr1_default = new Array(a1); // default is any type
arr1_i32[0] = 32;
arr1_default[0] = 30;
console.log(arr1_i32[0]);
console.log(arr1_default[0]);

const a2: i64 = 20;
const arr2_i64: i64[] = new Array(a2);
const arr2_default = new Array(a2); // default is any type
arr2_i64[0] = 64;
arr2_default[0] = 60;
console.log(arr2_i64[0]);
console.log(arr2_default[0]);

const a3: f32 = 30;
const arr3_f32: f32[] = new Array(a3);
const arr3_default = new Array(a3); // default is any type
arr3_f32[0] = 32.32;
arr3_default[0] = 30.30;
console.log(arr3_f32[0]);
console.log(arr3_default[0]);

const a4: f64 = 40;
const arr4_f64: f64[] = new Array(a4);
const arr4_default = new Array(a4); // default is any type
arr4_f64[0] = 64.64;
arr4_default[0] = 60.60;
console.log(arr4_f64[0]);
console.log(arr4_default[0]);
}
10 changes: 10 additions & 0 deletions tools/validate/wamr/validation.json
Original file line number Diff line number Diff line change
Expand Up @@ -3864,6 +3864,16 @@
}
]
},
{
"module": "array_wasm_basic_type",
"entries": [
{
"name": "arrayLength",
"args": [],
"result": "32\n30\n64\n60\n32.31999969482422\n30.3\n64.64\n60.6"
}
]
},
{
"module": "arraybuffer_basic",
"entries": [
Expand Down

0 comments on commit 70f08fa

Please sign in to comment.