2 * Copyright (C) 2013-2015 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.
27 #import "JavaScriptCore.h"
29 #if JSC_OBJC_API_ENABLED
32 #import "JSAPIWrapperObject.h"
33 #import "JSCInlines.h"
34 #import "JSCallbackObject.h"
35 #import "JSContextInternal.h"
36 #import "JSWrapperMap.h"
37 #import "ObjCCallbackFunction.h"
38 #import "ObjcRuntimeExtras.h"
40 #import "WeakGCMapInlines.h"
41 #import <wtf/HashSet.h>
42 #import <wtf/Vector.h>
43 #import <wtf/spi/cocoa/NSMapTableSPI.h>
45 #include <mach-o/dyld.h>
47 static const int32_t webkitFirstVersionWithInitConstructorSupport = 0x21A0400; // 538.4.0
49 @class JSObjCClassInfo;
51 @interface JSWrapperMap ()
53 - (JSObjCClassInfo*)classInfoForClass:(Class)cls;
57 // Default conversion of selectors to property names.
58 // All semicolons are removed, lowercase letters following a semicolon are capitalized.
59 static NSString *selectorToPropertyName(const char* start)
61 // Use 'index' to check for colons, if there are none, this is easy!
62 const char* firstColon = strchr(start, ':');
64 return [NSString stringWithUTF8String:start];
66 // 'header' is the length of string up to the first colon.
67 size_t header = firstColon - start;
68 // The new string needs to be long enough to hold 'header', plus the remainder of the string, excluding
69 // at least one ':', but including a '\0'. (This is conservative if there are more than one ':').
70 char* buffer = static_cast<char*>(malloc(header + strlen(firstColon + 1) + 1));
71 // Copy 'header' characters, set output to point to the end of this & input to point past the first ':'.
72 memcpy(buffer, start, header);
73 char* output = buffer + header;
74 const char* input = start + header + 1;
76 // On entry to the loop, we have already skipped over a ':' from the input.
79 // Skip over any additional ':'s. We'll leave c holding the next character after the
80 // last ':', and input pointing past c.
81 while ((c = *(input++)) == ':');
82 // Copy the character, converting to upper case if necessary.
83 // If the character we copy is '\0', then we're done!
84 if (!(*(output++) = toupper(c)))
86 // Loop over characters other than ':'.
87 while ((c = *(input++)) != ':') {
88 // Copy the character.
89 // If the character we copy is '\0', then we're done!
90 if (!(*(output++) = c))
93 // If we get here, we've consumed a ':' - wash, rinse, repeat.
96 NSString *result = [NSString stringWithUTF8String:buffer];
101 static bool constructorHasInstance(JSContextRef ctx, JSObjectRef constructorRef, JSValueRef possibleInstance, JSValueRef*)
103 JSC::ExecState* exec = toJS(ctx);
104 JSC::JSLockHolder locker(exec);
106 JSC::JSObject* constructor = toJS(constructorRef);
107 JSC::JSValue instance = toJS(exec, possibleInstance);
108 return JSC::JSObject::defaultHasInstance(exec, instance, constructor->get(exec, exec->propertyNames().prototype));
111 static JSC::JSObject* makeWrapper(JSContextRef ctx, JSClassRef jsClass, id wrappedObject)
113 JSC::ExecState* exec = toJS(ctx);
114 JSC::JSLockHolder locker(exec);
117 JSC::JSCallbackObject<JSC::JSAPIWrapperObject>* object = JSC::JSCallbackObject<JSC::JSAPIWrapperObject>::create(exec, exec->lexicalGlobalObject(), exec->lexicalGlobalObject()->objcWrapperObjectStructure(), jsClass, 0);
118 object->setWrappedObject(wrappedObject);
119 if (JSC::JSObject* prototype = jsClass->prototype(exec))
120 object->setPrototype(exec->vm(), prototype);
125 // Make an object that is in all ways a completely vanilla JavaScript object,
126 // other than that it has a native brand set that will be displayed by the default
127 // Object.prototype.toString conversion.
128 static JSC::JSObject *objectWithCustomBrand(JSContext *context, NSString *brand, Class cls = 0)
130 JSClassDefinition definition;
131 definition = kJSClassDefinitionEmpty;
132 definition.className = [brand UTF8String];
133 JSClassRef classRef = JSClassCreate(&definition);
134 JSC::JSObject* result = makeWrapper([context JSGlobalContextRef], classRef, cls);
135 JSClassRelease(classRef);
139 static JSC::JSObject *constructorWithCustomBrand(JSContext *context, NSString *brand, Class cls)
141 JSClassDefinition definition;
142 definition = kJSClassDefinitionEmpty;
143 definition.className = [brand UTF8String];
144 definition.hasInstance = constructorHasInstance;
145 JSClassRef classRef = JSClassCreate(&definition);
146 JSC::JSObject* result = makeWrapper([context JSGlobalContextRef], classRef, cls);
147 JSClassRelease(classRef);
151 // Look for @optional properties in the prototype containing a selector to property
152 // name mapping, separated by a __JS_EXPORT_AS__ delimiter.
153 static NSMutableDictionary *createRenameMap(Protocol *protocol, BOOL isInstanceMethod)
155 NSMutableDictionary *renameMap = [[NSMutableDictionary alloc] init];
157 forEachMethodInProtocol(protocol, NO, isInstanceMethod, ^(SEL sel, const char*){
158 NSString *rename = @(sel_getName(sel));
159 NSRange range = [rename rangeOfString:@"__JS_EXPORT_AS__"];
160 if (range.location == NSNotFound)
162 NSString *selector = [rename substringToIndex:range.location];
163 NSUInteger begin = range.location + range.length;
164 NSUInteger length = [rename length] - begin - 1;
165 NSString *name = [rename substringWithRange:(NSRange){ begin, length }];
166 renameMap[selector] = name;
172 inline void putNonEnumerable(JSValue *base, NSString *propertyName, JSValue *value)
174 [base defineProperty:propertyName descriptor:@{
175 JSPropertyDescriptorValueKey: value,
176 JSPropertyDescriptorWritableKey: @YES,
177 JSPropertyDescriptorEnumerableKey: @NO,
178 JSPropertyDescriptorConfigurableKey: @YES
182 static bool isInitFamilyMethod(NSString *name)
186 // Skip over initial underscores.
187 for (; i < [name length]; ++i) {
188 if ([name characterAtIndex:i] != '_')
193 NSUInteger initIndex = 0;
194 NSString* init = @"init";
195 for (; i < [name length] && initIndex < [init length]; ++i, ++initIndex) {
196 if ([name characterAtIndex:i] != [init characterAtIndex:initIndex])
200 // We didn't match all of 'init'.
201 if (initIndex < [init length])
204 // If we're at the end or the next character is a capital letter then this is an init-family selector.
205 return i == [name length] || [[NSCharacterSet uppercaseLetterCharacterSet] characterIsMember:[name characterAtIndex:i]];
208 static bool shouldSkipMethodWithName(NSString *name)
210 // For clients that don't support init-based constructors just copy
211 // over the init method as we would have before.
212 if (!supportsInitMethodConstructors())
215 // Skip over init family methods because we handle those specially
216 // for the purposes of hooking up the constructor correctly.
217 return isInitFamilyMethod(name);
220 // This method will iterate over the set of required methods in the protocol, and:
221 // * Determine a property name (either via a renameMap or default conversion).
222 // * If an accessorMap is provided, and contains this name, store the method in the map.
223 // * Otherwise, if the object doesn't already contain a property with name, create it.
224 static void copyMethodsToObject(JSContext *context, Class objcClass, Protocol *protocol, BOOL isInstanceMethod, JSValue *object, NSMutableDictionary *accessorMethods = nil)
226 NSMutableDictionary *renameMap = createRenameMap(protocol, isInstanceMethod);
228 forEachMethodInProtocol(protocol, YES, isInstanceMethod, ^(SEL sel, const char* types){
229 const char* nameCStr = sel_getName(sel);
230 NSString *name = @(nameCStr);
232 if (shouldSkipMethodWithName(name))
235 if (accessorMethods && accessorMethods[name]) {
236 JSObjectRef method = objCCallbackFunctionForMethod(context, objcClass, protocol, isInstanceMethod, sel, types);
239 accessorMethods[name] = [JSValue valueWithJSValueRef:method inContext:context];
241 name = renameMap[name];
243 name = selectorToPropertyName(nameCStr);
244 if ([object hasProperty:name])
246 JSObjectRef method = objCCallbackFunctionForMethod(context, objcClass, protocol, isInstanceMethod, sel, types);
248 putNonEnumerable(object, name, [JSValue valueWithJSValueRef:method inContext:context]);
255 static bool parsePropertyAttributes(objc_property_t property, char*& getterName, char*& setterName)
257 bool readonly = false;
258 unsigned attributeCount;
259 objc_property_attribute_t* attributes = property_copyAttributeList(property, &attributeCount);
260 if (attributeCount) {
261 for (unsigned i = 0; i < attributeCount; ++i) {
262 switch (*(attributes[i].name)) {
264 getterName = strdup(attributes[i].value);
267 setterName = strdup(attributes[i].value);
281 static char* makeSetterName(const char* name)
283 size_t nameLength = strlen(name);
284 char* setterName = (char*)malloc(nameLength + 5); // "set" Name ":\0"
288 setterName[3] = toupper(*name);
289 memcpy(setterName + 4, name + 1, nameLength - 1);
290 setterName[nameLength + 3] = ':';
291 setterName[nameLength + 4] = '\0';
295 static void copyPrototypeProperties(JSContext *context, Class objcClass, Protocol *protocol, JSValue *prototypeValue)
297 // First gather propreties into this list, then handle the methods (capturing the accessor methods).
303 __block Vector<Property> propertyList;
305 // Map recording the methods used as getters/setters.
306 NSMutableDictionary *accessorMethods = [NSMutableDictionary dictionary];
309 JSValue *undefined = [JSValue valueWithUndefinedInContext:context];
311 forEachPropertyInProtocol(protocol, ^(objc_property_t property){
312 char* getterName = 0;
313 char* setterName = 0;
314 bool readonly = parsePropertyAttributes(property, getterName, setterName);
315 const char* name = property_getName(property);
317 // Add the names of the getter & setter methods to
319 getterName = strdup(name);
320 accessorMethods[@(getterName)] = undefined;
323 setterName = makeSetterName(name);
324 accessorMethods[@(setterName)] = undefined;
327 // Add the properties to a list.
328 propertyList.append((Property){ name, getterName, setterName });
331 // Copy methods to the prototype, capturing accessors in the accessorMethods map.
332 copyMethodsToObject(context, objcClass, protocol, YES, prototypeValue, accessorMethods);
334 // Iterate the propertyList & generate accessor properties.
335 for (size_t i = 0; i < propertyList.size(); ++i) {
336 Property& property = propertyList[i];
338 JSValue *getter = accessorMethods[@(property.getterName)];
339 free(property.getterName);
340 ASSERT(![getter isUndefined]);
342 JSValue *setter = undefined;
343 if (property.setterName) {
344 setter = accessorMethods[@(property.setterName)];
345 free(property.setterName);
346 ASSERT(![setter isUndefined]);
349 [prototypeValue defineProperty:@(property.name) descriptor:@{
350 JSPropertyDescriptorGetKey: getter,
351 JSPropertyDescriptorSetKey: setter,
352 JSPropertyDescriptorEnumerableKey: @NO,
353 JSPropertyDescriptorConfigurableKey: @YES
358 @interface JSObjCClassInfo : NSObject {
359 JSContext *m_context;
362 JSClassRef m_classRef;
363 JSC::Weak<JSC::JSObject> m_prototype;
364 JSC::Weak<JSC::JSObject> m_constructor;
367 - (id)initWithContext:(JSContext *)context forClass:(Class)cls;
368 - (JSC::JSObject *)wrapperForObject:(id)object;
369 - (JSC::JSObject *)constructor;
370 - (JSC::JSObject *)prototype;
374 @implementation JSObjCClassInfo
376 - (id)initWithContext:(JSContext *)context forClass:(Class)cls
382 const char* className = class_getName(cls);
385 m_block = [cls isSubclassOfClass:getNSBlockClass()];
386 JSClassDefinition definition;
387 definition = kJSClassDefinitionEmpty;
388 definition.className = className;
389 m_classRef = JSClassCreate(&definition);
396 JSClassRelease(m_classRef);
400 static JSC::JSObject* allocateConstructorForCustomClass(JSContext *context, const char* className, Class cls)
402 if (!supportsInitMethodConstructors())
403 return constructorWithCustomBrand(context, [NSString stringWithFormat:@"%sConstructor", className], cls);
405 // For each protocol that the class implements, gather all of the init family methods into a hash table.
406 __block HashMap<String, Protocol *> initTable;
407 Protocol *exportProtocol = getJSExportProtocol();
408 for (Class currentClass = cls; currentClass; currentClass = class_getSuperclass(currentClass)) {
409 forEachProtocolImplementingProtocol(currentClass, exportProtocol, ^(Protocol *protocol) {
410 forEachMethodInProtocol(protocol, YES, YES, ^(SEL selector, const char*) {
411 const char* name = sel_getName(selector);
412 if (!isInitFamilyMethod(@(name)))
414 initTable.set(name, protocol);
419 for (Class currentClass = cls; currentClass; currentClass = class_getSuperclass(currentClass)) {
420 __block unsigned numberOfInitsFound = 0;
421 __block SEL initMethod = 0;
422 __block Protocol *initProtocol = 0;
423 __block const char* types = 0;
424 forEachMethodInClass(currentClass, ^(Method method) {
425 SEL selector = method_getName(method);
426 const char* name = sel_getName(selector);
427 auto iter = initTable.find(name);
429 if (iter == initTable.end())
432 numberOfInitsFound++;
433 initMethod = selector;
434 initProtocol = iter->value;
435 types = method_getTypeEncoding(method);
438 if (!numberOfInitsFound)
441 if (numberOfInitsFound > 1) {
442 NSLog(@"ERROR: Class %@ exported more than one init family method via JSExport. Class %@ will not have a callable JavaScript constructor function.", cls, cls);
446 JSObjectRef method = objCCallbackFunctionForInit(context, cls, initProtocol, initMethod, types);
449 return constructorWithCustomBrand(context, [NSString stringWithFormat:@"%sConstructor", className], cls);
452 typedef std::pair<JSC::JSObject*, JSC::JSObject*> ConstructorPrototypePair;
454 - (ConstructorPrototypePair)allocateConstructorAndPrototype
456 JSObjCClassInfo* superClassInfo = [m_context.wrapperMap classInfoForClass:class_getSuperclass(m_class)];
458 ASSERT(!m_constructor || !m_prototype);
459 ASSERT((m_class == [NSObject class]) == !superClassInfo);
461 JSC::JSObject* jsPrototype = m_prototype.get();
462 JSC::JSObject* jsConstructor = m_constructor.get();
464 if (!superClassInfo) {
465 JSContextRef cContext = [m_context JSGlobalContextRef];
466 JSValue *constructor = m_context[@"Object"];
468 jsConstructor = toJS(JSValueToObject(cContext, valueInternalValue(constructor), 0));
471 JSValue *prototype = constructor[@"prototype"];
472 jsPrototype = toJS(JSValueToObject(cContext, valueInternalValue(prototype), 0));
475 const char* className = class_getName(m_class);
477 // Create or grab the prototype/constructor pair.
479 jsPrototype = objectWithCustomBrand(m_context, [NSString stringWithFormat:@"%sPrototype", className]);
482 jsConstructor = allocateConstructorForCustomClass(m_context, className, m_class);
484 JSValue* prototype = [JSValue valueWithJSValueRef:toRef(jsPrototype) inContext:m_context];
485 JSValue* constructor = [JSValue valueWithJSValueRef:toRef(jsConstructor) inContext:m_context];
486 putNonEnumerable(prototype, @"constructor", constructor);
487 putNonEnumerable(constructor, @"prototype", prototype);
489 Protocol *exportProtocol = getJSExportProtocol();
490 forEachProtocolImplementingProtocol(m_class, exportProtocol, ^(Protocol *protocol){
491 copyPrototypeProperties(m_context, m_class, protocol, prototype);
492 copyMethodsToObject(m_context, m_class, protocol, NO, constructor);
496 JSC::JSObject* superClassPrototype = [superClassInfo prototype];
497 JSObjectSetPrototype([m_context JSGlobalContextRef], toRef(jsPrototype), toRef(superClassPrototype));
500 m_prototype = jsPrototype;
501 m_constructor = jsConstructor;
502 return ConstructorPrototypePair(jsConstructor, jsPrototype);
505 - (JSC::JSObject*)wrapperForObject:(id)object
507 ASSERT([object isKindOfClass:m_class]);
508 ASSERT(m_block == [object isKindOfClass:getNSBlockClass()]);
510 if (JSObjectRef method = objCCallbackFunctionForBlock(m_context, object)) {
511 JSValue *constructor = [JSValue valueWithJSValueRef:method inContext:m_context];
512 JSValue *prototype = [JSValue valueWithNewObjectInContext:m_context];
513 putNonEnumerable(constructor, @"prototype", prototype);
514 putNonEnumerable(prototype, @"constructor", constructor);
519 JSC::JSObject* prototype = [self prototype];
521 JSC::JSObject* wrapper = makeWrapper([m_context JSGlobalContextRef], m_classRef, object);
522 JSObjectSetPrototype([m_context JSGlobalContextRef], toRef(wrapper), toRef(prototype));
526 - (JSC::JSObject*)constructor
528 JSC::JSObject* constructor = m_constructor.get();
530 constructor = [self allocateConstructorAndPrototype].first;
531 ASSERT(!!constructor);
535 - (JSC::JSObject*)prototype
537 JSC::JSObject* prototype = m_prototype.get();
539 prototype = [self allocateConstructorAndPrototype].second;
546 @implementation JSWrapperMap {
547 JSContext *m_context;
548 NSMutableDictionary *m_classMap;
549 std::unique_ptr<JSC::WeakGCMap<id, JSC::JSObject>> m_cachedJSWrappers;
550 NSMapTable *m_cachedObjCWrappers;
553 - (id)initWithContext:(JSContext *)context
559 NSPointerFunctionsOptions keyOptions = NSPointerFunctionsOpaqueMemory | NSPointerFunctionsOpaquePersonality;
560 NSPointerFunctionsOptions valueOptions = NSPointerFunctionsWeakMemory | NSPointerFunctionsObjectPersonality;
561 m_cachedObjCWrappers = [[NSMapTable alloc] initWithKeyOptions:keyOptions valueOptions:valueOptions capacity:0];
563 m_cachedJSWrappers = std::make_unique<JSC::WeakGCMap<id, JSC::JSObject>>(toJS([context JSGlobalContextRef])->vm());
566 m_classMap = [[NSMutableDictionary alloc] init];
572 [m_cachedObjCWrappers release];
573 [m_classMap release];
577 - (JSObjCClassInfo*)classInfoForClass:(Class)cls
582 // Check if we've already created a JSObjCClassInfo for this Class.
583 if (JSObjCClassInfo* classInfo = (JSObjCClassInfo*)m_classMap[cls])
586 // Skip internal classes beginning with '_' - just copy link to the parent class's info.
587 if ('_' == *class_getName(cls))
588 return m_classMap[cls] = [self classInfoForClass:class_getSuperclass(cls)];
590 return m_classMap[cls] = [[[JSObjCClassInfo alloc] initWithContext:m_context forClass:cls] autorelease];
593 - (JSValue *)jsWrapperForObject:(id)object
595 JSC::JSObject* jsWrapper = m_cachedJSWrappers->get(object);
597 return [JSValue valueWithJSValueRef:toRef(jsWrapper) inContext:m_context];
599 if (class_isMetaClass(object_getClass(object)))
600 jsWrapper = [[self classInfoForClass:(Class)object] constructor];
602 JSObjCClassInfo* classInfo = [self classInfoForClass:[object class]];
603 jsWrapper = [classInfo wrapperForObject:object];
606 // FIXME: https://bugs.webkit.org/show_bug.cgi?id=105891
607 // This general approach to wrapper caching is pretty effective, but there are a couple of problems:
608 // (1) For immortal objects JSValues will effectively leak and this results in error output being logged - we should avoid adding associated objects to immortal objects.
609 // (2) A long lived object may rack up many JSValues. When the contexts are released these will unprotect the associated JavaScript objects,
610 // but still, would probably nicer if we made it so that only one associated object was required, broadcasting object dealloc.
611 m_cachedJSWrappers->set(object, jsWrapper);
612 return [JSValue valueWithJSValueRef:toRef(jsWrapper) inContext:m_context];
615 - (JSValue *)objcWrapperForJSValueRef:(JSValueRef)value
617 JSValue *wrapper = static_cast<JSValue *>(NSMapGet(m_cachedObjCWrappers, value));
619 wrapper = [[[JSValue alloc] initWithValue:value inContext:m_context] autorelease];
620 NSMapInsert(m_cachedObjCWrappers, value, wrapper);
627 id tryUnwrapObjcObject(JSGlobalContextRef context, JSValueRef value)
629 if (!JSValueIsObject(context, value))
631 JSValueRef exception = 0;
632 JSObjectRef object = JSValueToObject(context, value, &exception);
634 JSC::JSLockHolder locker(toJS(context));
635 if (toJS(object)->inherits(JSC::JSCallbackObject<JSC::JSAPIWrapperObject>::info()))
636 return (id)JSC::jsCast<JSC::JSAPIWrapperObject*>(toJS(object))->wrappedObject();
637 if (id target = tryUnwrapConstructor(object))
642 // This class ensures that the JSExport protocol is registered with the runtime.
643 NS_ROOT_CLASS @interface JSExport <JSExport>
645 @implementation JSExport
648 bool supportsInitMethodConstructors()
650 #if PLATFORM(APPLETV)
651 // There are no old clients on Apple TV, so there's no need for backwards compatibility.
655 static int32_t versionOfLinkTimeLibrary = 0;
656 if (!versionOfLinkTimeLibrary)
657 versionOfLinkTimeLibrary = NSVersionOfLinkTimeLibrary("JavaScriptCore");
658 return versionOfLinkTimeLibrary >= webkitFirstVersionWithInitConstructorSupport;
661 Protocol *getJSExportProtocol()
663 static Protocol *protocol = objc_getProtocol("JSExport");
667 Class getNSBlockClass()
669 static Class cls = objc_getClass("NSBlock");