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