Skip to content

Commit

Permalink
Remove channel parameter from deno_send/recv.
Browse files Browse the repository at this point in the history
  • Loading branch information
ry committed Jul 6, 2018
1 parent 9778ece commit 7d709f7
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 43 deletions.
7 changes: 3 additions & 4 deletions Roadmap.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,7 @@ typedef struct {
size_t len;
} deno_buf;

typedef void (*deno_sub_cb)(Deno* d, const char* channel,
deno_buf bufs[], size_t nbufs)
typedef void (*deno_sub_cb)(Deno* d, deno_buf bufs[], size_t nbufs)
void deno_set_callback(Deno* deno, deno_sub_cb cb);

// Executes javascript source code.
Expand Down Expand Up @@ -139,10 +138,10 @@ There are three layers of API to consider:
### L1
```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
an ArrayBuffer back. The channel parameter specifies the purpose of the message.
an ArrayBuffer back.

```typescript
function poll(): ArrayBuffer[];
Expand Down
4 changes: 2 additions & 2 deletions js/deno.d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// Copyright 2018 Ryan Dahl <[email protected]>
// All rights reserved. MIT License.
type MessageCallback = (channel: string, msg: ArrayBuffer) => void;
type MessageCallback = (msg: ArrayBuffer) => void;

interface Deno {
recv(cb: MessageCallback): void;
send(channel: string, msg: ArrayBuffer): null | ArrayBuffer;
send(msg: ArrayBuffer): null | ArrayBuffer;
print(x: string): void;
}

Expand Down
2 changes: 1 addition & 1 deletion js/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ window["denoMain"] = () => {
// 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
// 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.
if (res == null) {
Expand Down
14 changes: 6 additions & 8 deletions js/mock_runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,13 @@ global.TypedArraySnapshots = () => {
};

global.SendSuccess = () => {
deno.recv((channel, msg) => {
assert(channel === "SendSuccess");
deno.recv(msg => {
deno.print("SendSuccess: ok");
});
};

global.SendByteLength = () => {
deno.recv((channel, msg) => {
assert(channel === "SendByteLength");
deno.recv(msg => {
assert(msg instanceof ArrayBuffer);
assert(msg.byteLength === 3);
});
Expand All @@ -45,16 +43,16 @@ global.SendByteLength = () => {
global.RecvReturnEmpty = () => {
const ui8 = new Uint8Array("abc".split("").map(c => c.charCodeAt(0)));
const ab = typedArrayToArrayBuffer(ui8);
let r = deno.send("RecvReturnEmpty", ab);
let r = deno.send(ab);
assert(r == null);
r = deno.send("RecvReturnEmpty", ab);
r = deno.send(ab);
assert(r == null);
};

global.RecvReturnBar = () => {
const ui8 = new Uint8Array("abc".split("").map(c => c.charCodeAt(0)));
const ab = typedArrayToArrayBuffer(ui8);
const r = deno.send("RecvReturnBar", ab);
const r = deno.send(ab);
assert(r instanceof ArrayBuffer);
assert(r.byteLength === 3);
const rui8 = new Uint8Array(r);
Expand Down Expand Up @@ -84,7 +82,7 @@ global.ErrorHandling = () => {
assert(line === 3);
assert(col === 1);
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");
};
20 changes: 6 additions & 14 deletions src/binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -167,15 +167,9 @@ void Send(const v8::FunctionCallbackInfo<v8::Value>& args) {
v8::Locker locker(d->isolate);
v8::EscapableHandleScope handle_scope(isolate);

CHECK_EQ(args.Length(), 2);
v8::Local<v8::Value> channel_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_EQ(args.Length(), 1);
v8::Local<v8::Value> ab_v = args[0];
CHECK(ab_v->IsArrayBuffer());

auto ab = v8::Local<v8::ArrayBuffer>::Cast(ab_v);
auto contents = ab->GetContents();

Expand All @@ -187,7 +181,7 @@ void Send(const v8::FunctionCallbackInfo<v8::Value>& args) {
DCHECK_EQ(d->currentArgs, nullptr);
d->currentArgs = &args;

d->cb(d, channel, buf);
d->cb(d, buf);

d->currentArgs = nullptr;
}
Expand Down Expand Up @@ -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;
}

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::Isolate::Scope isolate_scope(d->isolate);
v8::HandleScope handle_scope(d->isolate);
Expand All @@ -313,10 +307,8 @@ int deno_send(Deno* d, const char* channel, deno_buf buf) {
auto ab = v8::ArrayBuffer::New(d->isolate, buf.len);
memcpy(ab->GetContents().Data(), buf.data, buf.len);

v8::Local<v8::Value> args[2];
args[0] = deno::v8_str(channel);
args[1] = ab;

v8::Local<v8::Value> args[1];
args[0] = ab;
recv->Call(context->Global(), 1, args);

if (try_catch.HasCaught()) {
Expand Down
4 changes: 2 additions & 2 deletions src/deno.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ typedef struct deno_s Deno;

// A callback to receive a message from deno.send javascript 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();
const char* deno_v8_version();
Expand All @@ -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
// return value indicates error. Check deno_last_exception() for exception text.
// 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.
// If this is not called during the life time of a deno_recv_cb callback
Expand Down
6 changes: 3 additions & 3 deletions src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,16 @@ void HandleCodeFetch(Deno* d, const CodeFetch* msg) {
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);
flatbuffers::Verifier verifier(data, buf.len);
DCHECK(verifier.VerifyBuffer<Base>());

auto base = flatbuffers::GetRoot<Base>(buf.data);
auto msg_type = base->msg_type();
const char* msg_type_name = EnumNamesAny()[msg_type];
printf("MessagesFromJS channel %s, msg_type = %d, msg_type_name = %s\n",
channel, msg_type, msg_type_name);
printf("MessagesFromJS msg_type = %d, msg_type_name = %s\n", msg_type,
msg_type_name);
switch (msg_type) {
case Any_Start:
HandleStart(d);
Expand Down
15 changes: 6 additions & 9 deletions src/mock_runtime_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,30 +28,29 @@ deno_buf strbuf(const char* str) { return deno_buf{str, strlen(str)}; }
TEST(MockRuntimeTest, SendSuccess) {
Deno* d = deno_new(nullptr, nullptr);
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);
}

TEST(MockRuntimeTest, SendByteLength) {
Deno* d = deno_new(nullptr, nullptr);
EXPECT_TRUE(deno_execute(d, "a.js", "SendByteLength()"));
// 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);
}

TEST(MockRuntimeTest, SendNoCallback) {
Deno* d = deno_new(nullptr, nullptr);
// 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);
}

TEST(MockRuntimeTest, RecvReturnEmpty) {
static int count = 0;
Deno* d = deno_new(nullptr, [](auto _, auto channel, auto buf) {
Deno* d = deno_new(nullptr, [](auto _, auto buf) {
count++;
EXPECT_STREQ(channel, "RecvReturnEmpty");
EXPECT_EQ(static_cast<size_t>(3), buf.len);
EXPECT_EQ(buf.data[0], 'a');
EXPECT_EQ(buf.data[1], 'b');
Expand All @@ -64,9 +63,8 @@ TEST(MockRuntimeTest, RecvReturnEmpty) {

TEST(MockRuntimeTest, RecvReturnBar) {
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++;
EXPECT_STREQ(channel, "RecvReturnBar");
EXPECT_EQ(static_cast<size_t>(3), buf.len);
EXPECT_EQ(buf.data[0], 'a');
EXPECT_EQ(buf.data[1], 'b');
Expand Down Expand Up @@ -98,9 +96,8 @@ TEST(MockRuntimeTest, SnapshotBug) {

TEST(MockRuntimeTest, ErrorHandling) {
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++;
EXPECT_STREQ(channel, "ErrorHandling");
EXPECT_EQ(static_cast<size_t>(1), buf.len);
EXPECT_EQ(buf.data[0], 42);
});
Expand Down

0 comments on commit 7d709f7

Please sign in to comment.