gwt.team.knorton | 3bee6a4 | 2006-12-11 01:16:34 +0000 | [diff] [blame] | 1 | /* |
gwt.team.scottb | 5aa171d | 2007-04-11 21:11:15 +0000 | [diff] [blame] | 2 | * Copyright 2007 Google Inc. |
gwt.team.knorton | 3bee6a4 | 2006-12-11 01:16:34 +0000 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | * use this file except in compliance with the License. You may obtain a copy of |
| 6 | * the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | * License for the specific language governing permissions and limitations under |
| 14 | * the License. |
| 15 | */ |
gwt.team.scottb | ab0aa68 | 2006-12-06 23:14:19 +0000 | [diff] [blame] | 16 | #include "FunctionObject.h" |
| 17 | #include <kjs/array_object.h> |
| 18 | |
| 19 | using namespace KJS; |
| 20 | |
| 21 | const ClassInfo FunctionObject::info = {"Function", 0, 0, 0}; |
| 22 | |
| 23 | class CallFunction : public FunctionObject { |
| 24 | public: |
gwt.team.scottb | 5aa171d | 2007-04-11 21:11:15 +0000 | [diff] [blame] | 25 | CallFunction(): FunctionObject("call") { |
| 26 | } |
gwt.team.scottb | ab0aa68 | 2006-12-06 23:14:19 +0000 | [diff] [blame] | 27 | |
gwt.team.scottb | 5aa171d | 2007-04-11 21:11:15 +0000 | [diff] [blame] | 28 | virtual JSValue *callAsFunction(ExecState *exec, JSObject *thisObj, |
| 29 | const List &args) |
| 30 | { |
| 31 | // Copied from FunctionProtoFunc::callAsFunction() |
| 32 | JSValue *thisArg = args[0]; |
| 33 | JSObject *func = thisObj; |
gwt.team.scottb | ab0aa68 | 2006-12-06 23:14:19 +0000 | [diff] [blame] | 34 | |
gwt.team.scottb | 5aa171d | 2007-04-11 21:11:15 +0000 | [diff] [blame] | 35 | if (!func->implementsCall()) { |
| 36 | return throwError(exec, TypeError); |
| 37 | } |
gwt.team.scottb | ab0aa68 | 2006-12-06 23:14:19 +0000 | [diff] [blame] | 38 | |
gwt.team.scottb | 5aa171d | 2007-04-11 21:11:15 +0000 | [diff] [blame] | 39 | JSObject *callThis; |
| 40 | if (thisArg->isUndefinedOrNull()) { |
| 41 | callThis = exec->dynamicInterpreter()->globalObject(); |
| 42 | } else { |
| 43 | callThis = thisArg->toObject(exec); |
| 44 | } |
gwt.team.scottb | ab0aa68 | 2006-12-06 23:14:19 +0000 | [diff] [blame] | 45 | |
gwt.team.scottb | 5aa171d | 2007-04-11 21:11:15 +0000 | [diff] [blame] | 46 | return func->call(exec, callThis, args.copyTail()); |
| 47 | } |
gwt.team.scottb | ab0aa68 | 2006-12-06 23:14:19 +0000 | [diff] [blame] | 48 | }; |
| 49 | |
| 50 | class ApplyFunction : public FunctionObject { |
| 51 | public: |
gwt.team.scottb | 5aa171d | 2007-04-11 21:11:15 +0000 | [diff] [blame] | 52 | ApplyFunction(): FunctionObject("apply") { |
| 53 | } |
gwt.team.scottb | ab0aa68 | 2006-12-06 23:14:19 +0000 | [diff] [blame] | 54 | |
gwt.team.scottb | 5aa171d | 2007-04-11 21:11:15 +0000 | [diff] [blame] | 55 | virtual JSValue *callAsFunction(ExecState *exec, JSObject *thisObj, |
| 56 | const List &args) |
| 57 | { |
| 58 | // Copied from FunctionProtoFunc::callAsFunction() |
| 59 | JSObject *func = thisObj; |
| 60 | if (!func->implementsCall()) { |
| 61 | return throwError(exec, TypeError); |
| 62 | } |
gwt.team.scottb | ab0aa68 | 2006-12-06 23:14:19 +0000 | [diff] [blame] | 63 | |
gwt.team.scottb | 5aa171d | 2007-04-11 21:11:15 +0000 | [diff] [blame] | 64 | JSValue *thisArg = args[0]; |
| 65 | JSObject *applyThis; |
| 66 | if (thisArg->isUndefinedOrNull()) { |
| 67 | applyThis = exec->dynamicInterpreter()->globalObject(); |
| 68 | } else { |
| 69 | applyThis = thisArg->toObject(exec); |
| 70 | } |
gwt.team.scottb | ab0aa68 | 2006-12-06 23:14:19 +0000 | [diff] [blame] | 71 | |
gwt.team.scottb | 5aa171d | 2007-04-11 21:11:15 +0000 | [diff] [blame] | 72 | JSValue *argArray = args[1]; |
| 73 | List applyArgs; |
| 74 | if (!argArray->isUndefinedOrNull()) { |
| 75 | if (!argArray->isObject(&ArrayInstance::info)) { |
| 76 | return throwError(exec, TypeError); |
| 77 | } |
gwt.team.scottb | ab0aa68 | 2006-12-06 23:14:19 +0000 | [diff] [blame] | 78 | |
gwt.team.scottb | 5aa171d | 2007-04-11 21:11:15 +0000 | [diff] [blame] | 79 | JSObject *argArrayObj = static_cast<JSObject *>(argArray); |
| 80 | unsigned int length = argArrayObj->get(exec, lengthPropertyName) |
| 81 | ->toUInt32(exec); |
| 82 | for (unsigned int i = 0; i < length; ++i) { |
| 83 | applyArgs.append(argArrayObj->get(exec,i)); |
| 84 | } |
| 85 | } |
| 86 | return func->call(exec, applyThis, applyArgs); |
| 87 | } |
gwt.team.scottb | ab0aa68 | 2006-12-06 23:14:19 +0000 | [diff] [blame] | 88 | }; |
| 89 | |
| 90 | |
| 91 | static UString makeFunctionString(const UString& name) { |
gwt.team.scottb | 5aa171d | 2007-04-11 21:11:15 +0000 | [diff] [blame] | 92 | return "\nfunction " + name + "() {\n [native code]\n}\n"; |
gwt.team.scottb | ab0aa68 | 2006-12-06 23:14:19 +0000 | [diff] [blame] | 93 | } |
| 94 | |
gwt.team.scottb | 5aa171d | 2007-04-11 21:11:15 +0000 | [diff] [blame] | 95 | JSValue *FunctionObject::getter(ExecState* exec, JSObject* obj, |
| 96 | const Identifier& propertyName, const PropertySlot& slot) |
| 97 | { |
| 98 | if (propertyName.ustring() == "toString") { |
| 99 | return new ToStringFunction(); |
| 100 | } else if (propertyName.ustring() == "call") { |
| 101 | return new CallFunction(); |
| 102 | } else if (propertyName.ustring() == "apply") { |
| 103 | return new ApplyFunction(); |
| 104 | } |
| 105 | return jsUndefined(); |
gwt.team.scottb | ab0aa68 | 2006-12-06 23:14:19 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | FunctionObject::FunctionObject(const UString& name): name(name) { |
| 109 | } |
| 110 | |
gwt.team.scottb | 5aa171d | 2007-04-11 21:11:15 +0000 | [diff] [blame] | 111 | bool FunctionObject::getOwnPropertySlot(ExecState *exec, |
| 112 | const Identifier& propertyName, PropertySlot& slot) |
| 113 | { |
| 114 | if (propertyName.ustring() == "toString") { |
| 115 | slot.setCustom(this, getter); |
| 116 | return true; |
| 117 | } |
| 118 | if (propertyName.ustring() == "call") { |
| 119 | slot.setCustom(this, getter); |
| 120 | return true; |
| 121 | } |
| 122 | if (propertyName.ustring() == "apply") { |
| 123 | slot.setCustom(this, getter); |
| 124 | return true; |
| 125 | } |
| 126 | return false; |
gwt.team.scottb | ab0aa68 | 2006-12-06 23:14:19 +0000 | [diff] [blame] | 127 | } |
| 128 | |
gwt.team.scottb | 5aa171d | 2007-04-11 21:11:15 +0000 | [diff] [blame] | 129 | bool FunctionObject::canPut(ExecState *exec, const Identifier &propertyName) |
| 130 | const |
| 131 | { |
| 132 | return false; |
gwt.team.scottb | ab0aa68 | 2006-12-06 23:14:19 +0000 | [diff] [blame] | 133 | } |
| 134 | |
gwt.team.scottb | 5aa171d | 2007-04-11 21:11:15 +0000 | [diff] [blame] | 135 | void FunctionObject::put(ExecState *exec, const Identifier &propertyName, |
| 136 | JSValue *value, int attr) |
| 137 | { |
gwt.team.scottb | ab0aa68 | 2006-12-06 23:14:19 +0000 | [diff] [blame] | 138 | } |
| 139 | |
gwt.team.scottb | 5aa171d | 2007-04-11 21:11:15 +0000 | [diff] [blame] | 140 | bool FunctionObject::deleteProperty(ExecState *exec, |
| 141 | const Identifier &propertyName) |
| 142 | { |
| 143 | return false; |
gwt.team.scottb | ab0aa68 | 2006-12-06 23:14:19 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | JSValue *FunctionObject::defaultValue(ExecState *exec, JSType hint) const { |
gwt.team.scottb | 5aa171d | 2007-04-11 21:11:15 +0000 | [diff] [blame] | 147 | return jsString(makeFunctionString(name)); |
gwt.team.scottb | ab0aa68 | 2006-12-06 23:14:19 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | bool FunctionObject::implementsCall() const { |
gwt.team.scottb | 5aa171d | 2007-04-11 21:11:15 +0000 | [diff] [blame] | 151 | return true; |
gwt.team.scottb | ab0aa68 | 2006-12-06 23:14:19 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | // ToStringFunction |
gwt.team.scottb | ab0aa68 | 2006-12-06 23:14:19 +0000 | [diff] [blame] | 155 | ToStringFunction::ToStringFunction(): FunctionObject("toString") { |
| 156 | } |
| 157 | |
gwt.team.scottb | 5aa171d | 2007-04-11 21:11:15 +0000 | [diff] [blame] | 158 | JSValue *ToStringFunction::callAsFunction(ExecState *exec, JSObject *thisObj, |
| 159 | const List &args) |
| 160 | { |
| 161 | if (!thisObj) { |
| 162 | return throwError(exec, TypeError); |
| 163 | } |
| 164 | return jsString(thisObj->toString(exec)); |
gwt.team.scottb | ab0aa68 | 2006-12-06 23:14:19 +0000 | [diff] [blame] | 165 | } |