I’ve been experimenting with writing AssemblyScript on DFINITY and ran into some questions.
First I grabbed the reverse project from dfinity examples, compiled it and wasm2wat the results which gave me these definitions:
(import "ic0" "msg_arg_data_size" (func $dfn_ads (type $t0)))
(import "ic0" "msg_arg_data_copy" (func $dfn_adc (type $t1)))
(import "ic0" "debug_print" (func $dfn_print (type $t2)))
(import "ic0" "msg_reply_data_append" (func $dfn_reply_append (type $t2)))
(import "ic0" "msg_reply" (func $dfn_reply (type $t3)))
(func $canister_update_go (export "canister_update go") (type $t3)
So I created this AssemblyScript Interface: (ic0.ts)
export declare function debug_print(msg : string, length: i32) : void;
export declare function msg_arg_data_size() : i32;
export declare function msg_arg_data_copy(x : i32, y: i32, z: i32) : void;
export declare function msg_reply_data_append(x : string, y: i32) : void;
export declare function msg_reply() : void;
index.ts
function init() : void {
var text: string = "hello dfinity from assemblyscript";
IC.debug_print(text,String.UTF16.byteLength(text));
}
export {
init as canister_init,
}
Results:
[Canister rwlgt-iiaaa-aaaaa-aaaaa-cai] hello dfinity from assemblyscript
Now I wanted to query a function with my canister being called tshello
dfx canister call tshello sayhello
so I added the function:
function sayhello() : void {
var text: string = "sayhello";
IC.debug_print(text,String.UTF16.byteLength(text));
var results: ArrayBuffer = String.UTF8.encode(`DIDL\00\00`);
IC.msg_reply_data_append(results, results.byteLength)
IC.msg_reply();
}
export {
init as canister_init,
sayhello as canister_update_sayhello,
}
Which gives me the WAT
(export "canister_update_sayhello" (func $assembly/index/sayhello))
Which gives me the results:
Replica error (code 3): IC0302: Canister rwlgt-iiaaa-aaaaa-aaaaa-cai has no update method 'sayhello'
I realized there needs to be a space between canister_update and sayhello, so if I go into the .wat file and add a space.
(export "canister_update sayhello" (func $assembly/index/sayhello))
Results are good with --output raw
[Canister rwlgt-iiaaa-aaaaa-aaaaa-cai] sayhello
4449444c5c30305c3030
Without Raw
"Unsupported op_code 48 in type table",
Questions
- Is the space required with canister_update and the function name?
- Anyone familiar enough with AssemblyScript to properly define an export with a space in its name?
- Whats the correct way to use an empty reply?
DIDL\00\00
I’m going to keep experimenting with this, but there are the initial results.
btw I had a lot of problems submitting this post, something about the code.
Thanks,
Rick