1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-24 15:19:26 -05:00

Add special deno module to public api

This commit is contained in:
Ryan Dahl 2018-05-23 16:45:01 -04:00
parent b2ca48a621
commit 3171ba4c3f
7 changed files with 57 additions and 4 deletions

View file

@ -1,20 +1,25 @@
TS_FILES = \
tsconfig.json \
dispatch.ts \
globals.ts \
main.ts \
msg.pb.d.ts \
msg.pb.js \
os.ts \
runtime.ts \
test.js \
timers.ts \
tsconfig.json \
types.ts \
url.js \
util.ts \
v8_source_maps.ts
v8_source_maps.ts \
v8worker2.d.ts
GO_FILES = \
assets.go \
deno_dir.go \
deno_dir_test.go \
echo.go \
dispatch.go \
main.go \
msg.pb.go \
@ -23,11 +28,12 @@ GO_FILES = \
timers.go \
util.go
deno: $(GO_FILES)
deno: msg.pb.go $(GO_FILES)
go build -o deno
assets.go: dist/main.js
cp node_modules/typescript/lib/lib.d.ts dist/
cp deno.d.ts dist/
go-bindata -pkg main -o assets.go dist/
msg.pb.go: msg.proto

5
deno.d.ts vendored Normal file
View file

@ -0,0 +1,5 @@
declare module "deno" {
type MessageCallback = (msg: Uint8Array) => void;
function sub(channel: string, cb: MessageCallback): void;
function pub(channel: string, payload: Uint8Array): null | ArrayBuffer;
}

9
echo.go Normal file
View file

@ -0,0 +1,9 @@
package main
// For testing
func InitEcho() {
Sub("echo", func(buf []byte) []byte {
Pub("echo", buf)
return nil
})
}

View file

@ -55,6 +55,7 @@ func main() {
createWorker()
InitOS()
InitEcho()
InitTimers()
main_js := stringAsset("main.js")

View file

@ -10,11 +10,15 @@ import * as ts from "typescript";
import * as util from "./util";
import { log } from "./util";
import * as os from "./os";
import { pub, sub } from "./dispatch";
import * as sourceMaps from "./v8_source_maps";
import { _global, globalEval } from "./globals";
const EOL = "\n";
// Public deno module.
const deno = { pub, sub };
// tslint:disable-next-line:no-any
type AmdFactory = (...args: any[]) => undefined | object;
type AmdDefine = (deps: string[], factory: AmdFactory) => void;
@ -103,6 +107,8 @@ export function makeDefine(fileName: string): AmdDefine {
return localRequire;
} else if (dep === "exports") {
return localExports;
} else if (dep === "deno") {
return deno;
} else {
const resolved = resolveModuleName(dep, fileName);
const depModule = FileModule.load(resolved);
@ -262,7 +268,12 @@ class TypeScriptHost implements ts.LanguageServiceHost {
): Array<ts.ResolvedModule | undefined> {
util.log("resolveModuleNames", { moduleNames, reusedNames });
return moduleNames.map((name: string) => {
const resolvedFileName = resolveModuleName(name, containingFile);
let resolvedFileName;
if (name === "deno") {
resolvedFileName = resolveModuleName("deno.d.ts", "/$asset$/");
} else {
resolvedFileName = resolveModuleName(name, containingFile);
}
const isExternalLibraryImport = false;
return { resolvedFileName, isExternalLibraryImport };
});

18
testdata/009_pub_sub.ts vendored Normal file
View file

@ -0,0 +1,18 @@
import * as deno from "deno";
deno.sub("echo", (ui8: Uint8Array) => {
const str = String.fromCharCode.apply(null, ui8);
console.log("Got message", str);
});
function str2ui8(str: string): Uint8Array {
const ui8 = new Uint8Array(str.length);
for (let i = 0; i < str.length; i++) {
ui8[i] = str.charCodeAt(i);
}
return ui8;
}
console.log("Before deno.pub()");
deno.pub("echo", str2ui8("hello"));
console.log("After deno.pub()");

3
testdata/009_pub_sub.ts.out vendored Normal file
View file

@ -0,0 +1,3 @@
Before deno.pub()
After deno.pub()
Got message hello