]>
Commit | Line | Data |
---|---|---|
93a37866 A |
1 | /* |
2 | * Copyright (C) 2013 Apple Inc. All rights reserved. | |
3 | * | |
4 | * Redistribution and use in source and binary forms, with or without | |
5 | * modification, are permitted provided that the following conditions | |
6 | * are met: | |
7 | * 1. Redistributions of source code must retain the above copyright | |
8 | * notice, this list of conditions and the following disclaimer. | |
9 | * 2. Redistributions in binary form must reproduce the above copyright | |
10 | * notice, this list of conditions and the following disclaimer in the | |
11 | * documentation and/or other materials provided with the distribution. | |
12 | * | |
13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY | |
14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR | |
17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | |
21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
24 | */ | |
25 | ||
26 | #include "config.h" | |
27 | #import "JavaScriptCore.h" | |
28 | ||
29 | #if JSC_OBJC_API_ENABLED | |
30 | ||
12899fa2 | 31 | #import "APICallbackFunction.h" |
93a37866 | 32 | #import "APICast.h" |
93a37866 A |
33 | #import "Error.h" |
34 | #import "JSCJSValueInlines.h" | |
35 | #import "JSCell.h" | |
36 | #import "JSCellInlines.h" | |
37 | #import "JSContextInternal.h" | |
38 | #import "JSWrapperMap.h" | |
39 | #import "JSValueInternal.h" | |
40 | #import "ObjCCallbackFunction.h" | |
41 | #import "ObjcRuntimeExtras.h" | |
42 | #import <objc/runtime.h> | |
43 | #import <wtf/RetainPtr.h> | |
44 | ||
45 | class CallbackArgument { | |
46 | public: | |
47 | virtual ~CallbackArgument(); | |
48 | virtual void set(NSInvocation *, NSInteger, JSContext *, JSValueRef, JSValueRef*) = 0; | |
49 | ||
ed1e77d3 | 50 | std::unique_ptr<CallbackArgument> m_next; |
93a37866 A |
51 | }; |
52 | ||
53 | CallbackArgument::~CallbackArgument() | |
54 | { | |
55 | } | |
56 | ||
57 | class CallbackArgumentBoolean : public CallbackArgument { | |
58 | virtual void set(NSInvocation *invocation, NSInteger argumentNumber, JSContext *context, JSValueRef argument, JSValueRef*) override | |
59 | { | |
60 | bool value = JSValueToBoolean([context JSGlobalContextRef], argument); | |
61 | [invocation setArgument:&value atIndex:argumentNumber]; | |
62 | } | |
63 | }; | |
64 | ||
65 | template<typename T> | |
66 | class CallbackArgumentInteger : public CallbackArgument { | |
67 | virtual void set(NSInvocation *invocation, NSInteger argumentNumber, JSContext *context, JSValueRef argument, JSValueRef* exception) override | |
68 | { | |
69 | T value = (T)JSC::toInt32(JSValueToNumber([context JSGlobalContextRef], argument, exception)); | |
70 | [invocation setArgument:&value atIndex:argumentNumber]; | |
71 | } | |
72 | }; | |
73 | ||
74 | template<typename T> | |
75 | class CallbackArgumentDouble : public CallbackArgument { | |
76 | virtual void set(NSInvocation *invocation, NSInteger argumentNumber, JSContext *context, JSValueRef argument, JSValueRef* exception) override | |
77 | { | |
78 | T value = (T)JSValueToNumber([context JSGlobalContextRef], argument, exception); | |
79 | [invocation setArgument:&value atIndex:argumentNumber]; | |
80 | } | |
81 | }; | |
82 | ||
83 | class CallbackArgumentJSValue : public CallbackArgument { | |
84 | virtual void set(NSInvocation *invocation, NSInteger argumentNumber, JSContext *context, JSValueRef argument, JSValueRef*) override | |
85 | { | |
86 | JSValue *value = [JSValue valueWithJSValueRef:argument inContext:context]; | |
87 | [invocation setArgument:&value atIndex:argumentNumber]; | |
88 | } | |
89 | }; | |
90 | ||
91 | class CallbackArgumentId : public CallbackArgument { | |
92 | virtual void set(NSInvocation *invocation, NSInteger argumentNumber, JSContext *context, JSValueRef argument, JSValueRef*) override | |
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) | |
ed1e77d3 | 102 | : m_class(cls) |
93a37866 | 103 | { |
93a37866 A |
104 | } |
105 | ||
106 | private: | |
93a37866 A |
107 | virtual void set(NSInvocation *invocation, NSInteger argumentNumber, JSContext *context, JSValueRef argument, JSValueRef* exception) override |
108 | { | |
109 | JSGlobalContextRef contextRef = [context JSGlobalContextRef]; | |
110 | ||
111 | id object = tryUnwrapObjcObject(contextRef, argument); | |
ed1e77d3 | 112 | if (object && [object isKindOfClass:m_class.get()]) { |
93a37866 A |
113 | [invocation setArgument:&object atIndex:argumentNumber]; |
114 | return; | |
115 | } | |
116 | ||
117 | if (JSValueIsNull(contextRef, argument) || JSValueIsUndefined(contextRef, argument)) { | |
118 | object = nil; | |
119 | [invocation setArgument:&object atIndex:argumentNumber]; | |
120 | return; | |
121 | } | |
122 | ||
81345200 | 123 | *exception = toRef(JSC::createTypeError(toJS(contextRef), ASCIILiteral("Argument does not match Objective-C Class"))); |
93a37866 A |
124 | } |
125 | ||
ed1e77d3 | 126 | RetainPtr<Class> m_class; |
93a37866 A |
127 | }; |
128 | ||
129 | class CallbackArgumentNSNumber : public CallbackArgument { | |
130 | virtual void set(NSInvocation *invocation, NSInteger argumentNumber, JSContext *context, JSValueRef argument, JSValueRef* exception) override | |
131 | { | |
132 | id value = valueToNumber([context JSGlobalContextRef], argument, exception); | |
133 | [invocation setArgument:&value atIndex:argumentNumber]; | |
134 | } | |
135 | }; | |
136 | ||
137 | class CallbackArgumentNSString : public CallbackArgument { | |
138 | virtual void set(NSInvocation *invocation, NSInteger argumentNumber, JSContext *context, JSValueRef argument, JSValueRef* exception) override | |
139 | { | |
140 | id value = valueToString([context JSGlobalContextRef], argument, exception); | |
141 | [invocation setArgument:&value atIndex:argumentNumber]; | |
142 | } | |
143 | }; | |
144 | ||
145 | class CallbackArgumentNSDate : public CallbackArgument { | |
146 | virtual void set(NSInvocation *invocation, NSInteger argumentNumber, JSContext *context, JSValueRef argument, JSValueRef* exception) override | |
147 | { | |
148 | id value = valueToDate([context JSGlobalContextRef], argument, exception); | |
149 | [invocation setArgument:&value atIndex:argumentNumber]; | |
150 | } | |
151 | }; | |
152 | ||
153 | class CallbackArgumentNSArray : public CallbackArgument { | |
154 | virtual void set(NSInvocation *invocation, NSInteger argumentNumber, JSContext *context, JSValueRef argument, JSValueRef* exception) override | |
155 | { | |
156 | id value = valueToArray([context JSGlobalContextRef], argument, exception); | |
157 | [invocation setArgument:&value atIndex:argumentNumber]; | |
158 | } | |
159 | }; | |
160 | ||
161 | class CallbackArgumentNSDictionary : public CallbackArgument { | |
162 | virtual void set(NSInvocation *invocation, NSInteger argumentNumber, JSContext *context, JSValueRef argument, JSValueRef* exception) override | |
163 | { | |
164 | id value = valueToDictionary([context JSGlobalContextRef], argument, exception); | |
165 | [invocation setArgument:&value atIndex:argumentNumber]; | |
166 | } | |
167 | }; | |
168 | ||
169 | class CallbackArgumentStruct : public CallbackArgument { | |
170 | public: | |
171 | CallbackArgumentStruct(NSInvocation *conversionInvocation, const char* encodedType) | |
172 | : m_conversionInvocation(conversionInvocation) | |
173 | , m_buffer(encodedType) | |
174 | { | |
175 | } | |
176 | ||
177 | private: | |
178 | virtual void set(NSInvocation *invocation, NSInteger argumentNumber, JSContext *context, JSValueRef argument, JSValueRef*) override | |
179 | { | |
180 | JSValue *value = [JSValue valueWithJSValueRef:argument inContext:context]; | |
181 | [m_conversionInvocation invokeWithTarget:value]; | |
182 | [m_conversionInvocation getReturnValue:m_buffer]; | |
183 | [invocation setArgument:m_buffer atIndex:argumentNumber]; | |
184 | } | |
185 | ||
186 | RetainPtr<NSInvocation> m_conversionInvocation; | |
187 | StructBuffer m_buffer; | |
188 | }; | |
189 | ||
190 | class ArgumentTypeDelegate { | |
191 | public: | |
ed1e77d3 | 192 | typedef std::unique_ptr<CallbackArgument> ResultType; |
93a37866 A |
193 | |
194 | template<typename T> | |
195 | static ResultType typeInteger() | |
196 | { | |
ed1e77d3 | 197 | return std::make_unique<CallbackArgumentInteger<T>>(); |
93a37866 A |
198 | } |
199 | ||
200 | template<typename T> | |
201 | static ResultType typeDouble() | |
202 | { | |
ed1e77d3 | 203 | return std::make_unique<CallbackArgumentDouble<T>>(); |
93a37866 A |
204 | } |
205 | ||
206 | static ResultType typeBool() | |
207 | { | |
ed1e77d3 | 208 | return std::make_unique<CallbackArgumentBoolean>(); |
93a37866 A |
209 | } |
210 | ||
211 | static ResultType typeVoid() | |
212 | { | |
213 | RELEASE_ASSERT_NOT_REACHED(); | |
ed1e77d3 | 214 | return nullptr; |
93a37866 A |
215 | } |
216 | ||
217 | static ResultType typeId() | |
218 | { | |
ed1e77d3 | 219 | return std::make_unique<CallbackArgumentId>(); |
93a37866 A |
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) | |
ed1e77d3 | 227 | return nullptr; |
93a37866 A |
228 | |
229 | if (cls == [JSValue class]) | |
ed1e77d3 | 230 | return std::make_unique<CallbackArgumentJSValue>(); |
93a37866 | 231 | if (cls == [NSString class]) |
ed1e77d3 | 232 | return std::make_unique<CallbackArgumentNSString>(); |
93a37866 | 233 | if (cls == [NSNumber class]) |
ed1e77d3 | 234 | return std::make_unique<CallbackArgumentNSNumber>(); |
93a37866 | 235 | if (cls == [NSDate class]) |
ed1e77d3 | 236 | return std::make_unique<CallbackArgumentNSDate>(); |
93a37866 | 237 | if (cls == [NSArray class]) |
ed1e77d3 | 238 | return std::make_unique<CallbackArgumentNSArray>(); |
93a37866 | 239 | if (cls == [NSDictionary class]) |
ed1e77d3 | 240 | return std::make_unique<CallbackArgumentNSDictionary>(); |
93a37866 | 241 | |
ed1e77d3 | 242 | return std::make_unique<CallbackArgumentOfClass>(cls); |
93a37866 A |
243 | } |
244 | ||
245 | static ResultType typeBlock(const char*, const char*) | |
246 | { | |
ed1e77d3 | 247 | return nullptr; |
93a37866 A |
248 | } |
249 | ||
250 | static ResultType typeStruct(const char* begin, const char* end) | |
251 | { | |
252 | StringRange copy(begin, end); | |
253 | if (NSInvocation *invocation = valueToTypeInvocationFor(copy)) | |
ed1e77d3 A |
254 | return std::make_unique<CallbackArgumentStruct>(invocation, copy); |
255 | return nullptr; | |
93a37866 A |
256 | } |
257 | }; | |
258 | ||
259 | class CallbackResult { | |
260 | public: | |
261 | virtual ~CallbackResult() | |
262 | { | |
263 | } | |
264 | ||
265 | virtual JSValueRef get(NSInvocation *, JSContext *, JSValueRef*) = 0; | |
266 | }; | |
267 | ||
268 | class CallbackResultVoid : public CallbackResult { | |
269 | virtual JSValueRef get(NSInvocation *, JSContext *context, JSValueRef*) override | |
270 | { | |
271 | return JSValueMakeUndefined([context JSGlobalContextRef]); | |
272 | } | |
273 | }; | |
274 | ||
275 | class CallbackResultId : public CallbackResult { | |
276 | virtual JSValueRef get(NSInvocation *invocation, JSContext *context, JSValueRef*) override | |
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 { | |
286 | virtual JSValueRef get(NSInvocation *invocation, JSContext *context, JSValueRef*) override | |
287 | { | |
288 | T value; | |
289 | [invocation getReturnValue:&value]; | |
290 | return JSValueMakeNumber([context JSGlobalContextRef], value); | |
291 | } | |
292 | }; | |
293 | ||
294 | class CallbackResultBoolean : public CallbackResult { | |
295 | virtual JSValueRef get(NSInvocation *invocation, JSContext *context, JSValueRef*) override | |
296 | { | |
297 | bool value; | |
298 | [invocation getReturnValue:&value]; | |
299 | return JSValueMakeBoolean([context JSGlobalContextRef], value); | |
300 | } | |
301 | }; | |
302 | ||
303 | class CallbackResultStruct : public CallbackResult { | |
304 | public: | |
305 | CallbackResultStruct(NSInvocation *conversionInvocation, const char* encodedType) | |
306 | : m_conversionInvocation(conversionInvocation) | |
307 | , m_buffer(encodedType) | |
308 | { | |
309 | } | |
310 | ||
311 | private: | |
312 | virtual JSValueRef get(NSInvocation *invocation, JSContext *context, JSValueRef*) override | |
313 | { | |
314 | [invocation getReturnValue:m_buffer]; | |
315 | ||
316 | [m_conversionInvocation setArgument:m_buffer atIndex:2]; | |
317 | [m_conversionInvocation setArgument:&context atIndex:3]; | |
318 | [m_conversionInvocation invokeWithTarget:[JSValue class]]; | |
319 | ||
320 | JSValue *value; | |
321 | [m_conversionInvocation getReturnValue:&value]; | |
322 | return valueInternalValue(value); | |
323 | } | |
324 | ||
325 | RetainPtr<NSInvocation> m_conversionInvocation; | |
326 | StructBuffer m_buffer; | |
327 | }; | |
328 | ||
329 | class ResultTypeDelegate { | |
330 | public: | |
ed1e77d3 | 331 | typedef std::unique_ptr<CallbackResult> ResultType; |
93a37866 A |
332 | |
333 | template<typename T> | |
334 | static ResultType typeInteger() | |
335 | { | |
ed1e77d3 | 336 | return std::make_unique<CallbackResultNumeric<T>>(); |
93a37866 A |
337 | } |
338 | ||
339 | template<typename T> | |
340 | static ResultType typeDouble() | |
341 | { | |
ed1e77d3 | 342 | return std::make_unique<CallbackResultNumeric<T>>(); |
93a37866 A |
343 | } |
344 | ||
345 | static ResultType typeBool() | |
346 | { | |
ed1e77d3 | 347 | return std::make_unique<CallbackResultBoolean>(); |
93a37866 A |
348 | } |
349 | ||
350 | static ResultType typeVoid() | |
351 | { | |
ed1e77d3 | 352 | return std::make_unique<CallbackResultVoid>(); |
93a37866 A |
353 | } |
354 | ||
355 | static ResultType typeId() | |
356 | { | |
ed1e77d3 | 357 | return std::make_unique<CallbackResultId>(); |
93a37866 A |
358 | } |
359 | ||
360 | static ResultType typeOfClass(const char*, const char*) | |
361 | { | |
ed1e77d3 | 362 | return std::make_unique<CallbackResultId>(); |
93a37866 A |
363 | } |
364 | ||
365 | static ResultType typeBlock(const char*, const char*) | |
366 | { | |
ed1e77d3 | 367 | return std::make_unique<CallbackResultId>(); |
93a37866 A |
368 | } |
369 | ||
370 | static ResultType typeStruct(const char* begin, const char* end) | |
371 | { | |
372 | StringRange copy(begin, end); | |
373 | if (NSInvocation *invocation = typeToValueInvocationFor(copy)) | |
ed1e77d3 A |
374 | return std::make_unique<CallbackResultStruct>(invocation, copy); |
375 | return nullptr; | |
93a37866 A |
376 | } |
377 | }; | |
378 | ||
379 | enum CallbackType { | |
12899fa2 | 380 | CallbackInitMethod, |
93a37866 A |
381 | CallbackInstanceMethod, |
382 | CallbackClassMethod, | |
383 | CallbackBlock | |
384 | }; | |
385 | ||
386 | namespace JSC { | |
387 | ||
388 | class ObjCCallbackFunctionImpl { | |
389 | public: | |
ed1e77d3 | 390 | ObjCCallbackFunctionImpl(NSInvocation *invocation, CallbackType type, Class instanceClass, std::unique_ptr<CallbackArgument> arguments, std::unique_ptr<CallbackResult> result) |
81345200 | 391 | : m_type(type) |
ed1e77d3 | 392 | , m_instanceClass(instanceClass) |
93a37866 | 393 | , m_invocation(invocation) |
ed1e77d3 A |
394 | , m_arguments(WTF::move(arguments)) |
395 | , m_result(WTF::move(result)) | |
93a37866 | 396 | { |
12899fa2 | 397 | ASSERT((type != CallbackInstanceMethod && type != CallbackInitMethod) || instanceClass); |
93a37866 A |
398 | } |
399 | ||
81345200 | 400 | void destroy(Heap& heap) |
93a37866 | 401 | { |
81345200 | 402 | // We need to explicitly release the target since we didn't call |
12899fa2 A |
403 | // -retainArguments on m_invocation (and we don't want to do so). |
404 | if (m_type == CallbackBlock || m_type == CallbackClassMethod) | |
81345200 | 405 | heap.releaseSoon(adoptNS([m_invocation.get() target])); |
ed1e77d3 | 406 | m_instanceClass = nil; |
93a37866 A |
407 | } |
408 | ||
409 | JSValueRef call(JSContext *context, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception); | |
410 | ||
93a37866 A |
411 | id wrappedBlock() |
412 | { | |
413 | return m_type == CallbackBlock ? [m_invocation target] : nil; | |
414 | } | |
415 | ||
12899fa2 A |
416 | id wrappedConstructor() |
417 | { | |
418 | switch (m_type) { | |
419 | case CallbackBlock: | |
420 | return [m_invocation target]; | |
421 | case CallbackInitMethod: | |
ed1e77d3 | 422 | return m_instanceClass.get(); |
12899fa2 A |
423 | default: |
424 | return nil; | |
425 | } | |
426 | } | |
427 | ||
428 | bool isConstructible() | |
429 | { | |
430 | return !!wrappedBlock() || m_type == CallbackInitMethod; | |
431 | } | |
432 | ||
433 | String name(); | |
434 | ||
93a37866 | 435 | private: |
93a37866 | 436 | CallbackType m_type; |
ed1e77d3 | 437 | RetainPtr<Class> m_instanceClass; |
93a37866 | 438 | RetainPtr<NSInvocation> m_invocation; |
ed1e77d3 A |
439 | std::unique_ptr<CallbackArgument> m_arguments; |
440 | std::unique_ptr<CallbackResult> m_result; | |
93a37866 A |
441 | }; |
442 | ||
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: | |
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. | |
81345200 | 449 | JSC::JSLockHolder locker(toJS(callerContext)); |
93a37866 A |
450 | |
451 | ObjCCallbackFunction* callback = static_cast<ObjCCallbackFunction*>(toJS(function)); | |
452 | ObjCCallbackFunctionImpl* impl = callback->impl(); | |
81345200 | 453 | JSContext *context = [JSContext contextWithJSGlobalContextRef:toGlobalRef(callback->globalObject()->globalExec())]; |
93a37866 A |
454 | |
455 | CallbackData callbackData; | |
456 | JSValueRef result; | |
457 | @autoreleasepool { | |
81345200 | 458 | [context beginCallbackWithData:&callbackData calleeValue:function thisValue:thisObject argumentCount:argumentCount arguments:arguments]; |
93a37866 A |
459 | result = impl->call(context, thisObject, argumentCount, arguments, exception); |
460 | if (context.exception) | |
461 | *exception = valueInternalValue(context.exception); | |
462 | [context endCallbackWithData:&callbackData]; | |
463 | } | |
464 | return result; | |
465 | } | |
466 | ||
12899fa2 A |
467 | static JSObjectRef objCCallbackFunctionCallAsConstructor(JSContextRef callerContext, JSObjectRef constructor, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) |
468 | { | |
81345200 | 469 | JSC::JSLockHolder locker(toJS(callerContext)); |
12899fa2 A |
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 { | |
ed1e77d3 A |
478 | [context beginCallbackWithData:&callbackData calleeValue:constructor thisValue:nullptr argumentCount:argumentCount arguments:arguments]; |
479 | result = impl->call(context, nullptr, argumentCount, arguments, exception); | |
12899fa2 A |
480 | if (context.exception) |
481 | *exception = valueInternalValue(context.exception); | |
482 | [context endCallbackWithData:&callbackData]; | |
483 | } | |
484 | ||
485 | JSGlobalContextRef contextRef = [context JSGlobalContextRef]; | |
486 | if (*exception) | |
ed1e77d3 | 487 | return nullptr; |
12899fa2 A |
488 | |
489 | if (!JSValueIsObject(contextRef, result)) { | |
81345200 | 490 | *exception = toRef(JSC::createTypeError(toJS(contextRef), ASCIILiteral("Objective-C blocks called as constructors must return an object."))); |
ed1e77d3 | 491 | return nullptr; |
12899fa2 A |
492 | } |
493 | return (JSObjectRef)result; | |
494 | } | |
495 | ||
ed1e77d3 | 496 | const JSC::ClassInfo ObjCCallbackFunction::s_info = { "CallbackFunction", &Base::s_info, 0, CREATE_METHOD_TABLE(ObjCCallbackFunction) }; |
93a37866 | 497 | |
ed1e77d3 | 498 | ObjCCallbackFunction::ObjCCallbackFunction(JSC::VM& vm, JSC::JSGlobalObject* globalObject, JSObjectCallAsFunctionCallback functionCallback, JSObjectCallAsConstructorCallback constructCallback, std::unique_ptr<ObjCCallbackFunctionImpl> impl) |
81345200 | 499 | : Base(vm, globalObject->objcCallbackFunctionStructure()) |
12899fa2 A |
500 | , m_functionCallback(functionCallback) |
501 | , m_constructCallback(constructCallback) | |
ed1e77d3 | 502 | , m_impl(WTF::move(impl)) |
93a37866 A |
503 | { |
504 | } | |
505 | ||
ed1e77d3 | 506 | ObjCCallbackFunction* ObjCCallbackFunction::create(JSC::VM& vm, JSC::JSGlobalObject* globalObject, const String& name, std::unique_ptr<ObjCCallbackFunctionImpl> impl) |
93a37866 | 507 | { |
ed1e77d3 | 508 | ObjCCallbackFunction* function = new (NotNull, allocateCell<ObjCCallbackFunction>(vm.heap)) ObjCCallbackFunction(vm, globalObject, objCCallbackFunctionCallAsFunction, objCCallbackFunctionCallAsConstructor, WTF::move(impl)); |
12899fa2 | 509 | function->finishCreation(vm, name); |
93a37866 A |
510 | return function; |
511 | } | |
512 | ||
513 | void ObjCCallbackFunction::destroy(JSCell* cell) | |
514 | { | |
81345200 A |
515 | ObjCCallbackFunction& function = *jsCast<ObjCCallbackFunction*>(cell); |
516 | function.impl()->destroy(*Heap::heap(cell)); | |
517 | function.~ObjCCallbackFunction(); | |
93a37866 A |
518 | } |
519 | ||
12899fa2 A |
520 | |
521 | CallType ObjCCallbackFunction::getCallData(JSCell*, CallData& callData) | |
522 | { | |
523 | callData.native.function = APICallbackFunction::call<ObjCCallbackFunction>; | |
524 | return CallTypeHost; | |
525 | } | |
526 | ||
527 | ConstructType ObjCCallbackFunction::getConstructData(JSCell* cell, ConstructData& constructData) | |
528 | { | |
529 | ObjCCallbackFunction* callback = jsCast<ObjCCallbackFunction*>(cell); | |
530 | if (!callback->impl()->isConstructible()) | |
531 | return Base::getConstructData(cell, constructData); | |
532 | constructData.native.function = APICallbackFunction::construct<ObjCCallbackFunction>; | |
533 | return ConstructTypeHost; | |
534 | } | |
535 | ||
536 | String ObjCCallbackFunctionImpl::name() | |
537 | { | |
538 | if (m_type == CallbackInitMethod) | |
ed1e77d3 | 539 | return class_getName(m_instanceClass.get()); |
12899fa2 A |
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? | |
542 | return ""; | |
543 | } | |
544 | ||
93a37866 A |
545 | JSValueRef ObjCCallbackFunctionImpl::call(JSContext *context, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) |
546 | { | |
547 | JSGlobalContextRef contextRef = [context JSGlobalContextRef]; | |
548 | ||
12899fa2 | 549 | id target; |
93a37866 A |
550 | size_t firstArgument; |
551 | switch (m_type) { | |
12899fa2 A |
552 | case CallbackInitMethod: { |
553 | RELEASE_ASSERT(!thisObject); | |
554 | target = [m_instanceClass alloc]; | |
ed1e77d3 | 555 | if (!target || ![target isKindOfClass:m_instanceClass.get()]) { |
81345200 | 556 | *exception = toRef(JSC::createTypeError(toJS(contextRef), ASCIILiteral("self type check failed for Objective-C instance method"))); |
12899fa2 A |
557 | return JSValueMakeUndefined(contextRef); |
558 | } | |
559 | [m_invocation setTarget:target]; | |
560 | firstArgument = 2; | |
561 | break; | |
562 | } | |
93a37866 | 563 | case CallbackInstanceMethod: { |
12899fa2 | 564 | target = tryUnwrapObjcObject(contextRef, thisObject); |
ed1e77d3 | 565 | if (!target || ![target isKindOfClass:m_instanceClass.get()]) { |
81345200 | 566 | *exception = toRef(JSC::createTypeError(toJS(contextRef), ASCIILiteral("self type check failed for Objective-C instance method"))); |
93a37866 A |
567 | return JSValueMakeUndefined(contextRef); |
568 | } | |
569 | [m_invocation setTarget:target]; | |
12899fa2 A |
570 | firstArgument = 2; |
571 | break; | |
93a37866 | 572 | } |
93a37866 A |
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()) { | |
582 | JSValueRef value = argumentNumber < argumentCount ? arguments[argumentNumber] : JSValueMakeUndefined(contextRef); | |
583 | argument->set(m_invocation.get(), argumentNumber + firstArgument, context, value, exception); | |
584 | if (*exception) | |
585 | return JSValueMakeUndefined(contextRef); | |
586 | ++argumentNumber; | |
587 | } | |
588 | ||
589 | [m_invocation invoke]; | |
590 | ||
12899fa2 A |
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; | |
93a37866 A |
603 | } |
604 | ||
605 | } // namespace JSC | |
606 | ||
607 | static bool blockSignatureContainsClass() | |
608 | { | |
609 | static bool containsClass = ^{ | |
610 | id block = ^(NSString *string){ return string; }; | |
611 | return _Block_has_signature(block) && strstr(_Block_signature(block), "NSString"); | |
612 | }(); | |
613 | return containsClass; | |
614 | } | |
615 | ||
ed1e77d3 | 616 | static inline bool skipNumber(const char*& position) |
93a37866 A |
617 | { |
618 | if (!isASCIIDigit(*position)) | |
619 | return false; | |
620 | while (isASCIIDigit(*++position)) { } | |
621 | return true; | |
622 | } | |
623 | ||
624 | static JSObjectRef objCCallbackFunctionForInvocation(JSContext *context, NSInvocation *invocation, CallbackType type, Class instanceClass, const char* signatureWithObjcClasses) | |
625 | { | |
81345200 | 626 | if (!signatureWithObjcClasses) |
ed1e77d3 | 627 | return nullptr; |
81345200 | 628 | |
93a37866 A |
629 | const char* position = signatureWithObjcClasses; |
630 | ||
ed1e77d3 | 631 | auto result = parseObjCType<ResultTypeDelegate>(position); |
93a37866 | 632 | if (!result || !skipNumber(position)) |
ed1e77d3 | 633 | return nullptr; |
93a37866 A |
634 | |
635 | switch (type) { | |
12899fa2 | 636 | case CallbackInitMethod: |
93a37866 A |
637 | case CallbackInstanceMethod: |
638 | case CallbackClassMethod: | |
639 | // Methods are passed two implicit arguments - (id)self, and the selector. | |
640 | if ('@' != *position++ || !skipNumber(position) || ':' != *position++ || !skipNumber(position)) | |
ed1e77d3 | 641 | return nullptr; |
93a37866 A |
642 | break; |
643 | case CallbackBlock: | |
644 | // Blocks are passed one implicit argument - the block, of type "@?". | |
645 | if (('@' != *position++) || ('?' != *position++) || !skipNumber(position)) | |
ed1e77d3 | 646 | return nullptr; |
93a37866 A |
647 | // Only allow arguments of type 'id' if the block signature contains the NS type information. |
648 | if ((!blockSignatureContainsClass() && strchr(position, '@'))) | |
ed1e77d3 | 649 | return nullptr; |
93a37866 A |
650 | break; |
651 | } | |
652 | ||
ed1e77d3 A |
653 | std::unique_ptr<CallbackArgument> arguments; |
654 | auto* nextArgument = &arguments; | |
93a37866 A |
655 | unsigned argumentCount = 0; |
656 | while (*position) { | |
ed1e77d3 | 657 | auto argument = parseObjCType<ArgumentTypeDelegate>(position); |
93a37866 | 658 | if (!argument || !skipNumber(position)) |
ed1e77d3 | 659 | return nullptr; |
93a37866 | 660 | |
ed1e77d3 | 661 | *nextArgument = WTF::move(argument); |
93a37866 A |
662 | nextArgument = &(*nextArgument)->m_next; |
663 | ++argumentCount; | |
664 | } | |
665 | ||
666 | JSC::ExecState* exec = toJS([context JSGlobalContextRef]); | |
81345200 | 667 | JSC::JSLockHolder locker(exec); |
ed1e77d3 A |
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))); | |
12899fa2 A |
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)); | |
93a37866 A |
678 | } |
679 | ||
680 | JSObjectRef objCCallbackFunctionForMethod(JSContext *context, Class cls, Protocol *protocol, BOOL isInstanceMethod, SEL sel, const char* types) | |
681 | { | |
682 | NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[NSMethodSignature signatureWithObjCTypes:types]]; | |
683 | [invocation setSelector:sel]; | |
ed1e77d3 A |
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? | |
93a37866 | 686 | if (!isInstanceMethod) |
12899fa2 | 687 | [invocation setTarget:[cls retain]]; |
93a37866 A |
688 | return objCCallbackFunctionForInvocation(context, invocation, isInstanceMethod ? CallbackInstanceMethod : CallbackClassMethod, isInstanceMethod ? cls : nil, _protocol_getMethodTypeEncoding(protocol, sel, YES, isInstanceMethod)); |
689 | } | |
690 | ||
691 | JSObjectRef objCCallbackFunctionForBlock(JSContext *context, id target) | |
692 | { | |
693 | if (!_Block_has_signature(target)) | |
ed1e77d3 | 694 | return nullptr; |
93a37866 A |
695 | const char* signature = _Block_signature(target); |
696 | NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[NSMethodSignature signatureWithObjCTypes:signature]]; | |
12899fa2 A |
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. | |
93a37866 | 702 | [invocation setTarget:[target copy]]; |
12899fa2 | 703 | |
93a37866 A |
704 | return objCCallbackFunctionForInvocation(context, invocation, CallbackBlock, nil, signature); |
705 | } | |
706 | ||
12899fa2 | 707 | id tryUnwrapConstructor(JSObjectRef object) |
93a37866 | 708 | { |
81345200 | 709 | if (!toJS(object)->inherits(JSC::ObjCCallbackFunction::info())) |
93a37866 | 710 | return nil; |
12899fa2 A |
711 | JSC::ObjCCallbackFunctionImpl* impl = static_cast<JSC::ObjCCallbackFunction*>(toJS(object))->impl(); |
712 | if (!impl->isConstructible()) | |
713 | return nil; | |
714 | return impl->wrappedConstructor(); | |
93a37866 A |
715 | } |
716 | ||
717 | #endif |