refactor: 动态编译改经 CompilerService 注入,oxide_vm 去编译器依赖

This commit is contained in:
2026-10-08 05:51:58 +00:00
parent 66735f802f
commit 4f9b6ab444
94 changed files with 459 additions and 67 deletions
@@ -0,0 +1,39 @@
//! 默认编译服务:串联 `oxide_parser::parse` 与 `Compiler::compile`,
//! parse 错误映射与 `Allocator` 构造的唯一实现源。
use oxide_bytecode::module::CompiledModule;
use oxide_parser::Program;
use oxide_runtime_api::CompilerService;
use crate::compiler::Compiler;
/// 无状态默认编译服务:两方法均无内部可变状态,
/// `Arc<dyn CompilerService>` 可跨线程共享。
pub struct DefaultCompilerService;
impl CompilerService for DefaultCompilerService {
fn compile_script(&self, source: &str) -> Result<CompiledModule, String> {
// 局部分配器:AST 节点经 oxc bump 分配器分配,编译完成即弃。
let allocator = oxide_parser::Allocator::default();
let program = parse_program(&allocator, source)?;
// 动态路径源契约:调用方以源码域转义(`string_forge::source_escape`)
// 形态传源,故置 source_encoded。
Compiler::new().with_source_encoded(true).compile(&program)
}
fn compile_eval_script(&self, source: &str) -> Result<CompiledModule, String> {
let allocator = oxide_parser::Allocator::default();
let program = parse_program(&allocator, source)?;
// eval 脚本:顶层 var/function 声明落全局属性 configurable:true。
Compiler::new()
.with_eval_script(true)
.with_source_encoded(true)
.compile(&program)
}
}
/// 解析源码,解析错误各条消息换行拼接为单一错误串。
fn parse_program<'a>(allocator: &'a oxide_parser::Allocator, source: &'a str) -> Result<Program<'a>, String> {
oxide_parser::parse(allocator, source)
.map_err(|errs| errs.into_iter().map(|e| e.message).collect::<Vec<_>>().join("\n"))
}
+3
View File
@@ -6,5 +6,8 @@
pub mod compiler;
pub mod compiler_log;
pub mod compiler_service;
pub mod flatten;
pub mod hash;
pub use compiler_service::DefaultCompilerService;