[Pas2js] Records as pure data?

Michael Van Canneyt michael at freepascal.org
Wed May 9 16:48:28 CEST 2018



On Wed, 9 May 2018, Ryan Joseph wrote:

>
>
>> On May 9, 2018, at 8:46 PM, Michael Van Canneyt <michael at freepascal.org> wrote:
>> 
>> This will not happen.
>> 
>> We are transpiling to Javascript. Records translate to javascript object literals and memory layout is opaque.
>> The idea is explicitly not to make memory layout available in the language.
>> 
>> You can use the native constructs that the runtime makes available if you
>> need specific memory buffers, but no more than that.
>
> Then what happens with stuff like this? 
> Not sure how to append arrays (didn’t like push for some reason) but I guess we can build arrays like this and then iterate them to build memory buffers?

>
> I know I can just give up and do everything with arrays of floats but it’s worth trying to figure out a better way to do this in advance instead of wasting time down the road trying to construct confusing arrays.
>
> type
> 	TVec2 = array of single;
> 	TRGBAb = array of byte;
>
> function V2(x, y: single): TVec2;
> begin
> 	result[0] := x;
> 	result[1] := y;
> end;
>
> function RGBA(r, g, b, a: byte): TRGBAb;
> begin
> 	result[0] := r;
> 	result[1] := g;
> 	result[2] := b;
> 	result[3] := a;
> end;
>
> type
> 	GLTexVertex = record
> 		pos: TVec2;
> 		color: TRGBAb;
> 	end;
>
>
> var
> 	v: GLTexVertex;
> 	verts: array of GLTexVertex;
> begin
> v.pos := V2(100, 100);
> v.color := RGBA(1, 0, 0, 1);
> verts.push(v); // gives an error

Array of GLTexVertex is a pascal construct.

As such, it obeys the usual pascal rules, so you do:

Setlength(verts,1);
verts[0]:=v;

Or
verts[0].pos:=v(100,100);
verts[0].color:=RGBA(1, 0, 0, 1);


If you want to use a raw Javascript array which has support for push, use

var
   verts : TJSArray;

begin
   verts:=TJSArray.New;
   verts.push[v];
end;

Michael.


More information about the Pas2js mailing list