Files
browser/src/browser/webapi/net/Response.zig
Karl Seguin 62aa564df1 Remove Global v8::Local<V8::Context>
When we create a js.Context, we create the underlying v8.Context and store it
for the duration of the page lifetime. This works because we have a global
HandleScope - the v8.Context (which is really a v8::Local<v8::Context>) is that
to the global HandleScope, effectively making it a global.

If we want to remove our global HandleScope, then we can no longer pin the
v8.Context in our js.Context. Our js.Context now only holds a v8.Global of the
v8.Context (v8::Global<v8::Context).

This PR introduces a new type, js.Local, which takes over a lot of the
functionality previously found in either js.Caller or js.Context. The simplest
way to think about it is:

1 - For v8 -> zig calls, we create a js.Caller (as always)
2 - For zig -> v8 calls, we go through the js.Context (as always)
3 - The shared functionality, which works on a v8.Context, now belongs to js.Local

For #1 (v8 -> zig), creating a js.Local for a js.Caller is really simple and
centralized. v8 largely gives us everything we need from the
FunctionCallbackInfo or PropertyCallbackInfo.  For #2, it's messier, because we
can only create a local v8::Context if we have a HandleScope, which we may or
may not.

Unfortunately, in many cases, what to do becomes the responsibility of the caller
and much of the code has to become aware of this local-ness. What does it means
for our code? The impact is on WebAPIs that store .Global. Because the global
can't do anything. You always need to convert that .Global to a local
(e.g. js.Function.Global -> js.Function).

If you're 100% sure the WebAPI is only being invoked by a v8 callback, you can
use `page.js.local.?.toLocal(some_global).call(...)` to get the local value.

If you're 100% sure the WebAPI is only being invoked by Zig, you need to create
 `js.Local.Scope` to get access to a local:

```zig
var ls: js.Local.Scope = undefined;
page.js.localScope(&ls);
defer ls.deinit();
ls.toLocal(some_global).call(...)
// can also access `&ls.local` for APIs that require a *const js.Local
```
For functions that can be invoked by either V8 or Zig, you should generally push
the responsibility to the caller by accepting a `local: *const js.Local`. If the
caller is a v8 callback, it can pass `page.js.local.?`. If the caller is a Zig
callback, it can create a `Local.Scope`.

As an alternative, it is possible to simply pass the *Page, and check
`if page.js.local == null` and, if so, create a Local.Scope. But this should only
be done for performance reasons. We currently only do this in 1 place, and it's
because the Zig caller doesn't know whether a Local will actually be needed and
it's potentially called on every element creating from the parser.
2026-01-19 07:28:33 +08:00

156 lines
4.8 KiB
Zig

// Copyright (C) 2023-2025 Lightpanda (Selecy SAS)
//
// Francis Bouvier <francis@lightpanda.io>
// Pierre Tachoire <pierre@lightpanda.io>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
const std = @import("std");
const js = @import("../../js/js.zig");
const Page = @import("../../Page.zig");
const Headers = @import("Headers.zig");
const ReadableStream = @import("../streams/ReadableStream.zig");
const Allocator = std.mem.Allocator;
const Response = @This();
pub const Type = enum {
basic,
cors,
@"error",
@"opaque",
opaqueredirect,
};
_status: u16,
_arena: Allocator,
_headers: *Headers,
_body: ?[]const u8,
_type: Type,
_status_text: []const u8,
_url: [:0]const u8,
_is_redirected: bool,
const InitOpts = struct {
status: u16 = 200,
headers: ?Headers.InitOpts = null,
statusText: ?[]const u8 = null,
};
pub fn init(body_: ?[]const u8, opts_: ?InitOpts, page: *Page) !*Response {
const opts = opts_ orelse InitOpts{};
// Store empty string as empty string, not null
const body = if (body_) |b| try page.arena.dupe(u8, b) else null;
const status_text = if (opts.statusText) |st| try page.dupeString(st) else "";
return page._factory.create(Response{
._arena = page.arena,
._status = opts.status,
._status_text = status_text,
._url = "",
._body = body,
._type = .basic,
._is_redirected = false,
._headers = try Headers.init(opts.headers, page),
});
}
pub fn getStatus(self: *const Response) u16 {
return self._status;
}
pub fn getStatusText(self: *const Response) []const u8 {
// @TODO
// This property is meant to actually capture the response status text, not
// just return the text representation of self._status. If we do,
// new Response(null, {status: 200}).statusText, we should get empty string.
return self._status_text;
}
pub fn getURL(self: *const Response) []const u8 {
return self._url;
}
pub fn isRedirected(self: *const Response) bool {
return self._is_redirected;
}
pub fn getHeaders(self: *const Response) *Headers {
return self._headers;
}
pub fn getType(self: *const Response) []const u8 {
return @tagName(self._type);
}
pub fn getBody(self: *const Response, page: *Page) !?*ReadableStream {
const body = self._body orelse return null;
// Empty string should create a closed stream with no data
if (body.len == 0) {
const stream = try ReadableStream.init(null, null, page);
try stream._controller.close();
return stream;
}
return ReadableStream.initWithData(body, page);
}
pub fn isOK(self: *const Response) bool {
return self._status >= 200 and self._status <= 299;
}
pub fn getText(self: *const Response, page: *Page) !js.Promise {
const body = self._body orelse "";
return page.js.local.?.resolvePromise(body);
}
pub fn getJson(self: *Response, page: *Page) !js.Promise {
const body = self._body orelse "";
const local = page.js.local.?;
const value = local.parseJSON(body) catch |err| {
return local.rejectPromise(.{@errorName(err)});
};
return local.resolvePromise(try value.persist());
}
pub const JsApi = struct {
pub const bridge = js.Bridge(Response);
pub const Meta = struct {
pub const name = "Response";
pub const prototype_chain = bridge.prototypeChain();
pub var class_id: bridge.ClassId = undefined;
};
pub const constructor = bridge.constructor(Response.init, .{});
pub const ok = bridge.accessor(Response.isOK, null, .{});
pub const status = bridge.accessor(Response.getStatus, null, .{});
pub const statusText = bridge.accessor(Response.getStatusText, null, .{});
pub const @"type" = bridge.accessor(Response.getType, null, .{});
pub const text = bridge.function(Response.getText, .{});
pub const json = bridge.function(Response.getJson, .{});
pub const headers = bridge.accessor(Response.getHeaders, null, .{});
pub const body = bridge.accessor(Response.getBody, null, .{});
pub const url = bridge.accessor(Response.getURL, null, .{});
pub const redirected = bridge.accessor(Response.isRedirected, null, .{});
};
const testing = @import("../../../testing.zig");
test "WebApi: Response" {
try testing.htmlRunner("net/response.html", .{});
}