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