changeset 104:00c1351b3e82

Added support for unicode property names in context.define_property().
author Atul Varma <varmaa@toolness.com>
date Sat, 15 Aug 2009 22:48:13 -0700
parents 257de12e58c4
children 9d4cd0803df5
files context.cpp test_pymonkey.py
diffstat 2 files changed, 30 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/context.cpp	Sat Aug 15 17:38:03 2009 -0700
+++ b/context.cpp	Sat Aug 15 22:48:13 2009 -0700
@@ -309,27 +309,38 @@
   PYM_SANITY_CHECK(self->runtime);
   PYM_JSObject *object;
   PyObject *value;
-  const char *name;
+  char *name = NULL;
+  int namelen;
 
-  if (!PyArg_ParseTuple(args, "O!sO", &PYM_JSObjectType, &object,
-                        &name, &value))
+  if (!PyArg_ParseTuple(args, "O!es#O", &PYM_JSObjectType, &object,
+                        "utf-16", &name, &namelen, &value))
     return NULL;
 
-  PYM_ENSURE_RUNTIME_MATCH(self->runtime, object->runtime);
+  if (self->runtime != object->runtime) {
+    PyMem_Free(name);
+    PYM_ENSURE_RUNTIME_MATCH(self->runtime, object->runtime);
+  }
+
   jsval jsValue;
 
-  if (PYM_pyObjectToJsval(self, value, &jsValue) == -1)
+  if (PYM_pyObjectToJsval(self, value, &jsValue) == -1) {
+    PyMem_Free(name);
     return NULL;
+  }
 
-  // TODO: Support unicode naming.
-  if (!JS_DefineProperty(self->cx, object->obj, name, jsValue,
-                         NULL, NULL, JSPROP_ENUMERATE)) {
+  // Note that we're manipulating buffer and size here to get rid of
+  // the BOM.
+  if (!JS_DefineUCProperty(self->cx, object->obj, (jschar *) (name + 2),
+                           (namelen / 2) - 1, jsValue, NULL, NULL,
+                           JSPROP_ENUMERATE)) {
     // TODO: There's probably an exception pending on self->cx,
     // what should we do about it?
+    PyMem_Free(name);
     PyErr_SetString(PYM_error, "JS_DefineProperty() failed");
     return NULL;
   }
 
+  PyMem_Free(name);
   Py_RETURN_NONE;
 }
 
--- a/test_pymonkey.py	Sat Aug 15 17:38:03 2009 -0700
+++ b/test_pymonkey.py	Sat Aug 15 22:48:13 2009 -0700
@@ -318,6 +318,17 @@
         self.assertEqual(self._evalJsWrappedPyFunc(hai2u, 'hai2u()'),
                          2147483647)
 
+    def testDefinePropertyWorksWithUnicodePropertyNames(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.assertEqual(
+            cx.get_property(obj, u"foo\u2026"),
+            foo
+            )
+
     def testDefinePropertyWorksWithObject(self):
         cx = pymonkey.Runtime().new_context()
         obj = cx.new_object()