Files
Fig/src/Object/FunctionObject.hpp
T
PuqiAR 51a939ac45 对编译器和虚拟机进行重构,以支持闭包和垃圾回收功能
- 去除了不再使用的结构,并更新了编译器以处理新的闭包语义。
- 改进了 Compiler,使其能够生成带有源位置跟踪的指令。
- 在 FunctionObject 和 VM 中引入了作用域变量管理,以支持动态闭包。
- 实现了使用标记-扫描(Mark-And-Sweep) (Tri-Color tracing) 算法的垃圾回收机制,包括对作用域变量的处理。
- 在 VM 中增加了函数加载和作用域变量检索的支持。
- 更新了对象模型,包括引入 InstanceObject 并改进内存管理。
- 添加了用于调试的全局变量打印功能。
2026-03-11 16:53:10 +08:00

40 lines
1.2 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/*!
@file src/Object/FunctionObject.hpp
@brief 函数对象定义
*/
#pragma once
#include <Object/ObjectBase.hpp>
#include <Bytecode/Bytecode.hpp>
namespace Fig
{
// Upvalue (Stack Open / Heap Closed)
struct Upvalue
{
Value *location; // Open 状态指向 VM 的 registerBase[x]Closed 状态指向下面的 closedValue
Value closedValue; // 栈帧销毁时,数据物理迁移至此
Upvalue *next; // 侵入式链表,供 VM 追踪当前 Open 的 Upvalue
std::uint32_t refCount = 0; // 多少个闭包正在使用
};
struct FunctionObject final : public Object
{
String name;
Proto *proto; // 静态只读字节码
std::uint8_t paraCount;
std::uint32_t upvalueCount; // 捕获数量
// 柔性数组
Upvalue *upvalues[];
FunctionObject(const String &_name, Proto *_proto, std::uint32_t _upvalueCount) :
name(_name), proto(_proto), paraCount(_proto->numParams), upvalueCount(_upvalueCount)
{
type = ObjectType::Function;
}
~FunctionObject() = default;
};
} // namespace Fig