runtime_method.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * Copyright (C) 2003, 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 "runtime_method.h"
  27. #include "JSDOMBinding.h"
  28. #include "JSHTMLElement.h"
  29. #include "JSPluginElementFunctions.h"
  30. #include "runtime_object.h"
  31. #include <runtime/Error.h>
  32. #include <runtime/FunctionPrototype.h>
  33. using namespace WebCore;
  34. namespace JSC {
  35. using namespace Bindings;
  36. const ClassInfo RuntimeMethod::s_info = { "RuntimeMethod", &InternalFunction::s_info, 0, 0, CREATE_METHOD_TABLE(RuntimeMethod) };
  37. RuntimeMethod::RuntimeMethod(JSGlobalObject* globalObject, Structure* structure, Method* method)
  38. // Callers will need to pass in the right global object corresponding to this native object "method".
  39. : InternalFunction(globalObject, structure)
  40. , m_method(method)
  41. {
  42. }
  43. void RuntimeMethod::finishCreation(VM& vm, const String& ident)
  44. {
  45. Base::finishCreation(vm, ident);
  46. ASSERT(inherits(&s_info));
  47. }
  48. JSValue RuntimeMethod::lengthGetter(ExecState*, JSValue slotBase, PropertyName)
  49. {
  50. RuntimeMethod* thisObj = static_cast<RuntimeMethod*>(asObject(slotBase));
  51. return jsNumber(thisObj->m_method->numParameters());
  52. }
  53. bool RuntimeMethod::getOwnPropertySlot(JSCell* cell, ExecState* exec, PropertyName propertyName, PropertySlot &slot)
  54. {
  55. RuntimeMethod* thisObject = jsCast<RuntimeMethod*>(cell);
  56. if (propertyName == exec->propertyNames().length) {
  57. slot.setCacheableCustom(thisObject, thisObject->lengthGetter);
  58. return true;
  59. }
  60. return InternalFunction::getOwnPropertySlot(thisObject, exec, propertyName, slot);
  61. }
  62. bool RuntimeMethod::getOwnPropertyDescriptor(JSObject* object, ExecState* exec, PropertyName propertyName, PropertyDescriptor &descriptor)
  63. {
  64. RuntimeMethod* thisObject = jsCast<RuntimeMethod*>(object);
  65. if (propertyName == exec->propertyNames().length) {
  66. PropertySlot slot;
  67. slot.setCustom(thisObject, lengthGetter);
  68. descriptor.setDescriptor(slot.getValue(exec, propertyName), ReadOnly | DontDelete | DontEnum);
  69. return true;
  70. }
  71. return InternalFunction::getOwnPropertyDescriptor(thisObject, exec, propertyName, descriptor);
  72. }
  73. static EncodedJSValue JSC_HOST_CALL callRuntimeMethod(ExecState* exec)
  74. {
  75. RuntimeMethod* method = static_cast<RuntimeMethod*>(exec->callee());
  76. if (!method->method())
  77. return JSValue::encode(jsUndefined());
  78. RefPtr<Instance> instance;
  79. JSValue thisValue = exec->hostThisValue();
  80. if (thisValue.inherits(&RuntimeObject::s_info)) {
  81. RuntimeObject* runtimeObject = static_cast<RuntimeObject*>(asObject(thisValue));
  82. instance = runtimeObject->getInternalInstance();
  83. if (!instance)
  84. return JSValue::encode(RuntimeObject::throwInvalidAccessError(exec));
  85. } else {
  86. // Calling a runtime object of a plugin element?
  87. if (thisValue.inherits(&JSHTMLElement::s_info)) {
  88. HTMLElement* element = jsCast<JSHTMLElement*>(asObject(thisValue))->impl();
  89. instance = pluginInstance(element);
  90. }
  91. if (!instance)
  92. return throwVMTypeError(exec);
  93. }
  94. ASSERT(instance);
  95. instance->begin();
  96. JSValue result = instance->invokeMethod(exec, method);
  97. instance->end();
  98. return JSValue::encode(result);
  99. }
  100. CallType RuntimeMethod::getCallData(JSCell*, CallData& callData)
  101. {
  102. callData.native.function = callRuntimeMethod;
  103. return CallTypeHost;
  104. }
  105. }