Changeset 29047 in webkit for trunk/JavaScriptCore/kjs/array_object.cpp
- Timestamp:
- Dec 30, 2007, 11:03:31 PM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/JavaScriptCore/kjs/array_object.cpp
r29046 r29047 67 67 // ECMA 15.4.4 68 68 ArrayPrototype::ArrayPrototype(ExecState*, ObjectPrototype* objProto) 69 69 : ArrayInstance(objProto, 0) 70 70 { 71 71 } … … 107 107 str += separator; 108 108 if (str.isNull()) { 109 JSObject *error = Error::create(exec, GeneralError, "Out of memory");109 JSObject* error = Error::create(exec, GeneralError, "Out of memory"); 110 110 exec->setException(error); 111 111 break; … … 119 119 120 120 if (str.isNull()) { 121 JSObject *error = Error::create(exec, GeneralError, "Out of memory");121 JSObject* error = Error::create(exec, GeneralError, "Out of memory"); 122 122 exec->setException(error); 123 123 } … … 149 149 str += separator; 150 150 if (str.isNull()) { 151 JSObject *error = Error::create(exec, GeneralError, "Out of memory");151 JSObject* error = Error::create(exec, GeneralError, "Out of memory"); 152 152 exec->setException(error); 153 153 break; … … 158 158 continue; 159 159 160 bool fallback = false;161 160 JSObject* o = element->toObject(exec); 162 161 JSValue* conversionFunction = o->get(exec, exec->propertyNames().toLocaleString); 163 if (conversionFunction->isObject() && static_cast<JSObject*>(conversionFunction)->implementsCall()) { 164 List args; 165 str += static_cast<JSObject*>(conversionFunction)->call(exec, o, args)->toString(exec); 166 } else { 167 // try toString() fallback 168 fallback = true; 169 } 170 171 if (fallback) 162 if (conversionFunction->isObject() && static_cast<JSObject*>(conversionFunction)->implementsCall()) 163 str += static_cast<JSObject*>(conversionFunction)->call(exec, o, List::empty())->toString(exec); 164 else 172 165 str += element->toString(exec); 173 166 174 167 if (str.isNull()) { 175 JSObject *error = Error::create(exec, GeneralError, "Out of memory");168 JSObject* error = Error::create(exec, GeneralError, "Out of memory"); 176 169 exec->setException(error); 177 170 } … … 203 196 str += separator; 204 197 if (str.isNull()) { 205 JSObject *error = Error::create(exec, GeneralError, "Out of memory");198 JSObject* error = Error::create(exec, GeneralError, "Out of memory"); 206 199 exec->setException(error); 207 200 break; … … 215 208 216 209 if (str.isNull()) { 217 JSObject *error = Error::create(exec, GeneralError, "Out of memory");210 JSObject* error = Error::create(exec, GeneralError, "Out of memory"); 218 211 exec->setException(error); 219 212 } … … 230 223 JSObject* arr = static_cast<JSObject*>(exec->lexicalGlobalObject()->arrayConstructor()->construct(exec, List::empty())); 231 224 int n = 0; 232 JSValue *curArg = thisObj;233 JSObject *curObj = static_cast<JSObject *>(thisObj);225 JSValue* curArg = thisObj; 226 JSObject* curObj = static_cast<JSObject* >(thisObj); 234 227 List::const_iterator it = args.begin(); 235 228 List::const_iterator end = args.end(); 236 229 unsigned length = thisObj->get(exec, exec->propertyNames().length)->toUInt32(exec); 237 for (;;) {230 while (1) { 238 231 if (curArg->isObject() && curObj->inherits(&ArrayInstance::info)) { 239 232 unsigned int k = 0; … … 242 235 length = curObj->get(exec, exec->propertyNames().length)->toUInt32(exec); 243 236 while (k < length) { 244 if (JSValue *v = getProperty(exec, curObj, k))237 if (JSValue* v = getProperty(exec, curObj, k)) 245 238 arr->put(exec, n, v); 246 239 n++; … … 292 285 for (unsigned int k = 0; k < middle; k++) { 293 286 unsigned lk1 = length - k - 1; 294 JSValue *obj2 = getProperty(exec, thisObj, lk1);295 JSValue *obj = getProperty(exec, thisObj, k);287 JSValue* obj2 = getProperty(exec, thisObj, lk1); 288 JSValue* obj = getProperty(exec, thisObj, k); 296 289 297 290 if (obj2) … … 319 312 result = thisObj->get(exec, 0); 320 313 for (unsigned int k = 1; k < length; k++) { 321 if (JSValue *obj = getProperty(exec, thisObj, k))322 thisObj->put(exec, k -1, obj);314 if (JSValue* obj = getProperty(exec, thisObj, k)) 315 thisObj->put(exec, k - 1, obj); 323 316 else 324 thisObj->deleteProperty(exec, k -1);317 thisObj->deleteProperty(exec, k - 1); 325 318 } 326 319 thisObj->deleteProperty(exec, length - 1); … … 335 328 336 329 // We return a new array 337 JSObject *resObj = static_cast<JSObject *>(exec->lexicalGlobalObject()->arrayConstructor()->construct(exec,List::empty()));330 JSObject* resObj = static_cast<JSObject* >(exec->lexicalGlobalObject()->arrayConstructor()->construct(exec, List::empty())); 338 331 JSValue* result = resObj; 339 332 double begin = args[0]->toInteger(exec); … … 366 359 int e = static_cast<int>(end); 367 360 for (int k = b; k < e; k++, n++) { 368 if (JSValue *v = getProperty(exec, thisObj, k))361 if (JSValue* v = getProperty(exec, thisObj, k)) 369 362 resObj->put(exec, n, v); 370 363 } … … 375 368 JSValue* ArrayProtoFuncSort::callAsFunction(ExecState* exec, JSObject* thisObj, const List& args) 376 369 { 377 #if 0 378 printf("KJS Array::Sort length=%d\n", length); 379 for (unsigned int i = 0 ; i<length ; ++i) 380 printf("KJS Array::Sort: %d: %s\n", i, thisObj->get(exec, i)->toString(exec).ascii()); 381 #endif 382 JSObject *sortFunction = NULL; 370 JSObject* sortFunction = 0; 383 371 if (!args[0]->isUndefined()) { 384 372 sortFunction = args[0]->toObject(exec); 385 373 if (!sortFunction->implementsCall()) 386 sortFunction = NULL;374 sortFunction = 0; 387 375 } 388 376 389 377 if (thisObj->classInfo() == &ArrayInstance::info) { 390 378 if (sortFunction) 391 ((ArrayInstance *)thisObj)->sort(exec, sortFunction);379 static_cast<ArrayInstance*>(thisObj)->sort(exec, sortFunction); 392 380 else 393 ((ArrayInstance *)thisObj)->sort(exec);381 static_cast<ArrayInstance*>(thisObj)->sort(exec); 394 382 return thisObj; 395 383 } … … 397 385 unsigned length = thisObj->get(exec, exec->propertyNames().length)->toUInt32(exec); 398 386 399 if ( length == 0) {387 if (!length) { 400 388 thisObj->put(exec, exec->propertyNames().length, jsNumber(0), DontEnum | DontDelete); 401 389 return thisObj; … … 404 392 // "Min" sort. Not the fastest, but definitely less code than heapsort 405 393 // or quicksort, and much less swapping than bubblesort/insertionsort. 406 for (unsigned int i = 0 ; i <length-1 ; ++i) {407 JSValue *iObj = thisObj->get(exec,i);394 for (unsigned int i = 0 ; i < length - 1 ; ++i) { 395 JSValue* iObj = thisObj->get(exec, i); 408 396 unsigned int themin = i; 409 JSValue *minObj = iObj;410 for (unsigned int j = i +1 ; j<length ; ++j) {411 JSValue *jObj = thisObj->get(exec,j);412 double c mp;413 if (jObj->isUndefined()) {414 c mp= 1; // don't check minObj because there's no need to differentiate == (0) from > (1)415 } else if (minObj->isUndefined()) {416 c mp= -1;417 }else if (sortFunction) {397 JSValue* minObj = iObj; 398 for (unsigned int j = i + 1 ; j < length ; ++j) { 399 JSValue* jObj = thisObj->get(exec, j); 400 double compareResult; 401 if (jObj->isUndefined()) 402 compareResult = 1; // don't check minObj because there's no need to differentiate == (0) from > (1) 403 else if (minObj->isUndefined()) 404 compareResult = -1; 405 else if (sortFunction) { 418 406 List l; 419 407 l.append(jObj); 420 408 l.append(minObj); 421 c mp= sortFunction->call(exec, exec->dynamicGlobalObject(), l)->toNumber(exec);422 } else {423 c mp= (jObj->toString(exec) < minObj->toString(exec)) ? -1 : 1;424 } 425 if (c mp< 0) {409 compareResult = sortFunction->call(exec, exec->dynamicGlobalObject(), l)->toNumber(exec); 410 } else 411 compareResult = (jObj->toString(exec) < minObj->toString(exec)) ? -1 : 1; 412 413 if (compareResult < 0) { 426 414 themin = j; 427 415 minObj = jObj; … … 430 418 // Swap themin and i 431 419 if (themin > i) { 432 //printf("KJS Array::Sort: swapping %d and %d\n", i, themin);433 420 thisObj->put(exec, i, minObj); 434 421 thisObj->put(exec, themin, iObj); 435 422 } 436 423 } 437 #if 0438 printf("KJS Array::Sort -- Resulting array:\n");439 for (unsigned int i = 0 ; i<length ; ++i)440 printf("KJS Array::Sort: %d: %s\n", i, thisObj->get(exec, i)->toString(exec).ascii());441 #endif442 424 return thisObj; 443 425 } … … 446 428 { 447 429 // 15.4.4.12 448 JSObject *resObj = static_cast<JSObject *>(exec->lexicalGlobalObject()->arrayConstructor()->construct(exec, List::empty()));430 JSObject* resObj = static_cast<JSObject* >(exec->lexicalGlobalObject()->arrayConstructor()->construct(exec, List::empty())); 449 431 JSValue* result = resObj; 450 432 unsigned length = thisObj->get(exec, exec->propertyNames().length)->toUInt32(exec); … … 457 439 458 440 for (unsigned int k = 0; k < deleteCount; k++) { 459 if (JSValue *v = getProperty(exec, thisObj, k+begin))441 if (JSValue* v = getProperty(exec, thisObj, k + begin)) 460 442 resObj->put(exec, k, v); 461 443 } … … 466 448 if (additionalArgs < deleteCount) { 467 449 for (unsigned int k = begin; k < length - deleteCount; ++k) { 468 if (JSValue *v = getProperty(exec, thisObj, k+deleteCount))469 thisObj->put(exec, k +additionalArgs, v);450 if (JSValue* v = getProperty(exec, thisObj, k + deleteCount)) 451 thisObj->put(exec, k + additionalArgs, v); 470 452 else 471 thisObj->deleteProperty(exec, k +additionalArgs);453 thisObj->deleteProperty(exec, k + additionalArgs); 472 454 } 473 455 for (unsigned int k = length ; k > length - deleteCount + additionalArgs; --k) 474 thisObj->deleteProperty(exec, k -1);456 thisObj->deleteProperty(exec, k - 1); 475 457 } else { 476 458 for (unsigned int k = length - deleteCount; (int)k > begin; --k) { 477 if (JSValue *obj = getProperty(exec, thisObj, k + deleteCount - 1))459 if (JSValue* obj = getProperty(exec, thisObj, k + deleteCount - 1)) 478 460 thisObj->put(exec, k + additionalArgs - 1, obj); 479 461 else 480 thisObj->deleteProperty(exec, k +additionalArgs-1);462 thisObj->deleteProperty(exec, k + additionalArgs - 1); 481 463 } 482 464 } 483 465 } 484 for (unsigned int k = 0; k < additionalArgs; ++k) {485 thisObj->put(exec, k +begin, args[k+2]);486 } 466 for (unsigned int k = 0; k < additionalArgs; ++k) 467 thisObj->put(exec, k + begin, args[k + 2]); 468 487 469 thisObj->put(exec, exec->propertyNames().length, jsNumber(length - deleteCount + additionalArgs), DontEnum | DontDelete); 488 470 return result; … … 495 477 unsigned int nrArgs = args.size(); 496 478 for (unsigned int k = length; k > 0; --k) { 497 if (JSValue *v = getProperty(exec, thisObj, k - 1))498 thisObj->put(exec, k +nrArgs-1, v);479 if (JSValue* v = getProperty(exec, thisObj, k - 1)) 480 thisObj->put(exec, k + nrArgs-1, v); 499 481 else 500 thisObj->deleteProperty(exec, k +nrArgs-1);482 thisObj->deleteProperty(exec, k + nrArgs-1); 501 483 } 502 484 for (unsigned int k = 0; k < nrArgs; ++k) … … 514 496 return throwError(exec, TypeError); 515 497 516 JSObject *applyThis = args[1]->isUndefinedOrNull() ? exec->dynamicGlobalObject() : args[1]->toObject(exec);517 JSObject *resultArray = static_cast<JSObject*>(exec->lexicalGlobalObject()->arrayConstructor()->construct(exec, List::empty()));498 JSObject* applyThis = args[1]->isUndefinedOrNull() ? exec->dynamicGlobalObject() : args[1]->toObject(exec); 499 JSObject* resultArray = static_cast<JSObject*>(exec->lexicalGlobalObject()->arrayConstructor()->construct(exec, List::empty())); 518 500 519 501 unsigned filterIndex = 0; … … 525 507 continue; 526 508 527 JSValue *v = slot.getValue(exec, thisObj, k);509 JSValue* v = slot.getValue(exec, thisObj, k); 528 510 529 511 List eachArguments; … … 533 515 eachArguments.append(thisObj); 534 516 535 JSValue *result = eachFunction->call(exec, applyThis, eachArguments);517 JSValue* result = eachFunction->call(exec, applyThis, eachArguments); 536 518 537 519 if (result->toBoolean(exec)) … … 547 529 return throwError(exec, TypeError); 548 530 549 JSObject *applyThis = args[1]->isUndefinedOrNull() ? exec->dynamicGlobalObject() : args[1]->toObject(exec);531 JSObject* applyThis = args[1]->isUndefinedOrNull() ? exec->dynamicGlobalObject() : args[1]->toObject(exec); 550 532 551 533 unsigned length = thisObj->get(exec, exec->propertyNames().length)->toUInt32(exec); … … 560 542 continue; 561 543 562 JSValue *v = slot.getValue(exec, thisObj, k);544 JSValue* v = slot.getValue(exec, thisObj, k); 563 545 564 546 List eachArguments; … … 568 550 eachArguments.append(thisObj); 569 551 570 JSValue *result = eachFunction->call(exec, applyThis, eachArguments);552 JSValue* result = eachFunction->call(exec, applyThis, eachArguments); 571 553 resultArray->put(exec, k, result); 572 554 } … … 582 564 JSValue* ArrayProtoFuncEvery::callAsFunction(ExecState* exec, JSObject* thisObj, const List& args) 583 565 { 584 JSObject *eachFunction = args[0]->toObject(exec);566 JSObject* eachFunction = args[0]->toObject(exec); 585 567 586 568 if (!eachFunction->implementsCall()) 587 569 return throwError(exec, TypeError); 588 570 589 JSObject *applyThis = args[1]->isUndefinedOrNull() ? exec->dynamicGlobalObject() : args[1]->toObject(exec);571 JSObject* applyThis = args[1]->isUndefinedOrNull() ? exec->dynamicGlobalObject() : args[1]->toObject(exec); 590 572 591 573 JSValue* result = jsBoolean(true); … … 732 714 // ------------------------------ ArrayObjectImp ------------------------------- 733 715 734 ArrayObjectImp::ArrayObjectImp(ExecState *exec,735 FunctionPrototype *funcProto,736 ArrayPrototype *arrayProto)716 ArrayObjectImp::ArrayObjectImp(ExecState* exec, 717 FunctionPrototype* funcProto, 718 ArrayPrototype* arrayProto) 737 719 : InternalFunctionImp(funcProto) 738 720 { … … 750 732 751 733 // ECMA 15.4.2 752 JSObject *ArrayObjectImp::construct(ExecState *exec, const List &args)734 JSObject* ArrayObjectImp::construct(ExecState* exec, const List& args) 753 735 { 754 736 // a single numeric argument denotes the array size (!) … … 765 747 766 748 // ECMA 15.6.1 767 JSValue *ArrayObjectImp::callAsFunction(ExecState *exec, JSObject *, const List &args)749 JSValue* ArrayObjectImp::callAsFunction(ExecState* exec, JSObject*, const List& args) 768 750 { 769 751 // equivalent to 'new Array(....)'
Note:
See TracChangeset
for help on using the changeset viewer.