refactor(compiler): remove class count helpers

This commit is contained in:
爱特每羊队
2026-08-05 01:41:51 +08:00
parent fd4172203e
commit 0f78f341fa
12 changed files with 2 additions and 323 deletions
-10
View File
@@ -1,11 +1 @@
use crate::compiler::{CompileCtx, Compiler};
impl Compiler {
pub(crate) fn count_class(&self, class: &oxide_parser::Class, ctx: &mut CompileCtx) {
self.count_class_header(class, ctx);
self.count_class_prototype(class.super_class.is_some(), ctx);
self.count_class_methods(&class.body.body, ctx);
self.count_class_static_fields(&class.body.body, ctx);
self.count_class_static_blocks(&class.body.body, ctx);
}
}
+2 -28
View File
@@ -66,20 +66,7 @@ impl Compiler {
false,
&self_binding,
FunctionBodyContext::ClassElement,
Some(|compiler: &Compiler, field_ctx: &mut CompileCtx| {
for field in &instance_fields {
if matches!(field.key, PropertyKey::PrivateIdentifier(_)) {
compiler.count_private_field_init(field.value.as_ref(), field_ctx);
} else {
compiler.count_public_field_init(
&field.key,
field.computed,
field.value.as_ref(),
field_ctx,
);
}
}
}),
None::<fn(&Compiler, &mut CompileCtx)>,
Some(|compiler: &Compiler, field_ctx: &mut CompileCtx| -> Result<(), String> {
for field in &instance_fields {
if let PropertyKey::PrivateIdentifier(private) = &field.key {
@@ -111,20 +98,7 @@ impl Compiler {
false,
&self_binding,
FunctionBodyContext::ClassElement,
Some(|compiler: &Compiler, field_ctx: &mut CompileCtx| {
for field in &instance_fields {
if matches!(field.key, PropertyKey::PrivateIdentifier(_)) {
compiler.count_private_field_init(field.value.as_ref(), field_ctx);
} else {
compiler.count_public_field_init(
&field.key,
field.computed,
field.value.as_ref(),
field_ctx,
);
}
}
}),
None::<fn(&Compiler, &mut CompileCtx)>,
Some(|compiler: &Compiler, field_ctx: &mut CompileCtx| -> Result<(), String> {
for field in &instance_fields {
if let PropertyKey::PrivateIdentifier(private) = &field.key {
-17
View File
@@ -2,23 +2,6 @@ use crate::compiler::{CompileCtx, Compiler};
use oxide_parser::{ClassElement, PropertyKey};
impl Compiler {
pub(crate) fn count_class_static_fields(&self, elements: &[ClassElement], ctx: &mut CompileCtx) {
for element in elements {
if let ClassElement::PropertyDefinition(prop) = element {
let prop = prop.as_ref();
if prop.r#static {
self.count_class_key(&prop.key, prop.computed, ctx);
if let Some(value) = &prop.value {
self.count_expression(value, ctx);
} else {
ctx.count_load_const();
}
ctx.count_instr();
}
}
}
}
pub(crate) fn emit_class_static_fields(
&self, elements: &[ClassElement], ctor_reg: u8, ctx: &mut CompileCtx,
) -> Result<(), String> {
@@ -1,14 +1,6 @@
use crate::compiler::{CompileCtx, Compiler};
impl Compiler {
pub(crate) fn count_class_header(&self, class: &oxide_parser::Class, ctx: &mut CompileCtx) {
ctx.alloc_reg();
ctx.alloc_reg();
if let Some(super_class) = &class.super_class {
self.count_expression(super_class, ctx);
}
}
pub(crate) fn emit_class_header(
&self, class: &oxide_parser::Class, ctx: &mut CompileCtx,
) -> Result<(u8, u8, Option<u8>), String> {
-34
View File
@@ -4,40 +4,6 @@ use oxide_bytecode::opcode::{self, OpCode};
use oxide_parser::{ClassElement, MethodDefinitionKind, PropertyKey};
impl Compiler {
pub(crate) fn count_class_methods(&self, elements: &[ClassElement], ctx: &mut CompileCtx) {
for element in elements {
match element {
ClassElement::MethodDefinition(method) => {
let method = method.as_ref();
if matches!(method.kind, MethodDefinitionKind::Constructor) {
continue;
}
if matches!(method.key, PropertyKey::PrivateIdentifier(_)) {
ctx.count_load_const();
ctx.count_load_const();
ctx.count_instr();
ctx.count_instr();
continue;
}
self.count_class_key(&method.key, method.computed, ctx);
ctx.count_load_const();
ctx.count_instr();
match method.kind {
MethodDefinitionKind::Method => {
ctx.count_instr();
}
MethodDefinitionKind::Get | MethodDefinitionKind::Set => {
ctx.count_load_const();
ctx.count_define_accessor();
}
MethodDefinitionKind::Constructor => {}
}
}
_ => {}
}
}
}
pub(crate) fn emit_class_methods(
&self, elements: &[ClassElement], ctor_reg: u8, proto_reg: u8, self_binding: &[(&str, u8)],
ctx: &mut CompileCtx,
@@ -3,25 +3,6 @@ use oxide_bytecode::module::Constant;
use oxide_bytecode::opcode::{self, OpCode};
impl Compiler {
pub(crate) fn count_class_prototype(&self, has_super: bool, ctx: &mut CompileCtx) {
ctx.count_instr();
ctx.count_instr();
if has_super {
ctx.alloc_reg();
ctx.count_instr();
ctx.alloc_reg();
ctx.count_instr();
ctx.alloc_reg();
ctx.count_instr();
ctx.count_instr();
ctx.count_instr();
}
ctx.count_load_const();
ctx.count_instr();
ctx.count_load_const();
ctx.count_instr();
}
pub(crate) fn emit_class_prototype(
&self, ctor_reg: u8, proto_reg: u8, super_reg: Option<u8>, sub_idx: u32, ctx: &mut CompileCtx,
) -> Result<(), String> {
@@ -2,16 +2,6 @@ use crate::compiler::{CompileCtx, Compiler};
use oxide_parser::ClassElement;
impl Compiler {
pub(crate) fn count_class_static_blocks(&self, elements: &[ClassElement], ctx: &mut CompileCtx) {
for element in elements {
if let ClassElement::StaticBlock(block) = element {
for stmt in &block.body {
self.count_statement(stmt, ctx);
}
}
}
}
pub(crate) fn emit_class_static_blocks(
&self, elements: &[ClassElement], ctor_reg: u8, ctx: &mut CompileCtx,
) -> Result<(), String> {