Skip to content

Commit

Permalink
Allow deno_buf with null alloc_ptr to be memcpy'd
Browse files Browse the repository at this point in the history
This is a temporary hack to allow for easier restructuring of
the serialization code as we move Flatbuffer stuff from C++ to Rust.
  • Loading branch information
ry committed Jul 24, 2018
1 parent b87e6d5 commit b79ce93
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 6 deletions.
11 changes: 11 additions & 0 deletions js/mock_runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,14 @@ global.ErrorHandling = () => {
};
eval("\n\n notdefined()\n//# sourceURL=helloworld.js");
};

global.SendNullAllocPtr = () => {
deno.recv(msg => {
assert(msg instanceof Uint8Array);
assert(msg.byteLength === 4);
assert(msg[0] === "a".charCodeAt(0));
assert(msg[1] === "b".charCodeAt(0));
assert(msg[2] === "c".charCodeAt(0));
assert(msg[3] === "d".charCodeAt(0));
});
};
21 changes: 15 additions & 6 deletions src/binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,21 @@ void Print(const v8::FunctionCallbackInfo<v8::Value>& args) {
}

static v8::Local<v8::Uint8Array> ImportBuf(v8::Isolate* isolate, deno_buf buf) {
auto ab = v8::ArrayBuffer::New(
isolate, reinterpret_cast<void*>(buf.alloc_ptr), buf.alloc_len,
v8::ArrayBufferCreationMode::kInternalized);
auto view =
v8::Uint8Array::New(ab, buf.data_ptr - buf.alloc_ptr, buf.data_len);
return view;
if (buf.alloc_ptr == nullptr) {
// If alloc_ptr isn't set, we memcpy.
// This is currently used for flatbuffers created in Rust.
auto ab = v8::ArrayBuffer::New(isolate, buf.data_len);
memcpy(ab->GetContents().Data(), buf.data_ptr, buf.data_len);
auto view = v8::Uint8Array::New(ab, 0, buf.data_len);
return view;
} else {
auto ab = v8::ArrayBuffer::New(
isolate, reinterpret_cast<void*>(buf.alloc_ptr), buf.alloc_len,
v8::ArrayBufferCreationMode::kInternalized);
auto view =
v8::Uint8Array::New(ab, buf.data_ptr - buf.alloc_ptr, buf.data_len);
return view;
}
}

static deno_buf ExportBuf(v8::Isolate* isolate,
Expand Down
23 changes: 23 additions & 0 deletions src/mock_runtime_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ deno_buf strbuf(const char* str) {
return buf;
}

// Same as strbuf but with null alloc_ptr.
deno_buf StrBufNullAllocPtr(const char* str) {
auto len = strlen(str);
deno_buf buf;
buf.alloc_ptr = nullptr;
buf.alloc_len = 0;
buf.data_ptr = reinterpret_cast<uint8_t*>(strdup(str));
buf.data_len = len;
return buf;
}

TEST(MockRuntimeTest, SendSuccess) {
Deno* d = deno_new(nullptr, nullptr);
EXPECT_TRUE(deno_execute(d, "a.js", "SendSuccess()"));
Expand Down Expand Up @@ -176,3 +187,15 @@ TEST(MockRuntimeTest, ErrorHandling) {
EXPECT_EQ(count, 1);
deno_delete(d);
}

TEST(MockRuntimeTest, SendNullAllocPtr) {
static int count = 0;
Deno* d = deno_new(nullptr, [](auto _, auto buf) { count++; });
EXPECT_TRUE(deno_execute(d, "a.js", "SendNullAllocPtr()"));
deno_buf buf = StrBufNullAllocPtr("abcd");
EXPECT_EQ(buf.alloc_ptr, nullptr);
EXPECT_EQ(buf.data_len, 4u);
EXPECT_TRUE(deno_send(d, buf));
EXPECT_EQ(count, 0);
deno_delete(d);
}

0 comments on commit b79ce93

Please sign in to comment.