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

C
#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;
    HCkJsonObject arg;
    HCkJsonArray argsArray;
    HCkStringBuilder 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 = CkStringBuilder_Create();
    CkStringBuilder_Append(sbScript,"function describeCar(car) { console.log(`This is a ${car.year} ${car.make} ${car.model}.`); }");

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

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

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

    CkJsonObject_UpdateString(funcCall,"name","describeCar");

    // Create the JSON object that is the argument.
    arg = CkJsonObject_Create();
    CkJsonObject_UpdateString(arg,"make","Toyota");
    CkJsonObject_UpdateString(arg,"model","Corolla");
    CkJsonObject_UpdateInt(arg,"year",2022);

    // Create the arguments array.
    argsArray = CkJsonArray_Create();
    CkJsonArray_AddObjectCopyAt(argsArray,0,arg);

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

    printf("%s\n",CkJsonObject_emit(funcCall));

    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);
        CkJsonObject_Dispose(arg);
        CkJsonArray_Dispose(argsArray);
        return;
    }

    printf("%s\n",CkJsonObject_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 = CkStringBuilder_Create();
    CkJs_ConsoleOutputSb(js,sbOut);
    printf("%s\n",CkStringBuilder_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:

    CkJsonObject_Clear(funcCall);
    CkJsonObject_UpdateString(funcCall,"name","describeCar");
    CkJsonObject_UpdateString(funcCall,"args[0].make","Toyota");
    CkJsonObject_UpdateString(funcCall,"args[0].model","Corolla");
    CkJsonObject_UpdateInt(funcCall,"args[0].year",2022);
    printf("%s\n",CkJsonObject_emit(funcCall));


    CkStringBuilder_Dispose(sbScript);
    CkJs_Dispose(js);
    CkJsonObject_Dispose(result);
    CkJsonObject_Dispose(funcCall);
    CkJsonObject_Dispose(arg);
    CkJsonArray_Dispose(argsArray);
    CkStringBuilder_Dispose(sbOut);

    }