Smart pascal source code
type
TStringProc = procedure(s: String);
procedure Test(s: String; f: TStringProc = nil);
begin
if Assigned(f) then
f(s)
else
WriteLn(s);
end;
procedure WriteLnWrap(s: String);
begin
WriteLn('wrap: ');
WriteLn(s);
end;
Test('Hello');
Test('World', nil);
Test('Byebye', WriteLnWrap);
Test('World', @WriteLnWrap);
var
p := @WriteLnWrap;
Test('Hello', p);
p := nil;
Test('Again', p);
{<<< RESULT - CONSOLE LOG >>>
-----------------------------
Hello
World
wrap: Byebye
wrap: World
wrap: Hello
Again
-----------------------------
{<<<<<<<<< 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 WriteLnWrap(s) {
WriteLn("wrap: ");
WriteLn(s);
};
function Test(s$1, f) {
if (f) {
$CheckFunc(f,"")(s$1);
} else {
WriteLn(s$1);
}
};
var p = null;
/* <<< main JS >>> */
Test("Hello",null);
Test("World",null);
Test("Byebye",WriteLnWrap);
Test("World",WriteLnWrap);
p = WriteLnWrap;
Test("Hello",p);
p = null;
Test("Again",p);