<2024-01-07 周日>
emacs
源码分析(七)
这DEFUN
宏就像胶水一样,它把c
代码和emacs-lisp
代码给联系起来。但是DEFUN
宏看着怪恐怖的有没有!
/* This version of DEFUN declares a function prototype with the right
arguments, so we can catch errors with maxargs at compile-time. */
#define DEFUN(lname, fnname, sname, minargs, maxargs, intspec, doc) \
SUBR_SECTION_ATTRIBUTE \
static union Aligned_Lisp_Subr sname = \
{
{
{
PVEC_SUBR << PSEUDOVECTOR_AREA_BITS }, \
{
.a ## maxargs = fnname }, \
minargs, maxargs, lname, {
intspec}, 0}}; \
Lisp_Object fnname
自己动手把emacs
的DEFUN
宏抠出来
为了方便理解,我把DEFUN
宏给抠了出来,放在一个单独的工程里:ysouyno/t_emacs_defun,如果不想下载工程,本篇结尾会附上所有源码(仅一个文件,不到300
行代码)。
关于这个工程要注意:
- 仅适用于
windows
平台,为了编译方便,很多辅助宏能省略则省略。 - 设置
C++ Language Standard
为ISO C++20 Standard (/std:c++20)
。
挑了一个最简单的emacs-lisp
函数eq
:
DEFUN ("eq", Feq, Seq, 2, 2, 0,
doc: /* Return t if the two args are the same Lisp object. */
attributes: const)
(Lisp_Object obj1, Lisp_Object obj2)
{
if (EQ (obj1, obj2))
return Qt;
return Qnil;
}
展开后的eq
代码是:
static union Aligned_Lisp_Subr Seq =
{
{
{
PVEC_SUBR << PSEUDOVECTOR_AREA_BITS }, // struct Lisp_Subr::header
{
.a2 = Feq }, // struct Lisp_Subr::function
2, // struct Lisp_Subr::min_args
2, // struct Lisp_Subr::max_args
"eq", // struct Lisp_Subr::symbol_name
{
0}, // struct Lisp_Subr::intspec
0 // struct Lisp_Subr::doc
}
};
Lisp_Object Feq(Lisp_Object obj1, Lisp_Object obj2)
{
if (EQ(obj1, obj2))
return Qt;
return Qnil;
}
从上可得,DEFUN
有两个任务以(eq
函数为例):
- 声明一个静态变量
Seq
,它应该会将要用于emacs-lisp
代码中的某些地方,目前我还不清楚细节。 - 定义一个
c
函数Feq
。
我照着DEFUN
宏展开后样子定义了一个没有使用DEFUN
宏来定义的函数my-eq
,它可以正常工作:
// DEFUN("my-eq", ...)
static union Aligned_Lisp_Subr Smy_eq =
{
{
{
PVEC_SUBR << PSEUDOVECTOR_AREA_BITS },
{
.a2 = Fmy_eq },
2, 2, "my-eq", {
0}, 0} };
Lisp_Object Fmy_eq(Lisp_Object obj1, Lisp_Object obj2)
{
if (EQ(obj1, obj2))
return Qt;
return Qnil;
}
备注:
- 有一个
EXFUN
宏要关注一下,这个代码我也抠出来了,它是用于声明Feq
,否则编译器要报怨的:
error C2065: 'Feq':