Skip to content

Commit

Permalink
feat: make initState report errors
Browse files Browse the repository at this point in the history
  • Loading branch information
rpiszczatowski committed Mar 15, 2023
1 parent 7075481 commit 12fac48
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/core/modules/impl/handler/AbstractContractHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export abstract class AbstractContractHandler<State> implements HandlerApi<State
interactionData: InteractionData<Input>
): Promise<InteractionResult<State, Result>>;

abstract initState(state: State): void;
abstract initState(state: State);

abstract maybeCallStateConstructor(
initialState: State,
Expand Down
3 changes: 2 additions & 1 deletion src/core/modules/impl/handler/JsHandlerApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ export class JsHandlerApi<State> extends AbstractContractHandler<State> {
}

// eslint-disable-next-line
initState(state: State): void {}
initState(state: State) {
}

async maybeCallStateConstructor<Input>(
initialState: State,
Expand Down
10 changes: 7 additions & 3 deletions src/core/modules/impl/handler/WasmHandlerApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,15 @@ export class WasmHandlerApi<State> extends AbstractContractHandler<State> {
}
}

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.`);
Expand Down
14 changes: 13 additions & 1 deletion src/core/modules/impl/wasm/rust-wasm-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
};
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 12fac48

Please sign in to comment.