Move buffers between V8 and native

* send()/recv() now operate on TypedArrays rather than ArrayBuffers.

* Remove a copy (through ArrayBuffer.slice()) from the send path.

* Remove a copy (through v8::ArrayBuffer::New()) from the return path.

* After moving a buffer from JS to native, the ArrayBuffer object and
  it's views are made inaccessible ('neutered').

* `struct deno_buf` now holds two [ptr, length] tuples, one for the actual
  memory allocation, and one for the logical data contained therein.
  This is necessary because flatbuffers fills it's buffer bottom-up, so
  the serialized blob doesn't start at beginning of the buffer, but
  somewhere in the middle.
This commit is contained in:
Bert Belder 2018-07-09 03:35:34 +02:00
parent bbcd4c8dd3
commit 24b0e91d80
No known key found for this signature in database
GPG key ID: 7A77887B2E2ED461
12 changed files with 438 additions and 70 deletions

View file

@ -23,7 +23,17 @@ TEST(MockRuntimeTest, ErrorsCorrectly) {
deno_delete(d);
}
deno_buf strbuf(const char* str) { return deno_buf{str, strlen(str)}; }
deno_buf strbuf(const char* str) {
auto len = strlen(str);
deno_buf buf;
buf.alloc_ptr = reinterpret_cast<uint8_t*>(strdup(str));
buf.alloc_len = len + 1;
buf.data_ptr = buf.alloc_ptr;
buf.data_len = len;
return buf;
}
TEST(MockRuntimeTest, SendSuccess) {
Deno* d = deno_new(nullptr, nullptr);
@ -51,10 +61,10 @@ TEST(MockRuntimeTest, RecvReturnEmpty) {
static int count = 0;
Deno* d = deno_new(nullptr, [](auto _, auto buf) {
count++;
EXPECT_EQ(static_cast<size_t>(3), buf.len);
EXPECT_EQ(buf.data[0], 'a');
EXPECT_EQ(buf.data[1], 'b');
EXPECT_EQ(buf.data[2], 'c');
EXPECT_EQ(static_cast<size_t>(3), buf.data_len);
EXPECT_EQ(buf.data_ptr[0], 'a');
EXPECT_EQ(buf.data_ptr[1], 'b');
EXPECT_EQ(buf.data_ptr[2], 'c');
});
EXPECT_TRUE(deno_execute(d, "a.js", "RecvReturnEmpty()"));
EXPECT_EQ(count, 2);
@ -65,10 +75,10 @@ TEST(MockRuntimeTest, RecvReturnBar) {
static int count = 0;
Deno* d = deno_new(nullptr, [](auto deno, auto buf) {
count++;
EXPECT_EQ(static_cast<size_t>(3), buf.len);
EXPECT_EQ(buf.data[0], 'a');
EXPECT_EQ(buf.data[1], 'b');
EXPECT_EQ(buf.data[2], 'c');
EXPECT_EQ(static_cast<size_t>(3), buf.data_len);
EXPECT_EQ(buf.data_ptr[0], 'a');
EXPECT_EQ(buf.data_ptr[1], 'b');
EXPECT_EQ(buf.data_ptr[2], 'c');
deno_set_response(deno, strbuf("bar"));
});
EXPECT_TRUE(deno_execute(d, "a.js", "RecvReturnBar()"));
@ -82,6 +92,65 @@ TEST(MockRuntimeTest, DoubleRecvFails) {
deno_delete(d);
}
TEST(MockRuntimeTest, SendRecvSlice) {
static int count = 0;
Deno* d = deno_new(nullptr, [](auto deno, auto buf) {
static const size_t alloc_len = 1024;
size_t i = count++;
// Check the size and offset of the slice.
size_t data_offset = buf.data_ptr - buf.alloc_ptr;
EXPECT_EQ(data_offset, i * 11);
EXPECT_EQ(buf.data_len, alloc_len - i * 30);
EXPECT_EQ(buf.alloc_len, alloc_len);
// Check values written by the JS side.
EXPECT_EQ(buf.data_ptr[0], 100 + i);
EXPECT_EQ(buf.data_ptr[buf.data_len - 1], 100 - i);
// Make copy of the backing buffer -- this is currently necessary because
// deno_set_response() takes ownership over the buffer, but we are not given
// ownership of `buf` by our caller.
uint8_t* alloc_ptr = reinterpret_cast<uint8_t*>(malloc(alloc_len));
memcpy(alloc_ptr, buf.alloc_ptr, alloc_len);
// Make a slice that is a bit shorter than the original.
deno_buf buf2{alloc_ptr, alloc_len, alloc_ptr + data_offset,
buf.data_len - 19};
// Place some values into the buffer for the JS side to verify.
buf2.data_ptr[0] = 200 + i;
buf2.data_ptr[buf2.data_len - 1] = 200 - i;
// Send back.
deno_set_response(deno, buf2);
});
EXPECT_TRUE(deno_execute(d, "a.js", "SendRecvSlice()"));
EXPECT_EQ(count, 5);
deno_delete(d);
}
TEST(MockRuntimeTest, JSSendArrayBufferViewTypes) {
static int count = 0;
Deno* d = deno_new(nullptr, [](auto _, auto buf) {
count++;
size_t data_offset = buf.data_ptr - buf.alloc_ptr;
EXPECT_EQ(data_offset, 2468);
EXPECT_EQ(buf.data_len, 1000);
EXPECT_EQ(buf.alloc_len, 4321);
EXPECT_EQ(buf.data_ptr[0], count);
});
EXPECT_TRUE(deno_execute(d, "a.js", "JSSendArrayBufferViewTypes()"));
EXPECT_EQ(count, 3);
deno_delete(d);
}
TEST(MockRuntimeTest, JSSendNeutersBuffer) {
static int count = 0;
Deno* d = deno_new(nullptr, [](auto _, auto buf) {
count++;
EXPECT_EQ(buf.data_len, 1);
EXPECT_EQ(buf.data_ptr[0], 42);
});
EXPECT_TRUE(deno_execute(d, "a.js", "JSSendNeutersBuffer()"));
EXPECT_EQ(count, 1);
deno_delete(d);
}
TEST(MockRuntimeTest, TypedArraySnapshots) {
Deno* d = deno_new(nullptr, nullptr);
EXPECT_TRUE(deno_execute(d, "a.js", "TypedArraySnapshots()"));
@ -98,8 +167,8 @@ TEST(MockRuntimeTest, ErrorHandling) {
static int count = 0;
Deno* d = deno_new(nullptr, [](auto deno, auto buf) {
count++;
EXPECT_EQ(static_cast<size_t>(1), buf.len);
EXPECT_EQ(buf.data[0], 42);
EXPECT_EQ(static_cast<size_t>(1), buf.data_len);
EXPECT_EQ(buf.data_ptr[0], 42);
});
EXPECT_FALSE(deno_execute(d, "a.js", "ErrorHandling()"));
EXPECT_EQ(count, 1);