nsIXPCScriptable flags handling in xpc_map_end.h is a bit of a mess. - Half the flags relate to whether various functions are defined (PreCreate, GetProperty, etc). These are set using the XPC_MAP_WANT_* macros; for each one xpc_map_end.h inserts the corresponding flag using the preprocessor (see XPC_MAP_CLASSNAME::GetScriptableFlags()). - The other half of the flags relate to other things (IS_GLOBAL_OBJECT, DONT_REFLECT_INTERFACE_NAMES, etc). These are set using the XPC_MAP_FLAGS macro. Having two similar but different mechanisms to set the flags for a class is confusing. (Indeed, until recently we had some classes where a single flag was redundantly specified via both mechanisms.) Note also that the classes done in dom/base/nsIDOMClassInfo.h also specify all the flags in a single value, similar to how XPC_MAP_FLAGS works. This patch removes the XPC_MAP_WANT_* macros. All flags are now set via XPC_MAP_FLAGS. This is a significant simplification to xpc_map_end.h and all the places that use it. The downside of this change is that I had to change the flag constants from class constants (i.e. nsIXPCScriptable::FOO) to macros (i.e. NSIXPCSCRIPTABLE_FOO) because they need to be used in #if statements like this in xpc_map_end.h: #if !((XPC_MAP_FLAGS) & NSIXPCSCRIPTABLE_WANT_PRECREATE) and you can't use a '::'-qualified name inside a #if. I think this downside is outweighed by the simplification described above. Overall the patch removes 80 lines of code.
116 lines
3.1 KiB
C++
116 lines
3.1 KiB
C++
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*- */
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
#include "PerfMeasurement.h"
|
|
#include "jsperf.h"
|
|
#include "mozilla/ModuleUtils.h"
|
|
#include "nsMemory.h"
|
|
#include "mozilla/Preferences.h"
|
|
#include "mozJSComponentLoader.h"
|
|
#include "nsZipArchive.h"
|
|
#include "xpc_make_class.h"
|
|
|
|
#define JSPERF_CONTRACTID \
|
|
"@mozilla.org/jsperf;1"
|
|
|
|
#define JSPERF_CID \
|
|
{ 0x421c38e6, 0xaee0, 0x4509, \
|
|
{ 0xa0, 0x25, 0x13, 0x0f, 0x43, 0x78, 0x03, 0x5a } }
|
|
|
|
namespace mozilla {
|
|
namespace jsperf {
|
|
|
|
NS_GENERIC_FACTORY_CONSTRUCTOR(Module)
|
|
|
|
NS_IMPL_ISUPPORTS(Module, nsIXPCScriptable)
|
|
|
|
Module::Module() = default;
|
|
|
|
Module::~Module() = default;
|
|
|
|
#define XPC_MAP_CLASSNAME Module
|
|
#define XPC_MAP_QUOTED_CLASSNAME "Module"
|
|
#define XPC_MAP_FLAGS XPC_SCRIPTABLE_WANT_CALL
|
|
#include "xpc_map_end.h"
|
|
|
|
static bool
|
|
SealObjectAndPrototype(JSContext* cx, JS::Handle<JSObject *> parent, const char* name)
|
|
{
|
|
JS::Rooted<JS::Value> prop(cx);
|
|
if (!JS_GetProperty(cx, parent, name, &prop))
|
|
return false;
|
|
|
|
if (prop.isUndefined()) {
|
|
// Pretend we sealed the object.
|
|
return true;
|
|
}
|
|
|
|
JS::Rooted<JSObject*> obj(cx, prop.toObjectOrNull());
|
|
if (!JS_GetProperty(cx, obj, "prototype", &prop))
|
|
return false;
|
|
|
|
JS::Rooted<JSObject*> prototype(cx, prop.toObjectOrNull());
|
|
return JS_FreezeObject(cx, obj) && JS_FreezeObject(cx, prototype);
|
|
}
|
|
|
|
static bool
|
|
InitAndSealPerfMeasurementClass(JSContext* cx, JS::Handle<JSObject*> global)
|
|
{
|
|
// Init the PerfMeasurement class
|
|
if (!JS::RegisterPerfMeasurement(cx, global))
|
|
return false;
|
|
|
|
// Seal up Object, Function, and Array and their prototypes. (This single
|
|
// object instance is shared amongst everyone who imports the jsperf module.)
|
|
if (!SealObjectAndPrototype(cx, global, "Object") ||
|
|
!SealObjectAndPrototype(cx, global, "Function") ||
|
|
!SealObjectAndPrototype(cx, global, "Array"))
|
|
return false;
|
|
|
|
// Finally, seal the global object, for good measure. (But not recursively;
|
|
// this breaks things.)
|
|
return JS_FreezeObject(cx, global);
|
|
}
|
|
|
|
NS_IMETHODIMP
|
|
Module::Call(nsIXPConnectWrappedNative* wrapper,
|
|
JSContext* cx,
|
|
JSObject* obj,
|
|
const JS::CallArgs& args,
|
|
bool* _retval)
|
|
{
|
|
|
|
mozJSComponentLoader* loader = mozJSComponentLoader::Get();
|
|
JS::Rooted<JSObject*> targetObj(cx);
|
|
nsresult rv = loader->FindTargetObject(cx, &targetObj);
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
*_retval = InitAndSealPerfMeasurementClass(cx, targetObj);
|
|
return NS_OK;
|
|
}
|
|
|
|
} // namespace jsperf
|
|
} // namespace mozilla
|
|
|
|
NS_DEFINE_NAMED_CID(JSPERF_CID);
|
|
|
|
static const mozilla::Module::CIDEntry kPerfCIDs[] = {
|
|
{ &kJSPERF_CID, false, nullptr, mozilla::jsperf::ModuleConstructor },
|
|
{ nullptr }
|
|
};
|
|
|
|
static const mozilla::Module::ContractIDEntry kPerfContracts[] = {
|
|
{ JSPERF_CONTRACTID, &kJSPERF_CID },
|
|
{ nullptr }
|
|
};
|
|
|
|
static const mozilla::Module kPerfModule = {
|
|
mozilla::Module::kVersion,
|
|
kPerfCIDs,
|
|
kPerfContracts
|
|
};
|
|
|
|
NSMODULE_DEFN(jsperf) = &kPerfModule;
|