<p>Since it's missing the <strong>Pas2JS</strong> specific forum, I don't know if here is correct place to post things about "Pas2JS". <br /><br />I'm using it a lot and it's very promising. I came across with issue when dealing with array of records. <br /><br />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:</p>
<!-- HTML generated using hilite.me -->
<div style="background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;">
<pre style="margin: 0; line-height: 125%;"><span style="color: #008800; font-weight: bold;">type</span>
JC <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">record</span>
id <span style="color: #333333;">:</span> <span style="color: #333399; font-weight: bold;">integer</span><span style="color: #333333;">;</span> external <span style="background-color: #fff0f0;">'id'</span><span style="color: #333333;">;</span>
name <span style="color: #333333;">:</span> <span style="color: #008800; font-weight: bold;">string</span><span style="color: #333333;">;</span> external <span style="background-color: #fff0f0;">'name'</span><span style="color: #333333;">;</span>
age <span style="color: #333333;">:</span> <span style="color: #333399; font-weight: bold;">integer</span><span style="color: #333333;">;</span> external <span style="background-color: #fff0f0;">'age'</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">end</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">type</span>
JB <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">record</span>
field1 <span style="color: #333333;">:</span> <span style="color: #008800; font-weight: bold;">string</span><span style="color: #333333;">;</span> external <span style="background-color: #fff0f0;">'field1'</span><span style="color: #333333;">;</span>
params <span style="color: #333333;">:</span> <span style="color: #008800; font-weight: bold;">Array</span> <span style="color: #008800; font-weight: bold;">of</span> JC<span style="color: #333333;">;</span> external <span style="background-color: #fff0f0;">'params'</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">end</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">type</span>
JA<span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">record</span>
fields <span style="color: #333333;">:</span> <span style="color: #008800; font-weight: bold;">array</span> <span style="color: #008800; font-weight: bold;">of</span> JB<span style="color: #333333;">;</span> external <span style="background-color: #fff0f0;">'JA'</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">end</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">var</span> recA <span style="color: #333333;">:</span> JA<span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">var</span> recB <span style="color: #333333;">:</span> JB<span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">var</span> recC <span style="color: #333333;">:</span> JC<span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">for</span> <span style="color: #008800; font-weight: bold;">var</span> k<span style="color: #333333;">:=</span><span style="color: #0000dd; font-weight: bold;">0</span> <span style="color: #008800; font-weight: bold;">to</span> <span style="color: #0000dd; font-weight: bold;">2</span> <span style="color: #008800; font-weight: bold;">do</span> <span style="color: #008800; font-weight: bold;">begin</span>
recC<span style="color: #333333;">.</span>id <span style="color: #333333;">:=</span> k<span style="color: #333333;">;</span>
recC<span style="color: #333333;">.</span>name <span style="color: #333333;">:=</span> <span style="background-color: #fff0f0;">'abc'</span><span style="color: #333333;">+</span><span style="color: #007020;">IntToStr</span>(k)<span style="color: #333333;">;</span>
recC<span style="color: #333333;">.</span>age <span style="color: #333333;">:=</span> <span style="color: #0000dd; font-weight: bold;">10</span><span style="color: #333333;">+</span>k<span style="color: #333333;">;</span>
recB<span style="color: #333333;">.</span>field1 <span style="color: #333333;">:=</span> <span style="background-color: #fff0f0;">'rec'</span><span style="color: #333333;">+</span> <span style="color: #007020;">IntToStr</span>(k)<span style="color: #333333;">;</span>
recB<span style="color: #333333;">.</span>params<span style="color: #333333;">.</span>Add(recC)<span style="color: #333333;">;</span>
recA<span style="color: #333333;">.</span>fields<span style="color: #333333;">.</span>Add(recB)<span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">end</span><span style="color: #333333;">;</span>
</pre>
</div>
<p>Just to referente, the above DWScript code will emit the following JS:</p>
<!-- HTML generated using hilite.me -->
<div style="background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;">
<pre style="margin: 0; line-height: 125%;"><span style="color: #008800; font-weight: bold;">function</span> Copy$JC(s,d) {
d.id<span style="color: #333333;">=</span>s.id;
d.name<span style="color: #333333;">=</span>s.name;
d.age<span style="color: #333333;">=</span>s.age;
<span style="color: #008800; font-weight: bold;">return</span> d;
}
<span style="color: #008800; font-weight: bold;">function</span> Clone$JC($) {
<span style="color: #008800; font-weight: bold;">return</span> {
id<span style="color: #333333;">:</span>$.id,
name<span style="color: #333333;">:</span>$.name,
age<span style="color: #333333;">:</span>$.age
}
}
<span style="color: #888888;">/// JB = record </span>
<span style="color: #888888;">/// [line: 23, column: 3] </span>
<span style="color: #008800; font-weight: bold;">function</span> Copy$JB(s,d) {
d.field1<span style="color: #333333;">=</span>s.field1;
d.params<span style="color: #333333;">=</span>s.params;
<span style="color: #008800; font-weight: bold;">return</span> d;
}
<span style="color: #008800; font-weight: bold;">function</span> Clone$JB($) {
<span style="color: #008800; font-weight: bold;">return</span> {
field1<span style="color: #333333;">:</span>$.field1,
params<span style="color: #333333;">:</span>$.params
}
}
<span style="color: #888888;">/// JA = record </span>
<span style="color: #888888;">/// [line: 29, column: 3] </span>
<span style="color: #008800; font-weight: bold;">function</span> Copy$JA(s,d) {
d.JA<span style="color: #333333;">=</span>s.JA;
<span style="color: #008800; font-weight: bold;">return</span> d;
}
<span style="color: #008800; font-weight: bold;">function</span> Clone$JA($) {
<span style="color: #008800; font-weight: bold;">return</span> {
JA<span style="color: #333333;">:</span>$.JA
}
}
<span style="color: #008800; font-weight: bold;">var</span> recA <span style="color: #333333;">=</span> {JA<span style="color: #333333;">:</span>[]};
<span style="color: #008800; font-weight: bold;">var</span> recB <span style="color: #333333;">=</span> {field1<span style="color: #333333;">:</span><span style="background-color: #fff0f0;">""</span>,params<span style="color: #333333;">:</span>[]};
<span style="color: #008800; font-weight: bold;">var</span> recC <span style="color: #333333;">=</span> {id<span style="color: #333333;">:</span><span style="color: #0000dd; font-weight: bold;">0</span>,name<span style="color: #333333;">:</span><span style="background-color: #fff0f0;">""</span>,age<span style="color: #333333;">:</span><span style="color: #0000dd; font-weight: bold;">0</span>};
<span style="color: #008800; font-weight: bold;">var</span> k <span style="color: #333333;">=</span> <span style="color: #0000dd; font-weight: bold;">0</span>;
<span style="color: #008800; font-weight: bold;">for</span>(k<span style="color: #333333;">=</span><span style="color: #0000dd; font-weight: bold;">0</span>;k<span style="color: #333333;"><=</span><span style="color: #0000dd; font-weight: bold;">2</span>;k<span style="color: #333333;">++</span>) {
recC.id <span style="color: #333333;">=</span> k;
recC.name <span style="color: #333333;">=</span> <span style="background-color: #fff0f0;">"abc"</span><span style="color: #333333;">+</span>k.toString();
recC.age <span style="color: #333333;">=</span> <span style="color: #0000dd; font-weight: bold;">10</span><span style="color: #333333;">+</span>k;
recB.field1 <span style="color: #333333;">=</span> <span style="background-color: #fff0f0;">"rec"</span><span style="color: #333333;">+</span>k.toString();
recB.params.push(Clone$JC(recC));
recA.JA.push(Clone$JB(recB));
}
<span style="color: #888888;">/*</span>
<span style="color: #888888;"> RecA object</span>
<span style="color: #888888;">*/</span>
{<span style="background-color: #fff0f0;">"JA"</span> <span style="color: #333333;">:</span> [{
<span style="background-color: #fff0f0;">"field1"</span> <span style="color: #333333;">:</span> <span style="background-color: #fff0f0;">"rec0"</span>,
<span style="background-color: #fff0f0;">"params"</span> <span style="color: #333333;">:</span> [{
<span style="background-color: #fff0f0;">"id"</span> <span style="color: #333333;">:</span> <span style="color: #0000dd; font-weight: bold;">0</span>,
<span style="background-color: #fff0f0;">"name"</span> <span style="color: #333333;">:</span> <span style="background-color: #fff0f0;">"abc0"</span>,
<span style="background-color: #fff0f0;">"age"</span> <span style="color: #333333;">:</span> <span style="color: #0000dd; font-weight: bold;">10</span>
}, {
<span style="background-color: #fff0f0;">"id"</span> <span style="color: #333333;">:</span> <span style="color: #0000dd; font-weight: bold;">1</span>,
<span style="background-color: #fff0f0;">"name"</span> <span style="color: #333333;">:</span> <span style="background-color: #fff0f0;">"abc1"</span>,
<span style="background-color: #fff0f0;">"age"</span> <span style="color: #333333;">:</span> <span style="color: #0000dd; font-weight: bold;">11</span>
}, {
<span style="background-color: #fff0f0;">"id"</span> <span style="color: #333333;">:</span> <span style="color: #0000dd; font-weight: bold;">2</span>,
<span style="background-color: #fff0f0;">"name"</span> <span style="color: #333333;">:</span> <span style="background-color: #fff0f0;">"abc2"</span>,
<span style="background-color: #fff0f0;">"age"</span> <span style="color: #333333;">:</span> <span style="color: #0000dd; font-weight: bold;">12</span>
}
]
}, {
<span style="background-color: #fff0f0;">"field1"</span> <span style="color: #333333;">:</span> <span style="background-color: #fff0f0;">"rec1"</span>,
<span style="background-color: #fff0f0;">"params"</span> <span style="color: #333333;">:</span> [{
<span style="background-color: #fff0f0;">"id"</span> <span style="color: #333333;">:</span> <span style="color: #0000dd; font-weight: bold;">0</span>,
<span style="background-color: #fff0f0;">"name"</span> <span style="color: #333333;">:</span> <span style="background-color: #fff0f0;">"abc0"</span>,
<span style="background-color: #fff0f0;">"age"</span> <span style="color: #333333;">:</span> <span style="color: #0000dd; font-weight: bold;">10</span>
}, {
<span style="background-color: #fff0f0;">"id"</span> <span style="color: #333333;">:</span> <span style="color: #0000dd; font-weight: bold;">1</span>,
<span style="background-color: #fff0f0;">"name"</span> <span style="color: #333333;">:</span> <span style="background-color: #fff0f0;">"abc1"</span>,
<span style="background-color: #fff0f0;">"age"</span> <span style="color: #333333;">:</span> <span style="color: #0000dd; font-weight: bold;">11</span>
}, {
<span style="background-color: #fff0f0;">"id"</span> <span style="color: #333333;">:</span> <span style="color: #0000dd; font-weight: bold;">2</span>,
<span style="background-color: #fff0f0;">"name"</span> <span style="color: #333333;">:</span> <span style="background-color: #fff0f0;">"abc2"</span>,
<span style="background-color: #fff0f0;">"age"</span> <span style="color: #333333;">:</span> <span style="color: #0000dd; font-weight: bold;">12</span>
}
]
}, {
<span style="background-color: #fff0f0;">"field1"</span> <span style="color: #333333;">:</span> <span style="background-color: #fff0f0;">"rec2"</span>,
<span style="background-color: #fff0f0;">"params"</span> <span style="color: #333333;">:</span> [{
<span style="background-color: #fff0f0;">"id"</span> <span style="color: #333333;">:</span> <span style="color: #0000dd; font-weight: bold;">0</span>,
<span style="background-color: #fff0f0;">"name"</span> <span style="color: #333333;">:</span> <span style="background-color: #fff0f0;">"abc0"</span>,
<span style="background-color: #fff0f0;">"age"</span> <span style="color: #333333;">:</span> <span style="color: #0000dd; font-weight: bold;">10</span>
}, {
<span style="background-color: #fff0f0;">"id"</span> <span style="color: #333333;">:</span> <span style="color: #0000dd; font-weight: bold;">1</span>,
<span style="background-color: #fff0f0;">"name"</span> <span style="color: #333333;">:</span> <span style="background-color: #fff0f0;">"abc1"</span>,
<span style="background-color: #fff0f0;">"age"</span> <span style="color: #333333;">:</span> <span style="color: #0000dd; font-weight: bold;">11</span>
}, {
<span style="background-color: #fff0f0;">"id"</span> <span style="color: #333333;">:</span> <span style="color: #0000dd; font-weight: bold;">2</span>,
<span style="background-color: #fff0f0;">"name"</span> <span style="color: #333333;">:</span> <span style="background-color: #fff0f0;">"abc2"</span>,
<span style="background-color: #fff0f0;">"age"</span> <span style="color: #333333;">:</span> <span style="color: #0000dd; font-weight: bold;">12</span>
}
]
}
]}
</pre>
</div>
<p> 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.</p>
<p>The PAS2JS transpiler uses another approach (<strong>the record variable creates a JavaScript object</strong>) whereas DWScript with identical functionality using <strong>ordinary arrays</strong>.</p>
<p>I don't know, I believe it will achieves better performance using ordinary arrays, at least.</p>
<p>Anyway, what I couldn't find is the built-in kind of <strong>pseudo-methods for the record data type</strong>, set of operations, for inserting, removing, sorting and otherwise manipulate the content, for instance:</p>
<p>In addition to <strong>Low</strong>, <strong>High</strong>, <strong>Length</strong> and <strong>Count</strong>, <br /><br />· <strong>Add</strong>(item [,...]) / <strong>Push</strong>(item [,...]) : increases Length by one and adds one or more item at the end of the array, can also add arrays (concatenation). <br /><br />· <strong>Clear</strong>() : empties the array (equivalent to SetLength(0)) <br /><br />· <strong>Copy</strong>(startIndex[, count]) : creates a new dynamic array containing count items from startIndex, if count isn't specified, copies to the end of the array. <br /><br />· <strong>Delete</strong>(index[, count]) : deletes the item at the specified index and reduces Length by count (default one). <br /><br />· <strong>IndexOf</strong>([startIndex, ]item) : returns the index of an item, returns a negative value if not found. <br /><br />· <strong>Insert</strong>(index, item) : insert an item at the specified index. <br /><br />· <strong>Join</strong>(delimiter : String) : joins the elements of an array into a string, and returns the string. <br /><br />· <strong>Map</strong>([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. <br /><br />· <strong>Peek</strong>() : returns the last item. <br /><br />· <strong>Pop</strong>() : returns the last item and removes it from the array. <br /><br />· <strong>Remove</strong>([startIndex, ]item) : removes the item if it can be found and returns its previous index, returns a negative value if not found. <br /><br />· <strong>Reverse</strong>() : reverses the order of the items. <br /><br />· <strong>SetLength</strong>(newLength) : defines the length of a dynamic array <br /><br />· <strong>Sort</strong>([compare function]) : sort an array according to the compare function, the comparison function can be omitted for arrays of String, Integer and Float. <br /><br />· <strong>Swap</strong>(index1, index2) : swaps to items of specified indexes. </p>
<p>just an idea.</p>
<br/><hr align="left" width="300" />
Sent from the <a href="http://free-pascal-general.1045716.n5.nabble.com/">Free Pascal - General mailing list archive</a> at Nabble.com.<br/>