michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
michael@0: /* vim: set ts=8 sts=4 et sw=4 tw=99: */
michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0:
michael@0: #ifndef xpcquickstubs_h___
michael@0: #define xpcquickstubs_h___
michael@0:
michael@0: #include "XPCForwards.h"
michael@0:
michael@0: class qsObjectHelper;
michael@0: namespace mozilla {
michael@0: namespace dom {
michael@0: class NativePropertiesHolder;
michael@0: }
michael@0: }
michael@0:
michael@0: /* XPCQuickStubs.h - Support functions used only by quick stubs. */
michael@0:
michael@0: class XPCCallContext;
michael@0:
michael@0: #define XPC_QS_NULL_INDEX ((uint16_t) -1)
michael@0:
michael@0: struct xpc_qsPropertySpec {
michael@0: uint16_t name_index;
michael@0: JSNative getter;
michael@0: JSNative setter;
michael@0: };
michael@0:
michael@0: struct xpc_qsFunctionSpec {
michael@0: uint16_t name_index;
michael@0: uint16_t arity;
michael@0: JSNative native;
michael@0: };
michael@0:
michael@0: /** A table mapping interfaces to quick stubs. */
michael@0: struct xpc_qsHashEntry {
michael@0: nsID iid;
michael@0: uint16_t prop_index;
michael@0: uint16_t n_props;
michael@0: uint16_t func_index;
michael@0: uint16_t n_funcs;
michael@0: const mozilla::dom::NativePropertiesHolder* newBindingProperties;
michael@0: // These last two fields index to other entries in the same table.
michael@0: // XPC_QS_NULL_ENTRY indicates there are no more entries in the chain.
michael@0: uint16_t parentInterface;
michael@0: uint16_t chain;
michael@0: };
michael@0:
michael@0: bool
michael@0: xpc_qsDefineQuickStubs(JSContext *cx, JSObject *proto, unsigned extraFlags,
michael@0: uint32_t ifacec, const nsIID **interfaces,
michael@0: uint32_t tableSize, const xpc_qsHashEntry *table,
michael@0: const xpc_qsPropertySpec *propspecs,
michael@0: const xpc_qsFunctionSpec *funcspecs,
michael@0: const char *stringTable);
michael@0:
michael@0: /** Raise an exception on @a cx and return false. */
michael@0: bool
michael@0: xpc_qsThrow(JSContext *cx, nsresult rv);
michael@0:
michael@0: /**
michael@0: * Fail after an XPCOM getter or setter returned rv.
michael@0: *
michael@0: * NOTE: Here @a obj must be the JSObject whose private data field points to an
michael@0: * XPCWrappedNative, not merely an object that has an XPCWrappedNative
michael@0: * somewhere along the prototype chain! The same applies to @a obj in
michael@0: * xpc_qsThrowBadSetterValue and vp[1]
in xpc_qsThrowMethodFailed
michael@0: * and xpc_qsThrowBadArg.
michael@0: *
michael@0: * This is one reason the UnwrapThis functions below have an out parameter that
michael@0: * receives the wrapper JSObject. (The other reason is to help the caller keep
michael@0: * that JSObject GC-reachable.)
michael@0: */
michael@0: bool
michael@0: xpc_qsThrowGetterSetterFailed(JSContext *cx, nsresult rv,
michael@0: JSObject *obj, jsid memberId);
michael@0: // And variants using strings and string tables
michael@0: bool
michael@0: xpc_qsThrowGetterSetterFailed(JSContext *cx, nsresult rv,
michael@0: JSObject *obj, const char* memberName);
michael@0: bool
michael@0: xpc_qsThrowGetterSetterFailed(JSContext *cx, nsresult rv,
michael@0: JSObject *obj, uint16_t memberIndex);
michael@0:
michael@0: /**
michael@0: * Fail after an XPCOM method returned rv.
michael@0: *
michael@0: * See NOTE at xpc_qsThrowGetterSetterFailed.
michael@0: */
michael@0: bool
michael@0: xpc_qsThrowMethodFailed(JSContext *cx, nsresult rv, jsval *vp);
michael@0:
michael@0: /**
michael@0: * Fail after converting a method argument fails.
michael@0: *
michael@0: * See NOTE at xpc_qsThrowGetterSetterFailed.
michael@0: */
michael@0: void
michael@0: xpc_qsThrowBadArg(JSContext *cx, nsresult rv, jsval *vp, unsigned paramnum);
michael@0:
michael@0: void
michael@0: xpc_qsThrowBadArgWithCcx(XPCCallContext &ccx, nsresult rv, unsigned paramnum);
michael@0:
michael@0: void
michael@0: xpc_qsThrowBadArgWithDetails(JSContext *cx, nsresult rv, unsigned paramnum,
michael@0: const char *ifaceName, const char *memberName);
michael@0:
michael@0: /**
michael@0: * Fail after converting a setter argument fails.
michael@0: *
michael@0: * See NOTE at xpc_qsThrowGetterSetterFailed.
michael@0: */
michael@0: void
michael@0: xpc_qsThrowBadSetterValue(JSContext *cx, nsresult rv, JSObject *obj,
michael@0: jsid propId);
michael@0: // And variants using strings and string tables
michael@0: void
michael@0: xpc_qsThrowBadSetterValue(JSContext *cx, nsresult rv, JSObject *obj,
michael@0: const char* propName);
michael@0: void
michael@0: xpc_qsThrowBadSetterValue(JSContext *cx, nsresult rv, JSObject *obj,
michael@0: uint16_t name_index);
michael@0:
michael@0:
michael@0: bool
michael@0: xpc_qsGetterOnlyPropertyStub(JSContext *cx, JS::HandleObject obj, JS::HandleId id,
michael@0: bool strict, JS::MutableHandleValue vp);
michael@0:
michael@0: bool
michael@0: xpc_qsGetterOnlyNativeStub(JSContext *cx, unsigned argc, jsval *vp);
michael@0:
michael@0: /* Functions for converting values between COM and JS. */
michael@0:
michael@0: inline bool
michael@0: xpc_qsInt64ToJsval(JSContext *cx, int64_t i, JS::MutableHandleValue rv)
michael@0: {
michael@0: rv.setNumber(static_cast(i));
michael@0: return true;
michael@0: }
michael@0:
michael@0: inline bool
michael@0: xpc_qsUint64ToJsval(JSContext *cx, uint64_t u, JS::MutableHandleValue rv)
michael@0: {
michael@0: rv.setNumber(static_cast(u));
michael@0: return true;
michael@0: }
michael@0:
michael@0:
michael@0: /* Classes for converting jsvals to string types. */
michael@0:
michael@0: template
michael@0: class xpc_qsBasicString
michael@0: {
michael@0: public:
michael@0: typedef S interface_type;
michael@0: typedef T implementation_type;
michael@0:
michael@0: ~xpc_qsBasicString()
michael@0: {
michael@0: if (mValid)
michael@0: Ptr()->~implementation_type();
michael@0: }
michael@0:
michael@0: bool IsValid() const { return mValid; }
michael@0:
michael@0: implementation_type *Ptr()
michael@0: {
michael@0: MOZ_ASSERT(mValid);
michael@0: return reinterpret_cast(mBuf);
michael@0: }
michael@0:
michael@0: const implementation_type *Ptr() const
michael@0: {
michael@0: MOZ_ASSERT(mValid);
michael@0: return reinterpret_cast(mBuf);
michael@0: }
michael@0:
michael@0: operator interface_type &()
michael@0: {
michael@0: MOZ_ASSERT(mValid);
michael@0: return *Ptr();
michael@0: }
michael@0:
michael@0: operator const interface_type &() const
michael@0: {
michael@0: MOZ_ASSERT(mValid);
michael@0: return *Ptr();
michael@0: }
michael@0:
michael@0: /* Enum that defines how JS |null| and |undefined| should be treated. See
michael@0: * the WebIDL specification. eStringify means convert to the string "null"
michael@0: * or "undefined" respectively, via the standard JS ToString() operation;
michael@0: * eEmpty means convert to the string ""; eNull means convert to an empty
michael@0: * string with the void bit set.
michael@0: *
michael@0: * Per webidl the default behavior of an unannotated interface is
michael@0: * eStringify, but our de-facto behavior has been eNull for |null| and
michael@0: * eStringify for |undefined|, so leaving it that way for now. If we ever
michael@0: * get to a point where we go through and annotate our interfaces as
michael@0: * needed, we can change that.
michael@0: */
michael@0: enum StringificationBehavior {
michael@0: eStringify,
michael@0: eEmpty,
michael@0: eNull,
michael@0: eDefaultNullBehavior = eNull,
michael@0: eDefaultUndefinedBehavior = eStringify
michael@0: };
michael@0:
michael@0: protected:
michael@0: /*
michael@0: * Neither field is initialized; that is left to the derived class
michael@0: * constructor. However, the destructor destroys the string object
michael@0: * stored in mBuf, if mValid is true.
michael@0: */
michael@0: void *mBuf[JS_HOWMANY(sizeof(implementation_type), sizeof(void *))];
michael@0: bool mValid;
michael@0:
michael@0: /*
michael@0: * If null is returned, then we either failed or fully initialized
michael@0: * |this|; in either case the caller should return immediately
michael@0: * without doing anything else. Otherwise, the JSString* created
michael@0: * from |v| will be returned. It'll be rooted, as needed, in
michael@0: * *pval. nullBehavior and undefinedBehavior control what happens
michael@0: * when |v| is JSVAL_IS_NULL and JSVAL_IS_VOID respectively.
michael@0: */
michael@0: template
michael@0: JSString* InitOrStringify(JSContext* cx, JS::HandleValue v,
michael@0: JS::MutableHandleValue pval,
michael@0: bool notpassed,
michael@0: StringificationBehavior nullBehavior,
michael@0: StringificationBehavior undefinedBehavior) {
michael@0: JSString *s;
michael@0: if (JSVAL_IS_STRING(v)) {
michael@0: s = JSVAL_TO_STRING(v);
michael@0: } else {
michael@0: StringificationBehavior behavior = eStringify;
michael@0: if (JSVAL_IS_NULL(v)) {
michael@0: behavior = nullBehavior;
michael@0: } else if (JSVAL_IS_VOID(v)) {
michael@0: behavior = undefinedBehavior;
michael@0: }
michael@0:
michael@0: // If pval is null, that means the argument was optional and
michael@0: // not passed; turn those into void strings if they're
michael@0: // supposed to be stringified.
michael@0: if (behavior != eStringify || notpassed) {
michael@0: // Here behavior == eStringify implies notpassed, so both eNull and
michael@0: // eStringify should end up with void strings.
michael@0: (new(mBuf) implementation_type(traits::sEmptyBuffer, uint32_t(0)))->
michael@0: SetIsVoid(behavior != eEmpty);
michael@0: mValid = true;
michael@0: return nullptr;
michael@0: }
michael@0:
michael@0: s = JS::ToString(cx, v);
michael@0: if (!s) {
michael@0: mValid = false;
michael@0: return nullptr;
michael@0: }
michael@0: pval.setString(s); // Root the new string.
michael@0: }
michael@0:
michael@0: return s;
michael@0: }
michael@0: };
michael@0:
michael@0: /**
michael@0: * Class for converting a jsval to DOMString.
michael@0: *
michael@0: * xpc_qsDOMString arg0(cx, &argv[0]);
michael@0: * if (!arg0.IsValid())
michael@0: * return false;
michael@0: *
michael@0: * The second argument to the constructor is an in-out parameter. It must
michael@0: * point to a rooted jsval, such as a JSNative argument or return value slot.
michael@0: * The value in the jsval on entry is converted to a string. The constructor
michael@0: * may overwrite that jsval with a string value, to protect the characters of
michael@0: * the string from garbage collection. The caller must leave the jsval alone
michael@0: * for the lifetime of the xpc_qsDOMString.
michael@0: */
michael@0: class xpc_qsDOMString : public xpc_qsBasicString
michael@0: {
michael@0: public:
michael@0: xpc_qsDOMString(JSContext *cx, JS::HandleValue v,
michael@0: JS::MutableHandleValue pval, bool notpassed,
michael@0: StringificationBehavior nullBehavior,
michael@0: StringificationBehavior undefinedBehavior);
michael@0: };
michael@0:
michael@0: /**
michael@0: * The same as xpc_qsDOMString, but with slightly different conversion behavior,
michael@0: * corresponding to the [astring] magic XPIDL annotation rather than [domstring].
michael@0: */
michael@0: class xpc_qsAString : public xpc_qsDOMString
michael@0: {
michael@0: public:
michael@0: xpc_qsAString(JSContext *cx, JS::HandleValue v,
michael@0: JS::MutableHandleValue pval, bool notpassed)
michael@0: : xpc_qsDOMString(cx, v, pval, notpassed, eNull, eNull)
michael@0: {}
michael@0: };
michael@0:
michael@0: /**
michael@0: * Like xpc_qsDOMString and xpc_qsAString, but for XPIDL native types annotated
michael@0: * with [cstring] rather than [domstring] or [astring].
michael@0: */
michael@0: class xpc_qsACString : public xpc_qsBasicString
michael@0: {
michael@0: public:
michael@0: xpc_qsACString(JSContext *cx, JS::HandleValue v,
michael@0: JS::MutableHandleValue pval, bool notpassed,
michael@0: StringificationBehavior nullBehavior = eNull,
michael@0: StringificationBehavior undefinedBehavior = eNull);
michael@0: };
michael@0:
michael@0: /**
michael@0: * And similar for AUTF8String.
michael@0: */
michael@0: class xpc_qsAUTF8String :
michael@0: public xpc_qsBasicString
michael@0: {
michael@0: public:
michael@0: xpc_qsAUTF8String(JSContext* cx, JS::HandleValue v,
michael@0: JS::MutableHandleValue pval, bool notpassed);
michael@0: };
michael@0:
michael@0: struct xpc_qsSelfRef
michael@0: {
michael@0: xpc_qsSelfRef() : ptr(nullptr) {}
michael@0: explicit xpc_qsSelfRef(nsISupports *p) : ptr(p) {}
michael@0: ~xpc_qsSelfRef() { NS_IF_RELEASE(ptr); }
michael@0:
michael@0: nsISupports* ptr;
michael@0: };
michael@0:
michael@0: /**
michael@0: * Convert a jsval to char*, returning true on success.
michael@0: *
michael@0: * @param cx
michael@0: * A context.
michael@0: * @param v
michael@0: * A value to convert.
michael@0: * @param bytes
michael@0: * Out. On success it receives the converted string unless v is null or
michael@0: * undefinedin which case bytes->ptr() remains null.
michael@0: */
michael@0: bool
michael@0: xpc_qsJsvalToCharStr(JSContext *cx, jsval v, JSAutoByteString *bytes);
michael@0:
michael@0: bool
michael@0: xpc_qsJsvalToWcharStr(JSContext *cx, jsval v, JS::MutableHandleValue pval, const char16_t **pstr);
michael@0:
michael@0:
michael@0: nsresult
michael@0: getWrapper(JSContext *cx,
michael@0: JSObject *obj,
michael@0: XPCWrappedNative **wrapper,
michael@0: JSObject **cur,
michael@0: XPCWrappedNativeTearOff **tearoff);
michael@0:
michael@0: nsresult
michael@0: castNative(JSContext *cx,
michael@0: XPCWrappedNative *wrapper,
michael@0: JSObject *cur,
michael@0: XPCWrappedNativeTearOff *tearoff,
michael@0: const nsIID &iid,
michael@0: void **ppThis,
michael@0: nsISupports **ppThisRef,
michael@0: JS::MutableHandleValue vp);
michael@0:
michael@0: /**
michael@0: * Search @a obj and its prototype chain for an XPCOM object that implements
michael@0: * the interface T.
michael@0: *
michael@0: * If an object implementing T is found, store a reference to the wrapper
michael@0: * JSObject in @a *pThisVal, store a pointer to the T in @a *ppThis, and return
michael@0: * true. Otherwise, raise an exception on @a cx and return false.
michael@0: *
michael@0: * @a *pThisRef receives the same pointer as *ppThis if the T was AddRefed.
michael@0: * Otherwise it receives null (even on error).
michael@0: *
michael@0: * This supports split objects and XPConnect tear-offs and it sees through
michael@0: * XOWs, XPCNativeWrappers, and SafeJSObjectWrappers.
michael@0: *
michael@0: * Requires a request on @a cx.
michael@0: */
michael@0: template
michael@0: inline bool
michael@0: xpc_qsUnwrapThis(JSContext *cx,
michael@0: JS::HandleObject obj,
michael@0: T **ppThis,
michael@0: nsISupports **pThisRef,
michael@0: JS::MutableHandleValue pThisVal,
michael@0: bool failureFatal = true)
michael@0: {
michael@0: XPCWrappedNative *wrapper;
michael@0: XPCWrappedNativeTearOff *tearoff;
michael@0: JS::RootedObject current(cx);
michael@0: nsresult rv = getWrapper(cx, obj, &wrapper, current.address(), &tearoff);
michael@0: if (NS_SUCCEEDED(rv))
michael@0: rv = castNative(cx, wrapper, current, tearoff, NS_GET_TEMPLATE_IID(T),
michael@0: reinterpret_cast(ppThis), pThisRef, pThisVal);
michael@0:
michael@0: if (failureFatal)
michael@0: return NS_SUCCEEDED(rv) || xpc_qsThrow(cx, rv);
michael@0:
michael@0: if (NS_FAILED(rv))
michael@0: *ppThis = nullptr;
michael@0: return true;
michael@0: }
michael@0:
michael@0: nsISupports*
michael@0: castNativeFromWrapper(JSContext *cx,
michael@0: JSObject *obj,
michael@0: uint32_t interfaceBit,
michael@0: uint32_t protoID,
michael@0: int32_t protoDepth,
michael@0: nsISupports **pRef,
michael@0: JS::MutableHandleValue pVal,
michael@0: nsresult *rv);
michael@0:
michael@0: bool
michael@0: xpc_qsUnwrapThisFromCcxImpl(XPCCallContext &ccx,
michael@0: const nsIID &iid,
michael@0: void **ppThis,
michael@0: nsISupports **pThisRef,
michael@0: JS::MutableHandleValue vp);
michael@0:
michael@0: /**
michael@0: * Alternate implementation of xpc_qsUnwrapThis using information already
michael@0: * present in the given XPCCallContext.
michael@0: */
michael@0: template
michael@0: inline bool
michael@0: xpc_qsUnwrapThisFromCcx(XPCCallContext &ccx,
michael@0: T **ppThis,
michael@0: nsISupports **pThisRef,
michael@0: JS::MutableHandleValue pThisVal)
michael@0: {
michael@0: return xpc_qsUnwrapThisFromCcxImpl(ccx,
michael@0: NS_GET_TEMPLATE_IID(T),
michael@0: reinterpret_cast(ppThis),
michael@0: pThisRef,
michael@0: pThisVal);
michael@0: }
michael@0:
michael@0: MOZ_ALWAYS_INLINE JSObject*
michael@0: xpc_qsUnwrapObj(jsval v, nsISupports **ppArgRef, nsresult *rv)
michael@0: {
michael@0: *rv = NS_OK;
michael@0: if (v.isObject()) {
michael@0: return &v.toObject();
michael@0: }
michael@0:
michael@0: if (!v.isNullOrUndefined()) {
michael@0: *rv = ((v.isInt32() && v.toInt32() == 0)
michael@0: ? NS_ERROR_XPC_BAD_CONVERT_JS_ZERO_ISNOT_NULL
michael@0: : NS_ERROR_XPC_BAD_CONVERT_JS);
michael@0: }
michael@0:
michael@0: *ppArgRef = nullptr;
michael@0: return nullptr;
michael@0: }
michael@0:
michael@0: nsresult
michael@0: xpc_qsUnwrapArgImpl(JSContext *cx, JS::HandleValue v, const nsIID &iid, void **ppArg,
michael@0: nsISupports **ppArgRef, JS::MutableHandleValue vp);
michael@0:
michael@0: /** Convert a jsval to an XPCOM pointer. */
michael@0: template
michael@0: inline nsresult
michael@0: xpc_qsUnwrapArg(JSContext *cx, JS::HandleValue v, Interface **ppArg,
michael@0: StrongRefType **ppArgRef, JS::MutableHandleValue vp)
michael@0: {
michael@0: nsISupports* argRef = *ppArgRef;
michael@0: nsresult rv = xpc_qsUnwrapArgImpl(cx, v, NS_GET_TEMPLATE_IID(Interface),
michael@0: reinterpret_cast(ppArg), &argRef,
michael@0: vp);
michael@0: *ppArgRef = static_cast(argRef);
michael@0: return rv;
michael@0: }
michael@0:
michael@0: MOZ_ALWAYS_INLINE nsISupports*
michael@0: castNativeArgFromWrapper(JSContext *cx,
michael@0: jsval v,
michael@0: uint32_t bit,
michael@0: uint32_t protoID,
michael@0: int32_t protoDepth,
michael@0: nsISupports **pArgRef,
michael@0: JS::MutableHandleValue vp,
michael@0: nsresult *rv)
michael@0: {
michael@0: JSObject *src = xpc_qsUnwrapObj(v, pArgRef, rv);
michael@0: if (!src)
michael@0: return nullptr;
michael@0:
michael@0: return castNativeFromWrapper(cx, src, bit, protoID, protoDepth, pArgRef, vp, rv);
michael@0: }
michael@0:
michael@0: inline nsWrapperCache*
michael@0: xpc_qsGetWrapperCache(nsWrapperCache *cache)
michael@0: {
michael@0: return cache;
michael@0: }
michael@0:
michael@0: inline nsWrapperCache*
michael@0: xpc_qsGetWrapperCache(void *p)
michael@0: {
michael@0: return nullptr;
michael@0: }
michael@0:
michael@0: /** Convert an XPCOM pointer to jsval. Return true on success.
michael@0: * aIdentity is a performance optimization. Set it to true,
michael@0: * only if p is the identity pointer.
michael@0: */
michael@0: bool
michael@0: xpc_qsXPCOMObjectToJsval(JSContext *aCx,
michael@0: qsObjectHelper &aHelper,
michael@0: const nsIID *iid,
michael@0: XPCNativeInterface **iface,
michael@0: JS::MutableHandleValue rval);
michael@0:
michael@0: /**
michael@0: * Convert a variant to jsval. Return true on success.
michael@0: */
michael@0: bool
michael@0: xpc_qsVariantToJsval(JSContext *cx,
michael@0: nsIVariant *p,
michael@0: JS::MutableHandleValue rval);
michael@0:
michael@0: #ifdef DEBUG
michael@0: void
michael@0: xpc_qsAssertContextOK(JSContext *cx);
michael@0:
michael@0: inline bool
michael@0: xpc_qsSameResult(nsISupports *result1, nsISupports *result2)
michael@0: {
michael@0: return SameCOMIdentity(result1, result2);
michael@0: }
michael@0:
michael@0: inline bool
michael@0: xpc_qsSameResult(const nsString &result1, const nsString &result2)
michael@0: {
michael@0: return result1.Equals(result2);
michael@0: }
michael@0:
michael@0: inline bool
michael@0: xpc_qsSameResult(int32_t result1, int32_t result2)
michael@0: {
michael@0: return result1 == result2;
michael@0: }
michael@0:
michael@0: #define XPC_QS_ASSERT_CONTEXT_OK(cx) xpc_qsAssertContextOK(cx)
michael@0: #else
michael@0: #define XPC_QS_ASSERT_CONTEXT_OK(cx) ((void) 0)
michael@0: #endif
michael@0:
michael@0: // Apply |op| to |obj|, |id|, and |vp|. If |op| is a setter, treat the assignment as lenient.
michael@0: template
michael@0: inline bool ApplyPropertyOp(JSContext *cx, Op op, JS::HandleObject obj, JS::HandleId id,
michael@0: JS::MutableHandleValue vp);
michael@0:
michael@0: template<>
michael@0: inline bool
michael@0: ApplyPropertyOp(JSContext *cx, JSPropertyOp op, JS::HandleObject obj, JS::HandleId id,
michael@0: JS::MutableHandleValue vp)
michael@0: {
michael@0: return op(cx, obj, id, vp);
michael@0: }
michael@0:
michael@0: template<>
michael@0: inline bool
michael@0: ApplyPropertyOp(JSContext *cx, JSStrictPropertyOp op, JS::HandleObject obj,
michael@0: JS::HandleId id, JS::MutableHandleValue vp)
michael@0: {
michael@0: return op(cx, obj, id, true, vp);
michael@0: }
michael@0:
michael@0: template
michael@0: bool
michael@0: PropertyOpForwarder(JSContext *cx, unsigned argc, jsval *vp)
michael@0: {
michael@0: // Layout:
michael@0: // this = our this
michael@0: // property op to call = callee reserved slot 0
michael@0: // name of the property = callee reserved slot 1
michael@0:
michael@0: JS::CallArgs args = CallArgsFromVp(argc, vp);
michael@0:
michael@0: JS::RootedObject callee(cx, &args.callee());
michael@0: JS::RootedObject obj(cx, JS_THIS_OBJECT(cx, vp));
michael@0: if (!obj)
michael@0: return false;
michael@0:
michael@0: JS::RootedValue v(cx, js::GetFunctionNativeReserved(callee, 0));
michael@0:
michael@0: JSObject *ptrobj = JSVAL_TO_OBJECT(v);
michael@0: Op *popp = static_cast(JS_GetPrivate(ptrobj));
michael@0:
michael@0: v = js::GetFunctionNativeReserved(callee, 1);
michael@0:
michael@0: JS::RootedValue argval(cx, args.get(0));
michael@0: JS::RootedId id(cx);
michael@0: if (!JS_ValueToId(cx, v, &id))
michael@0: return false;
michael@0: args.rval().set(argval);
michael@0: return ApplyPropertyOp(cx, *popp, obj, id, args.rval());
michael@0: }
michael@0:
michael@0: extern const JSClass PointerHolderClass;
michael@0:
michael@0: template
michael@0: JSObject *
michael@0: GeneratePropertyOp(JSContext *cx, JS::HandleObject obj, JS::HandleId id, unsigned argc, Op pop)
michael@0: {
michael@0: // The JS engine provides two reserved slots on function objects for
michael@0: // XPConnect to use. Use them to stick the necessary info here.
michael@0: JSFunction *fun =
michael@0: js::NewFunctionByIdWithReserved(cx, PropertyOpForwarder, argc, 0, obj, id);
michael@0: if (!fun)
michael@0: return nullptr;
michael@0:
michael@0: JS::RootedObject funobj(cx, JS_GetFunctionObject(fun));
michael@0:
michael@0: // Unfortunately, we cannot guarantee that Op is aligned. Use a
michael@0: // second object to work around this.
michael@0: JSObject *ptrobj = JS_NewObject(cx, &PointerHolderClass, JS::NullPtr(), funobj);
michael@0: if (!ptrobj)
michael@0: return nullptr;
michael@0: Op *popp = new Op;
michael@0: if (!popp)
michael@0: return nullptr;
michael@0: *popp = pop;
michael@0: JS_SetPrivate(ptrobj, popp);
michael@0:
michael@0: js::SetFunctionNativeReserved(funobj, 0, OBJECT_TO_JSVAL(ptrobj));
michael@0: js::SetFunctionNativeReserved(funobj, 1, js::IdToValue(id));
michael@0: return funobj;
michael@0: }
michael@0:
michael@0: #endif /* xpcquickstubs_h___ */