Browse Source

Caching namespace/type references resolution

Sebastien Ros 11 years ago
parent
commit
089e5cb969
2 changed files with 21 additions and 2 deletions
  1. 3 0
      Jint/Engine.cs
  2. 18 2
      Jint/Runtime/Interop/NamespaceReference.cs

+ 3 - 0
Jint/Engine.cs

@@ -33,6 +33,9 @@ namespace Jint
         private JsValue _completionValue = JsValue.Undefined;
         private JsValue _completionValue = JsValue.Undefined;
         private int _statementsCount;
         private int _statementsCount;
         
         
+        // cache of types used when resolving CLR type names
+        internal Dictionary<string, Type> TypeCache = new Dictionary<string, Type>(); 
+
         public Engine() : this(null)
         public Engine() : this(null)
         {
         {
         }
         }

+ 18 - 2
Jint/Runtime/Interop/NamespaceReference.cs

@@ -45,11 +45,24 @@ namespace Jint.Runtime.Interop
         public override JsValue Get(string propertyName)
         public override JsValue Get(string propertyName)
         {
         {
             var newPath = _path + "." + propertyName;
             var newPath = _path + "." + propertyName;
-            
+
+            Type type;
+
+            if (Engine.TypeCache.TryGetValue(newPath, out type))
+            {
+                if (type == null)
+                {
+                    return new NamespaceReference(Engine, newPath);
+                }
+
+                return TypeReference.CreateTypeReference(Engine, type);
+            }
+
             // search for type in mscorlib
             // search for type in mscorlib
-            var type = Type.GetType(newPath);
+            type = Type.GetType(newPath);
             if (type != null)
             if (type != null)
             {
             {
+                Engine.TypeCache.Add(newPath, type);
                 return TypeReference.CreateTypeReference(Engine, type);
                 return TypeReference.CreateTypeReference(Engine, type);
             }
             }
 
 
@@ -59,6 +72,7 @@ namespace Jint.Runtime.Interop
                 type = assembly.GetType(newPath);
                 type = assembly.GetType(newPath);
                 if (type != null)
                 if (type != null)
                 {
                 {
+                    Engine.TypeCache.Add(newPath, type);
                     return TypeReference.CreateTypeReference(Engine, type);
                     return TypeReference.CreateTypeReference(Engine, type);
                 }
                 }
             }
             }
@@ -69,12 +83,14 @@ namespace Jint.Runtime.Interop
                 type = assembly.GetType(newPath);
                 type = assembly.GetType(newPath);
                 if (type != null)
                 if (type != null)
                 {
                 {
+                    Engine.TypeCache.Add(newPath, type);
                     return TypeReference.CreateTypeReference(Engine, type);
                     return TypeReference.CreateTypeReference(Engine, type);
                 }
                 }
             }
             }
 
 
             // the new path doesn't represent a known class, thus return a new namespace instance
             // the new path doesn't represent a known class, thus return a new namespace instance
 
 
+            Engine.TypeCache.Add(newPath, null);
             return new NamespaceReference(Engine, newPath);
             return new NamespaceReference(Engine, newPath);
         }
         }