Smart pascal source code
type
TMyProcStr = procedure(s: String);
type
TMyProcInts = procedure(i1, i2: Integer);
procedure TestS1(s: String);
begin
WriteLn('Hello ' + s)
end;
procedure TestS2(str: String);
begin
WriteLn('Bye bye ' + str)
end;
procedure TestI1(i1, i2: Integer);
begin
WriteLn(i1 + i2);
end;
procedure TestI2(i1, i2: Integer);
begin
WriteLn(i1 - i2);
end;
var ps: TMyProcStr;
var pis: TMyProcInts;
procedure RunPtrs;
begin
ps('Run');
pis(20, 3);
end;
ps := TestS1;
pis := TestI1;
ps('World');
pis(10, 5);
RunPtrs;
ps := TestS2;
pis := TestI2;
ps('World');
pis(10, 5);
RunPtrs;
{<<< RESULT - CONSOLE LOG >>>
-----------------------------
Hello World
15
Hello Run
23
Bye bye World
5
Bye bye Run
17
-----------------------------
{<<<<<<<<< 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 $CheckFunc(i,z) { if (i) return i; throw Exception.Create($New(Exception),"Function pointer is nil"+z); }
function WriteLn(value) {
if (window.console) { window.console.log(value); } };
function TestS2(str) {
WriteLn(("Bye bye "+str));
};
function TestS1(s) {
WriteLn(("Hello "+s));
};
function TestI2(i1, i2) {
WriteLn((i1-i2));
};
function TestI1(i1$1, i2$1) {
WriteLn((i1$1+i2$1));
};
function RunPtrs() {
$CheckFunc(ps,"")("Run");
$CheckFunc(pis,"")(20,3);
};
var ps = null;
var pis = null;
/* <<< main JS >>> */
ps = TestS1;
pis = TestI1;
$CheckFunc(ps,"")("World");
$CheckFunc(pis,"")(10,5);
RunPtrs();
ps = TestS2;
pis = TestI2;
$CheckFunc(ps,"")("World");
$CheckFunc(pis,"")(10,5);
RunPtrs();