Skip to content

Commit

Permalink
refactor(core): Move Deno.core bindings to ops (denoland#14793)
Browse files Browse the repository at this point in the history
  • Loading branch information
nayeemrmn committed Jun 7, 2022
1 parent cfb6067 commit 9385a91
Show file tree
Hide file tree
Showing 17 changed files with 941 additions and 1,260 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ jobs:
~/.cargo/registry/index
~/.cargo/registry/cache
~/.cargo/git/db
key: 14-cargo-home-${{ matrix.os }}-${{ hashFiles('Cargo.lock') }}
key: 15-cargo-home-${{ matrix.os }}-${{ hashFiles('Cargo.lock') }}

# In main branch, always creates fresh cache
- name: Cache build output (main)
Expand All @@ -251,7 +251,7 @@ jobs:
!./target/*/*.zip
!./target/*/*.tar.gz
key: |
14-cargo-target-${{ matrix.os }}-${{ matrix.profile }}-${{ github.sha }}
15-cargo-target-${{ matrix.os }}-${{ matrix.profile }}-${{ github.sha }}
# Restore cache from the latest 'main' branch build.
- name: Cache build output (PR)
Expand All @@ -267,7 +267,7 @@ jobs:
!./target/*/*.tar.gz
key: never_saved
restore-keys: |
14-cargo-target-${{ matrix.os }}-${{ matrix.profile }}-
15-cargo-target-${{ matrix.os }}-${{ matrix.profile }}-
# Don't save cache after building PRs or branches other than 'main'.
- name: Skip save cache (PR)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[WILDCARD]error: Uncaught Error: foo
Warning Couldn't format source line: Column 31 is out of bounds (source may have changed at runtime)
at file:https:///[WILDCARD]/eval_context_conflicting_source.ts:1:31
at [WILDCARD]
at file:https:///[WILDCARD]/eval_context_throw_with_conflicting_source.ts:[WILDCARD]
18 changes: 15 additions & 3 deletions core/00_primordials.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,6 @@
// Create copy of isNaN
primordials[isNaN.name] = isNaN;

// Create copy of queueMicrotask
primordials["queueMicrotask"] = queueMicrotask;

// Create copies of URI handling functions
[
decodeURI,
Expand Down Expand Up @@ -280,6 +277,7 @@
ArrayPrototypeForEach,
FunctionPrototypeCall,
Map,
ObjectDefineProperty,
ObjectFreeze,
ObjectSetPrototypeOf,
Promise,
Expand Down Expand Up @@ -456,6 +454,20 @@
.then(a, b)
);

// Create getter and setter for `queueMicrotask`, it hasn't been bound yet.
let queueMicrotask = undefined;
ObjectDefineProperty(primordials, "queueMicrotask", {
get() {
return queueMicrotask;
},
});
primordials.setQueueMicrotask = (value) => {
if (queueMicrotask !== undefined) {
throw new Error("queueMicrotask is already defined");
}
queueMicrotask = value;
};

ObjectSetPrototypeOf(primordials, null);
ObjectFreeze(primordials);

Expand Down
77 changes: 41 additions & 36 deletions core/01_core.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,10 @@
StringPrototypeSlice,
ObjectAssign,
SymbolFor,
setQueueMicrotask,
} = window.__bootstrap.primordials;
const ops = window.Deno.core.ops;

// Available on start due to bindings.
const { refOp_, unrefOp_ } = window.Deno.core;

const errorMap = {};
// Builtin v8 / JS errors
registerErrorClass("Error", Error);
Expand Down Expand Up @@ -176,53 +174,33 @@
if (!hasPromise(promiseId)) {
return;
}
refOp_(promiseId);
opSync("op_ref_op", promiseId);
}

function unrefOp(promiseId) {
if (!hasPromise(promiseId)) {
return;
}
unrefOp_(promiseId);
opSync("op_unref_op", promiseId);
}

function resources() {
return ObjectFromEntries(opSync("op_resources"));
}

function read(rid, buf) {
return opAsync("op_read", rid, buf);
}

function write(rid, buf) {
return opAsync("op_write", rid, buf);
}

function shutdown(rid) {
return opAsync("op_shutdown", rid);
}

function close(rid) {
opSync("op_close", rid);
}

function tryClose(rid) {
opSync("op_try_close", rid);
}

function print(str, isErr = false) {
opSync("op_print", str, isErr);
}

function metrics() {
const [aggregate, perOps] = opSync("op_metrics");
aggregate.ops = ObjectFromEntries(ArrayPrototypeMap(
core.opNames(),
core.opSync("op_op_names"),
(opName, opId) => [opName, perOps[opId]],
));
return aggregate;
}

function queueMicrotask(...args) {
return opSync("op_queue_microtask", ...args);
}

// Some "extensions" rely on "BadResource" and "Interrupted" errors in the
// JS code (eg. "deno_net") so they are provided in "Deno.core" but later
// reexported on "Deno.errors"
Expand All @@ -246,12 +224,6 @@
const core = ObjectAssign(globalThis.Deno.core, {
opAsync,
opSync,
close,
tryClose,
read,
write,
shutdown,
print,
resources,
metrics,
registerErrorBuilder,
Expand All @@ -266,8 +238,41 @@
opCallTraces,
refOp,
unrefOp,
close: opSync.bind(null, "op_close"),
tryClose: opSync.bind(null, "op_try_close"),
read: opAsync.bind(null, "op_read"),
write: opAsync.bind(null, "op_write"),
shutdown: opAsync.bind(null, "op_shutdown"),
print: opSync.bind(null, "op_print"),
setMacrotaskCallback: opSync.bind(null, "op_set_macrotask_callback"),
setNextTickCallback: opSync.bind(null, "op_set_next_tick_callback"),
runMicrotasks: opSync.bind(null, "op_run_microtasks"),
hasTickScheduled: opSync.bind(null, "op_has_tick_scheduled"),
setHasTickScheduled: opSync.bind(null, "op_set_has_tick_scheduled"),
evalContext: opSync.bind(null, "op_eval_context"),
createHostObject: opSync.bind(null, "op_create_host_object"),
encode: opSync.bind(null, "op_encode"),
decode: opSync.bind(null, "op_decode"),
serialize: opSync.bind(null, "op_serialize"),
deserialize: opSync.bind(null, "op_deserialize"),
getPromiseDetails: opSync.bind(null, "op_get_promise_details"),
getProxyDetails: opSync.bind(null, "op_get_proxy_details"),
isProxy: opSync.bind(null, "op_is_proxy"),
memoryUsage: opSync.bind(null, "op_memory_usage"),
setWasmStreamingCallback: opSync.bind(
null,
"op_set_wasm_streaming_callback",
),
abortWasmStreaming: opSync.bind(null, "op_abort_wasm_streaming"),
destructureError: opSync.bind(null, "op_destructure_error"),
terminate: opSync.bind(null, "op_terminate"),
opNames: opSync.bind(null, "op_op_names"),
});

ObjectAssign(globalThis.__bootstrap, { core });
ObjectAssign(globalThis.Deno, { core });

// Direct bindings on `globalThis`
ObjectAssign(globalThis, { queueMicrotask });
setQueueMicrotask(queueMicrotask);
})(globalThis);
2 changes: 1 addition & 1 deletion core/02_error.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@
if (fileName && lineNumber != null && columnNumber != null) {
return patchCallSite(
callSite,
core.applySourceMap({
core.opSync("op_apply_source_map", {
fileName,
lineNumber,
columnNumber,
Expand Down
Loading

0 comments on commit 9385a91

Please sign in to comment.