diff options
Diffstat (limited to 'core/libdeno')
-rw-r--r-- | core/libdeno/api.cc | 10 | ||||
-rw-r--r-- | core/libdeno/deno.h | 28 | ||||
-rw-r--r-- | core/libdeno/internal.h | 1 | ||||
-rw-r--r-- | core/libdeno/libdeno_test.cc | 14 | ||||
-rw-r--r-- | core/libdeno/modules_test.cc | 8 | ||||
-rw-r--r-- | core/libdeno/snapshot_creator.cc | 8 | ||||
-rw-r--r-- | core/libdeno/test.cc | 2 | ||||
-rw-r--r-- | core/libdeno/test.h | 3 |
8 files changed, 44 insertions, 30 deletions
diff --git a/core/libdeno/api.cc b/core/libdeno/api.cc index fa1fe92f9..ad9c2a574 100644 --- a/core/libdeno/api.cc +++ b/core/libdeno/api.cc @@ -93,7 +93,7 @@ void deno_unlock(Deno* d_) { d->locker_ = nullptr; } -deno_buf deno_get_snapshot(Deno* d_) { +deno_snapshot deno_snapshot_new(Deno* d_) { auto* d = unwrap(d_); CHECK_NOT_NULL(d->snapshot_creator_); d->ClearModules(); @@ -101,8 +101,12 @@ deno_buf deno_get_snapshot(Deno* d_) { auto blob = d->snapshot_creator_->CreateBlob( v8::SnapshotCreator::FunctionCodeHandling::kKeep); - return {nullptr, 0, reinterpret_cast<uint8_t*>(const_cast<char*>(blob.data)), - blob.raw_size, 0}; + return {reinterpret_cast<uint8_t*>(const_cast<char*>(blob.data)), + blob.raw_size}; +} + +void deno_snapshot_delete(deno_snapshot snapshot) { + delete[] snapshot.data_ptr; } static std::unique_ptr<v8::Platform> platform; diff --git a/core/libdeno/deno.h b/core/libdeno/deno.h index f3902985e..8054090ed 100644 --- a/core/libdeno/deno.h +++ b/core/libdeno/deno.h @@ -18,6 +18,11 @@ typedef struct { size_t zero_copy_id; // 0 = normal, 1 = must call deno_zero_copy_release. } deno_buf; +typedef struct { + uint8_t* data_ptr; + size_t data_len; +} deno_snapshot; + typedef struct deno_s Deno; // A callback to receive a message from a libdeno.send() javascript call. @@ -31,22 +36,27 @@ const char* deno_v8_version(); void deno_set_v8_flags(int* argc, char** argv); typedef struct { - int will_snapshot; // Default 0. If calling deno_get_snapshot 1. - deno_buf load_snapshot; // Optionally: A deno_buf from deno_get_snapshot. - deno_buf shared; // Shared buffer to be mapped to libdeno.shared - deno_recv_cb recv_cb; // Maps to libdeno.send() calls. + int will_snapshot; // Default 0. If calling deno_snapshot_new 1. + deno_snapshot load_snapshot; // A startup snapshot to use. + deno_buf shared; // Shared buffer to be mapped to libdeno.shared + deno_recv_cb recv_cb; // Maps to libdeno.send() calls. } deno_config; // Create a new deno isolate. -// Warning: If config.will_snapshot is set, deno_get_snapshot() must be called +// Warning: If config.will_snapshot is set, deno_snapshot_new() must be called // or an error will result. Deno* deno_new(deno_config config); +void deno_delete(Deno* d); -// Generate a snapshot. The resulting buf can be used with deno_new. -// The caller must free the returned data by calling delete[] buf.data_ptr. -deno_buf deno_get_snapshot(Deno* d); +// Generate a snapshot. The resulting buf can be used in as the load_snapshot +// member in deno_confg. +// When calling this function, the caller must have created the isolate "d" with +// "will_snapshot" set to 1. +// The caller must free the returned data with deno_snapshot_delete(). +deno_snapshot deno_snapshot_new(Deno* d); -void deno_delete(Deno* d); +// Only for use with data returned from deno_snapshot_new. +void deno_snapshot_delete(deno_snapshot); void deno_lock(Deno* d); void deno_unlock(Deno* d); diff --git a/core/libdeno/internal.h b/core/libdeno/internal.h index 71cf731b6..ce8c63c56 100644 --- a/core/libdeno/internal.h +++ b/core/libdeno/internal.h @@ -169,6 +169,7 @@ static intptr_t external_references[] = { 0}; static const deno_buf empty_buf = {nullptr, 0, nullptr, 0, 0}; +static const deno_snapshot empty_snapshot = {nullptr, 0}; Deno* NewFromSnapshot(void* user_data, deno_recv_cb cb); diff --git a/core/libdeno/libdeno_test.cc b/core/libdeno/libdeno_test.cc index 8949e7f4a..7b936cc32 100644 --- a/core/libdeno/libdeno_test.cc +++ b/core/libdeno/libdeno_test.cc @@ -10,10 +10,10 @@ TEST(LibDenoTest, InitializesCorrectly) { } TEST(LibDenoTest, Snapshotter) { - Deno* d1 = deno_new(deno_config{1, empty, empty, nullptr}); + Deno* d1 = deno_new(deno_config{1, empty_snapshot, empty, nullptr}); deno_execute(d1, nullptr, "a.js", "a = 1 + 2"); EXPECT_EQ(nullptr, deno_last_exception(d1)); - deno_buf test_snapshot = deno_get_snapshot(d1); + deno_snapshot test_snapshot = deno_snapshot_new(d1); deno_delete(d1); Deno* d2 = deno_new(deno_config{0, test_snapshot, empty, nullptr}); @@ -21,7 +21,7 @@ TEST(LibDenoTest, Snapshotter) { EXPECT_EQ(nullptr, deno_last_exception(d2)); deno_delete(d2); - delete[] test_snapshot.data_ptr; + deno_snapshot_delete(test_snapshot); } TEST(LibDenoTest, CanCallFunction) { @@ -249,7 +249,7 @@ TEST(LibDenoTest, CheckPromiseErrors) { } TEST(LibDenoTest, LastException) { - Deno* d = deno_new(deno_config{0, empty, empty, nullptr}); + Deno* d = deno_new(deno_config{0, empty_snapshot, empty, nullptr}); EXPECT_EQ(deno_last_exception(d), nullptr); deno_execute(d, nullptr, "a.js", "\n\nthrow Error('boo');\n\n"); EXPECT_STREQ(deno_last_exception(d), @@ -264,7 +264,7 @@ TEST(LibDenoTest, LastException) { } TEST(LibDenoTest, EncodeErrorBug) { - Deno* d = deno_new(deno_config{0, empty, empty, nullptr}); + Deno* d = deno_new(deno_config{0, empty_snapshot, empty, nullptr}); EXPECT_EQ(deno_last_exception(d), nullptr); deno_execute(d, nullptr, "a.js", "eval('a')"); EXPECT_STREQ( @@ -293,7 +293,7 @@ TEST(LibDenoTest, Shared) { } TEST(LibDenoTest, Utf8Bug) { - Deno* d = deno_new(deno_config{0, empty, empty, nullptr}); + Deno* d = deno_new(deno_config{0, empty_snapshot, empty, nullptr}); // 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\""); @@ -318,7 +318,7 @@ TEST(LibDenoTest, LibDenoEvalContextError) { TEST(LibDenoTest, SharedAtomics) { int32_t s[] = {0, 1, 2}; deno_buf shared = {nullptr, 0, reinterpret_cast<uint8_t*>(s), sizeof s, 0}; - Deno* d = deno_new(deno_config{0, empty, shared, nullptr}); + Deno* d = deno_new(deno_config{0, empty_snapshot, shared, nullptr}); deno_execute(d, nullptr, "a.js", "Atomics.add(new Int32Array(Deno.core.shared), 0, 1)"); EXPECT_EQ(nullptr, deno_last_exception(d)); diff --git a/core/libdeno/modules_test.cc b/core/libdeno/modules_test.cc index 9f9228430..0eaa9e8eb 100644 --- a/core/libdeno/modules_test.cc +++ b/core/libdeno/modules_test.cc @@ -13,7 +13,7 @@ void recv_cb(void* user_data, deno_buf buf, deno_buf zero_copy_buf) { TEST(ModulesTest, Resolution) { exec_count = 0; // Reset - Deno* d = deno_new(deno_config{0, empty, empty, recv_cb}); + Deno* d = deno_new(deno_config{0, empty_snapshot, empty, recv_cb}); EXPECT_EQ(0, exec_count); static deno_mod a = deno_mod_new(d, true, "a.js", @@ -66,7 +66,7 @@ TEST(ModulesTest, Resolution) { TEST(ModulesTest, ResolutionError) { exec_count = 0; // Reset - Deno* d = deno_new(deno_config{0, empty, empty, recv_cb}); + Deno* d = deno_new(deno_config{0, empty_snapshot, empty, recv_cb}); EXPECT_EQ(0, exec_count); static deno_mod a = deno_mod_new(d, true, "a.js", @@ -99,7 +99,7 @@ TEST(ModulesTest, ResolutionError) { TEST(ModulesTest, ImportMetaUrl) { exec_count = 0; // Reset - Deno* d = deno_new(deno_config{0, empty, empty, recv_cb}); + Deno* d = deno_new(deno_config{0, empty_snapshot, empty, recv_cb}); EXPECT_EQ(0, exec_count); static deno_mod a = @@ -119,7 +119,7 @@ TEST(ModulesTest, ImportMetaUrl) { } TEST(ModulesTest, ImportMetaMain) { - Deno* d = deno_new(deno_config{0, empty, empty, recv_cb}); + Deno* d = deno_new(deno_config{0, empty_snapshot, empty, recv_cb}); const char* throw_not_main_src = "if (!import.meta.main) throw 'err'"; static deno_mod throw_not_main = diff --git a/core/libdeno/snapshot_creator.cc b/core/libdeno/snapshot_creator.cc index 19098392d..081bd1156 100644 --- a/core/libdeno/snapshot_creator.cc +++ b/core/libdeno/snapshot_creator.cc @@ -8,8 +8,6 @@ #include "third_party/v8/include/v8.h" #include "third_party/v8/src/base/logging.h" -namespace deno {} // namespace deno - int main(int argc, char** argv) { const char* snapshot_out_bin = argv[1]; const char* js_fn = argv[2]; @@ -23,7 +21,7 @@ int main(int argc, char** argv) { CHECK(deno::ReadFileToString(js_fn, &js_source)); deno_init(); - deno_config config = {1, deno::empty_buf, deno::empty_buf, nullptr}; + deno_config config = {1, deno::empty_snapshot, deno::empty_buf, nullptr}; Deno* d = deno_new(config); deno_execute(d, nullptr, js_fn, js_source.c_str()); @@ -34,13 +32,13 @@ int main(int argc, char** argv) { return 1; } - auto snapshot = deno_get_snapshot(d); + auto snapshot = deno_snapshot_new(d); std::ofstream file_(snapshot_out_bin, std::ios::binary); file_.write(reinterpret_cast<char*>(snapshot.data_ptr), snapshot.data_len); file_.close(); - delete[] snapshot.data_ptr; + deno_snapshot_delete(snapshot); deno_delete(d); return file_.bad(); diff --git a/core/libdeno/test.cc b/core/libdeno/test.cc index 1340fe8c3..3d022c904 100644 --- a/core/libdeno/test.cc +++ b/core/libdeno/test.cc @@ -3,7 +3,7 @@ #include <string> #include "file_util.h" -deno_buf snapshot = {nullptr, 0, nullptr, 0, 0}; +deno_snapshot snapshot = {nullptr, 0}; int main(int argc, char** argv) { // Locate the snapshot. diff --git a/core/libdeno/test.h b/core/libdeno/test.h index 2f7c32384..dd5dc99b2 100644 --- a/core/libdeno/test.h +++ b/core/libdeno/test.h @@ -5,7 +5,8 @@ #include "deno.h" #include "testing/gtest/include/gtest/gtest.h" -extern deno_buf snapshot; // Loaded in libdeno/test.cc +extern deno_snapshot snapshot; // Loaded in libdeno/test.cc const deno_buf empty = {nullptr, 0, nullptr, 0, 0}; +const deno_snapshot empty_snapshot = {nullptr, 0}; #endif // TEST_H_ |