| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 1b7359d commit f5f790b
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -22,17 +22,6 @@ using v8::SideEffectType; | |||
| 22 | 22 | using v8::String; | |
| 23 | 23 | using v8::Value; | |
| 24 | 24 | ||
| 25 | - // TODO(joyeecheung): make these more general and put them into util.h | ||
| 26 | - Local<Set> ToJsSet(Local<Context> context, const std::set<std::string>& in) { | ||
| 27 | - Isolate* isolate = context->GetIsolate(); | ||
| 28 | - Local<Set> out = Set::New(isolate); | ||
| 29 | - for (auto const& x : in) { | ||
| 30 | - out->Add(context, OneByteString(isolate, x.c_str(), x.size())) | ||
| 31 | - .ToLocalChecked(); | ||
| 32 | - } | ||
| 33 | - return out; | ||
| 34 | - } | ||
| 35 | - | ||
| 36 | 25 | bool NativeModuleEnv::Add(const char* id, const UnionBytes& source) { | |
| 37 | 26 | return NativeModuleLoader::GetInstance()->Add(id, source); | |
| 38 | 27 | } | |
@@ -67,16 +56,26 @@ void NativeModuleEnv::GetModuleCategories( | |||
| 67 | 56 | cannot_be_required.insert("trace_events"); | |
| 68 | 57 | } | |
| 69 | 58 | ||
| 70 | - result | ||
| 59 | + Local<Value> cannot_be_required_js; | ||
| 60 | + Local<Value> can_be_required_js; | ||
| 61 | + | ||
| 62 | + if (!ToV8Value(context, cannot_be_required).ToLocal(&cannot_be_required_js)) | ||
| 63 | + return; | ||
| 64 | + if (result | ||
| 71 | 65 | ->Set(context, | |
| 72 | 66 | OneByteString(isolate, "cannotBeRequired"), | |
| 73 | - ToJsSet(context, cannot_be_required)) | ||
| 74 | - .FromJust(); | ||
| 75 | - result | ||
| 67 | + cannot_be_required_js) | ||
| 68 | + .IsNothing()) | ||
| 69 | + return; | ||
| 70 | + if (!ToV8Value(context, can_be_required).ToLocal(&can_be_required_js)) | ||
| 71 | + return; | ||
| 72 | + if (result | ||
| 76 | 73 | ->Set(context, | |
| 77 | 74 | OneByteString(isolate, "canBeRequired"), | |
| 78 | - ToJsSet(context, can_be_required)) | ||
| 79 | - .FromJust(); | ||
| 75 | + can_be_required_js) | ||
| 76 | + .IsNothing()) { | ||
| 77 | + return; | ||
| 78 | + } | ||
| 80 | 79 | info.GetReturnValue().Set(result); | |
| 81 | 80 | } | |
| 82 | 81 | ||
@@ -85,23 +84,45 @@ void NativeModuleEnv::GetCacheUsage(const FunctionCallbackInfo<Value>& args) { | |||
| 85 | 84 | Isolate* isolate = env->isolate(); | |
| 86 | 85 | Local<Context> context = env->context(); | |
| 87 | 86 | Local<Object> result = Object::New(isolate); | |
| 88 | - result | ||
| 87 | + | ||
| 88 | + Local<Value> native_modules_with_cache_js; | ||
| 89 | + Local<Value> native_modules_without_cache_js; | ||
| 90 | + Local<Value> native_modules_in_snapshot_js; | ||
| 91 | + if (!ToV8Value(context, env->native_modules_with_cache) | ||
| 92 | + .ToLocal(&native_modules_with_cache_js)) { | ||
| 93 | + return; | ||
| 94 | + } | ||
| 95 | + if (result | ||
| 89 | 96 | ->Set(env->context(), | |
| 90 | 97 | OneByteString(isolate, "compiledWithCache"), | |
| 91 | - ToJsSet(context, env->native_modules_with_cache)) | ||
| 92 | - .FromJust(); | ||
| 93 | - result | ||
| 98 | + native_modules_with_cache_js) | ||
| 99 | + .IsNothing()) { | ||
| 100 | + return; | ||
| 101 | + } | ||
| 102 | + | ||
| 103 | + if (!ToV8Value(context, env->native_modules_without_cache) | ||
| 104 | + .ToLocal(&native_modules_without_cache_js)) { | ||
| 105 | + return; | ||
| 106 | + } | ||
| 107 | + if (result | ||
| 94 | 108 | ->Set(env->context(), | |
| 95 | 109 | OneByteString(isolate, "compiledWithoutCache"), | |
| 96 | - ToJsSet(context, env->native_modules_without_cache)) | ||
| 97 | - .FromJust(); | ||
| 110 | + native_modules_without_cache_js) | ||
| 111 | + .IsNothing()) { | ||
| 112 | + return; | ||
| 113 | + } | ||
| 98 | 114 | ||
| 99 | - result | ||
| 115 | + if (!ToV8Value(context, env->native_modules_in_snapshot) | ||
| 116 | + .ToLocal(&native_modules_without_cache_js)) { | ||
| 117 | + return; | ||
| 118 | + } | ||
| 119 | + if (result | ||
| 100 | 120 | ->Set(env->context(), | |
| 101 | 121 | OneByteString(isolate, "compiledInSnapshot"), | |
| 102 | - ToV8Value(env->context(), env->native_modules_in_snapshot) | ||
| 103 | - .ToLocalChecked()) | ||
| 104 | - .FromJust(); | ||
| 122 | + native_modules_without_cache_js) | ||
| 123 | + .IsNothing()) { | ||
| 124 | + return; | ||
| 125 | + } | ||
| 105 | 126 | ||
| 106 | 127 | args.GetReturnValue().Set(result); | |
| 107 | 128 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -404,7 +404,7 @@ inline char* UncheckedCalloc(size_t n) { return UncheckedCalloc<char>(n); } | |||
| 404 | 404 | void ThrowErrStringTooLong(v8::Isolate* isolate); | |
| 405 | 405 | ||
| 406 | 406 | v8::MaybeLocal<v8::Value> ToV8Value(v8::Local<v8::Context> context, | |
| 407 | - const std::string& str, | ||
| 407 | + std::string_view str, | ||
| 408 | 408 | v8::Isolate* isolate) { | |
| 409 | 409 | if (isolate == nullptr) isolate = context->GetIsolate(); | |
| 410 | 410 | if (UNLIKELY(str.size() >= static_cast<size_t>(v8::String::kMaxLength))) { | |
@@ -436,6 +436,25 @@ v8::MaybeLocal<v8::Value> ToV8Value(v8::Local<v8::Context> context, | |||
| 436 | 436 | return handle_scope.Escape(v8::Array::New(isolate, arr.out(), arr.length())); | |
| 437 | 437 | } | |
| 438 | 438 | ||
| 439 | + template <typename T> | ||
| 440 | + v8::MaybeLocal<v8::Value> ToV8Value(v8::Local<v8::Context> context, | ||
| 441 | + const std::set<T>& set, | ||
| 442 | + v8::Isolate* isolate) { | ||
| 443 | + if (isolate == nullptr) isolate = context->GetIsolate(); | ||
| 444 | + v8::Local<v8::Set> set_js = v8::Set::New(isolate); | ||
| 445 | + v8::HandleScope handle_scope(isolate); | ||
| 446 | + | ||
| 447 | + for (const T& entry : set) { | ||
| 448 | + v8::Local<v8::Value> value; | ||
| 449 | + if (!ToV8Value(context, entry, isolate).ToLocal(&value)) | ||
| 450 | + return {}; | ||
| 451 | + if (set_js->Add(context, value).IsEmpty()) | ||
| 452 | + return {}; | ||
| 453 | + } | ||
| 454 | + | ||
| 455 | + return set_js; | ||
| 456 | + } | ||
| 457 | + | ||
| 439 | 458 | template <typename T, typename U> | |
| 440 | 459 | v8::MaybeLocal<v8::Value> ToV8Value(v8::Local<v8::Context> context, | |
| 441 | 460 | const std::unordered_map<T, U>& map, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -36,7 +36,9 @@ | |||
| 36 | 36 | #include <limits> | |
| 37 | 37 | #include <memory> | |
| 38 | 38 | #include <string> | |
| 39 | + #include <string_view> | ||
| 39 | 40 | #include <type_traits> | |
| 41 | + #include <set> | ||
| 40 | 42 | #include <unordered_map> | |
| 41 | 43 | #include <utility> | |
| 42 | 44 | #include <vector> | |
@@ -644,7 +646,7 @@ using DeleteFnPtr = typename FunctionDeleter<T, function>::Pointer; | |||
| 644 | 646 | std::vector<std::string> SplitString(const std::string& in, char delim); | |
| 645 | 647 | ||
| 646 | 648 | inline v8::MaybeLocal<v8::Value> ToV8Value(v8::Local<v8::Context> context, | |
| 647 | - const std::string& str, | ||
| 649 | + std::string_view str, | ||
| 648 | 650 | v8::Isolate* isolate = nullptr); | |
| 649 | 651 | template <typename T, typename test_for_number = | |
| 650 | 652 | typename std::enable_if<std::numeric_limits<T>::is_specialized, bool>::type> | |
@@ -655,6 +657,10 @@ template <typename T> | |||
| 655 | 657 | inline v8::MaybeLocal<v8::Value> ToV8Value(v8::Local<v8::Context> context, | |
| 656 | 658 | const std::vector<T>& vec, | |
| 657 | 659 | v8::Isolate* isolate = nullptr); | |
| 660 | + template <typename T> | ||
| 661 | + inline v8::MaybeLocal<v8::Value> ToV8Value(v8::Local<v8::Context> context, | ||
| 662 | + const std::set<T>& set, | ||
| 663 | + v8::Isolate* isolate = nullptr); | ||
| 658 | 664 | template <typename T, typename U> | |
| 659 | 665 | inline v8::MaybeLocal<v8::Value> ToV8Value(v8::Local<v8::Context> context, | |
| 660 | 666 | const std::unordered_map<T, U>& map, | |
| Back | FazBrowse Home | New Git URL |
0 commit comments