https://github.com/mozilla/gecko-dev
Tip revision: a8ff65885ca52d2ee9990b88a39c39ada5e3336b authored by Fabrice Desré on 01 October 2012, 23:55:24 UTC
Bug 782524 - window.navigator instanceof window.Window throws unexpected error [r=bz,a=akeybl]
Bug 782524 - window.navigator instanceof window.Window throws unexpected error [r=bz,a=akeybl]
Tip revision: a8ff658
jsfun.h
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef jsfun_h___
#define jsfun_h___
/*
* JS function definitions.
*/
#include "jsprvtd.h"
#include "jspubtd.h"
#include "jsobj.h"
#include "jsatom.h"
#include "jsscript.h"
#include "jsstr.h"
#include "gc/Barrier.h"
/*
* The high two bits of JSFunction.flags encode whether the function is native
* or interpreted, and if interpreted, what kind of optimized closure form (if
* any) it might be.
*
* 00 not interpreted
* 01 interpreted, not null closure
* 11 interpreted, null closure
*
* NB: JSFUN_EXPR_CLOSURE reuses JSFUN_STUB_GSOPS, which is an API request flag
* bit only, never stored in fun->flags.
*
* If we need more bits in the future, all flags for interpreted functions can
* move to u.i.script->flags. For now we use function flag bits to minimize
* pointer-chasing.
*/
#define JSFUN_PROTOTYPE 0x0800 /* function is Function.prototype for some
global object */
#define JSFUN_EXPR_CLOSURE 0x1000 /* expression closure: function(x) x*x */
#define JSFUN_EXTENDED 0x2000 /* structure is FunctionExtended */
#define JSFUN_INTERPRETED 0x4000 /* use u.i if kind >= this value else u.native */
#define JSFUN_NULL_CLOSURE 0x8000 /* null closure entrains no scope chain */
#define JSFUN_KINDMASK 0xc000 /* encode interp vs. native and closure
optimization level -- see above */
namespace js { class FunctionExtended; }
struct JSFunction : public JSObject
{
uint16_t nargs; /* maximum number of specified arguments,
reflected as f.length/f.arity */
uint16_t flags; /* flags, see JSFUN_* below and in jsapi.h */
union U {
js::Native native; /* native method pointer or null */
struct Scripted {
JSScript *script_; /* interpreted bytecode descriptor or null;
use the accessor! */
JSObject *env_; /* environment for new activations;
use the accessor! */
} i;
void *nativeOrScript;
} u;
js::HeapPtrAtom atom; /* name for diagnostics and decompiling */
bool hasDefaults() const { return flags & JSFUN_HAS_DEFAULTS; }
bool hasRest() const { return flags & JSFUN_HAS_REST; }
bool isInterpreted() const { return kind() >= JSFUN_INTERPRETED; }
bool isNative() const { return !isInterpreted(); }
bool isNativeConstructor() const { return flags & JSFUN_CONSTRUCTOR; }
bool isHeavyweight() const { return JSFUN_HEAVYWEIGHT_TEST(flags); }
bool isNullClosure() const { return kind() == JSFUN_NULL_CLOSURE; }
bool isFunctionPrototype() const { return flags & JSFUN_PROTOTYPE; }
bool isInterpretedConstructor() const { return isInterpreted() && !isFunctionPrototype(); }
bool isNamedLambda() const { return (flags & JSFUN_LAMBDA) && atom; }
uint16_t kind() const { return flags & JSFUN_KINDMASK; }
void setKind(uint16_t k) {
JS_ASSERT(!(k & ~JSFUN_KINDMASK));
flags = (flags & ~JSFUN_KINDMASK) | k;
}
/* Returns the strictness of this function, which must be interpreted. */
inline bool inStrictMode() const;
void setArgCount(uint16_t nargs) {
JS_ASSERT(this->nargs == 0);
this->nargs = nargs;
}
void setHasRest() {
JS_ASSERT(!hasRest());
this->flags |= JSFUN_HAS_REST;
}
void setHasDefaults() {
JS_ASSERT(!hasDefaults());
this->flags |= JSFUN_HAS_DEFAULTS;
}
/* uint16_t representation bounds number of call object dynamic slots. */
enum { MAX_ARGS_AND_VARS = 2 * ((1U << 16) - 1) };
/*
* For an interpreted function, accessors for the initial scope object of
* activations (stack frames) of the function.
*/
inline JSObject *environment() const;
inline void setEnvironment(JSObject *obj);
inline void initEnvironment(JSObject *obj);
static inline size_t offsetOfEnvironment() { return offsetof(JSFunction, u.i.env_); }
JSScript *script() const {
JS_ASSERT(isInterpreted());
return *(js::HeapPtrScript *)&u.i.script_;
}
js::HeapPtrScript &mutableScript() {
JS_ASSERT(isInterpreted());
return *(js::HeapPtrScript *)&u.i.script_;
}
inline void setScript(JSScript *script_);
inline void initScript(JSScript *script_);
JSScript *maybeScript() const {
return isInterpreted() ? script() : NULL;
}
JSNative native() const {
JS_ASSERT(isNative());
return u.native;
}
JSNative maybeNative() const {
return isInterpreted() ? NULL : native();
}
static unsigned offsetOfNativeOrScript() {
JS_STATIC_ASSERT(offsetof(U, native) == offsetof(U, i.script_));
JS_STATIC_ASSERT(offsetof(U, native) == offsetof(U, nativeOrScript));
return offsetof(JSFunction, u.nativeOrScript);
}
#if JS_BITS_PER_WORD == 32
static const js::gc::AllocKind FinalizeKind = js::gc::FINALIZE_OBJECT2_BACKGROUND;
static const js::gc::AllocKind ExtendedFinalizeKind = js::gc::FINALIZE_OBJECT4_BACKGROUND;
#else
static const js::gc::AllocKind FinalizeKind = js::gc::FINALIZE_OBJECT4_BACKGROUND;
static const js::gc::AllocKind ExtendedFinalizeKind = js::gc::FINALIZE_OBJECT8_BACKGROUND;
#endif
inline void trace(JSTracer *trc);
/* Bound function accessors. */
inline bool initBoundFunction(JSContext *cx, js::HandleValue thisArg,
const js::Value *args, unsigned argslen);
inline JSObject *getBoundFunctionTarget() const;
inline const js::Value &getBoundFunctionThis() const;
inline const js::Value &getBoundFunctionArgument(unsigned which) const;
inline size_t getBoundFunctionArgumentCount() const;
private:
inline js::FunctionExtended *toExtended();
inline const js::FunctionExtended *toExtended() const;
inline bool isExtended() const {
JS_STATIC_ASSERT(FinalizeKind != ExtendedFinalizeKind);
JS_ASSERT(!!(flags & JSFUN_EXTENDED) == (getAllocKind() == ExtendedFinalizeKind));
return !!(flags & JSFUN_EXTENDED);
}
public:
/* Accessors for data stored in extended functions. */
inline void initializeExtended();
inline void setExtendedSlot(size_t which, const js::Value &val);
inline const js::Value &getExtendedSlot(size_t which) const;
/* Constructs a new type for the function if necessary. */
bool setTypeForScriptedFunction(JSContext *cx, bool singleton = false);
private:
/*
* These member functions are inherited from JSObject, but should never be applied to
* a value statically known to be a JSFunction.
*/
inline JSFunction *toFunction() MOZ_DELETE;
inline const JSFunction *toFunction() const MOZ_DELETE;
};
inline JSFunction *
JSObject::toFunction()
{
JS_ASSERT(JS_ObjectIsFunction(NULL, this));
return static_cast<JSFunction *>(this);
}
inline const JSFunction *
JSObject::toFunction() const
{
JS_ASSERT(JS_ObjectIsFunction(NULL, const_cast<JSObject *>(this)));
return static_cast<const JSFunction *>(this);
}
extern JSString *
fun_toStringHelper(JSContext *cx, JSObject *obj, unsigned indent);
extern JSFunction *
js_NewFunction(JSContext *cx, JSObject *funobj, JSNative native, unsigned nargs,
unsigned flags, js::HandleObject parent, JSAtom *atom,
js::gc::AllocKind kind = JSFunction::FinalizeKind);
extern JSFunction * JS_FASTCALL
js_CloneFunctionObject(JSContext *cx, js::HandleFunction fun,
js::HandleObject parent, js::HandleObject proto,
js::gc::AllocKind kind = JSFunction::FinalizeKind);
extern JSFunction *
js_DefineFunction(JSContext *cx, js::HandleObject obj, js::HandleId id, JSNative native,
unsigned nargs, unsigned flags,
js::gc::AllocKind kind = JSFunction::FinalizeKind);
namespace js {
/*
* Function extended with reserved slots for use by various kinds of functions.
* Most functions do not have these extensions, but enough are that efficient
* storage is required (no malloc'ed reserved slots).
*/
class FunctionExtended : public JSFunction
{
friend struct JSFunction;
/* Reserved slots available for storage by particular native functions. */
HeapValue extendedSlots[2];
};
} // namespace js
inline js::FunctionExtended *
JSFunction::toExtended()
{
JS_ASSERT(isExtended());
return static_cast<js::FunctionExtended *>(this);
}
inline const js::FunctionExtended *
JSFunction::toExtended() const
{
JS_ASSERT(isExtended());
return static_cast<const js::FunctionExtended *>(this);
}
namespace js {
template<XDRMode mode>
bool
XDRInterpretedFunction(XDRState<mode> *xdr, HandleObject enclosingScope,
HandleScript enclosingScript, JSObject **objp);
extern JSObject *
CloneInterpretedFunction(JSContext *cx, HandleObject enclosingScope, HandleFunction fun);
} /* namespace js */
extern JSBool
js_fun_apply(JSContext *cx, unsigned argc, js::Value *vp);
extern JSBool
js_fun_call(JSContext *cx, unsigned argc, js::Value *vp);
extern JSObject*
js_fun_bind(JSContext *cx, js::HandleObject target, js::HandleValue thisArg,
js::Value *boundArgs, unsigned argslen);
#endif /* jsfun_h___ */