Skip to content

Commit

Permalink
Merge pull request #2249 from demergent-labs/remove_unnecessary_exper…
Browse files Browse the repository at this point in the history
…imental

Remove unnecessary experimental
  • Loading branch information
lastmjs authored Nov 1, 2024
2 parents 137bea9 + 2f5545e commit 9956d04
Show file tree
Hide file tree
Showing 28 changed files with 89 additions and 94 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 @@ -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 @@ -7,8 +7,8 @@ use crate::{
error::{handle_promise_error, quickjs_call_with_error_handling},
ic::register,
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 c_char;
Expand Down Expand Up @@ -42,9 +42,10 @@ fn initialize_and_get_candid() -> Result<CCharPtr, Box<dyn Error>> {

register(ctx.clone())?;

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

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use crate::{
execute_method_js::execute_method_js,
ic::register,
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 @@ -55,12 +55,19 @@ fn initialize(init: bool, function_index: i32, pass_arg_data: i32) -> Result<(),

let js = get_js_code();

initialize_js(str::from_utf8(&js)?, init, function_index, pass_arg_data)?;
initialize_js(
&wasm_data,
str::from_utf8(&js)?,
init,
function_index,
pass_arg_data,
)?;

Ok(())
}

pub fn initialize_js(
wasm_data: &WasmData,
js: &str,
init: bool,
function_index: i32,
Expand Down Expand Up @@ -112,7 +119,7 @@ pub fn initialize_js(

register(ctx.clone())?;

let promise = Module::evaluate(ctx.clone(), MODULE_NAME, js)?;
let promise = 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 @@ -22,10 +22,6 @@ mod quickjs_with_ctx;
mod stable_b_tree_map;
mod wasm_binary_manipulation;

// 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 @@ -7,6 +7,8 @@ use serde_json;
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;
};
2 changes: 1 addition & 1 deletion src/lib/experimental/canister_methods/execute_method.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import '../experimental';

import { handleUncaughtError } from '../../stable/error';
import { CandidType } from '../candid/candid_type';
import { decode } from '../candid/serde/decode';
import { encode } from '../candid/serde/encode';
import { handleUncaughtError } from '../error';
import { ic } from '../ic';
import { CanisterMethodInfo } from './types/canister_method_info';

Expand Down
10 changes: 0 additions & 10 deletions src/lib/experimental/error.ts

This file was deleted.

Loading

0 comments on commit 9956d04

Please sign in to comment.