Skip to content

Commit

Permalink
moving event loop into one function, various cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
lastmjs committed Jan 26, 2024
1 parent 9850015 commit 9398ff6
Show file tree
Hide file tree
Showing 12 changed files with 51 additions and 152 deletions.
7 changes: 7 additions & 0 deletions contributing/wasmedge_quickjs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
To develop locally you will need to do the following:

1. Set the `AZLE_WASMEDGE_QUICKJS_DIR` environment variable on deploy or test to the location of your locally cloned `wasmedge-quickjs`.

2. Change the `wasmedge_quickjs` dependency in `azle/rust/canister/Cargo.toml` to the location of your locally cloned `wasmedge-quickjs`.

3. When you are ready to commit your changes, make sure to update `azle/install_rust_dependencies.sh` with the new git commit hash of the `wasmedge-quickjs` repository, and delete the `wasmedge-quickjs` directory in `~/.config/azle/[version]`.
12 changes: 1 addition & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@
"text-encoding": "0.7.0",
"ts-node": "10.3.1",
"typescript": "^5.2.2",
"uuid": "^9.0.1",
"zlib": "^1.0.5"
"uuid": "^9.0.1"
},
"devDependencies": {
"@dfinity/agent": "^0.19.2",
Expand Down
17 changes: 3 additions & 14 deletions src/compiler/rust/canister/src/ic/call_raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use wasmedge_quickjs::{AsObject, Context, JsFn, JsValue};

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

pub struct NativeFunction;
impl JsFn for NativeFunction {
Expand Down Expand Up @@ -91,7 +91,7 @@ impl JsFn for NativeFunction {
js_exception.dump_error();
panic!("TODO needs error info");
}
_ => {}
_ => run_event_loop(context),
};
} else {
let reject = global
Expand All @@ -113,20 +113,9 @@ impl JsFn for NativeFunction {
js_exception.dump_error();
panic!("TODO needs error info");
}
_ => {}
_ => run_event_loop(context),
};
}

context.promise_loop_poll();

while (true) {
let num_tasks = context.event_loop().unwrap().run_tick_task();
context.promise_loop_poll();

if num_tasks == 0 {
break;
}
}
});
});
});
Expand Down
17 changes: 3 additions & 14 deletions src/compiler/rust/canister/src/ic/call_raw128.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use wasmedge_quickjs::{AsObject, Context, JsFn, JsValue};

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

pub struct NativeFunction;
impl JsFn for NativeFunction {
Expand Down Expand Up @@ -91,7 +91,7 @@ impl JsFn for NativeFunction {
js_exception.dump_error();
panic!("TODO needs error info");
}
_ => {}
_ => run_event_loop(context),
};
} else {
let reject = global
Expand All @@ -113,20 +113,9 @@ impl JsFn for NativeFunction {
js_exception.dump_error();
panic!("TODO needs error info");
}
_ => {}
_ => run_event_loop(context),
};
}

context.promise_loop_poll();

while (true) {
let num_tasks = context.event_loop().unwrap().run_tick_task();
context.promise_loop_poll();

if num_tasks == 0 {
break;
}
}
});
});
});
Expand Down
15 changes: 2 additions & 13 deletions src/compiler/rust/canister/src/ic/set_timer.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use slotmap::Key;
use wasmedge_quickjs::{AsObject, Context, JsFn, JsValue};

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

pub struct NativeFunction;
impl JsFn for NativeFunction {
Expand Down Expand Up @@ -47,20 +47,9 @@ impl JsFn for NativeFunction {
js_exception.dump_error();
panic!("TODO needs error info");
}
_ => {}
_ => run_event_loop(context),
};

context.promise_loop_poll();

while (true) {
let num_tasks = context.event_loop().unwrap().run_tick_task();
context.promise_loop_poll();

if num_tasks == 0 {
break;
}
}

// TODO handle errors
});
});
Expand Down
15 changes: 2 additions & 13 deletions src/compiler/rust/canister/src/ic/set_timer_interval.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use slotmap::Key;
use wasmedge_quickjs::{AsObject, Context, JsFn, JsValue};

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

pub struct NativeFunction;
impl JsFn for NativeFunction {
Expand Down Expand Up @@ -47,20 +47,9 @@ impl JsFn for NativeFunction {
js_exception.dump_error();
panic!("TODO needs error info");
}
_ => {}
_ => run_event_loop(context),
};

context.promise_loop_poll();

while (true) {
let num_tasks = context.event_loop().unwrap().run_tick_task();
context.promise_loop_poll();

if num_tasks == 0 {
break;
}
}

// TODO handle errors
});
});
Expand Down
51 changes: 16 additions & 35 deletions src/compiler/rust/canister/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,19 +108,8 @@ fn execute_js(function_name: &str, pass_arg_data: bool) {
js_exception.dump_error();
panic!("TODO needs error info");
}
_ => {}
_ => run_event_loop(context),
};

context.promise_loop_poll();

while (true) {
let num_tasks = context.event_loop().unwrap().run_tick_task();
context.promise_loop_poll();

if num_tasks == 0 {
break;
}
}
});
});
}
Expand All @@ -146,16 +135,7 @@ pub fn get_candid_pointer() -> *mut std::os::raw::c_char {
"azle_main",
);

context.promise_loop_poll();

while (true) {
let num_tasks = context.event_loop().unwrap().run_tick_task();
context.promise_loop_poll();

if num_tasks == 0 {
break;
}
}
run_event_loop(context);

let global = context.get_global();

Expand All @@ -172,21 +152,9 @@ pub fn get_candid_pointer() -> *mut std::os::raw::c_char {
js_exception.dump_error();
panic!("TODO needs error info");
}
_ => {}
_ => run_event_loop(context),
};

context.promise_loop_poll();

// TODO did I put this in the right place?
while (true) {
let num_tasks = context.event_loop().unwrap().run_tick_task();
context.promise_loop_poll();

if num_tasks == 0 {
break;
}
}

let candid_info_string = candid_info.to_string().unwrap().to_string();

let c_string = std::ffi::CString::new(candid_info_string).unwrap();
Expand All @@ -195,3 +163,16 @@ pub fn get_candid_pointer() -> *mut std::os::raw::c_char {
})
})
}

fn run_event_loop(context: &mut wasmedge_quickjs::Context) {
context.promise_loop_poll();

while (true) {
let num_tasks = context.event_loop().unwrap().run_tick_task();
context.promise_loop_poll();

if num_tasks == 0 {
break;
}
}
}
42 changes: 9 additions & 33 deletions src/compiler/rust/canister_methods/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,7 @@ pub fn canister_methods(_: TokenStream) -> TokenStream {
context.eval_global_str("globalThis.exports = {};".to_string());
context.eval_module_str(std::str::from_utf8(MAIN_JS).unwrap().to_string(), "azle_main");

context.promise_loop_poll();

while (true) {
let num_tasks = context.event_loop().unwrap().run_tick_task();
context.promise_loop_poll();

if num_tasks == 0 {
break;
}
}
run_event_loop(context);

// let temp = context.eval_module_str(std::str::from_utf8(MAIN_JS).unwrap().to_string(), "azle_main");

Expand Down Expand Up @@ -129,16 +120,7 @@ pub fn canister_methods(_: TokenStream) -> TokenStream {
context.eval_global_str("globalThis.exports = {};".to_string());
context.eval_module_str(std::str::from_utf8(MAIN_JS).unwrap().to_string(), "azle_main");

context.promise_loop_poll();

while (true) {
let num_tasks = context.event_loop().unwrap().run_tick_task();
context.promise_loop_poll();

if num_tasks == 0 {
break;
}
}
run_event_loop(context);

// let temp = context.eval_module_str(std::str::from_utf8(MAIN_JS).unwrap().to_string(), "azle_main");

Expand Down Expand Up @@ -320,26 +302,20 @@ fn get_guard_token_stream(
// TODO we would really like wasmedge-quickjs to add
// TODO good error info to JsException and move error handling
// TODO out of our own code
let final_result = match &result {
match &result {
wasmedge_quickjs::JsValue::Exception(js_exception) => {
js_exception.dump_error();
Err("TODO needs error info".to_string())
}
_ => Ok(())
};

context.promise_loop_poll();
_ => {
// TODO what if errors happen in here?
// TODO can guard functions even be async?
// TODO I don't think they can
run_event_loop(context);

while (true) {
let num_tasks = context.event_loop().unwrap().run_tick_task();
context.promise_loop_poll();

if num_tasks == 0 {
break;
Ok(())
}
}

final_result
})
})
}
Expand Down
11 changes: 3 additions & 8 deletions src/compiler/utils/global_paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,6 @@ export const GLOBAL_AZLE_BIN_DIR = join(
'bin'
);

export const GLOBAL_AZLE_WASMEDGE_QUICKJS_DIR = join(
GLOBAL_AZLE_CONFIG_DIR,
azleVersion,
'wasmedge-quickjs'
);

// This is for local development of wasmedge-quickjs js modules
// export const GLOBAL_AZLE_WASMEDGE_QUICKJS_DIR = '/home/wasmedge-quickjs';
export const GLOBAL_AZLE_WASMEDGE_QUICKJS_DIR =
process.env.AZLE_WASMEDGE_QUICKJS_DIR ??
join(GLOBAL_AZLE_CONFIG_DIR, azleVersion, 'wasmedge-quickjs');
3 changes: 0 additions & 3 deletions src/lib/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { AzleIc } from './ic/types/azle_ic';
import { Buffer } from 'buffer';
import { replacer } from './stable_structures/stable_json';
import * as process from 'process';
import { HttpResponse } from './server';

declare global {
var _azleInsideCanister: boolean;
Expand All @@ -14,8 +13,6 @@ declare global {
var _azleIcTimers: { [key: string]: string };
var _azleTimerCallbacks: { [key: string]: () => void };
var _azleGuardFunctions: { [key: string]: () => any };
var _azleExportedIc: typeof ic;
var _azleHttpResponse: typeof HttpResponse;
}

globalThis._azleInsideCanister =
Expand Down
Loading

0 comments on commit 9398ff6

Please sign in to comment.