From 912a2cbc120445706ce5cde6a7f32a66627448ac Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Fri, 3 Jan 2025 13:57:46 -0800 Subject: [PATCH] Expose some no-ops (#16125) Co-authored-by: Jarred-Sumner --- bench/snippets/native-overhead.mjs | 8 +---- src/bun.js/bindings/NoOpForTesting.cpp | 47 +++++++++++++++++++++++++ src/bun.js/bindings/NoOpForTesting.h | 3 ++ src/bun.js/bindings/ZigGlobalObject.cpp | 24 ------------- src/js/internal-for-testing.ts | 2 ++ 5 files changed, 53 insertions(+), 31 deletions(-) create mode 100644 src/bun.js/bindings/NoOpForTesting.cpp create mode 100644 src/bun.js/bindings/NoOpForTesting.h diff --git a/bench/snippets/native-overhead.mjs b/bench/snippets/native-overhead.mjs index 32d459247e..43576b21d4 100644 --- a/bench/snippets/native-overhead.mjs +++ b/bench/snippets/native-overhead.mjs @@ -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(); }); diff --git a/src/bun.js/bindings/NoOpForTesting.cpp b/src/bun.js/bindings/NoOpForTesting.cpp new file mode 100644 index 0000000000..919cd5b5f8 --- /dev/null +++ b/src/bun.js/bindings/NoOpForTesting.cpp @@ -0,0 +1,47 @@ + + +#include "root.h" + +#include "JavaScriptCore/CustomGetterSetter.h" +#include "JavaScriptCore/ObjectConstructor.h" +#include "JavaScriptCore/JSObject.h" +#include + +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(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; +} + +} diff --git a/src/bun.js/bindings/NoOpForTesting.h b/src/bun.js/bindings/NoOpForTesting.h new file mode 100644 index 0000000000..e39e84daa8 --- /dev/null +++ b/src/bun.js/bindings/NoOpForTesting.h @@ -0,0 +1,3 @@ +namespace Bun { +JSC::JSObject* createNoOpForTesting(JSC::JSGlobalObject* globalObject); +} diff --git a/src/bun.js/bindings/ZigGlobalObject.cpp b/src/bun.js/bindings/ZigGlobalObject.cpp index e718a28527..1889b0c9be 100644 --- a/src/bun.js/bindings/ZigGlobalObject.cpp +++ b/src/bun.js/bindings/ZigGlobalObject.cpp @@ -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(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); diff --git a/src/js/internal-for-testing.ts b/src/js/internal-for-testing.ts index 0cfaa5507e..893ff59006 100644 --- a/src/js/internal-for-testing.ts +++ b/src/js/internal-for-testing.ts @@ -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");