-
Notifications
You must be signed in to change notification settings - Fork 359
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Compile JS to bytecode and compress with zstd using the embedded dictionary. There is no compatibility guarantee of lrt files, they can be executed by the llrt version that created them.
- Loading branch information
Showing
5 changed files
with
137 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Shared with build.rs and compiler.rs | ||
|
||
use rquickjs::{ | ||
loader::{Loader, Resolver}, | ||
module::ModuleData, | ||
Ctx, | ||
}; | ||
|
||
struct DummyLoader; | ||
|
||
impl Loader for DummyLoader { | ||
fn load(&mut self, _ctx: &Ctx<'_>, name: &str) -> rquickjs::Result<ModuleData> { | ||
Ok(ModuleData::source(name, "")) | ||
} | ||
} | ||
|
||
struct DummyResolver; | ||
|
||
impl Resolver for DummyResolver { | ||
fn resolve(&mut self, _ctx: &Ctx<'_>, _base: &str, name: &str) -> rquickjs::Result<String> { | ||
Ok(name.into()) | ||
} | ||
} | ||
|
||
fn human_file_size(size: usize) -> String { | ||
let fsize = size as f64; | ||
let i = if size == 0 { | ||
0 | ||
} else { | ||
(fsize.log2() / 1024f64.log2()).floor() as i32 | ||
}; | ||
let size = fsize / 1024f64.powi(i); | ||
let units = ["B", "kB", "MB", "GB", "TB", "PB"]; | ||
format!("{:.3} {}", size, units[i as usize]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
use std::{fs, io, path::Path}; | ||
|
||
use rquickjs::{Context, Module, Runtime}; | ||
use tracing::trace; | ||
use zstd::bulk::Compressor; | ||
|
||
use crate::vm::COMPRESSION_DICT; | ||
|
||
include!("compiler-common.rs"); | ||
|
||
fn compress_module(bytes: &[u8]) -> io::Result<Vec<u8>> { | ||
let mut compressor = Compressor::with_dictionary(22, COMPRESSION_DICT)?; | ||
let raw: Vec<u8> = compressor.compress(bytes)?; | ||
let uncompressed_len = bytes.len() as u32; | ||
|
||
let mut compressed = Vec::with_capacity(4); | ||
compressed.extend_from_slice(&uncompressed_len.to_le_bytes()); | ||
compressed.extend_from_slice(&raw); | ||
|
||
Ok(compressed) | ||
} | ||
|
||
pub async fn compile_file(input_filename: &Path, output_filename: &Path) -> Result<(), String> { | ||
let resolver: (DummyResolver,) = (DummyResolver,); | ||
let loader = (DummyLoader,); | ||
|
||
let rt = Runtime::new().unwrap(); | ||
rt.set_loader(resolver, loader); | ||
let ctx = Context::full(&rt).unwrap(); | ||
|
||
let mut total_bytes: usize = 0; | ||
let mut compressed_bytes: usize = 0; | ||
let mut js_bytes: usize = 0; | ||
|
||
ctx.with(|ctx| { | ||
let source = fs::read_to_string(input_filename) | ||
.unwrap_or_else(|_| panic!("Unable to load: {}", input_filename.to_string_lossy())); | ||
js_bytes = source.len(); | ||
|
||
let module_name = input_filename | ||
.with_extension("") | ||
.to_string_lossy() | ||
.to_string(); | ||
|
||
trace!("Compiling module: {}", module_name); | ||
|
||
let module = unsafe { Module::unsafe_declare(ctx.clone(), module_name, source).unwrap() }; | ||
let bytes = module.write_object(false).unwrap(); | ||
let filename = output_filename.to_string_lossy().to_string(); | ||
let compressed = compress_module(&bytes).unwrap(); | ||
fs::write(filename, &compressed).unwrap(); | ||
|
||
total_bytes += bytes.len(); | ||
compressed_bytes += compressed.len(); | ||
}); | ||
|
||
trace!("JS size: {}", human_file_size(js_bytes)); | ||
trace!("Bytecode size: {}", human_file_size(total_bytes)); | ||
trace!( | ||
"Compressed bytecode size: {}", | ||
human_file_size(compressed_bytes) | ||
); | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters