#include "node_internals.h"
#include "node_watchdog.h"
namespace node {
namespace util {
using v8::Array;
using v8::Context;
using v8::FunctionCallbackInfo;
using v8::Integer;
using v8::Local;
using v8::Maybe;
using v8::Object;
using v8::Private;
using v8::Promise;
using v8::Proxy;
using v8::Value;
#define VALUE_METHOD_MAP(V) \
V(isArrayBuffer, IsArrayBuffer) \
V(isArrayBufferView, IsArrayBufferView) \
V(isAsyncFunction, IsAsyncFunction) \
V(isDataView, IsDataView) \
V(isDate, IsDate) \
V(isExternal, IsExternal) \
V(isMap, IsMap) \
V(isMapIterator, IsMapIterator) \
V(isNativeError, IsNativeError) \
V(isPromise, IsPromise) \
V(isRegExp, IsRegExp) \
V(isSet, IsSet) \
V(isSetIterator, IsSetIterator) \
V(isTypedArray, IsTypedArray) \
V(isUint8Array, IsUint8Array)
#define V(_, ucname) \
static void ucname(const FunctionCallbackInfo& args) { \
CHECK_EQ(1, args.Length()); \
args.GetReturnValue().Set(args[0]->ucname()); \
}
VALUE_METHOD_MAP(V)
#undef V
static void IsAnyArrayBuffer(const FunctionCallbackInfo& args) {
CHECK_EQ(1, args.Length());
args.GetReturnValue().Set(
args[0]->IsArrayBuffer() || args[0]->IsSharedArrayBuffer());
}
static void GetPromiseDetails(const FunctionCallbackInfo& args) {
// Return undefined if it's not a Promise.
if (!args[0]->IsPromise())
return;
auto isolate = args.GetIsolate();
Local promise = args[0].As();
Local ret = Array::New(isolate, 2);
int state = promise->State();
ret->Set(0, Integer::New(isolate, state));
if (state != Promise::PromiseState::kPending)
ret->Set(1, promise->Result());
args.GetReturnValue().Set(ret);
}
static void GetProxyDetails(const FunctionCallbackInfo& args) {
// Return undefined if it's not a proxy.
if (!args[0]->IsProxy())
return;
Local proxy = args[0].As();
Local ret = Array::New(args.GetIsolate(), 2);
ret->Set(0, proxy->GetTarget());
ret->Set(1, proxy->GetHandler());
args.GetReturnValue().Set(ret);
}
// Side effect-free stringification that will never throw exceptions.
static void SafeToString(const FunctionCallbackInfo& args) {
auto context = args.GetIsolate()->GetCurrentContext();
args.GetReturnValue().Set(args[0]->ToDetailString(context).ToLocalChecked());
}
inline Local IndexToPrivateSymbol(Environment* env, uint32_t index) {
#define V(name, _) &Environment::name,
static Local (Environment::*const methods[])() const = {
PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES(V)
};
#undef V
CHECK_LT(index, arraysize(methods));
return (env->*methods[index])();
}
static void GetHiddenValue(const FunctionCallbackInfo& args) {
Environment* env = Environment::GetCurrent(args);
if (!args[0]->IsObject())
return env->ThrowTypeError("obj must be an object");
if (!args[1]->IsUint32())
return env->ThrowTypeError("index must be an uint32");
Local