Skip to content

Commit

Permalink
make context a protected attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig committed Sep 9, 2024
1 parent 4d89e87 commit ac508a9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 17 deletions.
22 changes: 11 additions & 11 deletions src/workerd/api/node/zlib-util.c++
Original file line number Diff line number Diff line change
Expand Up @@ -499,11 +499,11 @@ void ZlibUtil::CompressionStream<CompressionContext>::writeStream(jsg::Lock& js,

writing = true;

context().setBuffers(input, inputLength, output, outputLength);
context().setFlush(flush);
context.setBuffers(input, inputLength, output, outputLength);
context.setFlush(flush);

if constexpr (!async) {
context().work();
context.work();
if (checkError(js)) {
updateWriteResult();
writing = false;
Expand All @@ -513,7 +513,7 @@ void ZlibUtil::CompressionStream<CompressionContext>::writeStream(jsg::Lock& js,

// On Node.js, this is called as a result of `ScheduleWork()` call.
// Since, we implement the whole thing as sync, we're going to ahead and call the whole thing here.
context().work();
context.work();

// This is implemented slightly differently in Node.js
// Node.js calls AfterThreadPoolWork().
Expand Down Expand Up @@ -543,7 +543,7 @@ void ZlibUtil::CompressionStream<CompressionContext>::close() {

template <typename CompressionContext>
bool ZlibUtil::CompressionStream<CompressionContext>::checkError(jsg::Lock& js) {
KJ_IF_SOME(error, context().getError()) {
KJ_IF_SOME(error, context.getError()) {
emitError(js, kj::mv(error));
return false;
}
Expand All @@ -562,7 +562,7 @@ template <typename CompressionContext>
void ZlibUtil::CompressionStream<CompressionContext>::updateWriteResult() {
KJ_IF_SOME(wr, writeResult) {
auto ptr = wr.template asArrayPtr<uint32_t>();
context().getAfterWriteResult(&ptr[1], &ptr[0]);
context.getAfterWriteResult(&ptr[1], &ptr[0]);
}
}

Expand Down Expand Up @@ -600,7 +600,7 @@ void ZlibUtil::CompressionStream<CompressionContext>::write(jsg::Lock& js,

template <typename CompressionContext>
void ZlibUtil::CompressionStream<CompressionContext>::reset(jsg::Lock& js) {
KJ_IF_SOME(error, context().resetStream()) {
KJ_IF_SOME(error, context.resetStream()) {
emitError(js, kj::mv(error));
}
}
Expand All @@ -617,13 +617,13 @@ void ZlibUtil::ZlibStream::initialize(int windowBits,
jsg::Function<void()> writeCallback,
jsg::Optional<kj::Array<kj::byte>> dictionary) {
initializeStream(kj::mv(writeState), kj::mv(writeCallback));
context().setAllocationFunctions(AllocForZlib, FreeForZlib, this);
context().initialize(level, windowBits, memLevel, strategy, kj::mv(dictionary));
context.setAllocationFunctions(AllocForZlib, FreeForZlib, this);
context.initialize(level, windowBits, memLevel, strategy, kj::mv(dictionary));
}

void ZlibUtil::ZlibStream::params(jsg::Lock& js, int _level, int _strategy) {
context().setParams(_level, _strategy);
KJ_IF_SOME(err, context().getError()) {
context.setParams(_level, _strategy);
KJ_IF_SOME(err, context.getError()) {
emitError(js, kj::mv(err));
}
}
Expand Down
9 changes: 3 additions & 6 deletions src/workerd/api/node/zlib-util.h
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ class ZlibUtil final: public jsg::Object {
template <class CompressionContext>
class CompressionStream: public jsg::Object {
public:
explicit CompressionStream(ZlibMode _mode): context_(_mode) {}
explicit CompressionStream(ZlibMode _mode): context(_mode) {}
CompressionStream() = default;
// TODO(soon): Find a way to add noexcept(false) to this destructor.
~CompressionStream();
Expand Down Expand Up @@ -338,9 +338,7 @@ class ZlibUtil final: public jsg::Object {
}

protected:
CompressionContext& context() {
return *&context_;
}
CompressionContext context;

void initializeStream(jsg::BufferSource _write_result, jsg::Function<void()> writeCallback);

Expand All @@ -357,7 +355,6 @@ class ZlibUtil final: public jsg::Object {
// context to avoid `heap-use-after-free` ASan error.
kj::HashMap<uint8_t*, kj::Array<uint8_t>> allocations;

CompressionContext context_;
bool initialized = false;
bool writing = false;
bool pending_close = false;
Expand Down Expand Up @@ -421,7 +418,7 @@ class ZlibUtil final: public jsg::Object {
}

CompressionContext& context() {
return this->CompressionStream<CompressionContext>::context();
return *&this->CompressionStream<CompressionContext>::context;
}
};

Expand Down

0 comments on commit ac508a9

Please sign in to comment.