v8中的v8 :: Isolate类和v8 :: internal :: Isolate之间是什么关系

时间:2019-06-30 17:17:38

标签: v8

我最近正在研究V8的源代码。 Isolate类有2个定义,一个在v8::Isolate中定义,另一个在v8::internal::Isolate中定义。似乎v8::Isolate使用了v8::internal::Isolate,但我无法理解这两个定义之间的关系/

我试图研究https://github.com/v8/v8/blob/master/include/v8.h#L7391中的v8::Isolatehttps://github.com/v8/v8/blob/master/src/execution/isolate.h#L452中的v8::internal::Isolate的类定义

但无法弄清楚。

更具体地说,在v8::Isolate::Newhttps://github.com/v8/v8/blob/master/src/api/api.cc#L7903)中,它返回类型为v8::Isolate的C ++对象。

Isolate* Isolate::New(const Isolate::CreateParams& params) {
  Isolate* isolate = Allocate();
  Initialize(isolate, params);
  return isolate;
}

但是在内部Allocate函数中,它返回类型为v8::internal::Isolate的对象,并重新解释广播到v8::Isolate

Isolate* Isolate::Allocate() {
    return reinterpret_cast<Isolate*>(i::Isolate::New());
}

如何从v8::Isolate投射类v8::internal::Isolate的对象?

任何熟悉V8的人都能给我一些指导吗?

1 个答案:

答案 0 :(得分:1)

对于库来说,这是一种不常见的技术:v8::internal::Isolate是实现,但是其详细信息完全封装在库中,并且对公共API隐藏。 v8::Isolate只是一个不透明的引用。观察它没有字段。嵌入程序对内存中的外观一无所知(或者它是否完全具有内存表示形式-就他们而言,可能类似于内核的文件描述符)。当然,这种封装的原因是为了分开关注点,并使组件彼此独立:该库可以更改类的内部定义,而不必担心嵌入程序(即,它们不可能依赖于内部状态) ,因此可以确保它们不会被更改破坏;它们甚至不必重新编译,因为当内部类布局更改时,公共API [和ABI]不会更改。

请考虑以下简化示例,说明该原理:

/* 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_);
}
相关问题