fix: 修复 Map/Set GC 追踪与 CodeForge 缓存键
This commit is contained in:
@@ -4,7 +4,7 @@ use crate::module::CompiledModule;
|
||||
use crate::opcode::{self, OpCode};
|
||||
use crate::symbol_table::{Binding, SymbolTable};
|
||||
|
||||
pub use crate::hash::structural_hash;
|
||||
pub use crate::hash::{compiled_module_hash, structural_hash};
|
||||
pub use crate::module::Constant;
|
||||
use crate::symbol_table::ScopeKind;
|
||||
pub use oxide_parser::VariableDeclarationKind;
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
use oxide_parser::{
|
||||
ClassElement, Expression, ForStatementInit, ObjectPropertyKind, PropertyKey, SimpleAssignmentTarget, Statement,
|
||||
BindingPattern, ClassElement, Expression, ForStatementInit, ObjectPropertyKind, PropertyKey,
|
||||
SimpleAssignmentTarget, Statement,
|
||||
};
|
||||
use std::hash::Hash;
|
||||
|
||||
@@ -24,103 +25,132 @@ macro_rules! hash_match {
|
||||
}
|
||||
|
||||
pub fn structural_hash(program: &oxide_parser::Program) -> u64 {
|
||||
hash_program(program, false)
|
||||
}
|
||||
|
||||
pub fn compiled_module_hash(program: &oxide_parser::Program) -> u64 {
|
||||
hash_program(program, true)
|
||||
}
|
||||
|
||||
fn hash_program(program: &oxide_parser::Program, include_binding_names: bool) -> u64 {
|
||||
use std::hash::Hasher;
|
||||
|
||||
let mut h = rustc_hash::FxHasher::default();
|
||||
|
||||
for stmt in &program.body {
|
||||
hash_statement(stmt, &mut h);
|
||||
hash_statement(stmt, &mut h, include_binding_names);
|
||||
}
|
||||
|
||||
h.finish()
|
||||
}
|
||||
|
||||
fn hash_statement(stmt: &Statement, h: &mut rustc_hash::FxHasher) {
|
||||
fn hash_binding_pattern(pattern: &BindingPattern, h: &mut rustc_hash::FxHasher) {
|
||||
if let BindingPattern::BindingIdentifier(ident) = pattern {
|
||||
ident.name.as_str().hash(h);
|
||||
}
|
||||
}
|
||||
|
||||
fn hash_statement(stmt: &Statement, h: &mut rustc_hash::FxHasher, include_binding_names: bool) {
|
||||
hash_match!(HashDomain::Statement, stmt, h, {
|
||||
Statement::ExpressionStatement(es) => {
|
||||
hash_expression(&es.expression, h);
|
||||
hash_expression(&es.expression, h, include_binding_names);
|
||||
}
|
||||
Statement::VariableDeclaration(decl) => {
|
||||
std::mem::discriminant(&decl.kind).hash(h);
|
||||
(decl.declarations.len() as u32).hash(h);
|
||||
for d in &decl.declarations {
|
||||
if include_binding_names {
|
||||
hash_binding_pattern(&d.id, h);
|
||||
}
|
||||
if let Some(init) = &d.init {
|
||||
hash_expression(init, h);
|
||||
hash_expression(init, h, include_binding_names);
|
||||
}
|
||||
}
|
||||
}
|
||||
Statement::ReturnStatement(ret) => {
|
||||
if let Some(arg) = &ret.argument {
|
||||
hash_expression(arg, h);
|
||||
hash_expression(arg, h, include_binding_names);
|
||||
}
|
||||
}
|
||||
Statement::IfStatement(ifs) => {
|
||||
hash_expression(&ifs.test, h);
|
||||
hash_statement(&ifs.consequent, h);
|
||||
hash_expression(&ifs.test, h, include_binding_names);
|
||||
hash_statement(&ifs.consequent, h, include_binding_names);
|
||||
if let Some(alt) = &ifs.alternate {
|
||||
hash_statement(alt, h);
|
||||
hash_statement(alt, h, include_binding_names);
|
||||
}
|
||||
}
|
||||
Statement::WhileStatement(wh) => {
|
||||
hash_expression(&wh.test, h);
|
||||
hash_statement(&wh.body, h);
|
||||
hash_expression(&wh.test, h, include_binding_names);
|
||||
hash_statement(&wh.body, h, include_binding_names);
|
||||
}
|
||||
Statement::ForStatement(fr) => {
|
||||
if let Some(init) = &fr.init {
|
||||
if let Some(expr) = init.as_expression() {
|
||||
hash_expression(expr, h);
|
||||
hash_expression(expr, h, include_binding_names);
|
||||
} else if let ForStatementInit::VariableDeclaration(decl) = init {
|
||||
std::mem::discriminant(&decl.kind).hash(h);
|
||||
(decl.declarations.len() as u32).hash(h);
|
||||
for d in &decl.declarations {
|
||||
if include_binding_names {
|
||||
hash_binding_pattern(&d.id, h);
|
||||
}
|
||||
if let Some(init_expr) = &d.init {
|
||||
hash_expression(init_expr, h);
|
||||
hash_expression(init_expr, h, include_binding_names);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if let Some(test) = &fr.test {
|
||||
hash_expression(test, h);
|
||||
hash_expression(test, h, include_binding_names);
|
||||
}
|
||||
if let Some(update) = &fr.update {
|
||||
hash_expression(update, h);
|
||||
hash_expression(update, h, include_binding_names);
|
||||
}
|
||||
hash_statement(&fr.body, h);
|
||||
hash_statement(&fr.body, h, include_binding_names);
|
||||
}
|
||||
Statement::BlockStatement(block) => {
|
||||
for s in &block.body {
|
||||
hash_statement(s, h);
|
||||
hash_statement(s, h, include_binding_names);
|
||||
}
|
||||
}
|
||||
Statement::BreakStatement(_) => {}
|
||||
Statement::ContinueStatement(_) => {}
|
||||
Statement::DoWhileStatement(dw) => {
|
||||
hash_statement(&dw.body, h);
|
||||
hash_expression(&dw.test, h);
|
||||
hash_statement(&dw.body, h, include_binding_names);
|
||||
hash_expression(&dw.test, h, include_binding_names);
|
||||
}
|
||||
Statement::ForInStatement(fi) => {
|
||||
hash_expression(&fi.right, h);
|
||||
hash_statement(&fi.body, h);
|
||||
hash_expression(&fi.right, h, include_binding_names);
|
||||
hash_statement(&fi.body, h, include_binding_names);
|
||||
}
|
||||
Statement::SwitchStatement(sw) => {
|
||||
hash_expression(&sw.discriminant, h);
|
||||
hash_expression(&sw.discriminant, h, include_binding_names);
|
||||
for case in &sw.cases {
|
||||
if let Some(test) = &case.test {
|
||||
hash_expression(test, h);
|
||||
hash_expression(test, h, include_binding_names);
|
||||
}
|
||||
for s in &case.consequent {
|
||||
hash_statement(s, h);
|
||||
hash_statement(s, h, include_binding_names);
|
||||
}
|
||||
}
|
||||
}
|
||||
Statement::FunctionDeclaration(fd) => {
|
||||
if let Some(id) = &fd.id {
|
||||
if include_binding_names {
|
||||
if let Some(id) = &fd.id {
|
||||
id.name.as_str().hash(h);
|
||||
}
|
||||
} else if let Some(id) = &fd.id {
|
||||
id.name.as_str().hash(h);
|
||||
}
|
||||
(fd.params.items.len() as u32).hash(h);
|
||||
if include_binding_names {
|
||||
for param in &fd.params.items {
|
||||
hash_binding_pattern(¶m.pattern, h);
|
||||
}
|
||||
}
|
||||
if let Some(body) = &fd.body {
|
||||
for s in &body.statements {
|
||||
hash_statement(s, h);
|
||||
hash_statement(s, h, include_binding_names);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -130,32 +160,34 @@ fn hash_statement(stmt: &Statement, h: &mut rustc_hash::FxHasher) {
|
||||
}
|
||||
class.super_class.is_some().hash(h);
|
||||
if let Some(super_class) = &class.super_class {
|
||||
hash_expression(super_class, h);
|
||||
hash_expression(super_class, h, include_binding_names);
|
||||
}
|
||||
for element in &class.body.body {
|
||||
hash_class_element(element, h);
|
||||
hash_class_element(element, h, include_binding_names);
|
||||
}
|
||||
}
|
||||
Statement::ThrowStatement(ts) => {
|
||||
hash_expression(&ts.argument, h);
|
||||
hash_expression(&ts.argument, h, include_binding_names);
|
||||
}
|
||||
Statement::TryStatement(ts) => {
|
||||
for s in &ts.block.body {
|
||||
hash_statement(s, h);
|
||||
hash_statement(s, h, include_binding_names);
|
||||
}
|
||||
if let Some(catch) = &ts.handler {
|
||||
if let Some(param) = &catch.param {
|
||||
if let oxide_parser::BindingPattern::BindingIdentifier(bi) = ¶m.pattern {
|
||||
if include_binding_names {
|
||||
hash_binding_pattern(¶m.pattern, h);
|
||||
} else if let oxide_parser::BindingPattern::BindingIdentifier(bi) = ¶m.pattern {
|
||||
bi.name.as_str().hash(h);
|
||||
}
|
||||
}
|
||||
for s in &catch.body.body {
|
||||
hash_statement(s, h);
|
||||
hash_statement(s, h, include_binding_names);
|
||||
}
|
||||
}
|
||||
if let Some(finally) = &ts.finalizer {
|
||||
for s in &finally.body {
|
||||
hash_statement(s, h);
|
||||
hash_statement(s, h, include_binding_names);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -163,7 +195,7 @@ fn hash_statement(stmt: &Statement, h: &mut rustc_hash::FxHasher) {
|
||||
});
|
||||
}
|
||||
|
||||
fn hash_property_key(key: &PropertyKey, h: &mut rustc_hash::FxHasher) {
|
||||
fn hash_property_key(key: &PropertyKey, h: &mut rustc_hash::FxHasher, include_binding_names: bool) {
|
||||
hash_match!(HashDomain::PropertyKey, key, h, {
|
||||
PropertyKey::StaticIdentifier(ident) => {
|
||||
ident.name.as_str().hash(h);
|
||||
@@ -181,80 +213,89 @@ fn hash_property_key(key: &PropertyKey, h: &mut rustc_hash::FxHasher) {
|
||||
pi.name.as_str().hash(h);
|
||||
}
|
||||
_ => {
|
||||
hash_expression(key.to_expression(), h);
|
||||
hash_expression(key.to_expression(), h, include_binding_names);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
fn hash_class_element(element: &ClassElement, h: &mut rustc_hash::FxHasher) {
|
||||
fn hash_class_element(element: &ClassElement, h: &mut rustc_hash::FxHasher, include_binding_names: bool) {
|
||||
hash_match!(HashDomain::ClassElement, element, h, {
|
||||
ClassElement::MethodDefinition(method) => {
|
||||
method.r#static.hash(h);
|
||||
method.computed.hash(h);
|
||||
std::mem::discriminant(&method.kind).hash(h);
|
||||
hash_property_key(&method.key, h);
|
||||
hash_property_key(&method.key, h, include_binding_names);
|
||||
if method.computed {
|
||||
hash_property_key(&method.key, h);
|
||||
hash_property_key(&method.key, h, include_binding_names);
|
||||
}
|
||||
(method.value.params.items.len() as u32).hash(h);
|
||||
if include_binding_names {
|
||||
for param in &method.value.params.items {
|
||||
hash_binding_pattern(¶m.pattern, h);
|
||||
}
|
||||
}
|
||||
if let Some(body) = &method.value.body {
|
||||
for stmt in &body.statements {
|
||||
hash_statement(stmt, h);
|
||||
hash_statement(stmt, h, include_binding_names);
|
||||
}
|
||||
}
|
||||
}
|
||||
ClassElement::PropertyDefinition(prop) => {
|
||||
prop.r#static.hash(h);
|
||||
prop.computed.hash(h);
|
||||
hash_property_key(&prop.key, h);
|
||||
hash_property_key(&prop.key, h, include_binding_names);
|
||||
if let Some(value) = &prop.value {
|
||||
hash_expression(value, h);
|
||||
hash_expression(value, h, include_binding_names);
|
||||
}
|
||||
}
|
||||
ClassElement::StaticBlock(block) => {
|
||||
for stmt in &block.body {
|
||||
hash_statement(stmt, h);
|
||||
hash_statement(stmt, h, include_binding_names);
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
});
|
||||
}
|
||||
|
||||
fn hash_expression(expr: &Expression, h: &mut rustc_hash::FxHasher) {
|
||||
fn hash_expression(expr: &Expression, h: &mut rustc_hash::FxHasher, include_binding_names: bool) {
|
||||
hash_match!(HashDomain::Expression, expr, h, {
|
||||
Expression::BinaryExpression(bin) => {
|
||||
std::mem::discriminant(&bin.operator).hash(h);
|
||||
hash_expression(&bin.left, h);
|
||||
hash_expression(&bin.right, h);
|
||||
hash_expression(&bin.left, h, include_binding_names);
|
||||
hash_expression(&bin.right, h, include_binding_names);
|
||||
}
|
||||
Expression::UnaryExpression(un) => {
|
||||
std::mem::discriminant(&un.operator).hash(h);
|
||||
hash_expression(&un.argument, h);
|
||||
hash_expression(&un.argument, h, include_binding_names);
|
||||
}
|
||||
Expression::CallExpression(call) => {
|
||||
(call.arguments.len() as u32).hash(h);
|
||||
hash_expression(&call.callee, h);
|
||||
hash_expression(&call.callee, h, include_binding_names);
|
||||
for arg in &call.arguments {
|
||||
if let Some(expr) = arg.as_expression() {
|
||||
hash_expression(expr, h);
|
||||
hash_expression(expr, h, include_binding_names);
|
||||
}
|
||||
}
|
||||
}
|
||||
Expression::LogicalExpression(log) => {
|
||||
std::mem::discriminant(&log.operator).hash(h);
|
||||
hash_expression(&log.left, h);
|
||||
hash_expression(&log.right, h);
|
||||
hash_expression(&log.left, h, include_binding_names);
|
||||
hash_expression(&log.right, h, include_binding_names);
|
||||
}
|
||||
Expression::ConditionalExpression(cond) => {
|
||||
hash_expression(&cond.test, h);
|
||||
hash_expression(&cond.consequent, h);
|
||||
hash_expression(&cond.alternate, h);
|
||||
hash_expression(&cond.test, h, include_binding_names);
|
||||
hash_expression(&cond.consequent, h, include_binding_names);
|
||||
hash_expression(&cond.alternate, h, include_binding_names);
|
||||
}
|
||||
Expression::PrivateInExpression(pin) => {
|
||||
pin.left.name.as_str().hash(h);
|
||||
hash_expression(&pin.right, h);
|
||||
hash_expression(&pin.right, h, include_binding_names);
|
||||
}
|
||||
Expression::Identifier(ident) => {
|
||||
if include_binding_names {
|
||||
ident.name.as_str().hash(h);
|
||||
}
|
||||
}
|
||||
Expression::Identifier(_) => {}
|
||||
Expression::NumericLiteral(num) => {
|
||||
num.value.to_bits().hash(h);
|
||||
}
|
||||
@@ -267,39 +308,52 @@ fn hash_expression(expr: &Expression, h: &mut rustc_hash::FxHasher) {
|
||||
Expression::AssignmentExpression(assign) => {
|
||||
std::mem::discriminant(&assign.operator).hash(h);
|
||||
if let Some(target) = assign.left.as_simple_assignment_target() {
|
||||
hash_simple_assignment_target(target, h);
|
||||
hash_simple_assignment_target(target, h, include_binding_names);
|
||||
}
|
||||
hash_expression(&assign.right, h);
|
||||
hash_expression(&assign.right, h, include_binding_names);
|
||||
}
|
||||
Expression::UpdateExpression(update) => {
|
||||
std::mem::discriminant(&update.operator).hash(h);
|
||||
update.prefix.hash(h);
|
||||
hash_simple_assignment_target(&update.argument, h);
|
||||
hash_simple_assignment_target(&update.argument, h, include_binding_names);
|
||||
}
|
||||
Expression::TemplateLiteral(tl) => {
|
||||
(tl.quasis.len() as u32).hash(h);
|
||||
for expr in &tl.expressions {
|
||||
hash_expression(expr, h);
|
||||
hash_expression(expr, h, include_binding_names);
|
||||
}
|
||||
}
|
||||
Expression::TaggedTemplateExpression(tt) => {
|
||||
hash_expression(&tt.tag, h);
|
||||
hash_expression(&tt.tag, h, include_binding_names);
|
||||
(tt.quasi.quasis.len() as u32).hash(h);
|
||||
for expr in &tt.quasi.expressions {
|
||||
hash_expression(expr, h);
|
||||
hash_expression(expr, h, include_binding_names);
|
||||
}
|
||||
}
|
||||
Expression::ArrowFunctionExpression(arrow) => {
|
||||
(arrow.params.items.len() as u32).hash(h);
|
||||
if include_binding_names {
|
||||
for param in &arrow.params.items {
|
||||
hash_binding_pattern(¶m.pattern, h);
|
||||
}
|
||||
}
|
||||
for s in &arrow.body.statements {
|
||||
hash_statement(s, h);
|
||||
hash_statement(s, h, include_binding_names);
|
||||
}
|
||||
}
|
||||
Expression::FunctionExpression(fe) => {
|
||||
(fe.params.items.len() as u32).hash(h);
|
||||
if include_binding_names {
|
||||
if let Some(id) = &fe.id {
|
||||
id.name.as_str().hash(h);
|
||||
}
|
||||
for param in &fe.params.items {
|
||||
hash_binding_pattern(¶m.pattern, h);
|
||||
}
|
||||
}
|
||||
if let Some(body) = &fe.body {
|
||||
for s in &body.statements {
|
||||
hash_statement(s, h);
|
||||
hash_statement(s, h, include_binding_names);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -309,29 +363,29 @@ fn hash_expression(expr: &Expression, h: &mut rustc_hash::FxHasher) {
|
||||
}
|
||||
class.super_class.is_some().hash(h);
|
||||
if let Some(super_class) = &class.super_class {
|
||||
hash_expression(super_class, h);
|
||||
hash_expression(super_class, h, include_binding_names);
|
||||
}
|
||||
for element in &class.body.body {
|
||||
hash_class_element(element, h);
|
||||
hash_class_element(element, h, include_binding_names);
|
||||
}
|
||||
}
|
||||
Expression::ObjectExpression(obj) => {
|
||||
(obj.properties.len() as u32).hash(h);
|
||||
for prop in &obj.properties {
|
||||
hash_object_property_kind(prop, h);
|
||||
hash_object_property_kind(prop, h, include_binding_names);
|
||||
}
|
||||
}
|
||||
Expression::NewExpression(ne) => {
|
||||
hash_expression(&ne.callee, h);
|
||||
hash_expression(&ne.callee, h, include_binding_names);
|
||||
(ne.arguments.len() as u32).hash(h);
|
||||
for arg in &ne.arguments {
|
||||
if let Some(expr) = arg.as_expression() {
|
||||
hash_expression(expr, h);
|
||||
hash_expression(expr, h, include_binding_names);
|
||||
}
|
||||
}
|
||||
}
|
||||
Expression::PrivateFieldExpression(member) => {
|
||||
hash_expression(&member.object, h);
|
||||
hash_expression(&member.object, h, include_binding_names);
|
||||
member.field.name.as_str().hash(h);
|
||||
}
|
||||
Expression::Super(_) => {}
|
||||
@@ -344,31 +398,37 @@ fn hash_expression(expr: &Expression, h: &mut rustc_hash::FxHasher) {
|
||||
});
|
||||
}
|
||||
|
||||
fn hash_object_property_kind(prop: &ObjectPropertyKind, h: &mut rustc_hash::FxHasher) {
|
||||
fn hash_object_property_kind(prop: &ObjectPropertyKind, h: &mut rustc_hash::FxHasher, include_binding_names: bool) {
|
||||
hash_match!(HashDomain::ObjectPropertyKind, prop, h, {
|
||||
ObjectPropertyKind::ObjectProperty(p) => {
|
||||
std::mem::discriminant(&p.kind).hash(h);
|
||||
p.method.hash(h);
|
||||
p.computed.hash(h);
|
||||
hash_property_key(&p.key, h);
|
||||
hash_expression(&p.value, h);
|
||||
hash_property_key(&p.key, h, include_binding_names);
|
||||
hash_expression(&p.value, h, include_binding_names);
|
||||
}
|
||||
ObjectPropertyKind::SpreadProperty(_) => {}
|
||||
});
|
||||
}
|
||||
|
||||
fn hash_simple_assignment_target(target: &SimpleAssignmentTarget, h: &mut rustc_hash::FxHasher) {
|
||||
fn hash_simple_assignment_target(
|
||||
target: &SimpleAssignmentTarget, h: &mut rustc_hash::FxHasher, include_binding_names: bool,
|
||||
) {
|
||||
hash_match!(HashDomain::SimpleAssignmentTarget, target, h, {
|
||||
SimpleAssignmentTarget::AssignmentTargetIdentifier(_) => {}
|
||||
SimpleAssignmentTarget::AssignmentTargetIdentifier(ident) => {
|
||||
if include_binding_names {
|
||||
ident.name.as_str().hash(h);
|
||||
}
|
||||
}
|
||||
SimpleAssignmentTarget::StaticMemberExpression(member) => {
|
||||
hash_expression(&member.object, h);
|
||||
hash_expression(&member.object, h, include_binding_names);
|
||||
}
|
||||
SimpleAssignmentTarget::ComputedMemberExpression(member) => {
|
||||
hash_expression(&member.object, h);
|
||||
hash_expression(&member.expression, h);
|
||||
hash_expression(&member.object, h, include_binding_names);
|
||||
hash_expression(&member.expression, h, include_binding_names);
|
||||
}
|
||||
SimpleAssignmentTarget::PrivateFieldExpression(member) => {
|
||||
hash_expression(&member.object, h);
|
||||
hash_expression(&member.object, h, include_binding_names);
|
||||
member.field.name.as_str().hash(h);
|
||||
}
|
||||
_ => {}
|
||||
|
||||
Reference in New Issue
Block a user