Remove channel parameter from deno_send/recv.

This commit is contained in:
Ryan Dahl 2018-07-06 16:26:34 -04:00
parent 9778eceaf5
commit a2dde56c59
8 changed files with 29 additions and 43 deletions

View file

@ -107,8 +107,7 @@ typedef struct {
size_t len; size_t len;
} deno_buf; } deno_buf;
typedef void (*deno_sub_cb)(Deno* d, const char* channel, typedef void (*deno_sub_cb)(Deno* d, deno_buf bufs[], size_t nbufs)
deno_buf bufs[], size_t nbufs)
void deno_set_callback(Deno* deno, deno_sub_cb cb); void deno_set_callback(Deno* deno, deno_sub_cb cb);
// Executes javascript source code. // Executes javascript source code.
@ -139,10 +138,10 @@ There are three layers of API to consider:
### L1 ### L1
```typescript ```typescript
function send(channel: string, ...ab: ArrayBuffer[]): ArrayBuffer[] | null; function send(...ab: ArrayBuffer[]): ArrayBuffer[] | null;
``` ```
Used to make calls outside of V8. Send an ArrayBuffer and synchronously receive Used to make calls outside of V8. Send an ArrayBuffer and synchronously receive
an ArrayBuffer back. The channel parameter specifies the purpose of the message. an ArrayBuffer back.
```typescript ```typescript
function poll(): ArrayBuffer[]; function poll(): ArrayBuffer[];

4
js/deno.d.ts vendored
View file

@ -1,10 +1,10 @@
// Copyright 2018 Ryan Dahl <ry@tinyclouds.org> // Copyright 2018 Ryan Dahl <ry@tinyclouds.org>
// All rights reserved. MIT License. // All rights reserved. MIT License.
type MessageCallback = (channel: string, msg: ArrayBuffer) => void; type MessageCallback = (msg: ArrayBuffer) => void;
interface Deno { interface Deno {
recv(cb: MessageCallback): void; recv(cb: MessageCallback): void;
send(channel: string, msg: ArrayBuffer): null | ArrayBuffer; send(msg: ArrayBuffer): null | ArrayBuffer;
print(x: string): void; print(x: string): void;
} }

View file

@ -27,7 +27,7 @@ window["denoMain"] = () => {
// First we send an empty "Start" message to let the privlaged side know we // First we send an empty "Start" message to let the privlaged side know we
// are ready. The response should be a "StartRes" message containing the CLI // are ready. The response should be a "StartRes" message containing the CLI
// argv and other info. // argv and other info.
const res = deno.send("start", startMsg()); const res = deno.send(startMsg());
// TODO(ry) Remove this conditional once main.rs gets up to speed. // TODO(ry) Remove this conditional once main.rs gets up to speed.
if (res == null) { if (res == null) {

View file

@ -28,15 +28,13 @@ global.TypedArraySnapshots = () => {
}; };
global.SendSuccess = () => { global.SendSuccess = () => {
deno.recv((channel, msg) => { deno.recv(msg => {
assert(channel === "SendSuccess");
deno.print("SendSuccess: ok"); deno.print("SendSuccess: ok");
}); });
}; };
global.SendByteLength = () => { global.SendByteLength = () => {
deno.recv((channel, msg) => { deno.recv(msg => {
assert(channel === "SendByteLength");
assert(msg instanceof ArrayBuffer); assert(msg instanceof ArrayBuffer);
assert(msg.byteLength === 3); assert(msg.byteLength === 3);
}); });
@ -45,16 +43,16 @@ global.SendByteLength = () => {
global.RecvReturnEmpty = () => { global.RecvReturnEmpty = () => {
const ui8 = new Uint8Array("abc".split("").map(c => c.charCodeAt(0))); const ui8 = new Uint8Array("abc".split("").map(c => c.charCodeAt(0)));
const ab = typedArrayToArrayBuffer(ui8); const ab = typedArrayToArrayBuffer(ui8);
let r = deno.send("RecvReturnEmpty", ab); let r = deno.send(ab);
assert(r == null); assert(r == null);
r = deno.send("RecvReturnEmpty", ab); r = deno.send(ab);
assert(r == null); assert(r == null);
}; };
global.RecvReturnBar = () => { global.RecvReturnBar = () => {
const ui8 = new Uint8Array("abc".split("").map(c => c.charCodeAt(0))); const ui8 = new Uint8Array("abc".split("").map(c => c.charCodeAt(0)));
const ab = typedArrayToArrayBuffer(ui8); const ab = typedArrayToArrayBuffer(ui8);
const r = deno.send("RecvReturnBar", ab); const r = deno.send(ab);
assert(r instanceof ArrayBuffer); assert(r instanceof ArrayBuffer);
assert(r.byteLength === 3); assert(r.byteLength === 3);
const rui8 = new Uint8Array(r); const rui8 = new Uint8Array(r);
@ -84,7 +82,7 @@ global.ErrorHandling = () => {
assert(line === 3); assert(line === 3);
assert(col === 1); assert(col === 1);
assert(error instanceof Error); assert(error instanceof Error);
deno.send("ErrorHandling", typedArrayToArrayBuffer(new Uint8Array([42]))); deno.send(typedArrayToArrayBuffer(new Uint8Array([42])));
}; };
eval("\n\n notdefined()\n//# sourceURL=helloworld.js"); eval("\n\n notdefined()\n//# sourceURL=helloworld.js");
}; };

View file

@ -167,15 +167,9 @@ void Send(const v8::FunctionCallbackInfo<v8::Value>& args) {
v8::Locker locker(d->isolate); v8::Locker locker(d->isolate);
v8::EscapableHandleScope handle_scope(isolate); v8::EscapableHandleScope handle_scope(isolate);
CHECK_EQ(args.Length(), 2); CHECK_EQ(args.Length(), 1);
v8::Local<v8::Value> channel_v = args[0]; v8::Local<v8::Value> ab_v = args[0];
CHECK(channel_v->IsString());
v8::String::Utf8Value channel_vstr(isolate, channel_v);
const char* channel = *channel_vstr;
v8::Local<v8::Value> ab_v = args[1];
CHECK(ab_v->IsArrayBuffer()); CHECK(ab_v->IsArrayBuffer());
auto ab = v8::Local<v8::ArrayBuffer>::Cast(ab_v); auto ab = v8::Local<v8::ArrayBuffer>::Cast(ab_v);
auto contents = ab->GetContents(); auto contents = ab->GetContents();
@ -187,7 +181,7 @@ void Send(const v8::FunctionCallbackInfo<v8::Value>& args) {
DCHECK_EQ(d->currentArgs, nullptr); DCHECK_EQ(d->currentArgs, nullptr);
d->currentArgs = &args; d->currentArgs = &args;
d->cb(d, channel, buf); d->cb(d, buf);
d->currentArgs = nullptr; d->currentArgs = nullptr;
} }
@ -293,7 +287,7 @@ int deno_execute(Deno* d, const char* js_filename, const char* js_source) {
return deno::Execute(context, js_filename, js_source) ? 1 : 0; return deno::Execute(context, js_filename, js_source) ? 1 : 0;
} }
int deno_send(Deno* d, const char* channel, deno_buf buf) { int deno_send(Deno* d, deno_buf buf) {
v8::Locker locker(d->isolate); v8::Locker locker(d->isolate);
v8::Isolate::Scope isolate_scope(d->isolate); v8::Isolate::Scope isolate_scope(d->isolate);
v8::HandleScope handle_scope(d->isolate); v8::HandleScope handle_scope(d->isolate);
@ -313,10 +307,8 @@ int deno_send(Deno* d, const char* channel, deno_buf buf) {
auto ab = v8::ArrayBuffer::New(d->isolate, buf.len); auto ab = v8::ArrayBuffer::New(d->isolate, buf.len);
memcpy(ab->GetContents().Data(), buf.data, buf.len); memcpy(ab->GetContents().Data(), buf.data, buf.len);
v8::Local<v8::Value> args[2]; v8::Local<v8::Value> args[1];
args[0] = deno::v8_str(channel); args[0] = ab;
args[1] = ab;
recv->Call(context->Global(), 1, args); recv->Call(context->Global(), 1, args);
if (try_catch.HasCaught()) { if (try_catch.HasCaught()) {

View file

@ -20,7 +20,7 @@ typedef struct deno_s Deno;
// A callback to receive a message from deno.send javascript call. // A callback to receive a message from deno.send javascript call.
// buf is valid only for the lifetime of the call. // buf is valid only for the lifetime of the call.
typedef void (*deno_recv_cb)(Deno* d, const char* channel, deno_buf buf); typedef void (*deno_recv_cb)(Deno* d, deno_buf buf);
void deno_init(); void deno_init();
const char* deno_v8_version(); const char* deno_v8_version();
@ -37,7 +37,7 @@ int deno_execute(Deno* d, const char* js_filename, const char* js_source);
// Routes message to the javascript callback set with deno.recv(). A false // Routes message to the javascript callback set with deno.recv(). A false
// return value indicates error. Check deno_last_exception() for exception text. // return value indicates error. Check deno_last_exception() for exception text.
// 0 = fail, 1 = success // 0 = fail, 1 = success
int deno_send(Deno* d, const char* channel, deno_buf buf); int deno_send(Deno* d, deno_buf buf);
// Call this inside a deno_recv_cb to respond synchronously to messages. // Call this inside a deno_recv_cb to respond synchronously to messages.
// If this is not called during the life time of a deno_recv_cb callback // If this is not called during the life time of a deno_recv_cb callback

View file

@ -53,7 +53,7 @@ void HandleCodeFetch(Deno* d, const CodeFetch* msg) {
handle_code_fetch(module_specifier, containing_file); handle_code_fetch(module_specifier, containing_file);
} }
void MessagesFromJS(Deno* d, const char* channel, deno_buf buf) { void MessagesFromJS(Deno* d, deno_buf buf) {
auto data = reinterpret_cast<const uint8_t*>(buf.data); auto data = reinterpret_cast<const uint8_t*>(buf.data);
flatbuffers::Verifier verifier(data, buf.len); flatbuffers::Verifier verifier(data, buf.len);
DCHECK(verifier.VerifyBuffer<Base>()); DCHECK(verifier.VerifyBuffer<Base>());
@ -61,8 +61,8 @@ void MessagesFromJS(Deno* d, const char* channel, deno_buf buf) {
auto base = flatbuffers::GetRoot<Base>(buf.data); auto base = flatbuffers::GetRoot<Base>(buf.data);
auto msg_type = base->msg_type(); auto msg_type = base->msg_type();
const char* msg_type_name = EnumNamesAny()[msg_type]; const char* msg_type_name = EnumNamesAny()[msg_type];
printf("MessagesFromJS channel %s, msg_type = %d, msg_type_name = %s\n", printf("MessagesFromJS msg_type = %d, msg_type_name = %s\n", msg_type,
channel, msg_type, msg_type_name); msg_type_name);
switch (msg_type) { switch (msg_type) {
case Any_Start: case Any_Start:
HandleStart(d); HandleStart(d);

View file

@ -28,7 +28,7 @@ deno_buf strbuf(const char* str) { return deno_buf{str, strlen(str)}; }
TEST(MockRuntimeTest, SendSuccess) { TEST(MockRuntimeTest, SendSuccess) {
Deno* d = deno_new(nullptr, nullptr); Deno* d = deno_new(nullptr, nullptr);
EXPECT_TRUE(deno_execute(d, "a.js", "SendSuccess()")); EXPECT_TRUE(deno_execute(d, "a.js", "SendSuccess()"));
EXPECT_TRUE(deno_send(d, "SendSuccess", strbuf("abc"))); EXPECT_TRUE(deno_send(d, strbuf("abc")));
deno_delete(d); deno_delete(d);
} }
@ -36,22 +36,21 @@ TEST(MockRuntimeTest, SendByteLength) {
Deno* d = deno_new(nullptr, nullptr); Deno* d = deno_new(nullptr, nullptr);
EXPECT_TRUE(deno_execute(d, "a.js", "SendByteLength()")); EXPECT_TRUE(deno_execute(d, "a.js", "SendByteLength()"));
// We pub the wrong sized message, it should throw. // We pub the wrong sized message, it should throw.
EXPECT_FALSE(deno_send(d, "SendByteLength", strbuf("abcd"))); EXPECT_FALSE(deno_send(d, strbuf("abcd")));
deno_delete(d); deno_delete(d);
} }
TEST(MockRuntimeTest, SendNoCallback) { TEST(MockRuntimeTest, SendNoCallback) {
Deno* d = deno_new(nullptr, nullptr); Deno* d = deno_new(nullptr, nullptr);
// We didn't call deno.recv() in JS, should fail. // We didn't call deno.recv() in JS, should fail.
EXPECT_FALSE(deno_send(d, "SendNoCallback", strbuf("abc"))); EXPECT_FALSE(deno_send(d, strbuf("abc")));
deno_delete(d); deno_delete(d);
} }
TEST(MockRuntimeTest, RecvReturnEmpty) { TEST(MockRuntimeTest, RecvReturnEmpty) {
static int count = 0; static int count = 0;
Deno* d = deno_new(nullptr, [](auto _, auto channel, auto buf) { Deno* d = deno_new(nullptr, [](auto _, auto buf) {
count++; count++;
EXPECT_STREQ(channel, "RecvReturnEmpty");
EXPECT_EQ(static_cast<size_t>(3), buf.len); EXPECT_EQ(static_cast<size_t>(3), buf.len);
EXPECT_EQ(buf.data[0], 'a'); EXPECT_EQ(buf.data[0], 'a');
EXPECT_EQ(buf.data[1], 'b'); EXPECT_EQ(buf.data[1], 'b');
@ -64,9 +63,8 @@ TEST(MockRuntimeTest, RecvReturnEmpty) {
TEST(MockRuntimeTest, RecvReturnBar) { TEST(MockRuntimeTest, RecvReturnBar) {
static int count = 0; static int count = 0;
Deno* d = deno_new(nullptr, [](auto deno, auto channel, auto buf) { Deno* d = deno_new(nullptr, [](auto deno, auto buf) {
count++; count++;
EXPECT_STREQ(channel, "RecvReturnBar");
EXPECT_EQ(static_cast<size_t>(3), buf.len); EXPECT_EQ(static_cast<size_t>(3), buf.len);
EXPECT_EQ(buf.data[0], 'a'); EXPECT_EQ(buf.data[0], 'a');
EXPECT_EQ(buf.data[1], 'b'); EXPECT_EQ(buf.data[1], 'b');
@ -98,9 +96,8 @@ TEST(MockRuntimeTest, SnapshotBug) {
TEST(MockRuntimeTest, ErrorHandling) { TEST(MockRuntimeTest, ErrorHandling) {
static int count = 0; static int count = 0;
Deno* d = deno_new(nullptr, [](auto deno, auto channel, auto buf) { Deno* d = deno_new(nullptr, [](auto deno, auto buf) {
count++; count++;
EXPECT_STREQ(channel, "ErrorHandling");
EXPECT_EQ(static_cast<size_t>(1), buf.len); EXPECT_EQ(static_cast<size_t>(1), buf.len);
EXPECT_EQ(buf.data[0], 42); EXPECT_EQ(buf.data[0], 42);
}); });