aboutsummaryrefslogtreecommitdiff
path: root/src/bun.js/bindings/JSFFIFunction.cpp
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <jarred@jarredsumner.com> 2023-08-16 22:10:01 -0700
committerGravatar GitHub <noreply@github.com> 2023-08-16 22:10:01 -0700
commit6c3dabd84e98f6bbb3ce8d4885d1b2d0421df52b (patch)
treedd13e91707acb50160781bc6d77110cc30d27a83 /src/bun.js/bindings/JSFFIFunction.cpp
parentd4438e94964fb006b2939e269945f0e5825ced7d (diff)
downloadbun-6c3dabd84e98f6bbb3ce8d4885d1b2d0421df52b.tar.gz
bun-6c3dabd84e98f6bbb3ce8d4885d1b2d0421df52b.tar.zst
bun-6c3dabd84e98f6bbb3ce8d4885d1b2d0421df52b.zip
Fix leaking .ptr (#4181)
Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com>
Diffstat (limited to 'src/bun.js/bindings/JSFFIFunction.cpp')
-rw-r--r--src/bun.js/bindings/JSFFIFunction.cpp14
1 files changed, 10 insertions, 4 deletions
diff --git a/src/bun.js/bindings/JSFFIFunction.cpp b/src/bun.js/bindings/JSFFIFunction.cpp
index a0cd83ba6..71a53be4c 100644
--- a/src/bun.js/bindings/JSFFIFunction.cpp
+++ b/src/bun.js/bindings/JSFFIFunction.cpp
@@ -113,10 +113,17 @@ extern "C" void Bun__untrackFFIFunction(Zig::GlobalObject* globalObject, JSC::En
{
globalObject->untrackFFIFunction(JSC::jsCast<JSC::JSFunction*>(JSC::JSValue::decode(function)));
}
-extern "C" JSC::EncodedJSValue Bun__CreateFFIFunctionValue(Zig::GlobalObject* globalObject, const ZigString* symbolName, unsigned argCount, Zig::FFIFunction functionPointer, bool strong);
-extern "C" JSC::EncodedJSValue Bun__CreateFFIFunctionValue(Zig::GlobalObject* globalObject, const ZigString* symbolName, unsigned argCount, Zig::FFIFunction functionPointer, bool strong)
+extern "C" JSC::EncodedJSValue Bun__CreateFFIFunctionValue(Zig::GlobalObject* globalObject, const ZigString* symbolName, unsigned argCount, Zig::FFIFunction functionPointer, bool strong, bool addPtrField)
{
- return JSC::JSValue::encode(JSC::JSValue(Bun__CreateFFIFunction(globalObject, symbolName, argCount, functionPointer, strong)));
+ auto* function = Bun__CreateFFIFunction(globalObject, symbolName, argCount, functionPointer, strong);
+ if (addPtrField) {
+ auto& vm = globalObject->vm();
+ // We should only expose the "ptr" field when it's a JSCallback for bun:ffi.
+ // Not for internal usages of this function type.
+ // We should also consider a separate JSFunction type for our usage to not have this branch in the first place...
+ function->putDirect(vm, JSC::Identifier::fromString(vm, String(MAKE_STATIC_STRING_IMPL("ptr"))), JSC::jsNumber(bitwise_cast<double>(functionPointer)), JSC::PropertyAttribute::ReadOnly | 0);
+ }
+ return JSC::JSValue::encode(function);
}
namespace Zig {
@@ -145,7 +152,6 @@ DEFINE_VISIT_CHILDREN(JSFFIFunction);
void JSFFIFunction::finishCreation(VM& vm, NativeExecutable* executable, unsigned length, const String& name)
{
Base::finishCreation(vm, executable, length, name);
- this->putDirect(vm, JSC::Identifier::fromString(vm, String(MAKE_STATIC_STRING_IMPL("ptr"))), jsNumber(bitwise_cast<double>(this->m_function)), JSC::PropertyAttribute::ReadOnly | 0);
ASSERT(inherits(info()));
}