barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 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 | #include "config.h" |
| 27 | #import "JavaScriptCore.h" |
| 28 | |
mhahnenberg@apple.com | f0712f9 | 2013-02-21 20:00:33 +0000 | [diff] [blame] | 29 | #if JSC_OBJC_API_ENABLED |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 30 | |
oliver@apple.com | 5109edb | 2013-07-25 04:03:12 +0000 | [diff] [blame] | 31 | #import "APICallbackFunction.h" |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 32 | #import "APICast.h" |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 33 | #import "Error.h" |
commit-queue@webkit.org | 906a4ec | 2014-09-09 00:21:24 +0000 | [diff] [blame] | 34 | #import "JSCJSValueInlines.h" |
mhahnenberg@apple.com | f0712f9 | 2013-02-21 20:00:33 +0000 | [diff] [blame] | 35 | #import "JSCell.h" |
commit-queue@webkit.org | 906a4ec | 2014-09-09 00:21:24 +0000 | [diff] [blame] | 36 | #import "JSCellInlines.h" |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 37 | #import "JSContextInternal.h" |
| 38 | #import "JSWrapperMap.h" |
| 39 | #import "JSValueInternal.h" |
| 40 | #import "ObjCCallbackFunction.h" |
| 41 | #import "ObjcRuntimeExtras.h" |
barraclough@apple.com | a56aeb2 | 2013-01-02 23:34:48 +0000 | [diff] [blame] | 42 | #import <objc/runtime.h> |
| 43 | #import <wtf/RetainPtr.h> |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 44 | |
| 45 | class CallbackArgument { |
| 46 | public: |
| 47 | virtual ~CallbackArgument(); |
barraclough@apple.com | e2c0742 | 2013-01-03 02:03:12 +0000 | [diff] [blame] | 48 | virtual void set(NSInvocation *, NSInteger, JSContext *, JSValueRef, JSValueRef*) = 0; |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 49 | |
darin@apple.com | d5fb2b4 | 2015-04-20 06:05:26 +0000 | [diff] [blame] | 50 | std::unique_ptr<CallbackArgument> m_next; |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 51 | }; |
| 52 | |
| 53 | CallbackArgument::~CallbackArgument() |
| 54 | { |
| 55 | } |
| 56 | |
| 57 | class CallbackArgumentBoolean : public CallbackArgument { |
barraclough@apple.com | e2c0742 | 2013-01-03 02:03:12 +0000 | [diff] [blame] | 58 | virtual void set(NSInvocation *invocation, NSInteger argumentNumber, JSContext *context, JSValueRef argument, JSValueRef*) override |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 59 | { |
ggaren@apple.com | 58bf4ea | 2013-04-30 21:55:43 +0000 | [diff] [blame] | 60 | bool value = JSValueToBoolean([context JSGlobalContextRef], argument); |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 61 | [invocation setArgument:&value atIndex:argumentNumber]; |
| 62 | } |
| 63 | }; |
| 64 | |
| 65 | template<typename T> |
| 66 | class CallbackArgumentInteger : public CallbackArgument { |
barraclough@apple.com | e2c0742 | 2013-01-03 02:03:12 +0000 | [diff] [blame] | 67 | virtual void set(NSInvocation *invocation, NSInteger argumentNumber, JSContext *context, JSValueRef argument, JSValueRef* exception) override |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 68 | { |
ggaren@apple.com | 58bf4ea | 2013-04-30 21:55:43 +0000 | [diff] [blame] | 69 | T value = (T)JSC::toInt32(JSValueToNumber([context JSGlobalContextRef], argument, exception)); |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 70 | [invocation setArgument:&value atIndex:argumentNumber]; |
| 71 | } |
| 72 | }; |
| 73 | |
| 74 | template<typename T> |
| 75 | class CallbackArgumentDouble : public CallbackArgument { |
barraclough@apple.com | e2c0742 | 2013-01-03 02:03:12 +0000 | [diff] [blame] | 76 | virtual void set(NSInvocation *invocation, NSInteger argumentNumber, JSContext *context, JSValueRef argument, JSValueRef* exception) override |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 77 | { |
ggaren@apple.com | 58bf4ea | 2013-04-30 21:55:43 +0000 | [diff] [blame] | 78 | T value = (T)JSValueToNumber([context JSGlobalContextRef], argument, exception); |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 79 | [invocation setArgument:&value atIndex:argumentNumber]; |
| 80 | } |
| 81 | }; |
| 82 | |
| 83 | class CallbackArgumentJSValue : public CallbackArgument { |
barraclough@apple.com | e2c0742 | 2013-01-03 02:03:12 +0000 | [diff] [blame] | 84 | virtual void set(NSInvocation *invocation, NSInteger argumentNumber, JSContext *context, JSValueRef argument, JSValueRef*) override |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 85 | { |
ggaren@apple.com | 58bf4ea | 2013-04-30 21:55:43 +0000 | [diff] [blame] | 86 | JSValue *value = [JSValue valueWithJSValueRef:argument inContext:context]; |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 87 | [invocation setArgument:&value atIndex:argumentNumber]; |
| 88 | } |
| 89 | }; |
| 90 | |
| 91 | class CallbackArgumentId : public CallbackArgument { |
barraclough@apple.com | e2c0742 | 2013-01-03 02:03:12 +0000 | [diff] [blame] | 92 | virtual void set(NSInvocation *invocation, NSInteger argumentNumber, JSContext *context, JSValueRef argument, JSValueRef*) override |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 93 | { |
| 94 | id value = valueToObject(context, argument); |
| 95 | [invocation setArgument:&value atIndex:argumentNumber]; |
| 96 | } |
| 97 | }; |
| 98 | |
| 99 | class CallbackArgumentOfClass : public CallbackArgument { |
| 100 | public: |
| 101 | CallbackArgumentOfClass(Class cls) |
darin@apple.com | d5fb2b4 | 2015-04-20 06:05:26 +0000 | [diff] [blame] | 102 | : m_class(cls) |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 103 | { |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 104 | } |
| 105 | |
barraclough@apple.com | a56aeb2 | 2013-01-02 23:34:48 +0000 | [diff] [blame] | 106 | private: |
barraclough@apple.com | e2c0742 | 2013-01-03 02:03:12 +0000 | [diff] [blame] | 107 | virtual void set(NSInvocation *invocation, NSInteger argumentNumber, JSContext *context, JSValueRef argument, JSValueRef* exception) override |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 108 | { |
ggaren@apple.com | 58bf4ea | 2013-04-30 21:55:43 +0000 | [diff] [blame] | 109 | JSGlobalContextRef contextRef = [context JSGlobalContextRef]; |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 110 | |
barraclough@apple.com | a56aeb2 | 2013-01-02 23:34:48 +0000 | [diff] [blame] | 111 | id object = tryUnwrapObjcObject(contextRef, argument); |
darin@apple.com | d5fb2b4 | 2015-04-20 06:05:26 +0000 | [diff] [blame] | 112 | if (object && [object isKindOfClass:m_class.get()]) { |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 113 | [invocation setArgument:&object atIndex:argumentNumber]; |
| 114 | return; |
| 115 | } |
| 116 | |
barraclough@apple.com | a56aeb2 | 2013-01-02 23:34:48 +0000 | [diff] [blame] | 117 | if (JSValueIsNull(contextRef, argument) || JSValueIsUndefined(contextRef, argument)) { |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 118 | object = nil; |
| 119 | [invocation setArgument:&object atIndex:argumentNumber]; |
| 120 | return; |
| 121 | } |
| 122 | |
mhahnenberg@apple.com | 635733b | 2014-02-14 22:44:52 +0000 | [diff] [blame] | 123 | *exception = toRef(JSC::createTypeError(toJS(contextRef), ASCIILiteral("Argument does not match Objective-C Class"))); |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 124 | } |
| 125 | |
darin@apple.com | d5fb2b4 | 2015-04-20 06:05:26 +0000 | [diff] [blame] | 126 | RetainPtr<Class> m_class; |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 127 | }; |
| 128 | |
| 129 | class CallbackArgumentNSNumber : public CallbackArgument { |
barraclough@apple.com | e2c0742 | 2013-01-03 02:03:12 +0000 | [diff] [blame] | 130 | virtual void set(NSInvocation *invocation, NSInteger argumentNumber, JSContext *context, JSValueRef argument, JSValueRef* exception) override |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 131 | { |
ggaren@apple.com | 58bf4ea | 2013-04-30 21:55:43 +0000 | [diff] [blame] | 132 | id value = valueToNumber([context JSGlobalContextRef], argument, exception); |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 133 | [invocation setArgument:&value atIndex:argumentNumber]; |
| 134 | } |
| 135 | }; |
| 136 | |
| 137 | class CallbackArgumentNSString : public CallbackArgument { |
barraclough@apple.com | e2c0742 | 2013-01-03 02:03:12 +0000 | [diff] [blame] | 138 | virtual void set(NSInvocation *invocation, NSInteger argumentNumber, JSContext *context, JSValueRef argument, JSValueRef* exception) override |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 139 | { |
ggaren@apple.com | 58bf4ea | 2013-04-30 21:55:43 +0000 | [diff] [blame] | 140 | id value = valueToString([context JSGlobalContextRef], argument, exception); |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 141 | [invocation setArgument:&value atIndex:argumentNumber]; |
| 142 | } |
| 143 | }; |
| 144 | |
| 145 | class CallbackArgumentNSDate : public CallbackArgument { |
barraclough@apple.com | e2c0742 | 2013-01-03 02:03:12 +0000 | [diff] [blame] | 146 | virtual void set(NSInvocation *invocation, NSInteger argumentNumber, JSContext *context, JSValueRef argument, JSValueRef* exception) override |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 147 | { |
ggaren@apple.com | 58bf4ea | 2013-04-30 21:55:43 +0000 | [diff] [blame] | 148 | id value = valueToDate([context JSGlobalContextRef], argument, exception); |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 149 | [invocation setArgument:&value atIndex:argumentNumber]; |
| 150 | } |
| 151 | }; |
| 152 | |
| 153 | class CallbackArgumentNSArray : public CallbackArgument { |
barraclough@apple.com | e2c0742 | 2013-01-03 02:03:12 +0000 | [diff] [blame] | 154 | virtual void set(NSInvocation *invocation, NSInteger argumentNumber, JSContext *context, JSValueRef argument, JSValueRef* exception) override |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 155 | { |
ggaren@apple.com | 58bf4ea | 2013-04-30 21:55:43 +0000 | [diff] [blame] | 156 | id value = valueToArray([context JSGlobalContextRef], argument, exception); |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 157 | [invocation setArgument:&value atIndex:argumentNumber]; |
| 158 | } |
| 159 | }; |
| 160 | |
| 161 | class CallbackArgumentNSDictionary : public CallbackArgument { |
barraclough@apple.com | e2c0742 | 2013-01-03 02:03:12 +0000 | [diff] [blame] | 162 | virtual void set(NSInvocation *invocation, NSInteger argumentNumber, JSContext *context, JSValueRef argument, JSValueRef* exception) override |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 163 | { |
ggaren@apple.com | 58bf4ea | 2013-04-30 21:55:43 +0000 | [diff] [blame] | 164 | id value = valueToDictionary([context JSGlobalContextRef], argument, exception); |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 165 | [invocation setArgument:&value atIndex:argumentNumber]; |
| 166 | } |
| 167 | }; |
| 168 | |
| 169 | class CallbackArgumentStruct : public CallbackArgument { |
| 170 | public: |
barraclough@apple.com | e2c0742 | 2013-01-03 02:03:12 +0000 | [diff] [blame] | 171 | CallbackArgumentStruct(NSInvocation *conversionInvocation, const char* encodedType) |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 172 | : m_conversionInvocation(conversionInvocation) |
barraclough@apple.com | a56aeb2 | 2013-01-02 23:34:48 +0000 | [diff] [blame] | 173 | , m_buffer(encodedType) |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 174 | { |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 175 | } |
| 176 | |
barraclough@apple.com | a56aeb2 | 2013-01-02 23:34:48 +0000 | [diff] [blame] | 177 | private: |
barraclough@apple.com | e2c0742 | 2013-01-03 02:03:12 +0000 | [diff] [blame] | 178 | virtual void set(NSInvocation *invocation, NSInteger argumentNumber, JSContext *context, JSValueRef argument, JSValueRef*) override |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 179 | { |
ggaren@apple.com | 58bf4ea | 2013-04-30 21:55:43 +0000 | [diff] [blame] | 180 | JSValue *value = [JSValue valueWithJSValueRef:argument inContext:context]; |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 181 | [m_conversionInvocation invokeWithTarget:value]; |
| 182 | [m_conversionInvocation getReturnValue:m_buffer]; |
| 183 | [invocation setArgument:m_buffer atIndex:argumentNumber]; |
| 184 | } |
| 185 | |
barraclough@apple.com | a56aeb2 | 2013-01-02 23:34:48 +0000 | [diff] [blame] | 186 | RetainPtr<NSInvocation> m_conversionInvocation; |
| 187 | StructBuffer m_buffer; |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 188 | }; |
| 189 | |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 190 | class ArgumentTypeDelegate { |
| 191 | public: |
darin@apple.com | d5fb2b4 | 2015-04-20 06:05:26 +0000 | [diff] [blame] | 192 | typedef std::unique_ptr<CallbackArgument> ResultType; |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 193 | |
| 194 | template<typename T> |
| 195 | static ResultType typeInteger() |
| 196 | { |
darin@apple.com | d5fb2b4 | 2015-04-20 06:05:26 +0000 | [diff] [blame] | 197 | return std::make_unique<CallbackArgumentInteger<T>>(); |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | template<typename T> |
| 201 | static ResultType typeDouble() |
| 202 | { |
darin@apple.com | d5fb2b4 | 2015-04-20 06:05:26 +0000 | [diff] [blame] | 203 | return std::make_unique<CallbackArgumentDouble<T>>(); |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | static ResultType typeBool() |
| 207 | { |
darin@apple.com | d5fb2b4 | 2015-04-20 06:05:26 +0000 | [diff] [blame] | 208 | return std::make_unique<CallbackArgumentBoolean>(); |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | static ResultType typeVoid() |
| 212 | { |
oliver@apple.com | 5598c18 | 2013-01-23 22:25:07 +0000 | [diff] [blame] | 213 | RELEASE_ASSERT_NOT_REACHED(); |
darin@apple.com | d5fb2b4 | 2015-04-20 06:05:26 +0000 | [diff] [blame] | 214 | return nullptr; |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | static ResultType typeId() |
| 218 | { |
darin@apple.com | d5fb2b4 | 2015-04-20 06:05:26 +0000 | [diff] [blame] | 219 | return std::make_unique<CallbackArgumentId>(); |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | static ResultType typeOfClass(const char* begin, const char* end) |
| 223 | { |
| 224 | StringRange copy(begin, end); |
| 225 | Class cls = objc_getClass(copy); |
| 226 | if (!cls) |
darin@apple.com | d5fb2b4 | 2015-04-20 06:05:26 +0000 | [diff] [blame] | 227 | return nullptr; |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 228 | |
| 229 | if (cls == [JSValue class]) |
darin@apple.com | d5fb2b4 | 2015-04-20 06:05:26 +0000 | [diff] [blame] | 230 | return std::make_unique<CallbackArgumentJSValue>(); |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 231 | if (cls == [NSString class]) |
darin@apple.com | d5fb2b4 | 2015-04-20 06:05:26 +0000 | [diff] [blame] | 232 | return std::make_unique<CallbackArgumentNSString>(); |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 233 | if (cls == [NSNumber class]) |
darin@apple.com | d5fb2b4 | 2015-04-20 06:05:26 +0000 | [diff] [blame] | 234 | return std::make_unique<CallbackArgumentNSNumber>(); |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 235 | if (cls == [NSDate class]) |
darin@apple.com | d5fb2b4 | 2015-04-20 06:05:26 +0000 | [diff] [blame] | 236 | return std::make_unique<CallbackArgumentNSDate>(); |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 237 | if (cls == [NSArray class]) |
darin@apple.com | d5fb2b4 | 2015-04-20 06:05:26 +0000 | [diff] [blame] | 238 | return std::make_unique<CallbackArgumentNSArray>(); |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 239 | if (cls == [NSDictionary class]) |
darin@apple.com | d5fb2b4 | 2015-04-20 06:05:26 +0000 | [diff] [blame] | 240 | return std::make_unique<CallbackArgumentNSDictionary>(); |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 241 | |
darin@apple.com | d5fb2b4 | 2015-04-20 06:05:26 +0000 | [diff] [blame] | 242 | return std::make_unique<CallbackArgumentOfClass>(cls); |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 243 | } |
| 244 | |
mhahnenberg@apple.com | 789bdff | 2013-03-01 21:14:24 +0000 | [diff] [blame] | 245 | static ResultType typeBlock(const char*, const char*) |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 246 | { |
darin@apple.com | d5fb2b4 | 2015-04-20 06:05:26 +0000 | [diff] [blame] | 247 | return nullptr; |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | static ResultType typeStruct(const char* begin, const char* end) |
| 251 | { |
| 252 | StringRange copy(begin, end); |
barraclough@apple.com | e2c0742 | 2013-01-03 02:03:12 +0000 | [diff] [blame] | 253 | if (NSInvocation *invocation = valueToTypeInvocationFor(copy)) |
darin@apple.com | d5fb2b4 | 2015-04-20 06:05:26 +0000 | [diff] [blame] | 254 | return std::make_unique<CallbackArgumentStruct>(invocation, copy); |
| 255 | return nullptr; |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 256 | } |
| 257 | }; |
| 258 | |
| 259 | class CallbackResult { |
| 260 | public: |
| 261 | virtual ~CallbackResult() |
| 262 | { |
| 263 | } |
| 264 | |
barraclough@apple.com | e2c0742 | 2013-01-03 02:03:12 +0000 | [diff] [blame] | 265 | virtual JSValueRef get(NSInvocation *, JSContext *, JSValueRef*) = 0; |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 266 | }; |
| 267 | |
| 268 | class CallbackResultVoid : public CallbackResult { |
barraclough@apple.com | e2c0742 | 2013-01-03 02:03:12 +0000 | [diff] [blame] | 269 | virtual JSValueRef get(NSInvocation *, JSContext *context, JSValueRef*) override |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 270 | { |
ggaren@apple.com | 58bf4ea | 2013-04-30 21:55:43 +0000 | [diff] [blame] | 271 | return JSValueMakeUndefined([context JSGlobalContextRef]); |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 272 | } |
| 273 | }; |
| 274 | |
| 275 | class CallbackResultId : public CallbackResult { |
barraclough@apple.com | e2c0742 | 2013-01-03 02:03:12 +0000 | [diff] [blame] | 276 | virtual JSValueRef get(NSInvocation *invocation, JSContext *context, JSValueRef*) override |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 277 | { |
| 278 | id value; |
| 279 | [invocation getReturnValue:&value]; |
| 280 | return objectToValue(context, value); |
| 281 | } |
| 282 | }; |
| 283 | |
| 284 | template<typename T> |
| 285 | class CallbackResultNumeric : public CallbackResult { |
barraclough@apple.com | e2c0742 | 2013-01-03 02:03:12 +0000 | [diff] [blame] | 286 | virtual JSValueRef get(NSInvocation *invocation, JSContext *context, JSValueRef*) override |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 287 | { |
| 288 | T value; |
| 289 | [invocation getReturnValue:&value]; |
ggaren@apple.com | 58bf4ea | 2013-04-30 21:55:43 +0000 | [diff] [blame] | 290 | return JSValueMakeNumber([context JSGlobalContextRef], value); |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 291 | } |
| 292 | }; |
| 293 | |
| 294 | class CallbackResultBoolean : public CallbackResult { |
barraclough@apple.com | e2c0742 | 2013-01-03 02:03:12 +0000 | [diff] [blame] | 295 | virtual JSValueRef get(NSInvocation *invocation, JSContext *context, JSValueRef*) override |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 296 | { |
| 297 | bool value; |
| 298 | [invocation getReturnValue:&value]; |
ggaren@apple.com | 58bf4ea | 2013-04-30 21:55:43 +0000 | [diff] [blame] | 299 | return JSValueMakeBoolean([context JSGlobalContextRef], value); |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 300 | } |
| 301 | }; |
| 302 | |
| 303 | class CallbackResultStruct : public CallbackResult { |
| 304 | public: |
barraclough@apple.com | e2c0742 | 2013-01-03 02:03:12 +0000 | [diff] [blame] | 305 | CallbackResultStruct(NSInvocation *conversionInvocation, const char* encodedType) |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 306 | : m_conversionInvocation(conversionInvocation) |
barraclough@apple.com | a56aeb2 | 2013-01-02 23:34:48 +0000 | [diff] [blame] | 307 | , m_buffer(encodedType) |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 308 | { |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 309 | } |
| 310 | |
barraclough@apple.com | a56aeb2 | 2013-01-02 23:34:48 +0000 | [diff] [blame] | 311 | private: |
barraclough@apple.com | e2c0742 | 2013-01-03 02:03:12 +0000 | [diff] [blame] | 312 | virtual JSValueRef get(NSInvocation *invocation, JSContext *context, JSValueRef*) override |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 313 | { |
barraclough@apple.com | a56aeb2 | 2013-01-02 23:34:48 +0000 | [diff] [blame] | 314 | [invocation getReturnValue:m_buffer]; |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 315 | |
barraclough@apple.com | a56aeb2 | 2013-01-02 23:34:48 +0000 | [diff] [blame] | 316 | [m_conversionInvocation setArgument:m_buffer atIndex:2]; |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 317 | [m_conversionInvocation setArgument:&context atIndex:3]; |
| 318 | [m_conversionInvocation invokeWithTarget:[JSValue class]]; |
| 319 | |
barraclough@apple.com | e2c0742 | 2013-01-03 02:03:12 +0000 | [diff] [blame] | 320 | JSValue *value; |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 321 | [m_conversionInvocation getReturnValue:&value]; |
| 322 | return valueInternalValue(value); |
| 323 | } |
| 324 | |
barraclough@apple.com | a56aeb2 | 2013-01-02 23:34:48 +0000 | [diff] [blame] | 325 | RetainPtr<NSInvocation> m_conversionInvocation; |
| 326 | StructBuffer m_buffer; |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 327 | }; |
| 328 | |
| 329 | class ResultTypeDelegate { |
| 330 | public: |
darin@apple.com | d5fb2b4 | 2015-04-20 06:05:26 +0000 | [diff] [blame] | 331 | typedef std::unique_ptr<CallbackResult> ResultType; |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 332 | |
| 333 | template<typename T> |
| 334 | static ResultType typeInteger() |
| 335 | { |
darin@apple.com | d5fb2b4 | 2015-04-20 06:05:26 +0000 | [diff] [blame] | 336 | return std::make_unique<CallbackResultNumeric<T>>(); |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | template<typename T> |
| 340 | static ResultType typeDouble() |
| 341 | { |
darin@apple.com | d5fb2b4 | 2015-04-20 06:05:26 +0000 | [diff] [blame] | 342 | return std::make_unique<CallbackResultNumeric<T>>(); |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | static ResultType typeBool() |
| 346 | { |
darin@apple.com | d5fb2b4 | 2015-04-20 06:05:26 +0000 | [diff] [blame] | 347 | return std::make_unique<CallbackResultBoolean>(); |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | static ResultType typeVoid() |
| 351 | { |
darin@apple.com | d5fb2b4 | 2015-04-20 06:05:26 +0000 | [diff] [blame] | 352 | return std::make_unique<CallbackResultVoid>(); |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | static ResultType typeId() |
| 356 | { |
darin@apple.com | d5fb2b4 | 2015-04-20 06:05:26 +0000 | [diff] [blame] | 357 | return std::make_unique<CallbackResultId>(); |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | static ResultType typeOfClass(const char*, const char*) |
| 361 | { |
darin@apple.com | d5fb2b4 | 2015-04-20 06:05:26 +0000 | [diff] [blame] | 362 | return std::make_unique<CallbackResultId>(); |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | static ResultType typeBlock(const char*, const char*) |
| 366 | { |
darin@apple.com | d5fb2b4 | 2015-04-20 06:05:26 +0000 | [diff] [blame] | 367 | return std::make_unique<CallbackResultId>(); |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | static ResultType typeStruct(const char* begin, const char* end) |
| 371 | { |
| 372 | StringRange copy(begin, end); |
barraclough@apple.com | e2c0742 | 2013-01-03 02:03:12 +0000 | [diff] [blame] | 373 | if (NSInvocation *invocation = typeToValueInvocationFor(copy)) |
darin@apple.com | d5fb2b4 | 2015-04-20 06:05:26 +0000 | [diff] [blame] | 374 | return std::make_unique<CallbackResultStruct>(invocation, copy); |
| 375 | return nullptr; |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 376 | } |
| 377 | }; |
| 378 | |
| 379 | enum CallbackType { |
mhahnenberg@apple.com | 8d51e00 | 2013-10-30 17:58:12 +0000 | [diff] [blame] | 380 | CallbackInitMethod, |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 381 | CallbackInstanceMethod, |
| 382 | CallbackClassMethod, |
| 383 | CallbackBlock |
| 384 | }; |
| 385 | |
mhahnenberg@apple.com | 9c4b210 | 2013-03-14 21:14:01 +0000 | [diff] [blame] | 386 | namespace JSC { |
| 387 | |
| 388 | class ObjCCallbackFunctionImpl { |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 389 | public: |
darin@apple.com | d5fb2b4 | 2015-04-20 06:05:26 +0000 | [diff] [blame] | 390 | ObjCCallbackFunctionImpl(NSInvocation *invocation, CallbackType type, Class instanceClass, std::unique_ptr<CallbackArgument> arguments, std::unique_ptr<CallbackResult> result) |
mhahnenberg@apple.com | e4b8bb7 | 2013-10-15 21:08:07 +0000 | [diff] [blame] | 391 | : m_type(type) |
darin@apple.com | d5fb2b4 | 2015-04-20 06:05:26 +0000 | [diff] [blame] | 392 | , m_instanceClass(instanceClass) |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 393 | , m_invocation(invocation) |
darin@apple.com | d5fb2b4 | 2015-04-20 06:05:26 +0000 | [diff] [blame] | 394 | , m_arguments(WTF::move(arguments)) |
| 395 | , m_result(WTF::move(result)) |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 396 | { |
mhahnenberg@apple.com | 8d51e00 | 2013-10-30 17:58:12 +0000 | [diff] [blame] | 397 | ASSERT((type != CallbackInstanceMethod && type != CallbackInitMethod) || instanceClass); |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 398 | } |
| 399 | |
mhahnenberg@apple.com | ac6f1fd | 2013-11-15 19:53:30 +0000 | [diff] [blame] | 400 | void destroy(Heap& heap) |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 401 | { |
mhahnenberg@apple.com | ac6f1fd | 2013-11-15 19:53:30 +0000 | [diff] [blame] | 402 | // We need to explicitly release the target since we didn't call |
mhahnenberg@apple.com | fc78fbc | 2013-11-05 22:51:52 +0000 | [diff] [blame] | 403 | // -retainArguments on m_invocation (and we don't want to do so). |
| 404 | if (m_type == CallbackBlock || m_type == CallbackClassMethod) |
mhahnenberg@apple.com | ac6f1fd | 2013-11-15 19:53:30 +0000 | [diff] [blame] | 405 | heap.releaseSoon(adoptNS([m_invocation.get() target])); |
darin@apple.com | d5fb2b4 | 2015-04-20 06:05:26 +0000 | [diff] [blame] | 406 | m_instanceClass = nil; |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | JSValueRef call(JSContext *context, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception); |
| 410 | |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 411 | id wrappedBlock() |
| 412 | { |
| 413 | return m_type == CallbackBlock ? [m_invocation target] : nil; |
| 414 | } |
| 415 | |
mhahnenberg@apple.com | 8d51e00 | 2013-10-30 17:58:12 +0000 | [diff] [blame] | 416 | id wrappedConstructor() |
| 417 | { |
| 418 | switch (m_type) { |
| 419 | case CallbackBlock: |
| 420 | return [m_invocation target]; |
| 421 | case CallbackInitMethod: |
darin@apple.com | d5fb2b4 | 2015-04-20 06:05:26 +0000 | [diff] [blame] | 422 | return m_instanceClass.get(); |
mhahnenberg@apple.com | 8d51e00 | 2013-10-30 17:58:12 +0000 | [diff] [blame] | 423 | default: |
| 424 | return nil; |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | bool isConstructible() |
| 429 | { |
| 430 | return !!wrappedBlock() || m_type == CallbackInitMethod; |
| 431 | } |
| 432 | |
| 433 | String name(); |
| 434 | |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 435 | private: |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 436 | CallbackType m_type; |
darin@apple.com | d5fb2b4 | 2015-04-20 06:05:26 +0000 | [diff] [blame] | 437 | RetainPtr<Class> m_instanceClass; |
barraclough@apple.com | a56aeb2 | 2013-01-02 23:34:48 +0000 | [diff] [blame] | 438 | RetainPtr<NSInvocation> m_invocation; |
darin@apple.com | d5fb2b4 | 2015-04-20 06:05:26 +0000 | [diff] [blame] | 439 | std::unique_ptr<CallbackArgument> m_arguments; |
| 440 | std::unique_ptr<CallbackResult> m_result; |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 441 | }; |
| 442 | |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 443 | static JSValueRef objCCallbackFunctionCallAsFunction(JSContextRef callerContext, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) |
| 444 | { |
| 445 | // Retake the API lock - we need this for a few reasons: |
barraclough@apple.com | a56aeb2 | 2013-01-02 23:34:48 +0000 | [diff] [blame] | 446 | // (1) We don't want to support the C-API's confusing drops-locks-once policy - should only drop locks if we can do so recursively. |
| 447 | // (2) We're calling some JSC internals that require us to be on the 'inside' - e.g. createTypeError. |
| 448 | // (3) We need to be locked (per context would be fine) against conflicting usage of the ObjCCallbackFunction's NSInvocation. |
mhahnenberg@apple.com | 085d24e | 2014-03-04 21:38:05 +0000 | [diff] [blame] | 449 | JSC::JSLockHolder locker(toJS(callerContext)); |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 450 | |
mhahnenberg@apple.com | 9c4b210 | 2013-03-14 21:14:01 +0000 | [diff] [blame] | 451 | ObjCCallbackFunction* callback = static_cast<ObjCCallbackFunction*>(toJS(function)); |
| 452 | ObjCCallbackFunctionImpl* impl = callback->impl(); |
mhahnenberg@apple.com | e4b8bb7 | 2013-10-15 21:08:07 +0000 | [diff] [blame] | 453 | JSContext *context = [JSContext contextWithJSGlobalContextRef:toGlobalRef(callback->globalObject()->globalExec())]; |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 454 | |
| 455 | CallbackData callbackData; |
| 456 | JSValueRef result; |
| 457 | @autoreleasepool { |
mhahnenberg@apple.com | 4f18e95 | 2014-02-07 01:56:45 +0000 | [diff] [blame] | 458 | [context beginCallbackWithData:&callbackData calleeValue:function thisValue:thisObject argumentCount:argumentCount arguments:arguments]; |
mhahnenberg@apple.com | 9c4b210 | 2013-03-14 21:14:01 +0000 | [diff] [blame] | 459 | result = impl->call(context, thisObject, argumentCount, arguments, exception); |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 460 | if (context.exception) |
| 461 | *exception = valueInternalValue(context.exception); |
| 462 | [context endCallbackWithData:&callbackData]; |
| 463 | } |
| 464 | return result; |
| 465 | } |
| 466 | |
mhahnenberg@apple.com | 2ac93f3 | 2013-10-10 18:20:13 +0000 | [diff] [blame] | 467 | static JSObjectRef objCCallbackFunctionCallAsConstructor(JSContextRef callerContext, JSObjectRef constructor, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) |
| 468 | { |
mhahnenberg@apple.com | 085d24e | 2014-03-04 21:38:05 +0000 | [diff] [blame] | 469 | JSC::JSLockHolder locker(toJS(callerContext)); |
mhahnenberg@apple.com | 2ac93f3 | 2013-10-10 18:20:13 +0000 | [diff] [blame] | 470 | |
| 471 | ObjCCallbackFunction* callback = static_cast<ObjCCallbackFunction*>(toJS(constructor)); |
| 472 | ObjCCallbackFunctionImpl* impl = callback->impl(); |
| 473 | JSContext *context = [JSContext contextWithJSGlobalContextRef:toGlobalRef(toJS(callerContext)->lexicalGlobalObject()->globalExec())]; |
| 474 | |
| 475 | CallbackData callbackData; |
| 476 | JSValueRef result; |
| 477 | @autoreleasepool { |
darin@apple.com | d5fb2b4 | 2015-04-20 06:05:26 +0000 | [diff] [blame] | 478 | [context beginCallbackWithData:&callbackData calleeValue:constructor thisValue:nullptr argumentCount:argumentCount arguments:arguments]; |
| 479 | result = impl->call(context, nullptr, argumentCount, arguments, exception); |
mhahnenberg@apple.com | 2ac93f3 | 2013-10-10 18:20:13 +0000 | [diff] [blame] | 480 | if (context.exception) |
| 481 | *exception = valueInternalValue(context.exception); |
| 482 | [context endCallbackWithData:&callbackData]; |
| 483 | } |
| 484 | |
| 485 | JSGlobalContextRef contextRef = [context JSGlobalContextRef]; |
| 486 | if (*exception) |
darin@apple.com | d5fb2b4 | 2015-04-20 06:05:26 +0000 | [diff] [blame] | 487 | return nullptr; |
mhahnenberg@apple.com | 2ac93f3 | 2013-10-10 18:20:13 +0000 | [diff] [blame] | 488 | |
| 489 | if (!JSValueIsObject(contextRef, result)) { |
mhahnenberg@apple.com | 635733b | 2014-02-14 22:44:52 +0000 | [diff] [blame] | 490 | *exception = toRef(JSC::createTypeError(toJS(contextRef), ASCIILiteral("Objective-C blocks called as constructors must return an object."))); |
darin@apple.com | d5fb2b4 | 2015-04-20 06:05:26 +0000 | [diff] [blame] | 491 | return nullptr; |
mhahnenberg@apple.com | 2ac93f3 | 2013-10-10 18:20:13 +0000 | [diff] [blame] | 492 | } |
| 493 | return (JSObjectRef)result; |
| 494 | } |
| 495 | |
akling@apple.com | 2de49b7 | 2014-07-30 22:26:22 +0000 | [diff] [blame] | 496 | const JSC::ClassInfo ObjCCallbackFunction::s_info = { "CallbackFunction", &Base::s_info, 0, CREATE_METHOD_TABLE(ObjCCallbackFunction) }; |
mhahnenberg@apple.com | 9c4b210 | 2013-03-14 21:14:01 +0000 | [diff] [blame] | 497 | |
darin@apple.com | d5fb2b4 | 2015-04-20 06:05:26 +0000 | [diff] [blame] | 498 | ObjCCallbackFunction::ObjCCallbackFunction(JSC::VM& vm, JSC::JSGlobalObject* globalObject, JSObjectCallAsFunctionCallback functionCallback, JSObjectCallAsConstructorCallback constructCallback, std::unique_ptr<ObjCCallbackFunctionImpl> impl) |
akling@apple.com | 5987552 | 2013-09-30 03:45:30 +0000 | [diff] [blame] | 499 | : Base(vm, globalObject->objcCallbackFunctionStructure()) |
mhahnenberg@apple.com | 2ac93f3 | 2013-10-10 18:20:13 +0000 | [diff] [blame] | 500 | , m_functionCallback(functionCallback) |
| 501 | , m_constructCallback(constructCallback) |
darin@apple.com | d5fb2b4 | 2015-04-20 06:05:26 +0000 | [diff] [blame] | 502 | , m_impl(WTF::move(impl)) |
barraclough@apple.com | a56aeb2 | 2013-01-02 23:34:48 +0000 | [diff] [blame] | 503 | { |
barraclough@apple.com | a56aeb2 | 2013-01-02 23:34:48 +0000 | [diff] [blame] | 504 | } |
| 505 | |
darin@apple.com | d5fb2b4 | 2015-04-20 06:05:26 +0000 | [diff] [blame] | 506 | ObjCCallbackFunction* ObjCCallbackFunction::create(JSC::VM& vm, JSC::JSGlobalObject* globalObject, const String& name, std::unique_ptr<ObjCCallbackFunctionImpl> impl) |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 507 | { |
darin@apple.com | d5fb2b4 | 2015-04-20 06:05:26 +0000 | [diff] [blame] | 508 | ObjCCallbackFunction* function = new (NotNull, allocateCell<ObjCCallbackFunction>(vm.heap)) ObjCCallbackFunction(vm, globalObject, objCCallbackFunctionCallAsFunction, objCCallbackFunctionCallAsConstructor, WTF::move(impl)); |
akling@apple.com | 5987552 | 2013-09-30 03:45:30 +0000 | [diff] [blame] | 509 | function->finishCreation(vm, name); |
mhahnenberg@apple.com | 9c4b210 | 2013-03-14 21:14:01 +0000 | [diff] [blame] | 510 | return function; |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 511 | } |
| 512 | |
mhahnenberg@apple.com | 9c4b210 | 2013-03-14 21:14:01 +0000 | [diff] [blame] | 513 | void ObjCCallbackFunction::destroy(JSCell* cell) |
| 514 | { |
mhahnenberg@apple.com | ac6f1fd | 2013-11-15 19:53:30 +0000 | [diff] [blame] | 515 | ObjCCallbackFunction& function = *jsCast<ObjCCallbackFunction*>(cell); |
| 516 | function.impl()->destroy(*Heap::heap(cell)); |
| 517 | function.~ObjCCallbackFunction(); |
mhahnenberg@apple.com | 9c4b210 | 2013-03-14 21:14:01 +0000 | [diff] [blame] | 518 | } |
| 519 | |
mhahnenberg@apple.com | 8d51e00 | 2013-10-30 17:58:12 +0000 | [diff] [blame] | 520 | |
oliver@apple.com | 5109edb | 2013-07-25 04:03:12 +0000 | [diff] [blame] | 521 | CallType ObjCCallbackFunction::getCallData(JSCell*, CallData& callData) |
| 522 | { |
| 523 | callData.native.function = APICallbackFunction::call<ObjCCallbackFunction>; |
| 524 | return CallTypeHost; |
| 525 | } |
| 526 | |
mhahnenberg@apple.com | 2ac93f3 | 2013-10-10 18:20:13 +0000 | [diff] [blame] | 527 | ConstructType ObjCCallbackFunction::getConstructData(JSCell* cell, ConstructData& constructData) |
| 528 | { |
| 529 | ObjCCallbackFunction* callback = jsCast<ObjCCallbackFunction*>(cell); |
mhahnenberg@apple.com | 8d51e00 | 2013-10-30 17:58:12 +0000 | [diff] [blame] | 530 | if (!callback->impl()->isConstructible()) |
mhahnenberg@apple.com | 2ac93f3 | 2013-10-10 18:20:13 +0000 | [diff] [blame] | 531 | return Base::getConstructData(cell, constructData); |
| 532 | constructData.native.function = APICallbackFunction::construct<ObjCCallbackFunction>; |
| 533 | return ConstructTypeHost; |
| 534 | } |
| 535 | |
mhahnenberg@apple.com | 8d51e00 | 2013-10-30 17:58:12 +0000 | [diff] [blame] | 536 | String ObjCCallbackFunctionImpl::name() |
| 537 | { |
| 538 | if (m_type == CallbackInitMethod) |
darin@apple.com | d5fb2b4 | 2015-04-20 06:05:26 +0000 | [diff] [blame] | 539 | return class_getName(m_instanceClass.get()); |
mhahnenberg@apple.com | fc78fbc | 2013-11-05 22:51:52 +0000 | [diff] [blame] | 540 | // FIXME: Maybe we could support having the selector as the name of the non-init |
| 541 | // functions to make it a bit more user-friendly from the JS side? |
mhahnenberg@apple.com | 8d51e00 | 2013-10-30 17:58:12 +0000 | [diff] [blame] | 542 | return ""; |
| 543 | } |
| 544 | |
mhahnenberg@apple.com | 9c4b210 | 2013-03-14 21:14:01 +0000 | [diff] [blame] | 545 | JSValueRef ObjCCallbackFunctionImpl::call(JSContext *context, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 546 | { |
ggaren@apple.com | 58bf4ea | 2013-04-30 21:55:43 +0000 | [diff] [blame] | 547 | JSGlobalContextRef contextRef = [context JSGlobalContextRef]; |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 548 | |
mhahnenberg@apple.com | 8d51e00 | 2013-10-30 17:58:12 +0000 | [diff] [blame] | 549 | id target; |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 550 | size_t firstArgument; |
| 551 | switch (m_type) { |
mhahnenberg@apple.com | 8d51e00 | 2013-10-30 17:58:12 +0000 | [diff] [blame] | 552 | case CallbackInitMethod: { |
| 553 | RELEASE_ASSERT(!thisObject); |
| 554 | target = [m_instanceClass alloc]; |
darin@apple.com | d5fb2b4 | 2015-04-20 06:05:26 +0000 | [diff] [blame] | 555 | if (!target || ![target isKindOfClass:m_instanceClass.get()]) { |
mhahnenberg@apple.com | 635733b | 2014-02-14 22:44:52 +0000 | [diff] [blame] | 556 | *exception = toRef(JSC::createTypeError(toJS(contextRef), ASCIILiteral("self type check failed for Objective-C instance method"))); |
barraclough@apple.com | a56aeb2 | 2013-01-02 23:34:48 +0000 | [diff] [blame] | 557 | return JSValueMakeUndefined(contextRef); |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 558 | } |
| 559 | [m_invocation setTarget:target]; |
mhahnenberg@apple.com | 8d51e00 | 2013-10-30 17:58:12 +0000 | [diff] [blame] | 560 | firstArgument = 2; |
| 561 | break; |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 562 | } |
mhahnenberg@apple.com | 8d51e00 | 2013-10-30 17:58:12 +0000 | [diff] [blame] | 563 | case CallbackInstanceMethod: { |
| 564 | target = tryUnwrapObjcObject(contextRef, thisObject); |
darin@apple.com | d5fb2b4 | 2015-04-20 06:05:26 +0000 | [diff] [blame] | 565 | if (!target || ![target isKindOfClass:m_instanceClass.get()]) { |
mhahnenberg@apple.com | 635733b | 2014-02-14 22:44:52 +0000 | [diff] [blame] | 566 | *exception = toRef(JSC::createTypeError(toJS(contextRef), ASCIILiteral("self type check failed for Objective-C instance method"))); |
mhahnenberg@apple.com | 8d51e00 | 2013-10-30 17:58:12 +0000 | [diff] [blame] | 567 | return JSValueMakeUndefined(contextRef); |
| 568 | } |
| 569 | [m_invocation setTarget:target]; |
| 570 | firstArgument = 2; |
| 571 | break; |
| 572 | } |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 573 | case CallbackClassMethod: |
| 574 | firstArgument = 2; |
| 575 | break; |
| 576 | case CallbackBlock: |
| 577 | firstArgument = 1; |
| 578 | } |
| 579 | |
| 580 | size_t argumentNumber = 0; |
| 581 | for (CallbackArgument* argument = m_arguments.get(); argument; argument = argument->m_next.get()) { |
barraclough@apple.com | a56aeb2 | 2013-01-02 23:34:48 +0000 | [diff] [blame] | 582 | JSValueRef value = argumentNumber < argumentCount ? arguments[argumentNumber] : JSValueMakeUndefined(contextRef); |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 583 | argument->set(m_invocation.get(), argumentNumber + firstArgument, context, value, exception); |
| 584 | if (*exception) |
barraclough@apple.com | a56aeb2 | 2013-01-02 23:34:48 +0000 | [diff] [blame] | 585 | return JSValueMakeUndefined(contextRef); |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 586 | ++argumentNumber; |
| 587 | } |
| 588 | |
| 589 | [m_invocation invoke]; |
| 590 | |
mhahnenberg@apple.com | 8d51e00 | 2013-10-30 17:58:12 +0000 | [diff] [blame] | 591 | JSValueRef result = m_result->get(m_invocation.get(), context, exception); |
| 592 | |
| 593 | // Balance our call to -alloc with a call to -autorelease. We have to do this after calling -init |
| 594 | // because init family methods are allowed to release the allocated object and return something |
| 595 | // else in its place. |
| 596 | if (m_type == CallbackInitMethod) { |
| 597 | id objcResult = tryUnwrapObjcObject(contextRef, result); |
| 598 | if (objcResult) |
| 599 | [objcResult autorelease]; |
| 600 | } |
| 601 | |
| 602 | return result; |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 603 | } |
| 604 | |
mhahnenberg@apple.com | 9c4b210 | 2013-03-14 21:14:01 +0000 | [diff] [blame] | 605 | } // namespace JSC |
| 606 | |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 607 | static bool blockSignatureContainsClass() |
| 608 | { |
barraclough@apple.com | a56aeb2 | 2013-01-02 23:34:48 +0000 | [diff] [blame] | 609 | static bool containsClass = ^{ |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 610 | id block = ^(NSString *string){ return string; }; |
barraclough@apple.com | a56aeb2 | 2013-01-02 23:34:48 +0000 | [diff] [blame] | 611 | return _Block_has_signature(block) && strstr(_Block_signature(block), "NSString"); |
mhahnenberg@apple.com | bf1fd77 | 2013-01-11 23:09:37 +0000 | [diff] [blame] | 612 | }(); |
barraclough@apple.com | a56aeb2 | 2013-01-02 23:34:48 +0000 | [diff] [blame] | 613 | return containsClass; |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 614 | } |
| 615 | |
darin@apple.com | d5fb2b4 | 2015-04-20 06:05:26 +0000 | [diff] [blame] | 616 | static inline bool skipNumber(const char*& position) |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 617 | { |
barraclough@apple.com | a56aeb2 | 2013-01-02 23:34:48 +0000 | [diff] [blame] | 618 | if (!isASCIIDigit(*position)) |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 619 | return false; |
barraclough@apple.com | a56aeb2 | 2013-01-02 23:34:48 +0000 | [diff] [blame] | 620 | while (isASCIIDigit(*++position)) { } |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 621 | return true; |
| 622 | } |
| 623 | |
barraclough@apple.com | e2c0742 | 2013-01-03 02:03:12 +0000 | [diff] [blame] | 624 | static JSObjectRef objCCallbackFunctionForInvocation(JSContext *context, NSInvocation *invocation, CallbackType type, Class instanceClass, const char* signatureWithObjcClasses) |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 625 | { |
mhahnenberg@apple.com | 3cdb3ab | 2014-02-20 19:40:04 +0000 | [diff] [blame] | 626 | if (!signatureWithObjcClasses) |
darin@apple.com | d5fb2b4 | 2015-04-20 06:05:26 +0000 | [diff] [blame] | 627 | return nullptr; |
mhahnenberg@apple.com | 3cdb3ab | 2014-02-20 19:40:04 +0000 | [diff] [blame] | 628 | |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 629 | const char* position = signatureWithObjcClasses; |
| 630 | |
darin@apple.com | d5fb2b4 | 2015-04-20 06:05:26 +0000 | [diff] [blame] | 631 | auto result = parseObjCType<ResultTypeDelegate>(position); |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 632 | if (!result || !skipNumber(position)) |
darin@apple.com | d5fb2b4 | 2015-04-20 06:05:26 +0000 | [diff] [blame] | 633 | return nullptr; |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 634 | |
| 635 | switch (type) { |
mhahnenberg@apple.com | 8d51e00 | 2013-10-30 17:58:12 +0000 | [diff] [blame] | 636 | case CallbackInitMethod: |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 637 | case CallbackInstanceMethod: |
| 638 | case CallbackClassMethod: |
barraclough@apple.com | a56aeb2 | 2013-01-02 23:34:48 +0000 | [diff] [blame] | 639 | // Methods are passed two implicit arguments - (id)self, and the selector. |
| 640 | if ('@' != *position++ || !skipNumber(position) || ':' != *position++ || !skipNumber(position)) |
darin@apple.com | d5fb2b4 | 2015-04-20 06:05:26 +0000 | [diff] [blame] | 641 | return nullptr; |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 642 | break; |
| 643 | case CallbackBlock: |
barraclough@apple.com | a56aeb2 | 2013-01-02 23:34:48 +0000 | [diff] [blame] | 644 | // Blocks are passed one implicit argument - the block, of type "@?". |
| 645 | if (('@' != *position++) || ('?' != *position++) || !skipNumber(position)) |
darin@apple.com | d5fb2b4 | 2015-04-20 06:05:26 +0000 | [diff] [blame] | 646 | return nullptr; |
barraclough@apple.com | a56aeb2 | 2013-01-02 23:34:48 +0000 | [diff] [blame] | 647 | // Only allow arguments of type 'id' if the block signature contains the NS type information. |
| 648 | if ((!blockSignatureContainsClass() && strchr(position, '@'))) |
darin@apple.com | d5fb2b4 | 2015-04-20 06:05:26 +0000 | [diff] [blame] | 649 | return nullptr; |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 650 | break; |
| 651 | } |
| 652 | |
darin@apple.com | d5fb2b4 | 2015-04-20 06:05:26 +0000 | [diff] [blame] | 653 | std::unique_ptr<CallbackArgument> arguments; |
| 654 | auto* nextArgument = &arguments; |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 655 | unsigned argumentCount = 0; |
| 656 | while (*position) { |
darin@apple.com | d5fb2b4 | 2015-04-20 06:05:26 +0000 | [diff] [blame] | 657 | auto argument = parseObjCType<ArgumentTypeDelegate>(position); |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 658 | if (!argument || !skipNumber(position)) |
darin@apple.com | d5fb2b4 | 2015-04-20 06:05:26 +0000 | [diff] [blame] | 659 | return nullptr; |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 660 | |
darin@apple.com | d5fb2b4 | 2015-04-20 06:05:26 +0000 | [diff] [blame] | 661 | *nextArgument = WTF::move(argument); |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 662 | nextArgument = &(*nextArgument)->m_next; |
| 663 | ++argumentCount; |
| 664 | } |
| 665 | |
ggaren@apple.com | 58bf4ea | 2013-04-30 21:55:43 +0000 | [diff] [blame] | 666 | JSC::ExecState* exec = toJS([context JSGlobalContextRef]); |
mhahnenberg@apple.com | 085d24e | 2014-03-04 21:38:05 +0000 | [diff] [blame] | 667 | JSC::JSLockHolder locker(exec); |
darin@apple.com | d5fb2b4 | 2015-04-20 06:05:26 +0000 | [diff] [blame] | 668 | auto impl = std::make_unique<JSC::ObjCCallbackFunctionImpl>(invocation, type, instanceClass, WTF::move(arguments), WTF::move(result)); |
| 669 | const String& name = impl->name(); |
| 670 | return toRef(JSC::ObjCCallbackFunction::create(exec->vm(), exec->lexicalGlobalObject(), name, WTF::move(impl))); |
mhahnenberg@apple.com | 8d51e00 | 2013-10-30 17:58:12 +0000 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | JSObjectRef objCCallbackFunctionForInit(JSContext *context, Class cls, Protocol *protocol, SEL sel, const char* types) |
| 674 | { |
| 675 | NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[NSMethodSignature signatureWithObjCTypes:types]]; |
| 676 | [invocation setSelector:sel]; |
| 677 | return objCCallbackFunctionForInvocation(context, invocation, CallbackInitMethod, cls, _protocol_getMethodTypeEncoding(protocol, sel, YES, YES)); |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 678 | } |
| 679 | |
barraclough@apple.com | e2c0742 | 2013-01-03 02:03:12 +0000 | [diff] [blame] | 680 | JSObjectRef objCCallbackFunctionForMethod(JSContext *context, Class cls, Protocol *protocol, BOOL isInstanceMethod, SEL sel, const char* types) |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 681 | { |
barraclough@apple.com | e2c0742 | 2013-01-03 02:03:12 +0000 | [diff] [blame] | 682 | NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[NSMethodSignature signatureWithObjCTypes:types]]; |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 683 | [invocation setSelector:sel]; |
darin@apple.com | d5fb2b4 | 2015-04-20 06:05:26 +0000 | [diff] [blame] | 684 | // We need to retain the target Class because m_invocation doesn't retain it by default (and we don't want it to). |
| 685 | // FIXME: What releases it? |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 686 | if (!isInstanceMethod) |
mhahnenberg@apple.com | fc78fbc | 2013-11-05 22:51:52 +0000 | [diff] [blame] | 687 | [invocation setTarget:[cls retain]]; |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 688 | return objCCallbackFunctionForInvocation(context, invocation, isInstanceMethod ? CallbackInstanceMethod : CallbackClassMethod, isInstanceMethod ? cls : nil, _protocol_getMethodTypeEncoding(protocol, sel, YES, isInstanceMethod)); |
| 689 | } |
| 690 | |
barraclough@apple.com | e2c0742 | 2013-01-03 02:03:12 +0000 | [diff] [blame] | 691 | JSObjectRef objCCallbackFunctionForBlock(JSContext *context, id target) |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 692 | { |
| 693 | if (!_Block_has_signature(target)) |
darin@apple.com | d5fb2b4 | 2015-04-20 06:05:26 +0000 | [diff] [blame] | 694 | return nullptr; |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 695 | const char* signature = _Block_signature(target); |
barraclough@apple.com | e2c0742 | 2013-01-03 02:03:12 +0000 | [diff] [blame] | 696 | NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[NSMethodSignature signatureWithObjCTypes:signature]]; |
mhahnenberg@apple.com | fc78fbc | 2013-11-05 22:51:52 +0000 | [diff] [blame] | 697 | |
| 698 | // We don't want to use -retainArguments because that leaks memory. Arguments |
| 699 | // would be retained indefinitely between invocations of the callback. |
| 700 | // Additionally, we copy the target because we want the block to stick around |
| 701 | // until the ObjCCallbackFunctionImpl is destroyed. |
| 702 | [invocation setTarget:[target copy]]; |
| 703 | |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 704 | return objCCallbackFunctionForInvocation(context, invocation, CallbackBlock, nil, signature); |
| 705 | } |
| 706 | |
mhahnenberg@apple.com | 8d51e00 | 2013-10-30 17:58:12 +0000 | [diff] [blame] | 707 | id tryUnwrapConstructor(JSObjectRef object) |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 708 | { |
fpizlo@apple.com | f00d709 | 2013-08-14 05:21:10 +0000 | [diff] [blame] | 709 | if (!toJS(object)->inherits(JSC::ObjCCallbackFunction::info())) |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 710 | return nil; |
mhahnenberg@apple.com | 8d51e00 | 2013-10-30 17:58:12 +0000 | [diff] [blame] | 711 | JSC::ObjCCallbackFunctionImpl* impl = static_cast<JSC::ObjCCallbackFunction*>(toJS(object))->impl(); |
| 712 | if (!impl->isConstructible()) |
| 713 | return nil; |
| 714 | return impl->wrappedConstructor(); |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 715 | } |
| 716 | |
| 717 | #endif |