[gjs/cairo: 2/5] [cairo] Add cairo.Context() class



commit d8f55ce32051319e73f3613f38b984c7017f1783
Author: Johan Dahlin <johan gnome org>
Date:   Wed Feb 17 19:17:25 2010 -0200

    [cairo] Add cairo.Context() class

 Makefile-modules.am     |    3 +-
 modules/cairo-context.c |  159 +++++++++++++++++++++++++++++++++++++++++++++++
 modules/cairo-private.h |   32 ++++++++++
 modules/cairo.c         |   11 +++-
 4 files changed, 203 insertions(+), 2 deletions(-)
---
diff --git a/Makefile-modules.am b/Makefile-modules.am
index 89c8754..bcc56b5 100644
--- a/Makefile-modules.am
+++ b/Makefile-modules.am
@@ -82,7 +82,8 @@ cairoNative_la_LDFLAGS = 				\
 	$(JS_NATIVE_MODULE_LDFLAGS)
 
 cairoNative_la_SOURCES =		\
-	modules/cairo.h	\
+	modules/cairo-private.h	\
+	modules/cairo-context.c \
 	modules/cairo.c
 
 
diff --git a/modules/cairo-context.c b/modules/cairo-context.c
new file mode 100644
index 0000000..e857791
--- /dev/null
+++ b/modules/cairo-context.c
@@ -0,0 +1,159 @@
+/* -*- mode: C; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
+/* Copyright 2010 litl, LLC. All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to
+ * deal in the Software without restriction, including without limitation the
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ */
+
+#include <config.h>
+
+#include <gjs/gjs.h>
+#include <cairo.h>
+#include "cairo-private.h"
+
+typedef struct {
+    void *dummy;
+    JSContext  *context;
+    JSObject   *object;
+} GjsCairoContext;
+
+static struct JSClass gjs_js_cairo_context_class;
+
+GJS_DEFINE_PRIV_FROM_JS(GjsCairoContext, gjs_js_cairo_context_class);
+
+static JSBool
+cairo_context_new_resolve(JSContext *context,
+                          JSObject  *obj,
+                          jsval      id,
+                          uintN      flags,
+                          JSObject **objp)
+{
+    GjsCairoContext *priv;
+    const char *name;
+
+    *objp = NULL;
+
+    if (!gjs_get_string_id(id, &name))
+        return JS_TRUE; /* not resolved, but no error */
+
+    priv = priv_from_js(context, obj);
+    if (priv == NULL)
+        return JS_TRUE; /* we are the prototype, or have the wrong class */
+
+    return JS_TRUE;
+}
+
+static JSBool
+cairo_context_constructor(JSContext *context,
+                          JSObject  *obj,
+                          uintN      argc,
+                          jsval     *argv,
+                          jsval     *retval)
+{
+    GjsCairoContext *priv;
+
+    priv = g_slice_new0(GjsCairoContext);
+
+    g_assert(priv_from_js(context, obj) == NULL);
+    JS_SetPrivate(context, obj, priv);
+
+    priv->context = context;
+    priv->object = obj;
+
+    return JS_TRUE;
+}
+
+static void
+cairo_context_finalize(JSContext *context,
+                       JSObject  *obj)
+{
+    GjsCairoContext *priv;
+    priv = priv_from_js(context, obj);
+    if (priv == NULL)
+        return;
+
+    g_slice_free(GjsCairoContext, priv);
+}
+
+static struct JSClass gjs_js_cairo_context_class = {
+    "CairoContext", /* means "new CairoContext()" works */
+    JSCLASS_HAS_PRIVATE |
+    JSCLASS_NEW_RESOLVE |
+    JSCLASS_NEW_RESOLVE_GETS_START,
+    JS_PropertyStub,
+    JS_PropertyStub,
+    JS_PropertyStub,
+    JS_PropertyStub,
+    JS_EnumerateStub,
+    (JSResolveOp) cairo_context_new_resolve, /* needs cast since it's the new resolve signature */
+    JS_ConvertStub,
+    cairo_context_finalize,
+    NULL,
+    NULL,
+    NULL,
+    NULL, NULL, NULL, NULL, NULL
+};
+
+static JSPropertySpec gjs_js_cairo_context_proto_props[] = {
+    { NULL }
+};
+
+static JSFunctionSpec gjs_js_cairo_context_proto_funcs[] = {
+    { NULL }
+};
+
+jsval
+gjs_cairo_context_create_proto(JSContext *context)
+{
+    JSObject *global;
+    jsval cairo_context;
+    JSContext *load_context;
+
+    load_context = gjs_runtime_get_load_context(JS_GetRuntime(context));
+    global = JS_GetGlobalObject(context);
+
+    if (!gjs_object_has_property(load_context, global,
+                                 gjs_js_cairo_context_class.name)) {
+        JSObject *prototype;
+
+        prototype = JS_InitClass(load_context, global,
+                                 NULL,
+                                 &gjs_js_cairo_context_class,
+                                 cairo_context_constructor,
+                                 0,
+                                 &gjs_js_cairo_context_proto_props[0],
+                                 &gjs_js_cairo_context_proto_funcs[0],
+                                 NULL,
+                                 NULL);
+        if (prototype == NULL) {
+            gjs_move_exception(load_context, context);
+            return JSVAL_NULL;
+        }
+
+        if (!gjs_object_require_property(
+                load_context, global, NULL,
+                gjs_js_cairo_context_class.name, &cairo_context)) {
+            gjs_move_exception(load_context, context);
+            return JSVAL_NULL;
+        }
+
+    }
+
+    return cairo_context;
+}
+
diff --git a/modules/cairo-private.h b/modules/cairo-private.h
new file mode 100644
index 0000000..1d40c75
--- /dev/null
+++ b/modules/cairo-private.h
@@ -0,0 +1,32 @@
+/* -*- mode: C; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
+/* Copyright 2010 litl, LLC. All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to
+ * deal in the Software without restriction, including without limitation the
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ */
+
+#ifndef __CAIRO_PRIVATE_H__
+#define __CAIRO_PRIVATE_H__
+
+JSBool gjs_js_define_cairo_stuff(JSContext      *context,
+                                 JSObject       *module_obj);
+
+jsval gjs_cairo_context_create_proto(JSContext *context);
+
+#endif /* __CAIRO_PRIVATE_H__ */
+
diff --git a/modules/cairo.c b/modules/cairo.c
index 9f27940..6efec00 100644
--- a/modules/cairo.c
+++ b/modules/cairo.c
@@ -23,12 +23,21 @@
 #include <config.h>
 
 #include <gjs/gjs.h>
-#include <cairo.h>
+
+#include "cairo-private.h"
 
 JSBool
 gjs_js_define_cairo_stuff(JSContext      *context,
                           JSObject       *module_obj)
 {
+    jsval obj;
+
+    obj = gjs_cairo_context_create_proto(context);
+    if (obj == JSVAL_NULL ||
+        !JS_DefineProperty(context, module_obj, "Context",
+                           obj, NULL, NULL, GJS_MODULE_PROP_FLAGS))
+        return JS_FALSE;
+
     return JS_TRUE;
 }
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]