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>
|
2011-10-10 23:00:28 -07:00
|
|
|
|
|
|
|
#include "mozilla/Util.h"
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "jstypes.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"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "jscntxt.h"
|
2012-02-20 02:58:00 -08:00
|
|
|
#include "jsexn.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"
|
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 "jsscope.h"
|
|
|
|
#include "jsscript.h"
|
|
|
|
#include "jsstr.h"
|
2011-10-17 14:54:28 -07:00
|
|
|
|
2011-10-17 14:54:30 -07:00
|
|
|
#include "frontend/BytecodeCompiler.h"
|
2011-10-27 07:15:40 -07:00
|
|
|
#include "frontend/BytecodeEmitter.h"
|
2011-10-17 14:54:28 -07:00
|
|
|
#include "frontend/TokenStream.h"
|
2011-07-27 16:03:34 -07:00
|
|
|
#include "vm/Debugger.h"
|
2012-02-20 13:56:10 -08:00
|
|
|
#include "vm/MethodGuard.h"
|
|
|
|
#include "vm/ScopeObject.h"
|
2012-02-20 02:58:00 -08:00
|
|
|
#include "vm/Xdr.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#if JS_HAS_GENERATORS
|
|
|
|
# include "jsiter.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"
|
2011-04-25 19:40:00 -07:00
|
|
|
#include "vm/ArgumentsObject-inl.h"
|
2012-02-20 13:56:10 -08:00
|
|
|
#include "vm/ScopeObject-inl.h"
|
2011-04-13 09:27:37 -07:00
|
|
|
#include "vm/Stack-inl.h"
|
|
|
|
|
2011-10-10 23:00:28 -07:00
|
|
|
using namespace mozilla;
|
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
|
|
|
|
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
|
|
|
{
|
2011-04-11 01:38:27 -07:00
|
|
|
while (!obj->isFunction()) {
|
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-10-11 15:28:54 -07:00
|
|
|
JSFunction *fun = obj->toFunction();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-07-01 16:24:32 -07:00
|
|
|
/*
|
|
|
|
* Mark the function's script as uninlineable, to expand any of its
|
|
|
|
* frames on the stack before we go looking for them. This allows the
|
|
|
|
* below walk to only check each explicit frame rather than needing to
|
|
|
|
* check any calls that were inlined.
|
|
|
|
*/
|
2011-07-21 07:28:01 -07:00
|
|
|
if (fun->isInterpreted()) {
|
|
|
|
fun->script()->uninlineable = true;
|
2011-07-15 10:14:07 -07:00
|
|
|
MarkTypeObjectFlags(cx, fun, OBJECT_FLAG_UNINLINEABLE);
|
2011-07-21 07:28:01 -07:00
|
|
|
}
|
2011-07-01 16:24:32 -07:00
|
|
|
|
2011-06-17 15:55:55 -07:00
|
|
|
/* Set to early to null in case of error */
|
|
|
|
vp->setNull();
|
2011-03-26 19:07:13 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/* Find fun's top-most activation record. */
|
2012-04-13 13:45:01 -07:00
|
|
|
StackIter iter(cx);
|
|
|
|
for (; !iter.done(); ++iter) {
|
|
|
|
if (!iter.isFunctionFrame() || iter.isEvalFrame())
|
2012-02-03 00:11:31 -08:00
|
|
|
continue;
|
2012-04-27 15:03:12 -07:00
|
|
|
if (iter.callee() == fun)
|
2012-02-03 00:11:31 -08:00
|
|
|
break;
|
|
|
|
}
|
2012-04-13 13:45:01 -07:00
|
|
|
if (iter.done())
|
2011-06-27 13:36:11 -07:00
|
|
|
return true;
|
|
|
|
|
2012-04-13 18:27:57 -07:00
|
|
|
StackFrame *fp = iter.fp();
|
2012-04-13 13:45:01 -07:00
|
|
|
|
|
|
|
if (JSID_IS_ATOM(id, cx->runtime->atomState.argumentsAtom)) {
|
|
|
|
/* Warn if strict about f.arguments or equivalent unqualified uses. */
|
|
|
|
if (!JS_ReportErrorFlagsAndNumber(cx, JSREPORT_WARNING | JSREPORT_STRICT, js_GetErrorMessage,
|
|
|
|
NULL, JSMSG_DEPRECATED_USAGE, js_arguments_str)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ArgumentsObject *argsobj = ArgumentsObject::createUnexpected(cx, fp);
|
|
|
|
if (!argsobj)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
*vp = ObjectValue(*argsobj);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-03-26 19:07:13 -07:00
|
|
|
#ifdef JS_METHODJIT
|
2011-07-01 16:24:32 -07:00
|
|
|
if (JSID_IS_ATOM(id, cx->runtime->atomState.callerAtom) && fp && fp->prev()) {
|
2011-06-05 22:39:45 -07:00
|
|
|
/*
|
|
|
|
* If the frame was called from within an inlined frame, mark the
|
|
|
|
* innermost function as uninlineable to expand its frame and allow us
|
|
|
|
* to recover its callee object.
|
|
|
|
*/
|
2011-03-26 19:07:13 -07:00
|
|
|
JSInlinedSite *inlined;
|
2012-01-18 16:40:18 -08:00
|
|
|
jsbytecode *prevpc = fp->prev()->pcQuadratic(cx->stack, fp, &inlined);
|
2011-03-26 19:07:13 -07:00
|
|
|
if (inlined) {
|
2012-01-18 16:40:18 -08:00
|
|
|
mjit::JITChunk *chunk = fp->prev()->jit()->chunk(prevpc);
|
|
|
|
JSFunction *fun = chunk->inlineFrames()[inlined->inlineIndex].fun;
|
2011-07-21 07:28:01 -07:00
|
|
|
fun->script()->uninlineable = true;
|
2011-07-15 10:14:07 -07:00
|
|
|
MarkTypeObjectFlags(cx, fun, OBJECT_FLAG_UNINLINEABLE);
|
2011-03-26 19:07:13 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-06-27 15:57:50 -07:00
|
|
|
if (JSID_IS_ATOM(id, cx->runtime->atomState.callerAtom)) {
|
2012-04-13 13:45:01 -07:00
|
|
|
StackIter prev(iter);
|
|
|
|
do {
|
|
|
|
++prev;
|
|
|
|
} while (!prev.done() && prev.isImplicitNativeCall());
|
2011-05-11 14:06:33 -07:00
|
|
|
|
2012-04-13 13:45:01 -07:00
|
|
|
if (prev.done() || !prev.isFunctionFrame()) {
|
2011-06-27 15:57:50 -07:00
|
|
|
JS_ASSERT(vp->isNull());
|
|
|
|
return true;
|
|
|
|
}
|
2012-04-13 13:45:01 -07:00
|
|
|
*vp = prev.calleev();
|
2012-03-23 17:59:56 -07:00
|
|
|
|
2011-06-27 15:57:50 -07:00
|
|
|
/* Censor the caller if it is from another compartment. */
|
|
|
|
JSObject &caller = vp->toObject();
|
|
|
|
if (caller.compartment() != cx->compartment) {
|
|
|
|
vp->setNull();
|
|
|
|
} else if (caller.isFunction()) {
|
2011-10-11 15:28:54 -07:00
|
|
|
JSFunction *callerFun = caller.toFunction();
|
2011-06-27 15:57:50 -07:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2011-06-27 15:57:50 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-06-27 15:57:50 -07:00
|
|
|
JS_NOT_REACHED("fun_getProperty");
|
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-08-02 23:52:12 -07:00
|
|
|
|
2008-09-04 21:56:37 -07:00
|
|
|
|
2011-10-10 23:00:28 -07:00
|
|
|
/* NB: no sentinels at ends -- use ArrayLength to bound loops.
|
2011-06-17 15:55:55 -07:00
|
|
|
* Properties censored into [[ThrowTypeError]] in strict mode. */
|
Bug 708735 - Use <stdint.h> types in JSAPI and throughout SpiderMonkey. Continue to provide the {u,}int{8,16,32,64} and JS{Uint,Int}{8,16,32,64} integer types through a single header, however, for a simpler backout strategy -- and also to ease the transition for embedders. r=timeless on switching the jsd API to use the <stdint.h> types, r=luke, r=dmandelin
2011-12-08 19:54:10 -08:00
|
|
|
static const uint16_t poisonPillProps[] = {
|
2011-06-17 15:55:55 -07:00
|
|
|
ATOM_OFFSET(arguments),
|
|
|
|
ATOM_OFFSET(caller),
|
2010-08-02 23:52:12 -07:00
|
|
|
};
|
|
|
|
|
2009-09-24 14:33:14 -07:00
|
|
|
static JSBool
|
|
|
|
fun_enumerate(JSContext *cx, JSObject *obj)
|
|
|
|
{
|
|
|
|
JS_ASSERT(obj->isFunction());
|
|
|
|
|
2011-12-31 06:32:04 -08:00
|
|
|
RootObject root(cx, &obj);
|
|
|
|
|
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;
|
2011-06-17 15:55:55 -07:00
|
|
|
|
|
|
|
id = ATOM_TO_JSID(cx->runtime->atomState.nameAtom);
|
|
|
|
if (!obj->hasProperty(cx, id, &found, JSRESOLVE_QUALIFIED))
|
|
|
|
return false;
|
2009-09-24 14:33:14 -07:00
|
|
|
|
2012-02-28 15:11:11 -08:00
|
|
|
for (unsigned i = 0; i < ArrayLength(poisonPillProps); i++) {
|
Bug 708735 - Use <stdint.h> types in JSAPI and throughout SpiderMonkey. Continue to provide the {u,}int{8,16,32,64} and JS{Uint,Int}{8,16,32,64} integer types through a single header, however, for a simpler backout strategy -- and also to ease the transition for embedders. r=timeless on switching the jsd API to use the <stdint.h> types, r=luke, r=dmandelin
2011-12-08 19:54:10 -08:00
|
|
|
const uint16_t offset = poisonPillProps[i];
|
2011-06-17 15:55:55 -07:00
|
|
|
id = ATOM_TO_JSID(OFFSET_TO_ATOM(cx->runtime, offset));
|
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 *
|
2012-04-12 09:23:51 -07:00
|
|
|
ResolveInterpretedFunctionPrototype(JSContext *cx, HandleObject obj)
|
2011-02-14 12:04:07 -08:00
|
|
|
{
|
2011-02-15 12:07:23 -08:00
|
|
|
#ifdef DEBUG
|
2011-10-11 15:28:54 -07:00
|
|
|
JSFunction *fun = obj->toFunction();
|
2011-02-14 12:04:07 -08:00
|
|
|
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.
|
|
|
|
*/
|
2012-02-07 11:45:18 -08:00
|
|
|
JSObject *objProto = obj->global().getOrCreateObjectPrototype(cx);
|
|
|
|
if (!objProto)
|
2011-02-14 12:04:07 -08:00
|
|
|
return NULL;
|
2012-04-12 09:23:51 -07:00
|
|
|
RootedVarObject proto(cx, NewObjectWithGivenProto(cx, &ObjectClass, objProto, NULL));
|
2011-07-15 10:14:07 -07:00
|
|
|
if (!proto || !proto->setSingletonType(cx))
|
2011-03-16 10:02:19 -07:00
|
|
|
return NULL;
|
2011-02-24 21:21:43 -08:00
|
|
|
|
2011-02-14 12:04:07 -08:00
|
|
|
/*
|
2011-06-15 10:15:37 -07:00
|
|
|
* Per ES5 15.3.5.2 a user-defined function's .prototype property is
|
|
|
|
* initially non-configurable, non-enumerable, and writable. Per ES5 13.2
|
|
|
|
* the prototype's .constructor property is configurable, non-enumerable,
|
|
|
|
* and writable.
|
2011-02-14 12:04:07 -08:00
|
|
|
*/
|
2011-10-05 01:00:23 -07:00
|
|
|
if (!obj->defineProperty(cx, cx->runtime->atomState.classPrototypeAtom,
|
2011-09-20 11:40:24 -07:00
|
|
|
ObjectValue(*proto), JS_PropertyStub, JS_StrictPropertyStub,
|
2011-06-15 10:15:37 -07:00
|
|
|
JSPROP_PERMANENT) ||
|
2011-10-05 01:00:23 -07:00
|
|
|
!proto->defineProperty(cx, cx->runtime->atomState.constructorAtom,
|
2011-09-20 11:40:24 -07:00
|
|
|
ObjectValue(*obj), JS_PropertyStub, JS_StrictPropertyStub, 0))
|
2011-06-15 10:15:37 -07:00
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-02-14 12:04:07 -08:00
|
|
|
return proto;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
static JSBool
|
2012-02-28 15:11:11 -08:00
|
|
|
fun_resolve(JSContext *cx, JSObject *obj, jsid id, unsigned 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
|
|
|
|
2011-12-31 06:32:04 -08:00
|
|
|
RootedVarFunction fun(cx);
|
|
|
|
fun = obj->toFunction();
|
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
|
2011-06-15 10:15:37 -07:00
|
|
|
* ECMA-262, or (Object.prototype, Function.prototype, etc.) have that
|
|
|
|
* property created eagerly.
|
2010-10-03 22:46:39 -07:00
|
|
|
*
|
|
|
|
* ES5 15.3.4: the non-native function object named Function.prototype
|
2011-06-15 10:15:37 -07:00
|
|
|
* does not have a .prototype property.
|
2010-10-03 22:46:39 -07:00
|
|
|
*
|
|
|
|
* 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-12-31 06:32:04 -08:00
|
|
|
if (!ResolveInterpretedFunctionPrototype(cx, fun))
|
2010-10-03 22:46:39 -07:00
|
|
|
return false;
|
2011-12-31 06:32:04 -08:00
|
|
|
*objp = fun;
|
2010-10-03 22:46:39 -07:00
|
|
|
return true;
|
2007-06-26 11:19:54 -07:00
|
|
|
}
|
|
|
|
|
2011-06-17 15:55:55 -07:00
|
|
|
if (JSID_IS_ATOM(id, cx->runtime->atomState.lengthAtom) ||
|
|
|
|
JSID_IS_ATOM(id, cx->runtime->atomState.nameAtom)) {
|
2010-06-02 23:44:24 -07:00
|
|
|
JS_ASSERT(!IsInternalFunctionObject(obj));
|
2011-06-17 15:55:55 -07:00
|
|
|
|
|
|
|
Value v;
|
|
|
|
if (JSID_IS_ATOM(id, cx->runtime->atomState.lengthAtom))
|
|
|
|
v.setInt32(fun->nargs);
|
|
|
|
else
|
|
|
|
v.setString(fun->atom ? fun->atom : cx->runtime->emptyString);
|
|
|
|
|
2011-12-31 06:32:04 -08:00
|
|
|
if (!DefineNativeProperty(cx, fun, id, v, JS_PropertyStub, JS_StrictPropertyStub,
|
2011-01-21 05:10:16 -08:00
|
|
|
JSPROP_PERMANENT | JSPROP_READONLY, 0, 0)) {
|
2010-10-03 22:46:39 -07:00
|
|
|
return false;
|
2010-06-02 23:44:24 -07:00
|
|
|
}
|
2011-12-31 06:32:04 -08:00
|
|
|
*objp = fun;
|
2010-10-03 22:46:39 -07:00
|
|
|
return true;
|
2010-06-02 23:44:24 -07:00
|
|
|
}
|
|
|
|
|
2012-02-28 15:11:11 -08:00
|
|
|
for (unsigned i = 0; i < ArrayLength(poisonPillProps); i++) {
|
Bug 708735 - Use <stdint.h> types in JSAPI and throughout SpiderMonkey. Continue to provide the {u,}int{8,16,32,64} and JS{Uint,Int}{8,16,32,64} integer types through a single header, however, for a simpler backout strategy -- and also to ease the transition for embedders. r=timeless on switching the jsd API to use the <stdint.h> types, r=luke, r=dmandelin
2011-12-08 19:54:10 -08:00
|
|
|
const uint16_t offset = poisonPillProps[i];
|
2010-08-02 23:52:12 -07:00
|
|
|
|
2011-06-17 15:55:55 -07:00
|
|
|
if (JSID_IS_ATOM(id, OFFSET_TO_ATOM(cx->runtime, offset))) {
|
2011-12-31 06:32:04 -08:00
|
|
|
JS_ASSERT(!IsInternalFunctionObject(fun));
|
2010-08-02 23:52:12 -07:00
|
|
|
|
2011-02-09 11:31:40 -08:00
|
|
|
PropertyOp getter;
|
|
|
|
StrictPropertyOp setter;
|
2012-02-28 15:11:11 -08:00
|
|
|
unsigned attrs = JSPROP_PERMANENT;
|
2011-12-31 06:32:04 -08:00
|
|
|
if (fun->isInterpreted() ? fun->inStrictMode() : fun->isBoundFunction()) {
|
2012-02-16 19:10:59 -08:00
|
|
|
JSObject *throwTypeError = fun->global().getThrowTypeError();
|
2010-08-02 23:52:12 -07:00
|
|
|
|
|
|
|
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-09-20 11:40:24 -07:00
|
|
|
setter = JS_StrictPropertyStub;
|
2010-08-02 23:52:12 -07:00
|
|
|
}
|
|
|
|
|
2011-12-31 06:32:04 -08:00
|
|
|
if (!DefineNativeProperty(cx, fun, id, UndefinedValue(), getter, setter,
|
2011-06-17 15:55:55 -07:00
|
|
|
attrs, 0, 0)) {
|
2010-10-03 22:46:39 -07:00
|
|
|
return false;
|
2010-08-02 23:52:12 -07:00
|
|
|
}
|
2011-12-31 06:32:04 -08:00
|
|
|
*objp = fun;
|
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
|
|
|
}
|
|
|
|
|
2012-02-20 02:58:00 -08:00
|
|
|
template<XDRMode mode>
|
|
|
|
bool
|
|
|
|
js::XDRInterpretedFunction(XDRState<mode> *xdr, JSObject **objp, JSScript *parentScript)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-05-01 20:39:05 -07:00
|
|
|
/* NB: Keep this in sync with CloneInterpretedFunction. */
|
2008-03-29 03:34:29 -07:00
|
|
|
JSFunction *fun;
|
2012-03-19 17:24:05 -07:00
|
|
|
JSAtom *atom;
|
Bug 708735 - Use <stdint.h> types in JSAPI and throughout SpiderMonkey. Continue to provide the {u,}int{8,16,32,64} and JS{Uint,Int}{8,16,32,64} integer types through a single header, however, for a simpler backout strategy -- and also to ease the transition for embedders. r=timeless on switching the jsd API to use the <stdint.h> types, r=luke, r=dmandelin
2011-12-08 19:54:10 -08:00
|
|
|
uint32_t firstword; /* flag telling whether fun->atom is non-null,
|
2009-06-04 18:58:47 -07:00
|
|
|
plus for fun->u.i.skipmin, fun->u.i.wrapper,
|
|
|
|
and 14 bits reserved for future use */
|
Bug 708735 - Use <stdint.h> types in JSAPI and throughout SpiderMonkey. Continue to provide the {u,}int{8,16,32,64} and JS{Uint,Int}{8,16,32,64} integer types through a single header, however, for a simpler backout strategy -- and also to ease the transition for embedders. r=timeless on switching the jsd API to use the <stdint.h> types, r=luke, r=dmandelin
2011-12-08 19:54:10 -08:00
|
|
|
uint32_t flagsword; /* word for argument count and fun->flags */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-02-20 02:58:00 -08:00
|
|
|
JSContext *cx = xdr->cx();
|
2011-09-22 03:08:55 -07:00
|
|
|
JSScript *script;
|
2012-02-20 02:58:00 -08:00
|
|
|
if (mode == XDR_ENCODE) {
|
2011-10-11 15:28:54 -07:00
|
|
|
fun = (*objp)->toFunction();
|
2011-08-18 10:39:44 -07:00
|
|
|
if (!fun->isInterpreted()) {
|
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
|
|
|
}
|
2011-10-26 13:02:57 -07:00
|
|
|
firstword = !!fun->atom;
|
2010-11-16 15:34:24 -08:00
|
|
|
flagsword = (fun->nargs << 16) | fun->flags;
|
2012-03-19 17:24:05 -07:00
|
|
|
atom = fun->atom;
|
2011-09-22 03:08:55 -07:00
|
|
|
script = fun->script();
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
2011-12-31 06:32:04 -08:00
|
|
|
RootedVarObject parent(cx, NULL);
|
|
|
|
fun = js_NewFunction(cx, NULL, NULL, 0, JSFUN_INTERPRETED, parent, NULL);
|
2008-03-28 15:27:36 -07:00
|
|
|
if (!fun)
|
2010-03-28 22:05:50 -07:00
|
|
|
return false;
|
2011-10-13 20:21:36 -07:00
|
|
|
if (!fun->clearParent(cx))
|
|
|
|
return false;
|
2011-10-07 20:09:09 -07:00
|
|
|
if (!fun->clearType(cx))
|
|
|
|
return false;
|
2012-03-19 17:24:05 -07:00
|
|
|
atom = NULL;
|
2011-09-22 03:08:55 -07:00
|
|
|
script = NULL;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-02-20 02:58:00 -08:00
|
|
|
if (!xdr->codeUint32(&firstword))
|
2010-03-28 22:05:50 -07:00
|
|
|
return false;
|
2012-02-20 02:58:00 -08:00
|
|
|
if ((firstword & 1U) && !XDRAtom(xdr, &atom))
|
2010-03-28 22:05:50 -07:00
|
|
|
return false;
|
2012-02-20 02:58:00 -08:00
|
|
|
if (!xdr->codeUint32(&flagsword))
|
2010-03-28 22:05:50 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-02-20 02:58:00 -08:00
|
|
|
if (!XDRScript(xdr, &script, parentScript))
|
2010-03-28 22:05:50 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-02-20 02:58:00 -08:00
|
|
|
if (mode == XDR_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);
|
Bug 708735 - Use <stdint.h> types in JSAPI and throughout SpiderMonkey. Continue to provide the {u,}int{8,16,32,64} and JS{Uint,Int}{8,16,32,64} integer types through a single header, however, for a simpler backout strategy -- and also to ease the transition for embedders. r=timeless on switching the jsd API to use the <stdint.h> types, r=luke, r=dmandelin
2011-12-08 19:54:10 -08:00
|
|
|
fun->flags = uint16_t(flagsword);
|
2012-03-19 17:24:05 -07:00
|
|
|
fun->atom.init(atom);
|
|
|
|
fun->initScript(script);
|
2011-09-22 03:08:55 -07:00
|
|
|
if (!script->typeSetFunction(cx, fun))
|
2011-03-07 08:25:27 -08:00
|
|
|
return false;
|
2012-04-02 08:58:30 -07:00
|
|
|
JS_ASSERT(fun->nargs == fun->script()->bindings.numArgs());
|
2010-11-16 15:30:56 -08:00
|
|
|
js_CallNewScriptHook(cx, fun->script(), fun);
|
2011-09-22 03:08:55 -07:00
|
|
|
*objp = 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
|
|
|
}
|
|
|
|
|
2012-02-20 02:58:00 -08:00
|
|
|
template bool
|
|
|
|
js::XDRInterpretedFunction(XDRState<XDR_ENCODE> *xdr, JSObject **objp, JSScript *parentScript);
|
|
|
|
|
|
|
|
template bool
|
|
|
|
js::XDRInterpretedFunction(XDRState<XDR_DECODE> *xdr, JSObject **objp, JSScript *parentScript);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-05-01 20:39:05 -07:00
|
|
|
JSObject *
|
|
|
|
js::CloneInterpretedFunction(JSContext *cx, JSFunction *srcFun)
|
|
|
|
{
|
|
|
|
/* NB: Keep this in sync with XDRInterpretedFunction. */
|
|
|
|
|
|
|
|
RootedVarObject parent(cx, NULL);
|
|
|
|
JSFunction *clone = js_NewFunction(cx, NULL, NULL, 0, JSFUN_INTERPRETED, parent, NULL);
|
|
|
|
if (!clone)
|
|
|
|
return NULL;
|
|
|
|
if (!clone->clearParent(cx))
|
|
|
|
return NULL;
|
|
|
|
if (!clone->clearType(cx))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
JSScript *clonedScript = CloneScript(cx, srcFun->script());
|
|
|
|
if (!clonedScript)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
clone->nargs = srcFun->nargs;
|
|
|
|
clone->flags = srcFun->flags;
|
|
|
|
clone->atom.init(srcFun->atom);
|
|
|
|
clone->initScript(clonedScript);
|
|
|
|
if (!clonedScript->typeSetFunction(cx, clone))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
js_CallNewScriptHook(cx, clone->script(), clone);
|
|
|
|
return clone;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/*
|
|
|
|
* [[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;
|
2011-10-26 13:02:57 -07:00
|
|
|
obj = obj->toFunction()->getBoundFunctionTarget();
|
2010-08-13 10:42:33 -07:00
|
|
|
}
|
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
Value pval;
|
2011-09-15 11:44:10 -07:00
|
|
|
if (!obj->getProperty(cx, cx->runtime->atomState.classPrototypeAtom, &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
|
|
|
}
|
|
|
|
|
2011-10-26 13:02:57 -07:00
|
|
|
inline void
|
|
|
|
JSFunction::trace(JSTracer *trc)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-11-14 09:13:33 -08:00
|
|
|
if (isExtended()) {
|
2011-11-18 15:20:21 -08:00
|
|
|
MarkValueRange(trc, ArrayLength(toExtended()->extendedSlots),
|
2011-11-14 09:13:33 -08:00
|
|
|
toExtended()->extendedSlots, "nativeReserved");
|
2011-10-26 13:02:57 -07:00
|
|
|
}
|
2010-08-29 11:57:08 -07:00
|
|
|
|
2011-10-26 13:02:57 -07:00
|
|
|
if (atom)
|
2012-03-19 17:24:05 -07:00
|
|
|
MarkString(trc, &atom, "atom");
|
2011-10-26 13:02:57 -07:00
|
|
|
|
|
|
|
if (isInterpreted()) {
|
2012-02-17 11:46:11 -08:00
|
|
|
if (u.i.script_)
|
|
|
|
MarkScriptUnbarriered(trc, &u.i.script_, "script");
|
|
|
|
if (u.i.env_)
|
|
|
|
MarkObjectUnbarriered(trc, &u.i.env_, "fun_callscope");
|
2011-10-14 15:31:53 -07:00
|
|
|
}
|
2008-04-02 00:46:12 -07:00
|
|
|
}
|
|
|
|
|
2011-10-26 13:02:57 -07:00
|
|
|
static void
|
|
|
|
fun_trace(JSTracer *trc, JSObject *obj)
|
|
|
|
{
|
|
|
|
obj->toFunction()->trace(trc);
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
*/
|
2011-10-04 07:06:54 -07:00
|
|
|
JS_FRIEND_DATA(Class) js::FunctionClass = {
|
2007-03-22 10:30:00 -07:00
|
|
|
js_Function_str,
|
2012-02-17 14:35:20 -08:00
|
|
|
JSCLASS_NEW_RESOLVE | JSCLASS_IMPLEMENTS_BARRIERS |
|
2011-09-29 20:29:13 -07:00
|
|
|
JSCLASS_HAS_CACHED_PROTO(JSProto_Function),
|
2011-09-20 11:40:24 -07:00
|
|
|
JS_PropertyStub, /* addProperty */
|
|
|
|
JS_PropertyStub, /* delProperty */
|
|
|
|
JS_PropertyStub, /* getProperty */
|
|
|
|
JS_StrictPropertyStub, /* setProperty */
|
2010-06-12 09:29:04 -07:00
|
|
|
fun_enumerate,
|
|
|
|
(JSResolveOp)fun_resolve,
|
2011-09-20 11:40:24 -07:00
|
|
|
JS_ConvertStub,
|
2012-02-27 23:49:02 -08:00
|
|
|
NULL, /* finalize */
|
2011-09-20 11:40:24 -07:00
|
|
|
NULL, /* checkAccess */
|
|
|
|
NULL, /* call */
|
|
|
|
NULL, /* construct */
|
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 *
|
2012-02-28 15:11:11 -08:00
|
|
|
fun_toStringHelper(JSContext *cx, JSObject *obj, unsigned indent)
|
2010-06-23 14:35:10 -07:00
|
|
|
{
|
|
|
|
if (!obj->isFunction()) {
|
2011-10-04 07:06:54 -07:00
|
|
|
if (IsFunctionProxy(obj))
|
2011-09-08 20:29:15 -07:00
|
|
|
return Proxy::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
|
|
|
|
2011-10-11 15:28:54 -07:00
|
|
|
JSFunction *fun = obj->toFunction();
|
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
|
2012-02-28 15:11:11 -08:00
|
|
|
fun_toString(JSContext *cx, unsigned 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;
|
|
|
|
|
2011-12-05 14:10:02 -08:00
|
|
|
if (argc != 0 && !ToUint32(cx, vp[2], &indent))
|
2010-06-23 14:35:10 -07:00
|
|
|
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
|
2012-02-28 15:11:11 -08:00
|
|
|
fun_toSource(JSContext *cx, unsigned 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
|
2012-02-28 15:11:11 -08:00
|
|
|
js_fun_call(JSContext *cx, unsigned argc, Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
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-09-08 21:02:26 -07:00
|
|
|
ReportIncompatibleMethod(cx, CallReceiverFromVp(vp), &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;
|
2012-01-17 14:32:36 -08:00
|
|
|
PodCopy(args.array(), argv, argc);
|
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
|
2012-02-28 15:11:11 -08:00
|
|
|
js_fun_apply(JSContext *cx, unsigned argc, Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-07-16 12:53:37 -07:00
|
|
|
/* Step 1. */
|
2012-04-30 17:10:30 -07:00
|
|
|
RootedVarValue fval(cx, vp[1]);
|
2010-02-22 22:25:09 -08:00
|
|
|
if (!js_IsCallable(fval)) {
|
2011-09-08 21:02:26 -07:00
|
|
|
ReportIncompatibleMethod(cx, CallReceiverFromVp(vp), &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);
|
|
|
|
|
2012-02-10 16:31:48 -08:00
|
|
|
InvokeArgsGuard args;
|
|
|
|
if (vp[3].isMagic(JS_OPTIMIZED_ARGUMENTS)) {
|
|
|
|
/*
|
|
|
|
* Pretend we have been passed the 'arguments' object for the current
|
|
|
|
* function and read actuals out of the frame.
|
|
|
|
*
|
|
|
|
* N.B. Changes here need to be propagated to stubs::SplatApplyArgs.
|
|
|
|
*/
|
|
|
|
/* Steps 4-6. */
|
|
|
|
unsigned length = cx->fp()->numActualArgs();
|
|
|
|
JS_ASSERT(length <= StackSpace::ARGS_LENGTH_MAX);
|
2010-10-21 11:33:22 -07:00
|
|
|
|
2012-02-10 16:31:48 -08:00
|
|
|
if (!cx->stack.pushInvokeArgs(cx, length, &args))
|
|
|
|
return false;
|
2010-03-03 17:52:26 -08:00
|
|
|
|
2012-02-10 16:31:48 -08:00
|
|
|
/* Push fval, obj, and aobj's elements as args. */
|
|
|
|
args.calleev() = fval;
|
|
|
|
args.thisv() = vp[2];
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-02-10 16:31:48 -08:00
|
|
|
/* Steps 7-8. */
|
|
|
|
cx->fp()->forEachCanonicalActualArg(CopyTo(args.array()));
|
|
|
|
} else {
|
|
|
|
/* Step 3. */
|
|
|
|
if (!vp[3].isObject()) {
|
|
|
|
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_BAD_APPLY_ARGS, js_apply_str);
|
|
|
|
return false;
|
|
|
|
}
|
2010-07-16 12:53:37 -07:00
|
|
|
|
2012-02-10 16:31:48 -08:00
|
|
|
/*
|
|
|
|
* Steps 4-5 (note erratum removing steps originally numbered 5 and 7 in
|
|
|
|
* original version of ES5).
|
|
|
|
*/
|
2012-04-30 17:10:30 -07:00
|
|
|
RootedVarObject aobj(cx, &vp[3].toObject());
|
2012-02-10 16:31:48 -08:00
|
|
|
uint32_t length;
|
|
|
|
if (!js_GetLengthProperty(cx, aobj, &length))
|
|
|
|
return false;
|
2012-02-10 16:31:48 -08:00
|
|
|
|
2012-02-10 16:31:48 -08:00
|
|
|
/* Step 6. */
|
|
|
|
if (length > StackSpace::ARGS_LENGTH_MAX) {
|
|
|
|
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_TOO_MANY_FUN_APPLY_ARGS);
|
|
|
|
return false;
|
|
|
|
}
|
2012-02-10 16:31:48 -08:00
|
|
|
|
2012-02-10 16:31:48 -08:00
|
|
|
if (!cx->stack.pushInvokeArgs(cx, length, &args))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* Push fval, obj, and aobj's elements as args. */
|
|
|
|
args.calleev() = fval;
|
|
|
|
args.thisv() = vp[2];
|
|
|
|
|
|
|
|
/* Steps 7-8. */
|
|
|
|
if (!GetElements(cx, aobj, length, args.array()))
|
|
|
|
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;
|
2012-02-10 16:31:48 -08:00
|
|
|
|
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
|
2012-02-28 15:11:11 -08:00
|
|
|
CallOrConstructBoundFunction(JSContext *cx, unsigned argc, Value *vp);
|
2010-08-13 10:42:33 -07:00
|
|
|
|
2011-02-09 18:17:34 -08:00
|
|
|
}
|
|
|
|
|
Bug 708735 - Use <stdint.h> types in JSAPI and throughout SpiderMonkey. Continue to provide the {u,}int{8,16,32,64} and JS{Uint,Int}{8,16,32,64} integer types through a single header, however, for a simpler backout strategy -- and also to ease the transition for embedders. r=timeless on switching the jsd API to use the <stdint.h> types, r=luke, r=dmandelin
2011-12-08 19:54:10 -08:00
|
|
|
static const uint32_t JSSLOT_BOUND_FUNCTION_THIS = 0;
|
|
|
|
static const uint32_t JSSLOT_BOUND_FUNCTION_ARGS_COUNT = 1;
|
2011-10-26 13:02:57 -07:00
|
|
|
|
Bug 708735 - Use <stdint.h> types in JSAPI and throughout SpiderMonkey. Continue to provide the {u,}int{8,16,32,64} and JS{Uint,Int}{8,16,32,64} integer types through a single header, however, for a simpler backout strategy -- and also to ease the transition for embedders. r=timeless on switching the jsd API to use the <stdint.h> types, r=luke, r=dmandelin
2011-12-08 19:54:10 -08:00
|
|
|
static const uint32_t BOUND_FUNCTION_RESERVED_SLOTS = 2;
|
2011-10-26 13:02:57 -07:00
|
|
|
|
2010-08-13 10:42:33 -07:00
|
|
|
inline bool
|
2012-04-30 17:10:30 -07:00
|
|
|
JSFunction::initBoundFunction(JSContext *cx, HandleValue thisArg,
|
2012-02-28 15:11:11 -08:00
|
|
|
const Value *args, unsigned argslen)
|
2010-08-13 10:42:33 -07:00
|
|
|
{
|
|
|
|
JS_ASSERT(isFunction());
|
|
|
|
|
2012-04-30 17:10:30 -07:00
|
|
|
RootedVarFunction self(cx, this);
|
|
|
|
|
2011-10-14 13:51:21 -07:00
|
|
|
/*
|
|
|
|
* Convert to a dictionary to set the BOUND_FUNCTION flag and increase
|
2011-10-26 13:02:57 -07:00
|
|
|
* the slot span to cover the arguments and additional slots for the 'this'
|
|
|
|
* value and arguments count.
|
2011-10-14 13:51:21 -07:00
|
|
|
*/
|
2012-04-30 17:10:30 -07:00
|
|
|
if (!self->toDictionaryMode(cx))
|
2011-10-14 13:51:21 -07:00
|
|
|
return false;
|
|
|
|
|
2012-04-30 17:10:30 -07:00
|
|
|
if (!self->setFlag(cx, BaseShape::BOUND_FUNCTION))
|
2011-12-24 06:32:27 -08:00
|
|
|
return false;
|
2011-10-14 13:51:21 -07:00
|
|
|
|
2012-04-30 17:10:30 -07:00
|
|
|
if (!self->setSlotSpan(cx, BOUND_FUNCTION_RESERVED_SLOTS + argslen))
|
2011-10-14 13:51:21 -07:00
|
|
|
return false;
|
|
|
|
|
2012-04-30 17:10:30 -07:00
|
|
|
self->setSlot(JSSLOT_BOUND_FUNCTION_THIS, thisArg);
|
|
|
|
self->setSlot(JSSLOT_BOUND_FUNCTION_ARGS_COUNT, PrivateUint32Value(argslen));
|
2011-10-26 13:02:57 -07:00
|
|
|
|
2012-04-30 17:10:30 -07:00
|
|
|
self->initSlotRange(BOUND_FUNCTION_RESERVED_SLOTS, args, argslen);
|
2011-10-14 13:51:21 -07:00
|
|
|
|
2010-08-13 10:42:33 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline JSObject *
|
2011-10-26 13:02:57 -07:00
|
|
|
JSFunction::getBoundFunctionTarget() const
|
2010-08-13 10:42:33 -07:00
|
|
|
{
|
|
|
|
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 &
|
2011-10-26 13:02:57 -07:00
|
|
|
JSFunction::getBoundFunctionThis() const
|
2010-08-13 10:42:33 -07:00
|
|
|
{
|
|
|
|
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 &
|
2012-02-28 15:11:11 -08:00
|
|
|
JSFunction::getBoundFunctionArgument(unsigned 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());
|
|
|
|
|
2011-10-26 13:02:57 -07:00
|
|
|
return getSlot(BOUND_FUNCTION_RESERVED_SLOTS + which);
|
2011-04-08 19:51:40 -07:00
|
|
|
}
|
2010-08-13 10:42:33 -07:00
|
|
|
|
2011-04-08 19:51:40 -07:00
|
|
|
inline size_t
|
2011-10-26 13:02:57 -07:00
|
|
|
JSFunction::getBoundFunctionArgumentCount() const
|
2011-04-08 19:51:40 -07:00
|
|
|
{
|
|
|
|
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
|
2012-02-28 15:11:11 -08:00
|
|
|
CallOrConstructBoundFunction(JSContext *cx, unsigned argc, Value *vp)
|
2010-08-13 10:42:33 -07:00
|
|
|
{
|
2011-10-26 13:02:57 -07:00
|
|
|
JSFunction *fun = vp[0].toObject().toFunction();
|
|
|
|
JS_ASSERT(fun->isBoundFunction());
|
2010-08-13 10:42:33 -07:00
|
|
|
|
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. */
|
2012-02-28 15:11:11 -08:00
|
|
|
unsigned argslen = fun->getBoundFunctionArgumentCount();
|
2010-08-13 10:42:33 -07:00
|
|
|
|
2011-06-30 09:26:56 -07:00
|
|
|
if (argc + argslen > StackSpace::ARGS_LENGTH_MAX) {
|
2010-08-13 10:42:33 -07:00
|
|
|
js_ReportAllocationOverflow(cx);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 15.3.4.5.1 step 3, 15.3.4.5.2 step 1. */
|
2011-10-26 13:02:57 -07:00
|
|
|
JSObject *target = fun->getBoundFunctionTarget();
|
2010-08-13 10:42:33 -07:00
|
|
|
|
|
|
|
/* 15.3.4.5.1 step 2. */
|
2011-10-26 13:02:57 -07:00
|
|
|
const Value &boundThis = fun->getBoundFunctionThis();
|
2010-08-13 10:42:33 -07:00
|
|
|
|
|
|
|
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. */
|
2012-02-28 15:11:11 -08:00
|
|
|
for (unsigned i = 0; i < argslen; i++)
|
2011-10-26 13:02:57 -07:00
|
|
|
args[i] = fun->getBoundFunctionArgument(i);
|
2012-01-17 14:32:36 -08:00
|
|
|
PodCopy(args.array() + argslen, vp + 2, argc);
|
2010-08-13 10:42:33 -07:00
|
|
|
|
|
|
|
/* 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
|
2012-02-28 15:11:11 -08:00
|
|
|
fun_isGenerator(JSContext *cx, unsigned argc, Value *vp)
|
2011-04-08 13:31:14 -07:00
|
|
|
{
|
2011-10-11 15:28:54 -07:00
|
|
|
JSFunction *fun;
|
|
|
|
if (!IsFunctionObject(vp[1], &fun)) {
|
2011-04-08 13:31:14 -07:00
|
|
|
JS_SET_RVAL(cx, vp, BooleanValue(false));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2012-04-02 08:58:30 -07:00
|
|
|
result = script->isGenerator;
|
2011-04-08 13:31:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2012-02-28 15:11:11 -08:00
|
|
|
fun_bind(JSContext *cx, unsigned argc, Value *vp)
|
2010-08-13 10:42:33 -07:00
|
|
|
{
|
2011-09-08 21:02:26 -07:00
|
|
|
CallArgs args = CallArgsFromVp(argc, vp);
|
|
|
|
|
2010-08-13 10:42:33 -07:00
|
|
|
/* Step 1. */
|
2011-09-08 21:02:26 -07:00
|
|
|
Value &thisv = args.thisv();
|
2010-08-13 10:42:33 -07:00
|
|
|
|
|
|
|
/* Step 2. */
|
2011-01-26 13:37:45 -08:00
|
|
|
if (!js_IsCallable(thisv)) {
|
2011-09-08 21:02:26 -07:00
|
|
|
ReportIncompatibleMethod(cx, args, &FunctionClass);
|
2010-08-13 10:42:33 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-12-31 06:32:04 -08:00
|
|
|
RootedVarObject target(cx);
|
|
|
|
target = &thisv.toObject();
|
2011-01-26 13:37:45 -08:00
|
|
|
|
2010-08-13 10:42:33 -07:00
|
|
|
/* Step 3. */
|
2011-09-08 21:02:26 -07:00
|
|
|
Value *boundArgs = NULL;
|
2012-02-28 15:11:11 -08:00
|
|
|
unsigned argslen = 0;
|
2011-09-08 21:02:26 -07:00
|
|
|
if (args.length() > 1) {
|
|
|
|
boundArgs = args.array() + 1;
|
|
|
|
argslen = args.length() - 1;
|
2010-08-13 10:42:33 -07:00
|
|
|
}
|
|
|
|
|
2012-04-05 10:14:52 -07:00
|
|
|
/* Steps 7-9. */
|
2012-04-30 17:10:30 -07:00
|
|
|
RootedVarValue thisArg(cx, args.length() >= 1 ? args[0] : UndefinedValue());
|
2012-04-05 10:14:52 -07:00
|
|
|
|
|
|
|
JSObject *boundFunction = js_fun_bind(cx, target, thisArg, boundArgs, argslen);
|
|
|
|
if (!boundFunction)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* Step 22. */
|
|
|
|
args.rval().setObject(*boundFunction);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject*
|
2012-04-30 17:10:30 -07:00
|
|
|
js_fun_bind(JSContext *cx, HandleObject target, HandleValue thisArg,
|
2012-04-05 10:14:52 -07:00
|
|
|
Value *boundArgs, unsigned argslen)
|
|
|
|
{
|
2010-08-13 10:42:33 -07:00
|
|
|
/* Steps 15-16. */
|
2012-02-28 15:11:11 -08:00
|
|
|
unsigned length = 0;
|
2010-08-13 10:42:33 -07:00
|
|
|
if (target->isFunction()) {
|
2012-02-28 15:11:11 -08:00
|
|
|
unsigned nargs = target->toFunction()->nargs;
|
2010-08-13 10:42:33 -07:00
|
|
|
if (nargs > argslen)
|
|
|
|
length = nargs - argslen;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Step 4-6, 10-11. */
|
2012-03-19 17:24:05 -07:00
|
|
|
JSAtom *name = target->isFunction() ? target->toFunction()->atom.get() : NULL;
|
2010-08-13 10:42:33 -07:00
|
|
|
|
2012-04-30 17:10:30 -07:00
|
|
|
RootedVarObject funobj(cx);
|
|
|
|
funobj = js_NewFunction(cx, NULL, CallOrConstructBoundFunction, length,
|
|
|
|
JSFUN_CONSTRUCTOR, target, name);
|
2010-08-13 10:42:33 -07:00
|
|
|
if (!funobj)
|
2012-04-05 10:14:52 -07:00
|
|
|
return NULL;
|
2010-08-13 10:42:33 -07:00
|
|
|
|
2011-10-13 12:10:52 -07:00
|
|
|
/* NB: Bound functions abuse |parent| to store their target. */
|
2012-04-30 17:10:30 -07:00
|
|
|
if (!JSObject::setParent(cx, funobj, target))
|
2012-04-05 10:14:52 -07:00
|
|
|
return NULL;
|
2011-10-13 12:10:52 -07:00
|
|
|
|
2011-10-26 13:02:57 -07:00
|
|
|
if (!funobj->toFunction()->initBoundFunction(cx, thisArg, boundArgs, argslen))
|
2012-04-05 10:14:52 -07:00
|
|
|
return NULL;
|
2010-08-13 10:42:33 -07:00
|
|
|
|
|
|
|
/* Steps 17, 19-21 are handled by fun_resolve. */
|
|
|
|
/* Step 18 is the default for new functions. */
|
2012-04-05 10:14:52 -07:00
|
|
|
return funobj;
|
2010-08-13 10:42:33 -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;
|
|
|
|
}
|
|
|
|
|
2011-06-15 11:58:45 -07:00
|
|
|
namespace js {
|
|
|
|
|
|
|
|
JSFunctionSpec function_methods[] = {
|
|
|
|
#if JS_HAS_TOSOURCE
|
|
|
|
JS_FN(js_toSource_str, fun_toSource, 0,0),
|
|
|
|
#endif
|
|
|
|
JS_FN(js_toString_str, fun_toString, 0,0),
|
|
|
|
JS_FN(js_apply_str, js_fun_apply, 2,0),
|
|
|
|
JS_FN(js_call_str, js_fun_call, 1,0),
|
|
|
|
JS_FN("bind", fun_bind, 1,0),
|
|
|
|
#if JS_HAS_GENERATORS
|
|
|
|
JS_FN("isGenerator", fun_isGenerator,0,0),
|
|
|
|
#endif
|
|
|
|
JS_FS_END
|
|
|
|
};
|
|
|
|
|
|
|
|
JSBool
|
2012-02-28 15:11:11 -08:00
|
|
|
Function(JSContext *cx, unsigned argc, Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-09-08 21:02:26 -07:00
|
|
|
CallArgs args = CallArgsFromVp(argc, vp);
|
2011-04-08 10:52:51 -07:00
|
|
|
|
2011-05-10 14:56:49 -07:00
|
|
|
/* Block this call if security callbacks forbid it. */
|
2011-12-31 06:32:04 -08:00
|
|
|
RootedVar<GlobalObject*> global(cx);
|
2012-01-02 15:02:05 -08:00
|
|
|
global = &args.callee().global();
|
2011-06-10 11:44:16 -07:00
|
|
|
if (!global->isRuntimeCodeGenEnabled(cx)) {
|
2011-05-10 14:56:49 -07:00
|
|
|
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-03-11 16:29:38 -08:00
|
|
|
Bindings bindings(cx);
|
2012-04-30 17:10:30 -07:00
|
|
|
Bindings::StackRoot bindingsRoot(cx, &bindings);
|
2011-06-27 17:42:53 -07:00
|
|
|
|
|
|
|
const char *filename;
|
2012-02-28 15:11:11 -08:00
|
|
|
unsigned lineno;
|
2011-06-27 17:42:53 -07:00
|
|
|
JSPrincipals *originPrincipals;
|
|
|
|
CurrentScriptFileLineOrigin(cx, &filename, &lineno, &originPrincipals);
|
|
|
|
JSPrincipals *principals = PrincipalsForCompiledCode(args, cx);
|
2011-04-08 10:52:51 -07:00
|
|
|
|
2012-02-28 15:11:11 -08:00
|
|
|
unsigned n = args.length() ? args.length() - 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;
|
2012-02-28 15:11:11 -08:00
|
|
|
for (unsigned i = 0; i < n; i++) {
|
2007-03-22 10:30:00 -07:00
|
|
|
/* Collect the lengths for all the function-argument arguments. */
|
2011-12-01 19:35:44 -08:00
|
|
|
JSString *arg = ToString(cx, args[i]);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!arg)
|
2011-03-23 14:33:02 -07:00
|
|
|
return false;
|
2011-09-08 21:02:26 -07:00
|
|
|
args[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
|
2011-10-21 16:53:30 -07:00
|
|
|
* for a terminating 0. Mark cx->tempLifeAlloc for later release, to
|
|
|
|
* free collected_args and its tokenstream in one swoop.
|
2007-03-22 10:30:00 -07:00
|
|
|
*/
|
2011-09-22 13:22:30 -07:00
|
|
|
LifoAllocScope las(&cx->tempLifoAlloc());
|
|
|
|
jschar *cp = cx->tempLifoAlloc().newArray<jschar>(args_length + 1);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!cp) {
|
2011-06-03 17:54:26 -07:00
|
|
|
js_ReportOutOfMemory(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.
|
|
|
|
*/
|
2012-02-28 15:11:11 -08:00
|
|
|
for (unsigned i = 0; i < n; i++) {
|
2011-09-08 21:02:26 -07:00
|
|
|
JSString *arg = args[i].toString();
|
2010-08-16 12:35:04 -07:00
|
|
|
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. */
|
2011-06-27 17:42:53 -07:00
|
|
|
TokenStream ts(cx, principals, originPrincipals);
|
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-19 09:15:45 -08:00
|
|
|
/* Check for a duplicate parameter name. */
|
2012-04-12 09:23:51 -07:00
|
|
|
RootedVar<PropertyName*> name(cx, ts.currentToken().name());
|
2011-10-08 15:45:29 -07:00
|
|
|
if (bindings.hasBinding(cx, name)) {
|
|
|
|
JSAutoByteString bytes;
|
|
|
|
if (!js_AtomToPrintableString(cx, name, &bytes))
|
2011-03-23 14:33:02 -07:00
|
|
|
return false;
|
2010-11-11 12:40:29 -08:00
|
|
|
if (!ReportCompileErrorNumber(cx, &ts, NULL,
|
|
|
|
JSREPORT_WARNING | JSREPORT_STRICT,
|
2011-10-08 15:45:29 -07:00
|
|
|
JSMSG_DUPLICATE_FORMAL, bytes.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
|
|
|
|
Bug 708735 - Use <stdint.h> types in JSAPI and throughout SpiderMonkey. Continue to provide the {u,}int{8,16,32,64} and JS{Uint,Int}{8,16,32,64} integer types through a single header, however, for a simpler backout strategy -- and also to ease the transition for embedders. r=timeless on switching the jsd API to use the <stdint.h> types, r=luke, r=dmandelin
2011-12-08 19:54:10 -08:00
|
|
|
uint16_t dummy;
|
2011-10-08 15:45:29 -07:00
|
|
|
if (!bindings.addArgument(cx, name, &dummy))
|
2011-03-23 14:33:02 -07:00
|
|
|
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;
|
|
|
|
|
2012-04-30 17:10:30 -07:00
|
|
|
SkipRoot skip(cx, &chars);
|
|
|
|
|
2011-09-08 21:02:26 -07:00
|
|
|
if (args.length()) {
|
2011-12-01 19:35:44 -08:00
|
|
|
JSString *str = ToString(cx, args[args.length() - 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-08-21 07:23:48 -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.
|
|
|
|
*/
|
2012-04-30 17:10:30 -07:00
|
|
|
RootedVarFunction fun(cx, js_NewFunction(cx, NULL, NULL, 0, JSFUN_LAMBDA | JSFUN_INTERPRETED,
|
|
|
|
global, cx->runtime->atomState.anonymousAtom));
|
2011-08-21 07:23:48 -07:00
|
|
|
if (!fun)
|
|
|
|
return false;
|
|
|
|
|
2011-06-27 17:42:53 -07:00
|
|
|
bool ok = frontend::CompileFunctionBody(cx, fun, principals, originPrincipals,
|
|
|
|
&bindings, chars, length, filename, lineno,
|
|
|
|
cx->findVersion());
|
2011-09-08 21:02:26 -07:00
|
|
|
args.rval().setObject(*fun);
|
2011-04-08 10:52:51 -07:00
|
|
|
return ok;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
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 *
|
2012-04-12 09:23:51 -07:00
|
|
|
LookupInterpretedFunctionPrototype(JSContext *cx, RootedVarObject funobj)
|
2011-02-14 12:04:07 -08:00
|
|
|
{
|
2011-02-15 12:07:23 -08:00
|
|
|
#ifdef DEBUG
|
2011-10-11 15:28:54 -07:00
|
|
|
JSFunction *fun = funobj->toFunction();
|
2011-02-14 12:04:07 -08:00
|
|
|
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);
|
2012-04-12 09:23:51 -07:00
|
|
|
RootedVar<const Shape*> shape(cx, funobj->nativeLookup(cx, id));
|
2011-02-14 12:04:07 -08:00
|
|
|
if (!shape) {
|
|
|
|
if (!ResolveInterpretedFunctionPrototype(cx, funobj))
|
2011-04-14 13:45:06 -07:00
|
|
|
return NULL;
|
2012-04-12 09:23:51 -07:00
|
|
|
id = ATOM_TO_JSID(cx->runtime->atomState.classPrototypeAtom);
|
2011-08-16 03:27:34 -07:00
|
|
|
shape = funobj->nativeLookup(cx, id);
|
2011-02-14 12:04:07 -08:00
|
|
|
}
|
|
|
|
JS_ASSERT(!shape->configurable());
|
|
|
|
JS_ASSERT(shape->isDataDescriptor());
|
|
|
|
JS_ASSERT(shape->hasSlot());
|
|
|
|
return shape;
|
|
|
|
}
|
|
|
|
|
2011-02-23 19:38:27 -08:00
|
|
|
} /* namespace js */
|
2011-01-26 18:28:49 -08:00
|
|
|
|
2008-03-29 03:34:29 -07:00
|
|
|
JSFunction *
|
2012-02-28 15:11:11 -08:00
|
|
|
js_NewFunction(JSContext *cx, JSObject *funobj, Native native, unsigned nargs,
|
2012-04-12 09:23:51 -07:00
|
|
|
unsigned flags, HandleObject parent, JSAtom *atom_, js::gc::AllocKind kind)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-11-18 15:20:21 -08:00
|
|
|
JS_ASSERT(kind == JSFunction::FinalizeKind || kind == JSFunction::ExtendedFinalizeKind);
|
|
|
|
JS_ASSERT(sizeof(JSFunction) <= gc::Arena::thingSize(JSFunction::FinalizeKind));
|
|
|
|
JS_ASSERT(sizeof(FunctionExtended) <= gc::Arena::thingSize(JSFunction::ExtendedFinalizeKind));
|
|
|
|
|
2012-04-12 09:23:51 -07:00
|
|
|
RootedVarAtom atom(cx, atom_);
|
|
|
|
|
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());
|
2011-10-13 12:10:52 -07:00
|
|
|
JS_ASSERT(funobj->getParent() == parent);
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
2011-11-02 13:34:19 -07:00
|
|
|
funobj = NewObjectWithClassProto(cx, &FunctionClass, NULL, SkipScopeParent(parent), kind);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!funobj)
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-08-21 07:23:48 -07:00
|
|
|
fun = static_cast<JSFunction *>(funobj);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/* Initialize all function members. */
|
Bug 708735 - Use <stdint.h> types in JSAPI and throughout SpiderMonkey. Continue to provide the {u,}int{8,16,32,64} and JS{Uint,Int}{8,16,32,64} integer types through a single header, however, for a simpler backout strategy -- and also to ease the transition for embedders. r=timeless on switching the jsd API to use the <stdint.h> types, r=luke, r=dmandelin
2011-12-08 19:54:10 -08:00
|
|
|
fun->nargs = uint16_t(nargs);
|
2011-10-26 13:02:57 -07:00
|
|
|
fun->flags = flags & (JSFUN_FLAGS_MASK | JSFUN_KINDMASK);
|
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);
|
2012-03-16 12:02:37 -07:00
|
|
|
fun->mutableScript().init(NULL);
|
2011-12-15 09:41:04 -08:00
|
|
|
fun->initEnvironment(parent);
|
2008-03-29 03:34:29 -07:00
|
|
|
} else {
|
2012-03-12 16:16:40 -07:00
|
|
|
fun->u.native = native;
|
|
|
|
JS_ASSERT(fun->u.native);
|
2008-03-29 03:34:29 -07:00
|
|
|
}
|
2011-10-28 08:43:04 -07:00
|
|
|
if (kind == JSFunction::ExtendedFinalizeKind) {
|
|
|
|
fun->flags |= JSFUN_EXTENDED;
|
2011-11-14 09:13:33 -08:00
|
|
|
fun->initializeExtended();
|
2011-10-28 08:43:04 -07:00
|
|
|
}
|
2012-03-19 17:24:05 -07:00
|
|
|
fun->atom.init(atom);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-10-30 08:27:19 -07:00
|
|
|
if (native && !fun->setSingletonType(cx))
|
|
|
|
return NULL;
|
|
|
|
|
2008-03-29 03:34:29 -07:00
|
|
|
return fun;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-10-11 15:28:54 -07:00
|
|
|
JSFunction * JS_FASTCALL
|
2012-04-12 09:23:51 -07:00
|
|
|
js_CloneFunctionObject(JSContext *cx, HandleFunction fun, HandleObject parent,
|
|
|
|
HandleObject proto, gc::AllocKind kind)
|
2007-11-18 06:10:28 -08:00
|
|
|
{
|
2010-02-19 09:44:23 -08:00
|
|
|
JS_ASSERT(parent);
|
|
|
|
JS_ASSERT(proto);
|
|
|
|
|
2011-11-02 13:34:19 -07:00
|
|
|
JSObject *cloneobj = NewObjectWithClassProto(cx, &FunctionClass, NULL, SkipScopeParent(parent), kind);
|
|
|
|
if (!cloneobj)
|
2011-10-11 15:28:54 -07:00
|
|
|
return NULL;
|
2012-04-30 17:10:30 -07:00
|
|
|
RootedVarFunction clone(cx, static_cast<JSFunction *>(cloneobj));
|
2011-10-11 15:28:54 -07:00
|
|
|
|
|
|
|
clone->nargs = fun->nargs;
|
2011-10-26 13:02:57 -07:00
|
|
|
clone->flags = fun->flags & ~JSFUN_EXTENDED;
|
2011-12-21 17:17:03 -08:00
|
|
|
if (fun->isInterpreted()) {
|
|
|
|
clone->initScript(fun->script());
|
|
|
|
clone->initEnvironment(parent);
|
|
|
|
} else {
|
2012-03-12 16:16:40 -07:00
|
|
|
clone->u.native = fun->native();
|
2011-12-21 17:17:03 -08:00
|
|
|
}
|
2012-03-19 17:24:05 -07:00
|
|
|
clone->atom.init(fun->atom);
|
2011-03-17 09:42:56 -07:00
|
|
|
|
2011-10-26 13:02:57 -07:00
|
|
|
if (kind == JSFunction::ExtendedFinalizeKind) {
|
|
|
|
clone->flags |= JSFUN_EXTENDED;
|
2011-11-14 09:13:33 -08:00
|
|
|
clone->initializeExtended();
|
2011-10-26 13:02:57 -07:00
|
|
|
}
|
|
|
|
|
2011-10-11 15:28:54 -07:00
|
|
|
if (cx->compartment == fun->compartment()) {
|
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-07-15 10:14:07 -07:00
|
|
|
if (fun->getProto() == proto && !fun->hasSingletonType())
|
2011-07-28 14:17:42 -07:00
|
|
|
clone->setType(fun->type());
|
2010-09-29 23:17:29 -07:00
|
|
|
} else {
|
|
|
|
/*
|
2011-10-11 15:28:54 -07:00
|
|
|
* Across compartments we have to clone the script for interpreted
|
|
|
|
* functions.
|
2010-09-29 23:17:29 -07:00
|
|
|
*/
|
2011-10-11 15:28:54 -07:00
|
|
|
if (clone->isInterpreted()) {
|
|
|
|
JSScript *script = clone->script();
|
2010-09-29 23:17:29 -07:00
|
|
|
JS_ASSERT(script);
|
2011-08-09 01:51:59 -07:00
|
|
|
JS_ASSERT(script->compartment() == fun->compartment());
|
|
|
|
JS_ASSERT(script->compartment() != cx->compartment);
|
2010-11-16 15:30:56 -08:00
|
|
|
|
2012-03-16 12:02:37 -07:00
|
|
|
clone->mutableScript().init(NULL);
|
2012-02-13 00:02:21 -08:00
|
|
|
JSScript *cscript = CloneScript(cx, script);
|
2011-08-02 10:13:03 -07:00
|
|
|
if (!cscript)
|
2010-11-16 15:30:56 -08:00
|
|
|
return NULL;
|
2011-08-09 13:29:11 -07:00
|
|
|
|
2012-01-02 15:02:05 -08:00
|
|
|
cscript->globalObject = &clone->global();
|
2011-10-19 14:26:08 -07:00
|
|
|
clone->setScript(cscript);
|
2011-11-14 09:13:33 -08:00
|
|
|
if (!cscript->typeSetFunction(cx, clone))
|
2011-03-07 08:25:27 -08:00
|
|
|
return NULL;
|
|
|
|
|
2011-10-11 15:28:54 -07:00
|
|
|
js_CallNewScriptHook(cx, clone->script(), clone);
|
2011-10-27 14:16:02 -07:00
|
|
|
Debugger::onNewScript(cx, clone->script(), NULL);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
JSFunction *
|
2011-12-31 06:32:04 -08:00
|
|
|
js_DefineFunction(JSContext *cx, HandleObject obj, jsid id, Native native,
|
2012-02-28 15:11:11 -08:00
|
|
|
unsigned nargs, unsigned attrs, AllocKind kind)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-12-31 06:32:04 -08:00
|
|
|
RootId idRoot(cx, &id);
|
|
|
|
|
2011-02-09 11:31:40 -08:00
|
|
|
PropertyOp gop;
|
|
|
|
StrictPropertyOp sop;
|
2011-12-31 06:32:04 -08:00
|
|
|
|
|
|
|
RootedVarFunction fun(cx);
|
2007-03-22 10:30:00 -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-09-20 11:40:24 -07:00
|
|
|
gop = JS_PropertyStub;
|
|
|
|
sop = JS_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-08-03 12:20:48 -07:00
|
|
|
fun = js_NewFunction(cx, NULL, native, nargs,
|
2011-11-22 14:41:42 -08:00
|
|
|
attrs & (JSFUN_FLAGS_MASK),
|
2010-11-10 10:59:02 -08:00
|
|
|
obj,
|
2011-10-26 13:02:57 -07:00
|
|
|
JSID_IS_ATOM(id) ? JSID_TO_ATOM(id) : NULL,
|
|
|
|
kind);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!fun)
|
|
|
|
return NULL;
|
2010-11-10 10:59:02 -08:00
|
|
|
|
2011-10-05 01:00:23 -07:00
|
|
|
if (!obj->defineGeneric(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 *
|
2012-02-28 15:11:11 -08:00
|
|
|
js_ValueToFunction(JSContext *cx, const Value *vp, unsigned flags)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-10-11 15:28:54 -07:00
|
|
|
JSFunction *fun;
|
|
|
|
if (!IsFunctionObject(*vp, &fun)) {
|
2010-08-17 09:44:41 -07:00
|
|
|
js_ReportIsNotFunction(cx, vp, flags);
|
2007-03-22 10:30:00 -07:00
|
|
|
return NULL;
|
|
|
|
}
|
2011-10-11 15:28:54 -07:00
|
|
|
return fun;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
JSObject *
|
2012-02-28 15:11:11 -08:00
|
|
|
js_ValueToCallableObject(JSContext *cx, Value *vp, unsigned 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
|
2012-02-28 15:11:11 -08:00
|
|
|
js_ReportIsNotFunction(JSContext *cx, const Value *vp, unsigned flags)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-03-03 18:10:13 -08:00
|
|
|
const char *name = NULL, *source = NULL;
|
2012-02-28 15:11:11 -08:00
|
|
|
unsigned error = (flags & JSV2F_CONSTRUCT) ? JSMSG_NOT_CONSTRUCTOR : JSMSG_NOT_FUNCTION;
|
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;
|
|
|
|
|
2012-04-27 15:03:12 -07:00
|
|
|
ScriptFrameIter i(cx);
|
2010-07-30 14:49:29 -07:00
|
|
|
if (!i.done()) {
|
2012-04-27 15:03:12 -07:00
|
|
|
unsigned depth = js_ReconstructStackDepth(cx, i.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
|
|
|
}
|