]> git.saurik.com Git - apple/javascriptcore.git/blob - API/JSValue.h
JavaScriptCore-7600.1.4.11.8.tar.gz
[apple/javascriptcore.git] / API / JSValue.h
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
26 #ifndef JSValue_h
27 #define JSValue_h
28
29 #if JSC_OBJC_API_ENABLED
30
31 #import <CoreGraphics/CGGeometry.h>
32
33 @class JSContext;
34
35 /*!
36 @interface
37 @discussion A JSValue is a reference to a value within the JavaScript object space of a
38 JSVirtualMachine. All instances of JSValue originate from a JSContext and
39 hold a strong reference to this JSContext. As long as any value associated with
40 a particular JSContext is retained, that JSContext will remain alive.
41 Where an instance method is invoked upon a JSValue, and this returns another
42 JSValue, the returned JSValue will originate from the same JSContext as the
43 JSValue on which the method was invoked.
44
45 All JavaScript values are associated with a particular JSVirtualMachine
46 (the associated JSVirtualMachine is available indirectly via the context
47 property). An instance of JSValue may only be passed as an argument to
48 methods on instances of JSValue and JSContext that belong to the same
49 JSVirtualMachine - passing a JSValue to a method on an object originating
50 from a different JSVirtualMachine will result in an Objective-C exception
51 being raised.
52 */
53 #ifndef JSC_OBJC_API_AVAILABLE_MAC_OS_X_1080
54 NS_CLASS_AVAILABLE(10_9, 7_0)
55 #else
56 OBJC_VISIBLE
57 #endif
58 @interface JSValue : NSObject
59
60 /*!
61 @property
62 @abstract The JSContext that this value originates from.
63 */
64 @property (readonly, strong) JSContext *context;
65
66 /*!
67 @methodgroup Creating JavaScript Values
68 */
69 /*!
70 @method
71 @abstract Create a JSValue by converting an Objective-C object.
72 @discussion The resulting JSValue retains the provided Objective-C object.
73 @param value The Objective-C object to be converted.
74 @result The new JSValue.
75 */
76 + (JSValue *)valueWithObject:(id)value inContext:(JSContext *)context;
77
78 /*!
79 @method
80 @abstract Create a JavaScript value from a BOOL primitive.
81 @param value
82 @param context The JSContext in which the resulting JSValue will be created.
83 @result The new JSValue representing the equivalent boolean value.
84 */
85 + (JSValue *)valueWithBool:(BOOL)value inContext:(JSContext *)context;
86
87 /*!
88 @method
89 @abstract Create a JavaScript value from a double primitive.
90 @param value
91 @param context The JSContext in which the resulting JSValue will be created.
92 @result The new JSValue representing the equivalent boolean value.
93 */
94 + (JSValue *)valueWithDouble:(double)value inContext:(JSContext *)context;
95
96 /*!
97 @method
98 @abstract Create a JavaScript value from an <code>int32_t</code> primitive.
99 @param value
100 @param context The JSContext in which the resulting JSValue will be created.
101 @result The new JSValue representing the equivalent boolean value.
102 */
103 + (JSValue *)valueWithInt32:(int32_t)value inContext:(JSContext *)context;
104
105 /*!
106 @method
107 @abstract Create a JavaScript value from a <code>uint32_t</code> primitive.
108 @param value
109 @param context The JSContext in which the resulting JSValue will be created.
110 @result The new JSValue representing the equivalent boolean value.
111 */
112 + (JSValue *)valueWithUInt32:(uint32_t)value inContext:(JSContext *)context;
113
114 /*!
115 @method
116 @abstract Create a new, empty JavaScript object.
117 @param context The JSContext in which the resulting object will be created.
118 @result The new JavaScript object.
119 */
120 + (JSValue *)valueWithNewObjectInContext:(JSContext *)context;
121
122 /*!
123 @method
124 @abstract Create a new, empty JavaScript array.
125 @param context The JSContext in which the resulting array will be created.
126 @result The new JavaScript array.
127 */
128 + (JSValue *)valueWithNewArrayInContext:(JSContext *)context;
129
130 /*!
131 @method
132 @abstract Create a new JavaScript regular expression object.
133 @param pattern The regular expression pattern.
134 @param flags The regular expression flags.
135 @param context The JSContext in which the resulting regular expression object will be created.
136 @result The new JavaScript regular expression object.
137 */
138 + (JSValue *)valueWithNewRegularExpressionFromPattern:(NSString *)pattern flags:(NSString *)flags inContext:(JSContext *)context;
139
140 /*!
141 @method
142 @abstract Create a new JavaScript error object.
143 @param message The error message.
144 @param context The JSContext in which the resulting error object will be created.
145 @result The new JavaScript error object.
146 */
147 + (JSValue *)valueWithNewErrorFromMessage:(NSString *)message inContext:(JSContext *)context;
148
149 /*!
150 @method
151 @abstract Create the JavaScript value <code>null</code>.
152 @param context The JSContext to which the resulting JSValue belongs.
153 @result The JSValue representing the JavaScript value <code>null</code>.
154 */
155 + (JSValue *)valueWithNullInContext:(JSContext *)context;
156
157 /*!
158 @method
159 @abstract Create the JavaScript value <code>undefined</code>.
160 @param context The JSContext to which the resulting JSValue belongs.
161 @result The JSValue representing the JavaScript value <code>undefined</code>.
162 */
163 + (JSValue *)valueWithUndefinedInContext:(JSContext *)context;
164
165 /*!
166 @methodgroup Converting to Objective-C Types
167 @discussion When converting between JavaScript values and Objective-C objects a copy is
168 performed. Values of types listed below are copied to the corresponding
169 types on conversion in each direction. For NSDictionaries, entries in the
170 dictionary that are keyed by strings are copied onto a JavaScript object.
171 For dictionaries and arrays, conversion is recursive, with the same object
172 conversion being applied to all entries in the collection.
173
174 <pre>
175 @textblock
176 Objective-C type | JavaScript type
177 --------------------+---------------------
178 nil | undefined
179 NSNull | null
180 NSString | string
181 NSNumber | number, boolean
182 NSDictionary | Object object
183 NSArray | Array object
184 NSDate | Date object
185 NSBlock (1) | Function object (1)
186 id (2) | Wrapper object (2)
187 Class (3) | Constructor object (3)
188 @/textblock
189 </pre>
190
191 (1) Instances of NSBlock with supported arguments types will be presented to
192 JavaScript as a callable Function object. For more information on supported
193 argument types see JSExport.h. If a JavaScript Function originating from an
194 Objective-C block is converted back to an Objective-C object the block will
195 be returned. All other JavaScript functions will be converted in the same
196 manner as a JavaScript object of type Object.
197
198 (2) For Objective-C instances that do not derive from the set of types listed
199 above, a wrapper object to provide a retaining handle to the Objective-C
200 instance from JavaScript. For more information on these wrapper objects, see
201 JSExport.h. When a JavaScript wrapper object is converted back to Objective-C
202 the Objective-C instance being retained by the wrapper is returned.
203
204 (3) For Objective-C Class objects a constructor object containing exported
205 class methods will be returned. See JSExport.h for more information on
206 constructor objects.
207
208 For all methods taking arguments of type id, arguments will be converted
209 into a JavaScript value according to the above conversion.
210 */
211 /*!
212 @method
213 @abstract Convert this JSValue to an Objective-C object.
214 @discussion The JSValue is converted to an Objective-C object according
215 to the conversion rules specified above.
216 @result The Objective-C representation of this JSValue.
217 */
218 - (id)toObject;
219
220 /*!
221 @method
222 @abstract Convert a JSValue to an Objective-C object of a specific class.
223 @discussion The JSValue is converted to an Objective-C object of the specified Class.
224 If the result is not of the specified Class then <code>nil</code> will be returned.
225 @result An Objective-C object of the specified Class or <code>nil</code>.
226 */
227 - (id)toObjectOfClass:(Class)expectedClass;
228
229 /*!
230 @method
231 @abstract Convert a JSValue to a boolean.
232 @discussion The JSValue is converted to a boolean according to the rules specified
233 by the JavaScript language.
234 @result The boolean result of the conversion.
235 */
236 - (BOOL)toBool;
237
238 /*!
239 @method
240 @abstract Convert a JSValue to a double.
241 @discussion The JSValue is converted to a number according to the rules specified
242 by the JavaScript language.
243 @result The double result of the conversion.
244 */
245 - (double)toDouble;
246
247 /*!
248 @method
249 @abstract Convert a JSValue to an <code>int32_t</code>.
250 @discussion The JSValue is converted to an integer according to the rules specified
251 by the JavaScript language.
252 @result The <code>int32_t</code> result of the conversion.
253 */
254 - (int32_t)toInt32;
255
256 /*!
257 @method
258 @abstract Convert a JSValue to a <code>uint32_t</code>.
259 @discussion The JSValue is converted to an integer according to the rules specified
260 by the JavaScript language.
261 @result The <code>uint32_t</code> result of the conversion.
262 */
263 - (uint32_t)toUInt32;
264
265 /*!
266 @method
267 @abstract Convert a JSValue to a NSNumber.
268 @discussion If the JSValue represents a boolean, a NSNumber value of YES or NO
269 will be returned. For all other types the value will be converted to a number according
270 to the rules specified by the JavaScript language.
271 @result The NSNumber result of the conversion.
272 */
273 - (NSNumber *)toNumber;
274
275 /*!
276 @method
277 @abstract Convert a JSValue to a NSString.
278 @discussion The JSValue is converted to a string according to the rules specified
279 by the JavaScript language.
280 @result The NSString containing the result of the conversion.
281 */
282 - (NSString *)toString;
283
284 /*!
285 @method
286 @abstract Convert a JSValue to a NSDate.
287 @discussion The value is converted to a number representing a time interval
288 since 1970 which is then used to create a new NSDate instance.
289 @result The NSDate created using the converted time interval.
290 */
291 - (NSDate *)toDate;
292
293 /*!
294 @method
295 @abstract Convert a JSValue to a NSArray.
296 @discussion If the value is <code>null</code> or <code>undefined</code> then <code>nil</code> is returned.
297 If the value is not an object then a JavaScript TypeError will be thrown.
298 The property <code>length</code> is read from the object, converted to an unsigned
299 integer, and an NSArray of this size is allocated. Properties corresponding
300 to indicies within the array bounds will be copied to the array, with
301 JSValues converted to equivalent Objective-C objects as specified.
302 @result The NSArray containing the recursively converted contents of the
303 converted JavaScript array.
304 */
305 - (NSArray *)toArray;
306
307 /*!
308 @method
309 @abstract Convert a JSValue to a NSDictionary.
310 @discussion If the value is <code>null</code> or <code>undefined</code> then <code>nil</code> is returned.
311 If the value is not an object then a JavaScript TypeError will be thrown.
312 All enumerable properties of the object are copied to the dictionary, with
313 JSValues converted to equivalent Objective-C objects as specified.
314 @result The NSDictionary containing the recursively converted contents of
315 the converted JavaScript object.
316 */
317 - (NSDictionary *)toDictionary;
318
319 /*!
320 @methodgroup Accessing Properties
321 */
322 /*!
323 @method
324 @abstract Access a property of a JSValue.
325 @result The JSValue for the requested property or the JSValue <code>undefined</code>
326 if the property does not exist.
327 */
328 - (JSValue *)valueForProperty:(NSString *)property;
329
330 /*!
331 @method
332 @abstract Set a property on a JSValue.
333 */
334 - (void)setValue:(id)value forProperty:(NSString *)property;
335
336 /*!
337 @method
338 @abstract Delete a property from a JSValue.
339 @result YES if deletion is successful, NO otherwise.
340 */
341 - (BOOL)deleteProperty:(NSString *)property;
342
343 /*!
344 @method
345 @abstract Check if a JSValue has a property.
346 @discussion This method has the same function as the JavaScript operator <code>in</code>.
347 @result Returns YES if property is present on the value.
348 */
349 - (BOOL)hasProperty:(NSString *)property;
350
351 /*!
352 @method
353 @abstract Define properties with custom descriptors on JSValues.
354 @discussion This method may be used to create a data or accessor property on an object.
355 This method operates in accordance with the Object.defineProperty method in the
356 JavaScript language.
357 */
358 - (void)defineProperty:(NSString *)property descriptor:(id)descriptor;
359
360 /*!
361 @method
362 @abstract Access an indexed (numerical) property on a JSValue.
363 @result The JSValue for the property at the specified index.
364 Returns the JavaScript value <code>undefined</code> if no property exists at that index.
365 */
366 - (JSValue *)valueAtIndex:(NSUInteger)index;
367
368 /*!
369 @method
370 @abstract Set an indexed (numerical) property on a JSValue.
371 @discussion For JSValues that are JavaScript arrays, indices greater than
372 UINT_MAX - 1 will not affect the length of the array.
373 */
374 - (void)setValue:(id)value atIndex:(NSUInteger)index;
375
376 /*!
377 @methodgroup Checking JavaScript Types
378 */
379 /*!
380 @method
381 @abstract Check if a JSValue corresponds to the JavaScript value <code>undefined</code>.
382 */
383 - (BOOL)isUndefined;
384
385 /*!
386 @method
387 @abstract Check if a JSValue corresponds to the JavaScript value <code>null</code>.
388 */
389 - (BOOL)isNull;
390
391 /*!
392 @method
393 @abstract Check if a JSValue is a boolean.
394 */
395 - (BOOL)isBoolean;
396
397 /*!
398 @method
399 @abstract Check if a JSValue is a number.
400 @discussion In JavaScript, there is no differentiation between types of numbers.
401 Semantically all numbers behave like doubles except in special cases like bit
402 operations.
403 */
404 - (BOOL)isNumber;
405
406 /*!
407 @method
408 @abstract Check if a JSValue is a string.
409 */
410 - (BOOL)isString;
411
412 /*!
413 @method
414 @abstract Check if a JSValue is an object.
415 */
416 - (BOOL)isObject;
417
418 /*!
419 @method
420 @abstract Compare two JSValues using JavaScript's <code>===</code> operator.
421 */
422 - (BOOL)isEqualToObject:(id)value;
423
424 /*!
425 @method
426 @abstract Compare two JSValues using JavaScript's <code>==</code> operator.
427 */
428 - (BOOL)isEqualWithTypeCoercionToObject:(id)value;
429
430 /*!
431 @method
432 @abstract Check if a JSValue is an instance of another object.
433 @discussion This method has the same function as the JavaScript operator <code>instanceof</code>.
434 If an object other than a JSValue is passed, it will first be converted according to
435 the aforementioned rules.
436 */
437 - (BOOL)isInstanceOf:(id)value;
438
439 /*!
440 @methodgroup Calling Functions and Constructors
441 */
442 /*!
443 @method
444 @abstract Invoke a JSValue as a function.
445 @discussion In JavaScript, if a function doesn't explicitly return a value then it
446 implicitly returns the JavaScript value <code>undefined</code>.
447 @param arguments The arguments to pass to the function.
448 @result The return value of the function call.
449 */
450 - (JSValue *)callWithArguments:(NSArray *)arguments;
451
452 /*!
453 @method
454 @abstract Invoke a JSValue as a constructor.
455 @discussion This is equivalent to using the <code>new</code> syntax in JavaScript.
456 @param arguments The arguments to pass to the constructor.
457 @result The return value of the constructor call.
458 */
459 - (JSValue *)constructWithArguments:(NSArray *)arguments;
460
461 /*!
462 @method
463 @abstract Invoke a method on a JSValue.
464 @discussion Accesses the property named <code>method</code> from this value and
465 calls the resulting value as a function, passing this JSValue as the <code>this</code>
466 value along with the specified arguments.
467 @param method The name of the method to be invoked.
468 @param arguments The arguments to pass to the method.
469 @result The return value of the method call.
470 */
471 - (JSValue *)invokeMethod:(NSString *)method withArguments:(NSArray *)arguments;
472
473 @end
474
475 /*!
476 @category
477 @discussion Objective-C methods exported to JavaScript may have argument and/or return
478 values of struct types, provided that conversion to and from the struct is
479 supported by JSValue. Support is provided for any types where JSValue
480 contains both a class method <code>valueWith<Type>:inContext:</code>, and and instance
481 method <code>to<Type></code>- where the string <code><Type></code> in these selector names match,
482 with the first argument to the former being of the same struct type as the
483 return type of the latter.
484 Support is provided for structs of type CGPoint, NSRange, CGRect and CGSize.
485 */
486 @interface JSValue (StructSupport)
487
488 /*!
489 @method
490 @abstract Create a JSValue from a CGPoint.
491 @result A newly allocated JavaScript object containing properties
492 named <code>x</code> and <code>y</code>, with values from the CGPoint.
493 */
494 + (JSValue *)valueWithPoint:(CGPoint)point inContext:(JSContext *)context;
495
496 /*!
497 @method
498 @abstract Create a JSValue from a NSRange.
499 @result A newly allocated JavaScript object containing properties
500 named <code>location</code> and <code>length</code>, with values from the NSRange.
501 */
502 + (JSValue *)valueWithRange:(NSRange)range inContext:(JSContext *)context;
503
504 /*!
505 @method
506 @abstract
507 Create a JSValue from a CGRect.
508 @result A newly allocated JavaScript object containing properties
509 named <code>x</code>, <code>y</code>, <code>width</code>, and <code>height</code>, with values from the CGRect.
510 */
511 + (JSValue *)valueWithRect:(CGRect)rect inContext:(JSContext *)context;
512
513 /*!
514 @method
515 @abstract Create a JSValue from a CGSize.
516 @result A newly allocated JavaScript object containing properties
517 named <code>width</code> and <code>height</code>, with values from the CGSize.
518 */
519 + (JSValue *)valueWithSize:(CGSize)size inContext:(JSContext *)context;
520
521 /*!
522 @method
523 @abstract Convert a JSValue to a CGPoint.
524 @discussion Reads the properties named <code>x</code> and <code>y</code> from
525 this JSValue, and converts the results to double.
526 @result The new CGPoint.
527 */
528 - (CGPoint)toPoint;
529
530 /*!
531 @method
532 @abstract Convert a JSValue to an NSRange.
533 @discussion Reads the properties named <code>location</code> and
534 <code>length</code> from this JSValue and converts the results to double.
535 @result The new NSRange.
536 */
537 - (NSRange)toRange;
538
539 /*!
540 @method
541 @abstract Convert a JSValue to a CGRect.
542 @discussion Reads the properties named <code>x</code>, <code>y</code>,
543 <code>width</code>, and <code>height</code> from this JSValue and converts the results to double.
544 @result The new CGRect.
545 */
546 - (CGRect)toRect;
547
548 /*!
549 @method
550 @abstract Convert a JSValue to a CGSize.
551 @discussion Reads the properties named <code>width</code> and
552 <code>height</code> from this JSValue and converts the results to double.
553 @result The new CGSize.
554 */
555 - (CGSize)toSize;
556
557 @end
558
559 /*!
560 @category
561 @discussion Instances of JSValue implement the following methods in order to enable
562 support for subscript access by key and index, for example:
563
564 @textblock
565 JSValue *objectA, *objectB;
566 JSValue *v1 = object[@"X"]; // Get value for property "X" from 'object'.
567 JSValue *v2 = object[42]; // Get value for index 42 from 'object'.
568 object[@"Y"] = v1; // Assign 'v1' to property "Y" of 'object'.
569 object[101] = v2; // Assign 'v2' to index 101 of 'object'.
570 @/textblock
571
572 An object key passed as a subscript will be converted to a JavaScript value,
573 and then the value converted to a string used as a property name.
574 */
575 @interface JSValue (SubscriptSupport)
576
577 - (JSValue *)objectForKeyedSubscript:(id)key;
578 - (JSValue *)objectAtIndexedSubscript:(NSUInteger)index;
579 - (void)setObject:(id)object forKeyedSubscript:(NSObject <NSCopying> *)key;
580 - (void)setObject:(id)object atIndexedSubscript:(NSUInteger)index;
581
582 @end
583
584 /*!
585 @category
586 @discussion These functions are for bridging between the C API and the Objective-C API.
587 */
588 @interface JSValue (JSValueRefSupport)
589
590 /*!
591 @method
592 @abstract Creates a JSValue, wrapping its C API counterpart.
593 @param value
594 @param context
595 @result The Objective-C API equivalent of the specified JSValueRef.
596 */
597 + (JSValue *)valueWithJSValueRef:(JSValueRef)value inContext:(JSContext *)context;
598
599 /*!
600 @property
601 @abstract Returns the C API counterpart wrapped by a JSContext.
602 @result The C API equivalent of this JSValue.
603 */
604 @property (readonly) JSValueRef JSValueRef;
605 @end
606
607 #ifdef __cplusplus
608 extern "C" {
609 #endif
610
611 /*!
612 @group Property Descriptor Constants
613 @discussion These keys may assist in creating a property descriptor for use with the
614 defineProperty method on JSValue.
615 Property descriptors must fit one of three descriptions:
616
617 Data Descriptor:
618 - A descriptor containing one or both of the keys <code>value</code> and <code>writable</code>,
619 and optionally containing one or both of the keys <code>enumerable</code> and
620 <code>configurable</code>. A data descriptor may not contain either the <code>get</code> or
621 <code>set</code> key.
622 A data descriptor may be used to create or modify the attributes of a
623 data property on an object (replacing any existing accessor property).
624
625 Accessor Descriptor:
626 - A descriptor containing one or both of the keys <code>get</code> and <code>set</code>, and
627 optionally containing one or both of the keys <code>enumerable</code> and
628 <code>configurable</code>. An accessor descriptor may not contain either the <code>value</code>
629 or <code>writable</code> key.
630 An accessor descriptor may be used to create or modify the attributes of
631 an accessor property on an object (replacing any existing data property).
632
633 Generic Descriptor:
634 - A descriptor containing one or both of the keys <code>enumerable</code> and
635 <code>configurable</code>. A generic descriptor may not contain any of the keys
636 <code>value</code>, <code>writable</code>, <code>get</code>, or <code>set</code>.
637 A generic descriptor may be used to modify the attributes of an existing
638 data or accessor property, or to create a new data property.
639 */
640 /*!
641 @const
642 */
643 JS_EXPORT extern NSString * const JSPropertyDescriptorWritableKey;
644 /*!
645 @const
646 */
647 JS_EXPORT extern NSString * const JSPropertyDescriptorEnumerableKey;
648 /*!
649 @const
650 */
651 JS_EXPORT extern NSString * const JSPropertyDescriptorConfigurableKey;
652 /*!
653 @const
654 */
655 JS_EXPORT extern NSString * const JSPropertyDescriptorValueKey;
656 /*!
657 @const
658 */
659 JS_EXPORT extern NSString * const JSPropertyDescriptorGetKey;
660 /*!
661 @const
662 */
663 JS_EXPORT extern NSString * const JSPropertyDescriptorSetKey;
664
665 #ifdef __cplusplus
666 } // extern "C"
667 #endif
668
669 #endif
670
671 #endif // JSValue_h