summaryrefslogtreecommitdiff
path: root/modules/basic/basic.c
diff options
context:
space:
mode:
authorStephen Leake <stephen_leake@stephe-leake.org>2015-06-09 17:32:30 -0500
committerStephen Leake <stephen_leake@stephe-leake.org>2015-06-09 17:32:30 -0500
commitf128e085bc0674967b988a72f8074a7d0cc8eba3 (patch)
tree09dbdeccc79ed5801582dc5aa860a4b04cafc5ef /modules/basic/basic.c
parent76f2d766ad6691eae6ae4006264f59724cc73a23 (diff)
downloademacs-scratch/dynamic-modules-2.tar.gz
Add loadable modules using Daniel Colascione's ideas.scratch/dynamic-modules-2
See https://lists.gnu.org/archive/html/emacs-devel/2015-02/msg00960.html * src/Makefile.in (base_obj): add module.o (LIBES): add -lltdl * src/emacs.c (main): add syms_of_module * src/lisp.h: add syms_of_module * src/emacs_module.h: New file; emacs API for modules. * src/module.c: New file; implement API. * modules/basic/Makefile: New file; build example module on Linux. * modules/basic/basic.c: New file; simple example module.
Diffstat (limited to 'modules/basic/basic.c')
-rw-r--r--modules/basic/basic.c64
1 files changed, 64 insertions, 0 deletions
diff --git a/modules/basic/basic.c b/modules/basic/basic.c
new file mode 100644
index 00000000000..f288b3832ca
--- /dev/null
+++ b/modules/basic/basic.c
@@ -0,0 +1,64 @@
+/*
+
+ basic.c - sample module
+
+ This module provides a simple `basic-sum' function.
+
+ I've used the following prefixes throughout the code:
+ - Sfoo: subr (function wraper)
+ - Qfoo: symbol value
+ - Ffoo: function value
+
+*/
+
+#include <emacs_module.h>
+
+int plugin_is_GPL_compatible;
+
+/* C function we want to expose to emacs */
+static int64_t sum (int64_t a, int64_t b)
+{
+ return a + b;
+}
+
+/* Proper module subr that wraps the C function */
+static emacs_value Fsum (emacs_env *env, int nargs, emacs_value args[])
+{
+ int64_t a = env->fixnum_to_int (env, args[0]);
+ int64_t b = env->fixnum_to_int (env, args[1]);
+
+ int64_t r = sum(a, b);
+
+ return env->make_fixnum (env, r);
+}
+
+/* Binds NAME to FUN */
+static void bind_function (emacs_env *env, const char *name, emacs_value Ffun)
+{
+ emacs_value Qfset = env->intern (env, "fset");
+ emacs_value Qsym = env->intern (env, name);
+ emacs_value args[] = { Qsym, Ffun };
+
+ env->funcall (env, Qfset, 2, args);
+}
+
+/* Provide FEATURE to Emacs */
+static void provide (emacs_env *env, const char *feature)
+{
+ emacs_value Qfeat = env->intern (env, feature);
+ emacs_value Qprovide = env->intern (env, "provide");
+ emacs_value args[] = { Qfeat };
+
+ env->funcall (env, Qprovide, 1, args);
+}
+
+int emacs_module_init (struct emacs_runtime *ert)
+{
+ emacs_env *env = ert->get_environment (ert);
+ emacs_value Ssum = env->make_function (env, 2, 2, Fsum);
+
+ bind_function (env, "basic-sum", Ssum);
+ provide (env, "basic");
+
+ return 0;
+}