[Pas2js] Multi dimensional static array to dynamic array?

Mattias Gaertner nc-gaertnma at netcologne.de
Tue May 15 16:44:53 CEST 2018


On Tue, 15 May 2018 19:51:36 +0700
Ryan Joseph <ryan at thealchemistguild.com> wrote:

> > On May 15, 2018, at 6:44 PM, Mattias Gaertner <nc-gaertnma at netcologne.de> wrote:
> >   
> >> TJSFloat32List = array of GLfloat;
> >> data: array[0..3, 0..3] of GLfloat;
> >> list: TJSFloat32List;
> >> 
> >> list := TJSFloat32List(data); // no go  
> > 
> > Rightfully so.  
> 
> What are you suggesting then? uniformMatrix4fv takes an array of GLfloat which we translated as TJSFloat32List. In Pascal I would use @data to pass the address of the start of the array and that works just fine.

This works for GLFloat. It is not guaranteed to work with all types
and directives. You are relying on an implementation detail.


> If this was JS I’m pretty sure I could have passed that array as the
> parameter also.

Don't guess. Try it out.

> It seems we have a problem with the static and dynamic array syntax not being compatible but no workaround when they need to be passed to JS functions.

In Pascal an "array[1..2,1..3] of t" is the same as "array[1..2] of
array[1..3] of t". For example:

type
  GLFloat = double;
  TArrA = array[1..3] of GLFloat;
  TArrB = array[1..2,1..3] of GLFloat;
  TArrC = array[1..6] of GLFloat;
var
  a: TArrA;
  b: TArrB;
  c: TArrC;
  p: ^GLFloat;
begin
  b[1]:=a; 
  a:=b[1];
  c:=b; // not allowed in Delphi/FPC
  p:=@b[1]; // leaving type safety of Pascal
end;

Note: This does not yet work in pas2js.

Theoretically pas2js could translate multi dimensional static arrays to
a one dimensional array, although that would make the converter more
complicated. Accessing the sub arrays (e.g. b[1]:=a) would
require cloning. So it depends on your usage which is better.

Can you use something like this:

TVec3x3 = class external name 'Float32Array' (TJSFloat32Array)
private
  ...
public
  property XY[x,y: integer]: Float32 read GetXY write SetXY; default;
end;


Mattias


More information about the Pas2js mailing list