mirror of
https://github.com/oven-sh/bun
synced 2026-02-18 23:01:58 +00:00
more child-process (#18688)
Co-authored-by: pfgithub <6010774+pfgithub@users.noreply.github.com>
This commit is contained in:
@@ -25,6 +25,7 @@
|
||||
#include "JavaScriptCore/ErrorInstanceInlines.h"
|
||||
#include "JavaScriptCore/JSInternalFieldObjectImplInlines.h"
|
||||
#include "JSDOMException.h"
|
||||
#include "JSDOMExceptionHandling.h"
|
||||
#include <openssl/err.h>
|
||||
#include "ErrorCode.h"
|
||||
#include "ErrorStackTrace.h"
|
||||
@@ -100,37 +101,30 @@ namespace Bun {
|
||||
using namespace JSC;
|
||||
using namespace WTF;
|
||||
|
||||
static JSC::JSObject* createErrorPrototype(JSC::VM& vm, JSC::JSGlobalObject* globalObject, JSC::ErrorType type, WTF::ASCIILiteral name, WTF::ASCIILiteral code, bool isDOMExceptionPrototype)
|
||||
static JSC::JSObject* createErrorPrototype(JSC::VM& vm, JSC::JSGlobalObject* globalObject, JSC::ErrorType type, WTF::ASCIILiteral name, WTF::ASCIILiteral code)
|
||||
{
|
||||
JSC::JSObject* prototype;
|
||||
|
||||
// Inherit from DOMException
|
||||
// But preserve the error.stack property.
|
||||
if (isDOMExceptionPrototype) {
|
||||
auto* domGlobalObject = defaultGlobalObject(globalObject);
|
||||
prototype = JSC::constructEmptyObject(globalObject, WebCore::JSDOMException::prototype(vm, *domGlobalObject));
|
||||
} else {
|
||||
switch (type) {
|
||||
case JSC::ErrorType::TypeError:
|
||||
prototype = JSC::constructEmptyObject(globalObject, globalObject->m_typeErrorStructure.prototype(globalObject));
|
||||
break;
|
||||
case JSC::ErrorType::RangeError:
|
||||
prototype = JSC::constructEmptyObject(globalObject, globalObject->m_rangeErrorStructure.prototype(globalObject));
|
||||
break;
|
||||
case JSC::ErrorType::Error:
|
||||
prototype = JSC::constructEmptyObject(globalObject, globalObject->errorPrototype());
|
||||
break;
|
||||
case JSC::ErrorType::URIError:
|
||||
prototype = JSC::constructEmptyObject(globalObject, globalObject->m_URIErrorStructure.prototype(globalObject));
|
||||
break;
|
||||
case JSC::ErrorType::SyntaxError:
|
||||
prototype = JSC::constructEmptyObject(globalObject, globalObject->m_syntaxErrorStructure.prototype(globalObject));
|
||||
break;
|
||||
default: {
|
||||
RELEASE_ASSERT_NOT_REACHED_WITH_MESSAGE("TODO: Add support for more error types");
|
||||
break;
|
||||
}
|
||||
}
|
||||
switch (type) {
|
||||
case JSC::ErrorType::TypeError:
|
||||
prototype = JSC::constructEmptyObject(globalObject, globalObject->m_typeErrorStructure.prototype(globalObject));
|
||||
break;
|
||||
case JSC::ErrorType::RangeError:
|
||||
prototype = JSC::constructEmptyObject(globalObject, globalObject->m_rangeErrorStructure.prototype(globalObject));
|
||||
break;
|
||||
case JSC::ErrorType::Error:
|
||||
prototype = JSC::constructEmptyObject(globalObject, globalObject->errorPrototype());
|
||||
break;
|
||||
case JSC::ErrorType::URIError:
|
||||
prototype = JSC::constructEmptyObject(globalObject, globalObject->m_URIErrorStructure.prototype(globalObject));
|
||||
break;
|
||||
case JSC::ErrorType::SyntaxError:
|
||||
prototype = JSC::constructEmptyObject(globalObject, globalObject->m_syntaxErrorStructure.prototype(globalObject));
|
||||
break;
|
||||
default: {
|
||||
RELEASE_ASSERT_NOT_REACHED_WITH_MESSAGE("TODO: Add support for more error types");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
prototype->putDirect(vm, vm.propertyNames->name, jsString(vm, String(name)), 0);
|
||||
@@ -187,18 +181,18 @@ static ErrorCodeCache* errorCache(Zig::GlobalObject* globalObject)
|
||||
}
|
||||
|
||||
// clang-format on
|
||||
static Structure* createErrorStructure(JSC::VM& vm, JSGlobalObject* globalObject, JSC::ErrorType type, WTF::ASCIILiteral name, WTF::ASCIILiteral code, bool isDOMExceptionPrototype)
|
||||
static Structure* createErrorStructure(JSC::VM& vm, JSGlobalObject* globalObject, JSC::ErrorType type, WTF::ASCIILiteral name, WTF::ASCIILiteral code)
|
||||
{
|
||||
auto* prototype = createErrorPrototype(vm, globalObject, type, name, code, isDOMExceptionPrototype);
|
||||
auto* prototype = createErrorPrototype(vm, globalObject, type, name, code);
|
||||
return ErrorInstance::createStructure(vm, globalObject, prototype);
|
||||
}
|
||||
|
||||
JSObject* ErrorCodeCache::createError(VM& vm, Zig::GlobalObject* globalObject, ErrorCode code, JSValue message, JSValue options, bool isDOMExceptionPrototype)
|
||||
JSObject* ErrorCodeCache::createError(VM& vm, Zig::GlobalObject* globalObject, ErrorCode code, JSValue message, JSValue options)
|
||||
{
|
||||
auto* cache = errorCache(globalObject);
|
||||
const auto& data = errors[static_cast<size_t>(code)];
|
||||
if (!cache->internalField(static_cast<unsigned>(code))) {
|
||||
auto* structure = createErrorStructure(vm, globalObject, data.type, data.name, data.code, isDOMExceptionPrototype);
|
||||
auto* structure = createErrorStructure(vm, globalObject, data.type, data.name, data.code);
|
||||
cache->internalField(static_cast<unsigned>(code)).set(vm, cache, structure);
|
||||
}
|
||||
|
||||
@@ -206,44 +200,44 @@ JSObject* ErrorCodeCache::createError(VM& vm, Zig::GlobalObject* globalObject, E
|
||||
return JSC::ErrorInstance::create(globalObject, structure, message, options, nullptr, JSC::RuntimeType::TypeNothing, data.type, true);
|
||||
}
|
||||
|
||||
JSObject* createError(VM& vm, Zig::GlobalObject* globalObject, ErrorCode code, const String& message, bool isDOMExceptionPrototype)
|
||||
JSObject* createError(VM& vm, Zig::GlobalObject* globalObject, ErrorCode code, const String& message)
|
||||
{
|
||||
return errorCache(globalObject)->createError(vm, globalObject, code, jsString(vm, message), jsUndefined(), isDOMExceptionPrototype);
|
||||
return errorCache(globalObject)->createError(vm, globalObject, code, jsString(vm, message), jsUndefined());
|
||||
}
|
||||
|
||||
JSObject* createError(Zig::GlobalObject* globalObject, ErrorCode code, const String& message, bool isDOMExceptionPrototype)
|
||||
JSObject* createError(Zig::GlobalObject* globalObject, ErrorCode code, const String& message)
|
||||
{
|
||||
return createError(globalObject->vm(), globalObject, code, message, isDOMExceptionPrototype);
|
||||
return createError(globalObject->vm(), globalObject, code, message);
|
||||
}
|
||||
|
||||
JSObject* createError(VM& vm, JSC::JSGlobalObject* globalObject, ErrorCode code, const String& message, bool isDOMExceptionPrototype)
|
||||
JSObject* createError(VM& vm, JSC::JSGlobalObject* globalObject, ErrorCode code, const String& message)
|
||||
{
|
||||
return createError(vm, defaultGlobalObject(globalObject), code, message, isDOMExceptionPrototype);
|
||||
return createError(vm, defaultGlobalObject(globalObject), code, message);
|
||||
}
|
||||
|
||||
JSObject* createError(VM& vm, JSC::JSGlobalObject* globalObject, ErrorCode code, JSValue message, bool isDOMExceptionPrototype)
|
||||
JSObject* createError(VM& vm, JSC::JSGlobalObject* globalObject, ErrorCode code, JSValue message)
|
||||
{
|
||||
if (auto* zigGlobalObject = jsDynamicCast<Zig::GlobalObject*>(globalObject))
|
||||
return createError(vm, zigGlobalObject, code, message, jsUndefined(), isDOMExceptionPrototype);
|
||||
return createError(vm, zigGlobalObject, code, message, jsUndefined());
|
||||
|
||||
auto* structure = createErrorStructure(vm, globalObject, errors[static_cast<size_t>(code)].type, errors[static_cast<size_t>(code)].name, errors[static_cast<size_t>(code)].code, isDOMExceptionPrototype);
|
||||
auto* structure = createErrorStructure(vm, globalObject, errors[static_cast<size_t>(code)].type, errors[static_cast<size_t>(code)].name, errors[static_cast<size_t>(code)].code);
|
||||
return JSC::ErrorInstance::create(globalObject, structure, message, jsUndefined(), nullptr, JSC::RuntimeType::TypeNothing, errors[static_cast<size_t>(code)].type, true);
|
||||
}
|
||||
|
||||
JSC::JSObject* createError(VM& vm, Zig::GlobalObject* globalObject, ErrorCode code, JSValue message, JSValue options, bool isDOMExceptionPrototype)
|
||||
JSC::JSObject* createError(VM& vm, Zig::GlobalObject* globalObject, ErrorCode code, JSValue message, JSValue options)
|
||||
{
|
||||
return errorCache(globalObject)->createError(vm, globalObject, code, message, options, isDOMExceptionPrototype);
|
||||
return errorCache(globalObject)->createError(vm, globalObject, code, message, options);
|
||||
}
|
||||
|
||||
JSObject* createError(JSC::JSGlobalObject* globalObject, ErrorCode code, const String& message, bool isDOMExceptionPrototype)
|
||||
JSObject* createError(JSC::JSGlobalObject* globalObject, ErrorCode code, const String& message)
|
||||
{
|
||||
return createError(globalObject->vm(), globalObject, code, message, isDOMExceptionPrototype);
|
||||
return createError(globalObject->vm(), globalObject, code, message);
|
||||
}
|
||||
|
||||
JSObject* createError(Zig::JSGlobalObject* globalObject, ErrorCode code, JSC::JSValue message, bool isDOMExceptionPrototype)
|
||||
JSObject* createError(Zig::JSGlobalObject* globalObject, ErrorCode code, JSC::JSValue message)
|
||||
{
|
||||
auto& vm = JSC::getVM(globalObject);
|
||||
return createError(vm, globalObject, code, message, isDOMExceptionPrototype);
|
||||
return createError(vm, globalObject, code, message);
|
||||
}
|
||||
|
||||
extern "C" BunString Bun__inspect(JSC::JSGlobalObject* globalObject, JSValue value);
|
||||
@@ -843,7 +837,7 @@ JSC::EncodedJSValue INVALID_ARG_VALUE_RangeError(JSC::ThrowScope& throwScope, JS
|
||||
JSValueToStringSafe(globalObject, builder, value, true);
|
||||
RETURN_IF_EXCEPTION(throwScope, {});
|
||||
|
||||
auto* structure = createErrorStructure(vm, globalObject, ErrorType::RangeError, "RangeError"_s, "ERR_INVALID_ARG_VALUE"_s, false);
|
||||
auto* structure = createErrorStructure(vm, globalObject, ErrorType::RangeError, "RangeError"_s, "ERR_INVALID_ARG_VALUE"_s);
|
||||
auto error = JSC::ErrorInstance::create(vm, structure, builder.toString(), jsUndefined(), nullptr, JSC::RuntimeType::TypeNothing, ErrorType::RangeError, true);
|
||||
throwScope.throwException(globalObject, error);
|
||||
return {};
|
||||
@@ -1502,6 +1496,25 @@ void throwCryptoOperationFailed(JSGlobalObject* globalObject, JSC::ThrowScope& s
|
||||
|
||||
} // namespace Bun
|
||||
|
||||
extern "C" JSC::EncodedJSValue Bun__wrapAbortError(JSC::JSGlobalObject* lexicalGlobalObject, JSC::EncodedJSValue causeParam)
|
||||
{
|
||||
auto* globalObject = defaultGlobalObject(lexicalGlobalObject);
|
||||
auto& vm = JSC::getVM(globalObject);
|
||||
auto scope = DECLARE_THROW_SCOPE(vm);
|
||||
auto cause = JSC::JSValue::decode(causeParam);
|
||||
|
||||
if (cause.isUndefined()) {
|
||||
return JSC::JSValue::encode(Bun::createError(vm, globalObject, Bun::ErrorCode::ABORT_ERR, JSC::JSValue(globalObject->commonStrings().OperationWasAbortedString(globalObject))));
|
||||
}
|
||||
|
||||
auto message = globalObject->commonStrings().OperationWasAbortedString(globalObject);
|
||||
JSC::JSObject* options = JSC::constructEmptyObject(globalObject, globalObject->objectPrototype(), 24);
|
||||
options->putDirect(vm, JSC::Identifier::fromString(vm, "cause"_s), cause);
|
||||
|
||||
auto error = Bun::createError(vm, globalObject, Bun::ErrorCode::ABORT_ERR, message, options);
|
||||
return JSC::JSValue::encode(error);
|
||||
}
|
||||
|
||||
JSC_DEFINE_HOST_FUNCTION(jsFunctionMakeAbortError, (JSC::JSGlobalObject * lexicalGlobalObject, JSC::CallFrame* callFrame))
|
||||
{
|
||||
auto* globalObject = defaultGlobalObject(lexicalGlobalObject);
|
||||
@@ -1512,28 +1525,25 @@ JSC_DEFINE_HOST_FUNCTION(jsFunctionMakeAbortError, (JSC::JSGlobalObject * lexica
|
||||
if (!options.isUndefined() && options.isCell() && !options.asCell()->isObject()) return Bun::ERR::INVALID_ARG_TYPE(scope, globalObject, "options"_s, "object"_s, options);
|
||||
|
||||
if (message.isUndefined() && options.isUndefined()) {
|
||||
return JSValue::encode(Bun::createError(vm, lexicalGlobalObject, Bun::ErrorCode::ABORT_ERR, JSValue(globalObject->commonStrings().OperationWasAbortedString(globalObject)), false));
|
||||
return JSValue::encode(Bun::createError(vm, lexicalGlobalObject, Bun::ErrorCode::ABORT_ERR, JSValue(globalObject->commonStrings().OperationWasAbortedString(globalObject))));
|
||||
}
|
||||
|
||||
if (message.isUndefined()) message = globalObject->commonStrings().OperationWasAbortedString(globalObject);
|
||||
auto error = Bun::createError(vm, globalObject, Bun::ErrorCode::ABORT_ERR, message, options, false);
|
||||
auto error = Bun::createError(vm, globalObject, Bun::ErrorCode::ABORT_ERR, message, options);
|
||||
return JSC::JSValue::encode(error);
|
||||
}
|
||||
|
||||
JSC::JSValue WebCore::toJS(JSC::JSGlobalObject* globalObject, CommonAbortReason abortReason)
|
||||
{
|
||||
auto* zigGlobalObject = defaultGlobalObject(globalObject);
|
||||
switch (abortReason) {
|
||||
case CommonAbortReason::Timeout: {
|
||||
return createError(globalObject, Bun::ErrorCode::ABORT_ERR, zigGlobalObject->commonStrings().OperationWasAbortedString(globalObject), true);
|
||||
return createDOMException(globalObject, ExceptionCode::TimeoutError, "The operation timed out."_s);
|
||||
}
|
||||
case CommonAbortReason::UserAbort: {
|
||||
// This message is a standardized error message. We cannot change it.
|
||||
// https://webidl.spec.whatwg.org/#idl-DOMException:~:text=The%20operation%20was%20aborted.
|
||||
return createError(globalObject, Bun::ErrorCode::ABORT_ERR, zigGlobalObject->commonStrings().OperationWasAbortedString(globalObject), true);
|
||||
return createDOMException(globalObject, ExceptionCode::AbortError, "The operation was aborted."_s);
|
||||
}
|
||||
case CommonAbortReason::ConnectionClosed: {
|
||||
return createError(globalObject, Bun::ErrorCode::ABORT_ERR, zigGlobalObject->commonStrings().ConnectionWasClosedString(globalObject), true);
|
||||
return createDOMException(globalObject, ExceptionCode::AbortError, "The connection was closed."_s);
|
||||
}
|
||||
default: {
|
||||
break;
|
||||
@@ -2232,6 +2242,8 @@ JSC_DEFINE_HOST_FUNCTION(Bun::jsFunctionMakeErrorWithCode, (JSC::JSGlobalObject
|
||||
return JSC::JSValue::encode(createError(globalObject, ErrorCode::ERR_SOCKET_DGRAM_NOT_RUNNING, "Socket is not running"_s));
|
||||
case ErrorCode::ERR_INVALID_CURSOR_POS:
|
||||
return JSC::JSValue::encode(createError(globalObject, ErrorCode::ERR_INVALID_CURSOR_POS, "Cannot set cursor row without setting its column"_s));
|
||||
case ErrorCode::ERR_INVALID_HANDLE_TYPE:
|
||||
return JSC::JSValue::encode(createError(globalObject, ErrorCode::ERR_INVALID_HANDLE_TYPE, "This handle type cannot be sent"_s));
|
||||
case ErrorCode::ERR_MULTIPLE_CALLBACK:
|
||||
return JSC::JSValue::encode(createError(globalObject, ErrorCode::ERR_MULTIPLE_CALLBACK, "Callback called multiple times"_s));
|
||||
case ErrorCode::ERR_STREAM_PREMATURE_CLOSE:
|
||||
|
||||
Reference in New Issue
Block a user