[gjs/burninate-macros: 8/13] cairo: Remove JSClass macros from cairo context
- From: Philip Chimento <pchimento src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gjs/burninate-macros: 8/13] cairo: Remove JSClass macros from cairo context
- Date: Wed, 3 Jun 2020 15:17:24 +0000 (UTC)
commit be842cc0af8a0b73a168e9df4a75b125cdb9f282
Author: Philip Chimento <philip chimento gmail com>
Date: Tue May 5 22:30:12 2020 -0700
cairo: Remove JSClass macros from cairo context
FIXME: Need commit message
modules/cairo-context.cpp | 157 ++++++++++++++++++++--------------------------
modules/cairo-private.h | 47 +++++++++++---
modules/cairo.cpp | 2 +-
3 files changed, 106 insertions(+), 100 deletions(-)
---
diff --git a/modules/cairo-context.cpp b/modules/cairo-context.cpp
index 52b7734c..a1ede125 100644
--- a/modules/cairo-context.cpp
+++ b/modules/cairo-context.cpp
@@ -24,7 +24,6 @@
#include <vector>
-#include <cairo-gobject.h>
#include <cairo.h>
#include <girepository.h>
#include <glib.h>
@@ -47,12 +46,27 @@
#include "gjs/macros.h"
#include "modules/cairo-private.h"
-#define _GJS_CAIRO_CONTEXT_DEFINE_FUNC_BEGIN(mname) \
- GJS_JSAPI_RETURN_CONVENTION \
- static bool mname##_func(JSContext* context, unsigned argc, \
- JS::Value* vp) { \
- GJS_GET_PRIV(context, argc, vp, argv, obj, cairo_t, cr); \
- if (!cr) \
+const js::ClassSpec CairoContext::class_spec = {
+ nullptr, // createConstructor
+ nullptr, // createPrototype
+ nullptr, // constructorFunctions
+ nullptr, // constructorProperties
+ CairoContext::proto_funcs,
+ nullptr, // prototypeProperties
+ CairoContext::define_gtype_prop,
+};
+
+const JSClass CairoContext::klass = {
+ "Context", JSCLASS_HAS_PRIVATE | JSCLASS_BACKGROUND_FINALIZE,
+ &CairoContext::class_ops};
+
+#define _GJS_CAIRO_CONTEXT_DEFINE_FUNC_BEGIN(mname) \
+ GJS_JSAPI_RETURN_CONVENTION \
+ static bool mname##_func(JSContext* context, unsigned argc, \
+ JS::Value* vp) { \
+ GJS_GET_THIS(context, argc, vp, argv, obj); \
+ cairo_t* cr = CairoContext::for_js(context, obj, argv); \
+ if (!cr) \
return true;
#define _GJS_CAIRO_CONTEXT_DEFINE_FUNC_END \
@@ -244,60 +258,35 @@ _GJS_CAIRO_CONTEXT_DEFINE_FUNC_BEGIN(method) \
argv.rval().setUndefined(); \
_GJS_CAIRO_CONTEXT_DEFINE_FUNC_END
-GJS_USE
-static JSObject *gjs_cairo_context_get_proto(JSContext *);
-
-GJS_DEFINE_PROTO_WITH_GTYPE("Context", cairo_context,
- CAIRO_GOBJECT_TYPE_CONTEXT,
- JSCLASS_BACKGROUND_FINALIZE)
-GJS_DEFINE_PRIV_FROM_JS(cairo_t, gjs_cairo_context_class);
-
-static void _gjs_cairo_context_construct_internal(JSObject* obj, cairo_t* cr) {
- g_assert(!JS_GetPrivate(obj));
- JS_SetPrivate(obj, cairo_reference(cr));
-}
-
-GJS_NATIVE_CONSTRUCTOR_DECLARE(cairo_context)
-{
- GJS_NATIVE_CONSTRUCTOR_VARIABLES(cairo_context)
+GJS_JSAPI_RETURN_CONVENTION
+cairo_t* CairoContext::constructor_impl(JSContext* context,
+ const JS::CallArgs& argv) {
cairo_t *cr;
- GJS_NATIVE_CONSTRUCTOR_PRELUDE(cairo_context);
-
JS::RootedObject surface_wrapper(context);
if (!gjs_parse_call_args(context, "Context", argv, "o",
"surface", &surface_wrapper))
- return false;
+ return nullptr;
cairo_surface_t* surface =
gjs_cairo_surface_get_surface(context, surface_wrapper);
if (!surface)
- return false;
+ return nullptr;
cr = cairo_create(surface);
if (!gjs_cairo_check_status(context, cairo_status(cr), "context"))
- return false;
-
- _gjs_cairo_context_construct_internal(object, cr);
- cairo_destroy(cr);
-
- GJS_NATIVE_CONSTRUCTOR_FINISH(cairo_context);
+ return nullptr;
- return true;
+ return cr;
}
-static void gjs_cairo_context_finalize(JSFreeOp*, JSObject* obj) {
- using AutoCairoContext = GjsAutoPointer<cairo_t, cairo_t, cairo_destroy>;
- AutoCairoContext cr = static_cast<cairo_t*>(JS_GetPrivate(obj));
- JS_SetPrivate(obj, nullptr);
+void CairoContext::finalize_impl(JSFreeOp*, cairo_t* cr) {
+ if (!cr)
+ return;
+ cairo_destroy(cr);
}
-/* Properties */
-JSPropertySpec gjs_cairo_context_proto_props[] = {
- JS_PS_END
-};
-
/* Methods */
_GJS_CAIRO_CONTEXT_DEFINE_FUNC5(arc, cairo_arc, "fffff",
@@ -383,7 +372,8 @@ dispose_func(JSContext *context,
unsigned argc,
JS::Value *vp)
{
- GJS_GET_PRIV(context, argc, vp, rec, obj, cairo_t, cr);
+ GJS_GET_THIS(context, argc, vp, rec, obj);
+ cairo_t* cr = CairoContext::for_js(context, obj, rec);
if (!cr)
return true;
@@ -400,7 +390,8 @@ appendPath_func(JSContext *context,
unsigned argc,
JS::Value *vp)
{
- GJS_GET_PRIV(context, argc, vp, argv, obj, cairo_t, cr);
+ GJS_GET_THIS(context, argc, vp, argv, obj);
+ cairo_t* cr = CairoContext::for_js(context, obj, argv);
if (!cr)
return true;
@@ -425,7 +416,8 @@ copyPath_func(JSContext *context,
unsigned argc,
JS::Value *vp)
{
- GJS_GET_PRIV(context, argc, vp, argv, obj, cairo_t, cr);
+ GJS_GET_THIS(context, argc, vp, argv, obj);
+ cairo_t* cr = CairoContext::for_js(context, obj, argv);
if (!cr)
return true;
@@ -445,7 +437,8 @@ copyPathFlat_func(JSContext *context,
unsigned argc,
JS::Value *vp)
{
- GJS_GET_PRIV(context, argc, vp, argv, obj, cairo_t, cr);
+ GJS_GET_THIS(context, argc, vp, argv, obj);
+ cairo_t* cr = CairoContext::for_js(context, obj, argv);
if (!cr)
return true;
@@ -465,7 +458,8 @@ mask_func(JSContext *context,
unsigned argc,
JS::Value *vp)
{
- GJS_GET_PRIV(context, argc, vp, argv, obj, cairo_t, cr);
+ GJS_GET_THIS(context, argc, vp, argv, obj);
+ cairo_t* cr = CairoContext::for_js(context, obj, argv);
if (!cr)
return true;
@@ -495,7 +489,8 @@ maskSurface_func(JSContext *context,
unsigned argc,
JS::Value *vp)
{
- GJS_GET_PRIV(context, argc, vp, argv, obj, cairo_t, cr);
+ GJS_GET_THIS(context, argc, vp, argv, obj);
+ cairo_t* cr = CairoContext::for_js(context, obj, argv);
if (!cr)
return true;
@@ -528,7 +523,8 @@ setDash_func(JSContext *context,
unsigned argc,
JS::Value *vp)
{
- GJS_GET_PRIV(context, argc, vp, argv, obj, cairo_t, cr);
+ GJS_GET_THIS(context, argc, vp, argv, obj);
+ cairo_t* cr = CairoContext::for_js(context, obj, argv);
if (!cr)
return true;
@@ -589,7 +585,8 @@ setSource_func(JSContext *context,
unsigned argc,
JS::Value *vp)
{
- GJS_GET_PRIV(context, argc, vp, argv, obj, cairo_t, cr);
+ GJS_GET_THIS(context, argc, vp, argv, obj);
+ cairo_t* cr = CairoContext::for_js(context, obj, argv);
if (!cr)
return true;
@@ -620,7 +617,8 @@ setSourceSurface_func(JSContext *context,
unsigned argc,
JS::Value *vp)
{
- GJS_GET_PRIV(context, argc, vp, argv, obj, cairo_t, cr);
+ GJS_GET_THIS(context, argc, vp, argv, obj);
+ cairo_t* cr = CairoContext::for_js(context, obj, argv);
if (!cr)
return true;
@@ -654,7 +652,8 @@ showText_func(JSContext *context,
unsigned argc,
JS::Value *vp)
{
- GJS_GET_PRIV(context, argc, vp, argv, obj, cairo_t, cr);
+ GJS_GET_THIS(context, argc, vp, argv, obj);
+ cairo_t* cr = CairoContext::for_js(context, obj, argv);
if (!cr)
return true;
@@ -680,7 +679,8 @@ selectFontFace_func(JSContext *context,
unsigned argc,
JS::Value *vp)
{
- GJS_GET_PRIV(context, argc, vp, argv, obj, cairo_t, cr);
+ GJS_GET_THIS(context, argc, vp, argv, obj);
+ cairo_t* cr = CairoContext::for_js(context, obj, argv);
if (!cr)
return true;
@@ -709,7 +709,8 @@ popGroup_func(JSContext *context,
unsigned argc,
JS::Value *vp)
{
- GJS_GET_PRIV(context, argc, vp, rec, obj, cairo_t, cr);
+ GJS_GET_THIS(context, argc, vp, rec, obj);
+ cairo_t* cr = CairoContext::for_js(context, obj, rec);
if (!cr)
return true;
@@ -742,7 +743,8 @@ getSource_func(JSContext *context,
unsigned argc,
JS::Value *vp)
{
- GJS_GET_PRIV(context, argc, vp, rec, obj, cairo_t, cr);
+ GJS_GET_THIS(context, argc, vp, rec, obj);
+ cairo_t* cr = CairoContext::for_js(context, obj, rec);
if (!cr)
return true;
@@ -776,7 +778,8 @@ getTarget_func(JSContext *context,
unsigned argc,
JS::Value *vp)
{
- GJS_GET_PRIV(context, argc, vp, rec, obj, cairo_t, cr);
+ GJS_GET_THIS(context, argc, vp, rec, obj);
+ cairo_t* cr = CairoContext::for_js(context, obj, rec);
if (!cr)
return true;
@@ -810,7 +813,8 @@ getGroupTarget_func(JSContext *context,
unsigned argc,
JS::Value *vp)
{
- GJS_GET_PRIV(context, argc, vp, rec, obj, cairo_t, cr);
+ GJS_GET_THIS(context, argc, vp, rec, obj);
+ cairo_t* cr = CairoContext::for_js(context, obj, rec);
if (!cr)
return true;
@@ -838,7 +842,8 @@ getGroupTarget_func(JSContext *context,
return true;
}
-JSFunctionSpec gjs_cairo_context_proto_funcs[] = {
+// clang-format off
+const JSFunctionSpec CairoContext::proto_funcs[] = {
JS_FN("$dispose", dispose_func, 0, 0),
JS_FN("appendPath", appendPath_func, 0, 0),
JS_FN("arc", arc_func, 0, 0),
@@ -938,30 +943,7 @@ JSFunctionSpec gjs_cairo_context_proto_funcs[] = {
JS_FN("userToDevice", userToDevice_func, 0, 0),
JS_FN("userToDeviceDistance", userToDeviceDistance_func, 0, 0),
JS_FS_END};
-
-JSFunctionSpec gjs_cairo_context_static_funcs[] = { JS_FS_END };
-
-JSObject *
-gjs_cairo_context_from_context(JSContext *context,
- cairo_t *cr)
-{
- JS::RootedObject proto(context, gjs_cairo_context_get_proto(context));
- JS::RootedObject object(context,
- JS_NewObjectWithGivenProto(context, &gjs_cairo_context_class, proto));
- if (!object)
- return nullptr;
-
- _gjs_cairo_context_construct_internal(object, cr);
-
- return object;
-}
-
-cairo_t *
-gjs_cairo_context_get_context(JSContext *context,
- JS::HandleObject object)
-{
- return priv_from_js(context, object);
-}
+// clang-format on
GJS_USE
static bool
@@ -986,9 +968,7 @@ context_to_g_argument(JSContext *context,
}
JS::RootedObject obj(context, &value.toObject());
- cairo_t *cr;
-
- cr = gjs_cairo_context_get_context(context, obj);
+ cairo_t* cr = CairoContext::for_js(context, obj);
if (!cr)
return false;
if (transfer == GI_TRANSFER_EVERYTHING)
@@ -1004,9 +984,8 @@ context_from_g_argument(JSContext *context,
JS::MutableHandleValue value_p,
GIArgument *arg)
{
- JSObject *obj;
-
- obj = gjs_cairo_context_from_context(context, (cairo_t*)arg->v_pointer);
+ JSObject* obj = CairoContext::from_c_ptr(
+ context, static_cast<cairo_t*>(arg->v_pointer));
if (!obj) {
gjs_throw(context, "Could not create Cairo context");
return false;
diff --git a/modules/cairo-private.h b/modules/cairo-private.h
index d41db0ef..54760ac1 100644
--- a/modules/cairo-private.h
+++ b/modules/cairo-private.h
@@ -26,12 +26,24 @@
#include <config.h>
#include <cairo-features.h> // for CAIRO_HAS_PDF_SURFACE, CAIRO_HAS_PS_SURFACE
+#include <cairo-gobject.h>
#include <cairo.h>
+#include <glib-object.h>
+#include <js/PropertySpec.h>
#include <js/TypeDecls.h>
+#include "gjs/jsapi-class.h"
#include "gjs/macros.h"
+namespace JS {
+class CallArgs;
+}
+namespace js {
+struct ClassSpec;
+}
+struct JSClass;
+
GJS_JSAPI_RETURN_CONVENTION
bool gjs_cairo_check_status (JSContext *context,
cairo_status_t status,
@@ -44,17 +56,32 @@ bool gjs_cairo_region_define_proto(JSContext *cx,
void gjs_cairo_region_init(void);
-GJS_JSAPI_RETURN_CONVENTION
-bool gjs_cairo_context_define_proto(JSContext *cx,
- JS::HandleObject module,
- JS::MutableHandleObject proto);
+class CairoContext;
+using CairoContextBase = NativeObject<CairoContext, cairo_t,
+ GJS_GLOBAL_SLOT_PROTOTYPE_cairo_context>;
+
+class CairoContext : public CairoContextBase {
+ friend CairoContextBase;
+
+ CairoContext() = delete;
+ CairoContext(CairoContext&) = delete;
+ CairoContext(CairoContext&&) = delete;
+
+ static GType gtype() { return CAIRO_GOBJECT_TYPE_CONTEXT; }
+
+ static cairo_t* copy_ptr(cairo_t* cr) { return cairo_reference(cr); }
+
+ GJS_JSAPI_RETURN_CONVENTION
+ static cairo_t* constructor_impl(JSContext* cx, const JS::CallArgs& args);
+
+ static void finalize_impl(JSFreeOp* fop, cairo_t* cr);
+
+ static const JSClass klass;
+ static const js::ClassSpec class_spec;
+ static const JSFunctionSpec proto_funcs[];
+ static const JSPropertySpec constructor_props[];
+};
-GJS_USE
-cairo_t * gjs_cairo_context_get_context (JSContext *context,
- JS::HandleObject object);
-GJS_JSAPI_RETURN_CONVENTION
-JSObject * gjs_cairo_context_from_context (JSContext *context,
- cairo_t *cr);
void gjs_cairo_context_init(void);
void gjs_cairo_surface_init(void);
diff --git a/modules/cairo.cpp b/modules/cairo.cpp
index b4a7fb96..cd26cc77 100644
--- a/modules/cairo.cpp
+++ b/modules/cairo.cpp
@@ -72,7 +72,7 @@ gjs_js_define_cairo_stuff(JSContext *context,
return false;
gjs_cairo_region_init();
- if (!gjs_cairo_context_define_proto(context, module, &proto))
+ if (!CairoContext::create_prototype(context, module))
return false;
gjs_cairo_context_init();
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]