这是indexloc提供的服务,不要输入任何密码
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
4 changes: 2 additions & 2 deletions examples/process.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ logStatus(status);

proc = tjs.spawn('cat');
console.log(`proc PID: ${proc.pid}`);
proc.kill(tjs.SIGTERM);
proc.kill('SIGTERM');
status = await proc.wait();
logStatus(status);
status = await proc.wait();
Expand All @@ -47,6 +47,6 @@ proc.stdin.write(input);
data = new Uint8Array(input.length);
await proc.stdout.read(data);
console.log(decoder.decode(data));
proc.kill(tjs.SIGTERM);
proc.kill('SIGTERM');
status = await proc.wait();
console.log(status);
5,792 changes: 2,936 additions & 2,856 deletions src/bundles/c/core/core.c

Large diffs are not rendered by default.

1,562 changes: 782 additions & 780 deletions src/bundles/c/core/run-main.c

Large diffs are not rendered by default.

17 changes: 16 additions & 1 deletion src/eval.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#include "private.h"


int tjs__eval_bytecode(JSContext *ctx, const uint8_t *buf, size_t buf_len) {
int tjs__eval_bytecode(JSContext *ctx, const uint8_t *buf, size_t buf_len, bool check_promise) {
JSValue obj = JS_ReadObject(ctx, buf, buf_len, JS_READ_OBJ_BYTECODE);

if (JS_IsException(obj))
Expand All @@ -44,6 +44,21 @@ int tjs__eval_bytecode(JSContext *ctx, const uint8_t *buf, size_t buf_len) {
if (JS_IsException(val))
goto error;

if (check_promise) {
JSPromiseStateEnum promise_state = JS_PromiseState(ctx, val);
if (promise_state != -1) {
// It's a promise!
if (promise_state == JS_PROMISE_REJECTED) {
JSValue res = JS_PromiseResult(ctx, val);
tjs_dump_error1(ctx, res);
JS_FreeValue(ctx, res);
JS_FreeValue(ctx, val);

return -1;
}
}
}

JS_FreeValue(ctx, val);

return 0;
Expand Down
17 changes: 9 additions & 8 deletions src/js/core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import pathModule from './path.js';
import { addSignalListener, removeSignalListener } from './signal.js';
import { connect, listen } from './sockets.js';
import { createStdin, createStdout, createStderr } from './stdio.js';
import system from './system.js';


// The "tjs" global.
Expand All @@ -19,12 +20,10 @@ const tjs = Object.create(null);
// Export these properties directly from the core.
const exports = [
'Error',
'availableParallelism',
'chdir',
'chmod',
'chown',
'copyFile',
'cpuInfo',
'createConsole',
'cwd',
'exePath',
Expand All @@ -36,12 +35,9 @@ const exports = [
'inspect',
'kill',
'lchown',
'loadavg',
'lstat',
'makeTempDir',
'networkInterfaces',
'pid',
'platform',
'ppid',
'readDir',
'readFile',
Expand All @@ -50,9 +46,6 @@ const exports = [
'spawn',
'stat',
'tmpDir',
'uname',
'uptime',
'userInfo',
'version',
'watch'
];
Expand Down Expand Up @@ -183,6 +176,14 @@ Object.defineProperty(tjs, 'stderr', {
value: createStderr()
});

// System.
Object.defineProperty(tjs, 'system', {
enumerable: true,
configurable: false,
writable: false,
value: system
});

// Internal stuff needed by the runtime.
globalThis[Symbol.for('tjs.internal.modules.path')] = pathModule;

Expand Down
66 changes: 66 additions & 0 deletions src/js/core/system.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
const core = globalThis[Symbol.for('tjs.internal.core')];
const uname = core.uname();

const system = Object.create(null);

Object.defineProperty(system, 'arch', {
enumerable: true,
configurable: false,
writable: false,
value: uname.machine
});

Object.defineProperty(system, 'availableParallelism', {
enumerable: true,
configurable: false,
get: core.availableParallelism
});


Object.defineProperty(system, 'cpus', {
enumerable: true,
configurable: false,
get: core.cpuInfo
});

Object.defineProperty(system, 'loadAvg', {
enumerable: true,
configurable: false,
get: core.loadavg
});

Object.defineProperty(system, 'networkInterfaces', {
enumerable: true,
configurable: false,
get: core.networkInterfaces
});


Object.defineProperty(system, 'osRelease', {
enumerable: true,
configurable: false,
writable: false,
value: uname.release
});


Object.defineProperty(system, 'platform', {
enumerable: true,
configurable: false,
writable: false,
value: core.platform
});

Object.defineProperty(system, 'uptime', {
enumerable: true,
configurable: false,
get: core.uptime
});

Object.defineProperty(system, 'userInfo', {
enumerable: true,
configurable: false,
get: () => core.userInfo
});

export default system;
2 changes: 1 addition & 1 deletion src/js/run-main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ if (options.help) {

let newFileName = outfile ?? `${infilePath.name}`;

if (tjs.platform === 'windows' && !newFileName.endsWith('.exe')) {
if (tjs.system.platform === 'windows' && !newFileName.endsWith('.exe')) {
newFileName += '.exe';
}

Expand Down
4 changes: 2 additions & 2 deletions src/js/run-main/run-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class Test {
this._stdout = this._slurpStdio(this._proc.stdout);
this._stderr = this._slurpStdio(this._proc.stderr);
this._timer = setTimeout(() => {
this._proc.kill(tjs.SIGKILL);
this._proc.kill('SIGKILL');
this._timeout = true;
}, TIMEOUT);
this._proc_exit = this._proc.wait();
Expand Down Expand Up @@ -118,7 +118,7 @@ export async function runTests(d) {
}

let failed = 0;
const testConcurrency = tjs.env.TJS_TEST_CONCURRENCY ?? tjs.availableParallelism();
const testConcurrency = tjs.env.TJS_TEST_CONCURRENCY ?? tjs.system.availableParallelism;
const running = new Set();

// eslint-disable-next-line no-constant-condition
Expand Down
2 changes: 1 addition & 1 deletion src/mod_sys.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ static JSValue tjs_evalScript(JSContext *ctx, JSValue this_val, int argc, JSValu
}

static JSValue tjs_runRepl(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) {
tjs__eval_bytecode(ctx, tjs__run_repl, tjs__run_repl_size);
tjs__eval_bytecode(ctx, tjs__run_repl, tjs__run_repl_size, false);

return JS_UNDEFINED;
}
Expand Down
2 changes: 1 addition & 1 deletion src/private.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ int js_module_set_import_meta(JSContext *ctx, JSValue func_val, JS_BOOL use_real

JSValue tjs__get_args(JSContext *ctx);

int tjs__eval_bytecode(JSContext *ctx, const uint8_t *buf, size_t buf_len);
int tjs__eval_bytecode(JSContext *ctx, const uint8_t *buf, size_t buf_len, bool check_promise);

void tjs__destroy_timers(TJSRuntime *qrt);

Expand Down
6 changes: 3 additions & 3 deletions src/vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -272,8 +272,8 @@ TJSRuntime *TJS_NewRuntimeInternal(bool is_worker, TJSRunOptions *options) {

tjs__bootstrap_core(qrt->ctx, core);

CHECK_EQ(tjs__eval_bytecode(qrt->ctx, tjs__polyfills, tjs__polyfills_size), 0);
CHECK_EQ(tjs__eval_bytecode(qrt->ctx, tjs__core, tjs__core_size), 0);
CHECK_EQ(tjs__eval_bytecode(qrt->ctx, tjs__polyfills, tjs__polyfills_size, true), 0);
CHECK_EQ(tjs__eval_bytecode(qrt->ctx, tjs__core, tjs__core_size, true), 0);

/* end bootstrap */
JS_FreeAtom(qrt->ctx, core_atom);
Expand Down Expand Up @@ -420,7 +420,7 @@ int TJS_Run(TJSRuntime *qrt) {
uv_unref((uv_handle_t *) &qrt->stop);

/* If we are running the main interpreter, run the entrypoint. */
ret = tjs__eval_bytecode(qrt->ctx, tjs__run_main, tjs__run_main_size);
ret = tjs__eval_bytecode(qrt->ctx, tjs__run_main, tjs__run_main_size, true);
}

if (ret != 0)
Expand Down
2 changes: 1 addition & 1 deletion src/worker.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ static void worker_entry(void *arg) {
JS_FreeValue(ctx, sym);
JS_FreeValue(ctx, global_obj);

CHECK_EQ(tjs__eval_bytecode(ctx, tjs__worker_bootstrap, tjs__worker_bootstrap_size), 0);
CHECK_EQ(tjs__eval_bytecode(ctx, tjs__worker_bootstrap, tjs__worker_bootstrap_size, true), 0);

/* Load and eval the specifier when the loop runs. */
JSValue specifier = JS_NewString(ctx, wd->specifier);
Expand Down
2 changes: 1 addition & 1 deletion tests/test-compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const status = await proc.wait();

assert.ok(status.exit_status === 0 && status.term_signal === null, 'succeeded');

const newExe = tjs.platform === 'windows' ? 'hello.exe' : 'hello';
const newExe = tjs.system.platform === 'windows' ? 'hello.exe' : 'hello';

const st = await tjs.stat(newExe);

Expand Down
2 changes: 1 addition & 1 deletion tests/test-ffi.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import initCParser from '../src/js/stdlib/ffi/ffiutils.js';

(function(){
let sopath = './build/libffi-test.so';
switch(tjs.platform){
switch(tjs.system.platform){
case 'linux':
sopath = './build/libffi-test.so';
break;
Expand Down
4 changes: 2 additions & 2 deletions tests/test-fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ async function mkdir() {
const result = await tjs.stat(path);
assert.ok(result.isDirectory, 'directory was created ok');
/* NOTE: File permission mode not supported on Windows. */
if (tjs.platform !== 'windows')
if (tjs.system.platform !== 'windows')
assert.eq(result.mode & ~s_ifmt, s_irwxu);
await tjs.remove(path);
};

async function chmod() {
/* NOTE: File permission mode not supported on Windows. */
if (tjs.platform === 'windows')
if (tjs.system.platform === 'windows')
return;

const path = `./test_mkdir${tjs.pid}`;
Expand Down
2 changes: 1 addition & 1 deletion tests/test-kill.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const proc = tjs.spawn(args, { stdout: 'ignore', stderr: 'ignore' });
tjs.kill(proc.pid, 'SIGKILL');
const status = await proc.wait();

if (tjs.platform === 'windows') {
if (tjs.system.platform === 'windows') {
/* uv_kill() behavior on Windows causes the process to exit 1 and
* does not propagate the terminating signal information to the process
* handle.
Expand Down
2 changes: 1 addition & 1 deletion tests/test-pipe abstract.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import assert from 'tjs:assert';

// Abstract sockets are a Linux only thing.

if (tjs.platform === 'linux') {
if (tjs.system.platform === 'linux') {
const encoder = new TextEncoder();
const decoder = new TextDecoder();
const pipeName = '\0testPipe';
Expand Down
2 changes: 1 addition & 1 deletion tests/test-pipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const encoder = new TextEncoder();
const decoder = new TextDecoder();

let pipeName;
if (tjs.platform === 'windows') {
if (tjs.system.platform === 'windows') {
pipeName = '\\\\?\\pipe\\testPipe';
} else {
pipeName = 'testPipe';
Expand Down
12 changes: 6 additions & 6 deletions tests/test-posix-socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const fromHexString = (hexString) =>
function testUdpSock(){
const sock = new PosixSocket(PosixSocket.defines.AF_INET, PosixSocket.defines.SOCK_DGRAM, 0);
const sockaddr_bind = PosixSocket.createSockaddrIn('0.0.0.0', 12345);
if(tjs.platform == 'darwin'){ // macos has a slightly different sockaddr definition
if(tjs.system.platform == 'darwin'){ // macos has a slightly different sockaddr definition
assert.eq(sockaddr_bind, fromHexString('00023039000000000000000000000000'));
}else{
assert.eq(sockaddr_bind, fromHexString('02003039000000000000000000000000'));
Expand All @@ -20,7 +20,7 @@ function testUdpSock(){
assert.eq(sendsz, sendbuf.length);
const recv = sock.recvmsg(sendbuf.length, 0);
assert.eq(sendbuf, recv.data);
if(tjs.platform == 'darwin'){ // macos prefixes sockaddr with length (when coming from kernel), so we just skip it
if(tjs.system.platform == 'darwin'){ // macos prefixes sockaddr with length (when coming from kernel), so we just skip it
assert.eq(sockaddr_rem.slice(1), recv.addr.slice(1));
}else{
assert.eq(sockaddr_rem, recv.addr);
Expand All @@ -31,7 +31,7 @@ function testUdpSock(){
async function testTcpSock(){
const sock = new PosixSocket(PosixSocket.defines.AF_INET, PosixSocket.defines.SOCK_STREAM, 0);
const sockaddr_bind = PosixSocket.createSockaddrIn('0.0.0.0', 55678);
if(tjs.platform == 'darwin'){ // macos has a slightly different sockaddr definition
if(tjs.system.platform == 'darwin'){ // macos has a slightly different sockaddr definition
assert.eq(sockaddr_bind, fromHexString('0002d97e000000000000000000000000'));
}else{
assert.eq(sockaddr_bind, fromHexString('0200d97e000000000000000000000000'));
Expand All @@ -44,7 +44,7 @@ async function testTcpSock(){
assert.throws(()=>sock.getopt(PosixSocket.defines.SOL_SOCKET, PosixSocket.defines.SO_REUSEADDR, 0));
assert.throws(()=>sock.getopt(PosixSocket.defines.SOL_SOCKET, PosixSocket.defines.SO_BINDTODEVICE, 1));
const optval2 = sock.getopt(PosixSocket.defines.SOL_SOCKET, PosixSocket.defines.SO_REUSEADDR, 4);
if(tjs.platform != 'darwin'){ // skip this one on macos, the result there seems to be different
if(tjs.system.platform != 'darwin'){ // skip this one on macos, the result there seems to be different
assert.eq(optval, optval2);
}

Expand Down Expand Up @@ -99,7 +99,7 @@ async function testPoll(){
}

function testHelpers(){
const nis = tjs.networkInterfaces();
const nis = tjs.system.networkInterfaces;
for(const ni of nis){
const ind = PosixSocket.nametoindex(ni.name);
assert.truthy(ind >= 0);
Expand All @@ -113,7 +113,7 @@ function testHelpers(){
}

async function run(){
if(tjs.platform == 'windows'){
if(tjs.system.platform == 'windows'){
// This module is only supported on Unix systems.
return;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/test-signal.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import assert from 'tjs:assert';

if (tjs.platform === 'windows') {
if (tjs.system.platform === 'windows') {
/* Signals emulated on Windows do not allow this to be tested
* by sending a signal via kill(), so don't continue the test.
*/
Expand Down
4 changes: 2 additions & 2 deletions tests/test-userinfo.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import assert from 'tjs:assert';


const { userId, groupId, shell, userName, homeDir } = tjs.userInfo;
const { userId, groupId, shell, userName, homeDir } = tjs.system.userInfo;

if (tjs.platform === 'windows') {
if (tjs.system.platform === 'windows') {
assert.eq(userId, -1);
assert.eq(groupId, -1);
assert.eq(shell, null);
Expand Down
Loading