From b94b19493b45d752b09dcdbaf112ceb65ecdaea5 Mon Sep 17 00:00:00 2001 From: Jordan Last Date: Wed, 17 Jan 2024 14:12:13 -0600 Subject: [PATCH] implement accept_message, rejection stuff, and method name --- .../rust/canister/src/ic/accept_message.rs | 18 +++--- .../rust/canister/src/ic/method_name.rs | 14 ++--- src/compiler/rust/canister/src/ic/mod.rs | 62 +++++++++---------- .../rust/canister/src/ic/reject_code.rs | 35 +++++------ .../rust/canister/src/ic/reject_message.rs | 16 ++--- 5 files changed, 70 insertions(+), 75 deletions(-) diff --git a/src/compiler/rust/canister/src/ic/accept_message.rs b/src/compiler/rust/canister/src/ic/accept_message.rs index 0742df03ed..c9a9b2fbca 100644 --- a/src/compiler/rust/canister/src/ic/accept_message.rs +++ b/src/compiler/rust/canister/src/ic/accept_message.rs @@ -1,10 +1,12 @@ -use quickjs_wasm_rs::{CallbackArg, JSContextRef, JSValueRef}; +use std::convert::TryFrom; -pub fn native_function<'a>( - context: &'a JSContextRef, - _this: &CallbackArg, - _args: &[CallbackArg], -) -> Result, anyhow::Error> { - ic_cdk::api::call::accept_message(); - context.undefined_value() +use wasmedge_quickjs::{Context, JsFn, JsValue}; + +pub struct NativeFunction; +impl JsFn for NativeFunction { + fn call(context: &mut Context, this_val: JsValue, argv: &[JsValue]) -> JsValue { + ic_cdk::api::call::accept_message(); + + JsValue::UnDefined + } } diff --git a/src/compiler/rust/canister/src/ic/method_name.rs b/src/compiler/rust/canister/src/ic/method_name.rs index 50a55c321e..703149b839 100644 --- a/src/compiler/rust/canister/src/ic/method_name.rs +++ b/src/compiler/rust/canister/src/ic/method_name.rs @@ -1,10 +1,8 @@ -use quickjs_wasm_rs::{to_qjs_value, CallbackArg, JSContextRef, JSValue, JSValueRef}; +use wasmedge_quickjs::{Context, JsFn, JsValue}; -pub fn native_function<'a>( - context: &'a JSContextRef, - _this: &CallbackArg, - _args: &[CallbackArg], -) -> Result, anyhow::Error> { - let method_name_js_value: JSValue = ic_cdk::api::call::method_name().into(); - to_qjs_value(&context, &method_name_js_value) +pub struct NativeFunction; +impl JsFn for NativeFunction { + fn call(context: &mut Context, this_val: JsValue, argv: &[JsValue]) -> JsValue { + context.new_string(&ic_cdk::api::call::method_name()).into() + } } diff --git a/src/compiler/rust/canister/src/ic/mod.rs b/src/compiler/rust/canister/src/ic/mod.rs index e69ca27162..b61e8da5a2 100644 --- a/src/compiler/rust/canister/src/ic/mod.rs +++ b/src/compiler/rust/canister/src/ic/mod.rs @@ -1,4 +1,4 @@ -// mod accept_message; +mod accept_message; mod arg_data_raw; mod arg_data_raw_size; mod call_raw; @@ -14,7 +14,7 @@ mod data_certificate; mod id; mod instruction_counter; mod is_controller; -// mod method_name; +mod method_name; // mod msg_cycles_accept; // mod msg_cycles_accept128; // mod msg_cycles_available; @@ -25,8 +25,8 @@ mod notify_raw; mod performance_counter; mod print; mod reject; -// mod reject_code; -// mod reject_message; +mod reject_code; +mod reject_message; mod reply_raw; mod set_certified_data; mod set_timer; @@ -59,13 +59,12 @@ use wasmedge_quickjs::AsObject; pub fn register(context: &mut wasmedge_quickjs::Context) { let mut ic = context.new_object(); - // ic.set_property( - // "acceptMessage", - // context - // .wrap_callback2(accept_message::native_function) - // .unwrap(), - // ) - // .unwrap(); + ic.set( + "acceptMessage", + context + .new_function::("") + .into(), + ); ic.set( "argDataRaw", @@ -163,13 +162,13 @@ pub fn register(context: &mut wasmedge_quickjs::Context) { .into(), ); - // ic.set_property( - // "methodName", - // context - // .wrap_callback2(method_name::native_function) - // .unwrap(), - // ) - // .unwrap(); + ic.set( + "methodName", + context + .new_function::("") + .into(), + ); + // ic.set_property( // "msgCyclesAccept", // context @@ -237,20 +236,19 @@ pub fn register(context: &mut wasmedge_quickjs::Context) { context.new_function::("").into(), ); - // ic.set_property( - // "rejectCode", - // context - // .wrap_callback2(reject_code::native_function) - // .unwrap(), - // ) - // .unwrap(); - // ic.set_property( - // "rejectMessage", - // context - // .wrap_callback2(reject_message::native_function) - // .unwrap(), - // ) - // .unwrap(); + ic.set( + "rejectCode", + context + .new_function::("") + .into(), + ); + + ic.set( + "rejectMessage", + context + .new_function::("") + .into(), + ); ic.set( "replyRaw", diff --git a/src/compiler/rust/canister/src/ic/reject_code.rs b/src/compiler/rust/canister/src/ic/reject_code.rs index 20b513ba04..4da4e927dc 100644 --- a/src/compiler/rust/canister/src/ic/reject_code.rs +++ b/src/compiler/rust/canister/src/ic/reject_code.rs @@ -1,23 +1,20 @@ -use quickjs_wasm_rs::{to_qjs_value, CallbackArg, JSContextRef, JSValue, JSValueRef}; +use wasmedge_quickjs::{Context, JsFn, JsValue}; -pub fn native_function<'a>( - context: &'a JSContextRef, - _this: &CallbackArg, - _args: &[CallbackArg], -) -> Result, anyhow::Error> { - let reject_code = ic_cdk::api::call::reject_code(); +pub struct NativeFunction; +impl JsFn for NativeFunction { + fn call(context: &mut Context, this_val: JsValue, argv: &[JsValue]) -> JsValue { + let reject_code = ic_cdk::api::call::reject_code(); - let reject_code_as_u8 = match reject_code { - ic_cdk::api::call::RejectionCode::NoError => 0, - ic_cdk::api::call::RejectionCode::SysFatal => 1, - ic_cdk::api::call::RejectionCode::SysTransient => 2, - ic_cdk::api::call::RejectionCode::DestinationInvalid => 3, - ic_cdk::api::call::RejectionCode::CanisterReject => 4, - ic_cdk::api::call::RejectionCode::CanisterError => 5, - ic_cdk::api::call::RejectionCode::Unknown => 6, - }; + let reject_code_number = match reject_code { + ic_cdk::api::call::RejectionCode::NoError => 0, + ic_cdk::api::call::RejectionCode::SysFatal => 1, + ic_cdk::api::call::RejectionCode::SysTransient => 2, + ic_cdk::api::call::RejectionCode::DestinationInvalid => 3, + ic_cdk::api::call::RejectionCode::CanisterReject => 4, + ic_cdk::api::call::RejectionCode::CanisterError => 5, + ic_cdk::api::call::RejectionCode::Unknown => 6, + }; - let reject_code_as_js_value: JSValue = reject_code_as_u8.into(); - - to_qjs_value(&context, &reject_code_as_js_value) + reject_code_number.into() + } } diff --git a/src/compiler/rust/canister/src/ic/reject_message.rs b/src/compiler/rust/canister/src/ic/reject_message.rs index ad5c54a1cd..7a932538d0 100644 --- a/src/compiler/rust/canister/src/ic/reject_message.rs +++ b/src/compiler/rust/canister/src/ic/reject_message.rs @@ -1,10 +1,10 @@ -use quickjs_wasm_rs::{to_qjs_value, CallbackArg, JSContextRef, JSValue, JSValueRef}; +use wasmedge_quickjs::{Context, JsFn, JsValue}; -pub fn native_function<'a>( - context: &'a JSContextRef, - _this: &CallbackArg, - _args: &[CallbackArg], -) -> Result, anyhow::Error> { - let return_js_value: JSValue = ic_cdk::api::call::reject_message().into(); - to_qjs_value(&context, &return_js_value) +pub struct NativeFunction; +impl JsFn for NativeFunction { + fn call(context: &mut Context, this_val: JsValue, argv: &[JsValue]) -> JsValue { + context + .new_string(&ic_cdk::api::call::reject_message()) + .into() + } }