Smart pascal source code
type
TProc = procedure;
type
TMyClass = class
class procedure SayHello; virtual;
begin
WriteLn('Hello ' + ClassName);
end;
class procedure Hello;
begin
SayHello;
end;
end;
type
TMyChild = class(TMyClass)
class procedure SayHello; override;
begin
WriteLn('Bye bye ' + ClassName);
end;
end;
var p: TProc;
p := TMyClass.Hello;
p();
p := TMyChild.Hello;
p();
var
o1 := TMyClass.Create;
p := o1.Hello;
p();
var
o2 := TMyChild.Create;
p := o2.Hello;
p();
{<<< RESULT - CONSOLE LOG >>>
-----------------------------
Hello TMyClass
Bye bye TMyChild
Hello TMyClass
Bye bye TMyChild
-----------------------------
{<<<<<<<<< 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 $Event0(i,f) {
var li=i,lf=f;
return function() {
return lf.call(li,li)
}
}
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($);
}
,SayHello:function(Self) {
WriteLn(("Hello "+TObject.ClassName($Check(Self,""))));
}
,Hello:function(Self) {
TMyClass.SayHello$($Check(Self,""));
}
,Destroy:TObject.Destroy
,SayHello$:function($){return $.SayHello($)}
};
var TMyChild = {
$ClassName:"TMyChild",$Parent:TMyClass
,$Init:function ($) {
TMyClass.$Init($);
}
,SayHello:function(Self) {
WriteLn(("Bye bye "+TObject.ClassName($Check(Self,""))));
}
,Destroy:TObject.Destroy
,SayHello$:function($){return $.SayHello($)}
};
var p = null;
var o1 = null,
o2 = null;
/* <<< main JS >>> */
p = $Event0(TMyClass,TMyClass.Hello);
$CheckFunc(p,"")();
p = $Event0(TMyChild,TMyClass.Hello);
$CheckFunc(p,"")();
o1 = TObject.Create($New(TMyClass));
p = $Event0(o1.ClassType,TMyClass.Hello);
$CheckFunc(p,"")();
o2 = TObject.Create($New(TMyChild));
p = $Event0(o2.ClassType,TMyClass.Hello);
$CheckFunc(p,"")();