-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
44 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters