Sample code for 30+ languages & platforms
PureBasic Requires Chilkat v11.4.0+

Call a JavaScript Function Returning an Object

See more JavaScript Examples

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

Chilkat PureBasic Downloads

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

Procedure ChilkatExample()

    success.i = 0

    ;  This is the JavaScript function we'll call:

    ;  function getSettings() {
    ;      return {
    ;          theme: "dark",
    ;          notifications: true,
    ;          version: 1.0
    ;      };
    ;  }

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

    CkStringBuilder::ckAppend(sbScript,"function getSettings() {")
    CkStringBuilder::ckAppend(sbScript,"    return {")
    CkStringBuilder::ckAppend(sbScript,"        theme: " + Chr(34) + "dark" + Chr(34) + ",")
    CkStringBuilder::ckAppend(sbScript,"        notifications: true,")
    CkStringBuilder::ckAppend(sbScript,"        version: 1.0")
    CkStringBuilder::ckAppend(sbScript,"    };")
    CkStringBuilder::ckAppend(sbScript,"}")

    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 getSettings()

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

    ;  Create JSON specifying the function name and arguments
    ;  The function has no arguments, so we only specify the name.

    ;  {
    ;    "name": "getSettings",
    ;  }

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

    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": "object",
    ;    "value": {
    ;      "theme": "dark",
    ;      "notifications": true,
    ;      "version": 1
    ;    }
    ;  }

    ;  Examine the object's members
    Debug "theme: " + CkJsonObject::ckStringOf(result,"value.theme")
    Debug "notifications: " + Str(CkJsonObject::ckBoolOf(result,"value.notifications"))
    Debug "version: " + Str(CkJsonObject::ckIntOf(result,"value.version"))

    ;  Output:
    ;  theme: dark
    ;  notifications: True
    ;  version: 1


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


    ProcedureReturn
EndProcedure