Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/componentize.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
}
Expand Down
11 changes: 11 additions & 0 deletions test/api/index.js
Original file line number Diff line number Diff line change
@@ -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;
44 changes: 40 additions & 4 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -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);
});
});