dom/xbl/nsXBLProtoImplProperty.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/xbl/nsXBLProtoImplProperty.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,367 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#include "nsIAtom.h"
    1.10 +#include "nsString.h"
    1.11 +#include "jsapi.h"
    1.12 +#include "nsIContent.h"
    1.13 +#include "nsXBLProtoImplProperty.h"
    1.14 +#include "nsUnicharUtils.h"
    1.15 +#include "nsCxPusher.h"
    1.16 +#include "nsReadableUtils.h"
    1.17 +#include "nsJSUtils.h"
    1.18 +#include "nsXBLPrototypeBinding.h"
    1.19 +#include "nsXBLSerialize.h"
    1.20 +#include "xpcpublic.h"
    1.21 +
    1.22 +using namespace mozilla;
    1.23 +
    1.24 +nsXBLProtoImplProperty::nsXBLProtoImplProperty(const char16_t* aName,
    1.25 +                                               const char16_t* aGetter, 
    1.26 +                                               const char16_t* aSetter,
    1.27 +                                               const char16_t* aReadOnly,
    1.28 +                                               uint32_t aLineNumber) :
    1.29 +  nsXBLProtoImplMember(aName), 
    1.30 +  mJSAttributes(JSPROP_ENUMERATE)
    1.31 +#ifdef DEBUG
    1.32 +  , mIsCompiled(false)
    1.33 +#endif
    1.34 +{
    1.35 +  MOZ_COUNT_CTOR(nsXBLProtoImplProperty);
    1.36 +
    1.37 +  if (aReadOnly) {
    1.38 +    nsAutoString readOnly; readOnly.Assign(*aReadOnly);
    1.39 +    if (readOnly.LowerCaseEqualsLiteral("true"))
    1.40 +      mJSAttributes |= JSPROP_READONLY;
    1.41 +  }
    1.42 +
    1.43 +  if (aGetter) {
    1.44 +    AppendGetterText(nsDependentString(aGetter));
    1.45 +    SetGetterLineNumber(aLineNumber);
    1.46 +  }
    1.47 +  if (aSetter) {
    1.48 +    AppendSetterText(nsDependentString(aSetter));
    1.49 +    SetSetterLineNumber(aLineNumber);
    1.50 +  }
    1.51 +}
    1.52 +
    1.53 +nsXBLProtoImplProperty::nsXBLProtoImplProperty(const char16_t* aName,
    1.54 +                                               const bool aIsReadOnly)
    1.55 +  : nsXBLProtoImplMember(aName),
    1.56 +    mJSAttributes(JSPROP_ENUMERATE)
    1.57 +#ifdef DEBUG
    1.58 +  , mIsCompiled(false)
    1.59 +#endif
    1.60 +{
    1.61 +  MOZ_COUNT_CTOR(nsXBLProtoImplProperty);
    1.62 +
    1.63 +  if (aIsReadOnly)
    1.64 +    mJSAttributes |= JSPROP_READONLY;
    1.65 +}
    1.66 +
    1.67 +nsXBLProtoImplProperty::~nsXBLProtoImplProperty()
    1.68 +{
    1.69 +  MOZ_COUNT_DTOR(nsXBLProtoImplProperty);
    1.70 +
    1.71 +  if (!mGetter.IsCompiled()) {
    1.72 +    delete mGetter.GetUncompiled();
    1.73 +  }
    1.74 +
    1.75 +  if (!mSetter.IsCompiled()) {
    1.76 +    delete mSetter.GetUncompiled();
    1.77 +  }
    1.78 +}
    1.79 +
    1.80 +void nsXBLProtoImplProperty::EnsureUncompiledText(PropertyOp& aPropertyOp)
    1.81 +{
    1.82 +  if (!aPropertyOp.GetUncompiled()) {
    1.83 +    nsXBLTextWithLineNumber* text = new nsXBLTextWithLineNumber();
    1.84 +    aPropertyOp.SetUncompiled(text);
    1.85 +  }
    1.86 +}
    1.87 +
    1.88 +void 
    1.89 +nsXBLProtoImplProperty::AppendGetterText(const nsAString& aText)
    1.90 +{
    1.91 +  NS_PRECONDITION(!mIsCompiled,
    1.92 +                  "Must not be compiled when accessing getter text");
    1.93 +  EnsureUncompiledText(mGetter);
    1.94 +  mGetter.GetUncompiled()->AppendText(aText);
    1.95 +}
    1.96 +
    1.97 +void 
    1.98 +nsXBLProtoImplProperty::AppendSetterText(const nsAString& aText)
    1.99 +{
   1.100 +  NS_PRECONDITION(!mIsCompiled,
   1.101 +                  "Must not be compiled when accessing setter text");
   1.102 +  EnsureUncompiledText(mSetter);
   1.103 +  mSetter.GetUncompiled()->AppendText(aText);
   1.104 +}
   1.105 +
   1.106 +void
   1.107 +nsXBLProtoImplProperty::SetGetterLineNumber(uint32_t aLineNumber)
   1.108 +{
   1.109 +  NS_PRECONDITION(!mIsCompiled,
   1.110 +                  "Must not be compiled when accessing getter text");
   1.111 +  EnsureUncompiledText(mGetter);
   1.112 +  mGetter.GetUncompiled()->SetLineNumber(aLineNumber);
   1.113 +}
   1.114 +
   1.115 +void
   1.116 +nsXBLProtoImplProperty::SetSetterLineNumber(uint32_t aLineNumber)
   1.117 +{
   1.118 +  NS_PRECONDITION(!mIsCompiled,
   1.119 +                  "Must not be compiled when accessing setter text");
   1.120 +  EnsureUncompiledText(mSetter);
   1.121 +  mSetter.GetUncompiled()->SetLineNumber(aLineNumber);
   1.122 +}
   1.123 +
   1.124 +const char* gPropertyArgs[] = { "val" };
   1.125 +
   1.126 +nsresult
   1.127 +nsXBLProtoImplProperty::InstallMember(JSContext *aCx,
   1.128 +                                      JS::Handle<JSObject*> aTargetClassObject)
   1.129 +{
   1.130 +  NS_PRECONDITION(mIsCompiled,
   1.131 +                  "Should not be installing an uncompiled property");
   1.132 +  MOZ_ASSERT(mGetter.IsCompiled() && mSetter.IsCompiled());
   1.133 +  MOZ_ASSERT(js::IsObjectInContextCompartment(aTargetClassObject, aCx));
   1.134 +  JS::Rooted<JSObject*> globalObject(aCx, JS_GetGlobalForObject(aCx, aTargetClassObject));
   1.135 +  MOZ_ASSERT(xpc::IsInXBLScope(globalObject) ||
   1.136 +             globalObject == xpc::GetXBLScope(aCx, globalObject));
   1.137 +
   1.138 +  JS::Rooted<JSObject*> getter(aCx, mGetter.GetJSFunction());
   1.139 +  JS::Rooted<JSObject*> setter(aCx, mSetter.GetJSFunction());
   1.140 +  if (getter || setter) {
   1.141 +    if (getter) {
   1.142 +      if (!(getter = ::JS_CloneFunctionObject(aCx, getter, globalObject)))
   1.143 +        return NS_ERROR_OUT_OF_MEMORY;
   1.144 +    }
   1.145 +
   1.146 +    if (setter) {
   1.147 +      if (!(setter = ::JS_CloneFunctionObject(aCx, setter, globalObject)))
   1.148 +        return NS_ERROR_OUT_OF_MEMORY;
   1.149 +    }
   1.150 +
   1.151 +    nsDependentString name(mName);
   1.152 +    if (!::JS_DefineUCProperty(aCx, aTargetClassObject,
   1.153 +                               static_cast<const jschar*>(mName),
   1.154 +                               name.Length(), JSVAL_VOID,
   1.155 +                               JS_DATA_TO_FUNC_PTR(JSPropertyOp, getter.get()),
   1.156 +                               JS_DATA_TO_FUNC_PTR(JSStrictPropertyOp, setter.get()),
   1.157 +                               mJSAttributes))
   1.158 +      return NS_ERROR_OUT_OF_MEMORY;
   1.159 +  }
   1.160 +  return NS_OK;
   1.161 +}
   1.162 +
   1.163 +nsresult
   1.164 +nsXBLProtoImplProperty::CompileMember(const nsCString& aClassStr,
   1.165 +                                      JS::Handle<JSObject*> aClassObject)
   1.166 +{
   1.167 +  AssertInCompilationScope();
   1.168 +  NS_PRECONDITION(!mIsCompiled,
   1.169 +                  "Trying to compile an already-compiled property");
   1.170 +  NS_PRECONDITION(aClassObject,
   1.171 +                  "Must have class object to compile");
   1.172 +  MOZ_ASSERT(!mGetter.IsCompiled() && !mSetter.IsCompiled());
   1.173 +
   1.174 +  if (!mName)
   1.175 +    return NS_ERROR_FAILURE; // Without a valid name, we can't install the member.
   1.176 +
   1.177 +  // We have a property.
   1.178 +  nsresult rv = NS_OK;
   1.179 +
   1.180 +  nsAutoCString functionUri;
   1.181 +  if (mGetter.GetUncompiled() || mSetter.GetUncompiled()) {
   1.182 +    functionUri = aClassStr;
   1.183 +    int32_t hash = functionUri.RFindChar('#');
   1.184 +    if (hash != kNotFound) {
   1.185 +      functionUri.Truncate(hash);
   1.186 +    }
   1.187 +  }
   1.188 +
   1.189 +  bool deletedGetter = false;
   1.190 +  nsXBLTextWithLineNumber *getterText = mGetter.GetUncompiled();
   1.191 +  if (getterText && getterText->GetText()) {
   1.192 +    nsDependentString getter(getterText->GetText());
   1.193 +    if (!getter.IsEmpty()) {
   1.194 +      AutoJSContext cx;
   1.195 +      JSAutoCompartment ac(cx, aClassObject);
   1.196 +      JS::CompileOptions options(cx);
   1.197 +      options.setFileAndLine(functionUri.get(), getterText->GetLineNumber())
   1.198 +             .setVersion(JSVERSION_LATEST);
   1.199 +      nsCString name = NS_LITERAL_CSTRING("get_") + NS_ConvertUTF16toUTF8(mName);
   1.200 +      JS::Rooted<JSObject*> getterObject(cx);
   1.201 +      rv = nsJSUtils::CompileFunction(cx, JS::NullPtr(), options, name, 0,
   1.202 +                                      nullptr, getter, getterObject.address());
   1.203 +
   1.204 +      delete getterText;
   1.205 +      deletedGetter = true;
   1.206 +
   1.207 +      mGetter.SetJSFunction(getterObject);
   1.208 +    
   1.209 +      if (mGetter.GetJSFunction() && NS_SUCCEEDED(rv)) {
   1.210 +        mJSAttributes |= JSPROP_GETTER | JSPROP_SHARED;
   1.211 +      }
   1.212 +      if (NS_FAILED(rv)) {
   1.213 +        mGetter.SetJSFunction(nullptr);
   1.214 +        mJSAttributes &= ~JSPROP_GETTER;
   1.215 +        /*chaining to return failure*/
   1.216 +      }
   1.217 +    }
   1.218 +  } // if getter is not empty
   1.219 +
   1.220 +  if (!deletedGetter) {  // Empty getter
   1.221 +    delete getterText;
   1.222 +    mGetter.SetJSFunction(nullptr);
   1.223 +  }
   1.224 +  
   1.225 +  if (NS_FAILED(rv)) {
   1.226 +    // We failed to compile our getter.  So either we've set it to null, or
   1.227 +    // it's still set to the text object.  In either case, it's safe to return
   1.228 +    // the error here, since then we'll be cleaned up as uncompiled and that
   1.229 +    // will be ok.  Going on and compiling the setter and _then_ returning an
   1.230 +    // error, on the other hand, will try to clean up a compiled setter as
   1.231 +    // uncompiled and crash.
   1.232 +    return rv;
   1.233 +  }
   1.234 +
   1.235 +  bool deletedSetter = false;
   1.236 +  nsXBLTextWithLineNumber *setterText = mSetter.GetUncompiled();
   1.237 +  if (setterText && setterText->GetText()) {
   1.238 +    nsDependentString setter(setterText->GetText());
   1.239 +    if (!setter.IsEmpty()) {
   1.240 +      AutoJSContext cx;
   1.241 +      JSAutoCompartment ac(cx, aClassObject);
   1.242 +      JS::CompileOptions options(cx);
   1.243 +      options.setFileAndLine(functionUri.get(), setterText->GetLineNumber())
   1.244 +             .setVersion(JSVERSION_LATEST);
   1.245 +      nsCString name = NS_LITERAL_CSTRING("set_") + NS_ConvertUTF16toUTF8(mName);
   1.246 +      JS::Rooted<JSObject*> setterObject(cx);
   1.247 +      rv = nsJSUtils::CompileFunction(cx, JS::NullPtr(), options, name, 1,
   1.248 +                                      gPropertyArgs, setter,
   1.249 +                                      setterObject.address());
   1.250 +
   1.251 +      delete setterText;
   1.252 +      deletedSetter = true;
   1.253 +      mSetter.SetJSFunction(setterObject);
   1.254 +
   1.255 +      if (mSetter.GetJSFunction() && NS_SUCCEEDED(rv)) {
   1.256 +        mJSAttributes |= JSPROP_SETTER | JSPROP_SHARED;
   1.257 +      }
   1.258 +      if (NS_FAILED(rv)) {
   1.259 +        mSetter.SetJSFunction(nullptr);
   1.260 +        mJSAttributes &= ~JSPROP_SETTER;
   1.261 +        /*chaining to return failure*/
   1.262 +      }
   1.263 +    }
   1.264 +  } // if setter wasn't empty....
   1.265 +
   1.266 +  if (!deletedSetter) {  // Empty setter
   1.267 +    delete setterText;
   1.268 +    mSetter.SetJSFunction(nullptr);
   1.269 +  }
   1.270 +
   1.271 +#ifdef DEBUG
   1.272 +  mIsCompiled = NS_SUCCEEDED(rv);
   1.273 +#endif
   1.274 +
   1.275 +  return rv;
   1.276 +}
   1.277 +
   1.278 +void
   1.279 +nsXBLProtoImplProperty::Trace(const TraceCallbacks& aCallbacks, void *aClosure)
   1.280 +{
   1.281 +  if (mJSAttributes & JSPROP_GETTER) {
   1.282 +    aCallbacks.Trace(&mGetter.AsHeapObject(), "mGetter", aClosure);
   1.283 +  }
   1.284 +
   1.285 +  if (mJSAttributes & JSPROP_SETTER) {
   1.286 +    aCallbacks.Trace(&mSetter.AsHeapObject(), "mSetter", aClosure);
   1.287 +  }
   1.288 +}
   1.289 +
   1.290 +nsresult
   1.291 +nsXBLProtoImplProperty::Read(nsIObjectInputStream* aStream,
   1.292 +                             XBLBindingSerializeDetails aType)
   1.293 +{
   1.294 +  AssertInCompilationScope();
   1.295 +  MOZ_ASSERT(!mIsCompiled);
   1.296 +  MOZ_ASSERT(!mGetter.GetUncompiled() && !mSetter.GetUncompiled());
   1.297 +
   1.298 +  AutoJSContext cx;
   1.299 +  JS::Rooted<JSObject*> getterObject(cx);
   1.300 +  if (aType == XBLBinding_Serialize_GetterProperty ||
   1.301 +      aType == XBLBinding_Serialize_GetterSetterProperty) {
   1.302 +    nsresult rv = XBL_DeserializeFunction(aStream, &getterObject);
   1.303 +    NS_ENSURE_SUCCESS(rv, rv);
   1.304 +
   1.305 +    mJSAttributes |= JSPROP_GETTER | JSPROP_SHARED;
   1.306 +  }
   1.307 +  mGetter.SetJSFunction(getterObject);
   1.308 +  
   1.309 +  JS::Rooted<JSObject*> setterObject(cx);
   1.310 +  if (aType == XBLBinding_Serialize_SetterProperty ||
   1.311 +      aType == XBLBinding_Serialize_GetterSetterProperty) {
   1.312 +    nsresult rv = XBL_DeserializeFunction(aStream, &setterObject);
   1.313 +    NS_ENSURE_SUCCESS(rv, rv);
   1.314 +
   1.315 +    mJSAttributes |= JSPROP_SETTER | JSPROP_SHARED;
   1.316 +  }
   1.317 +  mSetter.SetJSFunction(setterObject);
   1.318 +
   1.319 +#ifdef DEBUG
   1.320 +  mIsCompiled = true;
   1.321 +#endif
   1.322 +
   1.323 +  return NS_OK;
   1.324 +}
   1.325 +
   1.326 +nsresult
   1.327 +nsXBLProtoImplProperty::Write(nsIObjectOutputStream* aStream)
   1.328 +{
   1.329 +  AssertInCompilationScope();
   1.330 +  XBLBindingSerializeDetails type;
   1.331 +
   1.332 +  if (mJSAttributes & JSPROP_GETTER) {
   1.333 +    type = mJSAttributes & JSPROP_SETTER ?
   1.334 +           XBLBinding_Serialize_GetterSetterProperty :
   1.335 +           XBLBinding_Serialize_GetterProperty;
   1.336 +  }
   1.337 +  else {
   1.338 +    type = XBLBinding_Serialize_SetterProperty;
   1.339 +  }
   1.340 +
   1.341 +  if (mJSAttributes & JSPROP_READONLY) {
   1.342 +    type |= XBLBinding_Serialize_ReadOnly;
   1.343 +  }
   1.344 +
   1.345 +  nsresult rv = aStream->Write8(type);
   1.346 +  NS_ENSURE_SUCCESS(rv, rv);
   1.347 +  rv = aStream->WriteWStringZ(mName);
   1.348 +  NS_ENSURE_SUCCESS(rv, rv);
   1.349 +
   1.350 +  // The calls to fromMarkedLocation() below are safe because mSetter and
   1.351 +  // mGetter are traced by the Trace() method above, and because their values
   1.352 +  // are never changed after they have been set to a compiled function.
   1.353 +  MOZ_ASSERT_IF(mJSAttributes & (JSPROP_GETTER | JSPROP_SETTER), mIsCompiled);
   1.354 +
   1.355 +  if (mJSAttributes & JSPROP_GETTER) {
   1.356 +    JS::Handle<JSObject*> function =
   1.357 +      JS::Handle<JSObject*>::fromMarkedLocation(mGetter.AsHeapObject().address());
   1.358 +    rv = XBL_SerializeFunction(aStream, function);
   1.359 +    NS_ENSURE_SUCCESS(rv, rv);
   1.360 +  }
   1.361 +
   1.362 +  if (mJSAttributes & JSPROP_SETTER) {
   1.363 +     JS::Handle<JSObject*> function =
   1.364 +      JS::Handle<JSObject*>::fromMarkedLocation(mSetter.AsHeapObject().address());
   1.365 +    rv = XBL_SerializeFunction(aStream, function);
   1.366 +    NS_ENSURE_SUCCESS(rv, rv);
   1.367 +  }
   1.368 +
   1.369 +  return NS_OK;
   1.370 +}

mercurial