[banshee] [Ossifer.JavaScriptCore] Has/Get/DeleteProperty



commit e77d810ce7db9ffb4e87c1132d98a9dfc0e50e5c
Author: Aaron Bockover <abockover novell com>
Date:   Wed Oct 20 15:18:51 2010 -0400

    [Ossifer.JavaScriptCore] Has/Get/DeleteProperty
    
    Implemented the HasProperty, GetProperty, and DeleteProperty methods.
    Also added the IsFunction property. Implemented tests.

 .../Ossifer.JavaScriptCore/JSObject.cs             |   67 ++++++++++++++++++--
 .../Ossifer.JavaScriptCore/Tests/JSObjectTests.cs  |   66 +++++++++++++++++++
 2 files changed, 128 insertions(+), 5 deletions(-)
---
diff --git a/src/Core/Banshee.WebBrowser/Ossifer.JavaScriptCore/JSObject.cs b/src/Core/Banshee.WebBrowser/Ossifer.JavaScriptCore/JSObject.cs
index 37055f1..724a7c2 100644
--- a/src/Core/Banshee.WebBrowser/Ossifer.JavaScriptCore/JSObject.cs
+++ b/src/Core/Banshee.WebBrowser/Ossifer.JavaScriptCore/JSObject.cs
@@ -52,7 +52,8 @@ namespace Ossifer.JavaScriptCore
         }
 
         public JSObject (JSContext context, JSClass jsClass)
-            : base (context, JSObjectMake (context.Raw, jsClass.Raw, IntPtr.Zero))
+            : base (context, JSObjectMake (context.Raw,
+                jsClass == null ? IntPtr.Zero : jsClass.Raw, IntPtr.Zero))
         {
         }
 
@@ -89,17 +90,48 @@ namespace Ossifer.JavaScriptCore
         public delegate IntPtr
         ConvertToTypeCallback (IntPtr ctx, IntPtr obj, JSType type, ref IntPtr exception);
 
+#region Property API
+
+        [DllImport (JSContext.NATIVE_IMPORT)]
+        private static extern bool JSObjectHasProperty (IntPtr ctx, IntPtr obj, JSString propertyName);
+
+        public bool HasProperty (string propertyName)
+        {
+            var property = JSString.New (propertyName);
+            try {
+                return JSObjectHasProperty (Context.Raw, Raw, property);
+            } finally {
+                property.Release ();
+            }
+        }
+
+        [DllImport (JSContext.NATIVE_IMPORT)]
+        private static extern IntPtr JSObjectGetProperty (IntPtr ctx, IntPtr obj, JSString propertyName, ref IntPtr exception);
+
+        public JSValue GetProperty (string propertyName)
+        {
+            var exception = IntPtr.Zero;
+            var property = JSString.New (propertyName);
+            try {
+                var result = JSObjectGetProperty (Context.Raw, Raw, property, ref exception);
+                JSException.Proxy (Context, exception);
+                return new JSValue (Context, result);
+            } finally {
+                property.Release ();
+            }
+        }
+
         [DllImport (JSContext.NATIVE_IMPORT)]
         private static extern void JSObjectSetProperty (IntPtr ctx, IntPtr obj, JSString propertyName,
             IntPtr value, JSPropertyAttribute attributes, ref IntPtr exception);
 
-        public void SetProperty (JSContext context, string propertyName, JSValue value, JSPropertyAttribute attributes)
+        public void SetProperty (string propertyName, JSValue value, JSPropertyAttribute attributes)
         {
             var exception = IntPtr.Zero;
             var property = JSString.New (propertyName);
             try {
-                JSObjectSetProperty (context.Raw, Raw, property, value.Raw, attributes, ref exception);
-                JSException.Proxy (context, exception);
+                JSObjectSetProperty (Context.Raw, Raw, property, value.Raw, attributes, ref exception);
+                JSException.Proxy (Context, exception);
             } finally {
                 property.Release ();
             }
@@ -107,7 +139,32 @@ namespace Ossifer.JavaScriptCore
 
         public void SetProperty (string propertyName, JSValue value)
         {
-            SetProperty (Context, propertyName, value, JSPropertyAttribute.None);
+            SetProperty (propertyName, value, JSPropertyAttribute.None);
+        }
+
+        [DllImport (JSContext.NATIVE_IMPORT)]
+        private static extern bool JSObjectDeleteProperty (IntPtr ctx, IntPtr obj, JSString propertyName, ref IntPtr exception);
+
+        public bool DeleteProperty (string propertyName)
+        {
+            var exception = IntPtr.Zero;
+            var property = JSString.New (propertyName);
+            try {
+                var result = JSObjectDeleteProperty (Context.Raw, Raw, property, ref exception);
+                JSException.Proxy (Context, exception);
+                return result;
+            } finally {
+                property.Release ();
+            }
+        }
+
+#endregion
+
+        [DllImport (JSContext.NATIVE_IMPORT)]
+        private static extern bool JSObjectIsFunction (IntPtr ctx, IntPtr obj);
+
+        public bool IsFunction {
+            get { return JSObjectIsFunction (Context.Raw, Raw); }
         }
     }
 }
diff --git a/src/Core/Banshee.WebBrowser/Ossifer.JavaScriptCore/Tests/JSObjectTests.cs b/src/Core/Banshee.WebBrowser/Ossifer.JavaScriptCore/Tests/JSObjectTests.cs
index df839e8..bf86015 100644
--- a/src/Core/Banshee.WebBrowser/Ossifer.JavaScriptCore/Tests/JSObjectTests.cs
+++ b/src/Core/Banshee.WebBrowser/Ossifer.JavaScriptCore/Tests/JSObjectTests.cs
@@ -56,8 +56,74 @@ namespace Ossifer.JavaScriptCore.Tests
         }
 
         [Test]
+        public void SetPropertyTest ()
+        {
+            var obj = new JSObject (context, null);
+            var prop = new JSValue (context, "happy days");
+
+            obj.SetProperty ("str", prop);
+            Assert.IsTrue (obj.HasProperty ("str"));
+            Assert.IsTrue (obj.GetProperty ("str").IsEqual (prop));
+            Assert.IsTrue (obj.GetProperty ("str").IsStrictEqual (prop));
+            Assert.AreEqual ("happy days", obj.GetProperty ("str").StringValue);
+
+            obj.SetProperty ("str", obj);
+            Assert.IsTrue (obj.GetProperty ("str").IsEqual (obj));
+            Assert.IsTrue (obj.GetProperty ("str").IsStrictEqual (obj));
+        }
+
+        [Test]
         public void DeletePropertyTest ()
         {
+            var obj = new JSObject (context, null);
+            obj.SetProperty ("foo", new JSValue (context, "bar"));
+            Assert.IsTrue (obj.HasProperty ("foo"));
+            Assert.IsTrue (obj.DeleteProperty ("foo"));
+            Assert.IsFalse (obj.HasProperty ("foo"));
+            obj.SetProperty ("foo", new JSValue (context, 99));
+            Assert.IsTrue (obj.HasProperty ("foo"));
+            Assert.IsTrue (obj.DeleteProperty ("foo"));
+            Assert.IsFalse (obj.HasProperty ("foo"));
+        }
+
+        [Test]
+        public void DeleteDontDeletePropertyTest ()
+        {
+            var obj = new JSObject (context, null);
+            obj.SetProperty ("foo", new JSValue (context, "i am permanent"), JSPropertyAttribute.DontDelete);
+            Assert.IsTrue (obj.HasProperty ("foo"));
+            Assert.IsFalse (obj.DeleteProperty ("foo"));
+            Assert.IsTrue (obj.HasProperty ("foo"));
+        }
+
+        [Test]
+        public void ReadOnlyPropertyTest ()
+        {
+            var obj = new JSObject (context, null);
+            obj.SetProperty ("foo", new JSValue (context, "bar"), JSPropertyAttribute.ReadOnly);
+            Assert.AreEqual ("bar", obj.GetProperty ("foo").StringValue);
+            obj.SetProperty ("foo", new JSValue (context, "baz"));
+            Assert.AreEqual ("bar", obj.GetProperty ("foo").StringValue);
+            Assert.IsTrue (obj.DeleteProperty ("foo"));
+            Assert.IsFalse (obj.HasProperty ("foo"));
+        }
+
+        [Test]
+        public void ReadOnlyDontDeletePropertyTest ()
+        {
+            var obj = new JSObject (context, null);
+            obj.SetProperty ("foo", new JSValue (context, "bar"),
+                JSPropertyAttribute.ReadOnly | JSPropertyAttribute.DontDelete);
+            Assert.AreEqual ("bar", obj.GetProperty ("foo").StringValue);
+            obj.SetProperty ("foo", new JSValue (context, "baz"));
+            Assert.AreEqual ("bar", obj.GetProperty ("foo").StringValue);
+            Assert.IsFalse (obj.DeleteProperty ("foo"));
+            Assert.IsTrue (obj.HasProperty ("foo"));
+        }
+
+        [Test]
+        public void DeletePropertyTestScripted ()
+        {
             context.GlobalObject.SetProperty ("a", new JSValue (context, "apple"));
             context.GlobalObject.SetProperty ("b", new JSValue (context, "bear"));
             context.GlobalObject.SetProperty ("c", new JSValue (context, "car"));



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