Skip to content

Commit

Permalink
Bug 959787 - Handlify has property APIs r=terrence r=bz
Browse files Browse the repository at this point in the history
  • Loading branch information
jonco3 committed Jan 22, 2014
1 parent 230c374 commit c862a30
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 47 deletions.
30 changes: 30 additions & 0 deletions dom/bindings/BindingUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -2350,6 +2350,36 @@ CreateGlobal(JSContext* aCx, T* aObject, nsWrapperCache* aCache,
return global;
}

/*
* Holds a jsid that is initialized to an interned string, with conversion to
* Handle<jsid>.
*/
class InternedStringId
{
jsid id;

public:
InternedStringId() : id(JSID_VOID) {}

bool init(JSContext *cx, const char *string) {
MOZ_ASSERT(id == JSID_VOID);
JSString* str = JS_InternString(cx, string);
if (!str)
return false;
id = INTERNED_STRING_TO_JSID(cx, str);
return true;
}

operator const jsid& () {
return id;
}

operator JS::Handle<jsid> () {
/* This is safe because we have interned the string. */
return JS::Handle<jsid>::fromMarkedLocation(&id);
}
};

} // namespace dom
} // namespace mozilla

Expand Down
3 changes: 2 additions & 1 deletion dom/bindings/DOMJSProxyHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ DOMProxyShadows(JSContext* cx, JS::Handle<JSObject*> proxy, JS::Handle<jsid> id)
JS::Value v = js::GetProxyExtra(proxy, JSPROXYSLOT_EXPANDO);
if (v.isObject()) {
bool hasOwn;
if (!JS_AlreadyHasOwnPropertyById(cx, &v.toObject(), id, &hasOwn))
Rooted<JSObject*> object(cx, &v.toObject());
if (!JS_AlreadyHasOwnPropertyById(cx, object, id, &hasOwn))
return js::ShadowCheckFailed;

return hasOwn ? js::Shadows : js::DoesntShadow;
Expand Down
10 changes: 6 additions & 4 deletions dom/plugins/base/nsJSNPRuntime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,7 @@ nsJSObjWrapper::NP_InvokeDefault(NPObject *npobj, const NPVariant *args,

// static
bool
nsJSObjWrapper::NP_HasProperty(NPObject *npobj, NPIdentifier id)
nsJSObjWrapper::NP_HasProperty(NPObject *npobj, NPIdentifier npid)
{
NPP npp = NPPStack::Peek();
JSContext *cx = GetJSContext(npp);
Expand All @@ -733,11 +733,13 @@ nsJSObjWrapper::NP_HasProperty(NPObject *npobj, NPIdentifier id)
nsCxPusher pusher;
pusher.Push(cx);
AutoJSExceptionReporter reporter(cx);
JSAutoCompartment ac(cx, npjsobj->mJSObj);
JS::Rooted<JSObject*> jsobj(cx, npjsobj->mJSObj);
JSAutoCompartment ac(cx, jsobj);

NS_ASSERTION(NPIdentifierIsInt(id) || NPIdentifierIsString(id),
NS_ASSERTION(NPIdentifierIsInt(npid) || NPIdentifierIsString(npid),
"id must be either string or int!\n");
ok = ::JS_HasPropertyById(cx, npjsobj->mJSObj, NPIdentifierToJSId(id), &found);
JS::Rooted<jsid> id(cx, NPIdentifierToJSId(npid));
ok = ::JS_HasPropertyById(cx, jsobj, id, &found);
return ok && found;
}

Expand Down
46 changes: 24 additions & 22 deletions js/src/jsapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2814,10 +2814,8 @@ JS_LookupPropertyWithFlags(JSContext *cx, HandleObject obj, const char *name, un
}

JS_PUBLIC_API(bool)
JS_HasPropertyById(JSContext *cx, JSObject *objArg, jsid idArg, bool *foundp)
JS_HasPropertyById(JSContext *cx, HandleObject obj, HandleId id, bool *foundp)
{
RootedObject obj(cx, objArg);
RootedId id(cx, idArg);
RootedObject obj2(cx);
RootedShape prop(cx);
bool ok = LookupPropertyById(cx, obj, id, 0, &obj2, &prop);
Expand All @@ -2826,9 +2824,8 @@ JS_HasPropertyById(JSContext *cx, JSObject *objArg, jsid idArg, bool *foundp)
}

JS_PUBLIC_API(bool)
JS_HasElement(JSContext *cx, JSObject *objArg, uint32_t index, bool *foundp)
JS_HasElement(JSContext *cx, HandleObject obj, uint32_t index, bool *foundp)
{
RootedObject obj(cx, objArg);
AssertHeapIsIdle(cx);
CHECK_REQUEST(cx);
RootedId id(cx);
Expand All @@ -2838,26 +2835,28 @@ JS_HasElement(JSContext *cx, JSObject *objArg, uint32_t index, bool *foundp)
}

JS_PUBLIC_API(bool)
JS_HasProperty(JSContext *cx, JSObject *objArg, const char *name, bool *foundp)
JS_HasProperty(JSContext *cx, HandleObject obj, const char *name, bool *foundp)
{
RootedObject obj(cx, objArg);
JSAtom *atom = Atomize(cx, name, strlen(name));
return atom && JS_HasPropertyById(cx, obj, AtomToId(atom), foundp);
if (!atom)
return false;
RootedId id(cx, AtomToId(atom));
return JS_HasPropertyById(cx, obj, id, foundp);
}

JS_PUBLIC_API(bool)
JS_HasUCProperty(JSContext *cx, JSObject *objArg, const jschar *name, size_t namelen, bool *foundp)
JS_HasUCProperty(JSContext *cx, HandleObject obj, const jschar *name, size_t namelen, bool *foundp)
{
RootedObject obj(cx, objArg);
JSAtom *atom = AtomizeChars(cx, name, AUTO_NAMELEN(name, namelen));
return atom && JS_HasPropertyById(cx, obj, AtomToId(atom), foundp);
if (!atom)
return false;
RootedId id(cx, AtomToId(atom));
return JS_HasPropertyById(cx, obj, id, foundp);
}

JS_PUBLIC_API(bool)
JS_AlreadyHasOwnPropertyById(JSContext *cx, JSObject *objArg, jsid id_, bool *foundp)
JS_AlreadyHasOwnPropertyById(JSContext *cx, HandleObject obj, HandleId id, bool *foundp)
{
RootedObject obj(cx, objArg);
RootedId id(cx, id_);
AssertHeapIsIdle(cx);
CHECK_REQUEST(cx);
assertSameCompartment(cx, obj, id);
Expand All @@ -2882,9 +2881,8 @@ JS_AlreadyHasOwnPropertyById(JSContext *cx, JSObject *objArg, jsid id_, bool *fo
}

JS_PUBLIC_API(bool)
JS_AlreadyHasOwnElement(JSContext *cx, JSObject *objArg, uint32_t index, bool *foundp)
JS_AlreadyHasOwnElement(JSContext *cx, HandleObject obj, uint32_t index, bool *foundp)
{
RootedObject obj(cx, objArg);
AssertHeapIsIdle(cx);
CHECK_REQUEST(cx);
RootedId id(cx);
Expand All @@ -2894,20 +2892,24 @@ JS_AlreadyHasOwnElement(JSContext *cx, JSObject *objArg, uint32_t index, bool *f
}

JS_PUBLIC_API(bool)
JS_AlreadyHasOwnProperty(JSContext *cx, JSObject *objArg, const char *name, bool *foundp)
JS_AlreadyHasOwnProperty(JSContext *cx, HandleObject obj, const char *name, bool *foundp)
{
RootedObject obj(cx, objArg);
JSAtom *atom = Atomize(cx, name, strlen(name));
return atom && JS_AlreadyHasOwnPropertyById(cx, obj, AtomToId(atom), foundp);
if (!atom)
return false;
RootedId id(cx, AtomToId(atom));
return JS_AlreadyHasOwnPropertyById(cx, obj, id, foundp);
}

JS_PUBLIC_API(bool)
JS_AlreadyHasOwnUCProperty(JSContext *cx, JSObject *objArg, const jschar *name, size_t namelen,
JS_AlreadyHasOwnUCProperty(JSContext *cx, HandleObject obj, const jschar *name, size_t namelen,
bool *foundp)
{
RootedObject obj(cx, objArg);
JSAtom *atom = AtomizeChars(cx, name, AUTO_NAMELEN(name, namelen));
return atom && JS_AlreadyHasOwnPropertyById(cx, obj, AtomToId(atom), foundp);
if (!atom)
return false;
RootedId id(cx, AtomToId(atom));
return JS_AlreadyHasOwnPropertyById(cx, obj, id, foundp);
}

/* Wrapper functions to create wrappers with no corresponding JSJitInfo from API
Expand Down
16 changes: 8 additions & 8 deletions js/src/jsapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -2787,18 +2787,18 @@ JS_DefinePropertyWithTinyId(JSContext *cx, JSObject *obj, const char *name,
unsigned attrs);

extern JS_PUBLIC_API(bool)
JS_AlreadyHasOwnProperty(JSContext *cx, JSObject *obj, const char *name,
JS_AlreadyHasOwnProperty(JSContext *cx, JS::HandleObject obj, const char *name,
bool *foundp);

extern JS_PUBLIC_API(bool)
JS_AlreadyHasOwnPropertyById(JSContext *cx, JSObject *obj, jsid id,
JS_AlreadyHasOwnPropertyById(JSContext *cx, JS::HandleObject obj, JS::HandleId id,
bool *foundp);

extern JS_PUBLIC_API(bool)
JS_HasProperty(JSContext *cx, JSObject *obj, const char *name, bool *foundp);
JS_HasProperty(JSContext *cx, JS::HandleObject obj, const char *name, bool *foundp);

extern JS_PUBLIC_API(bool)
JS_HasPropertyById(JSContext *cx, JSObject *obj, jsid id, bool *foundp);
JS_HasPropertyById(JSContext *cx, JS::HandleObject obj, JS::HandleId id, bool *foundp);

extern JS_PUBLIC_API(bool)
JS_LookupProperty(JSContext *cx, JSObject *obj, const char *name, JS::MutableHandleValue vp);
Expand Down Expand Up @@ -3026,11 +3026,11 @@ JS_DefineUCPropertyWithTinyId(JSContext *cx, JSObject *obj,
unsigned attrs);

extern JS_PUBLIC_API(bool)
JS_AlreadyHasOwnUCProperty(JSContext *cx, JSObject *obj, const jschar *name,
JS_AlreadyHasOwnUCProperty(JSContext *cx, JS::HandleObject obj, const jschar *name,
size_t namelen, bool *foundp);

extern JS_PUBLIC_API(bool)
JS_HasUCProperty(JSContext *cx, JSObject *obj,
JS_HasUCProperty(JSContext *cx, JS::HandleObject obj,
const jschar *name, size_t namelen,
bool *vp);

Expand Down Expand Up @@ -3070,10 +3070,10 @@ JS_DefineElement(JSContext *cx, JSObject *obj, uint32_t index, jsval value,
JSPropertyOp getter, JSStrictPropertyOp setter, unsigned attrs);

extern JS_PUBLIC_API(bool)
JS_AlreadyHasOwnElement(JSContext *cx, JSObject *obj, uint32_t index, bool *foundp);
JS_AlreadyHasOwnElement(JSContext *cx, JS::HandleObject obj, uint32_t index, bool *foundp);

extern JS_PUBLIC_API(bool)
JS_HasElement(JSContext *cx, JSObject *obj, uint32_t index, bool *foundp);
JS_HasElement(JSContext *cx, JS::HandleObject obj, uint32_t index, bool *foundp);

extern JS_PUBLIC_API(bool)
JS_LookupElement(JSContext *cx, JSObject *obj, uint32_t index, JS::MutableHandleValue vp);
Expand Down
17 changes: 5 additions & 12 deletions js/xpconnect/src/dictionary_helper_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,27 +193,20 @@ def print_cpp_file(fd, conf):
if not c in conf.exclude_automatic_type_include:
fd.write("#include \"%s.h\"\n" % c)

fd.write("\nusing namespace mozilla::idl;\n\n")
fd.write("\n"
"using namespace mozilla::idl;\n"
"using namespace mozilla::dom;\n\n")

for a in attrnames:
fd.write("static jsid %s = JSID_VOID;\n"% get_jsid(a))
fd.write("static InternedStringId %s;\n" % get_jsid(a))

fd.write("\n"
"static bool\n"
"InternStaticJSVal(JSContext* aCx, jsid &id, const char* aString)\n"
"{\n"
" if (JSString* str = JS_InternString(aCx, aString)) {\n"
" id = INTERNED_STRING_TO_JSID(aCx, str);\n"
" return true;\n"
" }\n"
" return false;\n"
"}\n\n"
"bool\n"
"InternStaticDictionaryJSVals(JSContext* aCx)\n"
"{\n"
" return\n")
for a in attrnames:
fd.write(" InternStaticJSVal(aCx, %s, \"%s\") &&\n"
fd.write(" %s.init(aCx, \"%s\") &&\n"
% (get_jsid(a), a))

fd.write(" true;\n")
Expand Down

0 comments on commit c862a30

Please sign in to comment.