[Pas2js] Records as pure data?

Mattias Gaertner nc-gaertnma at netcologne.de
Wed May 9 17:18:33 CEST 2018


On Wed, 9 May 2018 21:21:42 +0700
Ryan Joseph <ryan at thealchemistguild.com> wrote:

>[...]
> Then what happens with stuff like this? Not sure how to append arrays (didn’t like push for some reason)

Pascal arrays don't have "push".
You can typecast to TJSArray. For example TJSArray(verts).push(v); .
Eventually when pas2js gets array helpers you can add such things more
easily.


> but I guess we can build arrays like this and then iterate them to build memory buffers?

You can build JS typed arrays from normal arrays or ArrayBuffers.

 
> 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.

Here are some more ideas:

type
  TFloat32Point = record
    f: TJSFloat32Point;
    x: Float32; external name 'f[0]';
    y: Float32; external name 'f[1]';
  end;

function CreateFloat32Point(x, y: Float32): TFloat32Point;
const
  a: array[1..2] of float32 = (0,0);
begin
  Result.f:=TJSFloat32Point(TJSFloat32Point.from(a));
  Result.x:=x;
  Result.y:=y;
end;


type
  TFloat32Point = class
  public
    f: TJSFloat32Point;
    x: Float32; external name 'f[0]';
    y: Float32; external name 'f[1]';
    constructor Create(x, y: Float32); overload;
  end;

constructor TFloat32Point.Create(x, y: Float32);
const
  a: array[1..2] of float32 = (0,0);
begin
  f:=TJSFloat32Point(TJSFloat32Point.from(a));
  Self.x:=x;
  Self.y:=y;
end;


Or with pas2js trunk version:

type
  TJSFloat32Point = class external name 'Float32Array' (TJSFloat32Array)
  public
    x: Float32; external name '[0]';
    y: Float32; external name '[1]';
  end;

function CreateFloat32Point(x,y: Float32): TJSFloat32Point;
const
  a: array[1..2] of float32 = (0,0);
begin
  Result:=TJSFloat32Point(TJSFloat32Point.from(a));
  Result.x:=x;
  Result.y:=y;
end;



Mattias


More information about the Pas2js mailing list