-
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.
Merge pull request #2000 from demergent-labs/refactor_rust
Refactor rust
- Loading branch information
Showing
29 changed files
with
501 additions
and
528 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
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
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
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
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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
use std::{cell::RefCell, collections::BTreeMap}; | ||
|
||
use crate::init_and_post_upgrade::initialize_js; | ||
|
||
thread_local! { | ||
static RELOADED_JS_TIMESTAMP: RefCell<u64> = RefCell::new(0); | ||
static RELOADED_JS: RefCell<BTreeMap<u64, Vec<u8>>> = RefCell::new(BTreeMap::new()); | ||
} | ||
|
||
pub fn reload_js( | ||
timestamp: u64, | ||
chunk_number: u64, | ||
js_bytes: Vec<u8>, | ||
total_len: u64, | ||
function_index: i32, | ||
) { | ||
RELOADED_JS_TIMESTAMP.with(|reloaded_js_timestamp| { | ||
let mut reloaded_js_timestamp_mut = reloaded_js_timestamp.borrow_mut(); | ||
|
||
if timestamp > *reloaded_js_timestamp_mut { | ||
*reloaded_js_timestamp_mut = timestamp; | ||
|
||
RELOADED_JS.with(|reloaded_js| { | ||
let mut reloaded_js_mut = reloaded_js.borrow_mut(); | ||
reloaded_js_mut.clear(); | ||
}); | ||
} | ||
}); | ||
|
||
RELOADED_JS.with(|reloaded_js| { | ||
let mut reloaded_js_mut = reloaded_js.borrow_mut(); | ||
reloaded_js_mut.insert(chunk_number, js_bytes); | ||
|
||
let reloaded_js_complete_bytes: Vec<u8> = | ||
reloaded_js_mut.values().flat_map(|v| v.clone()).collect(); | ||
|
||
if reloaded_js_complete_bytes.len() as u64 == total_len { | ||
let js_string = String::from_utf8_lossy(&reloaded_js_complete_bytes); | ||
initialize_js(&js_string, false, function_index, 1, true); // TODO should the last arg be 0? | ||
ic_cdk::println!("Azle: Reloaded canister JavaScript"); | ||
} | ||
}); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,72 @@ | ||
use wasmedge_quickjs::AsObject; | ||
|
||
use crate::{ic, run_event_loop, wasm_binary_manipulation::get_js_code, RUNTIME}; | ||
|
||
// Heavily inspired by https://stackoverflow.com/a/47676844 | ||
#[no_mangle] | ||
pub fn get_candid_pointer(experimental: i32) -> *mut std::os::raw::c_char { | ||
std::panic::set_hook(Box::new(|panic_info| { | ||
let msg = match panic_info.payload().downcast_ref::<&str>() { | ||
Some(s) => *s, | ||
None => "Unknown panic message", | ||
}; | ||
let location = if let Some(location) = panic_info.location() { | ||
format!(" at {}:{}", location.file(), location.line()) | ||
} else { | ||
" (unknown location)".to_string() | ||
}; | ||
|
||
let message = &format!("Panic occurred: {}{}", msg, location); | ||
|
||
ic_cdk::println!("{}", message); | ||
})); | ||
|
||
RUNTIME.with(|_| { | ||
let mut runtime = wasmedge_quickjs::Runtime::new(); | ||
|
||
runtime.run_with_context(|context| { | ||
context.get_global().set( | ||
"_azleWasmtimeCandidEnvironment", | ||
wasmedge_quickjs::JsValue::Bool(true), | ||
); | ||
|
||
ic::register(context); | ||
|
||
let js = get_js_code(); | ||
|
||
// TODO what do we do if there is an error in here? | ||
context.eval_global_str("globalThis.exports = {};".to_string()); | ||
context.eval_global_str(format!( | ||
"globalThis._azleExperimental = {};", | ||
if experimental == 1 { "true" } else { "false" } | ||
)); | ||
context.eval_module_str(std::str::from_utf8(&js).unwrap().to_string(), "azle_main"); | ||
|
||
run_event_loop(context); | ||
|
||
let global = context.get_global(); | ||
|
||
let candid_info_function = global.get("candidInfoFunction").to_function().unwrap(); | ||
|
||
let candid_info = candid_info_function.call(&[]); | ||
|
||
// TODO error handling is mostly done in JS right now | ||
// 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 | ||
match &candid_info { | ||
wasmedge_quickjs::JsValue::Exception(js_exception) => { | ||
js_exception.dump_error(); | ||
panic!("TODO needs error info"); | ||
} | ||
_ => run_event_loop(context), | ||
}; | ||
|
||
let candid_info_string = candid_info.to_string().unwrap().to_string(); | ||
|
||
let c_string = std::ffi::CString::new(candid_info_string).unwrap(); | ||
|
||
c_string.into_raw() | ||
}) | ||
}) | ||
} |
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,3 +1,4 @@ | ||
#[allow(unused)] | ||
pub async fn chunk() { | ||
let id = ic_cdk::id(); | ||
let method = "_azle_chunk"; | ||
|
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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
use wasmedge_quickjs::AsObject; | ||
|
||
use crate::{run_event_loop, RUNTIME}; | ||
|
||
#[no_mangle] | ||
#[allow(unused)] | ||
pub extern "C" fn execute_method_js(function_index: i32, pass_arg_data: i32) { | ||
let function_name = &function_index.to_string(); | ||
let pass_arg_data = if pass_arg_data == 1 { true } else { false }; | ||
|
||
RUNTIME.with(|runtime| { | ||
let mut runtime = runtime.borrow_mut(); | ||
let runtime = runtime.as_mut().unwrap(); | ||
|
||
runtime.run_with_context(|context| { | ||
let global = context.get_global(); | ||
let exports = global.get("exports"); | ||
|
||
let canister_methods = exports.get("canisterMethods").unwrap(); | ||
|
||
let callbacks = canister_methods.get("callbacks").unwrap(); | ||
let method_callback = callbacks.get(function_name).unwrap(); | ||
|
||
let candid_args = if pass_arg_data { | ||
ic_cdk::api::call::arg_data_raw() | ||
} else { | ||
vec![] | ||
}; | ||
|
||
let candid_args_js_value: wasmedge_quickjs::JsValue = | ||
context.new_array_buffer(&candid_args).into(); | ||
|
||
let method_callback_function = method_callback.to_function().unwrap(); | ||
|
||
let result = method_callback_function.call(&[candid_args_js_value]); | ||
|
||
// TODO error handling is mostly done in JS right now | ||
// 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 | ||
match &result { | ||
wasmedge_quickjs::JsValue::Exception(js_exception) => { | ||
js_exception.dump_error(); | ||
panic!("TODO needs error info"); | ||
} | ||
_ => run_event_loop(context), | ||
}; | ||
}); | ||
}); | ||
} |
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
2 changes: 1 addition & 1 deletion
2
src/compiler/rust/canister/src/ic/stable_b_tree_map_contains_key.rs
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
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
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
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
2 changes: 1 addition & 1 deletion
2
src/compiler/rust/canister/src/ic/stable_b_tree_map_is_empty.rs
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
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
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
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
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
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
Oops, something went wrong.