作者:静静敲代码 | 来源:互联网 | 2023-05-18 15:22
IcannotunderstandtheimplementationofinheritanceinGooglesV8JavaScriptengine.Itclearly(
I cannot understand the implementation of inheritance in Google's V8 Javascript engine. It clearly (?) implements an inheritance hierarchy, but seems to completely do away with virtual functions.
我无法理解谷歌的V8 Javascript引擎中继承的实现。它显然(?)实现了继承层次结构,但似乎完全不使用虚拟函数。
This is the inheritance hierarchy as detailed in the objects.h
header file:
这是对象中详细介绍的继承层次结构。h头文件:
// Inheritance hierarchy:
// - Object
// - Smi (immediate small integer)
// - HeapObject (superclass for everything allocated in the heap)
// - JSReceiver (suitable for property access)
// - JSObject
// - JSArray
// ... and many more entries
Most object types are derived from Object
, which is declared as follows:
大多数对象类型都是从对象派生出来的,声明如下:
// Object is the abstract superclass for all classes in the
// object hierarchy.
// Object does not use any virtual functions to avoid the
// allocation of the C++ vtable.
// Since both Smi and HeapObject are subclasses of Object no
// data members can be present in Object.
class Object {
// ... bunch of method declarations and definitions
};
The relatively simple Smi
class is declared next:
相对简单的Smi类将在下面声明:
class Smi: public Object {
public:
// methods declarations and static member definitions
};
and so on.
等等。
For the life of me, I cannot understand how can, say, an instance of Smi
can be used as an Object
; there are no virtual functions and I cannot find overrides in the the implementation file, objects.cc
. At 17,290 lines, though, trying to understand what is going on is proving a difficult task.
在我的一生中,我无法理解如何,比方说,Smi实例可以作为对象;在实现文件objects.cc中没有虚拟函数,我也找不到重写。然而,在17,290行,试图弄明白到底发生了什么是一项困难的任务。
As another difficulty, I found an ObjectVisitor
class in the same header file (this one is more classical; it consists of virtual methods). But I could not find the equivalent Accept(Visitor*)
(or similar) method in the Object
base class.
另一个困难是,我在同一个头文件中发现了ObjectVisitor类(这个更经典;它由虚拟方法组成)。但是我在对象基类中找不到等效的Accept(Visitor*)(或类似的)方法。
What I am asking in concrete is for a minimal example that illustrates how does this inheritance pattern works.
我在具体的内容中要问的是一个最小的示例,它说明了这个继承模式是如何工作的。
2 个解决方案