diff options
Diffstat (limited to 'libdeno')
-rw-r--r-- | libdeno/deno.h | 2 | ||||
-rw-r--r-- | libdeno/internal.h | 2 | ||||
-rw-r--r-- | libdeno/libdeno_test.cc | 132 | ||||
-rw-r--r-- | libdeno/new.cc (renamed from libdeno/from_snapshot.cc) | 43 | ||||
-rw-r--r-- | libdeno/test.cc | 16 | ||||
-rw-r--r-- | libdeno/test.h | 11 |
6 files changed, 111 insertions, 95 deletions
diff --git a/libdeno/deno.h b/libdeno/deno.h index 704045524..e93b2e7f4 100644 --- a/libdeno/deno.h +++ b/libdeno/deno.h @@ -30,7 +30,7 @@ void deno_init(); const char* deno_v8_version(); void deno_set_v8_flags(int* argc, char** argv); -Deno* deno_new(deno_recv_cb cb); +Deno* deno_new(deno_buf snapshot, deno_recv_cb cb); void deno_delete(Deno* d); // Returns false on error. diff --git a/libdeno/internal.h b/libdeno/internal.h index b902c556b..be9651010 100644 --- a/libdeno/internal.h +++ b/libdeno/internal.h @@ -17,7 +17,7 @@ struct deno_s { v8::Persistent<v8::Function> global_error_handler; v8::Persistent<v8::Function> promise_reject_handler; v8::Persistent<v8::Function> promise_error_examiner; - + v8::StartupData snapshot; v8::Persistent<v8::ArrayBuffer> global_import_buf; void* global_import_buf_ptr; diff --git a/libdeno/libdeno_test.cc b/libdeno/libdeno_test.cc index f79fa70d7..447012412 100644 --- a/libdeno/libdeno_test.cc +++ b/libdeno/libdeno_test.cc @@ -1,23 +1,30 @@ // Copyright 2018 the Deno authors. All rights reserved. MIT license. -#include "testing/gtest/include/gtest/gtest.h" - -#include "deno.h" +#include "test.h" TEST(LibDenoTest, InitializesCorrectly) { - Deno* d = deno_new(nullptr); + printf("snapshot.data_ptr %p\n", snapshot.data_ptr); + EXPECT_NE(snapshot.data_ptr, nullptr); + Deno* d = deno_new(empty, nullptr); + EXPECT_TRUE(deno_execute(d, nullptr, "a.js", "1 + 2")); + deno_delete(d); +} + +TEST(LibDenoTest, InitializesCorrectlyWithoutSnapshot) { + deno_buf empty = {nullptr, 0, nullptr, 0}; + Deno* d = deno_new(empty, nullptr); EXPECT_TRUE(deno_execute(d, nullptr, "a.js", "1 + 2")); deno_delete(d); } TEST(LibDenoTest, CanCallFunction) { - Deno* d = deno_new(nullptr); + Deno* d = deno_new(snapshot, nullptr); EXPECT_TRUE(deno_execute(d, nullptr, "a.js", "if (CanCallFunction() != 'foo') throw Error();")); deno_delete(d); } TEST(LibDenoTest, ErrorsCorrectly) { - Deno* d = deno_new(nullptr); + Deno* d = deno_new(snapshot, nullptr); EXPECT_FALSE(deno_execute(d, nullptr, "a.js", "throw Error()")); deno_delete(d); } @@ -54,7 +61,7 @@ void assert_null(deno_buf b) { TEST(LibDenoTest, RecvReturnEmpty) { static int count = 0; - Deno* d = deno_new([](auto _, int req_id, auto buf, auto data_buf) { + Deno* d = deno_new(snapshot, [](auto _, int req_id, auto buf, auto data_buf) { assert_null(data_buf); count++; EXPECT_EQ(static_cast<size_t>(3), buf.data_len); @@ -69,56 +76,58 @@ TEST(LibDenoTest, RecvReturnEmpty) { TEST(LibDenoTest, RecvReturnBar) { static int count = 0; - Deno* d = deno_new([](auto user_data, int req_id, auto buf, auto data_buf) { - auto d = reinterpret_cast<Deno*>(user_data); - assert_null(data_buf); - count++; - 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_respond(d, user_data, req_id, strbuf("bar")); - }); + Deno* d = deno_new(snapshot, + [](auto user_data, int req_id, auto buf, auto data_buf) { + auto d = reinterpret_cast<Deno*>(user_data); + assert_null(data_buf); + count++; + 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_respond(d, user_data, req_id, strbuf("bar")); + }); EXPECT_TRUE(deno_execute(d, d, "a.js", "RecvReturnBar()")); EXPECT_EQ(count, 1); deno_delete(d); } TEST(LibDenoTest, DoubleRecvFails) { - Deno* d = deno_new(nullptr); + Deno* d = deno_new(snapshot, nullptr); EXPECT_FALSE(deno_execute(d, nullptr, "a.js", "DoubleRecvFails()")); deno_delete(d); } TEST(LibDenoTest, SendRecvSlice) { static int count = 0; - Deno* d = deno_new([](auto user_data, int req_id, auto buf, auto data_buf) { - auto d = reinterpret_cast<Deno*>(user_data); - 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<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_respond(d, user_data, req_id, buf2); - }); + Deno* d = deno_new( + snapshot, [](auto user_data, int req_id, auto buf, auto data_buf) { + auto d = reinterpret_cast<Deno*>(user_data); + 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<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_respond(d, user_data, req_id, buf2); + }); EXPECT_TRUE(deno_execute(d, d, "a.js", "SendRecvSlice()")); EXPECT_EQ(count, 5); deno_delete(d); @@ -126,7 +135,7 @@ TEST(LibDenoTest, SendRecvSlice) { TEST(LibDenoTest, JSSendArrayBufferViewTypes) { static int count = 0; - Deno* d = deno_new([](auto _, int req_id, auto buf, auto data_buf) { + Deno* d = deno_new(snapshot, [](auto _, int req_id, auto buf, auto data_buf) { assert_null(data_buf); count++; size_t data_offset = buf.data_ptr - buf.alloc_ptr; @@ -141,20 +150,20 @@ TEST(LibDenoTest, JSSendArrayBufferViewTypes) { } TEST(LibDenoTest, TypedArraySnapshots) { - Deno* d = deno_new(nullptr); + Deno* d = deno_new(snapshot, nullptr); EXPECT_TRUE(deno_execute(d, nullptr, "a.js", "TypedArraySnapshots()")); deno_delete(d); } TEST(LibDenoTest, SnapshotBug) { - Deno* d = deno_new(nullptr); + Deno* d = deno_new(snapshot, nullptr); EXPECT_TRUE(deno_execute(d, nullptr, "a.js", "SnapshotBug()")); deno_delete(d); } TEST(LibDenoTest, GlobalErrorHandling) { static int count = 0; - Deno* d = deno_new([](auto _, int req_id, auto buf, auto data_buf) { + Deno* d = deno_new(snapshot, [](auto _, int req_id, auto buf, auto data_buf) { assert_null(data_buf); count++; EXPECT_EQ(static_cast<size_t>(1), buf.data_len); @@ -166,7 +175,7 @@ TEST(LibDenoTest, GlobalErrorHandling) { } TEST(LibDenoTest, DoubleGlobalErrorHandlingFails) { - Deno* d = deno_new(nullptr); + Deno* d = deno_new(snapshot, nullptr); EXPECT_FALSE( deno_execute(d, nullptr, "a.js", "DoubleGlobalErrorHandlingFails()")); deno_delete(d); @@ -175,16 +184,17 @@ TEST(LibDenoTest, DoubleGlobalErrorHandlingFails) { TEST(LibDenoTest, DataBuf) { static int count = 0; static deno_buf data_buf_copy; - Deno* d = deno_new([](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); - }); + Deno* d = deno_new(snapshot, + [](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, nullptr, "a.js", "DataBuf()")); EXPECT_EQ(count, 1); // data_buf was subsequently changed in JS, let's check that our copy reflects @@ -196,7 +206,7 @@ TEST(LibDenoTest, DataBuf) { TEST(LibDenoTest, PromiseRejectCatchHandling) { static int count = 0; - Deno* d = deno_new([](auto _, int req_id, auto buf, auto data_buf) { + Deno* d = deno_new(snapshot, [](auto _, int req_id, auto buf, auto data_buf) { // If no error, nothing should be sent, and count should not increment count++; }); diff --git a/libdeno/from_snapshot.cc b/libdeno/new.cc index 1280f2450..5223f0604 100644 --- a/libdeno/from_snapshot.cc +++ b/libdeno/new.cc @@ -10,22 +10,6 @@ #include "deno.h" #include "internal.h" -extern const char deno_snapshot_start asm("deno_snapshot_start"); -extern const char deno_snapshot_end asm("deno_snapshot_end"); -#ifdef LIBDENO_TEST -asm(".data\n" - "deno_snapshot_start: .incbin \"gen/snapshot_libdeno_test.bin\"\n" - "deno_snapshot_end:\n" - ".globl deno_snapshot_start;\n" - ".globl deno_snapshot_end;"); -#else -asm(".data\n" - "deno_snapshot_start: .incbin \"gen/snapshot_deno.bin\"\n" - "deno_snapshot_end:\n" - ".globl deno_snapshot_start;\n" - ".globl deno_snapshot_end;"); -#endif // LIBDENO_TEST - namespace deno { std::vector<InternalFieldData*> deserialized_data; @@ -43,7 +27,11 @@ void DeserializeInternalFields(v8::Local<v8::Object> holder, int index, deserialized_data.push_back(embedder_field); } -Deno* NewFromSnapshot(deno_recv_cb cb) { +} // namespace deno + +extern "C" { + +Deno* deno_new(deno_buf snapshot, deno_recv_cb cb) { Deno* d = new Deno; d->currentArgs = nullptr; d->cb = cb; @@ -51,16 +39,16 @@ Deno* NewFromSnapshot(deno_recv_cb cb) { v8::Isolate::CreateParams params; params.array_buffer_allocator = v8::ArrayBuffer::Allocator::NewDefaultAllocator(); - params.external_references = external_references; + params.external_references = deno::external_references; - CHECK_NE(&deno_snapshot_start, nullptr); - int snapshot_len = - static_cast<int>(&deno_snapshot_end - &deno_snapshot_start); - static v8::StartupData snapshot = {&deno_snapshot_start, snapshot_len}; - params.snapshot_blob = &snapshot; + if (snapshot.data_ptr) { + d->snapshot.data = reinterpret_cast<const char*>(snapshot.data_ptr); + d->snapshot.raw_size = static_cast<int>(snapshot.data_len); + params.snapshot_blob = &d->snapshot; + } v8::Isolate* isolate = v8::Isolate::New(params); - AddIsolate(d, isolate); + deno::AddIsolate(d, isolate); v8::Locker locker(isolate); v8::Isolate::Scope isolate_scope(isolate); @@ -70,15 +58,10 @@ Deno* NewFromSnapshot(deno_recv_cb cb) { v8::Context::New(isolate, nullptr, v8::MaybeLocal<v8::ObjectTemplate>(), v8::MaybeLocal<v8::Value>(), v8::DeserializeInternalFieldsCallback( - DeserializeInternalFields, nullptr)); + deno::DeserializeInternalFields, nullptr)); d->context.Reset(d->isolate, context); } return d; } - -} // namespace deno - -extern "C" { -Deno* deno_new(deno_recv_cb cb) { return deno::NewFromSnapshot(cb); } } diff --git a/libdeno/test.cc b/libdeno/test.cc index e97ba3f01..c178d109a 100644 --- a/libdeno/test.cc +++ b/libdeno/test.cc @@ -1,8 +1,20 @@ // Copyright 2018 the Deno authors. All rights reserved. MIT license. -#include "deno.h" -#include "testing/gtest/include/gtest/gtest.h" +#include "test.h" +#include "file_util.h" + +deno_buf snapshot = {nullptr, 0, nullptr, 0}; int main(int argc, char** argv) { + // Load the snapshot. + std::string contents; + if (!deno::ReadFileToString(SNAPSHOT_PATH, &contents)) { + printf("Failed to read file %s\n", SNAPSHOT_PATH); + return 1; + } + snapshot.data_ptr = + reinterpret_cast<uint8_t*>(const_cast<char*>(contents.c_str())); + snapshot.data_len = contents.size(); + testing::InitGoogleTest(&argc, argv); deno_init(); deno_set_v8_flags(&argc, argv); diff --git a/libdeno/test.h b/libdeno/test.h new file mode 100644 index 000000000..83d92e6fa --- /dev/null +++ b/libdeno/test.h @@ -0,0 +1,11 @@ +// Copyright 2018 the Deno authors. All rights reserved. MIT license. +#ifndef TEST_H +#define TEST_H + +#include "deno.h" +#include "testing/gtest/include/gtest/gtest.h" + +extern deno_buf snapshot; // Loaded in libdeno/test.cc +const deno_buf empty = {nullptr, 0, nullptr, 0}; + +#endif // TEST_H |