Skip to content

Commit

Permalink
Add lua WASM command (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
ianthomas23 authored Sep 11, 2024
1 parent c7c1ccd commit 32c5e03
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 3 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ In-browser bash-like shell implemented in a combination of TypeScript and WebAss

Used in the [JupyterLite terminal extension](https://github.com/jupyterlite/terminal).

This is an early-stage work in progress and should be considered experimental code. Anything and
everything could change at any time.

The commands used here are either built-in commands implemented in TypeScript, or WebAssembly
commands compiled into .js and .wasm files. The latter are built by
[Emscripten-forge](https://emscripten-forge.org/) and are added to the `cockle` NPM package using
Expand Down
11 changes: 10 additions & 1 deletion demo/serve/demo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,16 @@ export class Demo {
'file.txt': 'This is the contents of the file',
'other.txt': 'Some other file\nSecond line',
'months.txt':
'January\nFebruary\nMarch\nApril\nMay\nJune\nJuly\nAugust\nSeptember\nOctober\nNovember\nDecember\n'
'January\nFebruary\nMarch\nApril\nMay\nJune\nJuly\nAugust\nSeptember\nOctober\nNovember\nDecember\n',
'factorial.lua':
'function factorial(n)\n' +
' if n == 0 then\n' +
' return 1\n' +
' else\n' +
' return n * factorial(n-1)\n' +
' end\n' +
'end\n' +
'print(factorial(tonumber(arg[1])))\n'
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"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:create-env": "micromamba create -p $(pwd)/cockle_wasm_env -y cockle_fs grep coreutils lua --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 $(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 && cp src/wasm/*.wasm lib/wasm/",
Expand Down
4 changes: 3 additions & 1 deletion src/command_registry.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ICommandRunner } from './commands/command_runner';
import { CoreutilsCommandRunner } from './commands/coreutils_command_runner';
import { GrepCommandRunner } from './commands/grep_command_runner';
import { LuaCommandRunner } from './commands/lua_command_runner';
import * as AllBuiltinCommands from './builtin';
import { WasmLoader } from './wasm_loader';

Expand All @@ -10,7 +11,8 @@ export class CommandRegistry {

this._commandRunners = [
new CoreutilsCommandRunner(wasmLoader),
new GrepCommandRunner(wasmLoader)
new GrepCommandRunner(wasmLoader),
new LuaCommandRunner(wasmLoader)
];

// Command name -> runner mapping
Expand Down
16 changes: 16 additions & 0 deletions src/commands/lua_command_runner.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { WasmCommandRunner } from './wasm_command_runner';
import { WasmLoader } from '../wasm_loader';

export class LuaCommandRunner extends WasmCommandRunner {
constructor(wasmLoader: WasmLoader) {
super(wasmLoader);
}

moduleName(): string {
return 'lua';
}

names(): string[] {
return ['lua'];
}
}
33 changes: 33 additions & 0 deletions test/tests/command/lua.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { expect } from '@playwright/test';
import { shellLineSimple, shellLineSimpleN, test } from '../utils';

test.describe('lua command', () => {
test('should write to stdout', async ({ page }) => {
const output = await shellLineSimple(page, 'lua -v');
expect(output).toMatch('Lua 5.4.6 Copyright (C) 1994-2023 Lua.org, PUC-Rio');
});

test('should run lua script', async ({ page }) => {
const options = {
initialFiles: {
'factorial.lua':
'function factorial(n)\n' +
' if n == 0 then\n' +
' return 1\n' +
' else\n' +
' return n * factorial(n-1)\n' +
' end\n' +
'end\n' +
'print(factorial(tonumber(arg[1])))\n'
}
};
const output = await shellLineSimpleN(
page,
['lua factorial.lua 1', 'lua factorial.lua 3', 'lua factorial.lua 5'],
options
);
expect(output[0]).toMatch(/^lua factorial.lua 1\r\n1\r\n/);
expect(output[1]).toMatch(/^lua factorial.lua 3\r\n6\r\n/);
expect(output[2]).toMatch(/^lua factorial.lua 5\r\n120\r\n/);
});
});

0 comments on commit 32c5e03

Please sign in to comment.