mirror of
https://github.com/denoland/deno.git
synced 2024-12-22 07:14:47 -05:00
Remove channel parameter from deno_send/recv.
This commit is contained in:
parent
9778eceaf5
commit
a2dde56c59
8 changed files with 29 additions and 43 deletions
|
@ -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.
|
||||
|
@ -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[];
|
||||
|
|
4
js/deno.d.ts
vendored
4
js/deno.d.ts
vendored
|
@ -1,10 +1,10 @@
|
|||
// Copyright 2018 Ryan Dahl <ry@tinyclouds.org>
|
||||
// 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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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);
|
||||
});
|
||||
|
@ -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);
|
||||
|
@ -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");
|
||||
};
|
||||
|
|
|
@ -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();
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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);
|
||||
|
@ -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()) {
|
||||
|
|
|
@ -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();
|
||||
|
@ -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
|
||||
|
|
|
@ -53,7 +53,7 @@ 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>());
|
||||
|
@ -61,8 +61,8 @@ void MessagesFromJS(Deno* d, const char* channel, deno_buf buf) {
|
|||
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);
|
||||
|
|
|
@ -28,7 +28,7 @@ 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);
|
||||
}
|
||||
|
||||
|
@ -36,22 +36,21 @@ 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');
|
||||
|
@ -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');
|
||||
|
@ -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);
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue