kjs Library API Documentation

function.cpp

00001 // -*- c-basic-offset: 2 -*-
00002 /*
00003  *  This file is part of the KDE libraries
00004  *  Copyright (C) 1999-2002 Harri Porten (porten@kde.org)
00005  *  Copyright (C) 2001,2003 Peter Kelly (pmk@post.com)
00006  *  Copyright (C) 2003 Apple Computer, Inc.
00007  *
00008  *  This library is free software; you can redistribute it and/or
00009  *  modify it under the terms of the GNU Library General Public
00010  *  License as published by the Free Software Foundation; either
00011  *  version 2 of the License, or (at your option) any later version.
00012  *
00013  *  This library is distributed in the hope that it will be useful,
00014  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016  *  Library General Public License for more details.
00017  *
00018  *  You should have received a copy of the GNU Library General Public License
00019  *  along with this library; see the file COPYING.LIB.  If not, write to
00020  *  the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00021  *  Boston, MA 02111-1307, USA.
00022  *
00023  */
00024 
00025 #include "function.h"
00026 
00027 #include "internal.h"
00028 #include "function_object.h"
00029 #include "lexer.h"
00030 #include "nodes.h"
00031 #include "operations.h"
00032 #include "debugger.h"
00033 #include "context.h"
00034 
00035 #include <stdio.h>
00036 #include <stdlib.h>
00037 #include <assert.h>
00038 #include <string.h>
00039 #include <errno.h>
00040 #include <math.h>
00041 #include <ctype.h>
00042 
00043 using namespace KJS;
00044 
00045 // ------------------------- URI handling functions ---------------------------
00046 
00047 // ECMA 15.1.3
00048 UString encodeURI(ExecState *exec, UString string, UString unescapedSet)
00049 {
00050   char hexdigits[] = "0123456789ABCDEF";
00051   int encbufAlloc = 2;
00052   UChar *encbuf = (UChar*)malloc(encbufAlloc*sizeof(UChar));
00053   int encbufLen = 0;
00054 
00055   for (int k = 0; k < string.size(); k++) {
00056 
00057     UChar C = string[k];
00058     if (unescapedSet.find(C) >= 0) {
00059       if (encbufLen+1 >= encbufAlloc)
00060     encbuf = (UChar*)realloc(encbuf,(encbufAlloc *= 2)*sizeof(UChar));
00061       encbuf[encbufLen++] = C;
00062     }
00063     else {
00064       unsigned char octets[4];
00065       int octets_len = 0;
00066       if (C.uc <= 0x007F) {
00067     unsigned short zzzzzzz = C.uc;
00068     octets[0] = zzzzzzz;
00069     octets_len = 1;
00070       }
00071       else if (C.uc <= 0x07FF) {
00072     unsigned short zzzzzz = C.uc & 0x3F;
00073     unsigned short yyyyy = (C.uc >> 6) & 0x1F;
00074     octets[0] = 0xC0 | yyyyy;
00075     octets[1] = 0x80 | zzzzzz;
00076     octets_len = 2;
00077       }
00078       else if (C.uc >= 0xD800 && C.uc <= 0xDBFF) {
00079     
00080         // we need two chars
00081     if (k + 1 >= string.size()) {
00082       Object err = Error::create(exec,URIError);
00083       exec->setException(err);
00084       free(encbuf);
00085       return UString();
00086     }
00087 
00088     unsigned short Cnext = UChar(string[++k]).uc;
00089 
00090     if (Cnext < 0xDC00 || Cnext > 0xDFFF) {
00091       Object err = Error::create(exec,URIError);
00092       exec->setException(err);
00093       free(encbuf);
00094       return UString();
00095     }
00096 
00097     unsigned short zzzzzz = Cnext & 0x3F;
00098     unsigned short yyyy = (Cnext >> 6) & 0x0F;
00099     unsigned short xx = C.uc & 0x03;
00100     unsigned short wwww = (C.uc >> 2) & 0x0F;
00101     unsigned short vvvv = (C.uc >> 6) & 0x0F;
00102     unsigned short uuuuu = vvvv+1;
00103     octets[0] = 0xF0 | (uuuuu >> 2);
00104     octets[1] = 0x80 | ((uuuuu & 0x03) << 4) | wwww;
00105     octets[2] = 0x80 | (xx << 4) | yyyy;
00106     octets[3] = 0x80 | zzzzzz;
00107     octets_len = 4;
00108       }
00109       else if (C.uc >= 0xDC00 && C.uc <= 0xDFFF) {
00110     Object err = Error::create(exec,URIError);
00111     exec->setException(err);
00112     free(encbuf);
00113     return UString();
00114       }
00115       else {
00116     // 0x0800 - 0xD7FF or 0xE000 - 0xFFFF
00117     unsigned short zzzzzz = C.uc & 0x3F;
00118     unsigned short yyyyyy = (C.uc >> 6) & 0x3F;
00119     unsigned short xxxx = (C.uc >> 12) & 0x0F;
00120     octets[0] = 0xE0 | xxxx;
00121     octets[1] = 0x80 | yyyyyy;
00122     octets[2] = 0x80 | zzzzzz;
00123     octets_len = 3;
00124       }
00125 
00126       while (encbufLen+3*octets_len >= encbufAlloc)
00127     encbuf = (UChar*)realloc(encbuf,(encbufAlloc *= 2)*sizeof(UChar));
00128 
00129       for (int j = 0; j < octets_len; j++) {
00130     encbuf[encbufLen++] = '%';
00131     encbuf[encbufLen++] = hexdigits[octets[j] >> 4];
00132     encbuf[encbufLen++] = hexdigits[octets[j] & 0x0F];
00133       }
00134     }
00135   }
00136 
00137   UString encoded(encbuf,encbufLen);
00138   free(encbuf);
00139   return encoded;
00140 }
00141 
00142 static bool decodeHex(UChar hi, UChar lo, unsigned short *val)
00143 {
00144   *val = 0;
00145   if (hi.uc >= '0' && hi.uc <= '9')
00146     *val = (hi.uc-'0') << 4;
00147   else if (hi.uc >= 'a' && hi.uc <= 'f')
00148     *val = 10+(hi.uc-'a') << 4;
00149   else if (hi.uc >= 'A' && hi.uc <= 'F')
00150     *val = 10+(hi.uc-'A') << 4;
00151   else
00152     return false;
00153 
00154   if (lo.uc >= '0' && lo.uc <= '9')
00155     *val |= (lo.uc-'0');
00156   else if (lo.uc >= 'a' && lo.uc <= 'f')
00157     *val |= 10+(lo.uc-'a');
00158   else if (lo.uc >= 'A' && lo.uc <= 'F')
00159     *val |= 10+(lo.uc-'A');
00160   else
00161     return false;
00162 
00163   return true;
00164 }
00165 
00166 UString decodeURI(ExecState *exec, UString string, UString reservedSet)
00167 {
00168   int decbufAlloc = 2;
00169   UChar *decbuf = (UChar*)malloc(decbufAlloc*sizeof(UChar));
00170   int decbufLen = 0;
00171 
00172   for (int k = 0; k < string.size(); k++) {
00173     UChar C = string[k];
00174 
00175     if (C != UChar('%')) {
00176       // Normal unescaped character
00177       if (decbufLen+1 >= decbufAlloc)
00178     decbuf = (UChar*)realloc(decbuf,(decbufAlloc *= 2)*sizeof(UChar));
00179       decbuf[decbufLen++] = C;
00180       continue;
00181     }
00182 
00183     // We have % escape sequence... expect at least 2 more characters
00184     int start = k;
00185     if (k+2 >= string.size()) {
00186       Object err = Error::create(exec,URIError);
00187       exec->setException(err);
00188       free(decbuf);
00189       return UString();
00190     }
00191 
00192     unsigned short B;
00193     if (!decodeHex(string[k+1],string[k+2],&B)) {
00194       Object err = Error::create(exec,URIError);
00195       exec->setException(err);
00196       free(decbuf);
00197       return UString();
00198     }
00199 
00200     k += 2;
00201 
00202     if (decbufLen+2 >= decbufAlloc)
00203         decbuf = (UChar*)realloc(decbuf,(decbufAlloc *= 2)*sizeof(UChar));
00204 
00205     if ((B & 0x80) == 0) {
00206       // Single-byte character
00207       C = B;
00208     }
00209     else {
00210       // Multi-byte character
00211       int n = 0;
00212       while (((B << n) & 0x80) != 0)
00213     n++;
00214 
00215       if (n < 2 || n > 4) {
00216     Object err = Error::create(exec,URIError);
00217     exec->setException(err);
00218     free(decbuf);
00219     return UString();
00220       }
00221 
00222       if (k+3*(n-1) >= string.size()) {
00223     Object err = Error::create(exec,URIError);
00224     exec->setException(err);
00225     free(decbuf);
00226     return UString();
00227       }
00228 
00229       unsigned short octets[4];
00230       octets[0] = B;
00231       for (int j = 1; j < n; j++) {
00232     k++;
00233     if ((UChar(string[k]) != UChar('%')) ||
00234         !decodeHex(string[k+1],string[k+2],&B) ||
00235         ((B & 0xC0) != 0x80)) {
00236       Object err = Error::create(exec,URIError);
00237       exec->setException(err);
00238       free(decbuf);
00239       return UString();
00240     }
00241 
00242     k += 2;
00243     octets[j] = B;
00244       }
00245 
00246       // UTF-8 transform
00247       unsigned long V;
00248       if (n == 2) {
00249     unsigned long yyyyy = octets[0] & 0x1F;
00250     unsigned long zzzzzz = octets[1] & 0x3F;
00251     V = (yyyyy << 6) | zzzzzz;
00252     C = UChar((unsigned short)V);
00253       }
00254       else if (n == 3) {
00255     unsigned long xxxx = octets[0] & 0x0F;
00256     unsigned long yyyyyy = octets[1] & 0x3F;
00257     unsigned long zzzzzz = octets[2] & 0x3F;
00258     V = (xxxx << 12) | (yyyyyy << 6) | zzzzzz;
00259     C = UChar((unsigned short)V);
00260       }
00261       else {
00262     assert(n == 4);
00263     unsigned long uuuuu = ((octets[0] & 0x07) << 2) | ((octets[1] >> 4) & 0x03);
00264     unsigned long vvvv = uuuuu-1;
00265     if (vvvv > 0x0F) {
00266           Object err = Error::create(exec,URIError);
00267       exec->setException(err);
00268       free(decbuf);
00269       return UString();
00270     }        
00271     unsigned long wwww = octets[1] & 0x0F;
00272     unsigned long xx = (octets[2] >> 4) & 0x03;
00273     unsigned long yyyy = octets[2] & 0x0F;
00274     unsigned long zzzzzz = octets[3] & 0x3F;
00275     unsigned short H = 0xD800 | (vvvv << 6) | (wwww << 2) | xx;
00276     unsigned short L = 0xDC00 | (yyyy << 6) | zzzzzz;
00277     decbuf[decbufLen++] = UChar(H);
00278     decbuf[decbufLen++] = UChar(L);
00279     continue;
00280       }
00281     }
00282 
00283     if (reservedSet.find(C) < 0) {
00284         decbuf[decbufLen++] = C;
00285     }
00286     else {
00287       while (decbufLen+k-start >= decbufAlloc)
00288     decbuf = (UChar*)realloc(decbuf,(decbufAlloc *= 2)*sizeof(UChar));
00289       for (int p = start; p < k; p++)
00290     decbuf[decbufLen++] = string[p];
00291     }
00292   }
00293 
00294   UString decoded(decbuf,decbufLen);
00295   free(decbuf);
00296   return decoded;
00297 }
00298 
00299 static UString uriReserved = ";/?:@&=+$,";
00300 static UString uriAlpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
00301 static UString DecimalDigit = "0123456789";
00302 static UString uriMark = "-_.!~*'()";
00303 static UString uriUnescaped = uriAlpha+DecimalDigit+uriMark;
00304 
00305 // ----------------------------- FunctionImp ----------------------------------
00306 
00307 const ClassInfo FunctionImp::info = {"Function", &InternalFunctionImp::info, 0, 0};
00308 
00309 namespace KJS {
00310   class Parameter {
00311   public:
00312     Parameter(const Identifier &n) : name(n), next(0L) { }
00313     ~Parameter() { delete next; }
00314     Identifier name;
00315     Parameter *next;
00316   };
00317 }
00318 
00319 FunctionImp::FunctionImp(ExecState *exec, const Identifier &n)
00320   : InternalFunctionImp(
00321       static_cast<FunctionPrototypeImp*>(exec->interpreter()->builtinFunctionPrototype().imp())
00322       ), param(0L), line0(-1), line1(-1), sid(-1)
00323 {
00324   //fprintf(stderr,"FunctionImp::FunctionImp this=%p\n");
00325   ident = n;
00326 }
00327 
00328 FunctionImp::~FunctionImp()
00329 {
00330   delete param;
00331 }
00332 
00333 bool FunctionImp::implementsCall() const
00334 {
00335   return true;
00336 }
00337 
00338 Value FunctionImp::call(ExecState *exec, Object &thisObj, const List &args)
00339 {
00340   Object &globalObj = exec->interpreter()->globalObject();
00341 
00342   // enter a new execution context
00343   ContextImp ctx(globalObj, exec->interpreter()->imp(), thisObj, sid, codeType(),
00344                  exec->context().imp(), this, &args);
00345   ExecState newExec(exec->interpreter(), &ctx);
00346   newExec._exception = exec->exception(); // could be null
00347 
00348   // assign user supplied arguments to parameters
00349   processParameters(&newExec, args);
00350   // add variable declarations (initialized to undefined)
00351   processVarDecls(&newExec);
00352 
00353   ctx.setLines(line0,line0);
00354   Debugger *dbg = exec->interpreter()->imp()->debugger();
00355   if (dbg) {
00356     if (!dbg->enterContext(&newExec)) {
00357       // debugger requested we stop execution
00358       dbg->imp()->abort();
00359       return Undefined();
00360     }
00361   }
00362 
00363   Completion comp = execute(&newExec);
00364 
00365   ctx.setLines(line1,line1);
00366   if (dbg) {
00367     Object func(this);
00368     // ### lineno is inaccurate - we really want the end of the function _body_ here
00369     // line1 is suppoed to be the end of the function start, just before the body
00370     if (!dbg->exitContext(&newExec,comp)) {
00371       // debugger requested we stop execution
00372       dbg->imp()->abort();
00373       return Undefined();
00374     }
00375   }
00376 
00377   // if an exception occurred, propogate it back to the previous execution object
00378   if (newExec.hadException())
00379     exec->_exception = newExec.exception();
00380 
00381 #ifdef KJS_VERBOSE
00382   CString n = ident.isEmpty() ? CString("(internal)") : ident.ustring().cstring();
00383   if (comp.complType() == Throw) {
00384     n += " throws";
00385     printInfo(exec, n.c_str(), comp.value());
00386   } else if (comp.complType() == ReturnValue) {
00387     n += " returns";
00388     printInfo(exec, n.c_str(), comp.value());
00389   } else
00390     fprintf(stderr, "%s returns: undefined\n", n.c_str());
00391 #endif
00392 
00393   if (comp.complType() == Throw) {
00394     exec->_exception = comp.value();
00395     return comp.value();
00396   }
00397   else if (comp.complType() == ReturnValue)
00398     return comp.value();
00399   else
00400     return Undefined();
00401 }
00402 
00403 void FunctionImp::addParameter(const Identifier &n)
00404 {
00405   Parameter **p = &param;
00406   while (*p)
00407     p = &(*p)->next;
00408 
00409   *p = new Parameter(n);
00410 }
00411 
00412 Identifier FunctionImp::parameterProperty(int index) const
00413 {
00414   // Find the property name corresponding to the given parameter
00415   int pos = 0;
00416   Parameter *p;
00417   for (p = param; p && pos < index; p = p->next)
00418     pos++;
00419 
00420   if (!p)
00421     return Identifier::null();
00422 
00423   // Are there any subsequent parameters with the same name?
00424   Identifier name = p->name;
00425   for (p = p->next; p; p = p->next)
00426     if (p->name == name)
00427       return Identifier::null();
00428 
00429   return name;
00430 }
00431 
00432 UString FunctionImp::parameterString() const
00433 {
00434   UString s;
00435   const Parameter *p = param;
00436   while (p) {
00437     if (!s.isEmpty())
00438         s += ", ";
00439     s += p->name.ustring();
00440     p = p->next;
00441   }
00442 
00443   return s;
00444 }
00445 
00446 
00447 // ECMA 10.1.3q
00448 void FunctionImp::processParameters(ExecState *exec, const List &args)
00449 {
00450   Object variable = exec->context().imp()->variableObject();
00451 
00452 #ifdef KJS_VERBOSE
00453   fprintf(stderr, "---------------------------------------------------\n"
00454       "processing parameters for %s call\n",
00455       name().isEmpty() ? "(internal)" : name().ascii());
00456 #endif
00457 
00458   if (param) {
00459     ListIterator it = args.begin();
00460     Parameter *p = param;
00461     while (p) {
00462       if (it != args.end()) {
00463 #ifdef KJS_VERBOSE
00464     fprintf(stderr, "setting parameter %s ", p->name.ascii());
00465     printInfo(exec,"to", *it);
00466 #endif
00467     variable.put(exec, p->name, *it);
00468     it++;
00469       } else
00470     variable.put(exec, p->name, Undefined());
00471       p = p->next;
00472     }
00473   }
00474 #ifdef KJS_VERBOSE
00475   else {
00476     for (int i = 0; i < args.size(); i++)
00477       printInfo(exec,"setting argument", args[i]);
00478   }
00479 #endif
00480 }
00481 
00482 void FunctionImp::processVarDecls(ExecState */*exec*/)
00483 {
00484 }
00485 
00486 Value FunctionImp::get(ExecState *exec, const Identifier &propertyName) const
00487 {
00488     // Find the arguments from the closest context.
00489     if (propertyName == argumentsPropertyName) {
00490 // delme
00491         ContextImp *context = exec->context().imp();
00492 // fixme
00493 //         ContextImp *context = exec->_context;
00494         while (context) {
00495             if (context->function() == this)
00496                 return static_cast<ActivationImp *>
00497                     (context->activationObject())->get(exec, propertyName);
00498             context = context->callingContext();
00499         }
00500         return Null();
00501     }
00502     
00503     // Compute length of parameters.
00504     if (propertyName == lengthPropertyName) {
00505         const Parameter * p = param;
00506         int count = 0;
00507         while (p) {
00508             ++count;
00509             p = p->next;
00510         }
00511         return Number(count);
00512     }
00513     
00514     return InternalFunctionImp::get(exec, propertyName);
00515 }
00516 
00517 void FunctionImp::put(ExecState *exec, const Identifier &propertyName, const Value &value, int attr)
00518 {
00519     if (propertyName == argumentsPropertyName || propertyName == lengthPropertyName)
00520         return;
00521     InternalFunctionImp::put(exec, propertyName, value, attr);
00522 }
00523 
00524 bool FunctionImp::hasProperty(ExecState *exec, const Identifier &propertyName) const
00525 {
00526     if (propertyName == argumentsPropertyName || propertyName == lengthPropertyName)
00527         return true;
00528     return InternalFunctionImp::hasProperty(exec, propertyName);
00529 }
00530 
00531 bool FunctionImp::deleteProperty(ExecState *exec, const Identifier &propertyName)
00532 {
00533     if (propertyName == argumentsPropertyName || propertyName == lengthPropertyName)
00534         return false;
00535     return InternalFunctionImp::deleteProperty(exec, propertyName);
00536 }
00537 
00538 // ------------------------------ DeclaredFunctionImp --------------------------
00539 
00540 // ### is "Function" correct here?
00541 const ClassInfo DeclaredFunctionImp::info = {"Function", &FunctionImp::info, 0, 0};
00542 
00543 DeclaredFunctionImp::DeclaredFunctionImp(ExecState *exec, const Identifier &n,
00544                      FunctionBodyNode *b, const ScopeChain &sc)
00545   : FunctionImp(exec,n), body(b)
00546 {
00547   Value protect(this);
00548   body->ref();
00549   setScope(sc);
00550   line0 = body->firstLine();
00551   line1 = body->lastLine();
00552   sid = body->sourceId();
00553 }
00554 
00555 DeclaredFunctionImp::~DeclaredFunctionImp()
00556 {
00557   if ( body->deref() )
00558     delete body;
00559 }
00560 
00561 bool DeclaredFunctionImp::implementsConstruct() const
00562 {
00563   return true;
00564 }
00565 
00566 // ECMA 13.2.2 [[Construct]]
00567 Object DeclaredFunctionImp::construct(ExecState *exec, const List &args)
00568 {
00569   Object proto;
00570   Value p = get(exec,prototypePropertyName);
00571   if (p.type() == ObjectType)
00572     proto = Object(static_cast<ObjectImp*>(p.imp()));
00573   else
00574     proto = exec->interpreter()->builtinObjectPrototype();
00575 
00576   Object obj(new ObjectImp(proto));
00577 
00578   Value res = call(exec,obj,args);
00579 
00580   if (res.type() == ObjectType)
00581     return Object::dynamicCast(res);
00582   else
00583     return obj;
00584 }
00585 
00586 Completion DeclaredFunctionImp::execute(ExecState *exec)
00587 {
00588   Completion result = body->execute(exec);
00589 
00590   if (result.complType() == Throw || result.complType() == ReturnValue)
00591       return result;
00592   return Completion(Normal, Undefined()); // TODO: or ReturnValue ?
00593 }
00594 
00595 void DeclaredFunctionImp::processVarDecls(ExecState *exec)
00596 {
00597   body->processVarDecls(exec);
00598 }
00599 
00600 // ------------------------------- ShadowImp -----------------------------------
00601 
00602 namespace KJS {
00603 
00604 // Acts as a placeholder value to indicate that the actual value is kept
00605 // in the activation object
00606 class ShadowImp : public ObjectImp {
00607 public:
00608   ShadowImp(ObjectImp *_obj, Identifier _prop) : obj(_obj), prop(_prop) {}
00609   virtual void mark();
00610 
00611   virtual const ClassInfo *classInfo() const { return &info; }
00612   static const ClassInfo info;
00613 
00614   ObjectImp *obj;
00615   Identifier prop;
00616 };
00617 
00618 /*KDE_NOEXPORT*/ const ClassInfo ShadowImp::info = {"Shadow", 0, 0, 0};
00619 
00620 void ShadowImp::mark()
00621 {
00622   ObjectImp::mark();
00623   if (!obj->marked())
00624     obj->mark();
00625 }
00626 
00627 }
00628 
00629 // ------------------------------ ArgumentsImp ---------------------------------
00630 
00631 const ClassInfo ArgumentsImp::info = {"Arguments", 0, 0, 0};
00632 
00633 // ECMA 10.1.8
00634 ArgumentsImp::ArgumentsImp(ExecState *exec, FunctionImp *func, const List &args,
00635                ActivationImp *act)
00636   : ObjectImp(exec->interpreter()->builtinObjectPrototype()), activation(act)
00637 {
00638   Value protect(this);
00639   putDirect(calleePropertyName, func, DontEnum);
00640   putDirect(lengthPropertyName, args.size(), DontEnum);
00641   if (!args.isEmpty()) {
00642     ListIterator arg = args.begin();
00643     for (int i = 0; arg != args.end(); arg++, i++) {
00644       Identifier prop = func->parameterProperty(i);
00645       if (!prop.isEmpty()) {
00646     Object shadow(new ShadowImp(act,prop));
00647     ObjectImp::put(exec,Identifier::from(i), shadow, DontEnum);
00648       }
00649       else {
00650     ObjectImp::put(exec,Identifier::from(i), *arg, DontEnum);
00651       }
00652     }
00653   }
00654 }
00655 
00656 void ArgumentsImp::mark()
00657 {
00658   ObjectImp::mark();
00659   if (!activation->marked())
00660     activation->mark();
00661 }
00662 
00663 Value ArgumentsImp::get(ExecState *exec, const Identifier &propertyName) const
00664 {
00665   Value val = ObjectImp::get(exec,propertyName);
00666   assert(SimpleNumber::is(val.imp()) || !val.imp()->isDestroyed());
00667   Object obj = Object::dynamicCast(val);
00668   if (obj.isValid() && obj.inherits(&ShadowImp::info)) {
00669     ShadowImp *shadow = static_cast<ShadowImp*>(val.imp());
00670     return activation->get(exec,shadow->prop);
00671   }
00672   else {
00673     return val;
00674   }
00675 }
00676 
00677 void ArgumentsImp::put(ExecState *exec, const Identifier &propertyName,
00678                const Value &value, int attr)
00679 {
00680   Value val = ObjectImp::get(exec,propertyName);
00681   Object obj = Object::dynamicCast(val);
00682   if (obj.isValid() && obj.inherits(&ShadowImp::info)) {
00683     ShadowImp *shadow = static_cast<ShadowImp*>(val.imp());
00684     activation->put(exec,shadow->prop,value,attr);
00685   }
00686   else {
00687     ObjectImp::put(exec,propertyName,value,attr);
00688   }
00689 }
00690 
00691 // ------------------------------ ActivationImp --------------------------------
00692 
00693 const ClassInfo ActivationImp::info = {"Activation", 0, 0, 0};
00694 
00695 // ECMA 10.1.6
00696 ActivationImp::ActivationImp(FunctionImp *function, const List &arguments)
00697     : _function(function), _arguments(true), _argumentsObject(0)
00698 {
00699   _arguments = arguments.copy();
00700   // FIXME: Do we need to support enumerating the arguments property?
00701 }
00702 
00703 Value ActivationImp::get(ExecState *exec, const Identifier &propertyName) const
00704 {
00705   if (propertyName == argumentsPropertyName) {
00706     ValueImp *imp = getDirect(propertyName);
00707     if (imp)
00708       return Value(imp);
00709 
00710     if (!_argumentsObject)
00711       _argumentsObject = new ArgumentsImp(exec, _function, _arguments, const_cast<ActivationImp*>(this));
00712     return Value(_argumentsObject);
00713   }
00714   return ObjectImp::get(exec, propertyName);
00715 }
00716 
00717 bool ActivationImp::hasProperty(ExecState *exec, const Identifier &propertyName) const
00718 {
00719   if (propertyName == argumentsPropertyName)
00720     return true;
00721   return ObjectImp::hasProperty(exec, propertyName);
00722 }
00723 
00724 bool ActivationImp::deleteProperty(ExecState *exec, const Identifier &propertyName)
00725 {
00726   if (propertyName == argumentsPropertyName)
00727     return false;
00728   return ObjectImp::deleteProperty(exec, propertyName);
00729 }
00730 
00731 void ActivationImp::mark()
00732 {
00733   ObjectImp::mark();
00734   if (_function && !_function->marked()) 
00735     _function->mark();
00736   _arguments.mark();
00737   if (_argumentsObject && !_argumentsObject->marked())
00738     _argumentsObject->mark();
00739 }
00740 
00741 // ------------------------------ GlobalFunc -----------------------------------
00742 
00743 
00744 GlobalFuncImp::GlobalFuncImp(ExecState */*exec*/, FunctionPrototypeImp *funcProto,
00745                  int i, int len, const Identifier &_ident)
00746   : InternalFunctionImp(funcProto), id(i)
00747 {
00748   Value protect(this);
00749   putDirect(lengthPropertyName, len, DontDelete|ReadOnly|DontEnum);
00750   ident = _ident;
00751 }
00752 
00753 CodeType GlobalFuncImp::codeType() const
00754 {
00755   return id == Eval ? EvalCode : codeType();
00756 }
00757 
00758 bool GlobalFuncImp::implementsCall() const
00759 {
00760   return true;
00761 }
00762 
00763 Value GlobalFuncImp::call(ExecState *exec, Object &thisObj, const List &args)
00764 {
00765   Value res;
00766 
00767   static const char non_escape[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
00768                    "abcdefghijklmnopqrstuvwxyz"
00769                    "0123456789@*_+-./";
00770 
00771   switch (id) {
00772   case Eval: { // eval()
00773     Value x = args[0];
00774     if (x.type() != StringType)
00775       return x;
00776     else {
00777       UString s = x.toString(exec);
00778 
00779       int errLine;
00780       UString errMsg;
00781 #ifdef KJS_VERBOSE
00782       fprintf(stderr, "eval(): %s\n", s.ascii());
00783 #endif
00784       SourceCode *source;
00785       FunctionBodyNode *progNode = Parser::parse(s.data(),s.size(),&source,&errLine,&errMsg);
00786       if (progNode)
00787     progNode->setProgram(true);
00788 
00789       // notify debugger that source has been parsed
00790       Debugger *dbg = exec->interpreter()->imp()->debugger();
00791       if (dbg) {
00792     bool cont = dbg->sourceParsed(exec,source->sid,s,errLine);
00793     if (!cont) {
00794       source->deref();
00795       dbg->imp()->abort();
00796       if (progNode)
00797         delete progNode;
00798       return Undefined();
00799     }
00800       }
00801 
00802       exec->interpreter()->imp()->addSourceCode(source);
00803 
00804       // no program node means a syntax occurred
00805       if (!progNode) {
00806     Object err = Error::create(exec,SyntaxError,errMsg.ascii(),errLine);
00807         err.put(exec,"sid",Number(source->sid));
00808         exec->setException(err);
00809     source->deref();
00810         return err;
00811       }
00812 
00813       source->deref();
00814       progNode->ref();
00815 
00816       // enter a new execution context
00817       ContextImp ctx(exec->interpreter()->globalObject(),
00818                      exec->interpreter()->imp(),
00819                      thisObj,
00820                      source->sid,
00821                      EvalCode,
00822                      exec->context().imp());
00823 
00824       ExecState newExec(exec->interpreter(), &ctx);
00825       newExec.setException(exec->exception()); // could be null
00826 
00827       ctx.setLines(progNode->firstLine(),progNode->firstLine());
00828       if (dbg) {
00829     if (!dbg->enterContext(&newExec)) {
00830       // debugger requested we stop execution
00831       dbg->imp()->abort();
00832 
00833       if (progNode->deref())
00834         delete progNode;
00835       return Undefined();
00836     }
00837       }
00838 
00839       // execute the code
00840       Completion c = progNode->execute(&newExec);
00841 
00842       res = Undefined();
00843 
00844       ctx.setLines(progNode->lastLine(),progNode->lastLine());
00845       if (dbg && !dbg->exitContext(&newExec,c))
00846     // debugger requested we stop execution
00847     dbg->imp()->abort();
00848       else if (newExec.hadException()) // propagate back to parent context
00849     exec->_exception = newExec.exception(); 
00850       else if (c.complType() == Throw)
00851     exec->setException(c.value());
00852       else if (c.isValueCompletion())
00853     res = c.value();
00854 
00855       if (progNode->deref())
00856     delete progNode;
00857 
00858       return res;
00859     }
00860     break;
00861   }
00862   case ParseInt: { // ECMA 15.1.2.2
00863     CString cstr = args[0].toString(exec).cstring();
00864     const char* startptr = cstr.c_str();
00865     while ( *startptr && isspace( *startptr ) ) // first, skip leading spaces
00866       ++startptr;
00867 
00868     int base = 0;
00869     if (args.size() > 1)
00870       base = args[1].toInt32(exec);
00871 
00872     double sign = 1;
00873     if (*startptr == '-') {
00874       sign = -1;
00875       startptr++;
00876     }
00877     else if (*startptr == '+') {
00878       sign = 1;
00879       startptr++;
00880     }
00881 
00882     bool leading0 = false;
00883     if ((base == 0 || base == 16) &&
00884     (*startptr == '0' && (startptr[1] == 'x' || startptr[1] == 'X'))) {
00885       startptr += 2;
00886       base = 16;
00887     }
00888     else if (base == 0 && *startptr == '0') {
00889       base = 8;
00890       leading0 = true;
00891       startptr++;
00892     }
00893     else if (base == 0) {
00894       base = 10;
00895     }
00896 
00897     if (base < 2 || base > 36) {
00898       res = Number(NaN);
00899     }
00900     else {
00901       long double val = 0;
00902       int index = 0;
00903       for (; *startptr; startptr++) {
00904     int thisval = -1;
00905     if (*startptr >= '0' && *startptr <= '9')
00906       thisval = *startptr - '0';
00907     else if (*startptr >= 'a' && *startptr <= 'z')
00908       thisval = 10 + *startptr - 'a';
00909     else if (*startptr >= 'A' && *startptr <= 'Z')
00910       thisval = 10 + *startptr - 'A';
00911 
00912     if (thisval < 0 || thisval >= base)
00913       break;
00914 
00915     val *= base;
00916     val += thisval;
00917     index++;
00918       }
00919 
00920       if (index == 0 && !leading0)
00921     res = Number(NaN);
00922       else
00923     res = Number(double(val)*sign);
00924     }
00925     break;
00926   }
00927   case ParseFloat: {
00928     UString str = args[0].toString(exec);
00929     // don't allow hex numbers here
00930     bool isHex = false;
00931     if (str.is8Bit()) {
00932       const char *c = str.ascii();
00933       while (isspace(*c))
00934     c++;
00935       isHex = (c[0] == '0' && (c[1] == 'x' || c[1] == 'X'));
00936     }
00937     if (isHex)
00938       res = Number(0);
00939     else
00940       res = Number(str.toDouble( true /*tolerant*/, false ));
00941     }
00942     break;
00943   case IsNaN:
00944     res = Boolean(isNaN(args[0].toNumber(exec)));
00945     break;
00946   case IsFinite: {
00947     double n = args[0].toNumber(exec);
00948     res = Boolean(!isNaN(n) && !isInf(n));
00949     break;
00950   }
00951   case DecodeURI:
00952     res = String(decodeURI(exec,args[0].toString(exec),uriReserved+"#"));
00953     break;
00954   case DecodeURIComponent:
00955     res = String(decodeURI(exec,args[0].toString(exec),""));
00956     break;
00957   case EncodeURI:
00958     res = String(encodeURI(exec,args[0].toString(exec),uriReserved+uriUnescaped+"#"));
00959     break;
00960   case EncodeURIComponent:
00961     res = String(encodeURI(exec,args[0].toString(exec),uriUnescaped));
00962     break;
00963   case Escape: {
00964     UString r = "", s, str = args[0].toString(exec);
00965     const UChar *c = str.data();
00966     for (int k = 0; k < str.size(); k++, c++) {
00967       int u = c->uc;
00968       if (u > 255) {
00969     char tmp[7];
00970     sprintf(tmp, "%%u%04X", u);
00971     s = UString(tmp);
00972       } else if (strchr(non_escape, (char)u)) {
00973     s = UString(c, 1);
00974       } else {
00975     char tmp[4];
00976     sprintf(tmp, "%%%02X", u);
00977     s = UString(tmp);
00978       }
00979       r += s;
00980     }
00981     res = String(r);
00982     break;
00983   }
00984   case UnEscape: {
00985     UString s, str = args[0].toString(exec);
00986     int k = 0, len = str.size();
00987     while (k < len) {
00988       const UChar *c = str.data() + k;
00989       UChar u;
00990       if (*c == UChar('%') && k <= len - 6 && *(c+1) == UChar('u')) {
00991     if (Lexer::isHexDigit((c+2)->uc) && Lexer::isHexDigit((c+3)->uc) &&
00992         Lexer::isHexDigit((c+4)->uc) && Lexer::isHexDigit((c+5)->uc)) {
00993       u = Lexer::convertUnicode((c+2)->uc, (c+3)->uc,
00994                     (c+4)->uc, (c+5)->uc);
00995       c = &u;
00996       k += 5;
00997     }
00998       } else if (*c == UChar('%') && k <= len - 3 &&
00999          Lexer::isHexDigit((c+1)->uc) && Lexer::isHexDigit((c+2)->uc)) {
01000     u = UChar(Lexer::convertHex((c+1)->uc, (c+2)->uc));
01001     c = &u;
01002     k += 2;
01003       }
01004       k++;
01005       s += UString(c, 1);
01006     }
01007     res = String(s);
01008     break;
01009   }
01010   case KJSPrint: {
01011 #ifndef NDEBUG
01012     UString str = args[0].toString(exec);
01013     puts(str.ascii());
01014 #endif
01015     break;
01016   }
01017   }
01018 
01019   return res;
01020 }
KDE Logo
This file is part of the documentation for kjs Library Version 3.3.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sun Jan 15 13:33:07 2006 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003