Skip to content

Commit

Permalink
fix(http): Prevent using http client or request once its collected
Browse files Browse the repository at this point in the history
FIXME: it seems http client won't allow being collected if all opened connection or pending request are not resolved.
This means that our http client must keep track of all opened request and try to collect them first
  • Loading branch information
giann committed May 25, 2024
1 parent c2669f4 commit 7c5ebbf
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 22 deletions.
14 changes: 7 additions & 7 deletions src/Parser.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5637,11 +5637,6 @@ fn range(self: *Self, _: bool, low: Ast.Node.Index) Error!Ast.Node.Index {

self.markInitialized();

const list_def = obj.ObjList.ListDef.init(self.gc.allocator, self.gc.type_registry.int_type);
const resolved_type = obj.ObjTypeDef.TypeUnion{
.List = list_def,
};

return try self.ast.appendNode(
.{
.tag = .Range,
Expand All @@ -5651,7 +5646,12 @@ fn range(self: *Self, _: bool, low: Ast.Node.Index) Error!Ast.Node.Index {
.{
.optional = false,
.def_type = .Range,
.resolved_type = resolved_type,
.resolved_type = .{
.List = obj.ObjList.ListDef.init(
self.gc.allocator,
self.gc.type_registry.int_type,
),
},
},
),
.components = .{
Expand Down Expand Up @@ -6725,7 +6725,7 @@ fn enumDeclaration(self: *Self) Error!Ast.Node.Index {
// Generate the enum constant
var @"enum" = try self.gc.allocateObject(
obj.ObjEnum,
obj.ObjEnum.init(self.gc.allocator, enum_type),
obj.ObjEnum.init(enum_type),
);

var obj_cases = std.ArrayList(Value).init(self.gc.allocator);
Expand Down
1 change: 0 additions & 1 deletion src/lib/buffer.buzz
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
namespace buffer;

import "ffi";
import "std";

export object WriteWhileReadingError {}
export object OutOfBoundError {}
Expand Down
1 change: 0 additions & 1 deletion src/lib/buzz_http.zig
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@ pub export fn HttpRequestDeinit(ctx: *api.NativeCtx) c_int {
),
);

api.VM.allocator.free(request.response.parser.header_bytes_buffer);
request.deinit();
api.VM.allocator.destroy(request);

Expand Down
21 changes: 18 additions & 3 deletions src/lib/http.buzz
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
namespace http;

import "std";
import "buffer";
import "errors";

Expand Down Expand Up @@ -46,6 +45,8 @@ export enum HttpError {
UnsupportedTransferEncoding,
UnsupportedUriScheme,
UriMissingHost,
ClientCollected,
RequestCollected,
}

| https://datatracker.ietf.org/doc/html/rfc2616
Expand Down Expand Up @@ -174,6 +175,10 @@ export object Client {
}

fun start(Request request) > Request !> HttpError {
if (this.collected) {
throw HttpError.ClientCollected;
}

var requestPtr = HttpClientSend(
client: this.client,
method: request.method,
Expand All @@ -188,6 +193,8 @@ export object Client {

fun collect() > void {
if (!this.collected) {
this.collected = true;

HttpClientDeinit(this.client);
}
}
Expand All @@ -203,14 +210,22 @@ export object Request {
bool collected = false,

fun wait() > Response !> HttpError, errors.InvalidArgumentError, HttpParseError {
if (this.collected) {
return error.RequestCollected;
}

if (this.response != null) {
throw errors.InvalidArgumentError{};
}

if (this.request -> request) {
HttpRequestWait(request);

return HttpRequestRead(request);
const response = HttpRequestRead(request);

this.response = response;

return response;
}

throw errors.InvalidArgumentError{};
Expand Down Expand Up @@ -269,4 +284,4 @@ export object Response {

return "HTTP/1.1 500 Internal Server Error";
}
}
}
4 changes: 2 additions & 2 deletions src/memory.zig
Original file line number Diff line number Diff line change
Expand Up @@ -708,8 +708,8 @@ pub const GarbageCollector = struct {
free(self, ObjMap, obj_map);
},
.Enum => {
var obj_enum = ObjEnum.cast(obj).?;
obj_enum.deinit();
const obj_enum = ObjEnum.cast(obj).?;
self.allocator.free(obj_enum.cases);

free(self, ObjEnum, obj_enum);
},
Expand Down
8 changes: 1 addition & 7 deletions src/obj.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3358,11 +3358,9 @@ pub const ObjEnum = struct {

name: *ObjString,
cases: []Value,
allocator: Allocator,

pub fn init(allocator: Allocator, def: *ObjTypeDef) Self {
pub fn init(def: *ObjTypeDef) Self {
return Self{
.allocator = allocator,
.type_def = def,
.name = def.resolved_type.?.Enum.name,
.cases = undefined,
Expand Down Expand Up @@ -3397,10 +3395,6 @@ pub const ObjEnum = struct {
}
}

pub fn deinit(self: *Self) void {
self.allocator.free(self.cases);
}

pub inline fn toObj(self: *Self) *Obj {
return &self.obj;
}
Expand Down
1 change: 1 addition & 0 deletions src/value.zig
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pub const Value = packed struct {
pub const TrueBitMask: u64 = 1;
pub const TrueMask: u64 = BooleanMask | TrueBitMask;

// FIXME: Their's room to have a bigger integer. How would MIR handle it?
pub const IntegerMask: u64 = TaggedValueMask | (@as(u64, TagInteger) << 32);
pub const NullMask: u64 = TaggedValueMask | (@as(u64, TagNull) << 32);
pub const VoidMask: u64 = TaggedValueMask | (@as(u64, TagVoid) << 32);
Expand Down
1 change: 0 additions & 1 deletion tests/manual/006-http-client.buzz
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import "std";
import "http";
import "debug";
import "errors";
Expand Down

0 comments on commit 7c5ebbf

Please sign in to comment.