level 1
假设类名为 TFoo (type TFoo = object ...),指向其的指针类型为 PFoo ,
构造函数的参数是 declared parameters ,函数体为 declared body
则它会被转译成这种形式(伪代码)
function Create(this : PFoo;
vmt : Pointer;
{ declared parameters }
) : PFoo;
// local variables
begin
this:=PFoo(fpc_help_constructor(this, vmt, OFFSETOF(TFoo, __VPTR)));
if this=nil then
exit(this);
try
{ declared body }
except
fpc_help_fail(this, vmt, OFFSETOF(TFoo, __VPTR));
raise;
end;
exit(this);
end;
其中 vmt 是虚表地址,若无虚表则为 nil 。
OFFSETOF(TFoo, __VPTR) 为虚表指针在对象中的偏移,若无虚表则为 0 。
fpc_help_constructor 为编译器提供的函数,功能有二
1) 若传入的 this 是 nil ,则在堆上分配空间(使用 GetMem),并做一下记录
2) 若传入的 this 不是 nil 或分配完成后,将对象全部字节设为 $00 ,然后若有虚表则再设置虚表;
fpc_help_fail 为编译器提供的过程。功能是若构造过程中抛出异常,且之前记录了对象分配在堆上,则将对象释放。
x.Create(params) 被转译成 Create(@x, VMTOF(TFoo), OFFSETOF(TFoo, __VPTR), params)
new(ptr, Create(params)) 被转译成 ptr:=Create(nil, VMTOF(TFoo), OFFSETOF(TFoo, __VPTR), params)
2017年04月01日 02点04分