tanxium-0.1.2 has been yanked.
Tanxium
Standalone JavaScript runtime for Yasumu.
[!CAUTION]
This project is still in development and not ready for production use.
Installation
cargo add tanxium boa_engine
Example
This is an example of how to use Tanxium to run JavaScript/TypeScript code. You can also find this example in the examples directory.
use boa_engine::*;
use std::io::Write;
use tanxium::tanxium;
fn main() {
let file = std::env::args().nth(1).expect("No file provided");
let is_typescript = file.ends_with(".ts");
let code = std::fs::read_to_string(file).expect("Unable to read file");
let cwd = std::env::current_dir()
.unwrap()
.to_str()
.unwrap()
.to_string();
let builtins = tanxium::TanxiumBuiltinsExposure {
crypto: true,
performance: true,
runtime: true,
console: true,
};
let options = tanxium::TanxiumOptions {
cwd,
typescript: true,
builtins,
global_object_name: "Tanxium".to_string(),
};
let mut tanxium = tanxium::Tanxium::new(options).unwrap();
tanxium.init_runtime_apis().unwrap();
tanxium.load_default_extensions().unwrap();
let ctx = &mut tanxium.context;
ctx.register_global_builtin_callable(
js_string!("prompt"),
1,
NativeFunction::from_fn_ptr(|_, args, context| {
let src = args.get_or_undefined(0).to_string(context).unwrap();
let message = src.to_std_string().unwrap();
print!("{}: ", message);
std::io::stdout().flush().unwrap();
let mut input = String::new();
match std::io::stdin().read_line(&mut input) {
Ok(_) => Ok(JsValue::from(JsString::from(input.trim_end()))),
Err(e) => Err(JsError::from_native(
JsNativeError::error().with_message(e.to_string()),
)),
}
}),
)
.expect("Failed to register prompt function");
let code = if is_typescript {
tanxium
.transpile(code.as_str())
.expect("Failed to transpile typescript")
} else {
code
};
tanxium.execute(code.as_str()).unwrap();
}
Now you can run your JavaScript/TypeScript code using Tanxium:
cargo run ./file.ts