Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cli: refactor and implement subcommands #335

Merged
merged 1 commit into from
Dec 1, 2022
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
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ format:
clang-format -i src/*.{c,h}

test:
./$(BUILD_DIR)/tjs tests/run.js tests/
./$(BUILD_DIR)/tjs run tests/run.js tests/

test-advanced:
cd tests/advanced && npm install
./$(BUILD_DIR)/tjs tests/run.js tests/advanced/
./$(BUILD_DIR)/tjs run tests/run.js tests/advanced/

.PHONY: all build debug install clean distclean format test test-advanced
62 changes: 49 additions & 13 deletions src/js/runMain.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,31 @@ const {
} = tjs[Symbol.for('tjs.internal')];

const exeName = path.basename(tjs.args[0]);
const help = `Usage: ${exeName} [options] [file]
const help = `Usage: ${exeName} [options] [subcommand]

Options:
-v, --version print version
-h, --help list options
-e, --eval EXPR evaluate EXPR
--memory-limit LIMIT set the memory limit
--stack-size STACKSIZE set max stack size`;
-v, --version
Print version information

-h, --help
Print help

--memory-limit LIMIT
Set the memory limit for the JavaScript runtime

--stack-size STACKSIZE
Set the maximum JavaScript stack size

Subcommands:
run
Run a JavaScript program

eval
Evaluate a JavaScript expression`;

const helpEval = `Usage: ${exeName} eval EXPRESSION`;

const helpRun = `Usage: ${exeName} run FILE`;

const options = getopts(tjs.args.slice(1), {
alias: {
Expand Down Expand Up @@ -56,18 +73,37 @@ if (options.help) {
setMaxStackSize(parseNumberOption(stackSize, 'stack-size'));
}

const [ filename ] = options._;
const [ command, ...subargv ] = options._;

if (!command) {
if (isStdinTty()) {
runRepl();
} else {
evalStdin();
}
} else if (command === 'eval') {
const [ expr ] = subargv;

if (!expr) {
console.log(helpEval);
tjs.exit(1);
}

evalScript(expr);
} else if (command === 'run') {
const [ filename ] = subargv;

if (!filename) {
console.log(helpRun);
tjs.exit(1);
}

if (options.eval) {
evalScript(options.eval);
} else if (filename) {
// XXX: This looks weird. This file is being JS_Eval'd when we call `evalFile`,
// which does another JS_Eval, and something get's messed up :-(
globalThis.queueMicrotask(() => evalFile(filename));
} else if (isStdinTty()) {
runRepl();
} else {
evalStdin();
console.log(help);
tjs.exit(1);
}
}

Expand Down
4 changes: 2 additions & 2 deletions tests/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Test {
}

run() {
const args = [ tjs.exepath, this._fileName ];
const args = [ tjs.exepath, 'run', this._fileName ];
this._proc = tjs.spawn(args, { stdout: 'pipe', stderr: 'pipe' });
this._stdout = this._slurpStdio(this._proc.stdout);
this._stderr = this._slurpStdio(this._proc.stderr);
Expand Down Expand Up @@ -93,7 +93,7 @@ function printResult(result) {
}

(async function() {
const dir = await tjs.realpath(tjs.args[2] || import.meta.dirname);
const dir = await tjs.realpath(tjs.args[3] || import.meta.dirname);
const dirIter = await tjs.readdir(dir);
const tests = [];

Expand Down
1 change: 1 addition & 0 deletions tests/test-abort-on-unhandled-rejection.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { path } from '@tjs/std';
(async () => {
const args = [
tjs.exepath,
'run',
path.join(import.meta.dirname, 'helpers', 'unhandled-rejection.js')
];
const proc = tjs.spawn(args, { stdout: 'ignore', stderr: 'pipe' });
Expand Down
1 change: 1 addition & 0 deletions tests/test-eval-as-module.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { path } from '@tjs/std';
(async () => {
const args = [
tjs.exepath,
'run',
path.join(import.meta.dirname, 'helpers', 'log-import-meta.js')
];
const proc = tjs.spawn(args);
Expand Down
2 changes: 1 addition & 1 deletion tests/test-mem-limit.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import assert from './assert.js';
tjs.exepath,
'--memory-limit',
'10485760',
'-e',
'eval',
'new Uint8Array(104857600)'
];
const proc = tjs.spawn(args, { stdout: 'ignore', stderr: 'pipe' });
Expand Down
1 change: 1 addition & 0 deletions tests/test-wasm-wasi.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { path } from '@tjs/std';
(async () => {
const args = [
tjs.exepath,
'run',
path.join(import.meta.dirname, 'wasi', 'launcher.js'),
'test.wasm'
];
Expand Down
4 changes: 2 additions & 2 deletions tests/wasi/launcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { path } from '@tjs/std';


(async () => {
const script = tjs.args[2];
const args = tjs.args.slice(3);
const script = tjs.args[3];
const args = tjs.args.slice(4);
const bytes = await tjs.readFile(path.join(import.meta.dirname,script));
const module = new WebAssembly.Module(bytes);
const wasi = new WebAssembly.WASI({ args });
Expand Down