Skip to content

Commit

Permalink
Add SuppressedError
Browse files Browse the repository at this point in the history
  • Loading branch information
paperclover committed Jan 16, 2024
1 parent 62e3eae commit f46e6f9
Show file tree
Hide file tree
Showing 17 changed files with 441 additions and 1 deletion.
3 changes: 3 additions & 0 deletions Source/JavaScriptCore/Sources.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1229,3 +1229,6 @@ yarr/YarrTZoneImpls.cpp
yarr/YarrCanonicalizeUnicode.cpp

runtime/InternalFieldTuple.cpp
runtime/SuppressedError.cpp
runtime/SuppressedErrorConstructor.cpp
runtime/SuppressedErrorPrototype.cpp
2 changes: 1 addition & 1 deletion Source/JavaScriptCore/builtins/BuiltinNames.h
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ inline void BuiltinNames::appendExternalName(const Identifier& publicName, const
#ifndef BUN_SKIP_FAILING_ASSERTIONS
ASSERT_UNUSED(publicName, String(publicName.impl()) == String(privateName.impl()));
#else
UNUSED_PARAM(privateName);
UNUSED_PARAM(publicName);
#endif
checkPublicToPrivateMapConsistency(privateName.impl());

Expand Down
2 changes: 2 additions & 0 deletions Source/JavaScriptCore/runtime/CommonIdentifiers.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
macro(enumerable) \
macro(era) \
macro(eraYear) \
macro(error) \
macro(errors) \
macro(eval) \
macro(events) \
Expand Down Expand Up @@ -262,6 +263,7 @@
macro(style) \
macro(subarray) \
macro(summary) \
macro(suppressed) \
macro(target) \
macro(test) \
macro(then) \
Expand Down
2 changes: 2 additions & 0 deletions Source/JavaScriptCore/runtime/Error.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ JSObject* createError(JSGlobalObject* globalObject, ErrorTypeWithExtension error
return createURIError(globalObject, message);
case ErrorTypeWithExtension::AggregateError:
break;
case ErrorTypeWithExtension::SuppressedError:
break;
case ErrorTypeWithExtension::OutOfMemoryError:
return createOutOfMemoryError(globalObject, message);
}
Expand Down
1 change: 1 addition & 0 deletions Source/JavaScriptCore/runtime/ErrorType.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ namespace JSC {
macro(TypeError) \
macro(URIError) \
macro(AggregateError) \
macro(SuppressedError) \

#define JSC_ERROR_TYPES_WITH_EXTENSION(macro) \
JSC_ERROR_TYPES(macro) \
Expand Down
16 changes: 16 additions & 0 deletions Source/JavaScriptCore/runtime/JSGlobalObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,12 @@
#include "JSGlobalObject.h"

#include "AggregateError.h"
#include "SuppressedError.h"
#include "InternalFieldTuple.h"
#include "AggregateErrorConstructorInlines.h"
#include "SuppressedErrorConstructorInlines.h"
#include "AggregateErrorPrototypeInlines.h"
#include "SuppressedErrorPrototypeInlines.h"
#include "ArrayConstructorInlines.h"
#include "ArrayIteratorPrototypeInlines.h"
#include "ArrayPrototypeInlines.h"
Expand Down Expand Up @@ -636,6 +639,7 @@ const GlobalObjectMethodTable* JSGlobalObject::baseGlobalObjectMethodTable()
TypeError JSGlobalObject::m_typeErrorStructure DontEnum|ClassStructure
URIError JSGlobalObject::m_URIErrorStructure DontEnum|ClassStructure
AggregateError JSGlobalObject::m_aggregateErrorStructure DontEnum|ClassStructure
SuppressedError JSGlobalObject::m_suppressedErrorStructure DontEnum|ClassStructure
Proxy createProxyProperty DontEnum|PropertyCallback
Reflect createReflectProperty DontEnum|PropertyCallback
JSON createJSONProperty DontEnum|PropertyCallback
Expand Down Expand Up @@ -769,6 +773,13 @@ void JSGlobalObject::initializeAggregateErrorConstructor(LazyClassStructure::Ini
init.setConstructor(AggregateErrorConstructor::create(init.vm, AggregateErrorConstructor::createStructure(init.vm, this, m_errorStructure.constructor(this)), jsCast<AggregateErrorPrototype*>(init.prototype)));
}

void JSGlobalObject::initializeSuppressedErrorConstructor(LazyClassStructure::Initializer& init)
{
init.setPrototype(SuppressedErrorPrototype::create(init.vm, SuppressedErrorPrototype::createStructure(init.vm, this, m_errorStructure.prototype(this))));
init.setStructure(ErrorInstance::createStructure(init.vm, this, init.prototype));
init.setConstructor(SuppressedErrorConstructor::create(init.vm, SuppressedErrorConstructor::createStructure(init.vm, this, m_errorStructure.constructor(this)), jsCast<SuppressedErrorPrototype*>(init.prototype)));
}

SUPPRESS_ASAN inline void JSGlobalObject::initStaticGlobals(VM& vm)
{
GlobalPropertyInfo staticGlobals[] = {
Expand Down Expand Up @@ -1186,6 +1197,10 @@ capitalName ## Constructor* lowerName ## Constructor = featureFlag ? capitalName
[] (LazyClassStructure::Initializer& init) {
init.global->initializeAggregateErrorConstructor(init);
});
m_suppressedErrorStructure.initLater(
[] (LazyClassStructure::Initializer& init) {
init.global->initializeSuppressedErrorConstructor(init);
});

m_generatorFunctionPrototype.set(vm, this, GeneratorFunctionPrototype::create(vm, GeneratorFunctionPrototype::createStructure(vm, this, m_functionPrototype.get())));
GeneratorFunctionConstructor* generatorFunctionConstructor = GeneratorFunctionConstructor::create(vm, GeneratorFunctionConstructor::createStructure(vm, this, functionConstructor), m_generatorFunctionPrototype.get());
Expand Down Expand Up @@ -2446,6 +2461,7 @@ void JSGlobalObject::visitChildrenImpl(JSCell* cell, Visitor& visitor)
thisObject->m_typeErrorStructure.visit(visitor);
thisObject->m_URIErrorStructure.visit(visitor);
thisObject->m_aggregateErrorStructure.visit(visitor);
thisObject->m_suppressedErrorStructure.visit(visitor);
visitor.append(thisObject->m_arrayConstructor);
visitor.append(thisObject->m_shadowRealmConstructor);
visitor.append(thisObject->m_regExpConstructor);
Expand Down
2 changes: 2 additions & 0 deletions Source/JavaScriptCore/runtime/JSGlobalObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ class JSGlobalObject : public JSSegmentedVariableObject {
LazyClassStructure m_typeErrorStructure;
LazyClassStructure m_URIErrorStructure;
LazyClassStructure m_aggregateErrorStructure;
LazyClassStructure m_suppressedErrorStructure;

WriteBarrier<ObjectConstructor> m_objectConstructor;
WriteBarrier<ArrayConstructor> m_arrayConstructor;
Expand Down Expand Up @@ -1117,6 +1118,7 @@ class JSGlobalObject : public JSSegmentedVariableObject {
void initializeErrorConstructor(LazyClassStructure::Initializer&);

void initializeAggregateErrorConstructor(LazyClassStructure::Initializer&);
void initializeSuppressedErrorConstructor(LazyClassStructure::Initializer&);

JS_EXPORT_PRIVATE void init(VM&);
void initStaticGlobals(VM&);
Expand Down
3 changes: 3 additions & 0 deletions Source/JavaScriptCore/runtime/JSGlobalObjectInlines.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "ArrayAllocationProfile.h"
#include "ArrayConstructor.h"
#include "ArrayPrototype.h"
#include "ErrorType.h"
#include "JSClassRef.h"
#include "JSCustomGetterFunction.h"
#include "JSCustomSetterFunction.h"
Expand Down Expand Up @@ -427,6 +428,8 @@ inline Structure* JSGlobalObject::errorStructure(ErrorType errorType) const
return m_URIErrorStructure.get(this);
case ErrorType::AggregateError:
return m_aggregateErrorStructure.get(this);
case ErrorType::SuppressedError:
return m_suppressedErrorStructure.get(this);
}
ASSERT_NOT_REACHED();
return nullptr;
Expand Down
56 changes: 56 additions & 0 deletions Source/JavaScriptCore/runtime/SuppressedError.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (C) 2020 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 APPLE INC. AND ITS CONTRIBUTORS ``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 APPLE INC. OR ITS 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 "SuppressedError.h"

#include "ExceptionScope.h"
#include "IteratorOperations.h"
#include "JSCJSValueInlines.h"
#include "JSGlobalObjectInlines.h"

namespace JSC {

ErrorInstance* createSuppressedError(JSGlobalObject* globalObject, VM& vm, Structure* structure, JSValue error, JSValue suppressed, JSValue message, JSValue options, ErrorInstance::SourceAppender appender, RuntimeType type, bool useCurrentFrame)
{
auto scope = DECLARE_THROW_SCOPE(vm);

String messageString = message.isUndefined() ? String() : message.toWTFString(globalObject);
RETURN_IF_EXCEPTION(scope, nullptr);

JSValue cause;
if (options.isObject()) {
// Since `throw undefined;` is valid, we need to distinguish the case where `cause` is an explicit undefined.
cause = asObject(options)->getIfPropertyExists(globalObject, vm.propertyNames->cause);
RETURN_IF_EXCEPTION(scope, nullptr);
}

auto* inst = ErrorInstance::create(globalObject, vm, structure, messageString, cause, appender, type, ErrorType::SuppressedError, useCurrentFrame);
inst->putDirect(vm, vm.propertyNames->error, error, static_cast<unsigned>(0));
inst->putDirect(vm, vm.propertyNames->suppressed, suppressed, static_cast<unsigned>(0));
return inst;
}

} // namespace JSC
34 changes: 34 additions & 0 deletions Source/JavaScriptCore/runtime/SuppressedError.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright (C) 2020 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 APPLE INC. AND ITS CONTRIBUTORS ``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 APPLE INC. OR ITS 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.
*/

#pragma once

#include "ErrorInstance.h"

namespace JSC {

ErrorInstance* createSuppressedError(JSGlobalObject*, VM&, Structure*, JSValue errors, JSValue message, JSValue options, ErrorInstance::SourceAppender = nullptr, RuntimeType = TypeNothing, bool useCurrentFrame = true);

} // namespace JSC
87 changes: 87 additions & 0 deletions Source/JavaScriptCore/runtime/SuppressedErrorConstructor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Copyright (C) 2020 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 APPLE INC. AND ITS CONTRIBUTORS ``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 APPLE INC. OR ITS 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 "SuppressedErrorConstructor.h"

#include "SuppressedError.h"
#include "SuppressedErrorPrototype.h"
#include "ClassInfo.h"
#include "ExceptionScope.h"
#include "GCAssertions.h"
#include "JSCInlines.h"
#include "RuntimeType.h"

namespace JSC {

STATIC_ASSERT_IS_TRIVIALLY_DESTRUCTIBLE(SuppressedErrorConstructor);

const ClassInfo SuppressedErrorConstructor::s_info = { "Function"_s, &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(SuppressedErrorConstructor) };

static JSC_DECLARE_HOST_FUNCTION(callSuppressedErrorConstructor);
static JSC_DECLARE_HOST_FUNCTION(constructSuppressedErrorConstructor);

SuppressedErrorConstructor::SuppressedErrorConstructor(VM& vm, Structure* structure)
: Base(vm, structure, callSuppressedErrorConstructor, constructSuppressedErrorConstructor)
{
}

void SuppressedErrorConstructor::finishCreation(VM& vm, SuppressedErrorPrototype* prototype)
{
Base::finishCreation(vm, 2, errorTypeName(ErrorType::SuppressedError), PropertyAdditionMode::WithoutStructureTransition);
ASSERT(inherits(info()));

putDirectWithoutTransition(vm, vm.propertyNames->prototype, prototype, PropertyAttribute::DontDelete | PropertyAttribute::ReadOnly | PropertyAttribute::DontEnum);
}

JSC_DEFINE_HOST_FUNCTION(callSuppressedErrorConstructor, (JSGlobalObject* globalObject, CallFrame* callFrame))
{
VM& vm = globalObject->vm();
JSValue error = callFrame->argument(0);
JSValue suppressed = callFrame->argument(1);
JSValue message = callFrame->argument(2);
JSValue options = callFrame->argument(3);
Structure* errorStructure = globalObject->errorStructure(ErrorType::SuppressedError);
return JSValue::encode(createSuppressedError(globalObject, vm, errorStructure, error, suppressed, message, options, nullptr, TypeNothing, false));
}

JSC_DEFINE_HOST_FUNCTION(constructSuppressedErrorConstructor, (JSGlobalObject* globalObject, CallFrame* callFrame))
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
JSValue error = callFrame->argument(0);
JSValue suppressed = callFrame->argument(1);
JSValue message = callFrame->argument(2);
JSValue options = callFrame->argument(3);

JSObject* newTarget = asObject(callFrame->newTarget());
Structure* errorStructure = JSC_GET_DERIVED_STRUCTURE(vm, errorStructureWithErrorType<ErrorType::SuppressedError>, newTarget, callFrame->jsCallee());
RETURN_IF_EXCEPTION(scope, { });
ASSERT(errorStructure);

RELEASE_AND_RETURN(scope, JSValue::encode(createSuppressedError(globalObject, vm, errorStructure, error, suppressed, message, options, nullptr, TypeNothing, false)));
}

} // namespace JSC
55 changes: 55 additions & 0 deletions Source/JavaScriptCore/runtime/SuppressedErrorConstructor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright (C) 2020-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 APPLE INC. AND ITS CONTRIBUTORS ``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 APPLE INC. OR ITS 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.
*/

#pragma once

#include "InternalFunction.h"

namespace JSC {

class SuppressedErrorPrototype;

class SuppressedErrorConstructor final : public InternalFunction {
public:
using Base = InternalFunction;

DECLARE_INFO;

inline static Structure* createStructure(VM&, JSGlobalObject*, JSValue);

static SuppressedErrorConstructor* create(VM& vm, Structure* structure, SuppressedErrorPrototype* prototype)
{
SuppressedErrorConstructor* constructor = new (NotNull, allocateCell<SuppressedErrorConstructor>(vm)) SuppressedErrorConstructor(vm, structure);
constructor->finishCreation(vm, prototype);
return constructor;
}

private:
explicit SuppressedErrorConstructor(VM&, Structure*);

void finishCreation(VM&, SuppressedErrorPrototype*);
};

} // namespace JSC
Loading

0 comments on commit f46e6f9

Please sign in to comment.