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