pub fn op_sync<F, A, B, R>(op_fn: F) -> Box<OpFn> where
    F: Fn(&mut OpState, A, B) -> Result<R, Error> + 'static,
    A: DeserializeOwned,
    B: DeserializeOwned,
    R: Serialize + 'static, 
Expand description

Creates an op that passes data synchronously using JSON.

The provided function op_fn has the following parameters:

  • &mut OpState: the op state, can be used to read/write resources in the runtime from an op.
  • V: the deserializable value that is passed to the Rust function.
  • &mut [ZeroCopyBuf]: raw bytes passed along, usually not needed if the JSON value is used.

op_fn returns a serializable value, which is directly returned to JavaScript.

When registering an op like this…

let mut runtime = JsRuntime::new(...);
runtime.register_op("hello", deno_core::op_sync(Self::hello_op));
runtime.sync_ops_cache();

…it can be invoked from JS using the provided name, for example:

let result = Deno.core.opSync("hello", args);

runtime.sync_ops_cache() must be called after registering new ops A more complete example is available in the examples directory.