JSValuePrivate.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * Copyright (C) 2018 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. #if JSC_OBJC_API_ENABLED
  26. #import <JavaScriptCore/JavaScriptCore.h>
  27. @interface JSValue(JSPrivate)
  28. #if (defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && __MAC_OS_X_VERSION_MIN_REQUIRED < JSC_MAC_VERSION_TBA) || (defined(__IPHONE_OS_VERSION_MIN_REQUIRED) && __IPHONE_OS_VERSION_MIN_REQUIRED < JSC_IOS_VERSION_TBA)
  29. typedef NSString *JSValueProperty;
  30. #else
  31. typedef id JSValueProperty;
  32. #endif
  33. /*!
  34. @method
  35. @abstract Create a new, unique, symbol object.
  36. @param description The description of the symbol object being created.
  37. @param context The JSContext to which the resulting JSValue belongs.
  38. @result The JSValue representing a unique JavaScript value with type symbol.
  39. */
  40. + (JSValue *)valueWithNewSymbolFromDescription:(NSString *)description inContext:(JSContext *)context JSC_API_AVAILABLE(macosx(JSC_MAC_TBA), ios(JSC_IOS_TBA));
  41. /*!
  42. @method
  43. @abstract Access a property of a JSValue.
  44. @result The JSValue for the requested property or the JSValue <code>undefined</code>
  45. if the property does not exist.
  46. @discussion Corresponds to the JavaScript operation <code>object[property]</code>. After macOS TBA and iOS TBA, 'property' can be any 'id' and will be converted to a JSValue using the conversion rules of <code>valueWithObject:inContext:</code>. Prior to macOS TBA and iOS TBA, 'property' was expected to be an NSString *.
  47. */
  48. - (JSValue *)valueForProperty:(JSValueProperty)property;
  49. /*!
  50. @method
  51. @abstract Set a property on a JSValue.
  52. @discussion Corresponds to the JavaScript operation <code>object[property] = value</code>. After macOS TBA and iOS TBA, 'property' can be any 'id' and will be converted to a JSValue using the conversion rules of <code>valueWithObject:inContext:</code>. Prior to macOS TBA and iOS TBA, 'property' was expected to be an NSString *.
  53. */
  54. - (void)setValue:(id)value forProperty:(JSValueProperty)property;
  55. /*!
  56. @method
  57. @abstract Delete a property from a JSValue.
  58. @result YES if deletion is successful, NO otherwise.
  59. @discussion Corresponds to the JavaScript operation <code>delete object[property]</code>. After macOS TBA and iOS TBA, 'property' can be any 'id' and will be converted to a JSValue using the conversion rules of <code>valueWithObject:inContext:</code>. Prior to macOS TBA and iOS TBA, 'property' was expected to be an NSString *.
  60. */
  61. - (BOOL)deleteProperty:(JSValueProperty)property;
  62. /*!
  63. @method
  64. @abstract Check if a JSValue has a property.
  65. @discussion This method has the same function as the JavaScript operator <code>in</code>.
  66. @result Returns YES if property is present on the value.
  67. @discussion Corresponds to the JavaScript operation <code>property in object</code>. After macOS TBA and iOS TBA, 'property' can be any 'id' and will be converted to a JSValue using the conversion rules of <code>valueWithObject:inContext:</code>. Prior to macOS TBA and iOS TBA, 'property' was expected to be an NSString *.
  68. */
  69. - (BOOL)hasProperty:(JSValueProperty)property;
  70. /*!
  71. @method
  72. @abstract Define properties with custom descriptors on JSValues.
  73. @discussion This method may be used to create a data or accessor property on an object.
  74. This method operates in accordance with the Object.defineProperty method in the JavaScript language. After macOS TBA and iOS TBA, 'property' can be any 'id' and will be converted to a JSValue using the conversion rules of <code>valueWithObject:inContext:</code>. Prior to macOS TBA and iOS TBA, 'property' was expected to be an NSString *.
  75. */
  76. - (void)defineProperty:(JSValueProperty)property descriptor:(id)descriptor;
  77. /*!
  78. @property
  79. @abstract Check if a JSValue is a symbol.
  80. */
  81. @property (readonly) BOOL isSymbol JSC_API_AVAILABLE(macosx(JSC_MAC_TBA), ios(JSC_IOS_TBA));
  82. /*!
  83. @method
  84. @abstract Create a new promise object using the provided executor callback.
  85. @param callback A callback block invoked while the promise object is
  86. being initialized. The resolve and reject parameters are functions that
  87. can be called to notify any pending reactions about the state of the
  88. new promise object.
  89. @param context The JSContext to which the resulting JSValue belongs.
  90. @result The JSValue representing a new promise JavaScript object.
  91. @discussion This method is equivalent to calling the Promise constructor in JavaScript.
  92. the resolve and reject callbacks each normally take a single value, which they
  93. forward to all relevent pending reactions. While inside the executor callback context will act
  94. as if it were in any other callback, except calleeFunction will be <code>nil</code>. This also means
  95. means the new promise object may be accessed via <code>[context thisValue]</code>.
  96. */
  97. + (JSValue *)valueWithNewPromiseInContext:(JSContext *)context fromExecutor:(void (^)(JSValue *resolve, JSValue *reject))callback JSC_API_AVAILABLE(macosx(JSC_MAC_TBA), ios(JSC_IOS_TBA));
  98. /*!
  99. @method
  100. @abstract Create a new resolved promise object with the provided value.
  101. @param result The result value to be passed to any reactions.
  102. @param context The JSContext to which the resulting JSValue belongs.
  103. @result The JSValue representing a new promise JavaScript object.
  104. @discussion This method is equivalent to calling <code>[JSValue valueWithNewPromiseFromExecutor:^(JSValue *resolve, JSValue *reject) { [resolve callWithArguments:@[result]]; } inContext:context]</code>
  105. */
  106. + (JSValue *)valueWithNewPromiseResolvedWithResult:(id)result inContext:(JSContext *)context JSC_API_AVAILABLE(macosx(JSC_MAC_TBA), ios(JSC_IOS_TBA));
  107. /*!
  108. @method
  109. @abstract Create a new rejected promise object with the provided value.
  110. @param reason The result value to be passed to any reactions.
  111. @param context The JSContext to which the resulting JSValue belongs.
  112. @result The JSValue representing a new promise JavaScript object.
  113. @discussion This method is equivalent to calling <code>[JSValue valueWithNewPromiseFromExecutor:^(JSValue *resolve, JSValue *reject) { [reject callWithArguments:@[reason]]; } inContext:context]</code>
  114. */
  115. + (JSValue *)valueWithNewPromiseRejectedWithReason:(id)reason inContext:(JSContext *)context JSC_API_AVAILABLE(macosx(JSC_MAC_TBA), ios(JSC_IOS_TBA));
  116. @end
  117. /*!
  118. @category
  119. @discussion Instances of JSValue implement the following methods in order to enable
  120. support for subscript access by key and index, for example:
  121. @textblock
  122. JSValue *objectA, *objectB;
  123. JSValue *v1 = object[@"X"]; // Get value for property "X" from 'object'.
  124. JSValue *v2 = object[42]; // Get value for index 42 from 'object'.
  125. object[@"Y"] = v1; // Assign 'v1' to property "Y" of 'object'.
  126. object[101] = v2; // Assign 'v2' to index 101 of 'object'.
  127. @/textblock
  128. An object key passed as a subscript will be converted to a JavaScript value,
  129. and then the value using the same rules as <code>valueWithObject:inContext:</code>. In macOS
  130. TBA and iOS TBA and below, the <code>key</code> argument of
  131. <code>setObject:object forKeyedSubscript:key</code> was restricted to an
  132. <code>NSString <NSCopying> *</code> but that restriction was never used.
  133. */
  134. @interface JSValue (SubscriptSupportPrivate)
  135. - (JSValue *)objectForKeyedSubscript:(JSValueProperty)key;
  136. - (JSValue *)objectAtIndexedSubscript:(NSUInteger)index;
  137. - (void)setObject:(id)object forKeyedSubscript:(JSValueProperty)key;
  138. - (void)setObject:(id)object atIndexedSubscript:(NSUInteger)index;
  139. @end
  140. #endif // JSC_OBJC_API_ENABLED