• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • kjs
 

kjs

  • kjs
function_object.cpp
1/*
2 * This file is part of the KDE libraries
3 * Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
4 * Copyright (C) 2003 Apple Computer, Inc.
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 *
20 */
21
22#include "function_object.h"
23#include "internal.h"
24#include "function.h"
25#include "array_object.h"
26#include "nodes.h"
27#include "lexer.h"
28#include "debugger.h"
29#include "object.h"
30
31#include <assert.h>
32#include <stdio.h>
33#include <string.h>
34
35using namespace KJS;
36
37// ------------------------------ FunctionPrototypeImp -------------------------
38
39FunctionPrototypeImp::FunctionPrototypeImp(ExecState *exec)
40 : InternalFunctionImp((FunctionPrototypeImp*)0)
41{
42 Value protect(this);
43 putDirect(toStringPropertyName,
44 new FunctionProtoFuncImp(exec, this, FunctionProtoFuncImp::ToString, 0, toStringPropertyName),
45 DontEnum);
46 static const Identifier applyPropertyName("apply");
47 putDirect(applyPropertyName,
48 new FunctionProtoFuncImp(exec, this, FunctionProtoFuncImp::Apply, 2, applyPropertyName),
49 DontEnum);
50 static const Identifier callPropertyName("call");
51 putDirect(callPropertyName,
52 new FunctionProtoFuncImp(exec, this, FunctionProtoFuncImp::Call, 1, callPropertyName),
53 DontEnum);
54 putDirect(lengthPropertyName, 0, DontDelete|ReadOnly|DontEnum);
55}
56
57FunctionPrototypeImp::~FunctionPrototypeImp()
58{
59}
60
61bool FunctionPrototypeImp::implementsCall() const
62{
63 return true;
64}
65
66// ECMA 15.3.4
67Value FunctionPrototypeImp::call(ExecState* /*exec*/, Object &/*thisObj*/, const List &/*args*/)
68{
69 return Undefined();
70}
71
72// ------------------------------ FunctionProtoFuncImp -------------------------
73
74FunctionProtoFuncImp::FunctionProtoFuncImp(ExecState* /*exec*/, FunctionPrototypeImp *funcProto,
75 int i, int len, const Identifier &_ident)
76 : InternalFunctionImp(funcProto), id(i)
77{
78 Value protect(this);
79 putDirect(lengthPropertyName, len, DontDelete|ReadOnly|DontEnum);
80 ident = _ident;
81}
82
83
84bool FunctionProtoFuncImp::implementsCall() const
85{
86 return true;
87}
88
89Value FunctionProtoFuncImp::call(ExecState *exec, Object &thisObj, const List &args)
90{
91 Value result;
92
93 switch (id) {
94 case ToString: {
95 // ### also make this work for internal functions
96 if (!thisObj.isValid() || !thisObj.inherits(&InternalFunctionImp::info)) {
97#ifndef NDEBUG
98 fprintf(stderr,"attempted toString() call on null or non-function object\n");
99#endif
100 Object err = Error::create(exec,TypeError);
101 exec->setException(err);
102 return err;
103 }
104
105 if (thisObj.inherits(&DeclaredFunctionImp::info)) {
106 DeclaredFunctionImp *fi = static_cast<DeclaredFunctionImp*>
107 (thisObj.imp());
108 return String("function " + fi->name().ustring() + "(" +
109 fi->parameterString() + ") " + fi->body->toCode());
110 } else if (thisObj.inherits(&InternalFunctionImp::info) &&
111 !static_cast<InternalFunctionImp*>(thisObj.imp())->name().isNull()) {
112 result = String("\nfunction " + static_cast<InternalFunctionImp*>(thisObj.imp())->name().ustring() + "() {\n"
113 " [native code]\n}\n");
114 }
115 else {
116 result = String("[function]");
117 }
118 }
119 break;
120 case Apply: {
121 Value thisArg = args[0];
122 Value argArray = args[1];
123 Object func = thisObj;
124
125 if (!func.implementsCall()) {
126 Object err = Error::create(exec,TypeError);
127 exec->setException(err);
128 return err;
129 }
130
131 Object applyThis;
132 if (thisArg.isA(NullType) || thisArg.isA(UndefinedType))
133 applyThis = exec->dynamicInterpreter()->globalObject();
134 else
135 applyThis = thisArg.toObject(exec);
136
137 List applyArgs;
138 if (!argArray.isA(NullType) && !argArray.isA(UndefinedType)) {
139 if (argArray.isA(ObjectType) &&
140 (Object::dynamicCast(argArray).inherits(&ArrayInstanceImp::info) ||
141 Object::dynamicCast(argArray).inherits(&ArgumentsImp::info))) {
142
143 Object argArrayObj = Object::dynamicCast(argArray);
144 unsigned int length = argArrayObj.get(exec,lengthPropertyName).toUInt32(exec);
145 for (unsigned int i = 0; i < length; i++)
146 applyArgs.append(argArrayObj.get(exec,i));
147 }
148 else {
149 Object err = Error::create(exec,TypeError);
150 exec->setException(err);
151 return err;
152 }
153 }
154 result = func.call(exec,applyThis,applyArgs);
155 }
156 break;
157 case Call: {
158 Value thisArg = args[0];
159 Object func = thisObj;
160
161 if (!func.implementsCall()) {
162 Object err = Error::create(exec,TypeError);
163 exec->setException(err);
164 return err;
165 }
166
167 Object callThis;
168 if (thisArg.isA(NullType) || thisArg.isA(UndefinedType))
169 callThis = exec->dynamicInterpreter()->globalObject();
170 else
171 callThis = thisArg.toObject(exec);
172
173 result = func.call(exec,callThis,args.copyTail());
174 }
175 break;
176 }
177
178 return result;
179}
180
181// ------------------------------ FunctionObjectImp ----------------------------
182
183FunctionObjectImp::FunctionObjectImp(ExecState* /*exec*/, FunctionPrototypeImp *funcProto)
184 : InternalFunctionImp(funcProto)
185{
186 Value protect(this);
187 putDirect(prototypePropertyName, funcProto, DontEnum|DontDelete|ReadOnly);
188
189 // no. of arguments for constructor
190 putDirect(lengthPropertyName, NumberImp::one(), ReadOnly|DontDelete|DontEnum);
191}
192
193FunctionObjectImp::~FunctionObjectImp()
194{
195}
196
197bool FunctionObjectImp::implementsConstruct() const
198{
199 return true;
200}
201
202// ECMA 15.3.2 The Function Constructor
203Object FunctionObjectImp::construct(ExecState *exec, const List &args)
204{
205 UString p("");
206 UString body;
207 int argsSize = args.size();
208 if (argsSize == 0) {
209 body = "";
210 } else if (argsSize == 1) {
211 body = args[0].toString(exec);
212 } else {
213 p = args[0].toString(exec);
214 for (int k = 1; k < argsSize - 1; k++)
215 p += "," + args[k].toString(exec);
216 body = args[argsSize-1].toString(exec);
217 }
218
219 // parse the source code
220 SourceCode *source;
221 int errLine;
222 UString errMsg;
223 FunctionBodyNode *progNode = Parser::parse(body.data(),body.size(),&source,&errLine,&errMsg);
224
225 // notify debugger that source has been parsed
226 Debugger *dbg = exec->dynamicInterpreter()->imp()->debugger();
227 if (dbg) {
228 bool cont = dbg->sourceParsed(exec,source->sid,body,errLine);
229 if (!cont) {
230 source->deref();
231 dbg->imp()->abort();
232 if (progNode)
233 delete progNode;
234 return Object(new ObjectImp());
235 }
236 }
237
238 exec->interpreter()->imp()->addSourceCode(source);
239
240 // no program node == syntax error - throw a syntax error
241 if (!progNode) {
242 Object err = Error::create(exec,SyntaxError,errMsg.ascii(),errLine);
243 // we can't return a Completion(Throw) here, so just set the exception
244 // and return it
245 exec->setException(err);
246 source->deref();
247 return err;
248 }
249 source->deref();
250
251 ScopeChain scopeChain;
252 scopeChain.push(exec->dynamicInterpreter()->globalObject().imp());
253 FunctionBodyNode *bodyNode = progNode;
254
255 FunctionImp *fimp = new DeclaredFunctionImp(exec, Identifier::null(), bodyNode,
256 scopeChain);
257 Object ret(fimp); // protect from GC
258
259 // parse parameter list. throw syntax error on illegal identifiers
260 int len = p.size();
261 const UChar *c = p.data();
262 int i = 0, params = 0;
263 UString param;
264 while (i < len) {
265 while (*c == ' ' && i < len)
266 c++, i++;
267 if (Lexer::isIdentLetter(c->uc)) { // else error
268 param = UString(c, 1);
269 c++, i++;
270 while (i < len && (Lexer::isIdentLetter(c->uc) ||
271 Lexer::isDecimalDigit(c->uc))) {
272 param += UString(c, 1);
273 c++, i++;
274 }
275 while (i < len && *c == ' ')
276 c++, i++;
277 if (i == len) {
278 fimp->addParameter(Identifier(param));
279 params++;
280 break;
281 } else if (*c == ',') {
282 fimp->addParameter(Identifier(param));
283 params++;
284 c++, i++;
285 continue;
286 } // else error
287 }
288 Object err = Error::create(exec,SyntaxError,
289 I18N_NOOP("Syntax error in parameter list"),
290 -1);
291 exec->setException(err);
292 return err;
293 }
294
295 List consArgs;
296
297 Object objCons = exec->lexicalInterpreter()->builtinObject();
298 Object prototype = objCons.construct(exec,List::empty());
299 prototype.put(exec, constructorPropertyName, Value(fimp), DontEnum|DontDelete|ReadOnly);
300 fimp->put(exec, prototypePropertyName, prototype, DontEnum|DontDelete|ReadOnly);
301 return ret;
302}
303
304bool FunctionObjectImp::implementsCall() const
305{
306 return true;
307}
308
309// ECMA 15.3.1 The Function Constructor Called as a Function
310Value FunctionObjectImp::call(ExecState *exec, Object &/*thisObj*/, const List &args)
311{
312 return construct(exec,args);
313}
314
KJS::ExecState
Represents the current state of script execution.
Definition: interpreter.h:438
KJS::ExecState::dynamicInterpreter
Interpreter * dynamicInterpreter() const
Returns the interpreter associated with this execution state.
Definition: interpreter.h:452
KJS::ExecState::lexicalInterpreter
Interpreter * lexicalInterpreter() const
Returns the interpreter associated with the current scope's global object.
Definition: interpreter.cpp:394
KJS::FunctionImp
Implementation class for functions implemented in JS.
Definition: internal.h:389
KJS::FunctionPrototypeImp
The initial value of Function.prototype (and thus all objects created with the Function constructor)
Definition: function_object.h:34
KJS::Identifier
Represents an Identifier for a Javascript object.
Definition: identifier.h:32
KJS::Identifier::isNull
bool isNull() const
Returns the identfiers state of being unset.
Definition: identifier.h:78
KJS::Identifier::ustring
const UString & ustring() const
returns a UString of the identifier
Definition: identifier.h:52
KJS::InternalFunctionImp
Base class for all function objects.
Definition: function.h:40
KJS::Interpreter::builtinObject
Object builtinObject() const
Returns the builtin "Object" object.
Definition: interpreter.cpp:173
KJS::Interpreter::globalObject
Object & globalObject() const
Returns the object that is used as the global object during all script execution performed by this in...
Definition: interpreter.cpp:128
KJS::List
Native list type.
Definition: list.h:48
KJS::List::append
void append(const Value &val)
Append an object to the end of the list.
Definition: list.h:66
KJS::List::copyTail
List copyTail() const
Make a copy of the list, omitting the first element.
Definition: list.cpp:302
KJS::List::size
int size() const
Definition: list.h:90
KJS::Object
Represents an Object.
Definition: object.h:81
KJS::Object::implementsCall
bool implementsCall() const
Whether or not the object implements the call() method.
Definition: object.h:699
KJS::Object::construct
Object construct(ExecState *exec, const List &args)
Creates a new object based on this object.
Definition: object.h:696
KJS::Object::put
void put(ExecState *exec, const Identifier &propertyName, const Value &value, int attr=None)
Sets the specified property.
Definition: object.h:669
KJS::Object::call
Value call(ExecState *exec, Object &thisObj, const List &args)
Calls this object as if it is a function.
Definition: object.cpp:53
KJS::Object::get
Value get(ExecState *exec, const Identifier &propertyName) const
Retrieves the specified property from the object.
Definition: object.h:663
KJS::ScopeChain
A scope chain object.
Definition: scope_chain.h:47
KJS::String
Represents an primitive String value.
Definition: value.h:340
KJS::UString
Unicode string class.
Definition: ustring.h:189
KJS::UString::ascii
char * ascii() const
Convert the Unicode string to plain ASCII chars chopping of any higher bytes.
Definition: ustring.cpp:485
KJS::UString::size
int size() const
Definition: ustring.h:359
KJS::UString::data
const UChar * data() const
Definition: ustring.h:339
KJS::Undefined
Represents an primitive Undefined value.
Definition: value.h:269
KJS::Value
Value objects are act as wrappers ("smart pointers") around ValueImp objects and their descendents.
Definition: value.h:167
KJS::Value::isA
bool isA(Type t) const
Checks whether or not the value is of a particular tpye.
Definition: value.h:203
KJS::Value::toObject
Object toObject(ExecState *exec) const
Performs the ToObject type conversion operation on this value (ECMA 9.9)
Definition: object.h:358
KJS::Value::toUInt32
unsigned int toUInt32(ExecState *exec) const
Performs the ToUInt32 type conversion operation on this value (ECMA 9.6)
Definition: value.h:236
KJS::Value::isValid
bool isValid() const
Returns whether or not this is a valid value.
Definition: value.h:181
KJS::UChar
Unicode character.
Definition: ustring.h:51

kjs

Skip menu "kjs"
  • Main Page
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

kjs

Skip menu "kjs"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for kjs by doxygen 1.9.4
This website is maintained by Timothy Pearson.