Sample code for 30+ languages & platforms
PureBasic

Call a JavaScript Function Returning an Integer

See more JavaScript Examples

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

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkJs.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkJsonObject.pb"

Procedure ChilkatExample()

    success.i = 0

    ;  ----------------------------------------------------------------------------------
    ;  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.i = CkStringBuilder::ckCreate()
    If sbScript.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkStringBuilder::ckLoadFile(sbScript,"js_call_function.js","utf-8")
    If success = 0
        Debug CkStringBuilder::ckLastErrorText(sbScript)
        CkStringBuilder::ckDispose(sbScript)
        ProcedureReturn
    EndIf

    ;  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.i = CkJs::ckCreate()
    If js.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    result.i = CkJsonObject::ckCreate()
    If result.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::setCkEmitCompact(result, 0)

    ;  Call Eval to add the function (shown at the bottom of this page) to the context's global object
    success = CkJs::ckEval(js,sbScript,result)
    If success = 0
        ;  Examine the result for an exception.
        Debug CkJsonObject::ckEmit(result)

        ;  Also examine the LastErrorText.
        Debug CkJs::ckLastErrorText(js)
        CkStringBuilder::ckDispose(sbScript)
        CkJs::ckDispose(js)
        CkJsonObject::ckDispose(result)
        ProcedureReturn
    EndIf

    Debug CkJsonObject::ckEmit(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.i = CkJsonObject::ckCreate()
    If funcCall.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ;  Create JSON defining the function call:

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

    CkJsonObject::ckUpdateString(funcCall,"name","calculateScore")
    CkJsonObject::ckUpdateString(funcCall,"args[0]","Player1")
    CkJsonObject::ckUpdateInt(funcCall,"args[1]",10)
    CkJsonObject::ckUpdateInt(funcCall,"args[2]",20)

    CkJsonObject::setCkEmitCompact(funcCall, 0)
    Debug CkJsonObject::ckEmit(funcCall)

    success = CkJs::ckCallFunction(js,funcCall,result)
    If success = 0
        ;  Examine the result for an exception.
        Debug CkJsonObject::ckEmit(result)

        ;  Also examine the LastErrorText.
        Debug CkJs::ckLastErrorText(js)
        CkStringBuilder::ckDispose(sbScript)
        CkJs::ckDispose(js)
        CkJsonObject::ckDispose(result)
        CkJsonObject::ckDispose(funcCall)
        ProcedureReturn
    EndIf

    Debug CkJsonObject::ckEmit(result)

    ;  Output:

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

    retval.i = CkJsonObject::ckIntOf(result,"value")
    Debug "retval = " + Str(retval)


    CkStringBuilder::ckDispose(sbScript)
    CkJs::ckDispose(js)
    CkJsonObject::ckDispose(result)
    CkJsonObject::ckDispose(funcCall)


    ProcedureReturn
EndProcedure
JavaScript
function calculateScore(name, pointsA, pointsB) {
    // Return length of name + both scores
    return name.length + pointsA + pointsB;
    }