Skip to content

Commit

Permalink
ensure that wasm binaries are copied correctly, move event loop execu…
Browse files Browse the repository at this point in the history
…tion into the quickjs_with_ctx
  • Loading branch information
lastmjs committed Oct 18, 2024
1 parent e9b1b35 commit 7960255
Show file tree
Hide file tree
Showing 12 changed files with 26 additions and 32 deletions.
Binary file modified canister_templates/experimental.wasm
Binary file not shown.
Binary file modified canister_templates/stable.wasm
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export function compile(
);

execSyncPretty(
`wasi2ic target/wasm32-wasi/release/canister.wasm ${wasmDest}`,
`wasi2ic target/wasm32-wasi/release/experimental_canister_template.wasm ${wasmDest}`,
ioType
);
}
2 changes: 1 addition & 1 deletion src/build/stable/commands/compile/wasm_binary/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export function compile(
);

execSyncPretty(
`wasi2ic target/wasm32-wasi/release/stable.wasm ${wasmDest}`,
`wasi2ic target/wasm32-wasi/release/stable_canister_template.wasm ${wasmDest}`,
ioType
);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::{
ic, quickjs_with_ctx, run_event_loop, wasm_binary_manipulation::get_js_code, CONTEXT_REF_CELL,
MODULE_NAME,
ic, quickjs_with_ctx, wasm_binary_manipulation::get_js_code, CONTEXT_REF_CELL, MODULE_NAME,
};

// TODO we might not need any of these panic hooks
Expand Down Expand Up @@ -55,8 +54,6 @@ pub fn get_candid_and_method_meta_pointer() -> *mut std::os::raw::c_char {
// TODO this returns a promise...make sure we handle it appropriately
rquickjs::Module::evaluate(ctx.clone(), MODULE_NAME, js).unwrap();

run_event_loop(ctx.clone());

let get_candid_and_method_meta: rquickjs::Function =
ctx.globals().get("_azleGetCandidAndMethodMeta").unwrap();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{quickjs_with_ctx, run_event_loop};
use crate::quickjs_with_ctx;

#[no_mangle]
#[allow(unused)]
Expand All @@ -20,7 +20,5 @@ pub extern "C" fn execute_method_js(function_index: i32, pass_arg_data: i32) {
method_callback
.call::<_, rquickjs::Undefined>((candid_args,))
.unwrap();

run_event_loop(ctx.clone());
});
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{quickjs_with_ctx, run_event_loop};
use crate::quickjs_with_ctx;
use rquickjs::{Ctx, Exception, Function, IntoJs, TypedArray};

pub fn get_function(ctx: Ctx) -> Function {
Expand All @@ -19,7 +19,6 @@ pub fn get_function(ctx: Ctx) -> Function {

quickjs_with_ctx(move |ctx| {
resolve_or_reject(ctx.clone(), &call_result, &promise_id);
run_event_loop(ctx.clone());
});
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use rquickjs::{Ctx, Function};
use slotmap::Key;

use crate::{quickjs_with_ctx, run_event_loop};
use crate::quickjs_with_ctx;

pub fn get_function(ctx: Ctx) -> Function {
Function::new(ctx, |delay: String, callback_id: String| {
Expand All @@ -21,8 +21,6 @@ pub fn get_function(ctx: Ctx) -> Function {
if result.is_exception() {
panic!("Timer callback threw an exception");
}

run_event_loop(ctx.clone());
});
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use rquickjs::{Ctx, Function};
use slotmap::Key;

use crate::{quickjs_with_ctx, run_event_loop};
use crate::quickjs_with_ctx;

pub fn get_function(ctx: Ctx) -> Function {
Function::new(ctx, |interval: String, callback_id: String| {
Expand All @@ -21,8 +21,6 @@ pub fn get_function(ctx: Ctx) -> Function {
if result.is_exception() {
panic!("Timer interval callback threw an exception");
}

run_event_loop(ctx.clone());
});
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use ic_stable_structures::memory_manager::MemoryId;

use crate::{
execute_method_js::execute_method_js,
ic, quickjs_with_ctx, run_event_loop,
ic, quickjs_with_ctx,
wasm_binary_manipulation::{get_js_code, get_wasm_data},
CONTEXT_REF_CELL, MEMORY_MANAGER_REF_CELL, MODULE_NAME,
};
Expand Down Expand Up @@ -112,8 +112,6 @@ pub fn initialize_js(js: &str, init: bool, function_index: i32, pass_arg_data: i
// TODO is there a better name for this main module?
// TODO this returns a promise...make sure we handle it appropriately
rquickjs::Module::evaluate(ctx.clone(), MODULE_NAME, js).unwrap();

run_event_loop(ctx.clone());
});

// TODO is it possible to just put this all in the same quickjs_with_ctx?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,5 @@ thread_local! {
pub static MEMORY_MANAGER_REF_CELL: RefCell<MemoryManager<DefaultMemoryImpl>> = RefCell::new(MemoryManager::init(DefaultMemoryImpl::default()));
}

pub fn run_event_loop(context: rquickjs::Ctx) {
loop {
let result = context.execute_pending_job();

if result == false {
break;
}
}
}

#[ic_cdk_macros::update]
pub fn _azle_chunk() {}
Original file line number Diff line number Diff line change
@@ -1,14 +1,30 @@
use crate::CONTEXT_REF_CELL;
use rquickjs::Ctx;

pub fn quickjs_with_ctx<F, R>(f: F) -> R
pub fn quickjs_with_ctx<F, R>(callback: F) -> R
where
F: FnOnce(Ctx) -> R,
{
CONTEXT_REF_CELL.with(|context_ref_cell| {
let context_ref = context_ref_cell.borrow();
let context = context_ref.as_ref().unwrap();

context.with(f)
context.with(|ctx| {
let result = callback(ctx.clone());

run_event_loop(ctx);

result
})
})
}

fn run_event_loop(ctx: rquickjs::Ctx) {
loop {
let result = ctx.execute_pending_job();

if result == false {
break;
}
}
}

0 comments on commit 7960255

Please sign in to comment.