0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-30 23:43:09 +01:00
nodejs/src/node_util.cc
Ben Noordhuis 924cc6c633 src: upgrade to new v8::Private api
Stop using the deprecated `GetHiddenValue()` and `SetHiddenValue()`
methods, start using `GetPrivate()` and `SetPrivate()` instead.

This commit turns some of the entries in the per-isolate string table
into private symbols.

PR-URL: https://github.com/nodejs/node/pull/5045
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
2016-02-03 01:04:31 +01:00

93 lines
3.0 KiB
C++

#include "node.h"
#include "v8.h"
#include "env.h"
#include "env-inl.h"
namespace node {
namespace util {
using v8::Context;
using v8::FunctionCallbackInfo;
using v8::Local;
using v8::Object;
using v8::Private;
using v8::String;
using v8::Value;
#define VALUE_METHOD_MAP(V) \
V(isArrayBuffer, IsArrayBuffer) \
V(isDataView, IsDataView) \
V(isDate, IsDate) \
V(isMap, IsMap) \
V(isMapIterator, IsMapIterator) \
V(isPromise, IsPromise) \
V(isRegExp, IsRegExp) \
V(isSet, IsSet) \
V(isSetIterator, IsSetIterator) \
V(isTypedArray, IsTypedArray)
#define V(_, ucname) \
static void ucname(const FunctionCallbackInfo<Value>& args) { \
CHECK_EQ(1, args.Length()); \
args.GetReturnValue().Set(args[0]->ucname()); \
}
VALUE_METHOD_MAP(V)
#undef V
static void GetHiddenValue(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
if (!args[0]->IsObject())
return env->ThrowTypeError("obj must be an object");
if (!args[1]->IsString())
return env->ThrowTypeError("name must be a string");
Local<Object> obj = args[0].As<Object>();
Local<String> name = args[1].As<String>();
auto private_symbol = Private::ForApi(env->isolate(), name);
auto maybe_value = obj->GetPrivate(env->context(), private_symbol);
args.GetReturnValue().Set(maybe_value.ToLocalChecked());
}
static void SetHiddenValue(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
if (!args[0]->IsObject())
return env->ThrowTypeError("obj must be an object");
if (!args[1]->IsString())
return env->ThrowTypeError("name must be a string");
Local<Object> obj = args[0].As<Object>();
Local<String> name = args[1].As<String>();
auto private_symbol = Private::ForApi(env->isolate(), name);
auto maybe_value = obj->SetPrivate(env->context(), private_symbol, args[2]);
args.GetReturnValue().Set(maybe_value.FromJust());
}
void Initialize(Local<Object> target,
Local<Value> unused,
Local<Context> context) {
Environment* env = Environment::GetCurrent(context);
#define V(lcname, ucname) env->SetMethod(target, #lcname, ucname);
VALUE_METHOD_MAP(V)
#undef V
env->SetMethod(target, "getHiddenValue", GetHiddenValue);
env->SetMethod(target, "setHiddenValue", SetHiddenValue);
}
} // namespace util
} // namespace node
NODE_MODULE_CONTEXT_AWARE_BUILTIN(util, node::util::Initialize)