1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/xpconnect/src/XPCQuickStubs.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,644 @@ 1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 1.5 +/* vim: set ts=8 sts=4 et sw=4 tw=99: */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +#ifndef xpcquickstubs_h___ 1.11 +#define xpcquickstubs_h___ 1.12 + 1.13 +#include "XPCForwards.h" 1.14 + 1.15 +class qsObjectHelper; 1.16 +namespace mozilla { 1.17 +namespace dom { 1.18 +class NativePropertiesHolder; 1.19 +} 1.20 +} 1.21 + 1.22 +/* XPCQuickStubs.h - Support functions used only by quick stubs. */ 1.23 + 1.24 +class XPCCallContext; 1.25 + 1.26 +#define XPC_QS_NULL_INDEX ((uint16_t) -1) 1.27 + 1.28 +struct xpc_qsPropertySpec { 1.29 + uint16_t name_index; 1.30 + JSNative getter; 1.31 + JSNative setter; 1.32 +}; 1.33 + 1.34 +struct xpc_qsFunctionSpec { 1.35 + uint16_t name_index; 1.36 + uint16_t arity; 1.37 + JSNative native; 1.38 +}; 1.39 + 1.40 +/** A table mapping interfaces to quick stubs. */ 1.41 +struct xpc_qsHashEntry { 1.42 + nsID iid; 1.43 + uint16_t prop_index; 1.44 + uint16_t n_props; 1.45 + uint16_t func_index; 1.46 + uint16_t n_funcs; 1.47 + const mozilla::dom::NativePropertiesHolder* newBindingProperties; 1.48 + // These last two fields index to other entries in the same table. 1.49 + // XPC_QS_NULL_ENTRY indicates there are no more entries in the chain. 1.50 + uint16_t parentInterface; 1.51 + uint16_t chain; 1.52 +}; 1.53 + 1.54 +bool 1.55 +xpc_qsDefineQuickStubs(JSContext *cx, JSObject *proto, unsigned extraFlags, 1.56 + uint32_t ifacec, const nsIID **interfaces, 1.57 + uint32_t tableSize, const xpc_qsHashEntry *table, 1.58 + const xpc_qsPropertySpec *propspecs, 1.59 + const xpc_qsFunctionSpec *funcspecs, 1.60 + const char *stringTable); 1.61 + 1.62 +/** Raise an exception on @a cx and return false. */ 1.63 +bool 1.64 +xpc_qsThrow(JSContext *cx, nsresult rv); 1.65 + 1.66 +/** 1.67 + * Fail after an XPCOM getter or setter returned rv. 1.68 + * 1.69 + * NOTE: Here @a obj must be the JSObject whose private data field points to an 1.70 + * XPCWrappedNative, not merely an object that has an XPCWrappedNative 1.71 + * somewhere along the prototype chain! The same applies to @a obj in 1.72 + * xpc_qsThrowBadSetterValue and <code>vp[1]</code> in xpc_qsThrowMethodFailed 1.73 + * and xpc_qsThrowBadArg. 1.74 + * 1.75 + * This is one reason the UnwrapThis functions below have an out parameter that 1.76 + * receives the wrapper JSObject. (The other reason is to help the caller keep 1.77 + * that JSObject GC-reachable.) 1.78 + */ 1.79 +bool 1.80 +xpc_qsThrowGetterSetterFailed(JSContext *cx, nsresult rv, 1.81 + JSObject *obj, jsid memberId); 1.82 +// And variants using strings and string tables 1.83 +bool 1.84 +xpc_qsThrowGetterSetterFailed(JSContext *cx, nsresult rv, 1.85 + JSObject *obj, const char* memberName); 1.86 +bool 1.87 +xpc_qsThrowGetterSetterFailed(JSContext *cx, nsresult rv, 1.88 + JSObject *obj, uint16_t memberIndex); 1.89 + 1.90 +/** 1.91 + * Fail after an XPCOM method returned rv. 1.92 + * 1.93 + * See NOTE at xpc_qsThrowGetterSetterFailed. 1.94 + */ 1.95 +bool 1.96 +xpc_qsThrowMethodFailed(JSContext *cx, nsresult rv, jsval *vp); 1.97 + 1.98 +/** 1.99 + * Fail after converting a method argument fails. 1.100 + * 1.101 + * See NOTE at xpc_qsThrowGetterSetterFailed. 1.102 + */ 1.103 +void 1.104 +xpc_qsThrowBadArg(JSContext *cx, nsresult rv, jsval *vp, unsigned paramnum); 1.105 + 1.106 +void 1.107 +xpc_qsThrowBadArgWithCcx(XPCCallContext &ccx, nsresult rv, unsigned paramnum); 1.108 + 1.109 +void 1.110 +xpc_qsThrowBadArgWithDetails(JSContext *cx, nsresult rv, unsigned paramnum, 1.111 + const char *ifaceName, const char *memberName); 1.112 + 1.113 +/** 1.114 + * Fail after converting a setter argument fails. 1.115 + * 1.116 + * See NOTE at xpc_qsThrowGetterSetterFailed. 1.117 + */ 1.118 +void 1.119 +xpc_qsThrowBadSetterValue(JSContext *cx, nsresult rv, JSObject *obj, 1.120 + jsid propId); 1.121 +// And variants using strings and string tables 1.122 +void 1.123 +xpc_qsThrowBadSetterValue(JSContext *cx, nsresult rv, JSObject *obj, 1.124 + const char* propName); 1.125 +void 1.126 +xpc_qsThrowBadSetterValue(JSContext *cx, nsresult rv, JSObject *obj, 1.127 + uint16_t name_index); 1.128 + 1.129 + 1.130 +bool 1.131 +xpc_qsGetterOnlyPropertyStub(JSContext *cx, JS::HandleObject obj, JS::HandleId id, 1.132 + bool strict, JS::MutableHandleValue vp); 1.133 + 1.134 +bool 1.135 +xpc_qsGetterOnlyNativeStub(JSContext *cx, unsigned argc, jsval *vp); 1.136 + 1.137 +/* Functions for converting values between COM and JS. */ 1.138 + 1.139 +inline bool 1.140 +xpc_qsInt64ToJsval(JSContext *cx, int64_t i, JS::MutableHandleValue rv) 1.141 +{ 1.142 + rv.setNumber(static_cast<double>(i)); 1.143 + return true; 1.144 +} 1.145 + 1.146 +inline bool 1.147 +xpc_qsUint64ToJsval(JSContext *cx, uint64_t u, JS::MutableHandleValue rv) 1.148 +{ 1.149 + rv.setNumber(static_cast<double>(u)); 1.150 + return true; 1.151 +} 1.152 + 1.153 + 1.154 +/* Classes for converting jsvals to string types. */ 1.155 + 1.156 +template <class S, class T> 1.157 +class xpc_qsBasicString 1.158 +{ 1.159 +public: 1.160 + typedef S interface_type; 1.161 + typedef T implementation_type; 1.162 + 1.163 + ~xpc_qsBasicString() 1.164 + { 1.165 + if (mValid) 1.166 + Ptr()->~implementation_type(); 1.167 + } 1.168 + 1.169 + bool IsValid() const { return mValid; } 1.170 + 1.171 + implementation_type *Ptr() 1.172 + { 1.173 + MOZ_ASSERT(mValid); 1.174 + return reinterpret_cast<implementation_type *>(mBuf); 1.175 + } 1.176 + 1.177 + const implementation_type *Ptr() const 1.178 + { 1.179 + MOZ_ASSERT(mValid); 1.180 + return reinterpret_cast<const implementation_type *>(mBuf); 1.181 + } 1.182 + 1.183 + operator interface_type &() 1.184 + { 1.185 + MOZ_ASSERT(mValid); 1.186 + return *Ptr(); 1.187 + } 1.188 + 1.189 + operator const interface_type &() const 1.190 + { 1.191 + MOZ_ASSERT(mValid); 1.192 + return *Ptr(); 1.193 + } 1.194 + 1.195 + /* Enum that defines how JS |null| and |undefined| should be treated. See 1.196 + * the WebIDL specification. eStringify means convert to the string "null" 1.197 + * or "undefined" respectively, via the standard JS ToString() operation; 1.198 + * eEmpty means convert to the string ""; eNull means convert to an empty 1.199 + * string with the void bit set. 1.200 + * 1.201 + * Per webidl the default behavior of an unannotated interface is 1.202 + * eStringify, but our de-facto behavior has been eNull for |null| and 1.203 + * eStringify for |undefined|, so leaving it that way for now. If we ever 1.204 + * get to a point where we go through and annotate our interfaces as 1.205 + * needed, we can change that. 1.206 + */ 1.207 + enum StringificationBehavior { 1.208 + eStringify, 1.209 + eEmpty, 1.210 + eNull, 1.211 + eDefaultNullBehavior = eNull, 1.212 + eDefaultUndefinedBehavior = eStringify 1.213 + }; 1.214 + 1.215 +protected: 1.216 + /* 1.217 + * Neither field is initialized; that is left to the derived class 1.218 + * constructor. However, the destructor destroys the string object 1.219 + * stored in mBuf, if mValid is true. 1.220 + */ 1.221 + void *mBuf[JS_HOWMANY(sizeof(implementation_type), sizeof(void *))]; 1.222 + bool mValid; 1.223 + 1.224 + /* 1.225 + * If null is returned, then we either failed or fully initialized 1.226 + * |this|; in either case the caller should return immediately 1.227 + * without doing anything else. Otherwise, the JSString* created 1.228 + * from |v| will be returned. It'll be rooted, as needed, in 1.229 + * *pval. nullBehavior and undefinedBehavior control what happens 1.230 + * when |v| is JSVAL_IS_NULL and JSVAL_IS_VOID respectively. 1.231 + */ 1.232 + template<class traits> 1.233 + JSString* InitOrStringify(JSContext* cx, JS::HandleValue v, 1.234 + JS::MutableHandleValue pval, 1.235 + bool notpassed, 1.236 + StringificationBehavior nullBehavior, 1.237 + StringificationBehavior undefinedBehavior) { 1.238 + JSString *s; 1.239 + if (JSVAL_IS_STRING(v)) { 1.240 + s = JSVAL_TO_STRING(v); 1.241 + } else { 1.242 + StringificationBehavior behavior = eStringify; 1.243 + if (JSVAL_IS_NULL(v)) { 1.244 + behavior = nullBehavior; 1.245 + } else if (JSVAL_IS_VOID(v)) { 1.246 + behavior = undefinedBehavior; 1.247 + } 1.248 + 1.249 + // If pval is null, that means the argument was optional and 1.250 + // not passed; turn those into void strings if they're 1.251 + // supposed to be stringified. 1.252 + if (behavior != eStringify || notpassed) { 1.253 + // Here behavior == eStringify implies notpassed, so both eNull and 1.254 + // eStringify should end up with void strings. 1.255 + (new(mBuf) implementation_type(traits::sEmptyBuffer, uint32_t(0)))-> 1.256 + SetIsVoid(behavior != eEmpty); 1.257 + mValid = true; 1.258 + return nullptr; 1.259 + } 1.260 + 1.261 + s = JS::ToString(cx, v); 1.262 + if (!s) { 1.263 + mValid = false; 1.264 + return nullptr; 1.265 + } 1.266 + pval.setString(s); // Root the new string. 1.267 + } 1.268 + 1.269 + return s; 1.270 + } 1.271 +}; 1.272 + 1.273 +/** 1.274 + * Class for converting a jsval to DOMString. 1.275 + * 1.276 + * xpc_qsDOMString arg0(cx, &argv[0]); 1.277 + * if (!arg0.IsValid()) 1.278 + * return false; 1.279 + * 1.280 + * The second argument to the constructor is an in-out parameter. It must 1.281 + * point to a rooted jsval, such as a JSNative argument or return value slot. 1.282 + * The value in the jsval on entry is converted to a string. The constructor 1.283 + * may overwrite that jsval with a string value, to protect the characters of 1.284 + * the string from garbage collection. The caller must leave the jsval alone 1.285 + * for the lifetime of the xpc_qsDOMString. 1.286 + */ 1.287 +class xpc_qsDOMString : public xpc_qsBasicString<nsAString, nsDependentString> 1.288 +{ 1.289 +public: 1.290 + xpc_qsDOMString(JSContext *cx, JS::HandleValue v, 1.291 + JS::MutableHandleValue pval, bool notpassed, 1.292 + StringificationBehavior nullBehavior, 1.293 + StringificationBehavior undefinedBehavior); 1.294 +}; 1.295 + 1.296 +/** 1.297 + * The same as xpc_qsDOMString, but with slightly different conversion behavior, 1.298 + * corresponding to the [astring] magic XPIDL annotation rather than [domstring]. 1.299 + */ 1.300 +class xpc_qsAString : public xpc_qsDOMString 1.301 +{ 1.302 +public: 1.303 + xpc_qsAString(JSContext *cx, JS::HandleValue v, 1.304 + JS::MutableHandleValue pval, bool notpassed) 1.305 + : xpc_qsDOMString(cx, v, pval, notpassed, eNull, eNull) 1.306 + {} 1.307 +}; 1.308 + 1.309 +/** 1.310 + * Like xpc_qsDOMString and xpc_qsAString, but for XPIDL native types annotated 1.311 + * with [cstring] rather than [domstring] or [astring]. 1.312 + */ 1.313 +class xpc_qsACString : public xpc_qsBasicString<nsACString, nsCString> 1.314 +{ 1.315 +public: 1.316 + xpc_qsACString(JSContext *cx, JS::HandleValue v, 1.317 + JS::MutableHandleValue pval, bool notpassed, 1.318 + StringificationBehavior nullBehavior = eNull, 1.319 + StringificationBehavior undefinedBehavior = eNull); 1.320 +}; 1.321 + 1.322 +/** 1.323 + * And similar for AUTF8String. 1.324 + */ 1.325 +class xpc_qsAUTF8String : 1.326 + public xpc_qsBasicString<nsACString, NS_ConvertUTF16toUTF8> 1.327 +{ 1.328 +public: 1.329 + xpc_qsAUTF8String(JSContext* cx, JS::HandleValue v, 1.330 + JS::MutableHandleValue pval, bool notpassed); 1.331 +}; 1.332 + 1.333 +struct xpc_qsSelfRef 1.334 +{ 1.335 + xpc_qsSelfRef() : ptr(nullptr) {} 1.336 + explicit xpc_qsSelfRef(nsISupports *p) : ptr(p) {} 1.337 + ~xpc_qsSelfRef() { NS_IF_RELEASE(ptr); } 1.338 + 1.339 + nsISupports* ptr; 1.340 +}; 1.341 + 1.342 +/** 1.343 + * Convert a jsval to char*, returning true on success. 1.344 + * 1.345 + * @param cx 1.346 + * A context. 1.347 + * @param v 1.348 + * A value to convert. 1.349 + * @param bytes 1.350 + * Out. On success it receives the converted string unless v is null or 1.351 + * undefinedin which case bytes->ptr() remains null. 1.352 + */ 1.353 +bool 1.354 +xpc_qsJsvalToCharStr(JSContext *cx, jsval v, JSAutoByteString *bytes); 1.355 + 1.356 +bool 1.357 +xpc_qsJsvalToWcharStr(JSContext *cx, jsval v, JS::MutableHandleValue pval, const char16_t **pstr); 1.358 + 1.359 + 1.360 +nsresult 1.361 +getWrapper(JSContext *cx, 1.362 + JSObject *obj, 1.363 + XPCWrappedNative **wrapper, 1.364 + JSObject **cur, 1.365 + XPCWrappedNativeTearOff **tearoff); 1.366 + 1.367 +nsresult 1.368 +castNative(JSContext *cx, 1.369 + XPCWrappedNative *wrapper, 1.370 + JSObject *cur, 1.371 + XPCWrappedNativeTearOff *tearoff, 1.372 + const nsIID &iid, 1.373 + void **ppThis, 1.374 + nsISupports **ppThisRef, 1.375 + JS::MutableHandleValue vp); 1.376 + 1.377 +/** 1.378 + * Search @a obj and its prototype chain for an XPCOM object that implements 1.379 + * the interface T. 1.380 + * 1.381 + * If an object implementing T is found, store a reference to the wrapper 1.382 + * JSObject in @a *pThisVal, store a pointer to the T in @a *ppThis, and return 1.383 + * true. Otherwise, raise an exception on @a cx and return false. 1.384 + * 1.385 + * @a *pThisRef receives the same pointer as *ppThis if the T was AddRefed. 1.386 + * Otherwise it receives null (even on error). 1.387 + * 1.388 + * This supports split objects and XPConnect tear-offs and it sees through 1.389 + * XOWs, XPCNativeWrappers, and SafeJSObjectWrappers. 1.390 + * 1.391 + * Requires a request on @a cx. 1.392 + */ 1.393 +template <class T> 1.394 +inline bool 1.395 +xpc_qsUnwrapThis(JSContext *cx, 1.396 + JS::HandleObject obj, 1.397 + T **ppThis, 1.398 + nsISupports **pThisRef, 1.399 + JS::MutableHandleValue pThisVal, 1.400 + bool failureFatal = true) 1.401 +{ 1.402 + XPCWrappedNative *wrapper; 1.403 + XPCWrappedNativeTearOff *tearoff; 1.404 + JS::RootedObject current(cx); 1.405 + nsresult rv = getWrapper(cx, obj, &wrapper, current.address(), &tearoff); 1.406 + if (NS_SUCCEEDED(rv)) 1.407 + rv = castNative(cx, wrapper, current, tearoff, NS_GET_TEMPLATE_IID(T), 1.408 + reinterpret_cast<void **>(ppThis), pThisRef, pThisVal); 1.409 + 1.410 + if (failureFatal) 1.411 + return NS_SUCCEEDED(rv) || xpc_qsThrow(cx, rv); 1.412 + 1.413 + if (NS_FAILED(rv)) 1.414 + *ppThis = nullptr; 1.415 + return true; 1.416 +} 1.417 + 1.418 +nsISupports* 1.419 +castNativeFromWrapper(JSContext *cx, 1.420 + JSObject *obj, 1.421 + uint32_t interfaceBit, 1.422 + uint32_t protoID, 1.423 + int32_t protoDepth, 1.424 + nsISupports **pRef, 1.425 + JS::MutableHandleValue pVal, 1.426 + nsresult *rv); 1.427 + 1.428 +bool 1.429 +xpc_qsUnwrapThisFromCcxImpl(XPCCallContext &ccx, 1.430 + const nsIID &iid, 1.431 + void **ppThis, 1.432 + nsISupports **pThisRef, 1.433 + JS::MutableHandleValue vp); 1.434 + 1.435 +/** 1.436 + * Alternate implementation of xpc_qsUnwrapThis using information already 1.437 + * present in the given XPCCallContext. 1.438 + */ 1.439 +template <class T> 1.440 +inline bool 1.441 +xpc_qsUnwrapThisFromCcx(XPCCallContext &ccx, 1.442 + T **ppThis, 1.443 + nsISupports **pThisRef, 1.444 + JS::MutableHandleValue pThisVal) 1.445 +{ 1.446 + return xpc_qsUnwrapThisFromCcxImpl(ccx, 1.447 + NS_GET_TEMPLATE_IID(T), 1.448 + reinterpret_cast<void **>(ppThis), 1.449 + pThisRef, 1.450 + pThisVal); 1.451 +} 1.452 + 1.453 +MOZ_ALWAYS_INLINE JSObject* 1.454 +xpc_qsUnwrapObj(jsval v, nsISupports **ppArgRef, nsresult *rv) 1.455 +{ 1.456 + *rv = NS_OK; 1.457 + if (v.isObject()) { 1.458 + return &v.toObject(); 1.459 + } 1.460 + 1.461 + if (!v.isNullOrUndefined()) { 1.462 + *rv = ((v.isInt32() && v.toInt32() == 0) 1.463 + ? NS_ERROR_XPC_BAD_CONVERT_JS_ZERO_ISNOT_NULL 1.464 + : NS_ERROR_XPC_BAD_CONVERT_JS); 1.465 + } 1.466 + 1.467 + *ppArgRef = nullptr; 1.468 + return nullptr; 1.469 +} 1.470 + 1.471 +nsresult 1.472 +xpc_qsUnwrapArgImpl(JSContext *cx, JS::HandleValue v, const nsIID &iid, void **ppArg, 1.473 + nsISupports **ppArgRef, JS::MutableHandleValue vp); 1.474 + 1.475 +/** Convert a jsval to an XPCOM pointer. */ 1.476 +template <class Interface, class StrongRefType> 1.477 +inline nsresult 1.478 +xpc_qsUnwrapArg(JSContext *cx, JS::HandleValue v, Interface **ppArg, 1.479 + StrongRefType **ppArgRef, JS::MutableHandleValue vp) 1.480 +{ 1.481 + nsISupports* argRef = *ppArgRef; 1.482 + nsresult rv = xpc_qsUnwrapArgImpl(cx, v, NS_GET_TEMPLATE_IID(Interface), 1.483 + reinterpret_cast<void **>(ppArg), &argRef, 1.484 + vp); 1.485 + *ppArgRef = static_cast<StrongRefType*>(argRef); 1.486 + return rv; 1.487 +} 1.488 + 1.489 +MOZ_ALWAYS_INLINE nsISupports* 1.490 +castNativeArgFromWrapper(JSContext *cx, 1.491 + jsval v, 1.492 + uint32_t bit, 1.493 + uint32_t protoID, 1.494 + int32_t protoDepth, 1.495 + nsISupports **pArgRef, 1.496 + JS::MutableHandleValue vp, 1.497 + nsresult *rv) 1.498 +{ 1.499 + JSObject *src = xpc_qsUnwrapObj(v, pArgRef, rv); 1.500 + if (!src) 1.501 + return nullptr; 1.502 + 1.503 + return castNativeFromWrapper(cx, src, bit, protoID, protoDepth, pArgRef, vp, rv); 1.504 +} 1.505 + 1.506 +inline nsWrapperCache* 1.507 +xpc_qsGetWrapperCache(nsWrapperCache *cache) 1.508 +{ 1.509 + return cache; 1.510 +} 1.511 + 1.512 +inline nsWrapperCache* 1.513 +xpc_qsGetWrapperCache(void *p) 1.514 +{ 1.515 + return nullptr; 1.516 +} 1.517 + 1.518 +/** Convert an XPCOM pointer to jsval. Return true on success. 1.519 + * aIdentity is a performance optimization. Set it to true, 1.520 + * only if p is the identity pointer. 1.521 + */ 1.522 +bool 1.523 +xpc_qsXPCOMObjectToJsval(JSContext *aCx, 1.524 + qsObjectHelper &aHelper, 1.525 + const nsIID *iid, 1.526 + XPCNativeInterface **iface, 1.527 + JS::MutableHandleValue rval); 1.528 + 1.529 +/** 1.530 + * Convert a variant to jsval. Return true on success. 1.531 + */ 1.532 +bool 1.533 +xpc_qsVariantToJsval(JSContext *cx, 1.534 + nsIVariant *p, 1.535 + JS::MutableHandleValue rval); 1.536 + 1.537 +#ifdef DEBUG 1.538 +void 1.539 +xpc_qsAssertContextOK(JSContext *cx); 1.540 + 1.541 +inline bool 1.542 +xpc_qsSameResult(nsISupports *result1, nsISupports *result2) 1.543 +{ 1.544 + return SameCOMIdentity(result1, result2); 1.545 +} 1.546 + 1.547 +inline bool 1.548 +xpc_qsSameResult(const nsString &result1, const nsString &result2) 1.549 +{ 1.550 + return result1.Equals(result2); 1.551 +} 1.552 + 1.553 +inline bool 1.554 +xpc_qsSameResult(int32_t result1, int32_t result2) 1.555 +{ 1.556 + return result1 == result2; 1.557 +} 1.558 + 1.559 +#define XPC_QS_ASSERT_CONTEXT_OK(cx) xpc_qsAssertContextOK(cx) 1.560 +#else 1.561 +#define XPC_QS_ASSERT_CONTEXT_OK(cx) ((void) 0) 1.562 +#endif 1.563 + 1.564 +// Apply |op| to |obj|, |id|, and |vp|. If |op| is a setter, treat the assignment as lenient. 1.565 +template<typename Op> 1.566 +inline bool ApplyPropertyOp(JSContext *cx, Op op, JS::HandleObject obj, JS::HandleId id, 1.567 + JS::MutableHandleValue vp); 1.568 + 1.569 +template<> 1.570 +inline bool 1.571 +ApplyPropertyOp<JSPropertyOp>(JSContext *cx, JSPropertyOp op, JS::HandleObject obj, JS::HandleId id, 1.572 + JS::MutableHandleValue vp) 1.573 +{ 1.574 + return op(cx, obj, id, vp); 1.575 +} 1.576 + 1.577 +template<> 1.578 +inline bool 1.579 +ApplyPropertyOp<JSStrictPropertyOp>(JSContext *cx, JSStrictPropertyOp op, JS::HandleObject obj, 1.580 + JS::HandleId id, JS::MutableHandleValue vp) 1.581 +{ 1.582 + return op(cx, obj, id, true, vp); 1.583 +} 1.584 + 1.585 +template<typename Op> 1.586 +bool 1.587 +PropertyOpForwarder(JSContext *cx, unsigned argc, jsval *vp) 1.588 +{ 1.589 + // Layout: 1.590 + // this = our this 1.591 + // property op to call = callee reserved slot 0 1.592 + // name of the property = callee reserved slot 1 1.593 + 1.594 + JS::CallArgs args = CallArgsFromVp(argc, vp); 1.595 + 1.596 + JS::RootedObject callee(cx, &args.callee()); 1.597 + JS::RootedObject obj(cx, JS_THIS_OBJECT(cx, vp)); 1.598 + if (!obj) 1.599 + return false; 1.600 + 1.601 + JS::RootedValue v(cx, js::GetFunctionNativeReserved(callee, 0)); 1.602 + 1.603 + JSObject *ptrobj = JSVAL_TO_OBJECT(v); 1.604 + Op *popp = static_cast<Op *>(JS_GetPrivate(ptrobj)); 1.605 + 1.606 + v = js::GetFunctionNativeReserved(callee, 1); 1.607 + 1.608 + JS::RootedValue argval(cx, args.get(0)); 1.609 + JS::RootedId id(cx); 1.610 + if (!JS_ValueToId(cx, v, &id)) 1.611 + return false; 1.612 + args.rval().set(argval); 1.613 + return ApplyPropertyOp<Op>(cx, *popp, obj, id, args.rval()); 1.614 +} 1.615 + 1.616 +extern const JSClass PointerHolderClass; 1.617 + 1.618 +template<typename Op> 1.619 +JSObject * 1.620 +GeneratePropertyOp(JSContext *cx, JS::HandleObject obj, JS::HandleId id, unsigned argc, Op pop) 1.621 +{ 1.622 + // The JS engine provides two reserved slots on function objects for 1.623 + // XPConnect to use. Use them to stick the necessary info here. 1.624 + JSFunction *fun = 1.625 + js::NewFunctionByIdWithReserved(cx, PropertyOpForwarder<Op>, argc, 0, obj, id); 1.626 + if (!fun) 1.627 + return nullptr; 1.628 + 1.629 + JS::RootedObject funobj(cx, JS_GetFunctionObject(fun)); 1.630 + 1.631 + // Unfortunately, we cannot guarantee that Op is aligned. Use a 1.632 + // second object to work around this. 1.633 + JSObject *ptrobj = JS_NewObject(cx, &PointerHolderClass, JS::NullPtr(), funobj); 1.634 + if (!ptrobj) 1.635 + return nullptr; 1.636 + Op *popp = new Op; 1.637 + if (!popp) 1.638 + return nullptr; 1.639 + *popp = pop; 1.640 + JS_SetPrivate(ptrobj, popp); 1.641 + 1.642 + js::SetFunctionNativeReserved(funobj, 0, OBJECT_TO_JSVAL(ptrobj)); 1.643 + js::SetFunctionNativeReserved(funobj, 1, js::IdToValue(id)); 1.644 + return funobj; 1.645 +} 1.646 + 1.647 +#endif /* xpcquickstubs_h___ */