@@ -2856,6 +2856,146 @@ Session::OptionsObject::OptionsObject(Environment* env,
28562856 MakeWeak ();
28572857}
28582858
2859+ template <typename Opt>
2860+ Maybe<bool > Session::OptionsObject::SetOption (Opt* options,
2861+ const Local<Object>& object,
2862+ const Local<String>& name,
2863+ uint64_t Opt::*member) {
2864+ Local<Value> value;
2865+ if (!object->Get (env ()->context (), name).ToLocal (&value))
2866+ return Nothing<bool >();
2867+
2868+ if (value->IsUndefined ()) return Just (false );
2869+
2870+ CHECK_IMPLIES (!value->IsBigInt (), value->IsNumber ());
2871+
2872+ uint64_t val = 0 ;
2873+ if (value->IsBigInt ()) {
2874+ bool lossless = true ;
2875+ val = value.As <BigInt>()->Uint64Value (&lossless);
2876+ if (!lossless) {
2877+ Utf8Value label (env ()->isolate (), name);
2878+ THROW_ERR_OUT_OF_RANGE (
2879+ env (),
2880+ (std::string (" options." ) + (*label) + " is out of range" ).c_str ());
2881+ return Nothing<bool >();
2882+ }
2883+ } else {
2884+ val = static_cast <int64_t >(value.As <Number>()->Value ());
2885+ }
2886+ options->*member = val;
2887+ return Just (true );
2888+ }
2889+
2890+ template <typename Opt>
2891+ Maybe<bool > Session::OptionsObject::SetOption (Opt* options,
2892+ const Local<Object>& object,
2893+ const Local<String>& name,
2894+ uint32_t Opt::*member) {
2895+ Local<Value> value;
2896+ if (!object->Get (env ()->context (), name).ToLocal (&value))
2897+ return Nothing<bool >();
2898+
2899+ if (value->IsUndefined ()) return Just (false );
2900+
2901+ CHECK (value->IsUint32 ());
2902+ uint32_t val = value.As <Uint32>()->Value ();
2903+ options->*member = val;
2904+ return Just (true );
2905+ }
2906+
2907+ template <typename Opt>
2908+ Maybe<bool > Session::OptionsObject::SetOption (Opt* options,
2909+ const Local<Object>& object,
2910+ const Local<String>& name,
2911+ bool Opt::*member) {
2912+ Local<Value> value;
2913+ if (!object->Get (env ()->context (), name).ToLocal (&value))
2914+ return Nothing<bool >();
2915+ if (value->IsUndefined ()) return Just (false );
2916+ CHECK (value->IsBoolean ());
2917+ options->*member = value->IsTrue ();
2918+ return Just (true );
2919+ }
2920+
2921+ template <typename Opt>
2922+ Maybe<bool > Session::OptionsObject::SetOption (Opt* options,
2923+ const Local<Object>& object,
2924+ const Local<String>& name,
2925+ std::string Opt::*member) {
2926+ Local<Value> value;
2927+ if (!object->Get (env ()->context (), name).ToLocal (&value))
2928+ return Nothing<bool >();
2929+ if (value->IsUndefined ()) return Just (false );
2930+ Utf8Value val (env ()->isolate (), value);
2931+ options->*member = val.ToString ();
2932+ return Just (true );
2933+ }
2934+
2935+ template <typename Opt>
2936+ Maybe<bool > Session::OptionsObject::SetOption (
2937+ Opt* options,
2938+ const Local<Object>& object,
2939+ const Local<String>& name,
2940+ std::vector<std::shared_ptr<crypto::KeyObjectData>> Opt::*member) {
2941+ Local<Value> value;
2942+ if (!object->Get (env ()->context (), name).ToLocal (&value))
2943+ return Nothing<bool >();
2944+ if (value->IsArray ()) {
2945+ auto context = env ()->context ();
2946+ auto values = value.As <v8::Array>();
2947+ uint32_t count = values->Length ();
2948+ for (uint32_t n = 0 ; n < count; n++) {
2949+ Local<Value> item;
2950+ if (!values->Get (context, n).ToLocal (&item)) {
2951+ return Nothing<bool >();
2952+ }
2953+ if (crypto::KeyObjectHandle::HasInstance (env (), item)) {
2954+ crypto::KeyObjectHandle* handle;
2955+ ASSIGN_OR_RETURN_UNWRAP (&handle, item, Nothing<bool >());
2956+ (options->*member).push_back (handle->Data ());
2957+ }
2958+ }
2959+ } else if (crypto::KeyObjectHandle::HasInstance (env (), value)) {
2960+ crypto::KeyObjectHandle* handle;
2961+ ASSIGN_OR_RETURN_UNWRAP (&handle, value, Nothing<bool >());
2962+ (options->*member).push_back (handle->Data ());
2963+ } else {
2964+ UNREACHABLE ();
2965+ }
2966+ return Just (true );
2967+ }
2968+
2969+ template <typename Opt>
2970+ Maybe<bool > Session::OptionsObject::SetOption (Opt* options,
2971+ const Local<Object>& object,
2972+ const Local<String>& name,
2973+ std::vector<Store> Opt::*member) {
2974+ Local<Value> value;
2975+ if (!object->Get (env ()->context (), name).ToLocal (&value))
2976+ return Nothing<bool >();
2977+ if (value->IsArray ()) {
2978+ auto context = env ()->context ();
2979+ auto values = value.As <v8::Array>();
2980+ uint32_t count = values->Length ();
2981+ for (uint32_t n = 0 ; n < count; n++) {
2982+ Local<Value> item;
2983+ if (!values->Get (context, n).ToLocal (&item)) {
2984+ return Nothing<bool >();
2985+ }
2986+ if (item->IsArrayBufferView ()) {
2987+ Store store (item.As <ArrayBufferView>());
2988+ (options->*member).push_back (std::move (store));
2989+ }
2990+ }
2991+ } else if (value->IsArrayBufferView ()) {
2992+ Store store (value.As <ArrayBufferView>());
2993+ (options->*member).push_back (std::move (store));
2994+ }
2995+
2996+ return Just (true );
2997+ }
2998+
28592999void Session::OptionsObject::New (const FunctionCallbackInfo<Value>& args) {
28603000 CHECK (args.IsConstructCall ());
28613001 auto env = Environment::GetCurrent (args);
@@ -3169,146 +3309,6 @@ void Session::OptionsObject::New(const FunctionCallbackInfo<Value>& args) {
31693309 }
31703310}
31713311
3172- template <typename Opt>
3173- Maybe<bool > Session::OptionsObject::SetOption (Opt* options,
3174- const Local<Object>& object,
3175- const Local<String>& name,
3176- uint64_t Opt::*member) {
3177- Local<Value> value;
3178- if (!object->Get (env ()->context (), name).ToLocal (&value))
3179- return Nothing<bool >();
3180-
3181- if (value->IsUndefined ()) return Just (false );
3182-
3183- CHECK_IMPLIES (!value->IsBigInt (), value->IsNumber ());
3184-
3185- uint64_t val = 0 ;
3186- if (value->IsBigInt ()) {
3187- bool lossless = true ;
3188- val = value.As <BigInt>()->Uint64Value (&lossless);
3189- if (!lossless) {
3190- Utf8Value label (env ()->isolate (), name);
3191- THROW_ERR_OUT_OF_RANGE (
3192- env (),
3193- (std::string (" options." ) + (*label) + " is out of range" ).c_str ());
3194- return Nothing<bool >();
3195- }
3196- } else {
3197- val = static_cast <int64_t >(value.As <Number>()->Value ());
3198- }
3199- options->*member = val;
3200- return Just (true );
3201- }
3202-
3203- template <typename Opt>
3204- Maybe<bool > Session::OptionsObject::SetOption (Opt* options,
3205- const Local<Object>& object,
3206- const Local<String>& name,
3207- uint32_t Opt::*member) {
3208- Local<Value> value;
3209- if (!object->Get (env ()->context (), name).ToLocal (&value))
3210- return Nothing<bool >();
3211-
3212- if (value->IsUndefined ()) return Just (false );
3213-
3214- CHECK (value->IsUint32 ());
3215- uint32_t val = value.As <Uint32>()->Value ();
3216- options->*member = val;
3217- return Just (true );
3218- }
3219-
3220- template <typename Opt>
3221- Maybe<bool > Session::OptionsObject::SetOption (Opt* options,
3222- const Local<Object>& object,
3223- const Local<String>& name,
3224- bool Opt::*member) {
3225- Local<Value> value;
3226- if (!object->Get (env ()->context (), name).ToLocal (&value))
3227- return Nothing<bool >();
3228- if (value->IsUndefined ()) return Just (false );
3229- CHECK (value->IsBoolean ());
3230- options->*member = value->IsTrue ();
3231- return Just (true );
3232- }
3233-
3234- template <typename Opt>
3235- Maybe<bool > Session::OptionsObject::SetOption (Opt* options,
3236- const Local<Object>& object,
3237- const Local<String>& name,
3238- std::string Opt::*member) {
3239- Local<Value> value;
3240- if (!object->Get (env ()->context (), name).ToLocal (&value))
3241- return Nothing<bool >();
3242- if (value->IsUndefined ()) return Just (false );
3243- Utf8Value val (env ()->isolate (), value);
3244- options->*member = val.ToString ();
3245- return Just (true );
3246- }
3247-
3248- template <typename Opt>
3249- Maybe<bool > Session::OptionsObject::SetOption (
3250- Opt* options,
3251- const Local<Object>& object,
3252- const Local<String>& name,
3253- std::vector<std::shared_ptr<crypto::KeyObjectData>> Opt::*member) {
3254- Local<Value> value;
3255- if (!object->Get (env ()->context (), name).ToLocal (&value))
3256- return Nothing<bool >();
3257- if (value->IsArray ()) {
3258- auto context = env ()->context ();
3259- auto values = value.As <v8::Array>();
3260- uint32_t count = values->Length ();
3261- for (uint32_t n = 0 ; n < count; n++) {
3262- Local<Value> item;
3263- if (!values->Get (context, n).ToLocal (&item)) {
3264- return Nothing<bool >();
3265- }
3266- if (crypto::KeyObjectHandle::HasInstance (env (), item)) {
3267- crypto::KeyObjectHandle* handle;
3268- ASSIGN_OR_RETURN_UNWRAP (&handle, item, Nothing<bool >());
3269- (options->*member).push_back (handle->Data ());
3270- }
3271- }
3272- } else if (crypto::KeyObjectHandle::HasInstance (env (), value)) {
3273- crypto::KeyObjectHandle* handle;
3274- ASSIGN_OR_RETURN_UNWRAP (&handle, value, Nothing<bool >());
3275- (options->*member).push_back (handle->Data ());
3276- } else {
3277- UNREACHABLE ();
3278- }
3279- return Just (true );
3280- }
3281-
3282- template <typename Opt>
3283- Maybe<bool > Session::OptionsObject::SetOption (Opt* options,
3284- const Local<Object>& object,
3285- const Local<String>& name,
3286- std::vector<Store> Opt::*member) {
3287- Local<Value> value;
3288- if (!object->Get (env ()->context (), name).ToLocal (&value))
3289- return Nothing<bool >();
3290- if (value->IsArray ()) {
3291- auto context = env ()->context ();
3292- auto values = value.As <v8::Array>();
3293- uint32_t count = values->Length ();
3294- for (uint32_t n = 0 ; n < count; n++) {
3295- Local<Value> item;
3296- if (!values->Get (context, n).ToLocal (&item)) {
3297- return Nothing<bool >();
3298- }
3299- if (item->IsArrayBufferView ()) {
3300- Store store (item.As <ArrayBufferView>());
3301- (options->*member).push_back (std::move (store));
3302- }
3303- }
3304- } else if (value->IsArrayBufferView ()) {
3305- Store store (value.As <ArrayBufferView>());
3306- (options->*member).push_back (std::move (store));
3307- }
3308-
3309- return Just (true );
3310- }
3311-
33123312void Session::OptionsObject::MemoryInfo (MemoryTracker* tracker) const {
33133313 tracker->TrackField (" options" , options_);
33143314}
0 commit comments