we (web engine): Experimental web browser project to understand the limits of Claude
1//! JavaScript engine — lexer, parser, bytecode, register VM, GC, JIT (AArch64).
2
3pub mod ast;
4pub mod bytecode;
5pub mod compiler;
6pub mod gc;
7pub mod lexer;
8pub mod parser;
9pub mod vm;
10
11use std::fmt;
12
13/// An error produced by the JavaScript engine.
14#[derive(Debug)]
15pub enum JsError {
16 /// The engine does not yet support this feature or syntax.
17 NotImplemented,
18 /// A parse/syntax error in the source.
19 SyntaxError(String),
20 /// A runtime error during execution.
21 RuntimeError(String),
22}
23
24impl fmt::Display for JsError {
25 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
26 match self {
27 JsError::NotImplemented => write!(f, "not implemented"),
28 JsError::SyntaxError(msg) => write!(f, "SyntaxError: {}", msg),
29 JsError::RuntimeError(msg) => write!(f, "RuntimeError: {}", msg),
30 }
31 }
32}
33
34/// Evaluate a JavaScript source string and return the completion value as a string.
35///
36/// Parses the source, compiles to bytecode, and executes in the VM.
37pub fn evaluate(source: &str) -> Result<String, JsError> {
38 let program = parser::Parser::parse(source).map_err(|e| JsError::SyntaxError(e.to_string()))?;
39 let func = compiler::compile(&program)?;
40 let mut engine = vm::Vm::new();
41 let result = engine
42 .execute(&func)
43 .map_err(|e| JsError::RuntimeError(e.to_string()))?;
44 Ok(result.to_js_string(&engine.gc))
45}