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