JSExport.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * Copyright (C) 2013 Apple Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
  17. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  18. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  19. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  20. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  21. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  23. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #import <JavaScriptCore/JavaScriptCore.h>
  26. #if JSC_OBJC_API_ENABLED
  27. /*!
  28. @protocol
  29. @abstract JSExport provides a declarative way to export Objective-C objects and
  30. classes -- including properties, instance methods, class methods, and
  31. initializers -- to JavaScript.
  32. @discussion When an Objective-C object is exported to JavaScript, a JavaScript
  33. wrapper object is created.
  34. In JavaScript, inheritance works via a chain of prototype objects.
  35. For each Objective-C class in each JSContext, an object appropriate for use
  36. as a prototype will be provided. For the class NSObject the prototype
  37. will be the Object prototype. For all other Objective-C
  38. classes a prototype will be created. The prototype for a given
  39. Objective-C class will have its internal [Prototype] property set to point to
  40. the prototype created for the Objective-C class's superclass. As such the
  41. prototype chain for a JavaScript wrapper object will reflect the wrapped
  42. Objective-C type's inheritance hierarchy.
  43. JavaScriptCore also produces a constructor for each Objective-C class. The
  44. constructor has a property named 'prototype' that references the prototype,
  45. and the prototype has a property named 'constructor' that references the
  46. constructor.
  47. By default JavaScriptCore does not export any methods or properties from an
  48. Objective-C class to JavaScript; however methods and properties may be exported
  49. explicitly using JSExport. For each protocol that a class conforms to, if the
  50. protocol incorporates the protocol JSExport, JavaScriptCore exports the methods
  51. and properties in that protocol to JavaScript
  52. For each exported instance method JavaScriptCore will assign a corresponding
  53. JavaScript function to the prototype. For each exported Objective-C property
  54. JavaScriptCore will assign a corresponding JavaScript accessor to the prototype.
  55. For each exported class method JavaScriptCore will assign a corresponding
  56. JavaScript function to the constructor. For example:
  57. <pre>
  58. @textblock
  59. @protocol MyClassJavaScriptMethods <JSExport>
  60. - (void)foo;
  61. @end
  62. @interface MyClass : NSObject <MyClassJavaScriptMethods>
  63. - (void)foo;
  64. - (void)bar;
  65. @end
  66. @/textblock
  67. </pre>
  68. Data properties that are created on the prototype or constructor objects have
  69. the attributes: <code>writable:true</code>, <code>enumerable:false</code>, <code>configurable:true</code>.
  70. Accessor properties have the attributes: <code>enumerable:false</code> and <code>configurable:true</code>.
  71. If an instance of <code>MyClass</code> is converted to a JavaScript value, the resulting
  72. wrapper object will (via its prototype) export the method <code>foo</code> to JavaScript,
  73. since the class conforms to the <code>MyClassJavaScriptMethods</code> protocol, and this
  74. protocol incorporates <code>JSExport</code>. <code>bar</code> will not be exported.
  75. JSExport supports properties, arguments, and return values of the following types:
  76. Primitive numbers: signed values up to 32-bits convert using JSValue's
  77. valueWithInt32/toInt32. Unsigned values up to 32-bits convert using JSValue's
  78. valueWithUInt32/toUInt32. All other numeric values convert using JSValue's
  79. valueWithDouble/toDouble.
  80. BOOL: values convert using JSValue's valueWithBool/toBool.
  81. id: values convert using JSValue's valueWithObject/toObject.
  82. Objective-C instance pointers: Pointers convert using JSValue's
  83. valueWithObjectOfClass/toObject.
  84. C structs: C structs for CGPoint, NSRange, CGRect, and CGSize convert using
  85. JSValue's appropriate methods. Other C structs are not supported.
  86. Blocks: Blocks convert using JSValue's valueWithObject/toObject.
  87. All objects that conform to JSExport convert to JavaScript wrapper objects,
  88. even if they subclass classes that would otherwise behave differently. For
  89. example, if a subclass of NSString conforms to JSExport, it converts to
  90. JavaScript as a wrapper object rather than a JavaScript string.
  91. */
  92. @protocol JSExport
  93. @end
  94. /*!
  95. @define
  96. @abstract Rename a selector when it's exported to JavaScript.
  97. @discussion When a selector that takes one or more arguments is converted to a JavaScript
  98. property name, by default a property name will be generated by performing the
  99. following conversion:
  100. - All colons are removed from the selector
  101. - Any lowercase letter that had followed a colon will be capitalized.
  102. Under the default conversion a selector <code>doFoo:withBar:</code> will be exported as
  103. <code>doFooWithBar</code>. The default conversion may be overridden using the JSExportAs
  104. macro, for example to export a method <code>doFoo:withBar:</code> as <code>doFoo</code>:
  105. <pre>
  106. @textblock
  107. @protocol MyClassJavaScriptMethods <JSExport>
  108. JSExportAs(doFoo,
  109. - (void)doFoo:(id)foo withBar:(id)bar
  110. );
  111. @end
  112. @/textblock
  113. </pre>
  114. Note that the JSExport macro may only be applied to a selector that takes one
  115. or more argument.
  116. */
  117. #define JSExportAs(PropertyName, Selector) \
  118. @optional Selector __JS_EXPORT_AS__##PropertyName:(id)argument; @required Selector
  119. #endif