Skip to content

Commit

Permalink
Deno.core.ops
Browse files Browse the repository at this point in the history
  • Loading branch information
bartlomieju committed Sep 26, 2019
1 parent 62fc183 commit 3649445
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 28 deletions.
12 changes: 6 additions & 6 deletions core/examples/http_bench.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,29 +75,29 @@ function handleAsyncMsgFromRust(opId, buf) {

/** Listens on 0.0.0.0:4500, returns rid. */
function listen() {
return sendSync(Deno.core.ops["listen"], -1);
return sendSync(Deno.core.ops.get("listen"), -1);
}

/** Accepts a connection, returns rid. */
async function accept(rid) {
return await sendAsync(Deno.core.ops["accept"], rid);
return await sendAsync(Deno.core.ops.get("accept"), rid);
}

/**
* Reads a packet from the rid, presumably an http request. data is ignored.
* Returns bytes read.
*/
async function read(rid, data) {
return await sendAsync(Deno.core.ops["read"], rid, data);
return await sendAsync(Deno.core.ops.get("read"), rid, data);
}

/** Writes a fixed HTTP response to the socket rid. Returns bytes written. */
async function write(rid, data) {
return await sendAsync(Deno.core.ops["write"], rid, data);
return await sendAsync(Deno.core.ops.get("write"), rid, data);
}

function close(rid) {
return sendSync(Deno.core.ops["close"], rid);
return sendSync(Deno.core.ops.get("close"), rid);
}

async function serve(rid) {
Expand All @@ -117,7 +117,7 @@ async function serve(rid) {

async function main() {
Deno.core.setAsyncHandler(handleAsyncMsgFromRust);
Deno.core.initOps();
Deno.core.ops.init();

Deno.core.print("http_bench.js start\n");

Expand Down
35 changes: 15 additions & 20 deletions core/shared_queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ SharedQueue Binary Layout
let sharedBytes;
let shared32;
let initialized = false;
const opsMap = {};
let opsMap = {};

function maybeInit() {
if (!initialized) {
Expand All @@ -62,27 +62,20 @@ SharedQueue Binary Layout
function initOps() {
const opsMapBytes = Deno.core.send(0, new Uint8Array([]), null);
const opsMapJson = String.fromCharCode.apply(null, opsMapBytes);
for (const [key, value] of Object.entries(JSON.parse(opsMapJson))) {
opsMap[key] = value;
}
opsMap = JSON.parse(opsMapJson);
}

const opsMapProxy = new Proxy(opsMap, {
get(target, key) {
const opId = target[key];

if (!opId) {
throw new Error(
`Unknown op: "${key}". Did you forget to initialize ops with Deno.core.initOps()?`
);
}
function getOpId(name) {
const opId = opsMap[name];

return opId;
},
set() {
throw new Error("Setting op id is not allowed.");
if (!opId) {
throw new Error(
`Unknown op: "${key}". Did you forget to initialize ops with Deno.core.ops.init()?`
);
}
});

return opId;
}

function assert(cond) {
if (!cond) {
Expand Down Expand Up @@ -215,8 +208,10 @@ SharedQueue Binary Layout
reset,
shift
},
initOps,
ops: opsMapProxy
ops: {
init: initOps,
get: getOpId
}
};

assert(window[GLOBAL_NAMESPACE] != null);
Expand Down
6 changes: 4 additions & 2 deletions deno_typescript/lib.deno_core.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ declare interface DenoCore {
shift(): Uint8Array | null;
};

initOps(): void;
ops: Record<string, number>;
ops: {
init(): void;
get(name: string): number;
};

recv(cb: MessageCallback): void;

Expand Down

0 comments on commit 3649445

Please sign in to comment.