From 7e029b44a5b4afb247c259d76378e63fcdb0ecc3 Mon Sep 17 00:00:00 2001 From: Jordan Last Date: Mon, 25 Sep 2023 18:26:12 -0500 Subject: [PATCH] fixing pre_and_post_upgrade functionality and test --- examples/pre_and_post_upgrade/src/index.ts | 2 +- examples/pre_and_post_upgrade/test/tests.ts | 4 ++-- .../azle_generate_rearchitecture/src/main.rs | 23 +++++++++++-------- .../cargo_toml_files.ts | 1 + .../candid/reference/service.ts | 4 ++-- src/lib_functional/canister_methods/index.ts | 6 ++++- 6 files changed, 25 insertions(+), 15 deletions(-) diff --git a/examples/pre_and_post_upgrade/src/index.ts b/examples/pre_and_post_upgrade/src/index.ts index 3397576878..2fbab823d1 100644 --- a/examples/pre_and_post_upgrade/src/index.ts +++ b/examples/pre_and_post_upgrade/src/index.ts @@ -55,7 +55,7 @@ export default Service({ Object.entries(entries).map((entry) => { return { key: entry[0], - value: entry[1] + value: entry[1] + 1n }; }) ); diff --git a/examples/pre_and_post_upgrade/test/tests.ts b/examples/pre_and_post_upgrade/test/tests.ts index 1a1adb9ead..80cbcd125f 100644 --- a/examples/pre_and_post_upgrade/test/tests.ts +++ b/examples/pre_and_post_upgrade/test/tests.ts @@ -46,7 +46,7 @@ export function getTests( { name: 'deploy', prep: async () => { - execSync(`dfx deploy`, { + execSync(`dfx deploy --upgrade-unchanged`, { stdio: 'inherit' }); } @@ -60,7 +60,7 @@ export function getTests( Ok: result.length === 1 && result[0].key === '0' && - result[0].value === 0n + result[0].value === 1n }; } } diff --git a/src/compiler/typescript_to_rust/azle_generate_rearchitecture/src/main.rs b/src/compiler/typescript_to_rust/azle_generate_rearchitecture/src/main.rs index 2ad79ee237..bc7ec6105c 100644 --- a/src/compiler/typescript_to_rust/azle_generate_rearchitecture/src/main.rs +++ b/src/compiler/typescript_to_rust/azle_generate_rearchitecture/src/main.rs @@ -1,3 +1,6 @@ +// TODO So I'm going to create a macro for the dynamic portion of the code here +// TODO Then I'm going to integrate rquickjs + use crate::traits::to_ident::ToIdent; use quote::quote; use serde::{Deserialize, Serialize}; @@ -56,6 +59,16 @@ fn main() -> Result<(), String> { quote!(execute_js(#js_function_name, true);) }); + let post_upgrade_method_call = + compiler_info + .canister_methods + .post_upgrade + .map(|post_upgrade_method| { + let js_function_name = &post_upgrade_method.name; + + quote!(execute_js(#js_function_name, true);) + }); + let pre_upgrade_method = compiler_info .canister_methods .pre_upgrade @@ -243,20 +256,12 @@ fn main() -> Result<(), String> { context.eval_global("exports.js", "globalThis.exports = {}").unwrap(); context.eval_global("main.js", std::str::from_utf8(MAIN_JS).unwrap()).unwrap(); - let _azle_post_upgrade_name = global.get_property("_azlePostUpgradeName").unwrap(); - let _azle_post_upgrade_name_string = if !_azle_post_upgrade_name.is_undefined() { - let _azle_post_upgrade_name_js_value: JSValue = from_qjs_value(&_azle_post_upgrade_name).unwrap(); - _azle_post_upgrade_name_js_value.try_into().unwrap() - } else { "".to_string() }; - CONTEXT.with(|ctx| { let mut ctx = ctx.borrow_mut(); *ctx = Some(context); }); - if _azle_post_upgrade_name_string != "" { - execute_js(&_azle_post_upgrade_name_string, true); - } + #post_upgrade_method_call } #pre_upgrade_method diff --git a/src/compiler/typescript_to_rust/typescript_to_javascript/cargo_toml_files.ts b/src/compiler/typescript_to_rust/typescript_to_javascript/cargo_toml_files.ts index e658589fb8..f2dddd9978 100644 --- a/src/compiler/typescript_to_rust/typescript_to_javascript/cargo_toml_files.ts +++ b/src/compiler/typescript_to_rust/typescript_to_javascript/cargo_toml_files.ts @@ -77,6 +77,7 @@ export function generateLibCargoToml( ic-wasi-polyfill = { git = "https://github.com/demergent-labs/ic-wasi-polyfill", rev = "f3812b879c096faf930331e76441fadf2ebe6586", features = ["transient"] } quickjs-wasm-rs = {git = "https://github.com/ulan/javy.git", branch="ulan/local-changes"} + # rquickjs = "0.3.1" futures-core = "=0.3.27" anyhow = "1.0.75" diff --git a/src/lib_functional/candid/reference/service.ts b/src/lib_functional/candid/reference/service.ts index 40e18659a2..d10beb45db 100644 --- a/src/lib_functional/candid/reference/service.ts +++ b/src/lib_functional/candid/reference/service.ts @@ -220,8 +220,8 @@ export function Service( `; returnFunction.init = init; - returnFunction.postUpgrade = postUpgrade; - returnFunction.preUpgrade = preUpgrade; + returnFunction.post_upgrade = postUpgrade; + returnFunction.pre_upgrade = preUpgrade; returnFunction.heartbeat = heartbeat; returnFunction.inspect_message = inspectMessage; returnFunction.queries = queries; diff --git a/src/lib_functional/canister_methods/index.ts b/src/lib_functional/canister_methods/index.ts index f1788465a1..e9ad5d98ad 100644 --- a/src/lib_functional/canister_methods/index.ts +++ b/src/lib_functional/canister_methods/index.ts @@ -78,7 +78,11 @@ export function executeMethod( const result = callback(...myDecodedObject); - if (mode === 'init' || mode === 'inspectMessage') { + if ( + mode === 'init' || + mode === 'postUpgrade' || + mode === 'inspectMessage' + ) { return; }