2 * Copyright (C) 2003, 2008 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #include "runtime_method.h"
29 #include "JSDOMBinding.h"
30 #include "JSHTMLElement.h"
31 #include "JSPluginElementFunctions.h"
32 #include "runtime_object.h"
33 #include <runtime/Error.h>
34 #include <runtime/FunctionPrototype.h>
36 using namespace WebCore;
40 using namespace Bindings;
42 ASSERT_CLASS_FITS_IN_CELL(RuntimeMethod);
44 const ClassInfo RuntimeMethod::s_info = { "RuntimeMethod", &InternalFunction::s_info, 0, 0 };
46 RuntimeMethod::RuntimeMethod(JSGlobalObject* globalObject, Structure* structure, Bindings::MethodList& m)
47 // Callers will need to pass in the right global object corresponding to this native object "m".
48 : InternalFunction(globalObject, structure)
49 , _methodList(adoptPtr(new MethodList(m)))
53 void RuntimeMethod::finishCreation(JSGlobalData& globalData, const Identifier& ident)
55 Base::finishCreation(globalData, ident);
56 ASSERT(inherits(&s_info));
59 JSValue RuntimeMethod::lengthGetter(ExecState*, JSValue slotBase, const Identifier&)
61 RuntimeMethod* thisObj = static_cast<RuntimeMethod*>(asObject(slotBase));
63 // Ick! There may be more than one method with this name. Arbitrarily
64 // just pick the first method. The fundamental problem here is that
65 // JavaScript doesn't have the notion of method overloading and
67 // FIXME: a better solution might be to give the maximum number of parameters
69 return jsNumber(thisObj->_methodList->at(0)->numParameters());
72 bool RuntimeMethod::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot &slot)
74 if (propertyName == exec->propertyNames().length) {
75 slot.setCacheableCustom(this, lengthGetter);
79 return InternalFunction::getOwnPropertySlot(exec, propertyName, slot);
82 bool RuntimeMethod::getOwnPropertyDescriptor(ExecState* exec, const Identifier& propertyName, PropertyDescriptor &descriptor)
84 if (propertyName == exec->propertyNames().length) {
86 slot.setCustom(this, lengthGetter);
87 descriptor.setDescriptor(slot.getValue(exec, propertyName), ReadOnly | DontDelete | DontEnum);
91 return InternalFunction::getOwnPropertyDescriptor(exec, propertyName, descriptor);
94 static EncodedJSValue JSC_HOST_CALL callRuntimeMethod(ExecState* exec)
96 RuntimeMethod* method = static_cast<RuntimeMethod*>(exec->callee());
98 if (method->methods()->isEmpty())
99 return JSValue::encode(jsUndefined());
101 RefPtr<Instance> instance;
103 JSValue thisValue = exec->hostThisValue();
104 if (thisValue.inherits(&RuntimeObject::s_info)) {
105 RuntimeObject* runtimeObject = static_cast<RuntimeObject*>(asObject(thisValue));
106 instance = runtimeObject->getInternalInstance();
108 return JSValue::encode(RuntimeObject::throwInvalidAccessError(exec));
110 // Calling a runtime object of a plugin element?
111 if (thisValue.inherits(&JSHTMLElement::s_info)) {
112 HTMLElement* element = static_cast<JSHTMLElement*>(asObject(thisValue))->impl();
113 instance = pluginInstance(element);
116 return throwVMTypeError(exec);
121 JSValue result = instance->invokeMethod(exec, method);
123 return JSValue::encode(result);
126 CallType RuntimeMethod::getCallData(CallData& callData)
128 callData.native.function = callRuntimeMethod;