#include "config.h" #include "JSEventEmitter.h" #include "EventEmitter.h" #include "JSDOMWrapperCache.h" #include "JSEventListener.h" #include "ZigGlobalObject.h" #include "JSDOMConstructor.h" #include "JSDOMConvertBase.h" #include "JSDOMConvertBoolean.h" #include "JSDOMConvertDictionary.h" #include "JSDOMConvertInterface.h" #include "JSDOMConvertNullable.h" #include "JSDOMConvertNumbers.h" #include "JSDOMConvertSequences.h" #include "JSDOMConvertStrings.h" namespace WebCore { using namespace JSC; JSValue toJSNewlyCreated(JSGlobalObject*, JSDOMGlobalObject* globalObject, Ref&& value) { return createWrapper(globalObject, WTFMove(value)); } EventEmitter* JSEventEmitter::toWrapped(VM& vm, JSValue value) { if (value.inherits()) return &jsCast(asObject(value))->wrapped(); return nullptr; } std::unique_ptr jsEventEmitterCast(VM& vm, JSC::JSGlobalObject* lexicalGlobalObject, JSValue thisValue) { if (auto* emitter = jsEventEmitterCastFast(vm, lexicalGlobalObject, thisValue)) { return std::make_unique(emitter->wrapped(), asObject(thisValue)); } return nullptr; } JSEventEmitter* jsEventEmitterCastFast(VM& vm, JSC::JSGlobalObject* lexicalGlobalObject, JSValue thisValue) { if (thisValue.inherits()) return jsCast(asObject(thisValue)); if (UNLIKELY(thisValue.isUndefinedOrNull() || !thisValue.isObject())) { return nullptr; } auto* thisObject = asObject(thisValue); auto clientData = WebCore::clientData(vm); auto name = clientData->builtinNames()._eventsPublicName(); if (JSValue _events = thisObject->getIfPropertyExists(lexicalGlobalObject, name)) { if (!_events.isUndefinedOrNull() && _events.inherits()) { return jsCast(asObject(_events)); } } auto scope = DECLARE_CATCH_SCOPE(vm); auto* globalObject = reinterpret_cast(lexicalGlobalObject); auto impl = EventEmitter::create(*globalObject->scriptExecutionContext()); auto throwScope = DECLARE_THROW_SCOPE(vm); auto result = toJSNewlyCreated>(*lexicalGlobalObject, *globalObject, throwScope, WTFMove(impl)); thisObject->putDirect(vm, name, result, 0); if (scope.exception()) { scope.clearException(); return nullptr; } RETURN_IF_EXCEPTION(throwScope, nullptr); return jsCast(asObject(result)); } template void JSEventEmitter::visitAdditionalChildren(Visitor& visitor) { wrapped().eventListenerMap().visitJSEventListeners(visitor); } DEFINE_VISIT_ADDITIONAL_CHILDREN(JSEventEmitter); } // namespace WebCore