From 92e404706b0b1a26cdaf6f8cf81aac148292557f Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Mon, 12 Nov 2018 17:17:30 -0800 Subject: [PATCH] Use include_bytes! instead of incbin. (#1182) --- BUILD.gn | 28 ---------------------------- src/isolate.rs | 2 +- src/snapshot.cc | 18 ------------------ src/snapshot.rs | 18 ++++++++++++++++-- 4 files changed, 17 insertions(+), 49 deletions(-) delete mode 100644 src/snapshot.cc diff --git a/BUILD.gn b/BUILD.gn index ce15a762d3..198f681143 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -143,40 +143,12 @@ rust_executable("deno") { } source_set("snapshot") { - sources = [ - "src/snapshot.cc", - ] - configs += [ ":deno_config" ] inputs = [ "$target_gen_dir/snapshot_deno.bin", ] deps = [ ":create_snapshot_deno", ] - - # snapshot.cc doesn't need to depend on libdeno, it just needs deno_buf. - include_dirs = [ "libdeno/" ] - - # src/snapshot.cc uses an assembly '.incbin' directive to embed the snapshot. - # This causes trouble when using sccache: since the snapshot file is not - # inlined by the c preprocessor, sccache doesn't take its contents into - # consideration, leading to false-positive cache hits. - # Maybe other caching tools have this issue too, but ccache is unaffected. - # Therefore, if a cc_wrapper is used that isn't ccache, include a generated - # header file that contains the the sha256 hash of the snapshot. - if (cc_wrapper != "" && cc_wrapper != "ccache") { - hash_h = "$target_gen_dir/bundle/hash.h" - inputs += [ hash_h ] - deps += [ ":bundle_hash_h" ] - if (is_win) { - cflags = [ "/FI" + rebase_path(hash_h, target_out_dir) ] - } else { - cflags = [ - "-include", - rebase_path(hash_h, target_out_dir), - ] - } - } } rust_executable("hyper_hello") { diff --git a/src/isolate.rs b/src/isolate.rs index 15a76ba3cb..6e8b1864d1 100644 --- a/src/isolate.rs +++ b/src/isolate.rs @@ -139,7 +139,7 @@ impl Isolate { }); let shared = empty(); // TODO Use shared for message passing. let libdeno_isolate = unsafe { - libdeno::deno_new(snapshot::deno_snapshot.clone(), shared, pre_dispatch) + libdeno::deno_new(snapshot::deno_snapshot(), shared, pre_dispatch) }; // This channel handles sending async messages back to the runtime. let (tx, rx) = mpsc::channel::<(i32, Buf)>(); diff --git a/src/snapshot.cc b/src/snapshot.cc deleted file mode 100644 index b0945d319a..0000000000 --- a/src/snapshot.cc +++ /dev/null @@ -1,18 +0,0 @@ -// Copyright 2018 the Deno authors. All rights reserved. MIT license. - -#include "deno.h" - -extern "C" { - -extern const char snapshot_start asm("snapshot_start"); -extern const char snapshot_end asm("snapshot_end"); -asm(".data\n" - "snapshot_start: .incbin \"gen/snapshot_deno.bin\"\n" - "snapshot_end:\n" - ".globl snapshot_start;\n" - ".globl snapshot_end;"); -extern const deno_buf deno_snapshot = { - nullptr, 0, reinterpret_cast(const_cast(&snapshot_start)), - static_cast(&snapshot_end - &snapshot_start)}; - -} diff --git a/src/snapshot.rs b/src/snapshot.rs index 52c8df47de..fcb41f6c82 100644 --- a/src/snapshot.rs +++ b/src/snapshot.rs @@ -1,5 +1,19 @@ // Copyright 2018 the Deno authors. All rights reserved. MIT license. use libdeno::deno_buf; -extern "C" { - pub static deno_snapshot: deno_buf; +use std; + +pub fn deno_snapshot() -> deno_buf { + let data = + include_bytes!(concat!(env!("GN_OUT_DIR"), "/gen/snapshot_deno.bin")); + let ptr = data.as_ptr(); + // TODO The transmute is not necessary here. deno_buf specifies mutable + // pointers when it doesn't necessarally need mutable. So maybe the deno_buf + // type should be broken into a mutable and non-mutable version? + let ptr_mut = unsafe { std::mem::transmute::<*const u8, *mut u8>(ptr) }; + deno_buf { + alloc_ptr: std::ptr::null_mut(), + alloc_len: 0, + data_ptr: ptr_mut, + data_len: data.len(), + } }