Commit f128e085 authored by Stephen Leake's avatar Stephen Leake

Add loadable modules using Daniel Colascione's ideas.


* src/ (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.
parent 76f2d766
ROOT = ../..
all: basic.doc %.o
gcc -shared $(LDFLAGS) -o $@ $<
%.o: %.c
gcc -ggdb3 -Wall -I$(ROOT)/src $(CFLAGS) -fPIC -c $<
%.doc: %.c
$(ROOT)/lib-src/make-docfile $< > $@
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;
......@@ -376,7 +376,7 @@ base_obj = dispnew.o frame.o scroll.o xdisp.o menu.o $(XMENU_OBJ) window.o \
minibuf.o fileio.o dired.o \
cmds.o casetab.o casefiddle.o indent.o search.o regex.o undo.o \
alloc.o data.o doc.o editfns.o callint.o \
eval.o floatfns.o fns.o font.o print.o lread.o \
eval.o floatfns.o fns.o font.o print.o lread.o module.o \
syntax.o $(UNEXEC_OBJ) bytecode.o \
process.o gnutls.o callproc.o \
region-cache.o sound.o atimer.o \
......@@ -467,7 +467,7 @@ LIBES = $(LIBS) $(W32_LIBS) $(LIBS_GNUSTEP) $(LIBX_BASE) $(LIBIMAGE) \
$(leimdir)/leim-list.el: bootstrap-emacs$(EXEEXT)
$(MAKE) -C ../leim leim-list.el EMACS="$(bootstrap_exe)"
......@@ -1432,6 +1432,9 @@ Using an Emacs configured with --with-x-toolkit=lucid does not have this problem
syms_of_terminal ();
syms_of_term ();
syms_of_undo ();
syms_of_module ();
syms_of_sound ();
emacs_module.h - Module API
Copyright (C) 2015 Free Software Foundation, Inc.
This file is part of GNU Emacs.
GNU Emacs is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
GNU Emacs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Emacs. If not, see <>.
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>
/* Current environment */
typedef struct emacs_env_25 emacs_env;
/* The size of emacs_value must match EMACS_INT:
32 bit system: 32 bits
32 bit system with --with-wide-int: 64 bits
64 bit system: 64 bits.
When compiling modules, define the macro EMACS_VALUE_TYPE by the
result of `module-emacs_value-type'. */
typedef EMACS_VALUE_TYPE emacs_value;
/* Struct passed to a module init function (emacs_module_init) */
struct emacs_runtime {
size_t size;
emacs_env* (*get_environment)(struct emacs_runtime *ert);
/* Function prototype for the module init function */
typedef int (*emacs_init_function)(struct emacs_runtime *ert);
/* Function prototype for the module Lisp functions */
typedef emacs_value (*emacs_subr)(emacs_env *env,
int nargs,
emacs_value args[]);
struct emacs_env_25 {
* Structure size (for version checking)
size_t size;
* Constants
emacs_value Qt_value;
emacs_value Qnil_value;
* Memory management
emacs_value (*make_global_reference)(emacs_env *env,
emacs_value any_reference);
void (*free_global_reference)(emacs_env *env,
emacs_value global_reference);
* Error handling
bool (*error_check)(emacs_env *env);
void (*clear_error)(emacs_env *env);
bool (*get_error)(emacs_env *env,
emacs_value *error_symbol_out,
emacs_value *error_data_out);
void (*signal_error)(emacs_env *env,
const char* msg,
emacs_value error_data);
* Function registration
emacs_value (*make_function)(emacs_env *env,
int min_arity,
int max_arity,
emacs_subr function);
emacs_value (*funcall)(emacs_env *env,
emacs_value function,
int nargs,
emacs_value args[]);
emacs_value (*intern)(emacs_env *env,
const char *symbol_name);
emacs_value (*intern_soft)(emacs_env *env,
const char *symbol_name);
void (*bind_function) (emacs_env *env,
const char *name,
emacs_value definition);
* Type conversion
emacs_value (*type_of)(emacs_env *env,
emacs_value value);
int64_t (*fixnum_to_int)(emacs_env *env,
emacs_value value);
emacs_value (*make_fixnum)(emacs_env *env,
int64_t value);
double (*float_to_c_double)(emacs_env *env,
emacs_value value);
emacs_value (*make_float)(emacs_env *env,
double value);
bool (*copy_string_contents)(emacs_env *env,
emacs_value value,
char *buffer,
size_t* length_inout);
size_t (*buffer_byte_length)(emacs_env *env,
emacs_value start,
emacs_value end);
/* Return the size in bytes of the buffer substring in the current
buffer from START to END */
void (*copy_buffer_substring)(emacs_env *env,
emacs_value start,
emacs_value end,
char *buffer,
size_t* length_inout);
/* Copy buffer string from current buffer, BEG to END (integers or
markers), to BUFFER. On call, LENGTH_INOUT is the size in bytes
of BUFFER; on return, it is the size in bytes of the copied
If BUFFER is too small, signals an error. Use buffer_byte_length
to ensure BUFFER is not too small. */
emacs_value (*make_string)(emacs_env *env,
const char *contents);
* miscellaneous
void (*message)(emacs_env *env,
emacs_value msg);
/* msg must be already formatted */
emacs_value (*symbol_value)(emacs_env *env,
emacs_value symbol);
#endif /* EMACS_MODULE_H */
......@@ -4050,6 +4050,8 @@ Lisp_Object backtrace_top_function (void);
extern bool let_shadows_buffer_binding_p (struct Lisp_Symbol *symbol);
extern bool let_shadows_global_binding_p (Lisp_Object symbol);
/* Defined in module.c. */
void syms_of_module (void);
/* Defined in editfns.c. */
extern void insert1 (Lisp_Object);
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment