bool operator()(unsigned i, Value *src) { JS_ASSERT(dst->isUndefined()); if (!argsobj.isElementDeleted(i)) dst->set(compartment, *src); ++dst; return true; }
ArgumentsObject * ArgumentsObject::create(JSContext *cx, uint32_t argc, HandleObject callee) { JS_ASSERT(argc <= StackSpace::ARGS_LENGTH_MAX); RootedVarObject proto(cx, callee->global().getOrCreateObjectPrototype(cx)); if (!proto) return NULL; RootedVarTypeObject type(cx); type = proto->getNewType(cx); if (!type) return NULL; bool strict = callee->toFunction()->inStrictMode(); Class *clasp = strict ? &StrictArgumentsObjectClass : &NormalArgumentsObjectClass; RootedVarShape emptyArgumentsShape(cx); emptyArgumentsShape = EmptyShape::getInitialShape(cx, clasp, proto, proto->getParent(), FINALIZE_KIND, BaseShape::INDEXED); if (!emptyArgumentsShape) return NULL; unsigned numDeletedWords = NumWordsForBitArrayOfLength(argc); unsigned numBytes = offsetof(ArgumentsData, slots) + numDeletedWords * sizeof(size_t) + argc * sizeof(Value); ArgumentsData *data = (ArgumentsData *)cx->malloc_(numBytes); if (!data) return NULL; data->callee.init(ObjectValue(*callee)); for (HeapValue *vp = data->slots; vp != data->slots + argc; vp++) vp->init(UndefinedValue()); data->deletedBits = (size_t *)(data->slots + argc); ClearAllBitArrayElements(data->deletedBits, numDeletedWords); /* We have everything needed to fill in the object, so make the object. */ JSObject *obj = JSObject::create(cx, FINALIZE_KIND, emptyArgumentsShape, type, NULL); if (!obj) return NULL; ArgumentsObject &argsobj = obj->asArguments(); JS_ASSERT(UINT32_MAX > (uint64_t(argc) << PACKED_BITS_COUNT)); argsobj.initInitialLength(argc); argsobj.initData(data); argsobj.setStackFrame(NULL); JS_ASSERT(argsobj.numFixedSlots() >= NormalArgumentsObject::RESERVED_SLOTS); JS_ASSERT(argsobj.numFixedSlots() >= StrictArgumentsObject::RESERVED_SLOTS); return &argsobj; }
ArgumentsObject * ArgumentsObject::create(JSContext *cx, StackFrame *fp) { JSFunction &callee = fp->callee(); RootedObject proto(cx, callee.global().getOrCreateObjectPrototype(cx)); if (!proto) return NULL; RootedTypeObject type(cx); type = proto->getNewType(cx); if (!type) return NULL; bool strict = callee.inStrictMode(); Class *clasp = strict ? &StrictArgumentsObjectClass : &NormalArgumentsObjectClass; RootedShape emptyArgumentsShape(cx); emptyArgumentsShape = EmptyShape::getInitialShape(cx, clasp, proto, proto->getParent(), FINALIZE_KIND, BaseShape::INDEXED); if (!emptyArgumentsShape) return NULL; unsigned numActuals = fp->numActualArgs(); unsigned numFormals = fp->numFormalArgs(); unsigned numDeletedWords = NumWordsForBitArrayOfLength(numActuals); unsigned numArgs = Max(numActuals, numFormals); unsigned numBytes = offsetof(ArgumentsData, args) + numDeletedWords * sizeof(size_t) + numArgs * sizeof(Value); ArgumentsData *data = (ArgumentsData *)cx->malloc_(numBytes); if (!data) return NULL; data->numArgs = numArgs; data->callee.init(ObjectValue(callee)); data->script = fp->script(); /* Copy [0, numArgs) into data->slots. */ HeapValue *dst = data->args, *dstEnd = data->args + numArgs; for (Value *src = fp->formals(), *end = src + numFormals; src != end; ++src, ++dst) dst->init(*src); if (numActuals > numFormals) { for (Value *src = fp->actuals() + numFormals; dst != dstEnd; ++src, ++dst) dst->init(*src); } else if (numActuals < numFormals) { for (; dst != dstEnd; ++dst) dst->init(UndefinedValue()); } data->deletedBits = reinterpret_cast<size_t *>(dstEnd); ClearAllBitArrayElements(data->deletedBits, numDeletedWords); JSObject *obj = JSObject::create(cx, FINALIZE_KIND, emptyArgumentsShape, type, NULL); if (!obj) return NULL; obj->initFixedSlot(INITIAL_LENGTH_SLOT, Int32Value(numActuals << PACKED_BITS_COUNT)); obj->initFixedSlot(DATA_SLOT, PrivateValue(data)); /* * If it exists and the arguments object aliases formals, the call object * is the canonical location for formals. */ JSScript *script = fp->script(); if (fp->fun()->isHeavyweight() && script->argsObjAliasesFormals()) { obj->initFixedSlot(MAYBE_CALL_SLOT, ObjectValue(fp->callObj())); /* Flag each slot that canonically lives in the callObj. */ if (script->bindingsAccessedDynamically) { for (unsigned i = 0; i < numFormals; ++i) data->args[i] = MagicValue(JS_FORWARD_TO_CALL_OBJECT); } else { for (unsigned i = 0; i < script->numClosedArgs(); ++i) data->args[script->getClosedArg(i)] = MagicValue(JS_FORWARD_TO_CALL_OBJECT); } } ArgumentsObject &argsobj = obj->asArguments(); JS_ASSERT(argsobj.initialLength() == numActuals); JS_ASSERT(!argsobj.hasOverriddenLength()); return &argsobj; }