Skip to content

Commit e257db1

Browse files
committed
Fix snapshots getting around panic stub
1 parent a6f0aa9 commit e257db1

8 files changed

Lines changed: 37 additions & 25 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
/target
22
.vscode
3-
.idea
3+
.idea
4+
snapshot.bin

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name = "rustyscript"
33
description = "Effortless JS Integration for Rust"
44
edition = "2021"
55
license = "MIT OR Apache-2.0"
6-
version = "0.6.3"
6+
version = "0.6.4"
77
repository = "https://github.com/rscarson/rustyscript"
88

99
keywords = ["rust", "javascript", "deno", "runtime", "embedding"]

examples/interactive_prompt.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rustyscript::Runtime;
1+
use rustyscript::{Runtime, RuntimeOptions};
22

33
fn main() {
44
interactive_prompt()
@@ -13,7 +13,10 @@ fn interactive_prompt() {
1313
stack.insert(0, "exit".to_string());
1414
}
1515

16-
let mut runtime = Runtime::new(Default::default()).expect("Failed to create runtime");
16+
let mut runtime = Runtime::new(RuntimeOptions {
17+
..Default::default()
18+
})
19+
.expect("Failed to create runtime");
1720

1821
loop {
1922
// Make sure we have a command ready

src/ext/op_whitelist.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ export const whitelist = {
8080
"op_register_entrypoint": "Rustyscript builtin",
8181
"call_registered_function": "Rustyscript builtin",
8282
"call_registered_function_async": "Rustyscript builtin",
83+
"op_panic2": "Panic stub to replace op_panic",
8384

8485
//
8586
// v8 ops

src/ext/rustyscript/mod.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::{error::Error, RsAsyncFunction, RsFunction};
2-
use deno_core::{extension, op2, serde_json, v8, Extension, OpState};
2+
use deno_core::{anyhow::anyhow, extension, op2, serde_json, v8, Extension, OpState};
33
use std::collections::HashMap;
44

55
type FnCache = HashMap<String, Box<dyn RsFunction>>;
@@ -52,9 +52,14 @@ fn call_registered_function_async(
5252
Box::pin(std::future::ready(Err(Error::ValueNotCallable(name))))
5353
}
5454

55+
#[op2(fast)]
56+
fn op_panic2(#[string] msg: &str) -> Result<(), deno_core::anyhow::Error> {
57+
Err(anyhow!(msg.to_string()))
58+
}
59+
5560
extension!(
5661
rustyscript,
57-
ops = [op_register_entrypoint, call_registered_function, call_registered_function_async],
62+
ops = [op_register_entrypoint, call_registered_function, call_registered_function_async, op_panic2],
5863
esm_entry_point = "ext:rustyscript/rustyscript.js",
5964
esm = [ dir "src/ext/rustyscript", "rustyscript.js" ],
6065
);

src/ext/rustyscript/rustyscript.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,6 @@ globalThis.rustyscript = {
5151
};
5252
Object.freeze(globalThis.rustyscript);
5353

54-
// Stub out the panic function
55-
Deno.core.ops.op_panic = (msg) => { throw new Error(msg) };
56-
5754
export {
5855
nonEnumerable, readOnly, writeable, getterOnly, applyToGlobal
5956
};

src/inner_runtime.rs

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ pub struct InnerRuntime {
131131
}
132132
impl InnerRuntime {
133133
pub fn new(options: RuntimeOptions) -> Result<Self, Error> {
134-
let loader = Rc::new(RustyLoader::new(options.module_cache));
134+
let module_loader = Rc::new(RustyLoader::new(options.module_cache));
135135

136136
// If a snapshot is provided, do not reload ops
137137
let extensions = if options.startup_snapshot.is_some() {
@@ -140,26 +140,31 @@ impl InnerRuntime {
140140
ext::all_extensions(options.extensions, options.extension_options)
141141
};
142142

143-
Ok(Self {
144-
deno_runtime: JsRuntime::try_new(deno_core::RuntimeOptions {
145-
module_loader: Some(loader.clone()),
143+
let mut deno_runtime = JsRuntime::try_new(deno_core::RuntimeOptions {
144+
module_loader: Some(module_loader.clone()),
146145

147-
extension_transpiler: Some(Rc::new(|specifier, code| {
148-
transpile_extension(&specifier, &code)
149-
})),
146+
extension_transpiler: Some(Rc::new(|specifier, code| {
147+
transpile_extension(&specifier, &code)
148+
})),
150149

151-
source_map_getter: Some(loader.clone()),
152-
create_params: options.isolate_params,
153-
shared_array_buffer_store: options.shared_array_buffer_store,
150+
source_map_getter: Some(module_loader.clone()),
151+
create_params: options.isolate_params,
152+
shared_array_buffer_store: options.shared_array_buffer_store,
154153

155-
startup_snapshot: options.startup_snapshot,
156-
extensions,
154+
startup_snapshot: options.startup_snapshot,
155+
extensions,
157156

158-
..Default::default()
159-
})?,
157+
..Default::default()
158+
})?;
160159

161-
module_loader: loader,
160+
// Stub out bad and naughty functions
161+
deno_runtime
162+
.execute_script("", "Deno.core.ops.op_panic = Deno.core.ops.op_panic2;")
163+
.map_err(|e| Error::Runtime(format!("Could not initialize sandbox: {e}")))?;
162164

165+
Ok(Self {
166+
deno_runtime,
167+
module_loader,
163168
options: RuntimeOptions {
164169
timeout: options.timeout,
165170
default_entrypoint: options.default_entrypoint,

0 commit comments

Comments
 (0)