diff options
Diffstat (limited to 'src')
m--------- | src/bun.js/WebKit | 0 | ||||
-rw-r--r-- | src/bun.js/bindings/JSSink.h | 2 | ||||
-rw-r--r-- | src/bun.js/bindings/JSSinkLookupTable.h | 2 | ||||
-rw-r--r-- | src/bun.js/scripts/class-definitions.ts | 36 | ||||
-rw-r--r-- | src/bun.js/scripts/generate-classes.ts (renamed from src/bun.js/generate-classes.js) | 201 | ||||
-rw-r--r-- | src/bun.js/scripts/generate-jssink.js (renamed from src/bun.js/generate-jssink.js) | 53 | ||||
-rw-r--r-- | src/bun.js/webcore/response.classes.ts | 115 | ||||
-rw-r--r-- | src/bun.js/webcore/streams.zig | 6 |
8 files changed, 235 insertions, 180 deletions
diff --git a/src/bun.js/WebKit b/src/bun.js/WebKit -Subproject aa0f8d166f5ff005ad0ec502bdea805aedd71b3 +Subproject 208963e47abaf762c5c132a596afc36a86eb619 diff --git a/src/bun.js/bindings/JSSink.h b/src/bun.js/bindings/JSSink.h index b750cfabe..56a0d584a 100644 --- a/src/bun.js/bindings/JSSink.h +++ b/src/bun.js/bindings/JSSink.h @@ -1,6 +1,6 @@ // AUTO-GENERATED FILE. DO NOT EDIT. -// Generated by 'make generate-sink' at 2022-08-17T12:00:00.774Z +// Generated by 'make generate-sink' at 2022-08-18T01:38:52.471Z // #pragma once diff --git a/src/bun.js/bindings/JSSinkLookupTable.h b/src/bun.js/bindings/JSSinkLookupTable.h index c57b50618..bdd669d20 100644 --- a/src/bun.js/bindings/JSSinkLookupTable.h +++ b/src/bun.js/bindings/JSSinkLookupTable.h @@ -1,4 +1,4 @@ -// Automatically generated from src/bun.js/bindings/JSSink.cpp using /build/bun-webkit//Source/JavaScriptCore/create_hash_table. DO NOT EDIT! +// Automatically generated from src/bun.js/bindings/JSSink.cpp using /Users/jarred/Code/bun/src/bun.js/WebKit/Source/JavaScriptCore/create_hash_table. DO NOT EDIT! diff --git a/src/bun.js/scripts/class-definitions.ts b/src/bun.js/scripts/class-definitions.ts new file mode 100644 index 000000000..4b202f8a5 --- /dev/null +++ b/src/bun.js/scripts/class-definitions.ts @@ -0,0 +1,36 @@ +export type Field = + | { getter: string; cache?: true } + | { setter: string } + | { accessor: { getter: string; setter: string }; cache?: true } + | { + fn: string; + length?: number; + DOMJIT?: { + return: string; + args?: [string, string] | [string, string, string] | [string]; + symbol: string; + }; + }; + +export interface ClassDefinition { + name: string; + construct?: boolean; + finalize?: boolean; + klass: Record<string, Field>; + proto: Record<string, Field>; + JSType?: string; +} + +export function define( + { klass = {}, proto = {}, ...rest } = {} as ClassDefinition +): ClassDefinition { + return { + ...rest, + klass: Object.fromEntries( + Object.entries(klass).sort(([a], [b]) => a.localeCompare(b)) + ), + proto: Object.fromEntries( + Object.entries(proto).sort(([a], [b]) => a.localeCompare(b)) + ), + }; +} diff --git a/src/bun.js/generate-classes.js b/src/bun.js/scripts/generate-classes.ts index b033e606d..e8d4fd144 100644 --- a/src/bun.js/generate-classes.js +++ b/src/bun.js/scripts/generate-classes.ts @@ -1,3 +1,7 @@ +import { readdirSync } from "fs"; +import { resolve } from "path"; +import type { Field, ClassDefinition } from "./class-definitions"; + function symbolName(typeName, name) { return `${typeName}__${name}`; } @@ -50,7 +54,13 @@ function appendSymbols(to, symbolName, prop) { to.push([fn, symbolName(fn)]); } } -function propRow(symbolName, typeName, name, prop, isWrapped = true) { +function propRow( + symbolName: (a: string, b: string) => string, + typeName: string, + name: string, + prop: Field, + isWrapped = true +) { var { defaultValue, getter, @@ -114,7 +124,7 @@ function propRow(symbolName, typeName, name, prop, isWrapped = true) { throw "Unsupported property"; } -function generateHashTable( +export function generateHashTable( nameToUse, symbolName, typeName, @@ -510,8 +520,13 @@ function renderStaticDecls(symbolName, typeName, fields) { return rows.join("\n"); } -function renderFieldsImpl(symbolName, typeName, obj, proto) { - const rows = []; +function renderFieldsImpl( + symbolName: (typeName: string, name: string) => string, + typeName: string, + obj: ClassDefinition, + proto: ClassDefinition["proto"] +) { + const rows: string[] = []; if (obj.construct) { rows.push(` @@ -870,9 +885,9 @@ function generateImpl(typeName, obj) { function generateZig( typeName, - { klass = {}, proto = {}, construct, finalize } + { klass = {}, proto = {}, construct, finalize } = {} as ClassDefinition ) { - const exports = []; + const exports: [string, string][] = []; if (construct) { exports.push([`constructor`, classSymbolName(typeName, "construct")]); @@ -1083,138 +1098,6 @@ function generateLazyClassStructureImpl(typeName, { klass = {}, proto = {} }) { `.trim(); } -function define({ klass = {}, proto = {}, ...rest } = {}) { - return { - ...rest, - klass: Object.fromEntries( - Object.entries(klass).sort(([a], [b]) => a.localeCompare(b)) - ), - proto: Object.fromEntries( - Object.entries(proto).sort(([a], [b]) => a.localeCompare(b)) - ), - }; -} - -const classes = [ - define({ - name: "Request", - construct: true, - finalize: true, - klass: {}, - JSType: "0b11101110", - proto: { - url: { - getter: "getURL", - cache: true, - }, - - // body: { - // getter: "getBody", - // }, - - text: { fn: "getText" }, - json: { fn: "getJSON" }, - arrayBuffer: { fn: "getArrayBuffer" }, - blob: { fn: "getBlob" }, - clone: { fn: "doClone", length: 1 }, - cache: { - getter: "getCache", - }, - credentials: { - getter: "getCredentials", - }, - destination: { - getter: "getDestination", - }, - headers: { - getter: "getHeaders", - cache: true, - }, - integrity: { - getter: "getIntegrity", - }, - method: { - getter: "getMethod", - }, - mode: { - getter: "getMode", - }, - redirect: { - getter: "getRedirect", - }, - referrer: { - getter: "getReferrer", - }, - referrerPolicy: { - getter: "getReferrerPolicy", - }, - url: { - getter: "getUrl", - cache: true, - }, - bodyUsed: { - getter: "getBodyUsed", - }, - }, - }), - - define({ - name: "Response", - construct: true, - finalize: true, - JSType: "0b11101110", - klass: { - json: { - fn: "constructJSON", - }, - redirect: { - fn: "constructRedirect", - }, - error: { - fn: "constructError", - }, - }, - proto: { - url: { - getter: "getURL", - cache: true, - }, - - // body: { - // getter: "getBody", - // }, - - text: { fn: "getText" }, - json: { fn: "getJSON" }, - arrayBuffer: { fn: "getArrayBuffer" }, - blob: { fn: "getBlob" }, - clone: { fn: "doClone", length: 1 }, - - type: { - getter: "getResponseType", - }, - headers: { - getter: "getHeaders", - cache: true, - }, - - statusText: { - getter: "getStatusText", - cache: true, - }, - status: { - getter: "getStatus", - }, - ok: { - getter: "getOK", - }, - bodyUsed: { - getter: "getBodyUsed", - }, - }, - }), -]; - const GENERATED_CLASSES_HEADER = ` // GENERATED CODE - DO NOT MODIFY BY HAND // Generated by src/bun.js/generate-classes.js @@ -1309,27 +1192,53 @@ const StaticCallbackType = fn(*JSC.JSGlobalObject, *JSC.CallFrame) callconv(.C) `; -await Bun.write(`${import.meta.dir}/bindings/generated_classes.zig`, [ + +const directoriesToSearch = [ + resolve(`${import.meta.dir}/../api`), + resolve(`${import.meta.dir}/../webcore`), +]; + +function findClasses() { + var classes = []; + for (let directory of directoriesToSearch) { + readdirSync(directory).forEach((file) => { + if (file.endsWith(".classes.ts")) { + const result = require(`${directory}/${file}`); + console.log("Generated", result.default.length, "classes from", file); + for (let { name } of result.default) { + console.log(` - ${name}`); + } + + classes.push(...result.default); + } + }); + } + return classes; +} + +const classes = findClasses(); + +await Bun.write(`${import.meta.dir}/../bindings/generated_classes.zig`, [ ZIG_GENERATED_CLASSES_HEADER, ...classes.map((a) => generateZig(a.name, a).trim()).join("\n"), ]); -await Bun.write(`${import.meta.dir}/bindings/ZigGeneratedClasses.h`, [ +await Bun.write(`${import.meta.dir}/../bindings/ZigGeneratedClasses.h`, [ GENERATED_CLASSES_HEADER, ...classes.map((a) => generateHeader(a.name, a)), GENERATED_CLASSES_FOOTER, ]); -await Bun.write(`${import.meta.dir}/bindings/ZigGeneratedClasses.cpp`, [ +await Bun.write(`${import.meta.dir}/../bindings/ZigGeneratedClasses.cpp`, [ GENERATED_CLASSES_IMPL_HEADER, ...classes.map((a) => generateImpl(a.name, a)), GENERATED_CLASSES_IMPL_FOOTER, ]); await Bun.write( - `${import.meta.dir}/bindings/ZigGeneratedClasses+lazyStructureHeader.h`, + `${import.meta.dir}/../bindings/ZigGeneratedClasses+lazyStructureHeader.h`, classes.map((a) => generateLazyClassStructureHeader(a.name, a)).join("\n") ); await Bun.write( - `${import.meta.dir}/bindings/ZigGeneratedClasses+DOMClientIsoSubspaces.h`, + `${import.meta.dir}/../bindings/ZigGeneratedClasses+DOMClientIsoSubspaces.h`, classes.map((a) => [ `std::unique_ptr<GCClient::IsoSubspace> ${clientSubspaceFor(a.name)};`, @@ -1341,7 +1250,7 @@ await Bun.write( ); await Bun.write( - `${import.meta.dir}/bindings/ZigGeneratedClasses+DOMIsoSubspaces.h`, + `${import.meta.dir}/../bindings/ZigGeneratedClasses+DOMIsoSubspaces.h`, classes.map((a) => [ `std::unique_ptr<IsoSubspace> ${subspaceFor(a.name)};`, @@ -1351,10 +1260,12 @@ await Bun.write( ); await Bun.write( - `${import.meta.dir}/bindings/ZigGeneratedClasses+lazyStructureImpl.h`, + `${import.meta.dir}/../bindings/ZigGeneratedClasses+lazyStructureImpl.h`, initLazyClasses( classes.map((a) => generateLazyClassStructureImpl(a.name, a)) ) + "\n" + visitLazyClasses(classes) ); + +export {}; diff --git a/src/bun.js/generate-jssink.js b/src/bun.js/scripts/generate-jssink.js index aac782222..1caea3c22 100644 --- a/src/bun.js/generate-jssink.js +++ b/src/bun.js/scripts/generate-jssink.js @@ -1,3 +1,5 @@ +import { resolve } from "path"; + const classes = ["ArrayBufferSink", "HTTPResponseSink", "HTTPSResponseSink"]; const SINK_COUNT = 5; @@ -464,33 +466,40 @@ JSC_DEFINE_HOST_FUNCTION(${name}__doClose, (JSC::JSGlobalObject * lexicalGlobalO templ += ` /* Source for JS${name}PrototypeTableValues.lut.h @begin JS${name}PrototypeTable - close ${`${name}__doClose`.padEnd( + close ${`${name}__doClose`.padEnd( + padding + 8 + )} ReadOnly|DontDelete|Function 0 + flush ${`${name}__flush`.padEnd( + padding + 8 + )} ReadOnly|DontDelete|Function 1 + end ${`${name}__end`.padEnd( padding + 8 )} ReadOnly|DontDelete|Function 0 - flush ${`${name}__flush`.padEnd(padding + 8)} ReadOnly|DontDelete|Function 1 - end ${`${name}__end`.padEnd(padding + 8)} ReadOnly|DontDelete|Function 0 - start ${`${name}__start`.padEnd(padding + 8)} ReadOnly|DontDelete|Function 1 - write ${`${name}__write`.padEnd(padding + 8)} ReadOnly|DontDelete|Function 1 - + start ${`${name}__start`.padEnd( + padding + 8 + )} ReadOnly|DontDelete|Function 1 + write ${`${name}__write`.padEnd( + padding + 8 + )} ReadOnly|DontDelete|Function 1 @end */ /* Source for ${controllerPrototypeName}TableValues.lut.h @begin ${controllerPrototypeName}Table - close ${`${controller}__close`.padEnd( + close ${`${controller}__close`.padEnd( protopad + 4 )} ReadOnly|DontDelete|Function 0 - flush ${`${name}__flush`.padEnd( + flush ${`${name}__flush`.padEnd( protopad + 4 )} ReadOnly|DontDelete|Function 1 - end ${`${controller}__end`.padEnd( + end ${`${controller}__end`.padEnd( protopad + 4 )} ReadOnly|DontDelete|Function 0 - start ${`${name}__start`.padEnd( + start ${`${name}__start`.padEnd( protopad + 4 )} ReadOnly|DontDelete|Function 1 - write ${`${name}__write`.padEnd( + write ${`${name}__write`.padEnd( protopad + 4 )} ReadOnly|DontDelete|Function 1 @end @@ -499,9 +508,6 @@ JSC_DEFINE_HOST_FUNCTION(${name}__doClose, (JSC::JSGlobalObject * lexicalGlobalO `; } - templ += ` -${(await Bun.file(import.meta.dir + "/bindings/JSSink+custom.h").text()).trim()} -`; const footer = ` } // namespace WebCore @@ -872,24 +878,11 @@ extern "C" JSC__JSValue ${name}__assignToStream(JSC__JSGlobalObject* arg0, JSC__ { auto& vm = arg0->vm(); Zig::GlobalObject* globalObject = reinterpret_cast<Zig::GlobalObject*>(arg0); - auto clientData = WebCore::clientData(vm); - JSC::JSObject *readableStream = JSC::JSValue::decode(stream).getObject(); - auto scope = DECLARE_CATCH_SCOPE(vm); JSC::Structure* structure = WebCore::getDOMStructure<WebCore::${controller}>(vm, *globalObject); WebCore::${controller} *controller = WebCore::${controller}::create(vm, globalObject, structure, sinkPtr); *controllerValue = reinterpret_cast<void*>(JSC::JSValue::encode(controller)); - JSC::JSObject *function = globalObject->getDirect(vm, clientData->builtinNames().assignToStreamPrivateName()).getObject(); - auto callData = JSC::getCallData(function); - JSC::MarkedArgumentBuffer arguments; - arguments.append(JSC::JSValue::decode(stream)); - arguments.append(controller); - - auto result = JSC::call(arg0, function, callData, JSC::jsUndefined(), arguments); - if (scope.exception()) - return JSC::JSValue::encode(scope.exception()); - - return JSC::JSValue::encode(result); + return globalObject->assignToStream(JSC::JSValue::decode(stream), controller); } extern "C" void ${name}__onReady(JSC__JSValue controllerValue, JSC__JSValue amt, JSC__JSValue offset) @@ -940,8 +933,8 @@ extern "C" void ${name}__onClose(JSC__JSValue controllerValue, JSC__JSValue reas return templ; } -await Bun.write(import.meta.dir + "/bindings/JSSink.h", header()); +await Bun.write(resolve(import.meta.dir + "/../bindings/JSSink.h"), header()); await Bun.write( - import.meta.dir + "/bindings/JSSink.cpp", + resolve(import.meta.dir + "/../bindings/JSSink.cpp"), await implementation() ); diff --git a/src/bun.js/webcore/response.classes.ts b/src/bun.js/webcore/response.classes.ts new file mode 100644 index 000000000..783aa8616 --- /dev/null +++ b/src/bun.js/webcore/response.classes.ts @@ -0,0 +1,115 @@ +import { define } from "../scripts/class-definitions"; + +export default [ + define({ + name: "Request", + construct: true, + finalize: true, + klass: {}, + JSType: "0b11101110", + proto: { + // body: { + // getter: "getBody", + // }, + + text: { fn: "getText" }, + json: { fn: "getJSON" }, + arrayBuffer: { fn: "getArrayBuffer" }, + blob: { fn: "getBlob" }, + clone: { fn: "doClone", length: 1 }, + cache: { + getter: "getCache", + }, + credentials: { + getter: "getCredentials", + }, + destination: { + getter: "getDestination", + }, + headers: { + getter: "getHeaders", + cache: true, + }, + integrity: { + getter: "getIntegrity", + }, + method: { + getter: "getMethod", + }, + mode: { + getter: "getMode", + }, + redirect: { + getter: "getRedirect", + }, + referrer: { + getter: "getReferrer", + }, + referrerPolicy: { + getter: "getReferrerPolicy", + }, + url: { + getter: "getUrl", + cache: true, + }, + bodyUsed: { + getter: "getBodyUsed", + }, + }, + }), + define({ + name: "Response", + construct: true, + finalize: true, + JSType: "0b11101110", + klass: { + json: { + fn: "constructJSON", + }, + redirect: { + fn: "constructRedirect", + }, + error: { + fn: "constructError", + }, + }, + proto: { + url: { + getter: "getURL", + cache: true, + }, + + // body: { + // getter: "getBody", + // }, + + text: { fn: "getText" }, + json: { fn: "getJSON" }, + arrayBuffer: { fn: "getArrayBuffer" }, + blob: { fn: "getBlob" }, + clone: { fn: "doClone", length: 1 }, + + type: { + getter: "getResponseType", + }, + headers: { + getter: "getHeaders", + cache: true, + }, + + statusText: { + getter: "getStatusText", + cache: true, + }, + status: { + getter: "getStatus", + }, + ok: { + getter: "getOK", + }, + bodyUsed: { + getter: "getBodyUsed", + }, + }, + }), +]; diff --git a/src/bun.js/webcore/streams.zig b/src/bun.js/webcore/streams.zig index 47972baec..a9f1a5a37 100644 --- a/src/bun.js/webcore/streams.zig +++ b/src/bun.js/webcore/streams.zig @@ -1195,7 +1195,7 @@ pub fn NewJSSink(comptime SinkType: type, comptime name_: []const u8) type { return this.sink.writeLatin1(.{ .temporary = bun.ByteList.init(str.slice()) }).toJS(globalThis); } - pub fn writeString(globalThis: *JSGlobalObject, callframe: *JSC.CallFrame) callconv(.C) JSValue { + pub fn writeUTF8(globalThis: *JSGlobalObject, callframe: *JSC.CallFrame) callconv(.C) JSValue { JSC.markBinding(); var this = getThis(globalThis, callframe) orelse return invalidThis(globalThis); @@ -1209,10 +1209,10 @@ pub fn NewJSSink(comptime SinkType: type, comptime name_: []const u8) type { const args_list = callframe.arguments(4); const args = args_list.ptr[0..args_list.len]; - if (args.len == 0 or args[0].isEmptyOrUndefinedOrNull() or args[0].isNumber()) { + if (args.len == 0 or !args[0].isString()) { const err = JSC.toTypeError( if (args.len == 0) JSC.Node.ErrorCode.ERR_MISSING_ARGS else JSC.Node.ErrorCode.ERR_INVALID_ARG_TYPE, - "write() expects a string, ArrayBufferView, or ArrayBuffer", + "writeUTF8() expects a string", .{}, globalThis, ); |