从11.11.0版本开始,Node.js中出现了napi_create_date函数。https://nodejs.org/api/n-api.html#n
有哪些解决方法可以仅使用N-API和V8 API在Node.js 10.15.3上创建JS日期(从C++ double)?
或者我可以从N-API得到V8隔离?
或者,我如何结合N-API和NAN来创建日期并在napi_call_function中使用它?
我需要一些方法来为napi_call_function调用创建一个日期值(从C++ double)。
发布于 2019-03-24 16:21:30
现在,我写了一个这样的变通方法。使用env->context()而不是v8::Isolate::GetCurrent()更正确,但是napi_env是在src/js_native_api_v8.h中定义的,而~/.node-gyp/10.15.3/include/node中不存在,所以我没有找到使用env->context()的快速方法。
#include <v8.h>
// This asserts v8::Local<> will always be implemented with a single
// pointer field so that we can pass it around as a void*.
static_assert(sizeof(v8::Local<v8::Value>) == sizeof(napi_value),
"Cannot convert between v8::Local<v8::Value> and napi_value");
napi_status napi_create_date_by_v8(
double time,
napi_value* result
) {
v8::Isolate* isolate = v8::Isolate::GetCurrent();
v8::MaybeLocal<v8::Value> maybe_date = v8::Date::New(isolate, time);
v8::Local<v8::Value> local = maybe_date.ToLocalChecked();
*result = reinterpret_cast<napi_value>(*local);
return napi_ok;
}https://stackoverflow.com/questions/55307051
复制相似问题