Function Pointer example 05
|
Pas2JS pascal source code
type
TMyFunc = function(i: Integer): String;
type
TMyClass = class
FOffset: Integer;
class function Conv(i: Integer): String;
function DoConv(i: Integer): String;
end;
class function TMyClass.Conv(i: Integer): String;
begin
Result := '<' + IntToStr(i) + '> ' + ClassName;
end;
function TMyClass.DoConv(i: Integer): String;
begin
Result := '[' + IntToStr(i + FOffset) + ']';
end;
function MyIntToStr(i: Integer): String;
begin
Result := '*' + IntToStr(i) + '*';
end;
procedure WriteLnIt(f: TMyFunc; arg: Integer);
begin
WriteLn(f(arg));
end;
var
o := TMyClass.Create;
o.FOffset := 5000;
WriteLnIt(IntToStr, 123);
WriteLnIt(MyIntToStr, 123);
WriteLnIt(TMyClass.Conv, 123);
WriteLnIt(o.DoConv, 123);
var ff: TMyFunc;
ff := IntToStr;
WriteLnIt(ff, 456);
ff := MyIntToStr;
WriteLnIt(ff, 456);
ff := TMyClass.Conv;
WriteLnIt(ff, 456);
ff := o.DoConv;
WriteLnIt(ff, 456);
{<<< RESULT - CONSOLE LOG >>>
-----------------------------
123
*123*
<123> TMyClass
[5123]
456
*456*
<456> TMyClass
[5456]
-----------------------------
{<<<<<<<<< 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) }
}
function IntToStr(i) { return i.toString() }
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 $Event1(i,f) {
var li=i,lf=f;
return function(a) {
return lf.call(li,li,a)
}
}
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 WriteLnIt(f, arg) {
WriteLn($CheckFunc(f,"")(arg));
};
var TMyClass = {
$ClassName:"TMyClass",$Parent:TObject
,$Init:function ($) {
TObject.$Init($);
$.FOffset = 0;
}
,Conv:function(Self, i) {
return "<"+i.toString()+"> "+TObject.ClassName($Check(Self,""));
}
,DoConv:function(Self, i$1) {
return "["+(i$1+$Check(Self,"").FOffset).toString()+"]";
}
,Destroy:TObject.Destroy
};
function MyIntToStr(i$2) {
return "*"+i$2.toString()+"*";
};
var o = null,
ff = null;
/* <<< main JS >>> */
o = TObject.Create($New(TMyClass));
$Check(o,"").FOffset = 5000;
WriteLnIt(IntToStr,123);
WriteLnIt(MyIntToStr,123);
WriteLnIt($Event1(TMyClass,TMyClass.Conv),123);
WriteLnIt($Event1(o,TMyClass.DoConv),123);
ff = IntToStr;
WriteLnIt(ff,456);
ff = MyIntToStr;
WriteLnIt(ff,456);
ff = $Event1(TMyClass,TMyClass.Conv);
WriteLnIt(ff,456);
ff = $Event1(o,TMyClass.DoConv);
WriteLnIt(ff,456);