[fpc-pascal] PAS2JS: JSON suggestion

2018-01-11 Thread warleyalex via fpc-pascal
IMHO, theTJSJSONexternal class definition is incorrect.
Currently, we have class function, if you want to use the stringify method,
use this wierd way:TJSJSON.stringify( jsObject);
I think code that don't have mutual dependencies should be separate in
units:
unit ECMA.Json;interface{$mode objfpc}{$modeswitch externalclass}type 
TKeyValueProcessor = function (Key: String; Value: JSValue): JSValue;  
JJSON = class external name 'JSON'  publicfunction parse(Text: String):
JSValue; overload;function parse(Text: String; Reviver:
TKeyValueProcessor): JSValue; overload;function stringify(const Value:
JSValue): String; overload;function stringify(const Value: JSValue;
Replacer: TKeyValueProcessor): String; overload;function stringify(const
Value: JSValue; Replacer: TKeyValueProcessor; Space: String): String;
overload;  end;var  JSON: JJSON; external name 'JSON';implementationend.
and use like this: JSON.stringify( JsonObject );




--
Sent from: http://free-pascal-general.1045716.n5.nabble.com/___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] PAS2JS: operations with array of record

2018-01-11 Thread warleyalex via fpc-pascal
Thanks a lot for the feedback.
it worked as expected! The solution was:
/* in DWScript world */var recA : JA;recB : JB;recC : JC;for var
k:=0 to 2 do begin  recC.id := k;  recC.name := 'abc'+IntToStr(k);  recC.age
:= 10+k;  recB.field1 := 'rec'+ IntToStr(k);  recB.params.Add(recC); 
recA.fields.Add(recB);end;/* in Pas2JS world */var recA : JA;recB : JB;   
recC : JC;k:Integer;beginfor k:=0 to 2 do begin  recC.id := k; 
recC.name := 'abc'+IntToStr(k);  recC.age := 10+k;  recB.field1 := 'rec'+
IntToStr(k);  TJSArray(recB.params).push(recC); 
TJSArray(recA.fields).push(recB);end;
Take a look at this array of record example, in Smart Pascal world, as long
as the datatypes match (e.g. field params is a array of record), we have the
pseudo methods functionality "built-in" for all arrays, for instance. Pas2JS
could have this high level functionality as well. Using the Pas2JS approach
with external classes interfaces it looks so powerful :)
 



--
Sent from: http://free-pascal-general.1045716.n5.nabble.com/___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

[fpc-pascal] PAS2JS: operations with array of record

2018-01-10 Thread warleyalex via fpc-pascal
Since it's missing the Pas2JS specific forum, I don't know if here is correct
place to post things about "Pas2JS".

I'm using it a lot and it's very promising. I came across with issue when
dealing with array of records. 

I would like to convert this little piece of DWScript code to Pas2JS,
unfortunately, I couldn't find a way how to push an array of record. For
instance:
type   JC = record  id : integer; external 'id';  name : string;
external 'name';  age : integer; external 'age';   end; type   JB =
record field1 : string; external 'field1'; params : Array of JC;
external 'params';   end; type   JA= record fields : array of JB;
external 'JA';   end; var recA : JA; var recB : JB; var recC : JC; for var
k:=0 to 2 do begin   recC.id := k;   recC.name := 'abc'+IntToStr(k);  
recC.age := 10+k;   recB.field1 := 'rec'+ IntToStr(k);  
recB.params.Add(recC);   recA.fields.Add(recB); end; 
Just to referente, the above DWScript code will emit the following JS:
function Copy$JC(s,d) {d.id=s.id;d.name=s.name;d.age=s.age;   
return d; } function Clone$JC($) {return {   id:$.id,  
name:$.name,   age:$.age} } /// JB = record /// [line: 23, column:
3] function Copy$JB(s,d) {d.field1=s.field1;d.params=s.params;   
return d; } function Clone$JB($) {return {   field1:$.field1,  
params:$.params} } /// JA = record /// [line: 29, column: 3] function
Copy$JA(s,d) {d.JA=s.JA;return d; } function Clone$JA($) {return
{   JA:$.JA} } var recA = {JA:[]}; var recB = {field1:"",params:[]};
var recC = {id:0,name:"",age:0}; var k = 0; for(k=0;k=2;k++) {   recC.id
= k;   recC.name = "abc"+k.toString();   recC.age = 10+k;   recB.field1 =
"rec"+k.toString();   recB.params.push(Clone$JC(recC));  
recA.JA.push(Clone$JB(recB)); } /*  RecA object*/{"JA" : [{ "field1"
: "rec0", "params" : [{ "id" : 0,
"name" : "abc0", "age" : 10 }, {
"id" : 1, "name" : "abc1", "age" : 11   
 
}, { "id" : 2, "name" : "abc2",
"age" : 12 } ] }, { "field1" : "rec1",
"params" : [{ "id" : 0, "name" : "abc0",

"age" : 10 }, { "id" : 1, "name"
: "abc1", "age" : 11 }, { "id" :
2, "name" : "abc2", "age" : 12 }

] }, { "field1" : "rec2", "params" : [{
"id" : 0, "name" : "abc0", "age" : 10   
 
}, { "id" : 1, "name" : "abc1",
"age" : 11 }, { "id" : 2, "name"
: "abc2", "age" : 12 } ] } ]} 
I believe it will be nice if PAS2JS could generate similar code, each
record type, the DWScript compiler emits corresponding intermediate Clone
and Copy functions.
The PAS2JS transpiler uses another approach (the record variable creates a
JavaScript object) whereas DWScript with identical functionality using
ordinary arrays.
I don't know,I believe it will achieves better performance using
ordinary arrays, at least.
Anyway, what I couldn't find is the built-in kind ofpseudo-methods for
the record data type,set of operations, for inserting, removing,
sorting and otherwise manipulate the content, for instance:
In addition to Low, High, Lengthand Count,  

 Add(item [,...]) / Push(item [,...]) : increases Length by one and
adds one or more item at the end of the array, can also add arrays
(concatenation).  

 Clear() : empties the array (equivalent to SetLength(0))  

 Copy(startIndex[, count]) : creates a new dynamic array containing
count items from startIndex, if count isn't specified, copies to the end of
the array.  

 Delete(index[, count]) : deletes the item at the specified index
and reduces Length by count (default one).  

 IndexOf([startIndex, ]item) : returns the index of an item, returns
a negative value if not found.  

 Insert(index, item) : insert an item at the specified index.  

 Join(delimiter : String) : joins the elements of an array into a
string, and returns the string. 

 Map([map function]) : creates a new array by mapping elements of
the array according to a map function. The map function takes a single
parameter of the type of the array items and returns the mapped value.
 

 Peek() : returns the last item.  

 Pop() : returns the last item and removes it from the array.  

 Remove([startIndex, ]item) : removes the item if it can be found
and returns its previous index, returns a negative value if not found.
 

 Reverse() : reverses the order of the items.  

 SetLength(newLength) : defines the length of a dynamic array  

 Sort([compare function]) : sort an