javascriptnode.jsv8libuv

how nodejs actually interacts with the v8 engine


I am struggling to find out exactly how nodejs interact with v8 engine . Let's say I have some js code written and when I execute it , it goes to v8 engine for execution and let's say that js code contains some function given us by nodejs . In this scenario , how nodejs will know or how v8 will handle this external function call because v8 engine we know is independent of runtime environment.

I tried finding the articles explaining how nodejs actually interacts with v8 but didn't find any good article on that.


Solution

  • The environment that is running V8 can add new functions to the V8 environment, whether those be objects or global functions and it can then provide either a pure JS implementation for those or it can link in external code that can receive function arguments, return results and interact with garbage collection. This is what both the browser and nodejs do to support things specific to their environment.

    In case you didn't know, even things like setTimeout() are external to the JS specification so Chrome provides an implementation of that for use in a browser and nodejs provides a different implementation for use in nodejs.

    If you want to get more technical, you can read about the V8 engine here and how to embed or extend it here (including some programming examples).