Bug 1554223. Add a version of JS::NewFunctionFromSpec that does not require passing an id. r=jandem

Differential Revision: https://phabricator.services.mozilla.com/D32527

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Boris Zbarsky 2019-05-27 13:26:04 +00:00
Родитель 4bbb03b93b
Коммит 6b623aa87a
3 изменённых файлов: 31 добавлений и 17 удалений

Просмотреть файл

@ -311,14 +311,7 @@ static const JSFunctionSpec register_spec = JS_FNSPEC(
void U2F::GetRegister(JSContext* aCx,
JS::MutableHandle<JSObject*> aRegisterFunc,
ErrorResult& aRv) {
JS::Rooted<JSString*> str(aCx, JS_AtomizeAndPinString(aCx, "register"));
if (!str) {
aRv.NoteJSContextException(aCx);
return;
}
JS::Rooted<jsid> id(aCx, INTERNED_STRING_TO_JSID(aCx, str));
JSFunction* fun = JS::NewFunctionFromSpec(aCx, &register_spec, id);
JSFunction* fun = JS::NewFunctionFromSpec(aCx, &register_spec);
if (!fun) {
aRv.NoteJSContextException(aCx);
return;
@ -499,14 +492,7 @@ static const JSFunctionSpec sign_spec =
void U2F::GetSign(JSContext* aCx, JS::MutableHandle<JSObject*> aSignFunc,
ErrorResult& aRv) {
JS::Rooted<JSString*> str(aCx, JS_AtomizeAndPinString(aCx, "sign"));
if (!str) {
aRv.NoteJSContextException(aCx);
return;
}
JS::Rooted<jsid> id(aCx, INTERNED_STRING_TO_JSID(aCx, str));
JSFunction* fun = JS::NewFunctionFromSpec(aCx, &sign_spec, id);
JSFunction* fun = JS::NewFunctionFromSpec(aCx, &sign_spec);
if (!fun) {
aRv.NoteJSContextException(aCx);
return;

Просмотреть файл

@ -3190,6 +3190,16 @@ JS_PUBLIC_API JSFunction* JS::NewFunctionFromSpec(JSContext* cx,
HandleId id) {
cx->check(id);
#ifdef DEBUG
if (fs->name.isSymbol()) {
MOZ_ASSERT(SYMBOL_TO_JSID(cx->wellKnownSymbols().get(fs->name.symbol())) ==
id);
} else {
MOZ_ASSERT(JSID_IS_STRING(id) &&
StringEqualsAscii(JSID_TO_FLAT_STRING(id), fs->name.string()));
}
#endif
// Delay cloning self-hosted functions until they are called. This is
// achieved by passing DefineFunction a nullptr JSNative which produces an
// interpreted JSFunction where !hasScript. Interpreted call paths then
@ -3240,6 +3250,16 @@ JS_PUBLIC_API JSFunction* JS::NewFunctionFromSpec(JSContext* cx,
return fun;
}
JS_PUBLIC_API JSFunction* JS::NewFunctionFromSpec(JSContext* cx,
const JSFunctionSpec* fs) {
RootedId id(cx);
if (!PropertySpecNameToId(cx, fs->name, &id)) {
return nullptr;
}
return NewFunctionFromSpec(cx, fs, id);
}
static bool IsFunctionCloneable(HandleFunction fun) {
// If a function was compiled with non-global syntactic environments on
// the environment chain, we could have baked in EnvironmentCoordinates

Просмотреть файл

@ -1793,7 +1793,8 @@ extern JS_PUBLIC_API JSFunction* GetSelfHostedFunction(
/**
* Create a new function based on the given JSFunctionSpec, *fs.
* id is the result of a successful call to
* `PropertySpecNameToPermanentId(cx, fs->name, &id)`.
* `PropertySpecNameToId(cx, fs->name, &id)` or
`PropertySpecNameToPermanentId(cx, fs->name, &id)`.
*
* Unlike JS_DefineFunctions, this does not treat fs as an array.
* *fs must not be JS_FS_END.
@ -1802,6 +1803,13 @@ extern JS_PUBLIC_API JSFunction* NewFunctionFromSpec(JSContext* cx,
const JSFunctionSpec* fs,
HandleId id);
/**
* Same as above, but without an id arg, for callers who don't have
* the id already.
*/
extern JS_PUBLIC_API JSFunction* NewFunctionFromSpec(JSContext* cx,
const JSFunctionSpec* fs);
} /* namespace JS */
extern JS_PUBLIC_API JSObject* JS_GetFunctionObject(JSFunction* fun);