Chilkat Examples

ChilkatHOMEAndroid™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi DLLGoJavaJavaScriptNode.jsObjective-CPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwiftTclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

PowerBuilder Examples
Web API Categories

AI
ASN.1
AWS KMS
AWS Misc
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Box
CAdES
CSR
CSV
Cert Store
Certificates
Cloud Signature CSC
Code Signing
Compression
DKIM / DomainKey
DNS
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text
HTTP
HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
JavaScript
MHT / HTML Email
MIME
Markdown
Microsoft Graph
Misc
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
Regular Expressions
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
Secrets
SharePoint
SharePoint Online
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
X
XAdES
XML
XML Digital Signatures
XMP
Zip
curl
uncategorized

 

 

 

(PowerBuilder) Call a JavaScript Function Returning an Array

See more JavaScript Examples
Demonstrates how to call a JavaScript function that returns an array.

Note: This example requires Chilkat v11.4.0 or greater.

Chilkat ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

integer li_rc
integer li_Success
oleobject loo_SbScript
oleobject loo_Js
oleobject loo_Result
oleobject loo_FuncCall
integer li_Count
integer i

li_Success = 0

// ----------------------------------------------------------------------------------
// The Javascript functions called in this example are shown at the bottom of this page.
// -----------------------------------------------------------------------------------

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
li_Success = loo_SbScript.LoadFile("js_function_returning_array.js","utf-8")
if li_Success = 0 then
    Write-Debug loo_SbScript.LastErrorText
    destroy loo_SbScript
    return
end if

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

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.

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

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": "array",
//   "value": [
//     "Monday",
//     "Tuesday",
//     "Wednesday",
//     "Thursday",
//     "Friday"
//   ]
// }

// Access each array value..
li_Count = loo_Result.SizeOfArray("value")
i = 0
do while i < li_Count
    loo_Result.I = i
    Write-Debug loo_Result.StringOf("value[i]")
    i = i + 1
loop

// ------------------------------------------------------------------------------
// Call the getRange(start,end) function

loo_FuncCall.Clear()
loo_FuncCall.UpdateString("name","getRange")
loo_FuncCall.UpdateInt("args[0]",14)
loo_FuncCall.UpdateInt("args[1]",21)
li_Success = loo_Js.CallFunction(loo_FuncCall,loo_Result)
Write-Debug loo_Result.Emit()

// Output:
// {
//   "type": "array",
//   "value": [
//     14,
//     15,
//     16,
//     17,
//     18,
//     19,
//     20,
//     21
//   ]
// }

li_Count = loo_Result.SizeOfArray("value")
i = 0
do while i < li_Count
    loo_Result.I = i
    Write-Debug string(loo_Result.IntOf("value[i]"))
    i = i + 1
loop

// ------------------------------------------------------------------------------
// Call the getEmployees() function

loo_FuncCall.Clear()
loo_FuncCall.UpdateString("name","getEmployees")
li_Success = loo_Js.CallFunction(loo_FuncCall,loo_Result)
Write-Debug loo_Result.Emit()

// Output:
// {
//   "type": "array",
//   "value": [
//     {
//       "id": 101,
//       "name": "Alice",
//       "role": "Dev"
//     },
//     {
//       "id": 102,
//       "name": "Bob",
//       "role": "Manager"
//     }
//   ]
// }

li_Count = loo_Result.SizeOfArray("value")
i = 0
do while i < li_Count
    loo_Result.I = i
    Write-Debug "name: " + loo_Result.StringOf("value[i].name")
    Write-Debug "role: " + loo_Result.StringOf("value[i].role")
    Write-Debug "id: " + string(loo_Result.IntOf("value[i].id"))
    Write-Debug ""
    i = i + 1
loop


destroy loo_SbScript
destroy loo_Js
destroy loo_Result
destroy loo_FuncCall


function getDays() {
    return ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"];
}

function getRange(start, end) {
    var arr = [];
    for (var i = start; i <= end; i++) {
        arr.push(i);
    }
    return arr;
}
// Usage: getRange(1, 5) -> [1, 2, 3, 4, 5]

function getEmployees() {
    return [
        { id: 101, name: "Alice", role: "Dev" },
        { id: 102, name: "Bob", role: "Manager" }
    ];
}
 

© 2000-2026 Chilkat Software, Inc. All Rights Reserved.