|
| 1 | +# JavaScript Engine in Rust |
| 2 | + |
| 3 | +[](https://crates.io/crates/javascript) |
| 4 | +[](https://docs.rs/javascript) |
| 5 | +[](https://github.com/ssrlive/javascript/blob/master/LICENSE) |
| 6 | +[](https://www.rust-lang.org/) |
| 7 | +[](https://github.com/ssrlive/javascript/actions) |
| 8 | +[](https://github.com/ssrlive/javascript/actions) |
| 9 | +[](https://crates.io/crates/javascript) |
| 10 | + |
| 11 | +A JavaScript engine implementation written in Rust, providing a complete JavaScript runtime |
| 12 | +with support for modern language features. |
| 13 | + |
| 14 | +## Features |
| 15 | + |
| 16 | +### Core JavaScript Features |
| 17 | +- **Variables and Scoping**: `let`, `const`, `var` declarations |
| 18 | +- **Data Types**: Numbers, strings, booleans, objects, arrays, functions, classes |
| 19 | +- **Control Flow**: `if/else`, loops (`for`, `while`, `do-while`), `switch`, `try/catch/finally` |
| 20 | +- **Functions**: Regular functions, arrow functions, async/await |
| 21 | +- **Classes**: Class definitions, inheritance, static methods/properties, getters/setters |
| 22 | +- **Promises**: Promise creation, resolution, async/await syntax |
| 23 | +- **Destructuring**: Array and object destructuring |
| 24 | +- **Template Literals**: String interpolation |
| 25 | +- **Optional Chaining**: Safe property access (`?.`) |
| 26 | +- **Nullish Coalescing**: `??` operator and assignments (`??=`) |
| 27 | +- **Logical Assignments**: `&&=`, `||=` operators |
| 28 | + |
| 29 | +### Built-in Objects and APIs |
| 30 | +- **Array**: Full array methods and static constructors |
| 31 | +- **Object**: Property manipulation, prototype chain |
| 32 | +- **String**: String methods and UTF-16 support |
| 33 | +- **Number**: Number parsing and formatting |
| 34 | +- **Math**: Mathematical functions and constants |
| 35 | +- **Date**: Date/time handling with chrono integration |
| 36 | +- **RegExp**: Regular expressions with regex crate |
| 37 | +- **JSON**: JSON parsing and stringification |
| 38 | +- **Console**: Logging and debugging utilities |
| 39 | +- **OS**: File system operations, path manipulation |
| 40 | +- **File**: File I/O operations |
| 41 | + |
| 42 | +### Advanced Features |
| 43 | +- **Modules**: Import/export system with `import * as name from "module"` |
| 44 | +- **Event Loop**: Asynchronous task scheduling and execution |
| 45 | +- **Memory Management**: Reference counting and garbage collection |
| 46 | +- **FFI Integration**: C-compatible API similar to QuickJS |
| 47 | + |
| 48 | +## Installation |
| 49 | + |
| 50 | +Add this to your `Cargo.toml`: |
| 51 | + |
| 52 | +```toml |
| 53 | +[dependencies] |
| 54 | +javascript = "0.1.0" |
| 55 | +``` |
| 56 | + |
| 57 | +## Usage |
| 58 | + |
| 59 | +### Basic Evaluation |
| 60 | + |
| 61 | +```rust |
| 62 | +use javascript::evaluate_script; |
| 63 | + |
| 64 | +let result = evaluate_script(r#" |
| 65 | + let x = 42; |
| 66 | + let y = x * 2; |
| 67 | + y + 10 |
| 68 | +"#).unwrap(); |
| 69 | + |
| 70 | +match result { |
| 71 | + javascript::Value::Number(n) => println!("Result: {}", n), // Output: Result: 94 |
| 72 | + _ => println!("Unexpected result"), |
| 73 | +} |
| 74 | +``` |
| 75 | + |
| 76 | +### Using Built-in Modules |
| 77 | + |
| 78 | +```rust |
| 79 | +use javascript::evaluate_script; |
| 80 | + |
| 81 | +let result = evaluate_script(r#" |
| 82 | + import * as console from "console"; |
| 83 | + import * as os from "os"; |
| 84 | +
|
| 85 | + console.log("Hello from JavaScript!"); |
| 86 | + let cwd = os.getcwd(); |
| 87 | + cwd |
| 88 | +"#).unwrap(); |
| 89 | +``` |
| 90 | + |
| 91 | +### Command Line Interface |
| 92 | + |
| 93 | +The crate provides two example binaries: |
| 94 | + |
| 95 | +#### `js` - QuickJS-compatible FFI interface |
| 96 | +```bash |
| 97 | +cargo run --example js -- -e "console.log('Hello World!')" |
| 98 | +cargo run --example js script.js |
| 99 | +``` |
| 100 | + |
| 101 | +#### `rust_js` - High-level Rust interface |
| 102 | +```bash |
| 103 | +cargo run --example rust_js -- -e "1 + 2 * 3" |
| 104 | +cargo run --example rust_js script.js |
| 105 | +``` |
| 106 | + |
| 107 | +## API Reference |
| 108 | + |
| 109 | +### Core Functions |
| 110 | + |
| 111 | +- `evaluate_script(code: &str) -> Result<Value, JSError>`: Evaluate JavaScript code |
| 112 | +- `evaluate_script_async(code: &str) -> Result<Value, JSError>`: Evaluate with async support |
| 113 | +- `tokenize(code: &str) -> Result<Vec<Token>, JSError>`: Lexical analysis |
| 114 | + |
| 115 | +### FFI Interface (QuickJS-compatible) |
| 116 | + |
| 117 | +- `JS_NewRuntime() -> *mut JSRuntime`: Create a new runtime |
| 118 | +- `JS_NewContext(rt: *mut JSRuntime) -> *mut JSContext`: Create a context |
| 119 | +- `JS_Eval(ctx, code, len, filename, flags) -> JSValue`: Evaluate code |
| 120 | +- `JS_NewString(ctx, str) -> JSValue`: Create a string value |
| 121 | +- `JS_DefinePropertyValue(ctx, obj, atom, val, flags) -> i32`: Define object property |
| 122 | + |
| 123 | +### Value Types |
| 124 | + |
| 125 | +The engine uses a `Value` enum to represent JavaScript values. See the source code for the complete definition, which includes variants for numbers, strings, objects, functions, promises, and more. |
| 126 | + |
| 127 | +## Architecture |
| 128 | + |
| 129 | +The engine consists of several key components: |
| 130 | + |
| 131 | +- **Parser**: Converts JavaScript source code into an AST |
| 132 | +- **Evaluator**: Executes the AST in a managed environment |
| 133 | +- **Object System**: Reference-counted objects with prototype chains |
| 134 | +- **Memory Management**: Custom allocators and garbage collection |
| 135 | +- **FFI Layer**: C-compatible interface for embedding |
| 136 | +- **Built-in Modules**: Standard library implementations |
| 137 | + |
| 138 | +## Testing |
| 139 | + |
| 140 | +Run the test suite: |
| 141 | + |
| 142 | +```bash |
| 143 | +cargo test |
| 144 | +``` |
| 145 | + |
| 146 | +Run with logging: |
| 147 | + |
| 148 | +```bash |
| 149 | +RUST_LOG=debug cargo test |
| 150 | +``` |
| 151 | + |
| 152 | +## Performance |
| 153 | + |
| 154 | +The engine is optimized for: |
| 155 | +- Fast parsing and evaluation |
| 156 | +- Efficient memory usage with Rc<RefCell<>> |
| 157 | +- Minimal allocations during execution |
| 158 | +- QuickJS-compatible FFI for high-performance embedding |
| 159 | + |
| 160 | +## Limitations |
| 161 | + |
| 162 | +- No JIT compilation (interpreted only) |
| 163 | +- Limited browser API compatibility |
| 164 | +- Some ES6+ features may be incomplete |
| 165 | +- Error handling could be more robust |
| 166 | + |
| 167 | +## Contributing |
| 168 | + |
| 169 | +Contributions are welcome! Areas for improvement: |
| 170 | + |
| 171 | +- JIT compilation support |
| 172 | +- More comprehensive test coverage |
| 173 | +- Browser API compatibility |
| 174 | +- Performance optimizations |
| 175 | +- Additional language features |
| 176 | + |
| 177 | +## License |
| 178 | + |
| 179 | +This project is licensed under the MIT License - see the LICENSE file for details. |
| 180 | + |
| 181 | +## Acknowledgments |
| 182 | + |
| 183 | +- Inspired by the QuickJS JavaScript engine |
| 184 | +- Built with Rust's powerful type system and memory safety guarantees |
| 185 | +- Uses several excellent Rust crates: `regex`, `chrono`, `serde_json`, etc. |
0 commit comments