6.8 Sync OPs
Overview
Synchronous operations halt the ongoing execution of the current thread (whether it's the main thread or a worker) until a result becomes available. Many operations in Deno are of the synchronous kind. File system operations are the exception, as they are available in both synchronous and asynchronous forms. This distinction exists because file system operations can consume a significant amount of time. For other scenarios, either synchronous operations are not logical or the operations are so straightforward that implementing them as asynchronous would be excessive.
In our example, we will focus on a synchronous operation called Deno.env.get
. This operation is uncomplicated yet highly instructive for grasping the underlying principles. The purpose of this operation is to take an input and produce a corresponding output. To thoroughly comprehend how this operation functions from start to finish, we will dissect it step by step. Our exploration will encompass both JavaScript and Rust implementations.
JS part
User code
In the JS space, the application makes a call to get the value of an environment variable named HOME:
Deno.env.get
The Deno.env.get function is mapped to getEnv:
getEnv
This immediately calls the JS function getEnv as env.get is mapped to getEnv:
OPS
The Deno core gives us the global ops object. As we discussed earlier, the ops object is populated using the registerOp API, which is called when Deno starts up. Deno core handles the registration of every supported op. Here's a brief reminder of how the registerOp function looks:
The __op__registerOp function is called during the initialization of context. There are three inputs for each op:
isAsync: True if the op is async
Op: This is the Rust-side ID of the op
OpName: The name of the OP
Rust code
As op is registered as an external reference, V8 makes an external function call (like C extern functions).
--
That was all about sync ops. It is worth noting that the sync ops are quite easy compared to async ops. Async ops are quite complex to implement. The next revision of this book (4th edition) will have a new chapter, Chapter 8, dedicated to async ops.
Last updated