如何向Google V8添加新课程? [英] How to add a new class to Google V8?

查看:179
本文介绍了如何向Google V8添加新课程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Google V8和Javascript的新来者,我想使用C ++为Javascript添加一个新类。

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

我已经使用Webkit的V8绑定完成了一些工作,引用是: webkit idl v8 binding

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

现在我想直接将它集成到V8引擎,通过修改V8的代码,而不是简单地使用V8的API来做扩展。换句话说,我想添加一个新的类,就像Array类型在Javascript中,使用相同的实现机制。

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.

我在互联网上搜索过,包括Google的文档,但只看到了用本地代码嵌入V8的指南。

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

我在哪里可以找到有关修改V8代码的指南?

或在哪里可以找到有关V8设计架构的文档?

或者任何人都可以描述V8如何在C ++中实现Array类型?

非常感谢。

推荐答案

首先,你可能真的可以使用v8 api做任何你想做的事情。您可以使用它来创建原型,其主要与内置对象的行为一样,您可以将C ++函数调用绑定到JS函数调用。真的没有理由修改v8本身,除非你需要的东西是非常快的或检查或操纵v8内部。例如,Chrome的DOM实现使用v8 API,而不是直接在v8中实现。 embedder的指南实际上具有创建类所需的所有信息(记住,在JS中它实际上是原型继承): https://developers.google.com/v8/embed#templates

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.

数组对象本身就是这样:
https://code.google.com/p/v8/ source / browse / trunk / src / objects.h#8409

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

一些数组api函数在这里实现(许多使用与你相同的公共API将扩展):
https: //code.google.com/p/v8/source/browse/trunk/src/builtins.cc#511

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

一些数组API函数是以JavaScript实施的: https://code.google。 com / p / v8 / source / browse / trunk / src / array.js

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

搜索JSArray,你会看到更多。要特别注意本地代码生成器中的位,因为如果你真的想利用在这个级别编写的一些自定义类型,你将需要编写代码来生成高效的机器代码,对于一堆不同的架构...

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...

这篇关于如何向Google V8添加新课程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆