Smart pascal source code
type
TMyFunc = function(i: Integer): String;
type
TMyClass = class
FEvent: TMyFunc;
function DoConv(i: Integer): String;
end;
function TMyClass.DoConv(i: Integer): String;
begin
Result := '[' + IntToStr(i) + ']';
end;
var
o := TMyClass.Create;
if Assigned(o.FEvent) then
WriteLn(o.FEvent(1))
else
WriteLn('Unassigned');
o.FEvent := o.DoConv;
if Assigned(o.FEvent) then
WriteLn(o.FEvent(2));
var e: TMyFunc;
if Assigned(e) then
WriteLn(e(3))
else
WriteLn('Unassigned');
e := o.DoConv;
if Assigned(e) then
WriteLn(e(4));
e := nil;
o.FEvent := e;
if Assigned(e) then
WriteLn(e(5))
else
WriteLn('Unassigned');
{<<< RESULT - CONSOLE LOG >>>
-----------------------------
Unassigned
[2]
Unassigned
[4]
Unassigned
-----------------------------
{<<<<<<<<< THE END >>>>>>>>>}
var TObject={
$ClassName: "TObject",
$Parent: null,
ClassName: function (s) { return s.$ClassName },
ClassType: function (s) { return s },
ClassParent: function (s) { return s.$Parent },
$Init: function () {},
Create: function (s) { return s },
Destroy: function (s) { for (var prop in s) if (s.hasOwnProperty(prop)) delete s.prop },
Destroy$: function(s) { return s.ClassType.Destroy(s) },
Free: function (s) { if (s!==null) s.ClassType.Destroy(s) }
}
var Exception={
$ClassName: "Exception",
$Parent: TObject,
$Init: function () { FMessage="" },
Create: function (s,Msg) { s.FMessage=Msg; return s }
}
function $New(c) { var i={ClassType:c}; c.$Init(i); return i }
function $Event1(i,f) {
var li=i,lf=f;
return function(a) {
return lf.call(li,li,a)
}
}
function $CheckFunc(i,z) { if (i) return i; throw Exception.Create($New(Exception),"Function pointer is nil"+z); }
function $Check(i,z) { if (i) return i; throw Exception.Create($New(Exception),"Object not instantiated"+z); }
function WriteLn(value) {
if (window.console) { window.console.log(value); } };
var TMyClass = {
$ClassName:"TMyClass",$Parent:TObject
,$Init:function ($) {
TObject.$Init($);
$.FEvent = null;
}
,DoConv:function(Self, i) {
return "["+i.toString()+"]";
}
,Destroy:TObject.Destroy
};
var o = null,
e = null;
/* <<< main JS >>> */
o = TObject.Create($New(TMyClass));
if ($Check(o,"").FEvent) {
WriteLn($CheckFunc($Check(o,"").FEvent,"")(1));
} else {
WriteLn("Unassigned");
}
$Check(o,"").FEvent = $Event1(o,TMyClass.DoConv);
if ($Check(o,"").FEvent) {
WriteLn($CheckFunc($Check(o,"").FEvent,"")(2));
}
if (e) {
WriteLn($CheckFunc(e,"")(3));
} else {
WriteLn("Unassigned");
}
e = $Event1(o,TMyClass.DoConv);
if (e) {
WriteLn($CheckFunc(e,"")(4));
}
e = null;
$Check(o,"").FEvent = e;
if (e) {
WriteLn($CheckFunc(e,"")(5));
} else {
WriteLn("Unassigned");
}