Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use-snapshots build option for cross compile support. #1852

Merged
merged 20 commits into from
Mar 4, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions libdeno/api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <string>

#include "third_party/v8/include/libplatform/libplatform.h"
Expand All @@ -10,6 +11,7 @@

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

extern "C" {
Expand Down Expand Up @@ -106,10 +108,11 @@ deno_buf deno_get_snapshot(Deno* d_) {
static std::unique_ptr<v8::Platform> platform;

void deno_init() {
CHECK_NULL(platform.get());
platform = v8::platform::NewDefaultPlatform();
v8::V8::InitializePlatform(platform.get());
v8::V8::Initialize();
if (platform.get() == nullptr) {
afinch7 marked this conversation as resolved.
Show resolved Hide resolved
platform = v8::platform::NewDefaultPlatform();
v8::V8::InitializePlatform(platform.get());
v8::V8::Initialize();
}
}

const char* deno_v8_version() { return v8::V8::GetVersion(); }
Expand Down Expand Up @@ -225,4 +228,22 @@ void deno_terminate_execution(Deno* d_) {
deno::DenoIsolate* d = reinterpret_cast<deno::DenoIsolate*>(d_);
d->isolate_->TerminateExecution();
}

deno_buf deno_generate_snapshot(const char* js_file, const char* js_source) {
CHECK_NOT_NULL(js_file);
CHECK_NOT_NULL(js_source);
deno_init();
deno_config config = {1, deno::empty_buf, deno::empty_buf, nullptr};
Deno* d = deno_new(config);
deno_execute(d, nullptr, js_file, js_source);
if (deno_last_exception(d) != nullptr) {
std::cerr << "Snapshot Exception " << std::endl;
std::cerr << deno_last_exception(d) << std::endl;
deno_delete(d);
exit(1); // TODO(afinch7): better error reporting here
}
auto snapshot = deno_get_snapshot(d);
deno_delete(d);
afinch7 marked this conversation as resolved.
Show resolved Hide resolved
return snapshot;
}
}
2 changes: 2 additions & 0 deletions libdeno/deno.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ const char* deno_last_exception(Deno* d);

void deno_terminate_execution(Deno* d);

deno_buf deno_generate_snapshot(const char* js_file, const char* js_source);

// Module API

typedef int deno_mod;
Expand Down
15 changes: 1 addition & 14 deletions libdeno/snapshot_creator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,13 @@ int main(int argc, char** argv) {
std::string js_source;
CHECK(deno::ReadFileToString(js_fn, &js_source));

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

deno_execute(d, nullptr, js_fn, 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;
}

auto snapshot = deno_get_snapshot(d);
auto snapshot = deno_generate_snapshot(js_fn, js_source.c_str());

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_delete(d);

return file_.bad();
}
5 changes: 5 additions & 0 deletions src/libdeno.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,4 +189,9 @@ extern "C" {
user_data: *const c_void,
id: deno_mod,
);

pub fn deno_generate_snapshot(
js_file: *const c_char,
js_source: *const c_char,
) -> deno_buf;
}
55 changes: 55 additions & 0 deletions src/snapshot.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,37 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
use crate::libdeno::deno_buf;

#[cfg(target_arch = "aarch64")]
use crate::flags::v8_set_flags;
#[cfg(target_arch = "aarch64")]
use crate::libdeno::deno_generate_snapshot;
#[cfg(target_arch = "aarch64")]
use std::ffi::CString;

#[cfg(target_arch = "aarch64")]
pub fn deno_snapshot() -> deno_buf {
let js_file = "gen/bundle/main.js";
#[cfg(not(feature = "check-only"))]
let js_source =
include_bytes!(concat!(env!("GN_OUT_DIR"), "/gen/bundle/main.js"));
// The snapshot blob is not available when the Rust Language Server runs
// 'cargo check'.
#[cfg(feature = "check-only")]
let js_source = vec![];

let cjs_file = CString::new(js_file).unwrap();
let cjs_source =
CString::new(std::str::from_utf8(js_source).unwrap()).unwrap();

v8_set_flags(vec![
"gen/snapshot_deno.bin".to_string(),
js_file.to_string(),
]);

unsafe { deno_generate_snapshot(cjs_file.as_ptr(), cjs_source.as_ptr()) }
afinch7 marked this conversation as resolved.
Show resolved Hide resolved
}

#[cfg(not(target_arch = "aarch64"))]
pub fn deno_snapshot() -> deno_buf {
#[cfg(not(feature = "check-only"))]
let data =
Expand All @@ -13,6 +44,30 @@ pub fn deno_snapshot() -> deno_buf {
unsafe { deno_buf::from_raw_parts(data.as_ptr(), data.len()) }
}

#[cfg(target_arch = "aarch64")]
pub fn compiler_snapshot() -> deno_buf {
let js_file = "gen/bundle/compiler.js";
#[cfg(not(feature = "check-only"))]
let js_source =
include_bytes!(concat!(env!("GN_OUT_DIR"), "/gen/bundle/compiler.js"));
// The snapshot blob is not available when the Rust Language Server runs
// 'cargo check'.
#[cfg(feature = "check-only")]
let js_source = vec![];

let cjs_file = CString::new(js_file).unwrap();
let cjs_source =
CString::new(std::str::from_utf8(js_source).unwrap()).unwrap();

v8_set_flags(vec![
"gen/snapshot_compiler.bin".to_string(),
js_file.to_string(),
]);

unsafe { deno_generate_snapshot(cjs_file.as_ptr(), cjs_source.as_ptr()) }
}

#[cfg(not(target_arch = "aarch64"))]
pub fn compiler_snapshot() -> deno_buf {
#[cfg(not(feature = "check-only"))]
let data =
Expand Down