2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
2008-09-04 21:56:37 -07:00
|
|
|
* vim: set ts=8 sw=4 et tw=99:
|
2007-03-22 10:30:00 -07:00
|
|
|
*
|
|
|
|
* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is Mozilla Communicator client code, released
|
|
|
|
* March 31, 1998.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either of the GNU General Public License Version 2 or later (the "GPL"),
|
|
|
|
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* JS function support.
|
|
|
|
*/
|
|
|
|
#include "jsstddef.h"
|
|
|
|
#include <string.h>
|
|
|
|
#include "jstypes.h"
|
|
|
|
#include "jsbit.h"
|
|
|
|
#include "jsutil.h" /* Added by JSIFY */
|
|
|
|
#include "jsapi.h"
|
|
|
|
#include "jsarray.h"
|
|
|
|
#include "jsatom.h"
|
2008-10-08 15:08:33 -07:00
|
|
|
#include "jsbuiltins.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "jscntxt.h"
|
2008-09-05 10:19:17 -07:00
|
|
|
#include "jsversion.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "jsdbgapi.h"
|
|
|
|
#include "jsfun.h"
|
|
|
|
#include "jsgc.h"
|
|
|
|
#include "jsinterp.h"
|
|
|
|
#include "jslock.h"
|
|
|
|
#include "jsnum.h"
|
|
|
|
#include "jsobj.h"
|
|
|
|
#include "jsopcode.h"
|
|
|
|
#include "jsparse.h"
|
|
|
|
#include "jsscan.h"
|
|
|
|
#include "jsscope.h"
|
|
|
|
#include "jsscript.h"
|
|
|
|
#include "jsstr.h"
|
|
|
|
#include "jsexn.h"
|
2008-09-08 10:51:10 -07:00
|
|
|
#include "jsstaticcheck.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#if JS_HAS_GENERATORS
|
|
|
|
# include "jsiter.h"
|
|
|
|
#endif
|
|
|
|
|
2008-02-08 16:01:45 -08:00
|
|
|
#if JS_HAS_XDR
|
|
|
|
# include "jsxdrapi.h"
|
|
|
|
#endif
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/* Generic function/call/arguments tinyids -- also reflected bit numbers. */
|
|
|
|
enum {
|
|
|
|
CALL_ARGUMENTS = -1, /* predefined arguments local variable */
|
2007-06-17 22:10:19 -07:00
|
|
|
ARGS_LENGTH = -2, /* number of actual args, arity if inactive */
|
|
|
|
ARGS_CALLEE = -3, /* reference from arguments to active funobj */
|
|
|
|
FUN_ARITY = -4, /* number of formal parameters; desired argc */
|
|
|
|
FUN_NAME = -5, /* function name, "" if anonymous */
|
|
|
|
FUN_CALLER = -6 /* Function.prototype.caller, backward compat */
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
#if JSFRAME_OVERRIDE_BITS < 8
|
|
|
|
# error "not enough override bits in JSStackFrame.flags!"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define TEST_OVERRIDE_BIT(fp, tinyid) \
|
|
|
|
((fp)->flags & JS_BIT(JSFRAME_OVERRIDE_SHIFT - ((tinyid) + 1)))
|
|
|
|
|
|
|
|
#define SET_OVERRIDE_BIT(fp, tinyid) \
|
|
|
|
((fp)->flags |= JS_BIT(JSFRAME_OVERRIDE_SHIFT - ((tinyid) + 1)))
|
|
|
|
|
|
|
|
JSBool
|
|
|
|
js_GetArgsValue(JSContext *cx, JSStackFrame *fp, jsval *vp)
|
|
|
|
{
|
|
|
|
JSObject *argsobj;
|
|
|
|
|
|
|
|
if (TEST_OVERRIDE_BIT(fp, CALL_ARGUMENTS)) {
|
|
|
|
JS_ASSERT(fp->callobj);
|
|
|
|
return OBJ_GET_PROPERTY(cx, fp->callobj,
|
|
|
|
ATOM_TO_JSID(cx->runtime->atomState
|
|
|
|
.argumentsAtom),
|
|
|
|
vp);
|
|
|
|
}
|
|
|
|
argsobj = js_GetArgsObject(cx, fp);
|
|
|
|
if (!argsobj)
|
|
|
|
return JS_FALSE;
|
|
|
|
*vp = OBJECT_TO_JSVAL(argsobj);
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
|
|
|
MarkArgDeleted(JSContext *cx, JSStackFrame *fp, uintN slot)
|
|
|
|
{
|
|
|
|
JSObject *argsobj;
|
|
|
|
jsval bmapval, bmapint;
|
|
|
|
size_t nbits, nbytes;
|
|
|
|
jsbitmap *bitmap;
|
|
|
|
|
|
|
|
argsobj = fp->argsobj;
|
|
|
|
(void) JS_GetReservedSlot(cx, argsobj, 0, &bmapval);
|
|
|
|
nbits = fp->argc;
|
|
|
|
JS_ASSERT(slot < nbits);
|
|
|
|
if (JSVAL_IS_VOID(bmapval)) {
|
|
|
|
if (nbits <= JSVAL_INT_BITS) {
|
|
|
|
bmapint = 0;
|
|
|
|
bitmap = (jsbitmap *) &bmapint;
|
|
|
|
} else {
|
|
|
|
nbytes = JS_HOWMANY(nbits, JS_BITS_PER_WORD) * sizeof(jsbitmap);
|
|
|
|
bitmap = (jsbitmap *) JS_malloc(cx, nbytes);
|
|
|
|
if (!bitmap)
|
|
|
|
return JS_FALSE;
|
|
|
|
memset(bitmap, 0, nbytes);
|
|
|
|
bmapval = PRIVATE_TO_JSVAL(bitmap);
|
|
|
|
JS_SetReservedSlot(cx, argsobj, 0, bmapval);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (nbits <= JSVAL_INT_BITS) {
|
|
|
|
bmapint = JSVAL_TO_INT(bmapval);
|
|
|
|
bitmap = (jsbitmap *) &bmapint;
|
|
|
|
} else {
|
|
|
|
bitmap = (jsbitmap *) JSVAL_TO_PRIVATE(bmapval);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
JS_SET_BIT(bitmap, slot);
|
|
|
|
if (bitmap == (jsbitmap *) &bmapint) {
|
|
|
|
bmapval = INT_TO_JSVAL(bmapint);
|
|
|
|
JS_SetReservedSlot(cx, argsobj, 0, bmapval);
|
|
|
|
}
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* NB: Infallible predicate, false does not mean error/exception. */
|
|
|
|
static JSBool
|
|
|
|
ArgWasDeleted(JSContext *cx, JSStackFrame *fp, uintN slot)
|
|
|
|
{
|
|
|
|
JSObject *argsobj;
|
|
|
|
jsval bmapval, bmapint;
|
|
|
|
jsbitmap *bitmap;
|
|
|
|
|
|
|
|
argsobj = fp->argsobj;
|
|
|
|
(void) JS_GetReservedSlot(cx, argsobj, 0, &bmapval);
|
|
|
|
if (JSVAL_IS_VOID(bmapval))
|
|
|
|
return JS_FALSE;
|
|
|
|
if (fp->argc <= JSVAL_INT_BITS) {
|
|
|
|
bmapint = JSVAL_TO_INT(bmapval);
|
|
|
|
bitmap = (jsbitmap *) &bmapint;
|
|
|
|
} else {
|
|
|
|
bitmap = (jsbitmap *) JSVAL_TO_PRIVATE(bmapval);
|
|
|
|
}
|
|
|
|
return JS_TEST_BIT(bitmap, slot) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSBool
|
2007-06-14 00:07:01 -07:00
|
|
|
js_GetArgsProperty(JSContext *cx, JSStackFrame *fp, jsid id, jsval *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
jsval val;
|
|
|
|
JSObject *obj;
|
|
|
|
uintN slot;
|
|
|
|
|
|
|
|
if (TEST_OVERRIDE_BIT(fp, CALL_ARGUMENTS)) {
|
|
|
|
JS_ASSERT(fp->callobj);
|
|
|
|
if (!OBJ_GET_PROPERTY(cx, fp->callobj,
|
|
|
|
ATOM_TO_JSID(cx->runtime->atomState
|
|
|
|
.argumentsAtom),
|
|
|
|
&val)) {
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
if (JSVAL_IS_PRIMITIVE(val)) {
|
|
|
|
obj = js_ValueToNonNullObject(cx, val);
|
|
|
|
if (!obj)
|
|
|
|
return JS_FALSE;
|
|
|
|
} else {
|
|
|
|
obj = JSVAL_TO_OBJECT(val);
|
|
|
|
}
|
|
|
|
return OBJ_GET_PROPERTY(cx, obj, id, vp);
|
|
|
|
}
|
|
|
|
|
|
|
|
*vp = JSVAL_VOID;
|
|
|
|
if (JSID_IS_INT(id)) {
|
|
|
|
slot = (uintN) JSID_TO_INT(id);
|
|
|
|
if (slot < fp->argc) {
|
|
|
|
if (fp->argsobj && ArgWasDeleted(cx, fp, slot))
|
|
|
|
return OBJ_GET_PROPERTY(cx, fp->argsobj, id, vp);
|
|
|
|
*vp = fp->argv[slot];
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Per ECMA-262 Ed. 3, 10.1.8, last bulleted item, do not share
|
|
|
|
* storage between the formal parameter and arguments[k] for all
|
2007-08-01 21:33:52 -07:00
|
|
|
* fp->argc <= k && k < fp->fun->nargs. For example, in
|
2007-03-22 10:30:00 -07:00
|
|
|
*
|
|
|
|
* function f(x) { x = 42; return arguments[0]; }
|
|
|
|
* f();
|
|
|
|
*
|
|
|
|
* the call to f should return undefined, not 42. If fp->argsobj
|
|
|
|
* is null at this point, as it would be in the example, return
|
|
|
|
* undefined in *vp.
|
|
|
|
*/
|
|
|
|
if (fp->argsobj)
|
|
|
|
return OBJ_GET_PROPERTY(cx, fp->argsobj, id, vp);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (id == ATOM_TO_JSID(cx->runtime->atomState.lengthAtom)) {
|
|
|
|
if (fp->argsobj && TEST_OVERRIDE_BIT(fp, ARGS_LENGTH))
|
|
|
|
return OBJ_GET_PROPERTY(cx, fp->argsobj, id, vp);
|
|
|
|
*vp = INT_TO_JSVAL((jsint) fp->argc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject *
|
|
|
|
js_GetArgsObject(JSContext *cx, JSStackFrame *fp)
|
|
|
|
{
|
|
|
|
JSObject *argsobj, *global, *parent;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We must be in a function activation; the function must be lightweight
|
|
|
|
* or else fp must have a variable object.
|
|
|
|
*/
|
2008-03-29 03:34:29 -07:00
|
|
|
JS_ASSERT(fp->fun && (!(fp->fun->flags & JSFUN_HEAVYWEIGHT) || fp->varobj));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/* Skip eval and debugger frames. */
|
|
|
|
while (fp->flags & JSFRAME_SPECIAL)
|
|
|
|
fp = fp->down;
|
|
|
|
|
|
|
|
/* Create an arguments object for fp only if it lacks one. */
|
|
|
|
argsobj = fp->argsobj;
|
|
|
|
if (argsobj)
|
|
|
|
return argsobj;
|
|
|
|
|
|
|
|
/* Link the new object to fp so it can get actual argument values. */
|
2008-04-02 00:46:12 -07:00
|
|
|
argsobj = js_NewObject(cx, &js_ArgumentsClass, NULL, NULL, 0);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!argsobj || !JS_SetPrivate(cx, argsobj, fp)) {
|
|
|
|
cx->weakRoots.newborn[GCX_OBJECT] = NULL;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Give arguments an intrinsic scope chain link to fp's global object.
|
|
|
|
* Since the arguments object lacks a prototype because js_ArgumentsClass
|
|
|
|
* is not initialized, js_NewObject won't assign a default parent to it.
|
|
|
|
*
|
|
|
|
* Therefore if arguments is used as the head of an eval scope chain (via
|
|
|
|
* a direct or indirect call to eval(program, arguments)), any reference
|
|
|
|
* to a standard class object in the program will fail to resolve due to
|
|
|
|
* js_GetClassPrototype not being able to find a global object containing
|
|
|
|
* the standard prototype by starting from arguments and following parent.
|
|
|
|
*/
|
|
|
|
global = fp->scopeChain;
|
|
|
|
while ((parent = OBJ_GET_PARENT(cx, global)) != NULL)
|
|
|
|
global = parent;
|
|
|
|
STOBJ_SET_PARENT(argsobj, global);
|
|
|
|
fp->argsobj = argsobj;
|
|
|
|
return argsobj;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
|
|
|
args_enumerate(JSContext *cx, JSObject *obj);
|
|
|
|
|
2008-01-28 15:07:29 -08:00
|
|
|
JS_FRIEND_API(JSBool)
|
2007-03-22 10:30:00 -07:00
|
|
|
js_PutArgsObject(JSContext *cx, JSStackFrame *fp)
|
|
|
|
{
|
|
|
|
JSObject *argsobj;
|
|
|
|
jsval bmapval, rval;
|
|
|
|
JSBool ok;
|
|
|
|
JSRuntime *rt;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Reuse args_enumerate here to reflect fp's actual arguments as indexed
|
|
|
|
* elements of argsobj. Do this first, before clearing and freeing the
|
|
|
|
* deleted argument slot bitmap, because args_enumerate depends on that.
|
|
|
|
*/
|
|
|
|
argsobj = fp->argsobj;
|
|
|
|
ok = args_enumerate(cx, argsobj);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now clear the deleted argument number bitmap slot and free the bitmap,
|
|
|
|
* if one was actually created due to 'delete arguments[0]' or similar.
|
|
|
|
*/
|
|
|
|
(void) JS_GetReservedSlot(cx, argsobj, 0, &bmapval);
|
|
|
|
if (!JSVAL_IS_VOID(bmapval)) {
|
|
|
|
JS_SetReservedSlot(cx, argsobj, 0, JSVAL_VOID);
|
|
|
|
if (fp->argc > JSVAL_INT_BITS)
|
|
|
|
JS_free(cx, JSVAL_TO_PRIVATE(bmapval));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now get the prototype properties so we snapshot fp->fun and fp->argc
|
|
|
|
* before fp goes away.
|
|
|
|
*/
|
|
|
|
rt = cx->runtime;
|
|
|
|
ok &= js_GetProperty(cx, argsobj, ATOM_TO_JSID(rt->atomState.calleeAtom),
|
|
|
|
&rval);
|
|
|
|
ok &= js_SetProperty(cx, argsobj, ATOM_TO_JSID(rt->atomState.calleeAtom),
|
|
|
|
&rval);
|
|
|
|
ok &= js_GetProperty(cx, argsobj, ATOM_TO_JSID(rt->atomState.lengthAtom),
|
|
|
|
&rval);
|
|
|
|
ok &= js_SetProperty(cx, argsobj, ATOM_TO_JSID(rt->atomState.lengthAtom),
|
|
|
|
&rval);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Clear the private pointer to fp, which is about to go away (js_Invoke).
|
|
|
|
* Do this last because the args_enumerate and js_GetProperty calls above
|
|
|
|
* need to follow the private slot to find fp.
|
|
|
|
*/
|
|
|
|
ok &= JS_SetPrivate(cx, argsobj, NULL);
|
|
|
|
fp->argsobj = NULL;
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
|
|
|
args_delProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp)
|
|
|
|
{
|
|
|
|
jsint slot;
|
|
|
|
JSStackFrame *fp;
|
|
|
|
|
|
|
|
if (!JSVAL_IS_INT(id))
|
|
|
|
return JS_TRUE;
|
|
|
|
fp = (JSStackFrame *)
|
|
|
|
JS_GetInstancePrivate(cx, obj, &js_ArgumentsClass, NULL);
|
|
|
|
if (!fp)
|
|
|
|
return JS_TRUE;
|
|
|
|
JS_ASSERT(fp->argsobj);
|
|
|
|
|
|
|
|
slot = JSVAL_TO_INT(id);
|
|
|
|
switch (slot) {
|
|
|
|
case ARGS_CALLEE:
|
|
|
|
case ARGS_LENGTH:
|
|
|
|
SET_OVERRIDE_BIT(fp, slot);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
if ((uintN)slot < fp->argc && !MarkArgDeleted(cx, fp, slot))
|
|
|
|
return JS_FALSE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
|
|
|
args_getProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp)
|
|
|
|
{
|
|
|
|
jsint slot;
|
|
|
|
JSStackFrame *fp;
|
|
|
|
|
|
|
|
if (!JSVAL_IS_INT(id))
|
|
|
|
return JS_TRUE;
|
|
|
|
fp = (JSStackFrame *)
|
|
|
|
JS_GetInstancePrivate(cx, obj, &js_ArgumentsClass, NULL);
|
|
|
|
if (!fp)
|
|
|
|
return JS_TRUE;
|
|
|
|
JS_ASSERT(fp->argsobj);
|
|
|
|
|
|
|
|
slot = JSVAL_TO_INT(id);
|
|
|
|
switch (slot) {
|
|
|
|
case ARGS_CALLEE:
|
|
|
|
if (!TEST_OVERRIDE_BIT(fp, slot))
|
2007-08-01 21:33:52 -07:00
|
|
|
*vp = OBJECT_TO_JSVAL(fp->callee);
|
2007-03-22 10:30:00 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ARGS_LENGTH:
|
|
|
|
if (!TEST_OVERRIDE_BIT(fp, slot))
|
|
|
|
*vp = INT_TO_JSVAL((jsint)fp->argc);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
if ((uintN)slot < fp->argc && !ArgWasDeleted(cx, fp, slot))
|
|
|
|
*vp = fp->argv[slot];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
|
|
|
args_setProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp)
|
|
|
|
{
|
|
|
|
JSStackFrame *fp;
|
|
|
|
jsint slot;
|
|
|
|
|
|
|
|
if (!JSVAL_IS_INT(id))
|
|
|
|
return JS_TRUE;
|
|
|
|
fp = (JSStackFrame *)
|
|
|
|
JS_GetInstancePrivate(cx, obj, &js_ArgumentsClass, NULL);
|
|
|
|
if (!fp)
|
|
|
|
return JS_TRUE;
|
|
|
|
JS_ASSERT(fp->argsobj);
|
|
|
|
|
|
|
|
slot = JSVAL_TO_INT(id);
|
|
|
|
switch (slot) {
|
|
|
|
case ARGS_CALLEE:
|
|
|
|
case ARGS_LENGTH:
|
|
|
|
SET_OVERRIDE_BIT(fp, slot);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2008-03-29 03:34:29 -07:00
|
|
|
if (FUN_INTERPRETED(fp->fun) &&
|
2007-03-22 10:30:00 -07:00
|
|
|
(uintN)slot < fp->argc &&
|
|
|
|
!ArgWasDeleted(cx, fp, slot)) {
|
|
|
|
fp->argv[slot] = *vp;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
|
|
|
args_resolve(JSContext *cx, JSObject *obj, jsval id, uintN flags,
|
|
|
|
JSObject **objp)
|
|
|
|
{
|
|
|
|
JSStackFrame *fp;
|
|
|
|
uintN slot;
|
|
|
|
JSString *str;
|
|
|
|
JSAtom *atom;
|
|
|
|
intN tinyid;
|
|
|
|
jsval value;
|
|
|
|
|
|
|
|
*objp = NULL;
|
|
|
|
fp = (JSStackFrame *)
|
|
|
|
JS_GetInstancePrivate(cx, obj, &js_ArgumentsClass, NULL);
|
|
|
|
if (!fp)
|
|
|
|
return JS_TRUE;
|
|
|
|
JS_ASSERT(fp->argsobj);
|
|
|
|
|
|
|
|
if (JSVAL_IS_INT(id)) {
|
|
|
|
slot = JSVAL_TO_INT(id);
|
|
|
|
if (slot < fp->argc && !ArgWasDeleted(cx, fp, slot)) {
|
|
|
|
/* XXX ECMA specs DontEnum, contrary to other array-like objects */
|
|
|
|
if (!js_DefineProperty(cx, obj, INT_JSVAL_TO_JSID(id),
|
|
|
|
fp->argv[slot],
|
|
|
|
args_getProperty, args_setProperty,
|
|
|
|
0, NULL)) {
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
*objp = obj;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
str = JSVAL_TO_STRING(id);
|
|
|
|
atom = cx->runtime->atomState.lengthAtom;
|
|
|
|
if (str == ATOM_TO_STRING(atom)) {
|
|
|
|
tinyid = ARGS_LENGTH;
|
|
|
|
value = INT_TO_JSVAL(fp->argc);
|
|
|
|
} else {
|
|
|
|
atom = cx->runtime->atomState.calleeAtom;
|
|
|
|
if (str == ATOM_TO_STRING(atom)) {
|
|
|
|
tinyid = ARGS_CALLEE;
|
2007-08-01 21:33:52 -07:00
|
|
|
value = OBJECT_TO_JSVAL(fp->callee);
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
|
|
|
atom = NULL;
|
|
|
|
|
|
|
|
/* Quell GCC overwarnings. */
|
|
|
|
tinyid = 0;
|
|
|
|
value = JSVAL_NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (atom && !TEST_OVERRIDE_BIT(fp, tinyid)) {
|
|
|
|
if (!js_DefineNativeProperty(cx, obj, ATOM_TO_JSID(atom), value,
|
|
|
|
args_getProperty, args_setProperty, 0,
|
|
|
|
SPROP_HAS_SHORTID, tinyid, NULL)) {
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
*objp = obj;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
|
|
|
args_enumerate(JSContext *cx, JSObject *obj)
|
|
|
|
{
|
|
|
|
JSStackFrame *fp;
|
|
|
|
JSObject *pobj;
|
|
|
|
JSProperty *prop;
|
|
|
|
uintN slot, argc;
|
|
|
|
|
|
|
|
fp = (JSStackFrame *)
|
|
|
|
JS_GetInstancePrivate(cx, obj, &js_ArgumentsClass, NULL);
|
|
|
|
if (!fp)
|
|
|
|
return JS_TRUE;
|
|
|
|
JS_ASSERT(fp->argsobj);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Trigger reflection with value snapshot in args_resolve using a series
|
|
|
|
* of js_LookupProperty calls. We handle length, callee, and the indexed
|
|
|
|
* argument properties. We know that args_resolve covers all these cases
|
|
|
|
* and creates direct properties of obj, but that it may fail to resolve
|
|
|
|
* length or callee if overridden.
|
|
|
|
*/
|
|
|
|
if (!js_LookupProperty(cx, obj,
|
|
|
|
ATOM_TO_JSID(cx->runtime->atomState.lengthAtom),
|
|
|
|
&pobj, &prop)) {
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
if (prop)
|
|
|
|
OBJ_DROP_PROPERTY(cx, pobj, prop);
|
|
|
|
|
|
|
|
if (!js_LookupProperty(cx, obj,
|
|
|
|
ATOM_TO_JSID(cx->runtime->atomState.calleeAtom),
|
|
|
|
&pobj, &prop)) {
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
if (prop)
|
|
|
|
OBJ_DROP_PROPERTY(cx, pobj, prop);
|
|
|
|
|
|
|
|
argc = fp->argc;
|
|
|
|
for (slot = 0; slot < argc; slot++) {
|
|
|
|
if (!js_LookupProperty(cx, obj, INT_TO_JSID((jsint)slot), &pobj, &prop))
|
|
|
|
return JS_FALSE;
|
|
|
|
if (prop)
|
|
|
|
OBJ_DROP_PROPERTY(cx, pobj, prop);
|
|
|
|
}
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if JS_HAS_GENERATORS
|
|
|
|
/*
|
|
|
|
* If a generator-iterator's arguments or call object escapes, it needs to
|
|
|
|
* mark its generator object.
|
|
|
|
*/
|
2007-04-16 23:53:37 -07:00
|
|
|
static void
|
|
|
|
args_or_call_trace(JSTracer *trc, JSObject *obj)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
JSStackFrame *fp;
|
|
|
|
|
2007-07-05 13:37:47 -07:00
|
|
|
fp = (JSStackFrame *) JS_GetPrivate(trc->context, obj);
|
2007-07-16 13:57:16 -07:00
|
|
|
if (fp && (fp->flags & JSFRAME_GENERATOR)) {
|
|
|
|
JS_CALL_OBJECT_TRACER(trc, FRAME_TO_GENERATOR(fp)->obj,
|
|
|
|
"FRAME_TO_GENERATOR(fp)->obj");
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
#else
|
2007-04-16 23:53:37 -07:00
|
|
|
# define args_or_call_trace NULL
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The Arguments class is not initialized via JS_InitClass, and must not be,
|
|
|
|
* because its name is "Object". Per ECMA, that causes instances of it to
|
|
|
|
* delegate to the object named by Object.prototype. It also ensures that
|
|
|
|
* arguments.toString() returns "[object Object]".
|
|
|
|
*
|
|
|
|
* The JSClass functions below collaborate to lazily reflect and synchronize
|
|
|
|
* actual argument values, argument count, and callee function object stored
|
|
|
|
* in a JSStackFrame with their corresponding property values in the frame's
|
|
|
|
* arguments object.
|
|
|
|
*/
|
|
|
|
JSClass js_ArgumentsClass = {
|
|
|
|
js_Object_str,
|
|
|
|
JSCLASS_HAS_PRIVATE | JSCLASS_NEW_RESOLVE | JSCLASS_HAS_RESERVED_SLOTS(1) |
|
2007-04-16 23:53:37 -07:00
|
|
|
JSCLASS_MARK_IS_TRACE | JSCLASS_HAS_CACHED_PROTO(JSProto_Object),
|
2007-03-22 10:30:00 -07:00
|
|
|
JS_PropertyStub, args_delProperty,
|
|
|
|
args_getProperty, args_setProperty,
|
|
|
|
args_enumerate, (JSResolveOp) args_resolve,
|
|
|
|
JS_ConvertStub, JS_FinalizeStub,
|
|
|
|
NULL, NULL,
|
|
|
|
NULL, NULL,
|
|
|
|
NULL, NULL,
|
2007-04-16 23:53:37 -07:00
|
|
|
JS_CLASS_TRACE(args_or_call_trace), NULL
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
2008-06-30 10:17:33 -07:00
|
|
|
#define JSSLOT_SCRIPTED_FUNCTION (JSSLOT_PRIVATE + 1)
|
|
|
|
#define JSSLOT_CALL_ARGUMENTS (JSSLOT_PRIVATE + 2)
|
|
|
|
#define CALL_CLASS_FIXED_RESERVED_SLOTS 2
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
JSObject *
|
|
|
|
js_GetCallObject(JSContext *cx, JSStackFrame *fp, JSObject *parent)
|
|
|
|
{
|
|
|
|
JSObject *callobj, *funobj;
|
|
|
|
|
|
|
|
/* Create a call object for fp only if it lacks one. */
|
|
|
|
JS_ASSERT(fp->fun);
|
|
|
|
callobj = fp->callobj;
|
|
|
|
if (callobj)
|
|
|
|
return callobj;
|
|
|
|
|
|
|
|
/* The default call parent is its function's parent (static link). */
|
|
|
|
if (!parent) {
|
2007-08-01 21:33:52 -07:00
|
|
|
funobj = fp->callee;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (funobj)
|
|
|
|
parent = OBJ_GET_PARENT(cx, funobj);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create the call object and link it to its stack frame. */
|
2008-04-02 00:46:12 -07:00
|
|
|
callobj = js_NewObject(cx, &js_CallClass, NULL, parent, 0);
|
2008-06-30 10:17:33 -07:00
|
|
|
if (!callobj)
|
2007-03-22 10:30:00 -07:00
|
|
|
return NULL;
|
2008-06-30 10:17:33 -07:00
|
|
|
|
|
|
|
JS_SetPrivate(cx, callobj, fp);
|
|
|
|
STOBJ_SET_SLOT(callobj, JSSLOT_SCRIPTED_FUNCTION,
|
|
|
|
OBJECT_TO_JSVAL(FUN_OBJECT(fp->fun)));
|
2007-03-22 10:30:00 -07:00
|
|
|
fp->callobj = callobj;
|
|
|
|
|
|
|
|
/* Make callobj be the scope chain and the variables object. */
|
|
|
|
JS_ASSERT(fp->scopeChain == parent);
|
|
|
|
fp->scopeChain = callobj;
|
|
|
|
fp->varobj = callobj;
|
|
|
|
return callobj;
|
|
|
|
}
|
|
|
|
|
2008-06-30 10:17:33 -07:00
|
|
|
JSFunction *
|
|
|
|
js_GetCallObjectFunction(JSObject *obj)
|
|
|
|
{
|
|
|
|
jsval v;
|
|
|
|
|
|
|
|
JS_ASSERT(STOBJ_GET_CLASS(obj) == &js_CallClass);
|
|
|
|
v = STOBJ_GET_SLOT(obj, JSSLOT_SCRIPTED_FUNCTION);
|
|
|
|
if (JSVAL_IS_VOID(v)) {
|
|
|
|
/* Newborn or prototype object. */
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
JS_ASSERT(!JSVAL_IS_PRIMITIVE(v));
|
|
|
|
return (JSFunction *) JSVAL_TO_OBJECT(v);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-01-28 15:07:29 -08:00
|
|
|
JS_FRIEND_API(JSBool)
|
2007-03-22 10:30:00 -07:00
|
|
|
js_PutCallObject(JSContext *cx, JSStackFrame *fp)
|
|
|
|
{
|
|
|
|
JSObject *callobj;
|
|
|
|
JSBool ok;
|
2008-06-30 10:17:33 -07:00
|
|
|
JSFunction *fun;
|
|
|
|
uintN n;
|
|
|
|
JSScope *scope;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/*
|
2008-06-30 10:17:33 -07:00
|
|
|
* Since for a call object all fixed slots happen to be taken, we can copy
|
|
|
|
* arguments and variables straight into JSObject.dslots.
|
2007-03-22 10:30:00 -07:00
|
|
|
*/
|
2008-06-30 10:17:33 -07:00
|
|
|
JS_STATIC_ASSERT(JS_INITIAL_NSLOTS - JSSLOT_PRIVATE ==
|
|
|
|
1 + CALL_CLASS_FIXED_RESERVED_SLOTS);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
callobj = fp->callobj;
|
|
|
|
if (!callobj)
|
|
|
|
return JS_TRUE;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the arguments object to snapshot fp's actual argument values.
|
|
|
|
*/
|
2008-06-30 10:17:33 -07:00
|
|
|
ok = JS_TRUE;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (fp->argsobj) {
|
2007-06-14 00:26:19 -07:00
|
|
|
if (!TEST_OVERRIDE_BIT(fp, CALL_ARGUMENTS)) {
|
2008-06-30 10:17:33 -07:00
|
|
|
STOBJ_SET_SLOT(callobj, JSSLOT_CALL_ARGUMENTS,
|
|
|
|
OBJECT_TO_JSVAL(fp->argsobj));
|
2007-06-14 00:26:19 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
ok &= js_PutArgsObject(cx, fp);
|
|
|
|
}
|
|
|
|
|
2008-06-30 10:17:33 -07:00
|
|
|
fun = fp->fun;
|
|
|
|
JS_ASSERT(fun == js_GetCallObjectFunction(callobj));
|
|
|
|
n = JS_GET_LOCAL_NAME_COUNT(fun);
|
|
|
|
if (n != 0) {
|
|
|
|
JS_LOCK_OBJ(cx, callobj);
|
|
|
|
n += JS_INITIAL_NSLOTS;
|
|
|
|
if (n > STOBJ_NSLOTS(callobj))
|
|
|
|
ok &= js_ReallocSlots(cx, callobj, n, JS_TRUE);
|
|
|
|
scope = OBJ_SCOPE(callobj);
|
|
|
|
if (ok) {
|
|
|
|
memcpy(callobj->dslots, fp->argv, fun->nargs * sizeof(jsval));
|
2008-07-20 13:13:17 -07:00
|
|
|
memcpy(callobj->dslots + fun->nargs, fp->slots,
|
2008-06-30 10:17:33 -07:00
|
|
|
fun->u.i.nvars * sizeof(jsval));
|
|
|
|
if (scope->object == callobj && n > scope->map.freeslot)
|
|
|
|
scope->map.freeslot = n;
|
|
|
|
}
|
|
|
|
JS_UNLOCK_SCOPE(cx, scope);
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/*
|
|
|
|
* Clear the private pointer to fp, which is about to go away (js_Invoke).
|
2008-06-30 10:17:33 -07:00
|
|
|
* Do this last because js_GetProperty calls above need to follow the
|
|
|
|
* private slot to find fp.
|
2007-03-22 10:30:00 -07:00
|
|
|
*/
|
2008-06-30 10:17:33 -07:00
|
|
|
JS_SetPrivate(cx, callobj, NULL);
|
2007-03-22 10:30:00 -07:00
|
|
|
fp->callobj = NULL;
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
|
|
|
call_enumerate(JSContext *cx, JSObject *obj)
|
|
|
|
{
|
2008-03-29 03:34:29 -07:00
|
|
|
JSFunction *fun;
|
2008-06-30 10:17:33 -07:00
|
|
|
uintN n, i;
|
2008-01-20 11:11:59 -08:00
|
|
|
void *mark;
|
2008-02-08 16:01:45 -08:00
|
|
|
jsuword *names;
|
|
|
|
JSBool ok;
|
|
|
|
JSAtom *name;
|
2007-11-27 00:38:47 -08:00
|
|
|
JSObject *pobj;
|
2007-03-22 10:30:00 -07:00
|
|
|
JSProperty *prop;
|
|
|
|
|
2008-06-30 10:17:33 -07:00
|
|
|
fun = js_GetCallObjectFunction(obj);
|
2008-02-08 16:01:45 -08:00
|
|
|
n = JS_GET_LOCAL_NAME_COUNT(fun);
|
2007-11-27 00:38:47 -08:00
|
|
|
if (n == 0)
|
|
|
|
return JS_TRUE;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-01-20 11:11:59 -08:00
|
|
|
mark = JS_ARENA_MARK(&cx->tempPool);
|
2008-02-08 16:01:45 -08:00
|
|
|
|
2008-09-08 10:51:10 -07:00
|
|
|
MUST_FLOW_THROUGH("out");
|
2008-02-08 16:01:45 -08:00
|
|
|
names = js_GetLocalNameArray(cx, fun, &cx->tempPool);
|
|
|
|
if (!names) {
|
|
|
|
ok = JS_FALSE;
|
2008-01-20 11:11:59 -08:00
|
|
|
goto out;
|
2008-02-08 16:01:45 -08:00
|
|
|
}
|
2007-11-27 00:38:47 -08:00
|
|
|
|
|
|
|
for (i = 0; i != n; ++i) {
|
2008-02-08 16:01:45 -08:00
|
|
|
name = JS_LOCAL_NAME_TO_ATOM(names[i]);
|
2007-11-27 00:38:47 -08:00
|
|
|
if (!name)
|
|
|
|
continue;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/*
|
2007-11-27 00:38:47 -08:00
|
|
|
* Trigger reflection by looking up the name of the argument or
|
|
|
|
* variable.
|
2007-03-22 10:30:00 -07:00
|
|
|
*/
|
2008-02-08 16:01:45 -08:00
|
|
|
ok = js_LookupProperty(cx, obj, ATOM_TO_JSID(name), &pobj, &prop);
|
|
|
|
if (!ok)
|
2007-11-27 00:38:47 -08:00
|
|
|
goto out;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* At this point the call object always has a property corresponding
|
|
|
|
* to the local name because call_resolve creates the property using
|
|
|
|
* JSPROP_PERMANENT.
|
|
|
|
*/
|
|
|
|
JS_ASSERT(prop && pobj == obj);
|
|
|
|
OBJ_DROP_PROPERTY(cx, pobj, prop);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2008-02-08 16:01:45 -08:00
|
|
|
ok = JS_TRUE;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-11-27 00:38:47 -08:00
|
|
|
out:
|
2008-01-20 11:11:59 -08:00
|
|
|
JS_ARENA_RELEASE(&cx->tempPool, mark);
|
2008-02-08 16:01:45 -08:00
|
|
|
return ok;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2008-03-29 08:48:41 -07:00
|
|
|
typedef enum JSCallPropertyKind {
|
|
|
|
JSCPK_ARGUMENTS,
|
|
|
|
JSCPK_ARG,
|
|
|
|
JSCPK_VAR
|
|
|
|
} JSCallPropertyKind;
|
|
|
|
|
|
|
|
static JSBool
|
|
|
|
CallPropertyOp(JSContext *cx, JSObject *obj, jsid id, jsval *vp,
|
|
|
|
JSCallPropertyKind kind, JSBool setter)
|
|
|
|
{
|
|
|
|
JSFunction *fun;
|
2008-06-30 10:17:33 -07:00
|
|
|
JSStackFrame *fp;
|
2008-03-29 08:48:41 -07:00
|
|
|
uintN i;
|
|
|
|
jsval *array;
|
|
|
|
|
2008-06-30 10:17:33 -07:00
|
|
|
if (STOBJ_GET_CLASS(obj) != &js_CallClass)
|
2008-03-29 08:48:41 -07:00
|
|
|
return JS_TRUE;
|
2008-06-30 10:17:33 -07:00
|
|
|
|
|
|
|
fun = js_GetCallObjectFunction(obj);
|
|
|
|
fp = (JSStackFrame *) JS_GetPrivate(cx, obj);
|
2008-03-29 08:48:41 -07:00
|
|
|
|
|
|
|
if (kind == JSCPK_ARGUMENTS) {
|
|
|
|
if (setter) {
|
2008-06-30 10:17:33 -07:00
|
|
|
if (fp)
|
|
|
|
SET_OVERRIDE_BIT(fp, CALL_ARGUMENTS);
|
|
|
|
STOBJ_SET_SLOT(obj, JSSLOT_CALL_ARGUMENTS, *vp);
|
|
|
|
} else {
|
|
|
|
if (fp && !TEST_OVERRIDE_BIT(fp, CALL_ARGUMENTS)) {
|
|
|
|
JSObject *argsobj;
|
2008-03-29 08:48:41 -07:00
|
|
|
|
2008-06-30 10:17:33 -07:00
|
|
|
argsobj = js_GetArgsObject(cx, fp);
|
|
|
|
if (!argsobj)
|
|
|
|
return JS_FALSE;
|
|
|
|
*vp = OBJECT_TO_JSVAL(argsobj);
|
|
|
|
} else {
|
|
|
|
*vp = STOBJ_GET_SLOT(obj, JSSLOT_CALL_ARGUMENTS);
|
|
|
|
}
|
2008-03-29 08:48:41 -07:00
|
|
|
}
|
|
|
|
return JS_TRUE;
|
2008-09-08 02:47:12 -07:00
|
|
|
}
|
2008-03-29 08:48:41 -07:00
|
|
|
|
|
|
|
JS_ASSERT((int16) JSVAL_TO_INT(id) == JSVAL_TO_INT(id));
|
|
|
|
i = (uint16) JSVAL_TO_INT(id);
|
|
|
|
JS_ASSERT_IF(kind == JSCPK_ARG, i < fun->nargs);
|
|
|
|
JS_ASSERT_IF(kind == JSCPK_VAR, i < fun->u.i.nvars);
|
|
|
|
|
2008-06-30 10:17:33 -07:00
|
|
|
if (!fp) {
|
|
|
|
i += CALL_CLASS_FIXED_RESERVED_SLOTS;
|
|
|
|
if (kind == JSCPK_VAR)
|
|
|
|
i += fun->nargs;
|
|
|
|
else
|
|
|
|
JS_ASSERT(kind == JSCPK_ARG);
|
|
|
|
return setter
|
|
|
|
? JS_SetReservedSlot(cx, obj, i, *vp)
|
|
|
|
: JS_GetReservedSlot(cx, obj, i, vp);
|
|
|
|
}
|
|
|
|
|
2008-03-29 08:48:41 -07:00
|
|
|
if (kind == JSCPK_ARG) {
|
|
|
|
array = fp->argv;
|
|
|
|
} else {
|
|
|
|
JS_ASSERT(kind == JSCPK_VAR);
|
2008-07-20 13:13:17 -07:00
|
|
|
array = fp->slots;
|
2008-03-29 08:48:41 -07:00
|
|
|
}
|
|
|
|
if (setter)
|
|
|
|
array[i] = *vp;
|
|
|
|
else
|
|
|
|
*vp = array[i];
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
|
|
|
GetCallArguments(JSContext *cx, JSObject *obj, jsid id, jsval *vp)
|
|
|
|
{
|
|
|
|
return CallPropertyOp(cx, obj, id, vp, JSCPK_ARGUMENTS, JS_FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
|
|
|
SetCallArguments(JSContext *cx, JSObject *obj, jsid id, jsval *vp)
|
|
|
|
{
|
|
|
|
return CallPropertyOp(cx, obj, id, vp, JSCPK_ARGUMENTS, JS_TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
JSBool
|
|
|
|
js_GetCallArg(JSContext *cx, JSObject *obj, jsid id, jsval *vp)
|
|
|
|
{
|
|
|
|
return CallPropertyOp(cx, obj, id, vp, JSCPK_ARG, JS_FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
|
|
|
SetCallArg(JSContext *cx, JSObject *obj, jsid id, jsval *vp)
|
|
|
|
{
|
|
|
|
return CallPropertyOp(cx, obj, id, vp, JSCPK_ARG, JS_TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
JSBool
|
|
|
|
js_GetCallVar(JSContext *cx, JSObject *obj, jsid id, jsval *vp)
|
|
|
|
{
|
|
|
|
return CallPropertyOp(cx, obj, id, vp, JSCPK_VAR, JS_FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
|
|
|
SetCallVar(JSContext *cx, JSObject *obj, jsid id, jsval *vp)
|
|
|
|
{
|
|
|
|
return CallPropertyOp(cx, obj, id, vp, JSCPK_VAR, JS_TRUE);
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
static JSBool
|
2008-01-23 05:17:47 -08:00
|
|
|
call_resolve(JSContext *cx, JSObject *obj, jsval idval, uintN flags,
|
2007-03-22 10:30:00 -07:00
|
|
|
JSObject **objp)
|
|
|
|
{
|
2008-03-29 08:48:41 -07:00
|
|
|
JSFunction *fun;
|
2008-01-23 05:17:47 -08:00
|
|
|
jsid id;
|
2007-11-19 09:15:45 -08:00
|
|
|
JSLocalKind localKind;
|
2007-03-22 10:30:00 -07:00
|
|
|
JSPropertyOp getter, setter;
|
2007-11-19 09:15:45 -08:00
|
|
|
uintN slot, attrs;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-06-30 10:17:33 -07:00
|
|
|
if (!JSVAL_IS_STRING(idval))
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_TRUE;
|
|
|
|
|
2008-06-30 10:17:33 -07:00
|
|
|
fun = js_GetCallObjectFunction(obj);
|
|
|
|
if (!fun)
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_TRUE;
|
|
|
|
|
2008-01-23 05:17:47 -08:00
|
|
|
if (!js_ValueToStringId(cx, idval, &id))
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
|
2008-03-29 08:48:41 -07:00
|
|
|
localKind = js_LookupLocal(cx, fun, JSID_TO_ATOM(id), &slot);
|
2007-11-19 09:15:45 -08:00
|
|
|
if (localKind != JSLOCAL_NONE) {
|
2008-03-29 08:48:41 -07:00
|
|
|
JS_ASSERT((uint16) slot == slot);
|
2008-06-30 10:17:33 -07:00
|
|
|
attrs = JSPROP_PERMANENT | JSPROP_SHARED;
|
2007-11-19 09:15:45 -08:00
|
|
|
if (localKind == JSLOCAL_ARG) {
|
2008-03-29 08:48:41 -07:00
|
|
|
JS_ASSERT(slot < fun->nargs);
|
|
|
|
getter = js_GetCallArg;
|
|
|
|
setter = SetCallArg;
|
2007-11-19 09:15:45 -08:00
|
|
|
} else {
|
|
|
|
JS_ASSERT(localKind == JSLOCAL_VAR || localKind == JSLOCAL_CONST);
|
2008-03-29 08:48:41 -07:00
|
|
|
JS_ASSERT(slot < fun->u.i.nvars);
|
|
|
|
getter = js_GetCallVar;
|
|
|
|
setter = SetCallVar;
|
2008-06-30 10:17:33 -07:00
|
|
|
if (localKind == JSLOCAL_CONST)
|
|
|
|
attrs |= JSPROP_READONLY;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2008-06-30 10:17:33 -07:00
|
|
|
if (!js_DefineNativeProperty(cx, obj, id, JSVAL_VOID, getter, setter,
|
2008-03-29 08:48:41 -07:00
|
|
|
attrs, SPROP_HAS_SHORTID, (int16) slot,
|
2008-01-23 05:17:47 -08:00
|
|
|
NULL)) {
|
2007-11-19 09:15:45 -08:00
|
|
|
return JS_FALSE;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2007-11-19 09:15:45 -08:00
|
|
|
*objp = obj;
|
2007-06-15 17:29:21 -07:00
|
|
|
return JS_TRUE;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2007-12-11 13:40:51 -08:00
|
|
|
/*
|
|
|
|
* Resolve arguments so that we never store a particular Call object's
|
|
|
|
* arguments object reference in a Call prototype's |arguments| slot.
|
|
|
|
*/
|
2008-01-23 05:17:47 -08:00
|
|
|
if (id == ATOM_TO_JSID(cx->runtime->atomState.argumentsAtom)) {
|
|
|
|
if (!js_DefineNativeProperty(cx, obj, id, JSVAL_VOID,
|
2008-03-29 08:48:41 -07:00
|
|
|
GetCallArguments, SetCallArguments,
|
2008-06-30 10:17:33 -07:00
|
|
|
JSPROP_PERMANENT | JSPROP_SHARED,
|
|
|
|
0, 0, NULL)) {
|
2007-12-11 13:40:51 -08:00
|
|
|
return JS_FALSE;
|
2007-06-15 17:29:21 -07:00
|
|
|
}
|
2007-12-11 13:40:51 -08:00
|
|
|
*objp = obj;
|
|
|
|
return JS_TRUE;
|
2007-06-15 17:29:21 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
|
|
|
call_convert(JSContext *cx, JSObject *obj, JSType type, jsval *vp)
|
|
|
|
{
|
|
|
|
JSStackFrame *fp;
|
|
|
|
|
|
|
|
if (type == JSTYPE_FUNCTION) {
|
|
|
|
fp = (JSStackFrame *) JS_GetPrivate(cx, obj);
|
|
|
|
if (fp) {
|
|
|
|
JS_ASSERT(fp->fun);
|
2007-08-01 21:33:52 -07:00
|
|
|
*vp = OBJECT_TO_JSVAL(fp->callee);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
2008-06-30 10:17:33 -07:00
|
|
|
static uint32
|
|
|
|
call_reserveSlots(JSContext *cx, JSObject *obj)
|
|
|
|
{
|
|
|
|
JSFunction *fun;
|
|
|
|
|
|
|
|
fun = js_GetCallObjectFunction(obj);
|
|
|
|
return JS_GET_LOCAL_NAME_COUNT(fun);
|
|
|
|
}
|
|
|
|
|
2007-12-13 12:59:44 -08:00
|
|
|
JS_FRIEND_DATA(JSClass) js_CallClass = {
|
2007-03-22 10:30:00 -07:00
|
|
|
js_Call_str,
|
2008-06-30 10:17:33 -07:00
|
|
|
JSCLASS_HAS_PRIVATE |
|
|
|
|
JSCLASS_HAS_RESERVED_SLOTS(CALL_CLASS_FIXED_RESERVED_SLOTS) |
|
|
|
|
JSCLASS_NEW_RESOLVE | JSCLASS_IS_ANONYMOUS |
|
2007-04-16 23:53:37 -07:00
|
|
|
JSCLASS_MARK_IS_TRACE | JSCLASS_HAS_CACHED_PROTO(JSProto_Call),
|
2007-03-22 10:30:00 -07:00
|
|
|
JS_PropertyStub, JS_PropertyStub,
|
2008-03-29 08:48:41 -07:00
|
|
|
JS_PropertyStub, JS_PropertyStub,
|
2007-03-22 10:30:00 -07:00
|
|
|
call_enumerate, (JSResolveOp)call_resolve,
|
|
|
|
call_convert, JS_FinalizeStub,
|
|
|
|
NULL, NULL,
|
|
|
|
NULL, NULL,
|
|
|
|
NULL, NULL,
|
2008-06-30 10:17:33 -07:00
|
|
|
JS_CLASS_TRACE(args_or_call_trace), call_reserveSlots
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
static JSBool
|
|
|
|
fun_getProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp)
|
|
|
|
{
|
|
|
|
jsint slot;
|
|
|
|
JSFunction *fun;
|
|
|
|
JSStackFrame *fp;
|
2008-09-05 16:24:53 -07:00
|
|
|
JSSecurityCallbacks *callbacks;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (!JSVAL_IS_INT(id))
|
|
|
|
return JS_TRUE;
|
|
|
|
slot = JSVAL_TO_INT(id);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Loop because getter and setter can be delegated from another class,
|
|
|
|
* but loop only for ARGS_LENGTH because we must pretend that f.length
|
|
|
|
* is in each function instance f, per ECMA-262, instead of only in the
|
|
|
|
* Function.prototype object (we use JSPROP_PERMANENT with JSPROP_SHARED
|
|
|
|
* to make it appear so).
|
|
|
|
*
|
|
|
|
* This code couples tightly to the attributes for the function_props[]
|
2008-08-11 17:47:05 -07:00
|
|
|
* initializers above, and to js_SetProperty and js_HasOwnProperty.
|
2007-03-22 10:30:00 -07:00
|
|
|
*
|
|
|
|
* It's important to allow delegating objects, even though they inherit
|
|
|
|
* this getter (fun_getProperty), to override arguments, arity, caller,
|
|
|
|
* and name. If we didn't return early for slot != ARGS_LENGTH, we would
|
|
|
|
* clobber *vp with the native property value, instead of letting script
|
|
|
|
* override that value in delegating objects.
|
|
|
|
*
|
|
|
|
* Note how that clobbering is what simulates JSPROP_READONLY for all of
|
|
|
|
* the non-standard properties when the directly addressed object (obj)
|
|
|
|
* is a function object (i.e., when this loop does not iterate).
|
|
|
|
*/
|
|
|
|
while (!(fun = (JSFunction *)
|
|
|
|
JS_GetInstancePrivate(cx, obj, &js_FunctionClass, NULL))) {
|
|
|
|
if (slot != ARGS_LENGTH)
|
|
|
|
return JS_TRUE;
|
|
|
|
obj = OBJ_GET_PROTO(cx, obj);
|
|
|
|
if (!obj)
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Find fun's top-most activation record. */
|
|
|
|
for (fp = cx->fp; fp && (fp->fun != fun || (fp->flags & JSFRAME_SPECIAL));
|
|
|
|
fp = fp->down) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (slot) {
|
|
|
|
case CALL_ARGUMENTS:
|
|
|
|
/* Warn if strict about f.arguments or equivalent unqualified uses. */
|
|
|
|
if (!JS_ReportErrorFlagsAndNumber(cx,
|
|
|
|
JSREPORT_WARNING | JSREPORT_STRICT,
|
|
|
|
js_GetErrorMessage, NULL,
|
|
|
|
JSMSG_DEPRECATED_USAGE,
|
|
|
|
js_arguments_str)) {
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
if (fp) {
|
|
|
|
if (!js_GetArgsValue(cx, fp, vp))
|
|
|
|
return JS_FALSE;
|
|
|
|
} else {
|
|
|
|
*vp = JSVAL_NULL;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ARGS_LENGTH:
|
|
|
|
case FUN_ARITY:
|
2008-03-29 03:34:29 -07:00
|
|
|
*vp = INT_TO_JSVAL((jsint)fun->nargs);
|
2007-03-22 10:30:00 -07:00
|
|
|
break;
|
|
|
|
|
2008-03-29 03:34:29 -07:00
|
|
|
case FUN_NAME:
|
|
|
|
*vp = fun->atom
|
|
|
|
? ATOM_KEY(fun->atom)
|
|
|
|
: STRING_TO_JSVAL(cx->runtime->emptyString);
|
2007-03-22 10:30:00 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case FUN_CALLER:
|
2007-08-01 21:33:52 -07:00
|
|
|
if (fp && fp->down && fp->down->fun)
|
|
|
|
*vp = OBJECT_TO_JSVAL(fp->down->callee);
|
2007-03-22 10:30:00 -07:00
|
|
|
else
|
|
|
|
*vp = JSVAL_NULL;
|
2008-09-05 16:24:53 -07:00
|
|
|
if (!JSVAL_IS_PRIMITIVE(*vp)) {
|
|
|
|
callbacks = JS_GetSecurityCallbacks(cx);
|
|
|
|
if (callbacks && callbacks->checkObjectAccess) {
|
|
|
|
id = ATOM_KEY(cx->runtime->atomState.callerAtom);
|
|
|
|
if (!callbacks->checkObjectAccess(cx, obj, id, JSACC_READ, vp))
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
/* XXX fun[0] and fun.arguments[0] are equivalent. */
|
2008-03-29 03:34:29 -07:00
|
|
|
if (fp && fp->fun && (uintN)slot < fp->fun->nargs)
|
2007-03-22 10:30:00 -07:00
|
|
|
*vp = fp->argv[slot];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
2008-09-04 21:56:37 -07:00
|
|
|
/*
|
|
|
|
* ECMA-262 specifies that length is a property of function object instances,
|
|
|
|
* but we can avoid that space cost by delegating to a prototype property that
|
|
|
|
* is JSPROP_PERMANENT and JSPROP_SHARED. Each fun_getProperty call computes
|
|
|
|
* a fresh length value based on the arity of the individual function object's
|
|
|
|
* private data.
|
|
|
|
*
|
|
|
|
* The extensions below other than length, i.e., the ones not in ECMA-262,
|
|
|
|
* are neither JSPROP_READONLY nor JSPROP_SHARED, because for compatibility
|
|
|
|
* with ECMA we must allow a delegating object to override them. Therefore to
|
|
|
|
* avoid entraining garbage in Function.prototype slots, they must be resolved
|
|
|
|
* in non-prototype function objects, wherefore the lazy_function_props table
|
|
|
|
* and fun_resolve's use of it.
|
|
|
|
*/
|
|
|
|
#define LENGTH_PROP_ATTRS (JSPROP_READONLY|JSPROP_PERMANENT|JSPROP_SHARED)
|
|
|
|
|
|
|
|
static JSPropertySpec function_props[] = {
|
|
|
|
{js_length_str, ARGS_LENGTH, LENGTH_PROP_ATTRS, fun_getProperty, JS_PropertyStub},
|
|
|
|
{0,0,0,0,0}
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct LazyFunctionProp {
|
|
|
|
uint16 atomOffset;
|
|
|
|
int8 tinyid;
|
|
|
|
uint8 attrs;
|
|
|
|
} LazyFunctionProp;
|
|
|
|
|
|
|
|
/* NB: no sentinel at the end -- use JS_ARRAY_LENGTH to bound loops. */
|
|
|
|
static LazyFunctionProp lazy_function_props[] = {
|
|
|
|
{ATOM_OFFSET(arguments), CALL_ARGUMENTS, JSPROP_PERMANENT},
|
|
|
|
{ATOM_OFFSET(arity), FUN_ARITY, JSPROP_PERMANENT},
|
|
|
|
{ATOM_OFFSET(caller), FUN_CALLER, JSPROP_PERMANENT},
|
|
|
|
{ATOM_OFFSET(name), FUN_NAME, JSPROP_PERMANENT},
|
|
|
|
};
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
static JSBool
|
|
|
|
fun_enumerate(JSContext *cx, JSObject *obj)
|
|
|
|
{
|
|
|
|
jsid prototypeId;
|
|
|
|
JSObject *pobj;
|
|
|
|
JSProperty *prop;
|
|
|
|
|
|
|
|
prototypeId = ATOM_TO_JSID(cx->runtime->atomState.classPrototypeAtom);
|
|
|
|
if (!OBJ_LOOKUP_PROPERTY(cx, obj, prototypeId, &pobj, &prop))
|
|
|
|
return JS_FALSE;
|
|
|
|
if (prop)
|
|
|
|
OBJ_DROP_PROPERTY(cx, pobj, prop);
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
|
|
|
fun_resolve(JSContext *cx, JSObject *obj, jsval id, uintN flags,
|
|
|
|
JSObject **objp)
|
|
|
|
{
|
|
|
|
JSFunction *fun;
|
2007-06-26 11:19:54 -07:00
|
|
|
JSAtom *atom;
|
|
|
|
uintN i;
|
|
|
|
|
|
|
|
if (!JSVAL_IS_STRING(id))
|
|
|
|
return JS_TRUE;
|
|
|
|
|
2008-03-28 15:27:36 -07:00
|
|
|
fun = GET_FUNCTION_PRIVATE(cx, obj);
|
2007-06-19 11:49:53 -07:00
|
|
|
|
|
|
|
/*
|
2007-11-19 09:15:45 -08:00
|
|
|
* No need to reflect fun.prototype in 'fun.prototype = ... '.
|
|
|
|
*
|
|
|
|
* This is not just an optimization, because we must not resolve when
|
|
|
|
* defining hidden properties during compilation. The setup code for the
|
|
|
|
* prototype and the lazy properties below eventually calls the property
|
|
|
|
* hooks for the function object. That in turn calls fun_reserveSlots to
|
|
|
|
* get the number of the reserved slots which is just the number of
|
|
|
|
* regular expressions literals in the function. When compiling, that
|
|
|
|
* number is not yet ready so we must make sure that fun_resolve does
|
|
|
|
* nothing until the code for the function is generated.
|
2007-06-26 11:19:54 -07:00
|
|
|
*/
|
|
|
|
if (flags & JSRESOLVE_ASSIGNING)
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_TRUE;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Ok, check whether id is 'prototype' and bootstrap the function object's
|
|
|
|
* prototype property.
|
|
|
|
*/
|
2007-06-26 11:19:54 -07:00
|
|
|
atom = cx->runtime->atomState.classPrototypeAtom;
|
|
|
|
if (id == ATOM_KEY(atom)) {
|
2007-12-19 05:20:45 -08:00
|
|
|
JSObject *proto;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Beware of the wacky case of a user function named Object -- trying
|
|
|
|
* to find a prototype for that will recur back here _ad perniciem_.
|
|
|
|
*/
|
2008-03-29 03:34:29 -07:00
|
|
|
if (fun->atom == CLASS_ATOM(cx, Object))
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_TRUE;
|
|
|
|
|
|
|
|
/*
|
2007-12-19 05:20:45 -08:00
|
|
|
* Make the prototype object to have the same parent as the function
|
|
|
|
* object itself.
|
2007-03-22 10:30:00 -07:00
|
|
|
*/
|
2008-04-02 00:46:12 -07:00
|
|
|
proto = js_NewObject(cx, &js_ObjectClass, NULL, OBJ_GET_PARENT(cx, obj),
|
|
|
|
0);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!proto)
|
|
|
|
return JS_FALSE;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ECMA (15.3.5.2) says that constructor.prototype is DontDelete for
|
|
|
|
* user-defined functions, but DontEnum | ReadOnly | DontDelete for
|
|
|
|
* native "system" constructors such as Object or Function. So lazily
|
|
|
|
* set the former here in fun_resolve, but eagerly define the latter
|
|
|
|
* in JS_InitClass, with the right attributes.
|
|
|
|
*/
|
|
|
|
if (!js_SetClassPrototype(cx, obj, proto,
|
|
|
|
JSPROP_ENUMERATE | JSPROP_PERMANENT)) {
|
|
|
|
cx->weakRoots.newborn[GCX_OBJECT] = NULL;
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
*objp = obj;
|
2007-06-26 11:19:54 -07:00
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < JS_ARRAY_LENGTH(lazy_function_props); i++) {
|
|
|
|
LazyFunctionProp *lfp = &lazy_function_props[i];
|
|
|
|
|
|
|
|
atom = OFFSET_TO_ATOM(cx->runtime, lfp->atomOffset);
|
|
|
|
if (id == ATOM_KEY(atom)) {
|
|
|
|
if (!js_DefineNativeProperty(cx, obj,
|
|
|
|
ATOM_TO_JSID(atom), JSVAL_VOID,
|
2008-09-04 21:56:37 -07:00
|
|
|
fun_getProperty, JS_PropertyStub,
|
|
|
|
lfp->attrs, SPROP_HAS_SHORTID,
|
|
|
|
lfp->tinyid, NULL)) {
|
2007-06-26 11:19:54 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
*objp = obj;
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
|
|
|
fun_convert(JSContext *cx, JSObject *obj, JSType type, jsval *vp)
|
|
|
|
{
|
|
|
|
switch (type) {
|
|
|
|
case JSTYPE_FUNCTION:
|
|
|
|
*vp = OBJECT_TO_JSVAL(obj);
|
|
|
|
return JS_TRUE;
|
|
|
|
default:
|
|
|
|
return js_TryValueOf(cx, obj, type, vp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#if JS_HAS_XDR
|
|
|
|
|
|
|
|
/* XXX store parent and proto, if defined */
|
|
|
|
static JSBool
|
|
|
|
fun_xdrObject(JSXDRState *xdr, JSObject **objp)
|
|
|
|
{
|
|
|
|
JSContext *cx;
|
2008-03-29 03:34:29 -07:00
|
|
|
JSFunction *fun;
|
2007-03-22 10:30:00 -07:00
|
|
|
uint32 nullAtom; /* flag to indicate if fun->atom is NULL */
|
2008-01-23 01:56:28 -08:00
|
|
|
uintN nargs, nvars, n;
|
2007-11-19 09:15:45 -08:00
|
|
|
uint32 localsword; /* word to xdr argument and variable counts */
|
2007-11-18 16:58:46 -08:00
|
|
|
uint32 flagsword; /* originally only flags was JS_XDRUint8'd */
|
2007-11-19 09:15:45 -08:00
|
|
|
JSTempValueRooter tvr;
|
2007-03-22 10:30:00 -07:00
|
|
|
JSBool ok;
|
|
|
|
|
|
|
|
cx = xdr->cx;
|
|
|
|
if (xdr->mode == JSXDR_ENCODE) {
|
2008-03-29 03:34:29 -07:00
|
|
|
fun = GET_FUNCTION_PRIVATE(cx, *objp);
|
|
|
|
if (!FUN_INTERPRETED(fun)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
|
|
|
|
JSMSG_NOT_SCRIPTED_FUNCTION,
|
2008-03-29 03:34:29 -07:00
|
|
|
JS_GetFunctionName(fun));
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
}
|
2008-03-28 15:27:36 -07:00
|
|
|
nullAtom = !fun->atom;
|
|
|
|
nargs = fun->nargs;
|
2008-03-29 03:34:29 -07:00
|
|
|
nvars = fun->u.i.nvars;
|
2007-11-19 09:15:45 -08:00
|
|
|
localsword = (nargs << 16) | nvars;
|
2008-03-28 15:27:36 -07:00
|
|
|
flagsword = fun->flags;
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
2008-03-29 03:34:29 -07:00
|
|
|
fun = js_NewFunction(cx, NULL, NULL, 0, JSFUN_INTERPRETED, NULL, NULL);
|
2008-03-28 15:27:36 -07:00
|
|
|
if (!fun)
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
2008-07-20 05:53:21 -07:00
|
|
|
STOBJ_CLEAR_PARENT(FUN_OBJECT(fun));
|
|
|
|
STOBJ_CLEAR_PROTO(FUN_OBJECT(fun));
|
2007-11-19 09:15:45 -08:00
|
|
|
#ifdef __GNUC__
|
|
|
|
nvars = nargs = 0; /* quell GCC uninitialized warning */
|
|
|
|
#endif
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* From here on, control flow must flow through label out. */
|
2008-04-02 00:46:12 -07:00
|
|
|
JS_PUSH_TEMP_ROOT_OBJECT(cx, FUN_OBJECT(fun), &tvr);
|
2007-03-22 10:30:00 -07:00
|
|
|
ok = JS_TRUE;
|
|
|
|
|
|
|
|
if (!JS_XDRUint32(xdr, &nullAtom))
|
|
|
|
goto bad;
|
2008-03-28 15:27:36 -07:00
|
|
|
if (!nullAtom && !js_XDRStringAtom(xdr, &fun->atom))
|
2007-03-22 10:30:00 -07:00
|
|
|
goto bad;
|
2007-11-19 09:15:45 -08:00
|
|
|
if (!JS_XDRUint32(xdr, &localsword) ||
|
2007-03-22 10:30:00 -07:00
|
|
|
!JS_XDRUint32(xdr, &flagsword)) {
|
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
|
2007-11-19 09:15:45 -08:00
|
|
|
if (xdr->mode == JSXDR_DECODE) {
|
|
|
|
nargs = localsword >> 16;
|
|
|
|
nvars = localsword & JS_BITMASK(16);
|
2008-03-29 03:34:29 -07:00
|
|
|
JS_ASSERT(flagsword | JSFUN_INTERPRETED);
|
2008-03-28 15:27:36 -07:00
|
|
|
fun->flags = (uint16) flagsword;
|
2007-11-19 09:15:45 -08:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/* do arguments and local vars */
|
2008-04-02 00:46:12 -07:00
|
|
|
n = nargs + nvars;
|
|
|
|
if (n != 0) {
|
2008-01-23 01:56:28 -08:00
|
|
|
void *mark;
|
|
|
|
uintN i;
|
|
|
|
uintN bitmapLength;
|
|
|
|
uint32 *bitmap;
|
2008-02-08 16:01:45 -08:00
|
|
|
jsuword *names;
|
|
|
|
JSAtom *name;
|
2008-01-23 01:56:28 -08:00
|
|
|
JSLocalKind localKind;
|
|
|
|
|
|
|
|
mark = JS_ARENA_MARK(&xdr->cx->tempPool);
|
|
|
|
|
2008-02-08 16:01:45 -08:00
|
|
|
/*
|
|
|
|
* From this point the control must flow via the label release_mark.
|
|
|
|
*
|
|
|
|
* To xdr the names we prefix the names with a bitmap descriptor and
|
|
|
|
* then xdr the names as strings. For argument names (indexes below
|
|
|
|
* nargs) the corresponding bit in the bitmap is unset when the name
|
|
|
|
* is null. Such null names are not encoded or decoded. For variable
|
|
|
|
* names (indexes starting from nargs) bitmap's bit is set when the
|
|
|
|
* name is declared as const, not as ordinary var.
|
|
|
|
* */
|
2008-01-23 01:56:28 -08:00
|
|
|
bitmapLength = JS_HOWMANY(n, JS_BITS_PER_UINT32);
|
2008-02-08 16:01:45 -08:00
|
|
|
JS_ARENA_ALLOCATE_CAST(bitmap, uint32 *, &xdr->cx->tempPool,
|
|
|
|
bitmapLength * sizeof *bitmap);
|
|
|
|
if (!bitmap) {
|
|
|
|
js_ReportOutOfScriptQuota(xdr->cx);
|
|
|
|
ok = JS_FALSE;
|
|
|
|
goto release_mark;
|
|
|
|
}
|
2008-01-23 01:56:28 -08:00
|
|
|
if (xdr->mode == JSXDR_ENCODE) {
|
2008-03-28 15:27:36 -07:00
|
|
|
names = js_GetLocalNameArray(xdr->cx, fun, &xdr->cx->tempPool);
|
2008-01-23 01:56:28 -08:00
|
|
|
if (!names) {
|
|
|
|
ok = JS_FALSE;
|
|
|
|
goto release_mark;
|
|
|
|
}
|
2008-02-08 16:01:45 -08:00
|
|
|
memset(bitmap, 0, bitmapLength * sizeof *bitmap);
|
|
|
|
for (i = 0; i != n; ++i) {
|
2008-03-28 15:27:36 -07:00
|
|
|
if (i < fun->nargs
|
2008-02-08 16:01:45 -08:00
|
|
|
? JS_LOCAL_NAME_TO_ATOM(names[i]) != NULL
|
|
|
|
: JS_LOCAL_NAME_IS_CONST(names[i])) {
|
|
|
|
bitmap[i >> JS_BITS_PER_UINT32_LOG2] |=
|
|
|
|
JS_BIT(i & (JS_BITS_PER_UINT32 - 1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-01-23 01:56:28 -08:00
|
|
|
#ifdef __GNUC__
|
2008-02-08 16:01:45 -08:00
|
|
|
else {
|
2008-01-23 01:56:28 -08:00
|
|
|
names = NULL; /* quell GCC uninitialized warning */
|
|
|
|
}
|
2008-02-08 16:01:45 -08:00
|
|
|
#endif
|
2008-01-23 01:56:28 -08:00
|
|
|
for (i = 0; i != bitmapLength; ++i) {
|
|
|
|
ok = JS_XDRUint32(xdr, &bitmap[i]);
|
|
|
|
if (!ok)
|
|
|
|
goto release_mark;
|
|
|
|
}
|
|
|
|
for (i = 0; i != n; ++i) {
|
|
|
|
if (i < nargs &&
|
2008-02-08 16:01:45 -08:00
|
|
|
!(bitmap[i >> JS_BITS_PER_UINT32_LOG2] &
|
2008-01-23 01:56:28 -08:00
|
|
|
JS_BIT(i & (JS_BITS_PER_UINT32 - 1)))) {
|
|
|
|
if (xdr->mode == JSXDR_DECODE) {
|
2008-03-28 15:27:36 -07:00
|
|
|
ok = js_AddLocal(xdr->cx, fun, NULL, JSLOCAL_ARG);
|
2008-01-23 01:56:28 -08:00
|
|
|
if (!ok)
|
|
|
|
goto release_mark;
|
|
|
|
} else {
|
2008-02-08 16:01:45 -08:00
|
|
|
JS_ASSERT(!JS_LOCAL_NAME_TO_ATOM(names[i]));
|
2008-01-23 01:56:28 -08:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (xdr->mode == JSXDR_ENCODE)
|
2008-02-08 16:01:45 -08:00
|
|
|
name = JS_LOCAL_NAME_TO_ATOM(names[i]);
|
2008-01-23 01:56:28 -08:00
|
|
|
ok = js_XDRStringAtom(xdr, &name);
|
|
|
|
if (!ok)
|
|
|
|
goto release_mark;
|
|
|
|
if (xdr->mode == JSXDR_DECODE) {
|
|
|
|
localKind = (i < nargs)
|
|
|
|
? JSLOCAL_ARG
|
2008-02-08 16:01:45 -08:00
|
|
|
: bitmap[i >> JS_BITS_PER_UINT32_LOG2] &
|
2008-01-23 01:56:28 -08:00
|
|
|
JS_BIT(i & (JS_BITS_PER_UINT32 - 1))
|
|
|
|
? JSLOCAL_CONST
|
|
|
|
: JSLOCAL_VAR;
|
2008-03-28 15:27:36 -07:00
|
|
|
ok = js_AddLocal(xdr->cx, fun, name, localKind);
|
2008-01-23 01:56:28 -08:00
|
|
|
if (!ok)
|
|
|
|
goto release_mark;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ok = JS_TRUE;
|
|
|
|
|
|
|
|
release_mark:
|
|
|
|
JS_ARENA_RELEASE(&xdr->cx->tempPool, mark);
|
|
|
|
if (!ok)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
if (xdr->mode == JSXDR_DECODE)
|
2008-03-28 15:27:36 -07:00
|
|
|
js_FreezeLocalNames(cx, fun);
|
2008-01-23 01:56:28 -08:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-03-29 03:34:29 -07:00
|
|
|
if (!js_XDRScript(xdr, &fun->u.i.script, NULL))
|
2007-03-22 10:30:00 -07:00
|
|
|
goto bad;
|
|
|
|
|
|
|
|
if (xdr->mode == JSXDR_DECODE) {
|
2008-04-02 00:46:12 -07:00
|
|
|
*objp = FUN_OBJECT(fun);
|
2008-02-07 15:18:45 -08:00
|
|
|
#ifdef CHECK_SCRIPT_OWNER
|
2008-03-29 03:34:29 -07:00
|
|
|
fun->u.i.script->owner = NULL;
|
2008-02-07 15:18:45 -08:00
|
|
|
#endif
|
2008-03-29 03:34:29 -07:00
|
|
|
js_CallNewScriptHook(cx, fun->u.i.script, fun);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
JS_POP_TEMP_ROOT(cx, &tvr);
|
|
|
|
return ok;
|
|
|
|
|
|
|
|
bad:
|
|
|
|
ok = JS_FALSE;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else /* !JS_HAS_XDR */
|
|
|
|
|
|
|
|
#define fun_xdrObject NULL
|
|
|
|
|
|
|
|
#endif /* !JS_HAS_XDR */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* [[HasInstance]] internal method for Function objects: fetch the .prototype
|
|
|
|
* property of its 'this' parameter, and walks the prototype chain of v (only
|
|
|
|
* if v is an object) returning true if .prototype is found.
|
|
|
|
*/
|
|
|
|
static JSBool
|
|
|
|
fun_hasInstance(JSContext *cx, JSObject *obj, jsval v, JSBool *bp)
|
|
|
|
{
|
|
|
|
jsval pval;
|
|
|
|
|
|
|
|
if (!OBJ_GET_PROPERTY(cx, obj,
|
|
|
|
ATOM_TO_JSID(cx->runtime->atomState
|
|
|
|
.classPrototypeAtom),
|
|
|
|
&pval)) {
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (JSVAL_IS_PRIMITIVE(pval)) {
|
|
|
|
/*
|
|
|
|
* Throw a runtime error if instanceof is called on a function that
|
|
|
|
* has a non-object as its .prototype value.
|
|
|
|
*/
|
|
|
|
js_ReportValueError(cx, JSMSG_BAD_PROTOTYPE,
|
|
|
|
-1, OBJECT_TO_JSVAL(obj), NULL);
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return js_IsDelegate(cx, JSVAL_TO_OBJECT(pval), v, bp);
|
|
|
|
}
|
|
|
|
|
2008-04-02 00:46:12 -07:00
|
|
|
static void
|
|
|
|
TraceLocalNames(JSTracer *trc, JSFunction *fun);
|
|
|
|
|
|
|
|
static void
|
|
|
|
DestroyLocalNames(JSContext *cx, JSFunction *fun);
|
|
|
|
|
2007-04-16 23:53:37 -07:00
|
|
|
static void
|
|
|
|
fun_trace(JSTracer *trc, JSObject *obj)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
JSFunction *fun;
|
2008-03-28 15:27:36 -07:00
|
|
|
|
|
|
|
/* A newborn function object may have a not yet initialized private slot. */
|
|
|
|
fun = (JSFunction *) JS_GetPrivate(trc->context, obj);
|
2008-04-02 00:46:12 -07:00
|
|
|
if (!fun)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (FUN_OBJECT(fun) != obj) {
|
|
|
|
/* obj is cloned function object, trace the original. */
|
|
|
|
JS_CALL_TRACER(trc, FUN_OBJECT(fun), JSTRACE_OBJECT, "private");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (fun->atom)
|
|
|
|
JS_CALL_STRING_TRACER(trc, ATOM_TO_STRING(fun->atom), "atom");
|
|
|
|
if (FUN_INTERPRETED(fun)) {
|
|
|
|
if (fun->u.i.script)
|
|
|
|
js_TraceScript(trc, fun->u.i.script);
|
|
|
|
TraceLocalNames(trc, fun);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
fun_finalize(JSContext *cx, JSObject *obj)
|
|
|
|
{
|
|
|
|
JSFunction *fun;
|
|
|
|
|
|
|
|
/* Ignore newborn and cloned function objects. */
|
|
|
|
fun = (JSFunction *) JS_GetPrivate(cx, obj);
|
|
|
|
if (!fun || FUN_OBJECT(fun) != obj)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Null-check of u.i.script is required since the parser sets interpreted
|
|
|
|
* very early.
|
|
|
|
*/
|
|
|
|
if (FUN_INTERPRETED(fun)) {
|
|
|
|
if (fun->u.i.script)
|
|
|
|
js_DestroyScript(cx, fun->u.i.script);
|
|
|
|
DestroyLocalNames(cx, fun);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static uint32
|
|
|
|
fun_reserveSlots(JSContext *cx, JSObject *obj)
|
|
|
|
{
|
2008-03-28 15:27:36 -07:00
|
|
|
JSFunction *fun;
|
2008-08-21 03:47:33 -07:00
|
|
|
uint32 nslots;
|
2008-03-21 01:19:23 -07:00
|
|
|
|
2008-03-23 03:04:38 -07:00
|
|
|
/*
|
2008-03-28 15:27:36 -07:00
|
|
|
* We use JS_GetPrivate and not GET_FUNCTION_PRIVATE because during
|
|
|
|
* js_InitFunctionClass invocation the function is called before the
|
|
|
|
* private slot of the function object is set.
|
2008-03-23 03:04:38 -07:00
|
|
|
*/
|
2008-03-28 15:27:36 -07:00
|
|
|
fun = (JSFunction *) JS_GetPrivate(cx, obj);
|
2008-08-21 03:47:33 -07:00
|
|
|
nslots = 0;
|
|
|
|
if (fun && FUN_INTERPRETED(fun) && fun->u.i.script) {
|
|
|
|
if (fun->u.i.script->upvarsOffset != 0)
|
|
|
|
nslots = JS_SCRIPT_UPVARS(fun->u.i.script)->length;
|
|
|
|
if (fun->u.i.script->regexpsOffset != 0)
|
|
|
|
nslots += JS_SCRIPT_REGEXPS(fun->u.i.script)->length;
|
|
|
|
}
|
|
|
|
return nslots;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Reserve two slots in all function objects for XPConnect. Note that this
|
|
|
|
* does not bloat every instance, only those on which reserved slots are set,
|
|
|
|
* and those on which ad-hoc properties are defined.
|
|
|
|
*/
|
|
|
|
JS_FRIEND_DATA(JSClass) js_FunctionClass = {
|
|
|
|
js_Function_str,
|
|
|
|
JSCLASS_HAS_PRIVATE | JSCLASS_NEW_RESOLVE | JSCLASS_HAS_RESERVED_SLOTS(2) |
|
2007-12-20 15:29:31 -08:00
|
|
|
JSCLASS_MARK_IS_TRACE | JSCLASS_HAS_CACHED_PROTO(JSProto_Function),
|
2007-03-22 10:30:00 -07:00
|
|
|
JS_PropertyStub, JS_PropertyStub,
|
2008-09-04 21:56:37 -07:00
|
|
|
JS_PropertyStub, JS_PropertyStub,
|
2007-03-22 10:30:00 -07:00
|
|
|
fun_enumerate, (JSResolveOp)fun_resolve,
|
2008-04-02 00:46:12 -07:00
|
|
|
fun_convert, fun_finalize,
|
2007-03-22 10:30:00 -07:00
|
|
|
NULL, NULL,
|
|
|
|
NULL, NULL,
|
|
|
|
fun_xdrObject, fun_hasInstance,
|
2007-04-16 23:53:37 -07:00
|
|
|
JS_CLASS_TRACE(fun_trace), fun_reserveSlots
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
2007-08-01 21:33:52 -07:00
|
|
|
static JSBool
|
|
|
|
fun_toStringHelper(JSContext *cx, uint32 indent, uintN argc, jsval *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
jsval fval;
|
2007-08-01 21:33:52 -07:00
|
|
|
JSObject *obj;
|
2007-03-22 10:30:00 -07:00
|
|
|
JSFunction *fun;
|
|
|
|
JSString *str;
|
|
|
|
|
2008-02-17 16:12:33 -08:00
|
|
|
fval = JS_THIS(cx, vp);
|
|
|
|
if (JSVAL_IS_NULL(fval))
|
|
|
|
return JS_FALSE;
|
|
|
|
|
2007-08-01 21:33:52 -07:00
|
|
|
if (!VALUE_IS_FUNCTION(cx, fval)) {
|
|
|
|
/*
|
|
|
|
* If we don't have a function to start off with, try converting the
|
|
|
|
* object to a function. If that doesn't work, complain.
|
|
|
|
*/
|
|
|
|
if (!JSVAL_IS_PRIMITIVE(fval)) {
|
|
|
|
obj = JSVAL_TO_OBJECT(fval);
|
|
|
|
if (!OBJ_GET_CLASS(cx, obj)->convert(cx, obj, JSTYPE_FUNCTION,
|
|
|
|
&fval)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
}
|
2007-08-01 21:33:52 -07:00
|
|
|
vp[1] = fval;
|
|
|
|
}
|
|
|
|
if (!VALUE_IS_FUNCTION(cx, fval)) {
|
|
|
|
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
|
|
|
|
JSMSG_INCOMPATIBLE_PROTO,
|
|
|
|
js_Function_str, js_toString_str,
|
|
|
|
JS_GetTypeName(cx, JS_TypeOfValue(cx, fval)));
|
|
|
|
return JS_FALSE;
|
2007-07-21 14:39:42 -07:00
|
|
|
}
|
2007-07-21 16:21:20 -07:00
|
|
|
}
|
2007-07-21 14:39:42 -07:00
|
|
|
|
2007-08-01 21:33:52 -07:00
|
|
|
obj = JSVAL_TO_OBJECT(fval);
|
2008-03-06 13:40:43 -08:00
|
|
|
if (argc != 0) {
|
|
|
|
indent = js_ValueToECMAUint32(cx, &vp[2]);
|
2008-03-06 15:24:08 -08:00
|
|
|
if (JSVAL_IS_NULL(vp[2]))
|
2008-03-06 13:40:43 -08:00
|
|
|
return JS_FALSE;
|
|
|
|
}
|
2007-08-01 21:33:52 -07:00
|
|
|
|
|
|
|
JS_ASSERT(JS_ObjectIsFunction(cx, obj));
|
2008-03-28 15:27:36 -07:00
|
|
|
fun = GET_FUNCTION_PRIVATE(cx, obj);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!fun)
|
|
|
|
return JS_TRUE;
|
|
|
|
str = JS_DecompileFunction(cx, fun, (uintN)indent);
|
|
|
|
if (!str)
|
|
|
|
return JS_FALSE;
|
2007-08-01 21:33:52 -07:00
|
|
|
*vp = STRING_TO_JSVAL(str);
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2007-08-01 21:33:52 -07:00
|
|
|
fun_toString(JSContext *cx, uintN argc, jsval *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2007-08-01 21:33:52 -07:00
|
|
|
return fun_toStringHelper(cx, 0, argc, vp);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#if JS_HAS_TOSOURCE
|
|
|
|
static JSBool
|
2007-08-01 21:33:52 -07:00
|
|
|
fun_toSource(JSContext *cx, uintN argc, jsval *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2007-08-01 21:33:52 -07:00
|
|
|
return fun_toStringHelper(cx, JS_DONT_PRETTY_PRINT, argc, vp);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static const char call_str[] = "call";
|
|
|
|
|
|
|
|
static JSBool
|
2007-08-01 21:33:52 -07:00
|
|
|
fun_call(JSContext *cx, uintN argc, jsval *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2007-08-01 21:33:52 -07:00
|
|
|
JSObject *obj;
|
2007-09-18 00:34:54 -07:00
|
|
|
jsval fval, *argv, *invokevp;
|
2007-03-22 10:30:00 -07:00
|
|
|
JSString *str;
|
|
|
|
void *mark;
|
|
|
|
JSBool ok;
|
|
|
|
|
2008-02-17 16:12:33 -08:00
|
|
|
obj = JS_THIS_OBJECT(cx, vp);
|
|
|
|
if (!obj || !OBJ_DEFAULT_VALUE(cx, obj, JSTYPE_FUNCTION, &vp[1]))
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
2007-08-01 21:33:52 -07:00
|
|
|
fval = vp[1];
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (!VALUE_IS_FUNCTION(cx, fval)) {
|
|
|
|
str = JS_ValueToString(cx, fval);
|
|
|
|
if (str) {
|
|
|
|
const char *bytes = js_GetStringBytes(cx, str);
|
|
|
|
|
|
|
|
if (bytes) {
|
|
|
|
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
|
|
|
|
JSMSG_INCOMPATIBLE_PROTO,
|
|
|
|
js_Function_str, call_str,
|
|
|
|
bytes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
|
2007-08-01 21:33:52 -07:00
|
|
|
argv = vp + 2;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (argc == 0) {
|
|
|
|
/* Call fun with its global object as the 'this' param if no args. */
|
|
|
|
obj = NULL;
|
|
|
|
} else {
|
|
|
|
/* Otherwise convert the first arg to 'this' and skip over it. */
|
|
|
|
if (!JSVAL_IS_PRIMITIVE(argv[0]))
|
|
|
|
obj = JSVAL_TO_OBJECT(argv[0]);
|
|
|
|
else if (!js_ValueToObject(cx, argv[0], &obj))
|
|
|
|
return JS_FALSE;
|
|
|
|
argc--;
|
|
|
|
argv++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate stack space for fval, obj, and the args. */
|
2007-09-18 00:34:54 -07:00
|
|
|
invokevp = js_AllocStack(cx, 2 + argc, &mark);
|
|
|
|
if (!invokevp)
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
|
|
|
|
/* Push fval, obj, and the args. */
|
2007-09-18 00:34:54 -07:00
|
|
|
invokevp[0] = fval;
|
|
|
|
invokevp[1] = OBJECT_TO_JSVAL(obj);
|
|
|
|
memcpy(invokevp + 2, argv, argc * sizeof *argv);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-04-27 23:50:37 -07:00
|
|
|
ok = js_Invoke(cx, argc, invokevp, 0);
|
2007-09-18 00:34:54 -07:00
|
|
|
*vp = *invokevp;
|
2007-03-22 10:30:00 -07:00
|
|
|
js_FreeStack(cx, mark);
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
Kind of an Array initialiser tour-de-force for bug 452878:
1. Split FastNewArray from FastNewObject built-in for greater speed/specialization and further splitting into Array_1str, etc.
2. Add Array_1str, Array_2obj, and Array_3num builtins for benchmarked new Array(...) constructions.
3. Export ARRAY_SET_DENSE_LENGTH and ARRAY_GROWBY via jsarray.h to jstracer.cpp.
4. Tweaked SetArrayElement to make common/best case code be the predicted/prefetched path.
5. js_MakeArraySlow now preserves the pre-slow length in JSSLOT_ARRAY_COUTN as a jsval-tagged int if possible -- this will help the tracer avoid aborting on dense arrays that turned slow but not sparse by addition of a named property.
6. Export js_fun_apply and js_Object from their respective .cpp files, in these cases just to jstracer.cpp via local prototypes (no .h files involved).
7. More INS_CONSTPTR and INS_CONST macrology for better names in trace debug spew.
8. Fix TraceRecorder::test_property_cache to avoid aborting on JSOP_SETNAME that creates a new global, by setting it to undefined so it can be lazily imported. This helps 3d-raytrace.js, which has an unintended global loop control variable in a function.
9. JSTraceableNative loses its premature-deadwood tclasp member (my bad).
10. TraceRecorder::record_JSOP_NEW() handles 'new Object' now along with the 'new Array' variations. I also cut down the copy-paste code from JSOP_CALL's record method to mostly what is needed now.
11. Add KNOWN_NATIVE_DECL macro for concise prototype of library-private js_* native functions, and alphabetized the lists (too long for any other order to be winning).
12. Big honking special case for foo.apply(obj, [str]), which we can generalize as needed. Helps string-tagcloud.js. What's cool is how tracing allows us to rewrite this to foo(str) with this set to obj, eliminating the Function.prototype.apply. This requires some rewriting in JSOP_ENDINIT's record method.
2008-09-01 01:24:58 -07:00
|
|
|
JSBool
|
|
|
|
js_fun_apply(JSContext *cx, uintN argc, jsval *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2007-08-01 21:33:52 -07:00
|
|
|
JSObject *obj, *aobj;
|
2007-09-18 00:34:54 -07:00
|
|
|
jsval fval, *invokevp, *sp;
|
2007-03-22 10:30:00 -07:00
|
|
|
JSString *str;
|
|
|
|
jsuint length;
|
|
|
|
JSBool arraylike, ok;
|
|
|
|
void *mark;
|
|
|
|
uintN i;
|
|
|
|
|
|
|
|
if (argc == 0) {
|
|
|
|
/* Will get globalObject as 'this' and no other arguments. */
|
2007-08-01 21:33:52 -07:00
|
|
|
return fun_call(cx, argc, vp);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2008-02-17 16:12:33 -08:00
|
|
|
obj = JS_THIS_OBJECT(cx, vp);
|
|
|
|
if (!obj || !OBJ_DEFAULT_VALUE(cx, obj, JSTYPE_FUNCTION, &vp[1]))
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
2007-08-01 21:33:52 -07:00
|
|
|
fval = vp[1];
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (!VALUE_IS_FUNCTION(cx, fval)) {
|
|
|
|
str = JS_ValueToString(cx, fval);
|
|
|
|
if (str) {
|
|
|
|
const char *bytes = js_GetStringBytes(cx, str);
|
|
|
|
|
|
|
|
if (bytes) {
|
|
|
|
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
|
|
|
|
JSMSG_INCOMPATIBLE_PROTO,
|
|
|
|
js_Function_str, "apply",
|
|
|
|
bytes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Quell GCC overwarnings. */
|
|
|
|
aobj = NULL;
|
|
|
|
length = 0;
|
|
|
|
|
|
|
|
if (argc >= 2) {
|
|
|
|
/* If the 2nd arg is null or void, call the function with 0 args. */
|
2007-08-01 21:33:52 -07:00
|
|
|
if (JSVAL_IS_NULL(vp[3]) || JSVAL_IS_VOID(vp[3])) {
|
2007-03-22 10:30:00 -07:00
|
|
|
argc = 0;
|
|
|
|
} else {
|
|
|
|
/* The second arg must be an array (or arguments object). */
|
|
|
|
arraylike = JS_FALSE;
|
2007-08-01 21:33:52 -07:00
|
|
|
if (!JSVAL_IS_PRIMITIVE(vp[3])) {
|
|
|
|
aobj = JSVAL_TO_OBJECT(vp[3]);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!js_IsArrayLike(cx, aobj, &arraylike, &length))
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
if (!arraylike) {
|
|
|
|
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
|
|
|
|
JSMSG_BAD_APPLY_ARGS, "apply");
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Convert the first arg to 'this' and skip over it. */
|
2007-08-01 21:33:52 -07:00
|
|
|
if (!JSVAL_IS_PRIMITIVE(vp[2]))
|
|
|
|
obj = JSVAL_TO_OBJECT(vp[2]);
|
|
|
|
else if (!js_ValueToObject(cx, vp[2], &obj))
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
|
|
|
|
/* Allocate stack space for fval, obj, and the args. */
|
|
|
|
argc = (uintN)JS_MIN(length, ARRAY_INIT_LIMIT - 1);
|
2007-09-18 00:34:54 -07:00
|
|
|
invokevp = js_AllocStack(cx, 2 + argc, &mark);
|
|
|
|
if (!invokevp)
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
|
|
|
|
/* Push fval, obj, and aobj's elements as args. */
|
2007-09-18 00:34:54 -07:00
|
|
|
sp = invokevp;
|
2007-03-22 10:30:00 -07:00
|
|
|
*sp++ = fval;
|
|
|
|
*sp++ = OBJECT_TO_JSVAL(obj);
|
|
|
|
for (i = 0; i < argc; i++) {
|
|
|
|
ok = JS_GetElement(cx, aobj, (jsint)i, sp);
|
|
|
|
if (!ok)
|
|
|
|
goto out;
|
|
|
|
sp++;
|
|
|
|
}
|
|
|
|
|
2008-04-27 23:50:37 -07:00
|
|
|
ok = js_Invoke(cx, argc, invokevp, 0);
|
2007-09-18 00:34:54 -07:00
|
|
|
*vp = *invokevp;
|
2007-03-22 10:30:00 -07:00
|
|
|
out:
|
|
|
|
js_FreeStack(cx, mark);
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef NARCISSUS
|
|
|
|
static JSBool
|
2007-08-01 21:33:52 -07:00
|
|
|
fun_applyConstructor(JSContext *cx, uintN argc, jsval *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
JSObject *aobj;
|
|
|
|
uintN length, i;
|
|
|
|
void *mark;
|
2007-09-18 00:34:54 -07:00
|
|
|
jsval *invokevp, *sp;
|
2007-03-22 10:30:00 -07:00
|
|
|
JSBool ok;
|
|
|
|
|
2007-08-01 21:33:52 -07:00
|
|
|
if (JSVAL_IS_PRIMITIVE(vp[2]) ||
|
|
|
|
(aobj = JSVAL_TO_OBJECT(vp[2]),
|
2007-03-22 10:30:00 -07:00
|
|
|
OBJ_GET_CLASS(cx, aobj) != &js_ArrayClass &&
|
|
|
|
OBJ_GET_CLASS(cx, aobj) != &js_ArgumentsClass)) {
|
|
|
|
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
|
|
|
|
JSMSG_BAD_APPLY_ARGS, "__applyConstruct__");
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!js_GetLengthProperty(cx, aobj, &length))
|
|
|
|
return JS_FALSE;
|
|
|
|
|
|
|
|
if (length >= ARRAY_INIT_LIMIT)
|
|
|
|
length = ARRAY_INIT_LIMIT - 1;
|
2007-09-18 00:34:54 -07:00
|
|
|
invokevp = js_AllocStack(cx, 2 + length, &mark);
|
|
|
|
if (!invokevp)
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
|
2007-09-18 00:34:54 -07:00
|
|
|
sp = invokevp;
|
2007-08-01 21:33:52 -07:00
|
|
|
*sp++ = vp[1];
|
2008-02-17 16:12:33 -08:00
|
|
|
*sp++ = JSVAL_NULL; /* this is filled automagically */
|
2007-03-22 10:30:00 -07:00
|
|
|
for (i = 0; i < length; i++) {
|
|
|
|
ok = JS_GetElement(cx, aobj, (jsint)i, sp);
|
|
|
|
if (!ok)
|
|
|
|
goto out;
|
|
|
|
sp++;
|
|
|
|
}
|
|
|
|
|
2008-09-18 14:13:37 -07:00
|
|
|
ok = js_InvokeConstructor(cx, length, JS_TRUE, invokevp);
|
2007-09-18 00:34:54 -07:00
|
|
|
*vp = *invokevp;
|
2007-03-22 10:30:00 -07:00
|
|
|
out:
|
|
|
|
js_FreeStack(cx, mark);
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static JSFunctionSpec function_methods[] = {
|
|
|
|
#if JS_HAS_TOSOURCE
|
2008-08-08 09:02:50 -07:00
|
|
|
JS_FN(js_toSource_str, fun_toSource, 0,0),
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif
|
2008-08-08 09:02:50 -07:00
|
|
|
JS_FN(js_toString_str, fun_toString, 0,0),
|
Kind of an Array initialiser tour-de-force for bug 452878:
1. Split FastNewArray from FastNewObject built-in for greater speed/specialization and further splitting into Array_1str, etc.
2. Add Array_1str, Array_2obj, and Array_3num builtins for benchmarked new Array(...) constructions.
3. Export ARRAY_SET_DENSE_LENGTH and ARRAY_GROWBY via jsarray.h to jstracer.cpp.
4. Tweaked SetArrayElement to make common/best case code be the predicted/prefetched path.
5. js_MakeArraySlow now preserves the pre-slow length in JSSLOT_ARRAY_COUTN as a jsval-tagged int if possible -- this will help the tracer avoid aborting on dense arrays that turned slow but not sparse by addition of a named property.
6. Export js_fun_apply and js_Object from their respective .cpp files, in these cases just to jstracer.cpp via local prototypes (no .h files involved).
7. More INS_CONSTPTR and INS_CONST macrology for better names in trace debug spew.
8. Fix TraceRecorder::test_property_cache to avoid aborting on JSOP_SETNAME that creates a new global, by setting it to undefined so it can be lazily imported. This helps 3d-raytrace.js, which has an unintended global loop control variable in a function.
9. JSTraceableNative loses its premature-deadwood tclasp member (my bad).
10. TraceRecorder::record_JSOP_NEW() handles 'new Object' now along with the 'new Array' variations. I also cut down the copy-paste code from JSOP_CALL's record method to mostly what is needed now.
11. Add KNOWN_NATIVE_DECL macro for concise prototype of library-private js_* native functions, and alphabetized the lists (too long for any other order to be winning).
12. Big honking special case for foo.apply(obj, [str]), which we can generalize as needed. Helps string-tagcloud.js. What's cool is how tracing allows us to rewrite this to foo(str) with this set to obj, eliminating the Function.prototype.apply. This requires some rewriting in JSOP_ENDINIT's record method.
2008-09-01 01:24:58 -07:00
|
|
|
JS_FN("apply", js_fun_apply, 2,0),
|
2008-08-08 09:02:50 -07:00
|
|
|
JS_FN(call_str, fun_call, 1,0),
|
2007-03-22 10:30:00 -07:00
|
|
|
#ifdef NARCISSUS
|
2007-10-13 13:09:48 -07:00
|
|
|
JS_FN("__applyConstructor__", fun_applyConstructor, 0,1,0),
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif
|
2007-08-01 21:33:52 -07:00
|
|
|
JS_FS_END
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
static JSBool
|
|
|
|
Function(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
|
|
|
|
{
|
|
|
|
JSStackFrame *fp, *caller;
|
2008-03-29 03:34:29 -07:00
|
|
|
JSFunction *fun;
|
2007-03-22 10:30:00 -07:00
|
|
|
JSObject *parent;
|
2007-11-19 09:15:45 -08:00
|
|
|
uintN i, n, lineno;
|
2007-03-22 10:30:00 -07:00
|
|
|
JSAtom *atom;
|
|
|
|
const char *filename;
|
2007-11-19 09:15:45 -08:00
|
|
|
JSBool ok;
|
2007-03-22 10:30:00 -07:00
|
|
|
JSString *str, *arg;
|
2007-11-29 06:49:42 -08:00
|
|
|
JSTokenStream ts;
|
2007-03-22 10:30:00 -07:00
|
|
|
JSPrincipals *principals;
|
|
|
|
jschar *collected_args, *cp;
|
2007-10-02 16:10:09 -07:00
|
|
|
void *mark;
|
2007-03-22 10:30:00 -07:00
|
|
|
size_t arg_length, args_length, old_args_length;
|
|
|
|
JSTokenType tt;
|
|
|
|
|
|
|
|
fp = cx->fp;
|
2008-03-28 15:27:36 -07:00
|
|
|
if (!(fp->flags & JSFRAME_CONSTRUCTING)) {
|
2008-04-02 00:46:12 -07:00
|
|
|
obj = js_NewObject(cx, &js_FunctionClass, NULL, NULL, 0);
|
2008-03-28 15:27:36 -07:00
|
|
|
if (!obj)
|
|
|
|
return JS_FALSE;
|
|
|
|
*rval = OBJECT_TO_JSVAL(obj);
|
2008-04-02 00:46:12 -07:00
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* The constructor is called before the private slot is initialized so
|
|
|
|
* we must use JS_GetPrivate, not GET_FUNCTION_PRIVATE here.
|
|
|
|
*/
|
|
|
|
if (JS_GetPrivate(cx, obj))
|
|
|
|
return JS_TRUE;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2007-11-19 09:15:45 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/*
|
|
|
|
* NB: (new Function) is not lexically closed by its caller, it's just an
|
|
|
|
* anonymous function in the top-level scope that its constructor inhabits.
|
|
|
|
* Thus 'var x = 42; f = new Function("return x"); print(f())' prints 42,
|
|
|
|
* and so would a call to f from another top-level's script or function.
|
|
|
|
*
|
|
|
|
* In older versions, before call objects, a new Function was adopted by
|
|
|
|
* its running context's globalObject, which might be different from the
|
|
|
|
* top-level reachable from scopeChain (in HTML frames, e.g.).
|
|
|
|
*/
|
|
|
|
parent = OBJ_GET_PARENT(cx, JSVAL_TO_OBJECT(argv[-2]));
|
|
|
|
|
2008-03-29 03:34:29 -07:00
|
|
|
fun = js_NewFunction(cx, obj, NULL, 0, JSFUN_LAMBDA | JSFUN_INTERPRETED,
|
|
|
|
parent, cx->runtime->atomState.anonymousAtom);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-03-28 15:27:36 -07:00
|
|
|
if (!fun)
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Function is static and not called directly by other functions in this
|
|
|
|
* file, therefore it is callable only as a native function by js_Invoke.
|
|
|
|
* Find the scripted caller, possibly skipping other native frames such as
|
|
|
|
* are built for Function.prototype.call or .apply activations that invoke
|
|
|
|
* Function indirectly from a script.
|
|
|
|
*/
|
2008-03-29 03:34:29 -07:00
|
|
|
JS_ASSERT(!fp->script && fp->fun && fp->fun->u.n.native == Function);
|
2007-03-22 10:30:00 -07:00
|
|
|
caller = JS_GetScriptedCaller(cx, fp);
|
|
|
|
if (caller) {
|
|
|
|
principals = JS_EvalFramePrincipals(cx, fp, caller);
|
2008-03-06 14:52:58 -08:00
|
|
|
filename = js_ComputeFilename(cx, caller, principals, &lineno);
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
|
|
|
filename = NULL;
|
|
|
|
lineno = 0;
|
|
|
|
principals = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Belt-and-braces: check that the caller has access to parent. */
|
|
|
|
if (!js_CheckPrincipalsAccess(cx, parent, principals,
|
|
|
|
CLASS_ATOM(cx, Function))) {
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
n = argc ? argc - 1 : 0;
|
|
|
|
if (n > 0) {
|
2007-10-02 16:10:09 -07:00
|
|
|
enum { OK, BAD, BAD_FORMAL } state;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/*
|
|
|
|
* Collect the function-argument arguments into one string, separated
|
|
|
|
* by commas, then make a tokenstream from that string, and scan it to
|
|
|
|
* get the arguments. We need to throw the full scanner at the
|
|
|
|
* problem, because the argument string can legitimately contain
|
|
|
|
* comments and linefeeds. XXX It might be better to concatenate
|
|
|
|
* everything up into a function definition and pass it to the
|
|
|
|
* compiler, but doing it this way is less of a delta from the old
|
|
|
|
* code. See ECMA 15.3.2.1.
|
|
|
|
*/
|
2007-10-02 16:10:09 -07:00
|
|
|
state = BAD_FORMAL;
|
2007-03-22 10:30:00 -07:00
|
|
|
args_length = 0;
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
/* Collect the lengths for all the function-argument arguments. */
|
|
|
|
arg = js_ValueToString(cx, argv[i]);
|
|
|
|
if (!arg)
|
|
|
|
return JS_FALSE;
|
|
|
|
argv[i] = STRING_TO_JSVAL(arg);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check for overflow. The < test works because the maximum
|
|
|
|
* JSString length fits in 2 fewer bits than size_t has.
|
|
|
|
*/
|
|
|
|
old_args_length = args_length;
|
|
|
|
args_length = old_args_length + JSSTRING_LENGTH(arg);
|
|
|
|
if (args_length < old_args_length) {
|
2008-03-12 16:07:47 -07:00
|
|
|
js_ReportAllocationOverflow(cx);
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add 1 for each joining comma and check for overflow (two ways). */
|
|
|
|
old_args_length = args_length;
|
|
|
|
args_length = old_args_length + n - 1;
|
|
|
|
if (args_length < old_args_length ||
|
|
|
|
args_length >= ~(size_t)0 / sizeof(jschar)) {
|
2008-03-12 16:07:47 -07:00
|
|
|
js_ReportAllocationOverflow(cx);
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Allocate a string to hold the concatenated arguments, including room
|
|
|
|
* for a terminating 0. Mark cx->tempPool for later release, to free
|
|
|
|
* collected_args and its tokenstream in one swoop.
|
|
|
|
*/
|
|
|
|
mark = JS_ARENA_MARK(&cx->tempPool);
|
|
|
|
JS_ARENA_ALLOCATE_CAST(cp, jschar *, &cx->tempPool,
|
|
|
|
(args_length+1) * sizeof(jschar));
|
|
|
|
if (!cp) {
|
2007-11-22 14:21:18 -08:00
|
|
|
js_ReportOutOfScriptQuota(cx);
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
collected_args = cp;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Concatenate the arguments into the new string, separated by commas.
|
|
|
|
*/
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
arg = JSVAL_TO_STRING(argv[i]);
|
|
|
|
arg_length = JSSTRING_LENGTH(arg);
|
|
|
|
(void) js_strncpy(cp, JSSTRING_CHARS(arg), arg_length);
|
|
|
|
cp += arg_length;
|
|
|
|
|
|
|
|
/* Add separating comma or terminating 0. */
|
|
|
|
*cp++ = (i + 1 < n) ? ',' : 0;
|
|
|
|
}
|
|
|
|
|
2007-10-02 16:10:09 -07:00
|
|
|
/* Initialize a tokenstream that reads from the given string. */
|
2007-11-29 06:49:42 -08:00
|
|
|
if (!js_InitTokenStream(cx, &ts, collected_args, args_length,
|
|
|
|
NULL, filename, lineno)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
JS_ARENA_RELEASE(&cx->tempPool, mark);
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The argument string may be empty or contain no tokens. */
|
2007-11-29 06:49:42 -08:00
|
|
|
tt = js_GetToken(cx, &ts);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (tt != TOK_EOF) {
|
|
|
|
for (;;) {
|
|
|
|
/*
|
|
|
|
* Check that it's a name. This also implicitly guards against
|
|
|
|
* TOK_ERROR, which was already reported.
|
|
|
|
*/
|
|
|
|
if (tt != TOK_NAME)
|
2007-10-02 16:10:09 -07:00
|
|
|
goto after_args;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/*
|
2007-11-29 06:49:42 -08:00
|
|
|
* Get the atom corresponding to the name from the token
|
|
|
|
* stream; we're assured at this point that it's a valid
|
|
|
|
* identifier.
|
2007-03-22 10:30:00 -07:00
|
|
|
*/
|
2007-11-29 06:49:42 -08:00
|
|
|
atom = CURRENT_TOKEN(&ts).t_atom;
|
2007-11-19 09:15:45 -08:00
|
|
|
|
|
|
|
/* Check for a duplicate parameter name. */
|
2008-03-28 15:27:36 -07:00
|
|
|
if (js_LookupLocal(cx, fun, atom, NULL) != JSLOCAL_NONE) {
|
2007-11-19 09:15:45 -08:00
|
|
|
const char *name;
|
|
|
|
|
|
|
|
name = js_AtomToPrintableString(cx, atom);
|
|
|
|
ok = name &&
|
2007-11-29 06:49:42 -08:00
|
|
|
js_ReportCompileErrorNumber(cx, &ts, NULL,
|
2007-11-19 09:15:45 -08:00
|
|
|
JSREPORT_WARNING |
|
|
|
|
JSREPORT_STRICT,
|
|
|
|
JSMSG_DUPLICATE_FORMAL,
|
|
|
|
name);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!ok)
|
2007-10-02 16:10:09 -07:00
|
|
|
goto after_args;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2008-03-28 15:27:36 -07:00
|
|
|
if (!js_AddLocal(cx, fun, atom, JSLOCAL_ARG))
|
2007-10-02 16:10:09 -07:00
|
|
|
goto after_args;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the next token. Stop on end of stream. Otherwise
|
|
|
|
* insist on a comma, get another name, and iterate.
|
|
|
|
*/
|
2007-11-29 06:49:42 -08:00
|
|
|
tt = js_GetToken(cx, &ts);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (tt == TOK_EOF)
|
|
|
|
break;
|
|
|
|
if (tt != TOK_COMMA)
|
2007-10-02 16:10:09 -07:00
|
|
|
goto after_args;
|
2007-11-29 06:49:42 -08:00
|
|
|
tt = js_GetToken(cx, &ts);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-02 16:10:09 -07:00
|
|
|
state = OK;
|
|
|
|
after_args:
|
2007-11-29 06:49:42 -08:00
|
|
|
if (state == BAD_FORMAL && !(ts.flags & TSF_ERROR)) {
|
2007-10-02 16:10:09 -07:00
|
|
|
/*
|
|
|
|
* Report "malformed formal parameter" iff no illegal char or
|
|
|
|
* similar scanner error was already reported.
|
|
|
|
*/
|
|
|
|
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
|
|
|
|
JSMSG_BAD_FORMAL);
|
|
|
|
}
|
2007-11-29 06:49:42 -08:00
|
|
|
js_CloseTokenStream(cx, &ts);
|
2007-03-22 10:30:00 -07:00
|
|
|
JS_ARENA_RELEASE(&cx->tempPool, mark);
|
2007-10-02 16:10:09 -07:00
|
|
|
if (state != OK)
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argc) {
|
|
|
|
str = js_ValueToString(cx, argv[argc-1]);
|
2007-07-28 09:57:30 -07:00
|
|
|
if (!str)
|
|
|
|
return JS_FALSE;
|
|
|
|
argv[argc-1] = STRING_TO_JSVAL(str);
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
2007-07-28 09:57:30 -07:00
|
|
|
str = cx->runtime->emptyString;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2008-03-28 15:27:36 -07:00
|
|
|
return js_CompileFunctionBody(cx, fun, principals,
|
2007-11-29 06:49:42 -08:00
|
|
|
JSSTRING_CHARS(str), JSSTRING_LENGTH(str),
|
|
|
|
filename, lineno);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
JSObject *
|
|
|
|
js_InitFunctionClass(JSContext *cx, JSObject *obj)
|
|
|
|
{
|
|
|
|
JSObject *proto;
|
2008-03-29 03:34:29 -07:00
|
|
|
JSFunction *fun;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
proto = JS_InitClass(cx, obj, NULL, &js_FunctionClass, Function, 1,
|
|
|
|
function_props, function_methods, NULL, NULL);
|
|
|
|
if (!proto)
|
|
|
|
return NULL;
|
2008-03-29 03:34:29 -07:00
|
|
|
fun = js_NewFunction(cx, proto, NULL, 0, JSFUN_INTERPRETED, obj, NULL);
|
2008-03-28 15:27:36 -07:00
|
|
|
if (!fun)
|
2007-03-22 10:30:00 -07:00
|
|
|
goto bad;
|
2008-08-21 03:47:33 -07:00
|
|
|
fun->u.i.script = js_NewScript(cx, 1, 0, 0, 0, 0, 0, 0);
|
2008-03-29 03:34:29 -07:00
|
|
|
if (!fun->u.i.script)
|
2007-03-22 10:30:00 -07:00
|
|
|
goto bad;
|
2008-03-29 03:34:29 -07:00
|
|
|
fun->u.i.script->code[0] = JSOP_STOP;
|
2008-02-07 15:18:45 -08:00
|
|
|
#ifdef CHECK_SCRIPT_OWNER
|
2008-03-29 03:34:29 -07:00
|
|
|
fun->u.i.script->owner = NULL;
|
2008-02-07 15:18:45 -08:00
|
|
|
#endif
|
2007-03-22 10:30:00 -07:00
|
|
|
return proto;
|
|
|
|
|
|
|
|
bad:
|
|
|
|
cx->weakRoots.newborn[GCX_OBJECT] = NULL;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject *
|
|
|
|
js_InitCallClass(JSContext *cx, JSObject *obj)
|
|
|
|
{
|
|
|
|
JSObject *proto;
|
|
|
|
|
|
|
|
proto = JS_InitClass(cx, obj, NULL, &js_CallClass, NULL, 0,
|
2007-06-17 22:10:19 -07:00
|
|
|
NULL, NULL, NULL, NULL);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!proto)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Null Call.prototype's proto slot so that Object.prototype.* does not
|
|
|
|
* pollute the scope of heavyweight functions.
|
|
|
|
*/
|
2008-07-20 05:53:21 -07:00
|
|
|
OBJ_CLEAR_PROTO(cx, proto);
|
2007-03-22 10:30:00 -07:00
|
|
|
return proto;
|
|
|
|
}
|
|
|
|
|
2008-03-29 03:34:29 -07:00
|
|
|
JSFunction *
|
|
|
|
js_NewFunction(JSContext *cx, JSObject *funobj, JSNative native, uintN nargs,
|
|
|
|
uintN flags, JSObject *parent, JSAtom *atom)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2008-03-29 03:34:29 -07:00
|
|
|
JSFunction *fun;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (funobj) {
|
2008-04-02 00:46:12 -07:00
|
|
|
JS_ASSERT(HAS_FUNCTION_CLASS(funobj));
|
2008-03-28 15:27:36 -07:00
|
|
|
OBJ_SET_PARENT(cx, funobj, parent);
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
2008-04-02 00:46:12 -07:00
|
|
|
funobj = js_NewObject(cx, &js_FunctionClass, NULL, parent, 0);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!funobj)
|
|
|
|
return NULL;
|
|
|
|
}
|
2008-06-25 02:43:02 -07:00
|
|
|
JS_ASSERT(JSVAL_IS_VOID(funobj->fslots[JSSLOT_PRIVATE]));
|
2008-04-02 00:46:12 -07:00
|
|
|
fun = (JSFunction *) funobj;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/* Initialize all function members. */
|
2008-03-29 03:34:29 -07:00
|
|
|
fun->nargs = nargs;
|
2008-10-08 15:08:33 -07:00
|
|
|
fun->flags = flags & (JSFUN_FLAGS_MASK | JSFUN_INTERPRETED | JSFUN_TRACEABLE);
|
2008-03-29 03:34:29 -07:00
|
|
|
if (flags & JSFUN_INTERPRETED) {
|
|
|
|
JS_ASSERT(!native);
|
|
|
|
JS_ASSERT(nargs == 0);
|
|
|
|
fun->u.i.nvars = 0;
|
2008-08-21 03:47:33 -07:00
|
|
|
fun->u.i.nupvars = 0;
|
2008-03-29 03:34:29 -07:00
|
|
|
fun->u.i.script = NULL;
|
2007-11-27 00:38:47 -08:00
|
|
|
#ifdef DEBUG
|
2008-03-29 03:34:29 -07:00
|
|
|
fun->u.i.names.taggedAtom = 0;
|
2007-11-27 00:38:47 -08:00
|
|
|
#endif
|
2008-03-29 03:34:29 -07:00
|
|
|
} else {
|
|
|
|
fun->u.n.extra = 0;
|
2008-08-08 09:02:50 -07:00
|
|
|
fun->u.n.spare = 0;
|
2008-10-08 15:08:33 -07:00
|
|
|
if (flags & JSFUN_TRACEABLE) {
|
|
|
|
#ifdef JS_TRACER
|
|
|
|
JSTraceableNative *trcinfo = (JSTraceableNative *) native;
|
|
|
|
fun->u.n.native = (JSNative) trcinfo->native;
|
|
|
|
FUN_TRCINFO(fun) = trcinfo;
|
|
|
|
#else
|
|
|
|
JS_ASSERT(0);
|
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
fun->u.n.native = native;
|
|
|
|
FUN_CLASP(fun) = NULL;
|
|
|
|
}
|
2008-03-29 03:34:29 -07:00
|
|
|
}
|
|
|
|
fun->atom = atom;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-04-02 00:46:12 -07:00
|
|
|
/* Set private to self to indicate non-cloned fully initialized function. */
|
|
|
|
FUN_OBJECT(fun)->fslots[JSSLOT_PRIVATE] = PRIVATE_TO_JSVAL(fun);
|
2008-03-29 03:34:29 -07:00
|
|
|
return fun;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2008-04-02 00:46:12 -07:00
|
|
|
JSObject *
|
|
|
|
js_CloneFunctionObject(JSContext *cx, JSFunction *fun, JSObject *parent)
|
2007-11-18 06:10:28 -08:00
|
|
|
{
|
2008-04-02 00:46:12 -07:00
|
|
|
JSObject *clone;
|
2007-11-18 06:10:28 -08:00
|
|
|
|
2007-08-04 00:00:43 -07:00
|
|
|
/*
|
2008-04-02 00:46:12 -07:00
|
|
|
* The cloned function object does not need the extra fields beyond
|
|
|
|
* JSObject as it points to fun via the private slot.
|
2007-08-04 00:00:43 -07:00
|
|
|
*/
|
2008-04-02 00:46:12 -07:00
|
|
|
clone = js_NewObject(cx, &js_FunctionClass, NULL, parent,
|
|
|
|
sizeof(JSObject));
|
|
|
|
if (!clone)
|
2008-03-29 03:34:29 -07:00
|
|
|
return NULL;
|
2008-04-02 00:46:12 -07:00
|
|
|
clone->fslots[JSSLOT_PRIVATE] = PRIVATE_TO_JSVAL(fun);
|
|
|
|
return clone;
|
2008-03-29 03:34:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
JSFunction *
|
2007-03-22 10:30:00 -07:00
|
|
|
js_DefineFunction(JSContext *cx, JSObject *obj, JSAtom *atom, JSNative native,
|
|
|
|
uintN nargs, uintN attrs)
|
|
|
|
{
|
2008-03-29 03:34:29 -07:00
|
|
|
JSFunction *fun;
|
2008-02-07 15:18:45 -08:00
|
|
|
JSPropertyOp gsop;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-03-29 03:34:29 -07:00
|
|
|
fun = js_NewFunction(cx, NULL, native, nargs, attrs, obj, atom);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!fun)
|
|
|
|
return NULL;
|
2008-02-07 15:18:45 -08:00
|
|
|
gsop = (attrs & JSFUN_STUB_GSOPS) ? JS_PropertyStub : NULL;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!OBJ_DEFINE_PROPERTY(cx, obj, ATOM_TO_JSID(atom),
|
2008-04-02 00:46:12 -07:00
|
|
|
OBJECT_TO_JSVAL(FUN_OBJECT(fun)),
|
2008-02-07 15:18:45 -08:00
|
|
|
gsop, gsop,
|
2007-03-22 10:30:00 -07:00
|
|
|
attrs & ~JSFUN_FLAGS_MASK, NULL)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return fun;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if (JSV2F_CONSTRUCT & JSV2F_SEARCH_STACK)
|
|
|
|
# error "JSINVOKE_CONSTRUCT and JSV2F_SEARCH_STACK are not disjoint!"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
JSFunction *
|
|
|
|
js_ValueToFunction(JSContext *cx, jsval *vp, uintN flags)
|
|
|
|
{
|
|
|
|
jsval v;
|
|
|
|
JSObject *obj;
|
|
|
|
|
|
|
|
v = *vp;
|
|
|
|
obj = NULL;
|
|
|
|
if (JSVAL_IS_OBJECT(v)) {
|
|
|
|
obj = JSVAL_TO_OBJECT(v);
|
2008-03-28 15:27:36 -07:00
|
|
|
if (obj && OBJ_GET_CLASS(cx, obj) != &js_FunctionClass) {
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!OBJ_DEFAULT_VALUE(cx, obj, JSTYPE_FUNCTION, &v))
|
|
|
|
return NULL;
|
|
|
|
obj = VALUE_IS_FUNCTION(cx, v) ? JSVAL_TO_OBJECT(v) : NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!obj) {
|
|
|
|
js_ReportIsNotFunction(cx, vp, flags);
|
|
|
|
return NULL;
|
|
|
|
}
|
2008-03-28 15:27:36 -07:00
|
|
|
return GET_FUNCTION_PRIVATE(cx, obj);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
JSObject *
|
|
|
|
js_ValueToFunctionObject(JSContext *cx, jsval *vp, uintN flags)
|
|
|
|
{
|
|
|
|
JSFunction *fun;
|
|
|
|
JSStackFrame *caller;
|
|
|
|
JSPrincipals *principals;
|
|
|
|
|
|
|
|
if (VALUE_IS_FUNCTION(cx, *vp))
|
|
|
|
return JSVAL_TO_OBJECT(*vp);
|
|
|
|
|
|
|
|
fun = js_ValueToFunction(cx, vp, flags);
|
|
|
|
if (!fun)
|
|
|
|
return NULL;
|
2008-04-02 00:46:12 -07:00
|
|
|
*vp = OBJECT_TO_JSVAL(FUN_OBJECT(fun));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
caller = JS_GetScriptedCaller(cx, cx->fp);
|
|
|
|
if (caller) {
|
2008-01-31 16:21:12 -08:00
|
|
|
principals = JS_StackFramePrincipals(cx, caller);
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
|
|
|
/* No scripted caller, don't allow access. */
|
|
|
|
principals = NULL;
|
|
|
|
}
|
|
|
|
|
2008-04-02 00:46:12 -07:00
|
|
|
if (!js_CheckPrincipalsAccess(cx, FUN_OBJECT(fun), principals,
|
2008-03-29 03:34:29 -07:00
|
|
|
fun->atom
|
|
|
|
? fun->atom
|
2007-03-22 10:30:00 -07:00
|
|
|
: cx->runtime->atomState.anonymousAtom)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2008-04-02 00:46:12 -07:00
|
|
|
return FUN_OBJECT(fun);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
JSObject *
|
|
|
|
js_ValueToCallableObject(JSContext *cx, jsval *vp, uintN flags)
|
|
|
|
{
|
|
|
|
JSObject *callable;
|
|
|
|
|
|
|
|
callable = JSVAL_IS_PRIMITIVE(*vp) ? NULL : JSVAL_TO_OBJECT(*vp);
|
|
|
|
if (callable &&
|
|
|
|
((callable->map->ops == &js_ObjectOps)
|
|
|
|
? OBJ_GET_CLASS(cx, callable)->call
|
|
|
|
: callable->map->ops->call)) {
|
|
|
|
*vp = OBJECT_TO_JSVAL(callable);
|
|
|
|
} else {
|
|
|
|
callable = js_ValueToFunctionObject(cx, vp, flags);
|
|
|
|
}
|
|
|
|
return callable;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
js_ReportIsNotFunction(JSContext *cx, jsval *vp, uintN flags)
|
|
|
|
{
|
|
|
|
JSStackFrame *fp;
|
|
|
|
uintN error;
|
|
|
|
const char *name, *source;
|
2008-04-11 07:50:16 -07:00
|
|
|
JSTempValueRooter tvr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-03-17 01:58:28 -07:00
|
|
|
for (fp = cx->fp; fp && !fp->regs; fp = fp->down)
|
2007-03-22 10:30:00 -07:00
|
|
|
continue;
|
2008-04-11 07:50:16 -07:00
|
|
|
name = source = NULL;
|
|
|
|
JS_PUSH_TEMP_ROOT_STRING(cx, NULL, &tvr);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (flags & JSV2F_ITERATOR) {
|
|
|
|
error = JSMSG_BAD_ITERATOR;
|
|
|
|
name = js_iterator_str;
|
2008-04-11 07:50:16 -07:00
|
|
|
tvr.u.string = js_ValueToSource(cx, *vp);
|
|
|
|
if (!tvr.u.string)
|
|
|
|
goto out;
|
|
|
|
tvr.u.string = js_QuoteString(cx, tvr.u.string, 0);
|
|
|
|
if (!tvr.u.string)
|
|
|
|
goto out;
|
|
|
|
source = js_GetStringBytes(cx, tvr.u.string);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!source)
|
2008-04-11 07:50:16 -07:00
|
|
|
goto out;
|
2007-03-22 10:30:00 -07:00
|
|
|
} else if (flags & JSV2F_CONSTRUCT) {
|
|
|
|
error = JSMSG_NOT_CONSTRUCTOR;
|
|
|
|
} else {
|
|
|
|
error = JSMSG_NOT_FUNCTION;
|
|
|
|
}
|
|
|
|
|
|
|
|
js_ReportValueError3(cx, error,
|
2008-03-17 01:58:28 -07:00
|
|
|
(fp && fp->regs &&
|
2008-07-20 13:13:17 -07:00
|
|
|
StackBase(fp) <= vp && vp < fp->regs->sp)
|
2008-03-17 01:58:28 -07:00
|
|
|
? vp - fp->regs->sp
|
2007-03-22 10:30:00 -07:00
|
|
|
: (flags & JSV2F_SEARCH_STACK)
|
|
|
|
? JSDVG_SEARCH_STACK
|
|
|
|
: JSDVG_IGNORE_STACK,
|
|
|
|
*vp, NULL,
|
|
|
|
name, source);
|
2008-04-11 07:50:16 -07:00
|
|
|
|
|
|
|
out:
|
|
|
|
JS_POP_TEMP_ROOT(cx, &tvr);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2007-11-19 09:15:45 -08:00
|
|
|
|
2007-11-27 00:38:47 -08:00
|
|
|
/*
|
|
|
|
* When a function has between 2 and MAX_ARRAY_LOCALS arguments and variables,
|
|
|
|
* their name are stored as the JSLocalNames.array.
|
|
|
|
*/
|
|
|
|
#define MAX_ARRAY_LOCALS 8
|
|
|
|
|
|
|
|
JS_STATIC_ASSERT(2 <= MAX_ARRAY_LOCALS);
|
|
|
|
JS_STATIC_ASSERT(MAX_ARRAY_LOCALS < JS_BITMASK(16));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We use the lowest bit of the string atom to distinguish const from var
|
|
|
|
* name when there is only single name or when names are stored as an array.
|
|
|
|
*/
|
|
|
|
JS_STATIC_ASSERT((JSVAL_STRING & 1) == 0);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* When we use a hash table to store the local names, we use a singly linked
|
|
|
|
* list to record the indexes of duplicated parameter names to preserve the
|
|
|
|
* duplicates for the decompiler.
|
|
|
|
*/
|
|
|
|
typedef struct JSNameIndexPair JSNameIndexPair;
|
|
|
|
|
|
|
|
struct JSNameIndexPair {
|
|
|
|
JSAtom *name;
|
|
|
|
uint16 index;
|
|
|
|
JSNameIndexPair *link;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct JSLocalNameMap {
|
|
|
|
JSDHashTable names;
|
|
|
|
JSNameIndexPair *lastdup;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct JSLocalNameHashEntry {
|
|
|
|
JSDHashEntryHdr hdr;
|
|
|
|
JSAtom *name;
|
|
|
|
uint16 index;
|
|
|
|
uint8 localKind;
|
|
|
|
} JSLocalNameHashEntry;
|
|
|
|
|
|
|
|
static void
|
|
|
|
FreeLocalNameHash(JSContext *cx, JSLocalNameMap *map)
|
|
|
|
{
|
|
|
|
JSNameIndexPair *dup, *next;
|
|
|
|
|
|
|
|
for (dup = map->lastdup; dup; dup = next) {
|
|
|
|
next = dup->link;
|
|
|
|
JS_free(cx, dup);
|
|
|
|
}
|
|
|
|
JS_DHashTableFinish(&map->names);
|
|
|
|
JS_free(cx, map);
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
|
|
|
HashLocalName(JSContext *cx, JSLocalNameMap *map, JSAtom *name,
|
|
|
|
JSLocalKind localKind, uintN index)
|
|
|
|
{
|
|
|
|
JSLocalNameHashEntry *entry;
|
|
|
|
JSNameIndexPair *dup;
|
|
|
|
|
|
|
|
JS_ASSERT(index <= JS_BITMASK(16));
|
|
|
|
#if JS_HAS_DESTRUCTURING
|
|
|
|
if (!name) {
|
|
|
|
/* A destructuring pattern does not need a hash entry. */
|
|
|
|
JS_ASSERT(localKind == JSLOCAL_ARG);
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
JS_ASSERT(ATOM_IS_STRING(name));
|
|
|
|
entry = (JSLocalNameHashEntry *)
|
|
|
|
JS_DHashTableOperate(&map->names, name, JS_DHASH_ADD);
|
|
|
|
if (!entry) {
|
|
|
|
JS_ReportOutOfMemory(cx);
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
if (entry->name) {
|
|
|
|
JS_ASSERT(entry->name == name);
|
|
|
|
JS_ASSERT(entry->localKind == JSLOCAL_ARG);
|
|
|
|
dup = (JSNameIndexPair *) JS_malloc(cx, sizeof *dup);
|
|
|
|
if (!dup)
|
|
|
|
return JS_FALSE;
|
|
|
|
dup->name = entry->name;
|
|
|
|
dup->index = entry->index;
|
|
|
|
dup->link = map->lastdup;
|
|
|
|
map->lastdup = dup;
|
|
|
|
}
|
|
|
|
entry->name = name;
|
|
|
|
entry->index = (uint16) index;
|
|
|
|
entry->localKind = (uint8) localKind;
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
2007-11-19 09:15:45 -08:00
|
|
|
JSBool
|
2008-03-29 03:34:29 -07:00
|
|
|
js_AddLocal(JSContext *cx, JSFunction *fun, JSAtom *atom, JSLocalKind kind)
|
2007-11-19 09:15:45 -08:00
|
|
|
{
|
2007-11-27 00:38:47 -08:00
|
|
|
jsuword taggedAtom;
|
2007-11-19 09:15:45 -08:00
|
|
|
uint16 *indexp;
|
2007-11-27 00:38:47 -08:00
|
|
|
uintN n, i;
|
|
|
|
jsuword *array;
|
|
|
|
JSLocalNameMap *map;
|
2007-11-19 09:15:45 -08:00
|
|
|
|
2008-03-29 03:34:29 -07:00
|
|
|
JS_ASSERT(FUN_INTERPRETED(fun));
|
|
|
|
JS_ASSERT(!fun->u.i.script);
|
2007-11-27 00:38:47 -08:00
|
|
|
JS_ASSERT(((jsuword) atom & 1) == 0);
|
|
|
|
taggedAtom = (jsuword) atom;
|
2007-11-19 09:15:45 -08:00
|
|
|
if (kind == JSLOCAL_ARG) {
|
|
|
|
indexp = &fun->nargs;
|
2008-08-21 03:47:33 -07:00
|
|
|
} else if (kind == JSLOCAL_UPVAR) {
|
|
|
|
indexp = &fun->u.i.nupvars;
|
2007-11-19 09:15:45 -08:00
|
|
|
} else {
|
2008-03-29 03:34:29 -07:00
|
|
|
indexp = &fun->u.i.nvars;
|
2007-11-27 00:38:47 -08:00
|
|
|
if (kind == JSLOCAL_CONST)
|
|
|
|
taggedAtom |= 1;
|
|
|
|
else
|
|
|
|
JS_ASSERT(kind == JSLOCAL_VAR);
|
2007-11-19 09:15:45 -08:00
|
|
|
}
|
2008-02-08 16:01:45 -08:00
|
|
|
n = JS_GET_LOCAL_NAME_COUNT(fun);
|
2007-11-27 00:38:47 -08:00
|
|
|
if (n == 0) {
|
2008-03-29 03:34:29 -07:00
|
|
|
JS_ASSERT(fun->u.i.names.taggedAtom == 0);
|
|
|
|
fun->u.i.names.taggedAtom = taggedAtom;
|
2007-11-27 00:38:47 -08:00
|
|
|
} else if (n < MAX_ARRAY_LOCALS) {
|
|
|
|
if (n > 1) {
|
2008-03-29 03:34:29 -07:00
|
|
|
array = fun->u.i.names.array;
|
2007-11-27 00:38:47 -08:00
|
|
|
} else {
|
|
|
|
array = (jsuword *) JS_malloc(cx, MAX_ARRAY_LOCALS * sizeof *array);
|
|
|
|
if (!array)
|
|
|
|
return JS_FALSE;
|
2008-03-29 03:34:29 -07:00
|
|
|
array[0] = fun->u.i.names.taggedAtom;
|
|
|
|
fun->u.i.names.array = array;
|
2007-11-27 00:38:47 -08:00
|
|
|
}
|
|
|
|
if (kind == JSLOCAL_ARG) {
|
|
|
|
/*
|
|
|
|
* A destructuring argument pattern adds variables, not arguments,
|
|
|
|
* so for the following arguments nvars != 0.
|
|
|
|
*/
|
|
|
|
#if JS_HAS_DESTRUCTURING
|
2008-03-29 03:34:29 -07:00
|
|
|
if (fun->u.i.nvars != 0) {
|
2007-11-27 00:38:47 -08:00
|
|
|
memmove(array + fun->nargs + 1, array + fun->nargs,
|
2008-03-29 03:34:29 -07:00
|
|
|
fun->u.i.nvars * sizeof *array);
|
2007-11-27 00:38:47 -08:00
|
|
|
}
|
|
|
|
#else
|
2008-03-29 03:34:29 -07:00
|
|
|
JS_ASSERT(fun->u.i.nvars == 0);
|
2007-11-27 00:38:47 -08:00
|
|
|
#endif
|
|
|
|
array[fun->nargs] = taggedAtom;
|
|
|
|
} else {
|
|
|
|
array[n] = taggedAtom;
|
|
|
|
}
|
|
|
|
} else if (n == MAX_ARRAY_LOCALS) {
|
2008-03-29 03:34:29 -07:00
|
|
|
array = fun->u.i.names.array;
|
2007-11-27 00:38:47 -08:00
|
|
|
map = (JSLocalNameMap *) JS_malloc(cx, sizeof *map);
|
|
|
|
if (!map)
|
|
|
|
return JS_FALSE;
|
|
|
|
if (!JS_DHashTableInit(&map->names, JS_DHashGetStubOps(),
|
|
|
|
NULL, sizeof(JSLocalNameHashEntry),
|
|
|
|
JS_DHASH_DEFAULT_CAPACITY(MAX_ARRAY_LOCALS
|
|
|
|
* 2))) {
|
|
|
|
JS_ReportOutOfMemory(cx);
|
|
|
|
JS_free(cx, map);
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
2007-11-19 09:15:45 -08:00
|
|
|
|
2007-11-27 00:38:47 -08:00
|
|
|
map->lastdup = NULL;
|
|
|
|
for (i = 0; i != MAX_ARRAY_LOCALS; ++i) {
|
|
|
|
taggedAtom = array[i];
|
|
|
|
if (!HashLocalName(cx, map, (JSAtom *) (taggedAtom & ~1),
|
|
|
|
(i < fun->nargs)
|
|
|
|
? JSLOCAL_ARG
|
|
|
|
: (taggedAtom & 1) ? JSLOCAL_CONST : JSLOCAL_VAR,
|
|
|
|
(i < fun->nargs) ? i : i - fun->nargs)) {
|
|
|
|
FreeLocalNameHash(cx, map);
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!HashLocalName(cx, map, atom, kind, *indexp)) {
|
|
|
|
FreeLocalNameHash(cx, map);
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
2007-11-19 09:15:45 -08:00
|
|
|
|
2007-11-27 00:38:47 -08:00
|
|
|
/*
|
|
|
|
* At this point the entry is added and we cannot fail. It is time
|
|
|
|
* to replace fun->u.i.names with the built map.
|
|
|
|
*/
|
2008-03-29 03:34:29 -07:00
|
|
|
fun->u.i.names.map = map;
|
2007-11-27 00:38:47 -08:00
|
|
|
JS_free(cx, array);
|
|
|
|
} else {
|
|
|
|
if (*indexp == JS_BITMASK(16)) {
|
|
|
|
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
|
|
|
|
(kind == JSLOCAL_ARG)
|
|
|
|
? JSMSG_TOO_MANY_FUN_ARGS
|
2008-07-20 13:13:17 -07:00
|
|
|
: JSMSG_TOO_MANY_LOCALS);
|
2007-11-27 00:38:47 -08:00
|
|
|
return JS_FALSE;
|
|
|
|
}
|
2008-03-29 03:34:29 -07:00
|
|
|
if (!HashLocalName(cx, fun->u.i.names.map, atom, kind, *indexp))
|
2007-11-27 00:38:47 -08:00
|
|
|
return JS_FALSE;
|
2007-11-19 09:15:45 -08:00
|
|
|
}
|
|
|
|
|
2007-11-27 00:38:47 -08:00
|
|
|
/* Update the argument or variable counter. */
|
2007-11-19 09:15:45 -08:00
|
|
|
++*indexp;
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSLocalKind
|
2008-03-29 03:34:29 -07:00
|
|
|
js_LookupLocal(JSContext *cx, JSFunction *fun, JSAtom *atom, uintN *indexp)
|
2007-11-19 09:15:45 -08:00
|
|
|
{
|
2008-08-21 03:47:33 -07:00
|
|
|
uintN n, i, upvar_base;
|
2007-11-27 00:38:47 -08:00
|
|
|
jsuword *array;
|
|
|
|
JSLocalNameHashEntry *entry;
|
2007-11-19 09:15:45 -08:00
|
|
|
|
2008-03-29 03:34:29 -07:00
|
|
|
JS_ASSERT(FUN_INTERPRETED(fun));
|
2008-02-08 16:01:45 -08:00
|
|
|
n = JS_GET_LOCAL_NAME_COUNT(fun);
|
2007-11-27 00:38:47 -08:00
|
|
|
if (n == 0)
|
|
|
|
return JSLOCAL_NONE;
|
|
|
|
if (n <= MAX_ARRAY_LOCALS) {
|
2008-03-29 03:34:29 -07:00
|
|
|
array = (n == 1) ? &fun->u.i.names.taggedAtom : fun->u.i.names.array;
|
2007-11-27 00:38:47 -08:00
|
|
|
|
|
|
|
/* Search from the tail to pick up the last duplicated name. */
|
|
|
|
i = n;
|
2008-08-21 03:47:33 -07:00
|
|
|
upvar_base = JS_UPVAR_LOCAL_NAME_START(fun);
|
2007-11-27 00:38:47 -08:00
|
|
|
do {
|
|
|
|
--i;
|
2008-02-08 16:01:45 -08:00
|
|
|
if (atom == JS_LOCAL_NAME_TO_ATOM(array[i])) {
|
2007-11-27 00:38:47 -08:00
|
|
|
if (i < fun->nargs) {
|
|
|
|
if (indexp)
|
|
|
|
*indexp = i;
|
|
|
|
return JSLOCAL_ARG;
|
|
|
|
}
|
2008-08-21 03:47:33 -07:00
|
|
|
if (i >= upvar_base) {
|
|
|
|
if (indexp)
|
|
|
|
*indexp = i - upvar_base;
|
|
|
|
return JSLOCAL_UPVAR;
|
|
|
|
}
|
2007-11-27 00:38:47 -08:00
|
|
|
if (indexp)
|
|
|
|
*indexp = i - fun->nargs;
|
2008-02-08 16:01:45 -08:00
|
|
|
return JS_LOCAL_NAME_IS_CONST(array[i])
|
|
|
|
? JSLOCAL_CONST
|
|
|
|
: JSLOCAL_VAR;
|
2007-11-19 09:15:45 -08:00
|
|
|
}
|
2007-11-27 00:38:47 -08:00
|
|
|
} while (i != 0);
|
|
|
|
} else {
|
|
|
|
entry = (JSLocalNameHashEntry *)
|
2008-03-29 03:34:29 -07:00
|
|
|
JS_DHashTableOperate(&fun->u.i.names.map->names, atom,
|
2007-11-27 00:38:47 -08:00
|
|
|
JS_DHASH_LOOKUP);
|
|
|
|
if (JS_DHASH_ENTRY_IS_BUSY(&entry->hdr)) {
|
|
|
|
JS_ASSERT(entry->localKind != JSLOCAL_NONE);
|
2007-11-19 09:15:45 -08:00
|
|
|
if (indexp)
|
2007-11-27 00:38:47 -08:00
|
|
|
*indexp = entry->index;
|
|
|
|
return (JSLocalKind) entry->localKind;
|
2007-11-19 09:15:45 -08:00
|
|
|
}
|
|
|
|
}
|
2007-11-27 00:38:47 -08:00
|
|
|
return JSLOCAL_NONE;
|
|
|
|
}
|
|
|
|
|
2008-02-08 16:01:45 -08:00
|
|
|
typedef struct JSLocalNameEnumeratorArgs {
|
2008-03-29 03:34:29 -07:00
|
|
|
JSFunction *fun;
|
|
|
|
jsuword *names;
|
2007-11-27 00:38:47 -08:00
|
|
|
#ifdef DEBUG
|
2008-03-29 03:34:29 -07:00
|
|
|
uintN nCopiedArgs;
|
|
|
|
uintN nCopiedVars;
|
2007-11-27 00:38:47 -08:00
|
|
|
#endif
|
2008-02-08 16:01:45 -08:00
|
|
|
} JSLocalNameEnumeratorArgs;
|
2007-11-27 00:38:47 -08:00
|
|
|
|
2008-09-06 15:21:43 -07:00
|
|
|
static JSDHashOperator
|
2007-11-27 00:38:47 -08:00
|
|
|
get_local_names_enumerator(JSDHashTable *table, JSDHashEntryHdr *hdr,
|
|
|
|
uint32 number, void *arg)
|
|
|
|
{
|
|
|
|
JSLocalNameHashEntry *entry;
|
2008-02-08 16:01:45 -08:00
|
|
|
JSLocalNameEnumeratorArgs *args;
|
2007-11-27 00:38:47 -08:00
|
|
|
uint i;
|
2008-02-08 16:01:45 -08:00
|
|
|
jsuword constFlag;
|
2007-11-27 00:38:47 -08:00
|
|
|
|
|
|
|
entry = (JSLocalNameHashEntry *) hdr;
|
2008-02-08 16:01:45 -08:00
|
|
|
args = (JSLocalNameEnumeratorArgs *) arg;
|
2007-11-27 00:38:47 -08:00
|
|
|
JS_ASSERT(entry->name);
|
|
|
|
if (entry->localKind == JSLOCAL_ARG) {
|
|
|
|
JS_ASSERT(entry->index < args->fun->nargs);
|
|
|
|
JS_ASSERT(args->nCopiedArgs++ < args->fun->nargs);
|
|
|
|
i = entry->index;
|
2008-02-08 16:01:45 -08:00
|
|
|
constFlag = 0;
|
2007-11-27 00:38:47 -08:00
|
|
|
} else {
|
|
|
|
JS_ASSERT(entry->localKind == JSLOCAL_VAR ||
|
|
|
|
entry->localKind == JSLOCAL_CONST);
|
2008-03-29 03:34:29 -07:00
|
|
|
JS_ASSERT(entry->index < args->fun->u.i.nvars);
|
|
|
|
JS_ASSERT(args->nCopiedVars++ < args->fun->u.i.nvars);
|
2007-11-27 00:38:47 -08:00
|
|
|
i = args->fun->nargs + entry->index;
|
2008-02-08 16:01:45 -08:00
|
|
|
constFlag = (entry->localKind == JSLOCAL_CONST);
|
2007-11-27 00:38:47 -08:00
|
|
|
}
|
2008-02-08 16:01:45 -08:00
|
|
|
args->names[i] = (jsuword) entry->name | constFlag;
|
2007-11-27 00:38:47 -08:00
|
|
|
return JS_DHASH_NEXT;
|
2007-11-19 09:15:45 -08:00
|
|
|
}
|
|
|
|
|
2008-02-08 16:01:45 -08:00
|
|
|
jsuword *
|
2008-03-29 03:34:29 -07:00
|
|
|
js_GetLocalNameArray(JSContext *cx, JSFunction *fun, JSArenaPool *pool)
|
2007-11-19 09:15:45 -08:00
|
|
|
{
|
2008-02-08 16:01:45 -08:00
|
|
|
uintN n;
|
|
|
|
jsuword *names;
|
2007-11-27 00:38:47 -08:00
|
|
|
JSLocalNameMap *map;
|
2008-02-08 16:01:45 -08:00
|
|
|
JSLocalNameEnumeratorArgs args;
|
2007-11-27 00:38:47 -08:00
|
|
|
JSNameIndexPair *dup;
|
2007-11-19 09:15:45 -08:00
|
|
|
|
2008-03-29 03:34:29 -07:00
|
|
|
JS_ASSERT(FUN_INTERPRETED(fun));
|
2008-02-08 16:01:45 -08:00
|
|
|
n = JS_GET_LOCAL_NAME_COUNT(fun);
|
2007-11-19 09:15:45 -08:00
|
|
|
JS_ASSERT(n != 0);
|
2008-02-08 16:01:45 -08:00
|
|
|
|
|
|
|
if (n <= MAX_ARRAY_LOCALS)
|
2008-03-29 03:34:29 -07:00
|
|
|
return (n == 1) ? &fun->u.i.names.taggedAtom : fun->u.i.names.array;
|
2008-02-08 16:01:45 -08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* No need to check for overflow of the allocation size as we are making a
|
|
|
|
* copy of already allocated data. As such it must fit size_t.
|
|
|
|
*/
|
|
|
|
JS_ARENA_ALLOCATE_CAST(names, jsuword *, pool, (size_t) n * sizeof *names);
|
2008-01-20 11:11:59 -08:00
|
|
|
if (!names) {
|
|
|
|
js_ReportOutOfScriptQuota(cx);
|
2007-11-19 09:15:45 -08:00
|
|
|
return NULL;
|
2008-01-20 11:11:59 -08:00
|
|
|
}
|
2007-11-19 09:15:45 -08:00
|
|
|
|
|
|
|
#if JS_HAS_DESTRUCTURING
|
|
|
|
/* Some parameter names can be NULL due to destructuring patterns. */
|
|
|
|
memset(names, 0, fun->nargs * sizeof *names);
|
|
|
|
#endif
|
2008-03-29 03:34:29 -07:00
|
|
|
map = fun->u.i.names.map;
|
2008-02-08 16:01:45 -08:00
|
|
|
args.fun = fun;
|
|
|
|
args.names = names;
|
2007-11-27 00:38:47 -08:00
|
|
|
#ifdef DEBUG
|
2008-02-08 16:01:45 -08:00
|
|
|
args.nCopiedArgs = 0;
|
|
|
|
args.nCopiedVars = 0;
|
2007-11-27 00:38:47 -08:00
|
|
|
#endif
|
2008-02-08 16:01:45 -08:00
|
|
|
JS_DHashTableEnumerate(&map->names, get_local_names_enumerator, &args);
|
|
|
|
for (dup = map->lastdup; dup; dup = dup->link) {
|
|
|
|
JS_ASSERT(dup->index < fun->nargs);
|
|
|
|
JS_ASSERT(args.nCopiedArgs++ < fun->nargs);
|
|
|
|
names[dup->index] = (jsuword) dup->name;
|
|
|
|
}
|
2007-11-19 09:15:45 -08:00
|
|
|
#if !JS_HAS_DESTRUCTURING
|
2008-02-08 16:01:45 -08:00
|
|
|
JS_ASSERT(args.nCopiedArgs == fun->nargs);
|
2007-11-19 09:15:45 -08:00
|
|
|
#endif
|
2008-03-29 03:34:29 -07:00
|
|
|
JS_ASSERT(args.nCopiedVars == fun->u.i.nvars);
|
2007-11-27 00:38:47 -08:00
|
|
|
|
2007-11-19 09:15:45 -08:00
|
|
|
return names;
|
|
|
|
}
|
|
|
|
|
2008-09-06 15:21:43 -07:00
|
|
|
static JSDHashOperator
|
2007-11-27 00:38:47 -08:00
|
|
|
trace_local_names_enumerator(JSDHashTable *table, JSDHashEntryHdr *hdr,
|
|
|
|
uint32 number, void *arg)
|
|
|
|
{
|
|
|
|
JSLocalNameHashEntry *entry;
|
|
|
|
JSTracer *trc;
|
|
|
|
|
|
|
|
entry = (JSLocalNameHashEntry *) hdr;
|
|
|
|
JS_ASSERT(entry->name);
|
|
|
|
trc = (JSTracer *) arg;
|
|
|
|
JS_SET_TRACING_INDEX(trc,
|
|
|
|
entry->localKind == JSLOCAL_ARG ? "arg" : "var",
|
|
|
|
entry->index);
|
|
|
|
JS_CallTracer(trc, ATOM_TO_STRING(entry->name), JSTRACE_STRING);
|
|
|
|
return JS_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2008-03-29 03:34:29 -07:00
|
|
|
TraceLocalNames(JSTracer *trc, JSFunction *fun)
|
2007-11-27 00:38:47 -08:00
|
|
|
{
|
|
|
|
uintN n, i;
|
|
|
|
JSAtom *atom;
|
|
|
|
jsuword *array;
|
|
|
|
|
2008-03-29 03:34:29 -07:00
|
|
|
JS_ASSERT(FUN_INTERPRETED(fun));
|
2008-02-08 16:01:45 -08:00
|
|
|
n = JS_GET_LOCAL_NAME_COUNT(fun);
|
2007-11-27 00:38:47 -08:00
|
|
|
if (n == 0)
|
|
|
|
return;
|
|
|
|
if (n <= MAX_ARRAY_LOCALS) {
|
2008-03-29 03:34:29 -07:00
|
|
|
array = (n == 1) ? &fun->u.i.names.taggedAtom : fun->u.i.names.array;
|
2007-11-27 00:38:47 -08:00
|
|
|
i = n;
|
|
|
|
do {
|
|
|
|
--i;
|
|
|
|
atom = (JSAtom *) (array[i] & ~1);
|
|
|
|
if (atom) {
|
|
|
|
JS_SET_TRACING_INDEX(trc,
|
|
|
|
i < fun->nargs ? "arg" : "var",
|
|
|
|
i < fun->nargs ? i : i - fun->nargs);
|
|
|
|
JS_CallTracer(trc, ATOM_TO_STRING(atom), JSTRACE_STRING);
|
|
|
|
}
|
|
|
|
} while (i != 0);
|
|
|
|
} else {
|
2008-03-29 03:34:29 -07:00
|
|
|
JS_DHashTableEnumerate(&fun->u.i.names.map->names,
|
2007-11-27 00:38:47 -08:00
|
|
|
trace_local_names_enumerator, trc);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* No need to trace the list of duplicates in map->lastdup as the
|
|
|
|
* names there are traced when enumerating the hash table.
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-03-29 03:34:29 -07:00
|
|
|
DestroyLocalNames(JSContext *cx, JSFunction *fun)
|
2007-11-27 00:38:47 -08:00
|
|
|
{
|
|
|
|
uintN n;
|
|
|
|
|
2008-03-29 03:34:29 -07:00
|
|
|
n = fun->nargs + fun->u.i.nvars;
|
2007-11-27 00:38:47 -08:00
|
|
|
if (n <= 1)
|
|
|
|
return;
|
|
|
|
if (n <= MAX_ARRAY_LOCALS)
|
2008-03-29 03:34:29 -07:00
|
|
|
JS_free(cx, fun->u.i.names.array);
|
2007-11-27 00:38:47 -08:00
|
|
|
else
|
2008-03-29 03:34:29 -07:00
|
|
|
FreeLocalNameHash(cx, fun->u.i.names.map);
|
2007-11-27 00:38:47 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-03-29 03:34:29 -07:00
|
|
|
js_FreezeLocalNames(JSContext *cx, JSFunction *fun)
|
2007-11-27 00:38:47 -08:00
|
|
|
{
|
|
|
|
uintN n;
|
|
|
|
jsuword *array;
|
|
|
|
|
2008-03-29 03:34:29 -07:00
|
|
|
JS_ASSERT(FUN_INTERPRETED(fun));
|
|
|
|
JS_ASSERT(!fun->u.i.script);
|
2008-08-21 03:47:33 -07:00
|
|
|
n = fun->nargs + fun->u.i.nvars + fun->u.i.nupvars;
|
2007-11-27 00:38:47 -08:00
|
|
|
if (2 <= n && n < MAX_ARRAY_LOCALS) {
|
|
|
|
/* Shrink over-allocated array ignoring realloc failures. */
|
2008-03-29 03:34:29 -07:00
|
|
|
array = (jsuword *) JS_realloc(cx, fun->u.i.names.array,
|
|
|
|
n * sizeof *array);
|
2007-11-27 00:38:47 -08:00
|
|
|
if (array)
|
2008-03-29 03:34:29 -07:00
|
|
|
fun->u.i.names.array = array;
|
2007-11-27 00:38:47 -08:00
|
|
|
}
|
|
|
|
}
|