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

warleyalex warleyalex at yahoo.com.br
Wed Jan 10 19:40:48 CET 2018


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 of pseudo-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, Length and 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 array according to the compare
function, the comparison function can be omitted for arrays of String,
Integer and Float.   

· Swap(index1, index2) : swaps to items of specified indexes.  
just an idea.




--
Sent from: http://free-pascal-general.1045716.n5.nabble.com/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20180110/48bdfd05/attachment.html>


More information about the fpc-pascal mailing list