Ver código fonte

Refactoring Clr property descriptors

Sebastien Ros 12 anos atrás
pai
commit
c94e01068e

+ 2 - 1
Jint/Jint.csproj

@@ -116,7 +116,8 @@
     <Compile Include="Runtime\Descriptors\AccessorDescriptor.cs" />
     <Compile Include="Runtime\Descriptors\DataDescriptor.cs" />
     <Compile Include="Runtime\Descriptors\PropertyDescriptor.cs" />
-    <Compile Include="Runtime\Descriptors\Specialized\MethodPropertyDescriptor.cs" />
+    <Compile Include="Runtime\Descriptors\Specialized\ClrDataDescriptor.cs" />
+    <Compile Include="Runtime\Descriptors\Specialized\ClrAccessDescriptor.cs" />
     <Compile Include="Runtime\Environments\DeclarativeEnvironmentRecord.cs" />
     <Compile Include="Runtime\Environments\EnvironmentRecord.cs" />
     <Compile Include="Runtime\Environments\ExecutionContext.cs" />

+ 8 - 10
Jint/Native/Array/ArrayConstructor.cs

@@ -1,10 +1,7 @@
-using System;
-using Jint.Native.Errors;
-using Jint.Native.Function;
+using Jint.Native.Function;
 using Jint.Native.Object;
 using Jint.Runtime.Descriptors;
 using Jint.Runtime.Descriptors.Specialized;
-using Jint.Runtime.Interop;
 
 namespace Jint.Native.Array
 {
@@ -21,10 +18,10 @@ namespace Jint.Native.Array
             this.Prototype.DefineOwnProperty("prototype", new DataDescriptor(this.Prototype) { Writable = true, Enumerable = false, Configurable = false }, false);
                                   
             // Array prototype properties
-            this.Prototype.DefineOwnProperty("length", new MethodPropertyDescriptor<ArrayInstance>(_engine, x => x.Length), false);
+            this.Prototype.DefineOwnProperty("length", new ClrAccessDescriptor<ArrayInstance>(_engine, x => x.Length), false);
 
-            this.Prototype.DefineOwnProperty("push", new DataDescriptor(new ClrFunctionInstance(engine, (Action<ArrayInstance, object>)Push)), false);
-            this.Prototype.DefineOwnProperty("pop", new DataDescriptor(new ClrFunctionInstance(engine, (Func<ArrayInstance, object>)Pop)), false);
+            this.Prototype.DefineOwnProperty("push", new ClrDataDescriptor<ArrayInstance>(engine, Push), false);
+            this.Prototype.DefineOwnProperty("pop", new ClrDataDescriptor<ArrayInstance>(engine, Pop), false);
         }
 
         public override object Call(object thisObject, object[] arguments)
@@ -44,12 +41,13 @@ namespace Jint.Native.Array
             return instance;
         }
 
-        private static void Push(ArrayInstance thisObject, object o)
+        private static object Push(ArrayInstance thisObject, object[] arguments)
         {
-            thisObject.Push(o);
+            thisObject.Push(arguments[0]);
+            return Undefined.Instance;
         }
 
-        private static object Pop(ArrayInstance thisObject)
+        private static object Pop(ArrayInstance thisObject, object[] arguments)
         {
             return thisObject.Pop();
         }

+ 7 - 4
Jint/Native/Object/ObjectConstructor.cs

@@ -1,6 +1,8 @@
 using System;
 using Jint.Native.Function;
+using Jint.Runtime;
 using Jint.Runtime.Descriptors;
+using Jint.Runtime.Descriptors.Specialized;
 using Jint.Runtime.Interop;
 
 namespace Jint.Native.Object
@@ -12,8 +14,8 @@ namespace Jint.Native.Object
         public ObjectConstructor(Engine engine) : base(engine, engine.RootFunction, null, null)
         {
             _engine = engine;
-            engine.RootFunction.DefineOwnProperty("hasOwnProperty", new DataDescriptor(new ClrFunctionInstance(engine, (Func<ObjectInstance, string, bool>)HasOwnProperty)), false);
-            engine.RootFunction.DefineOwnProperty("toString", new DataDescriptor(new ClrFunctionInstance(engine, (Func<ObjectInstance, string>)ToString)), false);
+            engine.RootFunction.DefineOwnProperty("hasOwnProperty", new ClrDataDescriptor<ObjectInstance>(engine, HasOwnProperty), false);
+            engine.RootFunction.DefineOwnProperty("toString", new ClrDataDescriptor<ObjectInstance>(engine, ToString), false);
         }
 
         public override object Call(object thisObject, object[] arguments)
@@ -31,13 +33,14 @@ namespace Jint.Native.Object
             return instance;
         }
 
-        private static bool HasOwnProperty(ObjectInstance thisObject, string propertyName)
+        private static object HasOwnProperty(ObjectInstance thisObject, object[] arguments)
         {
+            var propertyName = TypeConverter.ToString(arguments[0]);
             var desc = thisObject.GetOwnProperty(propertyName);
             return desc != PropertyDescriptor.Undefined;
         }
 
-        private static string ToString(ObjectInstance thisObject)
+        private static object ToString(ObjectInstance thisObject, object[] arguments)
         {
             if (thisObject == null || thisObject == Undefined.Instance)
             {

+ 3 - 3
Jint/Runtime/Descriptors/Specialized/MethodPropertyDescriptor.cs → Jint/Runtime/Descriptors/Specialized/ClrAccessDescriptor.cs

@@ -3,14 +3,14 @@ using Jint.Runtime.Interop;
 
 namespace Jint.Runtime.Descriptors.Specialized
 {
-    public sealed class MethodPropertyDescriptor<T> : AccessorDescriptor
+    public sealed class ClrAccessDescriptor<T> : AccessorDescriptor
     {
-        public MethodPropertyDescriptor(Engine engine, Func<T, object> get)
+        public ClrAccessDescriptor(Engine engine, Func<T, object> get)
             : this(engine, get, null)
         {
         }
 
-        public MethodPropertyDescriptor(Engine engine, Func<T, object> get, Action<T, object> set)
+        public ClrAccessDescriptor(Engine engine, Func<T, object> get, Action<T, object> set)
             : base(
                 new GetterFunctionInstance<T>(engine, get),
                 set == null ? null : new SetterFunctionInstance<T>(engine, set)

+ 23 - 0
Jint/Runtime/Descriptors/Specialized/ClrDataDescriptor.cs

@@ -0,0 +1,23 @@
+using System;
+using Jint.Runtime.Interop;
+
+namespace Jint.Runtime.Descriptors.Specialized
+{
+    public sealed class ClrDataDescriptor<T> : DataDescriptor
+    {
+        public ClrDataDescriptor(Engine engine, Func<T, object[], object> func) 
+            : base(new ClrFunctionInstance<T>(engine, func))
+        {
+        }
+
+        public override bool IsAccessorDescriptor()
+        {
+            return false;
+        }
+
+        public override bool IsDataDescriptor()
+        {
+            return true;
+        }
+    }
+}

+ 6 - 10
Jint/Runtime/Interop/ClrFunctionInstance.cs

@@ -5,18 +5,18 @@ using Jint.Native.Function;
 namespace Jint.Runtime.Interop
 {
     /// <summary>
-    /// Reprensents a Property wrapper for static methods representing built-in properties.
+    /// Wraps a Clr method into a FunctionInstance
     /// </summary>
-    public sealed class ClrFunctionInstance : FunctionInstance
+    public sealed class ClrFunctionInstance<T> : FunctionInstance
     {
         private readonly Engine _engine;
-        private readonly Delegate _d;
+        private readonly Func<T, object[], object> _func;
 
-        public ClrFunctionInstance(Engine engine, Delegate d)
+        public ClrFunctionInstance(Engine engine, Func<T, object[], object> func)
             : base(engine, null, null, null)
         {
             _engine = engine;
-            _d = d;
+            _func = func;
         }
 
         public override object Call(object thisObject, object[] arguments)
@@ -24,12 +24,8 @@ namespace Jint.Runtime.Interop
             // initialize Return flag
             _engine.CurrentExecutionContext.Return = Undefined.Instance;
 
-            // built-in static method must have their first parameter as 'this'
-            var allArguments = new object[arguments.Length + 1];
-            allArguments[0] = thisObject;
-            Array.Copy(arguments, 0, allArguments, 1, arguments.Length);
 
-            return _d.DynamicInvoke(allArguments);
+            return _func((T) thisObject, arguments);
         }
     }
 }