对于
Context
:最好跟踪要使用的正确上下文。如果你只有一个,那么这很容易:-)只需将其存储在
v8::Persistent
当你创建它时
v8::Local<v8::Context>
如果你碰巧有几个上下文,你会(希望)欣赏到这给你带来的控制量——对于需要关心这些事情的应用程序来说,排除与安全相关的跨上下文访问尤其重要。
对于
Maybe<int32_t>
:检查它是否
Nothing
,如果是这样,请处理错误。
v8::Isolate* isolate = ...;
v8::Persistent context(v8::Context::New(isolate));
v8::Maybe<int32_t> maybe_value = value->Int32Value(context.Get(isolate));
int32_t int_value;
if (!maybe_value.To(&value)) {
}
if (maybe_value.IsNothing()) {
}
int32_t int_value = maybe_value.FromJust();
static_cast<Point*>(ptr)->x_ = int_value;
为什么
Maybe
dance是JavaScript代码所必需的,例如:
my_point.x = {valueOf: () => throw "this is why we can't have nice things"; }
旧版本在哪里
value->Int32Value()
在尝试获取int32值时,无法发出抛出异常的信号。当然,这不仅适用于此类人为示例,也适用于其他异常,如堆栈溢出或
ReferenceError
s等。