| Crates.io | javascript |
| lib.rs | javascript |
| version | 0.1.13 |
| created_at | 2025-11-29 06:55:43.796478+00 |
| updated_at | 2025-12-23 09:24:28.376407+00 |
| description | A JavaScript engine implementation in Rust |
| homepage | https://crates.io/crates/javascript |
| repository | https://github.com/ssrlive/javascript |
| max_upload_size | |
| id | 1956433 |
| size | 2,178,729 |
A JavaScript engine implementation written in Rust, providing a complete JavaScript runtime with support for modern language features including ES6+ modules, async/await, BigInt, TypedArray, and more.
let, const, var declarations with proper scope rulesif/else, loops (for, while, do-while), switch, try/catch/finally?.)?? operator and assignments (??=)&&=, ||= operatorsimport/export syntax and dynamic import()Symbol.iterator support and for...of loopsfunction*) and generator objectspush, pop, map, filter, reduce, etc.)keys, values, assign, etc.)Add this to your Cargo.toml:
[dependencies]
javascript = "0.1.8"
use javascript::evaluate_script;
let result = evaluate_script(r#"
let x = 42n;
let y = x * 2n;
y + 10n
"#, None::<&std::path::Path>).unwrap();
match result {
javascript::Value::BigInt(b) => println!("Result: {b}"), // Output: Result: 94n
_ => println!("Unexpected result"),
}
use javascript::evaluate_script;
let result = evaluate_script(r#"
import * as console from "console";
import * as os from "os";
console.log("Hello from JavaScript!");
let cwd = os.getcwd();
cwd
"#, None::<&std::path::Path>).unwrap();
use javascript::evaluate_script;
let result = evaluate_script(r#"
async function example() {
let promise = new Promise((resolve) => {
setTimeout(() => resolve("Done!"), 100);
});
return await promise;
}
example()
"#, None::<&std::path::Path>).unwrap();
// The engine automatically runs the event loop to resolve promises
use javascript::evaluate_script;
let result = evaluate_script(r#"
let timeoutId = setTimeout(() => {
console.log("Timeout executed!");
}, 1000);
// Cancel the timeout
clearTimeout(timeoutId);
"Timeout scheduled and cancelled"
"#, None::<&std::path::Path>).unwrap();
The crate provides a CLI binary with REPL support:
js - Command-line interface with REPL# Execute a script string
cargo run --example js -- -e "console.log('Hello World!')"
# Execute a JavaScript file
cargo run --example js -- script.js
# Start interactive REPL (persistent environment)
cargo run --example js
The REPL maintains state between evaluations, allowing you to define variables and functions that persist across multiple inputs.
evaluate_script<T: AsRef<str>, P: AsRef<Path>>(code: T, script_path: Option<P>) -> Result<Value, JSError>:
Evaluate JavaScript code with optional script path for error reportingtokenize(code: &str) -> Result<Vec<Token>, JSError>: Perform lexical analysisparse_statements(tokens: &mut Vec<Token>) -> Result<Vec<Statement>, JSError>: Parse tokens into ASTRepl::new() -> Repl: Create a new persistent REPL environmentRepl::eval(&self, code: &str) -> Result<Value, JSError>: Evaluate code in REPL contextThe engine uses a comprehensive Value enum to represent JavaScript values, including primitives
(numbers, strings, booleans), objects, functions, promises, symbols, BigInts, collections
(Map, Set, WeakMap, WeakSet), generators, proxies, and typed arrays.
The engine consists of several key components:
tokenize)parse_statements)evaluate_statements)Run the comprehensive test suite:
cargo test
Run with detailed logging:
RUST_LOG=debug cargo test
Run performance benchmarks:
cargo bench
The engine is optimized for:
Benchmark results show competitive performance for interpretation workloads.
While the engine supports most modern JavaScript features, some areas are still developing:
Contributions are welcome! Areas for potential improvement:
# Clone the repository
git clone https://github.com/ssrlive/javascript.git
cd javascript
# Run tests
cargo test
# Run benchmarks
cargo bench
# Build documentation
cargo doc --open
This project is licensed under the MIT License - see the LICENSE file for details.
chrono, fancy-regex, num-bigint, serde_json, thiserror, etc.