Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a new class to Google V8?

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.

like image 619
jean Avatar asked Dec 18 '12 09:12

jean


People also ask

Is V8 better than SpiderMonkey?

V8 is the fastest, because it compiles all JS to machine code. SpiderMonkey (what FF uses) is fast too, but compiles to an intermediate byte-code, not machine code. That's the major difference with V8. EDIT- Newer Firefox releases come with a newer variant of SpideMonkey; TraceMonkey.

What is the purpose of using hidden classes in V8?

This hidden class concept not only allows you to bypass dictionary lookups; it also allows you to reuse already created classes when similar objects are created or modified. For example, if you create another empty object called article ( const articleObject = {} ), the V8 engine will not create a new hidden class.

How does Google V8 work?

The V8 engine gets its speed from the Just in Time (JIT) compilation of JS code to native machine code. The ignition interpreter, a key component of V8, compiles the JS code and generates non-optimized machine code. On runtime, the machine code is analyzed and re-compiled for optimal performance.


1 Answers

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
like image 80
yiding Avatar answered Sep 18 '22 16:09

yiding