Skip to content

Commit

Permalink
Merge pull request #2193 from demergent-labs/rquickjs_move_crates
Browse files Browse the repository at this point in the history
Rquickjs move crates
  • Loading branch information
lastmjs authored Oct 30, 2024
2 parents 342dbc0 + 51a275c commit b1592ee
Show file tree
Hide file tree
Showing 210 changed files with 2,402 additions and 314 deletions.
86 changes: 65 additions & 21 deletions Cargo.lock

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

6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
[workspace]
members = ["src/build/rust/canister", "src/build/rust/open_value_sharing"]
members = [
"src/build/experimental/commands/compile/wasm_binary/rust/experimental_canister_template",
"src/build/experimental/commands/compile/wasm_binary/rust/open_value_sharing",
"src/build/stable/commands/compile/wasm_binary/rust/stable_canister_template",
]
Binary file modified canister_templates/experimental.wasm
Binary file not shown.
Binary file modified canister_templates/stable.wasm
Binary file not shown.
1 change: 1 addition & 0 deletions src/build/experimental/commands/compile/get_context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export async function getContext(
consumer,
managementDid,
recordBenchmarks:
process.env.npm_lifecycle_event === 'pre_tests' ||
process.env.npm_lifecycle_event === 'pretest' ||
process.env.npm_lifecycle_event === 'test'
? process.env.AZLE_RECORD_BENCHMARKS !== 'false'
Expand Down
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
);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "canister"
name = "experimental_canister_template"
version = "0.0.0"
edition = "2018"

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::cell::RefCell;
use std::collections::BTreeMap;

use candid::CandidType;
use wasmedge_quickjs::{AsObject, Context};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub fn get_candid_and_method_meta_pointer() -> *mut std::os::raw::c_char {
// 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 = {EXPERIMENTAL};"));
context.eval_module_str(std::str::from_utf8(&js).unwrap().to_string(), "azle_main");
context.eval_module_str(std::str::from_utf8(&js).unwrap().to_string(), "main");

run_event_loop(context);

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub extern "C" fn execute_method_js(function_index: i32, pass_arg_data: i32) {
runtime.run_with_context(|context| {
let global = context.get_global();
let callbacks = global.get("_azleCallbacks");

let method_callback = callbacks.get(&function_name).unwrap();

let candid_args = if pass_arg_data {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -312,5 +312,5 @@ pub fn register(context: &mut wasmedge_quickjs::Context) {
context.new_function::<trap::NativeFunction>("").into(),
);

context.get_global().set("_azleIc", ic.into());
context.get_global().set("_azleIcExperimental", ic.into());
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,11 @@ pub fn initialize_js(js: &str, init: bool, function_index: i32, pass_arg_data: i
context.eval_global_str(format!(
"globalThis._azleRecordBenchmarks = {record_benchmarks};"
));
context.eval_module_str(js.to_string(), "azle_main");
context.eval_module_str(js.to_string(), "main");

run_event_loop(context);

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

// match &temp {
// wasmedge_quickjs::JsValue::Exception(js_exception) => {
Expand Down
File renamed without changes.
6 changes: 5 additions & 1 deletion src/build/experimental/commands/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@ export async function runCommand(ioType: IOType): Promise<void> {
AZLE_PACKAGE_PATH,
'src',
'build',
'experimental',
'commands',
'compile',
'wasm_binary',
'rust',
'canister',
'experimental_canister_template',
'Cargo.toml'
),
EXPERIMENTAL_STATIC_CANISTER_TEMPLATE_PATH,
Expand Down
1 change: 1 addition & 0 deletions src/build/stable/commands/compile/get_context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export function getContext(
const wasmData: WasmData = {
envVars,
recordBenchmarks:
process.env.npm_lifecycle_event === 'pre_tests' ||
process.env.npm_lifecycle_event === 'pretest' ||
process.env.npm_lifecycle_event === 'test'
? process.env.AZLE_RECORD_BENCHMARKS !== 'false'
Expand Down
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/canister.wasm ${wasmDest}`,
`wasi2ic target/wasm32-wasi/release/stable_canister_template.wasm ${wasmDest}`,
ioType
);
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[package]
name = "stable_canister_template"
version = "0.0.0"
edition = "2018"

[lib]
crate-type = ["cdylib"]

[dependencies]
candid = "0.10.2"
candid_parser = "0.1.2"
ic-cdk = "0.12.2"
ic-cdk-macros = "0.8.4"
ic-cdk-timers = "0.6.0"
ic-stable-structures = "0.6.5"
ic-wasi-polyfill = "0.6.1"
rquickjs = { version = "0.6.2", features = ["array-buffer"] }
serde = "1.0.202"
serde_json = "1.0.107"
slotmap = "=1.0.6"
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use std::{cell::RefCell, error::Error};

use candid::CandidType;

use crate::quickjs_with_ctx;

#[derive(CandidType, Debug, Clone)]
pub struct BenchmarkEntry {
pub method_name: String,
pub instructions: u64,
pub timestamp: u64,
}

thread_local! {
pub static BENCHMARKS_REF_CELL: RefCell<Vec<BenchmarkEntry>> = RefCell::new(Vec::new());
}

pub fn record_benchmark(function_name: &str, instructions: u64) -> Result<(), Box<dyn Error>> {
quickjs_with_ctx(|ctx| {
let timestamp = ic_cdk::api::time();

let method_names: rquickjs::Object =
ctx.clone().globals().get("_azleCanisterMethodNames")?;

let method_name: String = method_names.get(function_name)?;

BENCHMARKS_REF_CELL.with(|benchmarks_ref_cell| {
let mut benchmarks = benchmarks_ref_cell.borrow_mut();
benchmarks.push(BenchmarkEntry {
method_name,
instructions,
timestamp,
});
});

Ok(())
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use crate::{
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

// Heavily inspired by https://stackoverflow.com/a/47676844
#[no_mangle]
pub fn get_candid_and_method_meta_pointer() -> *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);
}));

let runtime = rquickjs::Runtime::new().unwrap();
let context = rquickjs::Context::full(&runtime).unwrap();

CONTEXT_REF_CELL.with(|context_ref_cell| {
*context_ref_cell.borrow_mut() = Some(context);
});

quickjs_with_ctx(|ctx| {
ctx.clone()
.globals()
.set("_azleNodeWasmEnvironment", true)
.unwrap();

ic::register(ctx.clone());

ctx.clone()
.globals()
.set("exports", rquickjs::Object::new(ctx.clone()).unwrap())
.unwrap();

ctx.clone()
.globals()
.set("_azleExperimental", false)
.unwrap();

let js = get_js_code();

// 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();

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

let candid_and_method_meta: String = get_candid_and_method_meta.call(()).unwrap();

let c_string = std::ffi::CString::new(candid_and_method_meta).unwrap();

c_string.into_raw()
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#[allow(unused)]
pub async fn chunk() {
let id = ic_cdk::id();
let method = "_azle_chunk";
let args_raw = [68, 73, 68, 76, 0, 0]; // '()' pre encoded
let _ = ic_cdk::api::call::call_raw128(id, method, args_raw, 0).await;
}
Loading

0 comments on commit b1592ee

Please sign in to comment.