[gjs] Add Lang.seal() to make an object read-only.



commit a09ec03f310760b1f9b1e22b7a57ae8181390c4e
Author: C. Scott Ananian <cscott litl com>
Date:   Thu Aug 27 14:42:39 2009 -0400

    Add Lang.seal() to make an object read-only.
    
    This is useful for defining scoped constant enumerations, for example:
    
    const Color = Lang.seal({ RED: 0, GREEN: 1, BLUE: 2 })
    
    https://bugzilla.gnome.org/show_bug.cgi?id=593320

 Makefile-modules.am |   14 +++++++++++-
 modules/lang.c      |   61 +++++++++++++++++++++++++++++++++++++++++++++++++++
 modules/lang.h      |   39 ++++++++++++++++++++++++++++++++
 modules/lang.js     |    3 ++
 4 files changed, 116 insertions(+), 1 deletions(-)
---
diff --git a/Makefile-modules.am b/Makefile-modules.am
index 64e0f08..dceede4 100644
--- a/Makefile-modules.am
+++ b/Makefile-modules.am
@@ -10,7 +10,7 @@ dist_gjsjs_DATA +=		\
 	modules/signals.js	\
 	modules/dbus.js
 
-gjsnative_LTLIBRARIES += console.la debugger.la gi.la mainloop.la gettextNative.la dbusNative.la
+gjsnative_LTLIBRARIES += console.la debugger.la gi.la langNative.la mainloop.la gettextNative.la dbusNative.la
 
 JS_NATIVE_MODULE_CFLAGS =	\
         $(AM_CFLAGS)		\
@@ -35,6 +35,18 @@ gi_la_SOURCES =		\
 	modules/gi.h	\
 	modules/gi.c
 
+langNative_la_CFLAGS = 				\
+	$(JS_NATIVE_MODULE_CFLAGS)
+langNative_la_LIBADD = \
+	libgjs-gi.la				\
+	$(JS_NATIVE_MODULE_LIBADD)
+langNative_la_LDFLAGS = 			\
+	$(JS_NATIVE_MODULE_LDFLAGS)
+
+langNative_la_SOURCES =				\
+	modules/lang.h				\
+	modules/lang.c
+
 mainloop_la_CFLAGS = 				\
 	$(JS_NATIVE_MODULE_CFLAGS)
 mainloop_la_LIBADD = \
diff --git a/modules/lang.c b/modules/lang.c
new file mode 100644
index 0000000..025d48f
--- /dev/null
+++ b/modules/lang.c
@@ -0,0 +1,61 @@
+/* -*- mode: C; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
+/*
+ * Copyright (c) 2009  litl, LLC
+ *
+ * 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 "lang.h"
+#include <gjs/gjs.h>
+
+#include <glib.h>
+#include <jsapi.h>
+
+static JSBool
+gjs_lang_seal(JSContext *cx, JSObject *obj, uintN argc,
+              jsval *argv, jsval *retval)
+{
+    JSObject *target;
+    JSBool deep = JS_FALSE;
+
+    if (!JS_ConvertArguments(cx, argc, argv, "o/b", &target, &deep))
+        return JS_FALSE;
+    if (!target)
+        return JS_TRUE;
+    if (!JS_SealObject(cx, target, deep))
+        return JS_FALSE;
+
+    *retval = OBJECT_TO_JSVAL(target);
+    return JS_TRUE;
+}
+
+JSBool
+gjs_define_lang_stuff(JSContext      *context,
+                      JSObject      *module_obj)
+{
+    if (!JS_DefineFunction(context, module_obj,
+                           "seal",
+                           gjs_lang_seal,
+                           1, GJS_MODULE_PROP_FLAGS))
+        return JS_FALSE;
+
+    return JS_TRUE;
+}
+
+GJS_REGISTER_NATIVE_MODULE("langNative", gjs_define_lang_stuff)
diff --git a/modules/lang.h b/modules/lang.h
new file mode 100644
index 0000000..f1099ce
--- /dev/null
+++ b/modules/lang.h
@@ -0,0 +1,39 @@
+/* -*- mode: C; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
+/*
+ * Copyright (c) 2009  litl, LLC
+ *
+ * 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 __GJS_LANG_H__
+#define __GJS_LANG_H__
+
+#include <config.h>
+#include <glib.h>
+
+#include <jsapi.h>
+
+G_BEGIN_DECLS
+
+JSBool        gjs_define_lang_stuff     (JSContext      *context,
+                                         JSObject       *in_object);
+
+G_END_DECLS
+
+#endif  /* __GJS_LANG_H__ */
diff --git a/modules/lang.js b/modules/lang.js
index baa00fc..018928e 100644
--- a/modules/lang.js
+++ b/modules/lang.js
@@ -110,3 +110,6 @@ function bind(obj, callback) {
         return callback.apply(me, args);
     };
 }
+
+// Merge stuff defined in native code
+copyProperties(imports.langNative, this);



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