mirror of
https://github.com/oven-sh/bun
synced 2026-02-10 19:08:50 +00:00
ok
Former-commit-id: e7d0ff27193defd870ae29bd9b4d4c2044488eac
This commit is contained in:
@@ -88,6 +88,8 @@ pub const ZigString = extern struct {
|
||||
};
|
||||
};
|
||||
|
||||
pub const ReturnableException = *?*Exception;
|
||||
|
||||
pub const JSCell = extern struct {
|
||||
pub const shim = Shimmer("JSC", "JSCell", @This());
|
||||
bytes: shim.Bytes,
|
||||
@@ -548,29 +550,29 @@ pub const JSFunction = extern struct {
|
||||
pub const name = "JSC::JSFunction";
|
||||
pub const namespace = "JSC";
|
||||
|
||||
pub const NativeFunctionCallback = fn (ctx: ?*c_void, global: *JSGlobalObject, call_frame: *CallFrame) callconv(.C) JSValue;
|
||||
pub const NativeFunctionCallback = fn (ctx: ?*c_void, global: [*c]JSGlobalObject, call_frame: [*c]CallFrame) callconv(.C) JSValue;
|
||||
|
||||
pub fn createFromSourceCode(
|
||||
global: *JSGlobalObject,
|
||||
function_name: ?[*]const u8,
|
||||
function_name_len: u16,
|
||||
args: ?[*]JSValue,
|
||||
args_len: u16,
|
||||
source: *const SourceCode,
|
||||
origin: *SourceOrigin,
|
||||
exception: *?*JSObject,
|
||||
) *JSFunction {
|
||||
return cppFn("createFromSourceCode", .{
|
||||
global,
|
||||
function_name,
|
||||
function_name_len,
|
||||
args,
|
||||
args_len,
|
||||
source,
|
||||
origin,
|
||||
exception,
|
||||
});
|
||||
}
|
||||
// pub fn createFromSourceCode(
|
||||
// global: *JSGlobalObject,
|
||||
// function_name: ?[*]const u8,
|
||||
// function_name_len: u16,
|
||||
// args: ?[*]JSValue,
|
||||
// args_len: u16,
|
||||
// source: *const SourceCode,
|
||||
// origin: *SourceOrigin,
|
||||
// exception: *?*JSObject,
|
||||
// ) *JSFunction {
|
||||
// return cppFn("createFromSourceCode", .{
|
||||
// global,
|
||||
// function_name,
|
||||
// function_name_len,
|
||||
// args,
|
||||
// args_len,
|
||||
// source,
|
||||
// origin,
|
||||
// exception,
|
||||
// });
|
||||
// }
|
||||
pub fn createFromNative(
|
||||
global: *JSGlobalObject,
|
||||
argument_count: u16,
|
||||
@@ -589,9 +591,9 @@ pub const JSFunction = extern struct {
|
||||
pub fn calculatedDisplayName(this: *JSFunction, vm: *VM) String {
|
||||
return cppFn("calculatedDisplayName", .{ this, vm });
|
||||
}
|
||||
pub fn toString(this: *JSFunction, globalThis: *JSGlobalObject) *const JSString {
|
||||
return cppFn("toString", .{ this, globalThis });
|
||||
}
|
||||
// pub fn toString(this: *JSFunction, globalThis: *JSGlobalObject) *const JSString {
|
||||
// return cppFn("toString", .{ this, globalThis });
|
||||
// }
|
||||
|
||||
pub fn callWithArgumentsAndThis(
|
||||
function: JSValue,
|
||||
@@ -599,13 +601,13 @@ pub const JSFunction = extern struct {
|
||||
globalThis: *JSGlobalObject,
|
||||
arguments_ptr: [*]JSValue,
|
||||
arguments_len: usize,
|
||||
exception: *?*Exception,
|
||||
error_message: *const c_char,
|
||||
exception: ReturnableException,
|
||||
error_message: [*c]const u8,
|
||||
) JSValue {
|
||||
return cppFn("callWithArgumentsAndThis", .{
|
||||
function,
|
||||
globalThis,
|
||||
thisValue,
|
||||
globalThis,
|
||||
arguments_ptr,
|
||||
arguments_len,
|
||||
exception,
|
||||
@@ -618,20 +620,27 @@ pub const JSFunction = extern struct {
|
||||
globalThis: *JSGlobalObject,
|
||||
arguments_ptr: [*]JSValue,
|
||||
arguments_len: usize,
|
||||
exception: *?*Exception,
|
||||
error_message: *const c_char,
|
||||
exception: ReturnableException,
|
||||
error_message: [*c]const u8,
|
||||
) JSValue {
|
||||
return cppFn("callWithArguments", .{ function, globalThis, arguments_ptr, arguments_len, exception, exception, error_message });
|
||||
return cppFn("callWithArguments", .{
|
||||
function,
|
||||
globalThis,
|
||||
arguments_ptr,
|
||||
arguments_len,
|
||||
exception,
|
||||
error_message,
|
||||
});
|
||||
}
|
||||
|
||||
pub fn callWithThis(
|
||||
function: JSValue,
|
||||
globalThis: *JSGlobalObject,
|
||||
thisValue: JSValue,
|
||||
exception: *?*Exception,
|
||||
error_message: *const c_char,
|
||||
exception: ReturnableException,
|
||||
error_message: [*c]const u8,
|
||||
) JSValue {
|
||||
return cppFn("callWithArguments", .{
|
||||
return cppFn("callWithThis", .{
|
||||
function,
|
||||
globalThis,
|
||||
thisValue,
|
||||
@@ -643,10 +652,10 @@ pub const JSFunction = extern struct {
|
||||
pub fn callWithoutAnyArgumentsOrThis(
|
||||
function: JSValue,
|
||||
globalThis: *JSGlobalObject,
|
||||
exception: *?*Exception,
|
||||
error_message: *const c_char,
|
||||
exception: ReturnableException,
|
||||
error_message: [*c]const u8,
|
||||
) JSValue {
|
||||
return cppFn("callWithoutAnyArgumentsOrThis", .{ function, globalThis, exception, exception, error_message });
|
||||
return cppFn("callWithoutAnyArgumentsOrThis", .{ function, globalThis, exception, error_message });
|
||||
}
|
||||
|
||||
pub fn constructWithArgumentsAndNewTarget(
|
||||
@@ -655,13 +664,13 @@ pub const JSFunction = extern struct {
|
||||
globalThis: *JSGlobalObject,
|
||||
arguments_ptr: [*]JSValue,
|
||||
arguments_len: usize,
|
||||
exception: *?*Exception,
|
||||
error_message: *const c_char,
|
||||
exception: ReturnableException,
|
||||
error_message: [*c]const u8,
|
||||
) JSValue {
|
||||
return cppFn("constructWithArgumentsAndNewTarget", .{
|
||||
function,
|
||||
globalThis,
|
||||
newTarget,
|
||||
globalThis,
|
||||
arguments_ptr,
|
||||
arguments_len,
|
||||
exception,
|
||||
@@ -674,20 +683,27 @@ pub const JSFunction = extern struct {
|
||||
globalThis: *JSGlobalObject,
|
||||
arguments_ptr: [*]JSValue,
|
||||
arguments_len: usize,
|
||||
exception: *?*Exception,
|
||||
error_message: *const c_char,
|
||||
exception: ReturnableException,
|
||||
error_message: [*c]const u8,
|
||||
) JSValue {
|
||||
return cppFn("constructWithArguments", .{ function, globalThis, arguments_ptr, arguments_len, exception, exception, error_message });
|
||||
return cppFn("constructWithArguments", .{
|
||||
function,
|
||||
globalThis,
|
||||
arguments_ptr,
|
||||
arguments_len,
|
||||
exception,
|
||||
error_message,
|
||||
});
|
||||
}
|
||||
|
||||
pub fn constructWithNewTarget(
|
||||
function: JSValue,
|
||||
globalThis: *JSGlobalObject,
|
||||
newTarget: JSValue,
|
||||
exception: *?*Exception,
|
||||
error_message: *const c_char,
|
||||
exception: ReturnableException,
|
||||
error_message: [*c]const u8,
|
||||
) JSValue {
|
||||
return cppFn("constructWithArguments", .{
|
||||
return cppFn("constructWithNewTarget", .{
|
||||
function,
|
||||
globalThis,
|
||||
newTarget,
|
||||
@@ -699,15 +715,20 @@ pub const JSFunction = extern struct {
|
||||
pub fn constructWithoutAnyArgumentsOrNewTarget(
|
||||
function: JSValue,
|
||||
globalThis: *JSGlobalObject,
|
||||
exception: *?*Exception,
|
||||
error_message: *const c_char,
|
||||
exception: ReturnableException,
|
||||
error_message: [*c]const u8,
|
||||
) JSValue {
|
||||
return cppFn("constructWithoutAnyArgumentsOrNewTarget", .{ function, globalThis, exception, exception, error_message });
|
||||
return cppFn("constructWithoutAnyArgumentsOrNewTarget", .{
|
||||
function,
|
||||
globalThis,
|
||||
exception,
|
||||
error_message,
|
||||
});
|
||||
}
|
||||
|
||||
pub const Extern = [_][]const u8{
|
||||
"fromString",
|
||||
"createFromSourceCode",
|
||||
// "createFromSourceCode",
|
||||
"createFromNative",
|
||||
"getName",
|
||||
"displayName",
|
||||
@@ -1330,11 +1351,19 @@ pub const JSValue = enum(i64) {
|
||||
return @intToPtr(C_API.JSValueRef, @intCast(usize, @enumToInt(this)));
|
||||
}
|
||||
|
||||
pub inline fn fromRef(this: C_API.JSValueRef) JSValue {
|
||||
return @intToEnum(JSValue, @intCast(i64, @ptrToInt(this)));
|
||||
}
|
||||
|
||||
pub inline fn asObjectRef(this: JSValue) C_API.JSObjectRef {
|
||||
return @ptrCast(C_API.JSObjectRef, this.asVoid());
|
||||
}
|
||||
|
||||
pub inline fn asVoid(this: JSValue) *c_void {
|
||||
return @intToPtr(*c_void, @intCast(usize, @enumToInt(this)));
|
||||
}
|
||||
|
||||
pub const Extern = [_][]const u8{ "isClass", "getNameProperty", "getClassName", "getErrorsProperty", "toInt32", "toBoolean", "isInt32", "isIterable", "forEach", "isAggregateError", "toZigException", "isException", "toWTFString", "hasProperty", "getPropertyNames", "getDirect", "putDirect", "get", "getIfExists", "asString", "asObject", "asNumber", "isError", "jsNull", "jsUndefined", "jsTDZValue", "jsBoolean", "jsDoubleNumber", "jsNumberFromDouble", "jsNumberFromChar", "jsNumberFromU16", "jsNumberFromInt32", "jsNumberFromInt64", "jsNumberFromUint64", "isUndefined", "isNull", "isUndefinedOrNull", "isBoolean", "isAnyInt", "isUInt32AsAnyInt", "isInt32AsAnyInt", "isNumber", "isString", "isBigInt", "isHeapBigInt", "isBigInt32", "isSymbol", "isPrimitive", "isGetterSetter", "isCustomGetterSetter", "isObject", "isCell", "asCell", "toString", "toStringOrNull", "toPropertyKey", "toPropertyKeyValue", "toObject", "toString", "getPrototype", "getPropertyByPropertyName", "eqlValue", "eqlCell", "isCallable" };
|
||||
pub const Extern = [_][]const u8{ "asPromise", "isClass", "getNameProperty", "getClassName", "getErrorsProperty", "toInt32", "toBoolean", "isInt32", "isIterable", "forEach", "isAggregateError", "toZigException", "isException", "toWTFString", "hasProperty", "getPropertyNames", "getDirect", "putDirect", "get", "getIfExists", "asString", "asObject", "asNumber", "isError", "jsNull", "jsUndefined", "jsTDZValue", "jsBoolean", "jsDoubleNumber", "jsNumberFromDouble", "jsNumberFromChar", "jsNumberFromU16", "jsNumberFromInt32", "jsNumberFromInt64", "jsNumberFromUint64", "isUndefined", "isNull", "isUndefinedOrNull", "isBoolean", "isAnyInt", "isUInt32AsAnyInt", "isInt32AsAnyInt", "isNumber", "isString", "isBigInt", "isHeapBigInt", "isBigInt32", "isSymbol", "isPrimitive", "isGetterSetter", "isCustomGetterSetter", "isObject", "isCell", "asCell", "toString", "toStringOrNull", "toPropertyKey", "toPropertyKeyValue", "toObject", "toString", "getPrototype", "getPropertyByPropertyName", "eqlValue", "eqlCell", "isCallable" };
|
||||
};
|
||||
|
||||
pub const PropertyName = extern struct {
|
||||
|
||||
@@ -17,7 +17,7 @@ pub const ZigGlobalObject = extern struct {
|
||||
pub const name = "Zig::GlobalObject";
|
||||
pub const include = "\"ZigGlobalObject.h\"";
|
||||
pub const namespace = shim.namespace;
|
||||
pub const Interface: type = NewGlobalObject(std.meta.globalOption("JavaScriptVirtualMachine", type) orelse struct {});
|
||||
pub const Interface: type = NewGlobalObject(JS.VirtualMachine);
|
||||
|
||||
pub var sigaction: std.os.Sigaction = undefined;
|
||||
pub var sigaction_installed = false;
|
||||
@@ -533,6 +533,8 @@ pub const ZigConsoleClient = struct {
|
||||
};
|
||||
}
|
||||
|
||||
/// TODO: support %s %d %f %o %O
|
||||
/// https://console.spec.whatwg.org/#formatter
|
||||
pub fn messageWithTypeAndLevel(
|
||||
console_: ZigConsoleClient.Type,
|
||||
message_type: u32,
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
//-- AUTOGENERATED FILE -- 1627967644
|
||||
//-- AUTOGENERATED FILE -- 1628024438
|
||||
// clang-format off
|
||||
#pragma once
|
||||
|
||||
|
||||
@@ -50,3 +50,4 @@ pub const ZigString = bindings.ZigString;
|
||||
pub const ZigException = bindings.ZigException;
|
||||
pub const ResolvedSource = bindings.ResolvedSource;
|
||||
pub const ZigStackTrace = bindings.ZigStackTrace;
|
||||
pub const ReturnableException = bindings.ReturnableException;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
//-- AUTOGENERATED FILE -- 1627967644
|
||||
//-- AUTOGENERATED FILE -- 1628024438
|
||||
// clang-format: off
|
||||
#pragma once
|
||||
|
||||
@@ -314,14 +314,14 @@ CPP_DECL void JSC__SourceCode__fromString(JSC__SourceCode* arg0, const WTF__Stri
|
||||
#pragma mark - JSC::JSFunction
|
||||
|
||||
CPP_DECL bWTF__String JSC__JSFunction__calculatedDisplayName(JSC__JSFunction* arg0, JSC__VM* arg1);
|
||||
CPP_DECL JSC__JSValue JSC__JSFunction__callWithArguments(JSC__JSValue JSValue0, JSC__JSGlobalObject* arg1, JSC__JSValue* arg2, size_t arg3, JSC__Exception** arg4, const char* arg5);
|
||||
CPP_DECL JSC__JSValue JSC__JSFunction__callWithArgumentsAndThis(JSC__JSValue JSValue0, JSC__JSValue JSValue1, JSC__JSGlobalObject* arg2, JSC__JSValue* arg3, size_t arg4, JSC__Exception** arg5, const char* arg6);
|
||||
CPP_DECL JSC__JSValue JSC__JSFunction__callWithoutAnyArgumentsOrThis(JSC__JSValue JSValue0, JSC__JSGlobalObject* arg1, JSC__Exception** arg2, const char* arg3);
|
||||
CPP_DECL JSC__JSValue JSC__JSFunction__callWithThis(JSC__JSValue JSValue0, JSC__JSGlobalObject* arg1, JSC__JSValue JSValue2, JSC__Exception** arg3, const char* arg4);
|
||||
CPP_DECL JSC__JSValue JSC__JSFunction__constructWithArguments(JSC__JSValue JSValue0, JSC__JSGlobalObject* arg1, JSC__JSValue* arg2, size_t arg3, JSC__Exception** arg4, const char* arg5);
|
||||
CPP_DECL JSC__JSValue JSC__JSFunction__constructWithArgumentsAndNewTarget(JSC__JSValue JSValue0, JSC__JSValue JSValue1, JSC__JSGlobalObject* arg2, JSC__JSValue* arg3, size_t arg4, JSC__Exception** arg5, const char* arg6);
|
||||
CPP_DECL JSC__JSValue JSC__JSFunction__constructWithNewTarget(JSC__JSValue JSValue0, JSC__JSGlobalObject* arg1, JSC__JSValue JSValue2, JSC__Exception** arg3, const char* arg4);
|
||||
CPP_DECL JSC__JSValue JSC__JSFunction__constructWithoutAnyArgumentsOrNewTarget(JSC__JSValue JSValue0, JSC__JSGlobalObject* arg1, JSC__Exception** arg2, const char* arg3);
|
||||
CPP_DECL JSC__JSValue JSC__JSFunction__callWithArguments(JSC__JSValue JSValue0, JSC__JSGlobalObject* arg1, JSC__JSValue* arg2, size_t arg3, JSC__Exception** arg4, const unsigned char* arg5);
|
||||
CPP_DECL JSC__JSValue JSC__JSFunction__callWithArgumentsAndThis(JSC__JSValue JSValue0, JSC__JSValue JSValue1, JSC__JSGlobalObject* arg2, JSC__JSValue* arg3, size_t arg4, JSC__Exception** arg5, const unsigned char* arg6);
|
||||
CPP_DECL JSC__JSValue JSC__JSFunction__callWithoutAnyArgumentsOrThis(JSC__JSValue JSValue0, JSC__JSGlobalObject* arg1, JSC__Exception** arg2, const unsigned char* arg3);
|
||||
CPP_DECL JSC__JSValue JSC__JSFunction__callWithThis(JSC__JSValue JSValue0, JSC__JSGlobalObject* arg1, JSC__JSValue JSValue2, JSC__Exception** arg3, const unsigned char* arg4);
|
||||
CPP_DECL JSC__JSValue JSC__JSFunction__constructWithArguments(JSC__JSValue JSValue0, JSC__JSGlobalObject* arg1, JSC__JSValue* arg2, size_t arg3, JSC__Exception** arg4, const unsigned char* arg5);
|
||||
CPP_DECL JSC__JSValue JSC__JSFunction__constructWithArgumentsAndNewTarget(JSC__JSValue JSValue0, JSC__JSValue JSValue1, JSC__JSGlobalObject* arg2, JSC__JSValue* arg3, size_t arg4, JSC__Exception** arg5, const unsigned char* arg6);
|
||||
CPP_DECL JSC__JSValue JSC__JSFunction__constructWithNewTarget(JSC__JSValue JSValue0, JSC__JSGlobalObject* arg1, JSC__JSValue JSValue2, JSC__Exception** arg3, const unsigned char* arg4);
|
||||
CPP_DECL JSC__JSValue JSC__JSFunction__constructWithoutAnyArgumentsOrNewTarget(JSC__JSValue JSValue0, JSC__JSGlobalObject* arg1, JSC__Exception** arg2, const unsigned char* arg3);
|
||||
CPP_DECL JSC__JSFunction* JSC__JSFunction__createFromNative(JSC__JSGlobalObject* arg0, uint16_t arg1, const WTF__String* arg2, void* arg3, JSC__JSValue (* ArgFn4)(void* arg0, JSC__JSGlobalObject* arg1, JSC__CallFrame* arg2));
|
||||
CPP_DECL JSC__JSFunction* JSC__JSFunction__createFromSourceCode(JSC__JSGlobalObject* arg0, const unsigned char* arg1, uint16_t arg2, JSC__JSValue* arg3, uint16_t arg4, const JSC__SourceCode* arg5, JSC__SourceOrigin* arg6, JSC__JSObject** arg7);
|
||||
CPP_DECL bWTF__String JSC__JSFunction__displayName(JSC__JSFunction* arg0, JSC__VM* arg1);
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -50,8 +50,8 @@ pub const VirtualMachine = struct {
|
||||
require_cache: RequireCacheType,
|
||||
log: *logger.Log,
|
||||
event_listeners: EventListenerMixin.Map,
|
||||
pub threadlocal var vm_loaded = false;
|
||||
pub threadlocal var vm: *VirtualMachine = undefined;
|
||||
pub var vm_loaded = false;
|
||||
pub var vm: *VirtualMachine = undefined;
|
||||
|
||||
pub fn init(
|
||||
allocator: *std.mem.Allocator,
|
||||
@@ -66,7 +66,7 @@ pub const VirtualMachine = struct {
|
||||
log = try allocator.create(logger.Log);
|
||||
}
|
||||
|
||||
vm = try allocator.create(VirtualMachine);
|
||||
VirtualMachine.vm = try allocator.create(VirtualMachine);
|
||||
var console = try allocator.create(ZigConsoleClient);
|
||||
console.* = ZigConsoleClient.init(Output.errorWriter(), Output.writer());
|
||||
const bundler = try Bundler.init(
|
||||
@@ -75,7 +75,7 @@ pub const VirtualMachine = struct {
|
||||
try configureTransformOptionsForSpeedy(allocator, _args),
|
||||
existing_bundle,
|
||||
);
|
||||
vm.* = VirtualMachine{
|
||||
VirtualMachine.vm.* = VirtualMachine{
|
||||
.global = undefined,
|
||||
.allocator = allocator,
|
||||
.require_cache = RequireCacheType.init(allocator),
|
||||
@@ -86,20 +86,23 @@ pub const VirtualMachine = struct {
|
||||
.log = log,
|
||||
};
|
||||
|
||||
vm.bundler.configureLinker();
|
||||
VirtualMachine.vm.bundler.configureLinker();
|
||||
|
||||
var global_classes: [GlobalClasses.len]js.JSClassRef = undefined;
|
||||
inline for (GlobalClasses) |Class, i| {
|
||||
global_classes[i] = Class.get().*;
|
||||
}
|
||||
vm.global = ZigGlobalObject.create(
|
||||
VirtualMachine.vm.global = ZigGlobalObject.create(
|
||||
&global_classes,
|
||||
@intCast(i32, global_classes.len),
|
||||
vm.console,
|
||||
);
|
||||
vm_loaded = true;
|
||||
VirtualMachine.vm_loaded = true;
|
||||
std.debug.print("VM IS LOADED {}", .{
|
||||
VirtualMachine.vm_loaded,
|
||||
});
|
||||
|
||||
return vm;
|
||||
return VirtualMachine.vm;
|
||||
}
|
||||
|
||||
// dynamic import
|
||||
@@ -110,7 +113,7 @@ pub const VirtualMachine = struct {
|
||||
threadlocal var source_code_printer: js_printer.BufferPrinter = undefined;
|
||||
threadlocal var source_code_printer_loaded: bool = false;
|
||||
|
||||
inline fn _fetch(
|
||||
fn _fetch(
|
||||
global: *JSGlobalObject,
|
||||
specifier: string,
|
||||
source: string,
|
||||
@@ -242,7 +245,7 @@ pub const VirtualMachine = struct {
|
||||
path: string,
|
||||
};
|
||||
|
||||
inline fn _resolve(ret: *ResolveFunctionResult, global: *JSGlobalObject, specifier: string, source: string) !void {
|
||||
fn _resolve(ret: *ResolveFunctionResult, global: *JSGlobalObject, specifier: string, source: string) !void {
|
||||
std.debug.assert(VirtualMachine.vm_loaded);
|
||||
std.debug.assert(VirtualMachine.vm.global == global);
|
||||
if (vm.node_modules == null and strings.eqlComptime(specifier, Runtime.Runtime.Imports.Name)) {
|
||||
@@ -412,7 +415,30 @@ pub const VirtualMachine = struct {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn loadEntryPoint(this: *VirtualMachine, entry_point: string) !void {
|
||||
// TODO:
|
||||
pub fn deinit(this: *VirtualMachine) void {}
|
||||
|
||||
pub fn printException(this: *VirtualMachine, exception: *Exception) void {
|
||||
if (Output.enable_ansi_colors) {
|
||||
this.printErrorlikeObject(exception.value(), exception, true);
|
||||
} else {
|
||||
this.printErrorlikeObject(exception.value(), exception, false);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn defaultErrorHandler(this: *VirtualMachine, result: JSValue) void {
|
||||
if (result.isException(this.global.vm())) {
|
||||
var exception = @ptrCast(*Exception, result.asVoid());
|
||||
|
||||
this.printException(exception);
|
||||
} else if (Output.enable_ansi_colors) {
|
||||
this.printErrorlikeObject(result, null, true);
|
||||
} else {
|
||||
this.printErrorlikeObject(result, null, false);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn loadEntryPoint(this: *VirtualMachine, entry_point: string) !*JSInternalPromise {
|
||||
var path = this.bundler.normalizeEntryPointPath(entry_point);
|
||||
|
||||
var promise = JSModuleLoader.loadAndEvaluateModule(this.global, ZigString.init(path));
|
||||
@@ -423,23 +449,7 @@ pub const VirtualMachine = struct {
|
||||
this.global.vm().drainMicrotasks();
|
||||
}
|
||||
|
||||
if (promise.status(this.global.vm()) == JSPromise.Status.Rejected) {
|
||||
var result = promise.result(this.global.vm());
|
||||
|
||||
if (result.isException(this.global.vm())) {
|
||||
var exception = @ptrCast(*Exception, result.asVoid());
|
||||
|
||||
if (Output.enable_ansi_colors) {
|
||||
this.printErrorlikeObject(exception.value(), exception, true);
|
||||
} else {
|
||||
this.printErrorlikeObject(exception.value(), exception, false);
|
||||
}
|
||||
} else if (Output.enable_ansi_colors) {
|
||||
this.printErrorlikeObject(result, null, true);
|
||||
} else {
|
||||
this.printErrorlikeObject(result, null, false);
|
||||
}
|
||||
}
|
||||
return promise;
|
||||
}
|
||||
|
||||
// When the Error-like object is one of our own, it's best to rely on the object directly instead of serializing it to a ZigException.
|
||||
@@ -797,7 +807,6 @@ pub const EventListenerMixin = struct {
|
||||
.{},
|
||||
);
|
||||
|
||||
var exception: js.JSValueRef = null;
|
||||
// Rely on JS finalizer
|
||||
var fetch_event = try vm.allocator.create(FetchEvent);
|
||||
fetch_event.* = FetchEvent{
|
||||
@@ -805,32 +814,37 @@ pub const EventListenerMixin = struct {
|
||||
.request = Request{ .request_context = request_context },
|
||||
};
|
||||
|
||||
var fetch_args: [1]js.JSObjectRef = undefined;
|
||||
for (listeners.items) |listener| {
|
||||
fetch_args[0] = FetchEvent.Class.make(vm.global, fetch_event);
|
||||
var fetch_args: [1]JSValue = undefined;
|
||||
var exception: ?*Exception = null;
|
||||
const failed_str = "Failed";
|
||||
for (listeners.items) |listener_ref| {
|
||||
var listener = @intToEnum(JSValue, @intCast(i64, @ptrToInt(listener_ref)));
|
||||
|
||||
fetch_args[0] = JSValue.fromRef(FetchEvent.Class.make(vm.global.ref(), fetch_event));
|
||||
|
||||
var promise = JSPromise.resolvedPromise(vm.global, JSFunction.callWithArguments(listener, vm.global, &fetch_args, 1, &exception, failed_str));
|
||||
vm.global.vm().drainMicrotasks();
|
||||
|
||||
if (promise.status(vm.global.vm()) == .Rejected) {
|
||||
if (exception == null) {
|
||||
var res = promise.result(vm.global.vm());
|
||||
if (res.isException(vm.global.vm())) {
|
||||
exception = @ptrCast(*Exception, res.asVoid());
|
||||
}
|
||||
}
|
||||
} else {
|
||||
_ = promise.result(vm.global.vm());
|
||||
}
|
||||
|
||||
vm.global.vm().drainMicrotasks();
|
||||
|
||||
_ = js.JSObjectCallAsFunction(
|
||||
vm.ctx,
|
||||
listener,
|
||||
js.JSContextGetGlobalObject(vm.ctx),
|
||||
1,
|
||||
&fetch_args,
|
||||
&exception,
|
||||
);
|
||||
if (request_context.has_called_done) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (exception != null) {
|
||||
var message = js.JSValueToStringCopy(vm.ctx, exception, null);
|
||||
defer js.JSStringRelease(message);
|
||||
var buf = vm.allocator.alloc(u8, js.JSStringGetLength(message) + 1) catch unreachable;
|
||||
defer vm.allocator.free(buf);
|
||||
var note = buf[0 .. js.JSStringGetUTF8CString(message, buf.ptr, buf.len) - 1];
|
||||
|
||||
Output.prettyErrorln("<r><red>error<r>: <b>{s}<r>", .{note});
|
||||
Output.flush();
|
||||
if (exception) |except| {
|
||||
vm.printException(except);
|
||||
|
||||
if (!request_context.has_called_done) {
|
||||
request_context.sendInternalError(error.JavaScriptError) catch {};
|
||||
|
||||
@@ -2,7 +2,9 @@ usingnamespace @import("../base.zig");
|
||||
const std = @import("std");
|
||||
const Api = @import("../../../api/schema.zig").Api;
|
||||
const http = @import("../../../http.zig");
|
||||
const JavaScript = @import("../javascript.zig");
|
||||
usingnamespace @import("../javascript.zig");
|
||||
usingnamespace @import("../bindings/bindings.zig");
|
||||
|
||||
pub const Response = struct {
|
||||
pub const Class = NewClass(
|
||||
Response,
|
||||
@@ -1089,7 +1091,26 @@ pub const FetchEvent = struct {
|
||||
return js.JSValueMakeUndefined(ctx);
|
||||
}
|
||||
|
||||
var arg = arguments[0];
|
||||
var resolved = JSInternalPromise.resolvedPromise(VirtualMachine.vm.global, JSValue.fromRef(arguments[0]));
|
||||
|
||||
var status = resolved.status(VirtualMachine.vm.global.vm());
|
||||
|
||||
if (status == .Pending) {
|
||||
VirtualMachine.vm.global.vm().drainMicrotasks();
|
||||
}
|
||||
|
||||
status = resolved.status(VirtualMachine.vm.global.vm());
|
||||
|
||||
switch (status) {
|
||||
.Fulfilled => {},
|
||||
else => {
|
||||
this.request_context.sendInternalError(error.rejectedPromise) catch {};
|
||||
return js.JSValueMakeUndefined(ctx);
|
||||
},
|
||||
}
|
||||
|
||||
var arg = resolved.result(VirtualMachine.vm.global.vm()).asObjectRef();
|
||||
|
||||
if (!js.JSValueIsObjectOfClass(ctx, arg, Response.Class.ref)) {
|
||||
JSError(getAllocator(ctx), "event.respondWith() must be a Response or a Promise<Response>.", .{}, ctx, exception);
|
||||
this.request_context.sendInternalError(error.respondWithWasEmpty) catch {};
|
||||
@@ -1180,3 +1201,67 @@ pub const FetchEvent = struct {
|
||||
return js.JSValueMakeUndefined(ctx);
|
||||
}
|
||||
};
|
||||
|
||||
// pub const ReadableStream = struct {
|
||||
// pub const Class = NewClass(
|
||||
// ReadableStream,
|
||||
// .{
|
||||
// .name = "ReadableStream",
|
||||
// },
|
||||
// .{},
|
||||
// .{
|
||||
|
||||
// },
|
||||
// );
|
||||
// };
|
||||
|
||||
// pub const TextEncoder = struct {
|
||||
// pub const Class = NewClass(
|
||||
// TextEncoder,
|
||||
// .{
|
||||
// .name = "TextEncoder",
|
||||
// },
|
||||
// .{
|
||||
// .encoding = .{
|
||||
// .@"get" = getEncoding,
|
||||
// .ro = true,
|
||||
// },
|
||||
// },
|
||||
// .{
|
||||
// .encode = .{
|
||||
// .rfn = encode,
|
||||
// },
|
||||
// .constructor = .{
|
||||
// .rfn = constructor,
|
||||
// },
|
||||
// .encodeInto = .{
|
||||
// .rfn = encodeInto,
|
||||
// },
|
||||
// },
|
||||
// );
|
||||
|
||||
// const encoding_str = "utf-8";
|
||||
// pub fn getEncoding(
|
||||
// this: *TextEncoder,
|
||||
// ctx: js.JSContextRef,
|
||||
// thisObject: js.JSObjectRef,
|
||||
// prop: js.JSStringRef,
|
||||
// exception: js.ExceptionRef,
|
||||
// ) js.JSValueRef {
|
||||
// return ZigString.init(encoding_str).toValue(ctx).asRef()
|
||||
// }
|
||||
// };
|
||||
|
||||
// pub const TextDecoder = struct {
|
||||
// pub const Class = NewClass(
|
||||
// TextDecoder,
|
||||
// .{
|
||||
// .name = "TextDecoder",
|
||||
// },
|
||||
// .{},
|
||||
// .{
|
||||
// .decode = .{},
|
||||
// .constructor = .{},
|
||||
// },
|
||||
// );
|
||||
// };
|
||||
|
||||
Reference in New Issue
Block a user