2
0
Sebastien Ros 12 жил өмнө
parent
commit
9e9f0ef5ad

+ 0 - 3
Jint.Benchmark/Program.cs

@@ -1,8 +1,5 @@
 using System;
 using System.Diagnostics;
-using System.IO;
-using System.Reflection;
-using Jint.Parser;
 
 namespace Jint.Benchmark
 {

+ 12 - 12
Jint/Engine.cs

@@ -49,14 +49,14 @@ namespace Jint
             Date = new DateConstructor(this);
             Math = MathInstance.CreateMathObject(this, RootObject);
 
-            Global.Set("Object", Object);
-            Global.Set("Function", Function);
-            Global.Set("Array", Array);
-            Global.Set("String", String);
-            Global.Set("Number", Number);
-            Global.Set("Boolean", Boolean);
-            Global.Set("Date", Date);
-            Global.Set("Math", Math);
+            Global.FastDefineDataDescriptor("Object", Object);
+            Global.FastDefineDataDescriptor("Function", Function);
+            Global.FastDefineDataDescriptor("Array", Array);
+            Global.FastDefineDataDescriptor("String", String);
+            Global.FastDefineDataDescriptor("Number", Number);
+            Global.FastDefineDataDescriptor("Boolean", Boolean);
+            Global.FastDefineDataDescriptor("Date", Date);
+            Global.FastDefineDataDescriptor("Math", Math);
 
             // create the global environment http://www.ecma-international.org/ecma-262/5.1/#sec-10.2.3
             GlobalEnvironment = LexicalEnvironment.NewObjectEnvironment(Global, null, true);
@@ -75,7 +75,7 @@ namespace Jint
             {
                 foreach (var entry in Options.GetDelegates())
                 {
-                    Global.DefineOwnProperty(entry.Key, new DataDescriptor(new DelegateWrapper(this, entry.Value)), false);
+                    Global.FastDefineDataDescriptor(entry.Key, new DelegateWrapper(this, entry.Value));
                 }
             }
 
@@ -359,7 +359,7 @@ namespace Jint
             foreach (var functionDeclaration in functionScope.FunctionDeclarations)
             {
                 var fn = functionDeclaration.Id.Name;
-                var fo = this.Function.CreateFunctionObject(functionDeclaration);
+                var fo = Function.CreateFunctionObject(functionDeclaration);
                 var funcAlreadyDeclared = env.HasBinding(fn);
                 if (!funcAlreadyDeclared)
                 {
@@ -367,9 +367,9 @@ namespace Jint
                 }
                 else
                 {
-                    if (env == this.GlobalEnvironment.Record)
+                    if (env == GlobalEnvironment.Record)
                     {
-                        var go = this.Global;
+                        var go = Global;
                         var existingProp = go.GetProperty(fn);
                         if (existingProp.Configurable)
                         {

+ 12 - 0
Jint/Native/Object/ObjectInstance.cs

@@ -77,6 +77,7 @@ namespace Jint.Native.Object
                 Put(name, value, false);
             }
         }
+
         /// <summary>
         /// Returns the Property Descriptor of the named 
         /// own property of this object, or undefined if 
@@ -468,5 +469,16 @@ namespace Jint.Native.Object
 
             return true;
         }
+
+        /// <summary>
+        /// Optimized version of [[Set]] when the property is known to be undeclared already
+        /// </summary>
+        /// <param name="name"></param>
+        /// <param name="value"></param>
+        public void FastDefineDataDescriptor(string name, object value)
+        {
+            Properties.Add(name, new DataDescriptor(value) { Configurable = true, Enumerable = true, Writable = true });
+        }
+
     }
 }

+ 16 - 16
Jint/Runtime/TypeConverter.cs

@@ -42,6 +42,11 @@ namespace Jint.Runtime
         /// <returns></returns>
         public static bool ToBoolean(object o)
         {
+            if (o is bool)
+            {
+                return (bool)o;
+            }
+            
             if (o == Undefined.Instance || o == Null.Instance)
             {
                 return false;
@@ -53,11 +58,6 @@ namespace Jint.Runtime
                 o = p.PrimitiveValue;
             }
 
-            if (o is bool)
-            {
-                return (bool) o;
-            }
-            
             if (o is double)
             {
                 var n = (double) o;
@@ -94,6 +94,11 @@ namespace Jint.Runtime
         /// <returns></returns>
         public static double ToNumber(object o)
         {
+            if (o is double)
+            {
+                return (double)o;
+            }
+
             if (o == Undefined.Instance)
             {
                 return double.NaN;
@@ -109,11 +114,6 @@ namespace Jint.Runtime
                 return (bool)o ? 1 : 0;
             }
 
-            if (o is double)
-            {
-                return (double)o;
-            }
-
             var s = o as string;
             if (s != null)
             {
@@ -195,6 +195,12 @@ namespace Jint.Runtime
         /// <returns></returns>
         public static string ToString(object o)
         {
+            var s = o as string;
+            if (s != null)
+            {
+                return s;
+            }
+
             if (o == Undefined.Instance)
             {
                 return "undefined";
@@ -232,12 +238,6 @@ namespace Jint.Runtime
                 return n.ToString();
             }
 
-            var s = o as string;
-            if (s != null)
-            {
-                return s;
-            }
-
             return ToString(ToPrimitive(o, TypeCode.String));
         }