From 87fd5fd70ef52f3d3a22639c33e545b76b410388 Mon Sep 17 00:00:00 2001 From: theMackabu Date: Thu, 8 Jan 2026 18:02:41 -0800 Subject: [PATCH] remove promise from shell command execution --- examples/demo/uptime.js | 17 +++++++++++++++++ examples/spec/run.js | 3 +-- meson.build | 2 +- src/ant.c | 9 ++++++--- src/modules/shell.c | 12 ++---------- tests/test_shell.js | 14 +++++++------- 6 files changed, 34 insertions(+), 23 deletions(-) create mode 100755 examples/demo/uptime.js diff --git a/examples/demo/uptime.js b/examples/demo/uptime.js new file mode 100755 index 0000000..04ad100 --- /dev/null +++ b/examples/demo/uptime.js @@ -0,0 +1,17 @@ +#!/usr/bin/env ant + +import { $ } from 'ant:shell'; +import { uptime as osUptime, loadavg } from 'ant:os'; + +const uptime = osUptime(); +const users = $`who | wc -l`.text().trim(); + +const days = Math.floor(uptime / 86400); +const hours = Math.floor((uptime % 86400) / 3600); +const minutes = String(Math.floor((uptime % 3600) / 60)).padStart(2, '0'); + +const load = loadavg() + .map(n => n.toFixed(2)) + .join(' '); + +console.log(`up ${days} days, ${hours}:${minutes}, ${users} user${users == 1 ? '' : 's'}, load averages: ${load}`); diff --git a/examples/spec/run.js b/examples/spec/run.js index 7415760..33c6259 100644 --- a/examples/spec/run.js +++ b/examples/spec/run.js @@ -29,8 +29,7 @@ for (const file of files) { const name = path.basename(file, '.js'); try { - const result = await $`./build/ant ${filePath}`; - const output = result.text(); + const output = $`./build/ant ${filePath}`.text(); console.log(output); const passedMatch = output.match(/Passed:\s*(\d+)/); diff --git a/meson.build b/meson.build index e2ad5a1..fe951aa 100644 --- a/meson.build +++ b/meson.build @@ -96,7 +96,7 @@ endif build_date = run_command('date', '+%Y-%m-%d', check: true).stdout().strip() version_conf = configuration_data() -version_conf.set('ANT_VERSION', '0.3.2.36') +version_conf.set('ANT_VERSION', '0.3.2.37') version_conf.set('ANT_GIT_HASH', git_hash) version_conf.set('ANT_BUILD_DATE', build_date) diff --git a/src/ant.c b/src/ant.c index d196f35..ef8e2e2 100644 --- a/src/ant.c +++ b/src/ant.c @@ -8155,8 +8155,9 @@ static jsval_t js_call_dot(struct js *js) { js->consumed = 1; next(js); js->consumed = 1; - jsval_t result = js_tagged_template(js, tag_func); - return result; + res = js_tagged_template(js, tag_func); + if (is_err(res)) return res; + goto js_call_dot_loop; } if ((js->flags & F_STRICT) && is_eval_or_arguments(js, coderefoff(res), codereflen(res))) { uint8_t la = lookahead(js); @@ -8190,12 +8191,14 @@ static jsval_t js_call_dot(struct js *js) { if (is_err(res)) return res; } } + js_call_dot_loop: while (next(js) == TOK_LPAREN || next(js) == TOK_DOT || next(js) == TOK_OPTIONAL_CHAIN || next(js) == TOK_LBRACKET || next(js) == TOK_TEMPLATE) { if (js->tok == TOK_TEMPLATE) { if (vtype(res) == T_PROP) res = resolveprop(js, res); if (is_err(res)) return res; js->consumed = 1; - return js_tagged_template(js, res); + res = js_tagged_template(js, res); + if (is_err(res)) return res; } else if (js->tok == TOK_DOT || js->tok == TOK_OPTIONAL_CHAIN) { uint8_t op = js->tok; js->consumed = 1; diff --git a/src/modules/shell.c b/src/modules/shell.c index 648d4c7..d24a0a0 100644 --- a/src/modules/shell.c +++ b/src/modules/shell.c @@ -129,12 +129,7 @@ static jsval_t builtin_shell_dollar(struct js *js, jsval_t *args, int nargs) { size_t cmd_len; char *cmd = js_getstr(js, args[0], &cmd_len); if (!cmd) return js_mkerr(js, "Failed to get command string"); - - jsval_t result = shell_exec(js, cmd, cmd_len); - jsval_t promise = js_mkpromise(js); - js_resolve_promise(js, promise, result); - - return promise; + return shell_exec(js, cmd, cmd_len); } return js_mkerr(js, "$() requires a template string"); @@ -208,10 +203,7 @@ static jsval_t builtin_shell_dollar(struct js *js, jsval_t *args, int nargs) { jsval_t result = shell_exec(js, command, cmd_pos); free(command); - jsval_t promise = js_mkpromise(js); - js_resolve_promise(js, promise, result); - - return promise; + return result; } jsval_t shell_library(struct js *js) { diff --git a/tests/test_shell.js b/tests/test_shell.js index 574c478..d089fa6 100644 --- a/tests/test_shell.js +++ b/tests/test_shell.js @@ -3,7 +3,7 @@ import { $ } from 'ant:shell'; console.log('Testing $ shell command execution...'); console.log('\n=== Test 1: Simple echo ==='); -const result1 = await $`echo "Hello, world!"`; +const result1 = $`echo "Hello, world!"`; const text1 = result1.text(); console.log('Output:', text1); if (text1 !== 'Hello, world!') { @@ -14,7 +14,7 @@ if (result1.exitCode !== 0) { } console.log('\n=== Test 2: Multi-line output ==='); -const result2 = await $`echo "line1\nline2\nline3"`; +const result2 = $`echo "line1\nline2\nline3"`; const lines2 = result2.lines(); console.log('Line count:', lines2.length); console.log('Line 0:', lines2[0]); @@ -35,7 +35,7 @@ if (lines2[2] !== 'line3') { } console.log('\n=== Test 3: ls command ==='); -const result3 = await $`ls tests/test_shell.js`; +const result3 = $`ls tests/test_shell.js`; const text3 = result3.text(); console.log('Output:', text3); if (!text3.includes('test_shell.js')) { @@ -43,7 +43,7 @@ if (!text3.includes('test_shell.js')) { } console.log('\n=== Test 4: pwd command ==='); -const result4 = await $`pwd`; +const result4 = $`pwd`; const text4 = result4.text(); console.log('Current directory:', text4); if (text4.length === 0) { @@ -51,7 +51,7 @@ if (text4.length === 0) { } console.log('\n=== Test 5: .text() method ==='); -const result5 = await $`echo "test text method"`; +const result5 = $`echo "test text method"`; const text5 = result5.text(); console.log('Text:', text5); if (text5 !== 'test text method') { @@ -59,7 +59,7 @@ if (text5 !== 'test text method') { } console.log('\n=== Test 6: Listing files with .lines() ==='); -const result6 = await $`ls tests/*.cjs | head -5`; +const result6 = $`ls tests/*.cjs | head -5`; const lines6 = result6.lines(); console.log('Found', lines6.length, 'files'); for (let i = 0; i < lines6.length && i < 3; i++) { @@ -67,7 +67,7 @@ for (let i = 0; i < lines6.length && i < 3; i++) { } console.log('\n=== Test 7: date command ==='); -const result7 = await $`date +%Y`; +const result7 = $`date +%Y`; const year = result7.text(); console.log('Current year:', year); if (year.length !== 4) { -- 2.51.2