1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-22 15:06:54 -05:00

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.
This commit is contained in:
Ryan Dahl 2018-07-23 14:11:41 -04:00
parent b87e6d5604
commit b79ce93010
3 changed files with 49 additions and 6 deletions

View file

@ -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));
});
};

View file

@ -120,6 +120,14 @@ void Print(const v8::FunctionCallbackInfo<v8::Value>& args) {
}
static v8::Local<v8::Uint8Array> ImportBuf(v8::Isolate* isolate, deno_buf buf) {
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);
@ -127,6 +135,7 @@ static v8::Local<v8::Uint8Array> ImportBuf(v8::Isolate* isolate, deno_buf buf) {
v8::Uint8Array::New(ab, buf.data_ptr - buf.alloc_ptr, buf.data_len);
return view;
}
}
static deno_buf ExportBuf(v8::Isolate* isolate,
v8::Local<v8::ArrayBufferView> view) {

View file

@ -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);
}