How to add a new class to Google V8?

前端 未结 1 2129
灰色年华
灰色年华 2021-02-20 17:54

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, referenc

1条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-20 18:17

    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

    0 讨论(0)
提交回复
热议问题