-
Notifications
You must be signed in to change notification settings - Fork 5.4k
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
Deno.core.dispatch uses op name instead of id #6395
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,12 @@ | ||
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. | ||
|
||
import { sendAsyncMinimal, sendSyncMinimal } from "./dispatch_minimal.ts"; | ||
// TODO(bartlomieju): remove this import and maybe lazy-initialize | ||
// OPS_CACHE that belongs only to this module | ||
import { OPS_CACHE } from "../runtime.ts"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto |
||
|
||
// This is done because read/write are extremely performance sensitive. | ||
let OP_READ = -1; | ||
let OP_WRITE = -1; | ||
|
||
export function readSync(rid: number, buffer: Uint8Array): number | null { | ||
if (buffer.length == 0) { | ||
return 0; | ||
} | ||
if (OP_READ < 0) { | ||
OP_READ = OPS_CACHE["op_read"]; | ||
} | ||
const nread = sendSyncMinimal(OP_READ, rid, buffer); | ||
const nread = sendSyncMinimal("op_read", rid, buffer); | ||
if (nread < 0) { | ||
throw new Error("read error"); | ||
} else if (nread == 0) { | ||
|
@@ -33,10 +23,7 @@ export async function read( | |
if (buffer.length == 0) { | ||
return 0; | ||
} | ||
if (OP_READ < 0) { | ||
OP_READ = OPS_CACHE["op_read"]; | ||
} | ||
const nread = await sendAsyncMinimal(OP_READ, rid, buffer); | ||
const nread = await sendAsyncMinimal("op_read", rid, buffer); | ||
if (nread < 0) { | ||
throw new Error("read error"); | ||
} else if (nread == 0) { | ||
|
@@ -47,10 +34,7 @@ export async function read( | |
} | ||
|
||
export function writeSync(rid: number, data: Uint8Array): number { | ||
if (OP_WRITE < 0) { | ||
OP_WRITE = OPS_CACHE["op_write"]; | ||
} | ||
const result = sendSyncMinimal(OP_WRITE, rid, data); | ||
const result = sendSyncMinimal("op_write", rid, data); | ||
if (result < 0) { | ||
throw new Error("write error"); | ||
} else { | ||
|
@@ -59,10 +43,7 @@ export function writeSync(rid: number, data: Uint8Array): number { | |
} | ||
|
||
export async function write(rid: number, data: Uint8Array): Promise<number> { | ||
if (OP_WRITE < 0) { | ||
OP_WRITE = OPS_CACHE["op_write"]; | ||
} | ||
const result = await sendAsyncMinimal(OP_WRITE, rid, data); | ||
const result = await sendAsyncMinimal("op_write", rid, data); | ||
if (result < 0) { | ||
throw new Error("write error"); | ||
} else { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,8 +9,6 @@ import { setPrepareStackTrace } from "./error_stack.ts"; | |
import { Start, opStart } from "./ops/runtime.ts"; | ||
import { handleTimerMacrotask } from "./web/timers.ts"; | ||
|
||
export let OPS_CACHE: { [name: string]: number }; | ||
|
||
function getAsyncHandler(opName: string): (msg: Uint8Array) => void { | ||
switch (opName) { | ||
case "op_write": | ||
|
@@ -24,8 +22,8 @@ function getAsyncHandler(opName: string): (msg: Uint8Array) => void { | |
// TODO(bartlomieju): temporary solution, must be fixed when moving | ||
// dispatches to separate crates | ||
export function initOps(): void { | ||
OPS_CACHE = core.ops(); | ||
for (const [name, opId] of Object.entries(OPS_CACHE)) { | ||
const opsMap = core.ops(); | ||
for (const [name, opId] of Object.entries(opsMap)) { | ||
core.setAsyncHandler(opId, getAsyncHandler(name)); | ||
} | ||
Comment on lines
+26
to
28
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Async handler registration is the last API bit that's a bit strange IMO and could be structured better |
||
core.setMacrotaskCallback(handleTimerMacrotask); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,6 +37,7 @@ SharedQueue Binary Layout | |
let asyncHandlers; | ||
|
||
let initialized = false; | ||
let opsCache = {}; | ||
|
||
function maybeInit() { | ||
if (!initialized) { | ||
|
@@ -61,7 +62,8 @@ SharedQueue Binary Layout | |
// op id 0 is a special value to retrieve the map of registered ops. | ||
const opsMapBytes = send(0, new Uint8Array([])); | ||
const opsMapJson = String.fromCharCode.apply(null, opsMapBytes); | ||
return JSON.parse(opsMapJson); | ||
opsCache = JSON.parse(opsMapJson); | ||
return { ...opsCache }; | ||
} | ||
|
||
function assert(cond) { | ||
|
@@ -181,9 +183,14 @@ SharedQueue Binary Layout | |
} | ||
} | ||
|
||
function dispatch(opName, control, ...zeroCopy) { | ||
return send(opsCache[opName], control, ...zeroCopy); | ||
} | ||
|
||
Object.assign(window.Deno.core, { | ||
setAsyncHandler, | ||
dispatch: send, | ||
dispatchByName: dispatch, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. name to be determined |
||
ops, | ||
// sharedQueue is private but exposed for testing. | ||
sharedQueue: { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removes
OPS_CACHE
indirection which was once problematic place when trying to snapshot ES modules