Skip to content

Commit

Permalink
fix the candid encoding example
Browse files Browse the repository at this point in the history
  • Loading branch information
lastmjs committed Jan 15, 2024
1 parent 2437290 commit b5f36eb
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 47 deletions.
2 changes: 1 addition & 1 deletion examples/candid_encoding/src/index.did
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
service: () -> {
candidEncode: (text) -> (vec nat8) query;
candidDecode: (vec nat8) -> (text) query;
candidEncode: (text) -> (vec nat8) query;
}
28 changes: 13 additions & 15 deletions src/compiler/rust/canister/src/ic/candid_decode.rs
Original file line number Diff line number Diff line change
@@ -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<JSValueRef<'a>, anyhow::Error> {
let candid_encoded: Vec<u8> = _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()
}
}
28 changes: 13 additions & 15 deletions src/compiler/rust/canister/src/ic/candid_encode.rs
Original file line number Diff line number Diff line change
@@ -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<JSValueRef<'a>, 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<u8> = 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()
}
}
33 changes: 17 additions & 16 deletions src/compiler/rust/canister/src/ic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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::<candid_decode::NativeFunction>("")
.into(),
);

ic.set(
"candidEncode",
context
.new_function::<candid_encode::NativeFunction>("")
.into(),
);

// ic.set_property(
// "canisterBalance",
// context
Expand Down

0 comments on commit b5f36eb

Please sign in to comment.