Pusha - Array of Array

Issue #67 invalid
Warley Alex Camargo created an issue

Please take a look at this post There's probably issues with Array of Array of Integer and Array of JSON Array (pusha instruction generated)

Comments (5)

  1. Eric Grange repo owner

    Snippet in the linked page appears incomplete, I made some minor changes to have it compile (see below) and it works

    var a : array of Variant;
    
    for var x:=1 to 10 do
    begin
      var objeto :=
        [CLASS
          nome   : string  = "Nome#" + IntToStr(x);
          age    : integer =  + x;
        END,
        CLASS
          address: string  = "Addr#" + IntToStr(x);
          cep    : variant = '35700-00'+ IntToStr(x);
        END,
        CLASS
          city   : string  = "City#" + IntToStr(x);
          country: string  = "Country " + IntToStr(x);
        END];
        a.push(objeto);
    end;
    
    function JSON : Variant; external property;
    PrintLn(JSON.stringify(a));
    
  2. Warley Alex Camargo reporter

    Issue I

    var a : array of Variant;
    
    var obj := CLASS
      "JSONArray2": array of variant;
    END;
    
    for var x:=1 to 2 do
    begin
      var objeto : array of variant =
        [CLASS
          nome   : string  = "Nome#" + IntToStr(x);
        END,
        CLASS
          country: string  = "Country " + IntToStr(x);
        END];
        //obj.JSONArray2.Push(objeto);
        a.Push(objeto);
    end;
    
    function JSON : Variant; external property;
    //console.log(JSON.stringify(obj.JSONArray2));
    console.log(JSON.stringify(a));
    

    This will return an array of objects [{},{}]

    [{"nome":"Nome#1"},{"country":"Country 1"},{"nome":"Nome#2"},{"country":"Country 2"}] 
    

    I would like to return this notation [[{ }],[{ }]]

    [[{"nome":"Nome#1"},{"country":"Country 1"}],[{"nome":"Nome#2"},{"country":"Country 2"}]] 
    

    I had to modify the array prototype to get the expected notation:

    Array.prototype.pusha = function (e) { this.push.apply(this, e ); return this }
    
    to 
    
    Array.prototype.pusha = function (e) { this.push.apply(this, [e]); return this }
    

    Issue II

    var preloaded = CLASS
      values : array [0..4] of array [0..1] of Integer = [ [0, 100],[1000, 200],[2000, 300],[3000, 400],[4000, 500] ];
    END;
    
    console.log(JSON.stringify(preloaded));
    
    {"values":[[4000,500],[1000,200],[2000,300],[3000,400],[4000,500]]}
    the correct is
    {"values":[[0,100],[1000,200],[2000,300],[3000,400],[4000,500]]}
    
  3. Eric Grange repo owner

    For Issue 1, your declaration of a is an "array of Variant", thus [{},{}} is correct, if you want [[{ }],[{ }]] then just declare

    var a : array of array of Variant;
    

    For an "array of Variant" the .Push() is the method of the Pascal array, not the literal ".push()" of JS, and so it is an alias of .Add() meant to be used in conjunction with .Pop() in cases where the array is accessed as a LIFO stack. In Pascal, the name is case-insensitive, type-checked, and accepts both elements and array of elements parameters, and array of elements are appended.

    If you want to use the literal ".push()" of JS, you would have to use a "Variant" type, as in that case you would be referring to the method push of the Variant (case sensitive, directly mapped to JS, without type checking) rather than the method Push of the array (case insensitive, Pascal side). In that case you could use something like

    var a : Variant := JSON.parse('[]');
    ...
       a.push(objeto);   // lower case, as it's the case-sensitive .push method of JS now
    ...
    

    Issue II was already fixed some time ago and should no longer occur in future binaries.

  4. Log in to comment