diff --git a/packages/near-sdk-js/builder/builder.c b/packages/near-sdk-js/builder/builder.c index 55049e28..cc4162db 100644 --- a/packages/near-sdk-js/builder/builder.c +++ b/packages/near-sdk-js/builder/builder.c @@ -10,7 +10,6 @@ static JSContext *JS_NewCustomContext(JSRuntime *rt) return NULL; JS_AddIntrinsicBaseObjects(ctx); JS_AddIntrinsicDate(ctx); - JS_AddIntrinsicEval(ctx); JS_AddIntrinsicStringNormalize(ctx); JS_AddIntrinsicRegExp(ctx); JS_AddIntrinsicJSON(ctx); @@ -22,8 +21,15 @@ static JSContext *JS_NewCustomContext(JSRuntime *rt) return ctx; } +extern void _initialize(void); + #define DEFINE_NEAR_METHOD(name) \ void name () __attribute__((export_name(#name))) {\ + static volatile int initialized = 0;\ + if (initialized == 0) {\ + _initialize();\ + initialized = 1;\ + }\ JSRuntime *rt;\ JSContext *ctx;\ JSValue mod_obj, fun_obj, result, error, error_message, error_stack;\ @@ -1186,6 +1192,4 @@ static void js_add_near_host_functions(JSContext* ctx) { JSValue JS_Call(JSContext *ctx, JSValueConst func_obj, JSValueConst this_obj, int argc, JSValueConst *argv); -void _start() {} - #include "methods.h" diff --git a/packages/near-sdk-js/lib/cli/cli.js b/packages/near-sdk-js/lib/cli/cli.js index 0c6caea1..4d452be8 100755 --- a/packages/near-sdk-js/lib/cli/cli.js +++ b/packages/near-sdk-js/lib/cli/cli.js @@ -145,8 +145,6 @@ export async function transpileJsAndBuildWasmCom(target, { verbose = false }) { await createMethodsHeaderFile(getRollupTarget(target), verbose); signale.await(`Creating ${getContractTarget(target)} contract...`); await createWasmContract(getQjscTarget(target), getContractTarget(target), verbose); - signale.await("Executing wasi-stub..."); - await wasiStubContract(getContractTarget(target), verbose); signale.success(`Generated ${getContractTarget(target)} contract successfully!`); } export async function buildCom(source, target, packageJson, tsConfig, { verbose = false, generateABI = false }) { @@ -215,7 +213,7 @@ async function createMethodsHeaderFile(rollupTarget, verbose = false) { } async function createWasmContract(qjscTarget, contractTarget, verbose = false) { const WASI_SDK_PATH = `${NEAR_SDK_JS}/lib/cli/deps/wasi-sdk`; - const CC = `${WASI_SDK_PATH}/bin/clang --sysroot=${WASI_SDK_PATH}/share/wasi-sysroot`; + const CC = `${WASI_SDK_PATH}/bin/clang`; let DEFS = `-D_GNU_SOURCE '-DCONFIG_VERSION="2021-03-27"' -DCONFIG_BIGNUM`; if (process.env.NEAR_NIGHTLY) { DEFS = DEFS + " -DNIGHTLY"; @@ -228,9 +226,5 @@ async function createWasmContract(qjscTarget, contractTarget, verbose = false) { // copying builder.c file to the build folder fs.cpSync(ORIGINAL_BUILDER_PATH, NEW_BUILDER_PATH); fs.renameSync(qjscTarget, "build/code.h"); - await executeCommand(`${CC} --target=wasm32-wasi -nostartfiles -Oz -flto ${DEFS} ${INCLUDES} ${SOURCES} ${LIBS} -Wl,--no-entry -Wl,--allow-undefined -Wl,-z,stack-size=${256 * 1024} -Wl,--lto-O3 -o ${contractTarget}`, verbose); -} -async function wasiStubContract(contractTarget, verbose = false) { - const WASI_STUB = `${NEAR_SDK_JS}/lib/cli/deps/binaryen/wasi-stub/run.sh`; - await executeCommand(`${WASI_STUB} ${contractTarget}`, verbose); + await executeCommand(`${CC} -mcpu=mvp -Oz -fno-strict-aliasing ${DEFS} ${INCLUDES} ${SOURCES} ${LIBS} -mexec-model=reactor -Wl,--allow-undefined -Wl,--stack-first,-z,stack-size=${256 * 1024 * 2} -Wl,--compress-relocations,--strip-debug -o ${contractTarget}`, verbose); } diff --git a/packages/near-sdk-js/lib/cli/post-install.js b/packages/near-sdk-js/lib/cli/post-install.js index 82352d50..9bac0e42 100644 --- a/packages/near-sdk-js/lib/cli/post-install.js +++ b/packages/near-sdk-js/lib/cli/post-install.js @@ -23,22 +23,6 @@ if (!SUPPORTED_ARCH.includes(ARCH)) { console.error(`Architecture ${ARCH} is not supported at the moment`); process.exit(1); } -signale.await("Installing wasi-stub..."); -const BINARYEN_VERSION = `0.1.15`; -const BINARYEN_VERSION_TAG = `v${BINARYEN_VERSION}`; -const BINARYEN_SYSTEM_NAME = PLATFORM === "linux" - ? "Linux" - : PLATFORM === "darwin" - ? "macOS" - : PLATFORM === "win32" - ? "windows" - : "other"; -const BINARYEN_ARCH_NAME = ARCH === "x64" ? "X64" : ARCH === "arm64" ? "arm64" : "other"; -const BINARYEN_TAR_NAME = `binaryen-${BINARYEN_SYSTEM_NAME}-${BINARYEN_ARCH_NAME}.tar.gz`; -await download(`https://github.com/near/binaryen/releases/download/${BINARYEN_VERSION_TAG}/${BINARYEN_TAR_NAME}`); -fs.mkdirSync("binaryen"); -await executeCommand(`tar xvf ${BINARYEN_TAR_NAME} --directory binaryen`); -fs.rmSync(BINARYEN_TAR_NAME); signale.await("Installing QuickJS..."); const QUICK_JS_VERSION = `0.1.3`; const QUICK_JS_VERSION_TAG = `v${QUICK_JS_VERSION}`; diff --git a/packages/near-sdk-js/src/cli/cli.ts b/packages/near-sdk-js/src/cli/cli.ts index 52d4db0c..ed16f5d0 100644 --- a/packages/near-sdk-js/src/cli/cli.ts +++ b/packages/near-sdk-js/src/cli/cli.ts @@ -239,9 +239,6 @@ export async function transpileJsAndBuildWasmCom( verbose ); - signale.await("Executing wasi-stub..."); - await wasiStubContract(getContractTarget(target), verbose); - signale.success( `Generated ${getContractTarget(target)} contract successfully!` ); @@ -363,7 +360,7 @@ async function createWasmContract( ) { const WASI_SDK_PATH = `${NEAR_SDK_JS}/lib/cli/deps/wasi-sdk`; - const CC = `${WASI_SDK_PATH}/bin/clang --sysroot=${WASI_SDK_PATH}/share/wasi-sysroot`; + const CC = `${WASI_SDK_PATH}/bin/clang`; let DEFS = `-D_GNU_SOURCE '-DCONFIG_VERSION="2021-03-27"' -DCONFIG_BIGNUM`; if (process.env.NEAR_NIGHTLY) { @@ -382,14 +379,9 @@ async function createWasmContract( fs.renameSync(qjscTarget, "build/code.h"); await executeCommand( - `${CC} --target=wasm32-wasi -nostartfiles -Oz -flto ${DEFS} ${INCLUDES} ${SOURCES} ${LIBS} -Wl,--no-entry -Wl,--allow-undefined -Wl,-z,stack-size=${ - 256 * 1024 - } -Wl,--lto-O3 -o ${contractTarget}`, + `${CC} -mcpu=mvp -Oz -fno-strict-aliasing ${DEFS} ${INCLUDES} ${SOURCES} ${LIBS} -mexec-model=reactor -Wl,--allow-undefined -Wl,--stack-first,-z,stack-size=${ + 256 * 1024 * 2 + } -Wl,--compress-relocations,--strip-debug -o ${contractTarget}`, verbose ); } - -async function wasiStubContract(contractTarget: string, verbose = false) { - const WASI_STUB = `${NEAR_SDK_JS}/lib/cli/deps/binaryen/wasi-stub/run.sh`; - await executeCommand(`${WASI_STUB} ${contractTarget}`, verbose); -} diff --git a/packages/near-sdk-js/src/cli/post-install.ts b/packages/near-sdk-js/src/cli/post-install.ts index 4ec67962..2c47841b 100644 --- a/packages/near-sdk-js/src/cli/post-install.ts +++ b/packages/near-sdk-js/src/cli/post-install.ts @@ -31,34 +31,6 @@ if (!SUPPORTED_ARCH.includes(ARCH)) { process.exit(1); } -signale.await("Installing wasi-stub..."); - -const BINARYEN_VERSION = `0.1.15`; -const BINARYEN_VERSION_TAG = `v${BINARYEN_VERSION}`; - -const BINARYEN_SYSTEM_NAME = - PLATFORM === "linux" - ? "Linux" - : PLATFORM === "darwin" - ? "macOS" - : PLATFORM === "win32" - ? "windows" - : "other"; - -const BINARYEN_ARCH_NAME = - ARCH === "x64" ? "X64" : ARCH === "arm64" ? "arm64" : "other"; - -const BINARYEN_TAR_NAME = `binaryen-${BINARYEN_SYSTEM_NAME}-${BINARYEN_ARCH_NAME}.tar.gz`; - -await download( - `https://github.com/near/binaryen/releases/download/${BINARYEN_VERSION_TAG}/${BINARYEN_TAR_NAME}` -); - -fs.mkdirSync("binaryen"); - -await executeCommand(`tar xvf ${BINARYEN_TAR_NAME} --directory binaryen`); -fs.rmSync(BINARYEN_TAR_NAME); - signale.await("Installing QuickJS..."); const QUICK_JS_VERSION = `0.1.3`;