From 15cc9318bee62fa3d4095c83730df3b54447ba79 Mon Sep 17 00:00:00 2001 From: Jon Coppeard Date: Thu, 27 Oct 2016 11:03:53 +0100 Subject: [PATCH] Bug 927318 - Make native functions singletons by default but make promise resolving functions generic objects r=jandem --- js/src/asmjs/WasmJS.cpp | 2 +- js/src/builtin/Promise.cpp | 4 ++-- js/src/jsfun.cpp | 12 ++++-------- js/src/jsfun.h | 10 ++++++---- js/src/vm/GeneratorObject.cpp | 3 ++- 5 files changed, 15 insertions(+), 16 deletions(-) diff --git a/js/src/asmjs/WasmJS.cpp b/js/src/asmjs/WasmJS.cpp index 12c5c2e645f9..427e4a2b06ac 100644 --- a/js/src/asmjs/WasmJS.cpp +++ b/js/src/asmjs/WasmJS.cpp @@ -773,7 +773,7 @@ WasmInstanceObject::getExportedFunction(JSContext* cx, HandleWasmInstanceObject unsigned numArgs = instance.metadata().lookupFuncDefExport(funcDefIndex).sig().args().length(); fun.set(NewNativeConstructor(cx, WasmCall, numArgs, name, gc::AllocKind::FUNCTION_EXTENDED, - GenericObject, JSFunction::ASMJS_CTOR)); + SingletonObject, JSFunction::ASMJS_CTOR)); if (!fun) return false; diff --git a/js/src/builtin/Promise.cpp b/js/src/builtin/Promise.cpp index be5c80cd82ca..cf821ab51487 100644 --- a/js/src/builtin/Promise.cpp +++ b/js/src/builtin/Promise.cpp @@ -368,12 +368,12 @@ CreateResolvingFunctions(JSContext* cx, HandleValue promise, { RootedAtom funName(cx, cx->names().empty); RootedFunction resolve(cx, NewNativeFunction(cx, ResolvePromiseFunction, 1, funName, - gc::AllocKind::FUNCTION_EXTENDED)); + gc::AllocKind::FUNCTION_EXTENDED, GenericObject)); if (!resolve) return false; RootedFunction reject(cx, NewNativeFunction(cx, RejectPromiseFunction, 1, funName, - gc::AllocKind::FUNCTION_EXTENDED)); + gc::AllocKind::FUNCTION_EXTENDED, GenericObject)); if (!reject) return false; diff --git a/js/src/jsfun.cpp b/js/src/jsfun.cpp index 8e098aab6101..62e1d5c02f63 100644 --- a/js/src/jsfun.cpp +++ b/js/src/jsfun.cpp @@ -1933,8 +1933,9 @@ JSFunction::needsNamedLambdaEnvironment() const JSFunction* js::NewNativeFunction(ExclusiveContext* cx, Native native, unsigned nargs, HandleAtom atom, gc::AllocKind allocKind /* = AllocKind::FUNCTION */, - NewObjectKind newKind /* = GenericObject */) + NewObjectKind newKind /* = SingletonObject */) { + MOZ_ASSERT(native); return NewFunctionWithProto(cx, native, nargs, JSFunction::NATIVE_FUN, nullptr, atom, nullptr, allocKind, newKind); } @@ -1942,9 +1943,10 @@ js::NewNativeFunction(ExclusiveContext* cx, Native native, unsigned nargs, Handl JSFunction* js::NewNativeConstructor(ExclusiveContext* cx, Native native, unsigned nargs, HandleAtom atom, gc::AllocKind allocKind /* = AllocKind::FUNCTION */, - NewObjectKind newKind /* = GenericObject */, + NewObjectKind newKind /* = SingletonObject */, JSFunction::Flags flags /* = JSFunction::NATIVE_CTOR */) { + MOZ_ASSERT(native); MOZ_ASSERT(flags & JSFunction::NATIVE_CTOR); return NewFunctionWithProto(cx, native, nargs, flags, nullptr, atom, nullptr, allocKind, newKind); @@ -1992,12 +1994,6 @@ js::NewFunctionWithProto(ExclusiveContext* cx, Native native, MOZ_ASSERT(NewFunctionEnvironmentIsWellFormed(cx, enclosingEnv)); RootedObject funobj(cx); - // Don't mark asm.js module functions as singleton since they are - // cloned (via CloneFunctionObjectIfNotSingleton) which assumes that - // isSingleton implies isInterpreted. - if (native && !IsAsmJSModuleNative(native)) - newKind = SingletonObject; - if (protoHandling == NewFunctionClassProto) { funobj = NewObjectWithClassProto(cx, &JSFunction::class_, proto, allocKind, newKind); diff --git a/js/src/jsfun.h b/js/src/jsfun.h index 6d8a7ea6b72b..d4274f350e2c 100644 --- a/js/src/jsfun.h +++ b/js/src/jsfun.h @@ -601,17 +601,19 @@ Function(JSContext* cx, unsigned argc, Value* vp); extern bool Generator(JSContext* cx, unsigned argc, Value* vp); -// Allocate a new function backed by a JSNative. +// Allocate a new function backed by a JSNative. Note that by default this +// creates a singleton object. extern JSFunction* NewNativeFunction(ExclusiveContext* cx, JSNative native, unsigned nargs, HandleAtom atom, gc::AllocKind allocKind = gc::AllocKind::FUNCTION, - NewObjectKind newKind = GenericObject); + NewObjectKind newKind = SingletonObject); -// Allocate a new constructor backed by a JSNative. +// Allocate a new constructor backed by a JSNative. Note that by default this +// creates a singleton object. extern JSFunction* NewNativeConstructor(ExclusiveContext* cx, JSNative native, unsigned nargs, HandleAtom atom, gc::AllocKind allocKind = gc::AllocKind::FUNCTION, - NewObjectKind newKind = GenericObject, + NewObjectKind newKind = SingletonObject, JSFunction::Flags flags = JSFunction::NATIVE_CTOR); // Allocate a new scripted function. If enclosingEnv is null, the diff --git a/js/src/vm/GeneratorObject.cpp b/js/src/vm/GeneratorObject.cpp index 85e04c7a1ab3..c808fe5dd1fc 100644 --- a/js/src/vm/GeneratorObject.cpp +++ b/js/src/vm/GeneratorObject.cpp @@ -322,7 +322,8 @@ GlobalObject::initStarGenerators(JSContext* cx, Handle global) RootedAtom name(cx, cx->names().GeneratorFunction); RootedObject genFunction(cx, NewFunctionWithProto(cx, Generator, 1, JSFunction::NATIVE_CTOR, nullptr, name, - proto)); + proto, gc::AllocKind::FUNCTION, + SingletonObject)); if (!genFunction) return false; if (!LinkConstructorAndPrototype(cx, genFunction, genFunctionProto))