cycpt
cycpt13mo ago

Rust run JS/TS

How can I embed this project into a rust application so that rust can run js ts and has runtime api For example fetch
4 Replies
cycpt
cycptOP13mo ago
use deno_core can run js but can't run ts, and no runtime api
Deno AI Helper
Deno AI Helper13mo ago
To embed a JavaScript/TypeScript runtime into a Rust application, you can use the deno_core crate. This crate allows you to create a JavaScript runtime in your Rust application. Here's a step-by-step guide based on the documentation: 1. Add Dependencies: First, you need to add the deno_core and tokio dependencies to your project. You can do this by running the following commands:
$ cargo add deno_core
$ cargo add tokio --features=full
$ cargo add deno_core
$ cargo add tokio --features=full
2. Create a JavaScript Runtime: Next, you need to create an instance of JsRuntime, which is responsible for JavaScript execution. You can do this in an asynchronous Rust function. Here's an example:
async fn run_js(file_path: &str) -> Result<(), AnyError> {
let main_module = deno_core::resolve_path(file_path)?;
let runjs_extension = Extension::builder("runjs")
.ops(vec![
op_read_file::decl(),
op_write_file::decl(),
op_remove_file::decl(),
op_fetch::decl(),
])
.build();

let mut js_runtime = deno_core::JsRuntime::new(deno_core::RuntimeOptions {
module_loader: Some(Rc::new(TsModuleLoader)),
extensions: vec![runjs_extension],
..Default::default()
});

js_runtime.execute_script(
&deno_core::resolve_url_or_path("./runtime.js").unwrap(),
include_str!("./runtime.js"),
)?;

js_runtime.execute_module(&main_module).await?;
js_runtime.run_event_loop().await?;
Ok(())
}
async fn run_js(file_path: &str) -> Result<(), AnyError> {
let main_module = deno_core::resolve_path(file_path)?;
let runjs_extension = Extension::builder("runjs")
.ops(vec![
op_read_file::decl(),
op_write_file::decl(),
op_remove_file::decl(),
op_fetch::decl(),
])
.build();

let mut js_runtime = deno_core::JsRuntime::new(deno_core::RuntimeOptions {
module_loader: Some(Rc::new(TsModuleLoader)),
extensions: vec![runjs_extension],
..Default::default()
});

js_runtime.execute_script(
&deno_core::resolve_url_or_path("./runtime.js").unwrap(),
include_str!("./runtime.js"),
)?;

js_runtime.execute_module(&main_module).await?;
js_runtime.run_event_loop().await?;
Ok(())
}
3. Add a Fetch API: To add a fetch API, you can create a runtime.js file that will instantiate and make the fetch object globally available. Here's an example:
((globalThis) => {
const core = Deno.core;

globalThis.runjs = {
fetch: (url) => {
return core.ops.op_fetch(url);
},
};
})(globalThis);
((globalThis) => {
const core = Deno.core;

globalThis.runjs = {
fetch: (url) => {
return core.ops.op_fetch(url);
},
};
})(globalThis);
4. **Register the Fetch
Deno AI Helper
Deno AI Helper13mo ago
API**: You need to register the fetch API in your run_js function. Here's an example: ```rust let runjs_extension = Extension::builder("runjs") .ops(vec![ op_read_file::decl(), op_write_file::decl
cycpt
cycptOP13mo ago
how like deno cli The same supports all runtime api Instead of doing it manually