JSCallbackConstructor.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Copyright (C) 2006, 2007, 2008 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 COMPUTER, 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 COMPUTER, 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. #include "config.h"
  26. #include "JSCallbackConstructor.h"
  27. #include "APIShims.h"
  28. #include "APICast.h"
  29. #include "Error.h"
  30. #include "JSGlobalObject.h"
  31. #include "JSLock.h"
  32. #include "ObjectPrototype.h"
  33. #include "Operations.h"
  34. #include <wtf/Vector.h>
  35. namespace JSC {
  36. const ClassInfo JSCallbackConstructor::s_info = { "CallbackConstructor", &Base::s_info, 0, 0, CREATE_METHOD_TABLE(JSCallbackConstructor) };
  37. JSCallbackConstructor::JSCallbackConstructor(JSGlobalObject* globalObject, Structure* structure, JSClassRef jsClass, JSObjectCallAsConstructorCallback callback)
  38. : JSDestructibleObject(globalObject->vm(), structure)
  39. , m_class(jsClass)
  40. , m_callback(callback)
  41. {
  42. }
  43. void JSCallbackConstructor::finishCreation(JSGlobalObject* globalObject, JSClassRef jsClass)
  44. {
  45. Base::finishCreation(globalObject->vm());
  46. ASSERT(inherits(&s_info));
  47. if (m_class)
  48. JSClassRetain(jsClass);
  49. }
  50. JSCallbackConstructor::~JSCallbackConstructor()
  51. {
  52. if (m_class)
  53. JSClassRelease(m_class);
  54. }
  55. void JSCallbackConstructor::destroy(JSCell* cell)
  56. {
  57. static_cast<JSCallbackConstructor*>(cell)->JSCallbackConstructor::~JSCallbackConstructor();
  58. }
  59. static EncodedJSValue JSC_HOST_CALL constructJSCallback(ExecState* exec)
  60. {
  61. JSObject* constructor = exec->callee();
  62. JSContextRef ctx = toRef(exec);
  63. JSObjectRef constructorRef = toRef(constructor);
  64. JSObjectCallAsConstructorCallback callback = jsCast<JSCallbackConstructor*>(constructor)->callback();
  65. if (callback) {
  66. size_t argumentCount = exec->argumentCount();
  67. Vector<JSValueRef, 16> arguments;
  68. arguments.reserveInitialCapacity(argumentCount);
  69. for (size_t i = 0; i < argumentCount; ++i)
  70. arguments.uncheckedAppend(toRef(exec, exec->argument(i)));
  71. JSValueRef exception = 0;
  72. JSObjectRef result;
  73. {
  74. APICallbackShim callbackShim(exec);
  75. result = callback(ctx, constructorRef, argumentCount, arguments.data(), &exception);
  76. }
  77. if (exception)
  78. throwError(exec, toJS(exec, exception));
  79. // result must be a valid JSValue.
  80. if (!result)
  81. return throwVMTypeError(exec);
  82. return JSValue::encode(toJS(result));
  83. }
  84. return JSValue::encode(toJS(JSObjectMake(ctx, jsCast<JSCallbackConstructor*>(constructor)->classRef(), 0)));
  85. }
  86. ConstructType JSCallbackConstructor::getConstructData(JSCell*, ConstructData& constructData)
  87. {
  88. constructData.native.function = constructJSCallback;
  89. return ConstructTypeHost;
  90. }
  91. } // namespace JSC