summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRyan Dahl <ry@tinyclouds.org>2018-07-23 14:11:41 -0400
committerRyan Dahl <ry@tinyclouds.org>2018-07-24 12:29:54 -0400
commitb79ce93010d0cc80a9345f646e562326de4588e5 (patch)
tree3117c6b795b593bd369880ea36be18533c766a73
parentb87e6d560477354e8c1b3c69e9836cd92eaf9984 (diff)
Allow deno_buf with null alloc_ptr to be memcpy'd
This is a temporary hack to allow for easier restructuring of the serialization code as we move Flatbuffer stuff from C++ to Rust.
-rw-r--r--js/mock_runtime.js11
-rw-r--r--src/binding.cc21
-rw-r--r--src/mock_runtime_test.cc23
3 files changed, 49 insertions, 6 deletions
diff --git a/js/mock_runtime.js b/js/mock_runtime.js
index b4f353cdc..9fe59ae06 100644
--- a/js/mock_runtime.js
+++ b/js/mock_runtime.js
@@ -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));
+ });
+};
diff --git a/src/binding.cc b/src/binding.cc
index 60325df1e..7b5da2d7f 100644
--- a/src/binding.cc
+++ b/src/binding.cc
@@ -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,
diff --git a/src/mock_runtime_test.cc b/src/mock_runtime_test.cc
index a2c4b3699..fae0b32a5 100644
--- a/src/mock_runtime_test.cc
+++ b/src/mock_runtime_test.cc
@@ -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()"));
@@ -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);
+}