Skip to content

Commit

Permalink
make ops work
Browse files Browse the repository at this point in the history
  • Loading branch information
bartlomieju committed Sep 21, 2019
1 parent 6c9b4ef commit 31c2e7c
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 24 deletions.
28 changes: 16 additions & 12 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 rustOpsMap;
const jsOpsMap = new Map();
// const jsOpsMap = {};
let jsOpsAsyncHandlers;
let initialized = false;

Expand All @@ -61,24 +61,28 @@ SharedQueue Binary Layout
Deno.core.recv(handleAsyncMsgFromRust);
}

function initOps() {
function initOps(jsOpsMap) {
const opsMapBytes = Deno.core.send(0, new Uint8Array([]), null);
const opsMapJson = String.fromCharCode.apply(null, opsMapBytes);
rustOpsMap = JSON.parse(opsMapJson);
const opVector = new Array(Object.keys(rustOpsMap).length);

core.print(`rustOpsMap ${opsMapJson}\n`);
core.print(`jsOpsMap ${JSON.stringify(jsOpsMap)}\n`);
// core.print(`rustOpsMap ${opsMapJson}\n`);
// core.print(`jsOpsMap ${JSON.stringify(jsOpsMap)}\n`);

for (const [name, opId] of Object.entries(rustOpsMap)) {
const op = jsOpsMap.get(name);
const op = jsOpsMap[name];

if (!op) {
continue;
}

core.print(`op ${opId} ${name} ${op}`);
// core.print(`op ${opId} ${name} ${op}`);
op.setOpId(opId);
// core.print(`${op.constructor.handleAsyncMsgFromRust}\n`, true);
// core.print(`${op.handleAsyncMsgFromRust}\n`, true);
// core.print(`${op.constructor}\n`, true);
// core.print(`${op}\n`, true);
opVector[opId] = op.constructor.handleAsyncMsgFromRust;
}

Expand Down Expand Up @@ -209,15 +213,15 @@ SharedQueue Binary Layout

class Op {
constructor(name) {
core.print("registering op " + name + "\n", true);
throw new Error(`Registering op: ${name}`);
if (typeof jsOpsMap.get(name) !== "undefined") {
throw new Error(`Duplicate op: ${name}`);
}
// core.print("registering op " + name + "\n", true);
// throw new Error(`Registering op: ${name}`);
// if (typeof jsOpsMap[name] !== "undefined") {
// throw new Error(`Duplicate op: ${name}`);
// }

this.name = name;
this.opId = 0;
jsOpsMap.set(name, this);
// jsOpsMap[name] = this;
}

setOpId(opId) {
Expand Down
7 changes: 4 additions & 3 deletions deno_typescript/lib.deno_core.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ declare class Op {

static handleAsyncMsgFromRust(opId: OpId, buf: Uint8Array): void;

// eslint-disable @typescript-eslint/no-explicit-any
/* eslint-disable @typescript-eslint/no-explicit-any */
static sendSync(opId: OpId, control: any, zeroCopy?: Uint8Array): any;

static sendAsync(
opId: OpId,
control: any,
zeroCopy?: Uint8Array
): Promise<any>;
// eslint-enable @typescript-eslint/no-explicit-any
/* eslint-enable @typescript-eslint/no-explicit-any */
}

declare interface DenoCore {
Expand All @@ -61,7 +61,8 @@ declare interface DenoCore {
};

Op: typeof Op;
initOps(): void;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
initOps(map: Record<string, any>): void;

recv(cb: MessageCallback): void;

Expand Down
2 changes: 1 addition & 1 deletion js/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ interface EmitResult {
}

const OP_FETCH_ASSET = new JsonOp("fetch_asset");
const OP_FETCH_SOURCE_FILES = new JsonOp("op_fetch_source_files");
const OP_FETCH_SOURCE_FILES = new JsonOp("fetch_source_files");
const OP_CACHE = new JsonOp("cache");

function fetchAsset(name: string): string {
Expand Down
44 changes: 44 additions & 0 deletions js/dispatch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
export const dispatch: Record<string, any> = {};

export class DispatchOp {
public name: string;
public opId!: number;

constructor(name: string) {
// @ts-ignore
// Deno.core.print("registering op " + name + "\n", true);
// throw new Error(`Registering op: ${name}`);
if (typeof dispatch[name] !== "undefined") {
throw new Error(`Duplicate op: ${name}`);
}

this.name = name;
this.opId = 0;
dispatch[name] = this;
}

setOpId(opId: number): void {
this.opId = opId;
}

static handleAsyncMsgFromRust(_opId: number, _buf: Uint8Array): any {
throw new Error("Unimplemented");
}

static sendSync(
_opId: number,
_control: Uint8Array,
_zeroCopy?: Uint8Array
): any {
throw new Error("Unimplemented");
}

static sendAsync(
_opId: number,
_control: Uint8Array,
_zeroCopy?: Uint8Array
): Promise<any> {
throw new Error("Unimplemented");
}
}
10 changes: 4 additions & 6 deletions js/dispatch_json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as util from "./util.ts";
import { TextEncoder, TextDecoder } from "./text_encoding.ts";
import { core } from "./core.ts";
import { ErrorKind, DenoError } from "./errors.ts";

import { DispatchOp } from "./dispatch.ts";
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type Ok = any;

Expand All @@ -18,16 +18,15 @@ interface JsonResponse {
promiseId?: number; // Only present in async messages.
}

core.print("JSON op");
export class JsonOp extends core.Op {
export class JsonOp extends DispatchOp {
public opId!: number;

constructor(public name: string) {
super(name);
core.print("registering op " + name + "\n", true);
// core.print("registering op " + name + "\n", true);
}

static asyncMsgFromRust(opId: number, ui8: Uint8Array): void {
static handleAsyncMsgFromRust(opId: number, ui8: Uint8Array): void {
const res = decode(ui8);
util.assert(res.promiseId != null);

Expand Down Expand Up @@ -92,7 +91,6 @@ function encode(args: object): Uint8Array {
}

function unwrapResponse(res: JsonResponse): Ok {
core.print("json response" + JSON.stringify(res));
if (res.err != null) {
throw new DenoError(res.err!.kind, res.err!.message);
}
Expand Down
7 changes: 5 additions & 2 deletions js/os.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { assert } from "./util.ts";
import * as util from "./util.ts";
import { window } from "./window.ts";
import { OperatingSystem, Arch } from "./build.ts";
import { dispatch } from "./dispatch.ts";

// builtin modules
import { _setGlobals } from "./deno.ts";
Expand Down Expand Up @@ -80,9 +81,11 @@ const OP_START = new JsonOp("start");
// the runtime and the compiler environments.
// @internal
export function start(preserveDenoNamespace = true, source?: string): Start {
core.initOps();
// @ts-ignore
window.console.error("op start", OP_START, OP_START.opId);
// window.console.error("dispatch", JSON.stringify(Object.keys(dispatch)));
core.initOps(dispatch);
// @ts-ignore
// window.console.error("op start", OP_START, OP_START.opId);
// First we send an empty `Start` message to let the privileged side know we
// are ready. The response should be a `StartRes` message containing the CLI
// args and other info.
Expand Down

0 comments on commit 31c2e7c

Please sign in to comment.