Giter Club home page Giter Club logo

js-sandbox's Introduction

js-sandbox

crates.io docs.rs

js-sandbox is a Rust library for executing JavaScript code from Rust in a secure sandbox. It is based on the Deno project and uses serde_json for serialization.

This library's primary focus is embedding JS as a scripting language into Rust. It does not provide all possible integrations between the two languages, and is not tailored to JS's biggest domain as a client/server side language of the web.

Instead, js-sandbox focuses on calling standalone JS code from Rust, and tries to remain as simple as possible in doing so. The typical use case is a core Rust application that integrates with scripts from external users, for example a plugin system or a game that runs external mods.

This library is in early development, with a basic but powerful API. The API may still evolve considerably.

Examples

Print from JavaScript

The Hello World example -- print something using JavaScript -- is one line, as it should be:

fn main() {
	js_sandbox::eval_json("console.log('Hello Rust from JS')").expect("JS runs");
}

Call a JS function

A very basic application calls a JavaScript function triple() from Rust. It passes an argument and accepts a return value, both serialized via JSON:

use js_sandbox::{Script, AnyError};

fn main() -> Result<(), AnyError> {
	let js_code = "function triple(a) { return 3 * a; }";
	let mut script = Script::from_string(js_code)?;

	let arg = 7;
	let result: i32 = script.call("triple", &arg, None)?;

	assert_eq!(result, 21);
	Ok(())
}

An example that serializes a JSON object (Rust -> JS) and formats a string (JS -> Rust):

use js_sandbox::{Script, AnyError};
use serde::Serialize;

#[derive(Serialize, PartialEq)]
struct Person {
	name: String,
	age: u8,
}

fn main() -> Result<(), AnyError> {
	let src = r#"
		function toString(person) {
			return "A person named " + person.name + " of age " + person.age;
		}"#;

	let mut script = Script::from_string(src)
		.expect("Initialization succeeds");

	let person = Person { name: "Roger".to_string(), age: 42 };
	let result: String = script.call("toString", &person, None).unwrap();

	assert_eq!(result, "A person named Roger of age 42");
	Ok(())
}

Maintain state in JavaScript

It is possible to initialize a stateful JS script, and then use functions to modify that state over time. This example appends a string in two calls, and then gets the result in a third call:

use js_sandbox::{Script, AnyError};

fn main() -> Result<(), AnyError> {
	let src = r#"
		var total = '';
		function append(str) { total += str; }
		function get()       { return total; }"#;

	let mut script = Script::from_string(src)
		.expect("Initialization succeeds");

	let _: () = script.call("append", &"hello", None).unwrap();
	let _: () = script.call("append", &" world", None).unwrap();
	let result: String = script.call("get", &(), None).unwrap();

	assert_eq!(result, "hello world");
	Ok(())
}

Call a script with timeout

The JS code may contain long or forever running loops, that block Rust code. It is possible to set a timeout after which JS script execution is aborted.

use js_sandbox::{Script, AnyError};

fn main() -> Result<(), AnyError> {
	let js_code = "function run_forever() { for(;;){} }";
	let mut script = Script::from_string(js_code)?;

	let result: Result<String, AnyError> = script.call("run_forever", &(), Some(1000));

	debug_assert_eq!(result.unwrap_err().to_string(), "Uncaught Error: execution terminated".to_string());

	Ok(())
}

js-sandbox's People

Contributors

bromeon avatar sz3yd avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.