From d38ccfc6dcb8643daa4f9e695d47a79cf068f90e Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Thu, 27 Sep 2018 17:33:10 -0400 Subject: Support zero-copy data in libdeno.send(). (#838) This is a large API refactor of deno.h which replaces deno_send() and deno_set_response() with deno_respond(). It also adds a req_id parameter to the deno_recv_cb. Make writeFile/writeFileSync use it. --- libdeno/libdeno_test.cc | 134 ++++++++++++++++++++++++------------------------ 1 file changed, 67 insertions(+), 67 deletions(-) (limited to 'libdeno/libdeno_test.cc') diff --git a/libdeno/libdeno_test.cc b/libdeno/libdeno_test.cc index c8b0adfd0..5177ab4a8 100644 --- a/libdeno/libdeno_test.cc +++ b/libdeno/libdeno_test.cc @@ -45,34 +45,17 @@ deno_buf StrBufNullAllocPtr(const char* str) { return buf; } -TEST(LibDenoTest, SendSuccess) { - Deno* d = deno_new(nullptr, nullptr); - EXPECT_TRUE(deno_execute(d, "a.js", "SendSuccess()")); - EXPECT_TRUE(deno_send(d, strbuf("abc"))); - deno_delete(d); -} - -TEST(LibDenoTest, SendWrongByteLength) { - Deno* d = deno_new(nullptr, nullptr); - EXPECT_TRUE(deno_execute(d, "a.js", "SendWrongByteLength()")); - // deno_send the wrong sized message, it should throw. - EXPECT_FALSE(deno_send(d, strbuf("abcd"))); - std::string exception = deno_last_exception(d); - EXPECT_GT(exception.length(), 1u); - EXPECT_NE(exception.find("assert"), std::string::npos); - deno_delete(d); -} - -TEST(LibDenoTest, SendNoCallback) { - Deno* d = deno_new(nullptr, nullptr); - // We didn't call deno.recv() in JS, should fail. - EXPECT_FALSE(deno_send(d, strbuf("abc"))); - deno_delete(d); +void assert_null(deno_buf b) { + EXPECT_EQ(b.alloc_ptr, nullptr); + EXPECT_EQ(b.alloc_len, 0u); + EXPECT_EQ(b.data_ptr, nullptr); + EXPECT_EQ(b.data_len, 0u); } TEST(LibDenoTest, RecvReturnEmpty) { static int count = 0; - Deno* d = deno_new(nullptr, [](auto _, auto buf) { + Deno* d = deno_new(nullptr, [](auto _, int req_id, auto buf, auto data_buf) { + assert_null(data_buf); count++; EXPECT_EQ(static_cast(3), buf.data_len); EXPECT_EQ(buf.data_ptr[0], 'a'); @@ -86,14 +69,16 @@ TEST(LibDenoTest, RecvReturnEmpty) { TEST(LibDenoTest, RecvReturnBar) { static int count = 0; - Deno* d = deno_new(nullptr, [](auto deno, auto buf) { - count++; - EXPECT_EQ(static_cast(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")); - }); + Deno* d = + deno_new(nullptr, [](auto deno, int req_id, auto buf, auto data_buf) { + assert_null(data_buf); + count++; + EXPECT_EQ(static_cast(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_respond(deno, req_id, strbuf("bar")); + }); EXPECT_TRUE(deno_execute(d, "a.js", "RecvReturnBar()")); EXPECT_EQ(count, 1); deno_delete(d); @@ -107,31 +92,33 @@ TEST(LibDenoTest, DoubleRecvFails) { TEST(LibDenoTest, 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(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); - }); + Deno* d = + deno_new(nullptr, [](auto deno, int req_id, auto buf, auto data_buf) { + assert_null(data_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_respond() takes ownership over the buffer, but we are + // not given ownership of `buf` by our caller. + uint8_t* alloc_ptr = reinterpret_cast(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_respond(deno, req_id, buf2); + }); EXPECT_TRUE(deno_execute(d, "a.js", "SendRecvSlice()")); EXPECT_EQ(count, 5); deno_delete(d); @@ -139,7 +126,8 @@ TEST(LibDenoTest, SendRecvSlice) { TEST(LibDenoTest, JSSendArrayBufferViewTypes) { static int count = 0; - Deno* d = deno_new(nullptr, [](auto _, auto buf) { + Deno* d = deno_new(nullptr, [](auto _, int req_id, auto buf, auto data_buf) { + assert_null(data_buf); count++; size_t data_offset = buf.data_ptr - buf.alloc_ptr; EXPECT_EQ(data_offset, 2468u); @@ -166,7 +154,8 @@ TEST(LibDenoTest, SnapshotBug) { TEST(LibDenoTest, GlobalErrorHandling) { static int count = 0; - Deno* d = deno_new(nullptr, [](auto _, auto buf) { + Deno* d = deno_new(nullptr, [](auto _, int req_id, auto buf, auto data_buf) { + assert_null(data_buf); count++; EXPECT_EQ(static_cast(1), buf.data_len); EXPECT_EQ(buf.data_ptr[0], 42); @@ -182,14 +171,25 @@ TEST(LibDenoTest, DoubleGlobalErrorHandlingFails) { deno_delete(d); } -TEST(LibDenoTest, SendNullAllocPtr) { +TEST(LibDenoTest, DataBuf) { 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); + static deno_buf data_buf_copy; + Deno* d = deno_new(nullptr, + [](auto _, int req_id, deno_buf buf, deno_buf data_buf) { + count++; + data_buf.data_ptr[0] = 4; + data_buf.data_ptr[1] = 2; + data_buf_copy = data_buf; + EXPECT_EQ(2u, buf.data_len); + EXPECT_EQ(2u, data_buf.data_len); + EXPECT_EQ(buf.data_ptr[0], 1); + EXPECT_EQ(buf.data_ptr[1], 2); + }); + EXPECT_TRUE(deno_execute(d, "a.js", "DataBuf()")); + EXPECT_EQ(count, 1); + // data_buf was subsequently changed in JS, let's check that our copy reflects + // that. + EXPECT_EQ(data_buf_copy.data_ptr[0], 9); + EXPECT_EQ(data_buf_copy.data_ptr[1], 8); deno_delete(d); } -- cgit v1.2.3