mirror of
https://github.com/denoland/deno.git
synced 2025-09-27 12:49:10 +00:00
libdeno: deno_new should take a snapshot parameter.
This commit is contained in:
parent
3438dbe350
commit
fd68f85ce8
13 changed files with 201 additions and 168 deletions
|
@ -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++;
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue