Smart pascal source code
type
TMyFunc = procedure(v: Variant);
procedure WriteLn2(str: Variant);
begin
asm if (window.console) { window.console.log(@str); }; end;
end;
var f: TMyFunc := WriteLn2;
f('hello ');
f := WriteLn2;
f('world');
function MyWriteLn(b: Boolean): TMyFunc;
begin
if b then
Result := WriteLn2
else
Result := WriteLn2;
end;
MyWriteLn(True)('Line Feed');
MyWriteLn(False)('No');
MyWriteLn(False)('Line Feed');
var a: array [0 .. 1] of TMyFunc;
a[0] := WriteLn2;
a[1] := WriteLn2;
a[1]('!');
a[0]('No');
a[0]('Line Feed');
a[1]('Line Feed');
{<<< RESULT - CONSOLE LOG >>>
-----------------------------
hello world
Line Feed
NoLine Feed!
NoLine FeedLine Feed
-----------------------------
{<<<<<<<<< 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 WriteLn2(str) {
if (window.console) { window.console.log(str); }; };
function MyWriteLn(b) {
var Result = null;
if (b) {
Result = WriteLn2;
} else {
Result = WriteLn2;
}
return Result
};
var f = null,
a = [null,null];
/* MAIN JS CODE */
f = WriteLn2;
$CheckFunc(f,"")("hello ");
f = WriteLn2;
$CheckFunc(f,"")("world");
$CheckFunc(MyWriteLn(true),"")("Line Feed");
$CheckFunc(MyWriteLn(false),"")("No");
$CheckFunc(MyWriteLn(false),"")("Line Feed");
a[0] = WriteLn2;
a[1] = WriteLn2;
$CheckFunc(a[1],"")("!");
$CheckFunc(a[0],"")("No");
$CheckFunc(a[0],"")("Line Feed");
$CheckFunc(a[1],"")("Line Feed");