Rust Requires Chilkat v11.4.0+
Rust
Call a JavaScript Function Passing an Array Argument
See more JavaScript Examples
Demonstrates how to call a JavaScript function with an argument that is an array.Chilkat Rust Downloads
// This is the JavaScript function we'll call:
// function calculateTotal(numbers) {
// console.log(numbers);
// let total = 0;
//
// // Loop through every number in the array
// for (const num of numbers) {
// console.log(num);
// total += num;
// }
//
// return total;
// }
let sb_script = chilkat::StringBuilder::new();
if sb_script.load_file("js_function_array_arg.js", "utf-8").is_err() {
println!("{}", sb_script.last_error_text());
return;
}
let js = chilkat::Js::new();
let result = chilkat::JsonObject::new();
result.set_emit_compact(false);
// Call Eval to add the function to the context's global object
if js.eval(&sb_script, &result).is_err() {
// Examine the result for an exception.
println!("{}", result.emit().unwrap_or_default());
// Also examine the LastErrorText.
println!("{}", js.last_error_text());
return;
}
// ------------------------------------------------------------------------------
// Call the function calculateTotal(numbers)
let func_call = chilkat::JsonObject::new();
func_call.set_emit_compact(false);
// Create JSON specifying the function name and arguments
// In this case, there is only 1 argument, and it is an array.
let _ = func_call.update_string("name", "calculateTotal");
// Create the arguments array.
let args_array = chilkat::JsonArray::new();
// The 1st argument in the arguments array is itself an array.
// Passing -1 indicates to append to the array.
let arg = chilkat::JsonArray::new();
let _ = args_array.add_array_at2(-1, &arg);
// Fill in the values for the 1st argument.
let _ = arg.add_number_at(-1, "10.50");
let _ = arg.add_number_at(-1, "20.00");
let _ = arg.add_number_at(-1, "5.25");
// Add the "args" array to the funcCall.
let _ = func_call.append_array_copy("args", &args_array);
println!("{}", func_call.emit().unwrap_or_default());
// The funcCall is as follows. Notice that the 1st (and only) argument is an array.
// {
// "name": "calculateTotal",
// "args": [
// [
// 10.50,
// 20.00,
// 5.25
// ]
// ]
// }
if js.call_function(&func_call, &result).is_err() {
// Examine the result for an exception.
println!("{}", result.emit().unwrap_or_default());
// Also examine the LastErrorText.
println!("{}", js.last_error_text());
return;
}
println!("{}", result.emit().unwrap_or_default());
// Result:
// {
// "type": "double",
// "value": 35.75
// }
// The function also emitted text to the console.
let sb_out = chilkat::StringBuilder::new();
js.console_output_sb(&sb_out);
println!("{}", sb_out.get_as_string().unwrap_or_default());
// Output:
// 10.5,20,5.25
// 10.5
// 20
// 5.25
// -----------------------------------------------------------
// Note: If the array argument is simple, this is an alternative
// and simpler way of creating the funcCall:
func_call.clear();
let _ = func_call.update_string("name", "calculateTotal");
let _ = func_call.update_number("args[0][0]", "10.50");
let _ = func_call.update_number("args[0][1]", "20.00");
let _ = func_call.update_number("args[0][2]", "5.25");
println!("{}", func_call.emit().unwrap_or_default());
JavaScript
function calculateTotal(numbers) {
console.log(numbers);
let total = 0;
// Loop through every number in the array
for (const num of numbers) {
console.log(num);
total += num;
}
return total;
}