Skip to content

Commit

Permalink
misc: refactor how internal functions are exposed
Browse files Browse the repository at this point in the history
Use a global symbol with the 'tjs.internal' key to hide the stuff that
we might need at runtime, for example for the REPL, but we don't want to
make part of the API.
  • Loading branch information
saghul committed Nov 11, 2022
1 parent 8b3ac8f commit 8a521df
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 21 deletions.
16 changes: 7 additions & 9 deletions src/js/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -14764,19 +14764,19 @@ var noExport = [
"clearInterval",
"clearTimeout",
"evalScript",
"ffi",
"guessHandle",
"hrtimeMs",
"mkstemp",
"newStdioFile",
"open",
"posix_socket",
"random",
"setInterval",
"setTimeout",
"signal",
"signals",
"wasm",
"posix_socket",
"ffi"
"wasm"
];
for (const [key, value] of Object.entries(core10)) {
if (noExport.includes(key)) {
Expand Down Expand Up @@ -14804,12 +14804,6 @@ Object.defineProperty(tjs2, "prompt", {
writable: false,
value: prompt
});
Object.defineProperty(tjs2, "_evalScript", {
enumerable: false,
configurable: false,
writable: false,
value: core10.evalScript
});
Object.defineProperty(tjs2, "open", {
enumerable: true,
configurable: false,
Expand Down Expand Up @@ -14872,6 +14866,10 @@ if (core10.posix_socket) {
value: PosixSocket
});
}
var kInternal = Symbol.for("tjs.internal");
tjs2[kInternal] = {
evalScript: core10.evalScript
};
Object.defineProperty(globalThis, "tjs", {
enumerable: true,
configurable: false,
Expand Down
5 changes: 4 additions & 1 deletion src/js/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@

import * as std from '@tjs/std';

const kInternal = Symbol.for('tjs.internal');
const internal = tjs[kInternal];

window.addEventListener('unhandledrejection', event => {
// Avoid aborting in unhandled promised on the REPL.
event.preventDefault();
Expand Down Expand Up @@ -1144,7 +1147,7 @@ window.addEventListener('unhandledrejection', event => {
var now = (new Date).getTime();

/* eval as a script */
result = tjs._evalScript(expr);
result = internal.evalScript(expr);
eval_time = (new Date).getTime() - now;
stdout_write(colors[styles.result]);
print(result);
Expand Down
21 changes: 10 additions & 11 deletions src/js/tjs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,19 @@ const noExport = [
'clearInterval',
'clearTimeout',
'evalScript',
'ffi',
'guessHandle',
'hrtimeMs',
'mkstemp',
'newStdioFile',
'open',
'posix_socket',
'random',
'setInterval',
'setTimeout',
'signal',
'signals',
'wasm',
'posix_socket',
'ffi' // is exported as import from ffi.js
'wasm'
];

for (const [ key, value ] of Object.entries(core)) {
Expand Down Expand Up @@ -77,14 +77,6 @@ Object.defineProperty(tjs, 'prompt', {
value: prompt
});

// For the REPL.
Object.defineProperty(tjs, '_evalScript', {
enumerable: false,
configurable: false,
writable: false,
value: core.evalScript
});

// FS.
Object.defineProperty(tjs, 'open', {
enumerable: true,
Expand Down Expand Up @@ -158,6 +150,13 @@ if (core.posix_socket) {
});
}

// Internal stuff needed at runtime, like in the REPL.
const kInternal = Symbol.for('tjs.internal');

tjs[kInternal] = {
evalScript: core.evalScript
};

// tjs global.
Object.defineProperty(globalThis, 'tjs', {
enumerable: true,
Expand Down

0 comments on commit 8a521df

Please sign in to comment.