diff options
author | Jan Haller <bromeon@gmail.com> | 2020-11-06 02:26:14 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-11-06 12:26:14 +1100 |
commit | 6162807a7e8971524c9d8df746916be83d81b649 (patch) | |
tree | 61615feab63ec21682f8fcb2a11ae5f6df392396 /core/ops.rs | |
parent | 5f7c80986fd9cff6a65d4e419fc2972abe992ccf (diff) |
docs(core): document several concepts around JsRuntime and ops (#7897)
Diffstat (limited to 'core/ops.rs')
-rw-r--r-- | core/ops.rs | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/core/ops.rs b/core/ops.rs index 3351b6a5b..ed74ce873 100644 --- a/core/ops.rs +++ b/core/ops.rs @@ -31,6 +31,7 @@ pub enum Op { NotFound, } +/// Maintains the resources and ops inside a JS runtime. pub struct OpState { pub resource_table: crate::ResourceTable, pub op_table: OpTable, @@ -151,6 +152,29 @@ fn op_table() { ) } +/// 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. +/// * `Value`: the JSON 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 JSON value, which is directly returned to JavaScript. +/// +/// When registering an op like this... +/// ```ignore +/// let mut runtime = JsRuntime::new(...); +/// runtime.register_op("hello", deno_core::json_op_sync(Self::hello_op)); +/// ``` +/// +/// ...it can be invoked from JS using the provided name, for example: +/// ```js +/// Deno.core.ops(); +/// let result = Deno.core.jsonOpSync("function_name", args); +/// ``` +/// +/// The `Deno.core.ops()` statement is needed once before any op calls, for initialization. +/// A more complete example is available in the examples directory. pub fn json_op_sync<F>(op_fn: F) -> Box<OpFn> where F: Fn(&mut OpState, Value, &mut [ZeroCopyBuf]) -> Result<Value, AnyError> @@ -166,6 +190,30 @@ where }) } +/// Creates an op that passes data asynchronously using JSON. +/// +/// The provided function `op_fn` has the following parameters: +/// * `Rc<RefCell<OpState>`: the op state, can be used to read/write resources in the runtime from an op. +/// * `Value`: the JSON value that is passed to the Rust function. +/// * `BufVec`: raw bytes passed along, usually not needed if the JSON value is used. +/// +/// `op_fn` returns a future, whose output is a JSON value. This value will be asynchronously +/// returned to JavaScript. +/// +/// When registering an op like this... +/// ```ignore +/// let mut runtime = JsRuntime::new(...); +/// runtime.register_op("hello", deno_core::json_op_async(Self::hello_op)); +/// ``` +/// +/// ...it can be invoked from JS using the provided name, for example: +/// ```js +/// Deno.core.ops(); +/// let future = Deno.core.jsonOpAsync("function_name", args); +/// ``` +/// +/// The `Deno.core.ops()` statement is needed once before any op calls, for initialization. +/// A more complete example is available in the examples directory. pub fn json_op_async<F, R>(op_fn: F) -> Box<OpFn> where F: Fn(Rc<RefCell<OpState>>, Value, BufVec) -> R + 'static, |