Smart pascal source code
type
TMyObj = class
Field : Integer;
procedure Proc;
procedure ProcVirtual; virtual;
end;
procedure TMyObj.Proc;
begin
WriteLn('Proc');
WriteLn(Field);
end;
procedure TMyObj.ProcVirtual;
begin
WriteLn('ProcVirtual');
WriteLn(Field);
end;
{ unit1.pas }
var o : TMyObj;
Begin
if Assigned(o) then
WriteLn('bug');
o := TMyObj.Create;
if Assigned(o) then
WriteLn('Assigned'); // Assigned
o.Field:=1234;
o.Proc; // 1234
o.ProcVirtual; // ProcVirtual
o:=nil;
if not Assigned(o) then
WriteLn('not Assigned'); // not Assigned
try
o.Field:=456;
except
on E : Exception do
WriteLn(E.Message); // TypeError, Cannot set property 'Field' of null
end;
try
o.Proc;
except
on E : Exception do WriteLn(E.Message);
end;
try
o.ProcVirtual;
except
on E : Exception do
WriteLn(E.Message); // TypeError, Cannot read property 'Field' of null
end;
var Exception={
$ClassName: "Exception",
$Parent: TObject,
$Init: function () { FMessage="" },
Create: function (s,Msg) { s.FMessage=Msg; return s }
}
function $W(e) { return e.ClassType?e:Exception.Create($New(Exception),e.constructor.name+", "+e.message) }
function $New(c) { var i={ClassType:c}; c.$Init(i); return i }
function $Is(o,c) {
if (o===null) return false;
return $Inh(o.ClassType,c);
}
;
function $Inh(s,c) {
if (s===null) return false;
while ((s)&&(s!==c)) s=s.$Parent;
return (s)?true:false;
}
;
var o = null;
if (o) {
WriteLn("bug");
}
o = TObject.Create($New(TMyObj));
if (o) {
WriteLn("Assigned");
}
o.Field = 1234;
THelper$proc(o);
TMyObj.ProcVirtual(o);
o = null;
if (!o) {
WriteLn("not Assigned");
}
try {
o.Field = 456;
} catch ($e) {
var E = $W($e);
WriteLn(E.FMessage) }
try {
THelper$proc(o);
} catch ($e) {
var E$1 = $W($e);
WriteLn(E$1.FMessage) }
try {
TMyObj.ProcVirtual(o);
} catch ($e) {
var E$2 = $W($e);
WriteLn(E$2.FMessage) }
var TMyObj = {
$ClassName:"TMyObj",$Parent:TObject
,$Init:function ($) {
TObject.$Init($);
$.Field = 0;
}
/// procedure TMyObj.ProcVirtual()
/// [line: 85, column: 18, file: uMain]
,ProcVirtual:function(Self) {
WriteLn("ProcVirtual");
WriteLn(Self.Field);
}
,Destroy:TObject.Destroy
};