diff options
author | Anna Henningsen <anna@addaleax.net> | 2018-08-23 17:41:05 +0300 |
---|---|---|
committer | Anna Henningsen <anna@addaleax.net> | 2018-09-01 01:13:20 +0300 |
commit | e812be4a55915575fc1afce739848026a48b781e (patch) | |
tree | 2d654c5ef1a58ec22de69158305785b1556d06f6 /src/util-inl.h | |
parent | 403df7c8a15a9714038c73e90371bed0d37437fa (diff) |
src: make CLI options programatically accesible
Provide `internalBinding('options')` with some utilities
around making the options parser and current options values
programatically accessible.
PR-URL: https://github.com/nodejs/node/pull/22490
Reviewed-By: Michaƫl Zasso <targos@protonmail.com>
Diffstat (limited to 'src/util-inl.h')
-rw-r--r-- | src/util-inl.h | 59 |
1 files changed, 58 insertions, 1 deletions
diff --git a/src/util-inl.h b/src/util-inl.h index c6cd263aa27..bbee32615ff 100644 --- a/src/util-inl.h +++ b/src/util-inl.h @@ -24,8 +24,9 @@ #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS -#include "util.h" +#include <limits.h> // INT_MAX #include <cstring> +#include "util.h" #if defined(_MSC_VER) #include <intrin.h> @@ -397,6 +398,62 @@ inline char* Calloc(size_t n) { return Calloc<char>(n); } inline char* UncheckedMalloc(size_t n) { return UncheckedMalloc<char>(n); } inline char* UncheckedCalloc(size_t n) { return UncheckedCalloc<char>(n); } +// This is a helper in the .cc file so including util-inl.h doesn't include more +// headers than we really need to. +void ThrowErrStringTooLong(v8::Isolate* isolate); + +v8::MaybeLocal<v8::Value> ToV8Value(v8::Local<v8::Context> context, + const std::string& str) { + v8::Isolate* isolate = context->GetIsolate(); + if (UNLIKELY(str.size() >= static_cast<size_t>(v8::String::kMaxLength))) { + // V8 only has a TODO comment about adding an exception when the maximum + // string size is exceeded. + ThrowErrStringTooLong(isolate); + return v8::MaybeLocal<v8::Value>(); + } + + return v8::String::NewFromUtf8( + isolate, str.data(), v8::NewStringType::kNormal, str.size()) + .FromMaybe(v8::Local<v8::String>()); +} + +template <typename T> +v8::MaybeLocal<v8::Value> ToV8Value(v8::Local<v8::Context> context, + const std::vector<T>& vec) { + v8::Isolate* isolate = context->GetIsolate(); + v8::EscapableHandleScope handle_scope(isolate); + + v8::Local<v8::Array> arr = v8::Array::New(isolate, vec.size()); + for (size_t i = 0; i < vec.size(); ++i) { + v8::Local<v8::Value> val; + if (!ToV8Value(context, vec[i]).ToLocal(&val) || + arr->Set(context, i, val).IsNothing()) { + return v8::MaybeLocal<v8::Value>(); + } + } + + return handle_scope.Escape(arr); +} + +template <typename T, typename U> +v8::MaybeLocal<v8::Value> ToV8Value(v8::Local<v8::Context> context, + const std::unordered_map<T, U>& map) { + v8::Isolate* isolate = context->GetIsolate(); + v8::EscapableHandleScope handle_scope(isolate); + + v8::Local<v8::Map> ret = v8::Map::New(isolate); + for (const auto& item : map) { + v8::Local<v8::Value> first, second; + if (!ToV8Value(context, item.first).ToLocal(&first) || + !ToV8Value(context, item.second).ToLocal(&second) || + ret->Set(context, first, second).IsEmpty()) { + return v8::MaybeLocal<v8::Value>(); + } + } + + return handle_scope.Escape(ret); +} + } // namespace node #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS |