Skip to content

Commit

Permalink
use new ops in js
Browse files Browse the repository at this point in the history
  • Loading branch information
bartlomieju committed Sep 21, 2019
1 parent c4962b0 commit 6c9b4ef
Show file tree
Hide file tree
Showing 36 changed files with 342 additions and 336 deletions.
17 changes: 7 additions & 10 deletions core/shared_queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,17 @@ SharedQueue Binary Layout
rustOpsMap = JSON.parse(opsMapJson);
const opVector = new Array(Object.keys(rustOpsMap).length);

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);

if (!op) {
continue;
}

core.print(`op ${opId} ${name} ${op}`);
op.setOpId(opId);
opVector[opId] = op.constructor.handleAsyncMsgFromRust;
}
Expand Down Expand Up @@ -203,9 +207,10 @@ SharedQueue Binary Layout
return Deno.core.send(opId, control, zeroCopy);
}


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}`);
}
Expand All @@ -232,12 +237,6 @@ SharedQueue Binary Layout
}
}

function refreshOpsMap() {
const opsMapBytes = Deno.core.send(0, []);
const opsMapJson = String.fromCharCode.apply(null, opsMapBytes);
Deno.core.opsMap = JSON.parse(opsMapJson);
}

const denoCore = {
setAsyncHandler,
dispatch,
Expand All @@ -251,9 +250,7 @@ SharedQueue Binary Layout
shift
},
initOps,
Op,
opsMap: {},
refreshOpsMap
Op
};

assert(window[GLOBAL_NAMESPACE] != null);
Expand Down
16 changes: 9 additions & 7 deletions deno_typescript/lib.deno_core.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,24 @@ interface EvalErrorInfo {
declare type OpId = number;

declare class Op {
name: string;
opId: OpId;
public name: string;
public opId: OpId;

constructor(name: string);

setOpId(opId: Opid): void;
setOpId(opId: OpId): void;

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

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

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

declare interface DenoCore {
Expand All @@ -58,7 +60,7 @@ declare interface DenoCore {
shift(): Uint8Array | null;
};

Op: Op;
Op: typeof Op;
initOps(): void;

recv(cb: MessageCallback): void;
Expand Down
9 changes: 5 additions & 4 deletions js/chmod.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import { sendSync, sendAsync } from "./dispatch_json.ts";
import * as dispatch from "./dispatch.ts";
import { JsonOp } from "./dispatch_json.ts";

const OP_CHMOD = new JsonOp("chmod");

/** Changes the permission of a specific file/directory of specified path
* synchronously.
*
* Deno.chmodSync("/path/to/file", 0o666);
*/
export function chmodSync(path: string, mode: number): void {
sendSync(dispatch.OP_CHMOD, { path, mode });
OP_CHMOD.sendSync({ path, mode });
}

/** Changes the permission of a specific file/directory of specified path.
*
* await Deno.chmod("/path/to/file", 0o666);
*/
export async function chmod(path: string, mode: number): Promise<void> {
await sendAsync(dispatch.OP_CHMOD, { path, mode });
await OP_CHMOD.sendAsync({ path, mode });
}
9 changes: 5 additions & 4 deletions js/chown.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import { sendSync, sendAsync } from "./dispatch_json.ts";
import * as dispatch from "./dispatch.ts";
import { JsonOp } from "./dispatch_json.ts";

const OP_CHOWN = new JsonOp("chown");

/**
* Change owner of a regular file or directory synchronously. Unix only at the moment.
Expand All @@ -9,7 +10,7 @@ import * as dispatch from "./dispatch.ts";
* @param gid group id of the new owner
*/
export function chownSync(path: string, uid: number, gid: number): void {
sendSync(dispatch.OP_CHOWN, { path, uid, gid });
OP_CHOWN.sendSync({ path, uid, gid });
}

/**
Expand All @@ -23,5 +24,5 @@ export async function chown(
uid: number,
gid: number
): Promise<void> {
await sendAsync(dispatch.OP_CHOWN, { path, uid, gid });
await OP_CHOWN.sendAsync({ path, uid, gid });
}
13 changes: 8 additions & 5 deletions js/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import { Console } from "./console.ts";
import { core } from "./core.ts";
import { Diagnostic, fromTypeScriptDiagnostic } from "./diagnostics.ts";
import { cwd } from "./dir.ts";
import * as dispatch from "./dispatch.ts";
import { sendSync } from "./dispatch_json.ts";
import { JsonOp } from "./dispatch_json.ts";
import * as os from "./os.ts";
import { TextEncoder } from "./text_encoding.ts";
import { getMappedModuleName, parseTypeDirectives } from "./type_directives.ts";
Expand Down Expand Up @@ -132,8 +131,12 @@ interface EmitResult {
diagnostics?: Diagnostic;
}

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

function fetchAsset(name: string): string {
return sendSync(dispatch.OP_FETCH_ASSET, { name });
return OP_FETCH_ASSET.sendSync({ name });
}

/** Ops to Rust to resolve and fetch modules meta data. */
Expand All @@ -142,7 +145,7 @@ function fetchSourceFiles(
referrer: string
): SourceFile[] {
util.log("compiler.fetchSourceFiles", { specifiers, referrer });
const res = sendSync(dispatch.OP_FETCH_SOURCE_FILES, {
const res = OP_FETCH_SOURCE_FILES.sendSync({
specifiers,
referrer
});
Expand Down Expand Up @@ -175,7 +178,7 @@ function humanFileSize(bytes: number): string {

/** Ops to rest for caching source map and compiled js */
function cache(extension: string, moduleId: string, contents: string): void {
sendSync(dispatch.OP_CACHE, { extension, moduleId, contents });
OP_CACHE.sendSync({ extension, moduleId, contents });
}

const encoder = new TextEncoder();
Expand Down
9 changes: 5 additions & 4 deletions js/copy_file.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import { sendSync, sendAsync } from "./dispatch_json.ts";
import * as dispatch from "./dispatch.ts";
import { JsonOp } from "./dispatch_json.ts";

const OP_COPY_FILE = new JsonOp("copy_file");

/** Copies the contents of a file to another by name synchronously.
* Creates a new file if target does not exists, and if target exists,
Expand All @@ -12,7 +13,7 @@ import * as dispatch from "./dispatch.ts";
* Deno.copyFileSync("from.txt", "to.txt");
*/
export function copyFileSync(from: string, to: string): void {
sendSync(dispatch.OP_COPY_FILE, { from, to });
OP_COPY_FILE.sendSync({ from, to });
}

/** Copies the contents of a file to another by name.
Expand All @@ -26,5 +27,5 @@ export function copyFileSync(from: string, to: string): void {
* await Deno.copyFile("from.txt", "to.txt");
*/
export async function copyFile(from: string, to: string): Promise<void> {
await sendAsync(dispatch.OP_COPY_FILE, { from, to });
await OP_COPY_FILE.sendAsync({ from, to });
}
10 changes: 6 additions & 4 deletions js/dir.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import { sendSync } from "./dispatch_json.ts";
import * as dispatch from "./dispatch.ts";
import { JsonOp } from "./dispatch_json.ts";

const OP_CWD = new JsonOp("cwd");

/**
* `cwd()` Return a string representing the current working directory.
Expand All @@ -10,13 +11,14 @@ import * as dispatch from "./dispatch.ts";
* throws `NotFound` exception if directory not available
*/
export function cwd(): string {
return sendSync(dispatch.OP_CWD);
return OP_CWD.sendSync();
}

const OP_CHDIR = new JsonOp("chdir");
/**
* `chdir()` Change the current working directory to path.
* throws `NotFound` exception if directory not available
*/
export function chdir(directory: string): void {
sendSync(dispatch.OP_CHDIR, { directory });
OP_CHDIR.sendSync({ directory });
}
105 changes: 0 additions & 105 deletions js/dispatch.ts

This file was deleted.

Loading

0 comments on commit 6c9b4ef

Please sign in to comment.