• 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::Value
Value objects are act as wrappers ("smart pointers") around ValueImp objects and their descendents...
Definition: value.h:168
KJS::InternalFunctionImp
Base class for all function objects.
Definition: function.h:41
KJS::Value::toObject
Object toObject(ExecState *exec) const
Performs the ToObject type conversion operation on this value (ECMA 9.9)
Definition: object.h:359
KJS::ScopeChain
A scope chain object.
Definition: scope_chain.h:47
KJS::Object::construct
Object construct(ExecState *exec, const List &args)
Creates a new object based on this object.
Definition: object.h:697
KJS::List::append
void append(const Value &val)
Append an object to the end of the list.
Definition: list.h:66
KJS::Object::get
Value get(ExecState *exec, const Identifier &propertyName) const
Retrieves the specified property from the object.
Definition: object.h:664
KJS::ExecState::lexicalInterpreter
Interpreter * lexicalInterpreter() const
Returns the interpreter associated with the current scope&#39;s global object.
Definition: interpreter.cpp:395
KJS::UString::ascii
char * ascii() const
Convert the Unicode string to plain ASCII chars chopping of any higher bytes.
Definition: ustring.cpp:486
KJS::ExecState::dynamicInterpreter
Interpreter * dynamicInterpreter() const
Returns the interpreter associated with this execution state.
Definition: interpreter.h:453
KJS::Error::create
static Object create(ExecState *exec, ErrorType errtype=GeneralError, const char *message=0, int lineno=-1, int sourceId=-1)
Factory method for error objects.
Definition: object.cpp:504
KJS::FunctionPrototypeImp
The initial value of Function.prototype (and thus all objects created with the Function constructor) ...
Definition: function_object.h:35
KJS::Value::isA
bool isA(Type t) const
Checks whether or not the value is of a particular tpye.
Definition: value.h:204
KJS::List::size
int size() const
Definition: list.h:90
KJS::Object
Represents an Object.
Definition: object.h:82
KJS::Undefined
Represents an primitive Undefined value.
Definition: value.h:270
KJS::Identifier::isNull
bool isNull() const
Returns the identfiers state of being unset.
Definition: identifier.h:78
KJS::FunctionImp
Implementation class for functions implemented in JS.
Definition: internal.h:390
KJS::UString::data
const UChar * data() const
Definition: ustring.h:340
KJS::UString
Unicode string class.
Definition: ustring.h:190
KJS::UChar
Unicode character.
Definition: ustring.h:52
KJS::String
Represents an primitive String value.
Definition: value.h:341
KJS::Object::dynamicCast
static Object dynamicCast(const Value &v)
Converts a Value into an Object.
Definition: object.cpp:46
KJS::List::copyTail
List copyTail() const
Make a copy of the list, omitting the first element.
Definition: list.cpp:302
KJS::Object::implementsCall
bool implementsCall() const
Whether or not the object implements the call() method.
Definition: object.h:700
KJS
Definition: array_instance.h:28
KJS::List::empty
static const List & empty()
Returns a pointer to a static instance of an empty list.
Definition: list.cpp:322
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:129
KJS::Value::isValid
bool isValid() const
Returns whether or not this is a valid value.
Definition: value.h:182
KJS::Value::toUInt32
unsigned int toUInt32(ExecState *exec) const
Performs the ToUInt32 type conversion operation on this value (ECMA 9.6)
Definition: value.h:237
KJS::Object::put
void put(ExecState *exec, const Identifier &propertyName, const Value &value, int attr=None)
Sets the specified property.
Definition: object.h:670
KJS::List
Native list type.
Definition: list.h:48
KJS::Object::call
Value call(ExecState *exec, Object &thisObj, const List &args)
Calls this object as if it is a function.
Definition: object.cpp:54
KJS::Identifier::null
static const Identifier & null()
Creates an empty Identifier.
Definition: identifier.cpp:302
KJS::ExecState
Represents the current state of script execution.
Definition: interpreter.h:439
KJS::Identifier
Represents an Identifier for a Javascript object.
Definition: identifier.h:32
KJS::UString::size
int size() const
Definition: ustring.h:360
KJS::Identifier::ustring
const UString & ustring() const
returns a UString of the identifier
Definition: identifier.h:52
KJS::Interpreter::builtinObject
Object builtinObject() const
Returns the builtin "Object" object.
Definition: interpreter.cpp:174

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.8.13
This website is maintained by Timothy Pearson.