ReadableStreamDefaultController.cpp [plain text]
#include "config.h"
#include "ReadableStreamDefaultController.h"
#include "WebCoreJSClientData.h"
#include <JavaScriptCore/CatchScope.h>
#include <JavaScriptCore/HeapInlines.h>
#include <JavaScriptCore/IdentifierInlines.h>
#include <JavaScriptCore/JSObjectInlines.h>
namespace WebCore {
static bool invokeReadableStreamDefaultControllerFunction(JSC::JSGlobalObject& lexicalGlobalObject, const JSC::Identifier& identifier, const JSC::MarkedArgumentBuffer& arguments)
{
JSC::VM& vm = lexicalGlobalObject.vm();
JSC::JSLockHolder lock(vm);
auto function = lexicalGlobalObject.get(&lexicalGlobalObject, identifier);
ASSERT(function.isCallable(lexicalGlobalObject.vm()));
auto scope = DECLARE_CATCH_SCOPE(vm);
auto callData = JSC::getCallData(vm, function);
call(&lexicalGlobalObject, function, callData, JSC::jsUndefined(), arguments);
EXCEPTION_ASSERT(!scope.exception() || isTerminatedExecutionException(lexicalGlobalObject.vm(), scope.exception()));
return !scope.exception();
}
void ReadableStreamDefaultController::close()
{
JSC::MarkedArgumentBuffer arguments;
arguments.append(&jsController());
auto* clientData = static_cast<JSVMClientData*>(globalObject().vm().clientData);
auto& privateName = clientData->builtinFunctions().readableStreamInternalsBuiltins().readableStreamDefaultControllerClosePrivateName();
invokeReadableStreamDefaultControllerFunction(globalObject(), privateName, arguments);
}
void ReadableStreamDefaultController::error(const Exception& exception)
{
JSC::JSGlobalObject& lexicalGlobalObject = this->globalObject();
auto& vm = lexicalGlobalObject.vm();
JSC::JSLockHolder lock(vm);
auto scope = DECLARE_CATCH_SCOPE(vm);
auto value = createDOMException(&lexicalGlobalObject, exception.code(), exception.message());
if (UNLIKELY(scope.exception())) {
ASSERT(isTerminatedExecutionException(lexicalGlobalObject.vm(), scope.exception()));
return;
}
JSC::MarkedArgumentBuffer arguments;
arguments.append(&jsController());
arguments.append(value);
auto* clientData = static_cast<JSVMClientData*>(lexicalGlobalObject.vm().clientData);
auto& privateName = clientData->builtinFunctions().readableStreamInternalsBuiltins().readableStreamDefaultControllerErrorPrivateName();
invokeReadableStreamDefaultControllerFunction(globalObject(), privateName, arguments);
}
bool ReadableStreamDefaultController::enqueue(RefPtr<JSC::ArrayBuffer>&& buffer)
{
if (!buffer) {
error(Exception { OutOfMemoryError });
return false;
}
JSC::JSGlobalObject& lexicalGlobalObject = this->globalObject();
auto& vm = lexicalGlobalObject.vm();
JSC::JSLockHolder lock(vm);
auto scope = DECLARE_CATCH_SCOPE(vm);
auto length = buffer->byteLength();
auto chunk = JSC::Uint8Array::create(WTFMove(buffer), 0, length);
auto value = toJS(&lexicalGlobalObject, &lexicalGlobalObject, chunk.get());
if (UNLIKELY(scope.exception())) {
ASSERT(isTerminatedExecutionException(lexicalGlobalObject.vm(), scope.exception()));
return false;
}
JSC::MarkedArgumentBuffer arguments;
arguments.append(&jsController());
arguments.append(value);
auto* clientData = static_cast<JSVMClientData*>(lexicalGlobalObject.vm().clientData);
auto& privateName = clientData->builtinFunctions().readableStreamInternalsBuiltins().readableStreamDefaultControllerEnqueuePrivateName();
return invokeReadableStreamDefaultControllerFunction(globalObject(), privateName, arguments);
}
}