-
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
More flatbuffers work in TS/C++. Demonstrate linking rust lib into deno_cc. #334
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
// Copyright 2018 Ryan Dahl <[email protected]> | ||
// All rights reserved. MIT License. | ||
type MessageCallback = (msg: ArrayBuffer) => void; | ||
type MessageCallback = (channel: string, msg: ArrayBuffer) => void; | ||
|
||
interface Deno { | ||
recv(channel: string, cb: MessageCallback): void; | ||
recv(cb: MessageCallback): void; | ||
send(channel: string, msg: ArrayBuffer): null | ArrayBuffer; | ||
print(x: string): void; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,11 @@ | ||
// Copyright 2018 Ryan Dahl <[email protected]> | ||
// All rights reserved. MIT License. | ||
import { typedArrayToArrayBuffer } from "./util"; | ||
import { _global } from "./globals"; | ||
import { deno as pb } from "./msg.pb"; | ||
import { deno as fbs } from "./msg_generated"; | ||
|
||
export type MessageCallback = (msg: Uint8Array) => void; | ||
//type MessageStructCallback = (msg: pb.IMsg) => void; | ||
|
||
const send = V8Worker2.send; | ||
const channels = new Map<string, MessageCallback[]>(); | ||
|
||
export function sub(channel: string, cb: MessageCallback): void { | ||
|
@@ -19,55 +17,14 @@ export function sub(channel: string, cb: MessageCallback): void { | |
subscribers.push(cb); | ||
} | ||
|
||
/* | ||
export function subMsg(channel: string, cb: MessageStructCallback): void { | ||
sub(channel, (payload: Uint8Array) => { | ||
const msg = pb.Msg.decode(payload); | ||
if (msg.error != null) { | ||
f.onError(new Error(msg.error)); | ||
} else { | ||
cb(msg); | ||
} | ||
}); | ||
} | ||
*/ | ||
|
||
export function pub(channel: string, payload: Uint8Array): null | ArrayBuffer { | ||
const msg = pb.BaseMsg.fromObject({ channel, payload }); | ||
const ui8 = pb.BaseMsg.encode(msg).finish(); | ||
const ab = typedArrayToArrayBuffer(ui8); | ||
return send(ab); | ||
} | ||
|
||
// Internal version of "pub". | ||
// TODO add internal version of "sub" | ||
export function pubInternal(channel: string, obj: pb.IMsg): null | pb.Msg { | ||
const msg = pb.Msg.fromObject(obj); | ||
const ui8 = pb.Msg.encode(msg).finish(); | ||
const resBuf = pub(channel, ui8); | ||
if (resBuf != null && resBuf.byteLength > 0) { | ||
const res = pb.Msg.decode(new Uint8Array(resBuf)); | ||
if (res != null && res.error != null && res.error.length > 0) { | ||
throw Error(res.error); | ||
} | ||
return res; | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
V8Worker2.recv((ab: ArrayBuffer) => { | ||
const msg = pb.BaseMsg.decode(new Uint8Array(ab)); | ||
const subscribers = channels.get(msg.channel); | ||
deno.recv((channel: string, ab: ArrayBuffer) => { | ||
const subscribers = channels.get(channel); | ||
if (subscribers == null) { | ||
throw Error(`No subscribers for channel "${msg.channel}".`); | ||
throw Error(`No subscribers for channel "${channel}".`); | ||
} | ||
|
||
const ui8 = new Uint8Array(ab); | ||
for (const subscriber of subscribers) { | ||
subscriber(msg.payload); | ||
subscriber(ui8); | ||
} | ||
}); | ||
|
||
// Delete the V8Worker2 from the global object, so that no one else can receive | ||
// messages. | ||
_global["V8Worker2"] = null; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
// Copyright 2018 Ryan Dahl <[email protected]> | ||
// All rights reserved. MIT License. | ||
import * as timer from "./timers"; | ||
|
||
// If you use the eval function indirectly, by invoking it via a reference | ||
// other than eval, as of ECMAScript 5 it works in the global scope rather than | ||
|
@@ -11,22 +10,23 @@ export const globalEval = eval; | |
|
||
// A reference to the global object. | ||
// TODO The underscore is because it's conflicting with @types/node. | ||
export const _global = globalEval("this"); | ||
export const window = globalEval("this"); | ||
|
||
_global["window"] = _global; // Create a window object. | ||
import "./url"; | ||
window["window"] = window; // Create a window object. | ||
// import "./url"; | ||
|
||
_global["setTimeout"] = timer.setTimeout; | ||
_global["setInterval"] = timer.setInterval; | ||
_global["clearTimeout"] = timer.clearTimer; | ||
_global["clearInterval"] = timer.clearTimer; | ||
// import * as timer from "./timers"; | ||
// window["setTimeout"] = timer.setTimeout; | ||
// window["setInterval"] = timer.setInterval; | ||
// window["clearTimeout"] = timer.clearTimer; | ||
// window["clearInterval"] = timer.clearTimer; | ||
|
||
import { Console } from "./console"; | ||
_global["console"] = new Console(); | ||
window["console"] = new Console(); | ||
|
||
import { fetch } from "./fetch"; | ||
_global["fetch"] = fetch; | ||
// import { fetch } from "./fetch"; | ||
// window["fetch"] = fetch; | ||
|
||
import { TextEncoder, TextDecoder } from "text-encoding"; | ||
_global["TextEncoder"] = TextEncoder; | ||
_global["TextDecoder"] = TextDecoder; | ||
// import { TextEncoder, TextDecoder } from "text-encoding"; | ||
// window["TextEncoder"] = TextEncoder; | ||
// window["TextDecoder"] = TextDecoder; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
For the DCHECK below
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.
Surprising that DEBUG wasn't already set...