Sample code for 30+ languages & platforms
Tcl

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 Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

# This is the JavaScript function we'll call:

# function describeCar(car) {
# 	console.log(`This is a ${car.year} ${car.make} ${car.model}.`);
# }

set sbScript [new_CkStringBuilder]

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

set js [new_CkJs]

set result [new_CkJsonObject]

CkJsonObject_put_EmitCompact $result 0

# Call Eval to add the function to the context's global object
set success [CkJs_Eval $js $sbScript $result]
if {$success == 0} then {
    # Examine the result for an exception.
    puts [CkJsonObject_emit $result]

    # Also examine the LastErrorText.
    puts [CkJs_lastErrorText $js]
    delete_CkStringBuilder $sbScript
    delete_CkJs $js
    delete_CkJsonObject $result
    exit
}

# ------------------------------------------------------------------------------
# Call the function describeCar(car)

set funcCall [new_CkJsonObject]

CkJsonObject_put_EmitCompact $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_UpdateString $funcCall "name" "describeCar"

# Create the JSON object that is the argument.
set arg [new_CkJsonObject]

CkJsonObject_UpdateString $arg "make" "Toyota"
CkJsonObject_UpdateString $arg "model" "Corolla"
CkJsonObject_UpdateInt $arg "year" 2022

# Create the arguments array.
set argsArray [new_CkJsonArray]

CkJsonArray_AddObjectCopyAt $argsArray 0 $arg

# Add the "args" array to the funcCall.
CkJsonObject_AppendArrayCopy $funcCall "args" $argsArray

puts [CkJsonObject_emit $funcCall]

set success [CkJs_CallFunction $js $funcCall $result]
if {$success == 0} then {
    # Examine the result for an exception.
    puts [CkJsonObject_emit $result]

    # Also examine the LastErrorText.
    puts [CkJs_lastErrorText $js]
    delete_CkStringBuilder $sbScript
    delete_CkJs $js
    delete_CkJsonObject $result
    delete_CkJsonObject $funcCall
    delete_CkJsonObject $arg
    delete_CkJsonArray $argsArray
    exit
}

puts [CkJsonObject_emit $result]

# The describeCar JavaScript function returns nothing. 
# Therefore, the result is "undefined".

# {
#   "type": "undefined",
#   "value": "undefined"
# }

# However, the function emitted text to the console.

set sbOut [new_CkStringBuilder]

CkJs_ConsoleOutputSb $js $sbOut
puts [CkStringBuilder_getAsString $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_Clear $funcCall
CkJsonObject_UpdateString $funcCall "name" "describeCar"
CkJsonObject_UpdateString $funcCall "args[0].make" "Toyota"
CkJsonObject_UpdateString $funcCall "args[0].model" "Corolla"
CkJsonObject_UpdateInt $funcCall "args[0].year" 2022
puts [CkJsonObject_emit $funcCall]

delete_CkStringBuilder $sbScript
delete_CkJs $js
delete_CkJsonObject $result
delete_CkJsonObject $funcCall
delete_CkJsonObject $arg
delete_CkJsonArray $argsArray
delete_CkStringBuilder $sbOut