Skip to content

Commit

Permalink
Rewrite snapshot_creator in Rust
Browse files Browse the repository at this point in the history
  • Loading branch information
bartlomieju authored and ry committed Jul 8, 2019
1 parent 79c3439 commit d641782
Show file tree
Hide file tree
Showing 12 changed files with 98 additions and 240 deletions.
2 changes: 1 addition & 1 deletion cli/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import("//build_extra/flatbuffers/rust/rust_flatbuffer.gni")
import("//build_extra/rust/rust.gni")
import("//third_party/v8/gni/snapshot_toolchain.gni")
import("//third_party/v8/gni/v8.gni")
import("../deno.gni")
import("deno.gni")

main_extern = [
{
Expand Down
6 changes: 3 additions & 3 deletions deno.gni → cli/deno.gni
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ template("bundle") {
]
depfile = out_dir + out_name + ".d"
args = [
rebase_path("//third_party/node_modules/rollup/bin/rollup", root_build_dir),
rebase_path("//third_party/node_modules/rollup/bin/rollup",
root_build_dir),
"-c",
rebase_path("//rollup.config.js", root_build_dir),
"-i",
Expand Down Expand Up @@ -45,8 +46,7 @@ template("snapshot") {
"testonly",
"deps",
])
# TODO(ry) Rewrite snapshot_creator in Rust.
tool = "//core/libdeno:snapshot_creator"
tool = "//core:snapshot_creator"
visibility = [ ":*" ] # Only targets in this file can depend on this.
snapshot_out_bin = "$target_gen_dir/$target_name.bin"
inputs = [
Expand Down
12 changes: 12 additions & 0 deletions core/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ group("default") {
":deno_core_http_bench",
":deno_core_http_bench_test",
":deno_core_test",
":snapshot_creator",
]
}

Expand Down Expand Up @@ -77,3 +78,14 @@ rust_test("deno_core_http_bench_test") {
extern = http_bench_extern
extern_rlib = http_bench_extern_rlib
}

rust_executable("snapshot_creator") {
source_root = "snapshot_creator.rs"
extern = [
{
label = ":deno"
crate_name = "deno"
crate_type = "rlib"
},
]
}
4 changes: 4 additions & 0 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ repository = "https://github.com/denoland/deno"
[lib]
path = "lib.rs"

[[bin]]
name = "snapshot_creator"
path = "snapshot_creator.rs"

[dependencies]
futures = "0.1.27"
lazy_static = "1.3.0"
Expand Down
27 changes: 3 additions & 24 deletions core/libdeno/BUILD.gn
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import("//deno.gni")
import("//third_party/v8/gni/v8.gni")

config("deno_config") {
Expand Down Expand Up @@ -50,8 +49,6 @@ v8_source_set("libdeno") {
"deno.h",
"exceptions.cc",
"exceptions.h",
"file_util.cc",
"file_util.h",
"internal.h",
"modules.cc",
]
Expand All @@ -71,39 +68,21 @@ v8_static_library("libdeno_static_lib") {
configs = [ ":deno_config" ]
}

v8_executable("snapshot_creator") {
sources = [
"snapshot_creator.cc",
]
deps = [
":libdeno",
]
configs = [ ":deno_config" ]
}

v8_executable("libdeno_test") {
testonly = true
sources = [
"file_util_test.cc",
"libdeno_test.cc",
"modules_test.cc",
"test.cc",
]
deps = [
":libdeno",
":snapshot_test",
"//testing/gtest:gtest",
]
data = [
"$target_gen_dir/snapshot_test.bin",
"libdeno_test.js",
]
snapshot_path = rebase_path(data[0], root_build_dir)
defines = [ "SNAPSHOT_PATH=\"$snapshot_path\"" ]
js_path = rebase_path(data[0])
defines = [ "JS_PATH=\"$js_path\"" ]
configs = [ ":deno_config" ]
}

# Generates $target_gen_dir/snapshot_test.bin
snapshot("snapshot_test") {
testonly = true
source_root = "libdeno_test.js"
}
1 change: 0 additions & 1 deletion core/libdeno/api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

#include "deno.h"
#include "exceptions.h"
#include "file_util.h"
#include "internal.h"

extern "C" {
Expand Down
90 changes: 0 additions & 90 deletions core/libdeno/file_util.cc

This file was deleted.

14 changes: 0 additions & 14 deletions core/libdeno/file_util.h

This file was deleted.

46 changes: 0 additions & 46 deletions core/libdeno/file_util_test.cc

This file was deleted.

46 changes: 0 additions & 46 deletions core/libdeno/snapshot_creator.cc

This file was deleted.

46 changes: 31 additions & 15 deletions core/libdeno/test.cc
Original file line number Diff line number Diff line change
@@ -1,28 +1,44 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
#include "test.h"
#include <fstream>
#include <string>
#include "file_util.h"
#include "internal.h"

deno_snapshot snapshot = {nullptr, 0};

int main(int argc, char** argv) {
// Locate the snapshot.
std::string exe_path;
if (!deno::ExePath(&exe_path)) {
std::cerr << "deno::ExePath() failed" << std::endl;
return 1;
bool ReadFileToString(const char* fn, std::string* contents) {
std::ifstream file(fn, std::ios::binary);
if (file.fail()) {
return false;
}
std::string snapshot_path = deno::Dirname(exe_path) + SNAPSHOT_PATH;
contents->assign(std::istreambuf_iterator<char>{file}, {});
return !file.fail();
}

int main(int argc, char** argv) {
// All of the JS code in libdeno_test.js is tested after being snapshotted.
// We create that snapshot now at runtime, rather than at compile time to
// simplify the build process. So we load and execute the libdeno_test.js
// file, without running any of the tests and store the result in the global
// "snapshot" variable, which will be used later in the tests.
std::string js_fn = JS_PATH;
std::string js_source;
CHECK(ReadFileToString(js_fn.c_str(), &js_source));

deno_init();
deno_config config = {1, deno::empty_snapshot, deno::empty_buf, nullptr,
nullptr};
Deno* d = deno_new(config);

// Load the snapshot.
std::string contents;
if (!deno::ReadFileToString(snapshot_path.c_str(), &contents)) {
std::cerr << "Failed to read snapshot from " << snapshot_path << std::endl;
deno_execute(d, nullptr, js_fn.c_str(), js_source.c_str());
if (deno_last_exception(d) != nullptr) {
std::cerr << "Snapshot Exception " << std::endl;
std::cerr << deno_last_exception(d) << std::endl;
deno_delete(d);
return 1;
}
snapshot.data_ptr =
reinterpret_cast<uint8_t*>(const_cast<char*>(contents.c_str()));
snapshot.data_len = contents.size();

snapshot = deno_snapshot_new(d);

testing::InitGoogleTest(&argc, argv);
deno_init();
Expand Down
Loading

0 comments on commit d641782

Please sign in to comment.