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

provide core internals via import meta #491

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/builtins.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ JSModuleDef *tjs__load_builtin(JSContext *ctx, const char *name) {
CHECK_EQ(JS_IsException(obj), 0);
CHECK_EQ(JS_VALUE_GET_TAG(obj), JS_TAG_MODULE);

tjs_provide_internals(ctx, obj);

JSModuleDef *m = JS_VALUE_GET_PTR(obj);
JS_FreeValue(ctx, obj);

Expand Down
11,176 changes: 5,579 additions & 5,597 deletions src/bundles/c/core/core.c

Large diffs are not rendered by default.

154,619 changes: 77,300 additions & 77,319 deletions src/bundles/c/core/polyfills.c

Large diffs are not rendered by default.

13,599 changes: 6,795 additions & 6,804 deletions src/bundles/c/core/run-main.c

Large diffs are not rendered by default.

13,136 changes: 6,566 additions & 6,570 deletions src/bundles/c/stdlib/ffi.c

Large diffs are not rendered by default.

717 changes: 356 additions & 361 deletions src/bundles/c/stdlib/sqlite.c

Large diffs are not rendered by default.

34 changes: 30 additions & 4 deletions src/eval.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,47 @@

#include "private.h"


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

if (JS_IsException(obj))
goto error;
return obj;

if (JS_VALUE_GET_TAG(obj) == JS_TAG_MODULE) {
if (JS_ResolveModule(ctx, obj) < 0) {
JS_FreeValue(ctx, obj);
goto error;
return JS_EXCEPTION;
}

js_module_set_import_meta(ctx, obj, FALSE, FALSE);
}
return obj;
}

int tjs__eval_bytecode_internal(JSContext* ctx, const uint8_t *buf, size_t buf_len) {
JSValue obj = tjs__bytecode_load(ctx, buf, buf_len);
if (JS_IsException(obj))
goto error;

tjs_provide_internals(ctx, obj);

JSValue val = JS_EvalFunction(ctx, obj);
if (JS_IsException(val))
goto error;

JS_FreeValue(ctx, val);

return 0;

error:
tjs_dump_error(ctx);
return -1;
}

int tjs__eval_bytecode(JSContext *ctx, const uint8_t *buf, size_t buf_len) {
JSValue obj = tjs__bytecode_load(ctx, buf, buf_len);
if (JS_IsException(obj))
goto error;

JSValue val = JS_EvalFunction(ctx, obj);
if (JS_IsException(val))
Expand Down
2 changes: 1 addition & 1 deletion src/js/core/fs.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const core = globalThis[Symbol.for('tjs.internal.core')];
const core = import.meta.core;

import pathModule from './path.js';
import { readableStreamForHandle, writableStreamForHandle } from './stream-utils.js';
Expand Down
2 changes: 1 addition & 1 deletion src/js/core/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const core = globalThis[Symbol.for('tjs.internal.core')];
const core = import.meta.core;

import { alert, confirm, prompt } from './alert-confirm-prompt.js';
import { open, mkdir, mkstemp, rm } from './fs.js';
Expand Down
2 changes: 1 addition & 1 deletion src/js/core/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
/* eslint-disable */

// Adapted for txiki.js
const core = globalThis[Symbol.for('tjs.internal.core')];
const core = import.meta.core;

// Primordials

Expand Down
2 changes: 1 addition & 1 deletion src/js/core/posix-socket.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const core = globalThis[Symbol.for('tjs.internal.core')];
const core = import.meta.core;
const posixSocket = core.posix_socket;

export let PosixSocket;
Expand Down
2 changes: 1 addition & 1 deletion src/js/core/signal.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const core = globalThis[Symbol.for('tjs.internal.core')];
const core = import.meta.core;

const data = Object.create(null);

Expand Down
2 changes: 1 addition & 1 deletion src/js/core/sockets.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { readableStreamForHandle, writableStreamForHandle } from './stream-utils.js';

const core = globalThis[Symbol.for('tjs.internal.core')];
const core = import.meta.core;


export async function connect(transport, host, port, options = {}) {
Expand Down
2 changes: 1 addition & 1 deletion src/js/core/stdio.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const core = globalThis[Symbol.for('tjs.internal.core')];
const core = import.meta.core;

const kStdioHandle = Symbol('kStdioHandle');
const kStdioHandleType = Symbol('kStdioHandleType');
Expand Down
2 changes: 1 addition & 1 deletion src/js/polyfills/console.js
Original file line number Diff line number Diff line change
Expand Up @@ -325,4 +325,4 @@ Object.defineProperty(window, 'console', {
});


globalThis[Symbol.for('tjs.internal.core')].createConsole = createConsole;
import.meta.core.createConsole = createConsole;
2 changes: 1 addition & 1 deletion src/js/polyfills/crypto.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const core = globalThis[Symbol.for('tjs.internal.core')];
const core = import.meta.core;

const TypedArrayPrototype = Object.getPrototypeOf(Uint8Array.prototype);
const TypedArrayProto_toStringTag = Object.getOwnPropertyDescriptor(TypedArrayPrototype, Symbol.toStringTag).get;
Expand Down
2 changes: 1 addition & 1 deletion src/js/polyfills/storage.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* global tjs */

const core = globalThis[Symbol.for('tjs.internal.core')];
const core = import.meta.core;
const sqlite3 = core._sqlite3;

const kStorageMap = Symbol('kStorageMap');
Expand Down
2 changes: 1 addition & 1 deletion src/js/polyfills/timers.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const core = globalThis[Symbol.for('tjs.internal.core')];
const core = import.meta.core;

globalThis.setTimeout = core.setTimeout;
globalThis.clearTimeout = core.clearTimeout;
Expand Down
2 changes: 1 addition & 1 deletion src/js/polyfills/wasm.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const core = globalThis[Symbol.for('tjs.internal.core')];
const core = import.meta.core;
const wasm = core.wasm;

const kWasmModule = Symbol('kWasmModule');
Expand Down
2 changes: 1 addition & 1 deletion src/js/polyfills/worker.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const core = globalThis[Symbol.for('tjs.internal.core')];
const core = import.meta.core;
const _Worker = core.Worker;

import { defineEventAttribute } from './event-target';
Expand Down
2 changes: 1 addition & 1 deletion src/js/polyfills/ws.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { defineEventAttribute } from './event-target.js';

const core = globalThis[Symbol.for('tjs.internal.core')];
const core = import.meta.core;
const WS = core.WebSocket;
const kWS = Symbol('kWS');
const kWsBinaryType = Symbol('kWsBinaryType');
Expand Down
2 changes: 1 addition & 1 deletion src/js/polyfills/xhr.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { defineEventAttribute } from './event-target.js';

const core = globalThis[Symbol.for('tjs.internal.core')];
const core = import.meta.core;
const XHR = core.XMLHttpRequest;
const kXHR = Symbol('kXHR');

Expand Down
2 changes: 1 addition & 1 deletion src/js/run-main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { evalStdin } from './eval-stdin.js';
import { runRepl } from './repl.js';
import { runTests } from './run-tests.js';

const core = globalThis[Symbol.for('tjs.internal.core')];
const core = import.meta.core;

const exeName = path.basename(tjs.args[0]);
const help = `Usage: ${exeName} [options] [subcommand]
Expand Down
2 changes: 1 addition & 1 deletion src/js/run-main/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ function _run(g) {
/* current X position of the cursor in the terminal */
var term_cursor_x = 0;

var { evalScript } = globalThis[Symbol.for('tjs.internal.core')];
var { evalScript } = import.meta.core;

var encoder = new TextEncoder();

Expand Down
2 changes: 1 addition & 1 deletion src/js/stdlib/ffi/ffi.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const core = globalThis[Symbol.for('tjs.internal.core')];
const core = import.meta.core;
const ffiInt = core.ffi_load_native();

import buildCParser from './ffiutils.js';
Expand Down
2 changes: 1 addition & 1 deletion src/js/stdlib/sqlite.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const core = globalThis[Symbol.for('tjs.internal.core')];
const core = import.meta.core;
const sqlite3 = core._sqlite3;

const kSqlite3Handle = Symbol('kSqlite3Handle');
Expand Down
4 changes: 4 additions & 0 deletions src/private.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ struct TJSRuntime {
TJSTimer *timers;
int64_t next_timer;
} timers;
JSValue core;
};

void tjs__mod_dns_init(JSContext *ctx, JSValue ns);
Expand Down Expand Up @@ -99,13 +100,16 @@ int js_module_set_import_meta(JSContext *ctx, JSValueConst func_val, JS_BOOL use

JSValue tjs__get_args(JSContext *ctx);

JSValue tjs__load_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);
int tjs__eval_bytecode_internal(JSContext *ctx, const uint8_t *buf, size_t buf_len);

void tjs__destroy_timers(TJSRuntime *qrt);

uv_loop_t *TJS_GetLoop(TJSRuntime *qrt);
TJSRuntime *TJS_NewRuntimeWorker(void);
TJSRuntime *TJS_NewRuntimeInternal(bool is_worker, TJSRunOptions *options);
JSValue TJS_EvalModule(JSContext *ctx, const char *filename, bool is_main);
void tjs_provide_internals(JSContext *ctx, JSValue func);

#endif
30 changes: 17 additions & 13 deletions src/vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,16 @@ TJSRuntime *TJS_NewRuntimeWorker(void) {
return TJS_NewRuntimeInternal(true, &options);
}

void tjs_provide_internals(JSContext *ctx, JSValue func){
TJSRuntime *qrt = JS_GetContextOpaque(ctx);

CHECK_EQ(JS_VALUE_GET_TAG(func), JS_TAG_MODULE);
JSModuleDef* m = JS_VALUE_GET_PTR(func);
JSValue meta_obj = JS_GetImportMeta(ctx, m);
JS_DefinePropertyValueStr(ctx, meta_obj, "core", JS_DupValue(ctx, qrt->core), JS_PROP_ENUMERABLE);
JS_FreeValue(ctx, meta_obj);
}

TJSRuntime *TJS_NewRuntimeInternal(bool is_worker, TJSRunOptions *options) {
TJSRuntime *qrt = tjs__calloc(1, sizeof(*qrt));

Expand Down Expand Up @@ -265,23 +275,16 @@ TJSRuntime *TJS_NewRuntimeInternal(bool is_worker, TJSRunOptions *options) {
JS_SetHostPromiseRejectionTracker(qrt->rt, tjs__promise_rejection_tracker, NULL);

/* start bootstrap */
JSValue global_obj = JS_GetGlobalObject(qrt->ctx);
JSValue core_sym = JS_NewSymbol(qrt->ctx, "tjs.internal.core", TRUE);
JSAtom core_atom = JS_ValueToAtom(qrt->ctx, core_sym);
JSValue core = JS_NewObjectProto(qrt->ctx, JS_NULL);
qrt->core = JS_NewObjectProto(qrt->ctx, JS_NULL);

CHECK_EQ(JS_DefinePropertyValue(qrt->ctx, global_obj, core_atom, core, JS_PROP_C_W_E), TRUE);
CHECK_EQ(JS_DefinePropertyValueStr(qrt->ctx, core, "isWorker", JS_NewBool(qrt->ctx, is_worker), JS_PROP_C_W_E), TRUE);
CHECK_EQ(JS_DefinePropertyValueStr(qrt->ctx, qrt->core, "isWorker", JS_NewBool(qrt->ctx, is_worker), JS_PROP_C_W_E), TRUE);

tjs__bootstrap_core(qrt->ctx, core);
tjs__bootstrap_core(qrt->ctx, qrt->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_internal(qrt->ctx, tjs__polyfills, tjs__polyfills_size), 0);
CHECK_EQ(tjs__eval_bytecode_internal(qrt->ctx, tjs__core, tjs__core_size), 0);

/* end bootstrap */
JS_FreeAtom(qrt->ctx, core_atom);
JS_FreeValue(qrt->ctx, core_sym);
JS_FreeValue(qrt->ctx, global_obj);

/* WASM */
qrt->wasm_ctx.env = m3_NewEnvironment();
Expand Down Expand Up @@ -342,6 +345,7 @@ void TJS_FreeRuntime(TJSRuntime *qrt) {
qrt->curl_ctx.curlm_h = NULL;
}

JS_FreeValue(qrt->ctx, qrt->core);
JS_FreeContext(qrt->ctx);
JS_FreeRuntime(qrt->rt);

Expand Down Expand Up @@ -428,7 +432,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_internal(qrt->ctx, tjs__run_main, tjs__run_main_size);
}

if (ret != 0)
Expand Down