How to add a new class to Google V8?

好久不见. 提交于 2019-12-05 16:57:15

问题


I'm a new comer in Google V8 and Javascript, and I'm trying to add a new class to Javascript using C++.

I've finished some work using Webkit's V8 binding, references are: webkit idl and v8 binding

Now I want to integrate it into V8 engine directly, by modifying V8's code instead of simply using V8's api to make a extension. In other words, I want to add a new class just like Array type in Javascript, using the same implementation mechanism.

I've searched the Internet, including docs in Google, but have only seen guides on embedding V8 with native code.

Where can I find guides about modifying V8's code?
Or where can I find docs about V8's design architecture?
Or can anyone describe how V8 implements the Array type in C++?

Thanks a lot.


回答1:


Firstly, it's likely that you can actually get away with using the v8 api to do whatever it is that you want to do. You can use it to create prototypes that mostly behave the same as built-in objects, you can bind C++ function calls to JS function calls also. There's really no reason to modify v8 itself unless you need something to be extremely fast or to inspect or manipulate v8 internals. For instance, Chrome's DOM implementation uses the v8 API rather than being implemented in v8 directly. The embedder's guide actually has all the information you need to create "classes" (remember that in JS it's actually prototype inheritance): https://developers.google.com/v8/embed#templates.

That said, here's some good places to look in the source code for say, the array object. I'm not sure off any design doc, you're probably better off looking at the source.

The array object itself is here: https://code.google.com/p/v8/source/browse/trunk/src/objects.h#8409

Some of the array api functions are implemented here (many use the same public APIs as you would for extending): https://code.google.com/p/v8/source/browse/trunk/src/builtins.cc#511

Some of the array api functions are implemented in JavaScript: https://code.google.com/p/v8/source/browse/trunk/src/array.js

Do a search for JSArray and you'll see much more. Pay particular attention to the bits in the native code generator, because you if you really want to take advantage of some custom type written at this level, you'll want to write code to generate efficient machine code too, for a bunch of different architectures...

Edit: Looks like V8 documentation has moved (and are better) than when this answer was written, here's some quick links to useful documentation:

  • Wiki: https://github.com/v8/v8/wiki/Getting%20Started%20with%20Embedding
  • API docs: http://v8.paulfryzel.com/docs/master/index.html


来源:https://stackoverflow.com/questions/13929716/how-to-add-a-new-class-to-google-v8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!