[banshee] [Ossifer.JavaScriptCore] Bind PropertyNameArray



commit 4712807dafe01f86fe5eaba70ae2d5ed334006c1
Author: Aaron Bockover <abockover novell com>
Date:   Wed Oct 20 23:37:06 2010 -0400

    [Ossifer.JavaScriptCore] Bind PropertyNameArray
    
    Property enumeration and access is now supported.

 .../Banshee.WebBrowser/Banshee.WebBrowser.csproj   |    1 +
 src/Core/Banshee.WebBrowser/Makefile.am            |    1 +
 .../Ossifer.JavaScriptCore/JSObject.cs             |   20 +++++
 .../Ossifer.JavaScriptCore/JSPropertyNameArray.cs  |   83 ++++++++++++++++++++
 .../Ossifer.JavaScriptCore/Tests/JSObjectTests.cs  |   41 ++++++++++-
 5 files changed, 145 insertions(+), 1 deletions(-)
---
diff --git a/src/Core/Banshee.WebBrowser/Banshee.WebBrowser.csproj b/src/Core/Banshee.WebBrowser/Banshee.WebBrowser.csproj
index 55666f0..ae0465f 100644
--- a/src/Core/Banshee.WebBrowser/Banshee.WebBrowser.csproj
+++ b/src/Core/Banshee.WebBrowser/Banshee.WebBrowser.csproj
@@ -124,5 +124,6 @@
     <Compile Include="Ossifer.JavaScriptCore\Tests\JSValueTests.cs" />
     <Compile Include="Ossifer.JavaScriptCore\Tests\JSStringTests.cs" />
     <Compile Include="Ossifer.JavaScriptCore\Tests\JSObjectTests.cs" />
+    <Compile Include="Ossifer.JavaScriptCore\JSPropertyNameArray.cs" />
   </ItemGroup>
 </Project>
diff --git a/src/Core/Banshee.WebBrowser/Makefile.am b/src/Core/Banshee.WebBrowser/Makefile.am
index 40d1505..bee71ed 100644
--- a/src/Core/Banshee.WebBrowser/Makefile.am
+++ b/src/Core/Banshee.WebBrowser/Makefile.am
@@ -24,6 +24,7 @@ SOURCES =  \
 	Ossifer.JavaScriptCore/JSObject.cs \
 	Ossifer.JavaScriptCore/JSPropertyAttribute.cs \
 	Ossifer.JavaScriptCore/JSPropertyNameAccumulator.cs \
+	Ossifer.JavaScriptCore/JSPropertyNameArray.cs \
 	Ossifer.JavaScriptCore/JSString.cs \
 	Ossifer.JavaScriptCore/JSType.cs \
 	Ossifer.JavaScriptCore/JSValue.cs \
diff --git a/src/Core/Banshee.WebBrowser/Ossifer.JavaScriptCore/JSObject.cs b/src/Core/Banshee.WebBrowser/Ossifer.JavaScriptCore/JSObject.cs
index 724a7c2..980f0a0 100644
--- a/src/Core/Banshee.WebBrowser/Ossifer.JavaScriptCore/JSObject.cs
+++ b/src/Core/Banshee.WebBrowser/Ossifer.JavaScriptCore/JSObject.cs
@@ -158,6 +158,26 @@ namespace Ossifer.JavaScriptCore
             }
         }
 
+        [DllImport (JSContext.NATIVE_IMPORT)]
+        private static extern IntPtr JSObjectCopyPropertyNames (IntPtr ctx, IntPtr obj);
+
+        private JSPropertyNameArray CopyPropertyNames ()
+        {
+            return new JSPropertyNameArray (JSObjectCopyPropertyNames (Context.Raw, Raw));
+        }
+
+        public string [] PropertyNames {
+            get {
+                var names_native = CopyPropertyNames ();
+                var names = new string [names_native.Count];
+                for (int i = 0; i < names.Length; i++) {
+                    names[i] = names_native[i];
+                }
+                names_native.Release ();
+                return names;
+            }
+        }
+
 #endregion
 
         [DllImport (JSContext.NATIVE_IMPORT)]
diff --git a/src/Core/Banshee.WebBrowser/Ossifer.JavaScriptCore/JSPropertyNameArray.cs b/src/Core/Banshee.WebBrowser/Ossifer.JavaScriptCore/JSPropertyNameArray.cs
new file mode 100644
index 0000000..64805a2
--- /dev/null
+++ b/src/Core/Banshee.WebBrowser/Ossifer.JavaScriptCore/JSPropertyNameArray.cs
@@ -0,0 +1,83 @@
+// 
+// JSPropertyNameArray.cs
+// 
+// Author:
+//   Aaron Bockover <abockover novell com>
+// 
+// Copyright 2010 Novell, Inc.
+// 
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+// 
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+
+using System;
+using System.Runtime.InteropServices;
+
+namespace Ossifer.JavaScriptCore
+{
+    public struct JSPropertyNameArray
+    {
+        #pragma warning disable 0414
+        private IntPtr raw;
+        #pragma warning restore 0414
+
+        public static JSPropertyNameArray Zero {
+            get { return new JSPropertyNameArray (IntPtr.Zero); }
+        }
+
+        public JSPropertyNameArray (IntPtr raw)
+        {
+            this.raw = raw;
+        }
+
+        [DllImport (JSContext.NATIVE_IMPORT)]
+        private static extern IntPtr JSPropertyNameArrayRetain (JSPropertyNameArray array);
+
+        public void Retain ()
+        {
+            JSPropertyNameArrayRetain (this);
+        }
+
+        [DllImport (JSContext.NATIVE_IMPORT)]
+        private static extern void JSPropertyNameArrayRelease (JSPropertyNameArray array);
+
+        public void Release ()
+        {
+            JSPropertyNameArrayRelease (this);
+        }
+
+        [DllImport (JSContext.NATIVE_IMPORT)]
+        private static extern IntPtr JSPropertyNameArrayGetCount (JSPropertyNameArray array);
+
+        public int Count {
+            get { return JSPropertyNameArrayGetCount (this).ToInt32 (); }
+        }
+
+        [DllImport (JSContext.NATIVE_IMPORT)]
+        private static extern IntPtr JSPropertyNameArrayGetNameAtIndex (JSPropertyNameArray array, IntPtr index);
+
+        public string GetNameAtIndex (int index)
+        {
+            return new JSString (JSPropertyNameArrayGetNameAtIndex (this, new IntPtr (index))).Value;
+        }
+
+        public string this[int index] {
+            get { return GetNameAtIndex (index); }
+        }
+    }
+}
+
diff --git a/src/Core/Banshee.WebBrowser/Ossifer.JavaScriptCore/Tests/JSObjectTests.cs b/src/Core/Banshee.WebBrowser/Ossifer.JavaScriptCore/Tests/JSObjectTests.cs
index bf86015..cac0d82 100644
--- a/src/Core/Banshee.WebBrowser/Ossifer.JavaScriptCore/Tests/JSObjectTests.cs
+++ b/src/Core/Banshee.WebBrowser/Ossifer.JavaScriptCore/Tests/JSObjectTests.cs
@@ -122,7 +122,7 @@ namespace Ossifer.JavaScriptCore.Tests
         }
 
         [Test]
-        public void DeletePropertyTestScripted ()
+        public void DeletePropertyScriptedTest ()
         {
             context.GlobalObject.SetProperty ("a", new JSValue (context, "apple"));
             context.GlobalObject.SetProperty ("b", new JSValue (context, "bear"));
@@ -146,6 +146,45 @@ namespace Ossifer.JavaScriptCore.Tests
             Assert.AreEqual ("{}", context.GlobalObject.ToJsonString (0));
         }
 
+        [Test]
+        public void PropertyEnumerationTest ()
+        {
+            context.EvaluateScript ("this.a = 1");
+            context.EvaluateScript ("this.b = 2");
+            context.EvaluateScript ("this.c = 3");
+            context.EvaluateScript ("this.d = 4");
+            context.EvaluateScript ("this.e = 5");
+
+            Assert.AreEqual ("{\"a\":1,\"b\":2,\"c\":3,\"d\":4,\"e\":5}",
+                context.GlobalObject.ToJsonString (0));
+
+            context.GlobalObject.SetProperty ("f", new JSValue (context, 6));
+            context.GlobalObject.SetProperty ("g", new JSValue (context, 7));
+            context.GlobalObject.SetProperty ("h", new JSValue (context, 8));
+
+            var names = context.GlobalObject.PropertyNames;
+
+            Assert.AreEqual (8, names.Length);
+
+            Assert.AreEqual ("a", names[0]);
+            Assert.AreEqual ("b", names[1]);
+            Assert.AreEqual ("c", names[2]);
+            Assert.AreEqual ("d", names[3]);
+            Assert.AreEqual ("e", names[4]);
+            Assert.AreEqual ("f", names[5]);
+            Assert.AreEqual ("g", names[6]);
+            Assert.AreEqual ("h", names[7]);
+
+            Assert.AreEqual (1, context.GlobalObject.GetProperty (names[0]).NumberValue);
+            Assert.AreEqual (2, context.GlobalObject.GetProperty (names[1]).NumberValue);
+            Assert.AreEqual (3, context.GlobalObject.GetProperty (names[2]).NumberValue);
+            Assert.AreEqual (4, context.GlobalObject.GetProperty (names[3]).NumberValue);
+            Assert.AreEqual (5, context.GlobalObject.GetProperty (names[4]).NumberValue);
+            Assert.AreEqual (6, context.GlobalObject.GetProperty (names[5]).NumberValue);
+            Assert.AreEqual (7, context.GlobalObject.GetProperty (names[6]).NumberValue);
+            Assert.AreEqual (8, context.GlobalObject.GetProperty (names[7]).NumberValue);
+        }
+
         private class GetPropertyClassTest : JSClassDefinition
         {
             protected override JSValue OnJSGetProperty (JSObject obj, string propertyName)



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]