-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
+5 passing node:zlib tests #15944
Open
Jarred-Sumner
wants to merge
5
commits into
main
Choose a base branch
from
jarred/zlib-tests
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
+5 passing node:zlib tests #15944
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a62df5c
+4 passing node:zlib tests
Jarred-Sumner 75623dc
Create test-zlib-failed-init.js
Jarred-Sumner 4b58f9c
We aren't going to implement `--expose-internals`
Jarred-Sumner 3cac49f
Update zlib.classes.ts
Jarred-Sumner b77d569
Update node_zlib_binding.zig
Jarred-Sumner File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -6,6 +6,7 @@ const string = bun.string; | |||||||||||
const Output = bun.Output; | ||||||||||||
const ZigString = JSC.ZigString; | ||||||||||||
const validators = @import("./util/validators.zig"); | ||||||||||||
const debug = bun.Output.scoped(.zlib, true); | ||||||||||||
|
||||||||||||
pub fn crc32(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { | ||||||||||||
const arguments = callframe.arguments_old(2).ptr; | ||||||||||||
|
@@ -71,6 +72,8 @@ pub fn CompressionStream(comptime T: type) type { | |||||||||||
var in: ?[]const u8 = null; | ||||||||||||
var out: ?[]u8 = null; | ||||||||||||
|
||||||||||||
const this_value = callframe.this(); | ||||||||||||
|
||||||||||||
bun.assert(!arguments[0].isUndefined()); // must provide flush value | ||||||||||||
flush = arguments[0].toU32(); | ||||||||||||
_ = std.meta.intToEnum(bun.zlib.FlushValue, flush) catch bun.assert(false); // Invalid flush value | ||||||||||||
|
@@ -102,7 +105,9 @@ pub fn CompressionStream(comptime T: type) type { | |||||||||||
this.stream.setBuffers(in, out); | ||||||||||||
this.stream.setFlush(@intCast(flush)); | ||||||||||||
|
||||||||||||
// | ||||||||||||
// Only create the strong handle when we have a pending write | ||||||||||||
// And make sure to clear it when we are done. | ||||||||||||
this.this_value.set(globalThis, this_value); | ||||||||||||
|
||||||||||||
const vm = globalThis.bunVM(); | ||||||||||||
this.task = .{ .callback = &AsyncJob.runTask }; | ||||||||||||
|
@@ -136,13 +141,23 @@ pub fn CompressionStream(comptime T: type) type { | |||||||||||
|
||||||||||||
this.write_in_progress = false; | ||||||||||||
|
||||||||||||
if (!(this.checkError(globalThis) catch return globalThis.reportActiveExceptionAsUnhandled(error.JSError))) { | ||||||||||||
// Clear the strong handle before we call any callbacks. | ||||||||||||
const this_value = this.this_value.trySwap() orelse { | ||||||||||||
debug("this_value is null in runFromJSThread", .{}); | ||||||||||||
return; | ||||||||||||
}; | ||||||||||||
|
||||||||||||
this_value.ensureStillAlive(); | ||||||||||||
|
||||||||||||
if (!(this.checkError(globalThis, this_value) catch return globalThis.reportActiveExceptionAsUnhandled(error.JSError))) { | ||||||||||||
return; | ||||||||||||
} | ||||||||||||
|
||||||||||||
this.stream.updateWriteResult(&this.write_result.?[1], &this.write_result.?[0]); | ||||||||||||
this_value.ensureStillAlive(); | ||||||||||||
|
||||||||||||
_ = this.write_callback.get().?.call(globalThis, this.this_value.get().?, &.{}) catch |err| globalThis.reportActiveExceptionAsUnhandled(err); | ||||||||||||
const write_callback: JSC.JSValue = T.writeCallbackGetCached(this_value).?; | ||||||||||||
_ = write_callback.call(globalThis, this_value, &.{}) catch |err| globalThis.reportActiveExceptionAsUnhandled(err); | ||||||||||||
|
||||||||||||
if (this.pending_close) _ = this._close(); | ||||||||||||
} | ||||||||||||
|
@@ -192,11 +207,10 @@ pub fn CompressionStream(comptime T: type) type { | |||||||||||
|
||||||||||||
this.stream.setBuffers(in, out); | ||||||||||||
this.stream.setFlush(@intCast(flush)); | ||||||||||||
|
||||||||||||
// | ||||||||||||
const this_value = callframe.this(); | ||||||||||||
|
||||||||||||
this.stream.doWork(); | ||||||||||||
if (try this.checkError(globalThis)) { | ||||||||||||
if (try this.checkError(globalThis, this_value)) { | ||||||||||||
this.stream.updateWriteResult(&this.write_result.?[1], &this.write_result.?[0]); | ||||||||||||
this.write_in_progress = false; | ||||||||||||
} | ||||||||||||
|
@@ -206,11 +220,9 @@ pub fn CompressionStream(comptime T: type) type { | |||||||||||
} | ||||||||||||
|
||||||||||||
pub fn reset(this: *T, globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { | ||||||||||||
_ = callframe; | ||||||||||||
|
||||||||||||
const err = this.stream.reset(); | ||||||||||||
if (err.isError()) { | ||||||||||||
try this.emitError(globalThis, err); | ||||||||||||
try this.emitError(globalThis, callframe.this(), err); | ||||||||||||
} | ||||||||||||
return .undefined; | ||||||||||||
} | ||||||||||||
|
@@ -233,34 +245,34 @@ pub fn CompressionStream(comptime T: type) type { | |||||||||||
this.stream.close(); | ||||||||||||
} | ||||||||||||
|
||||||||||||
pub fn setOnError(this: *T, globalThis: *JSC.JSGlobalObject, value: JSC.JSValue) bool { | ||||||||||||
pub fn setOnError(_: *T, this_value: JSC.JSValue, globalObject: *JSC.JSGlobalObject, value: JSC.JSValue) bool { | ||||||||||||
if (value.isFunction()) { | ||||||||||||
this.onerror_value.set(globalThis, value); | ||||||||||||
T.errorCallbackSetCached(this_value, globalObject, value); | ||||||||||||
} | ||||||||||||
return true; | ||||||||||||
} | ||||||||||||
|
||||||||||||
pub fn getOnError(this: *T, globalThis: *JSC.JSGlobalObject) JSC.JSValue { | ||||||||||||
_ = globalThis; | ||||||||||||
return this.onerror_value.get() orelse .undefined; | ||||||||||||
pub fn getOnError(_: *T, this_value: JSC.JSValue, _: *JSC.JSGlobalObject) JSC.JSValue { | ||||||||||||
return T.errorCallbackGetCached(this_value) orelse .undefined; | ||||||||||||
} | ||||||||||||
|
||||||||||||
/// returns true if no error was detected/emitted | ||||||||||||
fn checkError(this: *T, globalThis: *JSC.JSGlobalObject) !bool { | ||||||||||||
fn checkError(this: *T, globalThis: *JSC.JSGlobalObject, this_value: JSC.JSValue) !bool { | ||||||||||||
const err = this.stream.getErrorInfo(); | ||||||||||||
if (!err.isError()) return true; | ||||||||||||
try this.emitError(globalThis, err); | ||||||||||||
try this.emitError(globalThis, this_value, err); | ||||||||||||
return false; | ||||||||||||
} | ||||||||||||
|
||||||||||||
fn emitError(this: *T, globalThis: *JSC.JSGlobalObject, err_: Error) !void { | ||||||||||||
fn emitError(this: *T, globalThis: *JSC.JSGlobalObject, this_value: JSC.JSValue, err_: Error) !void { | ||||||||||||
var msg_str = bun.String.createFormat("{s}", .{std.mem.sliceTo(err_.msg, 0) orelse ""}) catch bun.outOfMemory(); | ||||||||||||
const msg_value = msg_str.transferToJS(globalThis); | ||||||||||||
const err_value = JSC.jsNumber(err_.err); | ||||||||||||
var code_str = bun.String.createFormat("{s}", .{std.mem.sliceTo(err_.code, 0) orelse ""}) catch bun.outOfMemory(); | ||||||||||||
const code_value = code_str.transferToJS(globalThis); | ||||||||||||
|
||||||||||||
_ = try this.onerror_value.get().?.call(globalThis, this.this_value.get().?, &.{ msg_value, err_value, code_value }); | ||||||||||||
const callback: JSC.JSValue = T.errorCallbackGetCached(this_value) orelse Output.panic("Assertion failure: cachedErrorCallback is null in node:zlib binding", .{}); | ||||||||||||
_ = try callback.call(globalThis, this_value, &.{ msg_value, err_value, code_value }); | ||||||||||||
|
||||||||||||
this.write_in_progress = false; | ||||||||||||
if (this.pending_close) _ = this._close(); | ||||||||||||
|
@@ -307,8 +319,6 @@ pub const SNativeZlib = struct { | |||||||||||
globalThis: *JSC.JSGlobalObject, | ||||||||||||
stream: ZlibContext = .{}, | ||||||||||||
write_result: ?[*]u32 = null, | ||||||||||||
write_callback: JSC.Strong = .{}, | ||||||||||||
onerror_value: JSC.Strong = .{}, | ||||||||||||
poll_ref: CountedKeepAlive = .{}, | ||||||||||||
this_value: JSC.Strong = .{}, | ||||||||||||
write_in_progress: bool = false, | ||||||||||||
|
@@ -341,11 +351,10 @@ pub const SNativeZlib = struct { | |||||||||||
} | ||||||||||||
|
||||||||||||
//// adding this didnt help much but leaving it here to compare the number with later | ||||||||||||
// pub fn estimatedSize(this: *const SNativeZlib) usize { | ||||||||||||
// _ = this; | ||||||||||||
// const internal_state_size = 3309; // @sizeOf(@cImport(@cInclude("deflate.h")).internal_state) @ cloudflare/zlib @ 92530568d2c128b4432467b76a3b54d93d6350bd | ||||||||||||
// return @sizeOf(SNativeZlib) + internal_state_size; | ||||||||||||
// } | ||||||||||||
pub fn estimatedSize(_: *const SNativeZlib) usize { | ||||||||||||
const internal_state_size = 3309; // @sizeOf(@cImport(@cInclude("deflate.h")).internal_state) @ cloudflare/zlib @ 92530568d2c128b4432467b76a3b54d93d6350bd | ||||||||||||
return @sizeOf(SNativeZlib) + internal_state_size; | ||||||||||||
} | ||||||||||||
|
||||||||||||
pub fn init(this: *SNativeZlib, globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { | ||||||||||||
const arguments = callframe.argumentsUndef(7).slice(); | ||||||||||||
|
@@ -364,7 +373,7 @@ pub const SNativeZlib = struct { | |||||||||||
const dictionary = if (arguments[6].isUndefined()) null else arguments[6].asArrayBuffer(globalThis).?.byteSlice(); | ||||||||||||
|
||||||||||||
this.write_result = writeResult; | ||||||||||||
this.write_callback.set(globalThis, writeCallback); | ||||||||||||
SNativeZlib.writeCallbackSetCached(callframe.this(), globalThis, writeCallback); | ||||||||||||
|
||||||||||||
this.stream.init(level, windowBits, memLevel, strategy, dictionary); | ||||||||||||
|
||||||||||||
|
@@ -383,15 +392,15 @@ pub const SNativeZlib = struct { | |||||||||||
|
||||||||||||
const err = this.stream.setParams(level, strategy); | ||||||||||||
if (err.isError()) { | ||||||||||||
try this.emitError(globalThis, err); | ||||||||||||
try this.emitError(globalThis, callframe.this(), err); | ||||||||||||
} | ||||||||||||
return .undefined; | ||||||||||||
} | ||||||||||||
|
||||||||||||
pub fn deinit(this: *@This()) void { | ||||||||||||
this.write_callback.deinit(); | ||||||||||||
this.onerror_value.deinit(); | ||||||||||||
this.this_value.deinit(); | ||||||||||||
this.poll_ref.deinit(); | ||||||||||||
this.stream.close(); | ||||||||||||
this.destroy(); | ||||||||||||
} | ||||||||||||
}; | ||||||||||||
|
@@ -662,16 +671,14 @@ pub const NativeBrotli = JSC.Codegen.JSNativeBrotli.getConstructor; | |||||||||||
|
||||||||||||
pub const SNativeBrotli = struct { | ||||||||||||
pub usingnamespace bun.NewRefCounted(@This(), deinit); | ||||||||||||
pub usingnamespace JSC.Codegen.JSNativeZlib; | ||||||||||||
pub usingnamespace JSC.Codegen.JSNativeBrotli; | ||||||||||||
pub usingnamespace CompressionStream(@This()); | ||||||||||||
|
||||||||||||
ref_count: u32 = 1, | ||||||||||||
mode: bun.zlib.NodeMode, | ||||||||||||
globalThis: *JSC.JSGlobalObject, | ||||||||||||
stream: BrotliContext = .{}, | ||||||||||||
write_result: ?[*]u32 = null, | ||||||||||||
write_callback: JSC.Strong = .{}, | ||||||||||||
onerror_value: JSC.Strong = .{}, | ||||||||||||
poll_ref: CountedKeepAlive = .{}, | ||||||||||||
this_value: JSC.Strong = .{}, | ||||||||||||
write_in_progress: bool = false, | ||||||||||||
|
@@ -718,19 +725,22 @@ pub const SNativeBrotli = struct { | |||||||||||
|
||||||||||||
pub fn init(this: *@This(), globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue { | ||||||||||||
const arguments = callframe.argumentsUndef(3).slice(); | ||||||||||||
const this_value = callframe.this(); | ||||||||||||
if (arguments.len != 3) { | ||||||||||||
return globalThis.ERR_MISSING_ARGS("init(params, writeResult, writeCallback)", .{}).throw(); | ||||||||||||
} | ||||||||||||
|
||||||||||||
// this does not get gc'd because it is stored in the JS object's `this._writeState`. and the JS object is tied to the native handle as `_handle[owner_symbol]`. | ||||||||||||
const writeResult = arguments[1].asArrayBuffer(globalThis).?.asU32().ptr; | ||||||||||||
const writeCallback = try validators.validateFunction(globalThis, arguments[2], "writeCallback", .{}); | ||||||||||||
|
||||||||||||
this.write_result = writeResult; | ||||||||||||
this.write_callback.set(globalThis, writeCallback); | ||||||||||||
|
||||||||||||
SNativeBrotli.writeCallbackSetCached(this_value, globalThis, writeCallback); | ||||||||||||
|
||||||||||||
var err = this.stream.init(); | ||||||||||||
if (err.isError()) { | ||||||||||||
try this.emitError(globalThis, err); | ||||||||||||
try this.emitError(globalThis, this_value, err); | ||||||||||||
return JSC.jsBoolean(false); | ||||||||||||
} | ||||||||||||
|
||||||||||||
|
@@ -759,9 +769,12 @@ pub const SNativeBrotli = struct { | |||||||||||
} | ||||||||||||
|
||||||||||||
pub fn deinit(this: *@This()) void { | ||||||||||||
this.write_callback.deinit(); | ||||||||||||
this.onerror_value.deinit(); | ||||||||||||
this.this_value.deinit(); | ||||||||||||
this.poll_ref.deinit(); | ||||||||||||
switch (this.stream.mode) { | ||||||||||||
.BROTLI_ENCODE, .BROTLI_DECODE => this.stream.close(), | ||||||||||||
else => {}, | ||||||||||||
} | ||||||||||||
Comment on lines
+774
to
+777
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
this.destroy(); | ||||||||||||
} | ||||||||||||
}; | ||||||||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
! nice find