mirror of
https://github.com/pjh456/OxideJS.git
synced 2026-10-23 18:21:13 +08:00
refactor: 集中 AST 缓存逻辑实现并清理无关依赖
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
use oxide_compiler::cache::CodeCache;
|
||||
use oxide_compiler::compiler::Compiler;
|
||||
use oxide_compiler::opcode::{self, OpCode};
|
||||
use oxide_kernel::code_forge::CodeForge;
|
||||
use oxide_parser::Allocator;
|
||||
|
||||
fn compile_source(source: &str) -> oxide_compiler::module::CompiledModule {
|
||||
@@ -13,7 +13,7 @@ fn compile_source(source: &str) -> oxide_compiler::module::CompiledModule {
|
||||
fn parse_to_hash(source: &str) -> u64 {
|
||||
let allocator = Allocator::default();
|
||||
let program = oxide_parser::parse(&allocator, source).expect("parse failed");
|
||||
oxide_compiler::cache::structural_hash(&program)
|
||||
oxide_compiler::compiler::structural_hash(&program)
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -81,31 +81,34 @@ fn compile_binary_ops() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn structural_cache_hit() {
|
||||
fn structural_hash_hit() {
|
||||
let forge = CodeForge::new();
|
||||
let compiler = Compiler::new();
|
||||
let allocator = Allocator::default();
|
||||
let program = oxide_parser::parse(&allocator, "1 + 2").expect("parse failed");
|
||||
let compiler = Compiler::new();
|
||||
let mut cache = CodeCache::new();
|
||||
|
||||
let m1 = cache
|
||||
let m1 = forge
|
||||
.get_or_compile(&program, &compiler)
|
||||
.expect("compile failed");
|
||||
let m2 = cache
|
||||
.expect("first compile");
|
||||
let m2 = forge
|
||||
.get_or_compile(&program, &compiler)
|
||||
.expect("compile failed");
|
||||
.expect("second compile");
|
||||
|
||||
assert_eq!(m1.bytecode, m2.bytecode);
|
||||
assert_eq!(
|
||||
(m1.bytecode.len(), m1.n_registers),
|
||||
(m2.bytecode.len(), m2.n_registers)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn structural_cache_same_shape() {
|
||||
fn structural_hash_same_shape() {
|
||||
let hash_a = parse_to_hash("var x = 1 + 2; var y = x;");
|
||||
let hash_b = parse_to_hash("var a = 3 + 4; var b = a;");
|
||||
assert_eq!(hash_a, hash_b);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn structural_cache_different_shape() {
|
||||
fn structural_hash_different_shape() {
|
||||
let hash_a = parse_to_hash("1 + 2");
|
||||
let hash_b = parse_to_hash("1 + 2 * 3");
|
||||
assert_ne!(hash_a, hash_b);
|
||||
|
||||
Reference in New Issue
Block a user