Delphi DLL
Delphi DLL
Call a JavaScript Function Passing an Array Argument
See more JavaScript Examples
Demonstrates how to call a JavaScript function with an argument that is an array.Chilkat Delphi DLL Downloads
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Js, StringBuilder, JsonArray, JsonObject;
...
procedure TForm1.Button1Click(Sender: TObject);
var
success: Boolean;
sbScript: HCkStringBuilder;
js: HCkJs;
result: HCkJsonObject;
funcCall: HCkJsonObject;
argsArray: HCkJsonArray;
arg: HCkJsonArray;
sbOut: HCkStringBuilder;
begin
success := False;
// This is the JavaScript function we'll call:
// function calculateTotal(numbers) {
// console.log(numbers);
// let total = 0;
//
// // Loop through every number in the array
// for (const num of numbers) {
// console.log(num);
// total += num;
// }
//
// return total;
// }
sbScript := CkStringBuilder_Create();
success := CkStringBuilder_LoadFile(sbScript,'js_function_array_arg.js','utf-8');
if (success = False) then
begin
Memo1.Lines.Add(CkStringBuilder__lastErrorText(sbScript));
Exit;
end;
js := CkJs_Create();
result := CkJsonObject_Create();
CkJsonObject_putEmitCompact(result,False);
// Call Eval to add the function to the context's global object
success := CkJs_Eval(js,sbScript,result);
if (success = False) then
begin
// Examine the result for an exception.
Memo1.Lines.Add(CkJsonObject__emit(result));
// Also examine the LastErrorText.
Memo1.Lines.Add(CkJs__lastErrorText(js));
Exit;
end;
// ------------------------------------------------------------------------------
// Call the function calculateTotal(numbers)
funcCall := CkJsonObject_Create();
CkJsonObject_putEmitCompact(funcCall,False);
// Create JSON specifying the function name and arguments
// In this case, there is only 1 argument, and it is an array.
CkJsonObject_UpdateString(funcCall,'name','calculateTotal');
// Create the arguments array.
argsArray := CkJsonArray_Create();
// The 1st argument in the arguments array is itself an array.
// Passing -1 indicates to append to the array.
arg := CkJsonArray_Create();
CkJsonArray_AddArrayAt2(argsArray,-1,arg);
// Fill in the values for the 1st argument.
CkJsonArray_AddNumberAt(arg,-1,'10.50');
CkJsonArray_AddNumberAt(arg,-1,'20.00');
CkJsonArray_AddNumberAt(arg,-1,'5.25');
// Add the "args" array to the funcCall.
CkJsonObject_AppendArrayCopy(funcCall,'args',argsArray);
Memo1.Lines.Add(CkJsonObject__emit(funcCall));
// The funcCall is as follows. Notice that the 1st (and only) argument is an array.
// {
// "name": "calculateTotal",
// "args": [
// [
// 10.50,
// 20.00,
// 5.25
// ]
// ]
// }
success := CkJs_CallFunction(js,funcCall,result);
if (success = False) then
begin
// Examine the result for an exception.
Memo1.Lines.Add(CkJsonObject__emit(result));
// Also examine the LastErrorText.
Memo1.Lines.Add(CkJs__lastErrorText(js));
Exit;
end;
Memo1.Lines.Add(CkJsonObject__emit(result));
// Result:
// {
// "type": "double",
// "value": 35.75
// }
// The function also emitted text to the console.
sbOut := CkStringBuilder_Create();
CkJs_ConsoleOutputSb(js,sbOut);
Memo1.Lines.Add(CkStringBuilder__getAsString(sbOut));
// Output:
// 10.5,20,5.25
// 10.5
// 20
// 5.25
// -----------------------------------------------------------
// Note: If the array argument is simple, this is an alternative
// and simpler way of creating the funcCall:
CkJsonObject_Clear(funcCall);
CkJsonObject_UpdateString(funcCall,'name','calculateTotal');
CkJsonObject_UpdateNumber(funcCall,'args[0][0]','10.50');
CkJsonObject_UpdateNumber(funcCall,'args[0][1]','20.00');
CkJsonObject_UpdateNumber(funcCall,'args[0][2]','5.25');
Memo1.Lines.Add(CkJsonObject__emit(funcCall));
CkStringBuilder_Dispose(sbScript);
CkJs_Dispose(js);
CkJsonObject_Dispose(result);
CkJsonObject_Dispose(funcCall);
CkJsonArray_Dispose(argsArray);
CkJsonArray_Dispose(arg);
CkStringBuilder_Dispose(sbOut);
end;