Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove unnecessary experimental #2249

Merged
merged 11 commits into from
Nov 1, 2024
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 @@ -8,7 +8,7 @@ export function compile(
ioType: IOType
): void {
execSyncPretty(
`CARGO_TARGET_DIR=target cargo build --target wasm32-wasi --manifest-path ${manifestPath} --release --features "experimental"`,
`CARGO_TARGET_DIR=target cargo build --target wasm32-wasi --manifest-path ${manifestPath} --release`,
ioType
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ edition = "2018"
[lib]
crate-type = ["cdylib"]

[features]
experimental = ["open_value_sharing", "wasmi"]

[dependencies]
anyhow = "1.0.75"
ic-cdk = "0.12.2"
Expand All @@ -17,18 +14,12 @@ ic-cdk-timers = "0.6.0"
candid = "0.10.2"
candid_parser = "0.1.2"
ic-stable-structures = "0.6.5"
open_value_sharing = { path = "../open_value_sharing" }
slotmap = "=1.0.6"
sha2 = "0.10.8"
serde = "1.0.202"
serde_json = "1.0.107"
ic-wasi-polyfill = "0.6.1"
wasmedge_quickjs = { git = "https://github.com/demergent-labs/wasmedge-quickjs", rev = "573c6c07316de64e4bb9a9561b079f265fd9bcc4" }
# wasmedge_quickjs = { path = "/home/wasmedge-quickjs" }

[dependencies.open_value_sharing]
optional = true
path = "../open_value_sharing"

[dependencies.wasmi]
optional = true
version = "0.31.2"
wasmi = "0.31.2"
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{cell::RefCell, collections::BTreeMap};

use crate::init_and_post_upgrade::initialize_js;
use crate::{init_and_post_upgrade::initialize_js, WASM_DATA_REF_CELL};

thread_local! {
static RELOADED_JS_TIMESTAMP: RefCell<u64> = RefCell::new(0);
Expand Down Expand Up @@ -35,8 +35,11 @@ pub fn reload_js(
reloaded_js_mut.values().flat_map(|v| v.clone()).collect();

if reloaded_js_complete_bytes.len() as u64 == total_len {
let wasm_data = WASM_DATA_REF_CELL
.with(|wasm_data_ref_cell| wasm_data_ref_cell.borrow().as_ref().unwrap().clone());

let js_string = String::from_utf8_lossy(&reloaded_js_complete_bytes);
initialize_js(&js_string, false, function_index, 1); // TODO should the last arg be 0?
initialize_js(&wasm_data, &js_string, false, function_index, 1); // TODO should the last arg be 0?
ic_cdk::println!("Azle: Reloaded canister JavaScript");
}
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use wasmedge_quickjs::AsObject;

use crate::{ic, run_event_loop, wasm_binary_manipulation::get_js_code, EXPERIMENTAL, RUNTIME};
use crate::{
ic, run_event_loop,
wasm_binary_manipulation::{get_js_code, get_wasm_data},
RUNTIME,
};

// Heavily inspired by https://stackoverflow.com/a/47676844
#[no_mangle]
Expand Down Expand Up @@ -32,12 +36,16 @@ pub fn get_candid_and_method_meta_pointer() -> *mut std::os::raw::c_char {

ic::register(context);

let wasm_data = get_wasm_data();
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 = {EXPERIMENTAL};"));
context.eval_module_str(std::str::from_utf8(&js).unwrap().to_string(), "main");
context.eval_global_str(format!("globalThis._azleExperimental = true;"));
context.eval_module_str(
std::str::from_utf8(&js).unwrap().to_string(),
&wasm_data.main_js_path,
);

run_event_loop(context);

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

use crate::{
execute_method_js, ic, run_event_loop, wasm_binary_manipulation::get_js_code,
wasm_binary_manipulation::get_wasm_data, EXPERIMENTAL, MEMORY_MANAGER_REF_CELL, RUNTIME,
WASM_DATA_REF_CELL,
execute_method_js, ic, run_event_loop,
wasm_binary_manipulation::get_wasm_data,
wasm_binary_manipulation::{get_js_code, WasmData},
MEMORY_MANAGER_REF_CELL, RUNTIME, WASM_DATA_REF_CELL,
};

#[cfg(feature = "experimental")]
use crate::{upload_file, web_assembly};

#[inline(never)]
Expand All @@ -20,7 +20,6 @@ pub extern "C" fn init(function_index: i32, pass_arg_data: i32) {

initialize(true, function_index, pass_arg_data);

#[cfg(feature = "experimental")]
upload_file::init_hashes().unwrap();
}

Expand Down Expand Up @@ -69,31 +68,35 @@ fn initialize(init: bool, function_index: i32, pass_arg_data: i32) {
MEMORY_MANAGER_REF_CELL.with(|manager| manager.borrow().get(MemoryId::new(254)));
ic_wasi_polyfill::init_with_memory(&[], &env_vars, polyfill_memory);

#[cfg(feature = "experimental")]
std::fs::write("/candid/icp/management.did", &wasm_data.management_did).unwrap();

let js = get_js_code();

initialize_js(
&wasm_data,
std::str::from_utf8(&js).unwrap(),
init,
function_index,
pass_arg_data,
);

#[cfg(feature = "experimental")]
ic_cdk::spawn(async move {
open_value_sharing::init(&wasm_data.consumer).await;
});
}

pub fn initialize_js(js: &str, init: bool, function_index: i32, pass_arg_data: i32) {
pub fn initialize_js(
wasm_data: &WasmData,
js: &str,
init: bool,
function_index: i32,
pass_arg_data: i32,
) {
let mut rt = wasmedge_quickjs::Runtime::new();

rt.run_with_context(|context| {
ic::register(context);

#[cfg(feature = "experimental")]
web_assembly::register(context);

let mut env = context.new_object();
Expand All @@ -115,18 +118,12 @@ pub fn initialize_js(js: &str, init: bool, function_index: i32, pass_arg_data: i

// 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};"));
let record_benchmarks = WASM_DATA_REF_CELL.with(|wasm_data_ref_cell| {
wasm_data_ref_cell
.borrow()
.as_ref()
.unwrap()
.record_benchmarks
});
context.eval_global_str(format!("globalThis._azleExperimental = true;"));
context.eval_global_str(format!(
"globalThis._azleRecordBenchmarks = {record_benchmarks};"
"globalThis._azleRecordBenchmarks = {};",
wasm_data.record_benchmarks
));
context.eval_module_str(js.to_string(), "main");
context.eval_module_str(js.to_string(), &wasm_data.main_js_path);

run_event_loop(context);

Expand Down Expand Up @@ -160,9 +157,9 @@ pub fn initialize_js(js: &str, init: bool, function_index: i32, pass_arg_data: i

runtime.run_with_context(|context| {
let assignment = if init {
"globalThis._azleInitCalled = true;"
"globalThis._azleInitCalled = true;\nglobalThis._azlePostUpgradeCalled = false;"
} else {
"globalThis._azlePostUpgradeCalled = true;"
"globalThis._azleInitCalled = false;\nglobalThis._azlePostUpgradeCalled = true;"
};

context.eval_global_str(assignment.to_string());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use ic_stable_structures::{
DefaultMemoryImpl,
};

#[cfg(feature = "experimental")]
mod autoreload;
mod benchmarking;
mod candid;
Expand All @@ -22,10 +21,8 @@ mod guards;
mod ic;
mod init_and_post_upgrade;
mod stable_b_tree_map;
#[cfg(feature = "experimental")]
mod upload_file;
mod wasm_binary_manipulation;
#[cfg(feature = "experimental")]
mod web_assembly;

#[allow(unused)]
Expand All @@ -37,8 +34,6 @@ thread_local! {
static WASM_DATA_REF_CELL: RefCell<Option<wasm_binary_manipulation::WasmData>> = RefCell::new(None);
}

const EXPERIMENTAL: bool = cfg!(feature = "experimental");

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

Expand All @@ -56,7 +51,6 @@ pub fn run_event_loop(context: &mut wasmedge_quickjs::Context) {
#[ic_cdk_macros::update]
pub fn _azle_chunk() {}

#[cfg(feature = "experimental")]
#[ic_cdk_macros::update(guard = guard_against_non_controllers)]
fn _azle_reload_js(
timestamp: u64,
Expand All @@ -68,7 +62,6 @@ fn _azle_reload_js(
autoreload::reload_js(timestamp, chunk_number, js_bytes, total_len, function_index);
}

#[cfg(feature = "experimental")]
#[ic_cdk_macros::update(guard = guard_against_non_controllers)]
pub async fn _azle_upload_file_chunk(
dest_path: String,
Expand All @@ -87,13 +80,11 @@ pub async fn _azle_upload_file_chunk(
.await
}

#[cfg(feature = "experimental")]
#[ic_cdk_macros::update(guard = guard_against_non_controllers)]
pub fn _azle_clear_file_and_info(path: String) {
upload_file::reset_for_new_upload(&path, 0).unwrap()
}

#[cfg(feature = "experimental")]
#[ic_cdk_macros::query(guard = guard_against_non_controllers)]
pub fn _azle_get_file_hash(path: String) -> Option<String> {
upload_file::get_file_hash(path)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
#[cfg(feature = "experimental")]
use open_value_sharing::Consumer;

use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct WasmData {
pub consumer: Consumer,
#[serde(rename = "envVars")]
pub env_vars: Vec<(String, String)>,
#[serde(rename = "recordBenchmarks")]
pub record_benchmarks: bool,
#[cfg(feature = "experimental")]
pub consumer: Consumer,
#[cfg(feature = "experimental")]
#[serde(rename = "mainJsPath")]
pub main_js_path: String,
#[serde(rename = "managementDid")]
pub management_did: String,
#[serde(rename = "recordBenchmarks")]
pub record_benchmarks: bool,
}

#[inline(never)]
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 @@ -27,6 +27,7 @@ export function getContext(
const envVars = getEnvVars(canisterConfig);
const wasmData: WasmData = {
envVars,
mainJsPath: join(canisterPath, `main.js`),
recordBenchmarks:
process.env.npm_lifecycle_event === 'pre_tests' ||
process.env.npm_lifecycle_event === 'pretest' ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use std::error::Error;
use crate::{
error::{handle_promise_error, quickjs_call_with_error_handling},
ic, quickjs_with_ctx,
wasm_binary_manipulation::get_js_code,
CONTEXT_REF_CELL, MODULE_NAME,
wasm_binary_manipulation::{get_js_code, get_wasm_data},
CONTEXT_REF_CELL,
};

type CCharPtr = *mut std::os::raw::c_char;
Expand Down Expand Up @@ -40,10 +40,14 @@ fn initialize_and_get_candid() -> Result<CCharPtr, Box<dyn Error>> {

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

let wasm_data = get_wasm_data()?;
let js = get_js_code();

let promise =
rquickjs::Module::evaluate(ctx.clone(), MODULE_NAME, std::str::from_utf8(&js)?)?;
let promise = rquickjs::Module::evaluate(
ctx.clone(),
wasm_data.main_js_path,
std::str::from_utf8(&js)?,
)?;

handle_promise_error(ctx.clone(), promise)?;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use crate::{
error::handle_promise_error,
execute_method_js::execute_method_js,
ic, quickjs_with_ctx,
wasm_binary_manipulation::{get_js_code, get_wasm_data},
CONTEXT_REF_CELL, MEMORY_MANAGER_REF_CELL, MODULE_NAME, WASM_DATA_REF_CELL,
wasm_binary_manipulation::{get_js_code, get_wasm_data, WasmData},
CONTEXT_REF_CELL, MEMORY_MANAGER_REF_CELL, WASM_DATA_REF_CELL,
};

#[inline(never)]
Expand Down Expand Up @@ -54,6 +54,7 @@ fn initialize(
let js = get_js_code();

initialize_js(
&wasm_data,
std::str::from_utf8(&js)?,
init,
function_index,
Expand All @@ -64,6 +65,7 @@ fn initialize(
}

pub fn initialize_js(
wasm_data: &WasmData,
js: &str,
init: bool,
function_index: i32,
Expand Down Expand Up @@ -107,19 +109,13 @@ pub fn initialize_js(
ctx.clone().globals().set("_azlePostUpgradeCalled", true)?;
}

let record_benchmarks = WASM_DATA_REF_CELL
.with(|wasm_data_ref_cell| wasm_data_ref_cell.borrow().clone())
.as_ref()
.ok_or("could not convert wasm_data_ref_cell to ref")?
.record_benchmarks;

ctx.clone()
.globals()
.set("_azleRecordBenchmarks", record_benchmarks)?;
.set("_azleRecordBenchmarks", wasm_data.record_benchmarks)?;

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

let promise = rquickjs::Module::evaluate(ctx.clone(), MODULE_NAME, js)?;
let promise = rquickjs::Module::evaluate(ctx.clone(), wasm_data.main_js_path.clone(), js)?;

handle_promise_error(ctx.clone(), promise)?;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ mod wasm_binary_manipulation;
use guards::guard_against_non_controllers;
pub use quickjs_with_ctx::quickjs_with_ctx;

// TODO dynamically get the canister name
// TODO send it in through the Wasm meta data
const MODULE_NAME: &str = ".azle/[canister_name]/main.js";

#[allow(unused)]
type Memory = VirtualMemory<DefaultMemoryImpl>;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use serde::{Deserialize, Serialize};
pub struct WasmData {
#[serde(rename = "envVars")]
pub env_vars: Vec<(String, String)>,
#[serde(rename = "mainJsPath")]
pub main_js_path: String,
#[serde(rename = "recordBenchmarks")]
pub record_benchmarks: bool,
}
Expand Down
1 change: 1 addition & 0 deletions src/build/stable/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,6 @@ export type MethodMeta = {

export type WasmData = {
envVars: EnvVars;
mainJsPath: string;
recordBenchmarks: boolean;
};
Loading
Loading