2019-01-21 14:03:30 -05:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2018-10-23 19:39:31 -04:00
|
|
|
#include "test.h"
|
2018-06-11 11:01:35 -04:00
|
|
|
|
2018-08-10 15:09:28 -04:00
|
|
|
TEST(LibDenoTest, InitializesCorrectly) {
|
2018-10-23 19:39:31 -04:00
|
|
|
EXPECT_NE(snapshot.data_ptr, nullptr);
|
2019-06-06 19:07:47 -04:00
|
|
|
Deno* d = deno_new(deno_config{0, snapshot, empty, nullptr, nullptr});
|
2019-02-01 19:46:54 -05:00
|
|
|
deno_execute(d, nullptr, "a.js", "1 + 2");
|
|
|
|
EXPECT_EQ(nullptr, deno_last_exception(d));
|
2018-06-11 16:36:14 -04:00
|
|
|
deno_delete(d);
|
2018-06-11 11:01:35 -04:00
|
|
|
}
|
|
|
|
|
2018-10-24 01:12:13 -04:00
|
|
|
TEST(LibDenoTest, Snapshotter) {
|
2019-06-06 19:07:47 -04:00
|
|
|
Deno* d1 = deno_new(deno_config{1, empty_snapshot, empty, nullptr, nullptr});
|
2019-02-01 19:46:54 -05:00
|
|
|
deno_execute(d1, nullptr, "a.js", "a = 1 + 2");
|
|
|
|
EXPECT_EQ(nullptr, deno_last_exception(d1));
|
2019-04-08 10:12:43 -04:00
|
|
|
deno_snapshot test_snapshot = deno_snapshot_new(d1);
|
2018-12-03 14:22:26 -05:00
|
|
|
deno_delete(d1);
|
2018-10-24 01:12:13 -04:00
|
|
|
|
2019-06-06 19:07:47 -04:00
|
|
|
Deno* d2 = deno_new(deno_config{0, test_snapshot, empty, nullptr, nullptr});
|
2019-02-01 19:46:54 -05:00
|
|
|
deno_execute(d2, nullptr, "b.js", "if (a != 3) throw Error('x');");
|
|
|
|
EXPECT_EQ(nullptr, deno_last_exception(d2));
|
2018-10-24 01:12:13 -04:00
|
|
|
deno_delete(d2);
|
2018-12-03 14:22:26 -05:00
|
|
|
|
2019-04-08 10:12:43 -04:00
|
|
|
deno_snapshot_delete(test_snapshot);
|
2018-10-24 01:12:13 -04:00
|
|
|
}
|
|
|
|
|
2018-08-10 15:09:28 -04:00
|
|
|
TEST(LibDenoTest, CanCallFunction) {
|
2019-06-06 19:07:47 -04:00
|
|
|
Deno* d = deno_new(deno_config{0, snapshot, empty, nullptr, nullptr});
|
2019-02-26 17:36:05 -05:00
|
|
|
deno_lock(d);
|
2019-02-01 19:46:54 -05:00
|
|
|
deno_execute(d, nullptr, "a.js",
|
|
|
|
"if (CanCallFunction() != 'foo') throw Error();");
|
|
|
|
EXPECT_EQ(nullptr, deno_last_exception(d));
|
2019-02-26 17:36:05 -05:00
|
|
|
deno_unlock(d);
|
2018-06-11 16:36:14 -04:00
|
|
|
deno_delete(d);
|
2018-06-11 11:01:35 -04:00
|
|
|
}
|
|
|
|
|
2018-08-10 15:09:28 -04:00
|
|
|
TEST(LibDenoTest, ErrorsCorrectly) {
|
2019-06-06 19:07:47 -04:00
|
|
|
Deno* d = deno_new(deno_config{0, snapshot, empty, nullptr, nullptr});
|
2019-02-01 19:46:54 -05:00
|
|
|
deno_execute(d, nullptr, "a.js", "throw Error()");
|
|
|
|
EXPECT_NE(nullptr, deno_last_exception(d));
|
2018-06-11 16:36:14 -04:00
|
|
|
deno_delete(d);
|
2018-06-11 11:01:35 -04:00
|
|
|
}
|
|
|
|
|
2019-04-28 15:31:10 -04:00
|
|
|
void assert_null(deno_pinned_buf b) {
|
2018-09-27 17:33:10 -04:00
|
|
|
EXPECT_EQ(b.data_ptr, nullptr);
|
|
|
|
EXPECT_EQ(b.data_len, 0u);
|
2019-04-28 15:31:10 -04:00
|
|
|
EXPECT_EQ(b.pin, nullptr);
|
2018-06-11 12:17:28 -04:00
|
|
|
}
|
|
|
|
|
2018-08-10 15:09:28 -04:00
|
|
|
TEST(LibDenoTest, RecvReturnEmpty) {
|
2018-06-11 14:18:56 -04:00
|
|
|
static int count = 0;
|
2019-02-26 17:36:05 -05:00
|
|
|
auto recv_cb = [](auto _, auto buf, auto zero_copy_buf) {
|
|
|
|
assert_null(zero_copy_buf);
|
2018-12-04 18:06:20 -05:00
|
|
|
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');
|
|
|
|
};
|
2019-06-06 19:07:47 -04:00
|
|
|
Deno* d = deno_new(deno_config{0, snapshot, empty, recv_cb, nullptr});
|
2019-02-01 19:46:54 -05:00
|
|
|
deno_execute(d, nullptr, "a.js", "RecvReturnEmpty()");
|
|
|
|
EXPECT_EQ(nullptr, deno_last_exception(d));
|
2018-06-11 14:18:56 -04:00
|
|
|
EXPECT_EQ(count, 2);
|
2018-06-11 16:36:14 -04:00
|
|
|
deno_delete(d);
|
2018-06-11 14:18:56 -04:00
|
|
|
}
|
|
|
|
|
2018-08-10 15:09:28 -04:00
|
|
|
TEST(LibDenoTest, RecvReturnBar) {
|
2018-06-11 14:18:56 -04:00
|
|
|
static int count = 0;
|
2019-02-26 17:36:05 -05:00
|
|
|
auto recv_cb = [](auto user_data, auto buf, auto zero_copy_buf) {
|
2018-12-04 18:06:20 -05:00
|
|
|
auto d = reinterpret_cast<Deno*>(user_data);
|
2019-02-26 17:36:05 -05:00
|
|
|
assert_null(zero_copy_buf);
|
2018-12-04 18:06:20 -05:00
|
|
|
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');
|
2019-05-01 18:18:18 -04:00
|
|
|
uint8_t response[] = {'b', 'a', 'r'};
|
|
|
|
deno_respond(d, user_data, {response, sizeof response});
|
2018-12-04 18:06:20 -05:00
|
|
|
};
|
2019-06-06 19:07:47 -04:00
|
|
|
Deno* d = deno_new(deno_config{0, snapshot, empty, recv_cb, nullptr});
|
2019-02-01 19:46:54 -05:00
|
|
|
deno_execute(d, d, "a.js", "RecvReturnBar()");
|
|
|
|
EXPECT_EQ(nullptr, deno_last_exception(d));
|
2018-06-11 14:18:56 -04:00
|
|
|
EXPECT_EQ(count, 1);
|
2018-06-11 16:36:14 -04:00
|
|
|
deno_delete(d);
|
2018-06-11 14:18:56 -04:00
|
|
|
}
|
|
|
|
|
2018-08-10 15:09:28 -04:00
|
|
|
TEST(LibDenoTest, DoubleRecvFails) {
|
2019-06-06 19:07:47 -04:00
|
|
|
Deno* d = deno_new(deno_config{0, snapshot, empty, nullptr, nullptr});
|
2019-02-01 19:46:54 -05:00
|
|
|
deno_execute(d, nullptr, "a.js", "DoubleRecvFails()");
|
|
|
|
EXPECT_NE(nullptr, deno_last_exception(d));
|
2018-06-11 16:51:11 -04:00
|
|
|
deno_delete(d);
|
|
|
|
}
|
|
|
|
|
2018-08-10 15:09:28 -04:00
|
|
|
TEST(LibDenoTest, TypedArraySnapshots) {
|
2019-06-06 19:07:47 -04:00
|
|
|
Deno* d = deno_new(deno_config{0, snapshot, empty, nullptr, nullptr});
|
2019-02-01 19:46:54 -05:00
|
|
|
deno_execute(d, nullptr, "a.js", "TypedArraySnapshots()");
|
|
|
|
EXPECT_EQ(nullptr, deno_last_exception(d));
|
2018-06-12 00:36:01 -04:00
|
|
|
deno_delete(d);
|
|
|
|
}
|
|
|
|
|
2018-08-10 15:09:28 -04:00
|
|
|
TEST(LibDenoTest, SnapshotBug) {
|
2019-06-06 19:07:47 -04:00
|
|
|
Deno* d = deno_new(deno_config{0, snapshot, empty, nullptr, nullptr});
|
2019-02-01 19:46:54 -05:00
|
|
|
deno_execute(d, nullptr, "a.js", "SnapshotBug()");
|
|
|
|
EXPECT_EQ(nullptr, deno_last_exception(d));
|
2018-06-18 09:55:36 -04:00
|
|
|
deno_delete(d);
|
|
|
|
}
|
|
|
|
|
2018-08-26 03:57:16 -04:00
|
|
|
TEST(LibDenoTest, GlobalErrorHandling) {
|
2019-06-06 19:07:47 -04:00
|
|
|
Deno* d = deno_new(deno_config{0, snapshot, empty, nullptr, nullptr});
|
2019-02-01 19:46:54 -05:00
|
|
|
deno_execute(d, nullptr, "a.js", "GlobalErrorHandling()");
|
2018-12-06 23:05:36 -05:00
|
|
|
std::string expected =
|
2019-01-29 11:32:40 -05:00
|
|
|
"{\"message\":\"Uncaught ReferenceError: notdefined is not defined\","
|
2019-01-11 13:40:17 -05:00
|
|
|
"\"sourceLine\":\" "
|
|
|
|
"notdefined()\",\"scriptResourceName\":\"helloworld.js\","
|
|
|
|
"\"lineNumber\":3,\"startPosition\":3,\"endPosition\":4,\"errorLevel\":8,"
|
|
|
|
"\"startColumn\":1,\"endColumn\":2,\"isSharedCrossOrigin\":false,"
|
|
|
|
"\"isOpaque\":false,\"frames\":[{\"line\":3,\"column\":2,"
|
|
|
|
"\"functionName\":\"\",\"scriptName\":\"helloworld.js\",\"isEval\":true,"
|
2018-12-06 23:05:36 -05:00
|
|
|
"\"isConstructor\":false,\"isWasm\":false},";
|
|
|
|
std::string actual(deno_last_exception(d), 0, expected.length());
|
|
|
|
EXPECT_STREQ(expected.c_str(), actual.c_str());
|
2018-08-26 03:57:16 -04:00
|
|
|
deno_delete(d);
|
|
|
|
}
|
|
|
|
|
2019-02-26 17:36:05 -05:00
|
|
|
TEST(LibDenoTest, ZeroCopyBuf) {
|
2018-07-23 14:11:41 -04:00
|
|
|
static int count = 0;
|
2019-04-28 15:31:10 -04:00
|
|
|
static deno_pinned_buf zero_copy_buf2;
|
|
|
|
auto recv_cb = [](auto user_data, deno_buf buf,
|
|
|
|
deno_pinned_buf zero_copy_buf) {
|
2018-12-04 18:06:20 -05:00
|
|
|
count++;
|
2019-04-28 15:31:10 -04:00
|
|
|
EXPECT_NE(zero_copy_buf.pin, nullptr);
|
2019-02-26 17:36:05 -05:00
|
|
|
zero_copy_buf.data_ptr[0] = 4;
|
|
|
|
zero_copy_buf.data_ptr[1] = 2;
|
|
|
|
zero_copy_buf2 = zero_copy_buf;
|
2018-12-04 18:06:20 -05:00
|
|
|
EXPECT_EQ(2u, buf.data_len);
|
2019-02-26 17:36:05 -05:00
|
|
|
EXPECT_EQ(2u, zero_copy_buf.data_len);
|
2018-12-04 18:06:20 -05:00
|
|
|
EXPECT_EQ(buf.data_ptr[0], 1);
|
|
|
|
EXPECT_EQ(buf.data_ptr[1], 2);
|
2019-02-26 17:36:05 -05:00
|
|
|
// Note zero_copy_buf won't actually be freed here because in
|
|
|
|
// libdeno_test.js zeroCopyBuf is a rooted global. We just want to exercise
|
|
|
|
// the API here.
|
2019-04-28 15:31:10 -04:00
|
|
|
deno_pinned_buf_delete(&zero_copy_buf);
|
2018-12-04 18:06:20 -05:00
|
|
|
};
|
2019-06-06 19:07:47 -04:00
|
|
|
Deno* d = deno_new(deno_config{0, snapshot, empty, recv_cb, nullptr});
|
2019-02-26 17:36:05 -05:00
|
|
|
deno_execute(d, d, "a.js", "ZeroCopyBuf()");
|
2019-02-01 19:46:54 -05:00
|
|
|
EXPECT_EQ(nullptr, deno_last_exception(d));
|
2018-09-27 17:33:10 -04:00
|
|
|
EXPECT_EQ(count, 1);
|
2019-02-26 17:36:05 -05:00
|
|
|
// zero_copy_buf was subsequently changed in JS, let's check that our copy
|
|
|
|
// reflects that.
|
|
|
|
EXPECT_EQ(zero_copy_buf2.data_ptr[0], 9);
|
|
|
|
EXPECT_EQ(zero_copy_buf2.data_ptr[1], 8);
|
2018-07-23 14:11:41 -04:00
|
|
|
deno_delete(d);
|
|
|
|
}
|
2018-10-12 14:22:52 -04:00
|
|
|
|
2018-12-06 23:05:36 -05:00
|
|
|
TEST(LibDenoTest, CheckPromiseErrors) {
|
2018-10-12 14:22:52 -04:00
|
|
|
static int count = 0;
|
2019-02-26 17:36:05 -05:00
|
|
|
auto recv_cb = [](auto _, auto buf, auto zero_copy_buf) { count++; };
|
2019-06-06 19:07:47 -04:00
|
|
|
Deno* d = deno_new(deno_config{0, snapshot, empty, recv_cb, nullptr});
|
2018-12-06 23:05:36 -05:00
|
|
|
EXPECT_EQ(deno_last_exception(d), nullptr);
|
2019-02-01 19:46:54 -05:00
|
|
|
deno_execute(d, nullptr, "a.js", "CheckPromiseErrors()");
|
|
|
|
EXPECT_EQ(nullptr, deno_last_exception(d));
|
2018-12-06 23:05:36 -05:00
|
|
|
EXPECT_EQ(deno_last_exception(d), nullptr);
|
|
|
|
EXPECT_EQ(count, 1);
|
|
|
|
// We caught the exception. So still no errors after calling
|
|
|
|
// deno_check_promise_errors().
|
|
|
|
deno_check_promise_errors(d);
|
|
|
|
EXPECT_EQ(deno_last_exception(d), nullptr);
|
|
|
|
deno_delete(d);
|
|
|
|
}
|
2018-10-12 14:22:52 -04:00
|
|
|
|
2018-12-06 23:05:36 -05:00
|
|
|
TEST(LibDenoTest, LastException) {
|
2019-06-06 19:07:47 -04:00
|
|
|
Deno* d = deno_new(deno_config{0, empty_snapshot, empty, nullptr, nullptr});
|
2018-12-06 23:05:36 -05:00
|
|
|
EXPECT_EQ(deno_last_exception(d), nullptr);
|
2019-02-01 19:46:54 -05:00
|
|
|
deno_execute(d, nullptr, "a.js", "\n\nthrow Error('boo');\n\n");
|
2018-12-06 23:05:36 -05:00
|
|
|
EXPECT_STREQ(deno_last_exception(d),
|
2019-01-29 11:32:40 -05:00
|
|
|
"{\"message\":\"Uncaught Error: boo\",\"sourceLine\":\"throw "
|
2019-01-11 13:40:17 -05:00
|
|
|
"Error('boo');\",\"scriptResourceName\":\"a.js\",\"lineNumber\":"
|
|
|
|
"3,\"startPosition\":8,\"endPosition\":9,\"errorLevel\":8,"
|
|
|
|
"\"startColumn\":6,\"endColumn\":7,\"isSharedCrossOrigin\":"
|
|
|
|
"false,\"isOpaque\":false,\"frames\":[{\"line\":3,\"column\":7,"
|
|
|
|
"\"functionName\":\"\",\"scriptName\":\"a.js\",\"isEval\":false,"
|
2018-12-06 23:05:36 -05:00
|
|
|
"\"isConstructor\":false,\"isWasm\":false}]}");
|
2018-10-12 14:22:52 -04:00
|
|
|
deno_delete(d);
|
|
|
|
}
|
2018-10-24 02:17:10 -04:00
|
|
|
|
2019-01-09 15:56:42 -05:00
|
|
|
TEST(LibDenoTest, EncodeErrorBug) {
|
2019-06-06 19:07:47 -04:00
|
|
|
Deno* d = deno_new(deno_config{0, empty_snapshot, empty, nullptr, nullptr});
|
2019-01-09 15:56:42 -05:00
|
|
|
EXPECT_EQ(deno_last_exception(d), nullptr);
|
2019-02-01 19:46:54 -05:00
|
|
|
deno_execute(d, nullptr, "a.js", "eval('a')");
|
2019-01-11 13:40:17 -05:00
|
|
|
EXPECT_STREQ(
|
|
|
|
deno_last_exception(d),
|
2019-01-29 11:32:40 -05:00
|
|
|
"{\"message\":\"Uncaught ReferenceError: a is not "
|
2019-01-11 13:40:17 -05:00
|
|
|
"defined\",\"sourceLine\":\"a\",\"lineNumber\":1,\"startPosition\":0,"
|
|
|
|
"\"endPosition\":1,\"errorLevel\":8,\"startColumn\":0,\"endColumn\":1,"
|
|
|
|
"\"isSharedCrossOrigin\":false,\"isOpaque\":false,\"frames\":[{\"line\":"
|
|
|
|
"1,\"column\":1,\"functionName\":\"\",\"scriptName\":\"<unknown>\","
|
|
|
|
"\"isEval\":true,\"isConstructor\":false,\"isWasm\":false},{\"line\":1,"
|
|
|
|
"\"column\":1,\"functionName\":\"\",\"scriptName\":\"a.js\",\"isEval\":"
|
|
|
|
"false,\"isConstructor\":false,\"isWasm\":false}]}");
|
2019-01-09 15:56:42 -05:00
|
|
|
deno_delete(d);
|
|
|
|
}
|
|
|
|
|
2018-10-24 02:17:10 -04:00
|
|
|
TEST(LibDenoTest, Shared) {
|
|
|
|
uint8_t s[] = {0, 1, 2};
|
2019-05-01 18:18:18 -04:00
|
|
|
deno_buf shared = {s, sizeof s};
|
2019-06-06 19:07:47 -04:00
|
|
|
Deno* d = deno_new(deno_config{0, snapshot, shared, nullptr, nullptr});
|
2019-02-01 19:46:54 -05:00
|
|
|
deno_execute(d, nullptr, "a.js", "Shared()");
|
|
|
|
EXPECT_EQ(nullptr, deno_last_exception(d));
|
2018-10-24 02:17:10 -04:00
|
|
|
EXPECT_EQ(s[0], 42);
|
|
|
|
EXPECT_EQ(s[1], 43);
|
|
|
|
EXPECT_EQ(s[2], 44);
|
|
|
|
deno_delete(d);
|
|
|
|
}
|
2019-02-01 23:26:33 -05:00
|
|
|
|
|
|
|
TEST(LibDenoTest, Utf8Bug) {
|
2019-06-06 19:07:47 -04:00
|
|
|
Deno* d = deno_new(deno_config{0, empty_snapshot, empty, nullptr, nullptr});
|
2019-02-01 23:26:33 -05:00
|
|
|
// The following is a valid UTF-8 javascript which just defines a string
|
|
|
|
// literal. We had a bug where libdeno would choke on this.
|
|
|
|
deno_execute(d, nullptr, "a.js", "x = \"\xEF\xBF\xBD\"");
|
|
|
|
EXPECT_EQ(nullptr, deno_last_exception(d));
|
|
|
|
deno_delete(d);
|
|
|
|
}
|
2019-02-08 23:32:17 -05:00
|
|
|
|
2019-02-09 16:55:40 -05:00
|
|
|
TEST(LibDenoTest, LibDenoEvalContext) {
|
2019-06-06 19:07:47 -04:00
|
|
|
Deno* d = deno_new(deno_config{0, snapshot, empty, nullptr, nullptr});
|
2019-02-09 16:55:40 -05:00
|
|
|
deno_execute(d, nullptr, "a.js", "LibDenoEvalContext();");
|
|
|
|
EXPECT_EQ(nullptr, deno_last_exception(d));
|
|
|
|
deno_delete(d);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(LibDenoTest, LibDenoEvalContextError) {
|
2019-06-06 19:07:47 -04:00
|
|
|
Deno* d = deno_new(deno_config{0, snapshot, empty, nullptr, nullptr});
|
2019-02-09 16:55:40 -05:00
|
|
|
deno_execute(d, nullptr, "a.js", "LibDenoEvalContextError();");
|
|
|
|
EXPECT_EQ(nullptr, deno_last_exception(d));
|
|
|
|
deno_delete(d);
|
|
|
|
}
|
|
|
|
|
2019-02-08 23:32:17 -05:00
|
|
|
TEST(LibDenoTest, SharedAtomics) {
|
|
|
|
int32_t s[] = {0, 1, 2};
|
2019-05-01 18:18:18 -04:00
|
|
|
deno_buf shared = {reinterpret_cast<uint8_t*>(s), sizeof s};
|
2019-06-06 19:07:47 -04:00
|
|
|
Deno* d = deno_new(deno_config{0, empty_snapshot, shared, nullptr, nullptr});
|
2019-02-08 23:32:17 -05:00
|
|
|
deno_execute(d, nullptr, "a.js",
|
2019-03-26 08:22:07 -04:00
|
|
|
"Atomics.add(new Int32Array(Deno.core.shared), 0, 1)");
|
2019-02-08 23:32:17 -05:00
|
|
|
EXPECT_EQ(nullptr, deno_last_exception(d));
|
|
|
|
EXPECT_EQ(s[0], 1);
|
|
|
|
EXPECT_EQ(s[1], 1);
|
|
|
|
EXPECT_EQ(s[2], 2);
|
|
|
|
deno_delete(d);
|
|
|
|
}
|