Browse Source

Fixing Array member expression

Sebastien Ros 12 years ago
parent
commit
937ab7ef47

+ 13 - 3
Jint/Native/Array/ArrayConstructor.cs

@@ -1,4 +1,5 @@
-using System.Linq;
+using System;
+using System.Linq;
 using Jint.Native.Function;
 using Jint.Native.Object;
 using Jint.Runtime;
@@ -32,8 +33,17 @@ namespace Jint.Native.Array
         public ObjectInstance Construct(object[] arguments)
         {
             var instance = new ArrayInstance(_engine, Prototype);
-            instance.FastAddProperty("length", 0, true, false, true);
-            Push(instance, arguments);
+            
+            if (arguments.Length == 1 && TypeConverter.GetType(arguments[0]) == TypeCode.Double)
+            {
+                var length = TypeConverter.ToNumber(arguments[0]);
+                instance.FastAddProperty("length", length, true, false, true);
+            }
+            else
+            {
+                instance.FastAddProperty("length", 0, true, false, true);
+                Push(instance, arguments);
+            }
 
             return instance;
         }

+ 20 - 6
Jint/Runtime/ExpressionIntepreter.cs

@@ -516,16 +516,30 @@ namespace Jint.Runtime
             return obj;
         }
 
+        /// <summary>
+        /// http://www.ecma-international.org/ecma-262/5.1/#sec-11.2.1
+        /// </summary>
+        /// <param name="memberExpression"></param>
+        /// <returns></returns>
         public object EvaluateMemberExpression(MemberExpression memberExpression)
         {
-            var baseValue = _engine.GetValue(EvaluateExpression(memberExpression.Object));
+            var baseReference = EvaluateExpression(memberExpression.Object);
+            var baseValue = _engine.GetValue(baseReference);
 
-            string propertyName =
-                !memberExpression.Computed
-                    ? memberExpression.Property.As<Identifier>().Name // o.foo 
-                    : EvaluateExpression(memberExpression.Property).ToString(); // o['foo']
+            string propertyNameString;
+            if (!memberExpression.Computed) // index accessor ?
+            {
+                propertyNameString = memberExpression.Property.As<Identifier>().Name;
+            }
+            else
+            {
+                var propertyNameReference = EvaluateExpression(memberExpression.Property);
+                var propertyNameValue = _engine.GetValue(propertyNameReference);
+                TypeConverter.CheckObjectCoercible(_engine, baseValue);
+                propertyNameString = TypeConverter.ToString(propertyNameValue);
+            }
 
-            return new Reference(baseValue, propertyName, false);
+            return new Reference(baseValue, propertyNameString, _engine.Options.IsStrict());
         }
 
         public object EvaluateFunctionExpression(FunctionExpression functionExpression)

+ 8 - 0
Jint/Runtime/TypeConverter.cs

@@ -384,5 +384,13 @@ namespace Jint.Runtime
 
             return TypeCode.Object;
         }
+
+        public static void CheckObjectCoercible(Engine engine, object o)
+        {
+            if (o == Undefined.Instance || o == Null.Instance)
+            {
+                throw new JavaScriptException(engine.TypeError);
+            }
+        }
     }
 }