2 * Copyright (C) 2013 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
29 #import "DateInstance.h"
32 #import "JavaScriptCore.h"
33 #import "JSContextInternal.h"
34 #import "JSVirtualMachineInternal.h"
35 #import "JSValueInternal.h"
36 #import "JSWrapperMap.h"
37 #import "ObjcRuntimeExtras.h"
38 #import "JSCInlines.h"
39 #import "JSCJSValue.h"
41 #import "StrongInlines.h"
42 #import <wtf/HashMap.h>
43 #import <wtf/HashSet.h>
44 #import <wtf/ObjcRuntimeExtras.h>
45 #import <wtf/SpinLock.h>
46 #import <wtf/Vector.h>
47 #import <wtf/text/WTFString.h>
48 #import <wtf/text/StringHash.h>
50 #if ENABLE(REMOTE_INSPECTOR)
52 #import "JSGlobalObject.h"
53 #import "JSGlobalObjectInspectorController.h"
56 #if JSC_OBJC_API_ENABLED
58 NSString * const JSPropertyDescriptorWritableKey = @"writable";
59 NSString * const JSPropertyDescriptorEnumerableKey = @"enumerable";
60 NSString * const JSPropertyDescriptorConfigurableKey = @"configurable";
61 NSString * const JSPropertyDescriptorValueKey = @"value";
62 NSString * const JSPropertyDescriptorGetKey = @"get";
63 NSString * const JSPropertyDescriptorSetKey = @"set";
65 @implementation JSValue {
69 - (JSValueRef)JSValueRef
74 + (JSValue *)valueWithObject:(id)value inContext:(JSContext *)context
76 return [JSValue valueWithJSValueRef:objectToValue(context, value) inContext:context];
79 + (JSValue *)valueWithBool:(BOOL)value inContext:(JSContext *)context
81 return [JSValue valueWithJSValueRef:JSValueMakeBoolean([context JSGlobalContextRef], value) inContext:context];
84 + (JSValue *)valueWithDouble:(double)value inContext:(JSContext *)context
86 return [JSValue valueWithJSValueRef:JSValueMakeNumber([context JSGlobalContextRef], value) inContext:context];
89 + (JSValue *)valueWithInt32:(int32_t)value inContext:(JSContext *)context
91 return [JSValue valueWithJSValueRef:JSValueMakeNumber([context JSGlobalContextRef], value) inContext:context];
94 + (JSValue *)valueWithUInt32:(uint32_t)value inContext:(JSContext *)context
96 return [JSValue valueWithJSValueRef:JSValueMakeNumber([context JSGlobalContextRef], value) inContext:context];
99 + (JSValue *)valueWithNewObjectInContext:(JSContext *)context
101 return [JSValue valueWithJSValueRef:JSObjectMake([context JSGlobalContextRef], 0, 0) inContext:context];
104 + (JSValue *)valueWithNewArrayInContext:(JSContext *)context
106 return [JSValue valueWithJSValueRef:JSObjectMakeArray([context JSGlobalContextRef], 0, NULL, 0) inContext:context];
109 + (JSValue *)valueWithNewRegularExpressionFromPattern:(NSString *)pattern flags:(NSString *)flags inContext:(JSContext *)context
111 JSStringRef patternString = JSStringCreateWithCFString((CFStringRef)pattern);
112 JSStringRef flagsString = JSStringCreateWithCFString((CFStringRef)flags);
113 JSValueRef arguments[2] = { JSValueMakeString([context JSGlobalContextRef], patternString), JSValueMakeString([context JSGlobalContextRef], flagsString) };
114 JSStringRelease(patternString);
115 JSStringRelease(flagsString);
117 return [JSValue valueWithJSValueRef:JSObjectMakeRegExp([context JSGlobalContextRef], 2, arguments, 0) inContext:context];
120 + (JSValue *)valueWithNewErrorFromMessage:(NSString *)message inContext:(JSContext *)context
122 JSStringRef string = JSStringCreateWithCFString((CFStringRef)message);
123 JSValueRef argument = JSValueMakeString([context JSGlobalContextRef], string);
124 JSStringRelease(string);
126 return [JSValue valueWithJSValueRef:JSObjectMakeError([context JSGlobalContextRef], 1, &argument, 0) inContext:context];
129 + (JSValue *)valueWithNullInContext:(JSContext *)context
131 return [JSValue valueWithJSValueRef:JSValueMakeNull([context JSGlobalContextRef]) inContext:context];
134 + (JSValue *)valueWithUndefinedInContext:(JSContext *)context
136 return [JSValue valueWithJSValueRef:JSValueMakeUndefined([context JSGlobalContextRef]) inContext:context];
141 return valueToObject(_context, m_value);
144 - (id)toObjectOfClass:(Class)expectedClass
146 id result = [self toObject];
147 return [result isKindOfClass:expectedClass] ? result : nil;
152 return JSValueToBoolean([_context JSGlobalContextRef], m_value);
157 JSValueRef exception = 0;
158 double result = JSValueToNumber([_context JSGlobalContextRef], m_value, &exception);
160 [_context notifyException:exception];
161 return std::numeric_limits<double>::quiet_NaN();
169 return JSC::toInt32([self toDouble]);
174 return JSC::toUInt32([self toDouble]);
177 - (NSNumber *)toNumber
179 JSValueRef exception = 0;
180 id result = valueToNumber([_context JSGlobalContextRef], m_value, &exception);
182 [_context notifyException:exception];
186 - (NSString *)toString
188 JSValueRef exception = 0;
189 id result = valueToString([_context JSGlobalContextRef], m_value, &exception);
191 [_context notifyException:exception];
197 JSValueRef exception = 0;
198 id result = valueToDate([_context JSGlobalContextRef], m_value, &exception);
200 [_context notifyException:exception];
206 JSValueRef exception = 0;
207 id result = valueToArray([_context JSGlobalContextRef], m_value, &exception);
209 [_context notifyException:exception];
213 - (NSDictionary *)toDictionary
215 JSValueRef exception = 0;
216 id result = valueToDictionary([_context JSGlobalContextRef], m_value, &exception);
218 [_context notifyException:exception];
222 - (JSValue *)valueForProperty:(NSString *)propertyName
224 JSValueRef exception = 0;
225 JSObjectRef object = JSValueToObject([_context JSGlobalContextRef], m_value, &exception);
227 return [_context valueFromNotifyException:exception];
229 JSStringRef name = JSStringCreateWithCFString((CFStringRef)propertyName);
230 JSValueRef result = JSObjectGetProperty([_context JSGlobalContextRef], object, name, &exception);
231 JSStringRelease(name);
233 return [_context valueFromNotifyException:exception];
235 return [JSValue valueWithJSValueRef:result inContext:_context];
238 - (void)setValue:(id)value forProperty:(NSString *)propertyName
240 JSValueRef exception = 0;
241 JSObjectRef object = JSValueToObject([_context JSGlobalContextRef], m_value, &exception);
243 [_context notifyException:exception];
247 JSStringRef name = JSStringCreateWithCFString((CFStringRef)propertyName);
248 JSObjectSetProperty([_context JSGlobalContextRef], object, name, objectToValue(_context, value), 0, &exception);
249 JSStringRelease(name);
251 [_context notifyException:exception];
256 - (BOOL)deleteProperty:(NSString *)propertyName
258 JSValueRef exception = 0;
259 JSObjectRef object = JSValueToObject([_context JSGlobalContextRef], m_value, &exception);
261 return [_context boolFromNotifyException:exception];
263 JSStringRef name = JSStringCreateWithCFString((CFStringRef)propertyName);
264 BOOL result = JSObjectDeleteProperty([_context JSGlobalContextRef], object, name, &exception);
265 JSStringRelease(name);
267 return [_context boolFromNotifyException:exception];
272 - (BOOL)hasProperty:(NSString *)propertyName
274 JSValueRef exception = 0;
275 JSObjectRef object = JSValueToObject([_context JSGlobalContextRef], m_value, &exception);
277 return [_context boolFromNotifyException:exception];
279 JSStringRef name = JSStringCreateWithCFString((CFStringRef)propertyName);
280 BOOL result = JSObjectHasProperty([_context JSGlobalContextRef], object, name);
281 JSStringRelease(name);
285 - (void)defineProperty:(NSString *)property descriptor:(id)descriptor
287 [[_context globalObject][@"Object"] invokeMethod:@"defineProperty" withArguments:@[ self, property, descriptor ]];
290 - (JSValue *)valueAtIndex:(NSUInteger)index
292 // Properties that are higher than an unsigned value can hold are converted to a double then inserted as a normal property.
293 // Indices that are bigger than the max allowed index size (UINT_MAX - 1) will be handled internally in get().
294 if (index != (unsigned)index)
295 return [self valueForProperty:[[JSValue valueWithDouble:index inContext:_context] toString]];
297 JSValueRef exception = 0;
298 JSObjectRef object = JSValueToObject([_context JSGlobalContextRef], m_value, &exception);
300 return [_context valueFromNotifyException:exception];
302 JSValueRef result = JSObjectGetPropertyAtIndex([_context JSGlobalContextRef], object, (unsigned)index, &exception);
304 return [_context valueFromNotifyException:exception];
306 return [JSValue valueWithJSValueRef:result inContext:_context];
309 - (void)setValue:(id)value atIndex:(NSUInteger)index
311 // Properties that are higher than an unsigned value can hold are converted to a double, then inserted as a normal property.
312 // Indices that are bigger than the max allowed index size (UINT_MAX - 1) will be handled internally in putByIndex().
313 if (index != (unsigned)index)
314 return [self setValue:value forProperty:[[JSValue valueWithDouble:index inContext:_context] toString]];
316 JSValueRef exception = 0;
317 JSObjectRef object = JSValueToObject([_context JSGlobalContextRef], m_value, &exception);
319 [_context notifyException:exception];
323 JSObjectSetPropertyAtIndex([_context JSGlobalContextRef], object, (unsigned)index, objectToValue(_context, value), &exception);
325 [_context notifyException:exception];
332 return JSValueIsUndefined([_context JSGlobalContextRef], m_value);
337 return JSValueIsNull([_context JSGlobalContextRef], m_value);
342 return JSValueIsBoolean([_context JSGlobalContextRef], m_value);
347 return JSValueIsNumber([_context JSGlobalContextRef], m_value);
352 return JSValueIsString([_context JSGlobalContextRef], m_value);
357 return JSValueIsObject([_context JSGlobalContextRef], m_value);
362 return JSValueIsArray([_context JSGlobalContextRef], m_value);
367 return JSValueIsDate([_context JSGlobalContextRef], m_value);
370 - (BOOL)isEqualToObject:(id)value
372 return JSValueIsStrictEqual([_context JSGlobalContextRef], m_value, objectToValue(_context, value));
375 - (BOOL)isEqualWithTypeCoercionToObject:(id)value
377 JSValueRef exception = 0;
378 BOOL result = JSValueIsEqual([_context JSGlobalContextRef], m_value, objectToValue(_context, value), &exception);
380 return [_context boolFromNotifyException:exception];
385 - (BOOL)isInstanceOf:(id)value
387 JSValueRef exception = 0;
388 JSObjectRef constructor = JSValueToObject([_context JSGlobalContextRef], objectToValue(_context, value), &exception);
390 return [_context boolFromNotifyException:exception];
392 BOOL result = JSValueIsInstanceOfConstructor([_context JSGlobalContextRef], m_value, constructor, &exception);
394 return [_context boolFromNotifyException:exception];
399 - (JSValue *)callWithArguments:(NSArray *)argumentArray
401 NSUInteger argumentCount = [argumentArray count];
402 JSValueRef arguments[argumentCount];
403 for (unsigned i = 0; i < argumentCount; ++i)
404 arguments[i] = objectToValue(_context, [argumentArray objectAtIndex:i]);
406 JSValueRef exception = 0;
407 JSObjectRef object = JSValueToObject([_context JSGlobalContextRef], m_value, &exception);
409 return [_context valueFromNotifyException:exception];
411 JSValueRef result = JSObjectCallAsFunction([_context JSGlobalContextRef], object, 0, argumentCount, arguments, &exception);
413 return [_context valueFromNotifyException:exception];
415 return [JSValue valueWithJSValueRef:result inContext:_context];
418 - (JSValue *)constructWithArguments:(NSArray *)argumentArray
420 NSUInteger argumentCount = [argumentArray count];
421 JSValueRef arguments[argumentCount];
422 for (unsigned i = 0; i < argumentCount; ++i)
423 arguments[i] = objectToValue(_context, [argumentArray objectAtIndex:i]);
425 JSValueRef exception = 0;
426 JSObjectRef object = JSValueToObject([_context JSGlobalContextRef], m_value, &exception);
428 return [_context valueFromNotifyException:exception];
430 JSObjectRef result = JSObjectCallAsConstructor([_context JSGlobalContextRef], object, argumentCount, arguments, &exception);
432 return [_context valueFromNotifyException:exception];
434 return [JSValue valueWithJSValueRef:result inContext:_context];
437 - (JSValue *)invokeMethod:(NSString *)method withArguments:(NSArray *)arguments
439 NSUInteger argumentCount = [arguments count];
440 JSValueRef argumentArray[argumentCount];
441 for (unsigned i = 0; i < argumentCount; ++i)
442 argumentArray[i] = objectToValue(_context, [arguments objectAtIndex:i]);
444 JSValueRef exception = 0;
445 JSObjectRef thisObject = JSValueToObject([_context JSGlobalContextRef], m_value, &exception);
447 return [_context valueFromNotifyException:exception];
449 JSStringRef name = JSStringCreateWithCFString((CFStringRef)method);
450 JSValueRef function = JSObjectGetProperty([_context JSGlobalContextRef], thisObject, name, &exception);
451 JSStringRelease(name);
453 return [_context valueFromNotifyException:exception];
455 JSObjectRef object = JSValueToObject([_context JSGlobalContextRef], function, &exception);
457 return [_context valueFromNotifyException:exception];
459 JSValueRef result = JSObjectCallAsFunction([_context JSGlobalContextRef], object, thisObject, argumentCount, argumentArray, &exception);
461 return [_context valueFromNotifyException:exception];
463 return [JSValue valueWithJSValueRef:result inContext:_context];
468 @implementation JSValue(StructSupport)
473 static_cast<CGFloat>([self[@"x"] toDouble]),
474 static_cast<CGFloat>([self[@"y"] toDouble])
481 [[self[@"location"] toNumber] unsignedIntegerValue],
482 [[self[@"length"] toNumber] unsignedIntegerValue]
497 static_cast<CGFloat>([self[@"width"] toDouble]),
498 static_cast<CGFloat>([self[@"height"] toDouble])
502 + (JSValue *)valueWithPoint:(CGPoint)point inContext:(JSContext *)context
504 return [JSValue valueWithObject:@{
507 } inContext:context];
510 + (JSValue *)valueWithRange:(NSRange)range inContext:(JSContext *)context
512 return [JSValue valueWithObject:@{
513 @"location":@(range.location),
514 @"length":@(range.length)
515 } inContext:context];
518 + (JSValue *)valueWithRect:(CGRect)rect inContext:(JSContext *)context
520 return [JSValue valueWithObject:@{
521 @"x":@(rect.origin.x),
522 @"y":@(rect.origin.y),
523 @"width":@(rect.size.width),
524 @"height":@(rect.size.height)
525 } inContext:context];
528 + (JSValue *)valueWithSize:(CGSize)size inContext:(JSContext *)context
530 return [JSValue valueWithObject:@{
531 @"width":@(size.width),
532 @"height":@(size.height)
533 } inContext:context];
538 @implementation JSValue(SubscriptSupport)
540 - (JSValue *)objectForKeyedSubscript:(id)key
542 if (![key isKindOfClass:[NSString class]]) {
543 key = [[JSValue valueWithObject:key inContext:_context] toString];
545 return [JSValue valueWithUndefinedInContext:_context];
548 return [self valueForProperty:(NSString *)key];
551 - (JSValue *)objectAtIndexedSubscript:(NSUInteger)index
553 return [self valueAtIndex:index];
556 - (void)setObject:(id)object forKeyedSubscript:(NSObject <NSCopying> *)key
558 if (![key isKindOfClass:[NSString class]]) {
559 key = [[JSValue valueWithObject:key inContext:_context] toString];
564 [self setValue:object forProperty:(NSString *)key];
567 - (void)setObject:(id)object atIndexedSubscript:(NSUInteger)index
569 [self setValue:object atIndex:index];
574 inline bool isDate(JSObjectRef object, JSGlobalContextRef context)
576 JSC::JSLockHolder locker(toJS(context));
577 return toJS(object)->inherits(JSC::DateInstance::info());
580 inline bool isArray(JSObjectRef object, JSGlobalContextRef context)
582 JSC::JSLockHolder locker(toJS(context));
583 return toJS(object)->inherits(JSC::JSArray::info());
586 @implementation JSValue(Internal)
588 enum ConversionType {
594 class JSContainerConvertor {
602 JSContainerConvertor(JSGlobalContextRef context)
607 id convert(JSValueRef property);
610 bool isWorkListEmpty() const { return !m_worklist.size(); }
613 JSGlobalContextRef m_context;
614 HashMap<JSValueRef, id> m_objectMap;
615 Vector<Task> m_worklist;
616 Vector<JSC::Strong<JSC::Unknown>> m_jsValues;
619 inline id JSContainerConvertor::convert(JSValueRef value)
621 HashMap<JSValueRef, id>::iterator iter = m_objectMap.find(value);
622 if (iter != m_objectMap.end())
625 Task result = valueToObjectWithoutCopy(m_context, value);
631 void JSContainerConvertor::add(Task task)
633 JSC::ExecState* exec = toJS(m_context);
634 m_jsValues.append(JSC::Strong<JSC::Unknown>(exec->vm(), toJSForGC(exec, task.js)));
635 m_objectMap.add(task.js, task.objc);
636 if (task.type != ContainerNone)
637 m_worklist.append(task);
640 JSContainerConvertor::Task JSContainerConvertor::take()
642 ASSERT(!isWorkListEmpty());
643 Task last = m_worklist.last();
644 m_worklist.removeLast();
648 #if ENABLE(REMOTE_INSPECTOR)
649 static void reportExceptionToInspector(JSGlobalContextRef context, JSC::JSValue exceptionValue)
651 JSC::ExecState* exec = toJS(context);
652 JSC::Exception* exception = JSC::Exception::create(exec->vm(), exceptionValue);
653 exec->vmEntryGlobalObject()->inspectorController().reportAPIException(exec, exception);
657 static JSContainerConvertor::Task valueToObjectWithoutCopy(JSGlobalContextRef context, JSValueRef value)
659 if (!JSValueIsObject(context, value)) {
661 if (JSValueIsBoolean(context, value))
662 primitive = JSValueToBoolean(context, value) ? @YES : @NO;
663 else if (JSValueIsNumber(context, value)) {
664 // Normalize the number, so it will unique correctly in the hash map -
665 // it's nicer not to leak this internal implementation detail!
666 value = JSValueMakeNumber(context, JSValueToNumber(context, value, 0));
667 primitive = [NSNumber numberWithDouble:JSValueToNumber(context, value, 0)];
668 } else if (JSValueIsString(context, value)) {
669 // Would be nice to unique strings, too.
670 JSStringRef jsstring = JSValueToStringCopy(context, value, 0);
671 NSString * stringNS = (NSString *)JSStringCopyCFString(kCFAllocatorDefault, jsstring);
672 JSStringRelease(jsstring);
673 primitive = [stringNS autorelease];
674 } else if (JSValueIsNull(context, value))
675 primitive = [NSNull null];
677 ASSERT(JSValueIsUndefined(context, value));
680 return (JSContainerConvertor::Task){ value, primitive, ContainerNone };
683 JSObjectRef object = JSValueToObject(context, value, 0);
685 if (id wrapped = tryUnwrapObjcObject(context, object))
686 return (JSContainerConvertor::Task){ object, wrapped, ContainerNone };
688 if (isDate(object, context))
689 return (JSContainerConvertor::Task){ object, [NSDate dateWithTimeIntervalSince1970:JSValueToNumber(context, object, 0) / 1000.0], ContainerNone };
691 if (isArray(object, context))
692 return (JSContainerConvertor::Task){ object, [NSMutableArray array], ContainerArray };
694 return (JSContainerConvertor::Task){ object, [NSMutableDictionary dictionary], ContainerDictionary };
697 static id containerValueToObject(JSGlobalContextRef context, JSContainerConvertor::Task task)
699 ASSERT(task.type != ContainerNone);
700 JSC::JSLockHolder locker(toJS(context));
701 JSContainerConvertor convertor(context);
703 ASSERT(!convertor.isWorkListEmpty());
706 JSContainerConvertor::Task current = convertor.take();
707 ASSERT(JSValueIsObject(context, current.js));
708 JSObjectRef js = JSValueToObject(context, current.js, 0);
710 if (current.type == ContainerArray) {
711 ASSERT([current.objc isKindOfClass:[NSMutableArray class]]);
712 NSMutableArray *array = (NSMutableArray *)current.objc;
714 JSStringRef lengthString = JSStringCreateWithUTF8CString("length");
715 unsigned length = JSC::toUInt32(JSValueToNumber(context, JSObjectGetProperty(context, js, lengthString, 0), 0));
716 JSStringRelease(lengthString);
718 for (unsigned i = 0; i < length; ++i) {
719 id objc = convertor.convert(JSObjectGetPropertyAtIndex(context, js, i, 0));
720 [array addObject:objc ? objc : [NSNull null]];
723 ASSERT([current.objc isKindOfClass:[NSMutableDictionary class]]);
724 NSMutableDictionary *dictionary = (NSMutableDictionary *)current.objc;
726 JSC::JSLockHolder locker(toJS(context));
728 JSPropertyNameArrayRef propertyNameArray = JSObjectCopyPropertyNames(context, js);
729 size_t length = JSPropertyNameArrayGetCount(propertyNameArray);
731 for (size_t i = 0; i < length; ++i) {
732 JSStringRef propertyName = JSPropertyNameArrayGetNameAtIndex(propertyNameArray, i);
733 if (id objc = convertor.convert(JSObjectGetProperty(context, js, propertyName, 0)))
734 dictionary[[(NSString *)JSStringCopyCFString(kCFAllocatorDefault, propertyName) autorelease]] = objc;
737 JSPropertyNameArrayRelease(propertyNameArray);
740 } while (!convertor.isWorkListEmpty());
745 id valueToObject(JSContext *context, JSValueRef value)
747 JSContainerConvertor::Task result = valueToObjectWithoutCopy([context JSGlobalContextRef], value);
748 if (result.type == ContainerNone)
750 return containerValueToObject([context JSGlobalContextRef], result);
753 id valueToNumber(JSGlobalContextRef context, JSValueRef value, JSValueRef* exception)
756 if (id wrapped = tryUnwrapObjcObject(context, value)) {
757 if ([wrapped isKindOfClass:[NSNumber class]])
761 if (JSValueIsBoolean(context, value))
762 return JSValueToBoolean(context, value) ? @YES : @NO;
764 double result = JSValueToNumber(context, value, exception);
765 return [NSNumber numberWithDouble:*exception ? std::numeric_limits<double>::quiet_NaN() : result];
768 id valueToString(JSGlobalContextRef context, JSValueRef value, JSValueRef* exception)
771 if (id wrapped = tryUnwrapObjcObject(context, value)) {
772 if ([wrapped isKindOfClass:[NSString class]])
776 JSStringRef jsstring = JSValueToStringCopy(context, value, exception);
782 RetainPtr<CFStringRef> stringCF = adoptCF(JSStringCopyCFString(kCFAllocatorDefault, jsstring));
783 JSStringRelease(jsstring);
784 return (NSString *)stringCF.autorelease();
787 id valueToDate(JSGlobalContextRef context, JSValueRef value, JSValueRef* exception)
790 if (id wrapped = tryUnwrapObjcObject(context, value)) {
791 if ([wrapped isKindOfClass:[NSDate class]])
795 double result = JSValueToNumber(context, value, exception) / 1000.0;
796 return *exception ? nil : [NSDate dateWithTimeIntervalSince1970:result];
799 id valueToArray(JSGlobalContextRef context, JSValueRef value, JSValueRef* exception)
802 if (id wrapped = tryUnwrapObjcObject(context, value)) {
803 if ([wrapped isKindOfClass:[NSArray class]])
807 if (JSValueIsObject(context, value))
808 return containerValueToObject(context, (JSContainerConvertor::Task){ value, [NSMutableArray array], ContainerArray});
810 JSC::JSLockHolder locker(toJS(context));
811 if (!(JSValueIsNull(context, value) || JSValueIsUndefined(context, value))) {
812 JSC::JSObject* exceptionObject = JSC::createTypeError(toJS(context), ASCIILiteral("Cannot convert primitive to NSArray"));
813 *exception = toRef(exceptionObject);
814 #if ENABLE(REMOTE_INSPECTOR)
815 reportExceptionToInspector(context, exceptionObject);
821 id valueToDictionary(JSGlobalContextRef context, JSValueRef value, JSValueRef* exception)
824 if (id wrapped = tryUnwrapObjcObject(context, value)) {
825 if ([wrapped isKindOfClass:[NSDictionary class]])
829 if (JSValueIsObject(context, value))
830 return containerValueToObject(context, (JSContainerConvertor::Task){ value, [NSMutableDictionary dictionary], ContainerDictionary});
832 JSC::JSLockHolder locker(toJS(context));
833 if (!(JSValueIsNull(context, value) || JSValueIsUndefined(context, value))) {
834 JSC::JSObject* exceptionObject = JSC::createTypeError(toJS(context), ASCIILiteral("Cannot convert primitive to NSDictionary"));
835 *exception = toRef(exceptionObject);
836 #if ENABLE(REMOTE_INSPECTOR)
837 reportExceptionToInspector(context, exceptionObject);
843 class ObjcContainerConvertor {
851 ObjcContainerConvertor(JSContext *context)
856 JSValueRef convert(id object);
859 bool isWorkListEmpty() const { return !m_worklist.size(); }
862 JSContext *m_context;
863 HashMap<id, JSValueRef> m_objectMap;
864 Vector<Task> m_worklist;
865 Vector<JSC::Strong<JSC::Unknown>> m_jsValues;
868 JSValueRef ObjcContainerConvertor::convert(id object)
872 auto it = m_objectMap.find(object);
873 if (it != m_objectMap.end())
876 ObjcContainerConvertor::Task task = objectToValueWithoutCopy(m_context, object);
881 void ObjcContainerConvertor::add(ObjcContainerConvertor::Task task)
883 JSC::ExecState* exec = toJS(m_context.JSGlobalContextRef);
884 m_jsValues.append(JSC::Strong<JSC::Unknown>(exec->vm(), toJSForGC(exec, task.js)));
885 m_objectMap.add(task.objc, task.js);
886 if (task.type != ContainerNone)
887 m_worklist.append(task);
890 ObjcContainerConvertor::Task ObjcContainerConvertor::take()
892 ASSERT(!isWorkListEmpty());
893 Task last = m_worklist.last();
894 m_worklist.removeLast();
898 inline bool isNSBoolean(id object)
900 ASSERT([@YES class] == [@NO class]);
901 ASSERT([@YES class] != [NSNumber class]);
902 ASSERT([[@YES class] isSubclassOfClass:[NSNumber class]]);
903 return [object isKindOfClass:[@YES class]];
906 static ObjcContainerConvertor::Task objectToValueWithoutCopy(JSContext *context, id object)
908 JSGlobalContextRef contextRef = [context JSGlobalContextRef];
911 return (ObjcContainerConvertor::Task){ object, JSValueMakeUndefined(contextRef), ContainerNone };
913 if (!class_conformsToProtocol(object_getClass(object), getJSExportProtocol())) {
914 if ([object isKindOfClass:[NSArray class]])
915 return (ObjcContainerConvertor::Task){ object, JSObjectMakeArray(contextRef, 0, NULL, 0), ContainerArray };
917 if ([object isKindOfClass:[NSDictionary class]])
918 return (ObjcContainerConvertor::Task){ object, JSObjectMake(contextRef, 0, 0), ContainerDictionary };
920 if ([object isKindOfClass:[NSNull class]])
921 return (ObjcContainerConvertor::Task){ object, JSValueMakeNull(contextRef), ContainerNone };
923 if ([object isKindOfClass:[JSValue class]])
924 return (ObjcContainerConvertor::Task){ object, ((JSValue *)object)->m_value, ContainerNone };
926 if ([object isKindOfClass:[NSString class]]) {
927 JSStringRef string = JSStringCreateWithCFString((CFStringRef)object);
928 JSValueRef js = JSValueMakeString(contextRef, string);
929 JSStringRelease(string);
930 return (ObjcContainerConvertor::Task){ object, js, ContainerNone };
933 if ([object isKindOfClass:[NSNumber class]]) {
934 if (isNSBoolean(object))
935 return (ObjcContainerConvertor::Task){ object, JSValueMakeBoolean(contextRef, [object boolValue]), ContainerNone };
936 return (ObjcContainerConvertor::Task){ object, JSValueMakeNumber(contextRef, [object doubleValue]), ContainerNone };
939 if ([object isKindOfClass:[NSDate class]]) {
940 JSValueRef argument = JSValueMakeNumber(contextRef, [object timeIntervalSince1970] * 1000.0);
941 JSObjectRef result = JSObjectMakeDate(contextRef, 1, &argument, 0);
942 return (ObjcContainerConvertor::Task){ object, result, ContainerNone };
945 if ([object isKindOfClass:[JSManagedValue class]]) {
946 JSValue *value = [static_cast<JSManagedValue *>(object) value];
948 return (ObjcContainerConvertor::Task) { object, JSValueMakeUndefined(contextRef), ContainerNone };
949 return (ObjcContainerConvertor::Task){ object, value->m_value, ContainerNone };
953 return (ObjcContainerConvertor::Task){ object, valueInternalValue([context wrapperForObjCObject:object]), ContainerNone };
956 JSValueRef objectToValue(JSContext *context, id object)
958 JSGlobalContextRef contextRef = [context JSGlobalContextRef];
960 ObjcContainerConvertor::Task task = objectToValueWithoutCopy(context, object);
961 if (task.type == ContainerNone)
964 JSC::JSLockHolder locker(toJS(contextRef));
965 ObjcContainerConvertor convertor(context);
967 ASSERT(!convertor.isWorkListEmpty());
970 ObjcContainerConvertor::Task current = convertor.take();
971 ASSERT(JSValueIsObject(contextRef, current.js));
972 JSObjectRef js = JSValueToObject(contextRef, current.js, 0);
974 if (current.type == ContainerArray) {
975 ASSERT([current.objc isKindOfClass:[NSArray class]]);
976 NSArray *array = (NSArray *)current.objc;
977 NSUInteger count = [array count];
978 for (NSUInteger index = 0; index < count; ++index)
979 JSObjectSetPropertyAtIndex(contextRef, js, index, convertor.convert([array objectAtIndex:index]), 0);
981 ASSERT(current.type == ContainerDictionary);
982 ASSERT([current.objc isKindOfClass:[NSDictionary class]]);
983 NSDictionary *dictionary = (NSDictionary *)current.objc;
984 for (id key in [dictionary keyEnumerator]) {
985 if ([key isKindOfClass:[NSString class]]) {
986 JSStringRef propertyName = JSStringCreateWithCFString((CFStringRef)key);
987 JSObjectSetProperty(contextRef, js, propertyName, convertor.convert([dictionary objectForKey:key]), 0, 0);
988 JSStringRelease(propertyName);
993 } while (!convertor.isWorkListEmpty());
998 JSValueRef valueInternalValue(JSValue * value)
1000 return value->m_value;
1003 + (JSValue *)valueWithJSValueRef:(JSValueRef)value inContext:(JSContext *)context
1005 return [context wrapperForJSObject:value];
1013 - (JSValue *)initWithValue:(JSValueRef)value inContext:(JSContext *)context
1015 if (!value || !context)
1018 self = [super init];
1022 _context = [context retain];
1024 JSValueProtect([_context JSGlobalContextRef], m_value);
1028 struct StructTagHandler {
1032 typedef HashMap<String, StructTagHandler> StructHandlers;
1034 static StructHandlers* createStructHandlerMap()
1036 StructHandlers* structHandlers = new StructHandlers();
1038 size_t valueWithXinContextLength = strlen("valueWithX:inContext:");
1039 size_t toXLength = strlen("toX");
1041 // Step 1: find all valueWith<Foo>:inContext: class methods in JSValue.
1042 forEachMethodInClass(object_getClass([JSValue class]), ^(Method method){
1043 SEL selector = method_getName(method);
1044 const char* name = sel_getName(selector);
1045 size_t nameLength = strlen(name);
1046 // Check for valueWith<Foo>:context:
1047 if (nameLength < valueWithXinContextLength || memcmp(name, "valueWith", 9) || memcmp(name + nameLength - 11, ":inContext:", 11))
1049 // Check for [ id, SEL, <type>, <contextType> ]
1050 if (method_getNumberOfArguments(method) != 4)
1053 // Check 2nd argument type is "@"
1054 char* secondType = method_copyArgumentType(method, 3);
1055 if (strcmp(secondType, "@") != 0) {
1060 // Check result type is also "@"
1061 method_getReturnType(method, idType, 3);
1062 if (strcmp(idType, "@") != 0)
1064 char* type = method_copyArgumentType(method, 2);
1065 structHandlers->add(StringImpl::create(type), (StructTagHandler){ selector, 0 });
1069 // Step 2: find all to<Foo> instance methods in JSValue.
1070 forEachMethodInClass([JSValue class], ^(Method method){
1071 SEL selector = method_getName(method);
1072 const char* name = sel_getName(selector);
1073 size_t nameLength = strlen(name);
1074 // Check for to<Foo>
1075 if (nameLength < toXLength || memcmp(name, "to", 2))
1077 // Check for [ id, SEL ]
1078 if (method_getNumberOfArguments(method) != 2)
1080 // Try to find a matching valueWith<Foo>:context: method.
1081 char* type = method_copyReturnType(method);
1083 StructHandlers::iterator iter = structHandlers->find(type);
1085 if (iter == structHandlers->end())
1087 StructTagHandler& handler = iter->value;
1089 // check that strlen(<foo>) == strlen(<Foo>)
1090 const char* valueWithName = sel_getName(handler.typeToValueSEL);
1091 size_t valueWithLength = strlen(valueWithName);
1092 if (valueWithLength - valueWithXinContextLength != nameLength - toXLength)
1094 // Check that <Foo> == <Foo>
1095 if (memcmp(valueWithName + 9, name + 2, nameLength - toXLength - 1))
1097 handler.valueToTypeSEL = selector;
1100 // Step 3: clean up - remove entries where we found prospective valueWith<Foo>:inContext: conversions, but no matching to<Foo> methods.
1101 typedef HashSet<String> RemoveSet;
1102 RemoveSet removeSet;
1103 for (StructHandlers::iterator iter = structHandlers->begin(); iter != structHandlers->end(); ++iter) {
1104 StructTagHandler& handler = iter->value;
1105 if (!handler.valueToTypeSEL)
1106 removeSet.add(iter->key);
1109 for (RemoveSet::iterator iter = removeSet.begin(); iter != removeSet.end(); ++iter)
1110 structHandlers->remove(*iter);
1112 return structHandlers;
1115 static StructTagHandler* handerForStructTag(const char* encodedType)
1117 static StaticSpinLock handerForStructTagLock;
1118 SpinLockHolder lockHolder(&handerForStructTagLock);
1120 static StructHandlers* structHandlers = createStructHandlerMap();
1122 StructHandlers::iterator iter = structHandlers->find(encodedType);
1123 if (iter == structHandlers->end())
1125 return &iter->value;
1128 + (SEL)selectorForStructToValue:(const char *)structTag
1130 StructTagHandler* handler = handerForStructTag(structTag);
1131 return handler ? handler->typeToValueSEL : nil;
1134 + (SEL)selectorForValueToStruct:(const char *)structTag
1136 StructTagHandler* handler = handerForStructTag(structTag);
1137 return handler ? handler->valueToTypeSEL : nil;
1142 JSValueUnprotect([_context JSGlobalContextRef], m_value);
1148 - (NSString *)description
1150 if (id wrapped = tryUnwrapObjcObject([_context JSGlobalContextRef], m_value))
1151 return [wrapped description];
1152 return [self toString];
1155 NSInvocation *typeToValueInvocationFor(const char* encodedType)
1157 SEL selector = [JSValue selectorForStructToValue:encodedType];
1161 const char* methodTypes = method_getTypeEncoding(class_getClassMethod([JSValue class], selector));
1162 NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[NSMethodSignature signatureWithObjCTypes:methodTypes]];
1163 [invocation setSelector:selector];
1167 NSInvocation *valueToTypeInvocationFor(const char* encodedType)
1169 SEL selector = [JSValue selectorForValueToStruct:encodedType];
1173 const char* methodTypes = method_getTypeEncoding(class_getInstanceMethod([JSValue class], selector));
1174 NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[NSMethodSignature signatureWithObjCTypes:methodTypes]];
1175 [invocation setSelector:selector];