Sample code for 30+ languages & platforms
PureBasic

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 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 describeCar(car) {
    ; 	console.log(`This is a ${car.year} ${car.make} ${car.model}.`);
    ; }

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

    CkStringBuilder::ckAppend(sbScript,"function describeCar(car) { console.log(`This is a ${car.year} ${car.make} ${car.model}.`); }")

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

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

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

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

    ; Create the JSON object that is the argument.
    arg.i = CkJsonObject::ckCreate()
    If arg.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckUpdateString(arg,"make","Toyota")
    CkJsonObject::ckUpdateString(arg,"model","Corolla")
    CkJsonObject::ckUpdateInt(arg,"year",2022)

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

    CkJsonArray::ckAddObjectCopyAt(argsArray,0,arg)

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

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

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

    CkJs::ckConsoleOutputSb(js,sbOut)
    Debug CkStringBuilder::ckGetAsString(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::ckClear(funcCall)
    CkJsonObject::ckUpdateString(funcCall,"name","describeCar")
    CkJsonObject::ckUpdateString(funcCall,"args[0].make","Toyota")
    CkJsonObject::ckUpdateString(funcCall,"args[0].model","Corolla")
    CkJsonObject::ckUpdateInt(funcCall,"args[0].year",2022)
    Debug CkJsonObject::ckEmit(funcCall)


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


    ProcedureReturn
EndProcedure