Sample code for 30+ languages & platforms
Unicode 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 Unicode C Downloads

Unicode C
#include <C_CkStringBuilderW.h>
#include <C_CkJsW.h>
#include <C_CkJsonObjectW.h>
#include <C_CkJsonArrayW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkStringBuilderW sbScript;
    HCkJsW js;
    HCkJsonObjectW result;
    HCkJsonObjectW funcCall;
    HCkJsonArrayW argsArray;
    HCkJsonArrayW arg;
    HCkStringBuilderW 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 = CkStringBuilderW_Create();
    success = CkStringBuilderW_LoadFile(sbScript,L"js_function_array_arg.js",L"utf-8");
    if (success == FALSE) {
        wprintf(L"%s\n",CkStringBuilderW_lastErrorText(sbScript));
        CkStringBuilderW_Dispose(sbScript);
        return;
    }

    js = CkJsW_Create();

    result = CkJsonObjectW_Create();
    CkJsonObjectW_putEmitCompact(result,FALSE);

    // Call Eval to add the function to the context's global object
    success = CkJsW_Eval(js,sbScript,result);
    if (success == FALSE) {
        // Examine the result for an exception.
        wprintf(L"%s\n",CkJsonObjectW_emit(result));

        // Also examine the LastErrorText.
        wprintf(L"%s\n",CkJsW_lastErrorText(js));
        CkStringBuilderW_Dispose(sbScript);
        CkJsW_Dispose(js);
        CkJsonObjectW_Dispose(result);
        return;
    }

    // ------------------------------------------------------------------------------
    // Call the function calculateTotal(numbers)

    funcCall = CkJsonObjectW_Create();
    CkJsonObjectW_putEmitCompact(funcCall,FALSE);

    // Create JSON specifying the function name and arguments
    // In this case, there is only 1 argument, and it is an array.

    CkJsonObjectW_UpdateString(funcCall,L"name",L"calculateTotal");

    // Create the arguments array.
    argsArray = CkJsonArrayW_Create();

    // The 1st argument in the arguments array is itself an array.
    // Passing -1 indicates to append to the array.
    arg = CkJsonArrayW_Create();
    CkJsonArrayW_AddArrayAt2(argsArray,-1,arg);

    // Fill in the values for the 1st argument.
    CkJsonArrayW_AddNumberAt(arg,-1,L"10.50");
    CkJsonArrayW_AddNumberAt(arg,-1,L"20.00");
    CkJsonArrayW_AddNumberAt(arg,-1,L"5.25");

    // Add the "args" array to the funcCall.
    CkJsonObjectW_AppendArrayCopy(funcCall,L"args",argsArray);

    wprintf(L"%s\n",CkJsonObjectW_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 = CkJsW_CallFunction(js,funcCall,result);
    if (success == FALSE) {
        // Examine the result for an exception.
        wprintf(L"%s\n",CkJsonObjectW_emit(result));

        // Also examine the LastErrorText.
        wprintf(L"%s\n",CkJsW_lastErrorText(js));
        CkStringBuilderW_Dispose(sbScript);
        CkJsW_Dispose(js);
        CkJsonObjectW_Dispose(result);
        CkJsonObjectW_Dispose(funcCall);
        CkJsonArrayW_Dispose(argsArray);
        CkJsonArrayW_Dispose(arg);
        return;
    }

    wprintf(L"%s\n",CkJsonObjectW_emit(result));

    // Result:
    // {
    //   "type": "double",
    //   "value": 35.75
    // }

    // The function also emitted text to the console.

    sbOut = CkStringBuilderW_Create();
    CkJsW_ConsoleOutputSb(js,sbOut);
    wprintf(L"%s\n",CkStringBuilderW_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:

    CkJsonObjectW_Clear(funcCall);
    CkJsonObjectW_UpdateString(funcCall,L"name",L"calculateTotal");
    CkJsonObjectW_UpdateNumber(funcCall,L"args[0][0]",L"10.50");
    CkJsonObjectW_UpdateNumber(funcCall,L"args[0][1]",L"20.00");
    CkJsonObjectW_UpdateNumber(funcCall,L"args[0][2]",L"5.25");
    wprintf(L"%s\n",CkJsonObjectW_emit(funcCall));


    CkStringBuilderW_Dispose(sbScript);
    CkJsW_Dispose(js);
    CkJsonObjectW_Dispose(result);
    CkJsonObjectW_Dispose(funcCall);
    CkJsonArrayW_Dispose(argsArray);
    CkJsonArrayW_Dispose(arg);
    CkStringBuilderW_Dispose(sbOut);

    }