在v8中对值句柄使用Cast vs ToXXX

时间:2022-04-15 21:39:09

I'm embedding V8 as an auxiliary language in a C++ program.

我将V8作为一种辅助语言嵌入到c++程序中。

I retrieve a Handle<Value> from V8 when I call something like

当我调用类似的东西时,我从V8中检索句柄

Handle<Value> value_handle = context->Global()->Get(key_handle);

I can then find out that it is (say) a string with value_handle->IsString(). And if so I can convert it to a Handle<String> to access its string-specific methods.

然后我可以发现它是(比如说)一个带有value_handle->IsString()的字符串。如果可以,我可以将其转换为一个Handle ,以访问其特定于字符串的方法。

But there seems to be two ways of doing that, either:

但似乎有两种方法:

Handle<String> string = value_handle->ToString();

or

Handle<String> string = Handle<String>::Cast(value_handle);

However, for Arrays and Functions, there is no toArray() or toFunction methods, just the casting.

但是,对于数组和函数,没有toArray()或toFunction方法,只有强制转换。

So my question is: a) are the ToXXX just syntactic sugar for casting? and, if not b) what is the ToXXX method doing?

所以我的问题是:a)弓形虫只是用于铸造的句法糖吗?如果不是b)那么ToXXX方法是什么?

2 个解决方案

#1


8  

ToXXX functions perform type coercions as described in subsections of section 9 of ECMA-262 5th. For example ToString is described in section 9.8: when given a non-string value it'll return an appropriate string representation of it, if you are passing object it'll call toString method on it (or valueOf if toString is not present). Relevant code for ToString: in api.cc Value::ToString that calls into runtime.js ToString

ToXXX函数执行ECMA-262第5节第9节所述的强制类型。例如,在第9.8节中描述了ToString:当给定一个非字符串值时,它将返回它的适当的字符串表示形式,如果您正在传递对象,它将在对象上调用ToString方法(或valueOf if ToString不存在)。ToString的相关代码:在api中。cc值::ToString调用运行时。js ToString

On the other hand Handle<XXX>::Cast(...) does no coercions. It's just a type cast for handles. Essentially it is just a static_cast<XXX*>. In debug mode Handle<T>::Cast(...) is checked and aborts execution when types do not match. It would be a fatal error if you are given a Handle<Value> containing an Object and you are trying to cast it to a Handle<String>. In release mode casting to an incompatible type will just later lead to weird results and possibly crashes, when you try to use the result of the cast. Relevant code in v8.h Handle<T>::Cast which delegates to (for example) String::Cast which checks the cast (if checks are enabled) via String::CheckCast.

另一方面,句柄 : Cast(…)没有强制。它只是句柄的类型转换。本质上它只是一个static_cast 。在调试模式下,当类型不匹配时,将检查Cast(…)并中止执行。如果给定一个包含对象的句柄 ,并试图将其转换为句柄 ,那么这将是一个致命错误。在发布模式中,当您试图使用强制转换的结果时,对不兼容类型的强制转换只会导致奇怪的结果,甚至崩溃。相关代码在v8。h句柄 ::Cast哪些委托给(例如)String::Cast哪些委托通过String::CheckCast检查Cast。 *>

#2


0  

We can locate

我们可以找到

V8EXPORT Local ToString() const;

V8EXPORT当地ToString()常量;

in line 971 of v8.h where V8EXPORT is a OS dependent approach for functions.

在v8的971行。其中V8EXPORT是与操作系统相关的函数方法。


ToString of Handle of String is located at line 2362 of api.cc

字符串句柄的ToString位于api.cc的第2362行

Local<String> Value::ToString() const {
  i::Handle<i::Object> obj = Utils::OpenHandle(this);
  i::Handle<i::Object> str;
  if (obj->IsString()) {
    str = obj;
  } else {
    i::Isolate* isolate = i::Isolate::Current();
    if (IsDeadCheck(isolate, "v8::Value::ToString()")) {
      return Local<String>();
    }
    LOG_API(isolate, "ToString");
    ENTER_V8(isolate);
    EXCEPTION_PREAMBLE(isolate);
    str = i::Execution::ToString(obj, &has_pending_exception);
    EXCEPTION_BAILOUT_CHECK(isolate, Local<String>());
  }
  return Local<String>(ToApi<String>(str));
}

For consistency and take advantages from further upgrade of V8 versions, I strongly recommend to use toString() instead of the primitive cast.

为了保持一致性并从V8版本的进一步升级中获益,我强烈建议使用toString()而不是原始的cast。

#1


8  

ToXXX functions perform type coercions as described in subsections of section 9 of ECMA-262 5th. For example ToString is described in section 9.8: when given a non-string value it'll return an appropriate string representation of it, if you are passing object it'll call toString method on it (or valueOf if toString is not present). Relevant code for ToString: in api.cc Value::ToString that calls into runtime.js ToString

ToXXX函数执行ECMA-262第5节第9节所述的强制类型。例如,在第9.8节中描述了ToString:当给定一个非字符串值时,它将返回它的适当的字符串表示形式,如果您正在传递对象,它将在对象上调用ToString方法(或valueOf if ToString不存在)。ToString的相关代码:在api中。cc值::ToString调用运行时。js ToString

On the other hand Handle<XXX>::Cast(...) does no coercions. It's just a type cast for handles. Essentially it is just a static_cast<XXX*>. In debug mode Handle<T>::Cast(...) is checked and aborts execution when types do not match. It would be a fatal error if you are given a Handle<Value> containing an Object and you are trying to cast it to a Handle<String>. In release mode casting to an incompatible type will just later lead to weird results and possibly crashes, when you try to use the result of the cast. Relevant code in v8.h Handle<T>::Cast which delegates to (for example) String::Cast which checks the cast (if checks are enabled) via String::CheckCast.

另一方面,句柄 : Cast(…)没有强制。它只是句柄的类型转换。本质上它只是一个static_cast 。在调试模式下,当类型不匹配时,将检查Cast(…)并中止执行。如果给定一个包含对象的句柄 ,并试图将其转换为句柄 ,那么这将是一个致命错误。在发布模式中,当您试图使用强制转换的结果时,对不兼容类型的强制转换只会导致奇怪的结果,甚至崩溃。相关代码在v8。h句柄 ::Cast哪些委托给(例如)String::Cast哪些委托通过String::CheckCast检查Cast。 *>

#2


0  

We can locate

我们可以找到

V8EXPORT Local ToString() const;

V8EXPORT当地ToString()常量;

in line 971 of v8.h where V8EXPORT is a OS dependent approach for functions.

在v8的971行。其中V8EXPORT是与操作系统相关的函数方法。


ToString of Handle of String is located at line 2362 of api.cc

字符串句柄的ToString位于api.cc的第2362行

Local<String> Value::ToString() const {
  i::Handle<i::Object> obj = Utils::OpenHandle(this);
  i::Handle<i::Object> str;
  if (obj->IsString()) {
    str = obj;
  } else {
    i::Isolate* isolate = i::Isolate::Current();
    if (IsDeadCheck(isolate, "v8::Value::ToString()")) {
      return Local<String>();
    }
    LOG_API(isolate, "ToString");
    ENTER_V8(isolate);
    EXCEPTION_PREAMBLE(isolate);
    str = i::Execution::ToString(obj, &has_pending_exception);
    EXCEPTION_BAILOUT_CHECK(isolate, Local<String>());
  }
  return Local<String>(ToApi<String>(str));
}

For consistency and take advantages from further upgrade of V8 versions, I strongly recommend to use toString() instead of the primitive cast.

为了保持一致性并从V8版本的进一步升级中获益,我强烈建议使用toString()而不是原始的cast。