changeset 133:a9c0db56e06b

Added a context.has_property() function.
author Atul Varma <varmaa@toolness.com>
date Sun, 23 Aug 2009 19:18:19 -0700
parents 537cf7deadc9
children 0c81cb18c935
files src/context.cpp tests/test_pymonkey.py
diffstat 2 files changed, 42 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/src/context.cpp	Sun Aug 23 18:43:46 2009 -0700
+++ b/src/context.cpp	Sun Aug 23 19:18:19 2009 -0700
@@ -222,6 +222,37 @@
 }
 
 static PyObject *
+PYM_hasProperty(PYM_JSContextObject *self, PyObject *args)
+{
+  PYM_SANITY_CHECK(self->runtime);
+  PYM_JSObject *object;
+  char *buffer = NULL;
+  int size;
+
+  if (!PyArg_ParseTuple(args, "O!es#", &PYM_JSObjectType, &object,
+                        "utf-16", &buffer, &size))
+    return NULL;
+
+  PYM_UTF16String str(buffer, size);
+
+  PYM_ENSURE_RUNTIME_MATCH(self->runtime, object->runtime);
+
+  JSBool hasProperty;
+  JSBool result;
+  result = JS_HasUCProperty(self->cx, object->obj, str.jsbuffer,
+                            str.jslen, &hasProperty);
+
+  if (!result) {
+    PYM_jsExceptionToPython(self);
+    return NULL;
+  }
+
+  if (hasProperty)
+    Py_RETURN_TRUE;
+  Py_RETURN_FALSE;
+}
+
+static PyObject *
 PYM_getProperty(PYM_JSContextObject *self, PyObject *args)
 {
   PYM_SANITY_CHECK(self->runtime);
@@ -470,6 +501,8 @@
    "Defines a property on an object."},
   {"get_property", (PyCFunction) PYM_getProperty, METH_VARARGS,
    "Gets the given property for the given JavaScript object."},
+  {"has_property", (PyCFunction) PYM_hasProperty, METH_VARARGS,
+   "Returns whether the given JavaScript object has the given property."},
   {"gc", (PyCFunction) PYM_gc, METH_VARARGS,
    "Performs garbage collection on the context's runtime."},
   {"set_operation_callback", (PyCFunction) PYM_setOperationCallback,
--- a/tests/test_pymonkey.py	Sun Aug 23 18:43:46 2009 -0700
+++ b/tests/test_pymonkey.py	Sun Aug 23 19:18:19 2009 -0700
@@ -344,6 +344,15 @@
         self.assertEqual(self._evalJsWrappedPyFunc(hai2u, 'hai2u()'),
                          2147483647)
 
+    def testHasPropertyWorks(self):
+        cx = pymonkey.Runtime().new_context()
+        obj = cx.new_object()
+        cx.init_standard_classes(obj)
+        foo = cx.new_object()
+        cx.define_property(obj, u"foo\u2026", foo)
+        self.assertTrue(cx.has_property(obj, u"foo\u2026"))
+        self.assertFalse(cx.has_property(obj, "bar"))
+
     def testDefinePropertyWorksWithUnicodePropertyNames(self):
         cx = pymonkey.Runtime().new_context()
         obj = cx.new_object()