1

我正在研究 v8 源代码。我已经花了 3 周时间,但我找不到 8v 如何调用 DOM 的函数。

Example for, 
<script>
    document.writeln("Hello V8");
</script>

我想知道调用序列的过程,DOM 的 writeln() 函数。你能否解释一下或给我一些提示。

4

1 回答 1

1

您可以检查找到.writeln函数的V8HTMLDocumentCustom.cpp文件:

void V8HTMLDocument::writelnMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& args){
      HTMLDocument* htmlDocument = V8HTMLDocument::toNative(args.Holder());
      htmlDocument->writeln(writeHelperGetString(args), activeDOMWindow()->document());
}

如您所见,其中包含了几个标头,其中一些包含将您引导到其他标头,您可以在其中找到V8DOMConfiguration.h 之类的文件

V8DOMConfiguration.h 有一些注释:

class V8DOMConfiguration {
    public:
    // The following Batch structs and methods are used for setting multiple
    // properties on an ObjectTemplate, used from the generated bindings
    // initialization (ConfigureXXXTemplate). This greatly reduces the binary
    // size by moving from code driven setup to data table driven setup.

我从中得到的是,Chrome V8 使用对象创建“包装器世界”,为每个对象重新创建 DOM,然后它只是将数据传递给创建的活动窗口。

我不精通 V8,但这是一个起点。也许对它有更深入了解的人可以更好地解释它。

更新

正如@Esailija 指出的那样,在没有浏览器的情况下运行 V8 引擎时没有可用的 DOM。由于 DOM 是 Webkit/Blink 的一部分,因此链接引用指向它们。一旦浏览器渲染了 DOM,V8 对象就会与 DOM 树元素匹配。这里有一个相关的问题:V8 Access to DOM

于 2013-10-24T03:29:38.643 回答