Skip to content

Commit fc92c2d

Browse files
committed
Also print function data when printing module functions.
This is especially useful in cases where modules only use a single entry point and use the data to dispatch to the actual function. Such a design is common for languages such as Go and C++. * src/emacs-module.c (module_function_data): New function. * src/print.c (print_vectorlike): Use it to print module function data if not NULL. (print_object): Adapt size of buffer. * test/data/emacs-module/mod-test.c (emacs_module_init): Pass some non-NULL data to ‘mod-test-sum’. (Fmod_test_sum): Check that correct data is passed through. * test/src/emacs-module-tests.el (mod-test-sum-test) (module-function-object): Adapt unit tests.
1 parent 9d38564 commit fc92c2d

File tree

5 files changed

+31
-5
lines changed

5 files changed

+31
-5
lines changed

src/emacs-module.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1098,6 +1098,12 @@ module_function_address (const struct Lisp_Module_Function *function)
10981098
return (module_funcptr) function->subr;
10991099
}
11001100

1101+
void *
1102+
module_function_data (const struct Lisp_Module_Function *function)
1103+
{
1104+
return function->data;
1105+
}
1106+
11011107

11021108
/* Helper functions. */
11031109

src/lisp.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4245,6 +4245,7 @@ extern Lisp_Object module_function_documentation
42454245
(struct Lisp_Module_Function const *);
42464246
extern module_funcptr module_function_address
42474247
(struct Lisp_Module_Function const *);
4248+
extern void *module_function_data (const struct Lisp_Module_Function *);
42484249
extern void module_finalize_function (const struct Lisp_Module_Function *);
42494250
extern void mark_modules (void);
42504251
extern void init_module_assertions (bool);

src/print.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1796,7 +1796,8 @@ print_vectorlike (Lisp_Object obj, Lisp_Object printcharfun, bool escapeflag,
17961796
case PVEC_MODULE_FUNCTION:
17971797
{
17981798
print_c_string ("#<module function ", printcharfun);
1799-
module_funcptr ptr = module_function_address (XMODULE_FUNCTION (obj));
1799+
const struct Lisp_Module_Function *function = XMODULE_FUNCTION (obj);
1800+
module_funcptr ptr = module_function_address (function);
18001801
char const *file;
18011802
char const *symbol;
18021803
dynlib_addr (ptr, &file, &symbol);
@@ -1815,6 +1816,19 @@ print_vectorlike (Lisp_Object obj, Lisp_Object printcharfun, bool escapeflag,
18151816
else
18161817
print_c_string (symbol, printcharfun);
18171818

1819+
void *data = module_function_data (function);
1820+
if (data != NULL)
1821+
{
1822+
uintptr_t ui = (uintptr_t) data;
1823+
1824+
/* In theory this assignment could lose info on pre-C99
1825+
hosts, but in practice it doesn't. */
1826+
uintmax_t up = ui;
1827+
1828+
int len = sprintf (buf, " with data 0x%"PRIxMAX, up);
1829+
strout (buf, len, len, printcharfun);
1830+
}
1831+
18181832
if (file != NULL)
18191833
{
18201834
print_c_string (" from ", printcharfun);
@@ -1838,7 +1852,7 @@ print_object (Lisp_Object obj, Lisp_Object printcharfun, bool escapeflag)
18381852
{
18391853
char buf[max (sizeof "from..to..in " + 2 * INT_STRLEN_BOUND (EMACS_INT),
18401854
max (sizeof " . #" + INT_STRLEN_BOUND (intmax_t),
1841-
max ((sizeof "at 0x"
1855+
max ((sizeof " with data 0x"
18421856
+ (sizeof (uintmax_t) * CHAR_BIT + 4 - 1) / 4),
18431857
40)))];
18441858
current_thread->stack_top = buf;

test/data/emacs-module/mod-test.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. */
2424

2525
#include <errno.h>
2626
#include <limits.h>
27+
#include <stdint.h>
2728
#include <stdio.h>
2829
#include <stdlib.h>
2930
#include <string.h>
@@ -86,6 +87,7 @@ static emacs_value
8687
Fmod_test_sum (emacs_env *env, ptrdiff_t nargs, emacs_value args[], void *data)
8788
{
8889
assert (nargs == 2);
90+
assert ((uintptr_t) data == 0x1234);
8991

9092
intmax_t a = env->extract_integer (env, args[0]);
9193
intmax_t b = env->extract_integer (env, args[1]);
@@ -587,7 +589,8 @@ emacs_module_init (struct emacs_runtime *ert)
587589
env->make_function (env, amin, amax, csym, doc, data))
588590

589591
DEFUN ("mod-test-return-t", Fmod_test_return_t, 1, 1, NULL, NULL);
590-
DEFUN ("mod-test-sum", Fmod_test_sum, 2, 2, "Return A + B\n\n(fn a b)", NULL);
592+
DEFUN ("mod-test-sum", Fmod_test_sum, 2, 2, "Return A + B\n\n(fn a b)",
593+
(void *) (uintptr_t) 0x1234);
591594
DEFUN ("mod-test-signal", Fmod_test_signal, 0, 0, NULL, NULL);
592595
DEFUN ("mod-test-throw", Fmod_test_throw, 0, 0, NULL, NULL);
593596
DEFUN ("mod-test-non-local-exit-funcall", Fmod_test_non_local_exit_funcall,

test/src/emacs-module-tests.el

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,9 @@
6060
(should (eq 0
6161
(string-match
6262
(concat "#<module function "
63-
"\\(at \\(0x\\)?[[:xdigit:]]+\\( from .*\\)?"
64-
"\\|Fmod_test_sum from .*\\)>")
63+
"\\(at \\(0x\\)?[[:xdigit:]]+ "
64+
"with data 0x1234\\( from .*\\)?"
65+
"\\|Fmod_test_sum with data 0x1234 from .*\\)>")
6566
(prin1-to-string (nth 1 descr)))))
6667
(should (= (nth 2 descr) 3)))
6768
(should-error (mod-test-sum "1" 2) :type 'wrong-type-argument)
@@ -97,6 +98,7 @@ changes."
9798
(rx bos "#<module function "
9899
(or "Fmod_test_sum"
99100
(and "at 0x" (+ hex-digit)))
101+
" with data 0x1234"
100102
(? " from " (* nonl) "mod-test" (* nonl) )
101103
">" eos)
102104
(prin1-to-string func)))))

0 commit comments

Comments
 (0)