<commit_before>/**
 * Copyright (c) 2012-2013 <NAME>
 * Alle Rechte vorbehalten / All Rights Reserved.  Use is subject to license terms.
 *
 * <https://github.com/anba/es6draft>
 */
package com.github.anba.es6draft.runtime.objects;

import static com.github.anba.es6draft.runtime.AbstractOperations.CreateOwnDataProperty;
import static com.github.anba.es6draft.runtime.AbstractOperations.OrdinaryCreateFromConstructor;
import static com.github.anba.es6draft.runtime.AbstractOperations.ToString;
import static com.github.anba.es6draft.runtime.internal.Properties.createProperties;
import static com.github.anba.es6draft.runtime.types.Undefined.UNDEFINED;
import static com.github.anba.es6draft.runtime.types.builtins.OrdinaryFunction.AddRestrictedFunctionProperties;
import static com.github.anba.es6draft.runtime.types.builtins.OrdinaryFunction.OrdinaryConstruct;

import com.github.anba.es6draft.runtime.ExecutionContext;
import com.github.anba.es6draft.runtime.Realm;
import com.github.anba.es6draft.runtime.internal.Initialisable;
import com.github.anba.es6draft.runtime.internal.ObjectAllocator;
import com.github.anba.es6draft.runtime.internal.Properties.Attributes;
import com.github.anba.es6draft.runtime.internal.Properties.Function;
import com.github.anba.es6draft.runtime.internal.Properties.Prototype;
import com.github.anba.es6draft.runtime.internal.Properties.Value;
import com.github.anba.es6draft.runtime.types.BuiltinSymbol;
import com.github.anba.es6draft.runtime.types.Constructor;
import com.github.anba.es6draft.runtime.types.Intrinsics;
import com.github.anba.es6draft.runtime.types.Type;
import com.github.anba.es6draft.runtime.types.builtins.BuiltinFunction;

/**
 * <h1>15 Standard Built-in ECMAScript Objects</h1><br>
 * <h2>15.11 Error Objects</h2>
 * <ul>
 * <li>15.11.6 Native Error Types Used in This Standard
 * <li>15.11.7 NativeError Object Structure
 * <ul>
 * <li>15.11.7.1 NativeError Constructors Called as Functions
 * <li>15.11.7.2 The NativeError Constructors
 * <li>15.11.7.3 Properties of the NativeError Constructors
 * </ul>
 * </ul>
 */
public class NativeErrorConstructor extends BuiltinFunction implements Constructor, Initialisable {
    /**
     * 15.11.6 Native Error Types Used in This Standard
     * <ul>
     * <li>15.11.6.1 EvalError
     * <li>15.11.6.2 RangeError
     * <li>15.11.6.3 ReferenceError
     * <li>15.11.6.4 SyntaxError
     * <li>15.11.6.5 TypeError
     * <li>15.11.6.6 URIError
     * </ul>
     */
    public enum ErrorType {
        EvalError, RangeError, ReferenceError, SyntaxError, TypeError, URIError, InternalError;

        private Intrinsics prototype() {
            switch (this) {
            case EvalError:
                return Intrinsics.EvalErrorPrototype;
            case RangeError:
                return Intrinsics.RangeErrorPrototype;
            case ReferenceError:
                return Intrinsics.ReferenceErrorPrototype;
            case SyntaxError:
                return Intrinsics.SyntaxErrorPrototype;
            case TypeError:
                return Intrinsics.TypeErrorPrototype;
            case URIError:
                return Intrinsics.URIErrorPrototype;
            case InternalError:
                return Intrinsics.InternalErrorPrototype;
            default:
                throw new IllegalStateException();
            }
        }
    }

    private final ErrorType type;

    public NativeErrorConstructor(Realm realm, ErrorType type) {
        super(realm);
        this.type = type;
    }

    @Override
    public void initialise(ExecutionContext cx) {
        switch (type) {
        case EvalError:
            createProperties(this, cx, EvalErrorConstructorProperties.class);
            break;
        case RangeError:
            createProperties(this, cx, RangeErrorConstructorProperties.class);
            break;
        case ReferenceError:
            createProperties(this, cx, ReferenceErrorConstructorProperties.class);
            break;
        case SyntaxError:
            createProperties(this, cx, SyntaxErrorConstructorProperties.class);
            break;
        case TypeError:
            createProperties(this, cx, TypeErrorConstructorProperties.class);
            break;
        case URIError:
            createProperties(this, cx, URIErrorConstructorProperties.class);
            break;
        case InternalError:
            createProperties(this, cx, InternalErrorConstructorProperties.class);
            break;
        default:
            throw new IllegalStateException();
        }
        AddRestrictedFunctionProperties(cx, this);
    }

    /**
     * 15.11.7.1.1 NativeError (message)
     */
    @Override
    public Object call(ExecutionContext callerContext, Object thisValue, Object... args) {
        ExecutionContext calleeContext = realm().defaultContext();
        Object message = args.length > 0 ? args[0] : UNDEFINED;

        ErrorObject obj;
        if (!Type.isObject(thisValue) || !(thisValue instanceof ErrorObject)
                || ((ErrorObject) thisValue).isInitialised()) {
            obj = OrdinaryCreateFromConstructor(calleeContext, this, type.prototype(),
                    NativeErrorObjectAllocator.INSTANCE);
        } else {
            obj = (ErrorObject) thisValue;
        }

        obj.initialise();

        if (!Type.isUndefined(message)) {
            CharSequence msg = ToString(calleeContext, message);
            // FIXME: spec bug - no longer non-enumerable (Bug 1404)
            CreateOwnDataProperty(calleeContext, obj, "message", msg);
        }

        return obj;
    }

    /**<commit_msg>Add non-standard Error constructor to NativeError as well<commit_after>/**
 * Copyright (c) 2012-2013 <NAME>
 * Alle Rechte vorbehalten / All Rights Reserved.  Use is subject to license terms.
 *
 * <https://github.com/anba/es6draft>
 */
package com.github.anba.es6draft.runtime.objects;

import static com.github.anba.es6draft.runtime.AbstractOperations.CreateOwnDataProperty;
import static com.github.anba.es6draft.runtime.AbstractOperations.OrdinaryCreateFromConstructor;
import static com.github.anba.es6draft.runtime.AbstractOperations.ToInt32;
import static com.github.anba.es6draft.runtime.AbstractOperations.ToString;
import static com.github.anba.es6draft.runtime.internal.Properties.createProperties;
import static com.github.anba.es6draft.runtime.types.Undefined.UNDEFINED;
import static com.github.anba.es6draft.runtime.types.builtins.OrdinaryFunction.AddRestrictedFunctionProperties;
import static com.github.anba.es6draft.runtime.types.builtins.OrdinaryFunction.OrdinaryConstruct;

import com.github.anba.es6draft.runtime.ExecutionContext;
import com.github.anba.es6draft.runtime.Realm;
import com.github.anba.es6draft.runtime.internal.Initialisable;
import com.github.anba.es6draft.runtime.internal.ObjectAllocator;
import com.github.anba.es6draft.runtime.internal.Properties.Attributes;
import com.github.anba.es6draft.runtime.internal.Properties.Function;
import com.github.anba.es6draft.runtime.internal.Properties.Prototype;
import com.github.anba.es6draft.runtime.internal.Properties.Value;
import com.github.anba.es6draft.runtime.types.BuiltinSymbol;
import com.github.anba.es6draft.runtime.types.Constructor;
import com.github.anba.es6draft.runtime.types.Intrinsics;
import com.github.anba.es6draft.runtime.types.Type;
import com.github.anba.es6draft.runtime.types.builtins.BuiltinFunction;

/**
 * <h1>15 Standard Built-in ECMAScript Objects</h1><br>
 * <h2>15.11 Error Objects</h2>
 * <ul>
 * <li>15.11.6 Native Error Types Used in This Standard
 * <li>15.11.7 NativeError Object Structure
 * <ul>
 * <li>15.11.7.1 NativeError Constructors Called as Functions
 * <li>15.11.7.2 The NativeError Constructors
 * <li>15.11.7.3 Properties of the NativeError Constructors
 * </ul>
 * </ul>
 */
public class NativeErrorConstructor extends BuiltinFunction implements Constructor, Initialisable {
    /**
     * 15.11.6 Native Error Types Used in This Standard
     * <ul>
     * <li>15.11.6.1 EvalError
     * <li>15.11.6.2 RangeError
     * <li>15.11.6.3 ReferenceError
     * <li>15.11.6.4 SyntaxError
     * <li>15.11.6.5 TypeError
     * <li>15.11.6.6 URIError
     * </ul>
     */
    public enum ErrorType {
        EvalError, RangeError, ReferenceError, SyntaxError, TypeError, URIError, InternalError;

        private Intrinsics prototype() {
            switch (this) {
            case EvalError:
                return Intrinsics.EvalErrorPrototype;
            case RangeError:
                return Intrinsics.RangeErrorPrototype;
            case ReferenceError:
                return Intrinsics.ReferenceErrorPrototype;
            case SyntaxError:
                return Intrinsics.SyntaxErrorPrototype;
            case TypeError:
                return Intrinsics.TypeErrorPrototype;
            case URIError:
                return Intrinsics.URIErrorPrototype;
            case InternalError:
                return Intrinsics.InternalErrorPrototype;
            default:
                throw new IllegalStateException();
            }
        }
    }

    private final ErrorType type;

    public NativeErrorConstructor(Realm realm, ErrorType type) {
        super(realm);
        this.type = type;
    }

    @Override
    public void initialise(ExecutionContext cx) {
        switch (type) {
        case EvalError:
            createProperties(this, cx, EvalErrorConstructorProperties.class);
            break;
        case RangeError:
            createProperties(this, cx, RangeErrorConstructorProperties.class);
            break;
        case ReferenceError:
            createProperties(this, cx, ReferenceErrorConstructorProperties.class);
            break;
        case SyntaxError:
            createProperties(this, cx, SyntaxErrorConstructorProperties.class);
            break;
        case TypeError:
            createProperties(this, cx, TypeErrorConstructorProperties.class);
            break;
        case URIError:
            createProperties(this, cx, URIErrorConstructorProperties.class);
            break;
        case InternalError:
            createProperties(this, cx, InternalErrorConstructorProperties.class);
            break;
        default:
            throw new IllegalStateException();
        }
        AddRestrictedFunctionProperties(cx, this);
    }

    /**
     * 15.11.7.1.1 NativeError (message)
     * <p>
     * <strong>Extension</strong>: NativeError (message, fileName, lineNumber)
     */
    @Override
    public Object call(ExecutionContext callerContext, Object thisValue, Object... args) {
        ExecutionContext calleeContext = realm().defaultContext();
        Object message = args.length > 0 ? args[0] : UNDEFINED;

        ErrorObject obj;
        if (!Type.isObject(thisValue) || !(thisValue instanceof ErrorObject)
                || ((ErrorObject) thisValue).isInitialised()) {
            obj = OrdinaryCreateFromConstructor(calleeContext, this, type.prototype(),
                    NativeErrorObjectAllocator.INSTANCE);
        } else {
            obj = (ErrorObject) thisValue;
        }

        obj.initialise();

        if (!Type.isUndefined(message)) {
            CharSequence msg = ToString(calleeContext, message);
            // FIXME: spec bug - no longer non-enumerable (Bug 1404)
            CreateOwnDataProperty(calleeContext, obj, "message", msg);
        }
        if (args.length > 1) {
            CharSequence fileName = ToString(calleeContext, args[1]);
            CreateOwnDataProperty(calleeContext, obj, "fileName", fileName);
        }
        if (args.length > 2) {
            int lineNumber = ToInt32(calleeContext, args[2]);
            CreateOwnDataProperty(calleeContext, obj, "lineNumber", lineNumber);
        }

        return obj;
    }

    /**
{'fraction_non_alphanumeric': 0.09668952671238676, 'fraction_numerical': 0.021420709508085684, 'mean_word_length': 2.7919743178170147, 'pattern_counts': {'":': 0, '<': 54, '<?xml version=': 0, '>': 58, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 20, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6544720', 'n_tokens_mistral': 3401, 'n_tokens_neox': 3271, 'n_words': 660}