1

How does the chrome browser supply host objects to the native JavaScript environment? Are the host objects implemented in C++ and then linked with the V8 source code ?

How is the Window object made available for the programmer to use? Clearly, the JavaScript environment on the browser is different from the node.js environment. Node.js and chrome both use the v8 engine but they provide two different JavaScript environments. I was wondering how this was made possible.

nnkparikh
  • 313
  • 2
  • 7
  • Not really sure what you asking, internally the V8 will be using C++ / assembler etc. But the interpreted JS will need some form of binding to these internal structures. Node.js, would be very similar here, so maybe this might help. -> https://stackoverflow.com/questions/24042861/nodejs-what-does-process-binding-mean – Keith Oct 13 '17 at 09:54
  • how does the browser provide host objects like Window, Document, etc..? V8 has no notion of these objects. – nnkparikh Oct 13 '17 at 10:31
  • 1
    Do you call _node.js_ n native environment? What do you mean with `V8 has no notion of these objects.`. The browser is not more or less a native then _node.js_ is, both extend the native types provided by the V8 engine with additional API which is written in C++ and JS. And V8 provides an API to define classes in C++ and to communicate between C++ and JS [v8: Embedder's Guide](https://github.com/v8/v8/wiki/Embedder's-Guide). You often have a mixture of JS and C++ for objects like Window, Nodes, Streams, ... – t.niese Oct 13 '17 at 10:49
  • ^ I think youve answered my question. Im not calling node.js a native environment. I apologize, the wording in my question is awkward. When I say "V8 has no notion of these objects" im referring to the objects available only in the browser environment. – nnkparikh Oct 13 '17 at 10:55
  • The browser is just a very fancy rendering engine, Chrome will be exposing these using some form of bindings to the V8 engine. `"V8 has no notion of these objects"` , V8 doesn't, but the bindings do. In some respects it's no different to `dll's`, it's just the bindings are bit more involved.. – Keith Oct 13 '17 at 11:54
  • "*Are the host objects implemented in C++ and then linked with the V8 source code?*" - Yes, exactly that. (Though I'm not sure what you understand as "linked") – Bergi Oct 13 '17 at 12:51

1 Answers1

3

V8 developer here. As the comments on the question already mention, V8 has an API for embedders, which embedders use to expose whatever objects they need to JavaScript. The choice is entirely up to the embedder here: Chrome does one thing, node.js another, your own application (if you write one) could do something completely different.

jmrk
  • 34,271
  • 7
  • 59
  • 74