Skip to content

Commit

Permalink
Merge pull request #673 from cloudflare/felix/anon-namespace
Browse files Browse the repository at this point in the history
[nfc] move static code into anonymous namespaces
  • Loading branch information
fhanau authored May 19, 2023
2 parents 6466b47 + 19d40ee commit 7c1806b
Show file tree
Hide file tree
Showing 12 changed files with 66 additions and 29 deletions.
34 changes: 17 additions & 17 deletions src/workerd/api/actor-state.c++
Original file line number Diff line number Diff line change
Expand Up @@ -118,23 +118,6 @@ ActorObserver& currentActorMetrics() {
return IoContext::current().getActorOrThrow().getMetrics();
}

} // namespace

jsg::Promise<jsg::Value> DurableObjectStorageOperations::get(
kj::OneOf<kj::String, kj::Array<kj::String>> keys, jsg::Optional<GetOptions> maybeOptions,
v8::Isolate* isolate) {
auto options = configureOptions(kj::mv(maybeOptions).orDefault(GetOptions{}));
KJ_SWITCH_ONEOF(keys) {
KJ_CASE_ONEOF(s, kj::String) {
return getOne(kj::mv(s), options, isolate);
}
KJ_CASE_ONEOF(a, kj::Array<kj::String>) {
return getMultiple(kj::mv(a), options, isolate);
}
}
KJ_UNREACHABLE
}

jsg::Value listResultsToMap(v8::Isolate* isolate, ActorCacheOps::GetResultList value, bool completelyCached) {
v8::HandleScope scope(isolate);
auto context = isolate->GetCurrentContext();
Expand Down Expand Up @@ -210,6 +193,23 @@ kj::Function<jsg::Value(v8::Isolate*, ActorCacheOps::GetResultList)> getMultiple
};
}

} // namespace

jsg::Promise<jsg::Value> DurableObjectStorageOperations::get(
kj::OneOf<kj::String, kj::Array<kj::String>> keys, jsg::Optional<GetOptions> maybeOptions,
v8::Isolate* isolate) {
auto options = configureOptions(kj::mv(maybeOptions).orDefault(GetOptions{}));
KJ_SWITCH_ONEOF(keys) {
KJ_CASE_ONEOF(s, kj::String) {
return getOne(kj::mv(s), options, isolate);
}
KJ_CASE_ONEOF(a, kj::Array<kj::String>) {
return getMultiple(kj::mv(a), options, isolate);
}
}
KJ_UNREACHABLE
}

jsg::Promise<jsg::Value> DurableObjectStorageOperations::getOne(
kj::String key, const GetOptions& options, v8::Isolate* isolate) {
ActorStorageLimits::checkMaxKeySize(key);
Expand Down
4 changes: 4 additions & 0 deletions src/workerd/api/crypto-impl-asymmetric.c++
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,8 @@ kj::Maybe<T> fromBignum(kj::ArrayPtr<kj::byte> value) {
return asUnsigned;
}

namespace {

void validateRsaParams(int modulusLength, kj::ArrayPtr<kj::byte> publicExponent,
bool warnImport = false) {
// The W3C standard itself doesn't describe any parameter validation but the conformance tests
Expand Down Expand Up @@ -834,6 +836,8 @@ void validateRsaParams(int modulusLength, kj::ArrayPtr<kj::byte> publicExponent,
}
}

} // namespace

kj::OneOf<jsg::Ref<CryptoKey>, CryptoKeyPair> CryptoKey::Impl::generateRsa(
kj::StringPtr normalizedName,
SubtleCrypto::GenerateKeyAlgorithm&& algorithm, bool extractable,
Expand Down
4 changes: 4 additions & 0 deletions src/workerd/api/html-rewriter.c++
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,8 @@ Rewriter::Rewriter(
ioContext(IoContext::current()),
maybeAsyncContext(jsg::AsyncContextFrame::currentRef(js)) {}

namespace {

// The stack size floor enforced by kj. We could go lower,
// but it'd always be increased to this anyway.
const size_t FIBER_STACK_SIZE = 1024 * 64;
Expand All @@ -405,6 +407,8 @@ const kj::FiberPool& getFiberPool() {
return FIBER_POOL;
}

} // namespace

kj::Promise<void> Rewriter::write(const void* buffer, size_t size) {
KJ_ASSERT(maybeWaitScope == nullptr);
return getFiberPool().startFiber([this, buffer, size](kj::WaitScope& scope) {
Expand Down
4 changes: 4 additions & 0 deletions src/workerd/api/http.c++
Original file line number Diff line number Diff line change
Expand Up @@ -1823,6 +1823,8 @@ jsg::Ref<Response> makeHttpResponse(
nullptr, kj::mv(responseBody), flags, kj::mv(urlList), kj::mv(webSocket));
}

namespace {

jsg::Promise<jsg::Ref<Response>> fetchImplNoOutputLock(
jsg::Lock& js,
kj::Maybe<jsg::Ref<Fetcher>> fetcher,
Expand Down Expand Up @@ -1867,6 +1869,8 @@ jsg::Promise<jsg::Ref<Response>> fetchImplNoOutputLock(
});
}

} // namespace

jsg::Promise<jsg::Ref<Response>> fetchImpl(
jsg::Lock& js,
kj::Maybe<jsg::Ref<Fetcher>> fetcher,
Expand Down
22 changes: 15 additions & 7 deletions src/workerd/api/r2-bucket.c++
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ static kj::Maybe<jsg::Ref<T>> parseObjectMetadata(kj::StringPtr action, R2Result
return parseObjectMetadata<T>(responseBuilder, expectedFields, kj::fwd<Args>(args)...);
}

namespace {

void addEtagsToBuilder(capnp::List<R2Etag>::Builder etagListBuilder, kj::ArrayPtr<R2Bucket::Etag> etagArray) {
R2Bucket::Etag* currentEtag = etagArray.begin();
for (unsigned int i = 0; i < etagArray.size(); i++) {
Expand All @@ -207,6 +209,8 @@ void addEtagsToBuilder(capnp::List<R2Etag>::Builder etagListBuilder, kj::ArrayPt
}
}

} // namespace

template <typename Builder, typename Options>
void initOnlyIf(jsg::Lock& js, Builder& builder, Options& o) {
KJ_IF_MAYBE(i, o.onlyIf) {
Expand Down Expand Up @@ -826,6 +830,8 @@ jsg::Promise<R2Bucket::ListResult> R2Bucket::list(
});
}

namespace {

kj::Array<R2Bucket::Etag> parseConditionalEtagHeader(
kj::StringPtr condHeader,
kj::Vector<R2Bucket::Etag> etagAccumulator = kj::Vector<R2Bucket::Etag>(),
Expand Down Expand Up @@ -907,6 +913,15 @@ kj::Array<R2Bucket::Etag> parseConditionalEtagHeader(
}
}

kj::Array<R2Bucket::Etag> buildSingleStrongEtagArray(kj::StringPtr etagValue) {
struct R2Bucket::StrongEtag etag = {.value = kj::str(etagValue)};
kj::ArrayBuilder<R2Bucket::Etag> etagArrayBuilder = kj::heapArrayBuilder<R2Bucket::Etag>(1);
etagArrayBuilder.add(kj::mv(etag));
return etagArrayBuilder.finish();
}

} // namespace

R2Bucket::UnwrappedConditional::UnwrappedConditional(jsg::Lock& js, Headers& h)
: secondsGranularity(true) {
KJ_IF_MAYBE(e, h.get(jsg::ByteString(kj::str("if-match")))) {
Expand All @@ -925,13 +940,6 @@ R2Bucket::UnwrappedConditional::UnwrappedConditional(jsg::Lock& js, Headers& h)
}
}

kj::Array<R2Bucket::Etag> buildSingleStrongEtagArray(kj::StringPtr etagValue) {
struct R2Bucket::StrongEtag etag = {.value = kj::str(etagValue)};
kj::ArrayBuilder<R2Bucket::Etag> etagArrayBuilder = kj::heapArrayBuilder<R2Bucket::Etag>(1);
etagArrayBuilder.add(kj::mv(etag));
return etagArrayBuilder.finish();
}

R2Bucket::UnwrappedConditional::UnwrappedConditional(const Conditional& c)
: secondsGranularity(c.secondsGranularity.orDefault(false)) {
KJ_IF_MAYBE(e, c.etagMatches) {
Expand Down
3 changes: 3 additions & 0 deletions src/workerd/api/sockets.c++
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace workerd::api {

namespace {

bool isValidHost(kj::StringPtr host) {
// This function performs some basic length and characters checks, it does not guarantee that
Expand Down Expand Up @@ -102,6 +103,8 @@ jsg::Ref<Socket> setupSocket(
return result;
}

} // namespace

jsg::Ref<Socket> connectImplNoOutputLock(
jsg::Lock& js, jsg::Ref<Fetcher> fetcher, AnySocketAddress address,
jsg::Optional<SocketOptions> options) {
Expand Down
8 changes: 7 additions & 1 deletion src/workerd/api/web-socket.c++
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ void WebSocket::initConnection(jsg::Lock& js, kj::Promise<PackedWebSocket> prom)
// whichever comes first.
}

namespace {

// See item 10 of https://datatracker.ietf.org/doc/html/rfc6455#section-4.1
bool validProtoToken(const kj::StringPtr protocol) {
if (kj::size(protocol) == 0) {
Expand Down Expand Up @@ -160,6 +162,8 @@ bool validProtoToken(const kj::StringPtr protocol) {
return true;
}

} // namespace

jsg::Ref<WebSocket> WebSocket::constructor(
jsg::Lock& js,
kj::String url,
Expand Down Expand Up @@ -711,6 +715,7 @@ void WebSocket::ensurePumping(jsg::Lock& js) {
}

namespace {

size_t countBytesFromMessage(const kj::WebSocket::Message& message) {
// This does not count the extra data of the RPC frame or the savings from any compression.
// We're incentivizing customers to use reasonably sized messages, not trying to get an exact
Expand All @@ -733,7 +738,8 @@ size_t countBytesFromMessage(const kj::WebSocket::Message& message) {

KJ_UNREACHABLE;
}
}

} // namespace

kj::Promise<void> WebSocket::pump(
IoContext& context, OutgoingMessagesMap& outgoingMessages, kj::WebSocket& ws, Native& native) {
Expand Down
4 changes: 4 additions & 0 deletions src/workerd/io/io-gate.c++
Original file line number Diff line number Diff line change
Expand Up @@ -336,8 +336,12 @@ bool OutputGate::isBroken() {
return brokenState.is<kj::Exception>();
}

namespace {

void END_OUTPUT_LOCK_CANCELATION_STACK_START_WAITER_STACK() {}

} // namespace

kj::Exception OutputGate::makeUnfulfilledException() {
return kj::getDestructionReason(
reinterpret_cast<void*>(&END_OUTPUT_LOCK_CANCELATION_STACK_START_WAITER_STACK),
Expand Down
3 changes: 3 additions & 0 deletions src/workerd/jsg/exception.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ namespace workerd::jsg {
} \
} while (0)

kj::StringPtr stripRemoteExceptionPrefix(kj::StringPtr internalMessage);
// Given a KJ exception's description, strips any leading "remote exception: " prefixes.

bool isTunneledException(kj::StringPtr internalMessage);
// Given a KJ exception's description, returns whether it contains a tunneled exception that could
// be converted back to JavaScript via makeInternalError().
Expand Down
3 changes: 0 additions & 3 deletions src/workerd/jsg/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,6 @@ kj::Exception createTunneledException(v8::Isolate* isolate, v8::Local<v8::Value>
//
// Equivalent to throwing the exception returned by `createTunneledException(exception)`.

kj::StringPtr stripRemoteExceptionPrefix(kj::StringPtr internalMessage);
// Given a KJ exception's description, strips any leading "remote exception: " prefixes.

template <typename T>
v8::Local<T> check(v8::MaybeLocal<T> maybe) {
// V8 usually returns a MaybeLocal to mean that the function can throw a JavaScript exception.
Expand Down
4 changes: 4 additions & 0 deletions src/workerd/server/alarm-scheduler.c++
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ int AlarmScheduler::maxJitterMsForDelay(kj::Duration delay) {
return std::floor(RETRY_JITTER_FACTOR * delayMs);
}

namespace {

std::default_random_engine makeSeededRandomEngine() {
// Using the time as a seed here is fine, we just want to have some randomness for retry jitter
auto time = kj::systemPreciseMonotonicClock().now();
Expand All @@ -20,6 +22,8 @@ std::default_random_engine makeSeededRandomEngine() {
return engine;
}

} // namespace

AlarmScheduler::AlarmScheduler(
const kj::Clock& clock,
kj::Timer& timer,
Expand Down
2 changes: 1 addition & 1 deletion src/workerd/util/wait-list.c++
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ namespace {
thread_local CrossThreadWaitList::WaiterMap threadLocalWaiters;
// Optimization: If the same wait list is waited multiple times in the same thread, we want to
// share the signal rather than send two cross-thread signals.
} // namespace

void END_WAIT_LIST_CANCELER_STACK_START_CANCELEE_STACK() {}
} // namespace

CrossThreadWaitList::CrossThreadWaitList(Options options)
: state(kj::atomicRefcounted<State>(options)) {}
Expand Down

0 comments on commit 7c1806b

Please # to comment.