APICallbackFunction.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Copyright (C) 2013, 2016 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. #ifndef APICallbackFunction_h
  26. #define APICallbackFunction_h
  27. #include "APICast.h"
  28. #include "Error.h"
  29. #include "JSCallbackConstructor.h"
  30. #include "JSLock.h"
  31. #include <wtf/Vector.h>
  32. namespace JSC {
  33. struct APICallbackFunction {
  34. template <typename T> static EncodedJSValue JSC_HOST_CALL call(ExecState*);
  35. template <typename T> static EncodedJSValue JSC_HOST_CALL construct(ExecState*);
  36. };
  37. template <typename T>
  38. EncodedJSValue JSC_HOST_CALL APICallbackFunction::call(ExecState* exec)
  39. {
  40. VM& vm = exec->vm();
  41. auto scope = DECLARE_THROW_SCOPE(vm);
  42. JSContextRef execRef = toRef(exec);
  43. JSObjectRef functionRef = toRef(exec->jsCallee());
  44. JSObjectRef thisObjRef = toRef(jsCast<JSObject*>(exec->thisValue().toThis(exec, NotStrictMode)));
  45. int argumentCount = static_cast<int>(exec->argumentCount());
  46. Vector<JSValueRef, 16> arguments;
  47. arguments.reserveInitialCapacity(argumentCount);
  48. for (int i = 0; i < argumentCount; i++)
  49. arguments.uncheckedAppend(toRef(exec, exec->uncheckedArgument(i)));
  50. JSValueRef exception = 0;
  51. JSValueRef result;
  52. {
  53. JSLock::DropAllLocks dropAllLocks(exec);
  54. result = jsCast<T*>(toJS(functionRef))->functionCallback()(execRef, functionRef, thisObjRef, argumentCount, arguments.data(), &exception);
  55. }
  56. if (exception)
  57. throwException(exec, scope, toJS(exec, exception));
  58. // result must be a valid JSValue.
  59. if (!result)
  60. return JSValue::encode(jsUndefined());
  61. return JSValue::encode(toJS(exec, result));
  62. }
  63. template <typename T>
  64. EncodedJSValue JSC_HOST_CALL APICallbackFunction::construct(ExecState* exec)
  65. {
  66. VM& vm = exec->vm();
  67. auto scope = DECLARE_THROW_SCOPE(vm);
  68. JSObject* constructor = exec->jsCallee();
  69. JSContextRef ctx = toRef(exec);
  70. JSObjectRef constructorRef = toRef(constructor);
  71. JSObjectCallAsConstructorCallback callback = jsCast<T*>(constructor)->constructCallback();
  72. if (callback) {
  73. size_t argumentCount = exec->argumentCount();
  74. Vector<JSValueRef, 16> arguments;
  75. arguments.reserveInitialCapacity(argumentCount);
  76. for (size_t i = 0; i < argumentCount; ++i)
  77. arguments.uncheckedAppend(toRef(exec, exec->uncheckedArgument(i)));
  78. JSValueRef exception = 0;
  79. JSObjectRef result;
  80. {
  81. JSLock::DropAllLocks dropAllLocks(exec);
  82. result = callback(ctx, constructorRef, argumentCount, arguments.data(), &exception);
  83. }
  84. if (exception) {
  85. throwException(exec, scope, toJS(exec, exception));
  86. return JSValue::encode(toJS(exec, exception));
  87. }
  88. // result must be a valid JSValue.
  89. if (!result)
  90. return throwVMTypeError(exec, scope);
  91. return JSValue::encode(toJS(result));
  92. }
  93. return JSValue::encode(toJS(JSObjectMake(ctx, jsCast<JSCallbackConstructor*>(constructor)->classRef(), 0)));
  94. }
  95. } // namespace JSC
  96. #endif // APICallbackFunction_h