mirror of
https://github.com/pjh456/OxideJS.git
synced 2026-10-27 12:11:12 +08:00
refactor: 函数体编译链收进 FunctionFlags 与 ClassElementHooks 结构体
This commit is contained in:
@@ -2,7 +2,9 @@
|
||||
//!
|
||||
//! 函数:`emit_class`。
|
||||
|
||||
use crate::{symbol_table::ScopeKind, CompileCtx, Emitter, FunctionBodyContext};
|
||||
use crate::{
|
||||
symbol_table::ScopeKind, ClassElementHooks, CompileCtx, Emitter, FunctionBodyContext, FunctionFlags,
|
||||
};
|
||||
use oxide_bytecode::module::Constant;
|
||||
use oxide_bytecode::opcode::OpCode;
|
||||
use oxide_ir::inst::Inst;
|
||||
@@ -270,36 +272,40 @@ impl Emitter {
|
||||
Ok(())
|
||||
};
|
||||
|
||||
// 构造器(显式与隐式派生)共用同一组标志与钩子:恒 strict、无表达式体、
|
||||
// 字段发射钩子按派生与否在 super() 前后执行。
|
||||
let ctor_flags = FunctionFlags {
|
||||
is_expression_body: false,
|
||||
is_generator: false,
|
||||
is_async: false,
|
||||
own_strict: true,
|
||||
name: None,
|
||||
};
|
||||
let ctor_hooks = ClassElementHooks {
|
||||
extra_bindings: &self_binding,
|
||||
emit_fields: Some(emit_instance_fields),
|
||||
fields_after_super: is_derived,
|
||||
extra_capture_exprs: &field_value_exprs,
|
||||
extra_upvalue_names: &extra_upvalue_names,
|
||||
};
|
||||
let mut ctor_module = if let Some(method) = constructor_method {
|
||||
let (param_names, body_stmts) = self.extract_function_parts(method.value.as_ref())?;
|
||||
self.compile_function_body_with_field_hooks(
|
||||
self.compile_function_body(
|
||||
¶m_names,
|
||||
body_stmts,
|
||||
ctx,
|
||||
false,
|
||||
&self_binding,
|
||||
FunctionBodyContext::ClassElement,
|
||||
Some(&emit_instance_fields),
|
||||
is_derived,
|
||||
&field_value_exprs,
|
||||
&extra_upvalue_names,
|
||||
true,
|
||||
None,
|
||||
ctor_flags,
|
||||
ctor_hooks,
|
||||
)?
|
||||
} else {
|
||||
let mut module = self.compile_function_body_with_field_hooks(
|
||||
let mut module = self.compile_function_body(
|
||||
&[],
|
||||
&[],
|
||||
ctx,
|
||||
false,
|
||||
&self_binding,
|
||||
FunctionBodyContext::ClassElement,
|
||||
Some(&emit_instance_fields),
|
||||
is_derived,
|
||||
&field_value_exprs,
|
||||
&extra_upvalue_names,
|
||||
true,
|
||||
None,
|
||||
ctor_flags,
|
||||
ctor_hooks,
|
||||
)?;
|
||||
if is_derived {
|
||||
module.insts.clear();
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
//! 类方法 emit:`emit_class_methods` 逐方法构造,`emit_class_method_function`
|
||||
//! 负责单个方法函数体(含 `super` 与 home object)。
|
||||
|
||||
use crate::{CompileCtx, Emitter, FunctionBodyContext};
|
||||
use crate::{ClassElementHooks, CompileCtx, Emitter, FunctionBodyContext, FunctionFlags};
|
||||
use oxide_bytecode::module::Constant;
|
||||
use oxide_bytecode::opcode::OpCode;
|
||||
use oxide_ir::inst::Inst;
|
||||
@@ -125,21 +125,23 @@ impl Emitter {
|
||||
// 生成器/异步/异步生成器方法与普通方法统一走带标志入口:`*m` 置 is_generator,
|
||||
// `async *m` 同时置 is_generator 与 is_async,VM 据此按生成器协议创建对象并
|
||||
// 处理 body 内 SUSPEND/YIELD/异常边界。
|
||||
let mut method_module = self.compile_function_body_with_field_hooks_gen(
|
||||
// 方法无字段发射钩子:以空钩子为基底,注入类名自绑定与 brand upvalue。
|
||||
let mut hooks = ClassElementHooks::none();
|
||||
hooks.extra_bindings = self_binding;
|
||||
hooks.extra_upvalue_names = &extra_uv;
|
||||
let mut method_module = self.compile_function_body(
|
||||
¶m_names,
|
||||
body_stmts,
|
||||
ctx,
|
||||
false,
|
||||
self_binding,
|
||||
FunctionBodyContext::ClassElement,
|
||||
None::<fn(&Emitter, &mut CompileCtx) -> Result<(), String>>,
|
||||
false,
|
||||
&[],
|
||||
&extra_uv,
|
||||
method_value.generator,
|
||||
method_value.r#async,
|
||||
true,
|
||||
None,
|
||||
FunctionFlags {
|
||||
is_expression_body: false,
|
||||
is_generator: method_value.generator,
|
||||
is_async: method_value.r#async,
|
||||
own_strict: true,
|
||||
name: None,
|
||||
},
|
||||
hooks,
|
||||
)?;
|
||||
ctx.in_instance_method = saved_instance;
|
||||
ctx.in_static_method = saved_static;
|
||||
|
||||
Reference in New Issue
Block a user