aboutsummaryrefslogtreecommitdiffstats
path: root/modules
diff options
context:
space:
mode:
Diffstat (limited to 'modules')
-rw-r--r--modules/basic/Makefile15
-rw-r--r--modules/basic/basic.c64
2 files changed, 79 insertions, 0 deletions
diff --git a/modules/basic/Makefile b/modules/basic/Makefile
new file mode 100644
index 00000000000..bb136f3577f
--- /dev/null
+++ b/modules/basic/Makefile
@@ -0,0 +1,15 @@
1ROOT = ../..
2
3CFLAGS =
4LDFLAGS =
5
6all: basic.so basic.doc
7
8%.so: %.o
9 gcc -shared $(LDFLAGS) -o $@ $<
10
11%.o: %.c
12 gcc -ggdb3 -Wall -I$(ROOT)/src $(CFLAGS) -fPIC -c $<
13
14%.doc: %.c
15 $(ROOT)/lib-src/make-docfile $< > $@
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 @@
1/*
2
3 basic.c - sample module
4
5 This module provides a simple `basic-sum' function.
6
7 I've used the following prefixes throughout the code:
8 - Sfoo: subr (function wraper)
9 - Qfoo: symbol value
10 - Ffoo: function value
11
12*/
13
14#include <emacs_module.h>
15
16int plugin_is_GPL_compatible;
17
18/* C function we want to expose to emacs */
19static int64_t sum (int64_t a, int64_t b)
20{
21 return a + b;
22}
23
24/* Proper module subr that wraps the C function */
25static emacs_value Fsum (emacs_env *env, int nargs, emacs_value args[])
26{
27 int64_t a = env->fixnum_to_int (env, args[0]);
28 int64_t b = env->fixnum_to_int (env, args[1]);
29
30 int64_t r = sum(a, b);
31
32 return env->make_fixnum (env, r);
33}
34
35/* Binds NAME to FUN */
36static void bind_function (emacs_env *env, const char *name, emacs_value Ffun)
37{
38 emacs_value Qfset = env->intern (env, "fset");
39 emacs_value Qsym = env->intern (env, name);
40 emacs_value args[] = { Qsym, Ffun };
41
42 env->funcall (env, Qfset, 2, args);
43}
44
45/* Provide FEATURE to Emacs */
46static void provide (emacs_env *env, const char *feature)
47{
48 emacs_value Qfeat = env->intern (env, feature);
49 emacs_value Qprovide = env->intern (env, "provide");
50 emacs_value args[] = { Qfeat };
51
52 env->funcall (env, Qprovide, 1, args);
53}
54
55int emacs_module_init (struct emacs_runtime *ert)
56{
57 emacs_env *env = ert->get_environment (ert);
58 emacs_value Ssum = env->make_function (env, 2, 2, Fsum);
59
60 bind_function (env, "basic-sum", Ssum);
61 provide (env, "basic");
62
63 return 0;
64}