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 36using namespace WebCore; 37 38namespace JSC { 39 40using namespace Bindings; 41 42const ClassInfo RuntimeMethod::s_info = { "RuntimeMethod", &InternalFunction::s_info, 0, 0, CREATE_METHOD_TABLE(RuntimeMethod) }; 43 44RuntimeMethod::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, structure) 47 , m_method(method) 48{ 49} 50 51void RuntimeMethod::finishCreation(VM& vm, const String& ident) 52{ 53 Base::finishCreation(vm, ident); 54 ASSERT(inherits(&s_info)); 55} 56 57JSValue RuntimeMethod::lengthGetter(ExecState*, JSValue slotBase, PropertyName) 58{ 59 RuntimeMethod* thisObj = static_cast<RuntimeMethod*>(asObject(slotBase)); 60 61 return jsNumber(thisObj->m_method->numParameters()); 62} 63 64bool RuntimeMethod::getOwnPropertySlot(JSCell* cell, ExecState* exec, PropertyName propertyName, PropertySlot &slot) 65{ 66 RuntimeMethod* thisObject = jsCast<RuntimeMethod*>(cell); 67 if (propertyName == exec->propertyNames().length) { 68 slot.setCacheableCustom(thisObject, thisObject->lengthGetter); 69 return true; 70 } 71 72 return InternalFunction::getOwnPropertySlot(thisObject, exec, propertyName, slot); 73} 74 75bool RuntimeMethod::getOwnPropertyDescriptor(JSObject* object, ExecState* exec, PropertyName propertyName, PropertyDescriptor &descriptor) 76{ 77 RuntimeMethod* thisObject = jsCast<RuntimeMethod*>(object); 78 if (propertyName == exec->propertyNames().length) { 79 PropertySlot slot; 80 slot.setCustom(thisObject, lengthGetter); 81 descriptor.setDescriptor(slot.getValue(exec, propertyName), ReadOnly | DontDelete | DontEnum); 82 return true; 83 } 84 85 return InternalFunction::getOwnPropertyDescriptor(thisObject, exec, propertyName, descriptor); 86} 87 88static EncodedJSValue JSC_HOST_CALL callRuntimeMethod(ExecState* exec) 89{ 90 RuntimeMethod* method = static_cast<RuntimeMethod*>(exec->callee()); 91 92 if (!method->method()) 93 return JSValue::encode(jsUndefined()); 94 95 RefPtr<Instance> instance; 96 97 JSValue thisValue = exec->hostThisValue(); 98 if (thisValue.inherits(&RuntimeObject::s_info)) { 99 RuntimeObject* runtimeObject = static_cast<RuntimeObject*>(asObject(thisValue)); 100 instance = runtimeObject->getInternalInstance(); 101 if (!instance) 102 return JSValue::encode(RuntimeObject::throwInvalidAccessError(exec)); 103 } else { 104 // Calling a runtime object of a plugin element? 105 if (thisValue.inherits(&JSHTMLElement::s_info)) { 106 HTMLElement* element = jsCast<JSHTMLElement*>(asObject(thisValue))->impl(); 107 instance = pluginInstance(element); 108 } 109 if (!instance) 110 return throwVMTypeError(exec); 111 } 112 ASSERT(instance); 113 114 instance->begin(); 115 JSValue result = instance->invokeMethod(exec, method); 116 instance->end(); 117 return JSValue::encode(result); 118} 119 120CallType RuntimeMethod::getCallData(JSCell*, CallData& callData) 121{ 122 callData.native.function = callRuntimeMethod; 123 return CallTypeHost; 124} 125 126} 127