aboutsummaryrefslogtreecommitdiffstats
path: root/modules/basic/basic.c
diff options
context:
space:
mode:
authorStephen Leake2015-06-09 17:32:30 -0500
committerStephen Leake2015-06-09 17:32:30 -0500
commitf128e085bc0674967b988a72f8074a7d0cc8eba3 (patch)
tree09dbdeccc79ed5801582dc5aa860a4b04cafc5ef /modules/basic/basic.c
parent76f2d766ad6691eae6ae4006264f59724cc73a23 (diff)
downloademacs-scratch/dynamic-modules-2.tar.gz
emacs-scratch/dynamic-modules-2.zip
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 @@
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}