mirror of
https://github.com/lightpanda-io/browser.git
synced 2026-03-22 04:34:44 +00:00
Move V8 pipe callback helpers into js/ layer
ReadableStream.zig was the only webapi file importing v8 directly. Extract the repeated newFunctionWithData / callback boilerplate into js/Local (newFunctionWithData) and js/Caller (initFromHandle, FunctionCallbackInfo.getData), and update ReadableStream and Context to use them.
This commit is contained in:
@@ -60,6 +60,11 @@ fn initWithContext(self: *Caller, ctx: *Context, v8_context: *const v8.Context)
|
|||||||
ctx.local = &self.local;
|
ctx.local = &self.local;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn initFromHandle(self: *Caller, handle: ?*const v8.FunctionCallbackInfo) void {
|
||||||
|
const isolate = v8.v8__FunctionCallbackInfo__GetIsolate(handle).?;
|
||||||
|
self.init(isolate);
|
||||||
|
}
|
||||||
|
|
||||||
pub fn deinit(self: *Caller) void {
|
pub fn deinit(self: *Caller) void {
|
||||||
const ctx = self.local.ctx;
|
const ctx = self.local.ctx;
|
||||||
const call_depth = ctx.call_depth - 1;
|
const call_depth = ctx.call_depth - 1;
|
||||||
@@ -441,6 +446,11 @@ pub const FunctionCallbackInfo = struct {
|
|||||||
return .{ .local = local, .handle = v8.v8__FunctionCallbackInfo__INDEX(self.handle, @intCast(index)).? };
|
return .{ .local = local, .handle = v8.v8__FunctionCallbackInfo__INDEX(self.handle, @intCast(index)).? };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn getData(self: FunctionCallbackInfo) ?*anyopaque {
|
||||||
|
const data = v8.v8__FunctionCallbackInfo__Data(self.handle) orelse return null;
|
||||||
|
return v8.v8__External__Value(@ptrCast(data));
|
||||||
|
}
|
||||||
|
|
||||||
pub fn getThis(self: FunctionCallbackInfo) *const v8.Object {
|
pub fn getThis(self: FunctionCallbackInfo) *const v8.Object {
|
||||||
return v8.v8__FunctionCallbackInfo__This(self.handle).?;
|
return v8.v8__FunctionCallbackInfo__This(self.handle).?;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -539,15 +539,6 @@ fn postCompileModule(self: *Context, mod: js.Module, url: [:0]const u8, local: *
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn newFunctionWithData(local: *const js.Local, comptime callback: *const fn (?*const v8.FunctionCallbackInfo) callconv(.c) void, data: *anyopaque) js.Function {
|
|
||||||
const external = local.isolate.createExternal(data);
|
|
||||||
const handle = v8.v8__Function__New__DEFAULT2(local.handle, callback, @ptrCast(external)).?;
|
|
||||||
return .{
|
|
||||||
.local = local,
|
|
||||||
.handle = handle,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
// == Callbacks ==
|
// == Callbacks ==
|
||||||
// Callback from V8, asking us to load a module. The "specifier" is
|
// Callback from V8, asking us to load a module. The "specifier" is
|
||||||
// the src of the module to load.
|
// the src of the module to load.
|
||||||
@@ -866,15 +857,14 @@ fn resolveDynamicModule(self: *Context, state: *DynamicModuleResolveState, modul
|
|||||||
// last value of the module. But, for module loading, we need to
|
// last value of the module. But, for module loading, we need to
|
||||||
// resolve to the module's namespace.
|
// resolve to the module's namespace.
|
||||||
|
|
||||||
const then_callback = newFunctionWithData(local, struct {
|
const then_callback = local.newFunctionWithData(struct {
|
||||||
pub fn callback(callback_handle: ?*const v8.FunctionCallbackInfo) callconv(.c) void {
|
pub fn callback(callback_handle: ?*const v8.FunctionCallbackInfo) callconv(.c) void {
|
||||||
const isolate = v8.v8__FunctionCallbackInfo__GetIsolate(callback_handle).?;
|
|
||||||
var c: Caller = undefined;
|
var c: Caller = undefined;
|
||||||
c.init(isolate);
|
c.initFromHandle(callback_handle);
|
||||||
defer c.deinit();
|
defer c.deinit();
|
||||||
|
|
||||||
const info_data = v8.v8__FunctionCallbackInfo__Data(callback_handle).?;
|
const info = Caller.FunctionCallbackInfo{ .handle = callback_handle.? };
|
||||||
const s: *DynamicModuleResolveState = @ptrCast(@alignCast(v8.v8__External__Value(@ptrCast(info_data))));
|
const s: *DynamicModuleResolveState = @ptrCast(@alignCast(info.getData() orelse return));
|
||||||
|
|
||||||
if (s.context_id != c.local.ctx.id) {
|
if (s.context_id != c.local.ctx.id) {
|
||||||
// The microtask is tied to the isolate, not the context
|
// The microtask is tied to the isolate, not the context
|
||||||
@@ -891,19 +881,17 @@ fn resolveDynamicModule(self: *Context, state: *DynamicModuleResolveState, modul
|
|||||||
}
|
}
|
||||||
}.callback, @ptrCast(state));
|
}.callback, @ptrCast(state));
|
||||||
|
|
||||||
const catch_callback = newFunctionWithData(local, struct {
|
const catch_callback = local.newFunctionWithData(struct {
|
||||||
pub fn callback(callback_handle: ?*const v8.FunctionCallbackInfo) callconv(.c) void {
|
pub fn callback(callback_handle: ?*const v8.FunctionCallbackInfo) callconv(.c) void {
|
||||||
const isolate = v8.v8__FunctionCallbackInfo__GetIsolate(callback_handle).?;
|
|
||||||
var c: Caller = undefined;
|
var c: Caller = undefined;
|
||||||
c.init(isolate);
|
c.initFromHandle(callback_handle);
|
||||||
defer c.deinit();
|
defer c.deinit();
|
||||||
|
|
||||||
const info_data = v8.v8__FunctionCallbackInfo__Data(callback_handle).?;
|
const info = Caller.FunctionCallbackInfo{ .handle = callback_handle.? };
|
||||||
const s: *DynamicModuleResolveState = @ptrCast(@alignCast(v8.v8__External__Value(@ptrCast(info_data))));
|
const s: *DynamicModuleResolveState = @ptrCast(@alignCast(info.getData() orelse return));
|
||||||
|
|
||||||
const l = &c.local;
|
const l = &c.local;
|
||||||
const ctx = l.ctx;
|
if (s.context_id != l.ctx.id) {
|
||||||
if (s.context_id != ctx.id) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -82,6 +82,16 @@ pub fn createTypedArray(self: *const Local, comptime array_type: js.ArrayType, s
|
|||||||
return .init(self, size);
|
return .init(self, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn newFunctionWithData(
|
||||||
|
self: *const Local,
|
||||||
|
comptime callback: *const fn (?*const v8.FunctionCallbackInfo) callconv(.c) void,
|
||||||
|
data: *anyopaque,
|
||||||
|
) js.Function {
|
||||||
|
const external = self.isolate.createExternal(data);
|
||||||
|
const handle = v8.v8__Function__New__DEFAULT2(self.handle, callback, @ptrCast(external)).?;
|
||||||
|
return .{ .local = self, .handle = handle };
|
||||||
|
}
|
||||||
|
|
||||||
pub fn runMacrotasks(self: *const Local) void {
|
pub fn runMacrotasks(self: *const Local) void {
|
||||||
const env = self.ctx.env;
|
const env = self.ctx.env;
|
||||||
env.pumpMessageLoop();
|
env.pumpMessageLoop();
|
||||||
|
|||||||
@@ -20,7 +20,6 @@ const std = @import("std");
|
|||||||
const log = @import("../../../log.zig");
|
const log = @import("../../../log.zig");
|
||||||
|
|
||||||
const js = @import("../../js/js.zig");
|
const js = @import("../../js/js.zig");
|
||||||
const v8 = js.v8;
|
|
||||||
const Page = @import("../../Page.zig");
|
const Page = @import("../../Page.zig");
|
||||||
|
|
||||||
const ReadableStreamDefaultReader = @import("ReadableStreamDefaultReader.zig");
|
const ReadableStreamDefaultReader = @import("ReadableStreamDefaultReader.zig");
|
||||||
@@ -310,22 +309,21 @@ const PipeState = struct {
|
|||||||
const read_promise = try state.reader.read(state.page);
|
const read_promise = try state.reader.read(state.page);
|
||||||
|
|
||||||
// Create JS callback functions for .then() and .catch()
|
// Create JS callback functions for .then() and .catch()
|
||||||
const then_fn = newFunctionWithData(local, &onReadFulfilled, state);
|
const then_fn = local.newFunctionWithData(&onReadFulfilled, state);
|
||||||
const catch_fn = newFunctionWithData(local, &onReadRejected, state);
|
const catch_fn = local.newFunctionWithData(&onReadRejected, state);
|
||||||
|
|
||||||
_ = read_promise.thenAndCatch(then_fn, catch_fn) catch {
|
_ = read_promise.thenAndCatch(then_fn, catch_fn) catch {
|
||||||
state.finish(local);
|
state.finish(local);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
fn onReadFulfilled(callback_handle: ?*const v8.FunctionCallbackInfo) callconv(.c) void {
|
fn onReadFulfilled(callback_handle: ?*const js.v8.FunctionCallbackInfo) callconv(.c) void {
|
||||||
const isolate = v8.v8__FunctionCallbackInfo__GetIsolate(callback_handle).?;
|
|
||||||
var c: js.Caller = undefined;
|
var c: js.Caller = undefined;
|
||||||
c.init(isolate);
|
c.initFromHandle(callback_handle);
|
||||||
defer c.deinit();
|
defer c.deinit();
|
||||||
|
|
||||||
const info_data = v8.v8__FunctionCallbackInfo__Data(callback_handle).?;
|
const info = js.Caller.FunctionCallbackInfo{ .handle = callback_handle.? };
|
||||||
const state: *PipeState = @ptrCast(@alignCast(v8.v8__External__Value(@ptrCast(info_data))));
|
const state: *PipeState = @ptrCast(@alignCast(info.getData() orelse return));
|
||||||
|
|
||||||
if (state.context_id != c.local.ctx.id) return;
|
if (state.context_id != c.local.ctx.id) return;
|
||||||
|
|
||||||
@@ -333,10 +331,7 @@ const PipeState = struct {
|
|||||||
defer l.runMicrotasks();
|
defer l.runMicrotasks();
|
||||||
|
|
||||||
// Get the read result argument {done, value}
|
// Get the read result argument {done, value}
|
||||||
const result_val = js.Value{
|
const result_val = info.getArg(0, l);
|
||||||
.local = l,
|
|
||||||
.handle = v8.v8__FunctionCallbackInfo__INDEX(callback_handle, 0) orelse return,
|
|
||||||
};
|
|
||||||
|
|
||||||
if (!result_val.isObject()) {
|
if (!result_val.isObject()) {
|
||||||
state.finish(l);
|
state.finish(l);
|
||||||
@@ -374,14 +369,13 @@ const PipeState = struct {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
fn onReadRejected(callback_handle: ?*const v8.FunctionCallbackInfo) callconv(.c) void {
|
fn onReadRejected(callback_handle: ?*const js.v8.FunctionCallbackInfo) callconv(.c) void {
|
||||||
const isolate = v8.v8__FunctionCallbackInfo__GetIsolate(callback_handle).?;
|
|
||||||
var c: js.Caller = undefined;
|
var c: js.Caller = undefined;
|
||||||
c.init(isolate);
|
c.initFromHandle(callback_handle);
|
||||||
defer c.deinit();
|
defer c.deinit();
|
||||||
|
|
||||||
const info_data = v8.v8__FunctionCallbackInfo__Data(callback_handle).?;
|
const info = js.Caller.FunctionCallbackInfo{ .handle = callback_handle.? };
|
||||||
const state: *PipeState = @ptrCast(@alignCast(v8.v8__External__Value(@ptrCast(info_data))));
|
const state: *PipeState = @ptrCast(@alignCast(info.getData() orelse return));
|
||||||
|
|
||||||
if (state.context_id != c.local.ctx.id) return;
|
if (state.context_id != c.local.ctx.id) return;
|
||||||
|
|
||||||
@@ -404,19 +398,6 @@ const PipeState = struct {
|
|||||||
local.toLocal(r).resolve("pipe finished", {});
|
local.toLocal(r).resolve("pipe finished", {});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn newFunctionWithData(
|
|
||||||
local: *const js.Local,
|
|
||||||
comptime callback: *const fn (?*const v8.FunctionCallbackInfo) callconv(.c) void,
|
|
||||||
data: *anyopaque,
|
|
||||||
) js.Function {
|
|
||||||
const external = local.isolate.createExternal(data);
|
|
||||||
const handle = v8.v8__Function__New__DEFAULT2(local.handle, callback, @ptrCast(external)).?;
|
|
||||||
return .{
|
|
||||||
.local = local,
|
|
||||||
.handle = handle,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const Cancel = struct {
|
const Cancel = struct {
|
||||||
|
|||||||
Reference in New Issue
Block a user