Sample code for 30+ languages & platforms
C

Call a JavaScript Function Returning an Integer

See more JavaScript Examples

Demonstrates how to call a JavaScript function that returns an integer.

Chilkat C Downloads

C
#include <C_CkStringBuilder.h>
#include <C_CkJs.h>
#include <C_CkJsonObject.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkStringBuilder sbScript;
    HCkJs js;
    HCkJsonObject result;
    HCkJsonObject funcCall;
    int retval;

    success = FALSE;

    //  ----------------------------------------------------------------------------------
    //  The Javascript function called in this example is shown at the bottom of this page.
    //  -----------------------------------------------------------------------------------

    //  In this example, we'll load the Javascript function definition from a file.
    //  It doesn't need to come from a file.  It could just as easily be loaded from a string.
    sbScript = CkStringBuilder_Create();
    success = CkStringBuilder_LoadFile(sbScript,"js_call_function.js","utf-8");
    if (success == FALSE) {
        printf("%s\n",CkStringBuilder_lastErrorText(sbScript));
        CkStringBuilder_Dispose(sbScript);
        return;
    }

    //  Note: Each instance of a Chilkat Js object automatically establishes
    //  its own internal runtime and context.  Applications do not need to explicitly create
    //  the JavaScript runtime or context.
    js = CkJs_Create();

    result = CkJsonObject_Create();
    CkJsonObject_putEmitCompact(result,FALSE);

    //  Call Eval to add the function (shown at the bottom of this page) 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;
    }

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

    //  The expected output is "undefined":

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

    //     When Eval processes a script containing only a function declaration,
    //     it successfully performs the action (the function becomes defined).
    //     However, since the script consists of a statement that produces no value,
    //     the script's overall completion value is empty. In JavaScript, the
    //     absence of a value is represented by `undefined`.
    //  
    //     Therefore, the Eval call returns `undefined`.

    //  ------------------------------------------------------------------------------
    //  Call the function calculateScore("Player1", 10, 20)

    funcCall = CkJsonObject_Create();

    //  Create JSON defining the function call:

    //  {
    //    "name": "calculateScore",
    //    "args": [ "Player1", 10, 20 ]
    //  }

    CkJsonObject_UpdateString(funcCall,"name","calculateScore");
    CkJsonObject_UpdateString(funcCall,"args[0]","Player1");
    CkJsonObject_UpdateInt(funcCall,"args[1]",10);
    CkJsonObject_UpdateInt(funcCall,"args[2]",20);

    CkJsonObject_putEmitCompact(funcCall,FALSE);
    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);
        return;
    }

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

    //  Output:

    //  {
    //    "type": "int",
    //    "value": 37
    //  }

    retval = CkJsonObject_IntOf(result,"value");
    printf("retval = %d\n",retval);


    CkStringBuilder_Dispose(sbScript);
    CkJs_Dispose(js);
    CkJsonObject_Dispose(result);
    CkJsonObject_Dispose(funcCall);

    }