V8 中的 `v8::Isolate` class 和 `v8::internal::Isolate` 之间的关系是什么
What is the relationship between `v8::Isolate` class and `v8::internal::Isolate` in V8
最近在研究V8的源码。 Isolate
class有2个定义,一个在v8::Isolate
,一个在v8::internal::Isolate
。好像v8::Isolate
用的是v8::internal::Isolate
,但是我没看懂这2个定义/
之间的关系
我试图研究 class 两者的定义,v8::Isolate
在 https://github.com/v8/v8/blob/master/include/v8.h#L7391 and v8::internal::Isolate
in https://github.com/v8/v8/blob/master/src/execution/isolate.h#L452
但想不通。
更具体地说,在 v8::Isolate::New
(https://github.com/v8/v8/blob/master/src/api/api.cc#L7903) 中,returns 一个 v8::Isolate
.
类型的 C++ 对象
Isolate* Isolate::New(const Isolate::CreateParams& params) {
Isolate* isolate = Allocate();
Initialize(isolate, params);
return isolate;
}
但在Allocate
函数内部,它returns一个v8::internal::Isolate
类型的对象,reinterpret_casted到v8::Isolate
Isolate* Isolate::Allocate() {
return reinterpret_cast<Isolate*>(i::Isolate::New());
}
如何从 v8::internal::Isolate
转换 class v8::Isolate
的对象?
熟悉 V8 的人可以给我一些指导吗?
这种技术对于库来说并不罕见:v8::internal::Isolate
是实现,但它的细节完全封装在库中,并且对 public API 隐藏。 v8::Isolate
只是一个不透明的引用。观察它是如何没有字段的;嵌入者对它在内存中的样子一无所知(或者它是否有内存表示——就他们而言,它可能类似于内核的文件描述符)。这种封装的原因当然是为了分离关注点,并使组件彼此独立:库可以更改 class 的内部定义,而无需嵌入器关心(即它们不可能依赖在内部状态上,因此可以保证它们不会被更改破坏;它们甚至不必重新编译,因为 public API [和 ABI] 在内部 class布局更改)。
考虑这个演示原理的简化示例:
/* public API */
class BlackBox {
public:
static BlackBox* Create();
void DoSomething();
}
void StandaloneFunction(BlackBox* box);
/* internal implementation */
class TheRealThing {
public:
TheRealThing(int value) : value_(value) {}
private:
int value_;
}
BlackBox* BlackBox::Create() {
TheRealThing* real = new TheRealThing(42);
return reinterpret_cast<BlackBox*>(real);
}
void BlackBox::DoSomething() {
TheRealThing* real = reinterpret_cast<TheRealThing*>(this);
printf("Hello %d\n", real->value_);
}
void StandaloneFunction(BlackBox* box) {
TheRealThing* real = reinterpret_cast<TheRealThing*>(box);
printf("Good bye %d\n", real->value_);
}
最近在研究V8的源码。 Isolate
class有2个定义,一个在v8::Isolate
,一个在v8::internal::Isolate
。好像v8::Isolate
用的是v8::internal::Isolate
,但是我没看懂这2个定义/
我试图研究 class 两者的定义,v8::Isolate
在 https://github.com/v8/v8/blob/master/include/v8.h#L7391 and v8::internal::Isolate
in https://github.com/v8/v8/blob/master/src/execution/isolate.h#L452
但想不通。
更具体地说,在 v8::Isolate::New
(https://github.com/v8/v8/blob/master/src/api/api.cc#L7903) 中,returns 一个 v8::Isolate
.
Isolate* Isolate::New(const Isolate::CreateParams& params) {
Isolate* isolate = Allocate();
Initialize(isolate, params);
return isolate;
}
但在Allocate
函数内部,它returns一个v8::internal::Isolate
类型的对象,reinterpret_casted到v8::Isolate
Isolate* Isolate::Allocate() {
return reinterpret_cast<Isolate*>(i::Isolate::New());
}
如何从 v8::internal::Isolate
转换 class v8::Isolate
的对象?
熟悉 V8 的人可以给我一些指导吗?
这种技术对于库来说并不罕见:v8::internal::Isolate
是实现,但它的细节完全封装在库中,并且对 public API 隐藏。 v8::Isolate
只是一个不透明的引用。观察它是如何没有字段的;嵌入者对它在内存中的样子一无所知(或者它是否有内存表示——就他们而言,它可能类似于内核的文件描述符)。这种封装的原因当然是为了分离关注点,并使组件彼此独立:库可以更改 class 的内部定义,而无需嵌入器关心(即它们不可能依赖在内部状态上,因此可以保证它们不会被更改破坏;它们甚至不必重新编译,因为 public API [和 ABI] 在内部 class布局更改)。
考虑这个演示原理的简化示例:
/* public API */
class BlackBox {
public:
static BlackBox* Create();
void DoSomething();
}
void StandaloneFunction(BlackBox* box);
/* internal implementation */
class TheRealThing {
public:
TheRealThing(int value) : value_(value) {}
private:
int value_;
}
BlackBox* BlackBox::Create() {
TheRealThing* real = new TheRealThing(42);
return reinterpret_cast<BlackBox*>(real);
}
void BlackBox::DoSomething() {
TheRealThing* real = reinterpret_cast<TheRealThing*>(this);
printf("Hello %d\n", real->value_);
}
void StandaloneFunction(BlackBox* box) {
TheRealThing* real = reinterpret_cast<TheRealThing*>(box);
printf("Good bye %d\n", real->value_);
}