Bug 540675: eviscerate JSStackFrame::callerVersion. (r=lw)
[mozilla-central.git] / js / src / jsfun.cpp
blob33751dc1296bc4def77cd7e3608fb720f4150b92
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 * vim: set ts=8 sw=4 et tw=99:
4 * ***** BEGIN LICENSE BLOCK *****
5 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
7 * The contents of this file are subject to the Mozilla Public License Version
8 * 1.1 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://www.mozilla.org/MPL/
12 * Software distributed under the License is distributed on an "AS IS" basis,
13 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14 * for the specific language governing rights and limitations under the
15 * License.
17 * The Original Code is Mozilla Communicator client code, released
18 * March 31, 1998.
20 * The Initial Developer of the Original Code is
21 * Netscape Communications Corporation.
22 * Portions created by the Initial Developer are Copyright (C) 1998
23 * the Initial Developer. All Rights Reserved.
25 * Contributor(s):
27 * Alternatively, the contents of this file may be used under the terms of
28 * either of the GNU General Public License Version 2 or later (the "GPL"),
29 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 * in which case the provisions of the GPL or the LGPL are applicable instead
31 * of those above. If you wish to allow use of your version of this file only
32 * under the terms of either the GPL or the LGPL, and not to allow others to
33 * use your version of this file under the terms of the MPL, indicate your
34 * decision by deleting the provisions above and replace them with the notice
35 * and other provisions required by the GPL or the LGPL. If you do not delete
36 * the provisions above, a recipient may use your version of this file under
37 * the terms of any one of the MPL, the GPL or the LGPL.
39 * ***** END LICENSE BLOCK ***** */
42 * JS function support.
44 #include <string.h>
45 #include "jstypes.h"
46 #include "jsstdint.h"
47 #include "jsbit.h"
48 #include "jsutil.h" /* Added by JSIFY */
49 #include "jsapi.h"
50 #include "jsarray.h"
51 #include "jsatom.h"
52 #include "jsbool.h"
53 #include "jsbuiltins.h"
54 #include "jscntxt.h"
55 #include "jsversion.h"
56 #include "jsdbgapi.h"
57 #include "jsemit.h"
58 #include "jsfun.h"
59 #include "jsgc.h"
60 #include "jsinterp.h"
61 #include "jslock.h"
62 #include "jsnum.h"
63 #include "jsobj.h"
64 #include "jsopcode.h"
65 #include "jsparse.h"
66 #include "jspropertytree.h"
67 #include "jsproxy.h"
68 #include "jsscan.h"
69 #include "jsscope.h"
70 #include "jsscript.h"
71 #include "jsstr.h"
72 #include "jsexn.h"
73 #include "jsstaticcheck.h"
74 #include "jstracer.h"
76 #if JS_HAS_GENERATORS
77 # include "jsiter.h"
78 #endif
80 #if JS_HAS_XDR
81 # include "jsxdrapi.h"
82 #endif
84 #ifdef JS_METHODJIT
85 #include "methodjit/MethodJIT.h"
86 #endif
88 #include "jsatominlines.h"
89 #include "jscntxtinlines.h"
90 #include "jsfuninlines.h"
91 #include "jsinterpinlines.h"
92 #include "jsobjinlines.h"
94 using namespace js;
96 inline JSObject *
97 JSObject::getThrowTypeError() const
99 return &getGlobal()->getReservedSlot(JSRESERVED_GLOBAL_THROWTYPEERROR).toObject();
102 JSBool
103 js_GetArgsValue(JSContext *cx, JSStackFrame *fp, Value *vp)
105 JSObject *argsobj;
107 if (fp->hasOverriddenArgs()) {
108 JS_ASSERT(fp->hasCallObj());
109 jsid id = ATOM_TO_JSID(cx->runtime->atomState.argumentsAtom);
110 return fp->callObj().getProperty(cx, id, vp);
112 argsobj = js_GetArgsObject(cx, fp);
113 if (!argsobj)
114 return JS_FALSE;
115 vp->setObject(*argsobj);
116 return JS_TRUE;
119 JSBool
120 js_GetArgsProperty(JSContext *cx, JSStackFrame *fp, jsid id, Value *vp)
122 JS_ASSERT(fp->isFunctionFrame());
124 if (fp->hasOverriddenArgs()) {
125 JS_ASSERT(fp->hasCallObj());
127 jsid argumentsid = ATOM_TO_JSID(cx->runtime->atomState.argumentsAtom);
128 Value v;
129 if (!fp->callObj().getProperty(cx, argumentsid, &v))
130 return false;
132 JSObject *obj;
133 if (v.isPrimitive()) {
134 obj = js_ValueToNonNullObject(cx, v);
135 if (!obj)
136 return false;
137 } else {
138 obj = &v.toObject();
140 return obj->getProperty(cx, id, vp);
143 vp->setUndefined();
144 if (JSID_IS_INT(id)) {
145 uint32 arg = uint32(JSID_TO_INT(id));
146 JSObject *argsobj = fp->maybeArgsObj();
147 if (arg < fp->numActualArgs()) {
148 if (argsobj) {
149 if (argsobj->getArgsElement(arg).isMagic(JS_ARGS_HOLE))
150 return argsobj->getProperty(cx, id, vp);
152 *vp = fp->canonicalActualArg(arg);
153 } else {
155 * Per ECMA-262 Ed. 3, 10.1.8, last bulleted item, do not share
156 * storage between the formal parameter and arguments[k] for all
157 * fp->argc <= k && k < fp->fun->nargs. For example, in
159 * function f(x) { x = 42; return arguments[0]; }
160 * f();
162 * the call to f should return undefined, not 42. If fp->argsobj
163 * is null at this point, as it would be in the example, return
164 * undefined in *vp.
166 if (argsobj)
167 return argsobj->getProperty(cx, id, vp);
169 } else if (JSID_IS_ATOM(id, cx->runtime->atomState.lengthAtom)) {
170 JSObject *argsobj = fp->maybeArgsObj();
171 if (argsobj && argsobj->isArgsLengthOverridden())
172 return argsobj->getProperty(cx, id, vp);
173 vp->setInt32(fp->numActualArgs());
175 return true;
178 static JSObject *
179 NewArguments(JSContext *cx, JSObject *parent, uint32 argc, JSObject &callee)
181 JSObject *proto;
182 if (!js_GetClassPrototype(cx, parent, JSProto_Object, &proto))
183 return NULL;
185 JSObject *argsobj = js_NewGCObject(cx);
186 if (!argsobj)
187 return NULL;
189 ArgumentsData *data = (ArgumentsData *)
190 cx->malloc(offsetof(ArgumentsData, slots) + argc * sizeof(Value));
191 if (!data)
192 return NULL;
193 SetValueRangeToUndefined(data->slots, argc);
195 /* Can't fail from here on, so initialize everything in argsobj. */
196 argsobj->init(callee.getFunctionPrivate()->inStrictMode()
197 ? &StrictArgumentsClass
198 : &js_ArgumentsClass,
199 proto, parent, NULL, cx);
201 argsobj->setMap(cx->runtime->emptyArgumentsShape);
203 argsobj->setArgsLength(argc);
204 argsobj->setArgsData(data);
205 data->callee.setObject(callee);
207 return argsobj;
210 namespace {
212 struct PutArg
214 PutArg(Value *dst) : dst(dst) {}
215 Value *dst;
216 void operator()(uintN, Value *src) {
217 if (!dst->isMagic(JS_ARGS_HOLE))
218 *dst = *src;
219 ++dst;
225 JSObject *
226 js_GetArgsObject(JSContext *cx, JSStackFrame *fp)
229 * We must be in a function activation; the function must be lightweight
230 * or else fp must have a variable object.
232 JS_ASSERT_IF(fp->fun()->isHeavyweight(), fp->hasCallObj());
234 while (fp->isEvalOrDebuggerFrame())
235 fp = fp->prev();
237 /* Create an arguments object for fp only if it lacks one. */
238 if (fp->hasArgsObj())
239 return &fp->argsObj();
241 /* Compute the arguments object's parent slot from fp's scope chain. */
242 JSObject *global = fp->scopeChain().getGlobal();
243 JSObject *argsobj = NewArguments(cx, global, fp->numActualArgs(), fp->callee());
244 if (!argsobj)
245 return argsobj;
248 * Strict mode functions have arguments objects that copy the initial
249 * actual parameter values. It is the caller's responsibility to get the
250 * arguments object before any parameters are modified! (The emitter
251 * ensures this by synthesizing an arguments access at the start of any
252 * strict mode function that contains an assignment to a parameter, or
253 * that calls eval.) Non-strict mode arguments use the frame pointer to
254 * retrieve up-to-date parameter values.
256 if (argsobj->isStrictArguments())
257 fp->forEachCanonicalActualArg(PutArg(argsobj->getArgsData()->slots));
258 else
259 argsobj->setPrivate(fp);
261 fp->setArgsObj(*argsobj);
262 return argsobj;
265 void
266 js_PutArgsObject(JSContext *cx, JSStackFrame *fp)
268 JSObject &argsobj = fp->argsObj();
269 if (argsobj.isNormalArguments()) {
270 JS_ASSERT(argsobj.getPrivate() == fp);
271 fp->forEachCanonicalActualArg(PutArg(argsobj.getArgsData()->slots));
272 argsobj.setPrivate(NULL);
273 } else {
274 JS_ASSERT(!argsobj.getPrivate());
278 #ifdef JS_TRACER
281 * Traced versions of js_GetArgsObject and js_PutArgsObject.
283 JSObject * JS_FASTCALL
284 js_NewArgumentsOnTrace(JSContext *cx, JSObject *parent, uint32 argc, JSObject *callee)
286 JSObject *argsobj = NewArguments(cx, parent, argc, *callee);
287 if (!argsobj)
288 return NULL;
290 if (argsobj->isStrictArguments()) {
292 * Strict mode callers must copy arguments into the created arguments
293 * object. The trace-JITting code is in TraceRecorder::newArguments.
295 JS_ASSERT(!argsobj->getPrivate());
296 } else {
297 argsobj->setPrivate(JS_ARGUMENTS_OBJECT_ON_TRACE);
300 return argsobj;
302 JS_DEFINE_CALLINFO_4(extern, OBJECT, js_NewArgumentsOnTrace, CONTEXT, OBJECT, UINT32, OBJECT,
303 0, nanojit::ACCSET_STORE_ANY)
305 /* FIXME change the return type to void. */
306 JSBool JS_FASTCALL
307 js_PutArgumentsOnTrace(JSContext *cx, JSObject *argsobj, Value *args)
309 JS_ASSERT(argsobj->isNormalArguments());
310 JS_ASSERT(argsobj->getPrivate() == JS_ARGUMENTS_OBJECT_ON_TRACE);
313 * TraceRecorder::putActivationObjects builds a single, contiguous array of
314 * the arguments, regardless of whether #actuals > #formals so there is no
315 * need to worry about actual vs. formal arguments.
317 Value *srcend = args + argsobj->getArgsInitialLength();
318 Value *dst = argsobj->getArgsData()->slots;
319 for (Value *src = args; src != srcend; ++src, ++dst) {
320 if (!dst->isMagic(JS_ARGS_HOLE))
321 *dst = *src;
324 argsobj->setPrivate(NULL);
325 return true;
327 JS_DEFINE_CALLINFO_3(extern, BOOL, js_PutArgumentsOnTrace, CONTEXT, OBJECT, VALUEPTR, 0,
328 nanojit::ACCSET_STORE_ANY)
330 #endif /* JS_TRACER */
332 static JSBool
333 args_delProperty(JSContext *cx, JSObject *obj, jsid id, Value *vp)
335 JS_ASSERT(obj->isArguments());
337 if (JSID_IS_INT(id)) {
338 uintN arg = uintN(JSID_TO_INT(id));
339 if (arg < obj->getArgsInitialLength())
340 obj->setArgsElement(arg, MagicValue(JS_ARGS_HOLE));
341 } else if (JSID_IS_ATOM(id, cx->runtime->atomState.lengthAtom)) {
342 obj->setArgsLengthOverridden();
343 } else if (JSID_IS_ATOM(id, cx->runtime->atomState.calleeAtom)) {
344 obj->setArgsCallee(MagicValue(JS_ARGS_HOLE));
346 return true;
349 static JS_REQUIRES_STACK JSObject *
350 WrapEscapingClosure(JSContext *cx, JSStackFrame *fp, JSFunction *fun)
352 JS_ASSERT(fun->optimizedClosure());
353 JS_ASSERT(!fun->u.i.wrapper);
356 * We do not attempt to reify Call and Block objects on demand for outer
357 * scopes. This could be done (see the "v8" patch in bug 494235) but it is
358 * fragile in the face of ongoing compile-time optimization. Instead, the
359 * _DBG* opcodes used by wrappers created here must cope with unresolved
360 * upvars and throw them as reference errors. Caveat debuggers!
362 JSObject *scopeChain = js_GetScopeChain(cx, fp);
363 if (!scopeChain)
364 return NULL;
366 JSObject *wfunobj = NewFunction(cx, scopeChain);
367 if (!wfunobj)
368 return NULL;
369 AutoObjectRooter tvr(cx, wfunobj);
371 JSFunction *wfun = (JSFunction *) wfunobj;
372 wfunobj->setPrivate(wfun);
373 wfun->nargs = fun->nargs;
374 wfun->flags = fun->flags | JSFUN_HEAVYWEIGHT;
375 wfun->u.i.nvars = fun->u.i.nvars;
376 wfun->u.i.nupvars = fun->u.i.nupvars;
377 wfun->u.i.skipmin = fun->u.i.skipmin;
378 wfun->u.i.wrapper = true;
379 wfun->u.i.script = NULL;
380 wfun->u.i.names = fun->u.i.names;
381 wfun->atom = fun->atom;
383 JSScript *script = fun->u.i.script;
384 jssrcnote *snbase = script->notes();
385 jssrcnote *sn = snbase;
386 while (!SN_IS_TERMINATOR(sn))
387 sn = SN_NEXT(sn);
388 uintN nsrcnotes = (sn - snbase) + 1;
390 /* NB: GC must not occur before wscript is homed in wfun->u.i.script. */
391 JSScript *wscript = js_NewScript(cx, script->length, nsrcnotes,
392 script->atomMap.length,
393 (script->objectsOffset != 0)
394 ? script->objects()->length
395 : 0,
396 fun->u.i.nupvars,
397 (script->regexpsOffset != 0)
398 ? script->regexps()->length
399 : 0,
400 (script->trynotesOffset != 0)
401 ? script->trynotes()->length
402 : 0,
403 (script->constOffset != 0)
404 ? script->consts()->length
405 : 0,
406 (script->globalsOffset != 0)
407 ? script->globals()->length
408 : 0);
409 if (!wscript)
410 return NULL;
412 memcpy(wscript->code, script->code, script->length);
413 wscript->main = wscript->code + (script->main - script->code);
415 memcpy(wscript->notes(), snbase, nsrcnotes * sizeof(jssrcnote));
416 memcpy(wscript->atomMap.vector, script->atomMap.vector,
417 wscript->atomMap.length * sizeof(JSAtom *));
418 if (script->objectsOffset != 0) {
419 memcpy(wscript->objects()->vector, script->objects()->vector,
420 wscript->objects()->length * sizeof(JSObject *));
422 if (script->regexpsOffset != 0) {
423 memcpy(wscript->regexps()->vector, script->regexps()->vector,
424 wscript->regexps()->length * sizeof(JSObject *));
426 if (script->trynotesOffset != 0) {
427 memcpy(wscript->trynotes()->vector, script->trynotes()->vector,
428 wscript->trynotes()->length * sizeof(JSTryNote));
430 if (script->globalsOffset != 0) {
431 memcpy(wscript->globals()->vector, script->globals()->vector,
432 wscript->globals()->length * sizeof(GlobalSlotArray::Entry));
435 if (wfun->u.i.nupvars != 0) {
436 JS_ASSERT(wfun->u.i.nupvars == wscript->upvars()->length);
437 memcpy(wscript->upvars()->vector, script->upvars()->vector,
438 wfun->u.i.nupvars * sizeof(uint32));
441 jsbytecode *pc = wscript->code;
442 while (*pc != JSOP_STOP) {
443 /* FIXME should copy JSOP_TRAP? */
444 JSOp op = js_GetOpcode(cx, wscript, pc);
445 const JSCodeSpec *cs = &js_CodeSpec[op];
446 ptrdiff_t oplen = cs->length;
447 if (oplen < 0)
448 oplen = js_GetVariableBytecodeLength(pc);
451 * Rewrite JSOP_{GET,CALL}FCSLOT as JSOP_{GET,CALL}UPVAR_DBG for the
452 * case where fun is an escaping flat closure. This works because the
453 * UPVAR and FCSLOT ops by design have the same format: an upvar index
454 * immediate operand.
456 switch (op) {
457 case JSOP_GETUPVAR: *pc = JSOP_GETUPVAR_DBG; break;
458 case JSOP_CALLUPVAR: *pc = JSOP_CALLUPVAR_DBG; break;
459 case JSOP_GETFCSLOT: *pc = JSOP_GETUPVAR_DBG; break;
460 case JSOP_CALLFCSLOT: *pc = JSOP_CALLUPVAR_DBG; break;
461 case JSOP_DEFFUN_FC: *pc = JSOP_DEFFUN_DBGFC; break;
462 case JSOP_DEFLOCALFUN_FC: *pc = JSOP_DEFLOCALFUN_DBGFC; break;
463 case JSOP_LAMBDA_FC: *pc = JSOP_LAMBDA_DBGFC; break;
464 default:;
466 pc += oplen;
470 * Fill in the rest of wscript. This means if you add members to JSScript
471 * you must update this code. FIXME: factor into JSScript::clone method.
473 wscript->noScriptRval = script->noScriptRval;
474 wscript->savedCallerFun = script->savedCallerFun;
475 wscript->hasSharps = script->hasSharps;
476 wscript->strictModeCode = script->strictModeCode;
477 wscript->setVersion(script->getVersion());
478 wscript->nfixed = script->nfixed;
479 wscript->filename = script->filename;
480 wscript->lineno = script->lineno;
481 wscript->nslots = script->nslots;
482 wscript->staticLevel = script->staticLevel;
483 wscript->principals = script->principals;
484 if (wscript->principals)
485 JSPRINCIPALS_HOLD(cx, wscript->principals);
486 #ifdef CHECK_SCRIPT_OWNER
487 wscript->owner = script->owner;
488 #endif
490 /* Deoptimize wfun from FUN_{FLAT,NULL}_CLOSURE to FUN_INTERPRETED. */
491 FUN_SET_KIND(wfun, JSFUN_INTERPRETED);
492 wfun->u.i.script = wscript;
493 return wfunobj;
496 static JSBool
497 ArgGetter(JSContext *cx, JSObject *obj, jsid id, Value *vp)
499 LeaveTrace(cx);
501 if (!InstanceOf(cx, obj, &js_ArgumentsClass, NULL))
502 return true;
504 if (JSID_IS_INT(id)) {
506 * arg can exceed the number of arguments if a script changed the
507 * prototype to point to another Arguments object with a bigger argc.
509 uintN arg = uintN(JSID_TO_INT(id));
510 if (arg < obj->getArgsInitialLength()) {
511 JSStackFrame *fp = (JSStackFrame *) obj->getPrivate();
512 if (fp) {
513 JS_ASSERT(fp->numActualArgs() == obj->getArgsInitialLength());
514 *vp = fp->canonicalActualArg(arg);
515 } else {
516 const Value &v = obj->getArgsElement(arg);
517 if (!v.isMagic(JS_ARGS_HOLE))
518 *vp = v;
521 } else if (JSID_IS_ATOM(id, cx->runtime->atomState.lengthAtom)) {
522 if (!obj->isArgsLengthOverridden())
523 vp->setInt32(obj->getArgsInitialLength());
524 } else {
525 JS_ASSERT(JSID_IS_ATOM(id, cx->runtime->atomState.calleeAtom));
526 const Value &v = obj->getArgsCallee();
527 if (!v.isMagic(JS_ARGS_HOLE)) {
529 * If this function or one in it needs upvars that reach above it
530 * in the scope chain, it must not be a null closure (it could be a
531 * flat closure, or an unoptimized closure -- the latter itself not
532 * necessarily heavyweight). Rather than wrap here, we simply throw
533 * to reduce code size and tell debugger users the truth instead of
534 * passing off a fibbing wrapper.
536 if (GET_FUNCTION_PRIVATE(cx, &v.toObject())->needsWrapper()) {
537 JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
538 JSMSG_OPTIMIZED_CLOSURE_LEAK);
539 return false;
541 *vp = v;
544 return true;
547 static JSBool
548 ArgSetter(JSContext *cx, JSObject *obj, jsid id, Value *vp)
550 #ifdef JS_TRACER
551 // To be able to set a property here on trace, we would have to make
552 // sure any updates also get written back to the trace native stack.
553 // For simplicity, we just leave trace, since this is presumably not
554 // a common operation.
555 if (JS_ON_TRACE(cx)) {
556 DeepBail(cx);
557 return false;
559 #endif
561 if (!InstanceOf(cx, obj, &js_ArgumentsClass, NULL))
562 return true;
564 if (JSID_IS_INT(id)) {
565 uintN arg = uintN(JSID_TO_INT(id));
566 if (arg < obj->getArgsInitialLength()) {
567 JSStackFrame *fp = (JSStackFrame *) obj->getPrivate();
568 if (fp) {
569 fp->canonicalActualArg(arg) = *vp;
570 return true;
573 } else {
574 JS_ASSERT(JSID_IS_ATOM(id, cx->runtime->atomState.lengthAtom) ||
575 JSID_IS_ATOM(id, cx->runtime->atomState.calleeAtom));
579 * For simplicity we use delete/set to replace the property with one
580 * backed by the default Object getter and setter. Note that we rely on
581 * args_delProperty to clear the corresponding reserved slot so the GC can
582 * collect its value.
584 AutoValueRooter tvr(cx);
585 return js_DeleteProperty(cx, obj, id, tvr.addr()) &&
586 js_SetProperty(cx, obj, id, vp);
589 static JSBool
590 args_resolve(JSContext *cx, JSObject *obj, jsid id, uintN flags,
591 JSObject **objp)
593 JS_ASSERT(obj->isNormalArguments());
595 *objp = NULL;
596 bool valid = false;
597 uintN attrs = JSPROP_SHARED;
598 if (JSID_IS_INT(id)) {
599 uint32 arg = uint32(JSID_TO_INT(id));
600 attrs = JSPROP_ENUMERATE | JSPROP_SHARED;
601 if (arg < obj->getArgsInitialLength() && !obj->getArgsElement(arg).isMagic(JS_ARGS_HOLE))
602 valid = true;
603 } else if (JSID_IS_ATOM(id, cx->runtime->atomState.lengthAtom)) {
604 if (!obj->isArgsLengthOverridden())
605 valid = true;
606 } else if (JSID_IS_ATOM(id, cx->runtime->atomState.calleeAtom)) {
607 if (!obj->getArgsCallee().isMagic(JS_ARGS_HOLE))
608 valid = true;
611 if (valid) {
612 Value tmp = UndefinedValue();
613 if (!js_DefineProperty(cx, obj, id, &tmp, ArgGetter, ArgSetter, attrs))
614 return JS_FALSE;
615 *objp = obj;
617 return true;
620 static JSBool
621 args_enumerate(JSContext *cx, JSObject *obj)
623 JS_ASSERT(obj->isNormalArguments());
626 * Trigger reflection in args_resolve using a series of js_LookupProperty
627 * calls.
629 int argc = int(obj->getArgsInitialLength());
630 for (int i = -2; i != argc; i++) {
631 jsid id = (i == -2)
632 ? ATOM_TO_JSID(cx->runtime->atomState.lengthAtom)
633 : (i == -1)
634 ? ATOM_TO_JSID(cx->runtime->atomState.calleeAtom)
635 : INT_TO_JSID(i);
637 JSObject *pobj;
638 JSProperty *prop;
639 if (!js_LookupProperty(cx, obj, id, &pobj, &prop))
640 return false;
642 /* prop is null when the property was deleted. */
643 if (prop)
644 pobj->dropProperty(cx, prop);
646 return true;
649 namespace {
651 JSBool
652 StrictArgGetter(JSContext *cx, JSObject *obj, jsid id, Value *vp)
654 LeaveTrace(cx);
656 if (!InstanceOf(cx, obj, &StrictArgumentsClass, NULL))
657 return true;
659 if (JSID_IS_INT(id)) {
661 * arg can exceed the number of arguments if a script changed the
662 * prototype to point to another Arguments object with a bigger argc.
664 uintN arg = uintN(JSID_TO_INT(id));
665 if (arg < obj->getArgsInitialLength()) {
666 const Value &v = obj->getArgsElement(arg);
667 if (!v.isMagic(JS_ARGS_HOLE))
668 *vp = v;
670 } else {
671 JS_ASSERT(JSID_IS_ATOM(id, cx->runtime->atomState.lengthAtom));
672 if (!obj->isArgsLengthOverridden())
673 vp->setInt32(obj->getArgsInitialLength());
675 return true;
678 JSBool
679 StrictArgSetter(JSContext *cx, JSObject *obj, jsid id, Value *vp)
681 if (!InstanceOf(cx, obj, &StrictArgumentsClass, NULL))
682 return true;
684 if (JSID_IS_INT(id)) {
685 uintN arg = uintN(JSID_TO_INT(id));
686 if (arg < obj->getArgsInitialLength()) {
687 obj->setArgsElement(arg, *vp);
688 return true;
690 } else {
691 JS_ASSERT(JSID_IS_ATOM(id, cx->runtime->atomState.lengthAtom));
695 * For simplicity we use delete/set to replace the property with one
696 * backed by the default Object getter and setter. Note that we rely on
697 * args_delProperty to clear the corresponding reserved slot so the GC can
698 * collect its value.
700 AutoValueRooter tvr(cx);
701 return js_DeleteProperty(cx, obj, id, tvr.addr()) &&
702 js_SetProperty(cx, obj, id, vp);
705 JSBool
706 strictargs_resolve(JSContext *cx, JSObject *obj, jsid id, uintN flags, JSObject **objp)
708 JS_ASSERT(obj->isStrictArguments());
710 *objp = NULL;
711 bool valid = false;
712 uintN attrs = JSPROP_SHARED;
713 if (JSID_IS_INT(id)) {
714 uint32 arg = uint32(JSID_TO_INT(id));
715 attrs = JSPROP_SHARED | JSPROP_ENUMERATE;
716 if (arg < obj->getArgsInitialLength() && !obj->getArgsElement(arg).isMagic(JS_ARGS_HOLE))
717 valid = true;
718 } else if (JSID_IS_ATOM(id, cx->runtime->atomState.lengthAtom)) {
719 if (!obj->isArgsLengthOverridden())
720 valid = true;
721 } else if (JSID_IS_ATOM(id, cx->runtime->atomState.calleeAtom)) {
722 Value tmp = UndefinedValue();
723 PropertyOp throwTypeError = CastAsPropertyOp(obj->getThrowTypeError());
724 uintN attrs = JSPROP_PERMANENT | JSPROP_GETTER | JSPROP_SETTER | JSPROP_SHARED;
725 if (!js_DefineProperty(cx, obj, id, &tmp, throwTypeError, throwTypeError, attrs))
726 return false;
728 *objp = obj;
729 return true;
730 } else if (JSID_IS_ATOM(id, cx->runtime->atomState.callerAtom)) {
732 * Strict mode arguments objects have an immutable poison-pill caller
733 * property that throws a TypeError on getting or setting.
735 PropertyOp throwTypeError = CastAsPropertyOp(obj->getThrowTypeError());
736 Value tmp = UndefinedValue();
737 if (!js_DefineProperty(cx, obj, id, &tmp, throwTypeError, throwTypeError,
738 JSPROP_PERMANENT | JSPROP_GETTER | JSPROP_SETTER | JSPROP_SHARED)) {
739 return false;
742 *objp = obj;
743 return true;
746 if (valid) {
747 Value tmp = UndefinedValue();
748 if (!js_DefineProperty(cx, obj, id, &tmp, StrictArgGetter, StrictArgSetter, attrs))
749 return false;
750 *objp = obj;
752 return true;
755 JSBool
756 strictargs_enumerate(JSContext *cx, JSObject *obj)
758 JS_ASSERT(obj->isStrictArguments());
761 * Trigger reflection in strictargs_resolve using a series of
762 * js_LookupProperty calls. Beware deleted properties!
764 JSObject *pobj;
765 JSProperty *prop;
767 // length
768 if (!js_LookupProperty(cx, obj, ATOM_TO_JSID(cx->runtime->atomState.lengthAtom), &pobj, &prop))
769 return false;
770 if (prop)
771 pobj->dropProperty(cx, prop);
773 // callee
774 if (!js_LookupProperty(cx, obj, ATOM_TO_JSID(cx->runtime->atomState.calleeAtom), &pobj, &prop))
775 return false;
776 if (prop)
777 pobj->dropProperty(cx, prop);
779 // caller
780 if (!js_LookupProperty(cx, obj, ATOM_TO_JSID(cx->runtime->atomState.callerAtom), &pobj, &prop))
781 return false;
782 if (prop)
783 pobj->dropProperty(cx, prop);
785 for (uint32 i = 0, argc = obj->getArgsInitialLength(); i < argc; i++) {
786 if (!js_LookupProperty(cx, obj, INT_TO_JSID(i), &pobj, &prop))
787 return false;
788 if (prop)
789 pobj->dropProperty(cx, prop);
792 return true;
795 } // namespace
797 static void
798 args_finalize(JSContext *cx, JSObject *obj)
800 cx->free((void *) obj->getArgsData());
804 * If a generator's arguments or call object escapes, and the generator frame
805 * is not executing, the generator object needs to be marked because it is not
806 * otherwise reachable. An executing generator is rooted by its invocation. To
807 * distinguish the two cases (which imply different access paths to the
808 * generator object), we use the JSFRAME_FLOATING_GENERATOR flag, which is only
809 * set on the JSStackFrame kept in the generator object's JSGenerator.
811 static inline void
812 MaybeMarkGenerator(JSTracer *trc, JSObject *obj)
814 #if JS_HAS_GENERATORS
815 JSStackFrame *fp = (JSStackFrame *) obj->getPrivate();
816 if (fp && fp->isFloatingGenerator()) {
817 JSObject *genobj = js_FloatingFrameToGenerator(fp)->obj;
818 MarkObject(trc, *genobj, "generator object");
820 #endif
823 static void
824 args_trace(JSTracer *trc, JSObject *obj)
826 JS_ASSERT(obj->isArguments());
827 if (obj->getPrivate() == JS_ARGUMENTS_OBJECT_ON_TRACE) {
828 JS_ASSERT(!obj->isStrictArguments());
829 return;
832 ArgumentsData *data = obj->getArgsData();
833 if (data->callee.isObject())
834 MarkObject(trc, data->callee.toObject(), js_callee_str);
835 MarkValueRange(trc, obj->getArgsInitialLength(), data->slots, js_arguments_str);
837 MaybeMarkGenerator(trc, obj);
841 * The Arguments classes aren't initialized via js_InitClass, because arguments
842 * objects have the initial value of Object.prototype as their [[Prototype]].
843 * However, Object.prototype.toString.call(arguments) === "[object Arguments]"
844 * per ES5 (although not ES3), so the class name is "Arguments" rather than
845 * "Object".
847 * The JSClass functions below collaborate to lazily reflect and synchronize
848 * actual argument values, argument count, and callee function object stored
849 * in a JSStackFrame with their corresponding property values in the frame's
850 * arguments object.
852 Class js_ArgumentsClass = {
853 "Arguments",
854 JSCLASS_HAS_PRIVATE | JSCLASS_NEW_RESOLVE |
855 JSCLASS_HAS_RESERVED_SLOTS(JSObject::ARGS_CLASS_RESERVED_SLOTS) |
856 JSCLASS_MARK_IS_TRACE | JSCLASS_HAS_CACHED_PROTO(JSProto_Object),
857 PropertyStub, /* addProperty */
858 args_delProperty,
859 PropertyStub, /* getProperty */
860 PropertyStub, /* setProperty */
861 args_enumerate,
862 (JSResolveOp) args_resolve,
863 ConvertStub,
864 args_finalize, /* finalize */
865 NULL, /* reserved0 */
866 NULL, /* checkAccess */
867 NULL, /* call */
868 NULL, /* construct */
869 NULL, /* xdrObject */
870 NULL, /* hasInstance */
871 JS_CLASS_TRACE(args_trace)
874 namespace js {
877 * Strict mode arguments is significantly less magical than non-strict mode
878 * arguments, so it is represented by a different class while sharing some
879 * functionality.
881 Class StrictArgumentsClass = {
882 "Arguments",
883 JSCLASS_HAS_PRIVATE | JSCLASS_NEW_RESOLVE |
884 JSCLASS_HAS_RESERVED_SLOTS(JSObject::ARGS_CLASS_RESERVED_SLOTS) |
885 JSCLASS_MARK_IS_TRACE | JSCLASS_HAS_CACHED_PROTO(JSProto_Object),
886 PropertyStub, /* addProperty */
887 args_delProperty,
888 PropertyStub, /* getProperty */
889 PropertyStub, /* setProperty */
890 strictargs_enumerate,
891 reinterpret_cast<JSResolveOp>(strictargs_resolve),
892 ConvertStub,
893 args_finalize, /* finalize */
894 NULL, /* reserved0 */
895 NULL, /* checkAccess */
896 NULL, /* call */
897 NULL, /* construct */
898 NULL, /* xdrObject */
899 NULL, /* hasInstance */
900 JS_CLASS_TRACE(args_trace)
906 * A Declarative Environment object stores its active JSStackFrame pointer in
907 * its private slot, just as Call and Arguments objects do.
909 Class js_DeclEnvClass = {
910 js_Object_str,
911 JSCLASS_HAS_PRIVATE | JSCLASS_HAS_CACHED_PROTO(JSProto_Object),
912 PropertyStub, /* addProperty */
913 PropertyStub, /* delProperty */
914 PropertyStub, /* getProperty */
915 PropertyStub, /* setProperty */
916 EnumerateStub,
917 ResolveStub,
918 ConvertStub
921 static JSBool
922 CheckForEscapingClosure(JSContext *cx, JSObject *obj, Value *vp)
924 JS_ASSERT(obj->isCall() || obj->getClass() == &js_DeclEnvClass);
926 const Value &v = *vp;
928 JSObject *funobj;
929 if (IsFunctionObject(v, &funobj)) {
930 JSFunction *fun = GET_FUNCTION_PRIVATE(cx, funobj);
933 * Any escaping null or flat closure that reaches above itself or
934 * contains nested functions that reach above it must be wrapped.
935 * We can wrap only when this Call or Declarative Environment obj
936 * still has an active stack frame associated with it.
938 if (fun->needsWrapper()) {
939 LeaveTrace(cx);
941 JSStackFrame *fp = (JSStackFrame *) obj->getPrivate();
942 if (fp) {
943 JSObject *wrapper = WrapEscapingClosure(cx, fp, fun);
944 if (!wrapper)
945 return false;
946 vp->setObject(*wrapper);
947 return true;
950 JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
951 JSMSG_OPTIMIZED_CLOSURE_LEAK);
952 return false;
955 return true;
958 static JSBool
959 CalleeGetter(JSContext *cx, JSObject *obj, jsid id, Value *vp)
961 return CheckForEscapingClosure(cx, obj, vp);
964 static JSObject *
965 NewCallObject(JSContext *cx, JSFunction *fun, JSObject &scopeChain, JSObject &callee)
967 JSObject *callobj = js_NewGCObject(cx);
968 if (!callobj)
969 return NULL;
971 callobj->init(&js_CallClass, NULL, &scopeChain, NULL, cx);
972 callobj->setMap(fun->u.i.names);
974 /* This must come after callobj->lastProp has been set. */
975 if (!callobj->ensureInstanceReservedSlots(cx, fun->countArgsAndVars()))
976 return NULL;
978 #ifdef DEBUG
979 for (Shape::Range r = callobj->lastProp; !r.empty(); r.popFront()) {
980 const Shape &s = r.front();
981 if (s.slot != SHAPE_INVALID_SLOT) {
982 JS_ASSERT(s.slot + 1 == callobj->slotSpan());
983 break;
986 #endif
988 callobj->setCallObjCallee(callee);
989 return callobj;
992 static inline JSObject *
993 NewDeclEnvObject(JSContext *cx, JSStackFrame *fp)
995 JSObject *envobj = js_NewGCObject(cx);
996 if (!envobj)
997 return NULL;
999 envobj->init(&js_DeclEnvClass, NULL, &fp->scopeChain(), fp, cx);
1000 envobj->setMap(cx->runtime->emptyDeclEnvShape);
1001 return envobj;
1004 JSObject *
1005 js_GetCallObject(JSContext *cx, JSStackFrame *fp)
1007 /* Create a call object for fp only if it lacks one. */
1008 JS_ASSERT(fp->isFunctionFrame());
1009 if (fp->hasCallObj())
1010 return &fp->callObj();
1012 #ifdef DEBUG
1013 /* A call object should be a frame's outermost scope chain element. */
1014 Class *clasp = fp->scopeChain().getClass();
1015 if (clasp == &js_WithClass || clasp == &js_BlockClass)
1016 JS_ASSERT(fp->scopeChain().getPrivate() != js_FloatingFrameIfGenerator(cx, fp));
1017 else if (clasp == &js_CallClass)
1018 JS_ASSERT(fp->scopeChain().getPrivate() != fp);
1019 #endif
1022 * Create the call object, using the frame's enclosing scope as its
1023 * parent, and link the call to its stack frame. For a named function
1024 * expression Call's parent points to an environment object holding
1025 * function's name.
1027 JSAtom *lambdaName =
1028 (fp->fun()->flags & JSFUN_LAMBDA) ? fp->fun()->atom : NULL;
1029 if (lambdaName) {
1030 JSObject *envobj = NewDeclEnvObject(cx, fp);
1031 if (!envobj)
1032 return NULL;
1034 /* Root envobj before js_DefineNativeProperty (-> JSClass.addProperty). */
1035 fp->setScopeChainNoCallObj(*envobj);
1036 if (!js_DefineNativeProperty(cx, &fp->scopeChain(), ATOM_TO_JSID(lambdaName),
1037 ObjectValue(fp->callee()),
1038 CalleeGetter, NULL,
1039 JSPROP_PERMANENT | JSPROP_READONLY,
1040 0, 0, NULL)) {
1041 return NULL;
1045 JSObject *callobj = NewCallObject(cx, fp->fun(), fp->scopeChain(), fp->callee());
1046 if (!callobj)
1047 return NULL;
1049 callobj->setPrivate(fp);
1050 JS_ASSERT(fp->fun() == fp->callee().getFunctionPrivate());
1053 * Push callobj on the top of the scope chain, and make it the
1054 * variables object.
1056 fp->setScopeChainAndCallObj(*callobj);
1057 return callobj;
1060 JSObject * JS_FASTCALL
1061 js_CreateCallObjectOnTrace(JSContext *cx, JSFunction *fun, JSObject *callee, JSObject *scopeChain)
1063 JS_ASSERT(!js_IsNamedLambda(fun));
1064 JS_ASSERT(scopeChain);
1065 return NewCallObject(cx, fun, *scopeChain, *callee);
1068 JS_DEFINE_CALLINFO_4(extern, OBJECT, js_CreateCallObjectOnTrace, CONTEXT, FUNCTION, OBJECT, OBJECT,
1069 0, nanojit::ACCSET_STORE_ANY)
1071 inline static void
1072 CopyValuesToCallObject(JSObject &callobj, uintN nargs, Value *argv, uintN nvars, Value *slots)
1074 /* Copy however many args fit into fslots. */
1075 uintN first = JSSLOT_PRIVATE + JSObject::CALL_RESERVED_SLOTS + 1;
1076 JS_ASSERT(first <= JS_INITIAL_NSLOTS);
1078 Value *vp = &callobj.fslots[first];
1079 uintN len = Min(nargs, uintN(JS_INITIAL_NSLOTS) - first);
1081 memcpy(vp, argv, len * sizeof(Value));
1082 vp += len;
1084 nargs -= len;
1085 if (nargs != 0) {
1086 /* Copy any remaining args into dslots. */
1087 vp = callobj.dslots;
1088 memcpy(vp, argv + len, nargs * sizeof(Value));
1089 vp += nargs;
1090 } else {
1091 /* Copy however many vars fit into any remaining fslots. */
1092 first += len;
1093 len = JS_MIN(nvars, JS_INITIAL_NSLOTS - first);
1094 memcpy(vp, slots, len * sizeof(Value));
1095 slots += len;
1096 nvars -= len;
1097 vp = callobj.dslots;
1100 /* Copy any remaining vars into dslots. */
1101 memcpy(vp, slots, nvars * sizeof(Value));
1104 void
1105 js_PutCallObject(JSContext *cx, JSStackFrame *fp)
1107 JSObject &callobj = fp->callObj();
1109 /* Get the arguments object to snapshot fp's actual argument values. */
1110 if (fp->hasArgsObj()) {
1111 if (!fp->hasOverriddenArgs())
1112 callobj.setCallObjArguments(ObjectValue(fp->argsObj()));
1113 js_PutArgsObject(cx, fp);
1116 JSFunction *fun = fp->fun();
1117 JS_ASSERT(fun == callobj.getCallObjCalleeFunction());
1118 uintN n = fun->countArgsAndVars();
1121 * Since for a call object all fixed slots happen to be taken, we can copy
1122 * arguments and variables straight into JSObject.dslots.
1124 JS_STATIC_ASSERT(JS_INITIAL_NSLOTS - JSSLOT_PRIVATE ==
1125 1 + JSObject::CALL_RESERVED_SLOTS);
1126 if (n != 0) {
1127 JS_ASSERT(JSFunction::FIRST_FREE_SLOT + n <= callobj.numSlots());
1129 uint32 nargs = fun->nargs;
1130 uint32 nvars = fun->u.i.nvars;
1132 #ifdef JS_METHODJIT
1133 JS_STATIC_ASSERT(JS_INITIAL_NSLOTS == JSSLOT_PRIVATE + JSObject::CALL_RESERVED_SLOTS + 1);
1134 JSScript *script = fun->u.i.script;
1135 memcpy(callobj.dslots, fp->formalArgs(), nargs * sizeof(Value));
1136 if (!script->jit || script->usesEval) {
1137 memcpy(callobj.dslots + nargs, fp->slots(), nvars * sizeof(Value));
1138 } else if (script->jit) {
1139 for (uint32 i = 0; i < script->jit->nescaping; i++) {
1140 uint32 e = script->jit->escaping[i];
1141 callobj.dslots[nargs + e] = fp->slots()[e];
1144 #else
1145 CopyValuesToCallObject(callobj, nargs, fp->formalArgs(), nvars, fp->slots());
1146 #endif
1149 /* Clear private pointers to fp, which is about to go away (js_Invoke). */
1150 if (js_IsNamedLambda(fun)) {
1151 JSObject *env = callobj.getParent();
1153 JS_ASSERT(env->getClass() == &js_DeclEnvClass);
1154 JS_ASSERT(env->getPrivate() == fp);
1155 env->setPrivate(NULL);
1158 callobj.setPrivate(NULL);
1161 JSBool JS_FASTCALL
1162 js_PutCallObjectOnTrace(JSContext *cx, JSObject *callobj, uint32 nargs, Value *argv,
1163 uint32 nvars, Value *slots)
1165 JS_ASSERT(callobj->isCall());
1166 JS_ASSERT(!callobj->getPrivate());
1168 uintN n = nargs + nvars;
1169 if (n != 0)
1170 CopyValuesToCallObject(*callobj, nargs, argv, nvars, slots);
1172 return true;
1175 JS_DEFINE_CALLINFO_6(extern, BOOL, js_PutCallObjectOnTrace, CONTEXT, OBJECT, UINT32, VALUEPTR,
1176 UINT32, VALUEPTR, 0, nanojit::ACCSET_STORE_ANY)
1178 enum JSCallPropertyKind {
1179 JSCPK_ARGUMENTS,
1180 JSCPK_ARG,
1181 JSCPK_VAR,
1182 JSCPK_UPVAR
1185 static JSBool
1186 CallPropertyOp(JSContext *cx, JSObject *obj, jsid id, Value *vp,
1187 JSCallPropertyKind kind, JSBool setter = false)
1189 JS_ASSERT(obj->isCall());
1191 uintN i = 0;
1192 if (kind != JSCPK_ARGUMENTS) {
1193 JS_ASSERT((int16) JSID_TO_INT(id) == JSID_TO_INT(id));
1194 i = (uint16) JSID_TO_INT(id);
1197 Value *array;
1198 if (kind == JSCPK_UPVAR) {
1199 JSObject &callee = obj->getCallObjCallee();
1201 #ifdef DEBUG
1202 JSFunction *callee_fun = (JSFunction *) callee.getPrivate();
1203 JS_ASSERT(FUN_FLAT_CLOSURE(callee_fun));
1204 JS_ASSERT(i < callee_fun->u.i.nupvars);
1205 #endif
1207 array = callee.getFlatClosureUpvars();
1208 } else {
1209 JSFunction *fun = obj->getCallObjCalleeFunction();
1210 JS_ASSERT_IF(kind == JSCPK_ARG, i < fun->nargs);
1211 JS_ASSERT_IF(kind == JSCPK_VAR, i < fun->u.i.nvars);
1213 JSStackFrame *fp = (JSStackFrame *) obj->getPrivate();
1215 if (kind == JSCPK_ARGUMENTS) {
1216 if (setter) {
1217 if (fp)
1218 fp->setOverriddenArgs();
1219 obj->setCallObjArguments(*vp);
1220 } else {
1221 if (fp && !fp->hasOverriddenArgs()) {
1222 JSObject *argsobj;
1224 argsobj = js_GetArgsObject(cx, fp);
1225 if (!argsobj)
1226 return false;
1227 vp->setObject(*argsobj);
1228 } else {
1229 *vp = obj->getCallObjArguments();
1232 return true;
1235 if (!fp) {
1236 if (kind == JSCPK_VAR)
1237 i += fun->nargs;
1238 else
1239 JS_ASSERT(kind == JSCPK_ARG);
1241 const uintN first = JSSLOT_PRIVATE + JSObject::CALL_RESERVED_SLOTS + 1;
1242 JS_ASSERT(first == JSSLOT_FREE(&js_CallClass));
1243 JS_ASSERT(first <= JS_INITIAL_NSLOTS);
1245 array = (i < JS_INITIAL_NSLOTS - first) ? obj->fslots : obj->dslots;
1246 } else if (kind == JSCPK_ARG) {
1247 array = fp->formalArgs();
1248 } else {
1249 JS_ASSERT(kind == JSCPK_VAR);
1250 array = fp->slots();
1254 if (setter) {
1255 GC_POKE(cx, array[i]);
1256 array[i] = *vp;
1257 } else {
1258 *vp = array[i];
1260 return true;
1263 static JSBool
1264 GetCallArguments(JSContext *cx, JSObject *obj, jsid id, Value *vp)
1266 return CallPropertyOp(cx, obj, id, vp, JSCPK_ARGUMENTS);
1269 static JSBool
1270 SetCallArguments(JSContext *cx, JSObject *obj, jsid id, Value *vp)
1272 return CallPropertyOp(cx, obj, id, vp, JSCPK_ARGUMENTS, true);
1275 JSBool
1276 js_GetCallArg(JSContext *cx, JSObject *obj, jsid id, Value *vp)
1278 return CallPropertyOp(cx, obj, id, vp, JSCPK_ARG);
1281 JSBool
1282 SetCallArg(JSContext *cx, JSObject *obj, jsid id, Value *vp)
1284 return CallPropertyOp(cx, obj, id, vp, JSCPK_ARG, true);
1287 JSBool
1288 GetFlatUpvar(JSContext *cx, JSObject *obj, jsid id, Value *vp)
1290 return CallPropertyOp(cx, obj, id, vp, JSCPK_UPVAR);
1293 JSBool
1294 SetFlatUpvar(JSContext *cx, JSObject *obj, jsid id, Value *vp)
1296 return CallPropertyOp(cx, obj, id, vp, JSCPK_UPVAR, true);
1299 JSBool
1300 js_GetCallVar(JSContext *cx, JSObject *obj, jsid id, Value *vp)
1302 return CallPropertyOp(cx, obj, id, vp, JSCPK_VAR);
1305 JSBool
1306 js_GetCallVarChecked(JSContext *cx, JSObject *obj, jsid id, Value *vp)
1308 if (!CallPropertyOp(cx, obj, id, vp, JSCPK_VAR))
1309 return false;
1311 return CheckForEscapingClosure(cx, obj, vp);
1314 JSBool
1315 SetCallVar(JSContext *cx, JSObject *obj, jsid id, Value *vp)
1317 return CallPropertyOp(cx, obj, id, vp, JSCPK_VAR, true);
1320 #if JS_TRACER
1321 JSBool JS_FASTCALL
1322 js_SetCallArg(JSContext *cx, JSObject *obj, jsid slotid, ValueArgType arg)
1324 Value argcopy = ValueArgToConstRef(arg);
1325 return CallPropertyOp(cx, obj, slotid, &argcopy, JSCPK_ARG, true);
1327 JS_DEFINE_CALLINFO_4(extern, BOOL, js_SetCallArg, CONTEXT, OBJECT, JSID, VALUE, 0,
1328 nanojit::ACCSET_STORE_ANY)
1330 JSBool JS_FASTCALL
1331 js_SetCallVar(JSContext *cx, JSObject *obj, jsid slotid, ValueArgType arg)
1333 Value argcopy = ValueArgToConstRef(arg);
1334 return CallPropertyOp(cx, obj, slotid, &argcopy, JSCPK_VAR, true);
1336 JS_DEFINE_CALLINFO_4(extern, BOOL, js_SetCallVar, CONTEXT, OBJECT, JSID, VALUE, 0,
1337 nanojit::ACCSET_STORE_ANY)
1338 #endif
1340 static JSBool
1341 call_resolve(JSContext *cx, JSObject *obj, jsid id, uintN flags,
1342 JSObject **objp)
1344 JS_ASSERT(obj->isCall());
1345 JS_ASSERT(!obj->getProto());
1347 if (!JSID_IS_ATOM(id))
1348 return JS_TRUE;
1350 #ifdef DEBUG
1351 JSFunction *fun = obj->getCallObjCalleeFunction();
1352 JS_ASSERT(fun->lookupLocal(cx, JSID_TO_ATOM(id), NULL) == JSLOCAL_NONE);
1353 #endif
1356 * Resolve arguments so that we never store a particular Call object's
1357 * arguments object reference in a Call prototype's |arguments| slot.
1359 if (JSID_IS_ATOM(id, cx->runtime->atomState.argumentsAtom)) {
1360 if (!js_DefineNativeProperty(cx, obj, id, UndefinedValue(),
1361 GetCallArguments, SetCallArguments,
1362 JSPROP_PERMANENT | JSPROP_SHARED,
1363 0, 0, NULL, JSDNP_DONT_PURGE)) {
1364 return JS_FALSE;
1366 *objp = obj;
1367 return JS_TRUE;
1370 /* Control flow reaches here only if id was not resolved. */
1371 return JS_TRUE;
1374 static void
1375 call_trace(JSTracer *trc, JSObject *obj)
1377 JS_ASSERT(obj->isCall());
1378 JSStackFrame *fp = (JSStackFrame *) obj->getPrivate();
1379 if (fp) {
1381 * FIXME: Hide copies of stack values rooted by fp from the Cycle
1382 * Collector, which currently lacks a non-stub Unlink implementation
1383 * for JS objects (including Call objects), so is unable to collect
1384 * cycles involving Call objects whose frames are active without this
1385 * hiding hack.
1387 uintN first = JSSLOT_PRIVATE + JSObject::CALL_RESERVED_SLOTS + 1;
1388 JS_ASSERT(first <= JS_INITIAL_NSLOTS);
1390 uintN count = fp->fun()->countArgsAndVars();
1391 uintN fixed = JS_MIN(count, JS_INITIAL_NSLOTS - first);
1393 SetValueRangeToUndefined(&obj->fslots[first], fixed);
1394 SetValueRangeToUndefined(obj->dslots, count - fixed);
1397 MaybeMarkGenerator(trc, obj);
1400 JS_PUBLIC_DATA(Class) js_CallClass = {
1401 "Call",
1402 JSCLASS_HAS_PRIVATE |
1403 JSCLASS_HAS_RESERVED_SLOTS(JSObject::CALL_RESERVED_SLOTS) |
1404 JSCLASS_NEW_RESOLVE | JSCLASS_IS_ANONYMOUS | JSCLASS_MARK_IS_TRACE,
1405 PropertyStub, /* addProperty */
1406 PropertyStub, /* delProperty */
1407 PropertyStub, /* getProperty */
1408 PropertyStub, /* setProperty */
1409 JS_EnumerateStub,
1410 (JSResolveOp)call_resolve,
1411 NULL, /* convert */
1412 NULL, /* finalize */
1413 NULL, /* reserved0 */
1414 NULL, /* checkAccess */
1415 NULL, /* call */
1416 NULL, /* construct */
1417 NULL, /* xdrObject */
1418 NULL, /* hasInstance */
1419 JS_CLASS_TRACE(call_trace)
1422 bool
1423 JSStackFrame::getValidCalleeObject(JSContext *cx, Value *vp)
1425 if (!isFunctionFrame()) {
1426 vp->setUndefined();
1427 return true;
1430 JSFunction *fun = this->fun();
1433 * See the equivalent condition in ArgGetter for the 'callee' id case, but
1434 * note that here we do not want to throw, since this escape can happen via
1435 * a foo.caller reference alone, without any debugger or indirect eval. And
1436 * alas, it seems foo.caller is still used on the Web.
1438 if (fun->needsWrapper()) {
1439 JSObject *wrapper = WrapEscapingClosure(cx, this, fun);
1440 if (!wrapper)
1441 return false;
1442 vp->setObject(*wrapper);
1443 return true;
1446 JSObject &funobj = callee();
1447 vp->setObject(funobj);
1450 * Check for an escape attempt by a joined function object, which must go
1451 * through the frame's |this| object's method read barrier for the method
1452 * atom by which it was uniquely associated with a property.
1454 const Value &thisv = functionThis();
1455 if (thisv.isObject()) {
1456 JS_ASSERT(funobj.getFunctionPrivate() == fun);
1458 if (&fun->compiledFunObj() == &funobj && fun->methodAtom()) {
1459 JSObject *thisp = &thisv.toObject();
1460 JS_ASSERT(thisp->canHaveMethodBarrier());
1462 if (thisp->hasMethodBarrier()) {
1463 const Shape *shape = thisp->nativeLookup(ATOM_TO_JSID(fun->methodAtom()));
1466 * The method property might have been deleted while the method
1467 * barrier flag stuck, so we must lookup and test here.
1469 * Two cases follow: the method barrier was not crossed yet, so
1470 * we cross it here; the method barrier *was* crossed, in which
1471 * case we must fetch and validate the cloned (unjoined) funobj
1472 * in the method property's slot.
1474 * In either case we must allow for the method property to have
1475 * been replaced, or its value to have been overwritten.
1477 if (shape) {
1478 if (shape->isMethod() && &shape->methodObject() == &funobj) {
1479 if (!thisp->methodReadBarrier(cx, *shape, vp))
1480 return false;
1481 calleeValue().setObject(vp->toObject());
1482 return true;
1484 if (shape->hasSlot()) {
1485 Value v = thisp->getSlot(shape->slot);
1486 JSObject *clone;
1488 if (IsFunctionObject(v, &clone) &&
1489 GET_FUNCTION_PRIVATE(cx, clone) == fun &&
1490 clone->hasMethodObj(*thisp)) {
1491 JS_ASSERT(clone != &funobj);
1492 *vp = v;
1493 calleeValue().setObject(*clone);
1494 return true;
1500 * If control flows here, we can't find an already-existing
1501 * clone (or force to exist a fresh clone) created via thisp's
1502 * method read barrier, so we must clone fun and store it in
1503 * fp's callee to avoid re-cloning upon repeated foo.caller
1504 * access. It seems that there are no longer any properties
1505 * referring to fun.
1507 JSObject *newfunobj = CloneFunctionObject(cx, fun, fun->getParent());
1508 if (!newfunobj)
1509 return false;
1510 newfunobj->setMethodObj(*thisp);
1511 calleeValue().setObject(*newfunobj);
1512 return true;
1517 return true;
1520 /* Generic function tinyids. */
1521 enum {
1522 FUN_ARGUMENTS = -1, /* predefined arguments local variable */
1523 FUN_LENGTH = -2, /* number of actual args, arity if inactive */
1524 FUN_ARITY = -3, /* number of formal parameters; desired argc */
1525 FUN_NAME = -4, /* function name, "" if anonymous */
1526 FUN_CALLER = -5 /* Function.prototype.caller, backward compat */
1529 static JSBool
1530 fun_getProperty(JSContext *cx, JSObject *obj, jsid id, Value *vp)
1532 if (!JSID_IS_INT(id))
1533 return true;
1535 jsint slot = JSID_TO_INT(id);
1538 * Loop because getter and setter can be delegated from another class,
1539 * but loop only for FUN_LENGTH because we must pretend that f.length
1540 * is in each function instance f, per ECMA-262, instead of only in the
1541 * Function.prototype object (we use JSPROP_PERMANENT with JSPROP_SHARED
1542 * to make it appear so).
1544 * This code couples tightly to the attributes for lazyFunctionDataProps[]
1545 * and poisonPillProps[] initializers below, and to js_SetProperty and
1546 * js_HasOwnProperty.
1548 * It's important to allow delegating objects, even though they inherit
1549 * this getter (fun_getProperty), to override arguments, arity, caller,
1550 * and name. If we didn't return early for slot != FUN_LENGTH, we would
1551 * clobber *vp with the native property value, instead of letting script
1552 * override that value in delegating objects.
1554 * Note how that clobbering is what simulates JSPROP_READONLY for all of
1555 * the non-standard properties when the directly addressed object (obj)
1556 * is a function object (i.e., when this loop does not iterate).
1558 JSFunction *fun;
1559 while (!(fun = (JSFunction *)
1560 GetInstancePrivate(cx, obj, &js_FunctionClass, NULL))) {
1561 if (slot != FUN_LENGTH)
1562 return true;
1563 obj = obj->getProto();
1564 if (!obj)
1565 return true;
1568 /* Find fun's top-most activation record. */
1569 JSStackFrame *fp;
1570 for (fp = js_GetTopStackFrame(cx);
1571 fp && (fp->maybeFun() != fun || fp->isEvalOrDebuggerFrame());
1572 fp = fp->prev()) {
1573 continue;
1576 switch (slot) {
1577 case FUN_ARGUMENTS:
1578 /* Warn if strict about f.arguments or equivalent unqualified uses. */
1579 if (!JS_ReportErrorFlagsAndNumber(cx,
1580 JSREPORT_WARNING | JSREPORT_STRICT,
1581 js_GetErrorMessage, NULL,
1582 JSMSG_DEPRECATED_USAGE,
1583 js_arguments_str)) {
1584 return false;
1586 if (fp) {
1587 if (!js_GetArgsValue(cx, fp, vp))
1588 return false;
1589 } else {
1590 vp->setNull();
1592 break;
1594 case FUN_LENGTH:
1595 case FUN_ARITY:
1596 vp->setInt32(fun->nargs);
1597 break;
1599 case FUN_NAME:
1600 vp->setString(fun->atom ? ATOM_TO_STRING(fun->atom)
1601 : cx->runtime->emptyString);
1602 break;
1604 case FUN_CALLER:
1605 vp->setNull();
1606 if (fp && fp->prev() && !fp->prev()->getValidCalleeObject(cx, vp))
1607 return false;
1609 if (vp->isObject()) {
1610 JSObject &caller = vp->toObject();
1612 /* Censor the caller if it is from another compartment. */
1613 if (caller.getCompartment(cx) != cx->compartment) {
1614 vp->setNull();
1615 } else if (caller.isFunction() && caller.getFunctionPrivate()->inStrictMode()) {
1616 JS_ReportErrorFlagsAndNumber(cx, JSREPORT_ERROR, js_GetErrorMessage, NULL,
1617 JSMSG_CALLER_IS_STRICT);
1618 return false;
1621 break;
1623 default:
1624 /* XXX fun[0] and fun.arguments[0] are equivalent. */
1625 if (fp && fp->isFunctionFrame() && uint16(slot) < fp->numFormalArgs())
1626 *vp = fp->formalArg(slot);
1627 break;
1630 return true;
1633 namespace {
1635 struct LazyFunctionDataProp {
1636 uint16 atomOffset;
1637 int8 tinyid;
1638 uint8 attrs;
1641 struct PoisonPillProp {
1642 uint16 atomOffset;
1643 int8 tinyid;
1646 /* NB: no sentinels at ends -- use JS_ARRAY_LENGTH to bound loops. */
1648 const LazyFunctionDataProp lazyFunctionDataProps[] = {
1649 {ATOM_OFFSET(arity), FUN_ARITY, JSPROP_PERMANENT},
1650 {ATOM_OFFSET(name), FUN_NAME, JSPROP_PERMANENT},
1653 /* Properties censored into [[ThrowTypeError]] in strict mode. */
1654 const PoisonPillProp poisonPillProps[] = {
1655 {ATOM_OFFSET(arguments), FUN_ARGUMENTS },
1656 {ATOM_OFFSET(caller), FUN_CALLER },
1661 static JSBool
1662 fun_enumerate(JSContext *cx, JSObject *obj)
1664 JS_ASSERT(obj->isFunction());
1666 jsid id;
1667 bool found;
1669 if (!obj->getFunctionPrivate()->isBound()) {
1670 id = ATOM_TO_JSID(cx->runtime->atomState.classPrototypeAtom);
1671 if (!obj->hasProperty(cx, id, &found, JSRESOLVE_QUALIFIED))
1672 return false;
1675 id = ATOM_TO_JSID(cx->runtime->atomState.lengthAtom);
1676 if (!obj->hasProperty(cx, id, &found, JSRESOLVE_QUALIFIED))
1677 return false;
1679 for (uintN i = 0; i < JS_ARRAY_LENGTH(lazyFunctionDataProps); i++) {
1680 const LazyFunctionDataProp &lfp = lazyFunctionDataProps[i];
1681 id = ATOM_TO_JSID(OFFSET_TO_ATOM(cx->runtime, lfp.atomOffset));
1682 if (!obj->hasProperty(cx, id, &found, JSRESOLVE_QUALIFIED))
1683 return false;
1686 for (uintN i = 0; i < JS_ARRAY_LENGTH(poisonPillProps); i++) {
1687 const PoisonPillProp &p = poisonPillProps[i];
1688 id = ATOM_TO_JSID(OFFSET_TO_ATOM(cx->runtime, p.atomOffset));
1689 if (!obj->hasProperty(cx, id, &found, JSRESOLVE_QUALIFIED))
1690 return false;
1693 return true;
1696 static JSBool
1697 fun_resolve(JSContext *cx, JSObject *obj, jsid id, uintN flags,
1698 JSObject **objp)
1700 if (!JSID_IS_ATOM(id))
1701 return JS_TRUE;
1703 JSFunction *fun = obj->getFunctionPrivate();
1706 * No need to reflect fun.prototype in 'fun.prototype = ... '. Assert that
1707 * fun is not a compiler-created function object, which must never leak to
1708 * script or embedding code and then be mutated.
1710 if ((flags & JSRESOLVE_ASSIGNING) && !JSID_IS_ATOM(id, cx->runtime->atomState.lengthAtom)) {
1711 JS_ASSERT(!IsInternalFunctionObject(obj));
1712 return JS_TRUE;
1716 * Ok, check whether id is 'prototype' and bootstrap the function object's
1717 * prototype property.
1719 JSAtom *atom = cx->runtime->atomState.classPrototypeAtom;
1720 if (id == ATOM_TO_JSID(atom)) {
1721 JS_ASSERT(!IsInternalFunctionObject(obj));
1724 * Beware of the wacky case of a user function named Object -- trying
1725 * to find a prototype for that will recur back here _ad perniciem_.
1727 if (fun->atom == CLASS_ATOM(cx, Object))
1728 return JS_TRUE;
1730 /* ES5 15.3.4.5: bound functions don't have a prototype property. */
1731 if (fun->isBound())
1732 return JS_TRUE;
1735 * Make the prototype object an instance of Object with the same parent
1736 * as the function object itself.
1738 JSObject *parent = obj->getParent();
1739 JSObject *proto;
1740 if (!js_GetClassPrototype(cx, parent, JSProto_Object, &proto))
1741 return JS_FALSE;
1742 proto = NewNativeClassInstance(cx, &js_ObjectClass, proto, parent);
1743 if (!proto)
1744 return JS_FALSE;
1747 * ECMA (15.3.5.2) says that constructor.prototype is DontDelete for
1748 * user-defined functions, but DontEnum | ReadOnly | DontDelete for
1749 * native "system" constructors such as Object or Function. So lazily
1750 * set the former here in fun_resolve, but eagerly define the latter
1751 * in js_InitClass, with the right attributes.
1753 if (!js_SetClassPrototype(cx, obj, proto, JSPROP_PERMANENT))
1754 return JS_FALSE;
1756 *objp = obj;
1757 return JS_TRUE;
1760 atom = cx->runtime->atomState.lengthAtom;
1761 if (id == ATOM_TO_JSID(atom)) {
1762 JS_ASSERT(!IsInternalFunctionObject(obj));
1763 if (!js_DefineNativeProperty(cx, obj, ATOM_TO_JSID(atom), Int32Value(fun->nargs),
1764 PropertyStub, PropertyStub,
1765 JSPROP_PERMANENT | JSPROP_READONLY, 0, 0, NULL)) {
1766 return JS_FALSE;
1768 *objp = obj;
1769 return JS_TRUE;
1772 for (uintN i = 0; i < JS_ARRAY_LENGTH(lazyFunctionDataProps); i++) {
1773 const LazyFunctionDataProp *lfp = &lazyFunctionDataProps[i];
1775 atom = OFFSET_TO_ATOM(cx->runtime, lfp->atomOffset);
1776 if (id == ATOM_TO_JSID(atom)) {
1777 JS_ASSERT(!IsInternalFunctionObject(obj));
1779 if (!js_DefineNativeProperty(cx, obj,
1780 ATOM_TO_JSID(atom), UndefinedValue(),
1781 fun_getProperty, PropertyStub,
1782 lfp->attrs, Shape::HAS_SHORTID,
1783 lfp->tinyid, NULL)) {
1784 return JS_FALSE;
1786 *objp = obj;
1787 return JS_TRUE;
1791 for (uintN i = 0; i < JS_ARRAY_LENGTH(poisonPillProps); i++) {
1792 const PoisonPillProp &p = poisonPillProps[i];
1794 atom = OFFSET_TO_ATOM(cx->runtime, p.atomOffset);
1795 if (id == ATOM_TO_JSID(atom)) {
1796 JS_ASSERT(!IsInternalFunctionObject(obj));
1798 PropertyOp getter, setter;
1799 uintN attrs = JSPROP_PERMANENT;
1800 if (fun->inStrictMode() || fun->isBound()) {
1801 JSObject *throwTypeError = obj->getThrowTypeError();
1803 getter = CastAsPropertyOp(throwTypeError);
1804 setter = CastAsPropertyOp(throwTypeError);
1805 attrs |= JSPROP_GETTER | JSPROP_SETTER;
1806 } else {
1807 getter = fun_getProperty;
1808 setter = PropertyStub;
1811 if (!js_DefineNativeProperty(cx, obj, ATOM_TO_JSID(atom), UndefinedValue(),
1812 getter, setter,
1813 attrs, Shape::HAS_SHORTID,
1814 p.tinyid, NULL)) {
1815 return JS_FALSE;
1817 *objp = obj;
1818 return JS_TRUE;
1822 return JS_TRUE;
1825 #if JS_HAS_XDR
1827 /* XXX store parent and proto, if defined */
1828 JSBool
1829 js_XDRFunctionObject(JSXDRState *xdr, JSObject **objp)
1831 JSContext *cx;
1832 JSFunction *fun;
1833 uint32 firstword; /* flag telling whether fun->atom is non-null,
1834 plus for fun->u.i.skipmin, fun->u.i.wrapper,
1835 and 14 bits reserved for future use */
1836 uintN nargs, nvars, nupvars, n;
1837 uint32 localsword; /* word for argument and variable counts */
1838 uint32 flagsword; /* word for fun->u.i.nupvars and fun->flags */
1840 cx = xdr->cx;
1841 if (xdr->mode == JSXDR_ENCODE) {
1842 fun = GET_FUNCTION_PRIVATE(cx, *objp);
1843 if (!FUN_INTERPRETED(fun)) {
1844 JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
1845 JSMSG_NOT_SCRIPTED_FUNCTION,
1846 JS_GetFunctionName(fun));
1847 return false;
1849 if (fun->u.i.wrapper) {
1850 JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
1851 JSMSG_XDR_CLOSURE_WRAPPER,
1852 JS_GetFunctionName(fun));
1853 return false;
1855 JS_ASSERT((fun->u.i.wrapper & ~1U) == 0);
1856 firstword = (fun->u.i.skipmin << 2) | (fun->u.i.wrapper << 1) | !!fun->atom;
1857 nargs = fun->nargs;
1858 nvars = fun->u.i.nvars;
1859 nupvars = fun->u.i.nupvars;
1860 localsword = (nargs << 16) | nvars;
1861 flagsword = (nupvars << 16) | fun->flags;
1862 } else {
1863 fun = js_NewFunction(cx, NULL, NULL, 0, JSFUN_INTERPRETED, NULL, NULL);
1864 if (!fun)
1865 return false;
1866 FUN_OBJECT(fun)->clearParent();
1867 FUN_OBJECT(fun)->clearProto();
1868 #ifdef __GNUC__
1869 nvars = nargs = nupvars = 0; /* quell GCC uninitialized warning */
1870 #endif
1873 AutoObjectRooter tvr(cx, FUN_OBJECT(fun));
1875 if (!JS_XDRUint32(xdr, &firstword))
1876 return false;
1877 if ((firstword & 1U) && !js_XDRAtom(xdr, &fun->atom))
1878 return false;
1879 if (!JS_XDRUint32(xdr, &localsword) ||
1880 !JS_XDRUint32(xdr, &flagsword)) {
1881 return false;
1884 if (xdr->mode == JSXDR_DECODE) {
1885 nargs = localsword >> 16;
1886 nvars = uint16(localsword);
1887 JS_ASSERT((flagsword & JSFUN_KINDMASK) >= JSFUN_INTERPRETED);
1888 nupvars = flagsword >> 16;
1889 fun->flags = uint16(flagsword);
1890 fun->u.i.skipmin = uint16(firstword >> 2);
1891 fun->u.i.wrapper = JSPackedBool((firstword >> 1) & 1);
1894 /* do arguments and local vars */
1895 n = nargs + nvars + nupvars;
1896 if (n != 0) {
1897 void *mark;
1898 uintN i;
1899 uintN bitmapLength;
1900 uint32 *bitmap;
1901 jsuword *names;
1902 JSAtom *name;
1903 JSLocalKind localKind;
1905 bool ok = true;
1906 mark = JS_ARENA_MARK(&xdr->cx->tempPool);
1909 * From this point the control must flow via the label release_mark.
1911 * To xdr the names we prefix the names with a bitmap descriptor and
1912 * then xdr the names as strings. For argument names (indexes below
1913 * nargs) the corresponding bit in the bitmap is unset when the name
1914 * is null. Such null names are not encoded or decoded. For variable
1915 * names (indexes starting from nargs) bitmap's bit is set when the
1916 * name is declared as const, not as ordinary var.
1917 * */
1918 MUST_FLOW_THROUGH("release_mark");
1919 bitmapLength = JS_HOWMANY(n, JS_BITS_PER_UINT32);
1920 JS_ARENA_ALLOCATE_CAST(bitmap, uint32 *, &xdr->cx->tempPool,
1921 bitmapLength * sizeof *bitmap);
1922 if (!bitmap) {
1923 js_ReportOutOfScriptQuota(xdr->cx);
1924 ok = false;
1925 goto release_mark;
1927 if (xdr->mode == JSXDR_ENCODE) {
1928 names = fun->getLocalNameArray(xdr->cx, &xdr->cx->tempPool);
1929 if (!names) {
1930 ok = false;
1931 goto release_mark;
1933 PodZero(bitmap, bitmapLength);
1934 for (i = 0; i != n; ++i) {
1935 if (i < fun->nargs
1936 ? JS_LOCAL_NAME_TO_ATOM(names[i]) != NULL
1937 : JS_LOCAL_NAME_IS_CONST(names[i])) {
1938 bitmap[i >> JS_BITS_PER_UINT32_LOG2] |=
1939 JS_BIT(i & (JS_BITS_PER_UINT32 - 1));
1943 #ifdef __GNUC__
1944 else {
1945 names = NULL; /* quell GCC uninitialized warning */
1947 #endif
1948 for (i = 0; i != bitmapLength; ++i) {
1949 ok = !!JS_XDRUint32(xdr, &bitmap[i]);
1950 if (!ok)
1951 goto release_mark;
1953 for (i = 0; i != n; ++i) {
1954 if (i < nargs &&
1955 !(bitmap[i >> JS_BITS_PER_UINT32_LOG2] &
1956 JS_BIT(i & (JS_BITS_PER_UINT32 - 1)))) {
1957 if (xdr->mode == JSXDR_DECODE) {
1958 ok = !!fun->addLocal(xdr->cx, NULL, JSLOCAL_ARG);
1959 if (!ok)
1960 goto release_mark;
1961 } else {
1962 JS_ASSERT(!JS_LOCAL_NAME_TO_ATOM(names[i]));
1964 continue;
1966 if (xdr->mode == JSXDR_ENCODE)
1967 name = JS_LOCAL_NAME_TO_ATOM(names[i]);
1968 ok = !!js_XDRAtom(xdr, &name);
1969 if (!ok)
1970 goto release_mark;
1971 if (xdr->mode == JSXDR_DECODE) {
1972 localKind = (i < nargs)
1973 ? JSLOCAL_ARG
1974 : (i < nargs + nvars)
1975 ? (bitmap[i >> JS_BITS_PER_UINT32_LOG2] &
1976 JS_BIT(i & (JS_BITS_PER_UINT32 - 1))
1977 ? JSLOCAL_CONST
1978 : JSLOCAL_VAR)
1979 : JSLOCAL_UPVAR;
1980 ok = !!fun->addLocal(xdr->cx, name, localKind);
1981 if (!ok)
1982 goto release_mark;
1986 release_mark:
1987 JS_ARENA_RELEASE(&xdr->cx->tempPool, mark);
1988 if (!ok)
1989 return false;
1991 if (xdr->mode == JSXDR_DECODE)
1992 fun->freezeLocalNames(cx);
1995 if (!js_XDRScript(xdr, &fun->u.i.script, false, NULL))
1996 return false;
1998 if (xdr->mode == JSXDR_DECODE) {
1999 *objp = FUN_OBJECT(fun);
2000 if (fun->u.i.script != JSScript::emptyScript()) {
2001 #ifdef CHECK_SCRIPT_OWNER
2002 fun->u.i.script->owner = NULL;
2003 #endif
2004 js_CallNewScriptHook(cx, fun->u.i.script, fun);
2008 return true;
2011 #else /* !JS_HAS_XDR */
2013 #define js_XDRFunctionObject NULL
2015 #endif /* !JS_HAS_XDR */
2018 * [[HasInstance]] internal method for Function objects: fetch the .prototype
2019 * property of its 'this' parameter, and walks the prototype chain of v (only
2020 * if v is an object) returning true if .prototype is found.
2022 static JSBool
2023 fun_hasInstance(JSContext *cx, JSObject *obj, const Value *v, JSBool *bp)
2025 while (obj->isFunction()) {
2026 if (!obj->getFunctionPrivate()->isBound())
2027 break;
2028 obj = obj->getBoundFunctionTarget();
2031 jsid id = ATOM_TO_JSID(cx->runtime->atomState.classPrototypeAtom);
2032 Value pval;
2033 if (!obj->getProperty(cx, id, &pval))
2034 return JS_FALSE;
2036 if (pval.isPrimitive()) {
2038 * Throw a runtime error if instanceof is called on a function that
2039 * has a non-object as its .prototype value.
2041 js_ReportValueError(cx, JSMSG_BAD_PROTOTYPE, -1, ObjectValue(*obj), NULL);
2042 return JS_FALSE;
2045 *bp = js_IsDelegate(cx, &pval.toObject(), *v);
2046 return JS_TRUE;
2049 static void
2050 fun_trace(JSTracer *trc, JSObject *obj)
2052 /* A newborn function object may have a not yet initialized private slot. */
2053 JSFunction *fun = (JSFunction *) obj->getPrivate();
2054 if (!fun)
2055 return;
2057 if (fun != obj) {
2058 /* obj is a cloned function object, trace the clone-parent, fun. */
2059 MarkObject(trc, *fun, "private");
2061 /* The function could be a flat closure with upvar copies in the clone. */
2062 if (FUN_FLAT_CLOSURE(fun) && fun->u.i.nupvars)
2063 MarkValueRange(trc, fun->u.i.nupvars, obj->getFlatClosureUpvars(), "upvars");
2064 return;
2067 if (fun->atom)
2068 MarkString(trc, ATOM_TO_STRING(fun->atom), "atom");
2070 if (FUN_INTERPRETED(fun)) {
2071 if (fun->u.i.script)
2072 js_TraceScript(trc, fun->u.i.script);
2073 for (const Shape *shape = fun->u.i.names; shape; shape = shape->previous())
2074 shape->trace(trc);
2078 static void
2079 fun_finalize(JSContext *cx, JSObject *obj)
2081 /* Ignore newborn function objects. */
2082 JSFunction *fun = (JSFunction *) obj->getPrivate();
2083 if (!fun)
2084 return;
2086 /* Cloned function objects may be flat closures with upvars to free. */
2087 if (fun != obj) {
2088 if (FUN_FLAT_CLOSURE(fun) && fun->u.i.nupvars != 0)
2089 cx->free((void *) obj->getFlatClosureUpvars());
2090 return;
2094 * Null-check of u.i.script is required since the parser sets interpreted
2095 * very early.
2097 if (FUN_INTERPRETED(fun) && fun->u.i.script)
2098 js_DestroyScript(cx, fun->u.i.script);
2102 JSFunction::sharpSlotBase(JSContext *cx)
2104 #if JS_HAS_SHARP_VARS
2105 JSAtom *name = js_Atomize(cx, "#array", 6, 0);
2106 if (name) {
2107 uintN index = uintN(-1);
2108 #ifdef DEBUG
2109 JSLocalKind kind =
2110 #endif
2111 lookupLocal(cx, name, &index);
2112 JS_ASSERT(kind == JSLOCAL_VAR);
2113 return int(index);
2115 #endif
2116 return -1;
2119 uint32
2120 JSFunction::countUpvarSlots() const
2122 JS_ASSERT(FUN_INTERPRETED(this));
2124 return (u.i.nupvars == 0) ? 0 : u.i.script->upvars()->length;
2128 * Reserve two slots in all function objects for XPConnect. Note that this
2129 * does not bloat every instance, only those on which reserved slots are set,
2130 * and those on which ad-hoc properties are defined.
2132 JS_PUBLIC_DATA(Class) js_FunctionClass = {
2133 js_Function_str,
2134 JSCLASS_HAS_PRIVATE | JSCLASS_NEW_RESOLVE |
2135 JSCLASS_HAS_RESERVED_SLOTS(JSFunction::CLASS_RESERVED_SLOTS) |
2136 JSCLASS_MARK_IS_TRACE | JSCLASS_HAS_CACHED_PROTO(JSProto_Function),
2137 PropertyStub, /* addProperty */
2138 PropertyStub, /* delProperty */
2139 PropertyStub, /* getProperty */
2140 PropertyStub, /* setProperty */
2141 fun_enumerate,
2142 (JSResolveOp)fun_resolve,
2143 ConvertStub,
2144 fun_finalize,
2145 NULL, /* reserved0 */
2146 NULL, /* checkAccess */
2147 NULL, /* call */
2148 NULL, /* construct */
2149 js_XDRFunctionObject,
2150 fun_hasInstance,
2151 JS_CLASS_TRACE(fun_trace)
2154 JSString *
2155 fun_toStringHelper(JSContext *cx, JSObject *obj, uintN indent)
2157 if (!obj->isFunction()) {
2158 if (obj->isFunctionProxy())
2159 return JSProxy::fun_toString(cx, obj, indent);
2160 JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
2161 JSMSG_INCOMPATIBLE_PROTO,
2162 js_Function_str, js_toString_str,
2163 "object");
2164 return NULL;
2167 JSFunction *fun = GET_FUNCTION_PRIVATE(cx, obj);
2168 if (!fun)
2169 return NULL;
2170 return JS_DecompileFunction(cx, fun, indent);
2173 static JSBool
2174 fun_toString(JSContext *cx, uintN argc, Value *vp)
2176 JS_ASSERT(IsFunctionObject(vp[0]));
2177 uint32_t indent = 0;
2179 if (argc != 0 && !ValueToECMAUint32(cx, vp[2], &indent))
2180 return false;
2182 JSObject *obj = ComputeThisFromVp(cx, vp);
2183 if (!obj)
2184 return false;
2186 JSString *str = fun_toStringHelper(cx, obj, indent);
2187 if (!str)
2188 return false;
2190 vp->setString(str);
2191 return true;
2194 #if JS_HAS_TOSOURCE
2195 static JSBool
2196 fun_toSource(JSContext *cx, uintN argc, Value *vp)
2198 JS_ASSERT(IsFunctionObject(vp[0]));
2200 JSObject *obj = ComputeThisFromVp(cx, vp);
2201 if (!obj)
2202 return false;
2204 JSString *str = fun_toStringHelper(cx, obj, JS_DONT_PRETTY_PRINT);
2205 if (!str)
2206 return false;
2208 vp->setString(str);
2209 return true;
2211 #endif
2213 JSBool
2214 js_fun_call(JSContext *cx, uintN argc, Value *vp)
2216 LeaveTrace(cx);
2218 JSObject *obj = ComputeThisFromVp(cx, vp);
2219 if (!obj)
2220 return JS_FALSE;
2221 Value fval = vp[1];
2223 if (!js_IsCallable(fval)) {
2224 JSString *str = js_ValueToString(cx, fval);
2225 if (str) {
2226 const char *bytes = js_GetStringBytes(cx, str);
2228 if (bytes) {
2229 JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
2230 JSMSG_INCOMPATIBLE_PROTO,
2231 js_Function_str, js_call_str,
2232 bytes);
2235 return JS_FALSE;
2238 Value *argv = vp + 2;
2239 if (argc == 0) {
2240 /* Call fun with its global object as the 'this' param if no args. */
2241 obj = NULL;
2242 } else {
2243 /* Otherwise convert the first arg to 'this' and skip over it. */
2244 if (argv[0].isObject())
2245 obj = &argv[0].toObject();
2246 else if (!js_ValueToObjectOrNull(cx, argv[0], &obj))
2247 return JS_FALSE;
2248 argc--;
2249 argv++;
2252 /* Allocate stack space for fval, obj, and the args. */
2253 InvokeArgsGuard args;
2254 if (!cx->stack().pushInvokeArgs(cx, argc, &args))
2255 return JS_FALSE;
2257 /* Push fval, obj, and the args. */
2258 args.callee() = fval;
2259 args.thisv().setObjectOrNull(obj);
2260 memcpy(args.argv(), argv, argc * sizeof *argv);
2262 bool ok = Invoke(cx, args, 0);
2263 *vp = args.rval();
2264 return ok;
2267 namespace {
2269 struct CopyNonHoleArgs
2271 CopyNonHoleArgs(JSObject *aobj, Value *dst) : aobj(aobj), dst(dst) {}
2272 JSObject *aobj;
2273 Value *dst;
2274 void operator()(uintN argi, Value *src) {
2275 if (aobj->getArgsElement(argi).isMagic(JS_ARGS_HOLE))
2276 dst->setUndefined();
2277 else
2278 *dst = *src;
2279 ++dst;
2285 /* ES5 15.3.4.3 */
2286 JSBool
2287 js_fun_apply(JSContext *cx, uintN argc, Value *vp)
2289 JSObject *obj = ComputeThisFromVp(cx, vp);
2290 if (!obj)
2291 return false;
2293 /* Step 1. */
2294 Value fval = vp[1];
2295 if (!js_IsCallable(fval)) {
2296 if (JSString *str = js_ValueToString(cx, fval)) {
2297 if (const char *bytes = js_GetStringBytes(cx, str)) {
2298 JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
2299 JSMSG_INCOMPATIBLE_PROTO,
2300 js_Function_str, js_apply_str,
2301 bytes);
2304 return false;
2307 /* Step 2. */
2308 if (argc < 2 || vp[3].isNullOrUndefined())
2309 return js_fun_call(cx, (argc > 0) ? 1 : 0, vp);
2311 /* Step 3. */
2312 if (!vp[3].isObject()) {
2313 JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_BAD_APPLY_ARGS, js_apply_str);
2314 return false;
2318 * Steps 4-5 (note erratum removing steps originally numbered 5 and 7 in
2319 * original version of ES5).
2321 JSObject *aobj = vp[3].toObject().wrappedObject(cx);
2322 jsuint length;
2323 if (aobj->isArray()) {
2324 length = aobj->getArrayLength();
2325 } else if (aobj->isArguments() && !aobj->isArgsLengthOverridden()) {
2326 length = aobj->getArgsInitialLength();
2327 } else {
2328 Value &lenval = vp[0];
2329 if (!aobj->getProperty(cx, ATOM_TO_JSID(cx->runtime->atomState.lengthAtom), &lenval))
2330 return false;
2332 if (lenval.isInt32()) {
2333 length = jsuint(lenval.toInt32()); /* jsuint cast does ToUint32 */
2334 } else {
2335 JS_STATIC_ASSERT(sizeof(jsuint) == sizeof(uint32_t));
2336 if (!ValueToECMAUint32(cx, lenval, (uint32_t *)&length))
2337 return false;
2341 /* Convert the first arg to 'this' and skip over it. */
2342 if (vp[2].isObject())
2343 obj = &vp[2].toObject();
2344 else if (!js_ValueToObjectOrNull(cx, vp[2], &obj))
2345 return JS_FALSE;
2347 LeaveTrace(cx);
2349 /* Step 6. */
2350 uintN n = uintN(JS_MIN(length, JS_ARGS_LENGTH_MAX));
2352 InvokeArgsGuard args;
2353 if (!cx->stack().pushInvokeArgs(cx, n, &args))
2354 return false;
2356 /* Push fval, obj, and aobj's elements as args. */
2357 args.callee() = fval;
2358 args.thisv().setObjectOrNull(obj);
2360 /* Steps 7-8. */
2361 if (aobj && aobj->isArguments() && !aobj->isArgsLengthOverridden()) {
2363 * Two cases, two loops: note how in the case of an active stack frame
2364 * backing aobj, even though we copy from fp->argv, we still must check
2365 * aobj->getArgsElement(i) for a hole, to handle a delete on the
2366 * corresponding arguments element. See args_delProperty.
2368 JSStackFrame *fp = (JSStackFrame *) aobj->getPrivate();
2369 Value *argv = args.argv();
2370 if (fp) {
2371 JS_ASSERT(fp->numActualArgs() <= JS_ARGS_LENGTH_MAX);
2372 fp->forEachCanonicalActualArg(CopyNonHoleArgs(aobj, argv));
2373 } else {
2374 for (uintN i = 0; i < n; i++) {
2375 argv[i] = aobj->getArgsElement(i);
2376 if (argv[i].isMagic(JS_ARGS_HOLE))
2377 argv[i].setUndefined();
2380 } else {
2381 Value *argv = args.argv();
2382 for (uintN i = 0; i < n; i++) {
2383 if (!aobj->getProperty(cx, INT_TO_JSID(jsint(i)), &argv[i]))
2384 return JS_FALSE;
2388 /* Step 9. */
2389 if (!Invoke(cx, args, 0))
2390 return false;
2391 *vp = args.rval();
2392 return true;
2395 namespace {
2397 JSBool
2398 CallOrConstructBoundFunction(JSContext *cx, uintN argc, Value *vp);
2401 bool
2402 JSFunction::isBound() const
2404 return isNative() && u.n.native == CallOrConstructBoundFunction;
2407 inline bool
2408 JSObject::initBoundFunction(JSContext *cx, const Value &thisArg,
2409 const Value *args, uintN argslen)
2411 JS_ASSERT(isFunction());
2412 JS_ASSERT(getFunctionPrivate()->isBound());
2414 fslots[JSSLOT_BOUND_FUNCTION_THIS] = thisArg;
2415 fslots[JSSLOT_BOUND_FUNCTION_ARGS_COUNT].setPrivateUint32(argslen);
2416 if (argslen != 0) {
2417 /* FIXME? Burn memory on an empty scope whose shape covers the args slots. */
2418 EmptyShape *empty = EmptyShape::create(cx, clasp);
2419 if (!empty)
2420 return false;
2422 empty->slotSpan += argslen;
2423 map = empty;
2425 if (!ensureInstanceReservedSlots(cx, argslen))
2426 return false;
2428 JS_ASSERT(dslots);
2429 JS_ASSERT(dslots[-1].toPrivateUint32() >= argslen);
2430 memcpy(&dslots[0], args, argslen * sizeof(Value));
2432 return true;
2435 inline JSObject *
2436 JSObject::getBoundFunctionTarget() const
2438 JS_ASSERT(isFunction());
2439 JS_ASSERT(getFunctionPrivate()->isBound());
2441 /* Bound functions abuse |parent| to store their target function. */
2442 return getParent();
2445 inline const js::Value &
2446 JSObject::getBoundFunctionThis() const
2448 JS_ASSERT(isFunction());
2449 JS_ASSERT(getFunctionPrivate()->isBound());
2451 return fslots[JSSLOT_BOUND_FUNCTION_THIS];
2454 inline const js::Value *
2455 JSObject::getBoundFunctionArguments(uintN &argslen) const
2457 JS_ASSERT(isFunction());
2458 JS_ASSERT(getFunctionPrivate()->isBound());
2460 argslen = fslots[JSSLOT_BOUND_FUNCTION_ARGS_COUNT].toPrivateUint32();
2461 JS_ASSERT_IF(argslen > 0, dslots);
2462 JS_ASSERT_IF(argslen > 0, dslots[-1].toPrivateUint32() >= argslen);
2463 return &dslots[0];
2466 namespace {
2468 /* ES5 15.3.4.5.1 and 15.3.4.5.2. */
2469 JSBool
2470 CallOrConstructBoundFunction(JSContext *cx, uintN argc, Value *vp)
2472 JSObject *obj = &vp[0].toObject();
2473 JS_ASSERT(obj->isFunction());
2474 JS_ASSERT(obj->getFunctionPrivate()->isBound());
2476 LeaveTrace(cx);
2478 bool constructing = IsConstructing(vp);
2480 /* 15.3.4.5.1 step 1, 15.3.4.5.2 step 3. */
2481 uintN argslen;
2482 const Value *boundArgs = obj->getBoundFunctionArguments(argslen);
2484 if (argc + argslen > JS_ARGS_LENGTH_MAX) {
2485 js_ReportAllocationOverflow(cx);
2486 return false;
2489 /* 15.3.4.5.1 step 3, 15.3.4.5.2 step 1. */
2490 JSObject *target = obj->getBoundFunctionTarget();
2492 /* 15.3.4.5.1 step 2. */
2493 const Value &boundThis = obj->getBoundFunctionThis();
2495 InvokeArgsGuard args;
2496 if (!cx->stack().pushInvokeArgs(cx, argc + argslen, &args))
2497 return false;
2499 /* 15.3.4.5.1, 15.3.4.5.2 step 4. */
2500 memcpy(args.argv(), boundArgs, argslen * sizeof(Value));
2501 memcpy(args.argv() + argslen, vp + 2, argc * sizeof(Value));
2503 /* 15.3.4.5.1, 15.3.4.5.2 step 5. */
2504 args.callee().setObject(*target);
2506 if (!constructing) {
2508 * FIXME Pass boundThis directly without boxing! This will go away
2509 * very shortly when this-boxing only occurs for non-strict
2510 * functions, callee-side, in bug 514570.
2512 JSObject *boundThisObj;
2513 if (boundThis.isObjectOrNull()) {
2514 boundThisObj = boundThis.toObjectOrNull();
2515 } else {
2516 if (!js_ValueToObjectOrNull(cx, boundThis, &boundThisObj))
2517 return false;
2520 args.thisv() = ObjectOrNullValue(boundThisObj);
2523 if (constructing ? !InvokeConstructor(cx, args) : !Invoke(cx, args, 0))
2524 return false;
2526 *vp = args.rval();
2527 return true;
2530 /* ES5 15.3.4.5. */
2531 JSBool
2532 fun_bind(JSContext *cx, uintN argc, Value *vp)
2534 /* Step 1. */
2535 JSObject *target = ComputeThisFromVp(cx, vp);
2536 if (!target)
2537 return false;
2539 /* Step 2. */
2540 if (!target->wrappedObject(cx)->isCallable()) {
2541 if (JSString *str = js_ValueToString(cx, vp[1])) {
2542 if (const char *bytes = js_GetStringBytes(cx, str)) {
2543 JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
2544 JSMSG_INCOMPATIBLE_PROTO,
2545 js_Function_str, "bind", bytes);
2548 return false;
2551 /* Step 3. */
2552 Value *args = NULL;
2553 uintN argslen = 0;
2554 if (argc > 1) {
2555 args = vp + 3;
2556 argslen = argc - 1;
2559 /* Steps 15-16. */
2560 uintN length = 0;
2561 if (target->isFunction()) {
2562 uintN nargs = target->getFunctionPrivate()->nargs;
2563 if (nargs > argslen)
2564 length = nargs - argslen;
2567 /* Step 4-6, 10-11. */
2568 JSAtom *name = target->isFunction() ? target->getFunctionPrivate()->atom : NULL;
2570 /* NB: Bound functions abuse |parent| to store their target. */
2571 JSObject *funobj =
2572 js_NewFunction(cx, NULL, CallOrConstructBoundFunction, length,
2573 JSFUN_CONSTRUCTOR, target, name);
2574 if (!funobj)
2575 return false;
2577 /* Steps 7-9. */
2578 Value thisArg = argc >= 1 ? vp[2] : UndefinedValue();
2579 if (!funobj->initBoundFunction(cx, thisArg, args, argslen))
2580 return false;
2582 /* Steps 17, 19-21 are handled by fun_resolve. */
2583 /* Step 18 is the default for new functions. */
2585 /* Step 22. */
2586 vp->setObject(*funobj);
2587 return true;
2592 static JSFunctionSpec function_methods[] = {
2593 #if JS_HAS_TOSOURCE
2594 JS_FN(js_toSource_str, fun_toSource, 0,0),
2595 #endif
2596 JS_FN(js_toString_str, fun_toString, 0,0),
2597 JS_FN(js_apply_str, js_fun_apply, 2,0),
2598 JS_FN(js_call_str, js_fun_call, 1,0),
2599 JS_FN("bind", fun_bind, 1,0),
2600 JS_FS_END
2603 static JSBool
2604 Function(JSContext *cx, uintN argc, Value *vp)
2606 JSObject *obj = NewFunction(cx, NULL);
2607 if (!obj)
2608 return JS_FALSE;
2610 /* N.B. overwriting callee with return value */
2611 JSObject *parent = vp[0].toObject().getParent();
2612 vp[0].setObject(*obj);
2615 * NB: (new Function) is not lexically closed by its caller, it's just an
2616 * anonymous function in the top-level scope that its constructor inhabits.
2617 * Thus 'var x = 42; f = new Function("return x"); print(f())' prints 42,
2618 * and so would a call to f from another top-level's script or function.
2620 * In older versions, before call objects, a new Function was adopted by
2621 * its running context's globalObject, which might be different from the
2622 * top-level reachable from scopeChain (in HTML frames, e.g.).
2624 JSFunction *fun = js_NewFunction(cx, obj, NULL, 0, JSFUN_LAMBDA | JSFUN_INTERPRETED,
2625 parent, cx->runtime->atomState.anonymousAtom);
2626 if (!fun)
2627 return JS_FALSE;
2630 * Function is static and not called directly by other functions in this
2631 * file, therefore it is callable only as a native function by js_Invoke.
2632 * Find the scripted caller, possibly skipping other native frames such as
2633 * are built for Function.prototype.call or .apply activations that invoke
2634 * Function indirectly from a script.
2636 JSStackFrame *caller = js_GetScriptedCaller(cx, NULL);
2637 uintN lineno;
2638 const char *filename;
2639 JSPrincipals *principals;
2640 if (caller) {
2641 JSObject *callee = &JS_CALLEE(cx, vp).toObject();
2642 principals = js_EvalFramePrincipals(cx, callee, caller);
2643 filename = js_ComputeFilename(cx, caller, principals, &lineno);
2644 } else {
2645 filename = NULL;
2646 lineno = 0;
2647 principals = NULL;
2650 /* Belt-and-braces: check that the caller has access to parent. */
2651 if (!js_CheckPrincipalsAccess(cx, parent, principals,
2652 CLASS_ATOM(cx, Function))) {
2653 return JS_FALSE;
2657 * CSP check: whether new Function() is allowed at all.
2658 * Report errors via CSP is done in the script security manager.
2659 * js_CheckContentSecurityPolicy is defined in jsobj.cpp
2661 if (!js_CheckContentSecurityPolicy(cx)) {
2662 JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_CSP_BLOCKED_FUNCTION);
2663 return JS_FALSE;
2666 Value *argv = vp + 2;
2667 uintN n = argc ? argc - 1 : 0;
2668 if (n > 0) {
2669 enum { OK, BAD, BAD_FORMAL } state;
2672 * Collect the function-argument arguments into one string, separated
2673 * by commas, then make a tokenstream from that string, and scan it to
2674 * get the arguments. We need to throw the full scanner at the
2675 * problem, because the argument string can legitimately contain
2676 * comments and linefeeds. XXX It might be better to concatenate
2677 * everything up into a function definition and pass it to the
2678 * compiler, but doing it this way is less of a delta from the old
2679 * code. See ECMA 15.3.2.1.
2681 state = BAD_FORMAL;
2682 size_t args_length = 0;
2683 for (uintN i = 0; i < n; i++) {
2684 /* Collect the lengths for all the function-argument arguments. */
2685 JSString *arg = js_ValueToString(cx, argv[i]);
2686 if (!arg)
2687 return JS_FALSE;
2688 argv[i].setString(arg);
2691 * Check for overflow. The < test works because the maximum
2692 * JSString length fits in 2 fewer bits than size_t has.
2694 size_t old_args_length = args_length;
2695 args_length = old_args_length + arg->length();
2696 if (args_length < old_args_length) {
2697 js_ReportAllocationOverflow(cx);
2698 return JS_FALSE;
2702 /* Add 1 for each joining comma and check for overflow (two ways). */
2703 size_t old_args_length = args_length;
2704 args_length = old_args_length + n - 1;
2705 if (args_length < old_args_length ||
2706 args_length >= ~(size_t)0 / sizeof(jschar)) {
2707 js_ReportAllocationOverflow(cx);
2708 return JS_FALSE;
2712 * Allocate a string to hold the concatenated arguments, including room
2713 * for a terminating 0. Mark cx->tempPool for later release, to free
2714 * collected_args and its tokenstream in one swoop.
2716 void *mark = JS_ARENA_MARK(&cx->tempPool);
2717 jschar *cp;
2718 JS_ARENA_ALLOCATE_CAST(cp, jschar *, &cx->tempPool,
2719 (args_length+1) * sizeof(jschar));
2720 if (!cp) {
2721 js_ReportOutOfScriptQuota(cx);
2722 return JS_FALSE;
2724 jschar *collected_args = cp;
2727 * Concatenate the arguments into the new string, separated by commas.
2729 for (uintN i = 0; i < n; i++) {
2730 JSString *arg = argv[i].toString();
2731 size_t arg_length = arg->length();
2732 (void) js_strncpy(cp, arg->chars(), arg_length);
2733 cp += arg_length;
2735 /* Add separating comma or terminating 0. */
2736 *cp++ = (i + 1 < n) ? ',' : 0;
2739 /* Initialize a tokenstream that reads from the given string. */
2740 TokenStream ts(cx);
2741 if (!ts.init(cx->findVersion(), collected_args, args_length, NULL, filename, lineno)) {
2742 JS_ARENA_RELEASE(&cx->tempPool, mark);
2743 return JS_FALSE;
2746 /* The argument string may be empty or contain no tokens. */
2747 TokenKind tt = ts.getToken();
2748 if (tt != TOK_EOF) {
2749 for (;;) {
2751 * Check that it's a name. This also implicitly guards against
2752 * TOK_ERROR, which was already reported.
2754 if (tt != TOK_NAME)
2755 goto after_args;
2758 * Get the atom corresponding to the name from the token
2759 * stream; we're assured at this point that it's a valid
2760 * identifier.
2762 JSAtom *atom = ts.currentToken().t_atom;
2764 /* Check for a duplicate parameter name. */
2765 if (fun->lookupLocal(cx, atom, NULL) != JSLOCAL_NONE) {
2766 const char *name;
2768 name = js_AtomToPrintableString(cx, atom);
2769 if (!name && ReportCompileErrorNumber(cx, &ts, NULL,
2770 JSREPORT_WARNING | JSREPORT_STRICT,
2771 JSMSG_DUPLICATE_FORMAL, name)) {
2772 goto after_args;
2775 if (!fun->addLocal(cx, atom, JSLOCAL_ARG))
2776 goto after_args;
2779 * Get the next token. Stop on end of stream. Otherwise
2780 * insist on a comma, get another name, and iterate.
2782 tt = ts.getToken();
2783 if (tt == TOK_EOF)
2784 break;
2785 if (tt != TOK_COMMA)
2786 goto after_args;
2787 tt = ts.getToken();
2791 state = OK;
2792 after_args:
2793 if (state == BAD_FORMAL && !ts.isError()) {
2795 * Report "malformed formal parameter" iff no illegal char or
2796 * similar scanner error was already reported.
2798 JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
2799 JSMSG_BAD_FORMAL);
2801 ts.close();
2802 JS_ARENA_RELEASE(&cx->tempPool, mark);
2803 if (state != OK)
2804 return JS_FALSE;
2807 JSString *str;
2808 if (argc) {
2809 str = js_ValueToString(cx, argv[argc-1]);
2810 if (!str)
2811 return JS_FALSE;
2812 argv[argc-1].setString(str);
2813 } else {
2814 str = cx->runtime->emptyString;
2817 return Compiler::compileFunctionBody(cx, fun, principals,
2818 str->chars(), str->length(),
2819 filename, lineno);
2822 namespace {
2824 JSBool
2825 ThrowTypeError(JSContext *cx, uintN argc, Value *vp)
2827 JS_ReportErrorFlagsAndNumber(cx, JSREPORT_ERROR, js_GetErrorMessage, NULL,
2828 JSMSG_THROW_TYPE_ERROR);
2829 return false;
2834 JSObject *
2835 js_InitFunctionClass(JSContext *cx, JSObject *obj)
2837 JSObject *proto = js_InitClass(cx, obj, NULL, &js_FunctionClass, Function, 1,
2838 NULL, function_methods, NULL, NULL);
2839 if (!proto)
2840 return NULL;
2842 JSFunction *fun = js_NewFunction(cx, proto, NULL, 0, JSFUN_INTERPRETED, obj, NULL);
2843 if (!fun)
2844 return NULL;
2845 fun->u.i.script = JSScript::emptyScript();
2847 if (obj->getClass()->flags & JSCLASS_IS_GLOBAL) {
2848 /* ES5 13.2.3: Construct the unique [[ThrowTypeError]] function object. */
2849 JSObject *throwTypeError =
2850 js_NewFunction(cx, NULL, reinterpret_cast<Native>(ThrowTypeError), 0,
2851 0, obj, NULL);
2852 if (!throwTypeError)
2853 return NULL;
2855 JS_ALWAYS_TRUE(js_SetReservedSlot(cx, obj, JSRESERVED_GLOBAL_THROWTYPEERROR,
2856 ObjectValue(*throwTypeError)));
2859 return proto;
2862 JSFunction *
2863 js_NewFunction(JSContext *cx, JSObject *funobj, Native native, uintN nargs,
2864 uintN flags, JSObject *parent, JSAtom *atom)
2866 JSFunction *fun;
2868 if (funobj) {
2869 JS_ASSERT(funobj->isFunction());
2870 funobj->setParent(parent);
2871 } else {
2872 funobj = NewFunction(cx, parent);
2873 if (!funobj)
2874 return NULL;
2876 JS_ASSERT(!funobj->getPrivate());
2877 fun = (JSFunction *) funobj;
2879 /* Initialize all function members. */
2880 fun->nargs = uint16(nargs);
2881 fun->flags = flags & (JSFUN_FLAGS_MASK | JSFUN_KINDMASK | JSFUN_TRCINFO);
2882 if ((flags & JSFUN_KINDMASK) >= JSFUN_INTERPRETED) {
2883 JS_ASSERT(!native);
2884 JS_ASSERT(nargs == 0);
2885 fun->u.i.nvars = 0;
2886 fun->u.i.nupvars = 0;
2887 fun->u.i.skipmin = 0;
2888 fun->u.i.wrapper = false;
2889 fun->u.i.script = NULL;
2890 fun->u.i.names = cx->runtime->emptyCallShape;
2891 } else {
2892 fun->u.n.clasp = NULL;
2893 if (flags & JSFUN_TRCINFO) {
2894 #ifdef JS_TRACER
2895 JSNativeTraceInfo *trcinfo =
2896 JS_FUNC_TO_DATA_PTR(JSNativeTraceInfo *, native);
2897 fun->u.n.native = (js::Native) trcinfo->native;
2898 fun->u.n.trcinfo = trcinfo;
2899 #else
2900 fun->u.n.trcinfo = NULL;
2901 #endif
2902 } else {
2903 fun->u.n.native = native;
2904 fun->u.n.trcinfo = NULL;
2906 JS_ASSERT(fun->u.n.native);
2908 fun->atom = atom;
2910 /* Set private to self to indicate non-cloned fully initialized function. */
2911 FUN_OBJECT(fun)->setPrivate(fun);
2912 return fun;
2915 JSObject * JS_FASTCALL
2916 js_CloneFunctionObject(JSContext *cx, JSFunction *fun, JSObject *parent,
2917 JSObject *proto)
2919 JS_ASSERT(parent);
2920 JS_ASSERT(proto);
2923 * The cloned function object does not need the extra JSFunction members
2924 * beyond JSObject as it points to fun via the private slot.
2926 JSObject *clone = NewNativeClassInstance(cx, &js_FunctionClass, proto, parent);
2927 if (!clone)
2928 return NULL;
2929 clone->setPrivate(fun);
2930 return clone;
2933 #ifdef JS_TRACER
2934 JS_DEFINE_CALLINFO_4(extern, OBJECT, js_CloneFunctionObject, CONTEXT, FUNCTION, OBJECT, OBJECT, 0,
2935 nanojit::ACCSET_STORE_ANY)
2936 #endif
2939 * Create a new flat closure, but don't initialize the imported upvar
2940 * values. The tracer calls this function and then initializes the upvar
2941 * slots on trace.
2943 JSObject * JS_FASTCALL
2944 js_AllocFlatClosure(JSContext *cx, JSFunction *fun, JSObject *scopeChain)
2946 JS_ASSERT(FUN_FLAT_CLOSURE(fun));
2947 JS_ASSERT((fun->u.i.script->upvarsOffset
2948 ? fun->u.i.script->upvars()->length
2949 : 0) == fun->u.i.nupvars);
2951 JSObject *closure = CloneFunctionObject(cx, fun, scopeChain);
2952 if (!closure)
2953 return closure;
2955 uint32 nslots = fun->countUpvarSlots();
2956 if (nslots == 0)
2957 return closure;
2959 Value *upvars = (Value *) cx->malloc(nslots * sizeof(Value));
2960 if (!upvars)
2961 return NULL;
2963 closure->setFlatClosureUpvars(upvars);
2964 return closure;
2967 JS_DEFINE_CALLINFO_3(extern, OBJECT, js_AllocFlatClosure,
2968 CONTEXT, FUNCTION, OBJECT, 0, nanojit::ACCSET_STORE_ANY)
2970 JS_REQUIRES_STACK JSObject *
2971 js_NewFlatClosure(JSContext *cx, JSFunction *fun)
2974 * Flat closures can be partial, they may need to search enclosing scope
2975 * objects via JSOP_NAME, etc.
2977 JSObject *scopeChain = js_GetScopeChain(cx, cx->fp());
2978 if (!scopeChain)
2979 return NULL;
2981 JSObject *closure = js_AllocFlatClosure(cx, fun, scopeChain);
2982 if (!closure || fun->u.i.nupvars == 0)
2983 return closure;
2985 Value *upvars = closure->getFlatClosureUpvars();
2986 uintN level = fun->u.i.script->staticLevel;
2987 JSUpvarArray *uva = fun->u.i.script->upvars();
2989 for (uint32 i = 0, n = uva->length; i < n; i++)
2990 upvars[i] = GetUpvar(cx, level, uva->vector[i]);
2992 return closure;
2995 JSObject *
2996 js_NewDebuggableFlatClosure(JSContext *cx, JSFunction *fun)
2998 JS_ASSERT(cx->fp()->fun()->flags & JSFUN_HEAVYWEIGHT);
2999 JS_ASSERT(!cx->fp()->fun()->optimizedClosure());
3000 JS_ASSERT(FUN_FLAT_CLOSURE(fun));
3002 return WrapEscapingClosure(cx, cx->fp(), fun);
3005 JSFunction *
3006 js_DefineFunction(JSContext *cx, JSObject *obj, JSAtom *atom, Native native,
3007 uintN nargs, uintN attrs)
3009 PropertyOp gsop;
3010 JSFunction *fun;
3012 if (attrs & JSFUN_STUB_GSOPS) {
3014 * JSFUN_STUB_GSOPS is a request flag only, not stored in fun->flags or
3015 * the defined property's attributes. This allows us to encode another,
3016 * internal flag using the same bit, JSFUN_EXPR_CLOSURE -- see jsfun.h
3017 * for more on this.
3019 attrs &= ~JSFUN_STUB_GSOPS;
3020 gsop = PropertyStub;
3021 } else {
3022 gsop = NULL;
3024 fun = js_NewFunction(cx, NULL, native, nargs,
3025 attrs & (JSFUN_FLAGS_MASK | JSFUN_TRCINFO),
3026 obj, atom);
3027 if (!fun)
3028 return NULL;
3029 if (!obj->defineProperty(cx, ATOM_TO_JSID(atom), ObjectValue(*fun),
3030 gsop, gsop, attrs & ~JSFUN_FLAGS_MASK)) {
3031 return NULL;
3033 return fun;
3036 #if (JSV2F_CONSTRUCT & JSV2F_SEARCH_STACK)
3037 # error "JSINVOKE_CONSTRUCT and JSV2F_SEARCH_STACK are not disjoint!"
3038 #endif
3040 JSFunction *
3041 js_ValueToFunction(JSContext *cx, const Value *vp, uintN flags)
3043 JSObject *funobj;
3044 if (!IsFunctionObject(*vp, &funobj)) {
3045 js_ReportIsNotFunction(cx, vp, flags);
3046 return NULL;
3048 return GET_FUNCTION_PRIVATE(cx, funobj);
3051 JSObject *
3052 js_ValueToFunctionObject(JSContext *cx, Value *vp, uintN flags)
3054 JSObject *funobj;
3055 if (!IsFunctionObject(*vp, &funobj)) {
3056 js_ReportIsNotFunction(cx, vp, flags);
3057 return NULL;
3060 return funobj;
3063 JSObject *
3064 js_ValueToCallableObject(JSContext *cx, Value *vp, uintN flags)
3066 if (vp->isObject()) {
3067 JSObject *callable = &vp->toObject();
3068 if (callable->isCallable())
3069 return callable;
3072 js_ReportIsNotFunction(cx, vp, flags);
3073 return NULL;
3076 void
3077 js_ReportIsNotFunction(JSContext *cx, const Value *vp, uintN flags)
3079 const char *name = NULL, *source = NULL;
3080 AutoValueRooter tvr(cx);
3081 uintN error = (flags & JSV2F_CONSTRUCT) ? JSMSG_NOT_CONSTRUCTOR : JSMSG_NOT_FUNCTION;
3082 LeaveTrace(cx);
3085 * We try to the print the code that produced vp if vp is a value in the
3086 * most recent interpreted stack frame. Note that additional values, not
3087 * directly produced by the script, may have been pushed onto the frame's
3088 * expression stack (e.g. by pushInvokeArgs) thereby incrementing sp past
3089 * the depth simulated by ReconstructPCStack. Since we must pass an offset
3090 * from the top of the simulated stack to js_ReportValueError3, it is
3091 * important to do bounds checking using the simulated, rather than actual,
3092 * stack depth.
3094 ptrdiff_t spindex = 0;
3096 FrameRegsIter i(cx);
3097 while (!i.done() && !i.pc())
3098 ++i;
3100 if (!i.done()) {
3101 uintN depth = js_ReconstructStackDepth(cx, i.fp()->script(), i.pc());
3102 Value *simsp = i.fp()->base() + depth;
3103 JS_ASSERT(simsp <= i.sp());
3104 if (i.fp()->base() <= vp && vp < simsp)
3105 spindex = vp - simsp;
3108 if (!spindex)
3109 spindex = ((flags & JSV2F_SEARCH_STACK) ? JSDVG_SEARCH_STACK : JSDVG_IGNORE_STACK);
3111 js_ReportValueError3(cx, error, spindex, *vp, NULL, name, source);
3114 const Shape *
3115 JSFunction::lastArg() const
3117 const Shape *shape = lastVar();
3118 if (u.i.nvars != 0) {
3119 while (shape->previous() && shape->getter() != js_GetCallArg)
3120 shape = shape->previous();
3122 return shape;
3125 const Shape *
3126 JSFunction::lastVar() const
3128 const Shape *shape = u.i.names;
3129 if (u.i.nupvars != 0) {
3130 while (shape->getter() == GetFlatUpvar)
3131 shape = shape->previous();
3133 return shape;
3136 bool
3137 JSFunction::addLocal(JSContext *cx, JSAtom *atom, JSLocalKind kind)
3139 JS_ASSERT(FUN_INTERPRETED(this));
3140 JS_ASSERT(!u.i.script);
3143 * We still follow 10.2.3 of ES3 and make argument and variable properties
3144 * of the Call objects enumerable. ES5 reformulated all of its Clause 10 to
3145 * avoid objects as activations, something we should do too.
3147 uintN attrs = JSPROP_ENUMERATE | JSPROP_PERMANENT | JSPROP_SHARED;
3148 uint16 *indexp;
3149 PropertyOp getter, setter;
3150 uint32 slot = JSSLOT_START(&js_CallClass) + JSObject::CALL_RESERVED_SLOTS;
3152 if (kind == JSLOCAL_ARG) {
3153 JS_ASSERT(u.i.nupvars == 0);
3155 indexp = &nargs;
3156 getter = js_GetCallArg;
3157 setter = SetCallArg;
3158 slot += nargs;
3159 } else if (kind == JSLOCAL_UPVAR) {
3160 indexp = &u.i.nupvars;
3161 getter = GetFlatUpvar;
3162 setter = SetFlatUpvar;
3163 slot = SHAPE_INVALID_SLOT;
3164 } else {
3165 JS_ASSERT(u.i.nupvars == 0);
3167 indexp = &u.i.nvars;
3168 getter = js_GetCallVar;
3169 setter = SetCallVar;
3170 if (kind == JSLOCAL_CONST)
3171 attrs |= JSPROP_READONLY;
3172 else
3173 JS_ASSERT(kind == JSLOCAL_VAR);
3174 slot += nargs + u.i.nvars;
3177 if (*indexp == JS_BITMASK(16)) {
3178 JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
3179 (kind == JSLOCAL_ARG)
3180 ? JSMSG_TOO_MANY_FUN_ARGS
3181 : JSMSG_TOO_MANY_LOCALS);
3182 return false;
3185 Shape **listp = &u.i.names;
3186 Shape *parent = *listp;
3187 jsid id;
3190 * The destructuring formal parameter parser adds a null atom, which we
3191 * encode as an INT id. The parser adds such locals after adding vars for
3192 * the destructured-to parameter bindings -- those must be vars to avoid
3193 * aliasing arguments[i] for any i -- so we must switch u.i.names to a
3194 * dictionary list to cope with insertion "in the middle" of an index-named
3195 * shape for the object or array argument.
3197 bool findArgInsertionPoint = false;
3198 if (!atom) {
3199 JS_ASSERT(kind == JSLOCAL_ARG);
3200 if (u.i.nvars != 0) {
3202 * A dictionary list needed only if the destructing pattern wasn't
3203 * empty, i.e., there were vars for its destructured-to bindings.
3205 if (!parent->inDictionary() && !(parent = Shape::newDictionaryList(cx, listp)))
3206 return false;
3207 findArgInsertionPoint = true;
3209 id = INT_TO_JSID(nargs);
3210 } else {
3211 if (kind == JSLOCAL_ARG && parent->inDictionary())
3212 findArgInsertionPoint = true;
3213 id = ATOM_TO_JSID(atom);
3216 if (findArgInsertionPoint) {
3217 while (parent->parent && parent->getter() != js_GetCallArg) {
3218 ++parent->slot;
3219 JS_ASSERT(parent->slot == parent->slotSpan);
3220 ++parent->slotSpan;
3221 listp = &parent->parent;
3222 parent = *listp;
3226 Shape child(id, getter, setter, slot, attrs, Shape::HAS_SHORTID, *indexp);
3228 Shape *shape = parent->getChild(cx, child, listp);
3229 if (!shape)
3230 return false;
3232 JS_ASSERT_IF(!shape->inDictionary(), u.i.names == shape);
3233 ++*indexp;
3234 return true;
3237 JSLocalKind
3238 JSFunction::lookupLocal(JSContext *cx, JSAtom *atom, uintN *indexp)
3240 JS_ASSERT(FUN_INTERPRETED(this));
3242 Shape *shape = SHAPE_FETCH(Shape::search(&u.i.names, ATOM_TO_JSID(atom)));
3243 if (shape) {
3244 JSLocalKind localKind;
3246 if (shape->getter() == js_GetCallArg)
3247 localKind = JSLOCAL_ARG;
3248 else if (shape->getter() == GetFlatUpvar)
3249 localKind = JSLOCAL_UPVAR;
3250 else if (!shape->writable())
3251 localKind = JSLOCAL_CONST;
3252 else
3253 localKind = JSLOCAL_VAR;
3255 if (indexp)
3256 *indexp = shape->shortid;
3257 return localKind;
3259 return JSLOCAL_NONE;
3262 jsuword *
3263 JSFunction::getLocalNameArray(JSContext *cx, JSArenaPool *pool)
3265 JS_ASSERT(hasLocalNames());
3267 uintN n = countLocalNames();
3268 jsuword *names;
3271 * No need to check for overflow of the allocation size as we are making a
3272 * copy of already allocated data. As such it must fit size_t.
3274 JS_ARENA_ALLOCATE_CAST(names, jsuword *, pool, size_t(n) * sizeof *names);
3275 if (!names) {
3276 js_ReportOutOfScriptQuota(cx);
3277 return NULL;
3280 #ifdef DEBUG
3281 for (uintN i = 0; i != n; i++)
3282 names[i] = 0xdeadbeef;
3283 #endif
3285 for (Shape::Range r = u.i.names; !r.empty(); r.popFront()) {
3286 const Shape &shape = r.front();
3287 uintN index = uint16(shape.shortid);
3288 jsuword constFlag = 0;
3290 if (shape.getter() == js_GetCallArg) {
3291 JS_ASSERT(index < nargs);
3292 } else if (shape.getter() == GetFlatUpvar) {
3293 JS_ASSERT(index < u.i.nupvars);
3294 index += nargs + u.i.nvars;
3295 } else {
3296 JS_ASSERT(index < u.i.nvars);
3297 index += nargs;
3298 if (!shape.writable())
3299 constFlag = 1;
3302 JSAtom *atom;
3303 if (JSID_IS_ATOM(shape.id)) {
3304 atom = JSID_TO_ATOM(shape.id);
3305 } else {
3306 JS_ASSERT(JSID_IS_INT(shape.id));
3307 JS_ASSERT(shape.getter() == js_GetCallArg);
3308 atom = NULL;
3311 names[index] = jsuword(atom);
3314 #ifdef DEBUG
3315 for (uintN i = 0; i != n; i++)
3316 JS_ASSERT(names[i] != 0xdeadbeef);
3317 #endif
3318 return names;
3321 void
3322 JSFunction::freezeLocalNames(JSContext *cx)
3324 JS_ASSERT(FUN_INTERPRETED(this));
3326 Shape *shape = u.i.names;
3327 if (shape->inDictionary()) {
3328 do {
3329 JS_ASSERT(!shape->frozen());
3330 shape->setFrozen();
3331 } while ((shape = shape->parent) != NULL);
3336 * This method is called only if we parsed a duplicate formal. Let's use the
3337 * simplest possible algorithm, risking O(n^2) pain -- anyone dup'ing formals
3338 * is asking for it!
3340 JSAtom *
3341 JSFunction::findDuplicateFormal() const
3343 JS_ASSERT(isInterpreted());
3345 if (nargs <= 1)
3346 return NULL;
3348 for (Shape::Range r = lastArg(); !r.empty(); r.popFront()) {
3349 const Shape &shape = r.front();
3350 for (Shape::Range r2 = shape.previous(); !r2.empty(); r2.popFront()) {
3351 if (r2.front().id == shape.id)
3352 return JSID_TO_ATOM(shape.id);
3355 return NULL;