Commits

Ronald Oussoren committed 96e066c

Enhance JSObjectRef.h tests, metadata is not yet updated and test isn't complete

  • Participants
  • Parent commits 3ded723

Comments (0)

Files changed (5)

pyobjc-framework-WebKit/Doc/api-notes-JavaScriptCore.txt

 API Notes: JavaScriptCore framework
 ===================================
 
+API Level
+---------
+
+The JavaScriptCore library is very low-level and the Python bindings don't change that. This means interacting with JavaScript
+through these bindings require a fairly large amount of code.
+
+A future version of these bindings might grow a simpler to use convenience API, but that is unlikely to happen soon because
+I have no pressing need for such an API. Contributions that make the JavaScriptCore APIs easier to use would be greatly appreciated.
+
+
 Reference counting
 ------------------
 
 leaking memory and/or crashing.
 
 
+``autoreleasing``
+-----------------
+
+This is a context manager that makes it easier to deal with reference counts::
+
+    with JavaScriptCore.autoreleasing(expression) as value:
+       pass
+
+is more or less equivalant to::
+
+     value = expression
+     try:
+          pass
+
+     finally:
+          JavaScriptCore.JSReleaseContext(value)
+
+The actual release function used depends on the type of *value*.
+
+
+
 ``JSStringGetCharactersPtr``
 ----------------------------
 
 This function is not supported. Convert the string to a Python type using ``JSStringCopyCFString`` or ``JSStringGetUTF8CString``.
+
+``JSObjectMake``, ``JSObjectGetPrivate``, ``JSObjectSetPrivate``
+----------------------------------------------------------------
+
+The private data of an object is a ``void*`` in (Objective-)C, and an integer in Python. Use the ``objc.context`` object to attach
+arbitrary data to an object::
+
+   anObj = ... #
+   JavaScriptCore.JSOObjectSetPrivate(anObject, objc.context.register(aValue))
+   aValue = objc.context.get(JavaScriptCore.JSObjectGetPrivate(anObject))

pyobjc-framework-WebKit/Examples/SimpleJavaScript/simple_javascript.py

+"""
+A basic command-line tool that uses JavaScriptCore
+
+See also <http://parmanoir.com/Taming_JavascriptCore_within_and_without_WebView>
+
+TODO: This needs to be an example that does something useful
+"""
+import JavaScriptCore
+
+with JavaScriptCore.autoreleasing(JavaScriptCore.JSGlobalContextCreate(None)) as ctx:
+
+    script = JavaScriptCore.JSStringCreateWithUTF8CString(b"return new Array")
+    fn = JavaScriptCore.JSObjectMakeFunction(ctx, None, 0, None, script, None, 1, None)
+    result = JavaScriptCore.JSObjectCallAsFunction(ctx, fn, None, 0, None, None)
+    JavaScriptCore.JSStringRelease(script)
+
+    # Result is now a reference to a JavaScript array.

pyobjc-framework-WebKit/Lib/JavaScriptCore/__init__.py

         '__loader__': globals().get('__loader__', None),
     }, (CoreFoundation,))
 del sys.modules['JavaScriptCore._metadata']
+
+import JavaScriptCore._util
+mod.autoreleasing = JavaScriptCore._util.autoreleasing

pyobjc-framework-WebKit/Lib/JavaScriptCore/_util.py

+import JavaScriptCore
+import contextlib
+
+@contextlib.contextmanager
+def autoreleasing(value):
+    try:
+        yield value
+
+    finally:
+        if isinstance(value, JavaScriptCore.JSContextGroupRef):
+            JavaScriptCore.JSContextGroupRelease(value)
+
+        elif isinstance(value, JavaScriptCore.JSGlobalContextRef):
+            JavaScriptCore.JSGlobalContextRelease(value)
+
+        elif isinstance(value, JavaScriptCore.JSClassRef):
+            JavaScriptCore.JSClassRelease(value)
+
+        elif isinstance(value, JavaScriptCore.JSPropertyNameArrayRef):
+            JavaScriptCore.JSPropertyNameArrayRelease(value)
+
+        elif isinstance(value, JavaScriptCore.JSStringRef):
+            JavaScriptCore.JSStringRelease(value)
+
+

pyobjc-framework-WebKit/PyObjCTest/test_jsobjectref.py

         self.assertEqual(JavaScriptCore.kJSClassAttributeNone, 0)
         self.assertEqual(JavaScriptCore.kJSClassAttributeNoAutomaticPrototype, 1<<1)
 
-    def test_functions(self):
-        self.fail("Need to think about these wrappers....")
-
-
-        definition = JavaScriptCore.kJSClassDefinitionEmpty.copy()
-        cls = JavaScriptCore.JSClassCreate(definition)
-        self.assertIsInstance(cls, JavaScriptCore.JSClassRef)
-
+    def test_simple_functions(self):
         self.assertResultHasType(JavaScriptCore.JSClassRetain, JavaScriptCore.JSClassRef)
         self.assertArgHasType(JavaScriptCore.JSClassRetain, 0, JavaScriptCore.JSClassRef)
 
         self.assertArgHasType(JavaScriptCore.JSClassRelease, 0, JavaScriptCore.JSClassRef)
 
-        # JSObjectMake
-        # JSObjectMakeFunctionWithCallback
-        # JSObjectMakeConstructor
-        # JSObjectMakeArray
-        # JSObjectMakeDate
-        # JSObjectMakeError
-        # JSObjectMakeRegExp
-        # JSObjectMakeFunction
-        # JSObjectGetPrototype
-        # JSObjectSetPrototype
-        # JSObjectHasProperty
-        # JSObjectGetProperty
-        # JSObjectSetProperty
-        # JSObjectDeleteProperty
-        # JSObjectGetPropertyAtIndex
-        # JSObjectSetPropertyAtIndex
-        # JSObjectGetPrivate
-        # JSObjectSetPrivate
-        # JSObjectIsFunction
-        # JSObjectCallAsFunction
-        # JSObjectIsConstructor
-        # JSObjectCallAsConstructor
-        # JSObjectCopyPropertyNames
-        # JSPropertyNameArrayRetain
-        # JSPropertyNameArrayRelease
-        # JSPropertyNameArrayGetCount
-        # JSPropertyNameArrayGetNameAtIndex
-        # JSPropertyNameAccumulatorAddName
+        self.assertResultHasType(JavaScriptCore.JSObjectMake, JavaScriptCore.JSObjectRef.__typestr__)
+        self.assertArgHasType(JavaScriptCore.JSObjectMake, 0, JavaScriptCore.JSContextRef.__typestr__)
+        self.assertArgHasType(JavaScriptCore.JSObjectMake, 1, JavaScriptCore.JSClassRef.__typestr__)
+        self.assertArgHasType(JavaScriptCore.JSObjectMake, 2, objc._C_PTR + objc._C_VOID)
+
+        self.assertResultHasType(JavaScriptCore.JSObjectMakeArray, JavaScriptCore.JSObjectRef.__typestr__)
+        self.assertArgHasType(JavaScriptCore.JSObjectMakeArray, 0, JavaScriptCore.JSContextRef.__typestr__)
+        self.assertArgHasType(JavaScriptCore.JSObjectMakeArray, 2, b'n^' + JavaScriptCore.JSValueRef.__typestr__)
+        self.assertArgSizeInArg(JavaScriptCore.JSObjectMakeArray, 2, 1)
+        self.assertArgHasType(JavaScriptCore.JSObjectMakeArray, 3, b'o^' + JavaScriptCore.JSValueRef.__typestr__)
+
+        self.assertResultHasType(JavaScriptCore.JSObjectMakeDate, JavaScriptCore.JSObjectRef.__typestr__)
+        self.assertArgHasType(JavaScriptCore.JSObjectMakeDate, 0, JavaScriptCore.JSContextRef.__typestr__)
+        self.assertArgHasType(JavaScriptCore.JSObjectMakeDate, 2, b'n^' + JavaScriptCore.JSValueRef.__typestr__)
+        self.assertArgSizeInArg(JavaScriptCore.JSObjectMakeDate, 2, 1)
+        self.assertArgHasType(JavaScriptCore.JSObjectMakeDate, 3, b'o^' + JavaScriptCore.JSValueRef.__typestr__)
+
+        self.assertResultHasType(JavaScriptCore.JSObjectMakeError, JavaScriptCore.JSObjectRef.__typestr__)
+        self.assertArgHasType(JavaScriptCore.JSObjectMakeError, 0, JavaScriptCore.JSContextRef.__typestr__)
+        self.assertArgHasType(JavaScriptCore.JSObjectMakeError, 2, b'n^' + JavaScriptCore.JSValueRef.__typestr__)
+        self.assertArgSizeInArg(JavaScriptCore.JSObjectMakeError, 2, 1)
+        self.assertArgHasType(JavaScriptCore.JSObjectMakeError, 3, b'o^' + JavaScriptCore.JSValueRef.__typestr__)
+
+        self.assertResultHasType(JavaScriptCore.JSObjectMakeRegExp, JavaScriptCore.JSObjectRef.__typestr__)
+        self.assertArgHasType(JavaScriptCore.JSObjectMakeRegExp, 0, JavaScriptCore.JSContextRef.__typestr__)
+        self.assertArgHasType(JavaScriptCore.JSObjectMakeRegExp, 2, b'n^' + JavaScriptCore.JSValueRef.__typestr__)
+        self.assertArgSizeInArg(JavaScriptCore.JSObjectMakeRegExp, 2, 1)
+        self.assertArgHasType(JavaScriptCore.JSObjectMakeRegExp, 3, b'o^' + JavaScriptCore.JSValueRef.__typestr__)
+
+        self.assertResultHasType(JavaScriptCore.JSObjectMakeFunction, JavaScriptCore.JSObjectRef.__typestr__)
+        self.assertArgHasType(JavaScriptCore.JSObjectMakeFunction, 0, JavaScriptCore.JSContextRef.__typestr__)
+        self.assertArgHasType(JavaScriptCore.JSObjectMakeFunction, 1, JavaScriptCore.JSStringRef.__typestr__)
+        self.assertArgHasType(JavaScriptCore.JSObjectMakeFunction, 3, b'n^' + JavaScriptCore.JSValueRef.__typestr__)
+        self.assertArgSizeInArg(JavaScriptCore.JSObjectMakeFunction, 3, 2)
+        self.assertArgHasType(JavaScriptCore.JSObjectMakeFunction, 4, JavaScriptCore.JSStringRef.__typestr__)
+        self.assertArgHasType(JavaScriptCore.JSObjectMakeFunction, 5, JavaScriptCore.JSStringRef.__typestr__)
+        self.assertArgHasType(JavaScriptCore.JSObjectMakeFunction, 7, b'o^' + JavaScriptCore.JSValueRef.__typestr__)
+
+        self.assertResultHasType(JavaScriptCore.JSObjectGetPrototype, JavaScriptCore.JSValueRef.__typestr__)
+        self.assertArgHasType(JavaScriptCore.JSObjectGetPrototype, 0, JavaScriptCore.JSContexRef.__typestr__)
+        self.assertArgHasType(JavaScriptCore.JSObjectGetPrototype, 1, JavaScriptCore.JSObjectRef.__typestr__)
+
+        self.assertArgHasType(JavaScriptCore.JSObjectSetPrototype, 0, JavaScriptCore.JSContexRef.__typestr__)
+        self.assertArgHasType(JavaScriptCore.JSObjectSetPrototype, 1, JavaScriptCore.JSObjectRef.__typestr__)
+        self.assertArgHasType(JavaScriptCore.JSObjectSetPrototype, 2, JavaScriptCore.JSValueRef.__typestr__)
+
+        self.assertResultHasType(JavaScriptCore.JSObjectHasProperty, objc._C_BOOL)
+        self.assertArgHasType(JavaScriptCore.JSObjectHasProperty, 0, JavaScriptCore.JSObjectRef.__typestr__)
+        self.assertArgHasType(JavaScriptCore.JSObjectHasProperty, 1, JavaScriptCore.JSStringRef.__typestr__)
+
+        self.assertResultHasType(JavaScriptCore.JSObjectGetProperty, JavaScriptCore.JSValueRef.__typestr__)
+        self.assertArgHasType(JavaScriptCore.JSObjectGetProperty, 0, JavaScriptCore.JSContexRef.__typestr__)
+        self.assertArgHasType(JavaScriptCore.JSObjectGetProperty, 1, JavaScriptCore.JSObjectRef.__typestr__)
+        self.assertArgHasType(JavaScriptCore.JSObjectGetProperty, 2, JavaScriptCore.JSStringRef.__typestr__)
+        self.assertArgHasType(JavaScriptCore.JSObjectGetProperty, 3, b'o^' + JavaScriptCore.JSStringRef.__typestr__)
+
+        self.assertResultHasType(JavaScriptCore.JSObjectSetProperty, JavaScriptCore.JSValueRef.__typestr__)
+        self.assertArgHasType(JavaScriptCore.JSObjectSetProperty, 0, JavaScriptCore.JSContexRef.__typestr__)
+        self.assertArgHasType(JavaScriptCore.JSObjectSetProperty, 1, JavaScriptCore.JSObjectRef.__typestr__)
+        self.assertArgHasType(JavaScriptCore.JSObjectSetProperty, 2, JavaScriptCore.JSStringRef.__typestr__)
+        self.assertArgHasType(JavaScriptCore.JSObjectSetProperty, 3, JavaScriptCore.JSValueRef.__typestr__)
+        self.assertArgHasType(JavaScriptCore.JSObjectSetProperty, 4, objc._C_UINT)
+        self.assertArgHasType(JavaScriptCore.JSObjectSetProperty, 5, b'o^' + JavaScriptCore.JSStringRef.__typestr__)
+
+        self.assertResultHasType(JavaScriptCore.JSObjectDeleteProperty, objc._C_BOOL)
+        self.assertArgHasType(JavaScriptCore.JSObjectDeleteProperty, 0, JavaScriptCore.JSContexRef.__typestr__)
+        self.assertArgHasType(JavaScriptCore.JSObjectDeleteProperty, 1, JavaScriptCore.JSObjectRef.__typestr__)
+        self.assertArgHasType(JavaScriptCore.JSObjectDeleteProperty, 2, JavaScriptCore.JSStringRef.__typestr__)
+        self.assertArgHasType(JavaScriptCore.JSObjectDeleteProperty, 3, b'o^' + JavaScriptCore.JSStringRef.__typestr__)
+
+        self.assertResultHasType(JavaScriptCore.JSObjectGetPropertyAtIndex, objc._C_BOOL)
+        self.assertArgHasType(JavaScriptCore.JSObjectGetPropertyAtIndex, 0, JavaScriptCore.JSContexRef.__typestr__)
+        self.assertArgHasType(JavaScriptCore.JSObjectGetPropertyAtIndex, 1, JavaScriptCore.JSObjectRef.__typestr__)
+        self.assertArgHasType(JavaScriptCore.JSObjectGetPropertyAtIndex, 2, objc._C_UINT)
+        self.assertArgHasType(JavaScriptCore.JSObjectGetPropertyAtIndex, 3, b'o^' + JavaScriptCore.JSStringRef.__typestr__)
+
+        self.assertResultHasType(JavaScriptCore.JSObjectSetPropertyAtIndex, JavaScriptCore.JSValueRef.__typestr__)
+        self.assertArgHasType(JavaScriptCore.JSObjectSetPropertyAtIndex, 0, JavaScriptCore.JSContexRef.__typestr__)
+        self.assertArgHasType(JavaScriptCore.JSObjectSetPropertyAtIndex, 1, JavaScriptCore.JSObjectRef.__typestr__)
+        self.assertArgHasType(JavaScriptCore.JSObjectSetPropertyAtIndex, 2, objc._C_UINT)
+        self.assertArgHasType(JavaScriptCore.JSObjectSetPropertyAtIndex, 3, JavaScriptCore.JSValueRef.__typestr__)
+        self.assertArgHasType(JavaScriptCore.JSObjectSetPropertyAtIndex, 4, objc._C_UINT)
+        self.assertArgHasType(JavaScriptCore.JSObjectSetPropertyAtIndex, 5, b'o^' + JavaScriptCore.JSStringRef.__typestr__)
+
+        self.assertResultHasType(JavaScriptCore.JSObjectGetPrivate, objc._C_PTR + objc._C_VOID)
+        self.assertArgHasType(JavaScriptCore.JSObjectGetPrivate, 0, JavaScriptCore.JSObjectRef.__typestr__)
+
+        self.assertResultHasType(JavaScriptCore.JSObjectSetPrivate, objc._C_BOOL)
+        self.assertArgHasType(JavaScriptCore.JSObjectSetPrivate, 0, JavaScriptCore.JSObjectRef.__typestr__)
+        self.assertArgHasType(JavaScriptCore.JSObjectSetPrivate, 1, objc._C_PTR + objc._C_VOID)
+
+        self.assertResultHasType(JavaScriptCore.JSObjectIsFunction, objc._C_BOOL)
+        self.assertArgHasType(JavaScriptCore.JSObjectIsFunction, 0, JavaScriptCore.JSContextRef.__typestr__)
+        self.assertArgHasType(JavaScriptCore.JSObjectIsFunction, 1, JavaScriptCore.JSObjectRef.__typestr__)
+
+        self.assertResultHasType(JavaScriptCore.JSObjectIsConstructor, objc._C_BOOL)
+        self.assertArgHasType(JavaScriptCore.JSObjectIsConstructor, 0, JavaScriptCore.JSContextRef.__typestr__)
+        self.assertArgHasType(JavaScriptCore.JSObjectIsConstructor, 1, JavaScriptCore.JSObjectRef.__typestr__)
+
+        self.assertResultHasType(JavaScriptCore.JSObjectCopyPropertyNames, JavaScriptCore.JSPropertyNameArrayRef.__typestr__)
+        self.assertArgHasType(JavaScriptCore.JSObjectCopyPropertyNames, 0, JavaScriptCore.JSContexRef.__typestr__)
+        self.assertArgHasType(JavaScriptCore.JSObjectCopyPropertyNames, 1, JavaScriptCore.JSObjectRef.__typestr__)
+
+        self.assertResultHasType(JavaScriptCore.JSPropertyNameArrayRetain, JavaScriptCore.JSPropertyNameArrayRef.__typestr__)
+        self.assertArgHasType(JavaScriptCore.JSPropertyNameArrayRetain, 0, JavaScriptCore.JSPropertyNameArrayRef.__typestr__)
+
+        self.assertArgHasType(JavaScriptCore.JSPropertyNameArrayRelease, 0, JavaScriptCore.JSPropertyNameArrayRef.__typestr__)
+
+        if sys.maxsize > 2 ** 32:
+            SIZE_T = objc._C_ULNG_LNG
+
+        else:
+            SIZE_T = objc._C_ULNG
+
+        self.assertResultHasType(JavaScriptCore.JSPropertyNameArrayGetCount, SIZE_T)
+        self.assertArgHasType(JavaScriptCore.JSPropertyNameArrayGetCount, 0, JavaScriptCore.JSPropertyNameArrayRef.__typestr__)
+
+        self.assertResultHasType(JavaScriptCore.JSPropertyNameArrayGetNameAtIndex, objc.JSStringRef.__typestr__)
+        self.assertArgHasType(JavaScriptCore.JSPropertyNameArrayGetNameAtIndex, 0, objc.JSPropertyNameArrayRef.__typestr__)
+        self.assertArgHasType(JavaScriptCore.JSPropertyNameArrayGetNameAtIndex, 1, SIZE_T)
+
+        self.assertArgHasType(JavaScriptCore.JSPropertyNameAccumulatorAddName, 0, JavaScriptCore.JSPropertyNameAccumulatorRef.__typestr__)
+        self.assertArgHasType(JavaScriptCore.JSPropertyNameAccumulatorAddName, 1, JavaScriptCore.JSStringRef.__typestr__)
+
+        self.assertResultHasType(JavaScriptCore.JSObjectCallAsFunction, JavaScriptCore.JSValueRef.__typestr__)
+        self.assertArgHasType(JavaScriptCore.JSObjectCallAsFunction, 0, JavaScriptCore.JSContexRef.__typestr__)
+        self.assertArgHasType(JavaScriptCore.JSObjectCallAsFunction, 1, JavaScriptCore.JSObjectRef.__typestr__)
+        self.assertArgHasType(JavaScriptCore.JSObjectCallAsFunction, 2, JavaScriptCore.JSObjectRef.__typestr__)
+        self.assertArgHasType(JavaScriptCore.JSObjectCallAsFunction, 3, SIZE_T)
+        self.assertArgHasType(JavaScriptCore.JSObjectCallAsFunction, 4, b'n^' + JavaScriptCore.JSValueRef.__typestr__)
+        self.assertArgSizeInArg(JavaScriptCore.JSObjectCallAsFunction, 4, 3)
+        self.assertArgHasType(JavaScriptCore.JSObjectCallAsFunction, 5, b'o^' + JavaScriptCore.JSStringRef.__typestr__)
+
+        self.assertResultHasType(JavaScriptCore.JSObjectCallAsConstructor, JavaScriptCore.JSValueRef.__typestr__)
+        self.assertArgHasType(JavaScriptCore.JSObjectCallAsConstructor, 0, JavaScriptCore.JSContexRef.__typestr__)
+        self.assertArgHasType(JavaScriptCore.JSObjectCallAsConstructor, 1, JavaScriptCore.JSObjectRef.__typestr__)
+        self.assertArgHasType(JavaScriptCore.JSObjectCallAsConstructor, 2, SIZE_T)
+        self.assertArgHasType(JavaScriptCore.JSObjectCallAsConstructor, 3, b'n^' + JavaScriptCore.JSValueRef.__typestr__)
+        self.assertArgSizeInArg(JavaScriptCore.JSObjectCallAsConstructor, 3, 2)
+        self.assertArgHasType(JavaScriptCore.JSObjectCallAsConstructor, 4, b'o^' + JavaScriptCore.JSStringRef.__typestr__)
+
+    def test_functions(self):
+        self.fail("Need to think about these wrappers....")
+
+        self.assertResultHasType(JavaScriptCore.JSObjectMakeConstructor, JavaScriptCore.JSObjectRef.__typestr__)
+        self.assertArgHasType(JavaScriptCore.JSObjectMakeConstructor, 0, JavaScriptCore.JSContextRef.__typestr__)
+        self.assertArgHasType(JavaScriptCore.JSObjectMakeConstructor, 1, JavaScriptCore.JSClassRef.__typestr__)
+        self.assertArgIsFunction(JavaScriptCore.JSObjectMakeConstructor, 2,
+                b'', # FIXME: requires complicated metadata
+                , True)
+
+        self.assertResultHasType(JavaScriptCore.JSObjectMakeFunctionWithCallback, JavaScriptCore.JSObjectRef.__typestr__)
+        self.assertArgHasType(JavaScriptCore.JSObjectMakeFunctionWithCallback, 0, JavaScriptCore.JSContextRef.__typestr__)
+        self.assertArgHasType(JavaScriptCore.JSObjectMakeFunctionWithCallback, 1, JavaScriptCore.JSStringRef.__typestr__)
+        self.assertArgIsFunction(JavaScriptCore.JSObjectMakeFunctionWithCallback, 2,
+                b'', # FIXME: requires complicated metadata
+                , True)
+
+
+        # XXX: Creating classes is probably easiest using custom code, I don't think the
+        #      bridge metadata can fully describe the C level interface.
+        definition = JavaScriptCore.kJSClassDefinitionEmpty.copy()
+        cls = JavaScriptCore.JSClassCreate(definition)
+        self.assertIsInstance(cls, JavaScriptCore.JSClassRef)
+
+
 
 if __name__ == "__main__":
     main()