1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-08 15:19:40 -05:00

feat(compile): Initialize runtime_compiler ops for standalone binaries (#10052)

This commit is contained in:
Divy Srivastava 2021-06-07 17:32:53 +05:30 committed by GitHub
parent 3b3be024fa
commit 89290741d1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 79 additions and 0 deletions

View file

@ -3,6 +3,9 @@
use crate::colors;
use crate::file_fetcher::get_source_from_bytes;
use crate::file_fetcher::strip_shebang;
use crate::flags::Flags;
use crate::ops;
use crate::program_state::ProgramState;
use crate::version;
use data_url::DataUrl;
use deno_core::error::type_error;
@ -181,11 +184,33 @@ impl ModuleLoader for EmbeddedModuleLoader {
}
}
fn metadata_to_flags(metadata: &Metadata) -> Flags {
let permissions = metadata.permissions.clone();
Flags {
argv: metadata.argv.clone(),
unstable: metadata.unstable,
seed: metadata.seed,
location: metadata.location.clone(),
allow_env: permissions.allow_env,
allow_hrtime: permissions.allow_hrtime,
allow_net: permissions.allow_net,
allow_plugin: permissions.allow_plugin,
allow_read: permissions.allow_read,
allow_run: permissions.allow_run,
allow_write: permissions.allow_write,
v8_flags: metadata.v8_flags.clone(),
log_level: metadata.log_level,
..Default::default()
}
}
pub async fn run(
source_code: String,
metadata: Metadata,
) -> Result<(), AnyError> {
let flags = metadata_to_flags(&metadata);
let main_module = resolve_url(SPECIFIER)?;
let program_state = ProgramState::build(flags).await?;
let permissions = Permissions::from_options(&metadata.permissions);
let blob_url_store = BlobUrlStore::default();
let broadcast_channel = InMemoryBroadcastChannel::default();
@ -226,6 +251,16 @@ pub async fn run(
};
let mut worker =
MainWorker::from_options(main_module.clone(), permissions, &options);
{
let js_runtime = &mut worker.js_runtime;
js_runtime
.op_state()
.borrow_mut()
.put::<Arc<ProgramState>>(program_state.clone());
ops::errors::init(js_runtime);
ops::runtime_compiler::init(js_runtime);
js_runtime.sync_ops_cache();
}
worker.bootstrap(&options);
worker.execute_module(&main_module).await?;
worker.execute("window.dispatchEvent(new Event('load'))")?;

View file

@ -5876,6 +5876,38 @@ console.log("finish");
assert_eq!(output.stdout, b"Hello Deno!\n");
}
#[test]
fn standalone_compiler_ops() {
let dir = TempDir::new().expect("tempdir fail");
let exe = if cfg!(windows) {
dir.path().join("standalone_compiler_ops.exe")
} else {
dir.path().join("standalone_compiler_ops")
};
let output = util::deno_cmd()
.current_dir(util::root_path())
.arg("compile")
.arg("--unstable")
.arg("--output")
.arg(&exe)
.arg("./cli/tests/standalone_compiler_ops.ts")
.stdout(std::process::Stdio::piped())
.spawn()
.unwrap()
.wait_with_output()
.unwrap();
assert!(output.status.success());
let output = Command::new(exe)
.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::piped())
.spawn()
.unwrap()
.wait_with_output()
.unwrap();
assert!(output.status.success());
assert_eq!(output.stdout, b"Hello, Compiler API!\n");
}
#[test]
fn compile_with_directory_exists_error() {
let dir = TempDir::new().expect("tempdir fail");

View file

@ -0,0 +1,12 @@
const { files } = await Deno.emit("/mod.ts", {
bundle: "classic",
sources: {
"/mod.ts": `import { hello } from "/hello.ts"; console.log(hello);`,
"/hello.ts": `export const hello: string = "Hello, Compiler API!"`,
},
compilerOptions: {
sourceMap: false,
},
});
eval(files["deno:///bundle.js"]);