PowerBuilder
PowerBuilder
Call a JavaScript Function Returning an Integer
See more JavaScript Examples
Demonstrates how to call a JavaScript function that returns an integer.Chilkat PowerBuilder Downloads
integer li_rc
integer li_Success
oleobject loo_SbScript
oleobject loo_Js
oleobject loo_Result
oleobject loo_FuncCall
integer li_Retval
li_Success = 0
// ----------------------------------------------------------------------------------
// The Javascript function called in this example is shown at the bottom of this page.
// -----------------------------------------------------------------------------------
// In this example, we'll load the Javascript function definition from a file.
// It doesn't need to come from a file. It could just as easily be loaded from a string.
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_call_function.js","utf-8")
if li_Success = 0 then
Write-Debug loo_SbScript.LastErrorText
destroy loo_SbScript
return
end if
// Note: Each instance of a Chilkat Js object automatically establishes
// its own internal runtime and context. Applications do not need to explicitly create
// the JavaScript runtime or context.
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 (shown at the bottom of this page) 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
Write-Debug loo_Result.Emit()
// The expected output is "undefined":
// {
// "type": "undefined",
// "value": "undefined"
// }
// When Eval processes a script containing only a function declaration,
// it successfully performs the action (the function becomes defined).
// However, since the script consists of a statement that produces no value,
// the script's overall completion value is empty. In JavaScript, the
// absence of a value is represented by `undefined`.
//
// Therefore, the Eval call returns `undefined`.
// ------------------------------------------------------------------------------
// Call the function calculateScore("Player1", 10, 20)
loo_FuncCall = create oleobject
li_rc = loo_FuncCall.ConnectToNewObject("Chilkat.JsonObject")
// Create JSON defining the function call:
// {
// "name": "calculateScore",
// "args": [ "Player1", 10, 20 ]
// }
loo_FuncCall.UpdateString("name","calculateScore")
loo_FuncCall.UpdateString("args[0]","Player1")
loo_FuncCall.UpdateInt("args[1]",10)
loo_FuncCall.UpdateInt("args[2]",20)
loo_FuncCall.EmitCompact = 0
Write-Debug loo_FuncCall.Emit()
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": "int",
// "value": 37
// }
li_Retval = loo_Result.IntOf("value")
Write-Debug "retval = " + string(li_Retval)
destroy loo_SbScript
destroy loo_Js
destroy loo_Result
destroy loo_FuncCall