From d6657f11ddc65cfc49938591b8e08d579a4b0960 Mon Sep 17 00:00:00 2001 From: karthik2804 Date: Tue, 8 Apr 2025 12:58:49 +0200 Subject: [PATCH 1/2] Fix building on windows when using new syntax The new sytax uses cwd() to strip the prefix when building using original source location. We were not running it through maybeWindowsPath which led to file not being found during initialization. This PR fixes that and thus enables building on windows using the new syntax. Signed-off-by: karthik2804 --- src/componentize.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/componentize.js b/src/componentize.js index c3954aed..cb50f527 100644 --- a/src/componentize.js +++ b/src/componentize.js @@ -85,8 +85,8 @@ function isNumeric(n) { } export async function componentize(opts, - _deprecatedWitWorldOrOpts = undefined, - _deprecatedOpts = undefined) { + _deprecatedWitWorldOrOpts = undefined, + _deprecatedOpts = undefined) { let useOriginalSourceFile = true; let jsSource; @@ -246,8 +246,9 @@ export async function componentize(opts, workspacePrefix = sourceDir; sourcePath = sourceName; } - if (workspacePrefix.startsWith(cwd())) { - workspacePrefix = cwd(); + let currentDir = maybeWindowsPath(cwd()); + if (workspacePrefix.startsWith(currentDir)) { + workspacePrefix = currentDir; sourcePath = sourcePath.slice(workspacePrefix.length + 1); } } From c2989decc73d0ec75107dcf12543ed30e4dc8216 Mon Sep 17 00:00:00 2001 From: karthik2804 Date: Tue, 8 Apr 2025 13:42:46 +0200 Subject: [PATCH 2/2] add test case for OriginalSourceFile api Signed-off-by: karthik2804 --- test/api/index.js | 11 +++++++++++ test/test.js | 44 ++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 51 insertions(+), 4 deletions(-) create mode 100644 test/api/index.js diff --git a/test/api/index.js b/test/api/index.js new file mode 100644 index 00000000..d0c6cf87 --- /dev/null +++ b/test/api/index.js @@ -0,0 +1,11 @@ +import { now } from 'wasi:clocks/wall-clock@0.2.3'; +import { getRandomBytes } from 'wasi:random/random@0.2.3'; + +let result; +export const run = { + run() { + result = `NOW: ${now().seconds}, RANDOM: ${getRandomBytes(2n)}`; + } +}; + +export const getResult = () => result; \ No newline at end of file diff --git a/test/test.js b/test/test.js index 928b9fdf..df4e45d8 100644 --- a/test/test.js +++ b/test/test.js @@ -109,9 +109,9 @@ suite('Builtins', () => { reject( new Error( 'test timed out with output:\n' + - stdout + - '\n\nstderr:\n' + - stderr + stdout + + '\n\nstderr:\n' + + stderr ) ); }, 10_000); @@ -254,7 +254,7 @@ suite('Bindings', () => { }); suite('WASI', () => { - test('basic app', async () => { + test('basic app (old API)', async () => { const { component } = await componentize( ` import { now } from 'wasi:clocks/wall-clock@0.2.3'; @@ -301,4 +301,40 @@ suite('WASI', () => { strictEqual(result.slice(0, 10), `NOW: ${String(Date.now()).slice(0, 5)}`); strictEqual(result.split(',').length, 3); }); + + test('basic app (OriginalSourceFile API)', async () => { + const { component } = await componentize( + { + sourcePath: "./test/api/index.js", + witPath: fileURLToPath(new URL('./wit', import.meta.url)), + worldName: 'test1', + enableAot, + debugBuild, + } + ); + + await writeFile( + new URL(`./output/wasi.component.wasm`, import.meta.url), + component + ); + + const { files } = await transpile(component, { tracing: DEBUG_TRACING }); + + await mkdir(new URL(`./output/wasi/interfaces`, import.meta.url), { + recursive: true, + }); + + for (const file of Object.keys(files)) { + await writeFile( + new URL(`./output/wasi/${file}`, import.meta.url), + files[file] + ); + } + + var instance = await import(`./output/wasi/component.js`); + instance.run.run(); + const result = instance.getResult(); + strictEqual(result.slice(0, 10), `NOW: ${String(Date.now()).slice(0, 5)}`); + strictEqual(result.split(',').length, 3); + }); });