Browse Source

Adding ability to convert ObjectInstance into object

Using an ExpandoObject which can be casted as a
IDictionary<string,object> if necessary
Sebastien Ros 11 years ago
parent
commit
6d0b917dba
3 changed files with 36 additions and 1 deletions
  1. 19 0
      Jint.Tests/Runtime/InteropTests.cs
  2. 1 1
      Jint/Engine.cs
  3. 16 0
      Jint/Native/JsValue.cs

+ 19 - 0
Jint.Tests/Runtime/InteropTests.cs

@@ -1,4 +1,5 @@
 using System;
 using System;
+using System.Collections.Generic;
 using System.Runtime.Remoting.Messaging;
 using System.Runtime.Remoting.Messaging;
 using Jint.Native;
 using Jint.Native;
 using Jint.Native.Object;
 using Jint.Native.Object;
@@ -365,6 +366,24 @@ namespace Jint.Tests.Runtime
             Assert.Equal("foo", value);
             Assert.Equal("foo", value);
         }
         }
 
 
+        [Fact]
+        public void ShouldConvertObjectInstanceToExpando()
+        {
+            _engine.Execute("var o = {a: 1, b: 'foo'}");
+            var result = _engine.GetGlobalValue("o");
+
+            dynamic value = result.ToObject();
+
+            Assert.Equal(1, value.a);
+            Assert.Equal("foo", value.b);
+
+            var dic = (IDictionary<string, object>)result.ToObject();
+
+            Assert.Equal(1d, dic["a"]);
+            Assert.Equal("foo", dic["b"]);
+
+        }
+
 
 
         public class Person
         public class Person
         {
         {

+ 1 - 1
Jint/Engine.cs

@@ -524,7 +524,7 @@ namespace Jint
         }
         }
 
 
 
 
-        public object GetGlobalValue(string propertyName)
+        public JsValue GetGlobalValue(string propertyName)
         {
         {
             if (System.String.IsNullOrEmpty(propertyName))
             if (System.String.IsNullOrEmpty(propertyName))
             {
             {

+ 16 - 0
Jint/Native/JsValue.cs

@@ -1,6 +1,8 @@
 using System;
 using System;
+using System.Collections.Generic;
 using System.Diagnostics;
 using System.Diagnostics;
 using System.Diagnostics.Contracts;
 using System.Diagnostics.Contracts;
+using System.Dynamic;
 using Jint.Native.Array;
 using Jint.Native.Array;
 using Jint.Native.Boolean;
 using Jint.Native.Boolean;
 using Jint.Native.Date;
 using Jint.Native.Date;
@@ -459,6 +461,20 @@ namespace Jint.Native
                             }
                             }
 
 
                             break;
                             break;
+
+                        case "Object":
+                            IDictionary<string, object> o = new ExpandoObject();
+                            foreach (var p in _object.Properties)
+                            {
+                                if (!p.Value.Enumerable.HasValue || p.Value.Enumerable.Value == false)
+                                {
+                                    continue;
+                                }
+
+                                o.Add(p.Key, _object.Get(p.Key).ToObject());
+                            }
+
+                            return o;
                     }
                     }