From 18d32c5d8601cfd331dcb53faab968929f2a2d33 Mon Sep 17 00:00:00 2001 From: robal Date: Tue, 14 Mar 2023 08:47:23 +0100 Subject: [PATCH] feat: make initState report errors gh-309 --- .../impl/handler/AbstractContractHandler.ts | 2 +- src/core/modules/impl/handler/JsHandlerApi.ts | 3 ++- src/core/modules/impl/handler/WasmHandlerApi.ts | 10 +++++++--- src/core/modules/impl/wasm/rust-wasm-imports.ts | 14 +++++++++++++- 4 files changed, 23 insertions(+), 6 deletions(-) diff --git a/src/core/modules/impl/handler/AbstractContractHandler.ts b/src/core/modules/impl/handler/AbstractContractHandler.ts index d32c11ff..38650fc6 100644 --- a/src/core/modules/impl/handler/AbstractContractHandler.ts +++ b/src/core/modules/impl/handler/AbstractContractHandler.ts @@ -26,7 +26,7 @@ export abstract class AbstractContractHandler implements HandlerApi ): Promise>; - abstract initState(state: State): void; + abstract initState(state: State); abstract maybeCallStateConstructor( initialState: State, diff --git a/src/core/modules/impl/handler/JsHandlerApi.ts b/src/core/modules/impl/handler/JsHandlerApi.ts index 9da28a58..167c1e01 100644 --- a/src/core/modules/impl/handler/JsHandlerApi.ts +++ b/src/core/modules/impl/handler/JsHandlerApi.ts @@ -45,7 +45,8 @@ export class JsHandlerApi extends AbstractContractHandler { } // eslint-disable-next-line - initState(state: State): void {} + initState(state: State) { + } async maybeCallStateConstructor( initialState: State, diff --git a/src/core/modules/impl/handler/WasmHandlerApi.ts b/src/core/modules/impl/handler/WasmHandlerApi.ts index b1a8d994..9c1b6fc4 100644 --- a/src/core/modules/impl/handler/WasmHandlerApi.ts +++ b/src/core/modules/impl/handler/WasmHandlerApi.ts @@ -66,11 +66,15 @@ export class WasmHandlerApi extends AbstractContractHandler { } } - initState(state: State): void { + initState(state: State) { switch (this.contractDefinition.srcWasmLang) { case 'rust': { - this.wasmExports.initState(state); - break; + const ret = this.wasmExports.initState(state); + if (ret) { + throw new Error(ret); + } else { + return; + } } default: { throw new Error(`Support for ${this.contractDefinition.srcWasmLang} not implemented yet.`); diff --git a/src/core/modules/impl/wasm/rust-wasm-imports.ts b/src/core/modules/impl/wasm/rust-wasm-imports.ts index 848329f5..ec9c71d5 100644 --- a/src/core/modules/impl/wasm/rust-wasm-imports.ts +++ b/src/core/modules/impl/wasm/rust-wasm-imports.ts @@ -369,11 +369,22 @@ export const rustWasmImports = (swGlobal, wbindgenImports, wasmInstance, dtorVal } /** * @param {any} state + * @returns {boolean} */ function initState(state) { try { - wasmInstance.exports.initState(addBorrowedObject(state)); + const retptr = wasmInstance.exports.__wbindgen_add_to_stack_pointer(-16); + wasmInstance.exports.initState(retptr, addBorrowedObject(state)); + var r0 = getInt32Memory0()[retptr / 4 + 0]; + var r1 = getInt32Memory0()[retptr / 4 + 1]; + let v0; + if (r0 !== 0) { + v0 = getStringFromWasm0(r0, r1).slice(); + wasmInstance.exports.__wbindgen_free(r0, r1 * 1); + } + return v0; } finally { + wasmInstance.exports.__wbindgen_add_to_stack_pointer(16); heap[stack_pointer++] = undefined; } }; @@ -772,6 +783,7 @@ export const rustWasmImports = (swGlobal, wbindgenImports, wasmInstance, dtorVal const ret = arg0; return addHeapObject(ret); }, + __wbindgen_jsval_loose_eq: function (arg0, arg1) { const ret = getObject(arg0) == getObject(arg1); _assertBoolean(ret);