Skip to content

Commit d082fc9

Browse files
authored
Update api/html-rewriter to use KJ_IF_SOME (#1203)
1 parent a466521 commit d082fc9

File tree

1 file changed

+46
-46
lines changed

1 file changed

+46
-46
lines changed

src/workerd/api/html-rewriter.c++

+46-46
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public:
7575
if (chars.begin() != nullptr) {
7676
return kj::str(chars);
7777
} else {
78-
return nullptr;
78+
return kj::none;
7979
}
8080
}
8181

@@ -89,7 +89,7 @@ private:
8989
kj::Maybe<kj::Exception> tryGetLastError() {
9090
auto maybeErrorString = lol_html_take_last_error();
9191
if (maybeErrorString.data == nullptr) {
92-
return nullptr;
92+
return kj::none;
9393
}
9494
auto errorString = LolString(maybeErrorString);
9595
return kj::Exception(kj::Exception::Type::FAILED, __FILE__, __LINE__,
@@ -146,12 +146,12 @@ public:
146146
explicit TokenScope(jsg::Ref<T>& value)
147147
: contentToken(value.addRef()) {}
148148
~TokenScope() noexcept(false) {
149-
KJ_IF_MAYBE(token, contentToken) {
150-
(*token)->htmlContentScopeEnd();
149+
KJ_IF_SOME(token, contentToken) {
150+
token->htmlContentScopeEnd();
151151
}
152152
}
153153
TokenScope(TokenScope&& o) : contentToken(kj::mv(o.contentToken)) {
154-
o.contentToken = nullptr;
154+
o.contentToken = kj::none;
155155
}
156156
KJ_DISALLOW_COPY(TokenScope);
157157

@@ -300,13 +300,13 @@ private:
300300
// If a call to `lol-html` returned an error or propagated a user error from a handler
301301
// (LOL_HTML_STOP for instance); we consider its instance as poisoned. Future calls to
302302
// `lol_html_rewriter_write` and `lol_html_rewriter_end` will probably throw.
303-
return maybeException != nullptr;
303+
return maybeException != kj::none;
304304
}
305305

306306
void maybePoison(kj::Exception exception) {
307307
// Ignore this error if maybeException is already populated -- this error is probably just a
308308
// secondary effect.
309-
if (maybeException == nullptr) {
309+
if (maybeException == kj::none) {
310310
maybeException = kj::mv(exception);
311311
}
312312
}
@@ -332,11 +332,11 @@ kj::Own<lol_html_HtmlRewriter> Rewriter::buildRewriter(
332332
check(lol_html_rewriter_builder_add_element_content_handlers(
333333
builder,
334334
elementHandlers.selector,
335-
element == nullptr ? nullptr : &Rewriter::thunk<Element>,
335+
element == kj::none ? nullptr : &Rewriter::thunk<Element>,
336336
element.orDefault(nullptr),
337-
comments == nullptr ? nullptr : &Rewriter::thunk<Comment>,
337+
comments == kj::none ? nullptr : &Rewriter::thunk<Comment>,
338338
comments.orDefault(nullptr),
339-
text == nullptr ? nullptr : &Rewriter::thunk<Text>,
339+
text == kj::none ? nullptr : &Rewriter::thunk<Text>,
340340
text.orDefault(nullptr)));
341341
}
342342
KJ_CASE_ONEOF(documentHandlers, UnregisteredDocumentHandlers) {
@@ -348,13 +348,13 @@ kj::Own<lol_html_HtmlRewriter> Rewriter::buildRewriter(
348348
// Adding document content handlers cannot fail, so no need for check().
349349
lol_html_rewriter_builder_add_document_content_handlers(
350350
builder,
351-
doctype == nullptr ? nullptr : &Rewriter::thunk<Doctype>,
351+
doctype == kj::none ? nullptr : &Rewriter::thunk<Doctype>,
352352
doctype.orDefault(nullptr),
353-
comments == nullptr ? nullptr : &Rewriter::thunk<Comment>,
353+
comments == kj::none ? nullptr : &Rewriter::thunk<Comment>,
354354
comments.orDefault(nullptr),
355-
text == nullptr ? nullptr : &Rewriter::thunk<Text>,
355+
text == kj::none ? nullptr : &Rewriter::thunk<Text>,
356356
text.orDefault(nullptr),
357-
end == nullptr ? nullptr : &Rewriter::thunk<DocumentEnd>,
357+
end == kj::none ? nullptr : &Rewriter::thunk<DocumentEnd>,
358358
end.orDefault(nullptr));
359359
}
360360
}
@@ -406,7 +406,7 @@ const kj::FiberPool& getFiberPool() {
406406
} // namespace
407407

408408
kj::Promise<void> Rewriter::write(const void* buffer, size_t size) {
409-
KJ_ASSERT(maybeWaitScope == nullptr);
409+
KJ_ASSERT(maybeWaitScope == kj::none);
410410
return getFiberPool().startFiber([this, buffer, size](kj::WaitScope& scope) {
411411
maybeWaitScope = scope;
412412
if (!isPoisoned()) {
@@ -423,7 +423,7 @@ kj::Promise<void> Rewriter::write(const void* buffer, size_t size) {
423423

424424
kj::Promise<void> Rewriter::write(
425425
kj::ArrayPtr<const kj::ArrayPtr<const byte>> pieces) {
426-
KJ_ASSERT(maybeWaitScope == nullptr);
426+
KJ_ASSERT(maybeWaitScope == kj::none);
427427
return getFiberPool().startFiber([this, pieces](kj::WaitScope& scope) {
428428
maybeWaitScope = scope;
429429
if (!isPoisoned()) {
@@ -444,7 +444,7 @@ kj::Promise<void> Rewriter::write(
444444
}
445445

446446
kj::Promise<void> Rewriter::end() {
447-
KJ_ASSERT(maybeWaitScope == nullptr);
447+
KJ_ASSERT(maybeWaitScope == kj::none);
448448
return getFiberPool().startFiber([this](kj::WaitScope& scope) {
449449
maybeWaitScope = scope;
450450
if (!isPoisoned()) {
@@ -467,21 +467,21 @@ void Rewriter::abort(kj::Exception reason) {
467467
}
468468

469469
kj::Promise<void> Rewriter::finishWrite() {
470-
maybeWaitScope = nullptr;
470+
maybeWaitScope = kj::none;
471471
auto checkException = [this]() -> kj::Promise<void> {
472-
KJ_ASSERT(writePromise == nullptr);
472+
KJ_ASSERT(writePromise == kj::none);
473473

474-
KJ_IF_MAYBE(exception, maybeException) {
475-
inner->abort(kj::cp(*exception));
476-
return kj::cp(*exception);
474+
KJ_IF_SOME(exception, maybeException) {
475+
inner->abort(kj::cp(exception));
476+
return kj::cp(exception);
477477
}
478478

479479
return kj::READY_NOW;
480480
};
481481

482-
KJ_IF_MAYBE(wp, writePromise) {
483-
KJ_DEFER(writePromise = nullptr);
484-
return wp->then([checkException]() {
482+
KJ_IF_SOME(wp, writePromise) {
483+
KJ_DEFER(writePromise = kj::none);
484+
return wp.then([checkException]() {
485485
return checkException();
486486
});
487487
}
@@ -505,7 +505,7 @@ lol_html_rewriter_directive_t Rewriter::thunkImpl(
505505
}
506506

507507
try {
508-
KJ_IF_MAYBE(exception, kj::runCatchingExceptions([&] {
508+
KJ_IF_SOME(exception, kj::runCatchingExceptions([&] {
509509
// V8 has a thread local pointer that points to where the stack limit is on this thread which
510510
// is tested for overflows when we enter any JS code. However since we're running in a fiber
511511
// here, we're in an entirely different stack that V8 doesn't know about, so it gets confused
@@ -518,7 +518,7 @@ lol_html_rewriter_directive_t Rewriter::thunkImpl(
518518
// need to unwind the stack because we're probably still inside a cool_thing_rewriter_write().
519519
// We can't unwind with an exception across the Rust/C++ boundary, so instead we'll keep this
520520
// exception around and disable all later handlers.
521-
maybePoison(kj::mv(*exception));
521+
maybePoison(kj::mv(exception));
522522
return LOL_HTML_STOP;
523523
}
524524
} catch (kj::CanceledException) {
@@ -602,8 +602,8 @@ void Rewriter::outputImpl(const char* buffer, size_t size) {
602602
}
603603

604604
auto bufferCopy = kj::heapArray(buffer, size);
605-
KJ_IF_MAYBE(wp, writePromise) {
606-
*wp = wp->then([this, bufferCopy = kj::mv(bufferCopy)]() mutable {
605+
KJ_IF_SOME(wp, writePromise) {
606+
writePromise = wp.then([this, bufferCopy = kj::mv(bufferCopy)]() mutable {
607607
return inner->write(bufferCopy.begin(), bufferCopy.size()).attach(kj::mv(bufferCopy));
608608
});
609609
} else {
@@ -654,16 +654,16 @@ kj::Maybe<kj::String> Element::getAttribute(kj::String name) {
654654
&checkToken(impl).element, name.cStr(), name.size()));
655655
// TODO(perf): We could construct a v8::String directly here, saving a copy.
656656
kj::Maybe kjAttr = attr.asKjString();
657-
if (kjAttr != nullptr) {
657+
if (kjAttr != kj::none) {
658658
return kj::mv(kjAttr);
659659
}
660660

661-
KJ_IF_MAYBE(exception, tryGetLastError()) {
662-
kj::throwFatalException(kj::mv(*exception));
661+
KJ_IF_SOME(exception, tryGetLastError()) {
662+
kj::throwFatalException(kj::mv(exception));
663663
}
664664

665665
// No error, just doesn't exist.
666-
return nullptr;
666+
return kj::none;
667667
}
668668

669669
bool Element::hasAttribute(kj::String name) {
@@ -733,7 +733,7 @@ void Element::onEndTag(ElementCallbackFunction&& callback) {
733733
EndTag::EndTag(CType& endTag, Rewriter&): impl(endTag) {}
734734

735735
void EndTag::htmlContentScopeEnd() {
736-
impl = nullptr;
736+
impl = kj::none;
737737
}
738738

739739
kj::String EndTag::getName() {
@@ -771,7 +771,7 @@ jsg::Ref<EndTag> EndTag::remove() {
771771
}
772772

773773
void Element::htmlContentScopeEnd() {
774-
impl = nullptr;
774+
impl = kj::none;
775775
}
776776

777777
Element::Impl::Impl(CType& element, Rewriter& rewriter): element(element), rewriter(rewriter) {}
@@ -799,7 +799,7 @@ Element::AttributesIterator::Next Element::AttributesIterator::next() {
799799
// End of iteration.
800800
// TODO(someday): Eagerly deallocate. Can't seem to nullify the Own without also nullifying the
801801
// enclosing Maybe, however.
802-
return { true, nullptr };
802+
return { true, kj::none };
803803
}
804804

805805
auto name = LolString(lol_html_attribute_name_get(attribute));
@@ -809,7 +809,7 @@ Element::AttributesIterator::Next Element::AttributesIterator::next() {
809809
}
810810

811811
void Element::AttributesIterator::htmlContentScopeEnd() {
812-
impl = nullptr;
812+
impl = kj::none;
813813
}
814814

815815
// =======================================================================================
@@ -868,7 +868,7 @@ jsg::Ref<Comment> Comment::remove() {
868868
}
869869

870870
void Comment::htmlContentScopeEnd() {
871-
impl = nullptr;
871+
impl = kj::none;
872872
}
873873

874874
// =======================================================================================
@@ -928,7 +928,7 @@ jsg::Ref<Text> Text::remove() {
928928
}
929929

930930
void Text::htmlContentScopeEnd() {
931-
impl = nullptr;
931+
impl = kj::none;
932932
}
933933

934934
// =======================================================================================
@@ -952,7 +952,7 @@ kj::Maybe<kj::String> Doctype::getSystemId() {
952952
}
953953

954954
void Doctype::htmlContentScopeEnd() {
955-
impl = nullptr;
955+
impl = kj::none;
956956
}
957957

958958
// =======================================================================================
@@ -971,7 +971,7 @@ jsg::Ref<DocumentEnd> DocumentEnd::append(Content content, jsg::Optional<Content
971971
}
972972

973973
void DocumentEnd::htmlContentScopeEnd() {
974-
impl = nullptr;
974+
impl = kj::none;
975975
}
976976

977977
// =======================================================================================
@@ -1026,7 +1026,7 @@ jsg::Ref<HTMLRewriter> HTMLRewriter::onDocument(DocumentContentHandlers&& handle
10261026
jsg::Ref<Response> HTMLRewriter::transform(jsg::Lock& js, jsg::Ref<Response> response) {
10271027
auto maybeInput = response->getBody();
10281028

1029-
if (maybeInput == nullptr) {
1029+
if (maybeInput == kj::none) {
10301030
// That was easy!
10311031
return kj::mv(response);
10321032
}
@@ -1042,12 +1042,12 @@ jsg::Ref<Response> HTMLRewriter::transform(jsg::Lock& js, jsg::Ref<Response> res
10421042
kj::String ownContentType;
10431043
kj::String encoding = kj::str("utf-8");
10441044
auto contentTypeKey = jsg::ByteString(kj::str("content-type"));
1045-
KJ_IF_MAYBE(contentType, response->getHeaders(js)->get(kj::mv(contentTypeKey))) {
1045+
KJ_IF_SOME(contentType, response->getHeaders(js)->get(kj::mv(contentTypeKey))) {
10461046
// TODO(cleanup): readContentTypeParameter can be replaced with using
10471047
// workerd/util/mimetype.h directly.
1048-
KJ_IF_MAYBE(charset, readContentTypeParameter(*contentType, "charset")) {
1049-
ownContentType = kj::mv(*contentType);
1050-
encoding = kj::mv(*charset);
1048+
KJ_IF_SOME(charset, readContentTypeParameter(contentType, "charset")) {
1049+
ownContentType = kj::mv(contentType);
1050+
encoding = kj::mv(charset);
10511051
}
10521052
}
10531053

0 commit comments

Comments
 (0)