[Pas2js] Pas2js 1.2.0RC1
warleyalex
warleyalex at yahoo.com.br
Mon Dec 17 20:16:06 CET 2018
I saw the sudden cloud of blue smoke from the pas2js - someone has
implemented the anonymous functions. I just can not believe, it worked!
It's so boring writing sub functions in pas2js :( to do simple things.
Note:
I uploaded the 1st release of Pas2js 1.2.0RC1 to test anonymous functions.
i) The nested setTimeout with anonymous functions work as expected in
pas2js!
ii) I have to add manually "pas2js_rtl" as Required Packages to compile the
project as expected.
If I remove "pas2js_rtl" as Required Packages, I have this output:
Compile Project, OS: browser, CPU: ecmascript5, Target: project1: Exit code
4, Errors: 8
Panic: Pas2JS Compiler version 1.2.0RC1 [2018/12/16] for Win32 i386
Panic: Info: (132) Message encoding is utf-8
Panic: Copyright (c) 2018 Free Pascal team.
Panic: Info: (126) Parsing D:\fpc2js\ex10\project1.lpr ...
Panic: Info: (126) Parsing D:\fpc2js\ex10\System.p ...
Panic: Fatal: (110) source file not found "D:\fpc2js\ex10\System.p"
Panic: Fatal: (118) Compilation aborted
Panic: tool stopped with exit code 4. Use context menu to get more
information.
iii) the required pas2js_rtl package does not compile out the box.
I had to edit the file node.js and comment some lines at the initialization
section:
initialization
NJS_OS:=TNJSOS(Require('os'));
//LineEnding:=NJS_OS.EOL;
//sLineBreak:=NJS_OS.EOL;
iv) I couldn't compile the 3 packages, it shows similar message:
a) "fcl_base_pas2js.lpk", it displays:
Package fcl_base_pas2js 1.0: Executing command before: Exit code 4, Errors:
5
Panic: Pas2JS Compiler version 1.2.0RC1 [2018/12/16] for Win32 i386
Panic: Copyright (c) 2018 Free Pascal team.
Panic: Fatal: (110) source file not found
"D:\fpc2js\pas2js-windows-1.2.0RC1\packages\fcl-db\System.p"
Panic: Fatal: (118) Compilation aborted
Panic: tool stopped with exit code 4. Use context menu to get more
information.
b) "pas2js_fcldb.lpk"
Package pas2js_fcldb 0.0: Executing command before: Exit code 4, Errors: 5
Panic: Pas2JS Compiler version 1.2.0RC1 [2018/12/16] for Win32 i386
Panic: Copyright (c) 2018 Free Pascal team.
Panic: Fatal: (110) source file not found
"D:\fpc2js\pas2js-windows-1.2.0RC1\packages\fpcunit\System.p"
Panic: Fatal: (118) Compilation aborted
Panic: tool stopped with exit code 4. Use context menu to get more
information.
c) "fpcunit_pas2js.lpk"
Package fpcunit_pas2js 1.0: Executing command before: Exit code 4, Errors: 5
Panic: Pas2JS Compiler version 1.2.0RC1 [2018/12/16] for Win32 i386
Panic: Copyright (c) 2018 Free Pascal team.
Panic: Fatal: (110) source file not found
"D:\fpc2js\pas2js-windows-1.2.0RC1\packages\widget\System.p"
Panic: Fatal: (118) Compilation aborted
Panic: tool stopped with exit code 4. Use context menu to get more
information.
iv) Code insight / code completion does not work with annonymous functions.
We use the keyword procedure without ";" to indicate annonymous functions
but the Lazarus code completion does undestand that.
Codetools, Errors: 1
project1.lpr(17,7) Error: unexpected keyword "procedure" in begin..end
found. start at (14,1)
-------------
I'll share you guys some tests I have done with annonymous functions with
pas2js.
--// Annonymous function testing //-----------------------
unit Unit1;
{$mode objfpc}
interface
uses
Classes, SysUtils, JS, Web;
type
TRefProc = reference to procedure;
TFunc = reference to function(str: String): String;
TProc = reference to procedure;
TEvent = reference to procedure;
TIntegerConvertFunc = reference to function(s: string): integer; // Has a
return type
type
{ TMyClass }
TMyClass = class
private
FEvent: TEvent;
public
constructor Create;
procedure DoIt;
procedure TestIt;
procedure DoStuff1;
procedure DoStuff2;
function AddDays1(aDays: integer): Double;
function AddDays2(aDays: integer): Double; overload;
published
property Event: TEvent read FEvent write FEvent;
end;
implementation
{ TMyClass }
constructor TMyClass.Create;
var
x: String;
begin
x := 'hello';
FEvent := procedure
begin
console.log(x); // Displays 'hello'
console.log(ClassName); // Displays 'TMyClass'
end;
end;
procedure TMyClass.DoIt;
begin
if (Assigned(FEvent)) then
FEvent;
end;
procedure TMyClass.TestIt;
var
myFunction: TIntegerConvertFunc;
begin
myfunction :=
function(s: string): integer
begin
result := StrToInt(s);
end;
console.log(
myfunction('123')
);
end;
procedure TMyClass.DoStuff1;
var
j: integer;
ShowCounter: TProc; // Method no longer has a parameter.
begin
ShowCounter := procedure
begin
// j is known here because it is wrapped in a closure
// and made available to us!
//for j := 1 to 10 do --> not allowed
writeln(IntToStr(j));
end;
for j := 1 to 10 do
ShowCounter; // j is no longer passed
end;
procedure TMyClass.DoStuff2;
var
ShowCounter: TProc;
begin
ShowCounter := procedure
var j: integer; // j is now local.
begin
for j := 1 to 10 do
writeln(IntToStr(j));
end;
ShowCounter;
end;
function TMyClass.AddDays1(aDays: integer): Double;
var
rc: TDateTime;
p: TProc;
begin
rc := now;
p := procedure
begin
// RESULT can live in a closure.
result := rc + aDays;
end;
p;
end;
function TMyClass.AddDays2(aDays: integer): Double;
var
rc: TDateTime;
p: TProc;
begin
rc := now;
p := procedure
begin
// rc can live in a closure.
rc := rc + aDays;
end;
p();
result := rc;
end;
end.
(...)
var
ref: TRefProc;
refProc: TRefProc;
proc: TProc;
f: TFunc;
MyClass: TMyClass;
begin
// Your code here
ref:= procedure()
var i: integer;
begin
console.log('initialization....');
end;
ref;
refProc:=procedure assembler
asm // embed JavaScript
console.log("foo");
end;
refProc;
// Note that typecasting to non "reference to" does not make a difference
// because in JS all functions are closures:
proc:=TProc(procedure
begin
console.log('teste');
end); proc();
f := function(str: String): String
begin
Result := str;
end;
console.log(
f('abcd')
);
MyClass := TMyClass.Create;
MyClass.DoIt; // displays 'hello' 'TMyClass'
MyClass.TestIt; // displays '123'
console.log(
MyClass.AddDays1(10)
);
console.log(
MyClass.AddDays2(10)
);
MyClass.DoStuff1;
MyClass.DoStuff2;
console.log('1');
window.setTimeout(procedure()
begin
window.setTimeout(procedure()
begin
console.log('2');
window.setTimeout(procedure()
begin
console.log('3');
end, 1000);
end, 1000);
end, 1000);
-------------------------
--
Sent from: http://pas2js.38893.n8.nabble.com/
More information about the Pas2js
mailing list