diff options
author | 2023-05-09 21:44:05 -0700 | |
---|---|---|
committer | 2023-05-09 21:44:05 -0700 | |
commit | c7c5dc14384f81a2d245870d2848c0512193690d (patch) | |
tree | 92da71a371cff795f0fd1b886fece062879af372 | |
parent | a5b3056c76268e4bf219f68a15827be5b564313b (diff) | |
download | bun-c7c5dc14384f81a2d245870d2848c0512193690d.tar.gz bun-c7c5dc14384f81a2d245870d2848c0512193690d.tar.zst bun-c7c5dc14384f81a2d245870d2848c0512193690d.zip |
Add `name` property
-rw-r--r-- | src/bun.js/bindings/ZigGeneratedClasses.cpp | 68 | ||||
-rw-r--r-- | src/bun.js/bindings/ZigGeneratedClasses.h | 6 | ||||
-rw-r--r-- | src/bun.js/bindings/generated_classes.zig | 26 | ||||
-rw-r--r-- | src/bun.js/webcore/blob.zig | 19 | ||||
-rw-r--r-- | src/bun.js/webcore/response.classes.ts | 13 |
5 files changed, 130 insertions, 2 deletions
diff --git a/src/bun.js/bindings/ZigGeneratedClasses.cpp b/src/bun.js/bindings/ZigGeneratedClasses.cpp index e259ea4fb..d13df9844 100644 --- a/src/bun.js/bindings/ZigGeneratedClasses.cpp +++ b/src/bun.js/bindings/ZigGeneratedClasses.cpp @@ -112,6 +112,9 @@ JSC_DECLARE_HOST_FUNCTION(BlobPrototype__jsonCallback); extern "C" JSC::EncodedJSValue BlobPrototype__getLastModified(void* ptr, JSC::JSGlobalObject* lexicalGlobalObject); JSC_DECLARE_CUSTOM_GETTER(BlobPrototype__lastModifiedGetterWrap); +extern "C" JSC::EncodedJSValue BlobPrototype__getName(void* ptr, JSC::JSGlobalObject* lexicalGlobalObject); +JSC_DECLARE_CUSTOM_GETTER(BlobPrototype__nameGetterWrap); + extern "C" JSC::EncodedJSValue BlobPrototype__getSize(void* ptr, JSC::JSGlobalObject* lexicalGlobalObject); JSC_DECLARE_CUSTOM_GETTER(BlobPrototype__sizeGetterWrap); @@ -137,6 +140,7 @@ static const HashTableValue JSBlobPrototypeTableValues[] = { { "formData"_s, static_cast<unsigned>(JSC::PropertyAttribute::Function | PropertyAttribute::DontDelete), NoIntrinsic, { HashTableValue::NativeFunctionType, BlobPrototype__formDataCallback, 0 } }, { "json"_s, static_cast<unsigned>(JSC::PropertyAttribute::Function | PropertyAttribute::DontDelete), NoIntrinsic, { HashTableValue::NativeFunctionType, BlobPrototype__jsonCallback, 0 } }, { "lastModified"_s, static_cast<unsigned>(JSC::PropertyAttribute::ReadOnly | JSC::PropertyAttribute::CustomAccessor | JSC::PropertyAttribute::DOMAttribute | PropertyAttribute::DontDelete), NoIntrinsic, { HashTableValue::GetterSetterType, BlobPrototype__lastModifiedGetterWrap, 0 } }, + { "name"_s, static_cast<unsigned>(JSC::PropertyAttribute::ReadOnly | JSC::PropertyAttribute::CustomAccessor | JSC::PropertyAttribute::DOMAttribute | PropertyAttribute::DontDelete), NoIntrinsic, { HashTableValue::GetterSetterType, BlobPrototype__nameGetterWrap, 0 } }, { "size"_s, static_cast<unsigned>(JSC::PropertyAttribute::ReadOnly | JSC::PropertyAttribute::CustomAccessor | JSC::PropertyAttribute::DOMAttribute | PropertyAttribute::DontDelete), NoIntrinsic, { HashTableValue::GetterSetterType, BlobPrototype__sizeGetterWrap, 0 } }, { "slice"_s, static_cast<unsigned>(JSC::PropertyAttribute::Function | PropertyAttribute::DontDelete), NoIntrinsic, { HashTableValue::NativeFunctionType, BlobPrototype__sliceCallback, 2 } }, { "stream"_s, static_cast<unsigned>(JSC::PropertyAttribute::Function | PropertyAttribute::DontDelete), NoIntrinsic, { HashTableValue::NativeFunctionType, BlobPrototype__streamCallback, 1 } }, @@ -219,6 +223,37 @@ JSC_DEFINE_CUSTOM_GETTER(BlobPrototype__lastModifiedGetterWrap, (JSGlobalObject RELEASE_AND_RETURN(throwScope, result); } +JSC_DEFINE_CUSTOM_GETTER(BlobPrototype__nameGetterWrap, (JSGlobalObject * lexicalGlobalObject, EncodedJSValue thisValue, PropertyName attributeName)) +{ + auto& vm = lexicalGlobalObject->vm(); + Zig::GlobalObject* globalObject = reinterpret_cast<Zig::GlobalObject*>(lexicalGlobalObject); + auto throwScope = DECLARE_THROW_SCOPE(vm); + JSBlob* thisObject = jsCast<JSBlob*>(JSValue::decode(thisValue)); + JSC::EnsureStillAliveScope thisArg = JSC::EnsureStillAliveScope(thisObject); + + if (JSValue cachedValue = thisObject->m_name.get()) + return JSValue::encode(cachedValue); + + JSC::JSValue result = JSC::JSValue::decode( + BlobPrototype__getName(thisObject->wrapped(), globalObject)); + RETURN_IF_EXCEPTION(throwScope, {}); + thisObject->m_name.set(vm, thisObject, result); + RELEASE_AND_RETURN(throwScope, JSValue::encode(result)); +} + +extern "C" void BlobPrototype__nameSetCachedValue(JSC::EncodedJSValue thisValue, JSC::JSGlobalObject* globalObject, JSC::EncodedJSValue value) +{ + auto& vm = globalObject->vm(); + auto* thisObject = jsCast<JSBlob*>(JSValue::decode(thisValue)); + thisObject->m_name.set(vm, thisObject, JSValue::decode(value)); +} + +extern "C" EncodedJSValue BlobPrototype__nameGetCachedValue(JSC::EncodedJSValue thisValue) +{ + auto* thisObject = jsCast<JSBlob*>(JSValue::decode(thisValue)); + return JSValue::encode(thisObject->m_name.get()); +} + JSC_DEFINE_CUSTOM_GETTER(BlobPrototype__sizeGetterWrap, (JSGlobalObject * lexicalGlobalObject, EncodedJSValue thisValue, PropertyName attributeName)) { auto& vm = lexicalGlobalObject->vm(); @@ -457,6 +492,39 @@ extern "C" EncodedJSValue Blob__create(Zig::GlobalObject* globalObject, void* pt return JSValue::encode(instance); } + +template<typename Visitor> +void JSBlob::visitChildrenImpl(JSCell* cell, Visitor& visitor) +{ + JSBlob* thisObject = jsCast<JSBlob*>(cell); + ASSERT_GC_OBJECT_INHERITS(thisObject, info()); + Base::visitChildren(thisObject, visitor); + + visitor.append(thisObject->m_name); +} + +DEFINE_VISIT_CHILDREN(JSBlob); + +template<typename Visitor> +void JSBlob::visitAdditionalChildren(Visitor& visitor) +{ + JSBlob* thisObject = this; + ASSERT_GC_OBJECT_INHERITS(thisObject, info()); + + visitor.append(thisObject->m_name); +} + +DEFINE_VISIT_ADDITIONAL_CHILDREN(JSBlob); + +template<typename Visitor> +void JSBlob::visitOutputConstraintsImpl(JSCell* cell, Visitor& visitor) +{ + JSBlob* thisObject = jsCast<JSBlob*>(cell); + ASSERT_GC_OBJECT_INHERITS(thisObject, info()); + thisObject->visitAdditionalChildren<Visitor>(visitor); +} + +DEFINE_VISIT_OUTPUT_CONSTRAINTS(JSBlob); class JSCryptoHasherPrototype final : public JSC::JSNonFinalObject { public: using Base = JSC::JSNonFinalObject; diff --git a/src/bun.js/bindings/ZigGeneratedClasses.h b/src/bun.js/bindings/ZigGeneratedClasses.h index 262e25809..86129ba51 100644 --- a/src/bun.js/bindings/ZigGeneratedClasses.h +++ b/src/bun.js/bindings/ZigGeneratedClasses.h @@ -63,6 +63,12 @@ public: } void finishCreation(JSC::VM&); + + DECLARE_VISIT_CHILDREN; + template<typename Visitor> void visitAdditionalChildren(Visitor&); + DECLARE_VISIT_OUTPUT_CONSTRAINTS; + + mutable JSC::WriteBarrier<JSC::Unknown> m_name; }; class JSCryptoHasher final : public JSC::JSDestructibleObject { diff --git a/src/bun.js/bindings/generated_classes.zig b/src/bun.js/bindings/generated_classes.zig index 857fbfe3c..eda0df453 100644 --- a/src/bun.js/bindings/generated_classes.zig +++ b/src/bun.js/bindings/generated_classes.zig @@ -37,6 +37,28 @@ pub const JSBlob = struct { return Blob__fromJS(value); } + extern fn BlobPrototype__nameSetCachedValue(JSC.JSValue, *JSC.JSGlobalObject, JSC.JSValue) void; + + extern fn BlobPrototype__nameGetCachedValue(JSC.JSValue) JSC.JSValue; + + /// `Blob.name` setter + /// This value will be visited by the garbage collector. + pub fn nameSetCached(thisValue: JSC.JSValue, globalObject: *JSC.JSGlobalObject, value: JSC.JSValue) void { + JSC.markBinding(@src()); + BlobPrototype__nameSetCachedValue(thisValue, globalObject, value); + } + + /// `Blob.name` getter + /// This value will be visited by the garbage collector. + pub fn nameGetCached(thisValue: JSC.JSValue) ?JSC.JSValue { + JSC.markBinding(@src()); + const result = BlobPrototype__nameGetCachedValue(thisValue); + if (result == .zero) + return null; + + return result; + } + /// Get the Blob constructor value. /// This loads lazily from the global object. pub fn getConstructor(globalObject: *JSC.JSGlobalObject) JSC.JSValue { @@ -93,6 +115,9 @@ pub const JSBlob = struct { if (@TypeOf(Blob.getLastModified) != GetterType) @compileLog("Expected Blob.getLastModified to be a getter"); + if (@TypeOf(Blob.getName) != GetterType) + @compileLog("Expected Blob.getName to be a getter"); + if (@TypeOf(Blob.getSize) != GetterType) @compileLog("Expected Blob.getSize to be a getter"); @@ -114,6 +139,7 @@ pub const JSBlob = struct { @export(Blob.getFormData, .{ .name = "BlobPrototype__getFormData" }); @export(Blob.getJSON, .{ .name = "BlobPrototype__getJSON" }); @export(Blob.getLastModified, .{ .name = "BlobPrototype__getLastModified" }); + @export(Blob.getName, .{ .name = "BlobPrototype__getName" }); @export(Blob.getSize, .{ .name = "BlobPrototype__getSize" }); @export(Blob.getSlice, .{ .name = "BlobPrototype__getSlice" }); @export(Blob.getStream, .{ .name = "BlobPrototype__getStream" }); diff --git a/src/bun.js/webcore/blob.zig b/src/bun.js/webcore/blob.zig index f0692d482..4005a6bcc 100644 --- a/src/bun.js/webcore/blob.zig +++ b/src/bun.js/webcore/blob.zig @@ -2544,6 +2544,25 @@ pub const Blob = struct { return ZigString.Empty.toValue(globalThis); } + // TODO: Move this to a separate `File` object or BunFile + pub fn getName( + this: *Blob, + globalThis: *JSC.JSGlobalObject, + ) callconv(.C) JSValue { + if (this.store) |store| { + if (store.data == .file) { + if (store.data.file.pathlike == .path) { + return ZigString.fromUTF8(store.data.file.pathlike.path.slice()).toValueGC(globalThis); + } + + // we shouldn't return Number here. + } + } + + return JSC.JSValue.jsUndefined(); + } + + // TODO: Move this to a separate `File` object or BunFile pub fn getLastModified( this: *Blob, _: *JSC.JSGlobalObject, diff --git a/src/bun.js/webcore/response.classes.ts b/src/bun.js/webcore/response.classes.ts index 5f3ba2e4a..b6ad452d2 100644 --- a/src/bun.js/webcore/response.classes.ts +++ b/src/bun.js/webcore/response.classes.ts @@ -137,14 +137,23 @@ export default [ getter: "getType", }, - size: { - getter: "getSize", + // TODO: Move this to a separate `File` object or BunFile + // This is *not* spec-compliant. + name: { + getter: "getName", + cache: true, }, + // TODO: Move this to a separate `File` object or BunFile + // This is *not* spec-compliant. lastModified: { getter: "getLastModified", }, + size: { + getter: "getSize", + }, + writer: { fn: "getWriter", length: 1, |