From 24c8404b0a41620349c42dad33fa2e3698f7c42d Mon Sep 17 00:00:00 2001 From: Su Yihan Date: Wed, 6 Dec 2023 13:57:02 +0800 Subject: [PATCH] fix type inconsistent for assignment Signed-off-by: Su Yihan --- src/backend/binaryen/wasm_expr_gen.ts | 18 ++++++++--- tests/samples/assign_different_type.ts | 43 ++++++++++++++++++++++++++ tools/validate/wamr/validation.json | 15 +++++++++ 3 files changed, 72 insertions(+), 4 deletions(-) create mode 100644 tests/samples/assign_different_type.ts diff --git a/src/backend/binaryen/wasm_expr_gen.ts b/src/backend/binaryen/wasm_expr_gen.ts index 2a4f51fd..3f1c1f3c 100644 --- a/src/backend/binaryen/wasm_expr_gen.ts +++ b/src/backend/binaryen/wasm_expr_gen.ts @@ -3924,14 +3924,24 @@ export class WASMExpressionGen { return module.f64.const(0); } case ValueTypeKind.STRING: { - return FunctionalFuncs.generateStringForStructArrayStr( - this.module, - '', - ); + if (getConfig().enableStringRef) { + return this.createStringRef(''); + } else { + return FunctionalFuncs.generateStringForStructArrayStr( + this.module, + '', + ); + } } case ValueTypeKind.BOOLEAN: { return module.i32.const(0); } + case ValueTypeKind.FUNCTION: { + return binaryenCAPI._BinaryenRefNull( + module.ptr, + builtinClosureType.typeRef, + ); + } default: { return binaryenCAPI._BinaryenRefNull( module.ptr, diff --git a/tests/samples/assign_different_type.ts b/tests/samples/assign_different_type.ts new file mode 100644 index 00000000..403b293d --- /dev/null +++ b/tests/samples/assign_different_type.ts @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2023 Intel Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +class A{ + x: string[]; + constructor(){ + this.x = []; + } +} + +export function assignStringArray() { + const a = new A(); + a.x.push('hello'); + console.log(a.x[0]); +} + +interface I { + instance: number; +} + +type cbType = (v: I) => number; + +class B { + x: Array; + constructor() { + this.x = []; + } +} + +function elemFunc(a: I) { + return a.instance; +} + +export function assignClosureArray() { + const a = new B(); + const i: I = { + instance: 100, + }; + a.x.push(elemFunc); + console.log(a.x[0](i)); +} diff --git a/tools/validate/wamr/validation.json b/tools/validate/wamr/validation.json index e4f91c5e..8d7db695 100644 --- a/tools/validate/wamr/validation.json +++ b/tools/validate/wamr/validation.json @@ -3719,6 +3719,21 @@ } ] }, + { + "module": "assign_different_type", + "entries": [ + { + "name": "assignStringArray", + "args": [], + "result": "hello" + }, + { + "name": "assignClosureArray", + "args": [], + "result": "100" + } + ] + }, { "module": "auto_box_unbox", "entries": [