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

kjs

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

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
  •     interface
  •     library
  •   kspeech
  •   ktexteditor
  • kabc
  • kate
  • kcmshell
  • kdecore
  • kded
  • kdefx
  • kdeprint
  • kdesu
  • kdeui
  • kdoctools
  • khtml
  • kimgio
  • kinit
  • kio
  •   bookmarks
  •   httpfilter
  •   kfile
  •   kio
  •   kioexec
  •   kpasswdserver
  •   kssl
  • kioslave
  •   http
  • kjs
  • kmdi
  •   kmdi
  • knewstuff
  • kparts
  • krandr
  • kresources
  • kspell2
  • kunittest
  • kutils
  • kwallet
  • libkmid
  • libkscreensaver
Generated for kjs by doxygen 1.8.1.2
This website is maintained by Timothy Pearson.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. |