mirror of
https://github.com/oven-sh/bun
synced 2026-02-16 05:42:43 +00:00
* Use debug mode by default * Enable build with assertions enabled * Update cli.zig * Update bun-linux-build.yml * Fixes * Fix `ASSERT_ENABLED` * try this * Update Dockerfile * mimalloc debug * Update CMakeLists.txt * `Bun.deepMatch` - fix assertion failures cc @dylan-conway, looks like we need to use `putDirectMayBeIndex` and check for `isCell` more carefully. * Object.create support in code generator and callbacks wrapper * Remove unused file * zig upgrade * zls * Fix various errors * Support `BuiltinAccessor` in create_hash_table script * Fix assertion failure in `process.mainModule` * Fix assertion failure in `onerror` * Fix assertion failure when creating a Worker * Fix asssertion failure when loading lots of files in bun test * Fix assertion failure when termating a `Worker` * Add helper for converting BunString to a WTFString * Fix assertion failure in notifyNeedTermination * Add more debug logs in `bun test` * Fix compiler warning in usockets * Fix assertion failure with `Worker` termination (another) * Fix assertion failure in `coerceToInt64` * Fix assertion failure in `BroadcastChannel` * Fix assertion failure in `Headers.prototype.getAll` * Fixes #7067 * Add heap analyzer label for CommonJS modules * Fix assertion failure in module.require && module.require.resolve * Remove unused code * Fix assertion failure in debugger * Fix crash in debugger * Fix assertion failures in bun:sqlite * Bump zig * Bump WebKit * Fix assertion failure in JSPromise::reject && JSInternalPromise::reject * Fix assertion failure in ReadableStream::cancel * Fix assertion failure in AsyncContextFrame::create * Fix assertion failure in bun:sqlite * Fix assertion failure in mocks * Fix assertion failure in ServerWebSocket.close * Fix assertion failure in N-API with subclasses * [napi] Make promises cheaper * undo * Don't check for exceptions in ObjectInitializationScope * Add separate entry point for test runner that doesn't generate code * Don't deref builtin code * Fix preload test * Fix assertion failure in memoryUsage() * Fix preload test, part 2 * Ensure that the env map for a Worker is empty after it is used * The pointer for the Arena allocator used in parsing should not change * Terminate thread on exit * Start to implement scriptExecutionStatus * Update worker.test.ts * Fix Dirent.name setter * Update settings.json * Fix assertion failure in node:http * Use correct value for `JSFinalObject::maxInlineCapacity` * JSFinalObject::maxInlineCapacity x2 * Don't strip when assertions are enabled * Make `m_wasTerminated` atomic * Preserve directives in the transpiler cc @ctjlewis * Workaround assertion failure in ServerWebSocket.sendBinary and ServerWebSocket.sendText * windows * Buffer lockfile serialization in-memory * PR feedback * PR feedback * PR feedback * Windows * quotes * Update CMakeLists.txt * Update bun-linux-build.yml * Update bun-linux-build.yml * Move this code to BunString.cpp * Update BunString.cpp --------- Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com>
248 lines
11 KiB
C++
248 lines
11 KiB
C++
/*
|
|
* Copyright (C) 2017-2021 Apple Inc. All rights reserved.
|
|
*
|
|
* Redistribution and use in source and binary forms, with or without
|
|
* modification, are permitted provided that the following conditions
|
|
* are met:
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
* notice, this list of conditions and the following disclaimer.
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
* documentation and/or other materials provided with the distribution.
|
|
*
|
|
* THIS SOFTWARE IS PROVIDED BY CANON INC. ``AS IS'' AND ANY
|
|
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CANON INC. OR
|
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
|
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
*/
|
|
|
|
#include "config.h"
|
|
#include "ReadableStream.h"
|
|
|
|
#include "Exception.h"
|
|
#include "ExceptionCode.h"
|
|
#include "JSDOMConvertSequences.h"
|
|
#include "JSReadableStreamSink.h"
|
|
#include "JSReadableStreamSource.h"
|
|
#include "WebCoreJSClientData.h"
|
|
|
|
namespace WebCore {
|
|
using namespace JSC;
|
|
|
|
static inline ExceptionOr<JSObject*> invokeConstructor(JSC::JSGlobalObject& lexicalGlobalObject, const JSC::Identifier& identifier, const Function<void(MarkedArgumentBuffer&, JSC::JSGlobalObject&, JSDOMGlobalObject&)>& buildArguments)
|
|
{
|
|
VM& vm = lexicalGlobalObject.vm();
|
|
auto scope = DECLARE_THROW_SCOPE(vm);
|
|
|
|
auto& globalObject = *JSC::jsCast<JSDOMGlobalObject*>(&lexicalGlobalObject);
|
|
|
|
auto constructorValue = globalObject.get(&lexicalGlobalObject, identifier);
|
|
EXCEPTION_ASSERT(!scope.exception() || vm.hasPendingTerminationException());
|
|
RETURN_IF_EXCEPTION(scope, Exception { ExistingExceptionError });
|
|
auto constructor = JSC::asObject(constructorValue);
|
|
|
|
auto constructData = JSC::getConstructData(constructor);
|
|
ASSERT(constructData.type != CallData::Type::None);
|
|
|
|
MarkedArgumentBuffer args;
|
|
buildArguments(args, lexicalGlobalObject, globalObject);
|
|
ASSERT(!args.hasOverflowed());
|
|
|
|
JSObject* object = JSC::construct(&lexicalGlobalObject, constructor, constructData, args);
|
|
ASSERT(!!scope.exception() == !object);
|
|
EXCEPTION_ASSERT(!scope.exception() || vm.hasPendingTerminationException());
|
|
RETURN_IF_EXCEPTION(scope, Exception { ExistingExceptionError });
|
|
|
|
return object;
|
|
}
|
|
|
|
ExceptionOr<Ref<ReadableStream>> ReadableStream::create(JSC::JSGlobalObject& lexicalGlobalObject, RefPtr<ReadableStreamSource>&& source)
|
|
{
|
|
auto& builtinNames = WebCore::builtinNames(lexicalGlobalObject.vm());
|
|
|
|
auto objectOrException = invokeConstructor(lexicalGlobalObject, builtinNames.ReadableStreamPrivateName(), [&source](auto& args, auto& lexicalGlobalObject, auto& globalObject) {
|
|
args.append(source ? toJSNewlyCreated(&lexicalGlobalObject, &globalObject, source.releaseNonNull()) : JSC::jsUndefined());
|
|
});
|
|
|
|
if (objectOrException.hasException())
|
|
return objectOrException.releaseException();
|
|
|
|
return create(*JSC::jsCast<JSDOMGlobalObject*>(&lexicalGlobalObject), *jsCast<JSReadableStream*>(objectOrException.releaseReturnValue()));
|
|
}
|
|
|
|
ExceptionOr<Ref<ReadableStream>> ReadableStream::create(JSC::JSGlobalObject& lexicalGlobalObject, RefPtr<ReadableStreamSource>&& source, JSC::JSValue nativePtr)
|
|
{
|
|
auto& builtinNames = WebCore::builtinNames(lexicalGlobalObject.vm());
|
|
RELEASE_ASSERT(source != nullptr);
|
|
|
|
auto objectOrException = invokeConstructor(lexicalGlobalObject, builtinNames.ReadableStreamPrivateName(), [&source, nativePtr](auto& args, auto& lexicalGlobalObject, auto& globalObject) {
|
|
auto sourceStream = toJSNewlyCreated(&lexicalGlobalObject, &globalObject, source.releaseNonNull());
|
|
auto tag = WebCore::clientData(lexicalGlobalObject.vm())->builtinNames().bunNativePtrPrivateName();
|
|
sourceStream.getObject()->putDirect(lexicalGlobalObject.vm(), tag, nativePtr, JSC::PropertyAttribute::DontDelete | JSC::PropertyAttribute::DontEnum);
|
|
args.append(sourceStream);
|
|
});
|
|
|
|
if (objectOrException.hasException())
|
|
return objectOrException.releaseException();
|
|
|
|
return create(*JSC::jsCast<JSDOMGlobalObject*>(&lexicalGlobalObject), *jsCast<JSReadableStream*>(objectOrException.releaseReturnValue()));
|
|
}
|
|
|
|
static inline std::optional<JSC::JSValue> invokeReadableStreamFunction(JSC::JSGlobalObject& lexicalGlobalObject, const JSC::Identifier& identifier, JSC::JSValue thisValue, const JSC::MarkedArgumentBuffer& arguments)
|
|
{
|
|
JSC::VM& vm = lexicalGlobalObject.vm();
|
|
JSC::JSLockHolder lock(vm);
|
|
|
|
auto function = lexicalGlobalObject.get(&lexicalGlobalObject, identifier);
|
|
ASSERT(function.isCallable());
|
|
|
|
auto scope = DECLARE_CATCH_SCOPE(vm);
|
|
auto callData = JSC::getCallData(function);
|
|
auto result = call(&lexicalGlobalObject, function, callData, thisValue, arguments);
|
|
EXCEPTION_ASSERT(!scope.exception() || vm.hasPendingTerminationException());
|
|
if (scope.exception())
|
|
return {};
|
|
return result;
|
|
}
|
|
|
|
void ReadableStream::pipeTo(ReadableStreamSink& sink)
|
|
{
|
|
auto& lexicalGlobalObject = *m_globalObject;
|
|
auto* clientData = static_cast<JSVMClientData*>(lexicalGlobalObject.vm().clientData);
|
|
auto& privateName = clientData->builtinFunctions().readableStreamInternalsBuiltins().readableStreamPipeToPrivateName();
|
|
|
|
MarkedArgumentBuffer arguments;
|
|
arguments.append(readableStream());
|
|
arguments.append(toJS(&lexicalGlobalObject, m_globalObject.get(), sink));
|
|
ASSERT(!arguments.hasOverflowed());
|
|
invokeReadableStreamFunction(lexicalGlobalObject, privateName, JSC::jsUndefined(), arguments);
|
|
}
|
|
|
|
std::optional<std::pair<Ref<ReadableStream>, Ref<ReadableStream>>> ReadableStream::tee()
|
|
{
|
|
auto& lexicalGlobalObject = *m_globalObject;
|
|
auto* clientData = static_cast<JSVMClientData*>(lexicalGlobalObject.vm().clientData);
|
|
auto& privateName = clientData->builtinFunctions().readableStreamInternalsBuiltins().readableStreamTeePrivateName();
|
|
|
|
MarkedArgumentBuffer arguments;
|
|
arguments.append(readableStream());
|
|
arguments.append(JSC::jsBoolean(true));
|
|
ASSERT(!arguments.hasOverflowed());
|
|
auto returnedValue = invokeReadableStreamFunction(lexicalGlobalObject, privateName, JSC::jsUndefined(), arguments);
|
|
if (!returnedValue)
|
|
return {};
|
|
|
|
auto results = Detail::SequenceConverter<IDLInterface<ReadableStream>>::convert(lexicalGlobalObject, *returnedValue);
|
|
|
|
ASSERT(results.size() == 2);
|
|
return std::make_pair(results[0].releaseNonNull(), results[1].releaseNonNull());
|
|
}
|
|
|
|
void ReadableStream::lock()
|
|
{
|
|
auto& builtinNames = WebCore::builtinNames(m_globalObject->vm());
|
|
invokeConstructor(*m_globalObject, builtinNames.ReadableStreamDefaultReaderPrivateName(), [this](auto& args, auto&, auto&) {
|
|
args.append(readableStream());
|
|
});
|
|
}
|
|
|
|
void ReadableStream::cancel(const Exception& exception)
|
|
{
|
|
auto& lexicalGlobalObject = *m_globalObject;
|
|
auto* clientData = static_cast<JSVMClientData*>(lexicalGlobalObject.vm().clientData);
|
|
auto& privateName = clientData->builtinFunctions().readableStreamInternalsBuiltins().readableStreamCancelPrivateName();
|
|
|
|
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(vm.hasPendingTerminationException());
|
|
return;
|
|
}
|
|
|
|
MarkedArgumentBuffer arguments;
|
|
arguments.append(readableStream());
|
|
arguments.append(value);
|
|
ASSERT(!arguments.hasOverflowed());
|
|
invokeReadableStreamFunction(lexicalGlobalObject, privateName, JSC::jsUndefined(), arguments);
|
|
}
|
|
|
|
void ReadableStream::cancel(WebCore::JSDOMGlobalObject& globalObject, JSReadableStream* readableStream, const Exception& exception)
|
|
{
|
|
auto* clientData = static_cast<JSVMClientData*>(globalObject.vm().clientData);
|
|
auto& privateName = clientData->builtinFunctions().readableStreamInternalsBuiltins().readableStreamCancelPrivateName();
|
|
|
|
auto& vm = globalObject.vm();
|
|
JSC::JSLockHolder lock(vm);
|
|
auto scope = DECLARE_CATCH_SCOPE(vm);
|
|
auto value = createDOMException(&globalObject, exception.code(), exception.message());
|
|
if (UNLIKELY(scope.exception())) {
|
|
ASSERT(vm.hasPendingTerminationException());
|
|
return;
|
|
}
|
|
|
|
MarkedArgumentBuffer arguments;
|
|
arguments.append(readableStream);
|
|
arguments.append(value);
|
|
ASSERT(!arguments.hasOverflowed());
|
|
invokeReadableStreamFunction(globalObject, privateName, JSC::jsUndefined(), arguments);
|
|
}
|
|
|
|
static inline bool checkReadableStream(JSDOMGlobalObject& globalObject, JSReadableStream* readableStream, JSC::JSValue function)
|
|
{
|
|
auto& lexicalGlobalObject = globalObject;
|
|
|
|
ASSERT(function);
|
|
JSC::MarkedArgumentBuffer arguments;
|
|
arguments.append(readableStream);
|
|
ASSERT(!arguments.hasOverflowed());
|
|
|
|
auto& vm = lexicalGlobalObject.vm();
|
|
auto scope = DECLARE_CATCH_SCOPE(vm);
|
|
auto callData = JSC::getCallData(function);
|
|
ASSERT(callData.type != JSC::CallData::Type::None);
|
|
|
|
auto result = call(&lexicalGlobalObject, function, callData, JSC::jsUndefined(), arguments);
|
|
EXCEPTION_ASSERT(!scope.exception() || vm.hasPendingTerminationException());
|
|
|
|
return result.isTrue() || scope.exception();
|
|
}
|
|
|
|
bool ReadableStream::isLocked() const
|
|
{
|
|
auto clientData = WebCore::clientData(m_globalObject->vm());
|
|
auto& privateName = clientData->builtinNames().readerPrivateName();
|
|
return readableStream()->getDirect(m_globalObject->vm(), privateName).isTrue();
|
|
}
|
|
|
|
bool ReadableStream::isLocked(JSGlobalObject* globalObject, JSReadableStream* readableStream)
|
|
{
|
|
auto clientData = WebCore::clientData(globalObject->vm());
|
|
auto& privateName = clientData->builtinNames().readerPrivateName();
|
|
return readableStream->getDirect(globalObject->vm(), privateName).isTrue();
|
|
}
|
|
|
|
bool ReadableStream::isDisturbed(JSGlobalObject* globalObject, JSReadableStream* readableStream)
|
|
{
|
|
auto clientData = WebCore::clientData(globalObject->vm());
|
|
auto& privateName = clientData->builtinNames().disturbedPrivateName();
|
|
return readableStream->getDirect(globalObject->vm(), privateName).isTrue();
|
|
}
|
|
|
|
bool ReadableStream::isDisturbed() const
|
|
{
|
|
auto clientData = WebCore::clientData(globalObject()->vm());
|
|
auto& privateName = clientData->builtinNames().disturbedPrivateName();
|
|
return readableStream()->getDirect(globalObject()->vm(), privateName).isTrue();
|
|
}
|
|
|
|
}
|