-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodule-helper.c
83 lines (71 loc) · 2.13 KB
/
module-helper.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <stdlib.h>
#include <string.h>
#include "module-helper.h"
/* copy_lisp_string gives a null terminated string
i.e. the last byte is not part of lisp string
(copy_string_contents semantics) */
char *
copy_lisp_string(emacs_env *env, emacs_value s_val)
{
ptrdiff_t len = 0;
env->copy_string_contents(env, s_val, NULL, &len);
if (env->non_local_exit_check(env)) return NULL;
char *c_str = malloc(len);
bool ok = env->copy_string_contents(env, s_val, c_str, &len);
if (!ok || env->non_local_exit_check(env)) {
free(c_str);
return NULL;
}
return c_str;
}
emacs_value
define_error(emacs_env *env, const char *name, const char *message,
const char *parent)
{
if (!name || !message) {
return el_nil;
}
emacs_value Qdeferr = env->intern(env, "define-error"), Qparent;
if (parent) {
Qparent = env->intern(env, parent);
} else {
Qparent = env->intern(env, "error");
}
emacs_value args[] = {env->intern(env, name),
env->make_string(env, message, strlen(message)),
Qparent};
return env->funcall(env, Qdeferr, NELEMS(args), args);
}
emacs_value
define_constant(emacs_env *env, const char *name, emacs_value val,
const char *doc)
{
if (!name || !doc) {
return el_nil;
}
emacs_value Qeval = env->intern(env, "eval");
emacs_value Qlist = env->intern(env, "list");
emacs_value args[] = {env->intern(env, "defconst"), env->intern(env, name),
val, env->make_string(env, doc, strlen(doc))};
return env->funcall(env, Qeval, 1, (emacs_value[1]){env->funcall(
env, Qlist, NELEMS(args), args)});
}
emacs_value
bind_function(emacs_env *env, const char *name, emacs_value fn_val)
{
if (!name) {
return el_nil;
}
emacs_value args[] = {env->intern(env, name), fn_val};
env->funcall(env, env->intern(env, "fset"), NELEMS(args), args);
return args[0];
}
void
provide(emacs_env *env, const char *feature)
{
if (!feature) {
return;
}
emacs_value args[] = {env->intern(env, feature)};
env->funcall(env, env->intern(env, "provide"), NELEMS(args), args);
}