Skip to content

Commit

Permalink
fixup! fixup! fixup! fixup! src: implement native quic api
Browse files Browse the repository at this point in the history
  • Loading branch information
jasnell committed Aug 28, 2022
1 parent eaa2d95 commit 2652d56
Showing 1 changed file with 140 additions and 140 deletions.
280 changes: 140 additions & 140 deletions src/quic/session.cc
Expand Up @@ -2856,6 +2856,146 @@ Session::OptionsObject::OptionsObject(Environment* env,
MakeWeak();
}

template <typename Opt>
Maybe<bool> Session::OptionsObject::SetOption(Opt* options,
const Local<Object>& object,
const Local<String>& name,
uint64_t Opt::*member) {
Local<Value> value;
if (!object->Get(env()->context(), name).ToLocal(&value))
return Nothing<bool>();

if (value->IsUndefined()) return Just(false);

CHECK_IMPLIES(!value->IsBigInt(), value->IsNumber());

uint64_t val = 0;
if (value->IsBigInt()) {
bool lossless = true;
val = value.As<BigInt>()->Uint64Value(&lossless);
if (!lossless) {
Utf8Value label(env()->isolate(), name);
THROW_ERR_OUT_OF_RANGE(
env(),
(std::string("options.") + (*label) + " is out of range").c_str());
return Nothing<bool>();
}
} else {
val = static_cast<int64_t>(value.As<Number>()->Value());
}
options->*member = val;
return Just(true);
}

template <typename Opt>
Maybe<bool> Session::OptionsObject::SetOption(Opt* options,
const Local<Object>& object,
const Local<String>& name,
uint32_t Opt::*member) {
Local<Value> value;
if (!object->Get(env()->context(), name).ToLocal(&value))
return Nothing<bool>();

if (value->IsUndefined()) return Just(false);

CHECK(value->IsUint32());
uint32_t val = value.As<Uint32>()->Value();
options->*member = val;
return Just(true);
}

template <typename Opt>
Maybe<bool> Session::OptionsObject::SetOption(Opt* options,
const Local<Object>& object,
const Local<String>& name,
bool Opt::*member) {
Local<Value> value;
if (!object->Get(env()->context(), name).ToLocal(&value))
return Nothing<bool>();
if (value->IsUndefined()) return Just(false);
CHECK(value->IsBoolean());
options->*member = value->IsTrue();
return Just(true);
}

template <typename Opt>
Maybe<bool> Session::OptionsObject::SetOption(Opt* options,
const Local<Object>& object,
const Local<String>& name,
std::string Opt::*member) {
Local<Value> value;
if (!object->Get(env()->context(), name).ToLocal(&value))
return Nothing<bool>();
if (value->IsUndefined()) return Just(false);
Utf8Value val(env()->isolate(), value);
options->*member = val.ToString();
return Just(true);
}

template <typename Opt>
Maybe<bool> Session::OptionsObject::SetOption(
Opt* options,
const Local<Object>& object,
const Local<String>& name,
std::vector<std::shared_ptr<crypto::KeyObjectData>> Opt::*member) {
Local<Value> value;
if (!object->Get(env()->context(), name).ToLocal(&value))
return Nothing<bool>();
if (value->IsArray()) {
auto context = env()->context();
auto values = value.As<v8::Array>();
uint32_t count = values->Length();
for (uint32_t n = 0; n < count; n++) {
Local<Value> item;
if (!values->Get(context, n).ToLocal(&item)) {
return Nothing<bool>();
}
if (crypto::KeyObjectHandle::HasInstance(env(), item)) {
crypto::KeyObjectHandle* handle;
ASSIGN_OR_RETURN_UNWRAP(&handle, item, Nothing<bool>());
(options->*member).push_back(handle->Data());
}
}
} else if (crypto::KeyObjectHandle::HasInstance(env(), value)) {
crypto::KeyObjectHandle* handle;
ASSIGN_OR_RETURN_UNWRAP(&handle, value, Nothing<bool>());
(options->*member).push_back(handle->Data());
} else {
UNREACHABLE();
}
return Just(true);
}

template <typename Opt>
Maybe<bool> Session::OptionsObject::SetOption(Opt* options,
const Local<Object>& object,
const Local<String>& name,
std::vector<Store> Opt::*member) {
Local<Value> value;
if (!object->Get(env()->context(), name).ToLocal(&value))
return Nothing<bool>();
if (value->IsArray()) {
auto context = env()->context();
auto values = value.As<v8::Array>();
uint32_t count = values->Length();
for (uint32_t n = 0; n < count; n++) {
Local<Value> item;
if (!values->Get(context, n).ToLocal(&item)) {
return Nothing<bool>();
}
if (item->IsArrayBufferView()) {
Store store(item.As<ArrayBufferView>());
(options->*member).push_back(std::move(store));
}
}
} else if (value->IsArrayBufferView()) {
Store store(value.As<ArrayBufferView>());
(options->*member).push_back(std::move(store));
}

return Just(true);
}

void Session::OptionsObject::New(const FunctionCallbackInfo<Value>& args) {
CHECK(args.IsConstructCall());
auto env = Environment::GetCurrent(args);
Expand Down Expand Up @@ -3169,146 +3309,6 @@ void Session::OptionsObject::New(const FunctionCallbackInfo<Value>& args) {
}
}

template <typename Opt>
Maybe<bool> Session::OptionsObject::SetOption(Opt* options,
const Local<Object>& object,
const Local<String>& name,
uint64_t Opt::*member) {
Local<Value> value;
if (!object->Get(env()->context(), name).ToLocal(&value))
return Nothing<bool>();

if (value->IsUndefined()) return Just(false);

CHECK_IMPLIES(!value->IsBigInt(), value->IsNumber());

uint64_t val = 0;
if (value->IsBigInt()) {
bool lossless = true;
val = value.As<BigInt>()->Uint64Value(&lossless);
if (!lossless) {
Utf8Value label(env()->isolate(), name);
THROW_ERR_OUT_OF_RANGE(
env(),
(std::string("options.") + (*label) + " is out of range").c_str());
return Nothing<bool>();
}
} else {
val = static_cast<int64_t>(value.As<Number>()->Value());
}
options->*member = val;
return Just(true);
}

template <typename Opt>
Maybe<bool> Session::OptionsObject::SetOption(Opt* options,
const Local<Object>& object,
const Local<String>& name,
uint32_t Opt::*member) {
Local<Value> value;
if (!object->Get(env()->context(), name).ToLocal(&value))
return Nothing<bool>();

if (value->IsUndefined()) return Just(false);

CHECK(value->IsUint32());
uint32_t val = value.As<Uint32>()->Value();
options->*member = val;
return Just(true);
}

template <typename Opt>
Maybe<bool> Session::OptionsObject::SetOption(Opt* options,
const Local<Object>& object,
const Local<String>& name,
bool Opt::*member) {
Local<Value> value;
if (!object->Get(env()->context(), name).ToLocal(&value))
return Nothing<bool>();
if (value->IsUndefined()) return Just(false);
CHECK(value->IsBoolean());
options->*member = value->IsTrue();
return Just(true);
}

template <typename Opt>
Maybe<bool> Session::OptionsObject::SetOption(Opt* options,
const Local<Object>& object,
const Local<String>& name,
std::string Opt::*member) {
Local<Value> value;
if (!object->Get(env()->context(), name).ToLocal(&value))
return Nothing<bool>();
if (value->IsUndefined()) return Just(false);
Utf8Value val(env()->isolate(), value);
options->*member = val.ToString();
return Just(true);
}

template <typename Opt>
Maybe<bool> Session::OptionsObject::SetOption(
Opt* options,
const Local<Object>& object,
const Local<String>& name,
std::vector<std::shared_ptr<crypto::KeyObjectData>> Opt::*member) {
Local<Value> value;
if (!object->Get(env()->context(), name).ToLocal(&value))
return Nothing<bool>();
if (value->IsArray()) {
auto context = env()->context();
auto values = value.As<v8::Array>();
uint32_t count = values->Length();
for (uint32_t n = 0; n < count; n++) {
Local<Value> item;
if (!values->Get(context, n).ToLocal(&item)) {
return Nothing<bool>();
}
if (crypto::KeyObjectHandle::HasInstance(env(), item)) {
crypto::KeyObjectHandle* handle;
ASSIGN_OR_RETURN_UNWRAP(&handle, item, Nothing<bool>());
(options->*member).push_back(handle->Data());
}
}
} else if (crypto::KeyObjectHandle::HasInstance(env(), value)) {
crypto::KeyObjectHandle* handle;
ASSIGN_OR_RETURN_UNWRAP(&handle, value, Nothing<bool>());
(options->*member).push_back(handle->Data());
} else {
UNREACHABLE();
}
return Just(true);
}

template <typename Opt>
Maybe<bool> Session::OptionsObject::SetOption(Opt* options,
const Local<Object>& object,
const Local<String>& name,
std::vector<Store> Opt::*member) {
Local<Value> value;
if (!object->Get(env()->context(), name).ToLocal(&value))
return Nothing<bool>();
if (value->IsArray()) {
auto context = env()->context();
auto values = value.As<v8::Array>();
uint32_t count = values->Length();
for (uint32_t n = 0; n < count; n++) {
Local<Value> item;
if (!values->Get(context, n).ToLocal(&item)) {
return Nothing<bool>();
}
if (item->IsArrayBufferView()) {
Store store(item.As<ArrayBufferView>());
(options->*member).push_back(std::move(store));
}
}
} else if (value->IsArrayBufferView()) {
Store store(value.As<ArrayBufferView>());
(options->*member).push_back(std::move(store));
}

return Just(true);
}

void Session::OptionsObject::MemoryInfo(MemoryTracker* tracker) const {
tracker->TrackField("options", options_);
}
Expand Down

0 comments on commit 2652d56

Please sign in to comment.