Creating custom ops for deno_core that take in arrays and strucks
I am interested in creating custom ops for deno_core.
I have one case where I want to take in a
Vec<String>
and return a Result that possibly includes my own custom enum type or struct as a result.
Secondly I want to be able to pass my own custom struct to my op.
What do I need to do to achieve this?2 Replies
I wasn't able to find the answer to your question in the official Deno documentation. However, based on your message, I can provide some general advice.
When creating custom ops for
deno_core
, you typically need to define the ops in Rust and then expose them to your JavaScript or TypeScript code. Here's a brief overview of what you might need to do:
1. Define your custom op in Rust, making sure it accepts a Vec<String>
as an argument. You'll need to use the op_sync
or op_async
macros provided by deno_core
to define your op.
2. Handle the serialization and deserialization of arguments and return values between Rust and JavaScript. You can use serde for serializing your custom enum type or struct.
3. Register your op with the Deno runtime by adding it to the op table when you initialize the runtime.
4. In your JavaScript or TypeScript code, you'll need to use Deno.core.opSync
or Deno.core.opAsync
to invoke your custom op, passing the necessary arguments.
Please note that this advice is not based on the Deno documentation, and you should double-check the accuracy of the advice given. Since working with deno_core
involves Rust and potentially complex interactions between Rust and JavaScript, you may want to look at existing examples in the Deno codebase or seek out community examples that demonstrate similar patterns.Any chance of getting a human response?