Sample code for 30+ languages & platforms
Rust Requires Chilkat v11.4.0+

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

Rust

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

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

let sb_script = chilkat::StringBuilder::new();
let _ = sb_script.append("function describeCar(car) { console.log(`This is a ${car.year} ${car.make} ${car.model}.`); }");

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 describeCar(car)

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 object.

// {
//   "name": "describeCar",
//   "args": [
//     {
//       "make": "Toyota",
//       "model": "Corolla",
//       "year": 2022
//     }
//   ]
// }

let _ = func_call.update_string("name", "describeCar");

// Create the JSON object that is the argument.
let arg = chilkat::JsonObject::new();
let _ = arg.update_string("make", "Toyota");
let _ = arg.update_string("model", "Corolla");
let _ = arg.update_int("year", 2022);

// Create the arguments array.
let args_array = chilkat::JsonArray::new();
let _ = args_array.add_object_copy_at(0, &arg);

// Add the "args" array to the funcCall.
let _ = func_call.append_array_copy("args", &args_array);

println!("{}", func_call.emit().unwrap_or_default());

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());

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

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

// However, the function 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:
// This is a 2022 Toyota Corolla.

// -----------------------------------------------------------
// Note: If the object argument is simple, this is an alternative
// and simpler way of creating the funcCall:

func_call.clear();
let _ = func_call.update_string("name", "describeCar");
let _ = func_call.update_string("args[0].make", "Toyota");
let _ = func_call.update_string("args[0].model", "Corolla");
let _ = func_call.update_int("args[0].year", 2022);
println!("{}", func_call.emit().unwrap_or_default());