From b5f36eb1e9fbf2984af1cf5c7423f14c461381c6 Mon Sep 17 00:00:00 2001 From: Jordan Last Date: Mon, 15 Jan 2024 17:30:13 -0600 Subject: [PATCH] fix the candid encoding example --- examples/candid_encoding/src/index.did | 2 +- .../rust/canister/src/ic/candid_decode.rs | 28 ++++++++-------- .../rust/canister/src/ic/candid_encode.rs | 28 ++++++++-------- src/compiler/rust/canister/src/ic/mod.rs | 33 ++++++++++--------- 4 files changed, 44 insertions(+), 47 deletions(-) diff --git a/examples/candid_encoding/src/index.did b/examples/candid_encoding/src/index.did index 86fb2df215..0421848c2d 100644 --- a/examples/candid_encoding/src/index.did +++ b/examples/candid_encoding/src/index.did @@ -1,4 +1,4 @@ service: () -> { - candidEncode: (text) -> (vec nat8) query; candidDecode: (vec nat8) -> (text) query; + candidEncode: (text) -> (vec nat8) query; } diff --git a/src/compiler/rust/canister/src/ic/candid_decode.rs b/src/compiler/rust/canister/src/ic/candid_decode.rs index bbc1cf7d04..94d3392527 100644 --- a/src/compiler/rust/canister/src/ic/candid_decode.rs +++ b/src/compiler/rust/canister/src/ic/candid_decode.rs @@ -1,21 +1,19 @@ use std::convert::TryInto; -use quickjs_wasm_rs::{to_qjs_value, CallbackArg, JSContextRef, JSValue, JSValueRef}; +use wasmedge_quickjs::{Context, JsArrayBuffer, JsFn, JsString, JsValue}; -pub fn native_function<'a>( - context: &'a JSContextRef, - _this: &CallbackArg, - _args: &[CallbackArg], -) -> Result, anyhow::Error> { - let candid_encoded: Vec = _args - .get(0) - .expect("candidDecode must have at least one argument") - .to_js_value()? - .try_into()?; +pub struct NativeFunction; +impl JsFn for NativeFunction { + fn call(context: &mut Context, this_val: JsValue, argv: &[JsValue]) -> JsValue { + let candid_encoded = if let JsValue::ArrayBuffer(js_array_buffer) = argv.get(0).unwrap() { + js_array_buffer.to_vec() + } else { + panic!("conversion from JsValue to JsArrayBuffer failed") + }; - let candid_args: candid::IDLArgs = candid::IDLArgs::from_bytes(&candid_encoded)?; - let candid_string = candid_args.to_string(); + let candid_args: candid::IDLArgs = candid::IDLArgs::from_bytes(&candid_encoded).unwrap(); + let candid_string = candid_args.to_string(); - let candid_string_js_value: JSValue = candid_string.into(); - to_qjs_value(&context, &candid_string_js_value) + context.new_string(&candid_string).into() + } } diff --git a/src/compiler/rust/canister/src/ic/candid_encode.rs b/src/compiler/rust/canister/src/ic/candid_encode.rs index 868569f7ef..ca05581f18 100644 --- a/src/compiler/rust/canister/src/ic/candid_encode.rs +++ b/src/compiler/rust/canister/src/ic/candid_encode.rs @@ -1,21 +1,19 @@ use std::convert::TryInto; -use quickjs_wasm_rs::{to_qjs_value, CallbackArg, JSContextRef, JSValue, JSValueRef}; +use wasmedge_quickjs::{Context, JsArrayBuffer, JsFn, JsValue}; -pub fn native_function<'a>( - context: &'a JSContextRef, - _this: &CallbackArg, - _args: &[CallbackArg], -) -> Result, anyhow::Error> { - let candid_string: String = _args - .get(0) - .expect("candidEncode must have at least one argument") - .to_js_value()? - .try_into()?; +pub struct NativeFunction; +impl JsFn for NativeFunction { + fn call(context: &mut Context, this_val: JsValue, argv: &[JsValue]) -> JsValue { + let candid_string = if let JsValue::String(js_string) = argv.get(0).unwrap() { + js_string.to_string() + } else { + panic!("conversion from JsValue to JsString failed") + }; - let candid_args: candid::IDLArgs = candid_string.parse()?; - let candid_encoded: Vec = candid_args.to_bytes()?; + let candid_args: candid::IDLArgs = candid_string.parse().unwrap(); + let candid_encoded = candid_args.to_bytes().unwrap(); - let candid_encoded_js_value: JSValue = candid_encoded.into(); - to_qjs_value(&context, &candid_encoded_js_value) + context.new_array_buffer(&candid_encoded).into() + } } diff --git a/src/compiler/rust/canister/src/ic/mod.rs b/src/compiler/rust/canister/src/ic/mod.rs index 0ef6cae79c..4d7d635c1d 100644 --- a/src/compiler/rust/canister/src/ic/mod.rs +++ b/src/compiler/rust/canister/src/ic/mod.rs @@ -4,8 +4,8 @@ // mod call_raw; // mod call_raw128; // mod caller; -// mod candid_decode; -// mod candid_encode; +mod candid_decode; +mod candid_encode; // mod canister_balance; // mod canister_balance128; // mod canister_version; @@ -97,20 +97,21 @@ pub fn register(context: &mut wasmedge_quickjs::Context) { // context.wrap_callback2(caller::native_function).unwrap(), // ) // .unwrap(); - // ic.set_property( - // "candidDecode", - // context - // .wrap_callback2(candid_decode::native_function) - // .unwrap(), - // ) - // .unwrap(); - // ic.set_property( - // "candidEncode", - // context - // .wrap_callback2(candid_encode::native_function) - // .unwrap(), - // ) - // .unwrap(); + + ic.set( + "candidDecode", + context + .new_function::("") + .into(), + ); + + ic.set( + "candidEncode", + context + .new_function::("") + .into(), + ); + // ic.set_property( // "canisterBalance", // context