Bug 1663365 - Various stylistic cleanups to ctypes-related functionality in jsfriendapi.h, in advance of a move into a separate header. r=jandem

Differential Revision: https://phabricator.services.mozilla.com/D95670
This commit is contained in:
Jeff Walden 2020-11-07 00:04:18 +00:00
Родитель a6e8f36d35
Коммит b0435c3949
14 изменённых файлов: 101 добавлений и 67 удалений

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

@ -52,14 +52,14 @@ bool DefineChromeWorkerFunctions(JSContext* aCx,
#ifdef BUILD_CTYPES
{
JS::Rooted<JS::Value> ctypes(aCx);
if (!JS_InitCTypesClass(aCx, aGlobal) ||
if (!JS::InitCTypesClass(aCx, aGlobal) ||
!JS_GetProperty(aCx, aGlobal, "ctypes", &ctypes)) {
return false;
}
static const JSCTypesCallbacks callbacks = {UnicodeToNative};
static const JS::CTypesCallbacks callbacks = {UnicodeToNative};
JS_SetCTypesCallbacks(ctypes.toObjectOrNull(), &callbacks);
JS::SetCTypesCallbacks(ctypes.toObjectOrNull(), &callbacks);
}
#endif // BUILD_CTYPES

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

@ -569,24 +569,24 @@ bool ContentSecurityPolicyAllows(JSContext* aCx, JS::HandleString aCode) {
return worker->IsEvalAllowed();
}
void CTypesActivityCallback(JSContext* aCx, js::CTypesActivityType aType) {
void CTypesActivityCallback(JSContext* aCx, JS::CTypesActivityType aType) {
WorkerPrivate* worker = GetWorkerPrivateFromContext(aCx);
worker->AssertIsOnWorkerThread();
switch (aType) {
case js::CTYPES_CALL_BEGIN:
case JS::CTypesActivityType::BeginCall:
worker->BeginCTypesCall();
break;
case js::CTYPES_CALL_END:
case JS::CTypesActivityType::EndCall:
worker->EndCTypesCall();
break;
case js::CTYPES_CALLBACK_BEGIN:
case JS::CTypesActivityType::BeginCallback:
worker->BeginCTypesCallback();
break;
case js::CTYPES_CALLBACK_END:
case JS::CTypesActivityType::EndCallback:
worker->EndCTypesCallback();
break;
@ -732,7 +732,7 @@ bool InitJSContextForWorker(WorkerPrivate* aWorkerPrivate,
JS_AddInterruptCallback(aWorkerCx, InterruptCallback);
js::SetCTypesActivityCallback(aWorkerCx, CTypesActivityCallback);
JS::SetCTypesActivityCallback(aWorkerCx, CTypesActivityCallback);
#ifdef JS_GC_ZEAL
JS_SetGCZeal(aWorkerCx, settings.gcZeal, settings.gcZealFrequency);

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

@ -67,7 +67,9 @@ using mozilla::IsAsciiAlpha;
using mozilla::IsAsciiDigit;
using JS::AutoCheckCannotGC;
using JS::AutoCTypesActivityCallback;
using JS::AutoStableStringChars;
using JS::CTypesActivityType;
namespace js::ctypes {
@ -441,7 +443,7 @@ static bool Join(JSContext* cx, unsigned argc, Value* vp);
*******************************************************************************/
// Class representing the 'ctypes' object itself. This exists to contain the
// JSCTypesCallbacks set of function pointers.
// JS::CTypesCallbacks set of function pointers.
static const JSClass sCTypesGlobalClass = {
"ctypes", JSCLASS_HAS_RESERVED_SLOTS(CTYPESGLOBAL_SLOTS)};
@ -2343,8 +2345,8 @@ bool IsCTypesGlobal(HandleValue v) {
return v.isObject() && IsCTypesGlobal(&v.toObject());
}
// Get the JSCTypesCallbacks struct from the 'ctypes' object 'obj'.
const JSCTypesCallbacks* GetCallbacks(JSObject* obj) {
// Get the JS::CTypesCallbacks struct from the 'ctypes' object 'obj'.
const JS::CTypesCallbacks* GetCallbacks(JSObject* obj) {
MOZ_ASSERT(IsCTypesGlobal(obj));
Value result = JS::GetReservedSlot(obj, SLOT_CALLBACKS);
@ -2352,7 +2354,7 @@ const JSCTypesCallbacks* GetCallbacks(JSObject* obj) {
return nullptr;
}
return static_cast<const JSCTypesCallbacks*>(result.toPrivate());
return static_cast<const JS::CTypesCallbacks*>(result.toPrivate());
}
// Utility function to access a property of an object as an object
@ -2380,7 +2382,8 @@ static bool GetObjectProperty(JSContext* cx, HandleObject obj,
using namespace js;
using namespace js::ctypes;
JS_PUBLIC_API bool JS_InitCTypesClass(JSContext* cx, HandleObject global) {
JS_PUBLIC_API bool JS::InitCTypesClass(JSContext* cx,
Handle<JSObject*> global) {
// attach ctypes property to global object
RootedObject ctypes(cx, JS_NewObject(cx, &sCTypesGlobalClass));
if (!ctypes) {
@ -2436,20 +2439,22 @@ JS_PUBLIC_API bool JS_InitCTypesClass(JSContext* cx, HandleObject global) {
return JS_FreezeObject(cx, ctypes);
}
JS_PUBLIC_API void JS_SetCTypesCallbacks(JSObject* ctypesObj,
const JSCTypesCallbacks* callbacks) {
JS_PUBLIC_API void JS::SetCTypesCallbacks(JSObject* ctypesObj,
const CTypesCallbacks* callbacks) {
MOZ_ASSERT(callbacks);
MOZ_ASSERT(IsCTypesGlobal(ctypesObj));
// Set the callbacks on a reserved slot.
JS_SetReservedSlot(ctypesObj, SLOT_CALLBACKS,
PrivateValue(const_cast<JSCTypesCallbacks*>(callbacks)));
PrivateValue(const_cast<CTypesCallbacks*>(callbacks)));
}
namespace js {
JS_FRIEND_API size_t SizeOfDataIfCDataObject(mozilla::MallocSizeOf mallocSizeOf,
JSObject* obj) {
namespace ctypes {
size_t SizeOfDataIfCDataObject(mozilla::MallocSizeOf mallocSizeOf,
JSObject* obj) {
if (!CData::IsCData(obj)) {
return 0;
}
@ -2470,8 +2475,6 @@ JS_FRIEND_API size_t SizeOfDataIfCDataObject(mozilla::MallocSizeOf mallocSizeOf,
return n;
}
namespace ctypes {
/*******************************************************************************
** Type conversion functions
*******************************************************************************/
@ -7088,8 +7091,8 @@ bool FunctionType::Call(JSContext* cx, unsigned argc, Value* vp) {
}
// Let the runtime callback know that we are about to call into C.
js::AutoCTypesActivityCallback autoCallback(cx, js::CTYPES_CALL_BEGIN,
js::CTYPES_CALL_END);
AutoCTypesActivityCallback autoCallback(cx, CTypesActivityType::BeginCall,
CTypesActivityType::EndCall);
uintptr_t fn = *reinterpret_cast<uintptr_t*>(CData::GetData(obj));
@ -7378,8 +7381,8 @@ void CClosure::ClosureStub(ffi_cif* cif, void* result, void** args,
bool CClosure::ArgClosure::operator()(JSContext* cx) {
// Let the runtime callback know that we are about to call into JS again. The
// end callback will fire automatically when we exit this function.
js::AutoCTypesActivityCallback autoCallback(cx, js::CTYPES_CALLBACK_BEGIN,
js::CTYPES_CALLBACK_END);
AutoCTypesActivityCallback autoCallback(cx, CTypesActivityType::BeginCallback,
CTypesActivityType::EndCallback);
RootedObject typeObj(cx, cinfo->typeObj);
RootedObject thisObj(cx, cinfo->thisObj);

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

@ -388,14 +388,14 @@ struct ClosureInfo {
bool IsCTypesGlobal(HandleValue v);
bool IsCTypesGlobal(JSObject* obj);
const JSCTypesCallbacks* GetCallbacks(JSObject* obj);
const JS::CTypesCallbacks* GetCallbacks(JSObject* obj);
/*******************************************************************************
** JSClass reserved slot definitions
*******************************************************************************/
enum CTypesGlobalSlot {
SLOT_CALLBACKS = 0, // pointer to JSCTypesCallbacks struct
SLOT_CALLBACKS = 0, // pointer to JS::CTypesCallbacks struct
SLOT_ERRNO = 1, // Value for latest |errno|
SLOT_LASTERROR =
2, // Value for latest |GetLastError|, used only with Windows
@ -512,7 +512,7 @@ ffi_type* GetFFIType(JSContext* cx, JSObject* obj);
JSString* GetName(JSContext* cx, HandleObject obj);
JSObject* GetProtoFromCtor(JSObject* obj, CTypeProtoSlot slot);
JSObject* GetProtoFromType(JSContext* cx, JSObject* obj, CTypeProtoSlot slot);
const JSCTypesCallbacks* GetCallbacksFromType(JSObject* obj);
const JS::CTypesCallbacks* GetCallbacksFromType(JSObject* obj);
} // namespace CType
namespace PointerType {

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

@ -97,7 +97,7 @@ bool Library::Name(JSContext* cx, unsigned argc, Value* vp) {
}
JSObject* Library::Create(JSContext* cx, HandleValue path,
const JSCTypesCallbacks* callbacks) {
const JS::CTypesCallbacks* callbacks) {
RootedObject libraryObj(cx, JS_NewObject(cx, &sLibraryClass));
if (!libraryObj) {
return nullptr;

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

@ -11,7 +11,10 @@
#include "js/TypeDecls.h"
struct JSCTypesCallbacks;
namespace JS {
struct CTypesCallbacks;
} // namespace JS
struct PRLibrary;
namespace js {
@ -23,7 +26,7 @@ namespace Library {
MOZ_MUST_USE bool Name(JSContext* cx, unsigned argc, JS::Value* vp);
JSObject* Create(JSContext* cx, JS::HandleValue path,
const JSCTypesCallbacks* callbacks);
const JS::CTypesCallbacks* callbacks);
bool IsLibrary(JSObject* obj);
PRLibrary* GetLibrary(JSObject* obj);

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

@ -667,28 +667,30 @@ extern JS_PUBLIC_API bool IsProfileTimelineRecordingEnabled();
} // namespace JS
#ifdef JS_HAS_CTYPES
namespace JS {
/**
* Initialize the 'ctypes' object on a global variable 'obj'. The 'ctypes'
* object will be sealed.
*/
extern JS_PUBLIC_API bool JS_InitCTypesClass(JSContext* cx,
JS::HandleObject global);
extern JS_PUBLIC_API bool InitCTypesClass(JSContext* cx,
JS::Handle<JSObject*> global);
/**
* Convert a unicode string 'source' of length 'slen' to the platform native
* charset, returning a null-terminated string allocated with JS_malloc. On
* failure, this function should report an error.
*/
using JSCTypesUnicodeToNativeFun = char* (*)(JSContext*, const char16_t*,
size_t);
using CTypesUnicodeToNativeFun = char* (*)(JSContext*, const char16_t*, size_t);
/**
* Set of function pointers that ctypes can use for various internal functions.
* See JS_SetCTypesCallbacks below. Providing nullptr for a function is safe,
* See JS::SetCTypesCallbacks below. Providing nullptr for a function is safe
* and will result in the applicable ctypes functionality not being available.
*/
struct JSCTypesCallbacks {
JSCTypesUnicodeToNativeFun unicodeToNative;
struct CTypesCallbacks {
CTypesUnicodeToNativeFun unicodeToNative;
};
/**
@ -697,8 +699,11 @@ struct JSCTypesCallbacks {
* may safely be altered after calling this function and without having
* to call this function again.
*/
extern JS_PUBLIC_API void JS_SetCTypesCallbacks(
JSObject* ctypesObj, const JSCTypesCallbacks* callbacks);
extern JS_PUBLIC_API void SetCTypesCallbacks(JSObject* ctypesObj,
const CTypesCallbacks* callbacks);
} // namespace JS
#endif
/*

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

@ -737,14 +737,13 @@ JS_FRIEND_API void js::SetScriptEnvironmentPreparer(
cx->runtime()->scriptEnvironmentPreparer = preparer;
}
JS_FRIEND_API void js::SetCTypesActivityCallback(JSContext* cx,
JS_FRIEND_API void JS::SetCTypesActivityCallback(JSContext* cx,
CTypesActivityCallback cb) {
cx->runtime()->ctypesActivityCallback = cb;
}
js::AutoCTypesActivityCallback::AutoCTypesActivityCallback(
JSContext* cx, js::CTypesActivityType beginType,
js::CTypesActivityType endType)
JS::AutoCTypesActivityCallback::AutoCTypesActivityCallback(
JSContext* cx, CTypesActivityType beginType, CTypesActivityType endType)
: cx(cx),
callback(cx->runtime()->ctypesActivityCallback),
endType(endType) {

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

@ -300,11 +300,6 @@ extern JS_FRIEND_API void IterateGrayObjects(
extern JS_FRIEND_API bool CheckGrayMarkingState(JSRuntime* rt);
#endif
#ifdef JS_HAS_CTYPES
extern JS_FRIEND_API size_t
SizeOfDataIfCDataObject(mozilla::MallocSizeOf mallocSizeOf, JSObject* obj);
#endif
// Note: this returns nullptr iff |zone| is the atoms zone.
extern JS_FRIEND_API JS::Realm* GetAnyRealmInZone(JS::Zone* zone);
@ -648,21 +643,32 @@ extern JS_FRIEND_API void PrepareScriptEnvironmentAndInvoke(
JS_FRIEND_API void SetScriptEnvironmentPreparer(
JSContext* cx, ScriptEnvironmentPreparer* preparer);
enum CTypesActivityType {
CTYPES_CALL_BEGIN,
CTYPES_CALL_END,
CTYPES_CALLBACK_BEGIN,
CTYPES_CALLBACK_END
} // namespace js
namespace JS {
/**
* The type of ctypes activity that is occurring.
*/
enum class CTypesActivityType {
BeginCall,
EndCall,
BeginCallback,
EndCallback,
};
/**
* The signature of a function invoked at the leading or trailing edge of ctypes
* activity.
*/
using CTypesActivityCallback = void (*)(JSContext*, CTypesActivityType);
/**
* Sets a callback that is run whenever js-ctypes is about to be used when
* calling into C.
*/
JS_FRIEND_API void SetCTypesActivityCallback(JSContext* cx,
CTypesActivityCallback cb);
extern JS_FRIEND_API void SetCTypesActivityCallback(JSContext* cx,
CTypesActivityCallback cb);
class MOZ_RAII JS_FRIEND_API AutoCTypesActivityCallback {
private:
@ -673,7 +679,9 @@ class MOZ_RAII JS_FRIEND_API AutoCTypesActivityCallback {
public:
AutoCTypesActivityCallback(JSContext* cx, CTypesActivityType beginType,
CTypesActivityType endType);
~AutoCTypesActivityCallback() { DoEndCallback(); }
void DoEndCallback() {
if (callback) {
callback(cx, endType);
@ -682,6 +690,10 @@ class MOZ_RAII JS_FRIEND_API AutoCTypesActivityCallback {
}
};
} // namespace JS
namespace js {
// Abstract base class for objects that build allocation metadata for JavaScript
// values.
struct AllocationMetadataBuilder {

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

@ -10100,7 +10100,7 @@ static JSObject* NewGlobalObject(JSContext* cx, JS::RealmOptions& options,
"having its [[Prototype]] be immutable");
#ifdef JS_HAS_CTYPES
if (!fuzzingSafe && !JS_InitCTypesClass(cx, glob)) {
if (!fuzzingSafe && !JS::InitCTypesClass(cx, glob)) {
return nullptr;
}
#endif

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

@ -3922,13 +3922,14 @@ void JSObject::addSizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf,
} else if (is<WeakCollectionObject>()) {
info->objectsMallocHeapMisc +=
as<WeakCollectionObject>().sizeOfExcludingThis(mallocSizeOf);
#ifdef JS_HAS_CTYPES
} else {
// This must be the last case.
info->objectsMallocHeapMisc +=
js::SizeOfDataIfCDataObject(mallocSizeOf, const_cast<JSObject*>(this));
#endif
}
#ifdef JS_HAS_CTYPES
else {
// This must be the last case.
info->objectsMallocHeapMisc += ctypes::SizeOfDataIfCDataObject(
mallocSizeOf, const_cast<JSObject*>(this));
}
#endif
}
size_t JSObject::sizeOfIncludingThisInNursery() const {

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

@ -1112,6 +1112,17 @@ void CallTraceMethod(JSTracer* trc, JSObject* obj) {
obj->as<ObjectSubclass>().trace(trc);
}
#ifdef JS_HAS_CTYPES
namespace ctypes {
extern size_t SizeOfDataIfCDataObject(mozilla::MallocSizeOf mallocSizeOf,
JSObject* obj);
} // namespace ctypes
#endif
} /* namespace js */
#endif /* vm_JSObject_h */

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

@ -414,7 +414,7 @@ struct JSRuntime {
js::MainThreadData<js::ScriptEnvironmentPreparer*> scriptEnvironmentPreparer;
js::MainThreadData<js::CTypesActivityCallback> ctypesActivityCallback;
js::MainThreadData<JS::CTypesActivityCallback> ctypesActivityCallback;
private:
js::WriteOnceData<const JSClass*> windowProxyClass_;

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

@ -33,7 +33,7 @@ static char* UnicodeToNative(JSContext* cx, const char16_t* source,
return result;
}
static JSCTypesCallbacks sCallbacks = {UnicodeToNative};
static JS::CTypesCallbacks sCallbacks = {UnicodeToNative};
NS_IMPL_ISUPPORTS(Module, nsIXPCScriptable)
@ -66,13 +66,13 @@ static bool SealObjectAndPrototype(JSContext* cx, JS::Handle<JSObject*> parent,
static bool InitAndSealCTypesClass(JSContext* cx,
JS::Handle<JSObject*> global) {
// Init the ctypes object.
if (!JS_InitCTypesClass(cx, global)) return false;
if (!JS::InitCTypesClass(cx, global)) return false;
// Set callbacks for charset conversion and such.
JS::Rooted<JS::Value> ctypes(cx);
if (!JS_GetProperty(cx, global, "ctypes", &ctypes)) return false;
JS_SetCTypesCallbacks(ctypes.toObjectOrNull(), &sCallbacks);
JS::SetCTypesCallbacks(ctypes.toObjectOrNull(), &sCallbacks);
// Seal up Object, Function, Array and Error and their prototypes. (This
// single object instance is shared amongst everyone who imports the ctypes