Skip to content

Commit

Permalink
Support separate WASM/JS files and remove use of callMain (#44)
Browse files Browse the repository at this point in the history
* Support separate WASM/JS files and remove use of callMain

* Don't use curly braces in copy commands on github runners
  • Loading branch information
ianthomas23 authored Aug 16, 2024
1 parent e4ae95a commit 41cc8b0
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 35 deletions.
2 changes: 1 addition & 1 deletion demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"private": true,
"scripts": {
"build": "npm run build:wasm && rspack build",
"build:wasm": "cp node_modules/@jupyterlite/cockle/src/wasm/*.js assets/",
"build:wasm": "cp node_modules/@jupyterlite/cockle/src/wasm/*.js assets/ && cp node_modules/@jupyterlite/cockle/src/wasm/*.wasm assets/",
"serve": "rspack serve"
},
"devDependencies": {
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
"url": "https://github.com/jupyterlite/cockle/issues"
},
"files": [
"lib/**/*.{d.ts,eot,gif,html,jpg,js,js.map,json,png,svg,woff2,ttf}",
"lib/**/*.{d.ts,eot,gif,html,jpg,js,js.map,json,png,svg,wasm,woff2,ttf}",
"src/**/*.ts"
],
"main": "lib/index.js",
"types": "lib/index.d.ts",
"scripts": {
"fetch:wasm:create-env": "micromamba create -p $(pwd)/cockle_wasm_env -y cockle_fs grep coreutils --platform=emscripten-wasm32 -c https://repo.mamba.pm/emscripten-forge -c https://repo.mamba.pm/conda-forge",
"fetch:wasm:copy": "mkdir -p src/wasm && cp -r $(pwd)/cockle_wasm_env/bin/*.js src/wasm",
"fetch:wasm:copy": "mkdir -p src/wasm && cp $(pwd)/cockle_wasm_env/bin/*.js src/wasm/ && cp $(pwd)/cockle_wasm_env/bin/*.wasm src/wasm/",
"fetch:wasm": "npm run fetch:wasm:create-env && npm run fetch:wasm:copy",
"build": "tsc",
"eslint": "npm run eslint:check -- --fix",
Expand Down Expand Up @@ -53,6 +53,7 @@
"dist",
"coverage",
"**/*.d.ts",
"**/*.wasm",
"**/package-lock.json"
],
"eslintConfig": {
Expand Down
43 changes: 22 additions & 21 deletions src/commands/wasm_command_runner.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ICommandRunner } from './command_runner';
import { Context } from '../context';
import { RunCommandError } from '../error_exit_code';
import { ExitCode } from '../exit_code';
import { WasmLoader } from '../wasm_loader';

export abstract class WasmCommandRunner implements ICommandRunner {
Expand Down Expand Up @@ -36,11 +36,18 @@ export abstract class WasmCommandRunner implements ICommandRunner {
];
}

let exitCode: number | undefined;

const wasm = await wasmModule({
thisProgram: cmdName,
noInitialRun: true,
arguments: args,
print: (text: string) => stdout.write(`${text}\n`),
printErr: (text: string) => stderr.write(`${text}\n`),
quit: (moduleExitCode: number, toThrow: any) => {
if (exitCode === undefined) {
exitCode = moduleExitCode;
}
},
preRun: (module: any) => {
if (Object.prototype.hasOwnProperty.call(module, 'FS')) {
// Use PROXYFS so that command sees the shared FS.
Expand All @@ -66,30 +73,24 @@ export abstract class WasmCommandRunner implements ICommandRunner {
}
}
});
const loaded = Date.now();

if (!Object.prototype.hasOwnProperty.call(wasm, 'callMain')) {
throw new RunCommandError(
cmdName,
"WASM module does not export 'callMain' so it cannot be called"
);
}

const exitCode = wasm.callMain(args);

if (Object.prototype.hasOwnProperty.call(wasm, 'FS')) {
const FS = wasm.FS;
FS.close(FS.streams[1]);
FS.close(FS.streams[2]);
}
if (exitCode === undefined) {
exitCode = ExitCode.CANNOT_RUN_COMMAND;
} else {
if (Object.prototype.hasOwnProperty.call(wasm, 'FS')) {
const FS = wasm.FS;
FS.close(FS.streams[1]);
FS.close(FS.streams[2]);
}

if (Object.prototype.hasOwnProperty.call(wasm, 'getEnvStrings')) {
// Copy environment variables back from command.
context.environment.copyFromCommand(wasm.getEnvStrings());
if (Object.prototype.hasOwnProperty.call(wasm, 'getEnvStrings')) {
// Copy environment variables back from command.
context.environment.copyFromCommand(wasm.getEnvStrings());
}
}

const end = Date.now();
console.log(`${cmdName} load time ${loaded - start} ms, run time ${end - loaded} ms`);
console.log(`${cmdName} load and run time ${end - start} ms`);
return exitCode;
}
}
6 changes: 0 additions & 6 deletions src/error_exit_code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,3 @@ export class ImproperUseError extends ErrorExitCode {
super(ExitCode.IMPROPER_USE, message);
}
}

export class RunCommandError extends ErrorExitCode {
constructor(commandName: string, message: string) {
super(ExitCode.CANNOT_RUN_COMMAND, `'${commandName}': ${message}`);
}
}
1 change: 0 additions & 1 deletion src/shell_impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ export class ShellImpl implements IShell {
} else if (code === 27) {
// Escape following by 1+ more characters
const remainder = char.slice(1);
console.log('Escape code', char);
if (
remainder === '[A' || // Up arrow
remainder === '[1A' ||
Expand Down
2 changes: 1 addition & 1 deletion test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"private": true,
"scripts": {
"build": "npm run build:wasm && rspack build",
"build:wasm": "cp node_modules/@jupyterlite/cockle/src/wasm/*.js assets",
"build:wasm": "cp node_modules/@jupyterlite/cockle/src/wasm/*.js assets/ && cp node_modules/@jupyterlite/cockle/src/wasm/*.wasm assets/",
"serve": "rspack serve",
"test": "playwright test",
"test:ui": "playwright test --ui",
Expand Down
3 changes: 1 addition & 2 deletions test/playwright.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ import { defineConfig, devices } from '@playwright/test';

export default defineConfig({
testDir: './tests',
fullyParallel: true,
forbidOnly: !!process.env.CI,
retries: process.env.CI ? 2 : 0,
workers: process.env.CI ? 1 : undefined,
workers: 1,
reporter: [['html', { open: 'never' }]],
use: {
baseURL: 'http://localhost:8000',
Expand Down
2 changes: 1 addition & 1 deletion test/rspack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module.exports = {
]
},
resolve: {
extensions: ['.tsx', '.ts', '.js']
extensions: ['.tsx', '.ts', '.js', '.wasm']
},
output: {
filename: 'bundle.js',
Expand Down
1 change: 1 addition & 0 deletions test/serve/shell_setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ async function _shell_setup_common(options: IOptions, level: number): Promise<IS
const shell = new Shell({
color: options.color ?? false,
outputCallback: output.callback,
wasmBaseUrl: 'http://localhost:8000/',
initialDirectories,
initialFiles
});
Expand Down

0 comments on commit 41cc8b0

Please sign in to comment.