Smart pascal source code
type
TVarProc = procedure(sender: TObject; var s: String);
var proc: TVarProc;
procedure Stuff(sender: TObject; var s: String);
begin
s := sender.ClassName;
end;
proc := Stuff;
var s: String := 'hello';
var obj: TObject := TObject.Create;
WriteLn(s);
proc(obj, s);
WriteLn(s);
type
TMyClass = class
procedure Stuff(sender: TObject; var s: String);
begin
s := sender.ClassName + '!';
end;
end;
proc := TMyClass.Create.Stuff;
proc(obj, s);
WriteLn(s);
proc(TMyClass.Create, s);
WriteLn(s);
{<<< RESULT - CONSOLE LOG >>>
-----------------------------
hello
TObject
TObject!
TMyClass!
-----------------------------
{<<<<<<<<< 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 $ToClassType(o) {
if (o===null) return o;
return o.ClassType
}
;
function $New(c) { var i={ClassType:c}; c.$Init(i); return i }
function $Event2(i,f) {
var li=i,lf=f;
return function(a,b) {
return lf.call(li,li,a,b)
}
}
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); } };
function ClassName$1(Self$1) {
return "Helper1."+TObject.ClassName($Check(Self$1,""));
}
var TMyClass = {
$ClassName:"TMyClass",$Parent:TObject
,$Init:function ($) {
TObject.$Init($);
}
,Stuff:function(Self, sender, s$1) {
s$1.v = ClassName$1($ToClassType(sender))+"!";
}
,Destroy:TObject.Destroy
};
function Stuff$1(sender$1, s$2) {
s$2.v = ClassName$1($ToClassType(sender$1));
};
var proc = null;
var s = {v:""},
obj = null;
/* <<< main JS >>> */
proc = Stuff$1;
s.v = "hello";
obj = TObject.Create($New(TObject));
WriteLn(s.v);
$CheckFunc(proc,"")(obj,s);
WriteLn(s.v);
proc = $Event2(TObject.Create($New(TMyClass)),TMyClass.Stuff);
$CheckFunc(proc,"")(obj,s);
WriteLn(s.v);
$CheckFunc(proc,"")(TObject.Create($New(TMyClass)),s);
WriteLn(s.v);