diff options
Diffstat (limited to 'modules/basic/basic.c')
| -rw-r--r-- | modules/basic/basic.c | 64 |
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 | |||
| 16 | int plugin_is_GPL_compatible; | ||
| 17 | |||
| 18 | /* C function we want to expose to emacs */ | ||
| 19 | static 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 */ | ||
| 25 | static 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 */ | ||
| 36 | static 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 */ | ||
| 46 | static 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 | |||
| 55 | int 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 | } | ||