From 99fe4c89a2f77235f38e586482f8fa08685e3b58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E4=BD=B3=E5=86=9B?= Date: Mon, 26 Jun 2017 17:34:24 +0800 Subject: [PATCH] Update 6.2.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixed 显示错误,增加可读性 --- 6.2.md | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/6.2.md b/6.2.md index 4251388..6c19fef 100644 --- a/6.2.md +++ b/6.2.md @@ -69,6 +69,7 @@ byref_compiletime_arginfo是一个arginfo结构体,我们在前面的章节中
原书中此处有arginfo在PHP4里的实现,被我略去了。
在Zend Engine 2 (PHP5+)中,arginfo的数据是由多个zend_arg_info结构体构成的数组,数组的每一个成员即每一个zend_arg_info结构体处理函数的一个参数。zend_arg_info结构体的定义如下: + ````c typedef struct _zend_arg_info { const char *name; /* 参数的名称*/ @@ -84,6 +85,7 @@ typedef struct _zend_arg_info { ```` 生成zend_arg_info结构的数组比较繁琐,为了方便PHP扩展开发者,内核已经准备好了相应的宏来专门处理此问题,首先先用一个宏函数来生成头部,然后用第二个宏生成具体的数据,最后用一个宏生成尾部代码。 + ````c #define ZEND_BEGIN_ARG_INFO(name, pass_rest_by_reference) ZEND_BEGIN_ARG_INFO_EX(name, pass_rest_by_reference, ZEND_RETURN_VALUE, -1) #define ZEND_BEGIN_ARG_INFO_EX(name, pass_rest_by_reference, return_reference, required_num_args) \ @@ -97,20 +99,27 @@ typedef struct _zend_arg_info { #define ZEND_ARG_ARRAY_INFO(pass_by_ref, name, allow_null) { #name, sizeof(#name)-1, NULL, 0, 1, allow_null, pass_by_ref, 0, 0 }, -#define ZEND_END_ARG_INFO() }; +#define ZEND_END_ARG_INFO() + +}; +```` + +``` //这里我们先看 ZEND_BEGIN_ARG_INFO(name, pass_rest_by_reference) ZEND_BEGIN_ARG_INFO_EX(name, pass_rest_by_reference, return_reference,required_num_args) +``` -```` 这两个宏函数的前两个参数的含义是一样的,name便是这个zend_arg_info数组变量的名字,这里我们定义它为:byref_compiletime_arginfo。pass_rest_by_reference如果被赋值为1,则代表着所有的参数默认都是需要以引用的方式传递的(在arginfo中单独声明的除外)。而对于ZEND_BEGIN_ARG_INFO_EX的后两个参数: + 接下来让我们看生成具体数据的宏: + ````c ZEND_ARG_PASS_INFO(by_ref) //强制所有参数使用引用的方式传递 @@ -130,7 +139,9 @@ ZEND_BEGIN_ARG_INFO(byref_compiletime_arginfo, 0) ZEND_END_ARG_INFO() ```` + 为了使我们的扩展能够兼容PHP4,还需要使用#ifdef进行特殊处理。 + ````c #ifdef ZEND_ENGINE_2 ZEND_BEGIN_ARG_INFO(byref_compiletime_arginfo, 0) @@ -141,7 +152,9 @@ static unsigned char byref_compiletime_arginfo[] = { 1, BYREF_FORCE }; #endif ```` + 我们copy一份ZEND_FUNCTION(byref_calltime)的实现,并重名成ZEND_FUNCTION(byref_compiletime)就行了。或者直接弄个ZEND_FALIAS就行了: + ````c ZEND_FALIAS(byref_compiletime,byref_calltime,byref_compiletime_arginfo)