0

Node JS executes the codebase in the Javascript VX (V8 currently) engine, and executes every asynchronous context in its own VM.

Given this information, is there an API that allows to find in which VM context the current code execution is taking place?

I am attempting to create context-specific globals.

Is this possible? Is my understanding of Node correct?

Philippe Hebert
  • 1,616
  • 2
  • 24
  • 51
  • “executes every asynchronous context in its own VM” – what’s an asynchronous context? – Ry- Mar 22 '17 at 23:08
  • I do not know whether or not I am using the proper terminology here. I think the easiest way to think about it is to think of it as an expressJS request. Every expressJS has it's own execution context, and it is managed asynchronously by NodeJS' main event loop. As such you have an asynchronous context. It is also similar in a way to a thread's stack/heap in a way. – Philippe Hebert Mar 22 '17 at 23:40
  • No, it’s not, sorry. It’s just a function call managed by an event loop. They all run in the same “context”. What are you trying to accomplish with a “context-specific global”? – Ry- Mar 22 '17 at 23:41
  • Ah, that saddens me. I'm trying to make the req and res objects from an expressJS request available throughout the stack so that I don't have to pass them in function signatures. – Philippe Hebert Mar 23 '17 at 00:12
  • That kind of thing, which you might see implemented using thread locals in other languages, isn’t really good design anyway. As an easy sort of starting pattern, I’d put all request context on the request object, pass it around, and return a promise representing the response instead of relying on it being passed in. – Ry- Mar 23 '17 at 01:15
  • I'm aware that it's not an optimal pattern, and I'd avoid it if preferable. However it is really useful for logging, debugging, and error-handling purposes. – Philippe Hebert Mar 23 '17 at 01:47
  • `Every expressJS has it's own execution context` that's not true. Every expressJS request passes the request object down the chain of functions that process requests. You have to realise that expressJS request processing pipeline is not object oriented, it's procedural – slebetman Mar 23 '17 at 02:46
  • 1
    Just to clarify: "VX" is an extremely toxic chemical weapon; "V8" is an open-source JavaScript engine (used by e.g. Chrome and Node.js). – jmrk Mar 23 '17 at 15:36

2 Answers2

1

Context specific globals? Isn't that a contradiction?

Javascript uses closures. So you want to declare and create functions within functions and this will establish and preserve your concept of "context specific globals".

Steve Harris
  • 306
  • 2
  • 4
  • Indeed, javascript uses closures. However you cannot propagate a closure beyond the limit of the file in which it is written. Thus you cannot have closures that extend beyond the module level. I don't think that context-specific global is a contradiction either. Because these are global for the complete context of execution, just not cross-context. It's essentially the same as a global under a thread. It is _global_ within the thread. – Philippe Hebert Mar 22 '17 at 23:37
1

Your understanding of node is incorrect. Specifically you said:

Javascript VX (V8 currently) engine, and executes every asynchronous context in its own VM

No. Javascript has only one context. The current VM. There are multithreading extensions in node.js such as webworker-threads but they're not often used because:

  1. They rarely improve throughput and almost always increase latency
  2. They're almost never necessary because almost nothing in node.js blocks

If you want a slightly more low-level technical detail of how asynchronous execution work in node.js see my answer to this question: I know that callback function runs asynchronously, but why?

Community
  • 1
  • 1
slebetman
  • 109,858
  • 19
  • 140
  • 171