Sample code for 30+ languages & platforms
PureBasic

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 PureBasic Downloads

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

Procedure ChilkatExample()

    success.i = 0

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

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

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

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

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

    CkJsonObject::setCkEmitCompact(funcCall, 0)

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

    CkJsonObject::ckUpdateString(funcCall,"name","calculateTotal")

    ; Create the arguments array.
    argsArray.i = CkJsonArray::ckCreate()
    If argsArray.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ; The 1st argument in the arguments array is itself an array.
    ; Passing -1 indicates to append to the array.
    arg.i = CkJsonArray::ckCreate()
    If arg.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonArray::ckAddArrayAt2(argsArray,-1,arg)

    ; Fill in the values for the 1st argument.
    CkJsonArray::ckAddNumberAt(arg,-1,"10.50")
    CkJsonArray::ckAddNumberAt(arg,-1,"20.00")
    CkJsonArray::ckAddNumberAt(arg,-1,"5.25")

    ; Add the "args" array to the funcCall.
    CkJsonObject::ckAppendArrayCopy(funcCall,"args",argsArray)

    Debug CkJsonObject::ckEmit(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 = 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)
        CkJsonArray::ckDispose(argsArray)
        CkJsonArray::ckDispose(arg)
        ProcedureReturn
    EndIf

    Debug CkJsonObject::ckEmit(result)

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

    ; The function also emitted text to the console.

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

    CkJs::ckConsoleOutputSb(js,sbOut)
    Debug CkStringBuilder::ckGetAsString(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:

    CkJsonObject::ckClear(funcCall)
    CkJsonObject::ckUpdateString(funcCall,"name","calculateTotal")
    CkJsonObject::ckUpdateNumber(funcCall,"args[0][0]","10.50")
    CkJsonObject::ckUpdateNumber(funcCall,"args[0][1]","20.00")
    CkJsonObject::ckUpdateNumber(funcCall,"args[0][2]","5.25")
    Debug CkJsonObject::ckEmit(funcCall)


    CkStringBuilder::ckDispose(sbScript)
    CkJs::ckDispose(js)
    CkJsonObject::ckDispose(result)
    CkJsonObject::ckDispose(funcCall)
    CkJsonArray::ckDispose(argsArray)
    CkJsonArray::ckDispose(arg)
    CkStringBuilder::ckDispose(sbOut)


    ProcedureReturn
EndProcedure