Sample code for 30+ languages & platforms
Unicode C

Call a JavaScript Function Passing an Object Argument

See more JavaScript Examples

Demonstrates how to call a JavaScript function with an argument that is an object.

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;
    HCkJsonObjectW arg;
    HCkJsonArrayW argsArray;
    HCkStringBuilderW sbOut;

    success = FALSE;

    // This is the JavaScript function we'll call:

    // function describeCar(car) {
    // 	console.log(`This is a ${car.year} ${car.make} ${car.model}.`);
    // }

    sbScript = CkStringBuilderW_Create();
    CkStringBuilderW_Append(sbScript,L"function describeCar(car) { console.log(`This is a ${car.year} ${car.make} ${car.model}.`); }");

    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 describeCar(car)

    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 object.

    // {
    //   "name": "describeCar",
    //   "args": [
    //     {
    //       "make": "Toyota",
    //       "model": "Corolla",
    //       "year": 2022
    //     }
    //   ]
    // }

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

    // Create the JSON object that is the argument.
    arg = CkJsonObjectW_Create();
    CkJsonObjectW_UpdateString(arg,L"make",L"Toyota");
    CkJsonObjectW_UpdateString(arg,L"model",L"Corolla");
    CkJsonObjectW_UpdateInt(arg,L"year",2022);

    // Create the arguments array.
    argsArray = CkJsonArrayW_Create();
    CkJsonArrayW_AddObjectCopyAt(argsArray,0,arg);

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

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

    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);
        CkJsonObjectW_Dispose(arg);
        CkJsonArrayW_Dispose(argsArray);
        return;
    }

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

    // The describeCar JavaScript function returns nothing. 
    // Therefore, the result is "undefined".

    // {
    //   "type": "undefined",
    //   "value": "undefined"
    // }

    // However, the function emitted text to the console.

    sbOut = CkStringBuilderW_Create();
    CkJsW_ConsoleOutputSb(js,sbOut);
    wprintf(L"%s\n",CkStringBuilderW_getAsString(sbOut));

    // Output:
    // This is a 2022 Toyota Corolla.

    // -----------------------------------------------------------
    // Note: If the object argument is simple, this is an alternative
    // and simpler way of creating the funcCall:

    CkJsonObjectW_Clear(funcCall);
    CkJsonObjectW_UpdateString(funcCall,L"name",L"describeCar");
    CkJsonObjectW_UpdateString(funcCall,L"args[0].make",L"Toyota");
    CkJsonObjectW_UpdateString(funcCall,L"args[0].model",L"Corolla");
    CkJsonObjectW_UpdateInt(funcCall,L"args[0].year",2022);
    wprintf(L"%s\n",CkJsonObjectW_emit(funcCall));


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

    }