Sample code for 30+ languages & platforms
PowerBuilder 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 PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_SbScript
oleobject loo_Js
oleobject loo_Result
oleobject loo_FuncCall

li_Success = 0

//  This is the JavaScript function we'll call:

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

loo_SbScript = create oleobject
li_rc = loo_SbScript.ConnectToNewObject("Chilkat.StringBuilder")
if li_rc < 0 then
    destroy loo_SbScript
    MessageBox("Error","Connecting to COM object failed")
    return
end if
loo_SbScript.Append("function getSettings() {")
loo_SbScript.Append("    return {")
loo_SbScript.Append("        theme: ~"dark~",")
loo_SbScript.Append("        notifications: true,")
loo_SbScript.Append("        version: 1.0")
loo_SbScript.Append("    };")
loo_SbScript.Append("}")

loo_Js = create oleobject
li_rc = loo_Js.ConnectToNewObject("Chilkat.Js")

loo_Result = create oleobject
li_rc = loo_Result.ConnectToNewObject("Chilkat.JsonObject")

loo_Result.EmitCompact = 0

//  Call Eval to add the function to the context's global object
li_Success = loo_Js.Eval(loo_SbScript,loo_Result)
if li_Success = 0 then
    //  Examine the result for an exception.
    Write-Debug loo_Result.Emit()

    //  Also examine the LastErrorText.
    Write-Debug loo_Js.LastErrorText
    destroy loo_SbScript
    destroy loo_Js
    destroy loo_Result
    return
end if

//  ------------------------------------------------------------------------------
//  Call the function getSettings()

loo_FuncCall = create oleobject
li_rc = loo_FuncCall.ConnectToNewObject("Chilkat.JsonObject")

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

//  {
//    "name": "getSettings",
//  }

loo_FuncCall.UpdateString("name","getSettings")

li_Success = loo_Js.CallFunction(loo_FuncCall,loo_Result)
if li_Success = 0 then
    //  Examine the result for an exception.
    Write-Debug loo_Result.Emit()

    //  Also examine the LastErrorText.
    Write-Debug loo_Js.LastErrorText
    destroy loo_SbScript
    destroy loo_Js
    destroy loo_Result
    destroy loo_FuncCall
    return
end if

Write-Debug loo_Result.Emit()

//  Output:
//  {
//    "type": "object",
//    "value": {
//      "theme": "dark",
//      "notifications": true,
//      "version": 1
//    }
//  }

//  Examine the object's members
Write-Debug "theme: " + loo_Result.StringOf("value.theme")
Write-Debug "notifications: " + string(loo_Result.BoolOf("value.notifications"))
Write-Debug "version: " + string(loo_Result.IntOf("value.version"))

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


destroy loo_SbScript
destroy loo_Js
destroy loo_Result
destroy loo_FuncCall