1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-28 16:20:57 -05:00

Use pub/sub instead of send/recv

This commit is contained in:
Ryan Dahl 2018-06-11 19:18:53 +02:00
parent 9590c87c62
commit 2443f7efee
6 changed files with 40 additions and 40 deletions

View file

@ -109,8 +109,8 @@ void Print(const v8::FunctionCallbackInfo<v8::Value>& args) {
fflush(stdout); fflush(stdout);
} }
// Sets the recv callback. // Sets the sub callback.
void Recv(const v8::FunctionCallbackInfo<v8::Value>& args) { void Sub(const v8::FunctionCallbackInfo<v8::Value>& args) {
v8::Isolate* isolate = args.GetIsolate(); v8::Isolate* isolate = args.GetIsolate();
Deno* d = reinterpret_cast<Deno*>(isolate->GetData(0)); Deno* d = reinterpret_cast<Deno*>(isolate->GetData(0));
assert(d->isolate == isolate); assert(d->isolate == isolate);
@ -121,11 +121,11 @@ void Recv(const v8::FunctionCallbackInfo<v8::Value>& args) {
assert(v->IsFunction()); assert(v->IsFunction());
v8::Local<v8::Function> func = v8::Local<v8::Function>::Cast(v); v8::Local<v8::Function> func = v8::Local<v8::Function>::Cast(v);
d->recv.Reset(isolate, func); d->sub.Reset(isolate, func);
} }
// Called from JavaScript, routes message to golang. // Called from JavaScript, routes message to golang.
void Send(const v8::FunctionCallbackInfo<v8::Value>& args) { void Pub(const v8::FunctionCallbackInfo<v8::Value>& args) {
v8::Isolate* isolate = args.GetIsolate(); v8::Isolate* isolate = args.GetIsolate();
Deno* d = static_cast<Deno*>(isolate->GetData(0)); Deno* d = static_cast<Deno*>(isolate->GetData(0));
assert(d->isolate == isolate); assert(d->isolate == isolate);
@ -213,13 +213,13 @@ v8::StartupData MakeSnapshot(v8::StartupData* prev_natives_blob,
CHECK( CHECK(
global->Set(context, deno::v8_str("deno_print"), print_val).FromJust()); global->Set(context, deno::v8_str("deno_print"), print_val).FromJust());
auto recv_tmpl = v8::FunctionTemplate::New(isolate, Recv); auto sub_tmpl = v8::FunctionTemplate::New(isolate, Sub);
auto recv_val = recv_tmpl->GetFunction(context).ToLocalChecked(); auto sub_val = sub_tmpl->GetFunction(context).ToLocalChecked();
CHECK(global->Set(context, deno::v8_str("deno_recv"), recv_val).FromJust()); CHECK(global->Set(context, deno::v8_str("deno_sub"), sub_val).FromJust());
auto send_tmpl = v8::FunctionTemplate::New(isolate, Send); auto pub_tmpl = v8::FunctionTemplate::New(isolate, Pub);
auto send_val = send_tmpl->GetFunction(context).ToLocalChecked(); auto pub_val = pub_tmpl->GetFunction(context).ToLocalChecked();
CHECK(global->Set(context, deno::v8_str("deno_send"), send_val).FromJust()); CHECK(global->Set(context, deno::v8_str("deno_pub"), pub_val).FromJust());
bool r = Load(context, js_filename, js_source); bool r = Load(context, js_filename, js_source);
assert(r); assert(r);
@ -274,10 +274,10 @@ bool deno_load(Deno* d, const char* name_s, const char* source_s) {
return deno::Load(context, name_s, source_s); return deno::Load(context, name_s, source_s);
} }
// Routes message to the javascript callback set with deno_recv(). // Routes message to the javascript callback set with deno_sub().
// False return value indicates error. Check deno_last_exception() for exception // False return value indicates error. Check deno_last_exception() for exception
// text. Caller owns buf. // text. Caller owns buf.
bool deno_send(Deno* d, deno_buf buf) { bool deno_pub(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);
@ -287,9 +287,9 @@ bool deno_send(Deno* d, deno_buf buf) {
v8::TryCatch try_catch(d->isolate); v8::TryCatch try_catch(d->isolate);
auto recv = d->recv.Get(d->isolate); auto sub = d->sub.Get(d->isolate);
if (recv.IsEmpty()) { if (sub.IsEmpty()) {
d->last_exception = "deno_recv has not been called."; d->last_exception = "deno_sub has not been called.";
return false; return false;
} }
@ -302,7 +302,7 @@ bool deno_send(Deno* d, deno_buf buf) {
assert(!args[0].IsEmpty()); assert(!args[0].IsEmpty());
assert(!try_catch.HasCaught()); assert(!try_catch.HasCaught());
recv->Call(context->Global(), 1, args); sub->Call(context->Global(), 1, args);
if (try_catch.HasCaught()) { if (try_catch.HasCaught()) {
deno::HandleException(context, try_catch.Exception()); deno::HandleException(context, try_catch.Exception());

View file

@ -12,9 +12,9 @@ extern "C" {
struct deno_s { struct deno_s {
v8::Isolate* isolate; v8::Isolate* isolate;
std::string last_exception; std::string last_exception;
v8::Persistent<v8::Function> recv; v8::Persistent<v8::Function> sub;
v8::Persistent<v8::Context> context; v8::Persistent<v8::Context> context;
deno_recv_cb cb; deno_sub_cb cb;
void* data; void* data;
}; };
} }
@ -22,13 +22,13 @@ struct deno_s {
namespace deno { namespace deno {
void Print(const v8::FunctionCallbackInfo<v8::Value>& args); void Print(const v8::FunctionCallbackInfo<v8::Value>& args);
void Recv(const v8::FunctionCallbackInfo<v8::Value>& args); void Sub(const v8::FunctionCallbackInfo<v8::Value>& args);
void Send(const v8::FunctionCallbackInfo<v8::Value>& args); void Pub(const v8::FunctionCallbackInfo<v8::Value>& args);
static intptr_t external_references[] = {reinterpret_cast<intptr_t>(Print), static intptr_t external_references[] = {reinterpret_cast<intptr_t>(Print),
reinterpret_cast<intptr_t>(Recv), reinterpret_cast<intptr_t>(Sub),
reinterpret_cast<intptr_t>(Send), 0}; reinterpret_cast<intptr_t>(Pub), 0};
Deno* NewFromSnapshot(void* data, deno_recv_cb cb); Deno* NewFromSnapshot(void* data, deno_sub_cb cb);
v8::StartupData MakeSnapshot(v8::StartupData* prev_natives_blob, v8::StartupData MakeSnapshot(v8::StartupData* prev_natives_blob,
v8::StartupData* prev_snapshot_blob, v8::StartupData* prev_snapshot_blob,

View file

@ -21,7 +21,7 @@ namespace deno {
#include "snapshot_deno.cc" #include "snapshot_deno.cc"
#endif #endif
Deno* NewFromSnapshot(void* data, deno_recv_cb cb) { Deno* NewFromSnapshot(void* data, deno_sub_cb cb) {
auto natives_blob = *StartupBlob_natives(); auto natives_blob = *StartupBlob_natives();
auto snapshot_blob = *StartupBlob_snapshot(); auto snapshot_blob = *StartupBlob_snapshot();
@ -52,7 +52,7 @@ Deno* NewFromSnapshot(void* data, deno_recv_cb cb) {
} // namespace deno } // namespace deno
extern "C" { extern "C" {
Deno* deno_new(void* data, deno_recv_cb cb) { Deno* deno_new(void* data, deno_sub_cb cb) {
return deno::NewFromSnapshot(data, cb); return deno::NewFromSnapshot(data, cb);
} }
} }

View file

@ -18,14 +18,14 @@ struct deno_s;
typedef struct deno_s Deno; typedef struct deno_s Deno;
// The callback from V8 when data is sent. // The callback from V8 when data is sent.
typedef deno_buf (*deno_recv_cb)(Deno* d, deno_buf buf); typedef deno_buf (*deno_sub_cb)(Deno* d, deno_buf buf);
void deno_init(); void deno_init();
const char* deno_v8_version(); const char* deno_v8_version();
void deno_set_flags(int* argc, char** argv); void deno_set_flags(int* argc, char** argv);
// Constructor // Constructor
Deno* deno_new(void* data, deno_recv_cb cb); Deno* deno_new(void* data, deno_sub_cb cb);
// Returns false on error. // Returns false on error.
// Get error text with deno_last_exception(). // Get error text with deno_last_exception().
@ -33,7 +33,7 @@ bool deno_load(Deno* d, const char* name_s, const char* source_s);
// Returns false on error. // Returns false on error.
// Get error text with deno_last_exception(). // Get error text with deno_last_exception().
bool deno_send(Deno* d, deno_buf buf); bool deno_pub(Deno* d, deno_buf buf);
const char* deno_last_exception(Deno* d); const char* deno_last_exception(Deno* d);

View file

@ -10,8 +10,8 @@ function assert(cond) {
if (!cond) throw Error("mock_runtime.js assert failed"); if (!cond) throw Error("mock_runtime.js assert failed");
} }
function recvabc() { function subabc() {
deno_recv((msg) => { deno_sub((msg) => {
assert(msg instanceof ArrayBuffer); assert(msg instanceof ArrayBuffer);
assert(msg.byteLength === 3); assert(msg.byteLength === 3);
}); });

View file

@ -27,25 +27,25 @@ deno_buf strbuf(const char* str) {
return deno_buf{d, strlen(str)}; return deno_buf{d, strlen(str)};
} }
TEST(MockRuntimeTest, SendSuccess) { TEST(MockRuntimeTest, PubSuccess) {
Deno* d = deno_new(NULL, NULL); Deno* d = deno_new(NULL, NULL);
EXPECT_TRUE(deno_load(d, "a.js", "recvabc();")); EXPECT_TRUE(deno_load(d, "a.js", "subabc();"));
EXPECT_TRUE(deno_send(d, strbuf("abc"))); EXPECT_TRUE(deno_pub(d, strbuf("abc")));
deno_dispose(d); deno_dispose(d);
} }
TEST(MockRuntimeTest, SendByteLength) { TEST(MockRuntimeTest, PubByteLength) {
Deno* d = deno_new(NULL, NULL); Deno* d = deno_new(NULL, NULL);
EXPECT_TRUE(deno_load(d, "a.js", "recvabc();")); EXPECT_TRUE(deno_load(d, "a.js", "subabc();"));
// We send the wrong sized message, it should throw. // We pub the wrong sized message, it should throw.
EXPECT_FALSE(deno_send(d, strbuf("abcd"))); EXPECT_FALSE(deno_pub(d, strbuf("abcd")));
deno_dispose(d); deno_dispose(d);
} }
TEST(MockRuntimeTest, SendNoCallback) { TEST(MockRuntimeTest, PubNoCallback) {
Deno* d = deno_new(NULL, NULL); Deno* d = deno_new(NULL, NULL);
// We didn't call deno_recv(), sending should fail. // We didn't call deno_sub(), pubing should fail.
EXPECT_FALSE(deno_send(d, strbuf("abc"))); EXPECT_FALSE(deno_pub(d, strbuf("abc")));
deno_dispose(d); deno_dispose(d);
} }