mirror of
https://github.com/denoland/deno.git
synced 2025-03-03 17:34:47 -05:00
Clarify memory guarantees of deno_buf
This commit is contained in:
parent
7242f2b5a5
commit
e89a49490c
3 changed files with 17 additions and 21 deletions
|
@ -124,7 +124,6 @@ void Sub(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||||
d->sub.Reset(isolate, func);
|
d->sub.Reset(isolate, func);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Called from JavaScript, routes message to golang.
|
|
||||||
void Pub(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));
|
||||||
|
@ -145,10 +144,12 @@ void Pub(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||||
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();
|
||||||
|
|
||||||
void* buf = contents.Data();
|
// data is only a valid pointer until the end of this call.
|
||||||
int buflen = static_cast<int>(contents.ByteLength());
|
const char* data =
|
||||||
|
const_cast<const char*>(reinterpret_cast<char*>(contents.Data()));
|
||||||
|
deno_buf buf{data, contents.ByteLength()};
|
||||||
|
|
||||||
auto retbuf = d->cb(d, channel, deno_buf{buf, buflen});
|
auto retbuf = d->cb(d, channel, buf);
|
||||||
if (retbuf.data) {
|
if (retbuf.data) {
|
||||||
// TODO(ry) Support zero-copy.
|
// TODO(ry) Support zero-copy.
|
||||||
auto ab = v8::ArrayBuffer::New(d->isolate, retbuf.len);
|
auto ab = v8::ArrayBuffer::New(d->isolate, retbuf.len);
|
||||||
|
@ -292,7 +293,7 @@ bool deno_pub(Deno* d, const char* channel, deno_buf buf) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO(ry) support zero copy.
|
// TODO(ry) support zero-copy.
|
||||||
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);
|
||||||
|
|
||||||
|
|
|
@ -10,14 +10,16 @@ extern "C" {
|
||||||
|
|
||||||
// Data that gets transmitted.
|
// Data that gets transmitted.
|
||||||
typedef struct {
|
typedef struct {
|
||||||
void* data;
|
const char* data;
|
||||||
size_t len;
|
size_t len;
|
||||||
} deno_buf;
|
} deno_buf;
|
||||||
|
|
||||||
struct deno_s;
|
struct deno_s;
|
||||||
typedef struct deno_s Deno;
|
typedef struct deno_s Deno;
|
||||||
|
|
||||||
// The callback from V8 when data is sent.
|
// A callback to receive a message from deno_pub javascript call.
|
||||||
|
// buf is valid only for the lifetime of the call.
|
||||||
|
// The returned deno_buf is returned from deno_pub in javascript.
|
||||||
typedef deno_buf (*deno_sub_cb)(Deno* d, const char* channel, deno_buf buf);
|
typedef deno_buf (*deno_sub_cb)(Deno* d, const char* channel, deno_buf buf);
|
||||||
|
|
||||||
void deno_init();
|
void deno_init();
|
||||||
|
|
|
@ -23,10 +23,7 @@ TEST(MockRuntimeTest, ErrorsCorrectly) {
|
||||||
deno_dispose(d);
|
deno_dispose(d);
|
||||||
}
|
}
|
||||||
|
|
||||||
deno_buf strbuf(const char* str) {
|
deno_buf strbuf(const char* str) { return deno_buf{str, strlen(str)}; }
|
||||||
void* d = reinterpret_cast<void*>(const_cast<char*>(str));
|
|
||||||
return deno_buf{d, strlen(str)};
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(MockRuntimeTest, PubSuccess) {
|
TEST(MockRuntimeTest, PubSuccess) {
|
||||||
Deno* d = deno_new(NULL, NULL);
|
Deno* d = deno_new(NULL, NULL);
|
||||||
|
@ -56,11 +53,9 @@ TEST(MockRuntimeTest, SubReturnEmpty) {
|
||||||
count++;
|
count++;
|
||||||
EXPECT_STREQ(channel, "SubReturnEmpty");
|
EXPECT_STREQ(channel, "SubReturnEmpty");
|
||||||
EXPECT_EQ(static_cast<size_t>(3), buf.len);
|
EXPECT_EQ(static_cast<size_t>(3), buf.len);
|
||||||
// TODO(ry) buf.data should just be a char*.
|
EXPECT_EQ(buf.data[0], 'a');
|
||||||
char* data = reinterpret_cast<char*>(buf.data);
|
EXPECT_EQ(buf.data[1], 'b');
|
||||||
EXPECT_EQ(data[0], 'a');
|
EXPECT_EQ(buf.data[2], 'c');
|
||||||
EXPECT_EQ(data[1], 'b');
|
|
||||||
EXPECT_EQ(data[2], 'c');
|
|
||||||
return deno_buf{nullptr, 0};
|
return deno_buf{nullptr, 0};
|
||||||
});
|
});
|
||||||
EXPECT_TRUE(deno_execute(d, "a.js", "SubReturnEmpty()"));
|
EXPECT_TRUE(deno_execute(d, "a.js", "SubReturnEmpty()"));
|
||||||
|
@ -74,11 +69,9 @@ TEST(MockRuntimeTest, SubReturnBar) {
|
||||||
count++;
|
count++;
|
||||||
EXPECT_STREQ(channel, "SubReturnBar");
|
EXPECT_STREQ(channel, "SubReturnBar");
|
||||||
EXPECT_EQ(static_cast<size_t>(3), buf.len);
|
EXPECT_EQ(static_cast<size_t>(3), buf.len);
|
||||||
// TODO(ry) buf.data should just be a char*.
|
EXPECT_EQ(buf.data[0], 'a');
|
||||||
char* data = reinterpret_cast<char*>(buf.data);
|
EXPECT_EQ(buf.data[1], 'b');
|
||||||
EXPECT_EQ(data[0], 'a');
|
EXPECT_EQ(buf.data[2], 'c');
|
||||||
EXPECT_EQ(data[1], 'b');
|
|
||||||
EXPECT_EQ(data[2], 'c');
|
|
||||||
return strbuf("bar");
|
return strbuf("bar");
|
||||||
});
|
});
|
||||||
EXPECT_TRUE(deno_execute(d, "a.js", "SubReturnBar()"));
|
EXPECT_TRUE(deno_execute(d, "a.js", "SubReturnBar()"));
|
||||||
|
|
Loading…
Add table
Reference in a new issue