diff options
-rw-r--r-- | src/bun.js/base.zig | 758 |
1 files changed, 0 insertions, 758 deletions
diff --git a/src/bun.js/base.zig b/src/bun.js/base.zig index 8b4d858c9..9acf675cb 100644 --- a/src/bun.js/base.zig +++ b/src/bun.js/base.zig @@ -1000,8 +1000,6 @@ pub fn NewClassWithInstanceType( js.ExceptionRef, ) js.JSValueRef = rfn, - pub const ts = typescriptDeclaration(); - pub fn rfn( _: *ReceiverType, ctx: js.JSContextRef, @@ -1128,639 +1126,6 @@ pub fn NewClassWithInstanceType( return definition; } - const GetterNameFormatter = struct { - index: usize = 0, - - pub fn format(this: @This(), comptime _: []const u8, _: std.fmt.FormatOptions, writer: anytype) !void { - try writer.writeAll(std.mem.span(class_name_str)); - try writer.writeAll("_get_"); - const property_name = property_names[this.index]; - try writer.writeAll(std.mem.span(property_name)); - } - }; - - const SetterNameFormatter = struct { - index: usize = 0, - - pub fn format(this: @This(), comptime _: []const u8, _: std.fmt.FormatOptions, writer: anytype) !void { - try writer.writeAll(std.mem.span(class_name_str)); - try writer.writeAll("_set_"); - const property_name = property_names[this.index]; - try writer.writeAll(std.mem.span(property_name)); - } - }; - - const FunctionNameFormatter = struct { - index: usize = 0, - - pub fn format(this: @This(), comptime _: []const u8, _: std.fmt.FormatOptions, writer: anytype) !void { - try writer.writeAll(std.mem.span(class_name_str)); - try writer.writeAll("_fn_"); - const property_name = function_names[this.index]; - try writer.writeAll(std.mem.span(property_name)); - } - }; - - const PropertyDeclaration = struct { - index: usize = 0, - pub fn format(this: @This(), comptime fmt: []const u8, opts: std.fmt.FormatOptions, writer: anytype) !void { - const definition = getDefinition(); - const property = definition.staticValues[this.index]; - - if (property.getProperty != null) { - try writer.writeAll("static JSC_DECLARE_CUSTOM_GETTER("); - const getter_name = GetterNameFormatter{ .index = this.index }; - try getter_name.format(fmt, opts, writer); - try writer.writeAll(");\n"); - } - - if (property.setProperty != null) { - try writer.writeAll("static JSC_DECLARE_CUSTOM_SETTER("); - const getter_name = SetterNameFormatter{ .index = this.index }; - try getter_name.format(fmt, opts, writer); - try writer.writeAll(");\n"); - } - } - }; - - const FunctionDeclaration = struct { - index: usize = 0, - pub fn format(this: @This(), comptime fmt: []const u8, opts: std.fmt.FormatOptions, writer: anytype) !void { - const definition = getDefinition(); - const function = definition.staticFunctions[this.index]; - - if (function.callAsFunction != null) { - try writer.writeAll("static JSC_DECLARE_HOST_FUNCTION("); - const getter_name = FunctionNameFormatter{ .index = this.index }; - try getter_name.format(fmt, opts, writer); - try writer.writeAll(");\n"); - } - } - }; - - const PropertyDefinition = struct { - index: usize = 0, - pub fn format(this: @This(), comptime fmt: []const u8, opts: std.fmt.FormatOptions, writer: anytype) !void { - const definition = getDefinition(); - const property = definition.staticValues[this.index]; - - if (property.getProperty != null) { - try writer.writeAll("static JSC_DEFINE_CUSTOM_GETTER("); - const getter_name = GetterNameFormatter{ .index = this.index }; - try getter_name.format(fmt, opts, writer); - try writer.writeAll(", (JSC::JSGlobalObject * globalObject, JSC::EncodedJSValue thisValue, JSC::PropertyName)) {\n"); - try std.fmt.format( - writer, - \\ JSC::VM& vm = globalObject->vm(); - \\ Bun::{[name]s}* thisObject = JSC::jsDynamicCast<Bun::{[name]s}*>( JSValue::decode(thisValue)); - \\ if (UNLIKELY(!thisObject)) {{ - \\ return JSValue::encode(JSC::jsUndefined()); - \\ }} - \\ - \\ auto clientData = Bun::clientData(vm); - \\ auto scope = DECLARE_THROW_SCOPE(vm); - \\ - , - .{ .name = std.mem.span(class_name_str) }, - ); - if (ZigType == void) { - try std.fmt.format( - writer, - \\ JSC::EncodedJSValue result = Zig__{[getter]any}(globalObject); - , - .{ .getter = getter_name }, - ); - } else { - try std.fmt.format( - writer, - \\ JSC::EncodedJSValue result = Zig__{[getter]any}(globalObject, thisObject->m_ptr); - , - .{ .getter = getter_name }, - ); - } - - try writer.writeAll( - \\ JSC::JSObject *obj = JSC::JSValue::decode(result).getObject(); - \\ - \\ if (UNLIKELY(obj != nullptr && obj->isErrorInstance())) { - \\ scope.throwException(globalObject, obj); - \\ return JSValue::encode(JSC::jsUndefined()); - \\ } - \\ - \\ scope.release(); - \\ - \\ return result; - ); - - try writer.writeAll("}\n"); - } - - if (property.setProperty != null) { - try writer.writeAll("JSC_DEFINE_CUSTOM_SETTER("); - const getter_name = SetterNameFormatter{ .index = this.index }; - try getter_name.format(fmt, opts, writer); - try writer.writeAll(", (JSC::JSGlobalObject * globalObject, JSC::EncodedJSValue thisValue, JSC::EncodedJSValue value, JSC::PropertyName)) {\n"); - try std.fmt.format(writer, - \\ JSC::VM& vm = globalObject->vm(); - \\ Bun::{[name]s}* thisObject = JSC::jsDynamicCast<Bun::{[name]s}*>( JSValue::decode(thisValue)); - \\ if (UNLIKELY(!thisObject)) {{ - \\ return false; - \\ }} - \\ - \\ auto clientData = Bun::clientData(vm); - \\ auto scope = DECLARE_THROW_SCOPE(vm); - \\ - \\ - , .{ .name = getter_name }); - try writer.writeAll("};\n"); - } - } - }; - - const PropertyDeclarationsFormatter = struct { - pub fn format(_: @This(), comptime fmt: []const u8, opts: std.fmt.FormatOptions, writer: anytype) !void { - const definition = getDefinition(); - for (definition.staticValues[0 .. static_values_ptr.len - 1]) |_, i| { - const property = PropertyDeclaration{ .index = i }; - try property.format(fmt, opts, writer); - } - } - }; - - const PropertyDefinitionsFormatter = struct { - pub fn format(_: @This(), comptime fmt: []const u8, opts: std.fmt.FormatOptions, writer: anytype) !void { - const definition = getDefinition(); - if (static_values_ptr.len > 1) { - for (definition.staticValues[0 .. static_values_ptr.len - 1]) |_, i| { - const property = PropertyDefinition{ .index = i }; - try property.format(fmt, opts, writer); - } - } - } - }; - - const FunctionDefinitionsFormatter = struct { - pub fn format(_: @This(), comptime fmt: []const u8, opts: std.fmt.FormatOptions, writer: anytype) !void { - _ = fmt; - _ = writer; - _ = opts; - // for (static_properties[0 .. static_properties.len - 1]) |_, i| { - // const property = FunctionDefinition{ .index = i }; - // try property.format(fmt, opts, writer); - // } - } - }; - - const FunctionDeclarationsFormatter = struct { - pub fn format(_: @This(), comptime fmt: []const u8, opts: std.fmt.FormatOptions, writer: anytype) !void { - _ = fmt; - _ = writer; - const definition = getDefinition(); - if (static_functions__.len > 1) { - for (definition.staticFunctions[0 .. static_functions__.len - 1]) |_, i| { - const function = FunctionDeclaration{ .index = i }; - try function.format(fmt, opts, writer); - } - } - } - }; - - pub fn @"generateC++Header"(writer: anytype) !void { - const header_file = - \\// AUTO-GENERATED FILE - \\#pragma once - \\ - \\#include "BunBuiltinNames.h" - \\#include "BunClientData.h" - \\#include "root.h" - \\ - \\ - \\namespace Bun {{ - \\ - \\using namespace JSC; - \\using namespace Zig; - \\ - \\class {[name]s} : public JSNonFinalObject {{ - \\ using Base = JSNonFinalObject; - \\ - \\public: - \\ {[name]s}(JSC::VM& vm, Structure* structure) : Base(vm, structure) {{}} - \\ - \\ - \\ DECLARE_INFO; - \\ - \\ static constexpr unsigned StructureFlags = Base::StructureFlags; - \\ template<typename CellType, SubspaceAccess> static GCClient::IsoSubspace* subspaceFor(VM& vm) - \\ {{ - \\ return &vm.cellSpace(); - \\ }} - \\ static JSC::Structure* createStructure(JSC::VM& vm, JSC::JSGlobalObject* globalObject, - \\ JSC::JSValue prototype) - \\ {{ - \\ return JSC::Structure::create(vm, globalObject, prototype, - \\ JSC::TypeInfo(JSC::ObjectType, StructureFlags), info()); - \\ }} - \\ - \\ static {[name]s}* create(JSC::VM& vm, JSC::Structure* structure) - \\ {{ - \\ {[name]s}* accessor = new (NotNull, JSC::allocateCell<{[name]s}>(vm)) {[name]s}(vm, structure); - \\ accessor->finishCreation(vm); - \\ return accessor; - \\ }} - \\ - \\ void finishCreation(JSC::VM& vm); - \\ - \\}}; - \\ - \\}} // namespace Bun - \\ - ; - _ = writer; - _ = header_file; - const Opts = struct { name: string }; - try writer.print(header_file, Opts{ - .name = std.mem.span(name), - }); - } - - const LookupTableFormatter = struct { - // example: - // - // /* Source for IntlLocalePrototype.lut.h - // @begin localePrototypeTable - // maximize intlLocalePrototypeFuncMaximize DontEnum|Function 0 - // minimize intlLocalePrototypeFuncMinimize DontEnum|Function 0 - // toString intlLocalePrototypeFuncToString DontEnum|Function 0 - // baseName intlLocalePrototypeGetterBaseName DontEnum|ReadOnly|CustomAccessor - // calendar intlLocalePrototypeGetterCalendar DontEnum|ReadOnly|CustomAccessor - // calendars intlLocalePrototypeGetterCalendars DontEnum|ReadOnly|CustomAccessor - // caseFirst intlLocalePrototypeGetterCaseFirst DontEnum|ReadOnly|CustomAccessor - // collation intlLocalePrototypeGetterCollation DontEnum|ReadOnly|CustomAccessor - // collations intlLocalePrototypeGetterCollations DontEnum|ReadOnly|CustomAccessor - // hourCycle intlLocalePrototypeGetterHourCycle DontEnum|ReadOnly|CustomAccessor - // hourCycles intlLocalePrototypeGetterHourCycles DontEnum|ReadOnly|CustomAccessor - // numeric intlLocalePrototypeGetterNumeric DontEnum|ReadOnly|CustomAccessor - // numberingSystem intlLocalePrototypeGetterNumberingSystem DontEnum|ReadOnly|CustomAccessor - // numberingSystems intlLocalePrototypeGetterNumberingSystems DontEnum|ReadOnly|CustomAccessor - // language intlLocalePrototypeGetterLanguage DontEnum|ReadOnly|CustomAccessor - // script intlLocalePrototypeGetterScript DontEnum|ReadOnly|CustomAccessor - // region intlLocalePrototypeGetterRegion DontEnum|ReadOnly|CustomAccessor - // timeZones intlLocalePrototypeGetterTimeZones DontEnum|ReadOnly|CustomAccessor - // textInfo intlLocalePrototypeGetterTextInfo DontEnum|ReadOnly|CustomAccessor - // weekInfo intlLocalePrototypeGetterWeekInfo DontEnum|ReadOnly|CustomAccessor - // @end - // */ - pub fn format(_: @This(), comptime _: []const u8, _: std.fmt.FormatOptions, writer: anytype) !void { - const definition = getDefinition(); - try writer.writeAll("/* Source for "); - try writer.writeAll(std.mem.span(definition.className)); - try writer.writeAll(".lut.h\n"); - try writer.writeAll("@begin "); - try writer.writeAll(std.mem.span(definition.className)); - try writer.writeAll("HashTableValues \n"); - var middle_padding: usize = 0; - if (property_names.len > 0) { - for (property_names) |prop| { - middle_padding = @maximum(prop.len, middle_padding); - } - } - if (function_names.len > 0) { - for (function_names[0..function_names.len]) |_name| { - middle_padding = @maximum(std.mem.span(_name).len, middle_padding); - } - } - - if (property_names.len > 0) { - comptime var i: usize = 0; - inline while (i < property_names.len) : (i += 1) { - try writer.writeAll(" "); - const name_ = property_names[i]; - try writer.writeAll(name_); - try writer.writeAll(" "); - var k: usize = 0; - while (k < middle_padding - name_.len) : (k += 1) { - try writer.writeAll(" "); - } - - try writer.print("{any} ", .{GetterNameFormatter{ .index = i }}); - - k = 0; - - while (k < middle_padding - name_.len) : (k += 1) { - try writer.writeAll(" "); - } - - try writer.writeAll("CustomAccessor"); - if (options.read_only or @hasField(@TypeOf(@field(properties, property_names[i])), "ro")) { - try writer.writeAll("|ReadOnly"); - } - - if (@hasField(@TypeOf(@field(properties, property_names[i])), "enumerable") and !@field(properties, property_names[i])) { - try writer.writeAll("|DontEnum"); - } - - try writer.writeAll("\n"); - } - } - if (function_names.len > 0) { - comptime var i: usize = 0; - inline while (i < function_names.len) : (i += 1) { - try writer.writeAll(" "); - const name_ = function_names[i]; - try writer.writeAll(name_); - try writer.writeAll(" "); - var k: usize = 0; - while (k < middle_padding - name_.len) : (k += 1) { - try writer.writeAll(" "); - } - - try writer.print("{any} ", .{FunctionNameFormatter{ .index = i }}); - k = 0; - - while (k < middle_padding - name_.len) : (k += 1) { - try writer.writeAll(" "); - } - var read_only_ = false; - if (options.read_only or @hasField(@TypeOf(comptime @field(staticFunctions, function_names[i])), "ro")) { - read_only_ = true; - try writer.writeAll("ReadOnly"); - } - - if (comptime std.meta.trait.isContainer( - @TypeOf(comptime @field(staticFunctions, function_names[i])), - ) and - @hasField(@TypeOf(comptime @field( - staticFunctions, - function_names[i], - )), "enumerable") and !@field(staticFunctions, function_names[i]).enumerable) { - if (read_only_) { - try writer.writeAll("|"); - } - try writer.writeAll("DontEnum"); - } - - try writer.writeAll("Function 1"); - - try writer.writeAll("\n"); - } - } - - try writer.writeAll("@end\n*/\n"); - } - }; - - pub fn @"generateC++Class"(writer: anytype) !void { - const implementation_file = - \\// AUTO-GENERATED FILE - \\ - \\#include "{[name]s}.generated.h" - \\#include "{[name]s}.lut.h" - \\ - \\namespace Bun {{ - \\ - \\{[lut]any} - \\ - \\using JSGlobalObject = JSC::JSGlobalObject; - \\using Exception = JSC::Exception; - \\using JSValue = JSC::JSValue; - \\using JSString = JSC::JSString; - \\using JSModuleLoader = JSC::JSModuleLoader; - \\using JSModuleRecord = JSC::JSModuleRecord; - \\using Identifier = JSC::Identifier; - \\using SourceOrigin = JSC::SourceOrigin; - \\using JSObject = JSC::JSObject; - \\using JSNonFinalObject = JSC::JSNonFinalObject; - \\namespace JSCastingHelpers = JSC::JSCastingHelpers; - \\ - \\#pragma mark - Function Declarations - \\ - \\{[function_declarations]any} - \\ - \\#pragma mark - Property Declarations - \\ - \\{[property_declarations]any} - \\ - \\#pragma mark - Function Definitions - \\ - \\{[function_definitions]any} - \\ - \\#pragma mark - Property Definitions - \\ - \\{[property_definitions]any} - \\ - \\const JSC::ClassInfo {[name]s}::s_info = {{ "{[name]s}"_s, &Base::s_info, &{[name]s}HashTableValues, nullptr, CREATE_METHOD_TABLE([name]s) }}; - \\ - \\ void {[name]s}::finishCreation(JSC::VM& vm) {{ - \\ Base::finishCreation(vm); - \\ auto clientData = Bun::clientData(vm); - \\ JSC::JSGlobalObject *globalThis = globalObject(); - \\ - \\ - \\#pragma mark - Property Initializers - \\ - \\{[property_initializers]any} - \\ - \\#pragma mark - Function Initializers - \\ - \\{[function_initializers]any} - \\ - \\ }} - \\ - \\}} // namespace Bun - \\ - ; - - try writer.print(implementation_file, .{ - .name = std.mem.span(class_name_str), - .function_initializers = @as(string, ""), - .property_initializers = @as(string, ""), - .function_declarations = FunctionDeclarationsFormatter{}, - .property_declarations = FunctionDeclarationsFormatter{}, - .function_definitions = FunctionDefinitionsFormatter{}, - .property_definitions = PropertyDefinitionsFormatter{}, - .lut = LookupTableFormatter{}, - }); - } - - // This should only be run at comptime - pub fn typescriptModuleDeclaration() d.ts.module { - comptime var class = options.ts.module; - comptime { - if (class.read_only == null) { - class.read_only = options.read_only; - } - - if (function_name_literals.len > 0) { - var count: usize = 0; - inline for (function_name_literals) |_, i| { - const func = @field(staticFunctions, function_names[i]); - const Func = @TypeOf(func); - - switch (@typeInfo(Func)) { - .Struct => { - var total: usize = 1; - if (hasTypeScript(Func)) { - if (std.meta.trait.isIndexable(@TypeOf(func.ts))) { - total = func.ts.len; - } - } - - count += total; - }, - else => continue, - } - } - - var funcs = std.mem.zeroes([count]d.ts); - class.functions = std.mem.span(&funcs); - var func_i: usize = 0; - @setEvalBranchQuota(99999); - inline for (function_name_literals) |_, i| { - const func = @field(staticFunctions, function_names[i]); - const Func = @TypeOf(func); - - switch (@typeInfo(Func)) { - .Struct => { - var ts_functions: []const d.ts = &[_]d.ts{}; - - if (hasTypeScript(Func)) { - if (std.meta.trait.isIndexable(@TypeOf(func.ts))) { - ts_functions = std.mem.span(func.ts); - } - } - - if (ts_functions.len == 0 and hasTypeScript(Func)) { - var funcs1 = std.mem.zeroes([1]d.ts); - funcs1[0] = func.ts; - ts_functions = std.mem.span(&funcs1); - } else { - var funcs1 = std.mem.zeroes([1]d.ts); - funcs1[0] = .{ .name = function_names[i] }; - ts_functions = std.mem.span(&funcs1); - } - - for (ts_functions) |ts_function_| { - var ts_function = ts_function_; - if (ts_function.name.len == 0) { - ts_function.name = function_names[i]; - } - - if (ts_function.read_only == null) { - ts_function.read_only = class.read_only; - } - - class.functions[func_i] = ts_function; - - func_i += 1; - } - }, - else => continue, - } - } - } - - if (property_names.len > 0) { - var count: usize = 0; - var class_count: usize = 0; - - inline for (property_names) |_, i| { - const field = @field(properties, property_names[i]); - const Field = @TypeOf(field); - - if (hasTypeScript(Field)) { - switch (getTypeScript(Field, field)) { - .decl => |dec| { - switch (dec) { - .class => { - class_count += 1; - }, - else => {}, - } - }, - .ts => { - count += 1; - }, - } - } - } - - var props = std.mem.zeroes([count]d.ts); - class.properties = std.mem.span(&props); - var property_i: usize = 0; - - var classes = std.mem.zeroes([class_count + class.classes.len]d.ts.class); - if (class.classes.len > 0) { - std.mem.copy(d.ts.class, classes, class.classes); - } - - var class_i: usize = class.classes.len; - class.classes = std.mem.span(&classes); - - inline for (property_names) |property_name, i| { - const field = @field(properties, property_names[i]); - const Field = @TypeOf(field); - - if (hasTypeScript(Field)) { - switch (getTypeScript(Field, field)) { - .decl => |dec| { - switch (dec) { - .class => |ts_class| { - class.classes[class_i] = ts_class; - class_i += 1; - }, - else => {}, - } - }, - .ts => |ts_field_| { - var ts_field: d.ts = ts_field_; - if (ts_field.name.len == 0) { - ts_field.name = property_name; - } - - if (ts_field.read_only == null) { - if (hasReadOnly(Field)) { - ts_field.read_only = field.ro; - } else { - ts_field.read_only = class.read_only; - } - } - - class.properties[property_i] = ts_field; - - property_i += 1; - }, - } - } - } - } - } - - return class; - } - - pub fn typescriptDeclaration() d.ts.decl { - comptime var decl = options.ts; - comptime switch (decl) { - .module => { - decl.module = typescriptModuleDeclaration(); - }, - .class => { - decl.class = typescriptClassDeclaration(decl.class); - }, - .empty => { - decl = d.ts.decl{ - .class = typescriptClassDeclaration( - d.ts.class{ - .name = options.name, - }, - ), - }; - }, - }; - - return decl; - } - pub fn getPropertyNames( _: js.JSContextRef, _: js.JSObjectRef, @@ -1796,129 +1161,6 @@ pub fn NewClassWithInstanceType( } } - // This should only be run at comptime - pub fn typescriptClassDeclaration(comptime original: d.ts.class) d.ts.class { - comptime var class = original; - - comptime { - if (class.name.len == 0) { - class.name = options.name; - } - - if (class.read_only == null) { - class.read_only = options.read_only; - } - - if (function_name_literals.len > 0) { - var count: usize = 0; - inline for (function_name_literals) |_, i| { - const func = @field(staticFunctions, function_names[i]); - const Func = @TypeOf(func); - - switch (@typeInfo(Func)) { - .Struct => { - var total: usize = 1; - if (hasTypeScript(Func)) { - if (std.meta.trait.isIndexable(@TypeOf(func.ts))) { - total = func.ts.len; - } - } - - count += total; - }, - else => continue, - } - } - - var funcs = std.mem.zeroes([count]d.ts); - class.functions = std.mem.span(&funcs); - var func_i: usize = 0; - - inline for (function_name_literals) |_, i| { - const func = @field(staticFunctions, function_names[i]); - const Func = @TypeOf(func); - - switch (@typeInfo(Func)) { - .Struct => { - var ts_functions: []const d.ts = &[_]d.ts{}; - - if (hasTypeScript(Func)) { - if (std.meta.trait.isIndexable(@TypeOf(func.ts))) { - ts_functions = std.mem.span(func.ts); - } - } - - if (ts_functions.len == 0 and hasTypeScript(Func)) { - var funcs1 = std.mem.zeroes([1]d.ts); - funcs1[0] = func.ts; - ts_functions = std.mem.span(&funcs1); - } else { - var funcs1 = std.mem.zeroes([1]d.ts); - funcs1[0] = .{ .name = function_names[i] }; - ts_functions = std.mem.span(&funcs1); - } - - for (ts_functions) |ts_function_| { - var ts_function = ts_function_; - if (ts_function.name.len == 0) { - ts_function.name = function_names[i]; - } - - if (class.interface and strings.eqlComptime(ts_function.name, "constructor")) { - ts_function.name = "new"; - } - - if (ts_function.read_only == null) { - ts_function.read_only = class.read_only; - } - - class.functions[func_i] = ts_function; - - func_i += 1; - } - }, - else => continue, - } - } - } - - if (property_names.len > 0) { - var count: usize = property_names.len; - - var props = std.mem.zeroes([count]d.ts); - class.properties = std.mem.span(&props); - var property_i: usize = 0; - - inline for (property_names) |property_name, i| { - const field = @field(properties, property_names[i]); - - var ts_field: d.ts = .{}; - - if (hasTypeScript(@TypeOf(field))) { - ts_field = field.ts; - } - - if (ts_field.name.len == 0) { - ts_field.name = property_name; - } - - if (ts_field.read_only == null) { - if (hasReadOnly(@TypeOf(field))) { - ts_field.read_only = field.ro; - } else { - ts_field.read_only = class.read_only; - } - } - - class.properties[property_i] = ts_field; - property_i += 1; - } - } - } - - return comptime class; - } - const static_properties: [property_names.len + 1]js.JSStaticValue = brk: { var props: [property_names.len + 1]js.JSStaticValue = undefined; std.mem.set( |