Moonglade

Modern Markdown blogging with AI-powered localization and search.


Using `readFile` as an example, how to implement it in Node.js

This article provides an in-depth analysis of the underlying implementation mechanisms of file reading operations in Node.js, with a focus on tracing the call chains of `fs.readFileSync` and `fs.readFile`. It points out that, whether synchronous or asynchronous, all operations ultimately converge on libuv's `uv_fs_read` function, with the execution mode distinguished by the presence or absence of a callback function. At the code level, it details how `FSReqCallback` and its base class `FSReqBase` encapsulate request state after entering the C++ layer from the JS layer, and how tasks are dispatched to the event loop through the `AsyncCall` and `Dispatch` mechanisms. For the asynchronous flow, it emphasizes how the `ReqWrap` template class adapts to the libuv interface, and how the `AfterInteger` callback function triggers the Promise's Resolve logic to return the result upon completion of the operation. Additionally, the article analyzes the details of permission checks and file descriptor management within the `open` function, revealing Node.js's meticulous considerations for platform compatibility and resource management at each level of abstraction. Finally, it summarizes legacy issues such as built-in module loading and asynchronous construction, aiming to help readers gain a deep understanding of Node.js's core architecture and design philosophy.

Views 186
2025 c++ daily javascript libuv nodejs source-code study v8

v8 build for Linux platform

This article documents the complete process of compiling V8 source code in an Ubuntu 24.04 environment and resolving linking issues. The author aims to obtain the V8 static library to research C++ and JS interoperation. However, the official documentation lacks explanations of the implicit relationships between build parameters, leading to two major challenges: first, the default linker reports an "unknown architecture" error; second, V8's custom C++ standard library results in undefined symbols. After extensive trial and error, it was found that using the lld linker resolves the architecture compatibility issue. Furthermore, setting `is_clang` to `false` and `use_custom_libcxx` to `false` in `args.gn`, combined with `v8_monolithic=true`, ultimately resulted in the successful generation of a static library that links and debugs correctly. The article reflects on the insufficient guidance in V8's official documentation regarding critical build configurations and suggests that maintainers improve parameter documentation to reduce developers' trial-and-error costs, ensuring that documentation serves as a guide rather than an obstacle.

Views 289
2025 c++ daily linux study v8