File: function.cpp

package info (click to toggle)
kdelibs 4:2.2.2-13.woody.14
  • links: PTS
  • area: main
  • in suites: woody
  • size: 36,832 kB
  • ctags: 40,077
  • sloc: cpp: 313,284; ansic: 20,558; xml: 11,448; sh: 11,318; makefile: 2,426; perl: 2,084; yacc: 1,663; java: 1,538; lex: 629; python: 300
file content (355 lines) | stat: -rw-r--r-- 7,892 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*
 *  This file is part of the KDE libraries
 *  Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public License
 *  along with this library; see the file COPYING.LIB.  If not, write to
 *  the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 *  Boston, MA 02111-1307, USA.
 */

#include "function.h"

#include "kjs.h"
#include "types.h"
#include "internal.h"
#include "operations.h"
#include "nodes.h"
#ifndef NDEBUG
#include <stdio.h>
#endif

using namespace KJS;

const TypeInfo FunctionImp::info = { "Function", FunctionType,
				      &ObjectImp::info, 0, 0 };
const TypeInfo InternalFunctionImp::info = { "InternalFunction",
					      InternalFunctionType,
					      &FunctionImp::info, 0, 0 };
const TypeInfo ConstructorImp::info = { "Function", ConstructorType,
					 &InternalFunctionImp::info, 0, 0 };

namespace KJS {

  class Parameter {
  public:
    Parameter(const UString &n) : name(n), next(0L) { }
    ~Parameter() { delete next; }
    UString name;
    Parameter *next;
  };

};

FunctionImp::FunctionImp()
  : ObjectImp(/*TODO*/BooleanClass), param(0L)
{
}

FunctionImp::FunctionImp(const UString &n)
  : ObjectImp(/*TODO*/BooleanClass), ident(n), param(0L)
{
}

FunctionImp::~FunctionImp()
{
  delete param;
}

KJSO FunctionImp::thisValue() const
{
  return KJSO(Context::current()->thisValue());
}

void FunctionImp::addParameter(const UString &n)
{
  Parameter **p = &param;
  while (*p)
    p = &(*p)->next;

  *p = new Parameter(n);
}

void FunctionImp::setLength(int l)
{
  put("length", Number(l), ReadOnly|DontDelete|DontEnum);
}

// ECMA 10.1.3
void FunctionImp::processParameters(const List *args)
{
  KJSO variable = Context::current()->variableObject();

  assert(args);

#ifdef KJS_VERBOSE
  fprintf(stderr, "---------------------------------------------------\n"
	  "processing parameters for %s call\n",
	  name().isEmpty() ? "(internal)" : name().ascii());
#endif

  if (param) {
    ListIterator it = args->begin();
    Parameter **p = &param;
    while (*p) {
      if (it != args->end()) {
#ifdef KJS_VERBOSE
	fprintf(stderr, "setting parameter %s ", (*p)->name.ascii());
	printInfo("to", *it);
#endif
	variable.put((*p)->name, *it);
	it++;
      } else
	variable.put((*p)->name, Undefined());
      p = &(*p)->next;
    }
  }
#ifdef KJS_VERBOSE
  else {
    for (int i = 0; i < args->size(); i++)
      printInfo("setting argument", (*args)[i]);
  }
#endif
#ifdef KJS_DEBUGGER
  if (KJScriptImp::current()->debugger()) {
    UString argStr;
    for (int i = 0; i < args->size(); i++) {
      if (i > 0)
	argStr += ", ";
      Imp *a = (*args)[i].imp();
      argStr += a->toString().value() + " : " +	UString(a->typeInfo()->name);
    }
    UString n = name().isEmpty() ? UString( "(internal)" ) : name();
    KJScriptImp::current()->debugger()->callEvent(n, argStr);
  }
#endif
}

// ECMA 13.2.1
KJSO FunctionImp::executeCall(Imp *thisV, const List *args)
{
  return executeCall(thisV,args,0);
}

KJSO FunctionImp::executeCall(Imp *thisV, const List *args, const List *extraScope)
{
  bool dummyList = false;
  if (!args) {
    args = new List();
    dummyList = true;
  }

  KJScriptImp *curr = KJScriptImp::current();
  Context *save = curr->context();

  Context *ctx = new Context(codeType(), save, this, args, thisV);
  curr->setContext(ctx);

  int numScopes = 0;
  if (extraScope) {
    ListIterator scopeIt = extraScope->begin();
    for (; scopeIt != extraScope->end(); scopeIt++) {
      KJSO obj(*scopeIt);
      ctx->pushScope(obj);
      numScopes++;
    }
  }

  // assign user supplied arguments to parameters
  processParameters(args);

  Completion comp = execute(*args);

  if (dummyList)
    delete args;

  int i;
  for (i = 0; i < numScopes; i++)
    ctx->popScope();

  put("arguments", Null());
  delete ctx;
  curr->setContext(save);

#ifdef KJS_VERBOSE
  if (comp.complType() == Throw)
    printInfo("throwing", comp.value());
  else if (comp.complType() == ReturnValue)
    printInfo("returning", comp.value());
  else
    fprintf(stderr, "returning: undefined\n");
#endif
#ifdef KJS_DEBUGGER
  if (KJScriptImp::current()->debugger())
    KJScriptImp::current()->debugger()->returnEvent();
#endif

  if (comp.complType() == Throw)
    return comp.value();
  else if (comp.complType() == ReturnValue)
    return comp.value();
  else
    return Undefined();
}

UString FunctionImp::name() const
{
  return ident;
}

InternalFunctionImp::InternalFunctionImp()
{
}

InternalFunctionImp::InternalFunctionImp(int l)
{
  if (l >= 0)
    setLength(l);
}

InternalFunctionImp::InternalFunctionImp(const UString &n)
  : FunctionImp(n)
{
}

String InternalFunctionImp::toString() const
{
  if (name().isNull())
    return UString("(Internal function)");
  else
    return UString("function " + name() + "()");
}

Completion InternalFunctionImp::execute(const List &)
{
  return Completion(ReturnValue, Undefined());
}

ConstructorImp::ConstructorImp() {
  setPrototype(Global::current().functionPrototype());
  // TODO ???  put("constructor", this);
  setLength(1);
}

ConstructorImp::ConstructorImp(const UString &n)
  : InternalFunctionImp(n)
{
}

ConstructorImp::ConstructorImp(const KJSO &p, int len)
{
  setPrototype(p);
  // TODO ???  put("constructor", *this);
  setLength(len);
}

ConstructorImp::ConstructorImp(const UString &n, const KJSO &p, int len)
  : InternalFunctionImp(n)
{
  setPrototype(p);
  // TODO ???  put("constructor", *this);
  setLength(len);
}

ConstructorImp::~ConstructorImp() { }

Completion ConstructorImp::execute(const List &)
{
  /* TODO */
  return Completion(ReturnValue, Null());
}

Function::Function(Imp *d)
  : KJSO(d)
{
  if (d) {
    static_cast<FunctionImp*>(rep)->attr = ImplicitNone;
    assert(Global::current().hasProperty("[[Function.prototype]]"));
    setPrototype(Global::current().functionPrototype());
  }
}

Completion Function::execute(const List &args)
{
  assert(rep);
  return static_cast<FunctionImp*>(rep)->execute(args);
}

bool Function::hasAttribute(FunctionAttribute a) const
{
  assert(rep);
  return static_cast<FunctionImp*>(rep)->hasAttribute(a);
}

#if 0
InternalFunction::InternalFunction(Imp *d)
  : Function(d)
{
  param = 0L;
}

InternalFunction::~InternalFunction()
{
}
#endif

Constructor::Constructor(Imp *d)
  : Function(d)
{
  if (d) {
    assert(Global::current().hasProperty("[[Function.prototype]]"));
    setPrototype(Global::current().get("[[Function.prototype]]"));
    put("constructor", *this);
    KJSO tmp(d); // protect from GC
    ((FunctionImp*)d)->setLength(1);
  }
}

#if 0
Constructor::Constructor(const Object& proto, int len)
{
  setPrototype(proto);
  put("constructor", *this);
  put("length", len, DontEnum);
}
#endif

Constructor::~Constructor()
{
}

Completion Constructor::execute(const List &)
{
  /* TODO: call construct instead ? */
  return Completion(ReturnValue, Undefined());
}

Object Constructor::construct(const List &args)
{
  assert(rep && rep->type() == ConstructorType);
  return ((ConstructorImp*)rep)->construct(args);
}

Constructor Constructor::dynamicCast(const KJSO &obj)
{
  // return null object on type mismatch
  if (!obj.isA(ConstructorType))
    return Constructor(0L);

  return Constructor(obj.imp());
}

KJSO Function::thisValue() const
{
  return KJSO(Context::current()->thisValue());
}