Parcourir la source

Fix some error related issues (#1071)

Marko Lahma il y a 3 ans
Parent
commit
9daf50cf25

+ 2 - 2
Jint.Tests.Ecma/TestCases/alltests.json

@@ -17685,8 +17685,8 @@
     source: "ch15/15.1/15.1.2/15.1.2.1/S15.1.2.1_A4.1.js"
   },
   {
-    skip: false,
-    reason: "",
+    skip: true,
+    reason: "Spec has changed",
     source: "ch15/15.1/15.1.2/15.1.2.1/S15.1.2.1_A4.2.js"
   },
   {

+ 0 - 11
Jint/Engine.cs

@@ -56,10 +56,6 @@ namespace Jint
         // cache of types used when resolving CLR type names
         internal readonly Dictionary<string, Type> TypeCache = new();
 
-        // shared frozen version
-        internal readonly PropertyDescriptor _callerCalleeArgumentsThrowerConfigurable;
-        internal readonly PropertyDescriptor _callerCalleeArgumentsThrowerNonConfigurable;
-
         internal readonly JintCallStack CallStack;
 
         // needed in initial engine setup, for example CLR function construction
@@ -96,13 +92,6 @@ namespace Jint
         {
             _executionContexts = new ExecutionContextStack(2);
 
-            _callerCalleeArgumentsThrowerConfigurable = new GetSetPropertyDescriptor.ThrowerPropertyDescriptor(this,
-                PropertyFlag.Configurable | PropertyFlag.CustomJsValue,
-                "'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them");
-            _callerCalleeArgumentsThrowerNonConfigurable = new GetSetPropertyDescriptor.ThrowerPropertyDescriptor(this,
-                PropertyFlag.CustomJsValue,
-                "'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them");
-
             Options = new Options();
             options?.Invoke(this, Options);
 

+ 1 - 1
Jint/Native/Argument/ArgumentsInstance.cs

@@ -67,7 +67,7 @@ namespace Jint.Native.Argument
                     CreateDataProperty(JsString.Create(i), val);
                 }
 
-                DefinePropertyOrThrow(CommonProperties.Callee, _engine._callerCalleeArgumentsThrowerNonConfigurable);
+                DefinePropertyOrThrow(CommonProperties.Callee, new GetSetPropertyDescriptor.ThrowerPropertyDescriptor(_engine, PropertyFlag.CustomJsValue));
             }
             else
             {

+ 10 - 6
Jint/Native/Error/ErrorConstructor.cs

@@ -1,4 +1,5 @@
-using Jint.Native.Function;
+using System;
+using Jint.Native.Function;
 using Jint.Native.Object;
 using Jint.Runtime;
 using Jint.Runtime.Descriptors;
@@ -7,20 +8,20 @@ namespace Jint.Native.Error
 {
     public sealed class ErrorConstructor : FunctionInstance, IConstructor
     {
-        private readonly JsString _name;
+        private readonly Func<Intrinsics, ObjectInstance> _intrinsicDefaultProto;
 
         internal ErrorConstructor(
             Engine engine,
             Realm realm,
             ObjectInstance functionPrototype,
             ObjectInstance objectPrototype,
-            JsString name)
+            JsString name, Func<Intrinsics, ObjectInstance> intrinsicDefaultProto)
             : base(engine, realm, name)
         {
-            _name = name;
+            _intrinsicDefaultProto = intrinsicDefaultProto;
             _prototype = functionPrototype;
             PrototypeObject = new ErrorPrototype(engine, realm, this, objectPrototype, name, ObjectClass.Object);
-            _length = PropertyDescriptor.AllForbiddenDescriptor.NumberOne;
+            _length = new PropertyDescriptor(JsNumber.PositiveOne, PropertyFlag.Configurable);
             _prototypeDescriptor = new PropertyDescriptor(PrototypeObject, PropertyFlag.AllForbidden);
         }
 
@@ -38,11 +39,14 @@ namespace Jint.Native.Error
 
         ObjectInstance IConstructor.Construct(JsValue[] arguments, JsValue newTarget) => Construct(arguments, newTarget);
 
+        /// <summary>
+        /// https://tc39.es/ecma262/#sec-nativeerror
+        /// </summary>
         private ObjectInstance Construct(JsValue[] arguments, JsValue newTarget)
         {
             var o = OrdinaryCreateFromConstructor(
                 newTarget,
-                static intrinsics => intrinsics.Error.PrototypeObject,
+                _intrinsicDefaultProto,
                 static (engine, realm, state) => new ErrorInstance(engine));
 
             var jsValue = arguments.At(0);

+ 1 - 1
Jint/Native/Function/EvalFunctionInstance.cs

@@ -23,7 +23,7 @@ namespace Jint.Native.Function
                 StrictModeScope.IsStrictModeCode ? FunctionThisMode.Strict : FunctionThisMode.Global)
         {
             _prototype = functionPrototype;
-            _length = PropertyDescriptor.AllForbiddenDescriptor.NumberOne;
+            _length = new PropertyDescriptor(JsNumber.PositiveOne, PropertyFlag.Configurable);
         }
 
         public override JsValue Call(JsValue thisObject, JsValue[] arguments)

+ 1 - 1
Jint/Native/Function/FunctionInstance.cs

@@ -16,7 +16,7 @@ namespace Jint.Native.Function
         protected PropertyDescriptor _prototypeDescriptor;
 
         protected internal PropertyDescriptor _length;
-        private PropertyDescriptor _nameDescriptor;
+        internal PropertyDescriptor _nameDescriptor;
 
         protected internal EnvironmentRecord _environment;
         internal readonly JintFunctionDefinition _functionDefinition;

+ 6 - 6
Jint/Native/Function/FunctionPrototype.cs

@@ -30,12 +30,12 @@ namespace Jint.Native.Function
             var properties = new PropertyDictionary(7, checkExistingKeys: false)
             {
                 ["constructor"] = new PropertyDescriptor(_realm.Intrinsics.Function, PropertyFlag.NonEnumerable),
-                ["toString"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "toString", ToString, 0, lengthFlags), propertyFlags),
-                ["apply"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "apply", Apply, 2, lengthFlags), propertyFlags),
-                ["call"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "call", CallImpl, 1, lengthFlags), propertyFlags),
-                ["bind"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "bind", Bind, 1, lengthFlags), propertyFlags),
-                ["arguments"] = _engine._callerCalleeArgumentsThrowerConfigurable,
-                ["caller"] = _engine._callerCalleeArgumentsThrowerConfigurable
+                ["toString"] = new PropertyDescriptor(new ClrFunctionInstance(_engine, "toString", ToString, 0, lengthFlags), propertyFlags),
+                ["apply"] = new PropertyDescriptor(new ClrFunctionInstance(_engine, "apply", Apply, 2, lengthFlags), propertyFlags),
+                ["call"] = new PropertyDescriptor(new ClrFunctionInstance(_engine, "call", CallImpl, 1, lengthFlags), propertyFlags),
+                ["bind"] = new PropertyDescriptor(new ClrFunctionInstance(_engine, "bind", Bind, 1, lengthFlags), propertyFlags),
+                ["arguments"] = new GetSetPropertyDescriptor.ThrowerPropertyDescriptor(_engine, PropertyFlag.Configurable | PropertyFlag.CustomJsValue),
+                ["caller"] = new GetSetPropertyDescriptor.ThrowerPropertyDescriptor(_engine, PropertyFlag.Configurable | PropertyFlag.CustomJsValue)
             };
             SetProperties(properties);
 

+ 1 - 1
Jint/Native/Function/ScriptFunctionInstance.cs

@@ -44,7 +44,7 @@ namespace Jint.Native.Function
 
             if (!function.Strict && !engine._isStrict && function.Function is not ArrowFunctionExpression)
             {
-                DefineOwnProperty(CommonProperties.Arguments, engine._callerCalleeArgumentsThrowerConfigurable);
+                DefineOwnProperty(CommonProperties.Arguments, new GetSetPropertyDescriptor.ThrowerPropertyDescriptor(engine, PropertyFlag.Configurable | PropertyFlag.CustomJsValue));
                 DefineOwnProperty(CommonProperties.Caller, new PropertyDescriptor(Undefined, PropertyFlag.Configurable));
             }
         }

+ 4 - 11
Jint/Native/Function/ThrowTypeError.cs

@@ -5,25 +5,18 @@ namespace Jint.Native.Function
 {
     public sealed class ThrowTypeError : FunctionInstance
     {
-        private static readonly JsString _functionName = new JsString("throwTypeError");
-
-        private readonly string _message;
-
-        public ThrowTypeError(
-            Engine engine,
-            Realm realm,
-            string message = null)
-            : base(engine, realm, _functionName)
+        public ThrowTypeError(Engine engine, Realm realm)
+            : base(engine, realm, null)
         {
-            _message = message;
             _length = PropertyDescriptor.AllForbiddenDescriptor.NumberZero;
+            _nameDescriptor = new PropertyDescriptor(JsString.Empty, PropertyFlag.AllForbidden);
             _environment = realm.GlobalEnv;
             PreventExtensions();
         }
 
         public override JsValue Call(JsValue thisObject, JsValue[] arguments)
         {
-            ExceptionHelper.ThrowTypeError(_realm, _message);
+            ExceptionHelper.ThrowTypeError(_realm);
             return null;
         }
     }

+ 3 - 6
Jint/Runtime/Descriptors/GetSetPropertyDescriptor.cs

@@ -44,18 +44,15 @@ namespace Jint.Runtime.Descriptors
         internal sealed class ThrowerPropertyDescriptor : PropertyDescriptor
         {
             private readonly Engine _engine;
-            private readonly string _message;
             private JsValue _thrower;
 
-            public ThrowerPropertyDescriptor(Engine engine, PropertyFlag flags, string message)
-                : base(flags)
+            public ThrowerPropertyDescriptor(Engine engine, PropertyFlag flags) : base(flags)
             {
                 _engine = engine;
-                _message = message;
             }
 
-            public override JsValue Get => _thrower ??= new ThrowTypeError(_engine, _engine.Realm, _message) { _prototype = _engine.Realm.Intrinsics.Function.PrototypeObject};
-            public override JsValue Set => _thrower ??= new ThrowTypeError(_engine, _engine.Realm, _message) { _prototype = _engine.Realm.Intrinsics.Function.PrototypeObject};
+            public override JsValue Get => _thrower ??= _engine.Realm.Intrinsics.ThrowTypeError;
+            public override JsValue Set => _thrower ??= _engine.Realm.Intrinsics.ThrowTypeError;
 
             protected internal override JsValue CustomValue
             {

+ 12 - 9
Jint/Runtime/Intrinsics.cs

@@ -40,6 +40,7 @@ namespace Jint.Runtime
         private readonly Realm _realm;
 
         // lazy properties
+        private ThrowTypeError _throwTypeError;
         private ErrorConstructor _error;
         private ErrorConstructor _evalError;
         private ErrorConstructor _rangeError;
@@ -221,25 +222,27 @@ namespace Jint.Runtime
             _eval ??= new EvalFunctionInstance(_engine, _realm, Function.PrototypeObject);
 
         public ErrorConstructor Error =>
-            _error ??= new ErrorConstructor(_engine, _realm, Function.PrototypeObject, Object.PrototypeObject, _errorFunctionName);
+            _error ??= new ErrorConstructor(_engine, _realm, Function.PrototypeObject, Object.PrototypeObject, _errorFunctionName, static intrinsics => intrinsics.Error.PrototypeObject);
 
         public ErrorConstructor EvalError =>
-            _evalError ??= new ErrorConstructor(_engine, _realm, Error, Error.PrototypeObject, _evalErrorFunctionName);
+            _evalError ??= new ErrorConstructor(_engine, _realm, Error, Error.PrototypeObject, _evalErrorFunctionName, static intrinsics => intrinsics.EvalError.PrototypeObject);
 
         public ErrorConstructor SyntaxError =>
-            _syntaxError ??= new ErrorConstructor(_engine, _realm, Error, Error.PrototypeObject, _syntaxErrorFunctionName);
+            _syntaxError ??= new ErrorConstructor(_engine, _realm, Error, Error.PrototypeObject, _syntaxErrorFunctionName, static intrinsics => intrinsics.SyntaxError.PrototypeObject);
 
         public ErrorConstructor TypeError =>
-            _typeError ??= new ErrorConstructor(_engine, _realm, Error, Error.PrototypeObject, _typeErrorFunctionName);
+            _typeError ??= new ErrorConstructor(_engine, _realm, Error, Error.PrototypeObject, _typeErrorFunctionName, static intrinsics => intrinsics.TypeError.PrototypeObject);
 
         public ErrorConstructor RangeError =>
-            _rangeError ??=
-                new ErrorConstructor(_engine, _realm, Error, Error.PrototypeObject, _rangeErrorFunctionName);
+            _rangeError ??= new ErrorConstructor(_engine, _realm, Error, Error.PrototypeObject, _rangeErrorFunctionName, static intrinsics => intrinsics.RangeError.PrototypeObject);
 
-        public ErrorConstructor ReferenceError
-            => _referenceError ??= new ErrorConstructor(_engine, _realm, Error, Error.PrototypeObject, _referenceErrorFunctionName);
+        public ErrorConstructor ReferenceError =>
+            _referenceError ??= new ErrorConstructor(_engine, _realm, Error, Error.PrototypeObject, _referenceErrorFunctionName, static intrinsics => intrinsics.ReferenceError.PrototypeObject);
 
         public ErrorConstructor UriError =>
-            _uriError ??= new ErrorConstructor(_engine, _realm, Error, Error.PrototypeObject, _uriErrorFunctionName);
+            _uriError ??= new ErrorConstructor(_engine, _realm, Error, Error.PrototypeObject, _uriErrorFunctionName, static intrinsics => intrinsics.UriError.PrototypeObject);
+
+        public ThrowTypeError ThrowTypeError =>
+            _throwTypeError ??= new ThrowTypeError(_engine, _engine.Realm) { _prototype = _engine.Realm.Intrinsics.Function.PrototypeObject };
     }
 }