C
C
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 C Downloads
#include <C_CkStringBuilder.h>
#include <C_CkJs.h>
#include <C_CkJsonObject.h>
#include <C_CkJsonArray.h>
void ChilkatSample(void)
{
BOOL success;
HCkStringBuilder sbScript;
HCkJs js;
HCkJsonObject result;
HCkJsonObject funcCall;
HCkJsonArray argsArray;
HCkJsonArray arg;
HCkStringBuilder sbOut;
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) {
printf("%s\n",CkStringBuilder_lastErrorText(sbScript));
CkStringBuilder_Dispose(sbScript);
return;
}
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) {
// Examine the result for an exception.
printf("%s\n",CkJsonObject_emit(result));
// Also examine the LastErrorText.
printf("%s\n",CkJs_lastErrorText(js));
CkStringBuilder_Dispose(sbScript);
CkJs_Dispose(js);
CkJsonObject_Dispose(result);
return;
}
// ------------------------------------------------------------------------------
// 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);
printf("%s\n",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) {
// Examine the result for an exception.
printf("%s\n",CkJsonObject_emit(result));
// Also examine the LastErrorText.
printf("%s\n",CkJs_lastErrorText(js));
CkStringBuilder_Dispose(sbScript);
CkJs_Dispose(js);
CkJsonObject_Dispose(result);
CkJsonObject_Dispose(funcCall);
CkJsonArray_Dispose(argsArray);
CkJsonArray_Dispose(arg);
return;
}
printf("%s\n",CkJsonObject_emit(result));
// Result:
// {
// "type": "double",
// "value": 35.75
// }
// The function also emitted text to the console.
sbOut = CkStringBuilder_Create();
CkJs_ConsoleOutputSb(js,sbOut);
printf("%s\n",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");
printf("%s\n",CkJsonObject_emit(funcCall));
CkStringBuilder_Dispose(sbScript);
CkJs_Dispose(js);
CkJsonObject_Dispose(result);
CkJsonObject_Dispose(funcCall);
CkJsonArray_Dispose(argsArray);
CkJsonArray_Dispose(arg);
CkStringBuilder_Dispose(sbOut);
}