Sample code for 30+ languages & platforms
Pascal (Lazarus/Delphi)

Call a JavaScript Function Returning an Array

See more JavaScript Examples

Demonstrates how to call a JavaScript function that returns an array.

Chilkat Pascal (Lazarus/Delphi) Downloads

Pascal (Lazarus/Delphi)
program ChilkatDemo;

// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  SysUtils,
  CkDllLoader,
  Chilkat.Js,
  Chilkat.StringBuilder,
  Chilkat.JsonObject;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  sbScript: TStringBuilder;
  js: TJs;
  result: TJsonObject;
  funcCall: TJsonObject;
  count: Integer;
  i: Integer;

begin
  success := False;

  //  ----------------------------------------------------------------------------------
  //  The Javascript functions called in this example are shown at the bottom of this page.
  //  -----------------------------------------------------------------------------------

  sbScript := TStringBuilder.Create;
  success := sbScript.LoadFile('js_function_returning_array.js','utf-8');
  if (success = False) then
    begin
      WriteLn(sbScript.LastErrorText);
      Exit;
    end;

  js := TJs.Create;

  result := TJsonObject.Create;
  result.EmitCompact := False;

  //  Call Eval to add the function to the context's global object
  success := js.Eval(sbScript,result);
  if (success = False) then
    begin
      //  Examine the result for an exception.
      WriteLn(result.Emit());

      //  Also examine the LastErrorText.
      WriteLn(js.LastErrorText);
      Exit;
    end;

  //  ------------------------------------------------------------------------------
  //  Call each function

  funcCall := TJsonObject.Create;

  //  Create JSON specifying the function name and arguments
  //  The function has no arguments, so we only specify the name.

  funcCall.UpdateString('name','getDays');

  success := js.CallFunction(funcCall,result);
  if (success = False) then
    begin
      //  Examine the result for an exception.
      WriteLn(result.Emit());

      //  Also examine the LastErrorText.
      WriteLn(js.LastErrorText);
      Exit;
    end;

  WriteLn(result.Emit());

  //  Output:
  //  {
  //    "type": "array",
  //    "value": [
  //      "Monday",
  //      "Tuesday",
  //      "Wednesday",
  //      "Thursday",
  //      "Friday"
  //    ]
  //  }

  //  Access each array value..
  count := result.SizeOfArray('value');
  i := 0;
  while i < count do
    begin
      result.I := i;
      WriteLn(result.StringOf('value[i]'));
      i := i + 1;
    end;

  //  ------------------------------------------------------------------------------
  //  Call the getRange(start,end) function

  funcCall.Clear();
  funcCall.UpdateString('name','getRange');
  funcCall.UpdateInt('args[0]',14);
  funcCall.UpdateInt('args[1]',21);
  success := js.CallFunction(funcCall,result);
  WriteLn(result.Emit());

  //  Output:
  //  {
  //    "type": "array",
  //    "value": [
  //      14,
  //      15,
  //      16,
  //      17,
  //      18,
  //      19,
  //      20,
  //      21
  //    ]
  //  }

  count := result.SizeOfArray('value');
  i := 0;
  while i < count do
    begin
      result.I := i;
      WriteLn(result.IntOf('value[i]'));
      i := i + 1;
    end;

  //  ------------------------------------------------------------------------------
  //  Call the getEmployees() function

  funcCall.Clear();
  funcCall.UpdateString('name','getEmployees');
  success := js.CallFunction(funcCall,result);
  WriteLn(result.Emit());

  //  Output:
  //  {
  //    "type": "array",
  //    "value": [
  //      {
  //        "id": 101,
  //        "name": "Alice",
  //        "role": "Dev"
  //      },
  //      {
  //        "id": 102,
  //        "name": "Bob",
  //        "role": "Manager"
  //      }
  //    ]
  //  }

  count := result.SizeOfArray('value');
  i := 0;
  while i < count do
    begin
      result.I := i;
      WriteLn('name: ' + result.StringOf('value[i].name'));
      WriteLn('role: ' + result.StringOf('value[i].role'));
      WriteLn('id: ' + result.IntOf('value[i].id'));
      WriteLn('');
      i := i + 1;
    end;



  sbScript.Free;
  js.Free;
  result.Free;
  funcCall.Free;

end;

// ---------------------------------------------------------------------------

begin

  try
    RunDemo;
  except
    on E: Exception do
      WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
  end;

  WriteLn;
  {$IFDEF MSWINDOWS}
  WriteLn('Press Enter to exit...');
  ReadLn;
  {$ENDIF}
end.