diff --git a/src/compiler/compile_typescript_code.ts b/src/compiler/compile_typescript_code.ts index 216ecc9404..035e7c1d46 100644 --- a/src/compiler/compile_typescript_code.ts +++ b/src/compiler/compile_typescript_code.ts @@ -4,22 +4,9 @@ import { Result } from './utils/result'; import { GLOBAL_AZLE_WASMEDGE_QUICKJS_DIR } from './utils/global_paths'; export function compileTypeScriptToJavaScript( - main: string, - canisterConfig: JSCanisterConfig + main: string ): Result { try { - const globalThisProcess = ` - globalThis.process = { - env: { - ${(canisterConfig.env ?? []) - .map((envVarName) => { - return `'${envVarName}': '${process.env[envVarName]}'`; - }) - .join(',')} - } - }; - `; - const imports = ` // Trying to make sure that all globalThis dependencies are defined // Before the developer imports azle on their own @@ -54,8 +41,7 @@ export function compileTypeScriptToJavaScript( globalThis.exports.canisterMethods = canisterMethods; `; - const bundledJavaScript = bundleAndTranspileJs(` - ${globalThisProcess} + const bundledJavaScript = bundleFromString(` ${imports} `); @@ -67,23 +53,6 @@ export function compileTypeScriptToJavaScript( } } -export function bundleAndTranspileJs(ts: TypeScript): JavaScript { - const jsBundled: JavaScript = bundleFromString(ts); - // const jsTranspiled: JavaScript = transpile(jsBundled); - - // TODO enabling strict mode is causing lots of issues - // TODO it would be nice if I could remove strict mode code in esbuild or swc - // TODO look into the implications of this, but since we are trying to transpile to es3 to cope with missing features in boa, I do not think we need strict mode - // const jsStrictModeRemoved: JavaScript = jsTranspiled.replace( - // /"use strict";/g, - // '' - // ); - - return jsBundled; -} - -// TODO there is a lot of minification/transpiling etc we could do with esbuild or with swc -// TODO we need to decide which to use for what export function bundleFromString(ts: TypeScript): JavaScript { // TODO tree-shaking does not seem to work with stdin. I have learned this from sad experience const buildResult = buildSync({ @@ -125,46 +94,3 @@ export function bundleFromString(ts: TypeScript): JavaScript { return bundleString; } - -// TODO I have left the code for bundleFromPath -// TODO We might run into the situation again where we need to use bundleFromPath -// TODO there are some issues with tree-shaking and possibly some others in bundleFromString, so I will just leave the code here for now until the project is more mature -// function bundleFromPath(tsPath: string): JavaScript { -// const buildResult = buildSync({ -// entryPoints: [tsPath], -// format: 'esm', -// bundle: true, -// treeShaking: true, -// write: false, -// logLevel: 'silent', -// // TODO tsconfig was here to attempt to set importsNotUsedAsValues to true to force Principal to always be bundled -// // TODO now we always bundle Principal for all code, but I am keeping this here in case we run into the problem elsewhere -// // tsconfig: path.join( __dirname, './esbuild-tsconfig.json') // TODO this path resolution may cause problems on non-Linux systems, beware...might not be necessary now that we are using stdin -// }); - -// const bundleArray = buildResult.outputFiles[0].contents; -// const bundleString = Buffer.from(bundleArray).toString('utf-8'); - -// return bundleString; -// } - -// TODO there is a lot of minification/transpiling etc we could do with esbuild or with swc -// TODO we need to decide which to use for what -// function transpile(js: JavaScript): JavaScript { -// return swc.transformSync(js, { -// module: { -// type: 'commonjs' -// }, -// jsc: { -// parser: { -// syntax: 'ecmascript' -// }, -// target: 'es2017', // TODO had to change this to get generator objects natively...not sure what else will break now -// experimental: { -// cacheRoot: '/dev/null' -// }, -// loose: true -// }, -// minify: false // TODO keeping this off for now, enable once the project is more stable -// }).code; -// } diff --git a/src/compiler/index.ts b/src/compiler/index.ts index 76f8da2ac1..db20c6ccf3 100644 --- a/src/compiler/index.ts +++ b/src/compiler/index.ts @@ -23,6 +23,7 @@ import { Err, ok } from './utils/result'; import { AzleError, CompilerInfo, + JSCanisterConfig, Toml, TsCompilationError, TsSyntaxErrorLocation @@ -64,8 +65,7 @@ async function azle() { installRustDependencies(azleVersion, rustVersion); const compilationResult = compileTypeScriptToJavaScript( - canisterConfig.main, - canisterConfig + canisterConfig.main ); if (!ok(compilationResult)) { @@ -113,14 +113,16 @@ async function azle() { // TODO stuff is repeated which is messy and bad of course writeFileSync(`${canisterPath}/canister/src/candid.did`, ''); // This is for the Rust canister to have access to the candid file + const envVars = getEnvVars(canisterConfig); + const compilerInfo0: CompilerInfo = { - // TODO The spread is because canisterMethods is a function with properties canister_methods: { candid: '', queries: [], updates: [], callbacks: {} - } // TODO we should probably just grab the props out that we need + }, + env_vars: envVars }; const compilerInfoPath0 = join( @@ -147,7 +149,8 @@ async function azle() { // TODO The spread is because canisterMethods is a function with properties canister_methods: { ...canisterMethods - } // TODO we should probably just grab the props out that we need + }, // TODO we should probably just grab the props out that we need + env_vars: envVars }; const compilerInfoPath = join( @@ -216,3 +219,9 @@ function generateVisualDisplayOfErrorLocation( )}${marker}`; return `${preciseLocation}\n${previousLine}\n${offendingLine}\n${subsequentLine}`; } + +function getEnvVars(canisterConfig: JSCanisterConfig): [string, string][] { + return (canisterConfig.env ?? []).map((envVarName) => { + return [envVarName, process.env[envVarName] ?? '']; + }); +} diff --git a/src/compiler/rust/canister_methods/src/lib.rs b/src/compiler/rust/canister_methods/src/lib.rs index beab71939d..6cecd28070 100644 --- a/src/compiler/rust/canister_methods/src/lib.rs +++ b/src/compiler/rust/canister_methods/src/lib.rs @@ -21,6 +21,7 @@ impl ToIdent for String { #[derive(Debug, Serialize, Deserialize)] struct CompilerInfo { canister_methods: CanisterMethods, + env_vars: Vec<(String, String)>, } #[derive(Debug, Serialize, Deserialize)] @@ -45,6 +46,12 @@ struct CanisterMethod { pub fn canister_methods(_: TokenStream) -> TokenStream { let compiler_info = get_compiler_info("canister/src/compiler_info.json").unwrap(); + let env_vars: Vec<_> = compiler_info + .env_vars + .iter() + .map(|(key, value)| quote!((#key, #value))) + .collect(); + let init_method_call = compiler_info.canister_methods.init.map(|init_method| { let js_function_name = &init_method.name; @@ -54,7 +61,7 @@ pub fn canister_methods(_: TokenStream) -> TokenStream { let init_method = quote! { #[ic_cdk_macros::init] fn init() { - ic_wasi_polyfill::init(&[], &[]); + ic_wasi_polyfill::init(&[], &[#(#env_vars),*]); let mut rt = wasmedge_quickjs::Runtime::new(); @@ -82,8 +89,6 @@ pub fn canister_methods(_: TokenStream) -> TokenStream { // ic_cdk::println!("temp: {:#?}", temp); }); - ic_cdk::println!("init result: {:#?}", r); - RUNTIME.with(|runtime| { let mut runtime = runtime.borrow_mut(); *runtime = Some(rt); @@ -106,7 +111,7 @@ pub fn canister_methods(_: TokenStream) -> TokenStream { let post_update_method = quote! { #[ic_cdk_macros::post_upgrade] fn post_upgrade() { - ic_wasi_polyfill::init(&[], &[]); + ic_wasi_polyfill::init(&[], &[#(#env_vars),*]); let mut rt = wasmedge_quickjs::Runtime::new(); @@ -134,8 +139,6 @@ pub fn canister_methods(_: TokenStream) -> TokenStream { // ic_cdk::println!("temp: {:#?}", temp); }); - ic_cdk::println!("post_upgrade result: {:#?}", r); - RUNTIME.with(|runtime| { let mut runtime = runtime.borrow_mut(); *runtime = Some(rt); diff --git a/src/compiler/utils/types.ts b/src/compiler/utils/types.ts index a117c5f1f5..582aa0df23 100644 --- a/src/compiler/utils/types.ts +++ b/src/compiler/utils/types.ts @@ -28,6 +28,7 @@ export type OptLevel = '0' | '1' | '2' | '3' | '4'; export type CompilerInfo = { canister_methods: CanisterMethods; + env_vars: [string, string][]; }; export type CanisterMethods = { diff --git a/src/lib/globals.ts b/src/lib/globals.ts index e1c82c9e59..2d967aec1d 100644 --- a/src/lib/globals.ts +++ b/src/lib/globals.ts @@ -2,6 +2,7 @@ import { ic } from './ic'; import { AzleIc } from './ic/types/azle_ic'; import { Buffer } from 'buffer'; import { replacer } from './stable_structures/stable_json'; +import * as process from 'process'; declare global { var _azleInsideCanister: boolean; @@ -68,3 +69,5 @@ globalThis.crypto = { }; globalThis.Buffer = Buffer; + +globalThis.process = process;