JSContext.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * Copyright (C) 2013-2019 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. #ifndef JSContext_h
  26. #define JSContext_h
  27. #include <JavaScriptCore/JavaScript.h>
  28. #include <JavaScriptCore/WebKitAvailability.h>
  29. #if JSC_OBJC_API_ENABLED
  30. @class JSScript, JSVirtualMachine, JSValue, JSContext;
  31. /*!
  32. @interface
  33. @discussion A JSContext is a JavaScript execution environment. All
  34. JavaScript execution takes place within a context, and all JavaScript values
  35. are tied to a context.
  36. */
  37. JSC_CLASS_AVAILABLE(macosx(10.9), ios(7.0))
  38. @interface JSContext : NSObject
  39. /*!
  40. @methodgroup Creating New JSContexts
  41. */
  42. /*!
  43. @method
  44. @abstract Create a JSContext.
  45. @result The new context.
  46. */
  47. - (instancetype)init;
  48. /*!
  49. @method
  50. @abstract Create a JSContext in the specified virtual machine.
  51. @param virtualMachine The JSVirtualMachine in which the context will be created.
  52. @result The new context.
  53. */
  54. - (instancetype)initWithVirtualMachine:(JSVirtualMachine *)virtualMachine;
  55. /*!
  56. @methodgroup Evaluating Scripts
  57. */
  58. /*!
  59. @method
  60. @abstract Evaluate a string of JavaScript code.
  61. @param script A string containing the JavaScript code to evaluate.
  62. @result The last value generated by the script.
  63. */
  64. - (JSValue *)evaluateScript:(NSString *)script;
  65. /*!
  66. @method
  67. @abstract Evaluate a string of JavaScript code, with a URL for the script's source file.
  68. @param script A string containing the JavaScript code to evaluate.
  69. @param sourceURL A URL for the script's source file. Used by debuggers and when reporting exceptions. This parameter is informative only: it does not change the behavior of the script.
  70. @result The last value generated by the script.
  71. */
  72. - (JSValue *)evaluateScript:(NSString *)script withSourceURL:(NSURL *)sourceURL JSC_API_AVAILABLE(macosx(10.10), ios(8.0));
  73. /*!
  74. @methodgroup Callback Accessors
  75. */
  76. /*!
  77. @method
  78. @abstract Get the JSContext that is currently executing.
  79. @discussion This method may be called from within an Objective-C block or method invoked
  80. as a callback from JavaScript to retrieve the callback's context. Outside of
  81. a callback from JavaScript this method will return nil.
  82. @result The currently executing JSContext or nil if there isn't one.
  83. */
  84. + (JSContext *)currentContext;
  85. /*!
  86. @method
  87. @abstract Get the JavaScript function that is currently executing.
  88. @discussion This method may be called from within an Objective-C block or method invoked
  89. as a callback from JavaScript to retrieve the callback's context. Outside of
  90. a callback from JavaScript this method will return nil.
  91. @result The currently executing JavaScript function or nil if there isn't one.
  92. */
  93. + (JSValue *)currentCallee JSC_API_AVAILABLE(macosx(10.10), ios(8.0));
  94. /*!
  95. @method
  96. @abstract Get the <code>this</code> value of the currently executing method.
  97. @discussion This method may be called from within an Objective-C block or method invoked
  98. as a callback from JavaScript to retrieve the callback's this value. Outside
  99. of a callback from JavaScript this method will return nil.
  100. @result The current <code>this</code> value or nil if there isn't one.
  101. */
  102. + (JSValue *)currentThis;
  103. /*!
  104. @method
  105. @abstract Get the arguments to the current callback.
  106. @discussion This method may be called from within an Objective-C block or method invoked
  107. as a callback from JavaScript to retrieve the callback's arguments, objects
  108. in the returned array are instances of JSValue. Outside of a callback from
  109. JavaScript this method will return nil.
  110. @result An NSArray of the arguments nil if there is no current callback.
  111. */
  112. + (NSArray *)currentArguments;
  113. /*!
  114. @functiongroup Global Properties
  115. */
  116. /*!
  117. @property
  118. @abstract Get the global object of the context.
  119. @discussion This method retrieves the global object of the JavaScript execution context.
  120. Instances of JSContext originating from WebKit will return a reference to the
  121. WindowProxy object.
  122. @result The global object.
  123. */
  124. @property (readonly, strong) JSValue *globalObject;
  125. /*!
  126. @property
  127. @discussion The <code>exception</code> property may be used to throw an exception to JavaScript.
  128. Before a callback is made from JavaScript to an Objective-C block or method,
  129. the prior value of the exception property will be preserved and the property
  130. will be set to nil. After the callback has completed the new value of the
  131. exception property will be read, and prior value restored. If the new value
  132. of exception is not nil, the callback will result in that value being thrown.
  133. This property may also be used to check for uncaught exceptions arising from
  134. API function calls (since the default behaviour of <code>exceptionHandler</code> is to
  135. assign an uncaught exception to this property).
  136. */
  137. @property (strong) JSValue *exception;
  138. /*!
  139. @property
  140. @discussion If a call to an API function results in an uncaught JavaScript exception, the
  141. <code>exceptionHandler</code> block will be invoked. The default implementation for the
  142. exception handler will store the exception to the exception property on
  143. context. As a consequence the default behaviour is for uncaught exceptions
  144. occurring within a callback from JavaScript to be rethrown upon return.
  145. Setting this value to nil will cause all exceptions occurring
  146. within a callback from JavaScript to be silently caught.
  147. */
  148. @property (copy) void(^exceptionHandler)(JSContext *context, JSValue *exception);
  149. /*!
  150. @property
  151. @discussion All instances of JSContext are associated with a JSVirtualMachine.
  152. */
  153. @property (readonly, strong) JSVirtualMachine *virtualMachine;
  154. /*!
  155. @property
  156. @discussion Name of the JSContext. Exposed when remote debugging the context.
  157. */
  158. @property (copy) NSString *name JSC_API_AVAILABLE(macosx(10.10), ios(8.0));
  159. @end
  160. /*!
  161. @category
  162. @discussion Instances of JSContext implement the following methods in order to enable
  163. support for subscript access by key and index, for example:
  164. @textblock
  165. JSContext *context;
  166. JSValue *v = context[@"X"]; // Get value for "X" from the global object.
  167. context[@"Y"] = v; // Assign 'v' to "Y" on the global object.
  168. @/textblock
  169. An object key passed as a subscript will be converted to a JavaScript value,
  170. and then the value converted to a string used to resolve a property of the
  171. global object.
  172. */
  173. @interface JSContext (SubscriptSupport)
  174. /*!
  175. @method
  176. @abstract Get a particular property on the global object.
  177. @result The JSValue for the global object's property.
  178. */
  179. - (JSValue *)objectForKeyedSubscript:(id)key;
  180. /*!
  181. @method
  182. @abstract Set a particular property on the global object.
  183. */
  184. - (void)setObject:(id)object forKeyedSubscript:(NSObject <NSCopying> *)key;
  185. @end
  186. /*!
  187. @category
  188. @discussion These functions are for bridging between the C API and the Objective-C API.
  189. */
  190. @interface JSContext (JSContextRefSupport)
  191. /*!
  192. @method
  193. @abstract Create a JSContext, wrapping its C API counterpart.
  194. @result The JSContext equivalent of the provided JSGlobalContextRef.
  195. */
  196. + (JSContext *)contextWithJSGlobalContextRef:(JSGlobalContextRef)jsGlobalContextRef;
  197. /*!
  198. @property
  199. @abstract Get the C API counterpart wrapped by a JSContext.
  200. @result The C API equivalent of this JSContext.
  201. */
  202. @property (readonly) JSGlobalContextRef JSGlobalContextRef;
  203. @end
  204. #endif
  205. #endif // JSContext_h