我们可以在Delphi的System unit中找到is, as的实现方法:
对于下面的语句:
MyForm is TForm
procedure _IsClass;
asm
{ -> EAX left operand (class) }//MyForm
{ EDX VMT of right operand } //右操作数所代表的类的VMT表, 即TForm的VMT
{ <- AL left is derived from right }
TEST EAX,EAX
JE @@exit
@@loop:
MOV EAX,[EAX] //得到MyForm的VMT
CMP EAX,EDX
JE @@success //if MyForm的VMT和TForm的VMT是同一个地址,就返回True
MOV EAX,[EAX].vmtParent //else
就取MyForm的parent
TEST EAX,EAX
JNE @@loop //循环和TForm的VMT比较,
JMP @@exit
@@success:
MOV AL,1
@@exit:
end;
As的处理方法大同小异
procedure _AsClass;
asm
{ -> EAX left operand (class) }
{ EDX VMT of right operand }
{ <- EAX if left is derived from right, else
runtime error }
TEST EAX,EAX
JE @@exit
MOV ECX,EAX
@@loop:
MOV ECX,[ECX]
CMP ECX,EDX
JE @@exit
MOV ECX,[ECX].vmtParent
TEST ECX,ECX
JNE @@loop
{ do
runtime error }
MOV AL,reInvalidCast
JMP Error
@@exit:
end;