summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPhilip Chimento <philip.chimento@gmail.com>2020-05-13 14:43:45 -0700
committerPhilip Chimento <philip.chimento@gmail.com>2021-01-30 13:56:30 -0800
commitbb490adb41613727a553d723f055dad83c0c11ec (patch)
tree851190231aa5f91eb1b9eaa46d3e71da37f9773f
parentaa77cf29c7ffcbc190b9b183b51b73113f407323 (diff)
downloadgjs-bb490adb41613727a553d723f055dad83c0c11ec.tar.gz
cairo: Remove JSClass macros from CairoRegion
Remove the usage of the GJS_DEFINE_PRIV_FROM_JS and GJS_DEFINE_PROTO families of macros from the Cairo region wrapper type. Use CWrapper instead, for more type safety and less boilerplate.
-rw-r--r--modules/cairo-private.h44
-rw-r--r--modules/cairo-region.cpp99
-rw-r--r--modules/cairo.cpp3
3 files changed, 61 insertions, 85 deletions
diff --git a/modules/cairo-private.h b/modules/cairo-private.h
index 76dd75d5..12901d1c 100644
--- a/modules/cairo-private.h
+++ b/modules/cairo-private.h
@@ -32,10 +32,46 @@ bool gjs_cairo_check_status (JSContext *contex
cairo_status_t status,
const char *name);
-GJS_JSAPI_RETURN_CONVENTION
-bool gjs_cairo_region_define_proto(JSContext *cx,
- JS::HandleObject module,
- JS::MutableHandleObject proto);
+class CairoRegion : public CWrapper<CairoRegion, cairo_region_t> {
+ friend CWrapperPointerOps<CairoRegion, cairo_region_t>;
+ friend CWrapper<CairoRegion, cairo_region_t>;
+
+ CairoRegion() = delete;
+ CairoRegion(CairoRegion&) = delete;
+ CairoRegion(CairoRegion&&) = delete;
+
+ static constexpr GjsGlobalSlot PROTOTYPE_SLOT =
+ GjsGlobalSlot::PROTOTYPE_cairo_region;
+ static constexpr GjsDebugTopic DEBUG_TOPIC = GJS_DEBUG_CAIRO;
+ static constexpr unsigned constructor_nargs = 0;
+
+ static GType gtype() { return CAIRO_GOBJECT_TYPE_REGION; }
+
+ static cairo_region_t* copy_ptr(cairo_region_t* region) {
+ return cairo_region_reference(region);
+ }
+
+ GJS_JSAPI_RETURN_CONVENTION
+ static cairo_region_t* constructor_impl(JSContext* cx,
+ const JS::CallArgs& args);
+
+ static void finalize_impl(JSFreeOp* fop, cairo_region_t* cr);
+
+ static const JSFunctionSpec proto_funcs[];
+ static const JSPropertySpec proto_props[];
+ static constexpr js::ClassSpec class_spec = {
+ nullptr, // createConstructor
+ nullptr, // createPrototype
+ nullptr, // constructorFunctions
+ nullptr, // constructorProperties
+ CairoRegion::proto_funcs,
+ CairoRegion::proto_props,
+ CairoRegion::define_gtype_prop,
+ };
+ static constexpr JSClass klass = {
+ "Region", JSCLASS_HAS_PRIVATE | JSCLASS_BACKGROUND_FINALIZE,
+ &CairoRegion::class_ops, &CairoRegion::class_spec};
+};
void gjs_cairo_region_init(void);
diff --git a/modules/cairo-region.cpp b/modules/cairo-region.cpp
index e731fb4f..a0fb1f1d 100644
--- a/modules/cairo-region.cpp
+++ b/modules/cairo-region.cpp
@@ -4,13 +4,10 @@
#include <config.h>
-#include <cairo-gobject.h>
#include <cairo.h>
#include <girepository.h>
-#include <glib.h>
#include <js/CallArgs.h>
-#include <js/Class.h>
#include <js/Conversions.h>
#include <js/PropertyDescriptor.h> // for JSPROP_READONLY
#include <js/PropertySpec.h>
@@ -25,43 +22,22 @@
#include "gjs/atoms.h"
#include "gjs/context-private.h"
#include "gjs/enum-utils.h"
-#include "gjs/jsapi-class.h"
#include "gjs/jsapi-util-args.h"
#include "gjs/jsapi-util.h"
#include "gjs/macros.h"
#include "modules/cairo-private.h"
-[[nodiscard]] static JSObject* gjs_cairo_region_get_proto(JSContext*);
-
-struct GjsCairoRegion
- : GjsAutoPointer<cairo_region_t, cairo_region_t, cairo_region_destroy,
- cairo_region_reference> {
- explicit GjsCairoRegion(cairo_region_t* region)
- : GjsAutoPointer(region, GjsAutoTakeOwnership()) {}
-};
-
-GJS_DEFINE_PROTO_WITH_GTYPE("Region", cairo_region,
- CAIRO_GOBJECT_TYPE_REGION,
- JSCLASS_BACKGROUND_FINALIZE)
-GJS_DEFINE_PRIV_FROM_JS(GjsCairoRegion, gjs_cairo_region_class);
-
-static cairo_region_t *
-get_region(JSContext *context,
- JS::HandleObject obj)
-{
- auto* priv = priv_from_js(context, obj);
- return priv ? priv->get() : nullptr;
-}
-
GJS_JSAPI_RETURN_CONVENTION
static bool
fill_rectangle(JSContext *context,
JS::HandleObject obj,
cairo_rectangle_int_t *rect);
-#define PRELUDE \
- GJS_GET_THIS(context, argc, vp, argv, obj); \
- auto* this_region = get_region(context, obj);
+#define PRELUDE \
+ GJS_GET_THIS(context, argc, vp, argv, obj); \
+ cairo_region_t* this_region; \
+ if (!CairoRegion::for_js_typecheck(context, obj, &this_region, &argv)) \
+ return false;
#define RETURN_STATUS \
return gjs_cairo_check_status(context, cairo_region_status(this_region), "region");
@@ -76,7 +52,8 @@ fill_rectangle(JSContext *context,
&other_obj)) \
return false; \
\
- cairo_region_t* other_region = get_region(context, other_obj); \
+ cairo_region_t* other_region = \
+ CairoRegion::for_js(context, other_obj); \
\
cairo_region_##method(this_region, other_region); \
argv.rval().setUndefined(); \
@@ -214,12 +191,12 @@ get_rectangle_func(JSContext *context,
}
// clang-format off
-JSPropertySpec gjs_cairo_region_proto_props[] = {
+const JSPropertySpec CairoRegion::proto_props[] = {
JS_STRING_SYM_PS(toStringTag, "Region", JSPROP_READONLY),
JS_PS_END};
// clang-format on
-JSFunctionSpec gjs_cairo_region_proto_funcs[] = {
+const JSFunctionSpec CairoRegion::proto_funcs[] = {
JS_FN("union", union_func, 0, 0),
JS_FN("subtract", subtract_func, 0, 0),
JS_FN("intersect", intersect_func, 0, 0),
@@ -234,54 +211,19 @@ JSFunctionSpec gjs_cairo_region_proto_funcs[] = {
JS_FN("getRectangle", get_rectangle_func, 0, 0),
JS_FS_END};
-JSFunctionSpec gjs_cairo_region_static_funcs[] = { JS_FS_END };
-
-static void _gjs_cairo_region_construct_internal(JSContext* context,
- JS::HandleObject obj,
- cairo_region_t* region) {
- g_assert(!priv_from_js(context, obj));
- JS_SetPrivate(obj, new GjsCairoRegion(region));
-}
-
-GJS_NATIVE_CONSTRUCTOR_DECLARE(cairo_region)
-{
- GJS_NATIVE_CONSTRUCTOR_VARIABLES(cairo_region)
- cairo_region_t *region;
-
- GJS_NATIVE_CONSTRUCTOR_PRELUDE(cairo_region);
-
+cairo_region_t* CairoRegion::constructor_impl(JSContext* context,
+ const JS::CallArgs& argv) {
if (!gjs_parse_call_args(context, "Region", argv, ""))
- return false;
-
- region = cairo_region_create();
-
- _gjs_cairo_region_construct_internal(context, object, region);
- cairo_region_destroy(region);
-
- GJS_NATIVE_CONSTRUCTOR_FINISH(cairo_region);
-
- return true;
-}
+ return nullptr;
-static void gjs_cairo_region_finalize(JSFreeOp*, JSObject* obj) {
- delete static_cast<GjsCairoRegion*>(JS_GetPrivate(obj));
- JS_SetPrivate(obj, nullptr);
+ return cairo_region_create();
}
-GJS_JSAPI_RETURN_CONVENTION
-static JSObject *
-gjs_cairo_region_from_region(JSContext *context,
- cairo_region_t *region)
-{
- JS::RootedObject proto(context, gjs_cairo_region_get_proto(context));
- JS::RootedObject object(context,
- JS_NewObjectWithGivenProto(context, &gjs_cairo_region_class, proto));
- if (!object)
- return nullptr;
-
- _gjs_cairo_region_construct_internal(context, object, region);
+void CairoRegion::finalize_impl(JSFreeOp*, cairo_region_t* region) {
+ if (!region)
+ return;
- return object;
+ cairo_region_destroy(region);
}
[[nodiscard]] static bool region_to_g_argument(
@@ -303,8 +245,7 @@ gjs_cairo_region_from_region(JSContext *context,
JS::RootedObject obj(context, &value.toObject());
cairo_region_t *region;
- region = get_region(context, obj);
- if (!region)
+ if (!CairoRegion::for_js_typecheck(context, obj, &region))
return false;
if (transfer == GI_TRANSFER_EVERYTHING)
cairo_region_destroy(region);
@@ -319,8 +260,8 @@ region_from_g_argument(JSContext *context,
JS::MutableHandleValue value_p,
GIArgument *arg)
{
- JSObject* obj = gjs_cairo_region_from_region(
- context, gjs_arg_get<cairo_region_t*>(arg));
+ JSObject* obj =
+ CairoRegion::from_c_ptr(context, gjs_arg_get<cairo_region_t*>(arg));
if (!obj)
return false;
diff --git a/modules/cairo.cpp b/modules/cairo.cpp
index 652b1b06..614c8ec5 100644
--- a/modules/cairo.cpp
+++ b/modules/cairo.cpp
@@ -55,9 +55,8 @@ gjs_js_define_cairo_stuff(JSContext *context,
JS::MutableHandleObject module)
{
module.set(JS_NewPlainObject(context));
- JS::RootedObject proto(context); /* not used */
- if (!gjs_cairo_region_define_proto(context, module, &proto))
+ if (!CairoRegion::create_prototype(context, module))
return false;
gjs_cairo_region_init();