Skip to content

Commit

Permalink
implement accept_message, rejection stuff, and method name
Browse files Browse the repository at this point in the history
  • Loading branch information
lastmjs committed Jan 17, 2024
1 parent 8e1d924 commit b94b194
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 75 deletions.
18 changes: 10 additions & 8 deletions src/compiler/rust/canister/src/ic/accept_message.rs
Original file line number Diff line number Diff line change
@@ -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<JSValueRef<'a>, 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
}
}
14 changes: 6 additions & 8 deletions src/compiler/rust/canister/src/ic/method_name.rs
Original file line number Diff line number Diff line change
@@ -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<JSValueRef<'a>, 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()
}
}
62 changes: 30 additions & 32 deletions src/compiler/rust/canister/src/ic/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// mod accept_message;
mod accept_message;
mod arg_data_raw;
mod arg_data_raw_size;
mod call_raw;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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::<accept_message::NativeFunction>("")
.into(),
);

ic.set(
"argDataRaw",
Expand Down Expand Up @@ -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::<method_name::NativeFunction>("")
.into(),
);

// ic.set_property(
// "msgCyclesAccept",
// context
Expand Down Expand Up @@ -237,20 +236,19 @@ pub fn register(context: &mut wasmedge_quickjs::Context) {
context.new_function::<reject::NativeFunction>("").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::<reject_code::NativeFunction>("")
.into(),
);

ic.set(
"rejectMessage",
context
.new_function::<reject_message::NativeFunction>("")
.into(),
);

ic.set(
"replyRaw",
Expand Down
35 changes: 16 additions & 19 deletions src/compiler/rust/canister/src/ic/reject_code.rs
Original file line number Diff line number Diff line change
@@ -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<JSValueRef<'a>, 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()
}
}
16 changes: 8 additions & 8 deletions src/compiler/rust/canister/src/ic/reject_message.rs
Original file line number Diff line number Diff line change
@@ -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<JSValueRef<'a>, 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()
}
}

0 comments on commit b94b194

Please sign in to comment.