Expose some no-ops (#16125)

Co-authored-by: Jarred-Sumner <Jarred-Sumner@users.noreply.github.com>
This commit is contained in:
Jarred Sumner
2025-01-03 13:57:46 -08:00
committed by GitHub
parent c130df6c58
commit 912a2cbc12
5 changed files with 53 additions and 31 deletions

View File

@@ -1,20 +1,14 @@
import { noOpForTesting as noop } from "bun:internal-for-testing";
import { bench, run } from "../runner.mjs";
// These are no-op C++ functions that are exported to JS.
const lazy = globalThis[Symbol.for("Bun.lazy")];
const noop = lazy("noop");
const fn = noop.function;
const regular = noop.functionRegular;
const callback = noop.callback;
bench("C++ callback into JS", () => {
callback(() => {});
});
bench("C++ fn regular", () => {
regular();
});
bench("C++ fn", () => {
fn();
});

View File

@@ -0,0 +1,47 @@
#include "root.h"
#include "JavaScriptCore/CustomGetterSetter.h"
#include "JavaScriptCore/ObjectConstructor.h"
#include "JavaScriptCore/JSObject.h"
#include <JavaScriptCore/JSFunction.h>
namespace Bun {
using namespace JSC;
JSC_DEFINE_HOST_FUNCTION(functionNoop, (JSC::JSGlobalObject*, JSC::CallFrame*))
{
return JSC::JSValue::encode(JSC::jsUndefined());
}
JSC_DEFINE_HOST_FUNCTION(functionCallback, (JSC::JSGlobalObject * globalObject, JSC::CallFrame* callFrame))
{
JSObject* callback = jsCast<JSObject*>(callFrame->uncheckedArgument(0));
JSC::CallData callData = JSC::getCallData(callback);
return JSC::JSValue::encode(JSC::profiledCall(globalObject, ProfilingReason::API, callback, callData, JSC::jsUndefined(), JSC::MarkedArgumentBuffer()));
}
JSC_DEFINE_CUSTOM_GETTER(noop_getter, (JSGlobalObject*, EncodedJSValue, PropertyName))
{
return JSC::JSValue::encode(JSC::jsUndefined());
}
JSC_DEFINE_CUSTOM_SETTER(noop_setter,
(JSC::JSGlobalObject*, JSC::EncodedJSValue,
JSC::EncodedJSValue, JSC::PropertyName))
{
return true;
}
JSC::JSObject* createNoOpForTesting(JSC::JSGlobalObject* globalObject)
{
auto& vm = globalObject->vm();
JSC::JSObject* object = JSC::constructEmptyObject(vm, globalObject->nullPrototypeObjectStructure());
object->putDirectNativeFunction(vm, globalObject, JSC::Identifier::fromString(vm, String("function"_s)), 0, functionNoop, ImplementationVisibility::Public, JSC::NoIntrinsic, 0);
object->putDirectNativeFunction(vm, globalObject, JSC::Identifier::fromString(vm, String("callback"_s)), 0, functionCallback, ImplementationVisibility::Public, JSC::NoIntrinsic, 0);
object->putDirectCustomAccessor(vm, JSC::Identifier::fromString(vm, String("getterSetter"_s)), JSC::CustomGetterSetter::create(vm, noop_getter, noop_setter), 0);
return object;
}
}

View File

@@ -0,0 +1,3 @@
namespace Bun {
JSC::JSObject* createNoOpForTesting(JSC::JSGlobalObject* globalObject);
}

View File

@@ -1883,30 +1883,6 @@ JSC_DEFINE_HOST_FUNCTION(functionCreateUninitializedArrayBuffer,
RELEASE_AND_RETURN(scope, JSValue::encode(JSC::JSArrayBuffer::create(globalObject->vm(), globalObject->arrayBufferStructure(JSC::ArrayBufferSharingMode::Default), WTFMove(arrayBuffer))));
}
JSC_DEFINE_HOST_FUNCTION(functionNoop, (JSC::JSGlobalObject*, JSC::CallFrame*))
{
return JSC::JSValue::encode(JSC::jsUndefined());
}
JSC_DEFINE_HOST_FUNCTION(functionCallback, (JSC::JSGlobalObject * globalObject, JSC::CallFrame* callFrame))
{
JSFunction* callback = jsCast<JSFunction*>(callFrame->uncheckedArgument(0));
JSC::CallData callData = JSC::getCallData(callback);
return JSC::JSValue::encode(JSC::profiledCall(globalObject, ProfilingReason::API, callback, callData, JSC::jsUndefined(), JSC::MarkedArgumentBuffer()));
}
JSC_DEFINE_CUSTOM_GETTER(noop_getter, (JSGlobalObject*, EncodedJSValue, PropertyName))
{
return JSC::JSValue::encode(JSC::jsUndefined());
}
JSC_DEFINE_CUSTOM_SETTER(noop_setter,
(JSC::JSGlobalObject*, JSC::EncodedJSValue,
JSC::EncodedJSValue, JSC::PropertyName))
{
return true;
}
static inline JSC::EncodedJSValue jsFunctionAddEventListenerBody(JSC::JSGlobalObject* lexicalGlobalObject, JSC::CallFrame* callFrame, Zig::GlobalObject* castedThis)
{
auto& vm = JSC::getVM(lexicalGlobalObject);

View File

@@ -149,3 +149,5 @@ export const bindgen = $zig("bindgen_test.zig", "getBindgenTestFunctions") as {
add: (a: any, b: any) => number;
requiredAndOptionalArg: (a: any, b?: any, c?: any, d?: any) => number;
};
export const noOpForTesting = $cpp("NoOpForTesting.cpp", "createNoOpForTesting");