2009-06-10 18:29:44 -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 <string.h>
|
|
|
|
#include "jstypes.h"
|
2009-03-18 11:38:16 -07:00
|
|
|
#include "jsstdint.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "jsbit.h"
|
2010-10-01 16:46:54 -07:00
|
|
|
#include "jsutil.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "jsapi.h"
|
|
|
|
#include "jsarray.h"
|
|
|
|
#include "jsatom.h"
|
2009-08-14 11:43:16 -07:00
|
|
|
#include "jsbool.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"
|
2008-11-21 12:03:58 -08:00
|
|
|
#include "jsemit.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "jsfun.h"
|
|
|
|
#include "jsgc.h"
|
2011-04-15 16:56:08 -07:00
|
|
|
#include "jsgcmark.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "jsinterp.h"
|
|
|
|
#include "jslock.h"
|
|
|
|
#include "jsnum.h"
|
|
|
|
#include "jsobj.h"
|
|
|
|
#include "jsopcode.h"
|
|
|
|
#include "jsparse.h"
|
2010-08-29 11:57:08 -07:00
|
|
|
#include "jspropertytree.h"
|
2010-06-23 14:35:10 -07:00
|
|
|
#include "jsproxy.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#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"
|
2009-09-09 11:46:32 -07:00
|
|
|
#include "jstracer.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
|
|
|
|
|
2010-08-27 15:54:30 -07:00
|
|
|
#ifdef JS_METHODJIT
|
|
|
|
#include "methodjit/MethodJIT.h"
|
|
|
|
#endif
|
|
|
|
|
2009-07-13 14:55:04 -07:00
|
|
|
#include "jsatominlines.h"
|
2010-08-11 23:26:59 -07:00
|
|
|
#include "jsfuninlines.h"
|
2010-10-29 08:05:55 -07:00
|
|
|
#include "jsinferinlines.h"
|
2010-03-30 14:42:48 -07:00
|
|
|
#include "jsobjinlines.h"
|
2010-11-16 15:34:24 -08:00
|
|
|
#include "jsscriptinlines.h"
|
2009-07-13 14:55:04 -07:00
|
|
|
|
2011-04-25 19:40:00 -07:00
|
|
|
#include "vm/ArgumentsObject-inl.h"
|
2011-04-13 09:27:37 -07:00
|
|
|
#include "vm/Stack-inl.h"
|
|
|
|
|
2010-01-22 14:49:18 -08:00
|
|
|
using namespace js;
|
2010-09-24 10:54:39 -07:00
|
|
|
using namespace js::gc;
|
2010-10-29 08:05:55 -07:00
|
|
|
using namespace js::types;
|
2010-01-22 14:49:18 -08:00
|
|
|
|
2010-08-02 23:52:12 -07:00
|
|
|
inline JSObject *
|
|
|
|
JSObject::getThrowTypeError() const
|
|
|
|
{
|
2011-04-18 13:50:47 -07:00
|
|
|
return getGlobal()->getThrowTypeError();
|
2010-08-02 23:52:12 -07:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
JSBool
|
2011-04-13 09:27:37 -07:00
|
|
|
js_GetArgsValue(JSContext *cx, StackFrame *fp, Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
JSObject *argsobj;
|
|
|
|
|
2011-06-02 10:40:27 -07:00
|
|
|
MarkTypeObjectFlags(cx, fp->fun()->getType(),
|
|
|
|
OBJECT_FLAG_CREATED_ARGUMENTS | OBJECT_FLAG_UNINLINEABLE);
|
2011-05-26 12:28:19 -07:00
|
|
|
|
2010-08-09 22:43:33 -07:00
|
|
|
if (fp->hasOverriddenArgs()) {
|
2010-08-12 15:46:03 -07:00
|
|
|
JS_ASSERT(fp->hasCallObj());
|
2009-08-14 11:43:16 -07:00
|
|
|
jsid id = ATOM_TO_JSID(cx->runtime->atomState.argumentsAtom);
|
2010-08-09 22:43:33 -07:00
|
|
|
return fp->callObj().getProperty(cx, id, vp);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
argsobj = js_GetArgsObject(cx, fp);
|
|
|
|
if (!argsobj)
|
|
|
|
return JS_FALSE;
|
2010-07-14 23:19:36 -07:00
|
|
|
vp->setObject(*argsobj);
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSBool
|
2011-04-13 09:27:37 -07:00
|
|
|
js_GetArgsProperty(JSContext *cx, StackFrame *fp, jsid id, Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-08-09 22:43:33 -07:00
|
|
|
JS_ASSERT(fp->isFunctionFrame());
|
|
|
|
|
|
|
|
if (fp->hasOverriddenArgs()) {
|
2010-08-12 15:46:03 -07:00
|
|
|
JS_ASSERT(fp->hasCallObj());
|
2009-08-14 11:43:16 -07:00
|
|
|
|
|
|
|
jsid argumentsid = ATOM_TO_JSID(cx->runtime->atomState.argumentsAtom);
|
2010-07-14 23:19:36 -07:00
|
|
|
Value v;
|
2010-08-09 22:43:33 -07:00
|
|
|
if (!fp->callObj().getProperty(cx, argumentsid, &v))
|
2009-08-14 11:43:16 -07:00
|
|
|
return false;
|
|
|
|
|
|
|
|
JSObject *obj;
|
2010-07-14 23:19:36 -07:00
|
|
|
if (v.isPrimitive()) {
|
2009-08-14 11:43:16 -07:00
|
|
|
obj = js_ValueToNonNullObject(cx, v);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!obj)
|
2009-08-14 11:43:16 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
2010-07-14 23:19:36 -07:00
|
|
|
obj = &v.toObject();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2009-08-11 13:05:44 -07:00
|
|
|
return obj->getProperty(cx, id, vp);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
vp->setUndefined();
|
2007-03-22 10:30:00 -07:00
|
|
|
if (JSID_IS_INT(id)) {
|
2009-08-14 11:43:16 -07:00
|
|
|
uint32 arg = uint32(JSID_TO_INT(id));
|
2011-04-25 19:40:00 -07:00
|
|
|
ArgumentsObject *argsobj = fp->maybeArgsObj();
|
2010-08-19 16:44:12 -07:00
|
|
|
if (arg < fp->numActualArgs()) {
|
2009-08-14 11:43:16 -07:00
|
|
|
if (argsobj) {
|
2011-04-25 19:40:00 -07:00
|
|
|
const Value &v = argsobj->element(arg);
|
2011-03-23 16:34:19 -07:00
|
|
|
if (v.isMagic(JS_ARGS_HOLE))
|
2009-08-14 11:43:16 -07:00
|
|
|
return argsobj->getProperty(cx, id, vp);
|
2011-03-23 16:34:19 -07:00
|
|
|
if (fp->functionScript()->strictModeCode) {
|
|
|
|
*vp = v;
|
|
|
|
return true;
|
|
|
|
}
|
2009-08-14 11:43:16 -07:00
|
|
|
}
|
2010-08-09 22:43:33 -07:00
|
|
|
*vp = fp->canonicalActualArg(arg);
|
2007-03-22 10:30:00 -07:00
|
|
|
} 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.
|
|
|
|
*/
|
2009-08-14 11:43:16 -07:00
|
|
|
if (argsobj)
|
|
|
|
return argsobj->getProperty(cx, id, vp);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2010-07-14 23:19:36 -07:00
|
|
|
} else if (JSID_IS_ATOM(id, cx->runtime->atomState.lengthAtom)) {
|
2011-04-25 19:40:00 -07:00
|
|
|
ArgumentsObject *argsobj = fp->maybeArgsObj();
|
|
|
|
if (argsobj && argsobj->hasOverriddenLength())
|
2009-08-14 11:43:16 -07:00
|
|
|
return argsobj->getProperty(cx, id, vp);
|
2010-08-19 16:44:12 -07:00
|
|
|
vp->setInt32(fp->numActualArgs());
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2009-08-14 11:43:16 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-04-25 19:40:00 -07:00
|
|
|
js::ArgumentsObject *
|
2011-05-11 14:04:25 -07:00
|
|
|
ArgumentsObject::create(JSContext *cx, uint32 argc, JSObject &callee)
|
2009-08-14 11:43:16 -07:00
|
|
|
{
|
2011-04-25 19:40:00 -07:00
|
|
|
JS_ASSERT(argc <= JS_ARGS_LENGTH_MAX);
|
|
|
|
|
2010-03-30 00:44:28 -07:00
|
|
|
JSObject *proto;
|
2011-05-11 14:04:25 -07:00
|
|
|
if (!js_GetClassPrototype(cx, callee.getGlobal(), JSProto_Object, &proto))
|
2010-03-30 00:44:28 -07:00
|
|
|
return NULL;
|
|
|
|
|
2010-12-18 20:44:51 -08:00
|
|
|
TypeObject *type = proto->getNewType(cx);
|
|
|
|
if (!type)
|
|
|
|
return NULL;
|
|
|
|
|
2011-04-25 19:40:00 -07:00
|
|
|
JS_STATIC_ASSERT(NormalArgumentsObject::RESERVED_SLOTS == 2);
|
|
|
|
JS_STATIC_ASSERT(StrictArgumentsObject::RESERVED_SLOTS == 2);
|
|
|
|
JSObject *obj = js_NewGCObject(cx, FINALIZE_OBJECT2);
|
|
|
|
if (!obj)
|
2009-08-14 11:43:16 -07:00
|
|
|
return NULL;
|
|
|
|
|
2011-03-07 18:42:04 -08:00
|
|
|
EmptyShape *emptyArgumentsShape = EmptyShape::getEmptyArgumentsShape(cx);
|
|
|
|
if (!emptyArgumentsShape)
|
|
|
|
return NULL;
|
|
|
|
AutoShapeRooter shapeRoot(cx, emptyArgumentsShape);
|
|
|
|
|
2010-08-29 11:57:08 -07:00
|
|
|
ArgumentsData *data = (ArgumentsData *)
|
2011-03-31 01:14:12 -07:00
|
|
|
cx->malloc_(offsetof(ArgumentsData, slots) + argc * sizeof(Value));
|
2010-08-29 11:57:08 -07:00
|
|
|
if (!data)
|
2010-03-30 00:44:28 -07:00
|
|
|
return NULL;
|
2010-08-29 11:57:08 -07:00
|
|
|
SetValueRangeToUndefined(data->slots, argc);
|
|
|
|
|
|
|
|
/* Can't fail from here on, so initialize everything in argsobj. */
|
2011-04-25 19:40:00 -07:00
|
|
|
obj->init(cx, callee.getFunctionPrivate()->inStrictMode()
|
|
|
|
? &StrictArgumentsObject::jsClass
|
|
|
|
: &NormalArgumentsObject::jsClass,
|
2011-05-22 08:59:06 -07:00
|
|
|
type, proto->getParent(), NULL, false);
|
2011-04-25 19:40:00 -07:00
|
|
|
obj->setMap(emptyArgumentsShape);
|
|
|
|
|
|
|
|
ArgumentsObject *argsobj = obj->asArguments();
|
2010-08-29 11:57:08 -07:00
|
|
|
|
2011-04-25 19:40:00 -07:00
|
|
|
JS_ASSERT(UINT32_MAX > (uint64(argc) << PACKED_BITS_COUNT));
|
|
|
|
argsobj->setInitialLength(argc);
|
2010-08-29 11:57:08 -07:00
|
|
|
|
2011-04-25 19:40:00 -07:00
|
|
|
argsobj->setCalleeAndData(callee, data);
|
2010-08-11 23:26:59 -07:00
|
|
|
|
2009-08-14 11:43:16 -07:00
|
|
|
return argsobj;
|
|
|
|
}
|
|
|
|
|
2010-10-01 21:00:55 -07:00
|
|
|
struct STATIC_SKIP_INFERENCE PutArg
|
2009-08-14 11:43:16 -07:00
|
|
|
{
|
2010-08-09 22:43:33 -07:00
|
|
|
PutArg(Value *dst) : dst(dst) {}
|
|
|
|
Value *dst;
|
2011-04-07 20:35:02 -07:00
|
|
|
bool operator()(uintN, Value *src) {
|
2010-08-09 22:43:33 -07:00
|
|
|
if (!dst->isMagic(JS_ARGS_HOLE))
|
|
|
|
*dst = *src;
|
|
|
|
++dst;
|
2011-04-07 20:35:02 -07:00
|
|
|
return true;
|
2009-08-14 11:43:16 -07:00
|
|
|
}
|
2010-08-09 22:43:33 -07:00
|
|
|
};
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
JSObject *
|
2011-04-13 09:27:37 -07:00
|
|
|
js_GetArgsObject(JSContext *cx, StackFrame *fp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* We must be in a function activation; the function must be lightweight
|
|
|
|
* or else fp must have a variable object.
|
|
|
|
*/
|
2010-08-09 22:43:33 -07:00
|
|
|
JS_ASSERT_IF(fp->fun()->isHeavyweight(), fp->hasCallObj());
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-05-11 09:11:40 -07:00
|
|
|
while (fp->isDirectEvalOrDebuggerFrame())
|
2010-08-09 22:43:33 -07:00
|
|
|
fp = fp->prev();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/* Create an arguments object for fp only if it lacks one. */
|
2010-08-12 15:46:03 -07:00
|
|
|
if (fp->hasArgsObj())
|
2010-08-09 22:43:33 -07:00
|
|
|
return &fp->argsObj();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-04-25 19:40:00 -07:00
|
|
|
ArgumentsObject *argsobj =
|
2011-05-11 14:04:25 -07:00
|
|
|
ArgumentsObject::create(cx, fp->numActualArgs(), fp->callee());
|
2009-08-04 03:55:58 -07:00
|
|
|
if (!argsobj)
|
2009-08-14 11:43:16 -07:00
|
|
|
return argsobj;
|
2009-08-04 03:55:58 -07:00
|
|
|
|
2010-08-11 23:27:33 -07:00
|
|
|
/*
|
2010-08-29 11:57:08 -07:00
|
|
|
* Strict mode functions have arguments objects that copy the initial
|
|
|
|
* actual parameter values. It is the caller's responsibility to get the
|
|
|
|
* arguments object before any parameters are modified! (The emitter
|
|
|
|
* ensures this by synthesizing an arguments access at the start of any
|
|
|
|
* strict mode function that contains an assignment to a parameter, or
|
|
|
|
* that calls eval.) Non-strict mode arguments use the frame pointer to
|
|
|
|
* retrieve up-to-date parameter values.
|
2010-08-11 23:27:33 -07:00
|
|
|
*/
|
2010-08-29 11:57:08 -07:00
|
|
|
if (argsobj->isStrictArguments())
|
2011-04-25 19:40:00 -07:00
|
|
|
fp->forEachCanonicalActualArg(PutArg(argsobj->data()->slots));
|
2010-08-29 11:57:08 -07:00
|
|
|
else
|
2010-08-11 23:27:33 -07:00
|
|
|
argsobj->setPrivate(fp);
|
|
|
|
|
2010-08-09 22:43:33 -07:00
|
|
|
fp->setArgsObj(*argsobj);
|
2007-03-22 10:30:00 -07:00
|
|
|
return argsobj;
|
|
|
|
}
|
|
|
|
|
2009-08-14 11:43:16 -07:00
|
|
|
void
|
2011-04-13 09:27:37 -07:00
|
|
|
js_PutArgsObject(StackFrame *fp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-04-25 19:40:00 -07:00
|
|
|
ArgumentsObject &argsobj = fp->argsObj();
|
2010-08-09 22:43:33 -07:00
|
|
|
if (argsobj.isNormalArguments()) {
|
|
|
|
JS_ASSERT(argsobj.getPrivate() == fp);
|
2011-04-25 19:40:00 -07:00
|
|
|
fp->forEachCanonicalActualArg(PutArg(argsobj.data()->slots));
|
2010-08-09 22:43:33 -07:00
|
|
|
argsobj.setPrivate(NULL);
|
2010-08-11 23:27:33 -07:00
|
|
|
} else {
|
2010-08-09 22:43:33 -07:00
|
|
|
JS_ASSERT(!argsobj.getPrivate());
|
2010-08-11 23:27:33 -07:00
|
|
|
}
|
2009-08-14 11:43:16 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-08-29 11:57:08 -07:00
|
|
|
#ifdef JS_TRACER
|
|
|
|
|
2009-08-14 11:43:16 -07:00
|
|
|
/*
|
|
|
|
* Traced versions of js_GetArgsObject and js_PutArgsObject.
|
|
|
|
*/
|
2009-08-14 11:54:26 -07:00
|
|
|
JSObject * JS_FASTCALL
|
2011-05-11 14:04:25 -07:00
|
|
|
js_NewArgumentsOnTrace(JSContext *cx, uint32 argc, JSObject *callee)
|
2009-08-14 11:43:16 -07:00
|
|
|
{
|
2011-05-11 14:04:25 -07:00
|
|
|
ArgumentsObject *argsobj = ArgumentsObject::create(cx, argc, *callee);
|
2009-09-17 17:44:19 -07:00
|
|
|
if (!argsobj)
|
|
|
|
return NULL;
|
2010-08-11 23:27:33 -07:00
|
|
|
|
2010-08-29 11:57:08 -07:00
|
|
|
if (argsobj->isStrictArguments()) {
|
2010-08-11 23:27:33 -07:00
|
|
|
/*
|
|
|
|
* Strict mode callers must copy arguments into the created arguments
|
2010-08-29 11:57:08 -07:00
|
|
|
* object. The trace-JITting code is in TraceRecorder::newArguments.
|
2010-08-11 23:27:33 -07:00
|
|
|
*/
|
|
|
|
JS_ASSERT(!argsobj->getPrivate());
|
|
|
|
} else {
|
2010-08-29 11:57:08 -07:00
|
|
|
argsobj->setPrivate(JS_ARGUMENTS_OBJECT_ON_TRACE);
|
2010-08-11 23:27:33 -07:00
|
|
|
}
|
|
|
|
|
2009-09-09 11:40:21 -07:00
|
|
|
return argsobj;
|
2009-08-14 11:43:16 -07:00
|
|
|
}
|
2011-05-11 14:04:25 -07:00
|
|
|
JS_DEFINE_CALLINFO_3(extern, OBJECT, js_NewArgumentsOnTrace, CONTEXT, UINT32, OBJECT,
|
2010-08-06 17:19:22 -07:00
|
|
|
0, nanojit::ACCSET_STORE_ANY)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-08-14 11:43:16 -07:00
|
|
|
/* FIXME change the return type to void. */
|
|
|
|
JSBool JS_FASTCALL
|
2011-04-25 19:40:00 -07:00
|
|
|
js_PutArgumentsOnTrace(JSContext *cx, JSObject *obj, Value *argv)
|
2009-08-14 11:43:16 -07:00
|
|
|
{
|
2011-04-25 19:40:00 -07:00
|
|
|
NormalArgumentsObject *argsobj = obj->asNormalArguments();
|
|
|
|
|
2010-08-29 11:57:08 -07:00
|
|
|
JS_ASSERT(argsobj->getPrivate() == JS_ARGUMENTS_OBJECT_ON_TRACE);
|
2010-08-09 22:43:33 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* TraceRecorder::putActivationObjects builds a single, contiguous array of
|
|
|
|
* the arguments, regardless of whether #actuals > #formals so there is no
|
|
|
|
* need to worry about actual vs. formal arguments.
|
|
|
|
*/
|
2011-04-25 19:40:00 -07:00
|
|
|
Value *srcend = argv + argsobj->initialLength();
|
|
|
|
Value *dst = argsobj->data()->slots;
|
|
|
|
for (Value *src = argv; src < srcend; ++src, ++dst) {
|
2010-08-09 22:43:33 -07:00
|
|
|
if (!dst->isMagic(JS_ARGS_HOLE))
|
|
|
|
*dst = *src;
|
|
|
|
}
|
|
|
|
|
2009-10-22 17:56:46 -07:00
|
|
|
argsobj->setPrivate(NULL);
|
2009-08-14 11:43:16 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2010-08-09 22:43:33 -07:00
|
|
|
JS_DEFINE_CALLINFO_3(extern, BOOL, js_PutArgumentsOnTrace, CONTEXT, OBJECT, VALUEPTR, 0,
|
2010-07-27 22:33:42 -07:00
|
|
|
nanojit::ACCSET_STORE_ANY)
|
2009-08-14 11:43:16 -07:00
|
|
|
|
2010-08-29 11:57:08 -07:00
|
|
|
#endif /* JS_TRACER */
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
static JSBool
|
2010-07-14 23:19:36 -07:00
|
|
|
args_delProperty(JSContext *cx, JSObject *obj, jsid id, Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-04-25 19:40:00 -07:00
|
|
|
ArgumentsObject *argsobj = obj->asArguments();
|
2010-07-14 23:19:36 -07:00
|
|
|
if (JSID_IS_INT(id)) {
|
|
|
|
uintN arg = uintN(JSID_TO_INT(id));
|
2011-04-25 19:40:00 -07:00
|
|
|
if (arg < argsobj->initialLength())
|
|
|
|
argsobj->setElement(arg, MagicValue(JS_ARGS_HOLE));
|
2010-07-14 23:19:36 -07:00
|
|
|
} else if (JSID_IS_ATOM(id, cx->runtime->atomState.lengthAtom)) {
|
2011-04-25 19:40:00 -07:00
|
|
|
argsobj->markLengthOverridden();
|
2010-07-14 23:19:36 -07:00
|
|
|
} else if (JSID_IS_ATOM(id, cx->runtime->atomState.calleeAtom)) {
|
2011-04-25 19:40:00 -07:00
|
|
|
argsobj->asNormalArguments()->clearCallee();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2009-08-04 03:55:58 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2009-06-04 18:58:47 -07:00
|
|
|
static JS_REQUIRES_STACK JSObject *
|
2011-04-13 09:27:37 -07:00
|
|
|
WrapEscapingClosure(JSContext *cx, StackFrame *fp, JSFunction *fun)
|
2009-06-04 18:58:47 -07:00
|
|
|
{
|
2009-06-09 00:51:02 -07:00
|
|
|
JS_ASSERT(fun->optimizedClosure());
|
2009-06-04 18:58:47 -07:00
|
|
|
JS_ASSERT(!fun->u.i.wrapper);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We do not attempt to reify Call and Block objects on demand for outer
|
|
|
|
* scopes. This could be done (see the "v8" patch in bug 494235) but it is
|
|
|
|
* fragile in the face of ongoing compile-time optimization. Instead, the
|
|
|
|
* _DBG* opcodes used by wrappers created here must cope with unresolved
|
|
|
|
* upvars and throw them as reference errors. Caveat debuggers!
|
|
|
|
*/
|
2010-10-27 16:18:16 -07:00
|
|
|
JSObject *scopeChain = GetScopeChain(cx, fp);
|
2009-06-04 18:58:47 -07:00
|
|
|
if (!scopeChain)
|
|
|
|
return NULL;
|
|
|
|
|
2010-12-18 20:44:51 -08:00
|
|
|
JSObject *wfunobj = NewFunction(cx, scopeChain);
|
2009-06-04 18:58:47 -07:00
|
|
|
if (!wfunobj)
|
|
|
|
return NULL;
|
2010-07-14 23:19:36 -07:00
|
|
|
AutoObjectRooter tvr(cx, wfunobj);
|
2009-06-04 18:58:47 -07:00
|
|
|
|
|
|
|
JSFunction *wfun = (JSFunction *) wfunobj;
|
2009-09-05 08:59:11 -07:00
|
|
|
wfunobj->setPrivate(wfun);
|
2010-08-29 11:57:08 -07:00
|
|
|
wfun->nargs = fun->nargs;
|
2009-06-04 18:58:47 -07:00
|
|
|
wfun->flags = fun->flags | JSFUN_HEAVYWEIGHT;
|
|
|
|
wfun->u.i.skipmin = fun->u.i.skipmin;
|
|
|
|
wfun->u.i.wrapper = true;
|
|
|
|
wfun->u.i.script = NULL;
|
|
|
|
wfun->atom = fun->atom;
|
|
|
|
|
2010-11-16 15:34:24 -08:00
|
|
|
JSScript *script = fun->script();
|
2009-09-09 16:52:55 -07:00
|
|
|
jssrcnote *snbase = script->notes();
|
2009-06-04 18:58:47 -07:00
|
|
|
jssrcnote *sn = snbase;
|
|
|
|
while (!SN_IS_TERMINATOR(sn))
|
|
|
|
sn = SN_NEXT(sn);
|
|
|
|
uintN nsrcnotes = (sn - snbase) + 1;
|
|
|
|
|
|
|
|
/* NB: GC must not occur before wscript is homed in wfun->u.i.script. */
|
2010-09-13 22:33:44 -07:00
|
|
|
JSScript *wscript = JSScript::NewScript(cx, script->length, nsrcnotes,
|
|
|
|
script->atomMap.length,
|
2010-12-02 14:23:51 -08:00
|
|
|
JSScript::isValidOffset(script->objectsOffset)
|
2010-09-13 22:33:44 -07:00
|
|
|
? script->objects()->length
|
|
|
|
: 0,
|
2010-11-16 15:34:24 -08:00
|
|
|
script->bindings.countUpvars(),
|
2010-12-02 14:23:51 -08:00
|
|
|
JSScript::isValidOffset(script->regexpsOffset)
|
2010-09-13 22:33:44 -07:00
|
|
|
? script->regexps()->length
|
|
|
|
: 0,
|
2010-12-02 14:23:51 -08:00
|
|
|
JSScript::isValidOffset(script->trynotesOffset)
|
2010-09-13 22:33:44 -07:00
|
|
|
? script->trynotes()->length
|
|
|
|
: 0,
|
2010-12-02 14:23:51 -08:00
|
|
|
JSScript::isValidOffset(script->constOffset)
|
2010-09-13 22:33:44 -07:00
|
|
|
? script->consts()->length
|
|
|
|
: 0,
|
2010-12-02 14:23:51 -08:00
|
|
|
JSScript::isValidOffset(script->globalsOffset)
|
2010-09-13 22:33:44 -07:00
|
|
|
? script->globals()->length
|
|
|
|
: 0,
|
|
|
|
script->nClosedArgs,
|
2011-01-27 02:54:58 -08:00
|
|
|
script->nClosedVars,
|
2011-05-14 05:45:13 -07:00
|
|
|
script->nTypeSets,
|
2011-01-27 02:54:58 -08:00
|
|
|
script->getVersion());
|
2009-06-04 18:58:47 -07:00
|
|
|
if (!wscript)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
memcpy(wscript->code, script->code, script->length);
|
|
|
|
wscript->main = wscript->code + (script->main - script->code);
|
|
|
|
|
2009-09-09 16:52:55 -07:00
|
|
|
memcpy(wscript->notes(), snbase, nsrcnotes * sizeof(jssrcnote));
|
2009-06-04 18:58:47 -07:00
|
|
|
memcpy(wscript->atomMap.vector, script->atomMap.vector,
|
|
|
|
wscript->atomMap.length * sizeof(JSAtom *));
|
2010-12-02 14:23:51 -08:00
|
|
|
if (JSScript::isValidOffset(script->objectsOffset)) {
|
2009-09-09 16:52:55 -07:00
|
|
|
memcpy(wscript->objects()->vector, script->objects()->vector,
|
|
|
|
wscript->objects()->length * sizeof(JSObject *));
|
2009-06-04 18:58:47 -07:00
|
|
|
}
|
2010-12-02 14:23:51 -08:00
|
|
|
if (JSScript::isValidOffset(script->regexpsOffset)) {
|
2009-09-09 16:52:55 -07:00
|
|
|
memcpy(wscript->regexps()->vector, script->regexps()->vector,
|
|
|
|
wscript->regexps()->length * sizeof(JSObject *));
|
2009-06-04 18:58:47 -07:00
|
|
|
}
|
2010-12-02 14:23:51 -08:00
|
|
|
if (JSScript::isValidOffset(script->trynotesOffset)) {
|
2009-09-09 16:52:55 -07:00
|
|
|
memcpy(wscript->trynotes()->vector, script->trynotes()->vector,
|
|
|
|
wscript->trynotes()->length * sizeof(JSTryNote));
|
2009-06-04 18:58:47 -07:00
|
|
|
}
|
2010-12-02 14:23:51 -08:00
|
|
|
if (JSScript::isValidOffset(script->globalsOffset)) {
|
2010-05-22 15:38:04 -07:00
|
|
|
memcpy(wscript->globals()->vector, script->globals()->vector,
|
|
|
|
wscript->globals()->length * sizeof(GlobalSlotArray::Entry));
|
|
|
|
}
|
2010-09-13 22:33:44 -07:00
|
|
|
if (script->nClosedArgs + script->nClosedVars != 0)
|
|
|
|
script->copyClosedSlotsTo(wscript);
|
2009-06-04 18:58:47 -07:00
|
|
|
|
2010-11-16 15:34:24 -08:00
|
|
|
if (script->bindings.hasUpvars()) {
|
|
|
|
JS_ASSERT(script->bindings.countUpvars() == wscript->upvars()->length);
|
2009-09-09 16:52:55 -07:00
|
|
|
memcpy(wscript->upvars()->vector, script->upvars()->vector,
|
2010-11-16 15:34:24 -08:00
|
|
|
script->bindings.countUpvars() * sizeof(uint32));
|
2009-06-04 18:58:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
jsbytecode *pc = wscript->code;
|
|
|
|
while (*pc != JSOP_STOP) {
|
2010-08-29 11:57:08 -07:00
|
|
|
/* FIXME should copy JSOP_TRAP? */
|
2009-06-04 18:58:47 -07:00
|
|
|
JSOp op = js_GetOpcode(cx, wscript, pc);
|
|
|
|
const JSCodeSpec *cs = &js_CodeSpec[op];
|
|
|
|
ptrdiff_t oplen = cs->length;
|
|
|
|
if (oplen < 0)
|
|
|
|
oplen = js_GetVariableBytecodeLength(pc);
|
|
|
|
|
|
|
|
/*
|
2010-09-02 14:50:44 -07:00
|
|
|
* Rewrite JSOP_{GET,CALL}FCSLOT as JSOP_{GET,CALL}UPVAR_DBG for the
|
2009-06-04 18:58:47 -07:00
|
|
|
* case where fun is an escaping flat closure. This works because the
|
2010-09-02 14:50:44 -07:00
|
|
|
* UPVAR and FCSLOT ops by design have the same format: an upvar index
|
2009-06-04 18:58:47 -07:00
|
|
|
* immediate operand.
|
|
|
|
*/
|
|
|
|
switch (op) {
|
2010-08-29 11:57:08 -07:00
|
|
|
case JSOP_GETFCSLOT: *pc = JSOP_GETUPVAR_DBG; break;
|
|
|
|
case JSOP_CALLFCSLOT: *pc = JSOP_CALLUPVAR_DBG; break;
|
2009-06-04 18:58:47 -07:00
|
|
|
case JSOP_DEFFUN_FC: *pc = JSOP_DEFFUN_DBGFC; break;
|
|
|
|
case JSOP_DEFLOCALFUN_FC: *pc = JSOP_DEFLOCALFUN_DBGFC; break;
|
|
|
|
case JSOP_LAMBDA_FC: *pc = JSOP_LAMBDA_DBGFC; break;
|
|
|
|
default:;
|
|
|
|
}
|
|
|
|
pc += oplen;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2009-06-09 00:51:02 -07:00
|
|
|
* Fill in the rest of wscript. This means if you add members to JSScript
|
|
|
|
* you must update this code. FIXME: factor into JSScript::clone method.
|
2009-06-04 18:58:47 -07:00
|
|
|
*/
|
2011-01-27 02:54:58 -08:00
|
|
|
JS_ASSERT(wscript->getVersion() == script->getVersion());
|
2009-06-04 18:58:47 -07:00
|
|
|
wscript->nfixed = script->nfixed;
|
|
|
|
wscript->filename = script->filename;
|
|
|
|
wscript->lineno = script->lineno;
|
|
|
|
wscript->nslots = script->nslots;
|
|
|
|
wscript->staticLevel = script->staticLevel;
|
|
|
|
wscript->principals = script->principals;
|
2010-09-13 22:33:44 -07:00
|
|
|
wscript->noScriptRval = script->noScriptRval;
|
|
|
|
wscript->savedCallerFun = script->savedCallerFun;
|
|
|
|
wscript->hasSharps = script->hasSharps;
|
|
|
|
wscript->strictModeCode = script->strictModeCode;
|
|
|
|
wscript->compileAndGo = script->compileAndGo;
|
|
|
|
wscript->usesEval = script->usesEval;
|
|
|
|
wscript->usesArguments = script->usesArguments;
|
|
|
|
wscript->warnedAboutTwoArgumentEval = script->warnedAboutTwoArgumentEval;
|
2009-06-04 18:58:47 -07:00
|
|
|
if (wscript->principals)
|
|
|
|
JSPRINCIPALS_HOLD(cx, wscript->principals);
|
|
|
|
#ifdef CHECK_SCRIPT_OWNER
|
|
|
|
wscript->owner = script->owner;
|
|
|
|
#endif
|
|
|
|
|
2010-11-16 15:34:24 -08:00
|
|
|
wscript->bindings.clone(cx, &script->bindings);
|
|
|
|
|
2009-06-04 18:58:47 -07:00
|
|
|
/* Deoptimize wfun from FUN_{FLAT,NULL}_CLOSURE to FUN_INTERPRETED. */
|
|
|
|
FUN_SET_KIND(wfun, JSFUN_INTERPRETED);
|
|
|
|
wfun->u.i.script = wscript;
|
2011-03-20 11:22:33 -07:00
|
|
|
if (!wscript->typeSetFunction(cx, wfun))
|
2011-03-07 08:25:27 -08:00
|
|
|
return NULL;
|
2011-02-11 13:23:24 -08:00
|
|
|
js_CallNewScriptHook(cx, wscript, wfun);
|
2009-06-04 18:58:47 -07:00
|
|
|
return wfunobj;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
static JSBool
|
2010-07-14 23:19:36 -07:00
|
|
|
ArgGetter(JSContext *cx, JSObject *obj, jsid id, Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-08-06 17:19:22 -07:00
|
|
|
LeaveTrace(cx);
|
|
|
|
|
2011-04-11 01:38:27 -07:00
|
|
|
if (!obj->isNormalArguments())
|
2009-08-04 03:55:58 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-04-25 19:40:00 -07:00
|
|
|
NormalArgumentsObject *argsobj = obj->asNormalArguments();
|
2010-07-14 23:19:36 -07:00
|
|
|
if (JSID_IS_INT(id)) {
|
2009-08-04 03:55:58 -07:00
|
|
|
/*
|
|
|
|
* arg can exceed the number of arguments if a script changed the
|
|
|
|
* prototype to point to another Arguments object with a bigger argc.
|
|
|
|
*/
|
2010-07-14 23:19:36 -07:00
|
|
|
uintN arg = uintN(JSID_TO_INT(id));
|
2011-04-25 19:40:00 -07:00
|
|
|
if (arg < argsobj->initialLength()) {
|
|
|
|
JS_ASSERT(!argsobj->element(arg).isMagic(JS_ARGS_HOLE));
|
|
|
|
if (StackFrame *fp = reinterpret_cast<StackFrame *>(argsobj->getPrivate()))
|
2010-08-09 22:43:33 -07:00
|
|
|
*vp = fp->canonicalActualArg(arg);
|
2010-10-06 10:09:40 -07:00
|
|
|
else
|
2011-04-25 19:40:00 -07:00
|
|
|
*vp = argsobj->element(arg);
|
2009-08-14 11:43:16 -07:00
|
|
|
}
|
2010-07-14 23:19:36 -07:00
|
|
|
} else if (JSID_IS_ATOM(id, cx->runtime->atomState.lengthAtom)) {
|
2011-04-25 19:40:00 -07:00
|
|
|
if (!argsobj->hasOverriddenLength())
|
|
|
|
vp->setInt32(argsobj->initialLength());
|
2009-08-04 03:55:58 -07:00
|
|
|
} else {
|
2010-07-14 23:19:36 -07:00
|
|
|
JS_ASSERT(JSID_IS_ATOM(id, cx->runtime->atomState.calleeAtom));
|
2011-04-25 19:40:00 -07:00
|
|
|
const Value &v = argsobj->callee();
|
2010-07-14 23:19:36 -07:00
|
|
|
if (!v.isMagic(JS_ARGS_HOLE)) {
|
2009-06-09 00:51:02 -07:00
|
|
|
/*
|
|
|
|
* If this function or one in it needs upvars that reach above it
|
|
|
|
* in the scope chain, it must not be a null closure (it could be a
|
|
|
|
* flat closure, or an unoptimized closure -- the latter itself not
|
|
|
|
* necessarily heavyweight). Rather than wrap here, we simply throw
|
|
|
|
* to reduce code size and tell debugger users the truth instead of
|
|
|
|
* passing off a fibbing wrapper.
|
|
|
|
*/
|
2010-07-14 23:19:36 -07:00
|
|
|
if (GET_FUNCTION_PRIVATE(cx, &v.toObject())->needsWrapper()) {
|
2009-06-05 11:41:32 -07:00
|
|
|
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
|
|
|
|
JSMSG_OPTIMIZED_CLOSURE_LEAK);
|
2009-08-04 03:55:58 -07:00
|
|
|
return false;
|
2009-06-05 11:41:32 -07:00
|
|
|
}
|
2009-08-14 11:43:16 -07:00
|
|
|
*vp = v;
|
2009-06-05 11:41:32 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2009-08-04 03:55:58 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2011-02-09 11:31:40 -08:00
|
|
|
ArgSetter(JSContext *cx, JSObject *obj, jsid id, JSBool strict, Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-11-09 16:42:38 -08:00
|
|
|
#ifdef JS_TRACER
|
2009-11-09 15:36:41 -08:00
|
|
|
// To be able to set a property here on trace, we would have to make
|
|
|
|
// sure any updates also get written back to the trace native stack.
|
|
|
|
// For simplicity, we just leave trace, since this is presumably not
|
|
|
|
// a common operation.
|
2011-02-14 12:01:58 -08:00
|
|
|
LeaveTrace(cx);
|
2009-11-09 16:42:38 -08:00
|
|
|
#endif
|
2009-11-09 15:36:41 -08:00
|
|
|
|
2011-04-11 01:38:27 -07:00
|
|
|
|
|
|
|
if (!obj->isNormalArguments())
|
2009-08-04 03:55:58 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-04-25 19:40:00 -07:00
|
|
|
NormalArgumentsObject *argsobj = obj->asNormalArguments();
|
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
if (JSID_IS_INT(id)) {
|
|
|
|
uintN arg = uintN(JSID_TO_INT(id));
|
2011-04-25 19:40:00 -07:00
|
|
|
if (arg < argsobj->initialLength()) {
|
|
|
|
if (StackFrame *fp = reinterpret_cast<StackFrame *>(argsobj->getPrivate())) {
|
2010-09-29 18:05:54 -07:00
|
|
|
JSScript *script = fp->functionScript();
|
2010-10-29 08:05:55 -07:00
|
|
|
if (script->usesArguments) {
|
2011-05-09 07:12:47 -07:00
|
|
|
if (arg < fp->numFormalArgs())
|
|
|
|
script->typeSetArgument(cx, arg, *vp);
|
2010-09-29 18:05:54 -07:00
|
|
|
fp->canonicalActualArg(arg) = *vp;
|
2010-10-29 08:05:55 -07:00
|
|
|
}
|
2009-08-14 11:43:16 -07:00
|
|
|
return true;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2009-08-04 03:55:58 -07:00
|
|
|
} else {
|
2010-07-14 23:19:36 -07:00
|
|
|
JS_ASSERT(JSID_IS_ATOM(id, cx->runtime->atomState.lengthAtom) ||
|
|
|
|
JSID_IS_ATOM(id, cx->runtime->atomState.calleeAtom));
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2009-08-14 11:43:16 -07:00
|
|
|
|
|
|
|
/*
|
2011-02-03 15:11:21 -08:00
|
|
|
* For simplicity we use delete/define to replace the property with one
|
2010-08-11 23:26:59 -07:00
|
|
|
* backed by the default Object getter and setter. Note that we rely on
|
|
|
|
* args_delProperty to clear the corresponding reserved slot so the GC can
|
2011-02-03 15:11:21 -08:00
|
|
|
* collect its value. Note also that we must define the property instead
|
|
|
|
* of setting it in case the user has changed the prototype to an object
|
|
|
|
* that has a setter for this id.
|
2009-08-14 11:43:16 -07:00
|
|
|
*/
|
2010-03-28 22:05:50 -07:00
|
|
|
AutoValueRooter tvr(cx);
|
2011-04-25 19:40:00 -07:00
|
|
|
return js_DeleteProperty(cx, argsobj, id, tvr.addr(), false) &&
|
|
|
|
js_DefineProperty(cx, argsobj, id, vp, NULL, NULL, JSPROP_ENUMERATE);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2010-07-14 23:19:36 -07:00
|
|
|
args_resolve(JSContext *cx, JSObject *obj, jsid id, uintN flags,
|
2007-03-22 10:30:00 -07:00
|
|
|
JSObject **objp)
|
|
|
|
{
|
2009-08-14 11:43:16 -07:00
|
|
|
*objp = NULL;
|
2010-12-13 18:10:39 -08:00
|
|
|
|
2011-04-25 19:40:00 -07:00
|
|
|
NormalArgumentsObject *argsobj = obj->asNormalArguments();
|
|
|
|
|
2010-12-13 18:10:39 -08:00
|
|
|
uintN attrs = JSPROP_SHARED | JSPROP_SHADOWABLE;
|
2010-07-14 23:19:36 -07:00
|
|
|
if (JSID_IS_INT(id)) {
|
|
|
|
uint32 arg = uint32(JSID_TO_INT(id));
|
2011-04-25 19:40:00 -07:00
|
|
|
if (arg >= argsobj->initialLength() || argsobj->element(arg).isMagic(JS_ARGS_HOLE))
|
2010-12-13 18:10:39 -08:00
|
|
|
return true;
|
|
|
|
|
|
|
|
attrs |= JSPROP_ENUMERATE;
|
2010-07-14 23:19:36 -07:00
|
|
|
} else if (JSID_IS_ATOM(id, cx->runtime->atomState.lengthAtom)) {
|
2011-04-25 19:40:00 -07:00
|
|
|
if (argsobj->hasOverriddenLength())
|
2010-12-13 18:10:39 -08:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
if (!JSID_IS_ATOM(id, cx->runtime->atomState.calleeAtom))
|
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-04-25 19:40:00 -07:00
|
|
|
if (argsobj->callee().isMagic(JS_ARGS_HOLE))
|
2010-12-13 18:10:39 -08:00
|
|
|
return true;
|
2009-08-14 11:43:16 -07:00
|
|
|
}
|
2010-12-13 18:10:39 -08:00
|
|
|
|
|
|
|
Value undef = UndefinedValue();
|
2011-04-25 19:40:00 -07:00
|
|
|
if (!js_DefineProperty(cx, argsobj, id, &undef, ArgGetter, ArgSetter, attrs))
|
2010-12-13 18:10:39 -08:00
|
|
|
return JS_FALSE;
|
|
|
|
|
2011-04-25 19:40:00 -07:00
|
|
|
*objp = argsobj;
|
2009-08-04 03:55:58 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
|
|
|
args_enumerate(JSContext *cx, JSObject *obj)
|
|
|
|
{
|
2011-04-25 19:40:00 -07:00
|
|
|
NormalArgumentsObject *argsobj = obj->asNormalArguments();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/*
|
2009-08-14 11:43:16 -07:00
|
|
|
* Trigger reflection in args_resolve using a series of js_LookupProperty
|
|
|
|
* calls.
|
2007-03-22 10:30:00 -07:00
|
|
|
*/
|
2011-04-25 19:40:00 -07:00
|
|
|
int argc = int(argsobj->initialLength());
|
2009-08-04 03:55:58 -07:00
|
|
|
for (int i = -2; i != argc; i++) {
|
|
|
|
jsid id = (i == -2)
|
|
|
|
? ATOM_TO_JSID(cx->runtime->atomState.lengthAtom)
|
|
|
|
: (i == -1)
|
|
|
|
? ATOM_TO_JSID(cx->runtime->atomState.calleeAtom)
|
2010-07-14 23:19:36 -07:00
|
|
|
: INT_TO_JSID(i);
|
2009-08-04 03:55:58 -07:00
|
|
|
|
|
|
|
JSObject *pobj;
|
|
|
|
JSProperty *prop;
|
2011-04-25 19:40:00 -07:00
|
|
|
if (!js_LookupProperty(cx, argsobj, id, &pobj, &prop))
|
2009-08-04 03:55:58 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2009-08-04 03:55:58 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-10-25 16:47:11 -07:00
|
|
|
static JSBool
|
2010-08-11 23:26:59 -07:00
|
|
|
StrictArgGetter(JSContext *cx, JSObject *obj, jsid id, Value *vp)
|
|
|
|
{
|
|
|
|
LeaveTrace(cx);
|
|
|
|
|
2011-04-11 01:38:27 -07:00
|
|
|
if (!obj->isStrictArguments())
|
2010-08-11 23:26:59 -07:00
|
|
|
return true;
|
|
|
|
|
2011-04-25 19:40:00 -07:00
|
|
|
StrictArgumentsObject *argsobj = obj->asStrictArguments();
|
|
|
|
|
2010-08-11 23:26:59 -07:00
|
|
|
if (JSID_IS_INT(id)) {
|
|
|
|
/*
|
|
|
|
* arg can exceed the number of arguments if a script changed the
|
|
|
|
* prototype to point to another Arguments object with a bigger argc.
|
|
|
|
*/
|
|
|
|
uintN arg = uintN(JSID_TO_INT(id));
|
2011-04-25 19:40:00 -07:00
|
|
|
if (arg < argsobj->initialLength()) {
|
|
|
|
const Value &v = argsobj->element(arg);
|
2010-08-11 23:27:33 -07:00
|
|
|
if (!v.isMagic(JS_ARGS_HOLE))
|
|
|
|
*vp = v;
|
2010-08-11 23:26:59 -07:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
JS_ASSERT(JSID_IS_ATOM(id, cx->runtime->atomState.lengthAtom));
|
2011-04-25 19:40:00 -07:00
|
|
|
if (!argsobj->hasOverriddenLength())
|
|
|
|
vp->setInt32(argsobj->initialLength());
|
2010-08-11 23:26:59 -07:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-10-25 16:47:11 -07:00
|
|
|
static JSBool
|
2011-02-09 11:31:40 -08:00
|
|
|
|
|
|
|
StrictArgSetter(JSContext *cx, JSObject *obj, jsid id, JSBool strict, Value *vp)
|
2010-08-11 23:26:59 -07:00
|
|
|
{
|
2011-04-11 01:38:27 -07:00
|
|
|
if (!obj->isStrictArguments())
|
2010-08-11 23:26:59 -07:00
|
|
|
return true;
|
|
|
|
|
2011-04-25 19:40:00 -07:00
|
|
|
StrictArgumentsObject *argsobj = obj->asStrictArguments();
|
|
|
|
|
2010-08-11 23:26:59 -07:00
|
|
|
if (JSID_IS_INT(id)) {
|
|
|
|
uintN arg = uintN(JSID_TO_INT(id));
|
2011-04-25 19:40:00 -07:00
|
|
|
if (arg < argsobj->initialLength()) {
|
|
|
|
argsobj->setElement(arg, *vp);
|
2010-08-11 23:26:59 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
JS_ASSERT(JSID_IS_ATOM(id, cx->runtime->atomState.lengthAtom));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* For simplicity we use delete/set to replace the property with one
|
|
|
|
* backed by the default Object getter and setter. Note that we rely on
|
|
|
|
* args_delProperty to clear the corresponding reserved slot so the GC can
|
|
|
|
* collect its value.
|
|
|
|
*/
|
|
|
|
AutoValueRooter tvr(cx);
|
2011-04-25 19:40:00 -07:00
|
|
|
return js_DeleteProperty(cx, argsobj, id, tvr.addr(), strict) &&
|
|
|
|
js_SetProperty(cx, argsobj, id, vp, strict);
|
2010-08-11 23:26:59 -07:00
|
|
|
}
|
|
|
|
|
2010-10-25 16:47:11 -07:00
|
|
|
static JSBool
|
2010-08-11 23:26:59 -07:00
|
|
|
strictargs_resolve(JSContext *cx, JSObject *obj, jsid id, uintN flags, JSObject **objp)
|
|
|
|
{
|
|
|
|
*objp = NULL;
|
2010-12-13 18:10:39 -08:00
|
|
|
|
2011-04-25 19:40:00 -07:00
|
|
|
StrictArgumentsObject *argsobj = obj->asStrictArguments();
|
|
|
|
|
2010-12-13 18:10:39 -08:00
|
|
|
uintN attrs = JSPROP_SHARED | JSPROP_SHADOWABLE;
|
|
|
|
PropertyOp getter = StrictArgGetter;
|
2011-02-09 11:31:40 -08:00
|
|
|
StrictPropertyOp setter = StrictArgSetter;
|
2010-12-13 18:10:39 -08:00
|
|
|
|
2010-08-11 23:26:59 -07:00
|
|
|
if (JSID_IS_INT(id)) {
|
|
|
|
uint32 arg = uint32(JSID_TO_INT(id));
|
2011-04-25 19:40:00 -07:00
|
|
|
if (arg >= argsobj->initialLength() || argsobj->element(arg).isMagic(JS_ARGS_HOLE))
|
2010-12-13 18:10:39 -08:00
|
|
|
return true;
|
2010-08-11 23:26:59 -07:00
|
|
|
|
2010-12-13 18:10:39 -08:00
|
|
|
attrs |= JSPROP_ENUMERATE;
|
|
|
|
} else if (JSID_IS_ATOM(id, cx->runtime->atomState.lengthAtom)) {
|
2011-04-25 19:40:00 -07:00
|
|
|
if (argsobj->hasOverriddenLength())
|
2010-12-13 18:10:39 -08:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
if (!JSID_IS_ATOM(id, cx->runtime->atomState.calleeAtom) &&
|
|
|
|
!JSID_IS_ATOM(id, cx->runtime->atomState.callerAtom)) {
|
|
|
|
return true;
|
2010-08-11 23:26:59 -07:00
|
|
|
}
|
|
|
|
|
2010-12-13 18:10:39 -08:00
|
|
|
attrs = JSPROP_PERMANENT | JSPROP_GETTER | JSPROP_SETTER | JSPROP_SHARED;
|
2011-04-25 19:40:00 -07:00
|
|
|
getter = CastAsPropertyOp(argsobj->getThrowTypeError());
|
|
|
|
setter = CastAsStrictPropertyOp(argsobj->getThrowTypeError());
|
2010-08-11 23:26:59 -07:00
|
|
|
}
|
|
|
|
|
2010-12-13 18:10:39 -08:00
|
|
|
Value undef = UndefinedValue();
|
2011-04-25 19:40:00 -07:00
|
|
|
if (!js_DefineProperty(cx, argsobj, id, &undef, getter, setter, attrs))
|
2010-12-13 18:10:39 -08:00
|
|
|
return false;
|
|
|
|
|
2011-04-25 19:40:00 -07:00
|
|
|
*objp = argsobj;
|
2010-08-11 23:26:59 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-10-25 16:47:11 -07:00
|
|
|
static JSBool
|
2010-08-11 23:26:59 -07:00
|
|
|
strictargs_enumerate(JSContext *cx, JSObject *obj)
|
|
|
|
{
|
2011-04-25 19:40:00 -07:00
|
|
|
StrictArgumentsObject *argsobj = obj->asStrictArguments();
|
2010-08-11 23:26:59 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Trigger reflection in strictargs_resolve using a series of
|
2010-10-22 17:04:22 -07:00
|
|
|
* js_LookupProperty calls.
|
2010-08-11 23:26:59 -07:00
|
|
|
*/
|
|
|
|
JSObject *pobj;
|
|
|
|
JSProperty *prop;
|
|
|
|
|
|
|
|
// length
|
2011-04-25 19:40:00 -07:00
|
|
|
if (!js_LookupProperty(cx, argsobj, ATOM_TO_JSID(cx->runtime->atomState.lengthAtom), &pobj, &prop))
|
2010-08-11 23:26:59 -07:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// callee
|
2011-04-25 19:40:00 -07:00
|
|
|
if (!js_LookupProperty(cx, argsobj, ATOM_TO_JSID(cx->runtime->atomState.calleeAtom), &pobj, &prop))
|
2010-08-11 23:26:59 -07:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// caller
|
2011-04-25 19:40:00 -07:00
|
|
|
if (!js_LookupProperty(cx, argsobj, ATOM_TO_JSID(cx->runtime->atomState.callerAtom), &pobj, &prop))
|
2010-08-11 23:26:59 -07:00
|
|
|
return false;
|
|
|
|
|
2011-04-25 19:40:00 -07:00
|
|
|
for (uint32 i = 0, argc = argsobj->initialLength(); i < argc; i++) {
|
|
|
|
if (!js_LookupProperty(cx, argsobj, INT_TO_JSID(i), &pobj, &prop))
|
2010-08-11 23:26:59 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-08-29 11:57:08 -07:00
|
|
|
static void
|
|
|
|
args_finalize(JSContext *cx, JSObject *obj)
|
|
|
|
{
|
2011-04-25 19:40:00 -07:00
|
|
|
cx->free_(reinterpret_cast<void *>(obj->asArguments()->data()));
|
2010-08-29 11:57:08 -07:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/*
|
2010-03-03 17:52:26 -08:00
|
|
|
* If a generator's arguments or call object escapes, and the generator frame
|
|
|
|
* is not executing, the generator object needs to be marked because it is not
|
|
|
|
* otherwise reachable. An executing generator is rooted by its invocation. To
|
|
|
|
* distinguish the two cases (which imply different access paths to the
|
|
|
|
* generator object), we use the JSFRAME_FLOATING_GENERATOR flag, which is only
|
2011-04-13 09:27:37 -07:00
|
|
|
* set on the StackFrame kept in the generator object's JSGenerator.
|
2007-03-22 10:30:00 -07:00
|
|
|
*/
|
2010-08-29 11:57:08 -07:00
|
|
|
static inline void
|
|
|
|
MaybeMarkGenerator(JSTracer *trc, JSObject *obj)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-08-29 11:57:08 -07:00
|
|
|
#if JS_HAS_GENERATORS
|
2011-04-13 09:27:37 -07:00
|
|
|
StackFrame *fp = (StackFrame *) obj->getPrivate();
|
2010-03-03 17:52:26 -08:00
|
|
|
if (fp && fp->isFloatingGenerator()) {
|
2010-08-29 11:57:08 -07:00
|
|
|
JSObject *genobj = js_FloatingFrameToGenerator(fp)->obj;
|
2010-08-09 22:43:33 -07:00
|
|
|
MarkObject(trc, *genobj, "generator object");
|
2007-07-16 13:57:16 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif
|
2010-08-29 11:57:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
args_trace(JSTracer *trc, JSObject *obj)
|
|
|
|
{
|
2011-04-25 19:40:00 -07:00
|
|
|
ArgumentsObject *argsobj = obj->asArguments();
|
|
|
|
if (argsobj->getPrivate() == JS_ARGUMENTS_OBJECT_ON_TRACE) {
|
|
|
|
JS_ASSERT(!argsobj->isStrictArguments());
|
2010-08-29 11:57:08 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-04-25 19:40:00 -07:00
|
|
|
ArgumentsData *data = argsobj->data();
|
2010-08-29 11:57:08 -07:00
|
|
|
if (data->callee.isObject())
|
2010-08-09 22:43:33 -07:00
|
|
|
MarkObject(trc, data->callee.toObject(), js_callee_str);
|
2011-04-25 19:40:00 -07:00
|
|
|
MarkValueRange(trc, argsobj->initialLength(), data->slots, js_arguments_str);
|
2010-08-29 11:57:08 -07:00
|
|
|
|
2011-04-25 19:40:00 -07:00
|
|
|
MaybeMarkGenerator(trc, argsobj);
|
2010-08-29 11:57:08 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-04-25 19:40:00 -07:00
|
|
|
namespace js {
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/*
|
2011-04-25 19:40:00 -07:00
|
|
|
* The classes below collaborate to lazily reflect and synchronize actual
|
|
|
|
* argument values, argument count, and callee function object stored in a
|
|
|
|
* StackFrame with their corresponding property values in the frame's
|
2007-03-22 10:30:00 -07:00
|
|
|
* arguments object.
|
|
|
|
*/
|
2011-04-25 19:40:00 -07:00
|
|
|
Class NormalArgumentsObject::jsClass = {
|
2010-07-13 14:39:03 -07:00
|
|
|
"Arguments",
|
2009-08-14 11:43:16 -07:00
|
|
|
JSCLASS_HAS_PRIVATE | JSCLASS_NEW_RESOLVE |
|
2011-04-25 19:40:00 -07:00
|
|
|
JSCLASS_HAS_RESERVED_SLOTS(RESERVED_SLOTS) |
|
2011-03-05 14:50:11 -08:00
|
|
|
JSCLASS_HAS_CACHED_PROTO(JSProto_Object),
|
2011-02-09 11:31:40 -08:00
|
|
|
PropertyStub, /* addProperty */
|
2010-06-12 09:29:04 -07:00
|
|
|
args_delProperty,
|
2011-02-09 11:31:40 -08:00
|
|
|
PropertyStub, /* getProperty */
|
|
|
|
StrictPropertyStub, /* setProperty */
|
2010-06-12 09:29:04 -07:00
|
|
|
args_enumerate,
|
2011-04-25 19:40:00 -07:00
|
|
|
reinterpret_cast<JSResolveOp>(args_resolve),
|
2010-06-12 09:29:04 -07:00
|
|
|
ConvertStub,
|
2011-02-09 11:31:40 -08:00
|
|
|
args_finalize, /* finalize */
|
|
|
|
NULL, /* reserved0 */
|
|
|
|
NULL, /* checkAccess */
|
|
|
|
NULL, /* call */
|
|
|
|
NULL, /* construct */
|
|
|
|
NULL, /* xdrObject */
|
|
|
|
NULL, /* hasInstance */
|
2011-03-05 14:50:11 -08:00
|
|
|
args_trace
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
2010-08-11 23:26:59 -07:00
|
|
|
/*
|
|
|
|
* Strict mode arguments is significantly less magical than non-strict mode
|
|
|
|
* arguments, so it is represented by a different class while sharing some
|
|
|
|
* functionality.
|
|
|
|
*/
|
2011-04-25 19:40:00 -07:00
|
|
|
Class StrictArgumentsObject::jsClass = {
|
2010-08-11 23:26:59 -07:00
|
|
|
"Arguments",
|
|
|
|
JSCLASS_HAS_PRIVATE | JSCLASS_NEW_RESOLVE |
|
2011-04-25 19:40:00 -07:00
|
|
|
JSCLASS_HAS_RESERVED_SLOTS(RESERVED_SLOTS) |
|
2011-03-05 14:50:11 -08:00
|
|
|
JSCLASS_HAS_CACHED_PROTO(JSProto_Object),
|
2011-02-09 11:31:40 -08:00
|
|
|
PropertyStub, /* addProperty */
|
2010-08-11 23:26:59 -07:00
|
|
|
args_delProperty,
|
2011-02-09 11:31:40 -08:00
|
|
|
PropertyStub, /* getProperty */
|
|
|
|
StrictPropertyStub, /* setProperty */
|
2010-08-11 23:26:59 -07:00
|
|
|
strictargs_enumerate,
|
|
|
|
reinterpret_cast<JSResolveOp>(strictargs_resolve),
|
|
|
|
ConvertStub,
|
2011-02-09 11:31:40 -08:00
|
|
|
args_finalize, /* finalize */
|
|
|
|
NULL, /* reserved0 */
|
|
|
|
NULL, /* checkAccess */
|
|
|
|
NULL, /* call */
|
|
|
|
NULL, /* construct */
|
|
|
|
NULL, /* xdrObject */
|
|
|
|
NULL, /* hasInstance */
|
2011-03-05 14:50:11 -08:00
|
|
|
args_trace
|
2010-08-11 23:26:59 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2009-06-04 18:58:47 -07:00
|
|
|
/*
|
2011-04-13 09:27:37 -07:00
|
|
|
* A Declarative Environment object stores its active StackFrame pointer in
|
2009-06-04 18:58:47 -07:00
|
|
|
* its private slot, just as Call and Arguments objects do.
|
|
|
|
*/
|
2010-07-14 23:19:36 -07:00
|
|
|
Class js_DeclEnvClass = {
|
2009-04-14 03:54:37 -07:00
|
|
|
js_Object_str,
|
2009-06-04 18:58:47 -07:00
|
|
|
JSCLASS_HAS_PRIVATE | JSCLASS_HAS_CACHED_PROTO(JSProto_Object),
|
2011-02-09 11:31:40 -08:00
|
|
|
PropertyStub, /* addProperty */
|
|
|
|
PropertyStub, /* delProperty */
|
|
|
|
PropertyStub, /* getProperty */
|
|
|
|
StrictPropertyStub, /* setProperty */
|
2010-06-12 09:29:04 -07:00
|
|
|
EnumerateStub,
|
|
|
|
ResolveStub,
|
|
|
|
ConvertStub
|
2009-04-14 03:54:37 -07:00
|
|
|
};
|
|
|
|
|
2009-06-25 12:12:20 -07:00
|
|
|
static JSBool
|
2010-07-14 23:19:36 -07:00
|
|
|
CheckForEscapingClosure(JSContext *cx, JSObject *obj, Value *vp)
|
2009-06-04 18:58:47 -07:00
|
|
|
{
|
2010-08-29 11:57:08 -07:00
|
|
|
JS_ASSERT(obj->isCall() || obj->getClass() == &js_DeclEnvClass);
|
2009-06-04 18:58:47 -07:00
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
const Value &v = *vp;
|
2009-06-04 18:58:47 -07:00
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
JSObject *funobj;
|
|
|
|
if (IsFunctionObject(v, &funobj)) {
|
2009-06-09 00:51:02 -07:00
|
|
|
JSFunction *fun = GET_FUNCTION_PRIVATE(cx, funobj);
|
2009-06-04 18:58:47 -07:00
|
|
|
|
2009-06-09 00:51:02 -07:00
|
|
|
/*
|
|
|
|
* Any escaping null or flat closure that reaches above itself or
|
|
|
|
* contains nested functions that reach above it must be wrapped.
|
|
|
|
* We can wrap only when this Call or Declarative Environment obj
|
|
|
|
* still has an active stack frame associated with it.
|
|
|
|
*/
|
|
|
|
if (fun->needsWrapper()) {
|
2010-01-22 14:49:18 -08:00
|
|
|
LeaveTrace(cx);
|
2009-06-25 12:12:20 -07:00
|
|
|
|
2011-04-13 09:27:37 -07:00
|
|
|
StackFrame *fp = (StackFrame *) obj->getPrivate();
|
2009-06-09 00:51:02 -07:00
|
|
|
if (fp) {
|
2010-08-12 05:07:20 -07:00
|
|
|
JSObject *wrapper = WrapEscapingClosure(cx, fp, fun);
|
2009-06-09 00:51:02 -07:00
|
|
|
if (!wrapper)
|
|
|
|
return false;
|
2010-07-14 23:19:36 -07:00
|
|
|
vp->setObject(*wrapper);
|
2009-06-09 00:51:02 -07:00
|
|
|
return true;
|
2009-06-04 18:58:47 -07:00
|
|
|
}
|
2009-06-09 00:51:02 -07:00
|
|
|
|
|
|
|
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
|
|
|
|
JSMSG_OPTIMIZED_CLOSURE_LEAK);
|
|
|
|
return false;
|
2009-06-04 18:58:47 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-06-25 12:12:20 -07:00
|
|
|
static JSBool
|
2010-07-14 23:19:36 -07:00
|
|
|
CalleeGetter(JSContext *cx, JSObject *obj, jsid id, Value *vp)
|
2009-06-04 18:58:47 -07:00
|
|
|
{
|
|
|
|
return CheckForEscapingClosure(cx, obj, vp);
|
|
|
|
}
|
|
|
|
|
2010-10-12 11:38:06 -07:00
|
|
|
/*
|
2011-03-14 11:30:36 -07:00
|
|
|
* Construct a call object for the given bindings. If this is a call object
|
|
|
|
* for a function invocation, callee should be the function being called.
|
|
|
|
* Otherwise it must be a call object for eval of strict mode code, and callee
|
|
|
|
* must be null.
|
2010-10-12 11:38:06 -07:00
|
|
|
*/
|
2011-03-14 11:30:36 -07:00
|
|
|
static JSObject *
|
|
|
|
NewCallObject(JSContext *cx, JSScript *script, JSObject &scopeChain, JSObject *callee)
|
2010-01-15 11:32:14 -08:00
|
|
|
{
|
2011-03-14 11:30:36 -07:00
|
|
|
Bindings &bindings = script->bindings;
|
|
|
|
size_t argsVars = bindings.countArgsAndVars();
|
2010-11-16 15:34:24 -08:00
|
|
|
size_t slots = JSObject::CALL_RESERVED_SLOTS + argsVars;
|
2011-05-16 22:59:40 -07:00
|
|
|
gc::FinalizeKind kind = gc::GetGCObjectKind(slots);
|
2010-10-13 11:49:22 -07:00
|
|
|
|
|
|
|
JSObject *callobj = js_NewGCObject(cx, kind);
|
2010-06-01 15:01:11 -07:00
|
|
|
if (!callobj)
|
|
|
|
return NULL;
|
|
|
|
|
2010-10-13 11:49:22 -07:00
|
|
|
/* Init immediately to avoid GC seeing a half-init'ed object. */
|
2011-03-15 12:18:36 -07:00
|
|
|
callobj->initCall(cx, bindings, &scopeChain);
|
2010-06-01 15:01:11 -07:00
|
|
|
|
2010-08-29 11:57:08 -07:00
|
|
|
/* This must come after callobj->lastProp has been set. */
|
2010-11-16 15:34:24 -08:00
|
|
|
if (!callobj->ensureInstanceReservedSlots(cx, argsVars))
|
2010-01-15 11:32:14 -08:00
|
|
|
return NULL;
|
2010-08-29 11:57:08 -07:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
for (Shape::Range r = callobj->lastProp; !r.empty(); r.popFront()) {
|
|
|
|
const Shape &s = r.front();
|
|
|
|
if (s.slot != SHAPE_INVALID_SLOT) {
|
2010-09-03 11:05:43 -07:00
|
|
|
JS_ASSERT(s.slot + 1 == callobj->slotSpan());
|
2010-08-29 11:57:08 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2010-08-09 22:43:33 -07:00
|
|
|
|
|
|
|
callobj->setCallObjCallee(callee);
|
2010-01-15 11:32:14 -08:00
|
|
|
return callobj;
|
|
|
|
}
|
|
|
|
|
2010-06-18 17:43:02 -07:00
|
|
|
static inline JSObject *
|
2011-04-13 09:27:37 -07:00
|
|
|
NewDeclEnvObject(JSContext *cx, StackFrame *fp)
|
2010-06-18 17:43:02 -07:00
|
|
|
{
|
2010-10-13 11:49:22 -07:00
|
|
|
JSObject *envobj = js_NewGCObject(cx, FINALIZE_OBJECT2);
|
2010-06-18 17:43:02 -07:00
|
|
|
if (!envobj)
|
|
|
|
return NULL;
|
|
|
|
|
2011-03-07 18:42:04 -08:00
|
|
|
EmptyShape *emptyDeclEnvShape = EmptyShape::getEmptyDeclEnvShape(cx);
|
|
|
|
if (!emptyDeclEnvShape)
|
2011-03-04 07:50:32 -08:00
|
|
|
return NULL;
|
2011-06-02 10:40:27 -07:00
|
|
|
envobj->init(cx, &js_DeclEnvClass, GetTypeEmpty(cx), &fp->scopeChain(), fp, false);
|
2011-03-07 18:42:04 -08:00
|
|
|
envobj->setMap(emptyDeclEnvShape);
|
2011-03-04 07:50:32 -08:00
|
|
|
|
2010-06-18 17:43:02 -07:00
|
|
|
return envobj;
|
|
|
|
}
|
|
|
|
|
2011-03-14 11:30:36 -07:00
|
|
|
namespace js {
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
JSObject *
|
2011-04-13 09:27:37 -07:00
|
|
|
CreateFunCallObject(JSContext *cx, StackFrame *fp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-03-14 11:30:36 -07:00
|
|
|
JS_ASSERT(fp->isNonEvalFunctionFrame());
|
|
|
|
JS_ASSERT(!fp->hasCallObj());
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-03-14 11:30:36 -07:00
|
|
|
JSObject *scopeChain = &fp->scopeChain();
|
|
|
|
JS_ASSERT_IF(scopeChain->isWith() || scopeChain->isBlock() || scopeChain->isCall(),
|
|
|
|
scopeChain->getPrivate() != fp);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-02-18 23:57:24 -08:00
|
|
|
/*
|
2011-03-14 11:30:36 -07:00
|
|
|
* For a named function expression Call's parent points to an environment
|
|
|
|
* object holding function's name.
|
2009-02-18 23:57:24 -08:00
|
|
|
*/
|
2011-03-14 11:30:36 -07:00
|
|
|
if (JSAtom *lambdaName = (fp->fun()->flags & JSFUN_LAMBDA) ? fp->fun()->atom : NULL) {
|
|
|
|
scopeChain = NewDeclEnvObject(cx, fp);
|
|
|
|
if (!scopeChain)
|
2009-04-22 23:22:42 -07:00
|
|
|
return NULL;
|
2009-04-16 15:26:26 -07:00
|
|
|
|
2011-01-21 05:10:16 -08:00
|
|
|
if (!DefineNativeProperty(cx, scopeChain, ATOM_TO_JSID(lambdaName),
|
|
|
|
ObjectValue(fp->callee()), CalleeGetter, NULL,
|
|
|
|
JSPROP_PERMANENT | JSPROP_READONLY, 0, 0)) {
|
2009-05-13 17:11:23 -07:00
|
|
|
return NULL;
|
|
|
|
}
|
2009-04-14 03:54:37 -07:00
|
|
|
}
|
2009-04-18 11:10:59 -07:00
|
|
|
|
2011-03-14 11:30:36 -07:00
|
|
|
JSObject *callobj = NewCallObject(cx, fp->script(), *scopeChain, &fp->callee());
|
2010-01-15 11:32:14 -08:00
|
|
|
if (!callobj)
|
2007-03-22 10:30:00 -07:00
|
|
|
return NULL;
|
2008-06-30 10:17:33 -07:00
|
|
|
|
2009-08-04 14:06:55 -07:00
|
|
|
callobj->setPrivate(fp);
|
2011-03-14 11:30:36 -07:00
|
|
|
fp->setScopeChainWithOwnCallObj(*callobj);
|
|
|
|
return callobj;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-03-14 11:30:36 -07:00
|
|
|
JSObject *
|
2011-04-13 09:27:37 -07:00
|
|
|
CreateEvalCallObject(JSContext *cx, StackFrame *fp)
|
2011-03-14 11:30:36 -07:00
|
|
|
{
|
|
|
|
JSObject *callobj = NewCallObject(cx, fp->script(), fp->scopeChain(), NULL);
|
|
|
|
if (!callobj)
|
2011-04-14 13:45:06 -07:00
|
|
|
return NULL;
|
2011-03-14 11:30:36 -07:00
|
|
|
|
|
|
|
callobj->setPrivate(fp);
|
|
|
|
fp->setScopeChainWithOwnCallObj(*callobj);
|
2007-03-22 10:30:00 -07:00
|
|
|
return callobj;
|
|
|
|
}
|
|
|
|
|
2011-03-14 11:30:36 -07:00
|
|
|
} // namespace js
|
|
|
|
|
2010-01-15 11:32:14 -08:00
|
|
|
JSObject * JS_FASTCALL
|
|
|
|
js_CreateCallObjectOnTrace(JSContext *cx, JSFunction *fun, JSObject *callee, JSObject *scopeChain)
|
|
|
|
{
|
|
|
|
JS_ASSERT(!js_IsNamedLambda(fun));
|
2010-08-09 22:43:33 -07:00
|
|
|
JS_ASSERT(scopeChain);
|
2010-10-12 11:38:06 -07:00
|
|
|
JS_ASSERT(callee);
|
2011-03-14 11:30:36 -07:00
|
|
|
return NewCallObject(cx, fun->script(), *scopeChain, callee);
|
2010-01-15 11:32:14 -08:00
|
|
|
}
|
|
|
|
|
2010-03-01 18:47:17 -08:00
|
|
|
JS_DEFINE_CALLINFO_4(extern, OBJECT, js_CreateCallObjectOnTrace, CONTEXT, FUNCTION, OBJECT, OBJECT,
|
2010-07-27 22:33:42 -07:00
|
|
|
0, nanojit::ACCSET_STORE_ANY)
|
2010-01-15 11:32:14 -08:00
|
|
|
|
|
|
|
inline static void
|
2010-08-09 22:43:33 -07:00
|
|
|
CopyValuesToCallObject(JSObject &callobj, uintN nargs, Value *argv, uintN nvars, Value *slots)
|
2010-01-15 11:32:14 -08:00
|
|
|
{
|
2010-10-13 11:49:22 -07:00
|
|
|
JS_ASSERT(callobj.numSlots() >= JSObject::CALL_RESERVED_SLOTS + nargs + nvars);
|
2011-04-08 19:51:40 -07:00
|
|
|
callobj.copySlotRange(JSObject::CALL_RESERVED_SLOTS, argv, nargs);
|
|
|
|
callobj.copySlotRange(JSObject::CALL_RESERVED_SLOTS + nargs, slots, nvars);
|
2010-01-15 11:32:14 -08:00
|
|
|
}
|
|
|
|
|
2009-08-14 11:43:16 -07:00
|
|
|
void
|
2011-04-13 09:27:37 -07:00
|
|
|
js_PutCallObject(StackFrame *fp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-08-09 22:43:33 -07:00
|
|
|
JSObject &callobj = fp->callObj();
|
2011-03-14 11:30:36 -07:00
|
|
|
JS_ASSERT(callobj.getPrivate() == fp);
|
|
|
|
JS_ASSERT_IF(fp->isEvalFrame(), fp->isStrictEvalFrame());
|
2011-01-05 12:54:04 -08:00
|
|
|
JS_ASSERT(fp->isEvalFrame() == callobj.callIsForEval());
|
|
|
|
|
2009-08-14 11:43:16 -07:00
|
|
|
/* Get the arguments object to snapshot fp's actual argument values. */
|
2010-08-12 15:46:03 -07:00
|
|
|
if (fp->hasArgsObj()) {
|
2010-08-09 22:43:33 -07:00
|
|
|
if (!fp->hasOverriddenArgs())
|
|
|
|
callobj.setCallObjArguments(ObjectValue(fp->argsObj()));
|
2011-04-13 09:27:37 -07:00
|
|
|
js_PutArgsObject(fp);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-01-05 11:55:18 -08:00
|
|
|
JSScript *script = fp->script();
|
|
|
|
Bindings &bindings = script->bindings;
|
2010-11-16 15:34:24 -08:00
|
|
|
|
2011-01-05 12:54:04 -08:00
|
|
|
if (callobj.callIsForEval()) {
|
|
|
|
JS_ASSERT(script->strictModeCode);
|
|
|
|
JS_ASSERT(bindings.countArgs() == 0);
|
2009-08-04 03:55:58 -07:00
|
|
|
|
2011-01-05 12:54:04 -08:00
|
|
|
/* This could be optimized as below, but keep it simple for now. */
|
|
|
|
CopyValuesToCallObject(callobj, 0, NULL, bindings.countVars(), fp->slots());
|
|
|
|
} else {
|
2011-01-05 11:55:18 -08:00
|
|
|
JSFunction *fun = fp->fun();
|
2011-01-05 12:54:04 -08:00
|
|
|
JS_ASSERT(fun == callobj.getCallObjCalleeFunction());
|
2011-01-05 11:55:18 -08:00
|
|
|
JS_ASSERT(script == fun->script());
|
2010-08-27 15:54:30 -07:00
|
|
|
|
2011-01-10 20:33:53 -08:00
|
|
|
uintN n = bindings.countArgsAndVars();
|
|
|
|
if (n > 0) {
|
2011-01-05 11:55:18 -08:00
|
|
|
JS_ASSERT(JSObject::CALL_RESERVED_SLOTS + n <= callobj.numSlots());
|
2010-08-27 15:54:30 -07:00
|
|
|
|
2011-01-05 11:55:18 -08:00
|
|
|
uint32 nvars = bindings.countVars();
|
|
|
|
uint32 nargs = bindings.countArgs();
|
|
|
|
JS_ASSERT(fun->nargs == nargs);
|
|
|
|
JS_ASSERT(nvars + nargs == n);
|
|
|
|
|
|
|
|
JSScript *script = fun->script();
|
|
|
|
if (script->usesEval
|
2010-10-02 01:21:17 -07:00
|
|
|
#ifdef JS_METHODJIT
|
2011-01-05 11:55:18 -08:00
|
|
|
|| script->debugMode
|
2010-10-02 01:21:17 -07:00
|
|
|
#endif
|
2011-01-05 11:55:18 -08:00
|
|
|
) {
|
|
|
|
CopyValuesToCallObject(callobj, nargs, fp->formalArgs(), nvars, fp->slots());
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* For each arg & var that is closed over, copy it from the stack
|
|
|
|
* into the call object.
|
|
|
|
*/
|
|
|
|
uint32 nclosed = script->nClosedArgs;
|
|
|
|
for (uint32 i = 0; i < nclosed; i++) {
|
|
|
|
uint32 e = script->getClosedArg(i);
|
|
|
|
callobj.setSlot(JSObject::CALL_RESERVED_SLOTS + e, fp->formalArg(e));
|
|
|
|
}
|
2010-09-13 22:33:44 -07:00
|
|
|
|
2011-01-05 11:55:18 -08:00
|
|
|
nclosed = script->nClosedVars;
|
|
|
|
for (uint32 i = 0; i < nclosed; i++) {
|
|
|
|
uint32 e = script->getClosedVar(i);
|
|
|
|
callobj.setSlot(JSObject::CALL_RESERVED_SLOTS + nargs + e, fp->slots()[e]);
|
|
|
|
}
|
2010-08-27 15:54:30 -07:00
|
|
|
}
|
|
|
|
}
|
2008-06-30 10:17:33 -07:00
|
|
|
|
2011-01-05 11:55:18 -08:00
|
|
|
/* Clear private pointers to fp, which is about to go away (js_Invoke). */
|
|
|
|
if (js_IsNamedLambda(fun)) {
|
|
|
|
JSObject *env = callobj.getParent();
|
2009-06-04 18:58:47 -07:00
|
|
|
|
2011-01-05 11:55:18 -08:00
|
|
|
JS_ASSERT(env->getClass() == &js_DeclEnvClass);
|
|
|
|
JS_ASSERT(env->getPrivate() == fp);
|
|
|
|
env->setPrivate(NULL);
|
|
|
|
}
|
2009-06-04 18:58:47 -07:00
|
|
|
}
|
|
|
|
|
2010-08-09 22:43:33 -07:00
|
|
|
callobj.setPrivate(NULL);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-01-15 11:32:14 -08:00
|
|
|
JSBool JS_FASTCALL
|
2011-04-13 09:27:37 -07:00
|
|
|
js_PutCallObjectOnTrace(JSObject *callobj, uint32 nargs, Value *argv,
|
2010-07-14 23:19:36 -07:00
|
|
|
uint32 nvars, Value *slots)
|
2010-01-15 11:32:14 -08:00
|
|
|
{
|
2010-08-09 22:43:33 -07:00
|
|
|
JS_ASSERT(callobj->isCall());
|
|
|
|
JS_ASSERT(!callobj->getPrivate());
|
2010-01-15 11:32:14 -08:00
|
|
|
|
|
|
|
uintN n = nargs + nvars;
|
|
|
|
if (n != 0)
|
2010-08-09 22:43:33 -07:00
|
|
|
CopyValuesToCallObject(*callobj, nargs, argv, nvars, slots);
|
2010-01-15 11:32:14 -08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-04-13 09:27:37 -07:00
|
|
|
JS_DEFINE_CALLINFO_5(extern, BOOL, js_PutCallObjectOnTrace, OBJECT, UINT32, VALUEPTR,
|
2010-07-27 22:33:42 -07:00
|
|
|
UINT32, VALUEPTR, 0, nanojit::ACCSET_STORE_ANY)
|
2010-01-15 11:32:14 -08:00
|
|
|
|
2010-12-29 20:21:18 -08:00
|
|
|
namespace js {
|
2008-03-29 08:48:41 -07:00
|
|
|
|
|
|
|
static JSBool
|
2010-12-29 20:21:18 -08:00
|
|
|
GetCallArguments(JSContext *cx, JSObject *obj, jsid id, Value *vp)
|
2008-03-29 08:48:41 -07:00
|
|
|
{
|
2011-04-13 09:27:37 -07:00
|
|
|
StackFrame *fp = obj->maybeCallObjStackFrame();
|
2010-12-29 20:21:18 -08:00
|
|
|
if (fp && !fp->hasOverriddenArgs()) {
|
|
|
|
JSObject *argsobj = js_GetArgsObject(cx, fp);
|
|
|
|
if (!argsobj)
|
|
|
|
return false;
|
|
|
|
vp->setObject(*argsobj);
|
2009-02-25 11:09:21 -08:00
|
|
|
} else {
|
2010-12-29 20:21:18 -08:00
|
|
|
*vp = obj->getCallObjArguments();
|
2009-02-25 11:09:21 -08:00
|
|
|
}
|
2010-03-02 17:30:54 -08:00
|
|
|
return true;
|
2008-03-29 08:48:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2011-02-09 11:31:40 -08:00
|
|
|
SetCallArguments(JSContext *cx, JSObject *obj, jsid id, JSBool strict, Value *vp)
|
2008-03-29 08:48:41 -07:00
|
|
|
{
|
2011-04-13 09:27:37 -07:00
|
|
|
if (StackFrame *fp = obj->maybeCallObjStackFrame())
|
2010-12-29 20:21:18 -08:00
|
|
|
fp->setOverriddenArgs();
|
|
|
|
obj->setCallObjArguments(*vp);
|
|
|
|
return true;
|
2008-03-29 08:48:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
JSBool
|
2010-11-16 15:30:57 -08:00
|
|
|
GetCallArg(JSContext *cx, JSObject *obj, jsid id, Value *vp)
|
2008-03-29 08:48:41 -07:00
|
|
|
{
|
2010-12-29 20:21:18 -08:00
|
|
|
JS_ASSERT((int16) JSID_TO_INT(id) == JSID_TO_INT(id));
|
|
|
|
uintN i = (uint16) JSID_TO_INT(id);
|
|
|
|
|
2011-04-13 09:27:37 -07:00
|
|
|
if (StackFrame *fp = obj->maybeCallObjStackFrame())
|
2010-12-29 20:21:18 -08:00
|
|
|
*vp = fp->formalArg(i);
|
|
|
|
else
|
|
|
|
*vp = obj->callObjArg(i);
|
|
|
|
return true;
|
2008-03-29 08:48:41 -07:00
|
|
|
}
|
|
|
|
|
2009-07-27 18:13:53 -07:00
|
|
|
JSBool
|
2011-02-09 11:31:40 -08:00
|
|
|
SetCallArg(JSContext *cx, JSObject *obj, jsid id, JSBool strict, Value *vp)
|
2008-03-29 08:48:41 -07:00
|
|
|
{
|
2010-12-29 20:21:18 -08:00
|
|
|
JS_ASSERT((int16) JSID_TO_INT(id) == JSID_TO_INT(id));
|
|
|
|
uintN i = (uint16) JSID_TO_INT(id);
|
|
|
|
|
|
|
|
Value *argp;
|
2011-04-13 09:27:37 -07:00
|
|
|
if (StackFrame *fp = obj->maybeCallObjStackFrame())
|
2010-12-29 20:21:18 -08:00
|
|
|
argp = &fp->formalArg(i);
|
|
|
|
else
|
|
|
|
argp = &obj->callObjArg(i);
|
|
|
|
|
2011-03-03 14:07:48 -08:00
|
|
|
JSScript *script = obj->getCallObjCalleeFunction()->script();
|
2011-05-09 07:12:47 -07:00
|
|
|
script->typeSetArgument(cx, i, *vp);
|
2011-02-24 21:21:43 -08:00
|
|
|
|
2011-06-01 17:48:52 -07:00
|
|
|
GCPoke(cx, *argp);
|
2010-12-29 20:21:18 -08:00
|
|
|
*argp = *vp;
|
|
|
|
return true;
|
2010-03-02 17:30:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
JSBool
|
2010-12-29 23:46:50 -08:00
|
|
|
GetCallUpvar(JSContext *cx, JSObject *obj, jsid id, Value *vp)
|
2010-03-02 17:30:54 -08:00
|
|
|
{
|
2010-12-29 20:21:18 -08:00
|
|
|
JS_ASSERT((int16) JSID_TO_INT(id) == JSID_TO_INT(id));
|
|
|
|
uintN i = (uint16) JSID_TO_INT(id);
|
|
|
|
|
2010-12-29 23:46:50 -08:00
|
|
|
*vp = obj->getCallObjCallee()->getFlatClosureUpvar(i);
|
2010-12-29 20:21:18 -08:00
|
|
|
return true;
|
2010-03-02 17:30:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
JSBool
|
2011-02-09 11:31:40 -08:00
|
|
|
SetCallUpvar(JSContext *cx, JSObject *obj, jsid id, JSBool strict, Value *vp)
|
2010-03-02 17:30:54 -08:00
|
|
|
{
|
2010-12-29 20:21:18 -08:00
|
|
|
JS_ASSERT((int16) JSID_TO_INT(id) == JSID_TO_INT(id));
|
|
|
|
uintN i = (uint16) JSID_TO_INT(id);
|
|
|
|
|
2010-12-29 23:46:50 -08:00
|
|
|
Value *up = &obj->getCallObjCallee()->getFlatClosureUpvar(i);
|
2010-10-12 11:38:06 -07:00
|
|
|
|
2011-06-01 17:48:52 -07:00
|
|
|
GCPoke(cx, *up);
|
2010-12-29 23:46:50 -08:00
|
|
|
*up = *vp;
|
2010-12-29 20:21:18 -08:00
|
|
|
return true;
|
2008-03-29 08:48:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
JSBool
|
2010-11-16 15:30:57 -08:00
|
|
|
GetCallVar(JSContext *cx, JSObject *obj, jsid id, Value *vp)
|
2009-06-09 00:51:02 -07:00
|
|
|
{
|
2010-12-29 20:21:18 -08:00
|
|
|
JS_ASSERT((int16) JSID_TO_INT(id) == JSID_TO_INT(id));
|
|
|
|
uintN i = (uint16) JSID_TO_INT(id);
|
|
|
|
|
2011-04-13 09:27:37 -07:00
|
|
|
if (StackFrame *fp = obj->maybeCallObjStackFrame())
|
2010-12-29 20:21:18 -08:00
|
|
|
*vp = fp->varSlot(i);
|
|
|
|
else
|
|
|
|
*vp = obj->callObjVar(i);
|
|
|
|
|
|
|
|
return true;
|
2009-06-09 00:51:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
JSBool
|
2010-11-16 15:30:57 -08:00
|
|
|
GetCallVarChecked(JSContext *cx, JSObject *obj, jsid id, Value *vp)
|
2008-03-29 08:48:41 -07:00
|
|
|
{
|
2010-12-29 20:21:18 -08:00
|
|
|
if (!GetCallVar(cx, obj, id, vp))
|
2010-03-02 17:30:54 -08:00
|
|
|
return false;
|
2009-06-04 18:58:47 -07:00
|
|
|
|
|
|
|
return CheckForEscapingClosure(cx, obj, vp);
|
2008-03-29 08:48:41 -07:00
|
|
|
}
|
|
|
|
|
2009-07-27 18:13:53 -07:00
|
|
|
JSBool
|
2011-02-09 11:31:40 -08:00
|
|
|
SetCallVar(JSContext *cx, JSObject *obj, jsid id, JSBool strict, Value *vp)
|
2008-03-29 08:48:41 -07:00
|
|
|
{
|
2010-12-29 20:21:18 -08:00
|
|
|
JS_ASSERT(obj->isCall());
|
|
|
|
|
|
|
|
JS_ASSERT((int16) JSID_TO_INT(id) == JSID_TO_INT(id));
|
|
|
|
uintN i = (uint16) JSID_TO_INT(id);
|
|
|
|
|
2011-01-05 15:21:59 -08:00
|
|
|
/*
|
|
|
|
* As documented in TraceRecorder::attemptTreeCall(), when recording an
|
|
|
|
* inner tree call, the recorder assumes the inner tree does not mutate
|
|
|
|
* any tracked upvars. The abort here is a pessimistic precaution against
|
|
|
|
* bug 620662, where an inner tree setting a closed stack variable in an
|
|
|
|
* outer tree is illegal, and runtime would fall off trace.
|
|
|
|
*/
|
|
|
|
#ifdef JS_TRACER
|
2011-02-01 10:18:06 -08:00
|
|
|
if (JS_ON_TRACE(cx)) {
|
|
|
|
TraceMonitor *tm = JS_TRACE_MONITOR_ON_TRACE(cx);
|
|
|
|
if (tm->recorder && tm->tracecx)
|
|
|
|
AbortRecording(cx, "upvar write in nested tree");
|
|
|
|
}
|
2011-01-05 15:21:59 -08:00
|
|
|
#endif
|
|
|
|
|
2010-12-29 20:21:18 -08:00
|
|
|
Value *varp;
|
2011-04-13 09:27:37 -07:00
|
|
|
if (StackFrame *fp = obj->maybeCallObjStackFrame())
|
2010-12-29 20:21:18 -08:00
|
|
|
varp = &fp->varSlot(i);
|
|
|
|
else
|
|
|
|
varp = &obj->callObjVar(i);
|
|
|
|
|
2011-03-03 14:07:48 -08:00
|
|
|
JSScript *script = obj->getCallObjCalleeFunction()->script();
|
2011-05-09 07:12:47 -07:00
|
|
|
script->typeSetLocal(cx, i, *vp);
|
2011-02-24 21:21:43 -08:00
|
|
|
|
2011-06-01 17:48:52 -07:00
|
|
|
GCPoke(cx, *varp);
|
2010-12-29 20:21:18 -08:00
|
|
|
*varp = *vp;
|
|
|
|
return true;
|
2008-03-29 08:48:41 -07:00
|
|
|
}
|
|
|
|
|
2010-11-16 15:30:57 -08:00
|
|
|
} // namespace js
|
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
#if JS_TRACER
|
2009-07-27 18:13:53 -07:00
|
|
|
JSBool JS_FASTCALL
|
2010-07-14 23:19:36 -07:00
|
|
|
js_SetCallArg(JSContext *cx, JSObject *obj, jsid slotid, ValueArgType arg)
|
2009-07-27 18:13:53 -07:00
|
|
|
{
|
2010-07-14 23:19:36 -07:00
|
|
|
Value argcopy = ValueArgToConstRef(arg);
|
2011-02-09 11:31:40 -08:00
|
|
|
return SetCallArg(cx, obj, slotid, false /* STRICT DUMMY */, &argcopy);
|
2009-07-27 18:13:53 -07:00
|
|
|
}
|
2010-07-14 23:19:36 -07:00
|
|
|
JS_DEFINE_CALLINFO_4(extern, BOOL, js_SetCallArg, CONTEXT, OBJECT, JSID, VALUE, 0,
|
2010-07-27 22:33:42 -07:00
|
|
|
nanojit::ACCSET_STORE_ANY)
|
2009-07-27 18:13:53 -07:00
|
|
|
|
|
|
|
JSBool JS_FASTCALL
|
2010-07-14 23:19:36 -07:00
|
|
|
js_SetCallVar(JSContext *cx, JSObject *obj, jsid slotid, ValueArgType arg)
|
2009-07-27 18:13:53 -07:00
|
|
|
{
|
2010-07-14 23:19:36 -07:00
|
|
|
Value argcopy = ValueArgToConstRef(arg);
|
2011-02-09 11:31:40 -08:00
|
|
|
return SetCallVar(cx, obj, slotid, false /* STRICT DUMMY */, &argcopy);
|
2009-07-27 18:13:53 -07:00
|
|
|
}
|
2010-07-14 23:19:36 -07:00
|
|
|
JS_DEFINE_CALLINFO_4(extern, BOOL, js_SetCallVar, CONTEXT, OBJECT, JSID, VALUE, 0,
|
2010-07-27 22:33:42 -07:00
|
|
|
nanojit::ACCSET_STORE_ANY)
|
2010-07-14 23:19:36 -07:00
|
|
|
#endif
|
2009-07-27 18:13:53 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
static JSBool
|
2010-07-14 23:19:36 -07:00
|
|
|
call_resolve(JSContext *cx, JSObject *obj, jsid id, uintN flags,
|
2007-03-22 10:30:00 -07:00
|
|
|
JSObject **objp)
|
|
|
|
{
|
2010-08-29 11:57:08 -07:00
|
|
|
JS_ASSERT(obj->isCall());
|
2010-03-04 20:44:09 -08:00
|
|
|
JS_ASSERT(!obj->getProto());
|
2009-05-15 02:43:19 -07:00
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
if (!JSID_IS_ATOM(id))
|
2010-10-12 11:38:06 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-10-12 11:38:06 -07:00
|
|
|
JSObject *callee = obj->getCallObjCallee();
|
2010-08-29 11:57:08 -07:00
|
|
|
#ifdef DEBUG
|
2010-10-12 11:38:06 -07:00
|
|
|
if (callee) {
|
|
|
|
JSScript *script = callee->getFunctionPrivate()->script();
|
|
|
|
JS_ASSERT(!script->bindings.hasBinding(cx, JSID_TO_ATOM(id)));
|
|
|
|
}
|
2010-08-29 11:57:08 -07:00
|
|
|
#endif
|
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.
|
2010-10-07 13:52:58 -07:00
|
|
|
*
|
|
|
|
* Include JSPROP_ENUMERATE for consistency with all other Call object
|
2010-11-16 15:34:24 -08:00
|
|
|
* properties; see js::Bindings::add and js::Interpret's JSOP_DEFFUN
|
2010-10-07 13:52:58 -07:00
|
|
|
* rebinding-Call-property logic.
|
2007-12-11 13:40:51 -08:00
|
|
|
*/
|
2010-10-12 11:38:06 -07:00
|
|
|
if (callee && id == ATOM_TO_JSID(cx->runtime->atomState.argumentsAtom)) {
|
2011-01-21 05:10:16 -08:00
|
|
|
if (!DefineNativeProperty(cx, obj, id, UndefinedValue(),
|
|
|
|
GetCallArguments, SetCallArguments,
|
|
|
|
JSPROP_PERMANENT | JSPROP_SHARED | JSPROP_ENUMERATE,
|
|
|
|
0, 0, DNP_DONT_PURGE)) {
|
2010-10-12 11:38:06 -07:00
|
|
|
return false;
|
2007-06-15 17:29:21 -07:00
|
|
|
}
|
2007-12-11 13:40:51 -08:00
|
|
|
*objp = obj;
|
2010-10-12 11:38:06 -07:00
|
|
|
return true;
|
2007-06-15 17:29:21 -07:00
|
|
|
}
|
2009-04-05 21:17:22 -07:00
|
|
|
|
|
|
|
/* Control flow reaches here only if id was not resolved. */
|
2010-10-12 11:38:06 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-08-29 11:57:08 -07:00
|
|
|
static void
|
|
|
|
call_trace(JSTracer *trc, JSObject *obj)
|
|
|
|
{
|
|
|
|
JS_ASSERT(obj->isCall());
|
2011-04-13 09:27:37 -07:00
|
|
|
if (StackFrame *fp = obj->maybeCallObjStackFrame()) {
|
2010-09-13 18:34:11 -07:00
|
|
|
/*
|
|
|
|
* FIXME: Hide copies of stack values rooted by fp from the Cycle
|
|
|
|
* Collector, which currently lacks a non-stub Unlink implementation
|
|
|
|
* for JS objects (including Call objects), so is unable to collect
|
|
|
|
* cycles involving Call objects whose frames are active without this
|
|
|
|
* hiding hack.
|
|
|
|
*/
|
2010-10-13 11:49:22 -07:00
|
|
|
uintN first = JSObject::CALL_RESERVED_SLOTS;
|
2011-01-05 11:55:18 -08:00
|
|
|
uintN count = fp->script()->bindings.countArgsAndVars();
|
2010-09-13 18:34:11 -07:00
|
|
|
|
2010-10-13 11:49:22 -07:00
|
|
|
JS_ASSERT(obj->numSlots() >= first + count);
|
2011-04-08 19:51:40 -07:00
|
|
|
obj->clearSlotRange(first, count);
|
2010-09-13 18:34:11 -07:00
|
|
|
}
|
2010-08-29 11:57:08 -07:00
|
|
|
|
|
|
|
MaybeMarkGenerator(trc, obj);
|
|
|
|
}
|
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
JS_PUBLIC_DATA(Class) js_CallClass = {
|
2009-04-18 11:10:59 -07:00
|
|
|
"Call",
|
2008-06-30 10:17:33 -07:00
|
|
|
JSCLASS_HAS_PRIVATE |
|
2010-08-09 22:43:33 -07:00
|
|
|
JSCLASS_HAS_RESERVED_SLOTS(JSObject::CALL_RESERVED_SLOTS) |
|
2011-03-05 14:50:11 -08:00
|
|
|
JSCLASS_NEW_RESOLVE | JSCLASS_IS_ANONYMOUS,
|
2011-02-09 11:31:40 -08:00
|
|
|
PropertyStub, /* addProperty */
|
|
|
|
PropertyStub, /* delProperty */
|
|
|
|
PropertyStub, /* getProperty */
|
|
|
|
StrictPropertyStub, /* setProperty */
|
2010-08-29 11:57:08 -07:00
|
|
|
JS_EnumerateStub,
|
2010-06-12 09:29:04 -07:00
|
|
|
(JSResolveOp)call_resolve,
|
2011-02-09 11:31:40 -08:00
|
|
|
NULL, /* convert: Leave it NULL so we notice if calls ever escape */
|
|
|
|
NULL, /* finalize */
|
|
|
|
NULL, /* reserved0 */
|
|
|
|
NULL, /* checkAccess */
|
|
|
|
NULL, /* call */
|
|
|
|
NULL, /* construct */
|
|
|
|
NULL, /* xdrObject */
|
|
|
|
NULL, /* hasInstance */
|
2011-03-05 14:50:11 -08:00
|
|
|
call_trace
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
2010-07-23 14:41:56 -07:00
|
|
|
bool
|
2011-04-13 09:27:37 -07:00
|
|
|
StackFrame::getValidCalleeObject(JSContext *cx, Value *vp)
|
2010-07-23 14:41:56 -07:00
|
|
|
{
|
2010-08-09 22:43:33 -07:00
|
|
|
if (!isFunctionFrame()) {
|
|
|
|
vp->setUndefined();
|
2010-07-23 14:41:56 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-08-09 22:43:33 -07:00
|
|
|
JSFunction *fun = this->fun();
|
2010-08-15 05:44:51 -07:00
|
|
|
|
2010-07-23 14:41:56 -07:00
|
|
|
/*
|
2010-08-29 11:57:08 -07:00
|
|
|
* See the equivalent condition in ArgGetter for the 'callee' id case, but
|
2010-07-23 14:41:56 -07:00
|
|
|
* note that here we do not want to throw, since this escape can happen via
|
|
|
|
* a foo.caller reference alone, without any debugger or indirect eval. And
|
|
|
|
* alas, it seems foo.caller is still used on the Web.
|
|
|
|
*/
|
|
|
|
if (fun->needsWrapper()) {
|
2010-08-12 05:07:20 -07:00
|
|
|
JSObject *wrapper = WrapEscapingClosure(cx, this, fun);
|
2010-07-23 14:41:56 -07:00
|
|
|
if (!wrapper)
|
|
|
|
return false;
|
|
|
|
vp->setObject(*wrapper);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-08-09 22:43:33 -07:00
|
|
|
JSObject &funobj = callee();
|
|
|
|
vp->setObject(funobj);
|
2010-07-23 14:41:56 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Check for an escape attempt by a joined function object, which must go
|
|
|
|
* through the frame's |this| object's method read barrier for the method
|
|
|
|
* atom by which it was uniquely associated with a property.
|
|
|
|
*/
|
2010-08-09 22:43:33 -07:00
|
|
|
const Value &thisv = functionThis();
|
|
|
|
if (thisv.isObject()) {
|
|
|
|
JS_ASSERT(funobj.getFunctionPrivate() == fun);
|
2010-07-23 14:41:56 -07:00
|
|
|
|
2011-04-08 10:52:48 -07:00
|
|
|
if (fun->compiledFunObj() == funobj && fun->methodAtom()) {
|
2010-08-09 22:43:33 -07:00
|
|
|
JSObject *thisp = &thisv.toObject();
|
2011-01-04 12:27:18 -08:00
|
|
|
JSObject *first_barriered_thisp = NULL;
|
2010-07-23 14:41:56 -07:00
|
|
|
|
2011-01-04 12:27:18 -08:00
|
|
|
do {
|
2010-07-23 14:41:56 -07:00
|
|
|
/*
|
2011-01-04 12:27:18 -08:00
|
|
|
* While a non-native object is responsible for handling its
|
|
|
|
* entire prototype chain, notable non-natives including dense
|
|
|
|
* and typed arrays have native prototypes, so keep going.
|
2010-07-23 14:41:56 -07:00
|
|
|
*/
|
2011-01-04 12:27:18 -08:00
|
|
|
if (!thisp->isNative())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (thisp->hasMethodBarrier()) {
|
|
|
|
const Shape *shape = thisp->nativeLookup(ATOM_TO_JSID(fun->methodAtom()));
|
|
|
|
if (shape) {
|
|
|
|
/*
|
|
|
|
* Two cases follow: the method barrier was not crossed
|
|
|
|
* yet, so we cross it here; the method barrier *was*
|
|
|
|
* crossed but after the call, in which case we fetch
|
|
|
|
* and validate the cloned (unjoined) funobj from the
|
|
|
|
* method property's slot.
|
|
|
|
*
|
|
|
|
* In either case we must allow for the method property
|
|
|
|
* to have been replaced, or its value overwritten.
|
|
|
|
*/
|
2011-04-08 10:52:48 -07:00
|
|
|
if (shape->isMethod() && shape->methodObject() == funobj) {
|
2011-01-04 12:27:18 -08:00
|
|
|
if (!thisp->methodReadBarrier(cx, *shape, vp))
|
|
|
|
return false;
|
2011-04-08 10:52:48 -07:00
|
|
|
calleev().setObject(vp->toObject());
|
2010-07-23 14:41:56 -07:00
|
|
|
return true;
|
|
|
|
}
|
2011-01-04 12:27:18 -08:00
|
|
|
|
|
|
|
if (shape->hasSlot()) {
|
|
|
|
Value v = thisp->getSlot(shape->slot);
|
|
|
|
JSObject *clone;
|
|
|
|
|
|
|
|
if (IsFunctionObject(v, &clone) &&
|
|
|
|
GET_FUNCTION_PRIVATE(cx, clone) == fun &&
|
|
|
|
clone->hasMethodObj(*thisp)) {
|
2011-06-03 20:48:16 -07:00
|
|
|
/*
|
|
|
|
* N.B. If the method barrier was on a function
|
|
|
|
* with singleton type, then while crossing the
|
|
|
|
* method barrier CloneFunctionObject will have
|
|
|
|
* ignored the attempt to clone the function.
|
|
|
|
*/
|
2011-03-27 18:25:05 -07:00
|
|
|
JS_ASSERT_IF(!clone->getType()->singleton, clone != &funobj);
|
2011-01-04 12:27:18 -08:00
|
|
|
*vp = v;
|
2011-04-08 10:52:48 -07:00
|
|
|
calleev().setObject(*clone);
|
2011-01-04 12:27:18 -08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2010-07-23 14:41:56 -07:00
|
|
|
}
|
2011-01-04 12:27:18 -08:00
|
|
|
|
|
|
|
if (!first_barriered_thisp)
|
|
|
|
first_barriered_thisp = thisp;
|
2010-07-23 14:41:56 -07:00
|
|
|
}
|
2011-01-04 12:27:18 -08:00
|
|
|
} while ((thisp = thisp->getProto()) != NULL);
|
2010-07-23 14:41:56 -07:00
|
|
|
|
2011-01-04 12:27:18 -08:00
|
|
|
if (!first_barriered_thisp)
|
2010-07-23 14:41:56 -07:00
|
|
|
return true;
|
2011-01-04 12:27:18 -08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* At this point, we couldn't find an already-existing clone (or
|
|
|
|
* force to exist a fresh clone) created via thisp's method read
|
|
|
|
* barrier, so we must clone fun and store it in fp's callee to
|
|
|
|
* avoid re-cloning upon repeated foo.caller access.
|
|
|
|
*
|
|
|
|
* This must mean the code in js_DeleteProperty could not find this
|
|
|
|
* stack frame on the stack when the method was deleted. We've lost
|
|
|
|
* track of the method, so we associate it with the first barriered
|
|
|
|
* object found starting from thisp on the prototype chain.
|
|
|
|
*/
|
2011-06-03 20:48:16 -07:00
|
|
|
JSObject *newfunobj = CloneFunctionObject(cx, fun, fun->getParent(), true);
|
2011-01-04 12:27:18 -08:00
|
|
|
if (!newfunobj)
|
|
|
|
return false;
|
|
|
|
newfunobj->setMethodObj(*first_barriered_thisp);
|
2011-04-08 10:52:48 -07:00
|
|
|
calleev().setObject(*newfunobj);
|
2011-01-04 12:27:18 -08:00
|
|
|
vp->setObject(*newfunobj);
|
|
|
|
return true;
|
2010-07-23 14:41:56 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-08-14 11:43:16 -07:00
|
|
|
/* Generic function tinyids. */
|
|
|
|
enum {
|
|
|
|
FUN_ARGUMENTS = -1, /* predefined arguments local variable */
|
|
|
|
FUN_LENGTH = -2, /* number of actual args, arity if inactive */
|
|
|
|
FUN_ARITY = -3, /* number of formal parameters; desired argc */
|
|
|
|
FUN_NAME = -4, /* function name, "" if anonymous */
|
|
|
|
FUN_CALLER = -5 /* Function.prototype.caller, backward compat */
|
|
|
|
};
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
static JSBool
|
2010-07-14 23:19:36 -07:00
|
|
|
fun_getProperty(JSContext *cx, JSObject *obj, jsid id, Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-07-14 23:19:36 -07:00
|
|
|
if (!JSID_IS_INT(id))
|
2010-07-23 14:41:56 -07:00
|
|
|
return true;
|
2010-06-02 23:44:24 -07:00
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
jsint slot = JSID_TO_INT(id);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Loop because getter and setter can be delegated from another class,
|
2009-08-14 11:43:16 -07:00
|
|
|
* but loop only for FUN_LENGTH because we must pretend that f.length
|
2007-03-22 10:30:00 -07:00
|
|
|
* 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).
|
|
|
|
*
|
2010-08-02 23:52:12 -07:00
|
|
|
* This code couples tightly to the attributes for lazyFunctionDataProps[]
|
|
|
|
* and poisonPillProps[] initializers below, 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,
|
2009-08-14 11:43:16 -07:00
|
|
|
* and name. If we didn't return early for slot != FUN_LENGTH, we would
|
2007-03-22 10:30:00 -07:00
|
|
|
* 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).
|
|
|
|
*/
|
2011-04-11 01:38:27 -07:00
|
|
|
|
|
|
|
while (!obj->isFunction()) {
|
2009-08-14 11:43:16 -07:00
|
|
|
if (slot != FUN_LENGTH)
|
2010-07-23 14:41:56 -07:00
|
|
|
return true;
|
2010-03-04 20:44:09 -08:00
|
|
|
obj = obj->getProto();
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!obj)
|
2010-07-23 14:41:56 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2011-04-11 01:38:27 -07:00
|
|
|
JSFunction *fun = obj->getFunctionPrivate();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-03-26 19:07:13 -07:00
|
|
|
if (slot == FUN_ARGUMENTS || slot == FUN_CALLER) {
|
|
|
|
/*
|
|
|
|
* Mark the function's script as uninlineable, to expand any of its
|
|
|
|
* frames on the stack before we go looking for them.
|
|
|
|
*/
|
2011-05-09 07:12:47 -07:00
|
|
|
if (fun->isInterpreted())
|
2011-06-02 10:40:27 -07:00
|
|
|
MarkTypeObjectFlags(cx, fun->getType(), OBJECT_FLAG_UNINLINEABLE);
|
2011-03-26 19:07:13 -07:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/* Find fun's top-most activation record. */
|
2011-04-13 09:27:37 -07:00
|
|
|
StackFrame *fp;
|
2011-03-26 19:07:13 -07:00
|
|
|
for (fp = js_GetTopStackFrame(cx, FRAME_EXPAND_NONE);
|
2011-05-11 09:11:40 -07:00
|
|
|
fp && (fp->maybeFun() != fun || fp->isDirectEvalOrDebuggerFrame());
|
2010-08-09 22:43:33 -07:00
|
|
|
fp = fp->prev()) {
|
2007-03-22 10:30:00 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-03-26 19:07:13 -07:00
|
|
|
#ifdef JS_METHODJIT
|
|
|
|
if (slot == FUN_CALLER && fp && fp->prev()) {
|
|
|
|
/* Also make sure the caller is uninlineable. */
|
|
|
|
JSInlinedSite *inlined;
|
|
|
|
fp->prev()->pc(cx, fp, &inlined);
|
|
|
|
if (inlined) {
|
|
|
|
JSFunction *fun = fp->prev()->jit()->inlineFrames()[inlined->inlineIndex].fun;
|
2011-06-02 10:40:27 -07:00
|
|
|
MarkTypeObjectFlags(cx, fun->getType(), OBJECT_FLAG_UNINLINEABLE);
|
2011-03-26 19:07:13 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
switch (slot) {
|
2009-08-14 11:43:16 -07:00
|
|
|
case FUN_ARGUMENTS:
|
2007-03-22 10:30:00 -07:00
|
|
|
/* 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)) {
|
2010-07-23 14:41:56 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
if (fp) {
|
|
|
|
if (!js_GetArgsValue(cx, fp, vp))
|
2010-07-23 14:41:56 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
2010-07-14 23:19:36 -07:00
|
|
|
vp->setNull();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2009-08-14 11:43:16 -07:00
|
|
|
case FUN_LENGTH:
|
2007-03-22 10:30:00 -07:00
|
|
|
case FUN_ARITY:
|
2010-07-14 23:19:36 -07:00
|
|
|
vp->setInt32(fun->nargs);
|
2007-03-22 10:30:00 -07:00
|
|
|
break;
|
|
|
|
|
2008-03-29 03:34:29 -07:00
|
|
|
case FUN_NAME:
|
2011-03-14 13:59:53 -07:00
|
|
|
vp->setString(fun->atom ? fun->atom
|
2010-07-14 23:19:36 -07:00
|
|
|
: cx->runtime->emptyString);
|
2007-03-22 10:30:00 -07:00
|
|
|
break;
|
|
|
|
|
2011-05-11 14:06:33 -07:00
|
|
|
case FUN_CALLER: {
|
2010-07-23 14:41:56 -07:00
|
|
|
vp->setNull();
|
2011-05-11 14:06:33 -07:00
|
|
|
|
|
|
|
StackFrame *callerframe = (fp && fp->prev()) ? js_GetScriptedCaller(cx, fp->prev()) : NULL;
|
|
|
|
if (callerframe && !callerframe->getValidCalleeObject(cx, vp))
|
2010-07-23 14:41:56 -07:00
|
|
|
return false;
|
2010-06-30 16:09:38 -07:00
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
if (vp->isObject()) {
|
2010-08-21 15:24:56 -07:00
|
|
|
JSObject &caller = vp->toObject();
|
|
|
|
|
2010-07-23 14:41:56 -07:00
|
|
|
/* Censor the caller if it is from another compartment. */
|
2011-05-11 14:06:33 -07:00
|
|
|
if (caller.compartment() != cx->compartment) {
|
2010-07-14 23:19:36 -07:00
|
|
|
vp->setNull();
|
2010-12-22 18:06:08 -08:00
|
|
|
} else if (caller.isFunction()) {
|
|
|
|
JSFunction *callerFun = caller.getFunctionPrivate();
|
|
|
|
if (callerFun->isInterpreted() && callerFun->inStrictMode()) {
|
|
|
|
JS_ReportErrorFlagsAndNumber(cx, JSREPORT_ERROR, js_GetErrorMessage, NULL,
|
|
|
|
JSMSG_CALLER_IS_STRICT);
|
|
|
|
return false;
|
|
|
|
}
|
2010-08-21 15:24:56 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
break;
|
2011-05-11 14:06:33 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
default:
|
2011-03-08 14:02:05 -08:00
|
|
|
JS_NOT_REACHED("fun_getProperty");
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-05-09 07:12:47 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-08-02 23:52:12 -07:00
|
|
|
struct LazyFunctionDataProp {
|
2008-09-04 21:56:37 -07:00
|
|
|
uint16 atomOffset;
|
|
|
|
int8 tinyid;
|
|
|
|
uint8 attrs;
|
2010-06-02 23:44:24 -07:00
|
|
|
};
|
2008-09-04 21:56:37 -07:00
|
|
|
|
2010-08-02 23:52:12 -07:00
|
|
|
struct PoisonPillProp {
|
|
|
|
uint16 atomOffset;
|
|
|
|
int8 tinyid;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* NB: no sentinels at ends -- use JS_ARRAY_LENGTH to bound loops. */
|
|
|
|
|
2010-10-25 16:47:11 -07:00
|
|
|
static const LazyFunctionDataProp lazyFunctionDataProps[] = {
|
2010-09-15 13:43:55 -07:00
|
|
|
{ATOM_OFFSET(arity), FUN_ARITY, JSPROP_PERMANENT|JSPROP_READONLY},
|
|
|
|
{ATOM_OFFSET(name), FUN_NAME, JSPROP_PERMANENT|JSPROP_READONLY},
|
2008-09-04 21:56:37 -07:00
|
|
|
};
|
|
|
|
|
2010-08-02 23:52:12 -07:00
|
|
|
/* Properties censored into [[ThrowTypeError]] in strict mode. */
|
2010-10-25 16:47:11 -07:00
|
|
|
static const PoisonPillProp poisonPillProps[] = {
|
2010-08-02 23:52:12 -07:00
|
|
|
{ATOM_OFFSET(arguments), FUN_ARGUMENTS },
|
|
|
|
{ATOM_OFFSET(caller), FUN_CALLER },
|
|
|
|
};
|
|
|
|
|
2009-09-24 14:33:14 -07:00
|
|
|
static JSBool
|
|
|
|
fun_enumerate(JSContext *cx, JSObject *obj)
|
|
|
|
{
|
|
|
|
JS_ASSERT(obj->isFunction());
|
|
|
|
|
2010-08-13 10:42:33 -07:00
|
|
|
jsid id;
|
2010-08-29 11:57:08 -07:00
|
|
|
bool found;
|
2010-08-13 10:42:33 -07:00
|
|
|
|
2010-09-16 19:31:11 -07:00
|
|
|
if (!obj->isBoundFunction()) {
|
2010-08-13 10:42:33 -07:00
|
|
|
id = ATOM_TO_JSID(cx->runtime->atomState.classPrototypeAtom);
|
2010-08-29 11:57:08 -07:00
|
|
|
if (!obj->hasProperty(cx, id, &found, JSRESOLVE_QUALIFIED))
|
2010-08-13 10:42:33 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-09-24 14:33:14 -07:00
|
|
|
id = ATOM_TO_JSID(cx->runtime->atomState.lengthAtom);
|
2010-08-29 11:57:08 -07:00
|
|
|
if (!obj->hasProperty(cx, id, &found, JSRESOLVE_QUALIFIED))
|
2009-09-24 14:33:14 -07:00
|
|
|
return false;
|
|
|
|
|
2010-08-02 23:52:12 -07:00
|
|
|
for (uintN i = 0; i < JS_ARRAY_LENGTH(lazyFunctionDataProps); i++) {
|
|
|
|
const LazyFunctionDataProp &lfp = lazyFunctionDataProps[i];
|
2009-09-24 14:33:14 -07:00
|
|
|
id = ATOM_TO_JSID(OFFSET_TO_ATOM(cx->runtime, lfp.atomOffset));
|
2010-08-29 11:57:08 -07:00
|
|
|
if (!obj->hasProperty(cx, id, &found, JSRESOLVE_QUALIFIED))
|
2009-09-24 14:33:14 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-08-02 23:52:12 -07:00
|
|
|
for (uintN i = 0; i < JS_ARRAY_LENGTH(poisonPillProps); i++) {
|
|
|
|
const PoisonPillProp &p = poisonPillProps[i];
|
|
|
|
id = ATOM_TO_JSID(OFFSET_TO_ATOM(cx->runtime, p.atomOffset));
|
2010-08-29 11:57:08 -07:00
|
|
|
if (!obj->hasProperty(cx, id, &found, JSRESOLVE_QUALIFIED))
|
2010-08-02 23:52:12 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-09-24 14:33:14 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-02-14 12:04:07 -08:00
|
|
|
static JSObject *
|
|
|
|
ResolveInterpretedFunctionPrototype(JSContext *cx, JSObject *obj)
|
|
|
|
{
|
2011-02-15 12:07:23 -08:00
|
|
|
#ifdef DEBUG
|
2011-02-14 12:04:07 -08:00
|
|
|
JSFunction *fun = obj->getFunctionPrivate();
|
|
|
|
JS_ASSERT(fun->isInterpreted());
|
|
|
|
JS_ASSERT(!fun->isFunctionPrototype());
|
2011-02-15 12:07:23 -08:00
|
|
|
#endif
|
2011-02-14 12:04:07 -08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Assert that fun is not a compiler-created function object, which
|
|
|
|
* must never leak to script or embedding code and then be mutated.
|
|
|
|
* Also assert that obj is not bound, per the ES5 15.3.4.5 ref above.
|
|
|
|
*/
|
|
|
|
JS_ASSERT(!IsInternalFunctionObject(obj));
|
|
|
|
JS_ASSERT(!obj->isBoundFunction());
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Make the prototype object an instance of Object with the same parent
|
|
|
|
* as the function object itself.
|
|
|
|
*/
|
|
|
|
JSObject *parent = obj->getParent();
|
2011-02-24 21:21:43 -08:00
|
|
|
JSObject *objProto;
|
|
|
|
if (!js_GetClassPrototype(cx, parent, JSProto_Object, &objProto))
|
2011-02-14 12:04:07 -08:00
|
|
|
return NULL;
|
2011-02-24 21:21:43 -08:00
|
|
|
JSObject *proto = NewNativeClassInstance(cx, &js_ObjectClass, objProto, parent);
|
2011-02-14 12:04:07 -08:00
|
|
|
if (!proto)
|
|
|
|
return NULL;
|
|
|
|
|
2011-02-24 21:21:43 -08:00
|
|
|
/* Make a new type for the prototype object. */
|
2011-05-19 09:15:12 -07:00
|
|
|
|
|
|
|
TypeObject *protoType = cx->compartment->types.newTypeObject(cx, NULL,
|
|
|
|
obj->getType()->name(),
|
|
|
|
"prototype",
|
|
|
|
false, false, objProto);
|
2011-03-16 10:02:19 -07:00
|
|
|
if (!protoType || !proto->setTypeAndUniqueShape(cx, protoType))
|
|
|
|
return NULL;
|
2011-02-24 21:21:43 -08:00
|
|
|
|
2011-02-14 12:04:07 -08:00
|
|
|
/*
|
|
|
|
* ECMA (15.3.5.2) says that a user-defined function's .prototype property
|
|
|
|
* is non-configurable, non-enumerable, and (initially) writable. Hence
|
|
|
|
* JSPROP_PERMANENT below. By contrast, the built-in constructors, such as
|
|
|
|
* Object (15.2.3.1) and Function (15.3.3.1), have non-writable
|
|
|
|
* .prototype properties. Those are eagerly defined, with attributes
|
|
|
|
* JSPROP_PERMANENT | JSPROP_READONLY, in js_InitClass.
|
|
|
|
*/
|
|
|
|
if (!js_SetClassPrototype(cx, obj, proto, JSPROP_PERMANENT))
|
|
|
|
return NULL;
|
|
|
|
return proto;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
static JSBool
|
2010-07-14 23:19:36 -07:00
|
|
|
fun_resolve(JSContext *cx, JSObject *obj, jsid id, uintN flags,
|
2007-03-22 10:30:00 -07:00
|
|
|
JSObject **objp)
|
|
|
|
{
|
2010-07-14 23:19:36 -07:00
|
|
|
if (!JSID_IS_ATOM(id))
|
2010-10-03 22:46:39 -07:00
|
|
|
return true;
|
2007-06-26 11:19:54 -07:00
|
|
|
|
2010-08-13 10:42:33 -07:00
|
|
|
JSFunction *fun = obj->getFunctionPrivate();
|
2007-06-19 11:49:53 -07:00
|
|
|
|
2010-10-03 22:46:39 -07:00
|
|
|
if (JSID_IS_ATOM(id, cx->runtime->atomState.classPrototypeAtom)) {
|
|
|
|
/*
|
|
|
|
* Native or "built-in" functions do not have a .prototype property per
|
|
|
|
* ECMA-262 (all editions). Built-in constructor functions, e.g. Object
|
|
|
|
* and Function to name two conspicuous examples, do have a .prototype
|
|
|
|
* property, but it is created eagerly by js_InitClass (jsobj.cpp).
|
|
|
|
*
|
|
|
|
* ES5 15.3.4: the non-native function object named Function.prototype
|
|
|
|
* must not have a .prototype property.
|
|
|
|
*
|
|
|
|
* ES5 15.3.4.5: bound functions don't have a prototype property. The
|
|
|
|
* isNative() test covers this case because bound functions are native
|
|
|
|
* functions by definition/construction.
|
|
|
|
*/
|
|
|
|
if (fun->isNative() || fun->isFunctionPrototype())
|
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-02-14 12:04:07 -08:00
|
|
|
if (!ResolveInterpretedFunctionPrototype(cx, obj))
|
2010-10-03 22:46:39 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
*objp = obj;
|
2010-10-03 22:46:39 -07:00
|
|
|
return true;
|
2007-06-26 11:19:54 -07:00
|
|
|
}
|
|
|
|
|
2010-10-03 22:46:39 -07:00
|
|
|
if (JSID_IS_ATOM(id, cx->runtime->atomState.lengthAtom)) {
|
2010-06-02 23:44:24 -07:00
|
|
|
JS_ASSERT(!IsInternalFunctionObject(obj));
|
2011-01-21 05:10:16 -08:00
|
|
|
if (!DefineNativeProperty(cx, obj, id, Int32Value(fun->nargs),
|
|
|
|
PropertyStub, StrictPropertyStub,
|
|
|
|
JSPROP_PERMANENT | JSPROP_READONLY, 0, 0)) {
|
2010-10-03 22:46:39 -07:00
|
|
|
return false;
|
2010-06-02 23:44:24 -07:00
|
|
|
}
|
|
|
|
*objp = obj;
|
2010-10-03 22:46:39 -07:00
|
|
|
return true;
|
2010-06-02 23:44:24 -07:00
|
|
|
}
|
|
|
|
|
2010-08-02 23:52:12 -07:00
|
|
|
for (uintN i = 0; i < JS_ARRAY_LENGTH(lazyFunctionDataProps); i++) {
|
|
|
|
const LazyFunctionDataProp *lfp = &lazyFunctionDataProps[i];
|
2007-06-26 11:19:54 -07:00
|
|
|
|
2010-10-03 22:46:39 -07:00
|
|
|
if (JSID_IS_ATOM(id, OFFSET_TO_ATOM(cx->runtime, lfp->atomOffset))) {
|
2010-03-30 00:44:28 -07:00
|
|
|
JS_ASSERT(!IsInternalFunctionObject(obj));
|
2011-01-21 05:10:16 -08:00
|
|
|
if (!DefineNativeProperty(cx, obj, id, UndefinedValue(),
|
|
|
|
fun_getProperty, StrictPropertyStub,
|
|
|
|
lfp->attrs, Shape::HAS_SHORTID, lfp->tinyid)) {
|
2010-10-03 22:46:39 -07:00
|
|
|
return false;
|
2007-06-26 11:19:54 -07:00
|
|
|
}
|
|
|
|
*objp = obj;
|
2010-10-03 22:46:39 -07:00
|
|
|
return true;
|
2007-06-26 11:19:54 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-08-02 23:52:12 -07:00
|
|
|
for (uintN i = 0; i < JS_ARRAY_LENGTH(poisonPillProps); i++) {
|
|
|
|
const PoisonPillProp &p = poisonPillProps[i];
|
|
|
|
|
2010-10-03 22:46:39 -07:00
|
|
|
if (JSID_IS_ATOM(id, OFFSET_TO_ATOM(cx->runtime, p.atomOffset))) {
|
2010-08-02 23:52:12 -07:00
|
|
|
JS_ASSERT(!IsInternalFunctionObject(obj));
|
|
|
|
|
2011-02-09 11:31:40 -08:00
|
|
|
PropertyOp getter;
|
|
|
|
StrictPropertyOp setter;
|
2010-08-02 23:52:12 -07:00
|
|
|
uintN attrs = JSPROP_PERMANENT;
|
2010-12-22 18:06:08 -08:00
|
|
|
if (fun->isInterpreted() ? fun->inStrictMode() : obj->isBoundFunction()) {
|
2010-08-02 23:52:12 -07:00
|
|
|
JSObject *throwTypeError = obj->getThrowTypeError();
|
|
|
|
|
|
|
|
getter = CastAsPropertyOp(throwTypeError);
|
2011-02-09 11:31:40 -08:00
|
|
|
setter = CastAsStrictPropertyOp(throwTypeError);
|
2010-08-02 23:52:12 -07:00
|
|
|
attrs |= JSPROP_GETTER | JSPROP_SETTER;
|
|
|
|
} else {
|
|
|
|
getter = fun_getProperty;
|
2011-02-09 11:31:40 -08:00
|
|
|
setter = StrictPropertyStub;
|
2010-08-02 23:52:12 -07:00
|
|
|
}
|
|
|
|
|
2011-01-21 05:10:16 -08:00
|
|
|
if (!DefineNativeProperty(cx, obj, id, UndefinedValue(), getter, setter,
|
|
|
|
attrs, Shape::HAS_SHORTID, p.tinyid)) {
|
2010-10-03 22:46:39 -07:00
|
|
|
return false;
|
2010-08-02 23:52:12 -07:00
|
|
|
}
|
|
|
|
*objp = obj;
|
2010-10-03 22:46:39 -07:00
|
|
|
return true;
|
2010-08-02 23:52:12 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-03 22:46:39 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#if JS_HAS_XDR
|
|
|
|
|
|
|
|
/* XXX store parent and proto, if defined */
|
2009-05-06 16:03:10 -07:00
|
|
|
JSBool
|
|
|
|
js_XDRFunctionObject(JSXDRState *xdr, JSObject **objp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
JSContext *cx;
|
2008-03-29 03:34:29 -07:00
|
|
|
JSFunction *fun;
|
2009-06-04 18:58:47 -07:00
|
|
|
uint32 firstword; /* flag telling whether fun->atom is non-null,
|
|
|
|
plus for fun->u.i.skipmin, fun->u.i.wrapper,
|
|
|
|
and 14 bits reserved for future use */
|
2010-11-16 15:34:24 -08:00
|
|
|
uint32 flagsword; /* word for argument count and fun->flags */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
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)) {
|
2010-11-11 12:40:29 -08:00
|
|
|
JSAutoByteString funNameBytes;
|
|
|
|
if (const char *name = GetFunctionNameBytes(cx, fun, &funNameBytes)) {
|
|
|
|
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_NOT_SCRIPTED_FUNCTION,
|
|
|
|
name);
|
|
|
|
}
|
2010-03-28 22:05:50 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2009-06-04 18:58:47 -07:00
|
|
|
if (fun->u.i.wrapper) {
|
2010-11-11 12:40:29 -08:00
|
|
|
JSAutoByteString funNameBytes;
|
|
|
|
if (const char *name = GetFunctionNameBytes(cx, fun, &funNameBytes))
|
|
|
|
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_XDR_CLOSURE_WRAPPER, name);
|
2010-03-28 22:05:50 -07:00
|
|
|
return false;
|
2009-06-04 18:58:47 -07:00
|
|
|
}
|
|
|
|
JS_ASSERT((fun->u.i.wrapper & ~1U) == 0);
|
|
|
|
firstword = (fun->u.i.skipmin << 2) | (fun->u.i.wrapper << 1) | !!fun->atom;
|
2010-11-16 15:34:24 -08:00
|
|
|
flagsword = (fun->nargs << 16) | fun->flags;
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
2010-10-29 08:05:55 -07:00
|
|
|
fun = js_NewFunction(cx, NULL, NULL, 0, JSFUN_INTERPRETED, NULL, NULL, NULL, NULL);
|
2008-03-28 15:27:36 -07:00
|
|
|
if (!fun)
|
2010-03-28 22:05:50 -07:00
|
|
|
return false;
|
2010-03-04 23:52:52 -08:00
|
|
|
FUN_OBJECT(fun)->clearParent();
|
2011-03-07 08:25:27 -08:00
|
|
|
if (!FUN_OBJECT(fun)->clearType(cx))
|
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
AutoObjectRooter tvr(cx, FUN_OBJECT(fun));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-06-04 18:58:47 -07:00
|
|
|
if (!JS_XDRUint32(xdr, &firstword))
|
2010-03-28 22:05:50 -07:00
|
|
|
return false;
|
2010-07-15 18:19:50 -07:00
|
|
|
if ((firstword & 1U) && !js_XDRAtom(xdr, &fun->atom))
|
2010-03-28 22:05:50 -07:00
|
|
|
return false;
|
2010-11-16 15:34:24 -08:00
|
|
|
if (!JS_XDRUint32(xdr, &flagsword))
|
2010-03-28 22:05:50 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-11-19 09:15:45 -08:00
|
|
|
if (xdr->mode == JSXDR_DECODE) {
|
2010-11-16 15:34:24 -08:00
|
|
|
fun->nargs = flagsword >> 16;
|
2009-04-05 21:17:22 -07:00
|
|
|
JS_ASSERT((flagsword & JSFUN_KINDMASK) >= JSFUN_INTERPRETED);
|
|
|
|
fun->flags = uint16(flagsword);
|
2009-06-04 18:58:47 -07:00
|
|
|
fun->u.i.skipmin = uint16(firstword >> 2);
|
2009-09-23 11:16:30 -07:00
|
|
|
fun->u.i.wrapper = JSPackedBool((firstword >> 1) & 1);
|
2007-11-19 09:15:45 -08:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-03-24 12:56:58 -07:00
|
|
|
if (!js_XDRScript(xdr, &fun->u.i.script))
|
2010-03-28 22:05:50 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
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
|
2010-11-16 15:30:56 -08:00
|
|
|
fun->script()->owner = NULL;
|
2008-02-07 15:18:45 -08:00
|
|
|
#endif
|
2011-03-20 11:22:33 -07:00
|
|
|
if (!fun->u.i.script->typeSetFunction(cx, fun))
|
2011-03-07 08:25:27 -08:00
|
|
|
return false;
|
2010-11-16 15:34:24 -08:00
|
|
|
JS_ASSERT(fun->nargs == fun->script()->bindings.countArgs());
|
2010-11-16 15:30:56 -08:00
|
|
|
js_CallNewScriptHook(cx, fun->script(), fun);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-03-28 22:05:50 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#else /* !JS_HAS_XDR */
|
|
|
|
|
2009-05-06 16:03:10 -07:00
|
|
|
#define js_XDRFunctionObject NULL
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#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
|
2010-07-14 23:19:36 -07:00
|
|
|
fun_hasInstance(JSContext *cx, JSObject *obj, const Value *v, JSBool *bp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-08-13 10:42:33 -07:00
|
|
|
while (obj->isFunction()) {
|
2010-09-16 19:31:11 -07:00
|
|
|
if (!obj->isBoundFunction())
|
2010-08-13 10:42:33 -07:00
|
|
|
break;
|
|
|
|
obj = obj->getBoundFunctionTarget();
|
|
|
|
}
|
|
|
|
|
2009-08-14 11:43:16 -07:00
|
|
|
jsid id = ATOM_TO_JSID(cx->runtime->atomState.classPrototypeAtom);
|
2010-07-14 23:19:36 -07:00
|
|
|
Value pval;
|
2009-08-14 11:43:16 -07:00
|
|
|
if (!obj->getProperty(cx, id, &pval))
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
if (pval.isPrimitive()) {
|
2007-03-22 10:30:00 -07:00
|
|
|
/*
|
|
|
|
* Throw a runtime error if instanceof is called on a function that
|
|
|
|
* has a non-object as its .prototype value.
|
|
|
|
*/
|
2010-07-14 23:19:36 -07:00
|
|
|
js_ReportValueError(cx, JSMSG_BAD_PROTOTYPE, -1, ObjectValue(*obj), NULL);
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
*bp = js_IsDelegate(cx, &pval.toObject(), *v);
|
|
|
|
return JS_TRUE;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2007-04-16 23:53:37 -07:00
|
|
|
static void
|
|
|
|
fun_trace(JSTracer *trc, JSObject *obj)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2008-03-28 15:27:36 -07:00
|
|
|
/* A newborn function object may have a not yet initialized private slot. */
|
2009-08-04 14:06:55 -07:00
|
|
|
JSFunction *fun = (JSFunction *) obj->getPrivate();
|
2008-04-02 00:46:12 -07:00
|
|
|
if (!fun)
|
|
|
|
return;
|
|
|
|
|
2010-08-29 11:57:08 -07:00
|
|
|
if (fun != obj) {
|
|
|
|
/* obj is a cloned function object, trace the clone-parent, fun. */
|
2010-08-09 22:43:33 -07:00
|
|
|
MarkObject(trc, *fun, "private");
|
2010-08-29 11:57:08 -07:00
|
|
|
|
|
|
|
/* The function could be a flat closure with upvar copies in the clone. */
|
2010-11-16 15:34:24 -08:00
|
|
|
if (fun->isFlatClosure() && fun->script()->bindings.hasUpvars()) {
|
|
|
|
MarkValueRange(trc, fun->script()->bindings.countUpvars(),
|
|
|
|
obj->getFlatClosureUpvars(), "upvars");
|
|
|
|
}
|
2008-04-02 00:46:12 -07:00
|
|
|
return;
|
|
|
|
}
|
2010-08-29 11:57:08 -07:00
|
|
|
|
2008-04-02 00:46:12 -07:00
|
|
|
if (fun->atom)
|
2011-03-14 13:59:53 -07:00
|
|
|
MarkString(trc, fun->atom, "atom");
|
2010-08-29 11:57:08 -07:00
|
|
|
|
2010-11-16 15:34:24 -08:00
|
|
|
if (fun->isInterpreted() && fun->script())
|
|
|
|
js_TraceScript(trc, fun->script());
|
2008-04-02 00:46:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
fun_finalize(JSContext *cx, JSObject *obj)
|
|
|
|
{
|
2010-08-29 11:57:08 -07:00
|
|
|
/* Ignore newborn function objects. */
|
2010-11-16 15:34:24 -08:00
|
|
|
JSFunction *fun = obj->getFunctionPrivate();
|
2010-08-29 11:57:08 -07:00
|
|
|
if (!fun)
|
2008-04-02 00:46:12 -07:00
|
|
|
return;
|
|
|
|
|
2010-08-29 11:57:08 -07:00
|
|
|
/* Cloned function objects may be flat closures with upvars to free. */
|
|
|
|
if (fun != obj) {
|
2010-11-16 15:34:24 -08:00
|
|
|
if (fun->isFlatClosure() && fun->script()->bindings.hasUpvars())
|
2011-03-31 01:14:12 -07:00
|
|
|
cx->free_((void *) obj->getFlatClosureUpvars());
|
2010-08-29 11:57:08 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-04-02 00:46:12 -07:00
|
|
|
/*
|
2010-11-16 15:34:24 -08:00
|
|
|
* Null-check fun->script() because the parser sets interpreted very early.
|
2008-04-02 00:46:12 -07:00
|
|
|
*/
|
2010-11-16 15:34:24 -08:00
|
|
|
if (fun->isInterpreted() && fun->script())
|
|
|
|
js_DestroyScriptFromGC(cx, fun->script());
|
2009-07-24 03:01:37 -07:00
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
*/
|
2010-07-14 23:19:36 -07:00
|
|
|
JS_PUBLIC_DATA(Class) js_FunctionClass = {
|
2007-03-22 10:30:00 -07:00
|
|
|
js_Function_str,
|
2010-07-23 14:41:56 -07:00
|
|
|
JSCLASS_HAS_PRIVATE | JSCLASS_NEW_RESOLVE |
|
2010-08-29 11:57:08 -07:00
|
|
|
JSCLASS_HAS_RESERVED_SLOTS(JSFunction::CLASS_RESERVED_SLOTS) |
|
2011-03-05 14:50:11 -08:00
|
|
|
JSCLASS_HAS_CACHED_PROTO(JSProto_Function),
|
2011-02-09 11:31:40 -08:00
|
|
|
PropertyStub, /* addProperty */
|
|
|
|
PropertyStub, /* delProperty */
|
|
|
|
PropertyStub, /* getProperty */
|
|
|
|
StrictPropertyStub, /* setProperty */
|
2010-06-12 09:29:04 -07:00
|
|
|
fun_enumerate,
|
|
|
|
(JSResolveOp)fun_resolve,
|
|
|
|
ConvertStub,
|
|
|
|
fun_finalize,
|
2011-02-09 11:31:40 -08:00
|
|
|
NULL, /* reserved0 */
|
|
|
|
NULL, /* checkAccess */
|
|
|
|
NULL, /* call */
|
|
|
|
NULL, /* construct */
|
2011-03-21 11:06:34 -07:00
|
|
|
NULL,
|
2010-06-12 09:29:04 -07:00
|
|
|
fun_hasInstance,
|
2011-03-05 14:50:11 -08:00
|
|
|
fun_trace
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
2010-06-23 14:35:10 -07:00
|
|
|
JSString *
|
|
|
|
fun_toStringHelper(JSContext *cx, JSObject *obj, uintN indent)
|
|
|
|
{
|
|
|
|
if (!obj->isFunction()) {
|
2010-06-24 11:25:09 -07:00
|
|
|
if (obj->isFunctionProxy())
|
2010-06-23 14:35:10 -07:00
|
|
|
return JSProxy::fun_toString(cx, obj, indent);
|
2010-04-11 20:55:22 -07:00
|
|
|
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
|
|
|
|
JSMSG_INCOMPATIBLE_PROTO,
|
|
|
|
js_Function_str, js_toString_str,
|
2010-06-23 14:35:10 -07:00
|
|
|
"object");
|
|
|
|
return NULL;
|
2008-03-06 13:40:43 -08:00
|
|
|
}
|
2007-08-01 21:33:52 -07:00
|
|
|
|
2010-06-23 14:35:10 -07:00
|
|
|
JSFunction *fun = GET_FUNCTION_PRIVATE(cx, obj);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!fun)
|
2010-06-23 14:35:10 -07:00
|
|
|
return NULL;
|
2011-01-28 10:19:13 -08:00
|
|
|
|
2011-03-03 15:28:25 -08:00
|
|
|
if (!indent && !cx->compartment->toSourceCache.empty()) {
|
|
|
|
ToSourceCache::Ptr p = cx->compartment->toSourceCache.ref().lookup(fun);
|
2011-01-28 10:19:13 -08:00
|
|
|
if (p)
|
|
|
|
return p->value;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSString *str = JS_DecompileFunction(cx, fun, indent);
|
|
|
|
if (!str)
|
2011-03-03 15:28:25 -08:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (!indent) {
|
2011-04-28 15:48:51 -07:00
|
|
|
Maybe<ToSourceCache> &lazy = cx->compartment->toSourceCache;
|
2011-01-28 10:19:13 -08:00
|
|
|
|
2011-03-03 15:28:25 -08:00
|
|
|
if (lazy.empty()) {
|
|
|
|
lazy.construct();
|
|
|
|
if (!lazy.ref().init())
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!lazy.ref().put(fun, str))
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-01-28 10:19:13 -08:00
|
|
|
|
|
|
|
return str;
|
2010-06-23 14:35:10 -07:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
static JSBool
|
2010-07-14 23:19:36 -07:00
|
|
|
fun_toString(JSContext *cx, uintN argc, Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-07-14 23:19:36 -07:00
|
|
|
JS_ASSERT(IsFunctionObject(vp[0]));
|
2010-06-23 14:35:10 -07:00
|
|
|
uint32_t indent = 0;
|
|
|
|
|
|
|
|
if (argc != 0 && !ValueToECMAUint32(cx, vp[2], &indent))
|
|
|
|
return false;
|
|
|
|
|
2011-01-26 13:37:45 -08:00
|
|
|
JSObject *obj = ToObject(cx, &vp[1]);
|
2010-06-23 14:35:10 -07:00
|
|
|
if (!obj)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
JSString *str = fun_toStringHelper(cx, obj, indent);
|
|
|
|
if (!str)
|
|
|
|
return false;
|
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
vp->setString(str);
|
2010-06-23 14:35:10 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#if JS_HAS_TOSOURCE
|
|
|
|
static JSBool
|
2010-07-14 23:19:36 -07:00
|
|
|
fun_toSource(JSContext *cx, uintN argc, Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-07-14 23:19:36 -07:00
|
|
|
JS_ASSERT(IsFunctionObject(vp[0]));
|
2010-06-23 14:35:10 -07:00
|
|
|
|
2011-01-26 13:37:45 -08:00
|
|
|
JSObject *obj = ToObject(cx, &vp[1]);
|
2010-06-23 14:35:10 -07:00
|
|
|
if (!obj)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
JSString *str = fun_toStringHelper(cx, obj, JS_DONT_PRETTY_PRINT);
|
|
|
|
if (!str)
|
|
|
|
return false;
|
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
vp->setString(str);
|
2010-06-23 14:35:10 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-06-25 12:12:19 -07:00
|
|
|
JSBool
|
2010-07-14 23:19:36 -07:00
|
|
|
js_fun_call(JSContext *cx, uintN argc, Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-01-22 14:49:18 -08:00
|
|
|
LeaveTrace(cx);
|
2010-07-14 23:19:36 -07:00
|
|
|
Value fval = vp[1];
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-02-22 22:25:09 -08:00
|
|
|
if (!js_IsCallable(fval)) {
|
2011-04-11 01:38:27 -07:00
|
|
|
ReportIncompatibleMethod(cx, vp, &js_FunctionClass);
|
2011-01-26 13:37:45 -08:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
Value *argv = vp + 2;
|
2010-10-12 11:50:03 -07:00
|
|
|
Value thisv;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (argc == 0) {
|
2010-10-12 11:50:03 -07:00
|
|
|
thisv.setUndefined();
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
2010-10-12 11:50:03 -07:00
|
|
|
thisv = argv[0];
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
argc--;
|
|
|
|
argv++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate stack space for fval, obj, and the args. */
|
2010-03-03 17:52:26 -08:00
|
|
|
InvokeArgsGuard args;
|
2011-04-13 09:27:37 -07:00
|
|
|
if (!cx->stack.pushInvokeArgs(cx, argc, &args))
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
|
2010-10-12 11:50:03 -07:00
|
|
|
/* Push fval, thisv, and the args. */
|
2011-04-08 10:52:48 -07:00
|
|
|
args.calleev() = fval;
|
2010-10-12 11:50:03 -07:00
|
|
|
args.thisv() = thisv;
|
2010-07-30 10:41:03 -07:00
|
|
|
memcpy(args.argv(), argv, argc * sizeof *argv);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-04-13 09:27:37 -07:00
|
|
|
bool ok = Invoke(cx, args);
|
2010-07-30 10:41:03 -07:00
|
|
|
*vp = args.rval();
|
2007-03-22 10:30:00 -07:00
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
2010-07-16 12:53:37 -07:00
|
|
|
/* ES5 15.3.4.3 */
|
2009-06-25 12:12:19 -07:00
|
|
|
JSBool
|
2010-07-14 23:19:36 -07:00
|
|
|
js_fun_apply(JSContext *cx, uintN argc, Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-07-16 12:53:37 -07:00
|
|
|
/* Step 1. */
|
2010-07-14 23:19:36 -07:00
|
|
|
Value fval = vp[1];
|
2010-02-22 22:25:09 -08:00
|
|
|
if (!js_IsCallable(fval)) {
|
2011-04-11 01:38:27 -07:00
|
|
|
ReportIncompatibleMethod(cx, vp, &js_FunctionClass);
|
2010-07-16 12:53:37 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-07-16 12:53:37 -07:00
|
|
|
/* Step 2. */
|
|
|
|
if (argc < 2 || vp[3].isNullOrUndefined())
|
|
|
|
return js_fun_call(cx, (argc > 0) ? 1 : 0, vp);
|
|
|
|
|
2010-10-21 11:33:22 -07:00
|
|
|
/* N.B. Changes need to be propagated to stubs::SplatApplyArgs. */
|
|
|
|
|
2010-07-16 12:53:37 -07:00
|
|
|
/* Step 3. */
|
|
|
|
if (!vp[3].isObject()) {
|
|
|
|
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_BAD_APPLY_ARGS, js_apply_str);
|
|
|
|
return false;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-07-16 12:53:37 -07:00
|
|
|
/*
|
|
|
|
* Steps 4-5 (note erratum removing steps originally numbered 5 and 7 in
|
|
|
|
* original version of ES5).
|
|
|
|
*/
|
2010-10-22 15:40:11 -07:00
|
|
|
JSObject *aobj = &vp[3].toObject();
|
2010-07-16 12:53:37 -07:00
|
|
|
jsuint length;
|
2010-10-20 23:52:55 -07:00
|
|
|
if (!js_GetLengthProperty(cx, aobj, &length))
|
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-07-16 12:53:37 -07:00
|
|
|
LeaveTrace(cx);
|
|
|
|
|
|
|
|
/* Step 6. */
|
|
|
|
uintN n = uintN(JS_MIN(length, JS_ARGS_LENGTH_MAX));
|
2010-03-03 17:52:26 -08:00
|
|
|
|
|
|
|
InvokeArgsGuard args;
|
2011-04-13 09:27:37 -07:00
|
|
|
if (!cx->stack.pushInvokeArgs(cx, n, &args))
|
2010-07-16 12:53:37 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/* Push fval, obj, and aobj's elements as args. */
|
2011-04-08 10:52:48 -07:00
|
|
|
args.calleev() = fval;
|
2010-10-12 11:50:03 -07:00
|
|
|
args.thisv() = vp[2];
|
2010-07-16 12:53:37 -07:00
|
|
|
|
|
|
|
/* Steps 7-8. */
|
2010-10-20 23:52:55 -07:00
|
|
|
if (!GetElements(cx, aobj, n, args.argv()))
|
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-07-16 12:53:37 -07:00
|
|
|
/* Step 9. */
|
2011-04-13 09:27:37 -07:00
|
|
|
if (!Invoke(cx, args))
|
2010-07-16 12:53:37 -07:00
|
|
|
return false;
|
2010-07-30 10:41:03 -07:00
|
|
|
*vp = args.rval();
|
2010-07-16 12:53:37 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-02-09 18:17:34 -08:00
|
|
|
namespace js {
|
|
|
|
|
|
|
|
JSBool
|
2010-08-13 10:42:33 -07:00
|
|
|
CallOrConstructBoundFunction(JSContext *cx, uintN argc, Value *vp);
|
|
|
|
|
2011-02-09 18:17:34 -08:00
|
|
|
}
|
|
|
|
|
2010-08-13 10:42:33 -07:00
|
|
|
inline bool
|
|
|
|
JSObject::initBoundFunction(JSContext *cx, const Value &thisArg,
|
|
|
|
const Value *args, uintN argslen)
|
|
|
|
{
|
|
|
|
JS_ASSERT(isFunction());
|
|
|
|
|
2010-09-16 19:31:11 -07:00
|
|
|
flags |= JSObject::BOUND_FUNCTION;
|
2010-10-13 11:49:22 -07:00
|
|
|
getSlotRef(JSSLOT_BOUND_FUNCTION_THIS) = thisArg;
|
|
|
|
getSlotRef(JSSLOT_BOUND_FUNCTION_ARGS_COUNT).setPrivateUint32(argslen);
|
2010-08-13 10:42:33 -07:00
|
|
|
if (argslen != 0) {
|
2010-08-29 11:57:08 -07:00
|
|
|
/* FIXME? Burn memory on an empty scope whose shape covers the args slots. */
|
|
|
|
EmptyShape *empty = EmptyShape::create(cx, clasp);
|
|
|
|
if (!empty)
|
|
|
|
return false;
|
|
|
|
|
2010-09-03 11:05:43 -07:00
|
|
|
empty->slotSpan += argslen;
|
2011-04-08 14:21:08 -07:00
|
|
|
setMap(empty);
|
2010-08-29 11:57:08 -07:00
|
|
|
|
|
|
|
if (!ensureInstanceReservedSlots(cx, argslen))
|
2010-08-13 10:42:33 -07:00
|
|
|
return false;
|
|
|
|
|
2010-10-13 11:49:22 -07:00
|
|
|
JS_ASSERT(numSlots() >= argslen + FUN_CLASS_RESERVED_SLOTS);
|
2011-04-08 19:51:40 -07:00
|
|
|
copySlotRange(FUN_CLASS_RESERVED_SLOTS, args, argslen);
|
2010-08-13 10:42:33 -07:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline JSObject *
|
|
|
|
JSObject::getBoundFunctionTarget() const
|
|
|
|
{
|
|
|
|
JS_ASSERT(isFunction());
|
2010-09-16 19:31:11 -07:00
|
|
|
JS_ASSERT(isBoundFunction());
|
2010-08-13 10:42:33 -07:00
|
|
|
|
|
|
|
/* Bound functions abuse |parent| to store their target function. */
|
|
|
|
return getParent();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline const js::Value &
|
|
|
|
JSObject::getBoundFunctionThis() const
|
|
|
|
{
|
|
|
|
JS_ASSERT(isFunction());
|
2010-09-16 19:31:11 -07:00
|
|
|
JS_ASSERT(isBoundFunction());
|
2010-08-13 10:42:33 -07:00
|
|
|
|
2010-10-13 11:49:22 -07:00
|
|
|
return getSlot(JSSLOT_BOUND_FUNCTION_THIS);
|
2010-08-13 10:42:33 -07:00
|
|
|
}
|
|
|
|
|
2011-04-08 19:51:40 -07:00
|
|
|
inline const js::Value &
|
|
|
|
JSObject::getBoundFunctionArgument(uintN which) const
|
2010-08-13 10:42:33 -07:00
|
|
|
{
|
|
|
|
JS_ASSERT(isFunction());
|
2010-09-16 19:31:11 -07:00
|
|
|
JS_ASSERT(isBoundFunction());
|
2011-04-08 19:51:40 -07:00
|
|
|
JS_ASSERT(which < getBoundFunctionArgumentCount());
|
|
|
|
|
|
|
|
return getSlot(FUN_CLASS_RESERVED_SLOTS + which);
|
|
|
|
}
|
2010-08-13 10:42:33 -07:00
|
|
|
|
2011-04-08 19:51:40 -07:00
|
|
|
inline size_t
|
|
|
|
JSObject::getBoundFunctionArgumentCount() const
|
|
|
|
{
|
|
|
|
JS_ASSERT(isFunction());
|
|
|
|
JS_ASSERT(isBoundFunction());
|
2010-10-13 11:49:22 -07:00
|
|
|
|
2011-04-08 19:51:40 -07:00
|
|
|
return getSlot(JSSLOT_BOUND_FUNCTION_ARGS_COUNT).toPrivateUint32();
|
2010-08-13 10:42:33 -07:00
|
|
|
}
|
|
|
|
|
2011-02-09 18:17:34 -08:00
|
|
|
namespace js {
|
|
|
|
|
2010-08-13 10:42:33 -07:00
|
|
|
/* ES5 15.3.4.5.1 and 15.3.4.5.2. */
|
2011-02-09 18:17:34 -08:00
|
|
|
JSBool
|
2010-08-13 10:42:33 -07:00
|
|
|
CallOrConstructBoundFunction(JSContext *cx, uintN argc, Value *vp)
|
|
|
|
{
|
|
|
|
JSObject *obj = &vp[0].toObject();
|
|
|
|
JS_ASSERT(obj->isFunction());
|
2010-09-16 19:31:11 -07:00
|
|
|
JS_ASSERT(obj->isBoundFunction());
|
2010-08-13 10:42:33 -07:00
|
|
|
|
|
|
|
LeaveTrace(cx);
|
|
|
|
|
2010-08-16 12:35:04 -07:00
|
|
|
bool constructing = IsConstructing(vp);
|
2010-08-13 10:42:33 -07:00
|
|
|
|
|
|
|
/* 15.3.4.5.1 step 1, 15.3.4.5.2 step 3. */
|
2011-04-08 19:51:40 -07:00
|
|
|
uintN argslen = obj->getBoundFunctionArgumentCount();
|
2010-08-13 10:42:33 -07:00
|
|
|
|
|
|
|
if (argc + argslen > JS_ARGS_LENGTH_MAX) {
|
|
|
|
js_ReportAllocationOverflow(cx);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 15.3.4.5.1 step 3, 15.3.4.5.2 step 1. */
|
|
|
|
JSObject *target = obj->getBoundFunctionTarget();
|
|
|
|
|
|
|
|
/* 15.3.4.5.1 step 2. */
|
|
|
|
const Value &boundThis = obj->getBoundFunctionThis();
|
|
|
|
|
|
|
|
InvokeArgsGuard args;
|
2011-04-13 09:27:37 -07:00
|
|
|
if (!cx->stack.pushInvokeArgs(cx, argc + argslen, &args))
|
2010-08-13 10:42:33 -07:00
|
|
|
return false;
|
|
|
|
|
|
|
|
/* 15.3.4.5.1, 15.3.4.5.2 step 4. */
|
2011-04-08 19:51:40 -07:00
|
|
|
for (uintN i = 0; i < argslen; i++)
|
|
|
|
args[i] = obj->getBoundFunctionArgument(i);
|
2010-08-13 10:42:33 -07:00
|
|
|
memcpy(args.argv() + argslen, vp + 2, argc * sizeof(Value));
|
|
|
|
|
|
|
|
/* 15.3.4.5.1, 15.3.4.5.2 step 5. */
|
2011-04-08 10:52:48 -07:00
|
|
|
args.calleev().setObject(*target);
|
2010-08-13 10:42:33 -07:00
|
|
|
|
2010-10-12 11:50:03 -07:00
|
|
|
if (!constructing)
|
|
|
|
args.thisv() = boundThis;
|
2010-08-13 10:42:33 -07:00
|
|
|
|
2011-04-13 09:27:37 -07:00
|
|
|
if (constructing ? !InvokeConstructor(cx, args) : !Invoke(cx, args))
|
2010-08-13 10:42:33 -07:00
|
|
|
return false;
|
|
|
|
|
|
|
|
*vp = args.rval();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-02-09 18:17:34 -08:00
|
|
|
}
|
|
|
|
|
2011-04-08 13:31:14 -07:00
|
|
|
#if JS_HAS_GENERATORS
|
|
|
|
static JSBool
|
|
|
|
fun_isGenerator(JSContext *cx, uintN argc, Value *vp)
|
|
|
|
{
|
|
|
|
JSObject *funobj;
|
|
|
|
if (!IsFunctionObject(vp[1], &funobj)) {
|
|
|
|
JS_SET_RVAL(cx, vp, BooleanValue(false));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSFunction *fun = GET_FUNCTION_PRIVATE(cx, funobj);
|
|
|
|
|
|
|
|
bool result = false;
|
|
|
|
if (fun->isInterpreted()) {
|
2011-05-20 11:27:27 -07:00
|
|
|
JSScript *script = fun->script();
|
2011-04-08 13:31:14 -07:00
|
|
|
JS_ASSERT(script->length != 0);
|
|
|
|
result = script->code[0] == JSOP_GENERATOR;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS_SET_RVAL(cx, vp, BooleanValue(result));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-08-13 10:42:33 -07:00
|
|
|
/* ES5 15.3.4.5. */
|
2010-10-25 16:47:11 -07:00
|
|
|
static JSBool
|
2010-08-13 10:42:33 -07:00
|
|
|
fun_bind(JSContext *cx, uintN argc, Value *vp)
|
|
|
|
{
|
|
|
|
/* Step 1. */
|
2011-01-26 13:37:45 -08:00
|
|
|
Value &thisv = vp[1];
|
2010-08-13 10:42:33 -07:00
|
|
|
|
|
|
|
/* Step 2. */
|
2011-01-26 13:37:45 -08:00
|
|
|
if (!js_IsCallable(thisv)) {
|
2011-04-11 01:38:27 -07:00
|
|
|
ReportIncompatibleMethod(cx, vp, &js_FunctionClass);
|
2010-08-13 10:42:33 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-01-26 13:37:45 -08:00
|
|
|
JSObject *target = &thisv.toObject();
|
|
|
|
|
2010-08-13 10:42:33 -07:00
|
|
|
/* Step 3. */
|
|
|
|
Value *args = NULL;
|
|
|
|
uintN argslen = 0;
|
|
|
|
if (argc > 1) {
|
|
|
|
args = vp + 3;
|
|
|
|
argslen = argc - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Steps 15-16. */
|
|
|
|
uintN length = 0;
|
|
|
|
if (target->isFunction()) {
|
|
|
|
uintN nargs = target->getFunctionPrivate()->nargs;
|
|
|
|
if (nargs > argslen)
|
|
|
|
length = nargs - argslen;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Step 4-6, 10-11. */
|
|
|
|
JSAtom *name = target->isFunction() ? target->getFunctionPrivate()->atom : NULL;
|
|
|
|
|
2011-05-24 14:35:19 -07:00
|
|
|
char *printName = NULL;
|
|
|
|
#ifdef DEBUG
|
|
|
|
static int boundCount = 0;
|
|
|
|
printName = (char *) alloca(20);
|
|
|
|
JS_snprintf(printName, 20, "BoundFunction:%d", ++boundCount);
|
|
|
|
#endif
|
|
|
|
|
2010-08-13 10:42:33 -07:00
|
|
|
/* NB: Bound functions abuse |parent| to store their target. */
|
|
|
|
JSObject *funobj =
|
2010-08-16 12:35:04 -07:00
|
|
|
js_NewFunction(cx, NULL, CallOrConstructBoundFunction, length,
|
2010-10-29 08:05:55 -07:00
|
|
|
JSFUN_CONSTRUCTOR, target, name,
|
2011-05-24 14:35:19 -07:00
|
|
|
JS_TypeHandlerDynamic, printName);
|
2010-08-13 10:42:33 -07:00
|
|
|
if (!funobj)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* Steps 7-9. */
|
|
|
|
Value thisArg = argc >= 1 ? vp[2] : UndefinedValue();
|
|
|
|
if (!funobj->initBoundFunction(cx, thisArg, args, argslen))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* Steps 17, 19-21 are handled by fun_resolve. */
|
|
|
|
/* Step 18 is the default for new functions. */
|
|
|
|
|
|
|
|
/* Step 22. */
|
|
|
|
vp->setObject(*funobj);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
static JSFunctionSpec function_methods[] = {
|
|
|
|
#if JS_HAS_TOSOURCE
|
2010-10-29 08:05:55 -07:00
|
|
|
JS_FN_TYPE(js_toSource_str, fun_toSource, 0,0, JS_TypeHandlerString),
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif
|
2010-10-29 08:05:55 -07:00
|
|
|
JS_FN_TYPE(js_toString_str, fun_toString, 0,0, JS_TypeHandlerString),
|
2011-05-16 22:59:40 -07:00
|
|
|
JS_FN_TYPE(js_apply_str, js_fun_apply, 2,0, JS_TypeHandlerDynamic),
|
|
|
|
JS_FN_TYPE(js_call_str, js_fun_call, 1,0, JS_TypeHandlerDynamic),
|
2010-10-29 08:05:55 -07:00
|
|
|
JS_FN_TYPE("bind", fun_bind, 1,0, JS_TypeHandlerDynamic),
|
2011-04-08 13:46:19 -07:00
|
|
|
#if JS_HAS_GENERATORS
|
2011-04-08 19:27:14 -07:00
|
|
|
JS_FN_TYPE("isGenerator", fun_isGenerator,0,0, JS_TypeHandlerBool),
|
2011-04-08 13:46:19 -07:00
|
|
|
#endif
|
2007-08-01 21:33:52 -07:00
|
|
|
JS_FS_END
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
2011-03-23 14:33:02 -07:00
|
|
|
/*
|
|
|
|
* Report "malformed formal parameter" iff no illegal char or similar scanner
|
|
|
|
* error was already reported.
|
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
OnBadFormal(JSContext *cx, TokenKind tt)
|
|
|
|
{
|
|
|
|
if (tt != TOK_ERROR)
|
|
|
|
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_BAD_FORMAL);
|
|
|
|
else
|
|
|
|
JS_ASSERT(cx->isExceptionPending());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
static JSBool
|
2010-08-16 12:35:04 -07:00
|
|
|
Function(JSContext *cx, uintN argc, Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-04-08 10:52:51 -07:00
|
|
|
CallArgs call = CallArgsFromVp(argc, vp);
|
|
|
|
|
2011-05-10 14:56:49 -07:00
|
|
|
/* Block this call if security callbacks forbid it. */
|
|
|
|
GlobalObject *global = call.callee().getGlobal();
|
|
|
|
if (!global->isEvalAllowed(cx)) {
|
|
|
|
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_CSP_BLOCKED_FUNCTION);
|
2011-03-23 14:33:02 -07:00
|
|
|
return false;
|
2011-05-10 14:56:49 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-05-10 14:56:49 -07:00
|
|
|
JS::Anchor<JSObject *> obj(NewFunction(cx, *global));
|
|
|
|
if (!obj.get())
|
|
|
|
return false;
|
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.
|
|
|
|
*/
|
2011-03-23 14:33:02 -07:00
|
|
|
JSFunction *fun = js_NewFunction(cx, obj.get(), NULL, 0, JSFUN_LAMBDA | JSFUN_INTERPRETED,
|
2011-05-22 08:59:06 -07:00
|
|
|
global, cx->runtime->atomState.anonymousAtom, NULL, NULL);
|
2008-03-28 15:27:36 -07:00
|
|
|
if (!fun)
|
2011-03-23 14:33:02 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-03-11 16:29:38 -08:00
|
|
|
Bindings bindings(cx);
|
2011-01-05 14:50:30 -08:00
|
|
|
AutoBindingsRooter root(cx, bindings);
|
2010-11-16 15:34:24 -08:00
|
|
|
|
2011-04-08 10:52:51 -07:00
|
|
|
uintN lineno;
|
|
|
|
const char *filename = CurrentScriptFileAndLine(cx, &lineno);
|
|
|
|
|
|
|
|
Value *argv = call.argv();
|
2010-08-16 12:35:04 -07:00
|
|
|
uintN n = argc ? argc - 1 : 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (n > 0) {
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2010-08-16 12:35:04 -07:00
|
|
|
size_t args_length = 0;
|
|
|
|
for (uintN i = 0; i < n; i++) {
|
2007-03-22 10:30:00 -07:00
|
|
|
/* Collect the lengths for all the function-argument arguments. */
|
2010-08-16 12:35:04 -07:00
|
|
|
JSString *arg = js_ValueToString(cx, argv[i]);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!arg)
|
2011-03-23 14:33:02 -07:00
|
|
|
return false;
|
2010-07-14 23:19:36 -07:00
|
|
|
argv[i].setString(arg);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Check for overflow. The < test works because the maximum
|
|
|
|
* JSString length fits in 2 fewer bits than size_t has.
|
|
|
|
*/
|
2010-08-16 12:35:04 -07:00
|
|
|
size_t old_args_length = args_length;
|
2009-06-10 18:29:44 -07:00
|
|
|
args_length = old_args_length + arg->length();
|
2007-03-22 10:30:00 -07:00
|
|
|
if (args_length < old_args_length) {
|
2008-03-12 16:07:47 -07:00
|
|
|
js_ReportAllocationOverflow(cx);
|
2011-03-23 14:33:02 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add 1 for each joining comma and check for overflow (two ways). */
|
2010-08-16 12:35:04 -07:00
|
|
|
size_t old_args_length = args_length;
|
2007-03-22 10:30:00 -07:00
|
|
|
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);
|
2011-03-23 14:33:02 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2011-03-23 14:33:02 -07:00
|
|
|
AutoArenaAllocator aaa(&cx->tempPool);
|
|
|
|
jschar *cp = aaa.alloc<jschar>(args_length + 1);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!cp) {
|
2007-11-22 14:21:18 -08:00
|
|
|
js_ReportOutOfScriptQuota(cx);
|
2011-03-23 14:33:02 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2010-08-16 12:35:04 -07:00
|
|
|
jschar *collected_args = cp;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Concatenate the arguments into the new string, separated by commas.
|
|
|
|
*/
|
2010-08-16 12:35:04 -07:00
|
|
|
for (uintN i = 0; i < n; i++) {
|
|
|
|
JSString *arg = argv[i].toString();
|
|
|
|
size_t arg_length = arg->length();
|
2010-12-06 10:26:58 -08:00
|
|
|
const jschar *arg_chars = arg->getChars(cx);
|
2011-03-23 14:33:02 -07:00
|
|
|
if (!arg_chars)
|
|
|
|
return false;
|
2010-12-06 10:26:58 -08:00
|
|
|
(void) js_strncpy(cp, arg_chars, arg_length);
|
2007-03-22 10:30:00 -07:00
|
|
|
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. */
|
2010-08-16 12:35:04 -07:00
|
|
|
TokenStream ts(cx);
|
2011-03-23 14:33:02 -07:00
|
|
|
if (!ts.init(collected_args, args_length, filename, lineno, cx->findVersion()))
|
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/* The argument string may be empty or contain no tokens. */
|
2010-08-16 12:35:04 -07:00
|
|
|
TokenKind tt = ts.getToken();
|
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)
|
2011-03-23 14:33:02 -07:00
|
|
|
return OnBadFormal(cx, tt);
|
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
|
|
|
*/
|
2010-08-16 12:35:04 -07:00
|
|
|
JSAtom *atom = ts.currentToken().t_atom;
|
2007-11-19 09:15:45 -08:00
|
|
|
|
|
|
|
/* Check for a duplicate parameter name. */
|
2011-01-03 17:14:55 -08:00
|
|
|
if (bindings.hasBinding(cx, atom)) {
|
2010-11-11 12:40:29 -08:00
|
|
|
JSAutoByteString name;
|
2011-03-23 14:33:02 -07:00
|
|
|
if (!js_AtomToPrintableString(cx, atom, &name))
|
|
|
|
return false;
|
2010-11-11 12:40:29 -08:00
|
|
|
if (!ReportCompileErrorNumber(cx, &ts, NULL,
|
|
|
|
JSREPORT_WARNING | JSREPORT_STRICT,
|
|
|
|
JSMSG_DUPLICATE_FORMAL, name.ptr())) {
|
2011-03-23 14:33:02 -07:00
|
|
|
return false;
|
2010-08-16 12:35:04 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2010-11-16 15:34:24 -08:00
|
|
|
|
|
|
|
uint16 dummy;
|
2011-03-23 14:33:02 -07:00
|
|
|
if (!bindings.addArgument(cx, atom, &dummy))
|
|
|
|
return false;
|
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.
|
|
|
|
*/
|
2010-03-23 17:30:36 -07:00
|
|
|
tt = ts.getToken();
|
2007-03-22 10:30:00 -07:00
|
|
|
if (tt == TOK_EOF)
|
|
|
|
break;
|
|
|
|
if (tt != TOK_COMMA)
|
2011-03-23 14:33:02 -07:00
|
|
|
return OnBadFormal(cx, tt);
|
2010-03-23 17:30:36 -07:00
|
|
|
tt = ts.getToken();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-23 14:33:02 -07:00
|
|
|
JS::Anchor<JSString *> strAnchor(NULL);
|
|
|
|
const jschar *chars;
|
|
|
|
size_t length;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if (argc) {
|
2011-03-23 14:33:02 -07:00
|
|
|
JSString *str = js_ValueToString(cx, argv[argc - 1]);
|
2007-07-28 09:57:30 -07:00
|
|
|
if (!str)
|
2011-03-23 14:33:02 -07:00
|
|
|
return false;
|
|
|
|
strAnchor.set(str);
|
|
|
|
chars = str->getChars(cx);
|
|
|
|
length = str->length();
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
2011-03-23 14:33:02 -07:00
|
|
|
chars = cx->runtime->emptyString->chars();
|
|
|
|
length = 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-04-08 10:52:51 -07:00
|
|
|
JSPrincipals *principals = PrincipalsForCompiledCode(call, cx);
|
|
|
|
bool ok = Compiler::compileFunctionBody(cx, fun, principals, &bindings,
|
|
|
|
chars, length, filename, lineno,
|
|
|
|
cx->findVersion());
|
|
|
|
call.rval().setObject(obj);
|
|
|
|
return ok;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-01-26 18:28:49 -08:00
|
|
|
namespace js {
|
|
|
|
|
2011-03-02 19:57:44 -08:00
|
|
|
bool
|
2011-01-26 18:28:49 -08:00
|
|
|
IsBuiltinFunctionConstructor(JSFunction *fun)
|
|
|
|
{
|
|
|
|
return fun->maybeNative() == Function;
|
|
|
|
}
|
|
|
|
|
2011-02-14 12:04:07 -08:00
|
|
|
const Shape *
|
|
|
|
LookupInterpretedFunctionPrototype(JSContext *cx, JSObject *funobj)
|
|
|
|
{
|
2011-02-15 12:07:23 -08:00
|
|
|
#ifdef DEBUG
|
2011-02-14 12:04:07 -08:00
|
|
|
JSFunction *fun = funobj->getFunctionPrivate();
|
|
|
|
JS_ASSERT(fun->isInterpreted());
|
|
|
|
JS_ASSERT(!fun->isFunctionPrototype());
|
|
|
|
JS_ASSERT(!funobj->isBoundFunction());
|
2011-02-15 12:07:23 -08:00
|
|
|
#endif
|
2011-02-14 12:04:07 -08:00
|
|
|
|
|
|
|
jsid id = ATOM_TO_JSID(cx->runtime->atomState.classPrototypeAtom);
|
|
|
|
const Shape *shape = funobj->nativeLookup(id);
|
|
|
|
if (!shape) {
|
|
|
|
if (!ResolveInterpretedFunctionPrototype(cx, funobj))
|
2011-04-14 13:45:06 -07:00
|
|
|
return NULL;
|
2011-02-14 12:04:07 -08:00
|
|
|
shape = funobj->nativeLookup(id);
|
|
|
|
}
|
|
|
|
JS_ASSERT(!shape->configurable());
|
|
|
|
JS_ASSERT(shape->isDataDescriptor());
|
|
|
|
JS_ASSERT(shape->hasSlot());
|
|
|
|
JS_ASSERT(!shape->isMethod());
|
|
|
|
return shape;
|
|
|
|
}
|
|
|
|
|
2011-02-23 19:38:27 -08:00
|
|
|
} /* namespace js */
|
2011-01-26 18:28:49 -08:00
|
|
|
|
2010-10-25 16:47:11 -07:00
|
|
|
static JSBool
|
2010-08-02 23:52:12 -07:00
|
|
|
ThrowTypeError(JSContext *cx, uintN argc, Value *vp)
|
|
|
|
{
|
|
|
|
JS_ReportErrorFlagsAndNumber(cx, JSREPORT_ERROR, js_GetErrorMessage, NULL,
|
|
|
|
JSMSG_THROW_TYPE_ERROR);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
JSObject *
|
|
|
|
js_InitFunctionClass(JSContext *cx, JSObject *obj)
|
|
|
|
{
|
2010-08-02 23:52:12 -07:00
|
|
|
JSObject *proto = js_InitClass(cx, obj, NULL, &js_FunctionClass, Function, 1,
|
2010-10-29 08:05:55 -07:00
|
|
|
JS_TypeHandlerDynamic, NULL, function_methods, NULL, NULL);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!proto)
|
|
|
|
return NULL;
|
2010-08-02 23:52:12 -07:00
|
|
|
|
2011-03-20 11:01:33 -07:00
|
|
|
/*
|
|
|
|
* The default 'new' object for Function.prototype has unknown properties.
|
|
|
|
* This will be used for generic scripted functions, e.g. from non-compileAndGo code.
|
|
|
|
*/
|
|
|
|
TypeObject *newType = proto->getNewType(cx);
|
2011-05-09 07:12:47 -07:00
|
|
|
if (!newType)
|
2011-03-20 11:01:33 -07:00
|
|
|
return NULL;
|
2011-06-02 10:40:27 -07:00
|
|
|
MarkTypeObjectUnknownProperties(cx, newType);
|
2011-03-20 11:01:33 -07:00
|
|
|
|
2010-10-29 08:05:55 -07:00
|
|
|
JSFunction *fun = js_NewFunction(cx, proto, NULL, 0, JSFUN_INTERPRETED, obj, NULL, NULL, NULL);
|
2008-03-28 15:27:36 -07:00
|
|
|
if (!fun)
|
2009-09-25 05:30:11 -07:00
|
|
|
return NULL;
|
2010-10-03 22:46:39 -07:00
|
|
|
fun->flags |= JSFUN_PROTOTYPE;
|
2010-11-16 15:30:56 -08:00
|
|
|
|
2011-05-14 05:45:13 -07:00
|
|
|
JSScript *script = JSScript::NewScript(cx, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, JSVERSION_DEFAULT);
|
2010-11-16 15:30:56 -08:00
|
|
|
if (!script)
|
|
|
|
return NULL;
|
|
|
|
script->noScriptRval = true;
|
|
|
|
script->code[0] = JSOP_STOP;
|
|
|
|
script->code[1] = SRC_NULL;
|
|
|
|
#ifdef CHECK_SCRIPT_OWNER
|
|
|
|
script->owner = NULL;
|
|
|
|
#endif
|
|
|
|
fun->u.i.script = script;
|
2011-03-10 12:01:11 -08:00
|
|
|
fun->getType()->asFunction()->script = script;
|
|
|
|
script->fun = fun;
|
2011-02-11 13:23:24 -08:00
|
|
|
js_CallNewScriptHook(cx, script, fun);
|
2010-08-02 23:52:12 -07:00
|
|
|
|
2011-01-08 15:01:52 -08:00
|
|
|
if (obj->isGlobal()) {
|
2010-08-02 23:52:12 -07:00
|
|
|
/* ES5 13.2.3: Construct the unique [[ThrowTypeError]] function object. */
|
2011-04-18 13:50:47 -07:00
|
|
|
JSFunction *throwTypeError =
|
2010-08-02 23:52:12 -07:00
|
|
|
js_NewFunction(cx, NULL, reinterpret_cast<Native>(ThrowTypeError), 0,
|
2010-10-29 08:05:55 -07:00
|
|
|
0, obj, NULL, JS_TypeHandlerVoid, "ThrowTypeError");
|
2010-08-02 23:52:12 -07:00
|
|
|
if (!throwTypeError)
|
|
|
|
return NULL;
|
|
|
|
|
2011-04-18 13:50:47 -07:00
|
|
|
obj->asGlobal()->setThrowTypeError(throwTypeError);
|
2010-08-02 23:52:12 -07:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
return proto;
|
|
|
|
}
|
|
|
|
|
2008-03-29 03:34:29 -07:00
|
|
|
JSFunction *
|
2010-07-14 23:19:36 -07:00
|
|
|
js_NewFunction(JSContext *cx, JSObject *funobj, Native native, uintN nargs,
|
2010-10-29 08:05:55 -07:00
|
|
|
uintN flags, JSObject *parent, JSAtom *atom,
|
|
|
|
JSTypeHandler handler, const char *fullName)
|
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) {
|
2010-03-31 22:13:51 -07:00
|
|
|
JS_ASSERT(funobj->isFunction());
|
2010-03-04 23:52:52 -08:00
|
|
|
funobj->setParent(parent);
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
2010-12-18 20:44:51 -08:00
|
|
|
funobj = NewFunction(cx, parent);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!funobj)
|
|
|
|
return NULL;
|
2010-12-18 20:44:51 -08:00
|
|
|
if (handler) {
|
2011-05-19 09:15:12 -07:00
|
|
|
TypeObject *type = cx->compartment->types.newTypeObject(cx, NULL, fullName, "",
|
|
|
|
true, false,
|
|
|
|
funobj->getProto());
|
2011-03-16 10:02:19 -07:00
|
|
|
if (!type || !funobj->setTypeAndUniqueShape(cx, type))
|
2010-12-18 20:44:51 -08:00
|
|
|
return NULL;
|
2011-05-19 09:15:12 -07:00
|
|
|
type->asFunction()->handler = handler;
|
|
|
|
type->asFunction()->singleton = funobj;
|
2010-12-18 20:44:51 -08:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2009-08-04 14:06:55 -07:00
|
|
|
JS_ASSERT(!funobj->getPrivate());
|
2008-04-02 00:46:12 -07:00
|
|
|
fun = (JSFunction *) funobj;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/* Initialize all function members. */
|
2010-03-04 20:44:09 -08:00
|
|
|
fun->nargs = uint16(nargs);
|
2010-08-16 12:35:04 -07:00
|
|
|
fun->flags = flags & (JSFUN_FLAGS_MASK | JSFUN_KINDMASK | JSFUN_TRCINFO);
|
2009-04-05 21:17:22 -07:00
|
|
|
if ((flags & JSFUN_KINDMASK) >= JSFUN_INTERPRETED) {
|
2008-03-29 03:34:29 -07:00
|
|
|
JS_ASSERT(!native);
|
|
|
|
JS_ASSERT(nargs == 0);
|
2009-06-04 18:58:47 -07:00
|
|
|
fun->u.i.skipmin = 0;
|
|
|
|
fun->u.i.wrapper = false;
|
2008-03-29 03:34:29 -07:00
|
|
|
fun->u.i.script = NULL;
|
|
|
|
} else {
|
2009-03-03 22:53:27 -08:00
|
|
|
fun->u.n.clasp = NULL;
|
2009-09-04 13:44:31 -07:00
|
|
|
if (flags & JSFUN_TRCINFO) {
|
2008-10-08 15:08:33 -07:00
|
|
|
#ifdef JS_TRACER
|
2009-09-04 13:44:31 -07:00
|
|
|
JSNativeTraceInfo *trcinfo =
|
|
|
|
JS_FUNC_TO_DATA_PTR(JSNativeTraceInfo *, native);
|
2010-07-14 23:19:36 -07:00
|
|
|
fun->u.n.native = (js::Native) trcinfo->native;
|
2009-03-03 22:53:27 -08:00
|
|
|
fun->u.n.trcinfo = trcinfo;
|
2008-10-08 15:08:33 -07:00
|
|
|
#else
|
2009-03-03 22:53:27 -08:00
|
|
|
fun->u.n.trcinfo = NULL;
|
2008-10-08 15:08:33 -07:00
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
fun->u.n.native = native;
|
2009-03-03 22:53:27 -08:00
|
|
|
fun->u.n.trcinfo = NULL;
|
2008-10-08 15:08:33 -07:00
|
|
|
}
|
2009-05-05 14:26:06 -07:00
|
|
|
JS_ASSERT(fun->u.n.native);
|
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. */
|
2009-08-04 14:06:55 -07:00
|
|
|
FUN_OBJECT(fun)->setPrivate(fun);
|
2008-03-29 03:34:29 -07:00
|
|
|
return fun;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-01-15 11:32:14 -08:00
|
|
|
JSObject * JS_FASTCALL
|
2010-02-19 09:44:23 -08:00
|
|
|
js_CloneFunctionObject(JSContext *cx, JSFunction *fun, JSObject *parent,
|
|
|
|
JSObject *proto)
|
2007-11-18 06:10:28 -08:00
|
|
|
{
|
2010-02-19 09:44:23 -08:00
|
|
|
JS_ASSERT(parent);
|
|
|
|
JS_ASSERT(proto);
|
|
|
|
|
2010-09-29 23:17:29 -07:00
|
|
|
JSObject *clone;
|
|
|
|
if (cx->compartment == fun->compartment()) {
|
|
|
|
/*
|
|
|
|
* The cloned function object does not need the extra JSFunction members
|
|
|
|
* beyond JSObject as it points to fun via the private slot.
|
|
|
|
*/
|
2010-12-18 20:44:51 -08:00
|
|
|
clone = NewNativeClassInstance(cx, &js_FunctionClass, proto, parent);
|
2011-03-17 09:42:56 -07:00
|
|
|
if (!clone)
|
2010-09-29 23:17:29 -07:00
|
|
|
return NULL;
|
2011-03-17 09:42:56 -07:00
|
|
|
|
|
|
|
/*
|
2011-06-03 20:48:16 -07:00
|
|
|
* We can use the same type as the original function provided that (a)
|
|
|
|
* its prototype is correct, and (b) its type is not a singleton. The
|
|
|
|
* first case will hold in all compileAndGo code, and the second case
|
|
|
|
* will have been caught by CloneFunctionObject coming from function
|
|
|
|
* definitions or read barriers, so will not get here.
|
2011-03-17 09:42:56 -07:00
|
|
|
*/
|
2011-06-03 20:48:16 -07:00
|
|
|
if (fun->getProto() == proto && !fun->getType()->singleton)
|
2011-03-17 09:42:56 -07:00
|
|
|
clone->setTypeAndShape(fun->getType(), fun->lastProperty());
|
|
|
|
|
2010-09-29 23:17:29 -07:00
|
|
|
clone->setPrivate(fun);
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Across compartments we have to deep copy JSFunction and clone the
|
|
|
|
* script (for interpreted functions).
|
|
|
|
*/
|
2010-12-18 20:44:51 -08:00
|
|
|
clone = NewFunction(cx, parent);
|
2011-03-17 09:42:56 -07:00
|
|
|
if (!clone)
|
2010-09-29 23:17:29 -07:00
|
|
|
return NULL;
|
2010-12-19 09:28:42 -08:00
|
|
|
|
2010-09-29 23:17:29 -07:00
|
|
|
JSFunction *cfun = (JSFunction *) clone;
|
|
|
|
cfun->nargs = fun->nargs;
|
|
|
|
cfun->flags = fun->flags;
|
|
|
|
cfun->u = fun->getFunctionPrivate()->u;
|
|
|
|
cfun->atom = fun->atom;
|
|
|
|
clone->setPrivate(cfun);
|
|
|
|
if (cfun->isInterpreted()) {
|
2011-05-20 11:27:27 -07:00
|
|
|
JSScript *script = cfun->script();
|
2010-09-29 23:17:29 -07:00
|
|
|
JS_ASSERT(script);
|
2010-11-16 15:30:56 -08:00
|
|
|
JS_ASSERT(script->compartment == fun->compartment());
|
|
|
|
JS_ASSERT(script->compartment != cx->compartment);
|
|
|
|
|
|
|
|
cfun->u.i.script = js_CloneScript(cx, script);
|
2011-05-20 11:27:27 -07:00
|
|
|
if (!cfun->script())
|
2010-11-16 15:30:56 -08:00
|
|
|
return NULL;
|
2011-03-20 11:22:33 -07:00
|
|
|
if (!cfun->u.i.script->typeSetFunction(cx, cfun))
|
2011-03-07 08:25:27 -08:00
|
|
|
return NULL;
|
|
|
|
|
2010-09-29 23:17:29 -07:00
|
|
|
#ifdef CHECK_SCRIPT_OWNER
|
2010-11-16 15:30:56 -08:00
|
|
|
cfun->script()->owner = NULL;
|
2010-09-29 23:17:29 -07:00
|
|
|
#endif
|
2010-11-16 15:30:56 -08:00
|
|
|
js_CallNewScriptHook(cx, cfun->script(), cfun);
|
2011-03-17 09:42:56 -07:00
|
|
|
} else {
|
2011-05-19 09:15:12 -07:00
|
|
|
TypeObject *type = cx->compartment->types.newTypeObject(cx, NULL, "ClonedFunction", "",
|
|
|
|
true, false,
|
|
|
|
clone->getProto());
|
2011-03-17 09:42:56 -07:00
|
|
|
if (!type || !clone->setTypeAndUniqueShape(cx, type))
|
|
|
|
return NULL;
|
2011-05-09 07:12:47 -07:00
|
|
|
if (fun->getType()->unknownProperties())
|
2011-06-02 10:40:27 -07:00
|
|
|
MarkTypeObjectUnknownProperties(cx, type);
|
2011-05-09 07:12:47 -07:00
|
|
|
else
|
2011-05-19 09:15:12 -07:00
|
|
|
type->asFunction()->handler = fun->getType()->asFunction()->handler;
|
2010-09-29 23:17:29 -07:00
|
|
|
}
|
|
|
|
}
|
2008-04-02 00:46:12 -07:00
|
|
|
return clone;
|
2008-03-29 03:34:29 -07:00
|
|
|
}
|
|
|
|
|
2010-02-19 09:44:23 -08:00
|
|
|
#ifdef JS_TRACER
|
2010-03-01 18:47:17 -08:00
|
|
|
JS_DEFINE_CALLINFO_4(extern, OBJECT, js_CloneFunctionObject, CONTEXT, FUNCTION, OBJECT, OBJECT, 0,
|
2010-07-27 22:33:42 -07:00
|
|
|
nanojit::ACCSET_STORE_ANY)
|
2010-02-19 09:44:23 -08:00
|
|
|
#endif
|
2010-01-15 11:32:14 -08:00
|
|
|
|
2009-06-03 11:19:20 -07:00
|
|
|
/*
|
|
|
|
* Create a new flat closure, but don't initialize the imported upvar
|
|
|
|
* values. The tracer calls this function and then initializes the upvar
|
|
|
|
* slots on trace.
|
|
|
|
*/
|
|
|
|
JSObject * JS_FASTCALL
|
|
|
|
js_AllocFlatClosure(JSContext *cx, JSFunction *fun, JSObject *scopeChain)
|
2009-04-05 21:17:22 -07:00
|
|
|
{
|
2010-11-16 15:34:24 -08:00
|
|
|
JS_ASSERT(fun->isFlatClosure());
|
|
|
|
JS_ASSERT(JSScript::isValidOffset(fun->script()->upvarsOffset) ==
|
|
|
|
fun->script()->bindings.hasUpvars());
|
|
|
|
JS_ASSERT_IF(JSScript::isValidOffset(fun->script()->upvarsOffset),
|
|
|
|
fun->script()->upvars()->length == fun->script()->bindings.countUpvars());
|
2009-04-05 21:17:22 -07:00
|
|
|
|
2010-02-19 09:44:23 -08:00
|
|
|
JSObject *closure = CloneFunctionObject(cx, fun, scopeChain);
|
2009-07-31 11:34:33 -07:00
|
|
|
if (!closure)
|
2009-04-05 21:17:22 -07:00
|
|
|
return closure;
|
2009-07-31 11:34:33 -07:00
|
|
|
|
2010-11-16 15:34:24 -08:00
|
|
|
uint32 nslots = fun->script()->bindings.countUpvars();
|
2010-06-18 17:43:02 -07:00
|
|
|
if (nslots == 0)
|
2009-07-31 11:34:33 -07:00
|
|
|
return closure;
|
2010-08-29 11:57:08 -07:00
|
|
|
|
2011-03-31 01:14:12 -07:00
|
|
|
Value *upvars = (Value *) cx->malloc_(nslots * sizeof(Value));
|
2010-08-29 11:57:08 -07:00
|
|
|
if (!upvars)
|
2009-04-05 21:17:22 -07:00
|
|
|
return NULL;
|
|
|
|
|
2010-08-29 11:57:08 -07:00
|
|
|
closure->setFlatClosureUpvars(upvars);
|
2009-06-03 11:19:20 -07:00
|
|
|
return closure;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS_DEFINE_CALLINFO_3(extern, OBJECT, js_AllocFlatClosure,
|
2010-07-27 22:33:42 -07:00
|
|
|
CONTEXT, FUNCTION, OBJECT, 0, nanojit::ACCSET_STORE_ANY)
|
2009-06-03 11:19:20 -07:00
|
|
|
|
2010-12-29 23:46:50 -08:00
|
|
|
JSObject *
|
2010-10-06 10:41:36 -07:00
|
|
|
js_NewFlatClosure(JSContext *cx, JSFunction *fun, JSOp op, size_t oplen)
|
2009-06-03 11:19:20 -07:00
|
|
|
{
|
2010-02-11 14:56:59 -08:00
|
|
|
/*
|
2010-12-29 23:46:50 -08:00
|
|
|
* Flat closures cannot yet be partial, that is, all upvars must be copied,
|
|
|
|
* or the closure won't be flattened. Therefore they do not need to search
|
|
|
|
* enclosing scope objects via JSOP_NAME, etc.
|
|
|
|
*
|
|
|
|
* FIXME: bug 545759 proposes to enable partial flat closures. Fixing this
|
|
|
|
* bug requires a GetScopeChainFast call here, along with JS_REQUIRES_STACK
|
|
|
|
* annotations on this function's prototype and definition.
|
2010-02-11 14:56:59 -08:00
|
|
|
*/
|
2010-12-29 23:46:50 -08:00
|
|
|
VOUCH_DOES_NOT_REQUIRE_STACK();
|
|
|
|
JSObject *scopeChain = &cx->fp()->scopeChain();
|
2010-02-11 14:56:59 -08:00
|
|
|
|
|
|
|
JSObject *closure = js_AllocFlatClosure(cx, fun, scopeChain);
|
2010-11-16 15:34:24 -08:00
|
|
|
if (!closure || !fun->script()->bindings.hasUpvars())
|
2009-06-08 12:13:37 -07:00
|
|
|
return closure;
|
2009-06-03 11:19:20 -07:00
|
|
|
|
2010-08-29 11:57:08 -07:00
|
|
|
Value *upvars = closure->getFlatClosureUpvars();
|
2011-05-20 11:27:27 -07:00
|
|
|
uintN level = fun->script()->staticLevel;
|
2010-11-16 15:34:24 -08:00
|
|
|
JSUpvarArray *uva = fun->script()->upvars();
|
2009-04-05 21:17:22 -07:00
|
|
|
|
2011-03-01 13:10:05 -08:00
|
|
|
for (uint32 i = 0, n = uva->length; i < n; i++) {
|
2010-08-29 11:57:08 -07:00
|
|
|
upvars[i] = GetUpvar(cx, level, uva->vector[i]);
|
2011-05-09 07:12:47 -07:00
|
|
|
fun->script()->typeSetUpvar(cx, i, upvars[i]);
|
2011-03-01 13:10:05 -08:00
|
|
|
}
|
2009-04-05 21:17:22 -07:00
|
|
|
|
|
|
|
return closure;
|
|
|
|
}
|
|
|
|
|
2009-06-04 18:58:47 -07:00
|
|
|
JSObject *
|
|
|
|
js_NewDebuggableFlatClosure(JSContext *cx, JSFunction *fun)
|
|
|
|
{
|
2010-08-09 22:43:33 -07:00
|
|
|
JS_ASSERT(cx->fp()->fun()->flags & JSFUN_HEAVYWEIGHT);
|
|
|
|
JS_ASSERT(!cx->fp()->fun()->optimizedClosure());
|
2010-03-02 17:30:54 -08:00
|
|
|
JS_ASSERT(FUN_FLAT_CLOSURE(fun));
|
2009-06-04 18:58:47 -07:00
|
|
|
|
2010-08-22 16:00:20 -07:00
|
|
|
return WrapEscapingClosure(cx, cx->fp(), fun);
|
2009-06-04 18:58:47 -07:00
|
|
|
}
|
|
|
|
|
2008-03-29 03:34:29 -07:00
|
|
|
JSFunction *
|
2010-10-28 08:15:53 -07:00
|
|
|
js_DefineFunction(JSContext *cx, JSObject *obj, jsid id, Native native,
|
2010-10-29 08:05:55 -07:00
|
|
|
uintN nargs, uintN attrs,
|
|
|
|
JSTypeHandler handler, const char *fullName)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-02-09 11:31:40 -08:00
|
|
|
PropertyOp gop;
|
|
|
|
StrictPropertyOp sop;
|
2009-04-05 21:17:22 -07:00
|
|
|
JSFunction *fun;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-12-18 20:44:51 -08:00
|
|
|
if (!handler) {
|
2010-12-28 11:53:50 -08:00
|
|
|
handler = JS_TypeHandlerDynamic;
|
2010-12-18 20:44:51 -08:00
|
|
|
if (!fullName)
|
|
|
|
fullName = "Unknown";
|
|
|
|
}
|
|
|
|
JS_ASSERT(fullName);
|
2010-10-29 08:05:55 -07:00
|
|
|
|
2009-04-05 21:17:22 -07:00
|
|
|
if (attrs & JSFUN_STUB_GSOPS) {
|
|
|
|
/*
|
|
|
|
* JSFUN_STUB_GSOPS is a request flag only, not stored in fun->flags or
|
|
|
|
* the defined property's attributes. This allows us to encode another,
|
|
|
|
* internal flag using the same bit, JSFUN_EXPR_CLOSURE -- see jsfun.h
|
|
|
|
* for more on this.
|
|
|
|
*/
|
|
|
|
attrs &= ~JSFUN_STUB_GSOPS;
|
2011-02-09 11:31:40 -08:00
|
|
|
gop = PropertyStub;
|
|
|
|
sop = StrictPropertyStub;
|
2009-04-05 21:17:22 -07:00
|
|
|
} else {
|
2011-02-09 11:31:40 -08:00
|
|
|
gop = NULL;
|
|
|
|
sop = NULL;
|
2009-04-05 21:17:22 -07:00
|
|
|
}
|
2010-11-09 12:09:07 -08:00
|
|
|
|
|
|
|
/*
|
2010-11-10 10:59:02 -08:00
|
|
|
* Historically, all objects have had a parent member as intrinsic scope
|
|
|
|
* chain link. We want to move away from this universal parent, but JS
|
|
|
|
* requires that function objects have something like parent (ES3 and ES5
|
|
|
|
* call it the [[Scope]] internal property), to bake a particular static
|
|
|
|
* scope environment into each function object.
|
2010-11-09 12:09:07 -08:00
|
|
|
*
|
2010-11-10 10:59:02 -08:00
|
|
|
* All function objects thus have parent, including all native functions.
|
|
|
|
* All native functions defined by the JS_DefineFunction* APIs are created
|
|
|
|
* via the call below to js_NewFunction, which passes obj as the parent
|
|
|
|
* parameter, and so binds fun's parent to obj using JSObject::setParent,
|
|
|
|
* under js_NewFunction (in JSObject::init, called from NewObject -- see
|
|
|
|
* jsobjinlines.h).
|
|
|
|
*
|
|
|
|
* But JSObject::setParent sets the DELEGATE object flag on its receiver,
|
|
|
|
* to mark the object as a proto or parent of another object. Such objects
|
|
|
|
* may intervene in property lookups and scope chain searches, so require
|
|
|
|
* special handling when caching lookup and search results (since such
|
|
|
|
* intervening objects can in general grow shadowing properties later).
|
|
|
|
*
|
|
|
|
* Thus using setParent prematurely flags certain objects, notably class
|
|
|
|
* prototypes, so that defining native methods on them, where the method's
|
|
|
|
* name (e.g., toString) is already bound on Object.prototype, triggers
|
|
|
|
* shadowingShapeChange events and gratuitous shape regeneration.
|
|
|
|
*
|
|
|
|
* To fix this longstanding bug, we set check whether obj is already a
|
|
|
|
* delegate, and if not, then if js_NewFunction flagged obj as a delegate,
|
|
|
|
* we clear the flag.
|
|
|
|
*
|
|
|
|
* We thus rely on the fact that native functions (including indirect eval)
|
|
|
|
* do not use the property cache or equivalent JIT techniques that require
|
|
|
|
* this bit to be set on their parent-linked scope chain objects.
|
|
|
|
*
|
|
|
|
* Note: we keep API compatibility by setting parent to obj for all native
|
|
|
|
* function objects, even if obj->getGlobal() would suffice. This should be
|
|
|
|
* revisited when parent is narrowed to exist only for function objects and
|
|
|
|
* possibly a few prehistoric scope objects (e.g. event targets).
|
2010-11-10 18:14:35 -08:00
|
|
|
*
|
|
|
|
* FIXME: bug 611190.
|
2010-11-09 12:09:07 -08:00
|
|
|
*/
|
2010-11-10 10:59:02 -08:00
|
|
|
bool wasDelegate = obj->isDelegate();
|
2010-11-09 12:09:07 -08:00
|
|
|
|
2010-08-03 12:20:48 -07:00
|
|
|
fun = js_NewFunction(cx, NULL, native, nargs,
|
2010-08-16 12:35:04 -07:00
|
|
|
attrs & (JSFUN_FLAGS_MASK | JSFUN_TRCINFO),
|
2010-11-10 10:59:02 -08:00
|
|
|
obj,
|
2010-11-12 09:02:43 -08:00
|
|
|
JSID_IS_ATOM(id) ? JSID_TO_ATOM(id) : NULL,
|
|
|
|
handler, fullName);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!fun)
|
|
|
|
return NULL;
|
2010-11-10 10:59:02 -08:00
|
|
|
|
|
|
|
if (!wasDelegate && obj->isDelegate())
|
|
|
|
obj->clearDelegate();
|
|
|
|
|
2011-02-09 11:31:40 -08:00
|
|
|
if (!obj->defineProperty(cx, id, ObjectValue(*fun), gop, sop, attrs & ~JSFUN_FLAGS_MASK))
|
2007-03-22 10:30:00 -07:00
|
|
|
return NULL;
|
2010-10-29 08:05:55 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
return fun;
|
|
|
|
}
|
|
|
|
|
2011-04-13 09:27:37 -07:00
|
|
|
JS_STATIC_ASSERT((JSV2F_CONSTRUCT & JSV2F_SEARCH_STACK) == 0);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
JSFunction *
|
2010-07-14 23:19:36 -07:00
|
|
|
js_ValueToFunction(JSContext *cx, const Value *vp, uintN flags)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-07-14 23:19:36 -07:00
|
|
|
JSObject *funobj;
|
|
|
|
if (!IsFunctionObject(*vp, &funobj)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
js_ReportIsNotFunction(cx, vp, flags);
|
|
|
|
return NULL;
|
|
|
|
}
|
2010-07-14 23:19:36 -07:00
|
|
|
return GET_FUNCTION_PRIVATE(cx, funobj);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
JSObject *
|
2010-07-14 23:19:36 -07:00
|
|
|
js_ValueToFunctionObject(JSContext *cx, Value *vp, uintN flags)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-07-14 23:19:36 -07:00
|
|
|
JSObject *funobj;
|
2010-08-17 09:44:41 -07:00
|
|
|
if (!IsFunctionObject(*vp, &funobj)) {
|
|
|
|
js_ReportIsNotFunction(cx, vp, flags);
|
2007-03-22 10:30:00 -07:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-08-17 09:44:41 -07:00
|
|
|
return funobj;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
JSObject *
|
2010-07-14 23:19:36 -07:00
|
|
|
js_ValueToCallableObject(JSContext *cx, Value *vp, uintN flags)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-07-14 23:19:36 -07:00
|
|
|
if (vp->isObject()) {
|
|
|
|
JSObject *callable = &vp->toObject();
|
|
|
|
if (callable->isCallable())
|
|
|
|
return callable;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2010-08-17 09:44:41 -07:00
|
|
|
|
|
|
|
js_ReportIsNotFunction(cx, vp, flags);
|
|
|
|
return NULL;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-07-14 23:19:36 -07:00
|
|
|
js_ReportIsNotFunction(JSContext *cx, const Value *vp, uintN flags)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-03-03 18:10:13 -08:00
|
|
|
const char *name = NULL, *source = NULL;
|
2010-03-28 22:05:50 -07:00
|
|
|
AutoValueRooter tvr(cx);
|
2010-05-29 19:04:01 -07:00
|
|
|
uintN error = (flags & JSV2F_CONSTRUCT) ? JSMSG_NOT_CONSTRUCTOR : JSMSG_NOT_FUNCTION;
|
2010-03-03 18:10:13 -08:00
|
|
|
LeaveTrace(cx);
|
2010-07-30 14:49:29 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We try to the print the code that produced vp if vp is a value in the
|
|
|
|
* most recent interpreted stack frame. Note that additional values, not
|
|
|
|
* directly produced by the script, may have been pushed onto the frame's
|
2010-09-02 16:03:11 -07:00
|
|
|
* expression stack (e.g. by pushInvokeArgs) thereby incrementing sp past
|
2011-01-03 09:06:35 -08:00
|
|
|
* the depth simulated by ReconstructPCStack.
|
|
|
|
*
|
|
|
|
* Conversely, values may have been popped from the stack in preparation
|
|
|
|
* for a call (e.g., by SplatApplyArgs). Since we must pass an offset from
|
|
|
|
* the top of the simulated stack to js_ReportValueError3, we do bounds
|
|
|
|
* checking using the minimum of both the simulated and actual stack depth.
|
2010-07-30 14:49:29 -07:00
|
|
|
*/
|
|
|
|
ptrdiff_t spindex = 0;
|
|
|
|
|
2010-03-03 18:10:13 -08:00
|
|
|
FrameRegsIter i(cx);
|
|
|
|
while (!i.done() && !i.pc())
|
|
|
|
++i;
|
|
|
|
|
2010-07-30 14:49:29 -07:00
|
|
|
if (!i.done()) {
|
2010-08-09 22:43:33 -07:00
|
|
|
uintN depth = js_ReconstructStackDepth(cx, i.fp()->script(), i.pc());
|
2010-07-30 14:49:29 -07:00
|
|
|
Value *simsp = i.fp()->base() + depth;
|
2011-01-03 09:06:35 -08:00
|
|
|
if (i.fp()->base() <= vp && vp < Min(simsp, i.sp()))
|
2010-07-30 14:49:29 -07:00
|
|
|
spindex = vp - simsp;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!spindex)
|
|
|
|
spindex = ((flags & JSV2F_SEARCH_STACK) ? JSDVG_SEARCH_STACK : JSDVG_IGNORE_STACK);
|
2010-03-03 18:10:13 -08:00
|
|
|
|
|
|
|
js_ReportValueError3(cx, error, spindex, *vp, NULL, name, source);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|