From e01ca1c8309534d6b2f1e3fb89a5104f2bcc2814 Mon Sep 17 00:00:00 2001 From: Victor Adossi Date: Thu, 20 Mar 2025 20:40:22 +0900 Subject: [PATCH 1/2] feat: add min stack size setting for AOT Signed-off-by: Victor Adossi --- src/cmd/componentize.js | 32 ++++++++++++++++++++++++-------- src/jco.js | 5 ++++- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/src/cmd/componentize.js b/src/cmd/componentize.js index 3890a09bc..d149fa2ab 100644 --- a/src/cmd/componentize.js +++ b/src/cmd/componentize.js @@ -1,15 +1,20 @@ -import { readFile, writeFile } from 'node:fs/promises'; -import { resolve, basename } from 'node:path'; -import c from 'chalk-template'; +import { freemem } from "node:os"; +import { readFile, writeFile } from "node:fs/promises"; +import { resolve, basename } from "node:path"; -export async function componentize (jsSource, opts) { - const { componentize: componentizeFn } = await eval('import("@bytecodealliance/componentize-js")'); - if (opts.disable?.includes('all')) { - opts.disable = ['stdio', 'random', 'clocks', 'http']; +import c from "chalk-template"; + +export async function componentize(jsSource, opts) { + const { componentize: componentizeFn } = await eval( + 'import("@bytecodealliance/componentize-js")', + ); + if (opts.disable?.includes("all")) { + opts.disable = ["stdio", "random", "clocks", "http"]; } - const source = await readFile(jsSource, 'utf8'); + const source = await readFile(jsSource, "utf8"); const { component } = await componentizeFn(source, { enableAot: opts.aot, + aotMinStackSizeBytes: opts.aotMinStackSizeBytes ?? defaultMinStackSize(), wevalBin: opts.wevalBin, sourceName: basename(jsSource), witPath: resolve(opts.wit), @@ -21,3 +26,14 @@ export async function componentize (jsSource, opts) { await writeFile(opts.out, component); console.log(c`{green OK} Successfully written {bold ${opts.out}}.`); } + +/** + * Calculate the min stack size depending on free memory + * + * @param {number} freeMemoryBytes - Amount of free memory in the system, in bytes (if not provided, os.freemem() is used) + * @returns {number} The minimum stack size that should be used as a default. + */ +function defaultMinStackSize(freeMemoryBytes) { + freeMemoryBytes = freeMemoryBytes ?? freemem(); + return Math.max(8 * 1024 * 1024, Math.floor(freeMemoryBytes * 0.1)); +} diff --git a/src/jco.js b/src/jco.js index d9c7ab41c..3bf212a06 100755 --- a/src/jco.js +++ b/src/jco.js @@ -1,11 +1,13 @@ #!/usr/bin/env node + +import c from 'chalk-template'; import { program, Option } from 'commander'; + import { opt } from './cmd/opt.js'; import { transpile, types, guestTypes } from './cmd/transpile.js'; import { run as runCmd, serve as serveCmd } from './cmd/run.js'; import { parse, print, componentNew, componentEmbed, metadataAdd, metadataShow, componentWit } from './cmd/wasm-tools.js'; import { componentize } from './cmd/componentize.js'; -import c from 'chalk-template'; program .name('jco') @@ -34,6 +36,7 @@ program.command('componentize') .requiredOption('-w, --wit ', 'WIT path to build with') .option('-n, --world-name ', 'WIT world to build') .option('--aot', 'Enable Weval AOT compilation of JS') + .option('--aot-min-stack-size-bytes ', 'Set the min stack size to be used during AOT') .option('--weval-bin ', 'Specify a custom weval binary to use') .addOption(new Option('-d, --disable ', 'disable WASI features').choices(['clocks', 'http', 'random', 'stdio', 'all'])) // .addOption(new Option('-e, --enable ', 'enable WASI features').choices(['http'])) From fd3bb29eb16fe672f7666552f60ebfa63afc1aa2 Mon Sep 17 00:00:00 2001 From: Victor Adossi Date: Fri, 21 Mar 2025 16:12:13 +0900 Subject: [PATCH 2/2] refactor: remove manual setting of min stack size This commit removes the manual setting of min stack size, since it's been upstreamed into componentize-js Signed-off-by: Victor Adossi --- src/cmd/componentize.js | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) diff --git a/src/cmd/componentize.js b/src/cmd/componentize.js index d149fa2ab..3c1fbf91f 100644 --- a/src/cmd/componentize.js +++ b/src/cmd/componentize.js @@ -1,4 +1,3 @@ -import { freemem } from "node:os"; import { readFile, writeFile } from "node:fs/promises"; import { resolve, basename } from "node:path"; @@ -14,7 +13,7 @@ export async function componentize(jsSource, opts) { const source = await readFile(jsSource, "utf8"); const { component } = await componentizeFn(source, { enableAot: opts.aot, - aotMinStackSizeBytes: opts.aotMinStackSizeBytes ?? defaultMinStackSize(), + aotMinStackSizeBytes: opts.aotMinStackSizeBytes, wevalBin: opts.wevalBin, sourceName: basename(jsSource), witPath: resolve(opts.wit), @@ -26,14 +25,3 @@ export async function componentize(jsSource, opts) { await writeFile(opts.out, component); console.log(c`{green OK} Successfully written {bold ${opts.out}}.`); } - -/** - * Calculate the min stack size depending on free memory - * - * @param {number} freeMemoryBytes - Amount of free memory in the system, in bytes (if not provided, os.freemem() is used) - * @returns {number} The minimum stack size that should be used as a default. - */ -function defaultMinStackSize(freeMemoryBytes) { - freeMemoryBytes = freeMemoryBytes ?? freemem(); - return Math.max(8 * 1024 * 1024, Math.floor(freeMemoryBytes * 0.1)); -}