diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..bac7452 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,3 @@ +*.h linguist-language=c +*.c linguist-language=c +*.phpt linguist-language=c diff --git a/.gitignore b/.gitignore index 2e38fab..44687ae 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,3 @@ # Created by .ignore support plugin (hsz.mobi) .idea CMakeLists.txt -vendor diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index fa4c536..0000000 --- a/.travis.yml +++ /dev/null @@ -1,32 +0,0 @@ -language: php - -compiler: - - gcc - - clang - -os: - - linux - -php: - - 7.0 - - 7.1 - - 7.2 - - 7.3 - -notifications: - email: bob_zy@yeah.net - -install: - - composer update - -before_script: - - phpize && ./configure && make && make install - - echo "extension = phptars.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini - -script: - - cd testcases - - php ../vendor/bin/phpunit test.php - - php ../vendor/bin/phpunit testTARSClient.php - - php ../vendor/bin/phpunit testTARSServer.php - - diff --git a/README.md b/README.md index d124f32..e0ad02c 100644 --- a/README.md +++ b/README.md @@ -1,277 +1,262 @@ -[![Build Status](https://travis-ci.org/TarsPHP/tars-extension.svg?branch=master)](https://travis-ci.org/TarsPHP/tars-extension) - -# phptars extension Introduction - -## PHP extension capability description - -In order to realize the whole system of tars package and unpacking and tup encoding and decoding in the extension, PHP extension mainly does three things: - -* All data structures of tar are mapped with extension types - -* Three complex types of tars are mapped with special extension types - -* It provides the ability of package, unpack, code and decode of tup and tar. - - - -## Mapping of basic types - -Here is our mapping of basic types: -``` - bool => \TARS::BOOL - char => \TARS::CHAR - uint8 => \TARS::UINT8 - short => \TARS::SHORT - uint16 => \TARS::UINT16 - float => \TARS::FLOAT - double => \TARS::DOUBLE - int32 => \TARS::INT32 - uint32 => \TARS::UINT32 - int64 => \TARS::INT64 - string => \TARS::STRING - vector => \TARS::VECTOR - map => \TARS::MAP - struct => \TARS::STRUCT +# phptars (version 2.3.0) 扩展使用说明 + +**特别说明:本扩展仅支持 php >= 8.0 以上的版本** + +未对 PHP 7.x 版本做过测试,不确定是否支持. + +由于官方项目维护不及时,暂不支持 PHP8,所以我创建了这个版本. + +官方项目地址: https://github.com/TarsPHP/tars-extension + +## 零、安装指令参考 + +```shell +cd /root/ && \ +wget https://github.com/lanlin/phptars-php8/archive/refs/tags/2.3.0.tar.gz && \ +tar -zxvf 2.3.0.tar.gz && \ +cd ./phptars-php8-2.3.0/ && \ +phpize --clean && \ +phpize && \ +./configure --enable-phptars && \ +make clean && \ +make && \ +make test && \ +make install && \ +mkdir -p /root/phptars && \ +cp ./tars2php.php /root/phptars && \ +cd /root/ && \ +rm -rf ./phptars-php8* ``` -When we need to identify specific variable types, we need to use these basic types, which are constants, from 1-14. - - - -## Mapping of complex types - -There are some special packaging and unpacking mechanisms for vector, map and struct. Therefore, special data types need to be introduced: - -Vector: +最后将 `extension=phptars.so` 添加到你的 `php.ini` 中即可。 + +## 一、php 扩展能力说明 + +为了在扩展中实现 tars 打包解包和 tup 编码解码的全部体系, 所以 php 扩展主要做了三件事情: + +* 将 tars 的所有数据结构进行了扩展类型的映射 +* 将 tars 的三种复杂类型进行了特殊的扩展类型的映射 +* 提供了 tup 和 tars 协议的打包解包与编码解码的能力。 + +## 二、基本类型的映射 + +如下是我们对基本类型的映射: +```php + bool => \TARS::BOOL, + char => \TARS::CHAR, + uint8 => \TARS::UINT8, + short => \TARS::SHORT, + uint16 => \TARS::UINT16, + float => \TARS::FLOAT, + double => \TARS::DOUBLE, + int32 => \TARS::INT32, + uint32 => \TARS::UINT32, + int64 => \TARS::INT64, + string => \TARS::STRING, + vector => \TARS::VECTOR, + map => \TARS::MAP, + struct => \TARS::STRUCT, ``` -vector => \TARS_VECTOR - -It has two member functions, push back() and push back() +当我们需要标识具体的变量类型的时候, 就需要用到这些基本的类型了, 这些类型都是常量, 从 1-14。 -The input parameter depends on what type of array the vector itself contains +## 三、复杂类型的映射 +针对 vector、map、struct 三种基本的类型, 有一些特殊的打包解包的机制, 因此需要引入特别的数据类型: +### 1. vector => \TARS_VECTOR: -For example: +它同时具有两个成员函数 pushBack() 和 push_back(). +入参为取决于 vector 本身是包含什么类型的数组 -$shorts = ["test1","test2"]; +```php +$shorts = ['test1', 'test2']; -$vector = new \ tars \ vector (\ tars:: String); / / define a vector of type string +// 定义一个 string 类型的 vector +$vector = new \TARS_VECTOR(\TARS::STRING); -foreach ($shorts as $short) { - -$vector - > pushback ($short); / / press test1 and test2 into vector - } -``` - -map: +foreach ($shorts as $short) +{ + // 依次吧 test1,test2 两个元素,压 入vector 中 + $vector->pushBack($short); +} ``` - map => \TARS_MAP -It has two member functions, push back() and push back() -The input parameter depends on what type the map itself contains +### 2. map => \TARS_MAP: +它同时具有两个成员函数 pushBack() 和 push_back(). +入参为取决于 map 本身包含什么类型 +```php +$strings = [ ['test1' => 1], ['test2' => 2] ]; -For example: - $strings = [["test1"=>1],["test2"=>2]]; - $map = new \TARS_MAP(\TARS::STRING,\TARS::INT64); //define one key为string,value is int64的map - foreach ($strings as $string) { - $map->pushBack($string); //Press two elements into the map in turn, and notice that pushback receives an array, and that array has only one element - } -``` +// 定义一个 key 为 string, value 是 int64 的 map +$map = new \TARS_MAP(\TARS::STRING,\TARS::INT64); -struct: +foreach ($strings as $string) +{ + // 依次把两个元素压入 map 中,注意 pushBack 接收一个 array,且 array 只有一个元素 + $map->pushBack($string); +} ``` - struct => \TARS_Struct - The constructor of struct is special. It takes two parameters, classname and classfields - -The first describes the name, and the second describes the information of the variables in struct - - - -For example: - class SimpleStruct extends \TARS_Struct { - const ID = 0; //TARS文件中每个struct的tag - const COUNT = 1; - - public $id; //strcut The value of each element is saved here - public $count; - - protected static $fields = array( - self::ID => array( - 'name'=>'id',//struct The value of each element is saved here - 'required'=>true,//struct Whether each element is required, corresponding to the require and optional in the tars file - 'type'=>\TARS::INT64,//struct every element type - ), - self::COUNT => array( - 'name'=>'count', - 'required'=>true, - 'type'=>\TARS::UINT8, - ), - ); - - public function __construct() { - parent::__construct('App_Server_Servant.SimpleStruct', self::$fields); - } - } -``` - -## Package and unpack and code decoding - -As the core function of the extension, it provides the ability of coding, decoding, packaging and unpacking of tars - - - -### Pack and unpack +### 3. struct => \TARS_Struct: + +struct 的构造函数比较特殊, 接收 classname 和 classfields 两个参数. +第一个描述名称,第二个描述struct内的变量的信息 + +```php +class SimpleStruct extends \TARS_Struct +{ + const ID = 0; // TARS 文件中每个 struct 的 tag + const COUNT = 1; + + public $id; // strcut 中每个元素的值保存在这里 + public $count; + + protected static $fields = + [ + self::ID => + [ + 'name' => 'id', // struct 中每个元素的名称 + 'type' => \TARS::INT64, // struct 中每个元素的类型 + 'required' => true, // struct 中每个元素是否必须,对应 tars 文件中的 require 和 optional + ], + self::COUNT => + [ + 'name' => 'count', + 'type' => \TARS::UINT8, + 'required' => true, + ], + ]; + + public function __construct() + { + parent::__construct('App_Server_Servant.SimpleStruct', self::$fields); + } +} ``` -//Output binary buf for basic types of packaging and unpacking methods - -//There are only 1 and 3 versions of iversion. In version 1, $nameortagnum needs to be passed in tagnum, the first parameter in the method is 1, the second parameter is 2, and so on - -//In version 3, $nameortagnum needs to pass in name and parameter name - -$buf = \TASAPI::put*($nameOrTagNum, $value, $iVersion = 3) -$value = \TUPAPI::get*($nameOrTagNum, $buf, $isRequire = false, $iVersion = 3) +## 四、打包解包与编码解码 +作为扩展的核心功能, 就是提供 tars 的编解码和打包解包的能力: +### 1. 打包解包 -//For struct, when the object is transferred and the result is returned, it is returned in the form of an array. Its elements correspond to the member variables of the class one by one +```php +// 针对基本类型的打包和解包的方法, 输出二进制 buf +// iVersion 只有 1 和 3 两个版本,1 版本时 $nameOrTagNum 需要传入 tagNum, 方法里面第一个参数为 1 第二个参数为 2 以此类推 +// 3 版本时 $nameOrTagNum 需要传入 name, 参数名 +$buf = \TASAPI::put*($nameOrTagNum, $value, $iVersion = 3); +$value = \TUPAPI::get*($nameOrTagNum, $buf, $isRequire = false, $iVersion = 3); -$buf = \TUPAPI::putStruct($nameOrTagNum, $clazz, $iVersion = 3) +// 针对 struct, 传输对象, 返回结果的时候, 以数组的方式返回, 其元素与类的成员变量一一对应 +$buf = \TUPAPI::putStruct($nameOrTagNum, $clazz, $iVersion = 3); +$result = \TUPAPI::getStruct($nameOrTagNum, $clazz, $buf, $isRequire = false, $iVersion = 3); -$result = \TUPAPI::getStruct($nameOrTagNum, $clazz, $buf, $isRequire = false, $iVersion = 3) +// 针对 vector, 传入完成 pushBack 的 vector +$buf = \TUPAPI::putVector($nameOrTagNum, TARS_Vector $clazz, $iVersion = 3); +$value = \TUPAPI::getVector($nameOrTagNum, TARS_Vector $clazz, $buf, $isRequire = false, $iVersion = 3); +// 针对 map, 传入完成 pushBack 的 map +$buf = \TUPAPI::putMap($nameOrTagNum, TARS_Map $clazz, $iVersion = 3); +$value = \TUPAPI::getMap($nameOrTagNum, TARS_Map $clazz, $buf, $isRequire = false, $iVersion = 3); - -//For vector, pass in the vector that completes pushback - -$buf = \TUPAPI::putVector($nameOrTagNum, TARS_Vector $clazz, $iVersion = 3) - -$value = \TUPAPI::getVector($nameOrTagNum, TARS_Vector $clazz, $buf, $isRequire = false, $iVersion = 3) - - - -//For map, pass in the map to complete pushback - -$buf = \TUPAPI::putMap($nameOrTagNum, TARS_Map $clazz, $iVersion = 3) - -$value = \TUPAPI::getMap($nameOrTagNum, TARS_Map $clazz, $buf, $isRequire = false, $iVersion = 3) - - - -//You need to put the above packed data together for coding - -$inbuf_arr[$nameOrTagNum] = $buf - +// 需要将上述打好包的数据放在一起用来编码 +$inbuf_arr[$nameOrTagNum] = $buf; ``` +### 2. 编码解码 -### Encoding and decoding +```php +// 针对 tup 协议 (iVersion=3) 的情况: +// 这种情况下客户端发包用 encode 编码,服务端收包用 decode 解码,服务端回包用 encode 编码,客户端收包用 decode 解码 -``` -//For the case of tup protocol (iversion = 3): +// 进行tup协议的编码,返回结果可以用来传输、持久化 +$reqBuffer = \TUPAPI::encode( + $iVersion = 3, + $iRequestId, + $servantName, + $funcName, + $cPacketType = 0, + $iMessageType = 0, + $iTimeout, + $context = [], + $statuses = [], + $bufs +); + +// 进行 tup 协议的解码 +$ret = \TUPAPI::decode($respBuffer, $iVersion = 3); +$buf = $ret['sBuffer']; +$code = $ret['iRet']; -//In this case, the client uses encode code to send the contract, the server uses decode to decode the received package, the server uses encode to encode the returned package, and the client uses decode to decode the received package +// 针对 tars 协议 (iVersion=1) 的情况: +// 这种情况下客户端发包用 encode 编码,服务端收包用 decodeReqPacket 解码,服务端回包用 encodeRspPacket 编码,客户端收包用 decode 解码 -//Code the tup protocol, and the returned results can be used for transmission and persistence -$reqBuffer = \TUPAPI::encode( - $iVersion = 3, - $iRequestId, - $servantName, - $funcName, - $cPacketType=0, - $iMessageType=0, - $iTimeout, - $context=[], - $statuses=[], - $bufs) -// Decoding the tup protocol -$ret = \TUPAPI::decode($respBuffer, $iVersion = 3) -$code = $ret['iRet'] -$buf = $ret['sBuffer'] - -//For the tar protocol (iversion = 1): - -//In this case, the client uses encode to code the contract, the server uses decodereqpacket to decode the received package, the server uses encoderssppacket to encode the returned package, and the client uses decode to decode the received package - -//Client contract +// 客户端发包 $reqBuffer = \TUPAPI::encode( - $iVersion = 1, - $iRequestId, - $servantName, - $funcName, - $cPacketType=0, - $iMessageType=0, - $iTimeout, - $context=[], - $statuses=[], - $bufs) -//Server receiving and unpacking -$ret = \TUPAPI::decodeReqPacket($respBuffer) -$code = $ret['iRet'] -$buf = $ret['sBuffer'] - -//Service end return package encodeRspPacket + $iVersion = 1, + $iRequestId, + $servantName, + $funcName, + $cPacketType = 0, + $iMessageType = 0, + $iTimeout, + $context = [], + $statuses = [], + $bufs +); + +// 服务端收包,解包 +$ret = \TUPAPI::decodeReqPacket($respBuffer); +$buf = $ret['sBuffer']; +$code = $ret['iRet']; + +// 服务端回包,打包 encodeRspPacket $reqBuffer = \TUPAPI::encodeRspPacket( - $iVersion = 1, - $cPacketType, - $iMessageType, - $iRequestid, - $iRet=0, - $sResultDesc='', - $bufs, - $statuses=[]) + $iVersion = 1, + $cPacketType, + $iMessageType, + $iRequestid, + $iRet = 0, + $sResultDesc = '', + $bufs, + $statuses = [] +); -//Client receiving and unpacking -$ret = \TUPAPI::decode($respBuffer, $iVersion = 1) -$code = $ret['iRet'] -$buf = $ret['sBuffer'] +// 客户端收包,解包 +$ret = \TUPAPI::decode($respBuffer, $iVersion = 1); +$buf = $ret['sBuffer']; +$code = $ret['iRet']; ``` -For more extensive use of different types of structures, please refer to tests/ - - -## Instructions for using tars2php - -Please refer to the documentation under the tars2php module: - -[detailed description](https://github.com/tarphp/tars2php/blob/master/readme.md) - - -## Test case - - -### Test case of phpunite version - -For the common use of extensions, test cases are added, which are located in the / ext / testcases folder, - -When testing, you only need to execute `php phpunit-4.8.36.phar test.php` to complete the execution of all test cases. It covers: - -* All basic types of packaging, unpacking and coding tests - -* Test of simple struct type package, unpacking and coding - -* Test of simple vector type package, unpack and code - -* The test of package, unpack and code of simple map type +对于不同类型的结构的打包解包的更丰富的使用请参考 tests/ -* Test of packaging, unpacking and encoding of complex vector types (including non basic data types) +## 五、tars2php(自动生成php类工具)使用说明 -* Test of packaging, unpacking and encoding of complex map types (including non basic data types) +请参见 tars2php 模块下的文档说明: +[详细说明](https://github.com/TarsPHP/tars2php/blob/master/README.md) -* Package, unpack and code test of complex struct types (nested vector and map) +## 六、测试用例 +### 1. phpunit 版本的测试用例 +针对扩展的常见使用, 增加了测试用例, 位于 /ext/testcases 文件夹下, +测试时只需要执行 `phpunit TestTars.php` 即可完成所有测试用例的执行。其中覆盖到了: -In addition, testtarsclient.php and testtarsserver.php are the test cases of client contracting, server unpacking, server callback and client unpacking under the tars protocol (iversion = 1). +* 所有基本类型的打包解包和编码的测试 +* 简单 struct 类型打包解包和编码的测试 +* 简单 vector 类型的打包解包和编码的测试 +* 简单 map 类型的打包解包和编码的测试 +* 复杂 vector 类型(包含非基本数据类型)的打包解包和编码的测试 +* 复杂 map 类型(包含非基本数据类型)的打包解包和编码的测试 +* 复杂 struct 类型(嵌套 vector 和 map)的打包解包和编码的测试 +另外 TestTarsClient.php 和 TestTarsServer.php 是 tars 协议(iVersion=1)情况下客户端发包,服务端解包 和 服务端回包,客户端解包的测试用例。 -Note that you need to download phpunit's executable or use the pre installed phpunit tool directly for unit testing. +注意,需要自行下载 phpunit 的可执行文件,或直接使用预先安装好的 phpunit 工具,进行单元测试。 -### At the same time, it points out the test cases of PHPT version +### 2. 同时指出 phpt 版本的测试用例 -After the extension is installed, execute make test. \ No newline at end of file +安装完成扩展后,执行 `make test` 即可。 \ No newline at end of file diff --git a/README.zh.md b/README.zh.md deleted file mode 100644 index fee164c..0000000 --- a/README.zh.md +++ /dev/null @@ -1,202 +0,0 @@ -[![Build Status](https://travis-ci.org/TarsPHP/tars-extension.svg?branch=master)](https://travis-ci.org/TarsPHP/tars-extension) - -# phptars 扩展使用说明 - -## php扩展能力说明 -为了在扩展中实现tars打包解包和tup编码解码的全部体系,所以php扩展主要做了三件事情: -* 将tars的所有数据结构进行了扩展类型的映射 -* 将tars的三种复杂类型进行了特殊的扩展类型的映射 -* 提供了tup和tars协议的打包解包与编码解码的能力。 - -## 基本类型的映射 -如下是我们对基本类型的映射: -``` - bool => \TARS::BOOL - char => \TARS::CHAR - uint8 => \TARS::UINT8 - short => \TARS::SHORT - uint16 => \TARS::UINT16 - float => \TARS::FLOAT - double => \TARS::DOUBLE - int32 => \TARS::INT32 - uint32 => \TARS::UINT32 - int64 => \TARS::INT64 - string => \TARS::STRING - vector => \TARS::VECTOR - map => \TARS::MAP - struct => \TARS::STRUCT -``` -当我们需要标识具体的变量类型的时候,就需要用到这些基本的类型了,这些类型都是常量,从1-14。 - -## 复杂类型的映射 -针对vector、map、struct三种基本的类型,有一些特殊的打包解包的机制,因此需要引入特别的数据类型: -vector: -``` - vector => \TARS_VECTOR - 它同时具有两个成员函数pushBack()和push_back() - 入参为取决于vector本身是包含什么类型的数组 - - 例如: - $shorts = ["test1","test2"]; - $vector = new \TARS_VECTOR(\TARS::STRING); //定义一个string类型的vector - foreach ($shorts as $short) { - $vector->pushBack($short); //依次吧test1,test2两个元素,压入vector中 - } -``` -map: -``` - map => \TARS_MAP - 它同时具有两个成员函数pushBack()和push_back() - 入参为取决于map本身包含什么类型 - - 例如: - $strings = [["test1"=>1],["test2"=>2]]; - $map = new \TARS_MAP(\TARS::STRING,\TARS::INT64); //定义一个key为string,value是int64的map - foreach ($strings as $string) { - $map->pushBack($string); //依次把两个元素压入map中,注意pushBack接收一个array,且array只有一个元素 - } -``` - -struct: -``` - struct => \TARS_Struct - struct的构造函数比较特殊,接收classname和classfields两个参数 - 第一个描述名称,第二个描述struct内的变量的信息 - - 例如: - class SimpleStruct extends \TARS_Struct { - const ID = 0; //TARS文件中每个struct的tag - const COUNT = 1; - - public $id; //strcut中每个元素的值保存在这里 - public $count; - - protected static $fields = array( - self::ID => array( - 'name'=>'id',//struct中每个元素的名称 - 'required'=>true,//struct中每个元素是否必须,对应tars文件中的require和optional - 'type'=>\TARS::INT64,//struct中每个元素的类型 - ), - self::COUNT => array( - 'name'=>'count', - 'required'=>true, - 'type'=>\TARS::UINT8, - ), - ); - - public function __construct() { - parent::__construct('App_Server_Servant.SimpleStruct', self::$fields); - } - } - -``` - -## 打包解包与编码解码 -作为扩展的核心功能,就是提供tars的编解码和打包解包的能力: - -### 打包解包 -``` -// 针对基本类型的打包和解包的方法,输出二进制buf -// iVersion只有1和3两个版本,1版本时$nameOrTagNum需要传入tagNum,方法里面第一个参数为1第二个参数为2以此类推 -// 3版本时$nameOrTagNum需要传入name,参数名 -$buf = \TASAPI::put*($nameOrTagNum, $value, $iVersion = 3) -$value = \TUPAPI::get*($nameOrTagNum, $buf, $isRequire = false, $iVersion = 3) - -// 针对struct,传输对象,返回结果的时候,以数组的方式返回,其元素与类的成员变量一一对应 -$buf = \TUPAPI::putStruct($nameOrTagNum, $clazz, $iVersion = 3) -$result = \TUPAPI::getStruct($nameOrTagNum, $clazz, $buf, $isRequire = false, $iVersion = 3) - -// 针对vector,传入完成pushBack的vector -$buf = \TUPAPI::putVector($nameOrTagNum, TARS_Vector $clazz, $iVersion = 3) -$value = \TUPAPI::getVector($nameOrTagNum, TARS_Vector $clazz, $buf, $isRequire = false, $iVersion = 3) - -// 针对map,传入完成pushBack的map -$buf = \TUPAPI::putMap($nameOrTagNum, TARS_Map $clazz, $iVersion = 3) -$value = \TUPAPI::getMap($nameOrTagNum, TARS_Map $clazz, $buf, $isRequire = false, $iVersion = 3) - -// 需要将上述打好包的数据放在一起用来编码 -$inbuf_arr[$nameOrTagNum] = $buf -``` -### 编码解码 -``` -//针对tup协议(iVersion=3)的情况: -//这种情况下客户端发包用encode编码,服务端收包用decode解码,服务端回包用encode编码,客户端收包用decode解码 -// 进行tup协议的编码,返回结果可以用来传输、持久化 -$reqBuffer = \TUPAPI::encode( - $iVersion = 3, - $iRequestId, - $servantName, - $funcName, - $cPacketType=0, - $iMessageType=0, - $iTimeout, - $context=[], - $statuses=[], - $bufs) -// 进行tup协议的解码 -$ret = \TUPAPI::decode($respBuffer, $iVersion = 3) -$code = $ret['iRet'] -$buf = $ret['sBuffer'] - -//针对tars协议(iVersion=1)的情况: -//这种情况下客户端发包用encode编码,服务端收包用decodeReqPacket解码,服务端回包用encodeRspPacket编码,客户端收包用decode解码 - -//客户端发包 -$reqBuffer = \TUPAPI::encode( - $iVersion = 1, - $iRequestId, - $servantName, - $funcName, - $cPacketType=0, - $iMessageType=0, - $iTimeout, - $context=[], - $statuses=[], - $bufs) -//服务端收包,解包 -$ret = \TUPAPI::decodeReqPacket($respBuffer) -$code = $ret['iRet'] -$buf = $ret['sBuffer'] - -//服务端回包,打包encodeRspPacket -$reqBuffer = \TUPAPI::encodeRspPacket( - $iVersion = 1, - $cPacketType, - $iMessageType, - $iRequestid, - $iRet=0, - $sResultDesc='', - $bufs, - $statuses=[]) - -//客户端收包,解包 -$ret = \TUPAPI::decode($respBuffer, $iVersion = 1) -$code = $ret['iRet'] -$buf = $ret['sBuffer'] -``` - -对于不同类型的结构的打包解包的更丰富的使用请参考tests/ - -## tars2php(自动生成php类工具)使用说明 -请参见tars2php模块下的文档说明: -[详细说明](https://github.com/TarsPHP/tars2php/blob/master/README.md) - -## 测试用例 - -### phpunite版本的测试用例 -针对扩展的常见使用,增加了测试用例,位于/ext/testcases文件夹下, -测试时只需要执行`php phpunit-4.8.36.phar test.php` 即可完成所有测试用例的执行。其中覆盖到了: -* 所有基本类型的打包解包和编码的测试 -* 简单struct类型打包解包和编码的测试 -* 简单vector类型的打包解包和编码的测试 -* 简单map类型的打包解包和编码的测试 -* 复杂vector类型(包含非基本数据类型)的打包解包和编码的测试 -* 复杂map类型(包含非基本数据类型)的打包解包和编码的测试 -* 复杂struct类型(嵌套vector和map)的打包解包和编码的测试 - -另外testTARSClient.php和testTARSServer.php是tars协议(iVersion=1)情况下客户端发包,服务端解包 和 服务端回包,客户端解包的测试用例。 - -注意,需要自行下载phpunit的可执行文件,或直接使用预先安装好的phpunit工具,进行单元测试。 - -### 同时指出phpt版本的测试用例 -安装完成扩展后,执行make test即可。 diff --git a/auto7.sh b/auto7.sh deleted file mode 100644 index 400fa52..0000000 --- a/auto7.sh +++ /dev/null @@ -1,11 +0,0 @@ -#!/bin/sh - -phpize=/data/env/runtime/php-7.1.7/bin/phpize -$phpize --clean -$phpize -phpconfig=/data/env/runtime/php-7.1.7/bin/php-config -./configure --enable-phptars --with-php-config=$phpconfig -make -make install -$phpize --clean - diff --git a/composer.json b/composer.json deleted file mode 100644 index c95d51c..0000000 --- a/composer.json +++ /dev/null @@ -1,19 +0,0 @@ -{ - "name": "phptars/tars-extension", - "description": "tars的扩展", - "version":"2.2.5", - "authors": [ - { - "name": "Chen Liang", - "email": "cedricliang21@gmail.com" - }, - { - "name": "Zhang Yong", - "email": "bob_zy@yeah.net" - } - ], - "minimum-stability": "dev", - "require": { - "phpunit/phpunit": "5 - 7" - } -} diff --git a/composer.lock b/composer.lock deleted file mode 100644 index 3f6456f..0000000 --- a/composer.lock +++ /dev/null @@ -1,1482 +0,0 @@ -{ - "_readme": [ - "This file locks the dependencies of your project to a known state", - "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", - "This file is @generated automatically" - ], - "content-hash": "256a4f591033b43906572231dae60250", - "packages": [ - { - "name": "doctrine/instantiator", - "version": "dev-master", - "source": { - "type": "git", - "url": "https://github.com/doctrine/instantiator.git", - "reference": "7c71fc2932158d00f24f10635bf3b3b8b6ee5b68" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/doctrine/instantiator/zipball/7c71fc2932158d00f24f10635bf3b3b8b6ee5b68", - "reference": "7c71fc2932158d00f24f10635bf3b3b8b6ee5b68", - "shasum": "" - }, - "require": { - "php": "^7.1" - }, - "require-dev": { - "doctrine/coding-standard": "^6.0", - "ext-pdo": "*", - "ext-phar": "*", - "phpbench/phpbench": "^0.13", - "phpstan/phpstan-phpunit": "^0.11", - "phpstan/phpstan-shim": "^0.11", - "phpunit/phpunit": "^7.0" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "1.2.x-dev" - } - }, - "autoload": { - "psr-4": { - "Doctrine\\Instantiator\\": "src/Doctrine/Instantiator/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Marco Pivetta", - "email": "ocramius@gmail.com", - "homepage": "http://ocramius.github.com/" - } - ], - "description": "A small, lightweight utility to instantiate objects in PHP without invoking their constructors", - "homepage": "https://www.doctrine-project.org/projects/instantiator.html", - "keywords": [ - "constructor", - "instantiate" - ], - "time": "2019-07-02T13:37:32+00:00" - }, - { - "name": "myclabs/deep-copy", - "version": "1.x-dev", - "source": { - "type": "git", - "url": "https://github.com/myclabs/DeepCopy.git", - "reference": "9012edbd1604a93cee7e7422d07a2c5776c56e0c" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/myclabs/DeepCopy/zipball/9012edbd1604a93cee7e7422d07a2c5776c56e0c", - "reference": "9012edbd1604a93cee7e7422d07a2c5776c56e0c", - "shasum": "" - }, - "require": { - "php": "^7.1" - }, - "replace": { - "myclabs/deep-copy": "self.version" - }, - "require-dev": { - "doctrine/collections": "^1.0", - "doctrine/common": "^2.6", - "phpunit/phpunit": "^7.1" - }, - "type": "library", - "autoload": { - "psr-4": { - "DeepCopy\\": "src/DeepCopy/" - }, - "files": [ - "src/DeepCopy/deep_copy.php" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "description": "Create deep copies (clones) of your objects", - "keywords": [ - "clone", - "copy", - "duplicate", - "object", - "object graph" - ], - "time": "2019-08-26T15:40:39+00:00" - }, - { - "name": "phar-io/manifest", - "version": "1.0.3", - "source": { - "type": "git", - "url": "https://github.com/phar-io/manifest.git", - "reference": "7761fcacf03b4d4f16e7ccb606d4879ca431fcf4" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/phar-io/manifest/zipball/7761fcacf03b4d4f16e7ccb606d4879ca431fcf4", - "reference": "7761fcacf03b4d4f16e7ccb606d4879ca431fcf4", - "shasum": "" - }, - "require": { - "ext-dom": "*", - "ext-phar": "*", - "phar-io/version": "^2.0", - "php": "^5.6 || ^7.0" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "1.0.x-dev" - } - }, - "autoload": { - "classmap": [ - "src/" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "BSD-3-Clause" - ], - "authors": [ - { - "name": "Arne Blankerts", - "email": "arne@blankerts.de", - "role": "Developer" - }, - { - "name": "Sebastian Heuer", - "email": "sebastian@phpeople.de", - "role": "Developer" - }, - { - "name": "Sebastian Bergmann", - "email": "sebastian@phpunit.de", - "role": "Developer" - } - ], - "description": "Component for reading phar.io manifest information from a PHP Archive (PHAR)", - "time": "2018-07-08T19:23:20+00:00" - }, - { - "name": "phar-io/version", - "version": "2.0.1", - "source": { - "type": "git", - "url": "https://github.com/phar-io/version.git", - "reference": "45a2ec53a73c70ce41d55cedef9063630abaf1b6" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/phar-io/version/zipball/45a2ec53a73c70ce41d55cedef9063630abaf1b6", - "reference": "45a2ec53a73c70ce41d55cedef9063630abaf1b6", - "shasum": "" - }, - "require": { - "php": "^5.6 || ^7.0" - }, - "type": "library", - "autoload": { - "classmap": [ - "src/" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "BSD-3-Clause" - ], - "authors": [ - { - "name": "Arne Blankerts", - "email": "arne@blankerts.de", - "role": "Developer" - }, - { - "name": "Sebastian Heuer", - "email": "sebastian@phpeople.de", - "role": "Developer" - }, - { - "name": "Sebastian Bergmann", - "email": "sebastian@phpunit.de", - "role": "Developer" - } - ], - "description": "Library for handling version information and constraints", - "time": "2018-07-08T19:19:57+00:00" - }, - { - "name": "phpdocumentor/reflection-common", - "version": "2.0.0", - "source": { - "type": "git", - "url": "https://github.com/phpDocumentor/ReflectionCommon.git", - "reference": "63a995caa1ca9e5590304cd845c15ad6d482a62a" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/phpDocumentor/ReflectionCommon/zipball/63a995caa1ca9e5590304cd845c15ad6d482a62a", - "reference": "63a995caa1ca9e5590304cd845c15ad6d482a62a", - "shasum": "" - }, - "require": { - "php": ">=7.1" - }, - "require-dev": { - "phpunit/phpunit": "~6" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "2.x-dev" - } - }, - "autoload": { - "psr-4": { - "phpDocumentor\\Reflection\\": "src/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Jaap van Otterdijk", - "email": "opensource@ijaap.nl" - } - ], - "description": "Common reflection classes used by phpdocumentor to reflect the code structure", - "homepage": "http://www.phpdoc.org", - "keywords": [ - "FQSEN", - "phpDocumentor", - "phpdoc", - "reflection", - "static analysis" - ], - "time": "2018-08-07T13:53:10+00:00" - }, - { - "name": "phpdocumentor/reflection-docblock", - "version": "dev-master", - "source": { - "type": "git", - "url": "https://github.com/phpDocumentor/ReflectionDocBlock.git", - "reference": "8fcadfe5f85c38705151c9ab23b4781f23e6a70e" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/phpDocumentor/ReflectionDocBlock/zipball/8fcadfe5f85c38705151c9ab23b4781f23e6a70e", - "reference": "8fcadfe5f85c38705151c9ab23b4781f23e6a70e", - "shasum": "" - }, - "require": { - "php": ">=7.1", - "phpdocumentor/type-resolver": "^0", - "webmozart/assert": "^1" - }, - "require-dev": { - "doctrine/instantiator": "^1", - "mockery/mockery": "^1" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "5.x-dev" - } - }, - "autoload": { - "psr-4": { - "phpDocumentor\\Reflection\\": "src" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Mike van Riel", - "email": "me@mikevanriel.com" - } - ], - "description": "With this component, a library can provide support for annotations via DocBlocks or otherwise retrieve information that is embedded in a DocBlock.", - "time": "2019-06-15T20:45:01+00:00" - }, - { - "name": "phpdocumentor/type-resolver", - "version": "0.7.x-dev", - "source": { - "type": "git", - "url": "https://github.com/phpDocumentor/TypeResolver.git", - "reference": "6f6f66c1d4e14e9b0ad124cae85864dfa03104c7" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/phpDocumentor/TypeResolver/zipball/6f6f66c1d4e14e9b0ad124cae85864dfa03104c7", - "reference": "6f6f66c1d4e14e9b0ad124cae85864dfa03104c7", - "shasum": "" - }, - "require": { - "php": ">=7.1", - "phpdocumentor/reflection-common": "~2.0.0-beta1" - }, - "require-dev": { - "mockery/mockery": "~1", - "phpunit/phpunit": "~6" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "0.x-dev" - } - }, - "autoload": { - "psr-4": { - "phpDocumentor\\Reflection\\": "src" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Mike van Riel", - "email": "me@mikevanriel.com" - } - ], - "description": "A PSR-5 based resolver of Class names, Types and Structural Element Names", - "time": "2019-08-22T17:55:41+00:00" - }, - { - "name": "phpspec/prophecy", - "version": "1.9.0", - "source": { - "type": "git", - "url": "https://github.com/phpspec/prophecy.git", - "reference": "f6811d96d97bdf400077a0cc100ae56aa32b9203" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/phpspec/prophecy/zipball/f6811d96d97bdf400077a0cc100ae56aa32b9203", - "reference": "f6811d96d97bdf400077a0cc100ae56aa32b9203", - "shasum": "" - }, - "require": { - "doctrine/instantiator": "^1.0.2", - "php": "^5.3|^7.0", - "phpdocumentor/reflection-docblock": "^2.0|^3.0.2|^4.0|^5.0", - "sebastian/comparator": "^1.1|^2.0|^3.0", - "sebastian/recursion-context": "^1.0|^2.0|^3.0" - }, - "require-dev": { - "phpspec/phpspec": "^2.5|^3.2", - "phpunit/phpunit": "^4.8.35 || ^5.7 || ^6.5 || ^7.1" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "1.8.x-dev" - } - }, - "autoload": { - "psr-4": { - "Prophecy\\": "src/Prophecy" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Konstantin Kudryashov", - "email": "ever.zet@gmail.com", - "homepage": "http://everzet.com" - }, - { - "name": "Marcello Duarte", - "email": "marcello.duarte@gmail.com" - } - ], - "description": "Highly opinionated mocking framework for PHP 5.3+", - "homepage": "https://github.com/phpspec/prophecy", - "keywords": [ - "Double", - "Dummy", - "fake", - "mock", - "spy", - "stub" - ], - "time": "2019-10-03T11:07:50+00:00" - }, - { - "name": "phpunit/php-code-coverage", - "version": "6.1.4", - "source": { - "type": "git", - "url": "https://github.com/sebastianbergmann/php-code-coverage.git", - "reference": "807e6013b00af69b6c5d9ceb4282d0393dbb9d8d" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/807e6013b00af69b6c5d9ceb4282d0393dbb9d8d", - "reference": "807e6013b00af69b6c5d9ceb4282d0393dbb9d8d", - "shasum": "" - }, - "require": { - "ext-dom": "*", - "ext-xmlwriter": "*", - "php": "^7.1", - "phpunit/php-file-iterator": "^2.0", - "phpunit/php-text-template": "^1.2.1", - "phpunit/php-token-stream": "^3.0", - "sebastian/code-unit-reverse-lookup": "^1.0.1", - "sebastian/environment": "^3.1 || ^4.0", - "sebastian/version": "^2.0.1", - "theseer/tokenizer": "^1.1" - }, - "require-dev": { - "phpunit/phpunit": "^7.0" - }, - "suggest": { - "ext-xdebug": "^2.6.0" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "6.1-dev" - } - }, - "autoload": { - "classmap": [ - "src/" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "BSD-3-Clause" - ], - "authors": [ - { - "name": "Sebastian Bergmann", - "email": "sebastian@phpunit.de", - "role": "lead" - } - ], - "description": "Library that provides collection, processing, and rendering functionality for PHP code coverage information.", - "homepage": "https://github.com/sebastianbergmann/php-code-coverage", - "keywords": [ - "coverage", - "testing", - "xunit" - ], - "time": "2018-10-31T16:06:48+00:00" - }, - { - "name": "phpunit/php-file-iterator", - "version": "dev-master", - "source": { - "type": "git", - "url": "https://github.com/sebastianbergmann/php-file-iterator.git", - "reference": "7f0f29702170e2786b2df813af970135765de6fc" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-file-iterator/zipball/7f0f29702170e2786b2df813af970135765de6fc", - "reference": "7f0f29702170e2786b2df813af970135765de6fc", - "shasum": "" - }, - "require": { - "php": "^7.1" - }, - "require-dev": { - "phpunit/phpunit": "^7.1" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "2.0.x-dev" - } - }, - "autoload": { - "classmap": [ - "src/" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "BSD-3-Clause" - ], - "authors": [ - { - "name": "Sebastian Bergmann", - "email": "sebastian@phpunit.de", - "role": "lead" - } - ], - "description": "FilterIterator implementation that filters files based on a list of suffixes.", - "homepage": "https://github.com/sebastianbergmann/php-file-iterator/", - "keywords": [ - "filesystem", - "iterator" - ], - "time": "2019-07-02T07:44:20+00:00" - }, - { - "name": "phpunit/php-text-template", - "version": "1.2.1", - "source": { - "type": "git", - "url": "https://github.com/sebastianbergmann/php-text-template.git", - "reference": "31f8b717e51d9a2afca6c9f046f5d69fc27c8686" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-text-template/zipball/31f8b717e51d9a2afca6c9f046f5d69fc27c8686", - "reference": "31f8b717e51d9a2afca6c9f046f5d69fc27c8686", - "shasum": "" - }, - "require": { - "php": ">=5.3.3" - }, - "type": "library", - "autoload": { - "classmap": [ - "src/" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "BSD-3-Clause" - ], - "authors": [ - { - "name": "Sebastian Bergmann", - "email": "sebastian@phpunit.de", - "role": "lead" - } - ], - "description": "Simple template engine.", - "homepage": "https://github.com/sebastianbergmann/php-text-template/", - "keywords": [ - "template" - ], - "time": "2015-06-21T13:50:34+00:00" - }, - { - "name": "phpunit/php-timer", - "version": "dev-master", - "source": { - "type": "git", - "url": "https://github.com/sebastianbergmann/php-timer.git", - "reference": "37d2894f3650acccb6e57207e63eb9699c1a82a6" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-timer/zipball/37d2894f3650acccb6e57207e63eb9699c1a82a6", - "reference": "37d2894f3650acccb6e57207e63eb9699c1a82a6", - "shasum": "" - }, - "require": { - "php": "^7.1" - }, - "require-dev": { - "phpunit/phpunit": "^7.0" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "2.1-dev" - } - }, - "autoload": { - "classmap": [ - "src/" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "BSD-3-Clause" - ], - "authors": [ - { - "name": "Sebastian Bergmann", - "email": "sebastian@phpunit.de", - "role": "lead" - } - ], - "description": "Utility class for timing", - "homepage": "https://github.com/sebastianbergmann/php-timer/", - "keywords": [ - "timer" - ], - "time": "2019-07-02T07:42:03+00:00" - }, - { - "name": "phpunit/php-token-stream", - "version": "dev-master", - "source": { - "type": "git", - "url": "https://github.com/sebastianbergmann/php-token-stream.git", - "reference": "36bdcb91de0484f77e256fd3d6119dcf7171c164" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-token-stream/zipball/36bdcb91de0484f77e256fd3d6119dcf7171c164", - "reference": "36bdcb91de0484f77e256fd3d6119dcf7171c164", - "shasum": "" - }, - "require": { - "ext-tokenizer": "*", - "php": "^7.1" - }, - "require-dev": { - "phpunit/phpunit": "^7.0" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "3.1-dev" - } - }, - "autoload": { - "classmap": [ - "src/" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "BSD-3-Clause" - ], - "authors": [ - { - "name": "Sebastian Bergmann", - "email": "sebastian@phpunit.de" - } - ], - "description": "Wrapper around PHP's tokenizer extension.", - "homepage": "https://github.com/sebastianbergmann/php-token-stream/", - "keywords": [ - "tokenizer" - ], - "time": "2019-10-05T05:20:56+00:00" - }, - { - "name": "phpunit/phpunit", - "version": "7.5.x-dev", - "source": { - "type": "git", - "url": "https://github.com/sebastianbergmann/phpunit.git", - "reference": "316afa6888d2562e04aeb67ea7f2017a0eb41661" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/316afa6888d2562e04aeb67ea7f2017a0eb41661", - "reference": "316afa6888d2562e04aeb67ea7f2017a0eb41661", - "shasum": "" - }, - "require": { - "doctrine/instantiator": "^1.1", - "ext-dom": "*", - "ext-json": "*", - "ext-libxml": "*", - "ext-mbstring": "*", - "ext-xml": "*", - "myclabs/deep-copy": "^1.7", - "phar-io/manifest": "^1.0.2", - "phar-io/version": "^2.0", - "php": "^7.1", - "phpspec/prophecy": "^1.7", - "phpunit/php-code-coverage": "^6.0.7", - "phpunit/php-file-iterator": "^2.0.1", - "phpunit/php-text-template": "^1.2.1", - "phpunit/php-timer": "^2.1", - "sebastian/comparator": "^3.0", - "sebastian/diff": "^3.0", - "sebastian/environment": "^4.0", - "sebastian/exporter": "^3.1", - "sebastian/global-state": "^2.0", - "sebastian/object-enumerator": "^3.0.3", - "sebastian/resource-operations": "^2.0", - "sebastian/version": "^2.0.1" - }, - "conflict": { - "phpunit/phpunit-mock-objects": "*" - }, - "require-dev": { - "ext-pdo": "*" - }, - "suggest": { - "ext-soap": "*", - "ext-xdebug": "*", - "phpunit/php-invoker": "^2.0" - }, - "bin": [ - "phpunit" - ], - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "7.5-dev" - } - }, - "autoload": { - "classmap": [ - "src/" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "BSD-3-Clause" - ], - "authors": [ - { - "name": "Sebastian Bergmann", - "email": "sebastian@phpunit.de", - "role": "lead" - } - ], - "description": "The PHP Unit Testing framework.", - "homepage": "https://phpunit.de/", - "keywords": [ - "phpunit", - "testing", - "xunit" - ], - "time": "2019-09-14T09:08:39+00:00" - }, - { - "name": "sebastian/code-unit-reverse-lookup", - "version": "dev-master", - "source": { - "type": "git", - "url": "https://github.com/sebastianbergmann/code-unit-reverse-lookup.git", - "reference": "5e860800beea5ea4c8590df866338c09c20d3a48" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/code-unit-reverse-lookup/zipball/5e860800beea5ea4c8590df866338c09c20d3a48", - "reference": "5e860800beea5ea4c8590df866338c09c20d3a48", - "shasum": "" - }, - "require": { - "php": "^5.6 || ^7.0" - }, - "require-dev": { - "phpunit/phpunit": "^5.7 || ^6.0" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "1.0.x-dev" - } - }, - "autoload": { - "classmap": [ - "src/" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "BSD-3-Clause" - ], - "authors": [ - { - "name": "Sebastian Bergmann", - "email": "sebastian@phpunit.de" - } - ], - "description": "Looks up which function or method a line of code belongs to", - "homepage": "https://github.com/sebastianbergmann/code-unit-reverse-lookup/", - "time": "2019-07-02T07:44:03+00:00" - }, - { - "name": "sebastian/comparator", - "version": "dev-master", - "source": { - "type": "git", - "url": "https://github.com/sebastianbergmann/comparator.git", - "reference": "9a1267ac19ecd74163989bcb3e01c5c9587f9e3b" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/comparator/zipball/9a1267ac19ecd74163989bcb3e01c5c9587f9e3b", - "reference": "9a1267ac19ecd74163989bcb3e01c5c9587f9e3b", - "shasum": "" - }, - "require": { - "php": "^7.1", - "sebastian/diff": "^3.0", - "sebastian/exporter": "^3.1" - }, - "require-dev": { - "phpunit/phpunit": "^7.1" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "3.0-dev" - } - }, - "autoload": { - "classmap": [ - "src/" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "BSD-3-Clause" - ], - "authors": [ - { - "name": "Jeff Welch", - "email": "whatthejeff@gmail.com" - }, - { - "name": "Volker Dusch", - "email": "github@wallbash.com" - }, - { - "name": "Bernhard Schussek", - "email": "bschussek@2bepublished.at" - }, - { - "name": "Sebastian Bergmann", - "email": "sebastian@phpunit.de" - } - ], - "description": "Provides the functionality to compare PHP values for equality", - "homepage": "https://github.com/sebastianbergmann/comparator", - "keywords": [ - "comparator", - "compare", - "equality" - ], - "time": "2019-07-02T07:45:15+00:00" - }, - { - "name": "sebastian/diff", - "version": "dev-master", - "source": { - "type": "git", - "url": "https://github.com/sebastianbergmann/diff.git", - "reference": "d7e7810940c78f3343420f76adf92dc437b7a557" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/diff/zipball/d7e7810940c78f3343420f76adf92dc437b7a557", - "reference": "d7e7810940c78f3343420f76adf92dc437b7a557", - "shasum": "" - }, - "require": { - "php": "^7.1" - }, - "require-dev": { - "phpunit/phpunit": "^7.5 || ^8.0", - "symfony/process": "^2 || ^3.3 || ^4" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "3.0-dev" - } - }, - "autoload": { - "classmap": [ - "src/" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "BSD-3-Clause" - ], - "authors": [ - { - "name": "Kore Nordmann", - "email": "mail@kore-nordmann.de" - }, - { - "name": "Sebastian Bergmann", - "email": "sebastian@phpunit.de" - } - ], - "description": "Diff implementation", - "homepage": "https://github.com/sebastianbergmann/diff", - "keywords": [ - "diff", - "udiff", - "unidiff", - "unified diff" - ], - "time": "2019-07-02T07:43:30+00:00" - }, - { - "name": "sebastian/environment", - "version": "dev-master", - "source": { - "type": "git", - "url": "https://github.com/sebastianbergmann/environment.git", - "reference": "520187a48d1fd3714dd4ebfd8eb914a4d69d26a7" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/environment/zipball/520187a48d1fd3714dd4ebfd8eb914a4d69d26a7", - "reference": "520187a48d1fd3714dd4ebfd8eb914a4d69d26a7", - "shasum": "" - }, - "require": { - "php": "^7.1" - }, - "require-dev": { - "phpunit/phpunit": "^7.5" - }, - "suggest": { - "ext-posix": "*" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "4.2-dev" - } - }, - "autoload": { - "classmap": [ - "src/" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "BSD-3-Clause" - ], - "authors": [ - { - "name": "Sebastian Bergmann", - "email": "sebastian@phpunit.de" - } - ], - "description": "Provides functionality to handle HHVM/PHP environments", - "homepage": "http://www.github.com/sebastianbergmann/environment", - "keywords": [ - "Xdebug", - "environment", - "hhvm" - ], - "time": "2019-09-07T09:51:27+00:00" - }, - { - "name": "sebastian/exporter", - "version": "dev-master", - "source": { - "type": "git", - "url": "https://github.com/sebastianbergmann/exporter.git", - "reference": "68609e1261d215ea5b21b7987539cbfbe156ec3e" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/exporter/zipball/68609e1261d215ea5b21b7987539cbfbe156ec3e", - "reference": "68609e1261d215ea5b21b7987539cbfbe156ec3e", - "shasum": "" - }, - "require": { - "php": "^7.0", - "sebastian/recursion-context": "^3.0" - }, - "require-dev": { - "ext-mbstring": "*", - "phpunit/phpunit": "^6.0" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "3.1.x-dev" - } - }, - "autoload": { - "classmap": [ - "src/" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "BSD-3-Clause" - ], - "authors": [ - { - "name": "Sebastian Bergmann", - "email": "sebastian@phpunit.de" - }, - { - "name": "Jeff Welch", - "email": "whatthejeff@gmail.com" - }, - { - "name": "Volker Dusch", - "email": "github@wallbash.com" - }, - { - "name": "Adam Harvey", - "email": "aharvey@php.net" - }, - { - "name": "Bernhard Schussek", - "email": "bschussek@gmail.com" - } - ], - "description": "Provides the functionality to export PHP variables for visualization", - "homepage": "http://www.github.com/sebastianbergmann/exporter", - "keywords": [ - "export", - "exporter" - ], - "time": "2019-09-14T09:02:43+00:00" - }, - { - "name": "sebastian/global-state", - "version": "2.0.0", - "source": { - "type": "git", - "url": "https://github.com/sebastianbergmann/global-state.git", - "reference": "e8ba02eed7bbbb9e59e43dedd3dddeff4a56b0c4" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/global-state/zipball/e8ba02eed7bbbb9e59e43dedd3dddeff4a56b0c4", - "reference": "e8ba02eed7bbbb9e59e43dedd3dddeff4a56b0c4", - "shasum": "" - }, - "require": { - "php": "^7.0" - }, - "require-dev": { - "phpunit/phpunit": "^6.0" - }, - "suggest": { - "ext-uopz": "*" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "2.0-dev" - } - }, - "autoload": { - "classmap": [ - "src/" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "BSD-3-Clause" - ], - "authors": [ - { - "name": "Sebastian Bergmann", - "email": "sebastian@phpunit.de" - } - ], - "description": "Snapshotting of global state", - "homepage": "http://www.github.com/sebastianbergmann/global-state", - "keywords": [ - "global state" - ], - "time": "2017-04-27T15:39:26+00:00" - }, - { - "name": "sebastian/object-enumerator", - "version": "dev-master", - "source": { - "type": "git", - "url": "https://github.com/sebastianbergmann/object-enumerator.git", - "reference": "63e5a3e0881ebf28c9fbb2a2e12b77d373850c12" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/object-enumerator/zipball/63e5a3e0881ebf28c9fbb2a2e12b77d373850c12", - "reference": "63e5a3e0881ebf28c9fbb2a2e12b77d373850c12", - "shasum": "" - }, - "require": { - "php": "^7.0", - "sebastian/object-reflector": "^1.1.1", - "sebastian/recursion-context": "^3.0" - }, - "require-dev": { - "phpunit/phpunit": "^6.0" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "3.0.x-dev" - } - }, - "autoload": { - "classmap": [ - "src/" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "BSD-3-Clause" - ], - "authors": [ - { - "name": "Sebastian Bergmann", - "email": "sebastian@phpunit.de" - } - ], - "description": "Traverses array structures and object graphs to enumerate all referenced objects", - "homepage": "https://github.com/sebastianbergmann/object-enumerator/", - "time": "2019-07-02T07:43:46+00:00" - }, - { - "name": "sebastian/object-reflector", - "version": "dev-master", - "source": { - "type": "git", - "url": "https://github.com/sebastianbergmann/object-reflector.git", - "reference": "3053ae3e6286fdf98769f18ec10894dbc6260a34" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/object-reflector/zipball/3053ae3e6286fdf98769f18ec10894dbc6260a34", - "reference": "3053ae3e6286fdf98769f18ec10894dbc6260a34", - "shasum": "" - }, - "require": { - "php": "^7.0" - }, - "require-dev": { - "phpunit/phpunit": "^6.0" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "1.1-dev" - } - }, - "autoload": { - "classmap": [ - "src/" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "BSD-3-Clause" - ], - "authors": [ - { - "name": "Sebastian Bergmann", - "email": "sebastian@phpunit.de" - } - ], - "description": "Allows reflection of object attributes, including inherited and non-public ones", - "homepage": "https://github.com/sebastianbergmann/object-reflector/", - "time": "2019-07-02T07:44:36+00:00" - }, - { - "name": "sebastian/recursion-context", - "version": "dev-master", - "source": { - "type": "git", - "url": "https://github.com/sebastianbergmann/recursion-context.git", - "reference": "a58220ae18565f6004bbe15321efc4470bfe02fd" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/recursion-context/zipball/a58220ae18565f6004bbe15321efc4470bfe02fd", - "reference": "a58220ae18565f6004bbe15321efc4470bfe02fd", - "shasum": "" - }, - "require": { - "php": "^7.0" - }, - "require-dev": { - "phpunit/phpunit": "^6.0" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "3.0.x-dev" - } - }, - "autoload": { - "classmap": [ - "src/" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "BSD-3-Clause" - ], - "authors": [ - { - "name": "Jeff Welch", - "email": "whatthejeff@gmail.com" - }, - { - "name": "Sebastian Bergmann", - "email": "sebastian@phpunit.de" - }, - { - "name": "Adam Harvey", - "email": "aharvey@php.net" - } - ], - "description": "Provides functionality to recursively process PHP variables", - "homepage": "http://www.github.com/sebastianbergmann/recursion-context", - "time": "2019-07-02T07:43:54+00:00" - }, - { - "name": "sebastian/resource-operations", - "version": "dev-master", - "source": { - "type": "git", - "url": "https://github.com/sebastianbergmann/resource-operations.git", - "reference": "d67fc89d3107c396d161411b620619f3e7a7c270" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/resource-operations/zipball/d67fc89d3107c396d161411b620619f3e7a7c270", - "reference": "d67fc89d3107c396d161411b620619f3e7a7c270", - "shasum": "" - }, - "require": { - "php": "^7.1" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "2.0-dev" - } - }, - "autoload": { - "classmap": [ - "src/" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "BSD-3-Clause" - ], - "authors": [ - { - "name": "Sebastian Bergmann", - "email": "sebastian@phpunit.de" - } - ], - "description": "Provides a list of PHP built-in functions that operate on resources", - "homepage": "https://www.github.com/sebastianbergmann/resource-operations", - "time": "2019-07-02T07:42:50+00:00" - }, - { - "name": "sebastian/version", - "version": "2.0.1", - "source": { - "type": "git", - "url": "https://github.com/sebastianbergmann/version.git", - "reference": "99732be0ddb3361e16ad77b68ba41efc8e979019" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/version/zipball/99732be0ddb3361e16ad77b68ba41efc8e979019", - "reference": "99732be0ddb3361e16ad77b68ba41efc8e979019", - "shasum": "" - }, - "require": { - "php": ">=5.6" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "2.0.x-dev" - } - }, - "autoload": { - "classmap": [ - "src/" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "BSD-3-Clause" - ], - "authors": [ - { - "name": "Sebastian Bergmann", - "email": "sebastian@phpunit.de", - "role": "lead" - } - ], - "description": "Library that helps with managing the version number of Git-hosted PHP projects", - "homepage": "https://github.com/sebastianbergmann/version", - "time": "2016-10-03T07:35:21+00:00" - }, - { - "name": "symfony/polyfill-ctype", - "version": "dev-master", - "source": { - "type": "git", - "url": "https://github.com/symfony/polyfill-ctype.git", - "reference": "550ebaac289296ce228a706d0867afc34687e3f4" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-ctype/zipball/550ebaac289296ce228a706d0867afc34687e3f4", - "reference": "550ebaac289296ce228a706d0867afc34687e3f4", - "shasum": "" - }, - "require": { - "php": ">=5.3.3" - }, - "suggest": { - "ext-ctype": "For best performance" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "1.12-dev" - } - }, - "autoload": { - "psr-4": { - "Symfony\\Polyfill\\Ctype\\": "" - }, - "files": [ - "bootstrap.php" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Gert de Pagter", - "email": "BackEndTea@gmail.com" - }, - { - "name": "Symfony Community", - "homepage": "https://symfony.com/contributors" - } - ], - "description": "Symfony polyfill for ctype functions", - "homepage": "https://symfony.com", - "keywords": [ - "compatibility", - "ctype", - "polyfill", - "portable" - ], - "time": "2019-08-06T08:03:45+00:00" - }, - { - "name": "theseer/tokenizer", - "version": "1.1.3", - "source": { - "type": "git", - "url": "https://github.com/theseer/tokenizer.git", - "reference": "11336f6f84e16a720dae9d8e6ed5019efa85a0f9" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/theseer/tokenizer/zipball/11336f6f84e16a720dae9d8e6ed5019efa85a0f9", - "reference": "11336f6f84e16a720dae9d8e6ed5019efa85a0f9", - "shasum": "" - }, - "require": { - "ext-dom": "*", - "ext-tokenizer": "*", - "ext-xmlwriter": "*", - "php": "^7.0" - }, - "type": "library", - "autoload": { - "classmap": [ - "src/" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "BSD-3-Clause" - ], - "authors": [ - { - "name": "Arne Blankerts", - "email": "arne@blankerts.de", - "role": "Developer" - } - ], - "description": "A small library for converting tokenized PHP source code into XML and potentially other formats", - "time": "2019-06-13T22:48:21+00:00" - }, - { - "name": "webmozart/assert", - "version": "1.5.0", - "source": { - "type": "git", - "url": "https://github.com/webmozart/assert.git", - "reference": "88e6d84706d09a236046d686bbea96f07b3a34f4" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/webmozart/assert/zipball/88e6d84706d09a236046d686bbea96f07b3a34f4", - "reference": "88e6d84706d09a236046d686bbea96f07b3a34f4", - "shasum": "" - }, - "require": { - "php": "^5.3.3 || ^7.0", - "symfony/polyfill-ctype": "^1.8" - }, - "require-dev": { - "phpunit/phpunit": "^4.8.36 || ^7.5.13" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "1.3-dev" - } - }, - "autoload": { - "psr-4": { - "Webmozart\\Assert\\": "src/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Bernhard Schussek", - "email": "bschussek@gmail.com" - } - ], - "description": "Assertions to validate method input/output with nice error messages.", - "keywords": [ - "assert", - "check", - "validate" - ], - "time": "2019-08-24T08:43:50+00:00" - } - ], - "packages-dev": [], - "aliases": [], - "minimum-stability": "dev", - "stability-flags": [], - "prefer-stable": false, - "prefer-lowest": false, - "platform": [], - "platform-dev": [] -} diff --git a/config.m4 b/config.m4 index f372a51..cee6961 100644 --- a/config.m4 +++ b/config.m4 @@ -2,12 +2,13 @@ PHP_ARG_ENABLE(phptars, whether to enable phptars support, Make sure that the comment is aligned: [ --enable-phptars Enable phptars support]) -if test "$PHP_PHPTARS" != "no"; then - +if test "$PHP_PHPTARS" != "no"; +then PHP_NEW_EXTENSION(phptars, tupapi.c ttars.c tup_c.c tars_c.c, $ext_shared) fi -if test -z "$PHP_DEBUG"; then +if test -z "$PHP_DEBUG"; +then AC_ARG_ENABLE(debug, [ --enable-debug compile with debugging symbols],[ PHP_DEBUG=$enableval @@ -18,7 +19,8 @@ fi PHP_ARG_ENABLE(phptars-gcov, whether to enable php gcov, [ --enable-gcov To enable code coverage reporting], no, no) -if test "$PHP_PHPTARS_GCOV" != "no"; then +if test "$PHP_PHPTARS_GCOV" != "no"; +then CFLAGS="$CFLAGS -O0 -fprofile-arcs -ftest-coverage" CXXFLAGS="$CXXFLAGS -O0 -fprofile-arcs -ftest-coverage" fi \ No newline at end of file diff --git a/include/php7_wrapper.h b/include/php7_wrapper.h deleted file mode 100644 index 408f548..0000000 --- a/include/php7_wrapper.h +++ /dev/null @@ -1,114 +0,0 @@ -/* - +----------------------------------------------------------------------+ - | PHP Version 5 | - +----------------------------------------------------------------------+ - | Copyright (c) 1997-2015 The PHP Group | - +----------------------------------------------------------------------+ - | This source file is subject to version 3.01 of the PHP license, | - | that is bundled with this package in the file LICENSE, and is | - | available through the world-wide-web at the following url: | - | http://www.php.net/license/3_01.txt | - | If you did not receive a copy of the PHP license and are unable to | - | obtain it through the world-wide-web, please send a note to | - | license@php.net so we can mail you a copy immediately. | - +----------------------------------------------------------------------+ - | Author:yong@yuewen.com | - +----------------------------------------------------------------------+ -*/ - -#ifndef EXT_PHP7_WRAPPER_H_ -#define EXT_PHP7_WRAPPER_H_ - - -#if PHP_MAJOR_VERSION < 7 -typedef int zend_size_t; -#define IS_TRUE 11 -#define IS_FALSE 12 -#define MY_MAKE_STD_ZVAL(p) MAKE_STD_ZVAL(p) -#define MY_RETURN_STRINGL RETURN_STRINGL -#define MY_ZEND_FETCH_RESOURCE ZEND_FETCH_RESOURCE -#define MY_ZEND_REGISTER_RESOURCE ZEND_REGISTER_RESOURCE -#define my_add_assoc_string add_assoc_string -#define my_zend_read_property zend_read_property -#define my_zval_ptr_dtor zval_ptr_dtor -#define MY_RETVAL_STRINGL RETVAL_STRINGL -#define my_add_assoc_stringl add_assoc_stringl -#define my_zend_register_internal_class_ex zend_register_internal_class_ex -#define my_zend_hash_get_current_key_ex zend_hash_get_current_key_ex -#define my_smart_str smart_str -#define MY_Z_ARRVAL_P Z_ARRVAL_P -#define MY_ZVAL_STRINGL ZVAL_STRINGL -#define my_zend_hash_index_update zend_hash_index_update -#define my_zend_hash_add zend_hash_add -#define my_zend_hash_next_index_insert zend_hash_next_index_insert - -static inline int MY_Z_TYPE_P(zval *z) -{ - if (Z_TYPE_P(z) == IS_BOOL) - { - if ((uint8_t) Z_BVAL_P(z) == 1) - { - return IS_TRUE; - } - else - { - return IS_FALSE; - } - } - else - { - return Z_TYPE_P(z); - } -} - -inline int MY_Z_TYPE_P(zval *z); -#define MY_Z_TYPE_PP(z) MY_Z_TYPE_P(*z) - -#else - -typedef size_t zend_size_t; -#define MY_MAKE_STD_ZVAL(p) zval _stack_zval_##p; p = &(_stack_zval_##p) -#define MY_RETURN_STRINGL(s, l, dup) RETURN_STRINGL(s, l) -#define MY_ZEND_FETCH_RESOURCE(rsrc, rsrc_type, passed_id, default_id, resource_type_name, resource_type) \ - rsrc = (rsrc_type) zend_fetch_resource(Z_RES_P(*passed_id), resource_type_name, resource_type); -#define MY_ZEND_REGISTER_RESOURCE(return_value, result, le_result) ZVAL_RES(return_value,zend_register_resource(result, le_result)) -#define my_add_assoc_string(array, key, value, duplicate) add_assoc_string(array, key, value) -static zval* my_zend_read_property(zend_class_entry *class_ptr, zval *obj, char *s, int len, int silent) -{ - zval rv; - return zend_read_property(class_ptr, obj, s, len, silent, &rv); -} -#define my_zval_ptr_dtor(p) zval_ptr_dtor(*p) -#define MY_RETVAL_STRINGL(s, l, dup) RETVAL_STRINGL(s, l); if (dup == 0) efree(s) -#define my_add_assoc_stringl(__arg, __key, __str, __length, __duplicate) add_assoc_stringl_ex(__arg, __key, strlen(__key), __str, __length) -#define Z_STRVAL_PP(s) Z_STRVAL_P(*s) -#define my_zend_register_internal_class_ex(entry,parent_ptr,str) zend_register_internal_class_ex(entry,parent_ptr) -#define my_smart_str smart_string -#define MY_Z_ARRVAL_P(z) Z_ARRVAL_P(z)->ht -#define Z_ARRVAL_PP(s) Z_ARRVAL_P(*s) -#define Z_STRLEN_PP(s) Z_STRLEN_P(*s) -#define Z_LVAL_PP(v) Z_LVAL_P(*v) -#define MY_ZVAL_STRINGL(z, s, l, dup) ZVAL_STRINGL(z, s, l) -#define MY_Z_TYPE_P Z_TYPE_P -#define MY_Z_TYPE_PP(s) MY_Z_TYPE_P(*s) -static inline int my_zend_hash_add(HashTable *ht, char *k, long len, void *pData, int datasize, void **pDest) -{ - zval **real_p = pData; - return zend_hash_str_add(ht, k, len - 1, *real_p) ? SUCCESS : FAILURE; -} -static inline int my_zend_hash_index_update(HashTable *ht, long key, void *pData, int datasize, void **pDest) -{ - zval **real_p = pData; - return zend_hash_index_update(ht, key, *real_p) ? SUCCESS : FAILURE; -} -#define ALLOC_INIT_ZVAL(z) zval z##Tmp; z = &(z##Tmp); -#define my_zend_hash_next_index_insert(ht, pData, a, b) zend_hash_next_index_insert(ht, *pData) - - - - - - -#endif - -#endif /* EXT_PHP7_WRAPPER_H_ */ diff --git a/include/php8_wrapper.h b/include/php8_wrapper.h new file mode 100644 index 0000000..ae8bd0b --- /dev/null +++ b/include/php8_wrapper.h @@ -0,0 +1,49 @@ +#ifndef EXT_PHP8_WRAPPER_H_ +#define EXT_PHP8_WRAPPER_H_ + +#define Z_LVAL_PP(v) Z_LVAL_P(*v) +#define Z_STRVAL_PP(s) Z_STRVAL_P(*s) +#define Z_ARRVAL_PP(s) Z_ARRVAL_P(*s) +#define Z_STRLEN_PP(s) Z_STRLEN_P(*s) +#define ALLOC_INIT_ZVAL(z) zval z##Tmp; z = &(z##Tmp); + +#define MY_Z_TYPE_P Z_TYPE_P +#define MY_Z_TYPE_PP(s) MY_Z_TYPE_P(*s) +#define MY_Z_ARRVAL_P(z) Z_ARRVAL_P(z)->ht +#define MY_ZVAL_STRINGL(z, s, l, dup) ZVAL_STRINGL(z, s, l) +#define MY_MAKE_STD_ZVAL(p) zval _stack_zval_##p; p = &(_stack_zval_##p) +#define MY_RETVAL_STRINGL(s, l, dup) RETVAL_STRINGL(s, l); if (dup == 0) efree(s) +#define MY_RETURN_STRINGL(s, l, dup) RETURN_STRINGL(s, l) + +#define MY_ZEND_REGISTER_RESOURCE(return_value, result, le_result) \ + ZVAL_RES(return_value,zend_register_resource(result, le_result)) + +#define MY_ZEND_FETCH_RESOURCE(rsrc, rsrc_type, passed_id, default_id, resource_type_name, resource_type) \ + rsrc = (rsrc_type) zend_fetch_resource(Z_RES_P(*passed_id), resource_type_name, resource_type); + +#define my_smart_str smart_string +#define my_zval_ptr_dtor(p) zval_ptr_dtor(*p) +#define my_zend_hash_next_index_insert(ht, pData, a, b) zend_hash_next_index_insert(ht, *pData) +#define my_zend_register_internal_class_ex(entry,parent_ptr,str) zend_register_internal_class_ex(entry,parent_ptr) +#define my_add_assoc_string(array, key, value, duplicate) add_assoc_string(array, key, value) +#define my_add_assoc_stringl(__arg, __key, __str, __length, __duplicate) add_assoc_stringl_ex(__arg, __key, strlen(__key), __str, __length) + +static inline int my_zend_hash_add(HashTable *ht, char *k, long len, void *pData, int datasize, void **pDest) +{ + zval **real_p = pData; + return zend_hash_str_add(ht, k, len - 1, *real_p) ? SUCCESS : FAILURE; +} + +static inline int my_zend_hash_index_update(HashTable *ht, long key, void *pData, int datasize, void **pDest) +{ + zval **real_p = pData; + return zend_hash_index_update(ht, key, *real_p) ? SUCCESS : FAILURE; +} + +static zval *my_zend_read_property(zend_class_entry *class_ptr, zval *obj, char *s, int len, int silent) +{ + zval rv; + return zend_read_property(class_ptr, Z_OBJ_P(obj), s, len, silent, &rv); +} + +#endif /* EXT_PHP8_WRAPPER_H_ */ diff --git a/include/php_error.h b/include/php_error.h index 4cdbcf3..dab180d 100644 --- a/include/php_error.h +++ b/include/php_error.h @@ -1,36 +1,36 @@ #ifndef PHP_ERROR_H #define PHP_ERROR_H -#define ERR_CANNOT_CONVERT 10001 -#define ERR_OUTOF_RANGE 10002 -#define ERR_MALLOC_FAILED 10003 -#define ERR_CLASS_UNINIT 10004 -#define ERR_REQUIRED_FIELD_LOST 10005 -#define ERR_DATA_FORMAT_ERROR 10006 -#define ERR_TYPE_INVALID 10007 -#define ERR_CLASS_MISMATCH 10008 -#define ERR_WRONG_PARAMS 10009 -#define ERR_STATIC_FIELDS_PARAM_LOST 10010 -#define ERR_ARRAY_RETRIEVE 10011 -#define ERR_READ_MAP_ERROR 10012 -#define ERR_SET_CONTEXT_ERROR 10013 -#define ERR_SET_STATUS_ERROR 10014 -#define ERR_ENCODE_BUF_ERROR 10015 -#define ERR_WRITE_IVERSION_ERROR 10016 -#define ERR_WRITE_CPACKETTYPE_ERROR 10017 -#define ERR_WRITE_IMESSAGETYPE_ERROR 10018 -#define ERR_WRITE_IREQUESTID_ERROR 10019 -#define ERR_WRITE_SSERVANTNAME_ERROR 10020 -#define ERR_WRITE_SFUNCNAME_ERROR 10021 -#define ERR_WRITE_SBUFFER_ERROR 10022 -#define ERR_WRITE_ITIMEOUT_ERROR 10023 -#define ERR_WRITE_CONTEXT_ERROR 10024 -#define ERR_WRITE_STATUS_ERROR 10025 -#define ERR_STRUCT_COMPLICATE_NOT_DEFINE 10026 -#define ERR_VECOTR_OR_MAP_EXT_PARAM_LOST 10027 -#define ERR_STATIC_NAME_NOT_STRING_ERROR 10028 +#define ERR_CANNOT_CONVERT 10001 +#define ERR_OUTOF_RANGE 10002 +#define ERR_MALLOC_FAILED 10003 +#define ERR_CLASS_UNINIT 10004 +#define ERR_REQUIRED_FIELD_LOST 10005 +#define ERR_DATA_FORMAT_ERROR 10006 +#define ERR_TYPE_INVALID 10007 +#define ERR_CLASS_MISMATCH 10008 +#define ERR_WRONG_PARAMS 10009 +#define ERR_STATIC_FIELDS_PARAM_LOST 10010 +#define ERR_ARRAY_RETRIEVE 10011 +#define ERR_READ_MAP_ERROR 10012 +#define ERR_SET_CONTEXT_ERROR 10013 +#define ERR_SET_STATUS_ERROR 10014 +#define ERR_ENCODE_BUF_ERROR 10015 +#define ERR_WRITE_IVERSION_ERROR 10016 +#define ERR_WRITE_CPACKETTYPE_ERROR 10017 +#define ERR_WRITE_IMESSAGETYPE_ERROR 10018 +#define ERR_WRITE_IREQUESTID_ERROR 10019 +#define ERR_WRITE_SSERVANTNAME_ERROR 10020 +#define ERR_WRITE_SFUNCNAME_ERROR 10021 +#define ERR_WRITE_SBUFFER_ERROR 10022 +#define ERR_WRITE_ITIMEOUT_ERROR 10023 +#define ERR_WRITE_CONTEXT_ERROR 10024 +#define ERR_WRITE_STATUS_ERROR 10025 +#define ERR_STRUCT_COMPLICATE_NOT_DEFINE 10026 +#define ERR_VECOTR_OR_MAP_EXT_PARAM_LOST 10027 +#define ERR_STATIC_NAME_NOT_STRING_ERROR 10028 #define ERR_STATIC_REQUIRED_NOT_BOOL_ERROR 10029 -#define ERR_STATIC_TYPE_NOT_LONG_ERROR 10030 +#define ERR_STATIC_TYPE_NOT_LONG_ERROR 10030 #endif diff --git a/include/php_tupapi.h b/include/php_tupapi.h index ed1914c..ec7a00c 100644 --- a/include/php_tupapi.h +++ b/include/php_tupapi.h @@ -1,55 +1,29 @@ -/* - +----------------------------------------------------------------------+ - | PHP Version 5 | - +----------------------------------------------------------------------+ - | Copyright (c) 1997-2010 The PHP Group | - +----------------------------------------------------------------------+ - | This source file is subject to version 3.01 of the PHP license, | - | that is bundled with this package in the file LICENSE, and is | - | available through the world-wide-web at the following url: | - | http://www.php.net/license/3_01.txt | - | If you did not receive a copy of the PHP license and are unable to | - | obtain it through the world-wide-web, please send a note to | - | license@php.net so we can mail you a copy immediately. | - +----------------------------------------------------------------------+ - | Author: | - +----------------------------------------------------------------------+ -*/ - -/* $Id: header 297205 2010-03-30 21:09:07Z johannes $ */ - #ifndef PHP_TUPAPI_H #define PHP_TUPAPI_H -extern zend_module_entry phptars_module_entry; +extern zend_module_entry phptars_module_entry; #define phpext_phptars_ptr &phptars_module_entry -#ifdef PHP_WIN32 -#define PHP_PHPTARS_API __declspec(dllexport) -#else #define PHP_PHPTARS_API -#endif +#define PHP_TARS_VERSION "2.3.0" #ifdef ZTS #include "TSRM.h" #endif -#include "php7_wrapper.h" +#include "php8_wrapper.h" -#define PHP_TARS_VERSION "2.2.5" - -extern zend_class_entry * tup_ce; -extern zend_class_entry * tup_exception_ce; -extern zend_class_entry * tars_ce; -extern zend_class_entry * tars_struct_ce; -extern zend_class_entry * tars_vector_ce; -extern zend_class_entry * tars_map_ce; - -#define JS_STRVAL(t) ((t)->_data) -#define JS_STRLEN(t) ((t)->_len) +extern zend_class_entry *tup_ce; +extern zend_class_entry *tup_exception_ce; +extern zend_class_entry *tars_ce; +extern zend_class_entry *tars_struct_ce; +extern zend_class_entry *tars_vector_ce; +extern zend_class_entry *tars_map_ce; +#define JS_STRVAL(t) ((t)->_data) +#define JS_STRLEN(t) ((t)->_len) +#define TUP_STARTUP(module) ZEND_MODULE_STARTUP_N(module)(INIT_FUNC_ARGS_PASSTHRU) #define TUP_STARTUP_FUNC(module) ZEND_MINIT_FUNCTION(module) -#define TUP_STARTUP(module) ZEND_MODULE_STARTUP_N(module)(INIT_FUNC_ARGS_PASSTHRU) PHP_METHOD(tup, __construct); PHP_METHOD(tup, encode); @@ -58,86 +32,70 @@ PHP_METHOD(tup, encodeRspPacket); PHP_METHOD(tup, decodeReqPacket); PHP_METHOD(tup, putStruct); PHP_METHOD(tup, getStruct); - PHP_METHOD(tup, putBool); PHP_METHOD(tup, getBool); - PHP_METHOD(tup, putChar); PHP_METHOD(tup, getChar); - PHP_METHOD(tup, putUInt8); PHP_METHOD(tup, getUInt8); - PHP_METHOD(tup, putShort); PHP_METHOD(tup, getShort); - PHP_METHOD(tup, putUInt16); PHP_METHOD(tup, getUInt16); - PHP_METHOD(tup, putInt32); PHP_METHOD(tup, getInt32); - PHP_METHOD(tup, putUInt32); PHP_METHOD(tup, getUInt32); - PHP_METHOD(tup, putInt64); PHP_METHOD(tup, getInt64); - PHP_METHOD(tup, putDouble); PHP_METHOD(tup, getDouble); - PHP_METHOD(tup, putFloat); PHP_METHOD(tup, getFloat); - PHP_METHOD(tup, putString); PHP_METHOD(tup, getString); - PHP_METHOD(tup, putVector); PHP_METHOD(tup, getVector); - PHP_METHOD(tup, putMap); PHP_METHOD(tup, getMap); - - PHP_METHOD(tup_exception, __construct); -#define OUTOF_EXCEPTION() tup_throw_exception(ERR_OUTOF_RANGE, "Outof the specified type range.") -#define MALLOC_EXCEPTION(t) tup_throw_exception(ERR_MALLOC_FAILED, "Unable to allocate enough memory type %s.", t) -#define CONVERT_EXCEPTION(errno, t) tup_throw_exception(errno, "Parameter can not be converted to the type %s.", t) -#define PACK_EXCEPTION(errno, t) tup_throw_exception(errno, "An error occurred when packing type %s.", t) -#define UNPACK_EXCEPTION(errno, t) tup_throw_exception(errno, "An error occurred when unpacking type %s, check binary buffer, check name of parameter check order of parameter", t) -#define UNINIT_EXCEPTION(clazz) tup_throw_exception(ERR_CLASS_UNINIT, "%s instance is not initialized.", clazz) -#define ENCODE_EXCEPTION(errno) tup_throw_exception(errno, "An error occurred when encoding.") -#define DECODE_EXCEPTION(errno) tup_throw_exception(errno, "An error occurred when decoding.") -#define NAME_EXCEPTOIN() tup_throw_exception(ERR_WRONG_PARAMS, "The length of the package name too short.") -#define TYPE_EXCEPTOIN() tup_throw_exception(ERR_TYPE_INVALID, "Data type is invalid.") -#define INTERNET_EXCEPTION(errno) tup_throw_exception(errno, "An internet error occurred.") -#define PARAM_FMT_EXCEPTION() tup_throw_exception(ERR_DATA_FORMAT_ERROR, "Parameter format is incorrect.") -#define READ_MAP_EXCEPTION() tup_throw_exception(ERR_READ_MAP_ERROR, "Read map failed, please check binary buffer.") -#define TUP_SET_CONTEXT_EXCEPTION() tup_throw_exception(ERR_SET_CONTEXT_ERROR, "Set context while encoding failed, please check contexts parameter while encoding.") -#define TUP_SET_STATUS_EXCEPTION() tup_throw_exception(ERR_SET_STATUS_ERROR, "Set status while encoding failed, please check statuses parameter while encoding.") -#define ENCODE_BUF_EXCEPTION() tup_throw_exception(ERR_ENCODE_BUF_ERROR, "Encode buf while encoding failed, please check in buf array parameter while encoding.") -#define ENCODE_WRITE_IVERSION_EXCEPTION() tup_throw_exception(ERR_WRITE_IVERSION_ERROR, "Encode failed while writing iversion.") -#define ENCODE_WRITE_CPACKETTYPE_EXCEPTION() tup_throw_exception(ERR_WRITE_CPACKETTYPE_ERROR, "Encode failed while writing cpackettype .") +#define PACK_EXCEPTION(errno, t) tup_throw_exception(errno, "An error occurred when packing type %s.", t) +#define UNPACK_EXCEPTION(errno, t) tup_throw_exception(errno, "An error occurred when unpacking type %s, check binary buffer, check name of parameter check order of parameter", t) +#define ENCODE_EXCEPTION(errno) tup_throw_exception(errno, "An error occurred when encoding.") +#define DECODE_EXCEPTION(errno) tup_throw_exception(errno, "An error occurred when decoding.") +#define CONVERT_EXCEPTION(errno, t) tup_throw_exception(errno, "Parameter can not be converted to the type %s.", t) +#define INTERNET_EXCEPTION(errno) tup_throw_exception(errno, "An internet error occurred.") +#define OUTOF_EXCEPTION() tup_throw_exception(ERR_OUTOF_RANGE, "Outof the specified type range.") +#define MALLOC_EXCEPTION(t) tup_throw_exception(ERR_MALLOC_FAILED, "Unable to allocate enough memory type %s.", t) +#define UNINIT_EXCEPTION(clazz) tup_throw_exception(ERR_CLASS_UNINIT, "%s instance is not initialized.", clazz) +#define PARAM_FMT_EXCEPTION() tup_throw_exception(ERR_DATA_FORMAT_ERROR, "Parameter format is incorrect.") +#define TYPE_EXCEPTOIN() tup_throw_exception(ERR_TYPE_INVALID, "Data type is invalid.") +#define NAME_EXCEPTOIN() tup_throw_exception(ERR_WRONG_PARAMS, "The length of the package name too short.") +#define READ_MAP_EXCEPTION() tup_throw_exception(ERR_READ_MAP_ERROR, "Read map failed, please check binary buffer.") +#define TUP_SET_CONTEXT_EXCEPTION() tup_throw_exception(ERR_SET_CONTEXT_ERROR, "Set context while encoding failed, please check contexts parameter while encoding.") +#define TUP_SET_STATUS_EXCEPTION() tup_throw_exception(ERR_SET_STATUS_ERROR, "Set status while encoding failed, please check statuses parameter while encoding.") +#define ENCODE_BUF_EXCEPTION() tup_throw_exception(ERR_ENCODE_BUF_ERROR, "Encode buf while encoding failed, please check in buf array parameter while encoding.") +#define ENCODE_WRITE_IVERSION_EXCEPTION() tup_throw_exception(ERR_WRITE_IVERSION_ERROR, "Encode failed while writing iversion.") +#define ENCODE_WRITE_CPACKETTYPE_EXCEPTION() tup_throw_exception(ERR_WRITE_CPACKETTYPE_ERROR, "Encode failed while writing cpackettype .") #define ENCODE_WRITE_IMESSAGETYPE_EXCEPTION() tup_throw_exception(ERR_WRITE_IMESSAGETYPE_ERROR, "Encode failed while writing imessagetype .") -#define ENCODE_WRITE_IREQUESTID_EXCEPTION() tup_throw_exception(ERR_WRITE_IREQUESTID_ERROR, "Encode failed while writing irequestid .") +#define ENCODE_WRITE_IREQUESTID_EXCEPTION() tup_throw_exception(ERR_WRITE_IREQUESTID_ERROR, "Encode failed while writing irequestid .") #define ENCODE_WRITE_SSERVANTNAME_EXCEPTION() tup_throw_exception(ERR_WRITE_SSERVANTNAME_ERROR, "Encode failed while writing SSERVANTNAME .") -#define ENCODE_WRITE_SFUNCNAME_EXCEPTION() tup_throw_exception(ERR_WRITE_SFUNCNAME_ERROR, "Encode failed while writing SFUNCNAME .") -#define ENCODE_WRITE_SBUFFER_EXCEPTION() tup_throw_exception(ERR_WRITE_SBUFFER_ERROR, "Encode failed while writing SBUFFER .") -#define ENCODE_WRITE_ITIMEOUT_EXCEPTION() tup_throw_exception(ERR_WRITE_ITIMEOUT_ERROR, "Encode failed while writing ITIMEOUT .") -#define ENCODE_WRITE_CONTEXT_EXCEPTION() tup_throw_exception(ERR_WRITE_CONTEXT_ERROR, "Encode failed while writing CONTEXT .") -#define ENCODE_WRITE_STATUS_EXCEPTION() tup_throw_exception(ERR_WRITE_STATUS_ERROR, "Encode failed while writing STATUS .") - +#define ENCODE_WRITE_SFUNCNAME_EXCEPTION() tup_throw_exception(ERR_WRITE_SFUNCNAME_ERROR, "Encode failed while writing SFUNCNAME .") +#define ENCODE_WRITE_SBUFFER_EXCEPTION() tup_throw_exception(ERR_WRITE_SBUFFER_ERROR, "Encode failed while writing SBUFFER .") +#define ENCODE_WRITE_ITIMEOUT_EXCEPTION() tup_throw_exception(ERR_WRITE_ITIMEOUT_ERROR, "Encode failed while writing ITIMEOUT .") +#define ENCODE_WRITE_CONTEXT_EXCEPTION() tup_throw_exception(ERR_WRITE_CONTEXT_ERROR, "Encode failed while writing CONTEXT .") +#define ENCODE_WRITE_STATUS_EXCEPTION() tup_throw_exception(ERR_WRITE_STATUS_ERROR, "Encode failed while writing STATUS .") PHP_MINIT_FUNCTION(phptars); -PHP_MSHUTDOWN_FUNCTION(phptars); +PHP_MINFO_FUNCTION(phptars); PHP_RINIT_FUNCTION(phptars); PHP_RSHUTDOWN_FUNCTION(phptars); -PHP_MINFO_FUNCTION(phptars); +PHP_MSHUTDOWN_FUNCTION(phptars); -void tup_throw_exception(long err_code, char * fmt, ...); +void tup_throw_exception(long err_code, char *fmt, ...); -#define TUP_NAME_MIN_LEN 1 +#define TUP_NAME_MIN_LEN 1 #ifdef ZTS #define PHPTARS_G(v) TSRMG(phptars_globals_id, zend_phptars_globals *, v) @@ -146,13 +104,3 @@ void tup_throw_exception(long err_code, char * fmt, ...); #endif #endif /* PHP_TTUP_H */ - - -/* - * Local variables: - * tab-width: 4 - * c-basic-offset: 4 - * End: - * vim600: noet sw=4 ts=4 fdm=marker - * vim<600: noet sw=4 ts=4 - */ diff --git a/include/tars_c.h b/include/tars_c.h index e2d1662..2b38fb2 100644 --- a/include/tars_c.h +++ b/include/tars_c.h @@ -28,15 +28,15 @@ typedef struct { Int32 high; Int32 low; -}MTKInt64; +} MTKInt64; #ifdef __MTK_64 -typedef MTKInt64 Int64; +typedef MTKInt64 Int64; #else #if __WORDSIZE == 64 -typedef long Int64; +typedef long Int64; #else -typedef long long Int64; +typedef long long Int64; #endif #endif @@ -48,51 +48,38 @@ typedef long long Int64; #endif #ifndef TarsMalloc -#define TarsMalloc(nsize) malloc(nsize); +#define TarsMalloc(nsize) malloc(nsize); #endif #ifndef TarsFree -#define TarsFree(p) free(p); +#define TarsFree(p) free(p); #endif -//����ֵ���� -extern const Int32 TARS_SUCCESS ; //�ɹ� -extern const Int32 TARS_ATTR_NOT_FOUND ; //���Ҳ���������� -extern const Int32 TARS_ENCODE_ERROR ; //������� -extern const Int32 TARS_DECODE_ERROR ; //������� -extern const Int32 TARS_RUNTIME_ERROR ; //��������ʱ���� -extern const Int32 TARS_MALLOC_ERROR ; //�ڴ�����ʧ�ܴ��� -extern const Int32 TARS_DECODE_EOPNEXT ; +extern const Int32 TARS_SUCCESS; +extern const Int32 TARS_ATTR_NOT_FOUND; +extern const Int32 TARS_ENCODE_ERROR; +extern const Int32 TARS_DECODE_ERROR; +extern const Int32 TARS_RUNTIME_ERROR; +extern const Int32 TARS_MALLOC_ERROR; +extern const Int32 TARS_DECODE_EOPNEXT; #ifndef TARS_MAX_STRING_LENGTH -#define TARS_MAX_STRING_LENGTH (100 * 1024 * 1024) +#define TARS_MAX_STRING_LENGTH (100 * 1024 * 1024) #endif #define tars__bswap_constant_16(x) \ ((((x) & 0xff00) >> 8) | (((x) & 0x00ff) << 8)); #define tars__bswap_constant_32(x) \ - ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) | \ - (((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24)); - -/* -#define tars__bswap_constant_64(x) \ - ((((x) & 0xff00000000000000ull) >> 56) \ - | (((x) & 0x00ff000000000000ull) >> 40) \ - | (((x) & 0x0000ff0000000000ull) >> 24) \ - | (((x) & 0x000000ff00000000ull) >> 8) \ - | (((x) & 0x00000000ff000000ull) << 8) \ - | (((x) & 0x0000000000ff0000ull) << 24) \ - | (((x) & 0x000000000000ff00ull) << 40) \ - | (((x) & 0x00000000000000ffull) << 56)); - */ - -Int64 tars__bswap_constant_64(Int64 x); - -Int64 tars_ntohll(Int64 x); -Float tars_ntohf(Float x); + ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) | \ + (((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24)); + +Int64 tars__bswap_constant_64(Int64 x); +Int64 tars_ntohll(Int64 x); +Float tars_ntohf(Float x); Double tars_ntohd(Double x); -#if 0// __BYTE_ORDER == __BIG_ENDIAN +#if 0 +// __BYTE_ORDER == __BIG_ENDIAN # define tars_ntohll(x) (x); # define tars_htonll(x) (x); # define tars_ntohf(x) (x); @@ -105,194 +92,134 @@ Double tars_ntohd(Double x); # define tars_htonl(x) (x); #else //# if __BYTE_ORDER == __LITTLE_ENDIAN - -# define tars_htonll(x) tars_ntohll(x); -# define tars_htonf(x) tars_ntohf(x); -# define tars_htond(x) tars_ntohd(x); -# define tars_ntohs(x) tars__bswap_constant_16(x); -# define tars_htons(x) tars__bswap_constant_16(x); -# define tars_ntohl(x) tars__bswap_constant_32(x); -# define tars_htonl(x) tars__bswap_constant_32(x); - +# define tars_htonll(x) tars_ntohll(x); +# define tars_htonf(x) tars_ntohf(x); +# define tars_htond(x) tars_ntohd(x); +# define tars_ntohs(x) tars__bswap_constant_16(x); +# define tars_htons(x) tars__bswap_constant_16(x); +# define tars_ntohl(x) tars__bswap_constant_32(x); +# define tars_htonl(x) tars__bswap_constant_32(x); #endif //////////////////////////////////////////////////////////////////////////////////////////////////// +typedef struct JArray JArray; +typedef struct JString JString; typedef struct JStructBase JStructBase; -typedef struct JString JString; -typedef struct JArray JArray; typedef struct JMapWrapper JMapWrapper; struct DataHead; struct TarsStream; -typedef struct DataHead DataHead; -typedef struct TarsStream TarsStream; -typedef struct TarsStream TarsInputStream; -typedef struct TarsStream TarsOutputStream; - +typedef struct DataHead DataHead; +typedef struct TarsStream TarsStream; +typedef struct TarsStream TarsInputStream; +typedef struct TarsStream TarsOutputStream; struct JStructBase { - char * className; - Int32 (*writeTo)(const void*, TarsOutputStream *); - Int32 (*readFrom)(void*, TarsInputStream *); + char *className; + Int32 (*readFrom)(void *, TarsInputStream *); + Int32 (*writeTo)(const void *, TarsOutputStream *); }; -//string��װ struct JString { - char * _data; + char *_data; unsigned int _len; unsigned int _buf_len; }; +void JString_copy(char *dest, const char *src, uint32_t len); void JString_del(JString **st); - - -void JString_copy(char * dest, const char * src, uint32_t len); - -Int32 JString_copyChar(JString * s, char * data, uint32_t len); - void JString_clear(JString *s); +int JString_empty(JString *s); +char *JString_data(JString *s); -Int32 JString_reserve(JString *s, uint32_t n); - -Int32 JString_assign(JString *s, const char * data, uint32_t len); - -Int32 JString_append(JString * s, const char * data, uint32_t len); - +Int32 JString_assign(JString *s, const char *data, uint32_t len); +Int32 JString_append(JString *s, const char *data, uint32_t len); Int32 JString_insert(JString *s, uint32_t pos, char v); - -uint32_t JString_size(JString *s); -int JString_empty(JString *s); -uint32_t JString_capacity(JString *s); -char * JString_data(JString *s); +Int32 JString_reserve(JString *s, uint32_t n); +Int32 JString_copyChar(JString *s, char *data, uint32_t len); Int32 JString_resize(JString *s, uint32_t n); - - Int32 JString_init(JString *s); -JString * JString_new(void); +uint32_t JString_size(JString *s); +uint32_t JString_capacity(JString *s); +JString *JString_new(void); -//�����װ struct JArray { - char * elem_type_name;//�������� + char *elem_type_name; unsigned elem_num; unsigned list_len; - int * list; + int *list; unsigned buff_used; unsigned buff_len; - char * buff; //Ԫ�ص�tars������ + char *buff; }; -void JArray_del(JArray ** arr); +void JArray_del(JArray **arr); Int32 JArray_reserveList(JArray *arr, uint32_t num); - Int32 JArray_reserveBuff(JArray *arr, uint32_t len); +Int32 JArray_pushBack(JArray *arr, const char *data, uint32_t len); +Int32 JArray_pushBackString(JArray *arr, const char *data); -Int32 JArray_pushBack(JArray *arr, const char * data, uint32_t len); - -Int32 JArray_pushBackString(JArray *arr, const char * data); - -int JArray_get(JArray *arr, unsigned index, char *data, uint32_t * len); - +int JArray_get(JArray *arr, unsigned index, char *data, uint32_t *len); char *JArray_getPtr(JArray *arr, unsigned index); - -int JArray_getLength(JArray *arr, unsigned index); - -int JArray_size(JArray *arr); - -int JArray_empty(JArray *arr); - +int JArray_getLength(JArray *arr, unsigned index); +int JArray_size(JArray *arr); +int JArray_empty(JArray *arr); void JArray_clear(JArray *arr); - void JArray_init(JArray *arr); +JArray *JArray_new(const char *type); -JArray * JArray_new(const char * type); - - - -//map��װ struct JMapWrapper { - JArray * first; - JArray * second; + JArray *first; + JArray *second; }; -void JMapWrapper_del(JMapWrapper ** m); - -int JMapWrapper_put(JMapWrapper *m, const char * first_data, unsigned first_len, const char *value_data, unsigned value_len); - -int JMapWrapper_find(JMapWrapper *m, const char * first_data, unsigned first_len, char **value_data, unsigned * value_len); - -int JMapWrapper_size(JMapWrapper * m); - -int JMapWrapper_getByIndex(JMapWrapper *m, unsigned index, char *first, uint32_t * first_len, char* second, uint32_t * second_len); - -int JMapWrapper_empty(JMapWrapper *m); - +int JMapWrapper_put(JMapWrapper *m, const char *first_data, unsigned first_len, const char *value_data, unsigned value_len); +int JMapWrapper_find(JMapWrapper *m, const char *first_data, unsigned first_len, char **value_data, unsigned *value_len); +int JMapWrapper_size(JMapWrapper *m); +int JMapWrapper_getByIndex(JMapWrapper *m, unsigned index, char *first, uint32_t *first_len, char *second, uint32_t *second_len); +int JMapWrapper_empty(JMapWrapper *m); +void JMapWrapper_del(JMapWrapper **m); void JMapWrapper_clear(JMapWrapper *m); - void JMapWrapper_init(JMapWrapper *m); - -JMapWrapper * JMapWrapper_new(const char * first_type, const char * second_type); - +JMapWrapper *JMapWrapper_new(const char *first_type, const char *second_type); enum { - eChar = 0, - eShort = 1, - eInt32 = 2, - eInt64 = 3, - eFloat = 4, - eDouble = 5, - eString1 = 6, - eString4 = 7, - eMap = 8, - eList = 9, + eChar = 0, + eShort = 1, + eInt32 = 2, + eInt64 = 3, + eFloat = 4, + eDouble = 5, + eString1 = 6, + eString4 = 7, + eMap = 8, + eList = 9, eStructBegin = 10, - eStructEnd = 11, - eZeroTag = 12, - eSimpleList = 13 + eStructEnd = 11, + eZeroTag = 12, + eSimpleList = 13 }; //////////////////////////////////////////////////////////////////////////////////////////////////// - - -//��������д����װ struct TarsStream { - JString * _buf; //< ������ - uint32_t _cur; //< ��ǰλ�� - char _err[32]; //< �������� - DataHead * _h; //< ����Э��ͷ���� + JString *_buf; + uint32_t _cur; + char _err[32]; + DataHead *_h; }; -/* -/// ��������ȡ����װ -struct TarsInputStream -{ - JString * _buf; //< ������ - uint32_t _cur; //< ��ǰλ�� - char _err[32]; //< �������� - DataHead * _h; // ����Э��ͷ���� -}; - -//////////////////////////////////////////////////////////////////////////// -/// ������д������װ -struct TarsOutputStream -{ - JString * _buf; //< ������ - char _err[32]; //< �������� - DataHead * _h; -}; -*/ - struct DataHead { uint8_t _type; @@ -301,168 +228,92 @@ struct DataHead typedef struct helper { - //unsigned int type : 4; - //unsigned int tag : 4; unsigned char type_tag; } helper; -//}__attribute__((packed)) helper; -uint8_t helper_getTag(helper *h); void helper_setTag(helper *h, unsigned int t); - -uint8_t helper_getType(helper *h); - void helper_setType(helper *h, unsigned int t); +void DataHead_del(DataHead **head); +void DataHead_init(DataHead *head); -void DataHead_del(DataHead ** head); - -uint8_t DataHead_getTag(DataHead * head); -//void DataHead_setTag(DataHead * head, uint8_t t); -uint8_t DataHead_getType(DataHead * head); -//void DataHead_setType(DataHead * head, uint8_t t); - -/// ��ȡͷ��Ϣ������ǰ������ƫ���� -Int32 DataHead_peekFrom(DataHead * head, TarsInputStream* is, uint32_t *n); - -/// ��ȡ����ͷ��Ϣ -Int32 DataHead_readFrom(DataHead * head, TarsInputStream* is); - -/// д������ͷ��Ϣ -Int32 DataHead_writeTo(DataHead * head, TarsOutputStream* os); - -//����type tag ��д��os -Int32 DataHead_setAndWriteTo(DataHead * head, unsigned int type, unsigned int tag,TarsOutputStream* os); +uint8_t helper_getTag(helper *h); +uint8_t helper_getType(helper *h); +uint8_t DataHead_getTag(DataHead *head); +uint8_t DataHead_getType(DataHead *head); +Int32 DataHead_peekFrom(DataHead *head, TarsInputStream *is, uint32_t *n); +Int32 DataHead_readFrom(DataHead *head, TarsInputStream *is); +Int32 DataHead_writeTo(DataHead *head, TarsOutputStream *os); +Int32 DataHead_setAndWriteTo(DataHead *head, unsigned int type, unsigned int tag,TarsOutputStream *os); -void DataHead_init(DataHead * head); +DataHead *DataHead_new(void); -DataHead * DataHead_new(void); //////////////////////////////////////////////////////////////////////////// - -void TarsInputStream_del(TarsInputStream ** is); - -void TarsInputStream_reset(TarsInputStream * is); - -/// ��ȡ���� -Int32 TarsInputStream_readBuf(TarsInputStream * is, void * buf, uint32_t len); - -/// ��ȡ���棬�����ı�ƫ���� -Int32 TarsInputStream_peekBuf(TarsInputStream * is, void * buf, uint32_t len, uint32_t offset); - -/// ����len���ֽ� -Int32 TarsInputStream_skip(TarsInputStream * is, uint32_t len); - -/// ���û��� -Int32 TarsInputStream_setBuffer(TarsInputStream * is, const char * buf, uint32_t len); - - -/// ����ָ����ǩ��Ԫ��ǰ -Int32 TarsInputStream_skipToTag(TarsInputStream * is, uint8_t tag); - -/// ������ǰ�ṹ�Ľ��� -Int32 TarsInputStream_skipToStructEnd(TarsInputStream * is); - -/// ����һ���ֶ� -Int32 TarsInputStream_skipField(TarsInputStream * is); - -/// ����һ���ֶΣ�������ͷ��Ϣ -Int32 TarsInputStream_skipFieldByType(TarsInputStream * is, uint8_t type); - -Int32 TarsInputStream_checkValid(TarsInputStream * is, uint8_t tag, Bool isRequire); - -Int32 TarsInputStream_readByChar(TarsInputStream * is, Char * n); - -Int32 TarsInputStream_readByShort(TarsInputStream * is, Short * n); - -Int32 TarsInputStream_readByInt32(TarsInputStream * is, Int32 * n); - -Int32 TarsInputStream_readBool (TarsInputStream * is, Bool* b, uint8_t tag, Bool isRequire); - -Int32 TarsInputStream_readChar (TarsInputStream * is, Char* c, uint8_t tag, Bool isRequire); - -Int32 TarsInputStream_readUInt8 (TarsInputStream * is, UInt8 * n, uint8_t tag, Bool isRequire); - -Int32 TarsInputStream_readShort (TarsInputStream * is, Short * n, uint8_t tag, Bool isRequire); - -Int32 TarsInputStream_readUInt16 (TarsInputStream * is, UInt16 * n, uint8_t tag, Bool isRequire); - -Int32 TarsInputStream_readInt32 (TarsInputStream * is, Int32 * n, uint8_t tag, Bool isRequire); - -Int32 TarsInputStream_readUInt32 (TarsInputStream * is, UInt32 * n, uint8_t tag, Bool isRequire); - -Int32 TarsInputStream_readInt64(TarsInputStream * is, Int64* n, uint8_t tag, Bool isRequire); - -Int32 TarsInputStream_readFloat(TarsInputStream * is, Float* n, uint8_t tag, Bool isRequire); - -Int32 TarsInputStream_readDouble(TarsInputStream * is, Double* n, uint8_t tag, Bool isRequire); - -Int32 TarsInputStream_readString(TarsInputStream * is, JString* s, uint8_t tag, Bool isRequire); - -Int32 TarsInputStream_readMap(TarsInputStream * is, JMapWrapper* m, uint8_t tag, Bool isRequire); - -Int32 TarsInputStream_readVector(TarsInputStream * is, JArray* v, uint8_t tag, Bool isRequire); - -Int32 TarsInputStream_readVectorChar(TarsInputStream * is, JString *v, uint8_t tag, Bool isRequire); - -/// ��ȡ�ṹ -Int32 TarsInputStream_readStruct(TarsInputStream * is, void *st, uint8_t tag, Bool isRequire); - -Int32 TarsInputStream_readStructString(TarsInputStream * is, JString * st, uint8_t tag, Bool isRequire); - -Int32 TarsInputStream_init(TarsInputStream* is); - -TarsInputStream * TarsInputStream_new(void); +void TarsInputStream_del(TarsInputStream **is); +void TarsInputStream_reset(TarsInputStream *is); + +Int32 TarsInputStream_readBuf(TarsInputStream *is, void *buf, uint32_t len); +Int32 TarsInputStream_peekBuf(TarsInputStream *is, void *buf, uint32_t len, uint32_t offset); +Int32 TarsInputStream_skip(TarsInputStream *is, uint32_t len); +Int32 TarsInputStream_setBuffer(TarsInputStream *is, const char *buf, uint32_t len); +Int32 TarsInputStream_skipToTag(TarsInputStream *is, uint8_t tag); +Int32 TarsInputStream_skipToStructEnd(TarsInputStream *is); +Int32 TarsInputStream_skipField(TarsInputStream *is); +Int32 TarsInputStream_skipFieldByType(TarsInputStream *is, uint8_t type); +Int32 TarsInputStream_checkValid(TarsInputStream *is, uint8_t tag, Bool isRequire); +Int32 TarsInputStream_readByChar(TarsInputStream *is, Char *n); +Int32 TarsInputStream_readByShort(TarsInputStream *is, Short *n); +Int32 TarsInputStream_readByInt32(TarsInputStream *is, Int32 *n); +Int32 TarsInputStream_readBool(TarsInputStream *is, Bool *b, uint8_t tag, Bool isRequire); +Int32 TarsInputStream_readChar(TarsInputStream *is, Char *c, uint8_t tag, Bool isRequire); +Int32 TarsInputStream_readUInt8(TarsInputStream *is, UInt8 *n, uint8_t tag, Bool isRequire); +Int32 TarsInputStream_readShort(TarsInputStream *is, Short *n, uint8_t tag, Bool isRequire); +Int32 TarsInputStream_readUInt16(TarsInputStream *is, UInt16 *n, uint8_t tag, Bool isRequire); +Int32 TarsInputStream_readInt32(TarsInputStream *is, Int32 *n, uint8_t tag, Bool isRequire); +Int32 TarsInputStream_readUInt32(TarsInputStream *is, UInt32 *n, uint8_t tag, Bool isRequire); +Int32 TarsInputStream_readInt64(TarsInputStream *is, Int64 *n, uint8_t tag, Bool isRequire); +Int32 TarsInputStream_readFloat(TarsInputStream *is, Float *n, uint8_t tag, Bool isRequire); +Int32 TarsInputStream_readDouble(TarsInputStream *is, Double *n, uint8_t tag, Bool isRequire); +Int32 TarsInputStream_readString(TarsInputStream *is, JString *s, uint8_t tag, Bool isRequire); +Int32 TarsInputStream_readMap(TarsInputStream *is, JMapWrapper *m, uint8_t tag, Bool isRequire); +Int32 TarsInputStream_readVector(TarsInputStream *is, JArray *v, uint8_t tag, Bool isRequire); +Int32 TarsInputStream_readVectorChar(TarsInputStream *is, JString *v, uint8_t tag, Bool isRequire); +Int32 TarsInputStream_readStruct(TarsInputStream *is, void *st, uint8_t tag, Bool isRequire); +Int32 TarsInputStream_readStructString(TarsInputStream *is, JString *st, uint8_t tag, Bool isRequire); +Int32 TarsInputStream_init(TarsInputStream *is); + +TarsInputStream *TarsInputStream_new(void); //////////////////////////////////////////////// -void TarsOutputStream_del(TarsOutputStream ** os); - -void TarsOutputStream_reset(TarsOutputStream * os); - -Int32 TarsOutputStream_writeBuf(TarsOutputStream * os, const void * buf, uint32_t len); - -char * TarsOutputStream_getBuffer(TarsOutputStream * os); - -uint32_t TarsOutputStream_getLength(TarsOutputStream * os); - -Int32 TarsOutputStream_writeBool(TarsOutputStream * os, Bool b, uint8_t tag); - -Int32 TarsOutputStream_writeChar(TarsOutputStream * os, Char n, uint8_t tag); - -Int32 TarsOutputStream_writeUInt8(TarsOutputStream * os, UInt8 n, uint8_t tag); - -Int32 TarsOutputStream_writeShort(TarsOutputStream * os, Short n, uint8_t tag); - -Int32 TarsOutputStream_writeUInt16(TarsOutputStream * os, UInt16 n, uint8_t tag); - -Int32 TarsOutputStream_writeInt32(TarsOutputStream * os, Int32 n, uint8_t tag); - -Int32 TarsOutputStream_writeUInt32(TarsOutputStream * os, UInt32 n, uint8_t tag); - -Int32 TarsOutputStream_writeInt64(TarsOutputStream * os, Int64 n, uint8_t tag); - -Int32 TarsOutputStream_writeFloat(TarsOutputStream * os, Float n, uint8_t tag); - -Int32 TarsOutputStream_writeDouble(TarsOutputStream * os, Double n, uint8_t tag); - -Int32 TarsOutputStream_writeString(TarsOutputStream * os, JString* s, uint8_t tag); -Int32 TarsOutputStream_writeStringBuffer(TarsOutputStream * os, const char* buff, uint32_t len, uint8_t tag); - -Int32 TarsOutputStream_writeMap(TarsOutputStream * os, JMapWrapper* m, uint8_t tag); - -Int32 TarsOutputStream_writeVector(TarsOutputStream * os, JArray* v, uint8_t tag); - -Int32 TarsOutputStream_writeVectorChar(TarsOutputStream * os, JString *v, uint8_t tag); -Int32 TarsOutputStream_writeVectorCharBuffer(TarsOutputStream * os, const char* buff, uint32_t len, uint8_t tag); - -Int32 TarsOutputStream_writeStruct(TarsOutputStream * os,const void * st, uint8_t tag); -Int32 TarsOutputStream_writeStructString(TarsOutputStream * os, JString * v, uint8_t tag); -Int32 TarsOutputStream_writeStructBuffer(TarsOutputStream * os, const char* buff, uint32_t len, uint8_t tag); - -Int32 TarsOutputStream_init(TarsOutputStream * os); - -TarsOutputStream * TarsOutputStream_new(void); +void TarsOutputStream_del(TarsOutputStream **os); +void TarsOutputStream_reset(TarsOutputStream *os); +char *TarsOutputStream_getBuffer(TarsOutputStream *os); + +uint32_t TarsOutputStream_getLength(TarsOutputStream *os); + +Int32 TarsOutputStream_writeBuf(TarsOutputStream *os, const void *buf, uint32_t len); +Int32 TarsOutputStream_writeBool(TarsOutputStream *os, Bool b, uint8_t tag); +Int32 TarsOutputStream_writeChar(TarsOutputStream *os, Char n, uint8_t tag); +Int32 TarsOutputStream_writeUInt8(TarsOutputStream *os, UInt8 n, uint8_t tag); +Int32 TarsOutputStream_writeShort(TarsOutputStream *os, Short n, uint8_t tag); +Int32 TarsOutputStream_writeUInt16(TarsOutputStream *os, UInt16 n, uint8_t tag); +Int32 TarsOutputStream_writeInt32(TarsOutputStream *os, Int32 n, uint8_t tag); +Int32 TarsOutputStream_writeUInt32(TarsOutputStream *os, UInt32 n, uint8_t tag); +Int32 TarsOutputStream_writeInt64(TarsOutputStream *os, Int64 n, uint8_t tag); +Int32 TarsOutputStream_writeFloat(TarsOutputStream *os, Float n, uint8_t tag); +Int32 TarsOutputStream_writeDouble(TarsOutputStream *os, Double n, uint8_t tag); +Int32 TarsOutputStream_writeString(TarsOutputStream *os, JString *s, uint8_t tag); +Int32 TarsOutputStream_writeStringBuffer(TarsOutputStream *os, const char *buff, uint32_t len, uint8_t tag); +Int32 TarsOutputStream_writeMap(TarsOutputStream *os, JMapWrapper *m, uint8_t tag); +Int32 TarsOutputStream_writeVector(TarsOutputStream *os, JArray *v, uint8_t tag); +Int32 TarsOutputStream_writeVectorChar(TarsOutputStream *os, JString *v, uint8_t tag); +Int32 TarsOutputStream_writeVectorCharBuffer(TarsOutputStream *os, const char *buff, uint32_t len, uint8_t tag); +Int32 TarsOutputStream_writeStruct(TarsOutputStream *os,const void *st, uint8_t tag); +Int32 TarsOutputStream_writeStructString(TarsOutputStream *os, JString *v, uint8_t tag); +Int32 TarsOutputStream_writeStructBuffer(TarsOutputStream *os, const char *buff, uint32_t len, uint8_t tag); +Int32 TarsOutputStream_init(TarsOutputStream *os); + +TarsOutputStream *TarsOutputStream_new(void); -Int32 Tars_readString(char* src, char ** output); -Int32 Tars_readStringLen(char* src); #endif diff --git a/include/ttars.h b/include/ttars.h index 4189fb1..09a87aa 100644 --- a/include/ttars.h +++ b/include/ttars.h @@ -35,19 +35,19 @@ #define STRUCT_NAME_MIN 2 /* vector */ -#define VECTOR_PROP_TYPE "__type" +#define VECTOR_PROP_TYPE "__type" #define VECTOR_PROP_TYPE_CLASS "__typeClass" -#define VECTOR_PROP_ORIG_TYPE TTARS_TYPE_VECTOR -#define TARS_PROP_VEC_COUNT "_count" -#define TARS_PROP_VEC_POS "_position" +#define VECTOR_PROP_ORIG_TYPE TTARS_TYPE_VECTOR +#define TARS_PROP_VEC_COUNT "_count" +#define TARS_PROP_VEC_POS "_position" /* map */ #define MAP_PROP_ORIG_TYPE TTARS_TYPE_MAP +#define MAP_PROP_FIRST_TYPE "__ft" +#define MAP_PROP_SECOND_TYPE "__st" #define MAP_PROP_PARAM_FORMAT "__format" -#define MAP_PROP_FIRST_TYPE "__ft" -#define MAP_PROP_SECOND_TYPE "__st" #define MAP_FIRST_TYPE_NAME "__ftn" #define MAP_SECOND_TYPE_NAME "__stn" @@ -73,62 +73,49 @@ #define PHP_TTARS_MAP "MAP" #define PHP_TTARS_STRUCT "STRUCT" -#define IS_CLASS_VECTOR(t) ((t) && (Z_TYPE_P(t) == IS_OBJECT) && instanceof_function(Z_OBJCE_P(t), tars_vector_ce TSRMLS_CC)) -#define IS_CLASS_MAP(t) ((t) && (Z_TYPE_P(t) == IS_OBJECT) && instanceof_function(Z_OBJCE_P(t), tars_map_ce TSRMLS_CC)) -#define IS_CLASS_STRUCT(t) ((t) && (Z_TYPE_P(t) == IS_OBJECT) && instanceof_function(Z_OBJCE_P(t), tars_struct_ce TSRMLS_CC)) +#define IS_CLASS_VECTOR(t) ((t) && (Z_TYPE_P(t) == IS_OBJECT) && instanceof_function(Z_OBJCE_P(t), tars_vector_ce)) +#define IS_CLASS_MAP(t) ((t) && (Z_TYPE_P(t) == IS_OBJECT) && instanceof_function(Z_OBJCE_P(t), tars_map_ce)) +#define IS_CLASS_STRUCT(t) ((t) && (Z_TYPE_P(t) == IS_OBJECT) && instanceof_function(Z_OBJCE_P(t), tars_struct_ce)) #define IS_VALID_TYPE(t) ((t) >= TTARS_TYPE_BOOL && (t) <= TTARS_TYPE_STRUCT) #define IS_BASE_TYPE(t) ((t) >= TTARS_TYPE_BOOL && (t) <= TTARS_TYPE_STRING) #define IS_JSTRING(t) ((t) == TTARS_TYPE_CHAR) #define IS_STRUCT(t) ((t) == TTARS_TYPE_STRUCT) -typedef int (* tars_pack_func_t)(zval *, TarsOutputStream *, uint8_t, void *); +typedef int (*tars_pack_func_t)(zval *, TarsOutputStream *, uint8_t, void *); typedef union vector_ctx { - JArray * vct; - JString * str; + JArray *vct; + JString *str; } vector_ctx; -#if PHP_MAJOR_VERSION < 7 -typedef struct { - zend_object std; - zend_uchar t; - vector_ctx * ctx; -} vector_wrapper ; - -typedef struct { - zend_object std; - JMapWrapper * ctx; -} map_wrapper ; -#else typedef struct { zend_uchar t; - vector_ctx * ctx; + vector_ctx *ctx; HashTable *props; zend_object std; -} vector_wrapper ; +} vector_wrapper; typedef struct { - JMapWrapper * ctx; + JMapWrapper *ctx; HashTable *props; zend_object std; -} map_wrapper ; -#endif - -int bool_packer(zval * , TarsOutputStream *, uint8_t, void *); -int char_packer(zval * , TarsOutputStream *, uint8_t, void *); -int uint8_packer(zval * , TarsOutputStream *, uint8_t, void *); -int short_packer(zval * , TarsOutputStream *, uint8_t, void *); -int uint16_packer(zval * , TarsOutputStream *, uint8_t, void *); -int int32_packer(zval * , TarsOutputStream *, uint8_t, void *); -int uint32_packer(zval * , TarsOutputStream *, uint8_t, void *); -int int64_packer(zval * , TarsOutputStream *, uint8_t, void *); -int string_packer(zval * , TarsOutputStream *, uint8_t, void *); -int double_packer(zval * , TarsOutputStream *, uint8_t, void *); -int float_packer(zval * , TarsOutputStream *, uint8_t, void *); -int vector_packer(zval * , TarsOutputStream *, uint8_t, void *); -int map_packer(zval * , TarsOutputStream *, uint8_t, void *); -int struct_packer(zval * occupy, TarsOutputStream *, uint8_t,void *); +} map_wrapper; + +int bool_packer(zval *, TarsOutputStream *, uint8_t, void *); +int char_packer(zval *, TarsOutputStream *, uint8_t, void *); +int uint8_packer(zval *, TarsOutputStream *, uint8_t, void *); +int short_packer(zval *, TarsOutputStream *, uint8_t, void *); +int uint16_packer(zval *, TarsOutputStream *, uint8_t, void *); +int int32_packer(zval *, TarsOutputStream *, uint8_t, void *); +int uint32_packer(zval *, TarsOutputStream *, uint8_t, void *); +int int64_packer(zval *, TarsOutputStream *, uint8_t, void *); +int string_packer(zval *, TarsOutputStream *, uint8_t, void *); +int double_packer(zval *, TarsOutputStream *, uint8_t, void *); +int float_packer(zval *, TarsOutputStream *, uint8_t, void *); +int vector_packer(zval *, TarsOutputStream *, uint8_t, void *); +int map_packer(zval *, TarsOutputStream *, uint8_t, void *); +int struct_packer(zval *occupy, TarsOutputStream *, uint8_t,void *); static const tars_pack_func_t packer_dispatch[] = { NULL, @@ -148,24 +135,25 @@ static const tars_pack_func_t packer_dispatch[] = { struct_packer }; -int bool_unpacker(TarsInputStream * stream, uint8_t tag, Bool is_require, zval * this_ptr, void ** zv); -int char_unpacker(TarsInputStream * stream, uint8_t tag, Bool is_require, zval * this_ptr, void ** zv); -int uint8_unpacker(TarsInputStream * stream, uint8_t tag, Bool is_require, zval * this_ptr, void ** zv); -int short_unpacker(TarsInputStream * stream, uint8_t tag, Bool is_require, zval * this_ptr, void ** zv); -int uint16_unpacker(TarsInputStream * stream, uint8_t tag, Bool is_require, zval * this_ptr, void ** zv); -int float_unpacker(TarsInputStream * stream, uint8_t tag, Bool is_require, zval * this_ptr, void ** zv); -int double_unpacker(TarsInputStream * stream, uint8_t tag, Bool is_require, zval * this_ptr, void ** zv); -int int64_unpacker(TarsInputStream * stream, uint8_t tag, Bool is_require, zval * this_ptr, void ** zv); - -int int32_unpacker(TarsInputStream * stream, uint8_t tag, Bool is_require, zval * this_ptr, void ** zv); -int uint32_unpacker(TarsInputStream * stream, uint8_t tag, Bool is_require, zval * this_ptr, void ** zv); -int string_unpacker(TarsInputStream * stream, uint8_t tag, Bool is_require, zval * this_ptr, void ** zv); -int vector_unpacker(TarsInputStream * stream, uint8_t tag, Bool is_require, zval * this_ptr, void ** zv); -int map_unpacker(TarsInputStream * stream, uint8_t tag, Bool is_require, zval * this_ptr, void ** zv); -int struct_unpacker(TarsInputStream * stream, uint8_t tag, Bool is_require, zval * this_ptr,void ** zv); - -typedef int (* tars_unpack_func_t)(TarsOutputStream *, uint8_t, Bool, zval *, void **); -static const tars_unpack_func_t unpacker_dispatch[] = { +int bool_unpacker(TarsInputStream *stream, uint8_t tag, Bool is_require, zval *this_ptr, void **zv); +int char_unpacker(TarsInputStream *stream, uint8_t tag, Bool is_require, zval *this_ptr, void **zv); +int uint8_unpacker(TarsInputStream *stream, uint8_t tag, Bool is_require, zval *this_ptr, void **zv); +int short_unpacker(TarsInputStream *stream, uint8_t tag, Bool is_require, zval *this_ptr, void **zv); +int uint16_unpacker(TarsInputStream *stream, uint8_t tag, Bool is_require, zval *this_ptr, void **zv); +int float_unpacker(TarsInputStream *stream, uint8_t tag, Bool is_require, zval *this_ptr, void **zv); +int double_unpacker(TarsInputStream *stream, uint8_t tag, Bool is_require, zval *this_ptr, void **zv); +int int64_unpacker(TarsInputStream *stream, uint8_t tag, Bool is_require, zval *this_ptr, void **zv); + +int int32_unpacker(TarsInputStream *stream, uint8_t tag, Bool is_require, zval *this_ptr, void **zv); +int uint32_unpacker(TarsInputStream *stream, uint8_t tag, Bool is_require, zval *this_ptr, void **zv); +int string_unpacker(TarsInputStream *stream, uint8_t tag, Bool is_require, zval *this_ptr, void **zv); +int vector_unpacker(TarsInputStream *stream, uint8_t tag, Bool is_require, zval *this_ptr, void **zv); +int map_unpacker(TarsInputStream *stream, uint8_t tag, Bool is_require, zval *this_ptr, void **zv); +int struct_unpacker(TarsInputStream *stream, uint8_t tag, Bool is_require, zval *this_ptr,void **zv); + +typedef int (*tars_unpack_func_t)(TarsOutputStream *, uint8_t, Bool, zval *, void **); + +static const tars_unpack_func_t unpacker_dispatch[] = { NULL, bool_unpacker, char_unpacker, @@ -184,38 +172,31 @@ static const tars_unpack_func_t unpacker_dispatch[] = { }; TUP_STARTUP_FUNC(ttars); -int tars_struct_write(zval * struct_obj, TarsOutputStream * out); -int php_TarsOutputStream_writeStruct(TarsOutputStream * os, zval * st, uint8_t tag); -inline const char * __complex_name(zval * this_ptr); -int map_converter(zval * , zval *); +int tars_struct_write(zval *struct_obj, TarsOutputStream *out); +int php_TarsOutputStream_writeStruct(TarsOutputStream *os, zval *st, uint8_t tag); +static inline const char *__complex_name(zval *this_ptr); -int struct_packer_wrapper(TarsOutputStream * out, void * struct_ptr); -int struct_unpacker_wrapper(TarsInputStream * is, zval * this_ptr, void ** zv); -int _map_to_array (zval * this_ptr, JMapWrapper * container, void **zv); +int map_converter(zval *, zval *); +int struct_packer_wrapper(TarsOutputStream *out, void *struct_ptr); +int struct_unpacker_wrapper(TarsInputStream *is, zval *this_ptr, void **zv); +int _map_to_array (zval *this_ptr, JMapWrapper *container, void **zv); -#if PHP_MAJOR_VERSION < 7 -#define Z_VECTOR_WRAPPER_P(zv) (vector_wrapper * ) zend_object_store_get_object(zv) -#define Z_MAP_WRAPPER_P(zv) (map_wrapper * ) zend_object_store_get_object(zv) -#else #define Z_VECTOR_WRAPPER_P(zv) vector_wrapper_fetch_object(Z_OBJ_P(zv)) -static inline vector_wrapper *vector_wrapper_fetch_object(zend_object *obj) /* {{{ */ { +static inline vector_wrapper *vector_wrapper_fetch_object(zend_object *obj) { return (vector_wrapper *)((char*)obj - XtOffsetOf(vector_wrapper, std)); } #define Z_MAP_WRAPPER_P(zv) map_wrapper_fetch_object(Z_OBJ_P(zv)) -static inline map_wrapper *map_wrapper_fetch_object(zend_object *obj) /* {{{ */ { +static inline map_wrapper *map_wrapper_fetch_object(zend_object *obj) { return (map_wrapper *)((char*)obj - XtOffsetOf(map_wrapper, std)); } -#endif PHP_METHOD(tars, __construct); PHP_METHOD(tars_vector, __construct); PHP_METHOD(tars_vector, pushBack); - PHP_METHOD(tars_map, __construct); PHP_METHOD(tars_map, pushBack); - PHP_METHOD(tars_struct, __construct); #endif diff --git a/include/tup_c.h b/include/tup_c.h index 926617a..3873403 100644 --- a/include/tup_c.h +++ b/include/tup_c.h @@ -1,115 +1,75 @@ -// ********************************************************************** -// TUP_C version 1.0.4 by WSRD Tencent. -// ********************************************************************** - #ifndef __TUP_C_H__ #define __TUP_C_H__ #include "tars_c.h" -typedef struct UniAttribute UniAttribute; -typedef struct UniPacket UniPacket; -//typedef struct UniPacket TafUniPacket; +typedef struct UniAttribute UniAttribute; +typedef struct UniPacket UniPacket; typedef struct ResponsePacket ResponsePacket; -//���Է�װ struct UniAttribute { JMapWrapper *_data; TarsOutputStream *value_os; TarsInputStream *value_is; - JMapWrapper *m_data; //encode decode helper - TarsOutputStream *os_string; //encode decode helper - - short _iVer; //TUP�汾�ı�ʾ(2:��ʾ��ͨTUPЭ�飬3:��ʾ����TUPЭ��) + JMapWrapper *m_data; + TarsOutputStream *os_string; + short _iVer; char _sLastError[32]; }; /////////////////////////////////////////////////////////////// -//put - -Int32 php_TUP_putStruct(void * pack, const char * name, void * st); - - -Int32 php_TUP_getStruct(void * pack, const char * name, void * st, void ** ret_val, Bool is_require); - - -void TUP_setVersion(void * pack, Short iVersion); - -Int32 TUP_putBool (void * pack, const char * name, Bool value); - -Int32 TUP_putChar (void * pack, const char * name, Char value); - -Int32 TUP_putUInt8 (void * pack, const char * name, UInt8 value); - -Int32 TUP_putShort (void * pack, const char * name, Short value); - -Int32 TUP_putUInt16 (void * pack, const char * name, UInt16 value); - -Int32 TUP_putFloat (void * pack, const char * name, Float value); - -Int32 TUP_putDouble (void * pack, const char * name, Double value); - -Int32 TUP_putInt32 (void * pack, const char * name, Int32 value); - -Int32 TUP_putUInt32 (void * pack, const char * name, UInt32 value); - -Int32 TUP_putInt64 (void * pack, const char * name, Int64 value); - -Int32 TUP_putString (void * pack, const char * name, JString * value); -Int32 TUP_putStringBuffer(void * pack, const char * name, const char* buff, uint32_t len); - -Int32 TUP_putVector (void * pack, const char * name, JArray * value); - -Int32 TUP_putVectorChar(void * pack, const char * name, JString* value); -Int32 TUP_putVectorCharBuffer(void * pack, const char * name, const char* buff, uint32_t len); - -Int32 TUP_putMap (void * pack, const char * name, JMapWrapper * value); - -Int32 TUP_putStruct (void * pack, const char * name, const void* st); - -//get -Short TUP_getVersion(const void * pack); - -Int32 TUP_getBool (const void * pack, const char * name, Bool * value, Bool is_require); - -Int32 TUP_getChar (const void * pack, const char * name, Char * value, Bool is_require); - -Int32 TUP_getUInt8 (const void * pack, const char * name, UInt8 * value, Bool is_require); - -Int32 TUP_getShort (const void * pack, const char * name, Short * value, Bool is_require); - -Int32 TUP_getUInt16 (const void * pack, const char * name, UInt16 * value, Bool is_require); - -Int32 TUP_getFloat (const void * pack, const char * name, Float* value, Bool is_require); - -Int32 TUP_getDouble (const void * pack, const char * name, Double* value, Bool is_require); - -Int32 TUP_getInt32 (const void * pack, const char * name, Int32* value, Bool is_require); - -Int32 TUP_getUInt32 (const void * pack, const char * name, UInt32 * value, Bool is_require); - -Int32 TUP_getInt64 (const void * pack, const char * name, Int64* value, Bool is_require); - -Int32 TUP_getString(const void * pack, const char * name, JString * value, Bool is_require); - -Int32 TUP_getVector(const void * pack, const char * name, JArray * value, Bool is_require); - -Int32 TUP_getVectorChar(const void * pack, const char * name, JString * value, Bool is_require); - -Int32 TUP_getMap (const void * pack, const char * name, JMapWrapper * value, Bool is_require); - - -UniAttribute * UniAttribute_new(void); +// put +Int32 php_TUP_putStruct(void *pack, const char *name, void *st); +Int32 php_TUP_getStruct(void *pack, const char *name, void *st, void **ret_val, Bool is_require); + +void TUP_setVersion(void *pack, Short iVersion); + +Int32 TUP_putBool(void *pack, const char *name, Bool value); +Int32 TUP_putChar(void *pack, const char *name, Char value); +Int32 TUP_putUInt8(void *pack, const char *name, UInt8 value); +Int32 TUP_putShort(void *pack, const char *name, Short value); +Int32 TUP_putUInt16(void *pack, const char *name, UInt16 value); +Int32 TUP_putFloat(void *pack, const char *name, Float value); +Int32 TUP_putDouble(void *pack, const char *name, Double value); +Int32 TUP_putInt32(void *pack, const char *name, Int32 value); +Int32 TUP_putUInt32(void *pack, const char *name, UInt32 value); +Int32 TUP_putInt64(void *pack, const char *name, Int64 value); +Int32 TUP_putString(void *pack, const char *name, JString *value); +Int32 TUP_putStringBuffer(void *pack, const char *name, const char *buff, uint32_t len); +Int32 TUP_putVector(void *pack, const char *name, JArray *value); +Int32 TUP_putVectorChar(void *pack, const char *name, JString *value); +Int32 TUP_putVectorCharBuffer(void *pack, const char *name, const char *buff, uint32_t len); +Int32 TUP_putMap(void *pack, const char *name, JMapWrapper *value); +Int32 TUP_putStruct(void *pack, const char *name, const void *st); + +// get +Short TUP_getVersion(const void *pack); + +Int32 TUP_getBool(const void *pack, const char *name, Bool *value, Bool is_require); +Int32 TUP_getChar(const void *pack, const char *name, Char *value, Bool is_require); +Int32 TUP_getUInt8(const void *pack, const char *name, UInt8 *value, Bool is_require); +Int32 TUP_getShort(const void *pack, const char *name, Short *value, Bool is_require); +Int32 TUP_getUInt16(const void *pack, const char *name, UInt16 *value, Bool is_require); +Int32 TUP_getFloat(const void *pack, const char *name, Float *value, Bool is_require); +Int32 TUP_getDouble(const void *pack, const char *name, Double *value, Bool is_require); +Int32 TUP_getInt32(const void *pack, const char *name, Int32 *value, Bool is_require); +Int32 TUP_getUInt32(const void *pack, const char *name, UInt32 *value, Bool is_require); +Int32 TUP_getInt64(const void *pack, const char *name, Int64 *value, Bool is_require); +Int32 TUP_getString(const void *pack, const char *name, JString *value, Bool is_require); +Int32 TUP_getVector(const void *pack, const char *name, JArray *value, Bool is_require); +Int32 TUP_getVectorChar(const void *pack, const char *name, JString *value, Bool is_require); +Int32 TUP_getMap(const void *pack, const char *name, JMapWrapper *value, Bool is_require); + +UniAttribute *UniAttribute_new(void); Int32 UniAttribute_init(UniAttribute *handle); -void UniAttribute_del(UniAttribute ** handle); +void UniAttribute_del(UniAttribute **handle); /////////////////////////////////////////////////////////////// -//�����Ӧ����װ struct UniPacket { UniAttribute attr; @@ -126,25 +86,21 @@ struct UniPacket JMapWrapper *status; //map status; }; -void UniPacket_setVersion(UniPacket * pack, Short siVersion); - -Short UniPacket_getVersion(UniPacket * pack); - -Int32 Unipacket_getCode(UniPacket* unpack,JString *tmp); +void UniPacket_setVersion(UniPacket *pack, Short siVersion); -Int32 Unipacket_getMsg(UniPacket* unpack,JString **tmp); +Short UniPacket_getVersion(UniPacket *pack); +Int32 Unipacket_getCode(UniPacket *unpack, JString *tmp); +Int32 Unipacket_getMsg(UniPacket *unpack, JString **tmp); Int32 Unipacket_getStatus(UniPacket *unpack, JString *tmp); - Int32 UniPacket_encode(const UniPacket *pack, TarsOutputStream *os_tmp); +Int32 UniPacket_decode(UniPacket *pack, const char *buff, uint32_t len); -Int32 UniPacket_decode(UniPacket * pack, const char* buff, uint32_t len); - -void UniPacket_del(UniPacket ** handle); +void UniPacket_del(UniPacket **handle); Int32 UniPacket_init(UniPacket *handle); -UniPacket * UniPacket_new(void); +UniPacket *UniPacket_new(void); struct ResponsePacket { @@ -159,14 +115,13 @@ struct ResponsePacket JMapWrapper *status; //map status; JString *sResultDesc; }; -ResponsePacket * ResponsePacket_new(void); -Int32 ResponsePacket_init(ResponsePacket *handle); +ResponsePacket *ResponsePacket_new(void); -Int32 ResponsePacket_decode(ResponsePacket * rsp_pack, const char* buff, uint32_t len); - -Int32 ResponsePacket_encode(const ResponsePacket * rsp_pack, TarsOutputStream *os_tmp); +Int32 ResponsePacket_init(ResponsePacket *handle); +Int32 ResponsePacket_decode(ResponsePacket *rsp_pack, const char *buff, uint32_t len); +Int32 ResponsePacket_encode(const ResponsePacket *rsp_pack, TarsOutputStream *os_tmp); -void ResponsePacket_del(ResponsePacket ** handle); +void ResponsePacket_del(ResponsePacket **handle); #endif diff --git a/tars2php.php b/tars2php.php new file mode 100644 index 0000000..5339a93 --- /dev/null +++ b/tars2php.php @@ -0,0 +1,2892 @@ +moduleScan(); + +$fileConverter->moduleParse(); + +class Utils +{ + public static $preEnums; + public static $preStructs; + + public static $wholeTypeMap = array( + 'bool' => '\TARS::BOOL', + 'boolean' => '\TARS::BOOL', + 'byte' => '\TARS::CHAR', + 'char' => '\TARS::CHAR', + 'unsigned byte' => '\TARS::UINT8', + 'unsigned char' => '\TARS::UINT8', + 'short' => '\TARS::SHORT', + 'unsigned short' => '\TARS::UINT16', + 'int' => '\TARS::INT32', + 'unsigned int' => '\TARS::UINT32', + 'long' => '\TARS::INT64', + 'float' => '\TARS::FLOAT', + 'double' => '\TARS::DOUBLE', + 'string' => '\TARS::STRING', + 'vector' => 'new \TARS_Vector', + 'map' => 'new \TARS_Map', + ); + + public static $typeMap = array( + 'bool' => '\TARS::BOOL', + 'boolean' => '\TARS::BOOL', + 'byte' => '\TARS::CHAR', + 'char' => '\TARS::CHAR', + 'unsigned byte' => '\TARS::UINT8', + 'unsigned char' => '\TARS::UINT8', + 'short' => '\TARS::SHORT', + 'unsigned short' => '\TARS::UINT16', + 'int' => '\TARS::INT32', + 'unsigned int' => '\TARS::UINT32', + 'long' => '\TARS::INT64', + 'float' => '\TARS::FLOAT', + 'double' => '\TARS::DOUBLE', + 'string' => '\TARS::STRING', + 'vector' => '\TARS::VECTOR', + 'map' => '\TARS::MAP', + 'enum' => '\TARS::UINT8', // 应该不会出现 + 'struct' => '\TARS::STRUCT', // 应该不会出现 + ); + + public static function getPackMethods($type) + { + $packMethods = [ + 'bool' => 'putBool', + 'boolean' => 'putBool', + 'byte' => 'putChar', + 'char' => 'putChar', + 'unsigned byte' => 'putUInt8', + 'unsigned char' => 'putUInt8', + 'short' => 'putShort', + 'unsigned short' => 'putUInt16', + 'int' => 'putInt32', + 'unsigned int' => 'putUInt32', + 'long' => 'putInt64', + 'float' => 'putFloat', + 'double' => 'putDouble', + 'string' => 'putString', + 'enum' => 'putUInt8', + 'map' => 'putMap', + 'vector' => 'putVector', + 'Bool' => 'putBool', + 'Boolean' => 'putBool', + 'Byte' => 'putChar', + 'Char' => 'putChar', + 'Unsigned byte' => 'putUInt8', + 'Unsigned char' => 'putUInt8', + 'Short' => 'putShort', + 'Unsigned short' => 'putUInt16', + 'Int' => 'putInt32', + 'Unsigned int' => 'putUInt32', + 'Long' => 'putInt64', + 'Float' => 'putFloat', + 'Double' => 'putDouble', + 'String' => 'putString', + 'Enum' => 'putUInt8', + 'Map' => 'putMap', + 'Vector' => 'putVector', + ]; + + if (isset($packMethods[$type])) { + return $packMethods[$type]; + } else { + return 'putStruct'; + } + } + + public static function getUnpackMethods($type) + { + $unpackMethods = [ + 'bool' => 'getBool', + 'boolean' => 'getBool', + 'byte' => 'getChar', + 'char' => 'getChar', + 'unsigned byte' => 'getUInt8', + 'unsigned char' => 'getUInt8', + 'short' => 'getShort', + 'unsigned short' => 'getUInt16', + 'int' => 'getInt32', + 'unsigned int' => 'getUInt32', + 'long' => 'getInt64', + 'float' => 'getFloat', + 'double' => 'getDouble', + 'string' => 'getString', + 'enum' => 'getUInt8', + 'map' => 'getMap', + 'vector' => 'getVector', + 'Bool' => 'getBool', + 'Boolean' => 'getBool', + 'Byte' => 'getChar', + 'Char' => 'getChar', + 'Unsigned byte' => 'getUInt8', + 'Unsigned char' => 'getUInt8', + 'Short' => 'getShort', + 'Unsigned short' => 'getUInt16', + 'Int' => 'getInt32', + 'Unsigned int' => 'getUInt32', + 'Long' => 'getInt64', + 'Float' => 'getFloat', + 'Double' => 'getDouble', + 'String' => 'getString', + 'Enum' => 'getUInt8', + 'Map' => 'getMap', + 'Vector' => 'getVector', + ]; + + if (isset($unpackMethods[strtolower($type)])) { + return $unpackMethods[strtolower($type)]; + } else { + return 'getStruct'; + } + } + + /** + * @param $char + * + * @return int + * 判断是不是tag + */ + public static function isTag($word) + { + if (!is_numeric($word)) { + return false; + } else { + return true; + } + } + + /** + * @param $word + * + * @return bool + * 判断收集到的word是不是 + */ + public static function isRequireType($word) + { + return in_array(strtolower($word), ['require', 'optional']); + } + + public static function isBasicType($word) + { + $basicTypes = [ + 'bool', 'boolean', 'byte', 'char', 'unsigned byte', 'unsigned char', 'short', 'unsigned short', + 'int', 'unsigned int', 'long', 'float', 'double', 'string', 'void', + ]; + + return in_array(strtolower($word), $basicTypes); + } + + public static function isEnum($word, $preEnums) + { + return in_array($word, $preEnums); + } + + public static function isMap($word) + { + return strtolower($word) == 'map'; + } + + public static function isStruct($word, $preStructs) + { + return in_array($word, $preStructs); + } + + public static function isVector($word) + { + return strtolower($word) == 'vector'; + } + + public static function isSpace($char) + { + if ($char == ' ' || $char == "\t") { + return true; + } else { + return false; + } + } + + public static function paramTypeMap($paramType) + { + if (self::isBasicType($paramType) || self::isMap($paramType) || self::isVector($paramType)) { + return ''; + } else { + return $paramType; + } + } + + public static function getRealType($type) + { + if (isset(self::$typeMap[strtolower($type)])) { + return self::$typeMap[strtolower($type)]; + } else { + return '\TARS::STRUCT'; + } + } + + public static function inIdentifier($char) + { + return ($char >= 'a' & $char <= 'z') | + ($char >= 'A' & $char <= 'Z') | + ($char >= '0' & $char <= '9') | + ($char == '_'); + } + + public static function abnormalExit($level, $msg) + { + echo "[$level]$msg"."\n"; + exit; + } + + public static function pregMatchByName($name, $line) + { + // 处理第一行,正则匹配出classname + $Tokens = preg_split("/$name/", $line); + + $mathName = $Tokens[1]; + $mathName = trim($mathName, " \r\0\x0B\t\n{"); + + preg_match('/[a-zA-Z][0-9a-zA-Z]/', $mathName, $matches); + if (empty($matches)) { + //Utils::abnormalExit('error',$name.'名称有误'.$line); + } + + return $mathName; + } + + public static function isReturn($char) + { + if ($char == "\n" || $char == '\r' || bin2hex($char) == '0a' || bin2hex($char) == '0b' || + bin2hex($char) == '0c' || bin2hex($char) == '0d') { + return true; + } else { + return false; + } + } +} + +class FileConverter +{ + public $moduleName; + public $uniqueName; + public $interfaceName; + public $fromFile; + public $outputDir; + + public $appName; + public $serverName; + public $objName; + public $servantName; + + public $namespaceName; + public $namespacePrefix; + + public $preStructs = []; + public $preEnums = []; + public $preConsts = []; + public $preNamespaceStructs = []; + public $preNamespaceEnums = []; + + public function __construct($config) + { + $this->fromFile = $config['tarsFiles'][0]; + if (empty($config['appName']) || empty($config['serverName']) || empty($config['objName'])) { + Utils::abnormalExit('error', 'appName or serverName or objName empty!'); + } + $this->servantName = $config['appName'].'.'.$config['serverName'].'.'.$config['objName']; + + $this->appName = $config['appName']; + $this->serverName = $config['serverName']; + $this->objName = $config['objName']; + + $this->outputDir = empty($config['dstPath']) ? './' : $config['dstPath'].'/'; + + $pos = strrpos($this->fromFile, '/', -1); + $inputDir = substr($this->fromFile, 0, $pos); + $this->inputDir = $inputDir; + + $this->namespacePrefix = $config['namespacePrefix']; + $this->withServant = $config['withServant']; + + $this->initDir(); + } + + /** + * 首先需要初始化一些文件目录. + * + * @return [type] [description] + */ + public function initDir() + { + if (strtolower(substr(php_uname('a'), 0, 3)) === 'win') { + exec('mkdir '.$this->outputDir.$this->appName); + exec('mkdir '.$this->outputDir.$this->appName.'\\'.$this->serverName); + exec('DEL '.$this->outputDir.$this->appName.'\\'.$this->serverName.'\\'.$this->objName.'\\*.*'); + exec('mkdir '.$this->outputDir.$this->appName.'\\'.$this->serverName.'\\'.$this->objName); + + $this->moduleName = $this->appName.'\\'.$this->serverName.'\\'.$this->objName; + + exec('mkdir '.$this->outputDir.$this->moduleName.'\\classes'); + exec('mkdir '.$this->outputDir.$this->moduleName.'\\tars'); + exec('copy '.$this->fromFile.' '.$this->outputDir.$this->moduleName.'\\tars'); + } else { + exec('mkdir '.$this->outputDir.$this->appName); + exec('mkdir '.$this->outputDir.$this->appName.'/'.$this->serverName); + exec('rm -rf '.$this->outputDir.$this->appName.'/'.$this->serverName.'/'.$this->objName); + exec('mkdir '.$this->outputDir.$this->appName.'/'.$this->serverName.'/'.$this->objName); + + $this->moduleName = $this->appName.'/'.$this->serverName.'/'.$this->objName; + + exec('mkdir '.$this->outputDir.$this->moduleName.'/classes'); + exec('mkdir '.$this->outputDir.$this->moduleName.'/tars'); + exec('cp '.$this->fromFile.' '.$this->outputDir.$this->moduleName.'/tars'); + } + + $this->namespaceName = empty($this->namespacePrefix) ? $this->appName.'\\'.$this->serverName.'\\'.$this->objName + : $this->namespacePrefix.'\\'.$this->appName.'\\'.$this->serverName.'\\'.$this->objName; + + $this->uniqueName = $this->appName.'_'.$this->serverName.'_'.$this->objName; + } + + public function usage() + { + echo 'php tars2php.php tars.proto.php'; + } + + public function moduleScan() + { + $fp = fopen($this->fromFile, 'r'); + if (!$fp) { + $this->usage(); + exit; + } + while (($line = fgets($fp, 1024)) !== false) { + + // 判断是否有module + $moduleFlag = strpos($line, 'module'); + if ($moduleFlag !== false) { + $name = Utils::pregMatchByName('module', $line); + $currentModule = $name; + } + + // 判断是否有include + $includeFlag = strpos($line, '#include'); + if ($includeFlag !== false) { + // 找出tars对应的文件名 + $tokens = preg_split('/#include/', $line); + $includeFile = trim($tokens[1], "\" \r\n"); + + if (strtolower(substr(php_uname('a'), 0, 3)) === 'win') { + exec('copy '.$includeFile.' '.$this->moduleName.'\\tars'); + } else { + exec('cp '.$includeFile.' '.$this->moduleName.'/tars'); + } + + $includeParser = new IncludeParser(); + $includeParser->includeScan($includeFile, $this->preEnums, $this->preStructs, + $this->preNamespaceEnums, $this->preNamespaceStructs); + } + + // 如果空行,或者是注释,就直接略过 + if (!$line || trim($line) == '' || trim($line)[0] === '/' || trim($line)[0] === '*' || trim($line) === '{') { + continue; + } + + // 正则匹配,发现是在enum中 + $enumFlag = strpos($line, 'enum'); + if ($enumFlag !== false) { + $name = Utils::pregMatchByName('enum', $line); + if (!empty($name)) { + $this->preEnums[] = $name; + + // 增加命名空间以备不时之需 + if (!empty($currentModule)) { + $this->preNamespaceEnums[] = $currentModule.'::'.$name; + } + + while (($lastChar = fgetc($fp)) != '}') { + continue; + } + } + } + + // 正则匹配,发现是在结构体中 + $structFlag = strpos($line, 'struct'); + // 一旦发现了struct,那么持续读到结束为止 + if ($structFlag !== false) { + $name = Utils::pregMatchByName('struct', $line); + + if (!empty($name)) { + $this->preStructs[] = $name; + // 增加命名空间以备不时之需 + if (!empty($currentModule)) { + $this->preNamespaceStructs[] = $currentModule.'::'.$name; + } + } + } + } + fclose($fp); + } + + public function moduleParse() + { + $fp = fopen($this->fromFile, 'r'); + if (!$fp) { + $this->usage(); + exit; + } + while (($line = fgets($fp, 1024)) !== false) { + + // 判断是否有include + $includeFlag = strpos($line, '#include'); + if ($includeFlag !== false) { + // 找出tars对应的文件名 + $tokens = preg_split('/#include/', $line); + $includeFile = trim($tokens[1], "\" \r\n"); + $includeParser = new IncludeParser(); + $includeParser->includeParse($includeFile, $this->preEnums, $this->preStructs, $this->uniqueName, + $this->moduleName, $this->namespaceName, $this->servantName, $this->preNamespaceEnums, $this->preNamespaceStructs, + $this->outputDir); + } + + // 如果空行,或者是注释,就直接略过 + if (!$line || trim($line) == '' || trim($line)[0] === '/' || trim($line)[0] === '*') { + continue; + } + + // 正则匹配,发现是在enum中 + $enumFlag = strpos($line, 'enum'); + if ($enumFlag !== false) { + // 处理第一行,正则匹配出classname + $enumTokens = preg_split('/enum/', $line); + + $enumName = $enumTokens[1]; + $enumName = trim($enumName, " \r\0\x0B\t\n{"); + + // 判断是否是合法的structName + preg_match('/[a-zA-Z][0-9a-zA-Z]/', $enumName, $matches); + if (empty($matches)) { + Utils::abnormalExit('error', 'Enum名称有误'); + } + + $this->preEnums[] = $enumName; + while (($lastChar = fgetc($fp)) != '}') { + continue; + } + } + + // 正则匹配,发现是在consts中 + $constFlag = strpos($line, 'const'); + if ($constFlag !== false) { + // 直接进行正则匹配 + Utils::abnormalExit('warning', 'const is not supported, please make sure you deal with them yourself in this version!'); + } + + // 正则匹配,发现是在结构体中 + $structFlag = strpos($line, 'struct'); + // 一旦发现了struct,那么持续读到结束为止 + if ($structFlag !== false) { + $name = Utils::pregMatchByName('struct', $line); + + $structParser = new StructParser($fp, $line, $this->uniqueName, $this->moduleName, $name, $this->preStructs, + $this->preEnums, $this->namespaceName, $this->preNamespaceEnums, $this->preNamespaceStructs); + $structClassStr = $structParser->parse(); + file_put_contents($this->outputDir.$this->moduleName.'/classes/'.$name.'.php', $structClassStr); + } + + // 正则匹配,发现是在interface中 + $interfaceFlag = strpos(strtolower($line), 'interface'); + // 一旦发现了struct,那么持续读到结束为止 + if ($interfaceFlag !== false) { + $name = Utils::pregMatchByName('interface', $line); + $interfaceName = $name.'Servant'; + + // 需要区分一下生成server还是client的代码 + if ($this->withServant) { + $servantParser = new ServantParser($fp, $line, $this->namespaceName, $this->moduleName, + $interfaceName, $this->preStructs, + $this->preEnums, $this->servantName, $this->preNamespaceEnums, $this->preNamespaceStructs); + $servant = $servantParser->parse(); + file_put_contents($this->outputDir.$this->moduleName.'/'.$interfaceName.'.php', $servant); + } else { + $interfaceParser = new InterfaceParser($fp, $line, $this->namespaceName, $this->moduleName, + $interfaceName, $this->preStructs, + $this->preEnums, $this->servantName, $this->preNamespaceEnums, $this->preNamespaceStructs); + $interfaces = $interfaceParser->parse(); + + // 需要区分同步和异步的两种方式 + file_put_contents($this->outputDir.$this->moduleName.'/'.$interfaceName.'.php', $interfaces['syn']); + } + } + } + } +} + +class IncludeParser +{ + public function includeScan($includeFile, &$preEnums, &$preStructs, + &$preNamespaceEnums, &$preNamespaceStructs) + { + $fp = fopen($includeFile, 'r'); + if (!$fp) { + echo 'Include file not exit, please check'; + exit; + } + while (($line = fgets($fp, 1024)) !== false) { + // 如果空行,或者是注释,就直接略过 + if (!$line || trim($line) == '' || trim($line)[0] === '/' || trim($line)[0] === '*') { + continue; + } + + // 判断是否有module + $moduleFlag = strpos($line, 'module'); + if ($moduleFlag !== false) { + $name = Utils::pregMatchByName('module', $line); + $currentModule = $name; + } + + // 正则匹配,发现是在enum中 + $enumFlag = strpos($line, 'enum'); + if ($enumFlag !== false) { + $name = Utils::pregMatchByName('enum', $line); + $preEnums[] = $name; + if (!empty($currentModule)) { + $preNamespaceEnums[] = $currentModule.'::'.$name; + } + while (($lastChar = fgetc($fp)) != '}') { + continue; + } + } + + // 正则匹配,发现是在结构体中 + $structFlag = strpos($line, 'struct'); + // 一旦发现了struct,那么持续读到结束为止 + if ($structFlag !== false) { + $name = Utils::pregMatchByName('struct', $line); + + $preStructs[] = $name; + if (!empty($currentModule)) { + $preNamespaceStructs[] = $currentModule.'::'.$name; + } + } + } + } + + public function includeParse($includeFile, &$preEnums, &$preStructs, $uniqueName, $moduleName, $namespaceName, $servantName, + &$preNamespaceEnums, &$preNamespaceStructs, $outputDir) + { + $fp = fopen($includeFile, 'r'); + if (!$fp) { + echo 'Include file not exit, please check'; + exit; + } + while (($line = fgets($fp, 1024)) !== false) { + // 如果空行,或者是注释,就直接略过 + if (!$line || trim($line) == '' || trim($line)[0] === '/' || trim($line)[0] === '*') { + continue; + } + + // 正则匹配,发现是在consts中 + $constFlag = strpos($line, 'const'); + if ($constFlag !== false) { + // 直接进行正则匹配 + echo 'CONST is not supported, please make sure you deal with them yourself in this version!'; + } + + // 正则匹配,发现是在结构体中 + $structFlag = strpos($line, 'struct'); + // 一旦发现了struct,那么持续读到结束为止 + if ($structFlag !== false) { + $name = Utils::pregMatchByName('struct', $line); + + $structParser = new StructParser($fp, $line, $uniqueName, $moduleName, $name, $preStructs, + $preEnums, $namespaceName, $preNamespaceEnums, $preNamespaceStructs); + $structClassStr = $structParser->parse(); + file_put_contents($outputDir.$moduleName.'/classes/'.$name.'.php', $structClassStr); + } + + // 正则匹配,发现是在interface中 + $interfaceFlag = strpos(strtolower($line), 'interface'); + // 一旦发现了struct,那么持续读到结束为止 + if ($interfaceFlag !== false) { + $name = Utils::pregMatchByName('interface', $line); + + if (in_array($name, $preStructs)) { + $name .= 'Servant'; + } + + $interfaceParser = new InterfaceParser($fp, $line, $namespaceName, $moduleName, + $name, $preStructs, + $preEnums, $servantName, $preNamespaceEnums, $preNamespaceStructs); + $interfaces = $interfaceParser->parse(); + + // 需要区分同步和异步的两种方式 + file_put_contents($outputDir.$moduleName.'/'.$name.'.php', $interfaces['syn']); + } + } + } +} + +class InterfaceParser +{ + public $namespaceName; + public $moduleName; + public $interfaceName; + public $asInterfaceName; + + public $state; + + // 这个结构体,可能会引用的部分,包括其他的结构体、枚举类型、常量 + public $useStructs = []; + public $extraUse; + public $preStructs; + public $preEnums; + + public $preNamespaceStructs; + public $preNamespaceEnums; + + public $returnSymbol = "\n"; + public $doubleReturn = "\n\n"; + public $tabSymbol = "\t"; + public $doubleTab = "\t\t"; + public $tripleTab = "\t\t\t"; + public $quardupleTab = "\t\t\t\t"; + + public $extraContructs = ''; + public $extraExtInit = ''; + + public $consts = ''; + public $variables = ''; + public $fields = ''; + + public $funcSet = ''; + + public $servantName; + + public function __construct($fp, $line, $namespaceName, $moduleName, + $interfaceName, $preStructs, + $preEnums, $servantName, $preNamespaceEnums, $preNamespaceStructs) + { + $this->fp = $fp; + $this->namespaceName = $namespaceName; + $this->moduleName = $moduleName; + $this->preStructs = $preStructs; + $this->preEnums = $preEnums; + $this->interfaceName = $interfaceName; + $this->servantName = $servantName; + + $this->extraUse = ''; + $this->useStructs = []; + + $this->preNamespaceEnums = $preNamespaceEnums; + $this->preNamespaceStructs = $preNamespaceStructs; + } + + public function copyAnnotation() + { + // 再读入一个字符 + $nextChar = fgetc($this->fp); + // 第一种 + if ($nextChar == '/') { + while (1) { + $tmpChar = fgetc($this->fp); + + if ($tmpChar == "\n") { + $this->state = 'lineEnd'; + break; + } + } + + return; + } elseif ($nextChar == '*') { + while (1) { + $tmpChar = fgetc($this->fp); + + if ($tmpChar === false) { + Utils::abnormalExit('error', '注释换行错误,请检查'.$tmpChar); + } elseif ($tmpChar === "\n") { + } elseif (($tmpChar) === '*') { + $nextnextChar = fgetc($this->fp); + if ($nextnextChar == '/') { + return; + } else { + $pos = ftell($this->fp); + fseek($this->fp, $pos - 1); + } + } + } + } + // 注释不正常 + else { + Utils::abnormalExit('error', '注释换行错误,请检查'.$nextChar); + } + } + + public function getFileHeader($prefix = '') + { + return "namespaceName.$prefix.';'.$this->doubleReturn. + 'use Tars\\client\\CommunicatorConfig;'.$this->returnSymbol. + 'use Tars\\client\\Communicator;'.$this->returnSymbol. + 'use Tars\\client\\RequestPacket;'.$this->returnSymbol. + 'use Tars\\client\\TUPAPIWrapper;'.$this->returnSymbol. + $this->returnSymbol; + } + + public function getInterfaceBasic() + { + return $this->tabSymbol.'protected $_communicator;'.$this->returnSymbol. + $this->tabSymbol.'protected $_iVersion;'.$this->returnSymbol. + $this->tabSymbol.'protected $_iTimeout;'.$this->returnSymbol. + $this->tabSymbol."public \$_servantName = \"$this->servantName\";".$this->doubleReturn. + $this->tabSymbol.'public function __construct(CommunicatorConfig $config) {'.$this->returnSymbol. + + $this->doubleTab.'try {'.$this->returnSymbol. + $this->tripleTab.'$config->setServantName($this->_servantName);'.$this->returnSymbol. + $this->tripleTab.'$this->_communicator = new Communicator($config);'.$this->returnSymbol. + $this->tripleTab.'$this->_iVersion = $config->getIVersion();'.$this->returnSymbol. + $this->tripleTab.'$this->_iTimeout = empty($config->getAsyncInvokeTimeout())?2:$config->getAsyncInvokeTimeout();'.$this->returnSymbol. + $this->doubleTab.'} catch (\\Exception $e) {'.$this->returnSymbol. + $this->tripleTab.'throw $e;'.$this->returnSymbol. + $this->doubleTab.'}'.$this->returnSymbol. + $this->tabSymbol.'}'.$this->doubleReturn; + } + + public function parse() + { + while ($this->state != 'end') { + $this->state = 'init'; + $this->InterfaceFuncParseLine(); + } + + $interfaceClass = $this->getFileHeader('').$this->extraUse.'class '.$this->interfaceName.' {'.$this->returnSymbol; + + $interfaceClass .= $this->getInterfaceBasic(); + + $interfaceClass .= $this->funcSet; + + $interfaceClass .= '}'.$this->doubleReturn; + + return [ + 'syn' => $interfaceClass, + ]; + } + + /** + * @param $fp + * @param $line + * 这里必须要引入状态机了 + */ + public function InterfaceFuncParseLine() + { + $line = ''; + $this->state = 'init'; + while (1) { + if ($this->state == 'init') { + $char = fgetc($this->fp); + + // 有可能是换行 + if ($char == '{' || Utils::isReturn($char)) { + continue; + } + // 遇到了注释会用贪婪算法全部处理完,同时填充到struct的类里面去 + elseif ($char == '/') { + $this->copyAnnotation(); + break; + } elseif (Utils::inIdentifier($char)) { + $this->state = 'identifier'; + $line .= $char; + } + // 终止条件之1,宣告struct结束 + elseif ($char == '}') { + // 需要贪心的读到"\n"为止 + while (($lastChar = fgetc($this->fp)) != "\n") { + continue; + } + $this->state = 'end'; + break; + } + } elseif ($this->state == 'identifier') { + $char = fgetc($this->fp); + + if ($char == '/') { + $this->copyAnnotation(); + } elseif ($char == ';') { + $line .= $char; + break; + } + // 终止条件之2,同样宣告interface结束 + elseif ($char == '}') { + // 需要贪心的读到"\n"为止 + while (($lastChar = fgetc($this->fp)) != "\n") { + continue; + } + $this->state = 'end'; + } elseif (Utils::isReturn($char)) { + continue; + } elseif ($char == ')') { + $line .= $char; + // 需要贪心的读到"\n"为止 + while (($lastChar = fgetc($this->fp)) != "\n") { + continue; + } + $this->state = 'lineEnd'; + } else { + $line .= $char; + } + } elseif ($this->state == 'lineEnd') { + $char = fgetc($this->fp); + if ($char == '}') { + // 需要贪心的读到"\n"为止 + while (($lastChar = fgetc($this->fp)) != "\n") { + continue; + } + $this->state = 'end'; + } + break; + } elseif ($this->state == 'end') { + break; + } + } + if (empty($line)) { + return; + } + + $line = trim($line); + // 如果空行,或者是注释,或者是大括号就直接略过 + if (!$line || $line[0] === '/' || $line[0] === '*' || $line === '{') { + return; + } + + $endFlag = strpos($line, '};'); + if ($endFlag !== false) { + $this->state = 'end'; + + return; + } + + $endFlag = strpos($line, '}'); + if ($endFlag !== false) { + $this->state = 'end'; + + return; + } + + // 有必要先分成三个部分,返回类型、接口名、参数列表 + $tokens = preg_split('/\(/', $line, 2); + $mix = trim($tokens[0]); + $rest = $tokens[1]; + + $pices = preg_split('/\s+/', $mix); + + $funcName = $pices[count($pices) - 1]; + + $returnType = implode('', array_slice($pices, 0, count($pices) - 1)); + + $state = 'init'; + $word = ''; + + $params = []; + + for ($i = 0; $i < strlen($rest); ++$i) { + $char = $rest[$i]; + + if ($state == 'init') { + // 有可能是换行 + if ($char == '(' || Utils::isSpace($char)) { + continue; + } elseif ($char == "\n") { + break; + } elseif (Utils::inIdentifier($char)) { + $state = 'identifier'; + $word .= $char; + } + // 终止条件之1,宣告interface结束 + elseif ($char == ')') { + break; + } else { + Utils::abnormalExit('error', 'Interface:'.$this->interfaceName.'内格式错误,请更正tars'); + } + } elseif ($state == 'identifier') { + if ($char == ',') { + $params[] = $word; + $state = 'init'; + $word = ''; + continue; + } + // 标志着map和vector的开始,不等到'>'的结束不罢休 + // 这时候需要使用栈来push,然后一个个对应的pop,从而达到type的遍历 + elseif ($char == '<') { + $mapVectorStack = []; + $word .= $char; + array_push($mapVectorStack, '<'); + while (!empty($mapVectorStack)) { + $moreChar = $rest[$i + 1]; + $word .= $moreChar; + if ($moreChar == '<') { + array_push($mapVectorStack, '<'); + } elseif ($moreChar == '>') { + array_pop($mapVectorStack); + } + ++$i; + } + continue; + } elseif ($char == ')') { + $params[] = $word; + break; + } elseif ($char == ';') { + continue; + } + // 终止条件之2,同样宣告struct结束 + elseif ($char == '}') { + $state = 'end'; + } elseif ($char == "\n") { + break; + } else { + $word .= $char; + } + } elseif ($state == 'lineEnd') { + break; + } elseif ($state == 'end') { + break; + } + } + $this->writeInterfaceLine($returnType, $funcName, $params); + } + + /** + * @param $wholeType + * 通过完整的类型获取vector的扩展类型 + * vector => new \TARS_VECTOR(new CateObj()) + * vector => new \TARS_VECTOR(\TARS::STRING) + * vector> => new \TARS_VECTOR(new \TARS_MAP(\TARS_MAP,new CateObj())) + */ + public function getExtType($wholeType, $valueName) + { + $state = 'init'; + $word = ''; + $extType = ''; + + for ($i = 0; $i < strlen($wholeType); ++$i) { + $char = $wholeType[$i]; + if ($state == 'init') { + // 如果遇到了空格 + if (Utils::isSpace($char)) { + continue; + } + // 回车是停止符号 + elseif (Utils::inIdentifier($char)) { + $state = 'indentifier'; + $word .= $char; + } elseif (Utils::isReturn($char)) { + break; + } elseif ($char == '>') { + $extType .= ')'; + continue; + } + } elseif ($state == 'indentifier') { + if ($char == '<') { + // 替换word,替换< 恢复初始状态 + $tmp = $this->VecMapReplace($word); + $extType .= $tmp; + $extType .= '('; + $word = ''; + $state = 'init'; + } elseif ($char == '>') { + // 替换word,替换> 恢复初始状态 + // 替换word,替换< 恢复初始状态 + $tmp = $this->VecMapReplace($word); + $extType .= $tmp; + $extType .= ')'; + $word = ''; + $state = 'init'; + } elseif ($char == ',') { + // 替换word,替换, 恢复初始状态 + // 替换word,替换< 恢复初始状态 + $tmp = $this->VecMapReplace($word); + $extType .= $tmp; + $extType .= ','; + $word = ''; + $state = 'init'; + } else { + $word .= $char; + continue; + } + } + } + + return $extType; + } + + public function VecMapReplace($word) + { + $word = trim($word); + // 遍历所有的类型 + foreach (Utils::$wholeTypeMap as $key => $value) { + if (Utils::isStruct($word, $this->preStructs)) { + if (!in_array($word, $this->useStructs)) { + $this->extraUse .= 'use '.$this->namespaceName.'\\classes\\'.$word.';'.$this->returnSymbol; + $this->useStructs[] = $word; + } + + $word = 'new '.$word.'()'; + } elseif (in_array($word, $this->preNamespaceStructs)) { + $words = explode('::', $word); + $word = $words[1]; + if (!in_array($word, $this->useStructs)) { + $this->extraUse .= 'use protocol\\'.$this->namespaceName.'\\classes\\'.$word.';'.$this->returnSymbol; + $this->useStructs[] = $word; + } + + $word = 'new '.$word.'()'; + break; + } else { + $word = preg_replace('/\b'.$key.'\b/', $value, $word); + } + } + + return $word; + } + + public function paramParser($params) + { + + // 输入和输出的参数全部捋一遍 + $inParams = []; + $outParams = []; + foreach ($params as $param) { + $state = 'init'; + $word = ''; + $wholeType = ''; + $paramType = 'in'; + $type = ''; + $mapVectorState = false; + + for ($i = 0; $i < strlen($param); ++$i) { + $char = $param[$i]; + if ($state == 'init') { + // 有可能是换行 + if (Utils::isSpace($char)) { + continue; + } elseif ($char == "\n") { + break; + } elseif (Utils::inIdentifier($char)) { + $state = 'identifier'; + $word .= $char; + } else { + Utils::abnormalExit('error', 'Interface:'.$this->interfaceName.'内格式错误,请更正tars'); + } + } elseif ($state == 'identifier') { + // 如果遇到了space,需要检查是不是在map或vector的类型中,如果当前积累的word并不合法 + // 并且又不是处在vector或map的前置状态下的话,那么就是出错了 + if (Utils::isSpace($char)) { + if ($word == 'out') { + $paramType = $word; + $state = 'init'; + $word = ''; + } elseif (Utils::isBasicType($word)) { + $type = $word; + $state = 'init'; + $word = ''; + } elseif (Utils::isStruct($word, $this->preStructs)) { + + // 同时要把它增加到本Interface的依赖中 + if (!in_array($word, $this->useStructs)) { + $this->extraUse .= 'use '.$this->namespaceName.'\\classes\\'.$word.';'.$this->returnSymbol; + $this->useStructs[] = $word; + } + + $type = $word; + $state = 'init'; + $word = ''; + } elseif (Utils::isEnum($word, $this->preEnums)) { + $type = 'unsigned byte'; + $state = 'init'; + $word = ''; + } elseif (in_array($word, $this->preNamespaceStructs)) { + $word = explode('::', $word); + $word = $word[1]; + // 同时要把它增加到本Interface的依赖中 + if (!in_array($word, $this->useStructs)) { + $this->extraUse .= 'use '.$this->namespaceName.'\\classes\\'.$word.';'.$this->returnSymbol; + $this->useStructs[] = $word; + } + + $type = $word; + $state = 'init'; + $word = ''; + } elseif (in_array($word, $this->preNamespaceEnums)) { + $type = 'unsigned byte'; + $state = 'init'; + $word = ''; + } elseif (Utils::isMap($word)) { + $mapVectorState = true; + } elseif (Utils::isVector($word)) { + $mapVectorState = true; + } else { + // 读到了vector和map中间的空格,还没读完 + if ($mapVectorState) { + continue; + } + // 否则剩余的部分应该就是值和默认值 + else { + if (!empty($word)) { + $valueName = $word; + } + $state = 'init'; + $word = ''; + } + } + } + // 标志着map和vector的开始,不等到'>'的结束不罢休 + // 这时候需要使用栈来push,然后一个个对应的pop,从而达到type的遍历 + elseif ($char == '<') { + // 贪婪的向后,直到找出所有的'>' + $type = $word; + // 还会有一个wholeType,表示完整的部分 + $mapVectorStack = []; + $wholeType = $type; + $wholeType .= '<'; + array_push($mapVectorStack, '<'); + while (!empty($mapVectorStack)) { + $moreChar = $param[$i + 1]; + $wholeType .= $moreChar; + if ($moreChar == '<') { + array_push($mapVectorStack, '<'); + } elseif ($moreChar == '>') { + array_pop($mapVectorStack); + } + ++$i; + } + + $state = 'init'; + $word = ''; + } else { + $word .= $char; + } + } + } + + if (!empty($word)) { + $valueName = $word; + } + + if ($paramType == 'in') { + $inParams[] = [ + 'type' => $type, + 'wholeType' => $wholeType, + 'valueName' => $valueName, + ]; + } else { + $outParams[] = [ + 'type' => $type, + 'wholeType' => $wholeType, + 'valueName' => $valueName, + ]; + } + } + + return [ + 'in' => $inParams, + 'out' => $outParams, + ]; + } + + public function returnParser($returnType) + { + if (Utils::isStruct($returnType, $this->preStructs)) { + if (!in_array($returnType, $this->useStructs)) { + $this->extraUse .= 'use '.$this->namespaceName.'\\classes\\'.$returnType.';'.$this->returnSymbol; + $this->useStructs[] = $returnType; + } + $returnInfo = [ + 'type' => $returnType, + 'wholeType' => $returnType, + 'valueName' => $returnType, + ]; + + return $returnInfo; + } elseif (Utils::isBasicType($returnType)) { + $returnInfo = [ + 'type' => $returnType, + 'wholeType' => $returnType, + 'valueName' => $returnType, + ]; + + return $returnInfo; + } + + $state = 'init'; + $word = ''; + $wholeType = ''; + $type = ''; + $mapVectorState = false; + $valueName = ''; + + for ($i = 0; $i < strlen($returnType); ++$i) { + $char = $returnType[$i]; + if ($state == 'init') { + // 有可能是换行 + if (Utils::isSpace($char)) { + continue; + } elseif ($char == "\n") { + break; + } elseif (Utils::inIdentifier($char)) { + $state = 'identifier'; + $word .= $char; + } else { + Utils::abnormalExit('error', 'Interface内格式错误,请更正tars'); + } + } elseif ($state == 'identifier') { + // 如果遇到了space,需要检查是不是在map或vector的类型中,如果当前积累的word并不合法 + // 并且又不是处在vector或map的前置状态下的话,那么就是出错了 + //echo "[debug][state={$this->state}]word:".$word."\n"; + if (Utils::isSpace($char)) { + if (Utils::isBasicType($word)) { + $type = $word; + $state = 'init'; + $word = ''; + } elseif (Utils::isStruct($word, $this->preStructs)) { + + // 同时要把它增加到本Interface的依赖中 + if (!in_array($word, $this->useStructs)) { + $this->extraUse .= 'use '.$this->namespaceName.'\\classes\\'.$word.';'.$this->returnSymbol; + $this->useStructs[] = $word; + } + + $type = $word; + $state = 'init'; + $word = ''; + } elseif (Utils::isEnum($word, $this->preEnums)) { + $type = 'unsigned byte'; + $state = 'init'; + $word = ''; + } elseif (in_array($word, $this->preNamespaceStructs)) { + $word = explode('::', $word); + $word = $word[1]; + // 同时要把它增加到本Interface的依赖中 + if (!in_array($word, $this->useStructs)) { + $this->extraUse .= 'use '.$this->namespaceName.'\\classes\\'.$word.';'.$this->returnSymbol; + $this->useStructs[] = $word; + } + + $type = $word; + $state = 'init'; + $word = ''; + } elseif (in_array($word, $this->preNamespaceEnums)) { + $type = 'unsigned byte'; + $state = 'init'; + $word = ''; + } elseif (Utils::isMap($word)) { + $mapVectorState = true; + } elseif (Utils::isVector($word)) { + $mapVectorState = true; + } else { + // 读到了vector和map中间的空格,还没读完 + if ($mapVectorState) { + continue; + } + // 否则剩余的部分应该就是值和默认值 + else { + if (!empty($word)) { + $valueName = $word; + } + $state = 'init'; + $word = ''; + } + } + } + // 标志着map和vector的开始,不等到'>'的结束不罢休 + // 这时候需要使用栈来push,然后一个个对应的pop,从而达到type的遍历 + elseif ($char == '<') { + // 贪婪的向后,直到找出所有的'>' + $type = $word; + // 还会有一个wholeType,表示完整的部分 + $mapVectorStack = []; + $wholeType = $type; + $wholeType .= '<'; + array_push($mapVectorStack, '<'); + while (!empty($mapVectorStack)) { + $moreChar = $returnType[$i + 1]; + $wholeType .= $moreChar; + if ($moreChar == '<') { + array_push($mapVectorStack, '<'); + } elseif ($moreChar == '>') { + array_pop($mapVectorStack); + } + ++$i; + } + + $state = 'init'; + $word = ''; + } else { + $word .= $char; + } + } + } + + $returnInfo = [ + 'type' => $type, + 'wholeType' => $wholeType, + 'valueName' => $valueName, + ]; + + return $returnInfo; + } + /** + * @param $tag + * @param $requireType + * @param $type + * @param $name + * @param $wholeType + * @param $defaultValue + */ + public function writeInterfaceLine($returnType, $funcName, $params) + { + $result = $this->paramParser($params); + $inParams = $result['in']; + $outParams = $result['out']; + + // 处理通用的头部 + $funcHeader = $this->generateFuncHeader($funcName, $inParams, $outParams); + $returnInfo = $this->returnParser($returnType); + + $funcBodyArr = $this->generateFuncBody($inParams, $outParams, $returnInfo); + $synFuncBody = $funcBodyArr['syn']; + + $funcTail = $this->tabSymbol.'}'.$this->doubleReturn; + + $this->funcSet .= $funcHeader.$synFuncBody.$funcTail; + } + + /** + * @param $funcName + * @param $inParams + * @param $outParams + * + * @return string + */ + public function generateFuncHeader($funcName, $inParams, $outParams) + { + $paramsStr = ''; + foreach ($inParams as $param) { + $paramPrefix = Utils::paramTypeMap($param['type']); + $paramSuffix = '$'.$param['valueName']; + $paramsStr .= !empty($paramPrefix) ? $paramPrefix.' '.$paramSuffix.',' : $paramSuffix.','; + } + + foreach ($outParams as $param) { + $paramPrefix = Utils::paramTypeMap($param['type']); + $paramSuffix = '&$'.$param['valueName']; + $paramsStr .= !empty($paramPrefix) ? $paramPrefix.' '.$paramSuffix.',' : $paramSuffix.','; + } + + $paramsStr = trim($paramsStr, ','); + $paramsStr .= ') {'.$this->returnSymbol; + + $funcHeader = $this->tabSymbol.'public function '.$funcName.'('.$paramsStr; + + return $funcHeader; + } + + /** + * @param $funcName + * @param $inParams + * @param $outParams + * 生成函数的包体 + */ + public function generateFuncBody($inParams, $outParams, $returnInfo) + { + $bodyPrefix = $this->doubleTab.'try {'.$this->returnSymbol; + + $bodySuffix = $this->doubleTab.'catch (\\Exception $e) {'.$this->returnSymbol. + $this->tripleTab.'throw $e;'.$this->returnSymbol. + $this->doubleTab.'}'.$this->returnSymbol; + + $bodyMiddle = $this->tripleTab.'$requestPacket = new RequestPacket();'.$this->returnSymbol. + $this->tripleTab.'$requestPacket->_iVersion = $this->_iVersion;'.$this->returnSymbol. + $this->tripleTab.'$requestPacket->_funcName = __FUNCTION__;'.$this->returnSymbol. + $this->tripleTab.'$requestPacket->_servantName = $this->_servantName;'.$this->returnSymbol. + $this->tripleTab.'$encodeBufs = [];'.$this->doubleReturn; + + $commonPrefix = '$__buffer = TUPAPIWrapper::'; + + $index = 0; + foreach ($inParams as $param) { + ++$index; + $type = $param['type']; + + $packMethod = Utils::getPackMethods($type); + $valueName = $param['valueName']; + + // 判断如果是vector需要特别的处理 + if (Utils::isVector($type)) { + $vecFill = $this->tripleTab.'$'.$valueName.'_vec = '.$this->getExtType($param['wholeType'], $valueName).';'.$this->returnSymbol. + $this->tripleTab.'foreach($'.$valueName.' as '.'$single'.$valueName.') {'.$this->returnSymbol. + $this->quardupleTab.'$'.$valueName.'_vec->pushBack($single'.$valueName.');'.$this->returnSymbol. + $this->tripleTab.'}'.$this->returnSymbol; + $bodyMiddle .= $vecFill; + $bodyMiddle .= $this->tripleTab.$commonPrefix.$packMethod.'("'.$valueName."\",{$index},\$".$valueName.'_vec,$this->_iVersion);'.$this->returnSymbol; + } + + // 判断如果是map需要特别的处理 + elseif (Utils::isMap($type)) { + $mapFill = $this->tripleTab.'$'.$valueName.'_map = '.$this->getExtType($param['wholeType'], $valueName).';'.$this->returnSymbol. + $this->tripleTab.'foreach($'.$valueName.' as '.'$key => $value) {'.$this->returnSymbol. + $this->quardupleTab.'$'.$valueName.'_map->pushBack([$key => $value]);'.$this->returnSymbol. + $this->tripleTab.'}'.$this->returnSymbol; + $bodyMiddle .= $mapFill; + $bodyMiddle .= $this->tripleTab.$commonPrefix.$packMethod.'("'.$valueName."\",{$index},\$".$valueName.'_map,$this->_iVersion);'.$this->returnSymbol; + } + // 针对struct,需要额外的use过程 + elseif (Utils::isStruct($type, $this->preStructs)) { + if (!in_array($type, $this->useStructs)) { + $this->extraUse .= 'use '.$this->namespaceName.'\\classes\\'.$param['type'].';'.$this->returnSymbol; + $this->useStructs[] = $param['type']; + } + $bodyMiddle .= $this->tripleTab.$commonPrefix.$packMethod.'("'.$valueName."\",{$index},\$".$valueName.',$this->_iVersion);'.$this->returnSymbol; + } else { + $bodyMiddle .= $this->tripleTab.$commonPrefix.$packMethod.'("'.$valueName."\",{$index},\$".$valueName.',$this->_iVersion);'.$this->returnSymbol; + } + + $bodyMiddle .= $this->tripleTab."\$encodeBufs['{$valueName}'] = \$__buffer;".$this->returnSymbol; + } + + $bodyMiddle .= $this->tripleTab.'$requestPacket->_encodeBufs = $encodeBufs;'. + $this->doubleReturn; + + $bodyMiddle .= $this->tripleTab.'$sBuffer = $this->_communicator->invoke($requestPacket,$this->_iTimeout);'.$this->doubleReturn; + + foreach ($outParams as $param) { + ++$index; + + $type = $param['type']; + + $unpackMethods = Utils::getUnpackMethods($type); + $name = $param['valueName']; + + if (Utils::isBasicType($type)) { + $bodyMiddle .= $this->tripleTab."\$$name = TUPAPIWrapper::".$unpackMethods.'("'.$name."\",{$index},\$sBuffer,\$this->_iVersion);".$this->returnSymbol; + } else { + // 判断如果是vector需要特别的处理 + if (Utils::isVector($type) || Utils::isMap($type)) { + $bodyMiddle .= $this->tripleTab."\$$name = TUPAPIWrapper::".$unpackMethods.'("'.$name."\",{$index},".$this->getExtType($param['wholeType'], $name).',$sBuffer,$this->_iVersion);'.$this->returnSymbol; + } + // 如果是struct + elseif (Utils::isStruct($type, $this->preStructs)) { + $bodyMiddle .= $this->tripleTab.'$ret = TUPAPIWrapper::'.$unpackMethods.'("'.$name."\",{$index},\$$name,\$sBuffer,\$this->_iVersion);".$this->returnSymbol; + + if (!in_array($type, $this->useStructs)) { + $this->extraUse .= 'use '.$this->namespaceName.'\\classes\\'.$param['type'].';'.$this->returnSymbol; + $this->useStructs[] = $param['type']; + } + } + } + } + + // 还要尝试去获取一下接口的返回码哦 + $returnUnpack = Utils::getUnpackMethods($returnInfo['type']); + $valueName = $returnInfo['valueName']; + + if ($returnInfo['type'] !== 'void') { + if (Utils::isVector($returnInfo['type']) || Utils::isMap($returnInfo['type'])) { + $bodyMiddle .= $this->tripleTab.'return TUPAPIWrapper::'.$returnUnpack.'("",0,' + .$this->getExtType($returnInfo['wholeType'], $valueName).',$sBuffer,$this->_iVersion);'.$this->doubleReturn. + $this->doubleTab.'}'.$this->returnSymbol; + } elseif (Utils::isStruct($returnInfo['type'], $this->preStructs)) { + $bodyMiddle .= $this->tripleTab."\$returnVal = new $valueName();".$this->returnSymbol; + $bodyMiddle .= $this->tripleTab.'TUPAPIWrapper::'.$returnUnpack.'("",0,$returnVal,$sBuffer,$this->_iVersion);'.$this->returnSymbol; + $bodyMiddle .= $this->tripleTab.'return $returnVal;'.$this->doubleReturn. + $this->doubleTab.'}'.$this->returnSymbol; + + if (!in_array($returnInfo['type'], $this->useStructs)) { + $this->extraUse .= 'use '.$this->namespaceName.'\\classes\\'.$returnInfo['type'].';'.$this->returnSymbol; + $this->useStructs[] = $returnInfo['type']; + } + } else { + $bodyMiddle .= $this->tripleTab.'return TUPAPIWrapper::'.$returnUnpack.'("",0,$sBuffer,$this->_iVersion);'.$this->doubleReturn. + $this->doubleTab.'}'.$this->returnSymbol; + } + } else { + $bodyMiddle .= $this->doubleTab.'}'.$this->returnSymbol; + } + + $bodyStr = $bodyPrefix.$bodyMiddle.$bodySuffix; + + return [ + 'syn' => $bodyStr, + ]; + } +} + +class StructParser +{ + public $uniqueName; + public $moduleName; + public $structName; + public $state; + + // 这个结构体,可能会引用的部分,包括其他的结构体、枚举类型、常量 + public $preStructs; + public $preEnums; + public $preNamespaceEnums; + public $preNamespaceStructs; + + public $returnSymbol = "\n"; + public $doubleReturn = "\n\n"; + public $tabSymbol = "\t"; + public $doubleTab = "\t\t"; + public $tripleTab = "\t\t\t"; + public $quardupleTab = "\t\t\t\t"; + + public $extraContructs = ''; + public $extraExtInit = ''; + + public $consts = ''; + public $variables = ''; + public $fields = ''; + + public $namespaceName; + + public function __construct($fp, $line, $uniqueName, $moduleName, $structName, $preStructs, $preEnums, $namespaceName, + $preNamespaceEnums, $preNamespaceStructs) + { + $this->fp = $fp; + $this->uniqueName = $uniqueName; + $this->moduleName = $moduleName; + $this->preStructs = $preStructs; + $this->preEnums = $preEnums; + $this->structName = $structName; + $this->namespaceName = $namespaceName; + + $this->consts = ''; + $this->variables = ''; + $this->fields = ''; + + $this->preNamespaceEnums = $preNamespaceEnums; + $this->preNamespaceStructs = $preNamespaceStructs; + } + + public function parse() + { + while ($this->state != 'end') { + $this->structBodyParseLine(); + } + + // 先把积累下来的三个部分处理掉 + $structClassStr = $this->getStructClassHeader('\\classes'). + 'class '.$this->structName." extends \TARS_Struct {".$this->returnSymbol; + + $structClassStr .= $this->consts.$this->doubleReturn; + $structClassStr .= $this->variables.$this->doubleReturn; + $fieldsPrefix = $this->tabSymbol.'protected static $_fields = array('.$this->returnSymbol; + $fieldsSuffix = $this->tabSymbol.');'.$this->doubleReturn; + + $structClassStr .= $fieldsPrefix; + $structClassStr .= $this->fields; + $structClassStr .= $fieldsSuffix; + + // 处理最后一行 + + $construct = $this->tabSymbol.'public function __construct() {'.$this->returnSymbol. + $this->doubleTab."parent::__construct('".$this->uniqueName.'_'.$this->structName."', self::\$_fields);".$this->returnSymbol + .$this->extraContructs + .$this->extraExtInit + .$this->tabSymbol.'}'.$this->returnSymbol; + + $structClassStr .= $construct.'}'.$this->returnSymbol; + + return $structClassStr; + } + + /** + * @param $startChar + * @param $lineString + * + * @return string + * 专门处理注释 + */ + public function copyAnnotation($startChar, $lineString) + { + $lineString .= $startChar; + // 再读入一个字符 + $nextChar = fgetc($this->fp); + // 第一种 + if ($nextChar == '/') { + $lineString .= $nextChar; + while (1) { + $tmpChar = fgetc($this->fp); + if (Utils::isReturn($tmpChar)) { + $this->state = 'lineEnd'; + break; + } + $lineString .= $tmpChar; + } + + return $lineString; + } elseif ($nextChar == '*') { + $lineString .= $nextChar; + while (1) { + $tmpChar = fgetc($this->fp); + $lineString .= $tmpChar; + + if ($tmpChar === false) { + Utils::abnormalExit('error', '注释换行错误,请检查'); + } elseif (Utils::isReturn($tmpChar)) { + } elseif (($tmpChar) === '*') { + $nextnextChar = fgetc($this->fp); + if ($nextnextChar == '/') { + $lineString .= $nextnextChar; + + return $lineString; + } else { + $pos = ftell($this->fp); + fseek($this->fp, $pos - 1); + } + } + } + } + // 注释不正常 + else { + Utils::abnormalExit('error', '注释换行错误,请检查'); + } + } + + /** + * @param $fp + * @param $line + * 这里必须要引入状态机了 + */ + public function structBodyParseLine() + { + $validLine = false; + + $this->state = 'init'; + + $lineString = ''; + $word = ''; + $wholeType = ''; + $defaultValue = null; + + + $mapVectorState = false; + while (1) { + $char = fgetc($this->fp); + + if ($this->state == 'init') { + // 有可能是换行 + if ($char == '{' || Utils::isSpace($char) || $char == '\r' + || $char == '\x0B' || $char == '\0') { + continue; + } elseif ($char == "\n") { + break; + } + // 遇到了注释会用贪婪算法全部处理完,同时填充到struct的类里面去 + elseif ($char == '/') { + $this->copyAnnotation($char, $lineString); + break; + } elseif (Utils::inIdentifier($char)) { + $this->state = 'identifier'; + $word .= $char; + } + // 终止条件之1,宣告struct结束 + elseif ($char == '}') { + // 需要贪心的读到"\n"为止 + while (($lastChar = fgetc($this->fp)) != "\n") { + continue; + } + $this->state = 'end'; + break; + } elseif ($char == '=') { + //遇到等号,可以贪婪的向后,直到遇到;或者换行符 + if (!empty($word)) { + $valueName = $word; + } + $moreChar = fgetc($this->fp); + + $defaultValue = ''; + + while ($moreChar != '\n' && $moreChar != ';' && $moreChar != '}') { + $defaultValue .= $moreChar; + + $moreChar = fgetc($this->fp); + } + //if(empty($defaultValue)) { + // Utils::abnormalExit('error','结构体'.$this->structName.'内默认值格式错误,请更正tars'); + //} + + if ($moreChar == '}') { + // 需要贪心的读到"\n"为止 + while (($lastChar = fgetc($this->fp)) != "\n") { + continue; + } + $this->state = 'end'; + } else { + $this->state = 'init'; + } + } else { + //echo "char:".var_export($char,true); + //Utils::abnormalExit('error','结构体'.$this->structName.'内格式错误,请更正tars'); + continue; + } + } elseif ($this->state == 'identifier') { + $validLine = true; + // 如果遇到了space,需要检查是不是在map或vector的类型中,如果当前积累的word并不合法 + // 并且又不是处在vector或map的前置状态下的话,那么就是出错了 + if (Utils::isSpace($char)) { + if (Utils::isTag($word)) { + $tag = $word; + $this->state = 'init'; + $word = ''; + } elseif (Utils::isRequireType($word)) { + $requireType = $word; + $this->state = 'init'; + $word = ''; + } elseif ($word == 'unsigned') { + $word = $word.' '; + continue; + } elseif (Utils::isBasicType($word)) { + $type = $word; + $this->state = 'init'; + $word = ''; + } elseif (Utils::isStruct($word, $this->preStructs)) { + $type = $word; + $this->state = 'init'; + $word = ''; + } elseif (Utils::isEnum($word, $this->preEnums)) { + $type = 'int'; + $this->state = 'init'; + $word = ''; + } + // 增加对namespace的支持 + elseif (in_array($word, $this->preNamespaceStructs)) { + $type = explode('::', $word); + $type = $type[1]; + $this->state = 'init'; + $word = ''; + } + // 增加对namespace的支持 + elseif (in_array($word, $this->preNamespaceEnums)) { + $type = 'int'; + $this->state = 'init'; + $word = ''; + } elseif ($word == 'unsigned') { + $word = $word.' '; + continue; + } else { + // 读到了vector和map中间的空格,还没读完 + if ($mapVectorState) { + continue; + } + // 否则剩余的部分应该就是值和默认值 + else { + if (!empty($word)) { + $valueName = $word; + } + $this->state = 'init'; + $word = ''; + } + } + } + // 标志着map和vector的开始,不等到'>'的结束不罢休 + // 这时候需要使用栈来push,然后一个个对应的pop,从而达到type的遍历 + elseif ($char == '<') { + // 贪婪的向后,直到找出所有的'>' + $type = $word; + // 还会有一个wholeType,表示完整的部分 + $mapVectorStack = []; + $wholeType = $type; + $wholeType .= '<'; + array_push($mapVectorStack, '<'); + while (!empty($mapVectorStack)) { + $moreChar = fgetc($this->fp); + $wholeType .= $moreChar; + if ($moreChar == '<') { + array_push($mapVectorStack, '<'); + } elseif ($moreChar == '>') { + array_pop($mapVectorStack); + } + } + + $this->state = 'init'; + $word = ''; + } elseif ($char == '=') { + //遇到等号,可以贪婪的向后,直到遇到;或者换行符 + if (!empty($word)) { + $valueName = $word; + } + $moreChar = fgetc($this->fp); + + $defaultValue = ''; + + while ($moreChar != '\n' && $moreChar != ';' && $moreChar != '}') { + $defaultValue .= $moreChar; + + $moreChar = fgetc($this->fp); + } + //if(empty($defaultValue)) { + // Utils::abnormalExit('error','结构体'.$this->structName.'内默认值格式错误,请更正tars'); + //} + + if ($moreChar == '}') { + // 需要贪心的读到"\n"为止 + while (($lastChar = fgetc($this->fp)) != "\n") { + continue; + } + $this->state = 'end'; + } else { + $this->state = 'init'; + } + } elseif ($char == ';') { + if (!empty($word)) { + $valueName = $word; + } + continue; + } + // 终止条件之2,同样宣告struct结束 + elseif ($char == '}') { + // 需要贪心的读到"\n"为止 + while (($lastChar = fgetc($this->fp)) != "\n") { + continue; + } + $this->state = 'end'; + } elseif ($char == '/') { + $lineString = $this->copyAnnotation($char, $lineString); + } elseif ($char == "\n") { + break; + } else { + $word .= $char; + } + } elseif ($this->state == 'lineEnd') { + if ($char == '}') { + // 需要贪心的读到"\n"为止 + while (($lastChar = fgetc($this->fp)) != "\n") { + continue; + } + $this->state = 'end'; + } + break; + } elseif ($this->state == 'end') { + break; + } + } + + if (!$validLine) { + return; + } + + // 完成了这一行的词法解析,需要输出如下的字段 + // echo "RAW tag:".$tag." requireType:".$requireType." type:".$type. + // " valueName:".$valueName. " wholeType:".$wholeType. + // " defaultValue:".$defaultValue." lineString:".$lineString."\n\n"; + + if (!isset($tag) || empty($requireType) || empty($type) || empty($valueName)) { + Utils::abnormalExit('error', '结构体'.$this->structName.'内格式错误,请更正tars'); + } elseif ($type == 'map' && empty($wholeType)) { + Utils::abnormalExit('error', '结构体'.$this->structName.'内map格式错误,请更正tars'); + } elseif ($type == 'vector' && empty($wholeType)) { + Utils::abnormalExit('error', '结构体'.$this->structName.'内vector格式错误,请更正tars'); + } else { + $this->writeStructLine($tag, $requireType, $type, $valueName, $wholeType, $defaultValue); + } + } + + /** + * @param $wholeType + * 通过完整的类型获取vector的扩展类型 + * vector => new \TARS_VECTOR(new CateObj()) + * vector => new \TARS_VECTOR(\TARS::STRING) + * vector> => new \TARS_VECTOR(new \TARS_MAP(\TARS_MAP,new CateObj())) + */ + public function getExtType($wholeType, $valueName) + { + $state = 'init'; + $word = ''; + $extType = ''; + + for ($i = 0; $i < strlen($wholeType); ++$i) { + $char = $wholeType[$i]; + if ($state == 'init') { + // 如果遇到了空格 + if (Utils::isSpace($char)) { + continue; + } + // 回车是停止符号 + elseif (Utils::inIdentifier($char)) { + $state = 'indentifier'; + $word .= $char; + } elseif (Utils::isReturn($char)) { + break; + } elseif ($char == '>') { + $extType .= ')'; + continue; + } + } elseif ($state == 'indentifier') { + if ($char == '<') { + // 替换word,替换< 恢复初始状态 + $tmp = $this->VecMapReplace($word); + $extType .= $tmp; + $extType .= '('; + $word = ''; + $state = 'init'; + } elseif ($char == '>') { + // 替换word,替换> 恢复初始状态 + // 替换word,替换< 恢复初始状态 + $tmp = $this->VecMapReplace($word); + $extType .= $tmp; + $extType .= ')'; + $word = ''; + $state = 'init'; + } elseif ($char == ',') { + // 替换word,替换, 恢复初始状态 + // 替换word,替换< 恢复初始状态 + $tmp = $this->VecMapReplace($word); + $extType .= $tmp; + $extType .= ','; + $word = ''; + $state = 'init'; + } else { + $word .= $char; + continue; + } + } + } + + return $extType; + } + + public function VecMapReplace($word) + { + $word = trim($word); + // 遍历所有的类型 + foreach (Utils::$wholeTypeMap as $key => $value) { + $word = preg_replace('/\b'.$key.'\b/', $value, $word); + } + + if (Utils::isStruct($word, $this->preStructs)) { + $word = 'new '.$word.'()'; + } + + return $word; + } + + /** + * @param $tag + * @param $requireType + * @param $type + * @param $name + * @param $wholeType + * @param $defaultValue + */ + public function writeStructLine($tag, $requireType, $type, $valueName, $wholeType, $defaultValue) + { + if ($requireType === 'require') { + $requireFlag = 'true'; + } else { + $requireFlag = 'false'; + } + + $this->consts .= $this->tabSymbol.'const '.strtoupper($valueName).' = '.$tag.';'.$this->returnSymbol; + if (!empty($defaultValue)) { + $this->variables .= $this->tabSymbol.'public $'.$valueName.'='.$defaultValue.';'.' '.$this->returnSymbol; + } else { + $this->variables .= $this->tabSymbol.'public $'.$valueName.';'.' '.$this->returnSymbol; + } + + // 基本类型,直接替换 + if (Utils::isBasicType($type)) { + $this->fields .= $this->doubleTab.'self::'.strtoupper($valueName).' => array('.$this->returnSymbol. + $this->tripleTab."'name'=>'".$valueName."',".$this->returnSymbol. + $this->tripleTab."'required'=>".$requireFlag.','.$this->returnSymbol. + $this->tripleTab."'type'=>".Utils::getRealType($type).','.$this->returnSymbol. + $this->tripleTab.'),'.$this->returnSymbol; + } elseif (Utils::isStruct($type, $this->preStructs)) { + $this->fields .= $this->doubleTab.'self::'.strtoupper($valueName).' => array('.$this->returnSymbol. + $this->tripleTab."'name'=>'".$valueName."',".$this->returnSymbol. + $this->tripleTab."'required'=>".$requireFlag.','.$this->returnSymbol. + $this->tripleTab."'type'=>".Utils::getRealType($type).','.$this->returnSymbol. + $this->tripleTab.'),'.$this->returnSymbol; + $this->extraContructs .= $this->doubleTab."\$this->$valueName = new $type();".$this->returnSymbol; + } elseif (Utils::isVector($type) || Utils::isMap($type)) { + $extType = $this->getExtType($wholeType, $valueName); + $this->extraExtInit .= $this->doubleTab.'$this->'.$valueName.' = '.$extType.';'.$this->returnSymbol; + + $this->fields .= $this->doubleTab.'self::'.strtoupper($valueName).' => array('.$this->returnSymbol. + $this->tripleTab."'name'=>'".$valueName."',".$this->returnSymbol. + $this->tripleTab."'required'=>".$requireFlag.','.$this->returnSymbol. + $this->tripleTab."'type'=>".Utils::getRealType($type).','.$this->returnSymbol. + $this->tripleTab.'),'.$this->returnSymbol; + } else { + Utils::abnormalExit('error', '结构体struct'.$this->structName.'内类型有误,请更正tars'); + } + } + + public function getStructClassHeader($prefix = 'getFileHea') + { + return "namespaceName.$prefix.';'. + $this->doubleReturn; + } +} + +class ServantParser +{ + public $namespaceName; + public $moduleName; + public $interfaceName; + + public $state; + + // 这个结构体,可能会引用的部分,包括其他的结构体、枚举类型、常量 + public $useStructs = []; + public $extraUse; + public $preStructs; + public $preEnums; + + public $preNamespaceEnums = []; + public $preNamespaceStructs = []; + + public $firstLine; + + public $returnSymbol = "\n"; + public $doubleReturn = "\n\n"; + public $tabSymbol = "\t"; + public $doubleTab = "\t\t"; + public $tripleTab = "\t\t\t"; + public $quardupleTab = "\t\t\t\t"; + + public $extraContructs = ''; + public $extraExtType = ''; + public $extraExtInit = ''; + + public $consts = ''; + public $variables = ''; + public $fields = ''; + + public $funcSet = ''; + + public $servantName; + + public function __construct($fp, $line, $namespaceName, $moduleName, + $interfaceName, $preStructs, + $preEnums, $servantName, $preNamespaceEnums, $preNamespaceStructs) + { + $this->fp = $fp; + $this->firstLine = $line; + $this->namespaceName = $namespaceName; + $this->moduleName = $moduleName; + $this->preStructs = $preStructs; + $this->preEnums = $preEnums; + $this->interfaceName = $interfaceName; + $this->servantName = $servantName; + + $this->extraUse = ''; + $this->useStructs = []; + + $this->preNamespaceEnums = $preNamespaceEnums; + $this->preNamespaceStructs = $preNamespaceStructs; + } + + public function isEnum($word) + { + return in_array($word, $this->preEnums); + } + + public function isStruct($word) + { + return in_array($word, $this->preStructs); + } + + public function getFileHeader($prefix = '') + { + return "namespaceName.$prefix.';'. + $this->doubleReturn; + } + + public function parse() + { + while ($this->state != 'end') { + $this->InterfaceFuncParseLine(); + } + + // todo serverName+servant + $interfaceClass = $this->getFileHeader('').$this->extraUse.'interface '.$this->interfaceName.' {'.$this->returnSymbol; + + $interfaceClass .= $this->funcSet; + + $interfaceClass .= '}'.$this->doubleReturn; + + return $interfaceClass; + } + + /** + * @param $startChar + * @param $lineString + * + * @return string + * 专门处理注释 + */ + public function copyAnnotation() + { + // 再读入一个字符 + $nextChar = fgetc($this->fp); + // 第一种 + if ($nextChar == '/') { + while (1) { + $tmpChar = fgetc($this->fp); + if (Utils::isReturn($tmpChar)) { + $this->state = 'lineEnd'; + break; + } + } + + return; + } elseif ($nextChar == '*') { + while (1) { + $tmpChar = fgetc($this->fp); + + if ($tmpChar === false) { + Utils::abnormalExit('error', $this->interfaceName.'注释换行错误,请检查'); + } elseif (Utils::isReturn($tmpChar)) { + } elseif (($tmpChar) === '*') { + $nextnextChar = fgetc($this->fp); + if ($nextnextChar == '/') { + return; + } else { + $pos = ftell($this->fp); + fseek($this->fp, $pos - 1); + } + } + } + } + // 注释不正常 + else { + Utils::abnormalExit('error', $this->interfaceName.'注释换行错误,请检查'); + } + } + + /** + * @param $fp + * @param $line + * 这里必须要引入状态机了 + * 这里并不一定要一个line呀,应该找)作为结束符 + */ + public function InterfaceFuncParseLine() + { + $line = ''; + $this->state = 'init'; + while (1) { + $char = fgetc($this->fp); + + if ($this->state == 'init') { + // 有可能是换行 + if ($char == '{' || Utils::isReturn($char)) { + continue; + } + // 遇到了注释会用贪婪算法全部处理完,同时填充到struct的类里面去 + elseif ($char == '/') { + $this->copyAnnotation(); + break; + } elseif (Utils::inIdentifier($char)) { + $this->state = 'identifier'; + $line .= $char; + } + // 终止条件之1,宣告struct结束 + elseif ($char == '}') { + // 需要贪心的读到"\n"为止 + while (($lastChar = fgetc($this->fp)) != "\n") { + continue; + } + $this->state = 'end'; + break; + } + } elseif ($this->state == 'identifier') { + if ($char == '/') { + $this->copyAnnotation(); + } elseif ($char == ';') { + $line .= $char; + break; + } + // 终止条件之2,同样宣告interface结束 + elseif ($char == '}') { + // 需要贪心的读到"\n"为止 + while (($lastChar = fgetc($this->fp)) != "\n") { + continue; + } + $this->state = 'end'; + } elseif (Utils::isReturn($char)) { + continue; + } elseif ($char == ')') { + $line .= $char; + // 需要贪心的读到"\n"为止 + while (($lastChar = fgetc($this->fp)) != "\n") { + continue; + } + $this->state = 'lineEnd'; + } else { + $line .= $char; + } + } elseif ($this->state == 'lineEnd') { + if ($char == '}') { + // 需要贪心的读到"\n"为止 + while (($lastChar = fgetc($this->fp)) != "\n") { + continue; + } + $this->state = 'end'; + } + break; + } elseif ($this->state == 'end') { + break; + } + } + + if (empty($line)) { + return; + } + + $line = trim($line); + + // 如果空行,或者是注释,或者是大括号就直接略过 + if (!trim($line) || trim($line)[0] === '/' || trim($line)[0] === '*' || trim($line) === '{') { + return; + } + + $endFlag = strpos($line, '};'); + if ($endFlag !== false) { + $this->state = 'end'; + + return; + } + + $endFlag = strpos($line, '}'); + if ($endFlag !== false) { + $this->state = 'end'; + + return; + } + + // 有必要先分成三个部分,返回类型、接口名、参数列表 todo + $tokens = preg_split('/\(/', $line, 2); + $mix = trim($tokens[0]); + $rest = $tokens[1]; + + $pices = preg_split('/\s+/', $mix); + + $funcName = $pices[count($pices) - 1]; + + $returnType = implode('', array_slice($pices, 0, count($pices) - 1)); + + $state = 'init'; + $word = ''; + + $params = []; + + for ($i = 0; $i < strlen($rest); ++$i) { + $char = $rest[$i]; + + if ($state == 'init') { + // 有可能是换行 + if ($char == '(' || Utils::isSpace($char)) { + continue; + } elseif (Utils::isReturn($char)) { + break; + } elseif (Utils::inIdentifier($char)) { + $state = 'identifier'; + $word .= $char; + } + // 终止条件之1,宣告interface结束 + elseif ($char == ')') { + break; + } else { + Utils::abnormalExit('error', 'Interface'.$this->interfaceName.'内格式错误,请更正tars in line:'.__LINE__); + } + } elseif ($state == 'identifier') { + if ($char == ',') { + $params[] = $word; + $state = 'init'; + $word = ''; + continue; + } + // 标志着map和vector的开始,不等到'>'的结束不罢休 + // 这时候需要使用栈来push,然后一个个对应的pop,从而达到type的遍历 + elseif ($char == '<') { + $mapVectorStack = []; + $word .= $char; + array_push($mapVectorStack, '<'); + while (!empty($mapVectorStack)) { + $moreChar = $rest[$i + 1]; + $word .= $moreChar; + if ($moreChar == '<') { + array_push($mapVectorStack, '<'); + } elseif ($moreChar == '>') { + array_pop($mapVectorStack); + } + ++$i; + } + continue; + } elseif ($char == ')') { + $params[] = $word; + break; + } elseif ($char == ';') { + continue; + } + // 终止条件之2,同样宣告struct结束 + elseif ($char == '}') { + $state = 'end'; + } elseif (Utils::isReturn($char)) { + break; + } else { + $word .= $char; + } + } elseif ($state == 'lineEnd') { + break; + } elseif ($state == 'end') { + break; + } + } + + $this->writeInterfaceLine($returnType, $funcName, $params); + } + + /** + * @param $wholeType + * 通过完整的类型获取vector的扩展类型 + */ + public function getExtType($wholeType) + { + $state = 'init'; + $word = ''; + $extType = ''; + + for ($i = 0; $i < strlen($wholeType); ++$i) { + $char = $wholeType[$i]; + if ($state == 'init') { + // 如果遇到了空格 + if (Utils::isSpace($char)) { + continue; + } + // 回车是停止符号 + elseif (Utils::inIdentifier($char)) { + $state = 'indentifier'; + $word .= $char; + } elseif (Utils::isReturn($char)) { + break; + } elseif ($char == '>') { + $extType .= ')'; + continue; + } + } elseif ($state == 'indentifier') { + if ($char == '<') { + // 替换word,替换< 恢复初始状态 + $tmp = $this->VecMapReplace($word); + $extType .= $tmp; + $extType .= '('; + $word = ''; + $state = 'init'; + } elseif ($char == '>') { + // 替换word,替换> 恢复初始状态 + // 替换word,替换< 恢复初始状态 + $tmp = $this->VecMapReplace($word); + $extType .= $tmp; + $extType .= ')'; + $word = ''; + $state = 'init'; + } elseif ($char == ',') { + // 替换word,替换, 恢复初始状态 + // 替换word,替换< 恢复初始状态 + $tmp = $this->VecMapReplace($word); + $extType .= $tmp; + $extType .= ','; + $word = ''; + $state = 'init'; + } else { + $word .= $char; + continue; + } + } + } + + return $extType; + } + + public function VecMapReplace($word) + { + $word = trim($word); + // 遍历所有的类型 + foreach (Utils::$wholeTypeMap as $key => $value) { + if ($this->isStruct($word)) { + if (!in_array($word, $this->useStructs)) { + $this->useStructs[] = $word; + } + $word = '\\'.$this->namespaceName.'\\classes\\'.$word; + break; + } elseif (in_array($word, $this->preNamespaceStructs)) { + $words = explode('::', $word); + $word = $words[1]; + if (!in_array($word, $this->useStructs)) { + $this->useStructs[] = $word; + } + $word = '\\'.$this->namespaceName.'\\classes\\'.$word; + break; + } else { + $word = preg_replace('/\b'.$key.'\b/', $value, $word); + } + } + + $word = trim($word, 'new '); + + return $word; + } + + public function paramParser($params) + { + + // 输入和输出的参数全部捋一遍 + $inParams = []; + $outParams = []; + foreach ($params as $param) { + $state = 'init'; + $word = ''; + $wholeType = ''; + $paramType = 'in'; + $type = ''; + $mapVectorState = false; + + for ($i = 0; $i < strlen($param); ++$i) { + $char = $param[$i]; + if ($state == 'init') { + // 有可能是换行 + if (Utils::isSpace($char)) { + continue; + } elseif (Utils::isReturn($char)) { + break; + } elseif (Utils::inIdentifier($char)) { + $state = 'identifier'; + $word .= $char; + } else { + Utils::abnormalExit('error', 'Interface内格式错误,请更正tars in line:'.__LINE__); + } + } elseif ($state == 'identifier') { + // 如果遇到了space,需要检查是不是在map或vector的类型中,如果当前积累的word并不合法 + // 并且又不是处在vector或map的前置状态下的话,那么就是出错了 + if (Utils::isSpace($char)) { + if ($word == 'out') { + $paramType = $word; + $state = 'init'; + $word = ''; + } elseif (Utils::isBasicType($word)) { + $type = $word; + $state = 'init'; + $word = ''; + } elseif ($this->isStruct($word)) { + + // 同时要把它增加到本Interface的依赖中 + if (!in_array($word, $this->useStructs)) { + $this->extraUse .= 'use '.$this->namespaceName.'\\classes\\'.$word.';'.$this->returnSymbol; + $this->useStructs[] = $word; + } + + $type = $word; + $state = 'init'; + $word = ''; + } elseif ($this->isEnum($word)) { + $type = 'int'; + $state = 'init'; + $word = ''; + } elseif (in_array($word, $this->preNamespaceStructs)) { + $word = explode('::', $word); + $word = $word[1]; + // 同时要把它增加到本Interface的依赖中 + if (!in_array($word, $this->useStructs)) { + $this->extraUse .= 'use '.$this->namespaceName.'\\classes\\'.$word.';'.$this->returnSymbol; + $this->useStructs[] = $word; + } + + $type = $word; + $state = 'init'; + $word = ''; + } elseif (in_array($word, $this->preNamespaceEnums)) { + $type = 'int'; + $state = 'init'; + $word = ''; + } elseif (Utils::isMap($word)) { + $mapVectorState = true; + } elseif (Utils::isVector($word)) { + $mapVectorState = true; + } else { + // 读到了vector和map中间的空格,还没读完 + if ($mapVectorState) { + continue; + } + // 否则剩余的部分应该就是值和默认值 + else { + if (!empty($word)) { + $valueName = $word; + } + $state = 'init'; + $word = ''; + } + } + } + // 标志着map和vector的开始,不等到'>'的结束不罢休 + // 这时候需要使用栈来push,然后一个个对应的pop,从而达到type的遍历 + elseif ($char == '<') { + // 贪婪的向后,直到找出所有的'>' + $type = $word; + // 还会有一个wholeType,表示完整的部分 + $mapVectorStack = []; + $wholeType = $type; + $wholeType .= '<'; + array_push($mapVectorStack, '<'); + while (!empty($mapVectorStack)) { + $moreChar = $param[$i + 1]; + $wholeType .= $moreChar; + if ($moreChar == '<') { + array_push($mapVectorStack, '<'); + } elseif ($moreChar == '>') { + array_pop($mapVectorStack); + } + ++$i; + } + + $state = 'init'; + $word = ''; + } else { + $word .= $char; + } + } + } + + if (!empty($word)) { + $valueName = $word; + } + + if ($paramType == 'in') { + $inParams[] = [ + 'type' => $type, + 'wholeType' => $wholeType, + 'valueName' => $valueName, + ]; + } else { + $outParams[] = [ + 'type' => $type, + 'wholeType' => $wholeType, + 'valueName' => $valueName, + ]; + } + } + + return [ + 'in' => $inParams, + 'out' => $outParams, + ]; + } + + public function returnParser($returnType) + { + if ($this->isStruct($returnType)) { + if (!in_array($returnType, $this->useStructs)) { + $this->useStructs[] = $returnType; + } + $returnInfo = [ + 'type' => $returnType, + 'wholeType' => $returnType, + 'valueName' => $returnType, + ]; + + return $returnInfo; + } elseif ($this->isEnum($returnType)) { + $returnInfo = [ + 'type' => $returnType, + 'wholeType' => $returnType, + 'valueName' => $returnType, + ]; + + return $returnInfo; + } elseif (Utils::isBasicType($returnType)) { + $returnInfo = [ + 'type' => $returnType, + 'wholeType' => $returnType, + 'valueName' => $returnType, + ]; + + return $returnInfo; + } + + $state = 'init'; + $word = ''; + $wholeType = ''; + $type = ''; + $mapVectorState = false; + $valueName = ''; + + for ($i = 0; $i < strlen($returnType); ++$i) { + $char = $returnType[$i]; + if ($state == 'init') { + // 有可能是换行 + if (Utils::isSpace($char)) { + continue; + } elseif ($char == "\n") { + break; + } elseif (Utils::inIdentifier($char)) { + $state = 'identifier'; + $word .= $char; + } else { + Utils::abnormalExit('error', 'Interface内格式错误,请更正tars'); + } + } elseif ($state == 'identifier') { + // 如果遇到了space,需要检查是不是在map或vector的类型中,如果当前积累的word并不合法 + // 并且又不是处在vector或map的前置状态下的话,那么就是出错了 + if (Utils::isSpace($char)) { + if (Utils::isBasicType($word)) { + $type = $word; + $state = 'init'; + $word = ''; + } elseif ($this->isStruct($word)) { + + // 同时要把它增加到本Interface的依赖中 + if (!in_array($word, $this->useStructs)) { + $this->extraUse .= 'use '.$this->namespaceName.'\\classes\\'.$word.';'.$this->returnSymbol; + $this->useStructs[] = $word; + } + + $type = $word; + $state = 'init'; + $word = ''; + } elseif ($this->isEnum($word)) { + $type = 'int'; + $state = 'init'; + $word = ''; + } elseif (in_array($word, $this->preNamespaceStructs)) { + $word = explode('::', $word); + $word = $word[1]; + // 同时要把它增加到本Interface的依赖中 + if (!in_array($word, $this->useStructs)) { + $this->extraUse .= 'use '.$this->namespaceName.'\\classes\\'.$word.';'.$this->returnSymbol; + $this->useStructs[] = $word; + } + + $type = $word; + $state = 'init'; + $word = ''; + } elseif (in_array($word, $this->preNamespaceEnums)) { + $type = 'int'; + $state = 'init'; + $word = ''; + } elseif (Utils::isMap($word)) { + $mapVectorState = true; + } elseif (Utils::isVector($word)) { + $mapVectorState = true; + } else { + // 读到了vector和map中间的空格,还没读完 + if ($mapVectorState) { + continue; + } + // 否则剩余的部分应该就是值和默认值 + else { + if (!empty($word)) { + $valueName = $word; + } + $state = 'init'; + $word = ''; + } + } + } + // 标志着map和vector的开始,不等到'>'的结束不罢休 + // 这时候需要使用栈来push,然后一个个对应的pop,从而达到type的遍历 + elseif ($char == '<') { + // 贪婪的向后,直到找出所有的'>' + $type = $word; + // 还会有一个wholeType,表示完整的部分 + $mapVectorStack = []; + $wholeType = $type; + $wholeType .= '<'; + array_push($mapVectorStack, '<'); + while (!empty($mapVectorStack)) { + $moreChar = $returnType[$i + 1]; + $wholeType .= $moreChar; + if ($moreChar == '<') { + array_push($mapVectorStack, '<'); + } elseif ($moreChar == '>') { + array_pop($mapVectorStack); + } + ++$i; + } + + $state = 'init'; + $word = ''; + } else { + $word .= $char; + } + } + } + + $returnInfo = [ + 'type' => $type, + 'wholeType' => $wholeType, + 'valueName' => $valueName, + ]; + + return $returnInfo; + } + + /** + * @param $tag + * @param $requireType + * @param $type + * @param $name + * @param $wholeType + * @param $defaultValue + */ + public function writeInterfaceLine($returnType, $funcName, $params) + { + $result = $this->paramParser($params); + $inParams = $result['in']; + $outParams = $result['out']; + + $returnInfo = $this->returnParser($returnType); + + $funcAnnotation = $this->generateFuncAnnotation($inParams, $outParams, $returnInfo); + + // 函数定义恰恰是要放在最后面了 + $funcDefinition = $this->generateFuncHeader($funcName, $inParams, $outParams); + + $this->funcSet .= $funcAnnotation.$funcDefinition; + } + + private function paramTypeMap($paramType) + { + if (Utils::isBasicType($paramType) || Utils::isMap($paramType) || Utils::isVector($paramType)) { + return ''; + } else { + return $paramType; + } + } + /** + * @param $funcName + * @param $inParams + * @param $outParams + * + * @return string + */ + public function generateFuncHeader($funcName, $inParams, $outParams) + { + $paramsStr = ''; + foreach ($inParams as $param) { + $paramPrefix = $this->paramTypeMap($param['type']); + $paramSuffix = '$'.$param['valueName']; + $paramsStr .= !empty($paramPrefix) ? $paramPrefix.' '.$paramSuffix.',' : $paramSuffix.','; + } + + foreach ($outParams as $param) { + $paramPrefix = $this->paramTypeMap($param['type']); + $paramSuffix = '&$'.$param['valueName']; + $paramsStr .= !empty($paramPrefix) ? $paramPrefix.' '.$paramSuffix.',' : $paramSuffix.','; + } + + $paramsStr = trim($paramsStr, ','); + $paramsStr .= ');'.$this->returnSymbol; + + $funcHeader = $this->tabSymbol.'public function '.$funcName.'('.$paramsStr; + + return $funcHeader; + } + + /** + * @param $funcName + * @param $inParams + * @param $outParams + * 生成函数的包体 + */ + public function generateFuncAnnotation($inParams, $outParams, $returnInfo) + { + $bodyPrefix = $this->tabSymbol.'/**'.$this->returnSymbol; + + $bodyMiddle = ''; + + foreach ($inParams as $param) { + $annotation = $this->tabSymbol.' * @param '; + $type = $param['type']; + $valueName = $param['valueName']; + $wholeType = $param['wholeType']; + + // 判断如果是vector需要特别的处理 + if (Utils::isVector($type)) { + $annotation .= 'vector'.' $'.$valueName.' '.$this->getExtType($wholeType); + } + + // 判断如果是map需要特别的处理 + elseif (Utils::isMap($type)) { + $annotation .= 'map'.' $'.$valueName.' '.$this->getExtType($wholeType); + } + // 针对struct,需要额外的use过程 + elseif ($this->isStruct($type)) { + $annotation .= 'struct'.' $'.$valueName.' \\'.$this->namespaceName.'\\classes\\'.$type; + } else { + $annotation .= $type.' $'.$valueName.' '; + } + $bodyMiddle .= $annotation.$this->returnSymbol; + } + + foreach ($outParams as $param) { + $annotation = $this->tabSymbol.' * @param '; + $type = $param['type']; + $valueName = $param['valueName']; + $wholeType = $param['wholeType']; + + if (Utils::isBasicType($type)) { + $annotation .= $type.' $'.$valueName; + } else { + // 判断如果是vector需要特别的处理 + if (Utils::isVector($type)) { + $annotation .= 'vector'.' $'.$valueName.' '.$this->getExtType($wholeType); + } elseif (Utils::isMap($type)) { + $annotation .= 'map'.' $'.$valueName.' '.$this->getExtType($wholeType); + } + // 如果是struct + elseif ($this->isStruct($type)) { + $annotation .= 'struct'.' $'.$valueName.' \\'.$this->namespaceName.'\\classes\\'.$type; + } + } + + $annotation .= ' =out='.$this->returnSymbol; + $bodyMiddle .= $annotation; + } + + // 还要尝试去获取一下接口的返回码哦 + $type = $returnInfo['type']; + $valueName = $returnInfo['valueName']; + $wholeType = $returnInfo['wholeType']; + + $annotation = $this->tabSymbol.' * @return '; + + if ($type !== 'void') { + if (Utils::isVector($type)) { + $annotation .= 'vector '.$this->getExtType($wholeType); + } elseif (Utils::isMap($type)) { + $annotation .= 'map '.$this->getExtType($wholeType); + } elseif ($this->isStruct($type)) { + $annotation .= 'struct \\'.$this->namespaceName.'\\classes\\'.$type; + } else { + $annotation .= $type; + } + } else { + $annotation .= 'void'; + } + + $bodyMiddle .= $annotation.$this->returnSymbol.$this->tabSymbol.' */'.$this->returnSymbol; + + $bodyStr = $bodyPrefix.$bodyMiddle; + + return $bodyStr; + } +} diff --git a/tars_c.c b/tars_c.c index e32cbfa..fddc36e 100644 --- a/tars_c.c +++ b/tars_c.c @@ -4,14 +4,13 @@ #include "./include/tars_c.h" -//����ֵ���� -const Int32 TARS_SUCCESS = 0; //�ɹ� -const Int32 TARS_ATTR_NOT_FOUND = -1; //���Ҳ���������� -const Int32 TARS_ENCODE_ERROR = -2; //������� -const Int32 TARS_DECODE_ERROR = -3; //������� -const Int32 TARS_RUNTIME_ERROR = -4; //��������ʱ���� -const Int32 TARS_MALLOC_ERROR = -5; //�ڴ�����ʧ�ܴ��� -const Int32 TARS_DECODE_EOPNEXT = -6; //��ѡ�ֶβ����� +const Int32 TARS_SUCCESS = 0; +const Int32 TARS_ATTR_NOT_FOUND = -1; +const Int32 TARS_ENCODE_ERROR = -2; +const Int32 TARS_DECODE_ERROR = -3; +const Int32 TARS_RUNTIME_ERROR = -4; +const Int32 TARS_MALLOC_ERROR = -5; +const Int32 TARS_DECODE_EOPNEXT = -6; Int64 tars__bswap_constant_64(Int64 x) { @@ -21,7 +20,7 @@ Int64 tars__bswap_constant_64(Int64 x) __t__.low = tars__bswap_constant_32(x.low); return __t__; #else - return((((x) & 0xff00000000000000ull) >> 56) \ + return ((((x) & 0xff00000000000000ull) >> 56) \ | (((x) & 0x00ff000000000000ull) >> 40) \ | (((x) & 0x0000ff0000000000ull) >> 24) \ | (((x) & 0x000000ff00000000ull) >> 8) \ @@ -48,6 +47,7 @@ Double tars_ntohd(Double x) Int64 __t__ = tars__bswap_constant_64((*((Int64 *)&x))); return *((Double *) &__t__); } + ///////////////////////////////////////////////////////////////////////////////////// void JString_del(JString **st) @@ -58,18 +58,18 @@ void JString_del(JString **st) *st = NULL; } -void JString_copy(char * dest, const char * src, uint32_t len) +void JString_copy(char *dest, const char *src, uint32_t len) { - char * pe = dest + len; + char *pe = dest + len; if (dest == NULL || src == NULL || (Int32)len < 0) return; for (; dest != pe; ++dest, ++src) *dest = *src; } -Int32 JString_copyChar(JString * s, char * data, uint32_t len) +Int32 JString_copyChar(JString *s, char *data, uint32_t len) { - char * p = TarsMalloc(sizeof(char) * len + 1); + char *p = TarsMalloc(sizeof(char) * len + 1); if (!p) return TARS_MALLOC_ERROR; @@ -83,21 +83,15 @@ Int32 JString_copyChar(JString * s, char * data, uint32_t len) return 0; } -void JString_clear(JString *s) { if (s != NULL) { s->_len = 0; s->_data[0] = 0;}} +void JString_clear(JString *s) { if (s != NULL) { s->_len = 0; s->_data[0] = 0; } } -/** - * ����n���ȵ�JString�ռ� - * @param s [description] - * @param n [description] - * @return [description] - */ Int32 JString_reserve(JString *s, uint32_t n) { if (s == NULL || (Int32)n < 0) return TARS_MALLOC_ERROR; if (s->_buf_len < n + 1) { - char * p = TarsMalloc(n + 1); + char *p = TarsMalloc(n + 1); if (!p) return TARS_MALLOC_ERROR; @@ -110,14 +104,7 @@ Int32 JString_reserve(JString *s, uint32_t n) return 0; } -/** - * Jstring���ڴ濽�� - * @param s [description] - * @param data [description] - * @param len [description] - * @return [description] - */ -Int32 JString_assign(JString *s, const char * data, uint32_t len) +Int32 JString_assign(JString *s, const char *data, uint32_t len) { Int32 ret=0; if (s == NULL || data == NULL || (Int32)len < 0) @@ -136,7 +123,7 @@ Int32 JString_assign(JString *s, const char * data, uint32_t len) return 0; } -Int32 JString_append(JString * s, const char * data, uint32_t len) +Int32 JString_append(JString *s, const char *data, uint32_t len) { if (s == NULL || data == NULL || (Int32)len < 0) return TARS_MALLOC_ERROR; @@ -176,7 +163,8 @@ Int32 JString_insert(JString *s, uint32_t pos, char v) uint32_t JString_size(JString *s) { if (s) return s->_len; return 0;} int JString_empty(JString *s) { if (s) return s->_len == 0; return 0;} uint32_t JString_capacity(JString *s) { if (s) return s->_buf_len; return 0;} -char * JString_data(JString *s) { if (s) return s->_data; return NULL;} +char *JString_data(JString *s) { if (s) return s->_data; return NULL;} + Int32 JString_resize(JString *s, uint32_t n) { if (s == NULL || (Int32)n < 0) @@ -206,7 +194,7 @@ Int32 JString_init(JString *s) return TARS_SUCCESS; } -JString * JString_new() +JString *JString_new() { Int32 ret; JString *s = TarsMalloc(sizeof(JString)); @@ -223,7 +211,7 @@ JString * JString_new() return s; } -void JArray_del(JArray ** arr) +void JArray_del(JArray **arr) { if (arr == NULL || *arr == NULL) return; if ((*arr)->elem_type_name != NULL) TarsFree((*arr)->elem_type_name); @@ -241,7 +229,7 @@ Int32 JArray_reserveList(JArray *arr, uint32_t num) if (arr->list_len < num) { - int * p = TarsMalloc(num); + int *p = TarsMalloc(num); if (!p) return TARS_MALLOC_ERROR; @@ -261,7 +249,7 @@ Int32 JArray_reserveBuff(JArray *arr, uint32_t len) if (arr->buff_len < len) { - char * p = TarsMalloc(len); + char *p = TarsMalloc(len); if (!p) return TARS_MALLOC_ERROR; @@ -273,7 +261,7 @@ Int32 JArray_reserveBuff(JArray *arr, uint32_t len) return 0; } -Int32 JArray_pushBack(JArray *arr, const char * data, uint32_t len) +Int32 JArray_pushBack(JArray *arr, const char *data, uint32_t len) { Int32 ret; if (arr == NULL || data == NULL || (Int32)len < 0) @@ -283,13 +271,13 @@ Int32 JArray_pushBack(JArray *arr, const char * data, uint32_t len) if (arr->list_len <= arr->elem_num * sizeof(int)) { - ret = JArray_reserveList(arr, 2* (arr->list_len + sizeof(int)) ); + ret = JArray_reserveList(arr, 2 * (arr->list_len + sizeof(int)) ); if (ret) return ret; } if (arr->buff_len < arr->buff_used + len) { - ret = JArray_reserveBuff(arr, 2*(arr->buff_len + len)); + ret = JArray_reserveBuff(arr, 2 * (arr->buff_len + len)); if (ret) return ret; } @@ -303,12 +291,12 @@ Int32 JArray_pushBack(JArray *arr, const char * data, uint32_t len) return 0; } -Int32 JArray_pushBackString(JArray *arr, const char * data) +Int32 JArray_pushBackString(JArray *arr, const char *data) { return JArray_pushBack(arr, data, strlen(data)); } -Int32 JArray_get(JArray *arr, unsigned index, char *data, uint32_t * len) +Int32 JArray_get(JArray *arr, unsigned index, char *data, uint32_t *len) { unsigned data_len = 0; if (arr == NULL || (Int32)index < 0 || data == NULL || len == NULL) @@ -384,22 +372,13 @@ void JArray_clear(JArray *arr) void JArray_init(JArray *arr) { -/* - arr->elem_type_name = NULL; - arr->elem_num = 0; - arr->list_len = 0; - arr->list = NULL; - arr->buff_used = 0; - arr->buff_len = 0; - arr->buff = NULL; - */ memset(arr, 0, sizeof(JArray)); } -JArray * JArray_new(const char * type) +JArray *JArray_new(const char *type) { unsigned len = 0; - JArray * arr = TarsMalloc(sizeof(JArray)); + JArray *arr = TarsMalloc(sizeof(JArray)); if (!arr) return NULL; @@ -418,7 +397,7 @@ JArray * JArray_new(const char * type) return arr; } -void JMapWrapper_del(JMapWrapper ** m) +void JMapWrapper_del(JMapWrapper **m) { if (m == NULL || *m == NULL) return; JArray_del(&((*m)->first)); @@ -427,16 +406,8 @@ void JMapWrapper_del(JMapWrapper ** m) TarsFree(*m); *m = NULL; } -/** - * �ֱ��key��val���ַ���ѹ��JString�� - * @param m [description] - * @param first_data [description] - * @param first_len [description] - * @param value_data [description] - * @param value_len [description] - * @return [description] - */ -Int32 JMapWrapper_put(JMapWrapper *m, const char * first_data, unsigned first_len, const char *value_data, unsigned value_len) + +Int32 JMapWrapper_put(JMapWrapper *m, const char *first_data, unsigned first_len, const char *value_data, unsigned value_len) { Int32 ret = 0; ret = JArray_pushBack(m->first, first_data, first_len); @@ -446,7 +417,7 @@ Int32 JMapWrapper_put(JMapWrapper *m, const char * first_data, unsigned first_le return JArray_pushBack(m->second, value_data, value_len); } -int JMapWrapper_find(JMapWrapper *m, const char * first_data, unsigned first_len, char **value_data, unsigned * value_len) +int JMapWrapper_find(JMapWrapper *m, const char *first_data, unsigned first_len, char **value_data, unsigned *value_len) { uint32_t i; @@ -465,12 +436,12 @@ int JMapWrapper_find(JMapWrapper *m, const char * first_data, unsigned first_len } -int JMapWrapper_size(JMapWrapper * m) +int JMapWrapper_size(JMapWrapper *m) { return JArray_size(m->first); } -int JMapWrapper_getByIndex(JMapWrapper *m, unsigned index, char *first, uint32_t * first_len, char* second, uint32_t * second_len) +int JMapWrapper_getByIndex(JMapWrapper *m, unsigned index, char *first, uint32_t *first_len, char *second, uint32_t *second_len) { JArray_get(m->first, index, first, first_len); JArray_get(m->second, index, second, second_len); @@ -492,9 +463,9 @@ void JMapWrapper_init(JMapWrapper *m) { } -JMapWrapper * JMapWrapper_new(const char * first_type, const char * second_type) +JMapWrapper *JMapWrapper_new(const char *first_type, const char *second_type) { - JMapWrapper * m = TarsMalloc(sizeof(JMapWrapper)); + JMapWrapper *m = TarsMalloc(sizeof(JMapWrapper)); if (!m) return NULL; @@ -542,7 +513,7 @@ void helper_setType(helper *h, unsigned int t) h->type_tag |= (t & 0x0F); } -void DataHead_del(DataHead ** head) +void DataHead_del(DataHead **head) { if (head == NULL || *head == NULL) return; TarsFree(*head); @@ -550,13 +521,10 @@ void DataHead_del(DataHead ** head) } -uint8_t DataHead_getTag(DataHead * head) { return head->_tag;} -//void DataHead_setTag(DataHead * head, uint8_t t) { head->_tag = t; } -uint8_t DataHead_getType(DataHead * head) { return head->_type;} -//void DataHead_setType(DataHead * head, uint8_t t) { head->_type = t; } +uint8_t DataHead_getTag(DataHead *head) { return head->_tag; } +uint8_t DataHead_getType(DataHead *head) { return head->_type; } -/// ��ȡͷ��Ϣ������ǰ������ƫ���� -Int32 DataHead_peekFrom(DataHead * head, TarsInputStream* is, uint32_t *n) +Int32 DataHead_peekFrom(DataHead *head, TarsInputStream *is, uint32_t *n) { Int32 ret = 0; helper h; @@ -579,8 +547,7 @@ Int32 DataHead_peekFrom(DataHead * head, TarsInputStream* is, uint32_t *n) return TARS_SUCCESS; } -/// ��ȡ����ͷ��Ϣ -Int32 DataHead_readFrom(DataHead * head, TarsInputStream* is) +Int32 DataHead_readFrom(DataHead *head, TarsInputStream *is) { Int32 ret = 0; uint32_t n; @@ -593,8 +560,7 @@ Int32 DataHead_readFrom(DataHead * head, TarsInputStream* is) } -/// д������ͷ��Ϣ -Int32 DataHead_writeTo(DataHead * head, TarsOutputStream* os) +Int32 DataHead_writeTo(DataHead *head, TarsOutputStream *os) { Int32 ret; helper h; @@ -613,23 +579,23 @@ Int32 DataHead_writeTo(DataHead * head, TarsOutputStream* os) } return TARS_SUCCESS; } -Int32 DataHead_setAndWriteTo(DataHead * head, unsigned int type, unsigned int tag,TarsOutputStream* os) + +Int32 DataHead_setAndWriteTo(DataHead *head, unsigned int type, unsigned int tag, TarsOutputStream *os) { head->_tag = tag; head->_type = type; return DataHead_writeTo(head, os); } - -void DataHead_init(DataHead * head) +void DataHead_init(DataHead *head) { head->_type = 0; head->_tag = 0; } -DataHead * DataHead_new() +DataHead *DataHead_new() { - DataHead * head = TarsMalloc(sizeof(DataHead)); + DataHead *head = TarsMalloc(sizeof(DataHead)); if (!head) { return NULL; @@ -637,8 +603,10 @@ DataHead * DataHead_new() DataHead_init(head); return head; } + //////////////////////////////////////////////////////////////////////////// -static void TarsStream_del(TarsStream ** js) + +static void TarsStream_del(TarsStream **js) { if (js == NULL || *js == NULL) return; JString_del(& (*js)->_buf); @@ -648,7 +616,7 @@ static void TarsStream_del(TarsStream ** js) *js = NULL; } -static Int32 TarsStream_init(TarsStream* js) +static Int32 TarsStream_init(TarsStream *js) { js->_buf = JString_new(); if (!js->_buf) @@ -667,10 +635,10 @@ static Int32 TarsStream_init(TarsStream* js) return TARS_SUCCESS; } -static TarsStream * TarsStream_new() +static TarsStream *TarsStream_new() { Int32 ret; - TarsStream * js = TarsMalloc(sizeof(TarsStream)); + TarsStream *js = TarsMalloc(sizeof(TarsStream)); if (!js) { return NULL; @@ -688,17 +656,17 @@ static TarsStream * TarsStream_new() //////////////////////////////////////////////////////////////////////////// -void TarsInputStream_del(TarsInputStream ** is) +void TarsInputStream_del(TarsInputStream **is) { TarsStream_del(is); } -void TarsInputStream_reset(TarsInputStream * is) +void TarsInputStream_reset(TarsInputStream *is) { is->_cur = 0; } -Int32 TarsInputStream_readBuf(TarsInputStream * is, void * buf, uint32_t len) +Int32 TarsInputStream_readBuf(TarsInputStream *is, void *buf, uint32_t len) { Int32 ret; @@ -709,7 +677,7 @@ Int32 TarsInputStream_readBuf(TarsInputStream * is, void * buf, uint32_t len) return TARS_SUCCESS; } -Int32 TarsInputStream_peekBuf(TarsInputStream * is, void * buf, uint32_t len, uint32_t offset) +Int32 TarsInputStream_peekBuf(TarsInputStream *is, void *buf, uint32_t len, uint32_t offset) { if (is->_cur + offset + len > JString_size(is->_buf)) { @@ -720,17 +688,13 @@ Int32 TarsInputStream_peekBuf(TarsInputStream * is, void * buf, uint32_t len, ui return TARS_SUCCESS; } - - -/// ����len���ֽ� -Int32 TarsInputStream_skip(TarsInputStream * is, uint32_t len) +Int32 TarsInputStream_skip(TarsInputStream *is, uint32_t len) { is->_cur += len; return TARS_SUCCESS; } -/// ���û��� -Int32 TarsInputStream_setBuffer(TarsInputStream * is, const char * buf, uint32_t len) +Int32 TarsInputStream_setBuffer(TarsInputStream *is, const char *buf, uint32_t len) { Int32 ret = JString_assign(is->_buf, buf, len); if (ret) @@ -740,9 +704,7 @@ Int32 TarsInputStream_setBuffer(TarsInputStream * is, const char * buf, uint32_t return TARS_SUCCESS; } - -/// ����ָ����ǩ��Ԫ��ǰ -Int32 TarsInputStream_skipToTag(TarsInputStream * is, uint8_t tag) +Int32 TarsInputStream_skipToTag(TarsInputStream *is, uint8_t tag) { Int32 ret; while (1) @@ -770,11 +732,11 @@ Int32 TarsInputStream_skipToTag(TarsInputStream * is, uint8_t tag) return TARS_DECODE_ERROR; } -/// ������ǰ�ṹ�Ľ��� -Int32 TarsInputStream_skipToStructEnd(TarsInputStream * is) +Int32 TarsInputStream_skipToStructEnd(TarsInputStream *is) { Int32 ret; - Int32 level = 1; //�жϽṹǶ�׵���� + Int32 level = 1; + do { ret = DataHead_readFrom(is->_h, is); @@ -794,8 +756,7 @@ Int32 TarsInputStream_skipToStructEnd(TarsInputStream * is) return TARS_SUCCESS; } -/// ����һ���ֶ� -Int32 TarsInputStream_skipField(TarsInputStream * is) +Int32 TarsInputStream_skipField(TarsInputStream *is) { Int32 ret; @@ -808,8 +769,7 @@ Int32 TarsInputStream_skipField(TarsInputStream * is) return TARS_SUCCESS; } -/// ����һ���ֶΣ�������ͷ��Ϣ -Int32 TarsInputStream_skipFieldByType(TarsInputStream * is, uint8_t type) +Int32 TarsInputStream_skipFieldByType(TarsInputStream *is, uint8_t type) { Int32 ret; uint32_t len = 0; @@ -916,7 +876,7 @@ Int32 TarsInputStream_skipFieldByType(TarsInputStream * is, uint8_t type) return TARS_SUCCESS; } -Int32 TarsInputStream_checkValid(TarsInputStream * is,uint8_t tag, Bool isRequire) +Int32 TarsInputStream_checkValid(TarsInputStream *is,uint8_t tag, Bool isRequire) { if (!TarsInputStream_skipToTag(is, tag)) { @@ -931,13 +891,13 @@ Int32 TarsInputStream_checkValid(TarsInputStream * is,uint8_t tag, Bool isRequir return TARS_DECODE_EOPNEXT; } -Int32 TarsInputStream_readByChar(TarsInputStream * is, Char * n) +Int32 TarsInputStream_readByChar(TarsInputStream *is, Char *n) { Int32 ret; ret = TarsInputStream_readBuf(is, n, sizeof(*n)); return ret; } -Int32 TarsInputStream_readByShort(TarsInputStream * is, Short * n) +Int32 TarsInputStream_readByShort(TarsInputStream *is, Short *n) { Int32 ret; ret = TarsInputStream_readBuf(is, n, sizeof(*n)); @@ -945,14 +905,14 @@ Int32 TarsInputStream_readByShort(TarsInputStream * is, Short * n) } -Int32 TarsInputStream_readByInt32(TarsInputStream * is, Int32 * n) +Int32 TarsInputStream_readByInt32(TarsInputStream *is, Int32 *n) { Int32 ret; ret = TarsInputStream_readBuf(is, n, sizeof(*n)); return ret; } -Int32 TarsInputStream_readBool(TarsInputStream * is, Bool* b, uint8_t tag, Bool isRequire) +Int32 TarsInputStream_readBool(TarsInputStream *is, Bool *b, uint8_t tag, Bool isRequire) { Int32 ret; Char c = *b; @@ -962,7 +922,7 @@ Int32 TarsInputStream_readBool(TarsInputStream * is, Bool* b, uint8_t tag, Bool return ret; } -Int32 TarsInputStream_readChar(TarsInputStream * is, Char* c, uint8_t tag, Bool isRequire) +Int32 TarsInputStream_readChar(TarsInputStream *is, Char *c, uint8_t tag, Bool isRequire) { Int32 ret = TarsInputStream_checkValid(is,tag,isRequire); if (TARS_DECODE_EOPNEXT == ret) return TARS_DECODE_EOPNEXT; @@ -987,7 +947,7 @@ Int32 TarsInputStream_readChar(TarsInputStream * is, Char* c, uint8_t tag, Bool return TARS_SUCCESS; } -Int32 TarsInputStream_readUInt8 (TarsInputStream * is, UInt8 * n, uint8_t tag, Bool isRequire) +Int32 TarsInputStream_readUInt8 (TarsInputStream *is, UInt8 *n, uint8_t tag, Bool isRequire) { Short ns = 0; Int32 ret = TarsInputStream_readShort(is, &ns, tag, isRequire); @@ -997,7 +957,7 @@ Int32 TarsInputStream_readUInt8 (TarsInputStream * is, UInt8 * n, uint8_t tag, B return TARS_SUCCESS; } -Int32 TarsInputStream_readShort(TarsInputStream * is, Short* n, uint8_t tag, Bool isRequire) +Int32 TarsInputStream_readShort(TarsInputStream *is, Short *n, uint8_t tag, Bool isRequire) { Char c; Int32 ret = TarsInputStream_checkValid(is,tag,isRequire); @@ -1029,7 +989,7 @@ Int32 TarsInputStream_readShort(TarsInputStream * is, Short* n, uint8_t tag, Boo return TARS_SUCCESS; } -Int32 TarsInputStream_readUInt16 (TarsInputStream *is, UInt16 *n, uint8_t tag, Bool isRequire) +Int32 TarsInputStream_readUInt16(TarsInputStream *is, UInt16 *n, uint8_t tag, Bool isRequire) { Int32 ns = 0; Int32 ret = TarsInputStream_readInt32(is, &ns, tag, isRequire); @@ -1039,7 +999,7 @@ Int32 TarsInputStream_readUInt16 (TarsInputStream *is, UInt16 *n, uint8_t tag, B return TARS_SUCCESS; } -Int32 TarsInputStream_readInt32(TarsInputStream * is, Int32* n, uint8_t tag, Bool isRequire) +Int32 TarsInputStream_readInt32(TarsInputStream *is, Int32 *n, uint8_t tag, Bool isRequire) { Char c; Short sh; @@ -1078,7 +1038,7 @@ Int32 TarsInputStream_readInt32(TarsInputStream * is, Int32* n, uint8_t tag, Boo } -Int32 TarsInputStream_readUInt32 (TarsInputStream *is, UInt32 * n, uint8_t tag, Bool isRequire) +Int32 TarsInputStream_readUInt32(TarsInputStream *is, UInt32 *n, uint8_t tag, Bool isRequire) { Int64 ns = 0; Int32 ret = TarsInputStream_readInt64(is, &ns, tag, isRequire); @@ -1088,7 +1048,7 @@ Int32 TarsInputStream_readUInt32 (TarsInputStream *is, UInt32 * n, uint8_t tag, return TARS_SUCCESS; } -Int32 TarsInputStream_readInt64(TarsInputStream * is, Int64* n, uint8_t tag, Bool isRequire) +Int32 TarsInputStream_readInt64(TarsInputStream *is, Int64 *n, uint8_t tag, Bool isRequire) { Char c; @@ -1166,17 +1126,8 @@ Int32 TarsInputStream_readInt64(TarsInputStream * is, Int64* n, uint8_t tag, Boo return TARS_SUCCESS; } -/* -Int32 TarsInputStream_readInt64_HL(TarsInputStream * is, Int32* high, Int32* low, uint8_t tag, Bool isRequire) -{ - Int64 n = 0; - TarsInputStream_readInt64(is, &n, tag,isRequire ); - *low = (Int32)n; - *high = (Int32)(n>>32); -} -*/ -Int32 TarsInputStream_readFloat(TarsInputStream * is, Float* n, uint8_t tag, Bool isRequire) +Int32 TarsInputStream_readFloat(TarsInputStream *is, Float *n, uint8_t tag, Bool isRequire) { Int32 ret = TarsInputStream_checkValid(is,tag,isRequire); if (TARS_DECODE_EOPNEXT == ret) return TARS_DECODE_EOPNEXT; @@ -1202,7 +1153,7 @@ Int32 TarsInputStream_readFloat(TarsInputStream * is, Float* n, uint8_t tag, Boo } -Int32 TarsInputStream_readDouble(TarsInputStream * is, Double* n, uint8_t tag, Bool isRequire) +Int32 TarsInputStream_readDouble(TarsInputStream *is, Double *n, uint8_t tag, Bool isRequire) { Int32 ret = TarsInputStream_checkValid(is,tag,isRequire); if (TARS_DECODE_EOPNEXT == ret) return TARS_DECODE_EOPNEXT; @@ -1236,91 +1187,7 @@ Int32 TarsInputStream_readDouble(TarsInputStream * is, Double* n, uint8_t tag, B return TARS_SUCCESS; } -Int32 Tars_readStringLen(char* src) -{ - helper h; - memcpy(&h, src, sizeof(h)); - - uint8_t type = helper_getType(&h); - int offset = sizeof(h); - - switch (type) - { - case eString1: - { - uint8_t n; - char ss[256]; - memcpy(&n, src + offset, sizeof(n)); - - return n; - } - case eString4: - { - uint32_t len; - memcpy(&len, src + offset, sizeof(len)); - - len = tars_ntohl(len); - - return len; - } - default: - { - break; - } - } - - return 0; -} - -Int32 Tars_readString(char* src, char ** output) -{ - helper h; - memcpy(&h, src, sizeof(h)); - - uint8_t type = helper_getType(&h); - int offset = sizeof(h); - - switch (type) - { - case eString1: - { - uint8_t n; - uint32_t len; - memcpy(&n, src + offset, sizeof(n)); - offset += sizeof(n); - - len = n; - memcpy(output, src + offset, len); - - break; - } - case eString4: - { - uint32_t len; - char *ss; - - memcpy(&len, src + offset, sizeof(len)); - offset += sizeof(len); - - len = tars_ntohl(len); - if (len > TARS_MAX_STRING_LENGTH) - { - return TARS_DECODE_ERROR; - } - - memcpy(output, src + offset, len); - break; - } - default: - { - break; - } - } - - return TARS_SUCCESS; -} - -Int32 TarsInputStream_readString(TarsInputStream * is, JString* s, uint8_t tag, Bool isRequire) +Int32 TarsInputStream_readString(TarsInputStream *is, JString *s, uint8_t tag, Bool isRequire) { Int32 ret = TarsInputStream_checkValid(is,tag,isRequire); if (TARS_DECODE_EOPNEXT == ret) return TARS_DECODE_EOPNEXT; @@ -1381,7 +1248,7 @@ Int32 TarsInputStream_readString(TarsInputStream * is, JString* s, uint8_t tag, return TARS_SUCCESS; } -Int32 TarsInputStream_readMap(TarsInputStream * is, JMapWrapper* m, uint8_t tag, Bool isRequire) +Int32 TarsInputStream_readMap(TarsInputStream *is, JMapWrapper *m, uint8_t tag, Bool isRequire) { Int32 ret = TarsInputStream_checkValid(is,tag,isRequire); if (TARS_DECODE_EOPNEXT == ret) return TARS_DECODE_EOPNEXT; @@ -1426,7 +1293,7 @@ Int32 TarsInputStream_readMap(TarsInputStream * is, JMapWrapper* m, uint8_t tag, return TARS_SUCCESS; } -Int32 TarsInputStream_readVectorChar(TarsInputStream * is, JString *v, uint8_t tag, Bool isRequire) +Int32 TarsInputStream_readVectorChar(TarsInputStream *is, JString *v, uint8_t tag, Bool isRequire) { Int32 ret = TarsInputStream_checkValid(is,tag,isRequire); if (TARS_DECODE_EOPNEXT == ret) return TARS_DECODE_EOPNEXT; @@ -1487,7 +1354,7 @@ Int32 TarsInputStream_readVectorChar(TarsInputStream * is, JString *v, uint8_t t return TARS_SUCCESS; } -Int32 TarsInputStream_readVector(TarsInputStream * is, JArray* v, uint8_t tag, Bool isRequire) +Int32 TarsInputStream_readVector(TarsInputStream *is, JArray *v, uint8_t tag, Bool isRequire) { Int32 ret = TarsInputStream_checkValid(is,tag,isRequire); if (TARS_DECODE_EOPNEXT == ret) return TARS_DECODE_EOPNEXT; @@ -1529,12 +1396,10 @@ Int32 TarsInputStream_readVector(TarsInputStream * is, JArray* v, uint8_t tag, B return TARS_SUCCESS; } - -/// ��ȡ�ṹ -Int32 TarsInputStream_readStruct(TarsInputStream * is, void * st, uint8_t tag, Bool isRequire) +Int32 TarsInputStream_readStruct(TarsInputStream *is, void *st, uint8_t tag, Bool isRequire) { Int32 ret=0; - const JStructBase* jst = st; + const JStructBase *jst = st; JString *s = JString_new(); TarsInputStream *i = TarsInputStream_new(); do @@ -1554,7 +1419,7 @@ Int32 TarsInputStream_readStruct(TarsInputStream * is, void * st, uint8_t tag, B return ret; } -Int32 TarsInputStream_readStructString(TarsInputStream * is, JString * st, uint8_t tag, Bool isRequire) +Int32 TarsInputStream_readStructString(TarsInputStream *is, JString *st, uint8_t tag, Bool isRequire) { uint32_t pos1,pos2; Int32 ret = TarsInputStream_checkValid(is,tag,isRequire); @@ -1578,61 +1443,56 @@ Int32 TarsInputStream_readStructString(TarsInputStream * is, JString * st, uint8 } -Int32 TarsInputStream_init(TarsInputStream* is) +Int32 TarsInputStream_init(TarsInputStream *is) { return TarsStream_init(is); } -TarsInputStream * TarsInputStream_new() +TarsInputStream *TarsInputStream_new() { return TarsStream_new(); } //////////////////////////////////////////////// -void TarsOutputStream_del(TarsOutputStream ** os) + +void TarsOutputStream_del(TarsOutputStream **os) { TarsStream_del(os); } - -void TarsOutputStream_reset(TarsOutputStream * os) +void TarsOutputStream_reset(TarsOutputStream *os) { JString_clear(os->_buf); } -Int32 TarsOutputStream_writeBuf(TarsOutputStream * os, const void * buf, uint32_t len) +Int32 TarsOutputStream_writeBuf(TarsOutputStream *os, const void *buf, uint32_t len) { - char * p = (char *) buf; + char *p = (char *) buf; return JString_append(os->_buf, p, len); } -char * TarsOutputStream_getBuffer(TarsOutputStream * os) { return JString_data(os->_buf);} -uint32_t TarsOutputStream_getLength(TarsOutputStream * os) { return JString_size(os->_buf);} +char *TarsOutputStream_getBuffer(TarsOutputStream *os) { return JString_data(os->_buf);} +uint32_t TarsOutputStream_getLength(TarsOutputStream *os) { return JString_size(os->_buf);} -Int32 TarsOutputStream_writeBool(TarsOutputStream * os, Bool b, uint8_t tag) +Int32 TarsOutputStream_writeBool(TarsOutputStream *os, Bool b, uint8_t tag) { TarsOutputStream_writeChar(os, (Char) b, tag); return TARS_SUCCESS; } -Int32 TarsOutputStream_writeChar(TarsOutputStream * os, Char n, uint8_t tag) +Int32 TarsOutputStream_writeChar(TarsOutputStream *os, Char n, uint8_t tag) { Int32 ret; - //DataHead_setTag(os->_h, tag); if (n == 0) { - //DataHead_setType(os->_h, eZeroTag); - //ret = DataHead_writeTo(os->_h, os); ret = DataHead_setAndWriteTo(os->_h, eZeroTag, tag, os); if (TARS_SUCCESS != ret) return ret; } else { - //DataHead_setType(os->_h, eChar); - //ret = DataHead_writeTo(os->_h, os); ret = DataHead_setAndWriteTo(os->_h, eChar, tag, os); if (TARS_SUCCESS != ret) return ret; @@ -1642,12 +1502,12 @@ Int32 TarsOutputStream_writeChar(TarsOutputStream * os, Char n, uint8_t tag) return TARS_SUCCESS; } -Int32 TarsOutputStream_writeUInt8(TarsOutputStream * os, UInt8 n, uint8_t tag) +Int32 TarsOutputStream_writeUInt8(TarsOutputStream *os, UInt8 n, uint8_t tag) { return TarsOutputStream_writeShort(os, (Short)n, tag); } -Int32 TarsOutputStream_writeShort(TarsOutputStream * os, Short n, uint8_t tag) +Int32 TarsOutputStream_writeShort(TarsOutputStream *os, Short n, uint8_t tag) { Int32 ret; @@ -1667,12 +1527,12 @@ Int32 TarsOutputStream_writeShort(TarsOutputStream * os, Short n, uint8_t tag) return TARS_SUCCESS; } -Int32 TarsOutputStream_writeUInt16(TarsOutputStream * os, UInt16 n, uint8_t tag) +Int32 TarsOutputStream_writeUInt16(TarsOutputStream *os, UInt16 n, uint8_t tag) { return TarsOutputStream_writeInt32(os, (Int32)n, tag); } -Int32 TarsOutputStream_writeInt32(TarsOutputStream * os, Int32 n, uint8_t tag) +Int32 TarsOutputStream_writeInt32(TarsOutputStream *os, Int32 n, uint8_t tag) { Int32 ret; @@ -1692,12 +1552,12 @@ Int32 TarsOutputStream_writeInt32(TarsOutputStream * os, Int32 n, uint8_t tag) return TARS_SUCCESS; } -Int32 TarsOutputStream_writeUInt32(TarsOutputStream * os, UInt32 n, uint8_t tag) +Int32 TarsOutputStream_writeUInt32(TarsOutputStream *os, UInt32 n, uint8_t tag) { return TarsOutputStream_writeInt64(os, (Int64)n, tag); } -Int32 TarsOutputStream_writeInt64(TarsOutputStream * os, Int64 n, uint8_t tag) +Int32 TarsOutputStream_writeInt64(TarsOutputStream *os, Int64 n, uint8_t tag) { Int32 ret; @@ -1726,7 +1586,7 @@ Int32 TarsOutputStream_writeInt64(TarsOutputStream * os, Int64 n, uint8_t tag) -Int32 TarsOutputStream_writeFloat(TarsOutputStream * os, Float n, uint8_t tag) +Int32 TarsOutputStream_writeFloat(TarsOutputStream *os, Float n, uint8_t tag) { Int32 ret; @@ -1737,37 +1597,30 @@ Int32 TarsOutputStream_writeFloat(TarsOutputStream * os, Float n, uint8_t tag) return TarsOutputStream_writeBuf(os, &n, sizeof(n)); } -Int32 TarsOutputStream_writeDouble(TarsOutputStream * os, Double n, uint8_t tag) +Int32 TarsOutputStream_writeDouble(TarsOutputStream *os, Double n, uint8_t tag) { Int32 ret=0; - //DataHead_setType(os->_h, eDouble); - //DataHead_setTag(os->_h, tag); - //ret = DataHead_writeTo(os->_h, os); ret = DataHead_setAndWriteTo(os->_h, eDouble, tag, os); if (TARS_SUCCESS != ret) return ret; n = tars_htond(n); return TarsOutputStream_writeBuf(os, &n, sizeof(n)); } -Int32 TarsOutputStream_writeString(TarsOutputStream * os, JString* s, uint8_t tag) + +Int32 TarsOutputStream_writeString(TarsOutputStream *os, JString *s, uint8_t tag) { return TarsOutputStream_writeStringBuffer(os, JString_data(s), JString_size(s), tag); } -Int32 TarsOutputStream_writeStringBuffer(TarsOutputStream * os, const char* buff, uint32_t len, uint8_t tag) +Int32 TarsOutputStream_writeStringBuffer(TarsOutputStream *os, const char *buff, uint32_t len, uint8_t tag) { Int32 ret=0; - //DataHead_setType(os->_h, eString1); - //DataHead_setTag(os->_h, tag); - if (len > 255) { uint32_t n; - //DataHead_setType(os->_h, eString4); - //ret = DataHead_writeTo(os->_h, os); ret = DataHead_setAndWriteTo(os->_h, eString4, tag, os); if (TARS_SUCCESS != ret) return ret; @@ -1801,23 +1654,12 @@ Int32 TarsOutputStream_writeStringBuffer(TarsOutputStream * os, const char* buff return TARS_SUCCESS; } -/** - * �����һ��map�Ľṹ - * @param os [description] - * @param m [description] - * @param tag [description] - * @return [description] - */ -Int32 TarsOutputStream_writeMap(TarsOutputStream * os, JMapWrapper* m, uint8_t tag) +Int32 TarsOutputStream_writeMap(TarsOutputStream *os, JMapWrapper *m, uint8_t tag) { uint32_t i; Int32 n; Int32 ret; - //DataHead_setType(os->_h, eMap); - //DataHead_setTag(os->_h, tag); - - // ret = DataHead_writeTo(os->_h, os); ret = DataHead_setAndWriteTo(os->_h, eMap, tag, os); if (TARS_SUCCESS != ret) return ret; @@ -1837,15 +1679,10 @@ Int32 TarsOutputStream_writeMap(TarsOutputStream * os, JMapWrapper* m, uint8_t t return TARS_SUCCESS; } - -Int32 TarsOutputStream_writeVector(TarsOutputStream * os, JArray* v, uint8_t tag) +Int32 TarsOutputStream_writeVector(TarsOutputStream *os, JArray *v, uint8_t tag) { Int32 i, n, ret; - //DataHead_setType(os->_h, eList); - // DataHead_setTag(os->_h, tag); - - //ret = DataHead_writeTo(os->_h, os); ret = DataHead_setAndWriteTo(os->_h, eList, tag, os); if (TARS_SUCCESS != ret) return ret; @@ -1862,30 +1699,22 @@ Int32 TarsOutputStream_writeVector(TarsOutputStream * os, JArray* v, uint8_t tag return TARS_SUCCESS; } -Int32 TarsOutputStream_writeVectorChar(TarsOutputStream * os, JString *v, uint8_t tag) +Int32 TarsOutputStream_writeVectorChar(TarsOutputStream *os, JString *v, uint8_t tag) { return TarsOutputStream_writeVectorCharBuffer(os, JString_data(v), JString_size(v), tag); } -Int32 TarsOutputStream_writeVectorCharBuffer(TarsOutputStream * os, const char* buff, uint32_t len, uint8_t tag) +Int32 TarsOutputStream_writeVectorCharBuffer(TarsOutputStream *os, const char *buff, uint32_t len, uint8_t tag) { Int32 ret; DataHead *hh; - //DataHead_setType(os->_h, eSimpleList); - // DataHead_setTag(os->_h, tag); - - //ret = DataHead_writeTo(os->_h, os); ret = DataHead_setAndWriteTo(os->_h, eSimpleList, tag, os); if (TARS_SUCCESS != ret) return ret; hh = DataHead_new(); if (!hh) return TARS_MALLOC_ERROR; - //DataHead_setType(hh, eChar); - //DataHead_setTag(hh, 0); - - //ret = DataHead_writeTo(hh, os); ret = DataHead_setAndWriteTo(hh, eChar, 0, os); DataHead_del(&hh); @@ -1897,17 +1726,10 @@ Int32 TarsOutputStream_writeVectorCharBuffer(TarsOutputStream * os, const char* return TarsOutputStream_writeBuf(os, buff, len); } -/** - * �������д��һ���ṹ�壬�㵱ȻҲ������buffer��д��һ���ṹ�� - * @param os [description] - * @param st [description] - * @param tag [description] - * @return [description] - */ -Int32 TarsOutputStream_writeStruct(TarsOutputStream * os,const void * st, uint8_t tag) +Int32 TarsOutputStream_writeStruct(TarsOutputStream *os,const void *st, uint8_t tag) { Int32 ret=0; - const JStructBase* jst = st; + const JStructBase *jst = st; TarsOutputStream *o = TarsOutputStream_new(); do { @@ -1923,43 +1745,31 @@ Int32 TarsOutputStream_writeStruct(TarsOutputStream * os,const void * st, uint8 return ret; } -Int32 TarsOutputStream_writeStructString(TarsOutputStream * os, JString * v, uint8_t tag) +Int32 TarsOutputStream_writeStructString(TarsOutputStream *os, JString *v, uint8_t tag) { return TarsOutputStream_writeStructBuffer(os, JString_data(v), JString_size(v), tag); } - -Int32 TarsOutputStream_writeStructBuffer(TarsOutputStream * os, const char* buff, uint32_t len, uint8_t tag) +Int32 TarsOutputStream_writeStructBuffer(TarsOutputStream *os, const char *buff, uint32_t len, uint8_t tag) { Int32 ret; - //DataHead_setType(os->_h, eStructBegin); - //DataHead_setTag(os->_h, tag); - //ret = DataHead_writeTo(os->_h, os); ret = DataHead_setAndWriteTo(os->_h, eStructBegin, tag, os); if (TARS_SUCCESS != ret) return ret; ret = TarsOutputStream_writeBuf(os, buff, len); if (TARS_SUCCESS != ret) return ret; - //DataHead_setType(os->_h, eStructEnd); - //DataHead_setTag(os->_h, 0); - - //ret = DataHead_writeTo(os->_h, os); ret = DataHead_setAndWriteTo(os->_h, eStructEnd, 0, os); return ret; } - -Int32 TarsOutputStream_init(TarsOutputStream * os) +Int32 TarsOutputStream_init(TarsOutputStream *os) { return TarsStream_init(os); } - -TarsOutputStream * TarsOutputStream_new() +TarsOutputStream *TarsOutputStream_new() { return TarsStream_new(); } - - diff --git a/testcases/test.php b/testcases/TestTars.php similarity index 94% rename from testcases/test.php rename to testcases/TestTars.php index 803102e..12537d9 100644 --- a/testcases/test.php +++ b/testcases/TestTars.php @@ -1,17 +1,11 @@ contexts, $this->statuses, $encodeBufs); $decodeRet = \TUPAPI::decode($requestBuf); - if ($decodeRet['status'] !== 0) { - } + + $this->assertTrue($decodeRet['status'] === 0); + $respBuf = $decodeRet['sBuffer']; $out = \TUPAPI::getBool('bool', $respBuf); @@ -58,8 +52,9 @@ public function testDefaultBool() $this->contexts, $this->statuses, $encodeBufs); $decodeRet = \TUPAPI::decode($requestBuf); - if ($decodeRet['status'] !== 0) { - } + + $this->assertTrue($decodeRet['status'] === 0); + $respBuf = $decodeRet['sBuffer']; $out = \TUPAPI::getBool('bool1', $respBuf); @@ -70,7 +65,6 @@ public function testDefaultBool() public function testChar() { $char = 1; - $buf = \TUPAPI::putChar('char', $char); $encodeBufs['char'] = $buf; @@ -80,8 +74,9 @@ public function testChar() $this->contexts, $this->statuses, $encodeBufs); $decodeRet = \TUPAPI::decode($requestBuf); - if ($decodeRet['status'] !== 0) { - } + + $this->assertTrue($decodeRet['status'] === 0); + $respBuf = $decodeRet['sBuffer']; $out = \TUPAPI::getChar('char', $respBuf); @@ -124,8 +119,9 @@ public function testCharBool1() $this->contexts, $this->statuses, $encodeBufs); $decodeRet = \TUPAPI::decode($requestBuf); - if ($decodeRet['status'] !== 0) { - } + + $this->assertTrue($decodeRet['status'] === 0); + $respBuf = $decodeRet['sBuffer']; $out = \TUPAPI::getChar('char', $respBuf); @@ -146,8 +142,9 @@ public function testCharBool2() $this->contexts, $this->statuses, $encodeBufs); $decodeRet = \TUPAPI::decode($requestBuf); - if ($decodeRet['status'] !== 0) { - } + + $this->assertTrue($decodeRet['status'] === 0); + $respBuf = $decodeRet['sBuffer']; $out = \TUPAPI::getChar('char', $respBuf); @@ -168,8 +165,9 @@ public function testCharNull() $this->contexts, $this->statuses, $encodeBufs); $decodeRet = \TUPAPI::decode($requestBuf); - if ($decodeRet['status'] !== 0) { - } + + $this->assertTrue($decodeRet['status'] === 0); + $respBuf = $decodeRet['sBuffer']; $out = \TUPAPI::getChar('char', $respBuf); @@ -190,8 +188,9 @@ public function testCharDouble() $this->contexts, $this->statuses, $encodeBufs); $decodeRet = \TUPAPI::decode($requestBuf); - if ($decodeRet['status'] !== 0) { - } + + $this->assertTrue($decodeRet['status'] === 0); + $respBuf = $decodeRet['sBuffer']; $out = \TUPAPI::getChar('char', $respBuf); @@ -212,8 +211,9 @@ public function testDefaultChar() $this->contexts, $this->statuses, $encodeBufs); $decodeRet = \TUPAPI::decode($requestBuf); - if ($decodeRet['status'] !== 0) { - } + + $this->assertTrue($decodeRet['status'] === 0); + $respBuf = $decodeRet['sBuffer']; $out = \TUPAPI::getChar('char', $respBuf); @@ -234,8 +234,9 @@ public function testUInt8() $this->contexts, $this->statuses, $encodeBufs); $decodeRet = \TUPAPI::decode($requestBuf); - if ($decodeRet['status'] !== 0) { - } + + $this->assertTrue($decodeRet['status'] === 0); + $respBuf = $decodeRet['sBuffer']; $out = \TUPAPI::getUInt8('uint8', $respBuf); @@ -256,8 +257,9 @@ public function testDefaultUInt8() $this->contexts, $this->statuses, $encodeBufs); $decodeRet = \TUPAPI::decode($requestBuf); - if ($decodeRet['status'] !== 0) { - } + + $this->assertTrue($decodeRet['status'] === 0); + $respBuf = $decodeRet['sBuffer']; $out = \TUPAPI::getUInt8('uint811', $respBuf); @@ -278,8 +280,9 @@ public function testShort() $this->contexts, $this->statuses, $encodeBufs); $decodeRet = \TUPAPI::decode($requestBuf); - if ($decodeRet['status'] !== 0) { - } + + $this->assertTrue($decodeRet['status'] === 0); + $respBuf = $decodeRet['sBuffer']; $out = \TUPAPI::getShort('short', $respBuf); @@ -300,8 +303,9 @@ public function testDefaultShort() $this->contexts, $this->statuses, $encodeBufs); $decodeRet = \TUPAPI::decode($requestBuf); - if ($decodeRet['status'] !== 0) { - } + + $this->assertTrue($decodeRet['status'] === 0); + $respBuf = $decodeRet['sBuffer']; $out = \TUPAPI::getShort('short1', $respBuf); @@ -322,8 +326,9 @@ public function testUInt16() $this->contexts, $this->statuses, $encodeBufs); $decodeRet = \TUPAPI::decode($requestBuf); - if ($decodeRet['status'] !== 0) { - } + + $this->assertTrue($decodeRet['status'] === 0); + $respBuf = $decodeRet['sBuffer']; $out = \TUPAPI::getUInt16('uint16', $respBuf); @@ -344,8 +349,9 @@ public function testDefaultUInt16() $this->contexts, $this->statuses, $encodeBufs); $decodeRet = \TUPAPI::decode($requestBuf); - if ($decodeRet['status'] !== 0) { - } + + $this->assertTrue($decodeRet['status'] === 0); + $respBuf = $decodeRet['sBuffer']; $out = \TUPAPI::getUInt16('uint1611', $respBuf); @@ -366,8 +372,9 @@ public function testInt32() $this->contexts, $this->statuses, $encodeBufs); $decodeRet = \TUPAPI::decode($requestBuf); - if ($decodeRet['status'] !== 0) { - } + + $this->assertTrue($decodeRet['status'] === 0); + $respBuf = $decodeRet['sBuffer']; $out = \TUPAPI::getInt32('int32', $respBuf); @@ -388,8 +395,9 @@ public function testDefaultInt32() $this->contexts, $this->statuses, $encodeBufs); $decodeRet = \TUPAPI::decode($requestBuf); - if ($decodeRet['status'] !== 0) { - } + + $this->assertTrue($decodeRet['status'] === 0); + $respBuf = $decodeRet['sBuffer']; $out = \TUPAPI::getInt32('int3211', $respBuf); @@ -410,8 +418,9 @@ public function testUInt32() $this->contexts, $this->statuses, $encodeBufs); $decodeRet = \TUPAPI::decode($requestBuf); - if ($decodeRet['status'] !== 0) { - } + + $this->assertTrue($decodeRet['status'] === 0); + $respBuf = $decodeRet['sBuffer']; $out = \TUPAPI::getUInt32('uint32', $respBuf); @@ -432,8 +441,9 @@ public function testDefaultUInt32() $this->contexts, $this->statuses, $encodeBufs); $decodeRet = \TUPAPI::decode($requestBuf); - if ($decodeRet['status'] !== 0) { - } + + $this->assertTrue($decodeRet['status'] === 0); + $respBuf = $decodeRet['sBuffer']; $out = \TUPAPI::getUInt32('uint32111', $respBuf); @@ -454,8 +464,9 @@ public function testInt64() $this->contexts, $this->statuses, $encodeBufs); $decodeRet = \TUPAPI::decode($requestBuf); - if ($decodeRet['status'] !== 0) { - } + + $this->assertTrue($decodeRet['status'] === 0); + $respBuf = $decodeRet['sBuffer']; $out = \TUPAPI::getInt64('int64', $respBuf); @@ -476,8 +487,9 @@ public function testDefaultInt64() $this->contexts, $this->statuses, $encodeBufs); $decodeRet = \TUPAPI::decode($requestBuf); - if ($decodeRet['status'] !== 0) { - } + + $this->assertTrue($decodeRet['status'] === 0); + $respBuf = $decodeRet['sBuffer']; $out = \TUPAPI::getInt64('int6411', $respBuf); @@ -498,8 +510,9 @@ public function testDouble() $this->contexts, $this->statuses, $encodeBufs); $decodeRet = \TUPAPI::decode($requestBuf); - if ($decodeRet['status'] !== 0) { - } + + $this->assertTrue($decodeRet['status'] === 0); + $respBuf = $decodeRet['sBuffer']; $out = \TUPAPI::getDouble('double', $respBuf); @@ -520,8 +533,9 @@ public function testDefaultDouble() $this->contexts, $this->statuses, $encodeBufs); $decodeRet = \TUPAPI::decode($requestBuf); - if ($decodeRet['status'] !== 0) { - } + + $this->assertTrue($decodeRet['status'] === 0); + $respBuf = $decodeRet['sBuffer']; $out = \TUPAPI::getDouble('double11', $respBuf); @@ -542,13 +556,14 @@ public function testFloat() $this->contexts, $this->statuses, $encodeBufs); $decodeRet = \TUPAPI::decode($requestBuf); - if ($decodeRet['status'] !== 0) { - } + + $this->assertTrue($decodeRet['status'] === 0); + $respBuf = $decodeRet['sBuffer']; $out = \TUPAPI::getFloat('float', $respBuf); - $this->assertEquals((float) $float, (float) $out, 'not equal', 0.0000001); + $this->assertEqualsWithDelta((float) $float, (float) $out, 0.0000001); } public function testDefaultFloat() @@ -564,13 +579,14 @@ public function testDefaultFloat() $this->contexts, $this->statuses, $encodeBufs); $decodeRet = \TUPAPI::decode($requestBuf); - if ($decodeRet['status'] !== 0) { - } + + $this->assertTrue($decodeRet['status'] === 0); + $respBuf = $decodeRet['sBuffer']; $out = \TUPAPI::getFloat('float111', $respBuf); - $this->assertEquals((float) $float, (float) $out, 'not equal', 0.0000001); + $this->assertEqualsWithDelta((float) $float, (float) $out, 0.0000001); } public function testString() @@ -586,8 +602,9 @@ public function testString() $this->contexts, $this->statuses, $encodeBufs); $decodeRet = \TUPAPI::decode($requestBuf); - if ($decodeRet['status'] !== 0) { - } + + $this->assertTrue($decodeRet['status'] === 0); + $respBuf = $decodeRet['sBuffer']; $out = \TUPAPI::getString('string', $respBuf); @@ -628,8 +645,9 @@ public function testStringFromInt() $this->contexts, $this->statuses, $encodeBufs); $decodeRet = \TUPAPI::decode($requestBuf); - if ($decodeRet['status'] !== 0) { - } + + $this->assertTrue($decodeRet['status'] === 0); + $respBuf = $decodeRet['sBuffer']; $out = \TUPAPI::getString('string', $respBuf); @@ -650,8 +668,9 @@ public function testDefaultString() $this->contexts, $this->statuses, $encodeBufs); $decodeRet = \TUPAPI::decode($requestBuf); - if ($decodeRet['status'] !== 0) { - } + + $this->assertTrue($decodeRet['status'] === 0); + $respBuf = $decodeRet['sBuffer']; $out = \TUPAPI::getString('string111', $respBuf); @@ -676,8 +695,9 @@ public function testSimpleVector() $this->contexts, $this->statuses, $encodeBufs); $decodeRet = \TUPAPI::decode($requestBuf); - if ($decodeRet['status'] !== 0) { - } + + $this->assertTrue($decodeRet['status'] === 0); + $respBuf = $decodeRet['sBuffer']; $v = new \TARS_VECTOR(\TARS::STRING); @@ -704,8 +724,9 @@ public function testDefaultVector() $this->contexts, $this->statuses, $encodeBufs); $decodeRet = \TUPAPI::decode($requestBuf); - if ($decodeRet['status'] !== 0) { - } + + $this->assertTrue($decodeRet['status'] === 0); + $respBuf = $decodeRet['sBuffer']; $v = new \TARS_VECTOR(\TARS::STRING); $out = \TUPAPI::getVector('vec111', $v, $respBuf); @@ -730,8 +751,9 @@ public function testSimpleVectorChar() $this->contexts, $this->statuses, $encodeBufs); $decodeRet = \TUPAPI::decode($requestBuf); - if ($decodeRet['status'] !== 0) { - } + + $this->assertTrue($decodeRet['status'] === 0); + $respBuf = $decodeRet['sBuffer']; $v = new \TARS_VECTOR(\TARS::CHAR); @@ -753,8 +775,9 @@ public function testEmptyVector() $this->contexts, $this->statuses, $encodeBufs); $decodeRet = \TUPAPI::decode($requestBuf); - if ($decodeRet['status'] !== 0) { - } + + $this->assertTrue($decodeRet['status'] === 0); + $respBuf = $decodeRet['sBuffer']; $vector = new \TARS_VECTOR(\TARS::STRING); @@ -813,7 +836,7 @@ public function testSimpleMapOtherType() $okData = [123 => 1.1]; - $this->assertEquals($okData, $out, 'not equal', 0.0000001); + $this->assertEqualsWithDelta($okData, $out, 0.0000001); } public function testSimpleMapOtherType2() @@ -838,7 +861,7 @@ public function testSimpleMapOtherType2() $okData = [123 => 1.1]; - $this->assertEquals($okData, $out, 'not equal', 0.0000001); + $this->assertEqualsWithDelta($okData, $out, 0.0000001); } public function testSimpleMapOtherType3() @@ -863,7 +886,7 @@ public function testSimpleMapOtherType3() $okData = [123 => 1.1]; - $this->assertEquals($okData, $out, 'not equal', 0.0000001); + $this->assertEqualsWithDelta($okData, $out, 0.0000001); } public function testMapInMap() @@ -1080,8 +1103,9 @@ public function testSimpleStruct() $this->contexts, $this->statuses, $encodeBufs); $decodeRet = \TUPAPI::decode($requestBuf); - if ($decodeRet['status'] !== 0) { - } + + $this->assertTrue($decodeRet['status'] === 0); + $respBuf = $decodeRet['sBuffer']; $outSimpleStruct = new SimpleStruct(); @@ -1106,8 +1130,9 @@ public function testDefaultStruct() $this->contexts, $this->statuses, $encodeBufs); $decodeRet = \TUPAPI::decode($requestBuf); - if ($decodeRet['status'] !== 0) { - } + + $this->assertTrue($decodeRet['status'] === 0); + $respBuf = $decodeRet['sBuffer']; $outSimpleStruct = new SimpleStruct(); @@ -1146,8 +1171,9 @@ public function testAllTypeStruct() $this->contexts, $this->statuses, $encodeBufs); $decodeRet = \TUPAPI::decode($requestBuf); - if ($decodeRet['status'] !== 0) { - } + + $this->assertTrue($decodeRet['status'] === 0); + $respBuf = $decodeRet['sBuffer']; $allTypeStruct->vecchar = 'ta'; @@ -1156,7 +1182,7 @@ public function testAllTypeStruct() $result = \TUPAPI::getStruct('struct', $outAllTypeStruct, $respBuf); $this->fromArray($result, $outAllTypeStruct); - $this->assertEquals($allTypeStruct, $outAllTypeStruct, 'not equal', 0.0000001); + $this->assertEqualsWithDelta($allTypeStruct, $outAllTypeStruct, 0.0000001); } public function testMapInVector() diff --git a/testcases/testTARSClient.php b/testcases/testTARSClient.php index 62f2fc5..4539210 100644 --- a/testcases/testTARSClient.php +++ b/testcases/testTARSClient.php @@ -1,16 +1,10 @@ iVersion); - $this->assertEquals((float) $float, (float) $out, 'not equal', 0.0000001); + $this->assertEqualsWithDelta((float) $float, (float) $out, 0.0000001); } public function testDefaultFloat() @@ -439,7 +433,7 @@ public function testDefaultFloat() $out = \TUPAPI::getFloat('2', $respBuf, false, $this->iVersion); - $this->assertEquals((float) $float, (float) $out, 'not equal', 0.0000001); + $this->assertEqualsWithDelta((float) $float, (float) $out, 0.0000001); } public function testString() diff --git a/testcases/testTARSServer.php b/testcases/testTARSServer.php index 1add0fa..c256a43 100644 --- a/testcases/testTARSServer.php +++ b/testcases/testTARSServer.php @@ -1,16 +1,10 @@ iVersion); - $this->assertEquals((float) $float, (float) $out, 'not equal', 0.0000001); + $this->assertEqualsWithDelta((float) $float, (float) $out, 0.0000001); } public function testDefaultFloat() @@ -441,7 +435,7 @@ public function testDefaultFloat() $out = \TUPAPI::getFloat('2', $respBuf, false, $this->iVersion); - $this->assertEquals((float) $float, (float) $out, 'not equal', 0.0000001); + $this->assertEqualsWithDelta((float) $float, (float) $out, 0.0000001); } public function testString() diff --git a/tests/combination/context_status.phpt b/tests/combination/context_status.phpt deleted file mode 100644 index 3a0ba92..0000000 --- a/tests/combination/context_status.phpt +++ /dev/null @@ -1,52 +0,0 @@ ---TEST-- -context: status - ---SKIPIF-- - ---INI-- -zend.assertions=-1 -assert.active=1 -assert.warning=1 -assert.bail=0 -assert.quiet_eval=0 - ---FILE-- - 'testYong', 'test2' => '11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111'); -$statuses = array('test1' => 'testStatus', 'test2' => '22222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222'); - -$char = 1; -$buf = \TUPAPI::putChar('char', $char); - -$encodeBufs["char"] = $buf; - -$requestBuf = \TUPAPI::encode($iVersion, $iRequestId, $servantName, - $funcName, $cPacketType, $iMessageType, $iTimeout, - $contexts, $statuses, $encodeBufs); - - -$decodeRet = \TUPAPI::decodeReqPacket($requestBuf); - -$contexts_decode = $decodeRet["context"]; - -if($contexts_decode === $contexts){ - echo "success"; -} - -$statuses_decode = $decodeRet["status"]; -if($statuses_decode === $statuses){ - echo "success"; -} - -?> ---EXPECT-- -success \ No newline at end of file diff --git a/tests/include/functions.php b/tests/include/functions.php index 0cc9337..2e2d359 100644 --- a/tests/include/functions.php +++ b/tests/include/functions.php @@ -1,16 +1,10 @@ $value) { if (method_exists($structObj, 'set'.ucfirst($key))) { - call_user_func_array([$this, 'set'.ucfirst($key)], [$value]); + call_user_func_array([$structObj, 'set'.ucfirst($key)], [$value]); } elseif ($structObj->$key instanceof \TARS_Struct) { fromArray($value, $structObj->$key); } else { diff --git a/ttars.c b/ttars.c index fdbfa1f..8ca5021 100644 --- a/ttars.c +++ b/ttars.c @@ -5,11 +5,7 @@ #include "php.h" #include "php_ini.h" #include "ext/standard/info.h" -#if PHP_MAJOR_VERSION < 7 -#include "ext/standard/php_smart_str.h" -#else #include "ext/standard/php_smart_string.h" -#endif #include "./include/tars_c.h" #include "./include/tup_c.h" #include "./include/php_tupapi.h" @@ -20,18 +16,13 @@ static zend_object_handlers vector_wrapper_handlers; static zend_object_handlers map_wrapper_handlers; -static void vector_wrapper_dtor(zend_object * object TSRMLS_DC); -static void map_wrapper_dtor(zend_object * object TSRMLS_DC); +static void vector_wrapper_dtor(zend_object *object); +static void map_wrapper_dtor(zend_object *object); -static size_t tars_type_name(zval * this_ptr, int * type, char ** out); +static size_t tars_type_name(zval *this_ptr, int *type, char **out); -#if PHP_MAJOR_VERSION < 7 - static inline zend_object_value map_wrapper_object_new(zend_class_entry * clazz TSRMLS_DC); - static inline zend_object_value vector_wrapper_object_new(zend_class_entry * clazz TSRMLS_DC); -#else -static inline zend_object * map_wrapper_object_new(zend_class_entry * clazz TSRMLS_DC); -static inline zend_object * vector_wrapper_object_new(zend_class_entry * clazz TSRMLS_DC); -#endif +static inline zend_object *map_wrapper_object_new(zend_class_entry *clazz); +static inline zend_object *vector_wrapper_object_new(zend_class_entry *clazz); /* }}} */ /* {{{ _TARS_ZVAL_TO_BIG_NUMERIC 将zval结构转换成特定的数字类型 @@ -75,7 +66,7 @@ static inline zend_object * vector_wrapper_object_new(zend_class_entry * clazz T #define _TARS_ZVAL_TO_BIG_NUMERIC(z, type, out, tag, s) do {\ type *tmp, __dest; \ Int64 ll; \ - char * end_ptr; \ + char *end_ptr; \ if (Z_TYPE_P(z) == IS_STRING) { \ errno = 0; \ ll = STR_TO_INT64 (Z_STRVAL_P(z), &end_ptr, 10); \ @@ -124,9 +115,9 @@ static inline zend_object * vector_wrapper_object_new(zend_class_entry * clazz T } while (0) /* }}} */ -/* {{{ bool_packer(zval * z, TarsOutputStream * stream, uint8_t tag, void * out) +/* {{{ bool_packer(zval *z, TarsOutputStream *stream, uint8_t tag, void *out) */ -int bool_packer(zval * z, TarsOutputStream * stream, uint8_t tag, void * out) { +int bool_packer(zval *z, TarsOutputStream *stream, uint8_t tag, void *out) { int ret = TARS_SUCCESS; if (Z_TYPE_P(z) == IS_OBJECT || Z_TYPE_P(z) == IS_RESOURCE) { @@ -134,7 +125,7 @@ int bool_packer(zval * z, TarsOutputStream * stream, uint8_t tag, void * out) { } convert_to_boolean(z); if (out) { - Bool * tmp = out; + Bool *tmp = out; *tmp = (MY_Z_TYPE_P(z) == IS_TRUE) ? true : false; } if (stream) ret = TarsOutputStream_writeBool(stream, (MY_Z_TYPE_P(z) == IS_TRUE) ? true : false, tag); @@ -142,9 +133,9 @@ int bool_packer(zval * z, TarsOutputStream * stream, uint8_t tag, void * out) { } /* }}} */ -/* {{{ char_packer(zval * z, TarsOutputStream * stream, uint8_t tag, void * out) +/* {{{ char_packer(zval *z, TarsOutputStream *stream, uint8_t tag, void *out) */ -int char_packer(zval * z, TarsOutputStream * stream, uint8_t tag, void * out) { +int char_packer(zval *z, TarsOutputStream *stream, uint8_t tag, void *out) { int ret = TARS_SUCCESS; Char __dest; @@ -186,81 +177,81 @@ int char_packer(zval * z, TarsOutputStream * stream, uint8_t tag, void * out) { } /* }}} */ -/* {{{ uint8_packer(zval * z, TarsOutputStream * stream, uint8_t tag, void * out) +/* {{{ uint8_packer(zval *z, TarsOutputStream *stream, uint8_t tag, void *out) */ -int uint8_packer(zval * z, TarsOutputStream * stream, uint8_t tag, void * out) { +int uint8_packer(zval *z, TarsOutputStream *stream, uint8_t tag, void *out) { _TARS_ZVAL_TO_NUMERIC(z, UInt8, stream, tag, out); return TARS_SUCCESS; } /* }}} */ -/* {{{ short_packer(zval * z, TarsOutputStream * stream, uint8_t tag, void * out) +/* {{{ short_packer(zval *z, TarsOutputStream *stream, uint8_t tag, void *out) */ -int short_packer(zval * z, TarsOutputStream * stream, uint8_t tag, void * out) { +int short_packer(zval *z, TarsOutputStream *stream, uint8_t tag, void *out) { _TARS_ZVAL_TO_NUMERIC(z, Short, stream, tag, out); return TARS_SUCCESS; } /* }}} */ -/* {{{ uint16_packer(zval * z, TarsOutputStream * stream, uint8_t tag, void * out) +/* {{{ uint16_packer(zval *z, TarsOutputStream *stream, uint8_t tag, void *out) */ -int uint16_packer(zval * z, TarsOutputStream * stream, uint8_t tag, void * out) { +int uint16_packer(zval *z, TarsOutputStream *stream, uint8_t tag, void *out) { _TARS_ZVAL_TO_NUMERIC(z, UInt16, stream, tag, out); return TARS_SUCCESS; } /* }}} */ -/* {{{ int32_packer(zval * z, TarsOutputStream * stream, uint8_t tag, void *out) +/* {{{ int32_packer(zval *z, TarsOutputStream *stream, uint8_t tag, void *out) */ -int int32_packer(zval * z, TarsOutputStream * stream, uint8_t tag, void *out) { +int int32_packer(zval *z, TarsOutputStream *stream, uint8_t tag, void *out) { _TARS_ZVAL_TO_NUMERIC(z, Int32, stream, tag, out); return TARS_SUCCESS; } /* }}} */ -/* {{{ uint32_packer(zval * z, TarsOutputStream * stream, uint8_t tag, void * out) +/* {{{ uint32_packer(zval *z, TarsOutputStream *stream, uint8_t tag, void *out) */ -int uint32_packer(zval * z, TarsOutputStream * stream, uint8_t tag, void * out) { +int uint32_packer(zval *z, TarsOutputStream *stream, uint8_t tag, void *out) { _TARS_ZVAL_TO_BIG_NUMERIC(z, UInt32, stream, tag, out); return TARS_SUCCESS; } /* }}} */ -/* {{{ int64_packer(zval * z, TarsOutputStream * stream, uint8_t tag, void * out) +/* {{{ int64_packer(zval *z, TarsOutputStream *stream, uint8_t tag, void *out) */ -int int64_packer(zval * z, TarsOutputStream * stream, uint8_t tag, void * out) { +int int64_packer(zval *z, TarsOutputStream *stream, uint8_t tag, void *out) { _TARS_ZVAL_TO_BIG_NUMERIC(z, Int64, stream, tag, out); return TARS_SUCCESS; } /* }}} */ -/* {{{ dobule_packer(zval * z, TarsOutputStream * stream, uint8_t tag, void * out) +/* {{{ dobule_packer(zval *z, TarsOutputStream *stream, uint8_t tag, void *out) */ -int double_packer(zval * z, TarsOutputStream * stream, uint8_t tag, void * out) { +int double_packer(zval *z, TarsOutputStream *stream, uint8_t tag, void *out) { TARS_ZVAL_TO_FD(z, Double, stream, tag, out); return TARS_SUCCESS; } /* }}} */ -/* {{{ float_packer(zval * z, TarsOutputStream * stream, uint8_t tag, void * out) +/* {{{ float_packer(zval *z, TarsOutputStream *stream, uint8_t tag, void *out) */ -int float_packer(zval * z, TarsOutputStream * stream, uint8_t tag, void * out) { +int float_packer(zval *z, TarsOutputStream *stream, uint8_t tag, void *out) { TARS_ZVAL_TO_FD(z, Float, stream, tag, out); return TARS_SUCCESS; } /* }}} */ -/* {{{ string_packer(zval * z, TarsOutputStream * stream, uint8_t tag, void * out) +/* {{{ string_packer(zval *z, TarsOutputStream *stream, uint8_t tag, void *out) */ -int string_packer(zval * z, TarsOutputStream * stream, uint8_t tag, void * out) { +int string_packer(zval *z, TarsOutputStream *stream, uint8_t tag, void *out) { int ret = TARS_SUCCESS; switch (Z_TYPE_P(z)) { @@ -283,12 +274,12 @@ int string_packer(zval * z, TarsOutputStream * stream, uint8_t tag, void * out) } /* }}} */ -/* {{{ vector_packer(zval * this_ptr, TarsOutputStream * out, uint8_t tag, void * arr) +/* {{{ vector_packer(zval *this_ptr, TarsOutputStream *out, uint8_t tag, void *arr) */ -int vector_packer(zval * zv, TarsOutputStream * out, uint8_t tag, void * vector_ptr) { +int vector_packer(zval *zv, TarsOutputStream *out, uint8_t tag, void *vector_ptr) { int ret = ERR_CANNOT_CONVERT; - zval * this_ptr, *tmp = vector_ptr; + zval *this_ptr, *tmp = vector_ptr; if (IS_CLASS_VECTOR(zv)) { this_ptr = zv; @@ -298,14 +289,14 @@ int vector_packer(zval * zv, TarsOutputStream * out, uint8_t tag, void * vector_ return ret; } - vector_wrapper * obj = Z_VECTOR_WRAPPER_P(this_ptr TSRMLS_CC); + vector_wrapper *obj = Z_VECTOR_WRAPPER_P(this_ptr); if (IS_JSTRING(obj->t)) { - JString * js = obj->ctx->str; + JString *js = obj->ctx->str; ret = TarsOutputStream_writeVectorCharBuffer(out, JString_data(js), JString_size(js), tag); JString_clear(js); } else { - JArray * container = obj->ctx->vct; + JArray *container = obj->ctx->vct; ret = TarsOutputStream_writeVector(out, container, tag); } @@ -314,12 +305,12 @@ int vector_packer(zval * zv, TarsOutputStream * out, uint8_t tag, void * vector_ } /* }}} */ -/* {{{ map_packer(zval * this_ptr, TarsOutputStream * out, uint8_t tag, void * map) +/* {{{ map_packer(zval *this_ptr, TarsOutputStream *out, uint8_t tag, void *map) */ -int map_packer(zval * zv, TarsOutputStream * out, uint8_t tag, void * map_ptr) { +int map_packer(zval *zv, TarsOutputStream *out, uint8_t tag, void *map_ptr) { int ret = ERR_CANNOT_CONVERT; - zval * this_ptr, *tmp = map_ptr; + zval *this_ptr, *tmp = map_ptr; if (IS_CLASS_MAP(zv)) { this_ptr = zv; @@ -329,19 +320,19 @@ int map_packer(zval * zv, TarsOutputStream * out, uint8_t tag, void * map_ptr) { return ret; } - map_wrapper * obj = Z_MAP_WRAPPER_P(this_ptr TSRMLS_CC); - JMapWrapper * container = obj->ctx; + map_wrapper *obj = Z_MAP_WRAPPER_P(this_ptr); + JMapWrapper *container = obj->ctx; ret = TarsOutputStream_writeMap(out, container, tag); return ret; } /* }}} */ -int struct_packer(zval * occupy, TarsOutputStream * out, uint8_t tag, void * struct_ptr) { +int struct_packer(zval *occupy, TarsOutputStream *out, uint8_t tag, void *struct_ptr) { int ret; - TarsOutputStream * o = TarsOutputStream_new(); + TarsOutputStream *o = TarsOutputStream_new(); if (!o) return ERR_MALLOC_FAILED; ret = struct_packer_wrapper(o, struct_ptr); @@ -353,115 +344,21 @@ int struct_packer(zval * occupy, TarsOutputStream * out, uint8_t tag, void * str return ret; } -int struct_packer_wrapper(TarsOutputStream * o, void * struct_ptr) { +int struct_packer_wrapper(TarsOutputStream *o, void *struct_ptr) { int ret = ERR_CANNOT_CONVERT; zval *this_ptr = struct_ptr; - HashTable * props, *fields = NULL; + HashTable *props, *fields = NULL; if (!IS_CLASS_STRUCT(this_ptr)) return ERR_CLASS_MISMATCH; // 获取字段信息列表 - zval * tmp = my_zend_read_property(tars_struct_ce, this_ptr, ZEND_STRL(STRUCT_PROP_FIELDS), 1 TSRMLS_CC); + zval *tmp = my_zend_read_property(tars_struct_ce, this_ptr, ZEND_STRL(STRUCT_PROP_FIELDS), 1); if (Z_TYPE_P(tmp) == IS_ARRAY) { fields = Z_ARRVAL_P(tmp); } // 思路需要转换,只需要打包那些必须的元素,也就是fields中的内容 -#if PHP_MAJOR_VERSION < 7 - - props = Z_OBJPROP_P(this_ptr); - - for ( - zend_hash_internal_pointer_reset(fields); - zend_hash_has_more_elements(fields) == SUCCESS; - zend_hash_move_forward(fields) - ) { - - char * tag_key; - int tag_key_len,t; - ulong tag; - zval ** attributes, **val, **type, *sub = NULL; - - if (zend_hash_get_current_key_ex(fields, &tag_key, &tag_key_len, &tag, 0, NULL) == HASH_KEY_IS_LONG) - { - // 获取fields中的属性 - if (zend_hash_get_current_data(fields, (void **)&attributes) == FAILURE) { - return ERR_STATIC_FIELDS_PARAM_LOST; - } else { - char *name_key; - uint name_key_len ; - // 首先获取name的具体名称,作为获取实际值的key - if (zend_hash_find(Z_ARRVAL_P(*attributes), "name", sizeof("name"), (void **)&val) == SUCCESS) { - if (Z_TYPE_PP(val) == IS_STRING) { - name_key = Z_STRVAL_PP(val); - } else { - return ERR_STATIC_NAME_NOT_STRING_ERROR; - } - - // 其次获取是否是必选的字段 - Bool is_required; - if (zend_hash_find(Z_ARRVAL_P(*attributes), "required", sizeof("required"), (void **)&val) == SUCCESS) { - if (Z_TYPE_PP(val) == IS_BOOL) { - is_required = Z_BVAL_PP(val); - } else { - return ERR_STATIC_REQUIRED_NOT_BOOL_ERROR; - } - - // 获取其中的值 - if (zend_hash_find(props, name_key, strlen(name_key)+1, (void **)&val) == SUCCESS) { - // 再次获取具体的类型 - if (zend_hash_find(Z_ARRVAL_P(*attributes), "type", sizeof("type"), (void **)&type) == SUCCESS) { - - int real_type; - if (Z_TYPE_PP(type) == IS_LONG) { - real_type = Z_LVAL_PP(type); - } else { - return ERR_STATIC_TYPE_NOT_LONG_ERROR; - } - // 此时的val是真正从类中获取到的对象 - - // 如果是struct类型 - if(IS_STRUCT(real_type)) { - sub = *val; - ret = struct_packer(NULL,o, tag, sub); - } - // 其他复杂类型,包括vector和map sub本来是候补用的,在新的架构下,获取与普通类型并无差别 - else if (!IS_BASE_TYPE(real_type)) { - sub = *val; - ret = packer_dispatch[real_type](sub, o, tag, sub); - } - else { - ret = packer_dispatch[real_type](*val, o, tag, sub); - } - - - if (ret != TARS_SUCCESS) return ret; - } - else { - return ERR_STATIC_FIELDS_PARAM_LOST; - } - } - else { - // 如果是必填但是获取失败,那么报错 - if(is_required) { - return ERR_REQUIRED_FIELD_LOST; - } - else return ERR_ARRAY_RETRIEVE; - } - } - else { - return ERR_STATIC_FIELDS_PARAM_LOST; - } - } - else { - return ERR_REQUIRED_FIELD_LOST; - } - } - } - } -#else - //PHP7 ulong tag; zend_string *zkey; zval *attributes; @@ -472,7 +369,7 @@ int struct_packer_wrapper(TarsOutputStream * o, void * struct_ptr) { } char *name_key; - zval * val, *type, *sub = NULL; + zval *val, *type, *sub = NULL; // 首先获取name的具体名称,作为获取实际值的key if ((val = zend_hash_str_find(Z_ARRVAL_P(attributes), "name", sizeof("name")-1)) != NULL) { @@ -492,7 +389,7 @@ int struct_packer_wrapper(TarsOutputStream * o, void * struct_ptr) { } // 获取其中的值 //TODO change old code ? - if ((val = my_zend_read_property(tars_struct_ce, this_ptr, name_key, strlen(name_key), 1 TSRMLS_CC))) { + if ((val = my_zend_read_property(tars_struct_ce, this_ptr, name_key, strlen(name_key), 1))) { // 再次获取具体的类型 if ((type = zend_hash_str_find(Z_ARRVAL_P(attributes), "type", sizeof("type")-1)) != NULL) { int real_type; @@ -540,14 +437,13 @@ int struct_packer_wrapper(TarsOutputStream * o, void * struct_ptr) { } } ZEND_HASH_FOREACH_END(); -#endif return ret; } -/* {{{ bool_unpacker(TarsInputStream * stream, uint8_t tag, Bool is_require, zval * this_ptr, void ** zv) +/* {{{ bool_unpacker(TarsInputStream *stream, uint8_t tag, Bool is_require, zval *this_ptr, void **zv) */ -int bool_unpacker(TarsInputStream * stream, uint8_t tag, Bool is_require, zval * this_ptr, void ** zv) { +int bool_unpacker(TarsInputStream *stream, uint8_t tag, Bool is_require, zval *this_ptr, void **zv) { int ret; Bool b = 0; @@ -559,9 +455,9 @@ int bool_unpacker(TarsInputStream * stream, uint8_t tag, Bool is_require, zval * } /* }}} */ -/* {{{ char_unpacker(TarsInputStream * stream, uint8_t tag, Bool is_require, zval * this_ptr, void ** zv) +/* {{{ char_unpacker(TarsInputStream *stream, uint8_t tag, Bool is_require, zval *this_ptr, void **zv) */ -int char_unpacker(TarsInputStream * stream, uint8_t tag, Bool is_require, zval * this_ptr, void ** zv) { +int char_unpacker(TarsInputStream *stream, uint8_t tag, Bool is_require, zval *this_ptr, void **zv) { int ret; Char c = '\0'; @@ -585,33 +481,33 @@ int char_unpacker(TarsInputStream * stream, uint8_t tag, Bool is_require, zval * } while (0) /* }}} */ -/* {{{ uint8_unpacker(TarsInputStream * stream, uint8_t tag, Bool is_require, zval * this_ptr, void ** zv) +/* {{{ uint8_unpacker(TarsInputStream *stream, uint8_t tag, Bool is_require, zval *this_ptr, void **zv) */ -int uint8_unpacker(TarsInputStream * stream, uint8_t tag, Bool is_require, zval * this_ptr, void ** zv) { +int uint8_unpacker(TarsInputStream *stream, uint8_t tag, Bool is_require, zval *this_ptr, void **zv) { _TARS_CONVERT_NUMERIC_ZVAL(UInt8, stream, tag, is_require, this_ptr, zv); } /* }}} */ -/* {{{ short_unpacker(TarsInputStream * stream, uint8_t tag, Bool is_require, zval * this_ptr, void ** zv) +/* {{{ short_unpacker(TarsInputStream *stream, uint8_t tag, Bool is_require, zval *this_ptr, void **zv) */ -int short_unpacker(TarsInputStream * stream, uint8_t tag, Bool is_require, zval * this_ptr, void ** zv) { +int short_unpacker(TarsInputStream *stream, uint8_t tag, Bool is_require, zval *this_ptr, void **zv) { _TARS_CONVERT_NUMERIC_ZVAL(Short, stream, tag, is_require, this_ptr, zv); } /* }}} */ -/* {{{ uint16_unpacker(TarsInputStream * stream, uint8_t tag, Bool is_require, zval * this_ptr, void ** zv) +/* {{{ uint16_unpacker(TarsInputStream *stream, uint8_t tag, Bool is_require, zval *this_ptr, void **zv) */ -int uint16_unpacker(TarsInputStream * stream, uint8_t tag, Bool is_require, zval * this_ptr, void ** zv) { +int uint16_unpacker(TarsInputStream *stream, uint8_t tag, Bool is_require, zval *this_ptr, void **zv) { _TARS_CONVERT_NUMERIC_ZVAL(UInt16, stream, tag, is_require, this_ptr, zv); } /* }}} */ -/* {{{ int32_unpacker(TarsInputStream * stream, uint8_t tag, Bool is_require, zval * this_ptr, void ** zv) +/* {{{ int32_unpacker(TarsInputStream *stream, uint8_t tag, Bool is_require, zval *this_ptr, void **zv) */ -int int32_unpacker(TarsInputStream * stream, uint8_t tag, Bool is_require, zval * this_ptr, void ** zv) { +int int32_unpacker(TarsInputStream *stream, uint8_t tag, Bool is_require, zval *this_ptr, void **zv) { _TARS_CONVERT_NUMERIC_ZVAL(Int32, stream, tag, is_require, this_ptr, zv); } @@ -626,28 +522,28 @@ int int32_unpacker(TarsInputStream * stream, uint8_t tag, Bool is_require, zval if (ret == TARS_SUCCESS) { \ l = (long) t; \ if (t == (Int64) l) { \ - ZVAL_LONG((zval*)* zv, l); \ + ZVAL_LONG((zval*) *zv, l); \ } else { \ char buf[32]; \ int len = slprintf(buf, 32, "%lld", l); \ - MY_ZVAL_STRINGL((zval*)* zv, buf, len, 1); \ + MY_ZVAL_STRINGL((zval*) *zv, buf, len, 1); \ } \ } \ return ret ; \ } while (0) /* }}} */ -/* {{{ uint32_unpacker(TarsInputStream * stream, uint8_t tag, Bool is_require, zval * this_ptr, void ** zv) +/* {{{ uint32_unpacker(TarsInputStream *stream, uint8_t tag, Bool is_require, zval *this_ptr, void **zv) */ -int uint32_unpacker(TarsInputStream * stream, uint8_t tag, Bool is_require, zval * this_ptr, void ** zv) { +int uint32_unpacker(TarsInputStream *stream, uint8_t tag, Bool is_require, zval *this_ptr, void **zv) { __UNPACK_INT64(stream, tag, is_require, this_ptr, zv); } /* }}} */ -/* {{{ int64_unpacker(TarsInputStream * stream, uint8_t tag, Bool is_require, zval * this_ptr, void ** zv) +/* {{{ int64_unpacker(TarsInputStream *stream, uint8_t tag, Bool is_require, zval *this_ptr, void **zv) */ -int int64_unpacker(TarsInputStream * stream, uint8_t tag, Bool is_require, zval * this_ptr, void ** zv) { +int int64_unpacker(TarsInputStream *stream, uint8_t tag, Bool is_require, zval *this_ptr, void **zv) { __UNPACK_INT64(stream, tag, is_require, this_ptr, zv); } @@ -659,39 +555,39 @@ int int64_unpacker(TarsInputStream * stream, uint8_t tag, Bool is_require, zval int ret; type f = 0.0;\ ret = TarsInputStream_read ##type (stream, &f, tag, require); \ if (ret == TARS_SUCCESS) { \ - ZVAL_DOUBLE((zval*)* zv, (double)f); \ + ZVAL_DOUBLE((zval*) *zv, (double)f); \ } \ return ret; \ } while (0) /* }}} */ -/* {{{ float_unpacker(TarsInputStream * stream, uint8_t tag, Bool is_require, zval * this_ptr, void ** zv) +/* {{{ float_unpacker(TarsInputStream *stream, uint8_t tag, Bool is_require, zval *this_ptr, void **zv) */ -int float_unpacker(TarsInputStream * stream, uint8_t tag, Bool is_require, zval * this_ptr, void ** zv) { +int float_unpacker(TarsInputStream *stream, uint8_t tag, Bool is_require, zval *this_ptr, void **zv) { __UNPACK_FD(Float, stream, tag, is_require, this_ptr, zv); } /* }}} */ -/* {{{ double_unpacker(TarsInputStream * stream, uint8_t tag, Bool is_require, zval * this_ptr, void ** zv) +/* {{{ double_unpacker(TarsInputStream *stream, uint8_t tag, Bool is_require, zval *this_ptr, void **zv) */ -int double_unpacker(TarsInputStream * stream, uint8_t tag, Bool is_require, zval * this_ptr, void ** zv) { +int double_unpacker(TarsInputStream *stream, uint8_t tag, Bool is_require, zval *this_ptr, void **zv) { __UNPACK_FD(Double, stream, tag, is_require, this_ptr, zv); } /* }}} */ -/* {{{ string_unpacker(TarsInputStream * stream, uint8_t tag, Bool is_require, zval * this_ptr, void **zv) +/* {{{ string_unpacker(TarsInputStream *stream, uint8_t tag, Bool is_require, zval *this_ptr, void **zv) */ -int string_unpacker(TarsInputStream * stream, uint8_t tag, Bool is_require, zval * this_ptr, void **zv) { +int string_unpacker(TarsInputStream *stream, uint8_t tag, Bool is_require, zval *this_ptr, void **zv) { int ret; - JString * js = JString_new(); + JString *js = JString_new(); if (!js) return ERR_MALLOC_FAILED; ret = TarsInputStream_readString(stream, js, tag, is_require); if (ret == TARS_SUCCESS) { - MY_ZVAL_STRINGL((zval*)* zv, JS_STRVAL(js), JS_STRLEN(js), 1); + MY_ZVAL_STRINGL((zval*) *zv, JS_STRVAL(js), JS_STRLEN(js), 1); } JString_del(&js); @@ -699,23 +595,23 @@ int string_unpacker(TarsInputStream * stream, uint8_t tag, Bool is_require, zval } /* }}} */ -/* {{{ vector_unpacker(TarsInputStream * stream, uint8_t tag, Bool is_require, zval * this_ptr, void ** zv) +/* {{{ vector_unpacker(TarsInputStream *stream, uint8_t tag, Bool is_require, zval *this_ptr, void **zv) */ -int vector_unpacker(TarsInputStream * stream, uint8_t tag, Bool is_require, zval * this_ptr, void ** zv) { +int vector_unpacker(TarsInputStream *stream, uint8_t tag, Bool is_require, zval *this_ptr, void **zv) { zval *sub = NULL, *tmp = NULL, *cntr = NULL; - JArray * container = NULL; - JString * js = NULL; - TarsInputStream * is = NULL; + JArray *container = NULL; + JString *js = NULL; + TarsInputStream *is = NULL; tars_unpack_func_t unpacker; int i, ret; - cntr = (zval *) *zv; + cntr = (zval*) *zv; // 检查对象类型 if (!IS_CLASS_VECTOR(this_ptr)) return ERR_CLASS_MISMATCH; - vector_wrapper * obj = Z_VECTOR_WRAPPER_P(this_ptr TSRMLS_CC); + vector_wrapper *obj = Z_VECTOR_WRAPPER_P(this_ptr); if (!IS_VALID_TYPE(obj->t)) return ERR_TYPE_INVALID; // vector 特殊处理 @@ -733,7 +629,7 @@ int vector_unpacker(TarsInputStream * stream, uint8_t tag, Bool is_require, zval unpacker = unpacker_dispatch[obj->t]; if (!IS_BASE_TYPE(obj->t)) { - sub = my_zend_read_property(tars_vector_ce, this_ptr, ZEND_STRL(TARS_PROP_TYPE_CLASS), 1 TSRMLS_CC); + sub = my_zend_read_property(tars_vector_ce, this_ptr, ZEND_STRL(TARS_PROP_TYPE_CLASS), 1); } if (stream) { @@ -768,15 +664,15 @@ int vector_unpacker(TarsInputStream * stream, uint8_t tag, Bool is_require, zval } /* }}} */ -/* {{{ map_unpacker(TarsInputStream * stream, uint8_t tag, Bool is_require, zval * this_ptr, void ** zv) +/* {{{ map_unpacker(TarsInputStream *stream, uint8_t tag, Bool is_require, zval *this_ptr, void **zv) */ -int map_unpacker(TarsInputStream * stream, uint8_t tag, Bool is_require, zval * this_ptr, void ** zv) { +int map_unpacker(TarsInputStream *stream, uint8_t tag, Bool is_require, zval *this_ptr, void **zv) { - JMapWrapper * container; + JMapWrapper *container; int ret; if (!IS_CLASS_MAP(this_ptr)) return ERR_CLASS_MISMATCH; - map_wrapper * obj = Z_MAP_WRAPPER_P(this_ptr TSRMLS_CC); + map_wrapper *obj = Z_MAP_WRAPPER_P(this_ptr); container = obj->ctx; JMapWrapper_clear(container); @@ -787,9 +683,9 @@ int map_unpacker(TarsInputStream * stream, uint8_t tag, Bool is_require, zval * } /* }}} */ -/* {{{ _map_to_array (zval * this_ptr, JMapWrapper * container, void **zv) +/* {{{ _map_to_array (zval *this_ptr, JMapWrapper *container, void **zv) */ -int _map_to_array(zval * this_ptr, JMapWrapper * container, void **zv) { +int _map_to_array(zval *this_ptr, JMapWrapper *container, void **zv) { zval *type, *fsub = NULL, *ssub = NULL, *cntr = NULL; tars_unpack_func_t funpacker, sunpacker; @@ -797,17 +693,17 @@ int _map_to_array(zval * this_ptr, JMapWrapper * container, void **zv) { cntr = (zval *) *zv; - type = my_zend_read_property(tars_map_ce, this_ptr, ZEND_STRL(MAP_PROP_FIRST_TYPE), 1 TSRMLS_CC); + type = my_zend_read_property(tars_map_ce, this_ptr, ZEND_STRL(MAP_PROP_FIRST_TYPE), 1); ft = Z_LVAL_P(type); if (!IS_VALID_TYPE(ft)) return ERR_TYPE_INVALID; - if (!IS_BASE_TYPE(ft)) fsub = my_zend_read_property(tars_map_ce, this_ptr, ZEND_STRL(MAP_FIRST_TYPE_CLASS), 1 TSRMLS_CC); + if (!IS_BASE_TYPE(ft)) fsub = my_zend_read_property(tars_map_ce, this_ptr, ZEND_STRL(MAP_FIRST_TYPE_CLASS), 1); - type = my_zend_read_property(tars_map_ce, this_ptr, ZEND_STRL(MAP_PROP_SECOND_TYPE), 1 TSRMLS_CC); + type = my_zend_read_property(tars_map_ce, this_ptr, ZEND_STRL(MAP_PROP_SECOND_TYPE), 1); st = Z_LVAL_P(type); if (!IS_VALID_TYPE(st)) return ERR_TYPE_INVALID; - if (!IS_BASE_TYPE(st)) ssub = my_zend_read_property(tars_map_ce, this_ptr, ZEND_STRL(MAP_SECOND_TYPE_CLASS), 1 TSRMLS_CC); + if (!IS_BASE_TYPE(st)) ssub = my_zend_read_property(tars_map_ce, this_ptr, ZEND_STRL(MAP_SECOND_TYPE_CLASS), 1); - type = my_zend_read_property(tars_map_ce, this_ptr, ZEND_STRL(MAP_PROP_PARAM_FORMAT), 1 TSRMLS_CC); + type = my_zend_read_property(tars_map_ce, this_ptr, ZEND_STRL(MAP_PROP_PARAM_FORMAT), 1); if (MY_Z_TYPE_P(type) == IS_TRUE) { format = 1; } @@ -815,12 +711,12 @@ int _map_to_array(zval * this_ptr, JMapWrapper * container, void **zv) { funpacker = unpacker_dispatch[ft]; sunpacker = unpacker_dispatch[st]; - TarsInputStream * stream = TarsInputStream_new(); + TarsInputStream *stream = TarsInputStream_new(); if (!stream) return ERR_MALLOC_FAILED; array_init(cntr); - zval * key = NULL, *value = NULL, *item = NULL; + zval *key = NULL, *value = NULL, *item = NULL; for (i = 0; i < JMapWrapper_size(container); ++i) { ALLOC_INIT_ZVAL(key); ALLOC_INIT_ZVAL(value); @@ -851,18 +747,10 @@ int _map_to_array(zval * this_ptr, JMapWrapper * container, void **zv) { if (Z_TYPE_P(key) == IS_LONG) { my_zend_hash_index_update(Z_ARRVAL_P(cntr), Z_LVAL_P(key), (void *) &value, sizeof(zval *), NULL); } else { -#if PHP_MAJOR_VERSION < 7 - zend_symtable_update(Z_ARRVAL_P(cntr), Z_STRVAL_P(key), Z_STRLEN_P(key) + 1, &value, sizeof(zval *), NULL); -#else zend_symtable_update(Z_ARRVAL_P(cntr), Z_STR_P(key), value); -#endif } -#if PHP_MAJOR_VERSION < 7 - zval_ptr_dtor(&key); -#else zval_dtor(key); -#endif } else { // 以两维数组的方式返回 ALLOC_INIT_ZVAL(item); @@ -884,12 +772,12 @@ do_clean : } /* }}} */ -/* {{{ struct_unpacker(TarsInputStream * stream, uint8_t tag, Bool is_require, zval * this_ptr, void ** zv) +/* {{{ struct_unpacker(TarsInputStream *stream, uint8_t tag, Bool is_require, zval *this_ptr, void **zv) */ -int struct_unpacker(TarsInputStream * stream, uint8_t tag, Bool is_require, zval * this_ptr,void ** zv) { +int struct_unpacker(TarsInputStream *stream, uint8_t tag, Bool is_require, zval *this_ptr,void **zv) { - JString * js; - TarsInputStream * is; + JString *js; + TarsInputStream *is; int ret ; js = JString_new(); @@ -914,22 +802,22 @@ int struct_unpacker(TarsInputStream * stream, uint8_t tag, Bool is_require, zval } /* }}} */ -/* {{{ struct_unpacker_wrapper(TarsInputStream * stream, zval * this_ptr, void ** zv) +/* {{{ struct_unpacker_wrapper(TarsInputStream *stream, zval *this_ptr, void **zv) */ -int struct_unpacker_wrapper(TarsInputStream * is, zval * this_ptr,void ** zv) { +int struct_unpacker_wrapper(TarsInputStream *is, zval *this_ptr,void **zv) { zval *prop, *ret_zv; - HashTable * props, *tpl_ht = NULL, * fields = NULL; + HashTable *props, *tpl_ht = NULL, *fields = NULL; int ret = ERR_CANNOT_CONVERT; - ret_zv = (zval*)* zv; + ret_zv = (zval*) *zv; if (!IS_CLASS_STRUCT(this_ptr)) return ERR_CLASS_MISMATCH; props = Z_OBJPROP_P(this_ptr); // 获取字段信息列表 - zval * tmp = my_zend_read_property(tars_struct_ce, this_ptr, ZEND_STRL(STRUCT_PROP_FIELDS), 1 TSRMLS_CC); + zval *tmp = my_zend_read_property(tars_struct_ce, this_ptr, ZEND_STRL(STRUCT_PROP_FIELDS), 1); if (MY_Z_TYPE_P(tmp) == IS_ARRAY) { fields = Z_ARRVAL_P(tmp); } @@ -937,111 +825,6 @@ int struct_unpacker_wrapper(TarsInputStream * is, zval * this_ptr,void ** zv) { array_init(ret_zv); // 思路需要转换,只需要打包那些必须的元素,也就是fields中的内容 -#if PHP_MAJOR_VERSION < 7 - for ( - zend_hash_internal_pointer_reset(fields); - zend_hash_has_more_elements(fields) == SUCCESS; - zend_hash_move_forward(fields) - ) { - - char * tag_key; - int tag_key_len,t; - ulong tag; - zval ** attributes, **val, **type,** complicate_val, *sub = NULL; - ALLOC_INIT_ZVAL(prop); - - if (zend_hash_get_current_key_ex(fields, &tag_key, &tag_key_len, &tag, 0, NULL) == HASH_KEY_IS_LONG) - { - // 获取fields中的属性 - if (zend_hash_get_current_data(fields, (void **)&attributes) == FAILURE) { - return ERR_STATIC_FIELDS_PARAM_LOST; - } else { - char *name_key; - uint name_key_len ; - // 首先获取name的具体名称,作为获取实际值的key - if (zend_hash_find(Z_ARRVAL_P(*attributes), "name", sizeof("name"), (void **)&val) == SUCCESS) { - if (Z_TYPE_PP(val) == IS_STRING) { - name_key = Z_STRVAL_PP(val); - name_key_len = Z_STRLEN_PP(val); - } else { - zval copyval = **val; - zval_copy_ctor(©val); - convert_to_string(©val); - name_key = Z_STRVAL(copyval); - name_key_len = Z_STRLEN(copyval); - zval_dtor(©val); - } - - // 其次获取是否是必选的字段 - Bool is_required; - if (zend_hash_find(Z_ARRVAL_P(*attributes), "required", sizeof("required"), (void **)&val) == SUCCESS) { - if (Z_TYPE_PP(val) == IS_BOOL) { - is_required = Z_BVAL_PP(val); - } else { - zval copyval = **val; - zval_copy_ctor(©val); - convert_to_boolean(©val); - is_required = Z_BVAL(copyval); - zval_dtor(©val); - } - - // 再次获取具体的类型 - if (zend_hash_find(props, name_key, strlen(name_key)+1, (void **)&val) == SUCCESS) { - if (zend_hash_find(Z_ARRVAL_P(*attributes), "type", sizeof("type"), (void **)&type) == SUCCESS) { - int real_type; - if (Z_TYPE_PP(type) == IS_LONG) { - real_type = Z_LVAL_PP(type); - } else { - zval copyval = **type; - zval_copy_ctor(©val); - convert_to_long(©val); - real_type = Z_LVAL(copyval); - zval_dtor(©val); - } - - zval * complicate_type; - - // 如果是struct类型 - if(IS_STRUCT(real_type)) { - ret = struct_unpacker(is, tag, is_required, *val, (void **)&prop); - } - // 其他复杂类型,包括vector和map, 增加了对extType的兼容 - else if (!IS_BASE_TYPE(real_type)) { - ret = unpacker_dispatch[real_type](is, tag, is_required, *val, (void **)&prop); - } - // 普通类型直接取fields里面的type - else { - ret = unpacker_dispatch[real_type](is, tag, is_required, *type, (void **)&prop); - } - - if(ret == -6) continue; - else if (ret != TARS_SUCCESS) return ret; - - // 获取其中的值 - zend_hash_add(Z_ARRVAL_P(ret_zv), name_key, name_key_len+1, (void **)&prop, sizeof(zval *), NULL); - } - else { - return ERR_STATIC_FIELDS_PARAM_LOST; - } - - } - else { - return ERR_STATIC_FIELDS_PARAM_LOST; - } - } - else { - return ERR_STATIC_FIELDS_PARAM_LOST; - } - } - else { - return ERR_REQUIRED_FIELD_LOST; - } - } - } - prop = NULL; - } -#else - //PHP7 ulong tag; zend_string *zkey; zval *attributes; @@ -1053,7 +836,7 @@ int struct_unpacker_wrapper(TarsInputStream * is, zval * this_ptr,void ** zv) { } char *name_key; - zval * val, *type, *sub = NULL; + zval *val, *type, *sub = NULL; // 首先获取name的具体名称,作为获取实际值的key if ((val = zend_hash_str_find(Z_ARRVAL_P(attributes), "name", sizeof("name")-1)) != NULL) { @@ -1074,7 +857,7 @@ int struct_unpacker_wrapper(TarsInputStream * is, zval * this_ptr,void ** zv) { // 再次获取具体的类型 // 获取其中的值 //TODO change old code ? - if ((val = my_zend_read_property(tars_struct_ce, this_ptr, name_key, strlen(name_key), 1 TSRMLS_CC))) { + if ((val = my_zend_read_property(tars_struct_ce, this_ptr, name_key, strlen(name_key), 1))) { if ((type = zend_hash_str_find(Z_ARRVAL_P(attributes), "type", sizeof("type")-1)) != NULL) { int real_type; if (Z_TYPE_P(type) == IS_LONG) { @@ -1121,21 +904,20 @@ int struct_unpacker_wrapper(TarsInputStream * is, zval * this_ptr,void ** zv) { prop = NULL; } ZEND_HASH_FOREACH_END(); -#endif return TARS_SUCCESS; } /* }}} */ -/* {{{ map_converter(zval * this_ptr, zval * zv, int tag, int depth) +/* {{{ map_converter(zval *this_ptr, zval *zv, int tag, int depth) */ -int map_converter(zval * this_ptr, zval * zv) { +int map_converter(zval *this_ptr, zval *zv) { zval *type, *key_zv = NULL, *value_zv = NULL; - HashTable * ht; + HashTable *ht; int ftype, stype, ret = 0, format = 0; - JMapWrapper * container; + JMapWrapper *container; tars_pack_func_t fpacker, spacker; if (Z_TYPE_P(zv) != IS_ARRAY) { @@ -1144,16 +926,16 @@ int map_converter(zval * this_ptr, zval * zv) { ht = Z_ARRVAL_P(zv); // 第一项参数类型 - type = my_zend_read_property(tars_map_ce, this_ptr, ZEND_STRL(MAP_PROP_FIRST_TYPE), 1 TSRMLS_CC); + type = my_zend_read_property(tars_map_ce, this_ptr, ZEND_STRL(MAP_PROP_FIRST_TYPE), 1); if (Z_TYPE_P(type) != IS_LONG || !IS_VALID_TYPE(Z_LVAL_P(type))) return ERR_WRONG_PARAMS; ftype = Z_LVAL_P(type); // 第二个参数类型 - type = my_zend_read_property(tars_map_ce, this_ptr, ZEND_STRL(MAP_PROP_SECOND_TYPE), 1 TSRMLS_CC); + type = my_zend_read_property(tars_map_ce, this_ptr, ZEND_STRL(MAP_PROP_SECOND_TYPE), 1); if (Z_TYPE_P(type) != IS_LONG || !IS_VALID_TYPE(Z_LVAL_P(type))) return ERR_WRONG_PARAMS; stype = Z_LVAL_P(type); - type = my_zend_read_property(tars_map_ce, this_ptr, ZEND_STRL(MAP_PROP_PARAM_FORMAT), 1 TSRMLS_CC); + type = my_zend_read_property(tars_map_ce, this_ptr, ZEND_STRL(MAP_PROP_PARAM_FORMAT), 1); if ((MY_Z_TYPE_P(type) == IS_TRUE)) { format = 1; } @@ -1168,90 +950,13 @@ int map_converter(zval * this_ptr, zval * zv) { TarsOutputStream *sStream = TarsOutputStream_new(); if (!sStream) { TarsOutputStream_del(&fStream); return ERR_MALLOC_FAILED;} - map_wrapper * obj = Z_MAP_WRAPPER_P(this_ptr TSRMLS_CC); + map_wrapper *obj = Z_MAP_WRAPPER_P(this_ptr); container = obj->ctx; -#if PHP_MAJOR_VERSION < 7 - for( - zend_hash_internal_pointer_reset(ht); - zend_hash_has_more_elements(ht) == SUCCESS; - zend_hash_move_forward(ht)) - { - zval ** key, **item, **value; - char * str; - ulong idx; - int key_type; - uint str_len; - - if (!format) { - // 参数是以关联数组的方式传即 数组的键是map的第一个元素, 数组的值是第二个元素 - - key_type = zend_hash_get_current_key_ex(ht, &str, &str_len, &idx, 0, NULL); - // 取一条记录 - if (zend_hash_get_current_data(ht, (void**)&value) == FAILURE) { - continue; - } - - // 申请一个zval - ALLOC_INIT_ZVAL(key_zv); - - if (key_type == HASH_KEY_IS_STRING) { - // 复制一份字符串 - ZVAL_STRINGL(key_zv, str, str_len - 1, 1); - } else { - ZVAL_LONG(key_zv, idx); - } - key = &key_zv; - - } else { - - // 参数是以二维数组的方式传即 数组中的键key对应的值是map的第一个元素, 键value对应的值是map的第二个元素 - - // 取一条记录 - if (zend_hash_get_current_data(ht, (void**)&item) == FAILURE) { - continue; - } - - // 检查参数是否正确 - if (Z_TYPE_PP(item) != IS_ARRAY || - zend_hash_find(Z_ARRVAL_PP(item), ZEND_STRS(MAP_FIRST_KEY), (void **)&key) == FAILURE || - zend_hash_find(Z_ARRVAL_PP(item), ZEND_STRS(MAP_SECOND_KEY), (void **)&value) == FAILURE) { - ret = ERR_DATA_FORMAT_ERROR; - goto do_clean; - } - } - - TarsOutputStream_reset(fStream); - // 针对结构体,进行特殊处理 - if(IS_STRUCT(ftype)) { - ret = fpacker (NULL,fStream, 0, *key); - } else { - ret = fpacker (*key, fStream, 0, NULL); - } - if (ret != TARS_SUCCESS) goto do_clean; - - TarsOutputStream_reset(sStream); - - // 针对结构体,进行特殊处理 - if(IS_STRUCT(stype)) { - ret = spacker (NULL,sStream, 1, *value); - } - else { - ret = spacker (*value, sStream, 1, NULL); - } - if (ret != TARS_SUCCESS) goto do_clean; - - ret = JMapWrapper_put(container, TarsOutputStream_getBuffer(fStream), TarsOutputStream_getLength(fStream), - TarsOutputStream_getBuffer(sStream), TarsOutputStream_getLength(sStream)); - if (ret != TARS_SUCCESS) goto do_clean; - if (key_zv) {zval_ptr_dtor(&key_zv); key_zv = NULL; } - } -#else - //PHP7 - zend_string * zskey; + zend_string *zskey; ulong num_key; - zval * key, *value, *item; + zval *key, *value, *item; ZEND_HASH_FOREACH_KEY_VAL(ht, num_key, zskey, item) { if (!item) { @@ -1319,7 +1024,6 @@ int map_converter(zval * this_ptr, zval * zv) { value_zv = NULL; } } ZEND_HASH_FOREACH_END(); -#endif do_clean : if (key_zv) my_zval_ptr_dtor(&key_zv); @@ -1331,37 +1035,34 @@ do_clean : } /* }}} */ -/* {{{ tars_type_name(zval * this_ptr, int * type, char **out) +/* {{{ tars_type_name(zval *this_ptr, int *type, char **out) */ -size_t tars_type_name(zval * this_ptr, int * type, char ** out) { +size_t tars_type_name(zval *this_ptr, int *type, char **out) { - zval * name = NULL; - char * type_name; + zval *name = NULL; + char *type_name; size_t len; - if (Z_TYPE_P(this_ptr) == IS_OBJECT) { - zval * orig_type = my_zend_read_property(Z_OBJCE_P(this_ptr), this_ptr, ZEND_STRL(TARS_PROP_ORIG_TYPE), 1 TSRMLS_CC); + zval *orig_type = my_zend_read_property(Z_OBJCE_P(this_ptr), this_ptr, ZEND_STRL(TARS_PROP_ORIG_TYPE), 1); -#if PHP_MAJOR_VERSION >= 7 if (Z_TYPE_P(orig_type) == IS_REFERENCE) { orig_type = Z_REFVAL_P(orig_type); } -#endif if (Z_TYPE_P(orig_type) == IS_LONG) { - * type = Z_LVAL_P(orig_type); + *type = Z_LVAL_P(orig_type); if (IS_BASE_TYPE(*type)) { return 0; } - name = my_zend_read_property(Z_OBJCE_P(this_ptr), this_ptr, ZEND_STRL(TARS_PROP_TYPE_NAME), 1 TSRMLS_CC); + name = my_zend_read_property(Z_OBJCE_P(this_ptr), this_ptr, ZEND_STRL(TARS_PROP_TYPE_NAME), 1); if (Z_TYPE_P(name) != IS_STRING) { return 0; } } } else if (Z_TYPE_P(this_ptr) == IS_LONG) { - * type = Z_LVAL_P(this_ptr); + *type = Z_LVAL_P(this_ptr); if (!IS_BASE_TYPE(*type)) { return 0; } @@ -1426,12 +1127,8 @@ size_t tars_type_name(zval * this_ptr, int * type, char ** out) { /* {{{ vector_wrapper_dtor(void * object) */ -static void vector_wrapper_dtor(zend_object * object TSRMLS_DC) { -#if PHP_MAJOR_VERSION < 7 - vector_wrapper * obj = (vector_wrapper *) object; -#else - vector_wrapper * obj = vector_wrapper_fetch_object(object); -#endif +static void vector_wrapper_dtor(zend_object *object) { + vector_wrapper *obj = vector_wrapper_fetch_object(object); if (obj->ctx) { if (obj->t == TTARS_TYPE_CHAR) { @@ -1443,67 +1140,29 @@ static void vector_wrapper_dtor(zend_object * object TSRMLS_DC) { efree(obj->ctx); } - zend_object_std_dtor(&obj->std TSRMLS_CC); -#if PHP_MAJOR_VERSION < 7 - efree(obj); -#endif + zend_object_std_dtor(&obj->std); } /* }}} */ -/* {{{ map_wrapper_dtor(void * object TSRMLS_DC) +/* {{{ map_wrapper_dtor(void *object) */ -static void map_wrapper_dtor(zend_object * object TSRMLS_DC) { -#if PHP_MAJOR_VERSION < 7 - map_wrapper * obj = (map_wrapper *) object; -#else - map_wrapper * obj = map_wrapper_fetch_object(object); -#endif +static void map_wrapper_dtor(zend_object *object) { + map_wrapper *obj = map_wrapper_fetch_object(object); if (obj->ctx) { JMapWrapper_del(&obj->ctx); } - zend_object_std_dtor(&obj->std TSRMLS_CC); -#if PHP_MAJOR_VERSION < 7 - efree(obj); -#endif + zend_object_std_dtor(&obj->std); } /* }}} */ -/* {{{ vector_wrapper_object_new(zend_class_entry * clazz TSRMLS_DC) +/* {{{ vector_wrapper_object_new(zend_class_entry *clazz) */ -#if PHP_MAJOR_VERSION < 7 -static inline zend_object_value vector_wrapper_object_new(zend_class_entry * clazz TSRMLS_DC) { - - zval * tmp; - zend_object_value retval; - vector_wrapper * obj = (vector_wrapper *)emalloc(sizeof(vector_wrapper)); - memset(obj, 0, sizeof(vector_wrapper)); - - zend_object_std_init(&obj->std, clazz TSRMLS_CC); - -#if PHP_VERSION_ID >=50400 - object_properties_init( (zend_object *) obj, clazz); -#else - { - zend_hash_copy(obj->std.properties, &clazz->properties_info,(copy_ctor_func_t)zval_add_ref, (void *) &tmp, sizeof(zval *) -); - } -#endif - - retval.handle = zend_objects_store_put(obj, (zend_objects_store_dtor_t)zend_objects_destroy_object, (zend_objects_free_object_storage_t)vector_wrapper_dtor, NULL TSRMLS_CC); - retval.handlers = &vector_wrapper_handlers; +static inline zend_object *vector_wrapper_object_new(zend_class_entry *clazz) { + vector_wrapper *obj = (vector_wrapper *) ecalloc(1, sizeof(vector_wrapper) + zend_object_properties_size(clazz)); - return retval; -} -#else -static inline zend_object * vector_wrapper_object_new(zend_class_entry * clazz) { - vector_wrapper * obj = (vector_wrapper *) ecalloc(1, sizeof(vector_wrapper) + zend_object_properties_size(clazz)); - -// memset(obj, 0, sizeof(vector_wrapper)); -// vector_wrapper * obj = ecalloc(1, sizeof(vector_wrapper) + zend_object_properties_size(clazz)); - - zend_object_std_init(&obj->std, clazz TSRMLS_CC); + zend_object_std_init(&obj->std, clazz); object_properties_init(&obj->std, clazz); vector_wrapper_handlers.offset = XtOffsetOf(vector_wrapper, std); @@ -1513,43 +1172,14 @@ static inline zend_object * vector_wrapper_object_new(zend_class_entry * clazz) return &obj->std; } - -#endif /* }}} */ -/* {{{ map_wrapper_object_new(zend_class_entry * clazz TSRMLS_DC) +/* {{{ map_wrapper_object_new(zend_class_entry *clazz) */ -#if PHP_MAJOR_VERSION < 7 -static inline zend_object_value map_wrapper_object_new(zend_class_entry * clazz TSRMLS_DC) { - - zval * tmp; - zend_object_value retval; - map_wrapper * obj = (map_wrapper *)emalloc(sizeof(map_wrapper)); - memset(obj, 0, sizeof(map_wrapper)); - - zend_object_std_init(&obj->std, clazz TSRMLS_CC); - -#if PHP_VERSION_ID >=50400 - object_properties_init( (zend_object *) obj, clazz); -#else - { - zend_hash_copy(obj->std.properties, &clazz->properties_info,(copy_ctor_func_t)zval_add_ref, (void *) &tmp, sizeof(zval *) -); - } -#endif - - retval.handle = zend_objects_store_put(obj, (zend_objects_store_dtor_t)zend_objects_destroy_object, (zend_objects_free_object_storage_t) map_wrapper_dtor, NULL TSRMLS_CC); - retval.handlers = &map_wrapper_handlers; - - return retval; -} -#else -static inline zend_object * map_wrapper_object_new(zend_class_entry * clazz TSRMLS_DC) { - map_wrapper * obj = (map_wrapper *) ecalloc(1, sizeof(map_wrapper) + zend_object_properties_size(clazz)); -// map_wrapper * obj = (map_wrapper *)emalloc(sizeof(map_wrapper)); -// memset(obj, 0, sizeof(map_wrapper)); +static inline zend_object *map_wrapper_object_new(zend_class_entry *clazz) { + map_wrapper *obj = (map_wrapper *) ecalloc(1, sizeof(map_wrapper) + zend_object_properties_size(clazz)); - zend_object_std_init(&obj->std, clazz TSRMLS_CC); + zend_object_std_init(&obj->std, clazz); object_properties_init(&obj->std, clazz); map_wrapper_handlers.offset = XtOffsetOf(map_wrapper, std); @@ -1559,7 +1189,6 @@ static inline zend_object * map_wrapper_object_new(zend_class_entry * clazz TSRM return &obj->std; } -#endif /* }}} */ /* {{{ TTARS ARG INFO @@ -1621,7 +1250,7 @@ TUP_STARTUP_FUNC(ttars) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, "TARS", tars_methods); - tars_ce = zend_register_internal_class(&ce TSRMLS_CC); + tars_ce = zend_register_internal_class(&ce); zend_declare_class_constant_long(tars_ce, ZEND_STRL(PHP_TTARS_BOOL), TTARS_TYPE_BOOL); zend_declare_class_constant_long(tars_ce, ZEND_STRL(PHP_TTARS_CHAR), TTARS_TYPE_CHAR); zend_declare_class_constant_long(tars_ce, ZEND_STRL(PHP_TTARS_UINT8), TTARS_TYPE_UINT8); @@ -1639,22 +1268,22 @@ TUP_STARTUP_FUNC(ttars) { INIT_CLASS_ENTRY(ce, "TARS_Vector", tars_vector_methods); ce.create_object = vector_wrapper_object_new; - tars_vector_ce = zend_register_internal_class(&ce TSRMLS_CC); + tars_vector_ce = zend_register_internal_class(&ce); memcpy(&vector_wrapper_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); vector_wrapper_handlers.clone_obj = NULL; - zend_declare_property_long(tars_vector_ce, ZEND_STRL(TARS_PROP_ORIG_TYPE), VECTOR_PROP_ORIG_TYPE, ZEND_ACC_PRIVATE TSRMLS_CC); + zend_declare_property_long(tars_vector_ce, ZEND_STRL(TARS_PROP_ORIG_TYPE), VECTOR_PROP_ORIG_TYPE, ZEND_ACC_PRIVATE); INIT_CLASS_ENTRY(ce, "TARS_Map", tars_map_methods); ce.create_object = map_wrapper_object_new; - tars_map_ce = zend_register_internal_class(&ce TSRMLS_CC); + tars_map_ce = zend_register_internal_class(&ce); memcpy(&map_wrapper_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); map_wrapper_handlers.clone_obj = NULL; - zend_declare_property_long(tars_map_ce, ZEND_STRL(TARS_PROP_ORIG_TYPE), MAP_PROP_ORIG_TYPE, ZEND_ACC_PRIVATE TSRMLS_CC); - zend_declare_property_bool(tars_map_ce, ZEND_STRL(MAP_PROP_PARAM_FORMAT), 0, ZEND_ACC_PRIVATE TSRMLS_CC); + zend_declare_property_long(tars_map_ce, ZEND_STRL(TARS_PROP_ORIG_TYPE), MAP_PROP_ORIG_TYPE, ZEND_ACC_PRIVATE); + zend_declare_property_bool(tars_map_ce, ZEND_STRL(MAP_PROP_PARAM_FORMAT), 0, ZEND_ACC_PRIVATE); INIT_CLASS_ENTRY(ce, "TARS_Struct", tars_struct_methods); - tars_struct_ce = zend_register_internal_class(&ce TSRMLS_CC); - zend_declare_property_long(tars_struct_ce, ZEND_STRL(TARS_PROP_ORIG_TYPE), STRUCT_PROP_ORIG_TYPE, ZEND_ACC_PROTECTED TSRMLS_CC); + tars_struct_ce = zend_register_internal_class(&ce); + zend_declare_property_long(tars_struct_ce, ZEND_STRL(TARS_PROP_ORIG_TYPE), STRUCT_PROP_ORIG_TYPE, ZEND_ACC_PROTECTED); return SUCCESS; } @@ -1664,19 +1293,18 @@ TUP_STARTUP_FUNC(ttars) { */ PHP_METHOD(tars_vector, __construct) { - zval * clazz; - char * name = NULL; + zval *clazz; + char *name = NULL; int type; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &clazz) == FAILURE) { - ZEND_WRONG_PARAM_COUNT(); - return ; - } + ZEND_PARSE_PARAMETERS_START(1, 1) + Z_PARAM_ZVAL(clazz) + ZEND_PARSE_PARAMETERS_END(); tars_type_name(clazz, &type, &name); if (!name) return TYPE_EXCEPTOIN(); - vector_wrapper * obj = Z_VECTOR_WRAPPER_P(getThis() TSRMLS_CC); + vector_wrapper *obj = Z_VECTOR_WRAPPER_P(getThis()); obj->ctx = (vector_ctx *) ecalloc(1, sizeof(vector_ctx)); if (IS_JSTRING(type)) { obj->ctx->str = JString_new(); @@ -1685,10 +1313,12 @@ PHP_METHOD(tars_vector, __construct) { } obj->t = type; - zend_update_property(tars_vector_ce, getThis(), ZEND_STRL(VECTOR_PROP_TYPE_CLASS), clazz TSRMLS_CC); - zend_update_property_long(tars_vector_ce, getThis(), ZEND_STRL(VECTOR_PROP_TYPE), type TSRMLS_CC); + zend_object *self = Z_OBJ_P(getThis()); - zend_update_property_string(tars_vector_ce, getThis(), ZEND_STRL(TARS_PROP_TYPE_NAME), name TSRMLS_CC); + zend_update_property(tars_vector_ce, self, ZEND_STRL(VECTOR_PROP_TYPE_CLASS), clazz); + zend_update_property_long(tars_vector_ce, self, ZEND_STRL(VECTOR_PROP_TYPE), type); + + zend_update_property_string(tars_vector_ce, self, ZEND_STRL(TARS_PROP_TYPE_NAME), name); efree(name); RETURN_ZVAL(getThis(), 1, 0); @@ -1701,19 +1331,18 @@ PHP_METHOD(tars_vector, __construct) { */ PHP_METHOD(tars_vector, pushBack) { - zval * value = NULL, * sub = NULL; - JString * js = NULL; - JArray * vct = NULL; - TarsOutputStream * out = NULL; + zval *value = NULL, *sub = NULL; + JString *js = NULL; + JArray *vct = NULL; + TarsOutputStream *out = NULL; int ret = 0; char b ; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &value) == FAILURE) { - ZEND_WRONG_PARAM_COUNT(); - return; - } + ZEND_PARSE_PARAMETERS_START(1, 1) + Z_PARAM_ZVAL(value) + ZEND_PARSE_PARAMETERS_END(); - vector_wrapper * obj = Z_VECTOR_WRAPPER_P(getThis() TSRMLS_CC); + vector_wrapper *obj = Z_VECTOR_WRAPPER_P(getThis()); if (!IS_VALID_TYPE(obj->t)) return UNINIT_EXCEPTION(tars_vector_ce->name); out = TarsOutputStream_new(); @@ -1730,7 +1359,7 @@ PHP_METHOD(tars_vector, pushBack) { ret = struct_packer(NULL,out, 0, value); } else if(!IS_BASE_TYPE(obj->t)) { - sub = my_zend_read_property(tars_vector_ce, getThis(), ZEND_STRL(TARS_PROP_TYPE_CLASS), 1 TSRMLS_CC); + sub = my_zend_read_property(tars_vector_ce, getThis(), ZEND_STRL(TARS_PROP_TYPE_CLASS), 1); ret = packer_dispatch[obj->t](value, out, 0, sub); } else ret = packer_dispatch[obj->t](value, out, 0, (void *)&b); @@ -1761,43 +1390,47 @@ do_clean : */ PHP_METHOD(tars_map, __construct) { - zval * fclazz, * sclazz; - char * fname = NULL, * sname = NULL; + zval *fclazz, *sclazz; + char *fname = NULL, *sname = NULL; int ftype, stype; zend_bool format = 0; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz|b", &fclazz, &sclazz, &format) == FAILURE) { - ZEND_WRONG_PARAM_COUNT(); - return ; - } + ZEND_PARSE_PARAMETERS_START(2, 3) + Z_PARAM_ZVAL(fclazz) + Z_PARAM_ZVAL(sclazz) + Z_PARAM_OPTIONAL + Z_PARAM_BOOL(format) + ZEND_PARSE_PARAMETERS_END(); tars_type_name(fclazz, &ftype, &fname); tars_type_name(sclazz, &stype, &sname); if (!fname || !sname) return TYPE_EXCEPTOIN(); - char * type_name; + char *type_name; spprintf(&type_name, 0, "%s,%s", fname, sname); - zend_update_property_string(tars_map_ce, getThis(), ZEND_STRL(TARS_PROP_TYPE_NAME), type_name TSRMLS_CC); + zend_update_property_string(tars_map_ce, Z_OBJ_P(getThis()), ZEND_STRL(TARS_PROP_TYPE_NAME), type_name); - map_wrapper * obj = Z_MAP_WRAPPER_P(getThis() TSRMLS_CC); + map_wrapper *obj = Z_MAP_WRAPPER_P(getThis()); obj->ctx = JMapWrapper_new(fname, sname); if (!obj->ctx) return MALLOC_EXCEPTION("Map"); + zend_object *self = Z_OBJ_P(getThis()); + // 类型名 - zend_update_property_string(tars_map_ce, getThis(), ZEND_STRL(MAP_FIRST_TYPE_NAME), fname TSRMLS_CC); - zend_update_property_string(tars_map_ce, getThis(), ZEND_STRL(MAP_SECOND_TYPE_NAME), sname TSRMLS_CC); + zend_update_property_string(tars_map_ce, self, ZEND_STRL(MAP_FIRST_TYPE_NAME), fname); + zend_update_property_string(tars_map_ce, self, ZEND_STRL(MAP_SECOND_TYPE_NAME), sname); // 类型值 - zend_update_property_long(tars_map_ce, getThis(), ZEND_STRL(MAP_PROP_FIRST_TYPE), ftype TSRMLS_CC); - zend_update_property_long(tars_map_ce, getThis(), ZEND_STRL(MAP_PROP_SECOND_TYPE), stype TSRMLS_CC); + zend_update_property_long(tars_map_ce, self, ZEND_STRL(MAP_PROP_FIRST_TYPE), ftype); + zend_update_property_long(tars_map_ce, self, ZEND_STRL(MAP_PROP_SECOND_TYPE), stype); // 类型对象 - zend_update_property(tars_map_ce, getThis(), ZEND_STRL(MAP_FIRST_TYPE_CLASS), fclazz TSRMLS_CC); - zend_update_property(tars_map_ce, getThis(), ZEND_STRL(MAP_SECOND_TYPE_CLASS), sclazz TSRMLS_CC); + zend_update_property(tars_map_ce, self, ZEND_STRL(MAP_FIRST_TYPE_CLASS), fclazz); + zend_update_property(tars_map_ce, self, ZEND_STRL(MAP_SECOND_TYPE_CLASS), sclazz); // 打包解包方式 if (format) { - zend_update_property_bool(tars_map_ce, getThis(), ZEND_STRL(MAP_PROP_PARAM_FORMAT), 1 TSRMLS_CC); + zend_update_property_bool(tars_map_ce, self, ZEND_STRL(MAP_PROP_PARAM_FORMAT), 1); } efree(fname); @@ -1812,16 +1445,15 @@ PHP_METHOD(tars_map, __construct) { */ PHP_METHOD(tars_map, pushBack) { - zval *type, * value, * array = NULL; + zval *type, *value, *array = NULL; int ret; int format = 0; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &value) == FAILURE) { - ZEND_WRONG_PARAM_COUNT(); - return ; - } + ZEND_PARSE_PARAMETERS_START(1, 1) + Z_PARAM_ARRAY(value) + ZEND_PARSE_PARAMETERS_END(); - type = my_zend_read_property(tars_map_ce, getThis(), ZEND_STRL(MAP_PROP_PARAM_FORMAT), 1 TSRMLS_CC); + type = my_zend_read_property(tars_map_ce, getThis(), ZEND_STRL(MAP_PROP_PARAM_FORMAT), 1); if (MY_Z_TYPE_P(type) == IS_TRUE) { format = 1; } @@ -1833,11 +1465,7 @@ PHP_METHOD(tars_map, pushBack) { array_init(array); // 增加计数 -#if PHP_MAJOR_VERSION <7 - Z_ADDREF_P(value); -#else Z_TRY_ADDREF_P(value); -#endif my_zend_hash_next_index_insert(Z_ARRVAL_P(array), (void **) &value, sizeof(zval *), NULL); } else { @@ -1859,26 +1487,28 @@ PHP_METHOD(tars_map, pushBack) { */ PHP_METHOD(tars_struct, __construct) { - char * class_name; - zend_size_t class_len; - zval * fields = NULL; // 所有字段信息列表 + char *class_name; + size_t class_len; + zval *fields = NULL; // 所有字段信息列表 - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sa",&class_name, &class_len ,&fields ) == FAILURE) { - ZEND_WRONG_PARAM_COUNT(); - return; - } + ZEND_PARSE_PARAMETERS_START(2, 2) + Z_PARAM_STRING(class_name, class_len) + Z_PARAM_ARRAY(fields) + ZEND_PARSE_PARAMETERS_END(); if (class_len < STRUCT_NAME_MIN || class_len > STRUCT_NAME_MAX) { tup_throw_exception(ERR_WRONG_PARAMS, "class %s name length exceeds the limit.", Z_OBJCE_P(getThis())->name); return ; } + zend_object *self = Z_OBJ_P(getThis()); + if (fields) { // 所有字段信息 - zend_update_property(tars_struct_ce, getThis(), ZEND_STRL(STRUCT_PROP_FIELDS), fields TSRMLS_CC); + zend_update_property(tars_struct_ce, self, ZEND_STRL(STRUCT_PROP_FIELDS), fields); } - zend_update_property_string(tars_struct_ce, getThis(), ZEND_STRL(TARS_PROP_TYPE_NAME), class_name TSRMLS_CC); + zend_update_property_string(tars_struct_ce, self, ZEND_STRL(TARS_PROP_TYPE_NAME), class_name); RETURN_ZVAL(getThis(), 1, 0); } /* }}} */ diff --git a/tup_c.c b/tup_c.c index a5c4b01..0976a63 100644 --- a/tup_c.c +++ b/tup_c.c @@ -8,12 +8,12 @@ #include "./include/php_tupapi.h" #include "./include/ttars.h" -static Int32 TUP_putAttributeV3(UniAttribute *pack, const char * name, const char * type, TarsOutputStream * data_value) +static Int32 TUP_putAttributeV3(UniAttribute *pack, const char *name, const char *type, TarsOutputStream *data_value) { Int32 ret = 0; TarsOutputStream *os_map_first=NULL, *os_map_second=NULL; - UniAttribute * attr = pack; + UniAttribute *attr = pack; os_map_first = TarsOutputStream_new(); os_map_second= TarsOutputStream_new(); @@ -55,10 +55,11 @@ static Int32 TUP_putAttributeV3(UniAttribute *pack, const char * name, const cha return ret; } -Int32 TUP_putBool(void * pack, const char * name, Bool value) + +Int32 TUP_putBool(void *pack, const char *name, Bool value) { Int32 ret=0; - UniAttribute * attr = pack; + UniAttribute *attr = pack; TarsOutputStream_reset(attr->value_os); ret = TarsOutputStream_writeBool(attr->value_os, value, 0); if (TARS_SUCCESS != ret) return ret; @@ -66,10 +67,10 @@ Int32 TUP_putBool(void * pack, const char * name, Bool value) return TUP_putAttributeV3(pack, name, "bool", attr->value_os); } -Int32 TUP_putChar (void * pack, const char * name, Char value) +Int32 TUP_putChar(void *pack, const char *name, Char value) { Int32 ret=0; - UniAttribute * attr = pack; + UniAttribute *attr = pack; TarsOutputStream_reset(attr->value_os); ret = TarsOutputStream_writeChar(attr->value_os, value, 0); if (TARS_SUCCESS != ret) return ret; @@ -77,10 +78,10 @@ Int32 TUP_putChar (void * pack, const char * name, Char value) return TUP_putAttributeV3(pack, name, "char", attr->value_os); } -Int32 TUP_putUInt8 (void * pack, const char * name, UInt8 value) +Int32 TUP_putUInt8(void *pack, const char *name, UInt8 value) { Int32 ret=0; - UniAttribute * attr = pack; + UniAttribute *attr = pack; TarsOutputStream_reset(attr->value_os); ret = TarsOutputStream_writeUInt8(attr->value_os, value, 0); if (TARS_SUCCESS != ret) return ret; @@ -88,10 +89,10 @@ Int32 TUP_putUInt8 (void * pack, const char * name, UInt8 value) return TUP_putAttributeV3(pack, name, "uint8", attr->value_os); } -Int32 TUP_putShort (void * pack, const char * name, Short value) +Int32 TUP_putShort(void *pack, const char *name, Short value) { Int32 ret=0; - UniAttribute * attr = pack; + UniAttribute *attr = pack; TarsOutputStream_reset(attr->value_os); ret = TarsOutputStream_writeShort(attr->value_os, value, 0); if (TARS_SUCCESS != ret) return ret; @@ -99,10 +100,10 @@ Int32 TUP_putShort (void * pack, const char * name, Short value) return TUP_putAttributeV3(pack, name, "short", attr->value_os); } -Int32 TUP_putUInt16 (void * pack, const char * name, UInt16 value) +Int32 TUP_putUInt16(void *pack, const char *name, UInt16 value) { Int32 ret=0; - UniAttribute * attr = pack; + UniAttribute *attr = pack; TarsOutputStream_reset(attr->value_os); ret = TarsOutputStream_writeUInt16(attr->value_os, value, 0); if (TARS_SUCCESS != ret) return ret; @@ -110,10 +111,10 @@ Int32 TUP_putUInt16 (void * pack, const char * name, UInt16 value) return TUP_putAttributeV3(pack, name, "uint16", attr->value_os); } -Int32 TUP_putFloat (void * pack, const char * name, Float value) +Int32 TUP_putFloat(void *pack, const char *name, Float value) { Int32 ret=0; - UniAttribute * attr = pack; + UniAttribute *attr = pack; TarsOutputStream_reset(attr->value_os); ret = TarsOutputStream_writeFloat(attr->value_os, value, 0); if (TARS_SUCCESS != ret) return ret; @@ -121,10 +122,10 @@ Int32 TUP_putFloat (void * pack, const char * name, Float value) return TUP_putAttributeV3(pack, name, "float", attr->value_os); } -Int32 TUP_putDouble(void * pack, const char * name, Double value) +Int32 TUP_putDouble(void *pack, const char *name, Double value) { Int32 ret=0; - UniAttribute * attr = pack; + UniAttribute *attr = pack; TarsOutputStream_reset(attr->value_os); ret = TarsOutputStream_writeDouble(attr->value_os, value, 0); if (TARS_SUCCESS != ret) return ret; @@ -132,10 +133,10 @@ Int32 TUP_putDouble(void * pack, const char * name, Double value) return TUP_putAttributeV3(pack, name, "double", attr->value_os); } -Int32 TUP_putInt32 (void * pack, const char * name, Int32 value) +Int32 TUP_putInt32(void *pack, const char *name, Int32 value) { Int32 ret=0; - UniAttribute * attr = pack; + UniAttribute *attr = pack; TarsOutputStream_reset(attr->value_os); ret = TarsOutputStream_writeInt32(attr->value_os, value, 0); if (TARS_SUCCESS != ret) return ret; @@ -143,10 +144,10 @@ Int32 TUP_putInt32 (void * pack, const char * name, Int32 value) return TARS_SUCCESS; } -Int32 TUP_putUInt32 (void * pack, const char * name, UInt32 value) +Int32 TUP_putUInt32(void *pack, const char *name, UInt32 value) { Int32 ret=0; - UniAttribute * attr = pack; + UniAttribute *attr = pack; TarsOutputStream_reset(attr->value_os); ret = TarsOutputStream_writeUInt32(attr->value_os, value, 0); if (TARS_SUCCESS != ret) return ret; @@ -154,11 +155,10 @@ Int32 TUP_putUInt32 (void * pack, const char * name, UInt32 value) return TARS_SUCCESS; } - -Int32 TUP_putInt64 (void * pack, const char * name, Int64 value) +Int32 TUP_putInt64(void *pack, const char *name, Int64 value) { Int32 ret=0; - UniAttribute * attr = pack; + UniAttribute *attr = pack; TarsOutputStream_reset(attr->value_os); TarsOutputStream_writeInt64(attr->value_os, value, 0); if (TARS_SUCCESS != ret) return ret; @@ -166,15 +166,15 @@ Int32 TUP_putInt64 (void * pack, const char * name, Int64 value) return TARS_SUCCESS; } -Int32 TUP_putString(void * pack, const char * name, JString * value) +Int32 TUP_putString(void *pack, const char *name, JString *value) { return TUP_putStringBuffer(pack, name, JString_data(value), JString_size(value)); } -Int32 TUP_putStringBuffer(void * pack, const char * name, const char* buff, uint32_t len) +Int32 TUP_putStringBuffer(void *pack, const char *name, const char *buff, uint32_t len) { Int32 ret=0; - UniAttribute * attr = pack; + UniAttribute *attr = pack; TarsOutputStream_reset(attr->value_os); ret = TarsOutputStream_writeStringBuffer(attr->value_os, buff, len, 0); if (TARS_SUCCESS != ret) return ret; @@ -182,12 +182,12 @@ Int32 TUP_putStringBuffer(void * pack, const char * name, const char* buff, uint return TUP_putAttributeV3(pack, name, "string", attr->value_os); } -Int32 TUP_putVector(void * pack, const char * name, JArray * value) +Int32 TUP_putVector(void *pack, const char *name, JArray *value) { Int32 ret=0; char sType[64+1]={0}; - UniAttribute * attr = pack; + UniAttribute *attr = pack; TarsOutputStream_reset(attr->value_os); ret = TarsOutputStream_writeVector(attr->value_os, value, 0); if (TARS_SUCCESS != ret) return ret; @@ -195,16 +195,16 @@ Int32 TUP_putVector(void * pack, const char * name, JArray * value) return TARS_SUCCESS; } -Int32 TUP_putVectorChar(void * pack, const char * name, JString* value) +Int32 TUP_putVectorChar(void *pack, const char *name, JString *value) { return TUP_putVectorCharBuffer(pack, name, JString_data(value), JString_size(value)); } -Int32 TUP_putVectorCharBuffer(void * pack, const char * name, const char* buff, uint32_t len) +Int32 TUP_putVectorCharBuffer(void *pack, const char *name, const char *buff, uint32_t len) { Int32 ret=0; - UniAttribute * attr = pack; + UniAttribute *attr = pack; TarsOutputStream_reset(attr->value_os); ret = TarsOutputStream_writeVectorCharBuffer(attr->value_os, buff, len, 0); if (TARS_SUCCESS != ret) return ret; @@ -212,12 +212,12 @@ Int32 TUP_putVectorCharBuffer(void * pack, const char * name, const char* buff, return TARS_SUCCESS; } -Int32 TUP_putMap(void * pack, const char * name, JMapWrapper * value) +Int32 TUP_putMap(void *pack, const char *name, JMapWrapper *value) { Int32 ret=0; char sType[64+1]={0}; - UniAttribute * attr = pack; + UniAttribute *attr = pack; TarsOutputStream_reset(attr->value_os); ret = TarsOutputStream_writeMap(attr->value_os, value, 0); if (TARS_SUCCESS != ret) return ret; @@ -225,11 +225,11 @@ Int32 TUP_putMap(void * pack, const char * name, JMapWrapper * value) return TARS_SUCCESS; } -Int32 TUP_putStruct(void * pack, const char * name, const void* st) +Int32 TUP_putStruct(void *pack, const char *name, const void *st) { Int32 ret=0; - const JStructBase * jst = st; - UniAttribute * attr = pack; + const JStructBase *jst = st; + UniAttribute *attr = pack; TarsOutputStream_reset(attr->value_os); // value_os中写入结构体,但是似乎之后没有用到? // 不对,就在下面的最后一个参数,用到了。 @@ -238,14 +238,14 @@ Int32 TUP_putStruct(void * pack, const char * name, const void* st) return TUP_putAttributeV3(pack, name, jst->className, attr->value_os); } -static Int32 TUP_getAttributeV3(const UniAttribute *pack, const char * name, JString ** s, Bool is_require) +static Int32 TUP_getAttributeV3(const UniAttribute *pack, const char *name, JString **s, Bool is_require) { Int32 ret = 0; - char * pBuff = NULL; + char *pBuff = NULL; uint32_t len = 0; TarsInputStream *is_vectorchar; - const UniAttribute * attr = pack; + const UniAttribute *attr = pack; is_vectorchar = TarsInputStream_new(); @@ -290,10 +290,10 @@ static Int32 TUP_getAttributeV3(const UniAttribute *pack, const char * name, JSt } //get -Int32 TUP_getBool (const void * pack, const char * name, Bool* value, Bool is_require) +Int32 TUP_getBool(const void *pack, const char *name, Bool *value, Bool is_require) { JString *s = NULL; - const UniAttribute * attr; + const UniAttribute *attr; Int32 ret; //s = JString_new(); @@ -314,10 +314,10 @@ Int32 TUP_getBool (const void * pack, const char * name, Bool* value, Bool is_r return ret; } -Int32 TUP_getChar (const void * pack, const char * name, Char* value, Bool is_require) +Int32 TUP_getChar(const void *pack, const char *name, Char *value, Bool is_require) { JString *s = NULL; - const UniAttribute * attr; + const UniAttribute *attr; Int32 ret; //s = JString_new(); @@ -338,10 +338,10 @@ Int32 TUP_getChar (const void * pack, const char * name, Char* value, Bool is_r return ret; } -Int32 TUP_getUInt8 (const void * pack, const char * name, UInt8 * value, Bool is_require) +Int32 TUP_getUInt8(const void *pack, const char *name, UInt8 *value, Bool is_require) { JString *s = NULL; - const UniAttribute * attr; + const UniAttribute *attr; Int32 ret; ret = TUP_getAttributeV3(pack, name, &s, is_require); @@ -359,15 +359,12 @@ Int32 TUP_getUInt8 (const void * pack, const char * name, UInt8 * value, Bool is return ret; } -Int32 TUP_getShort (const void * pack, const char * name, Short * value, Bool is_require) +Int32 TUP_getShort(const void *pack, const char *name, Short *value, Bool is_require) { JString *s = NULL; - const UniAttribute * attr; + const UniAttribute *attr; Int32 ret; - //s = JString_new(); - //if(!s) return TARS_MALLOC_ERROR; - ret = TUP_getAttributeV3(pack, name, &s, is_require); if (TARS_SUCCESS != ret) goto do_clean; @@ -383,10 +380,10 @@ Int32 TUP_getShort (const void * pack, const char * name, Short * value, Bool is return ret; } -Int32 TUP_getUInt16 (const void * pack, const char * name, UInt16 * value, Bool is_require) +Int32 TUP_getUInt16(const void *pack, const char *name, UInt16 *value, Bool is_require) { JString *s = NULL; - const UniAttribute * attr; + const UniAttribute *attr; Int32 ret; ret = TUP_getAttributeV3(pack, name, &s, is_require); @@ -404,15 +401,12 @@ Int32 TUP_getUInt16 (const void * pack, const char * name, UInt16 * value, Bool return ret; } -Int32 TUP_getFloat (const void * pack, const char * name, Float* value, Bool is_require) +Int32 TUP_getFloat (const void *pack, const char *name, Float *value, Bool is_require) { JString *s = NULL; - const UniAttribute * attr; + const UniAttribute *attr; Int32 ret; - //s = JString_new(); - //if(!s) return TARS_MALLOC_ERROR; - ret = TUP_getAttributeV3(pack, name, &s, is_require); if (TARS_SUCCESS != ret) goto do_clean; @@ -428,15 +422,12 @@ Int32 TUP_getFloat (const void * pack, const char * name, Float* value, Bool is_ return ret; } -Int32 TUP_getDouble(const void * pack, const char * name, Double* value, Bool is_require) +Int32 TUP_getDouble(const void *pack, const char *name, Double *value, Bool is_require) { JString *s = NULL; - const UniAttribute * attr; + const UniAttribute *attr; Int32 ret; - //s = JString_new(); - //if(!s) return TARS_MALLOC_ERROR; - ret = TUP_getAttributeV3(pack, name, &s, is_require); if (TARS_SUCCESS != ret) goto do_clean; @@ -452,10 +443,10 @@ Int32 TUP_getDouble(const void * pack, const char * name, Double* value, Bool is return ret; } -Int32 TUP_getInt32 (const void * pack, const char * name, Int32* value, Bool is_require) +Int32 TUP_getInt32(const void *pack, const char *name, Int32 *value, Bool is_require) { JString *s = NULL; - const UniAttribute * attr; + const UniAttribute *attr; Int32 ret; ret = TUP_getAttributeV3(pack, name, &s, is_require); @@ -473,10 +464,10 @@ Int32 TUP_getInt32 (const void * pack, const char * name, Int32* value, Bool is return ret; } -Int32 TUP_getUInt32 (const void * pack, const char * name, UInt32 * value, Bool is_require) +Int32 TUP_getUInt32(const void *pack, const char *name, UInt32 *value, Bool is_require) { JString *s = NULL; - const UniAttribute * attr; + const UniAttribute *attr; Int32 ret; ret = TUP_getAttributeV3(pack, name, &s, is_require); @@ -494,10 +485,10 @@ Int32 TUP_getUInt32 (const void * pack, const char * name, UInt32 * value, Bool return ret; } -Int32 TUP_getInt64 (const void * pack, const char * name, Int64* value, Bool is_require) +Int32 TUP_getInt64(const void *pack, const char *name, Int64 *value, Bool is_require) { JString *s = NULL; - const UniAttribute * attr; + const UniAttribute *attr; Int32 ret; ret = TUP_getAttributeV3(pack, name, &s, is_require); @@ -516,15 +507,12 @@ Int32 TUP_getInt64 (const void * pack, const char * name, Int64* value, Bool is } -Int32 TUP_getString(const void * pack, const char * name, JString * value, Bool is_require) +Int32 TUP_getString(const void *pack, const char *name, JString *value, Bool is_require) { JString *s = NULL; - const UniAttribute * attr; + const UniAttribute *attr; Int32 ret; - //s = JString_new(); - //if(!s) return TARS_MALLOC_ERROR; - ret = TUP_getAttributeV3(pack, name, &s, is_require); if (TARS_SUCCESS != ret) goto do_clean; @@ -540,10 +528,10 @@ Int32 TUP_getString(const void * pack, const char * name, JString * value, Bool return ret; } -Int32 TUP_getVector(const void * pack, const char * name, JArray * value, Bool is_require) +Int32 TUP_getVector(const void *pack, const char *name, JArray *value, Bool is_require) { JString *s = NULL; - const UniAttribute * attr; + const UniAttribute *attr; char sType[64+1]={0}; Int32 ret; @@ -562,10 +550,10 @@ Int32 TUP_getVector(const void * pack, const char * name, JArray * value, Bool i return ret; } -Int32 TUP_getVectorChar(const void * pack, const char * name, JString * value, Bool is_require) +Int32 TUP_getVectorChar(const void *pack, const char *name, JString *value, Bool is_require) { JString *s = NULL; - const UniAttribute * attr; + const UniAttribute *attr; Int32 ret; ret = TUP_getAttributeV3(pack, name, &s, is_require); @@ -583,10 +571,10 @@ Int32 TUP_getVectorChar(const void * pack, const char * name, JString * value, B return ret; } -Int32 TUP_getMap (const void * pack, const char * name, JMapWrapper * value, Bool is_require) +Int32 TUP_getMap(const void *pack, const char *name, JMapWrapper *value, Bool is_require) { JString *s = NULL; - const UniAttribute * attr; + const UniAttribute *attr; Int32 ret; char sType[64+1]={0}; @@ -607,9 +595,9 @@ Int32 TUP_getMap (const void * pack, const char * name, JMapWrapper * value, B /////////////////////////////////////////////////////////////// -void UniAttribute_del(UniAttribute ** handle) +void UniAttribute_del(UniAttribute **handle) { - UniAttribute ** thiz = (UniAttribute **)handle; + UniAttribute **thiz = (UniAttribute **)handle; if (thiz == NULL || *thiz == NULL) return; if ((*handle)->_data) JMapWrapper_del(&((*handle)->_data)); @@ -624,7 +612,7 @@ void UniAttribute_del(UniAttribute ** handle) Int32 UniAttribute_init(UniAttribute *handle) { - UniAttribute * thiz = (UniAttribute*) handle; + UniAttribute *thiz = (UniAttribute*) handle; thiz->_data = JMapWrapper_new("string", "map>"); thiz->value_os = TarsOutputStream_new(); @@ -645,7 +633,7 @@ Int32 UniAttribute_init(UniAttribute *handle) } -UniAttribute * UniAttribute_new() +UniAttribute *UniAttribute_new() { Int32 ret=0; UniAttribute *thiz = (UniAttribute*)TarsMalloc(sizeof(UniAttribute)); @@ -663,7 +651,7 @@ UniAttribute * UniAttribute_new() /////////////////////////////////////////////////////////////// -Int32 UniPacket_encode(const UniPacket * pack, TarsOutputStream *os_tmp) +Int32 UniPacket_encode(const UniPacket *pack, TarsOutputStream *os_tmp) { Int32 ret; @@ -681,9 +669,9 @@ Int32 UniPacket_encode(const UniPacket * pack, TarsOutputStream *os_tmp) return ret; } -Int32 UniPacket_decode(UniPacket * pack, const char* buff, uint32_t len) +Int32 UniPacket_decode(UniPacket *pack, const char *buff, uint32_t len) { - TarsInputStream *is; + TarsInputStream *is; Int32 ret; if (len < sizeof(Int32)) @@ -723,9 +711,9 @@ Int32 UniPacket_decode(UniPacket * pack, const char* buff, uint32_t len) return ret; } -void UniPacket_del(UniPacket ** handle) +void UniPacket_del(UniPacket **handle) { - UniPacket ** thiz = (UniPacket**)handle; + UniPacket **thiz = (UniPacket**)handle; if (handle == NULL || *handle == NULL) return; if ((*thiz)->sServantName) JString_del(&(*thiz)->sServantName); @@ -749,7 +737,7 @@ Int32 UniPacket_init(UniPacket *handle) { Int32 ret; - UniPacket * thiz = (UniPacket*) handle; + UniPacket *thiz = (UniPacket*) handle; ret = UniAttribute_init((UniAttribute*)thiz); if (ret) return ret; @@ -781,7 +769,7 @@ Int32 UniPacket_init(UniPacket *handle) } -UniPacket * UniPacket_new() +UniPacket *UniPacket_new() { Int32 ret; UniPacket *thiz = (UniPacket*)TarsMalloc(sizeof(UniPacket)); @@ -809,17 +797,17 @@ void UniPacket_setRequestId(UniPacket *handle, Int32 value) handle->iRequestId = value; } -const char* UniPacket_getServantName(UniPacket *handle) +const char *UniPacket_getServantName(UniPacket *handle) { return JString_data(handle->sServantName); } -void UniPacket_setServantName(UniPacket *handle, const char*value) +void UniPacket_setServantName(UniPacket *handle, const char *value) { JString_assign(handle->sServantName, value, strlen((const char*)value)); } -const char* UniPacket_getFuncName(UniPacket *handle) +const char *UniPacket_getFuncName(UniPacket *handle) { if (handle != NULL) { return JString_data(handle->sFuncName); @@ -828,17 +816,17 @@ const char* UniPacket_getFuncName(UniPacket *handle) return NULL; } -void UniPacket_setFuncName(UniPacket *handle, const char*value) +void UniPacket_setFuncName(UniPacket *handle, const char *value) { JString_assign(handle->sFuncName, value, strlen((const char*)value)); } /* ====================== for php only ===================== */ -Int32 php_TUP_putStruct(void * pack, const char * name, void * st) +Int32 php_TUP_putStruct(void *pack, const char *name, void *st) { Int32 ret=0; - UniAttribute * attr = pack; - zval * clazz = st; + UniAttribute *attr = pack; + zval *clazz = st; TarsOutputStream_reset(attr->value_os); ret = struct_packer(NULL,attr->value_os, 0, clazz); @@ -848,14 +836,14 @@ Int32 php_TUP_putStruct(void * pack, const char * name, void * st) } -Int32 php_TUP_getStruct(void * pack, const char * name, void * st,void ** ret_val, Bool is_require) +Int32 php_TUP_getStruct(void *pack, const char *name, void *st,void **ret_val, Bool is_require) { Int32 ret=0; - UniAttribute * att = pack; - zval *struct_name, * clazz = st; + UniAttribute *att = pack; + zval *struct_name, *clazz = st; - JString * js = NULL; + JString *js = NULL; do { ret = TUP_getAttributeV3(att, name, &js, is_require); if (ret != TARS_SUCCESS) break; @@ -869,10 +857,10 @@ Int32 php_TUP_getStruct(void * pack, const char * name, void * st,void ** ret_va return ret; } -Int32 Unipacket_getStatus(UniPacket* unpack,JString *tmp) { +Int32 Unipacket_getStatus(UniPacket *unpack,JString *tmp) { Int32 ret = 0; - char * pBuff = NULL; + char *pBuff = NULL; uint32_t len = 0; TarsInputStream *is_string; @@ -905,10 +893,10 @@ Int32 Unipacket_getStatus(UniPacket* unpack,JString *tmp) { return ret; } -Int32 Unipacket_getDesc(UniPacket* unpack,JString **tmp) { +Int32 Unipacket_getDesc(UniPacket *unpack,JString **tmp) { Int32 ret = 0; - char * pBuff = NULL; + char *pBuff = NULL; uint32_t len = 0; TarsInputStream *is_string; @@ -943,7 +931,7 @@ Int32 Unipacket_getDesc(UniPacket* unpack,JString **tmp) { /*for response packet*/ -Int32 ResponsePacket_decode(ResponsePacket * pack, const char* buff, uint32_t len) +Int32 ResponsePacket_decode(ResponsePacket *pack, const char *buff, uint32_t len) { TarsInputStream *is; Int32 ret; @@ -975,9 +963,9 @@ Int32 ResponsePacket_decode(ResponsePacket * pack, const char* buff, uint32_t le return ret; } -void ResponsePacket_del(ResponsePacket ** handle) +void ResponsePacket_del(ResponsePacket **handle) { - ResponsePacket ** thiz = (ResponsePacket**)handle; + ResponsePacket **thiz = (ResponsePacket**)handle; if (handle == NULL || *handle == NULL) return; if ((*thiz)->sResultDesc) JString_del(&(*thiz)->sResultDesc); @@ -999,7 +987,7 @@ Int32 ResponsePacket_init(ResponsePacket *handle) { Int32 ret; - ResponsePacket * thiz = (ResponsePacket*) handle; + ResponsePacket *thiz = (ResponsePacket*) handle; ret = UniAttribute_init((UniAttribute*)thiz); if (ret) return ret; @@ -1025,7 +1013,7 @@ Int32 ResponsePacket_init(ResponsePacket *handle) return 0; } -ResponsePacket * ResponsePacket_new() +ResponsePacket *ResponsePacket_new() { Int32 ret; ResponsePacket *thiz = (ResponsePacket*)TarsMalloc(sizeof(ResponsePacket)); @@ -1040,7 +1028,7 @@ ResponsePacket * ResponsePacket_new() return thiz; } -Int32 ResponsePacket_encode(const ResponsePacket * rsp_pack, TarsOutputStream *os_tmp) +Int32 ResponsePacket_encode(const ResponsePacket *rsp_pack, TarsOutputStream *os_tmp) { Int32 ret; diff --git a/tupapi.c b/tupapi.c index a1fa78a..fec8baf 100644 --- a/tupapi.c +++ b/tupapi.c @@ -1,22 +1,3 @@ -/* - * - +----------------------------------------------------------------------+ - | PHP Version 5 | - +----------------------------------------------------------------------+ - | Copyright (c) 1997-2010 The PHP Group | - +----------------------------------------------------------------------+ - | This source file is subject to version 3.01 of the PHP license, | - | that is bundled with this package in the file LICENSE, and is | - | available through the world-wide-web at the following url: | - | http://www.php.net/license/3_01.txt | - | If you did not receive a copy of the PHP license and are unable to | - | obtain it through the world-wide-web, please send a note to | - | license@php.net so we can mail you a copy immediately. | - +----------------------------------------------------------------------+ - | Author: vinsonliu@tencent.com | - +----------------------------------------------------------------------+ -*/ - #ifdef HAVE_CONFIG_H #include "config.h" #endif @@ -33,49 +14,145 @@ #include "./include/ttars.h" -zend_class_entry * tup_ce; -zend_class_entry * tup_exception_ce; -zend_class_entry * tars_ce; -zend_class_entry * tars_struct_ce; -zend_class_entry * tars_vector_ce; -zend_class_entry * tars_map_ce; +zend_class_entry *tup_ce; +zend_class_entry *tup_exception_ce; +zend_class_entry *tars_ce; +zend_class_entry *tars_struct_ce; +zend_class_entry *tars_vector_ce; +zend_class_entry *tars_map_ce; +// https://www.laruence.com/2020/02/27/5213.html +// tup_exception __construct +ZEND_BEGIN_ARG_INFO_EX(tup_exception___construct_arginfo, 0, 0, 1) + ZEND_ARG_INFO(0, msg) + ZEND_ARG_INFO(0, code) +ZEND_END_ARG_INFO() +// tup putVector ZEND_BEGIN_ARG_INFO_EX(tup_put_vector_arginfo, 0, 0, 2) ZEND_ARG_INFO(0, name) ZEND_ARG_OBJ_INFO(0, clazz, \\TARS_Vector, 0) + ZEND_ARG_INFO(0, iVersion) ZEND_END_ARG_INFO() +// tup getVector ZEND_BEGIN_ARG_INFO_EX(tup_get_vector_arginfo, 0, 0, 3) ZEND_ARG_INFO(0, name) - ZEND_ARG_OBJ_INFO(1, clazz, \\TARS_Vector, 0) - ZEND_ARG_INFO(1, buf) + ZEND_ARG_OBJ_INFO(0, clazz, \\TARS_Vector, 0) + ZEND_ARG_INFO(0, buf) + ZEND_ARG_INFO(0, is_require) + ZEND_ARG_INFO(0, iVersion) ZEND_END_ARG_INFO() +// tup putMap ZEND_BEGIN_ARG_INFO_EX(tup_put_map_arginfo, 0, 0, 2) ZEND_ARG_INFO(0, name) ZEND_ARG_OBJ_INFO(0, clazz, \\TARS_Map, 0) + ZEND_ARG_INFO(0, iVersion) ZEND_END_ARG_INFO() +// tup getMap ZEND_BEGIN_ARG_INFO_EX(tup_get_map_arginfo, 0, 0, 3) ZEND_ARG_INFO(0, name) - ZEND_ARG_OBJ_INFO(1, clazz, \\TARS_Map, 0) - ZEND_ARG_INFO(1, buf) + ZEND_ARG_OBJ_INFO(0, clazz, \\TARS_Map, 0) + ZEND_ARG_INFO(0, buf) + ZEND_ARG_INFO(0, is_require) + ZEND_ARG_INFO(0, iVersion) +ZEND_END_ARG_INFO() + +// tup putStruct +ZEND_BEGIN_ARG_INFO_EX(tup_put_struct_arginfo, 0, 0, 2) + ZEND_ARG_INFO(0, name) + ZEND_ARG_OBJ_INFO(0, clazz, \\TARS_Struct, 0) + ZEND_ARG_INFO(0, iVersion) +ZEND_END_ARG_INFO() + +// tup getStruct +ZEND_BEGIN_ARG_INFO_EX(tup_get_struct_arginfo, 0, 0, 3) + ZEND_ARG_INFO(0, name) + ZEND_ARG_OBJ_INFO(0, clazz, \\TARS_Struct, 0) + ZEND_ARG_INFO(0, buf) + ZEND_ARG_INFO(0, is_require) + ZEND_ARG_INFO(0, iVersion) +ZEND_END_ARG_INFO() + +// tup encode +ZEND_BEGIN_ARG_INFO_EX(tup_encode_arginfo, 0, 0, 10) + ZEND_ARG_INFO(0, iVersion) + ZEND_ARG_INFO(0, iRequestId) + ZEND_ARG_INFO(0, servantName) + ZEND_ARG_INFO(0, funcName) + ZEND_ARG_INFO(0, cPacketType) + ZEND_ARG_INFO(0, iMessageType) + ZEND_ARG_INFO(0, iTimeout) + ZEND_ARG_ARRAY_INFO(0, contexts, 0) + ZEND_ARG_ARRAY_INFO(0, statuses, 0) + ZEND_ARG_ARRAY_INFO(0, inbuf_arr, 0) +ZEND_END_ARG_INFO() + +// tup decode +ZEND_BEGIN_ARG_INFO_EX(tup_decode_arginfo, 0, 0, 1) + ZEND_ARG_INFO(0, respBuffer) + ZEND_ARG_INFO(0, iVersion) ZEND_END_ARG_INFO() +// tup encodeRspPacket +ZEND_BEGIN_ARG_INFO_EX(tup_encode_rsp_packet_arginfo, 0, 0, 8) + ZEND_ARG_INFO(0, iVersion) + ZEND_ARG_INFO(0, cPacketType) + ZEND_ARG_INFO(0, iMessageType) + ZEND_ARG_INFO(0, iRequestId) + ZEND_ARG_INFO(0, iRet) + ZEND_ARG_INFO(0, sResultDesc) + ZEND_ARG_ARRAY_INFO(0, inbuf_arr, 0) + ZEND_ARG_ARRAY_INFO(0, statuses, 0) +ZEND_END_ARG_INFO() + +// tup decodeReqPacket +ZEND_BEGIN_ARG_INFO_EX(tup_decode_req_packet_arginfo, 0, 0, 1) + ZEND_ARG_INFO(0, respBuffer) +ZEND_END_ARG_INFO() + +// put common ZEND_BEGIN_ARG_INFO_EX(put_common_arginfo, 0, 0, 2) ZEND_ARG_INFO(0, name) ZEND_ARG_INFO(0, value) ZEND_ARG_INFO(0, iVersion) ZEND_END_ARG_INFO() +// get common ZEND_BEGIN_ARG_INFO_EX(get_common_arginfo, 0, 0, 2) ZEND_ARG_INFO(0, name) - ZEND_ARG_INFO(1, buf) + ZEND_ARG_INFO(0, buf) ZEND_ARG_INFO(0, is_require) ZEND_ARG_INFO(0, iVersion) ZEND_END_ARG_INFO() +// tup putString +ZEND_BEGIN_ARG_INFO_EX(tup_put_string_arginfo, 0, 0, 2) + ZEND_ARG_INFO(0, name) + ZEND_ARG_INFO(0, value) + ZEND_ARG_INFO(0, iVersion) +ZEND_END_ARG_INFO() + +// tup getString +ZEND_BEGIN_ARG_INFO_EX(tup_get_string_arginfo, 0, 0, 2) + ZEND_ARG_INFO(0, name) + ZEND_ARG_INFO(0, buf) + ZEND_ARG_INFO(0, is_require) + ZEND_ARG_INFO(0, iVersion) +ZEND_END_ARG_INFO() + +/* {{{ ttup_exception_methods[] + * + * Every user visible function must have an entry in ttup_functions[]. + */ +zend_function_entry tup_exception_methods[] = { + PHP_ME(tup_exception, __construct, tup_exception___construct_arginfo, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR) + {NULL, NULL, NULL} +}; +/* }}} */ + /* {{{ tup_methods[] * @@ -83,15 +160,16 @@ ZEND_END_ARG_INFO() */ zend_function_entry tup_methods[] = { // 编解码接口 - PHP_ME(tup,encode,NULL,ZEND_ACC_PUBLIC| ZEND_ACC_STATIC) - PHP_ME(tup,decode,NULL,ZEND_ACC_PUBLIC| ZEND_ACC_STATIC) + PHP_ME(tup, encode, tup_encode_arginfo, ZEND_ACC_PUBLIC| ZEND_ACC_STATIC) + PHP_ME(tup, decode, tup_decode_arginfo, ZEND_ACC_PUBLIC| ZEND_ACC_STATIC) + // 这两个是给server用的,反向的编解码接口 - PHP_ME(tup,encodeRspPacket,NULL,ZEND_ACC_PUBLIC| ZEND_ACC_STATIC) + PHP_ME(tup, encodeRspPacket, tup_encode_rsp_packet_arginfo, ZEND_ACC_PUBLIC| ZEND_ACC_STATIC) // 这个接口未必需要啦,看情况 - PHP_ME(tup,decodeReqPacket,NULL,ZEND_ACC_PUBLIC| ZEND_ACC_STATIC) + PHP_ME(tup, decodeReqPacket, tup_decode_req_packet_arginfo, ZEND_ACC_PUBLIC| ZEND_ACC_STATIC) - PHP_ME(tup, putStruct, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - PHP_ME(tup, getStruct, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + PHP_ME(tup, putStruct, tup_put_struct_arginfo, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + PHP_ME(tup, getStruct, tup_get_struct_arginfo, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) PHP_ME(tup, putBool, put_common_arginfo, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) PHP_ME(tup, getBool, get_common_arginfo, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) @@ -113,33 +191,22 @@ zend_function_entry tup_methods[] = { PHP_ME(tup, getDouble, get_common_arginfo, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) PHP_ME(tup, putFloat, put_common_arginfo, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) PHP_ME(tup, getFloat, get_common_arginfo, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - PHP_ME(tup, putString, put_common_arginfo, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - PHP_ME(tup, getString, get_common_arginfo, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + PHP_ME(tup, putString, tup_put_string_arginfo, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + PHP_ME(tup, getString, tup_get_string_arginfo, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) PHP_ME(tup, putVector, tup_put_vector_arginfo, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) PHP_ME(tup, getVector, tup_get_vector_arginfo, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) PHP_ME(tup, putMap, tup_put_map_arginfo, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) PHP_ME(tup, getMap, tup_get_map_arginfo, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) {NULL, NULL, NULL} }; - -/* }}} */ - -/* {{{ ttup_exception_methods[] - * - * Every user visible function must have an entry in ttup_functions[]. - */ -zend_function_entry tup_exception_methods[] = { - PHP_ME(tup_exception, __construct, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR) - {NULL, NULL, NULL} -}; /* }}} */ /* {{{ ttup_module_entry */ zend_module_entry phptars_module_entry = { -#if ZEND_MODULE_API_NO >= 20010901 - STANDARD_MODULE_HEADER, -#endif + STANDARD_MODULE_HEADER_EX, + NULL, + NULL, "phptars", tup_methods, PHP_MINIT(phptars), @@ -147,9 +214,7 @@ zend_module_entry phptars_module_entry = { NULL, NULL, PHP_MINFO(phptars), -#if ZEND_MODULE_API_NO >= 20010901 - "0.2", /* Replace with version number for your extension */ -#endif + PHP_TARS_VERSION, STANDARD_MODULE_PROPERTIES }; /* }}} */ @@ -166,11 +231,11 @@ PHP_MINIT_FUNCTION(phptars) // tup INIT_CLASS_ENTRY(ce, "TUPAPI", tup_methods); - tup_ce = zend_register_internal_class(&ce TSRMLS_CC); + tup_ce = zend_register_internal_class(&ce); // tup_exception INIT_CLASS_ENTRY(ce, "TARS_Exception", tup_exception_methods); - tup_exception_ce = my_zend_register_internal_class_ex(&ce, zend_exception_get_default(TSRMLS_C), NULL TSRMLS_CC); + tup_exception_ce = my_zend_register_internal_class_ex(&ce, zend_exception_get_default(), NULL); TUP_STARTUP(ttars); @@ -202,9 +267,9 @@ PHP_MINFO_FUNCTION(phptars) } /* }}} */ -/* {{{ tup_throw_exception(int err_code, char * fmt, ...) +/* {{{ tup_throw_exception(int err_code, char *fmt, ...) */ -void tup_throw_exception(long err_code, char * fmt, ...) { +void tup_throw_exception(long err_code, char *fmt, ...) { va_list args; char *err_msg; @@ -213,27 +278,28 @@ void tup_throw_exception(long err_code, char * fmt, ...) { vspprintf(&err_msg, 128, fmt, args); va_end(args); - zend_throw_exception(tup_exception_ce, err_msg, err_code TSRMLS_CC); + zend_throw_exception(tup_exception_ce, err_msg, err_code); efree(err_msg); } /* }}} */ PHP_METHOD(tup_exception, __construct) { - char * msg; + char *msg; long code; - zend_size_t msg_len; - zval * self; + size_t msg_len; + zval *self; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &msg, &msg_len, &code) == FAILURE) { - php_error_docref(NULL TSRMLS_CC, E_ERROR, "Wrong parameters for TARS_Exception([string $exception [, long $code]])"); - return ; - } + ZEND_PARSE_PARAMETERS_START(1, 2) + Z_PARAM_STRING(msg, msg_len) + Z_PARAM_OPTIONAL + Z_PARAM_LONG(code) + ZEND_PARSE_PARAMETERS_END(); self = getThis(); - zend_update_property_string(Z_OBJCE_P(self), self, ZEND_STRL("message"), msg TSRMLS_CC); + zend_update_property_string(Z_OBJCE_P(self), Z_OBJ_P(self), ZEND_STRL("message"), msg); if (code) { - zend_update_property_long(Z_OBJCE_P(self), self, ZEND_STRL("code"), code TSRMLS_CC); + zend_update_property_long(Z_OBJCE_P(self), Z_OBJ_P(self), ZEND_STRL("code"), code); } } /* }}} */ @@ -241,18 +307,20 @@ PHP_METHOD(tup_exception, __construct) { /* {{{ __tup_PUT */ #define __TUP_PUT(type, packer) do { \ - char * name, * buf = NULL; \ + char *name, *buf; \ int ret; \ - zend_size_t name_len; \ + size_t name_len; \ uint32_t len; \ - zval * value; \ + zval *value; \ long iVersion=3; \ type dest; \ - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz|l", &name, &name_len, &value, &iVersion) == FAILURE) { \ - ZEND_WRONG_PARAM_COUNT(); \ - return ; \ - } \ - UniAttribute * att = UniAttribute_new(); \ + ZEND_PARSE_PARAMETERS_START(2, 3) \ + Z_PARAM_STRING(name, name_len) \ + Z_PARAM_ZVAL(value) \ + Z_PARAM_OPTIONAL \ + Z_PARAM_LONG(iVersion) \ + ZEND_PARSE_PARAMETERS_END(); \ + UniAttribute *att = UniAttribute_new(); \ if (!att) {return MALLOC_EXCEPTION(#type);} \ if(3 == iVersion) { \ ret = packer ## _packer(value, NULL, 0, (void *)&dest); \ @@ -289,16 +357,19 @@ PHP_METHOD(tup_exception, __construct) { /* {{{ __TUP_GET */ #define __TUP_GET(type, dest) do { \ - char * name, *buf; \ + char *name, *buf; \ long iVersion=3;\ zend_bool is_require=0;\ int ret; \ - zend_size_t name_len, len; \ - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|bl", &name, &name_len, &buf, &len, &is_require, &iVersion) == FAILURE) { \ - ZEND_WRONG_PARAM_COUNT(); \ - return ; \ - } \ - UniAttribute * att = UniAttribute_new(); \ + size_t name_len, len; \ + ZEND_PARSE_PARAMETERS_START(2, 4) \ + Z_PARAM_STRING(name, name_len) \ + Z_PARAM_STRING(buf, len) \ + Z_PARAM_OPTIONAL \ + Z_PARAM_BOOL(is_require) \ + Z_PARAM_LONG(iVersion) \ + ZEND_PARSE_PARAMETERS_END(); \ + UniAttribute *att = UniAttribute_new(); \ if (!att) {MALLOC_EXCEPTION(#type); return ;}\ /* 解码 */ \ if(3 == iVersion) { \ @@ -335,23 +406,30 @@ PHP_METHOD(tup_exception, __construct) { /** * 通用编码函数 */ -PHP_METHOD(tup,encode) { - char * servantName, *funcName; +PHP_METHOD(tup, encode) { + char *servantName, *funcName; long iVersion, iRequestId, cPacketType, iMessageType, iTimeout; uint32_t outBuffLen; char *outBuff = NULL; - zend_size_t servantLen, funcLen; + size_t servantLen, funcLen; zval *inbuf_arr; - zval * contexts; - zval * statuses; + zval *contexts; + zval *statuses; int ret; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "llsslllaaa", &iVersion, &iRequestId,&servantName,&servantLen, &funcName,&funcLen - ,&cPacketType,&iMessageType,&iTimeout,&contexts,&statuses,&inbuf_arr) == FAILURE) { - ZEND_WRONG_PARAM_COUNT(); - return ; - } + ZEND_PARSE_PARAMETERS_START(10, 10) + Z_PARAM_LONG(iVersion) + Z_PARAM_LONG(iRequestId) + Z_PARAM_STRING(servantName, servantLen) + Z_PARAM_STRING(funcName, funcLen) + Z_PARAM_LONG(cPacketType) + Z_PARAM_LONG(iMessageType) + Z_PARAM_LONG(iTimeout) + Z_PARAM_ARRAY(contexts) + Z_PARAM_ARRAY(statuses) + Z_PARAM_ARRAY(inbuf_arr) + ZEND_PARSE_PARAMETERS_END(); /* 从buf数组中读出一个个的buffer,每个buffer就对应了输入的一个参数和它的值 / 这是一个map的结构打成char之后的成果,tup_unipackert要做的事情就是把它放入正确的 @@ -374,56 +452,9 @@ PHP_METHOD(tup,encode) { } // 遍历buf数组,顺序看起来没关系,太赞了。 - JMapWrapper* map_wrapper = JMapWrapper_new("string", "list"); + JMapWrapper *map_wrapper = JMapWrapper_new("string", "list"); if(iVersion == 3) { -#if PHP_MAJOR_VERSION < 7 - HashTable *inbuf_ht = Z_ARRVAL_P(inbuf_arr); - for ( - zend_hash_internal_pointer_reset(inbuf_ht); - zend_hash_has_more_elements(inbuf_ht) == SUCCESS; - zend_hash_move_forward(inbuf_ht) - ) { - char *key; - uint key_len; - zval ** inbuf_iter; - long index; - if (zend_hash_get_current_key_ex(inbuf_ht, &key, &key_len, index, 0, NULL) == HASH_KEY_IS_STRING) { - if (zend_hash_get_current_data(inbuf_ht, (void **)&inbuf_iter) == FAILURE) { - continue; - } else { - // 针对每一个buf,现在已经获取了key->buf这样的一个键值对 - char *inbuf_val; - uint32_t inbuf_len; - convert_to_string(*inbuf_iter); - inbuf_val = Z_STRVAL_PP(inbuf_iter); - inbuf_len = Z_STRLEN_PP(inbuf_iter); - - TarsOutputStream_reset(os_tmp); - TarsOutputStream_reset(os_map); - - ret = TarsOutputStream_writeStringBuffer(os_tmp, key, strlen(key), 0); - if (ret) { - ENCODE_BUF_EXCEPTION(); - goto do_clean; - } - - ret = TarsOutputStream_writeVectorCharBuffer(os_map, inbuf_val, inbuf_len, 1); - if (ret) { - ENCODE_BUF_EXCEPTION(); - goto do_clean; - } - - ret = JMapWrapper_put(map_wrapper, TarsOutputStream_getBuffer(os_tmp), TarsOutputStream_getLength(os_tmp), TarsOutputStream_getBuffer(os_map), TarsOutputStream_getLength(os_map)); - if (ret) { - ENCODE_BUF_EXCEPTION(); - goto do_clean; - } - } - } - } -#else - //PHP7 zend_string *zkey; zval *inbuf_iter; ulong num_key; @@ -464,51 +495,12 @@ PHP_METHOD(tup,encode) { } }ZEND_HASH_FOREACH_END(); -#endif TarsOutputStream_reset(os_tmp); TarsOutputStream_writeMap(os_tmp,map_wrapper,0); } else if (iVersion == 1) { -#if PHP_MAJOR_VERSION < 7 - HashTable *inbuf_ht = Z_ARRVAL_P(inbuf_arr); - for ( - zend_hash_internal_pointer_reset(inbuf_ht); - zend_hash_has_more_elements(inbuf_ht) == SUCCESS; - zend_hash_move_forward(inbuf_ht) - ) { - char *key; - uint key_len; - zval ** inbuf_iter; - long cur_tag; - - if (zend_hash_get_current_key_ex(inbuf_ht, &key, &key_len, &cur_tag, 0, NULL) == HASH_KEY_IS_STRING) { - if (zend_hash_get_current_data(inbuf_ht, (void **)&inbuf_iter) == FAILURE) { - continue; - } else { - cur_tag = atoi(key); - } - } - else { - if (zend_hash_get_current_data(inbuf_ht, (void **)&inbuf_iter) == FAILURE) - continue; - } - // 针对每一个buf,现在已经获取了key->buf这样的一个键值对 - char *inbuf_val; - uint32_t inbuf_len; - convert_to_string(*inbuf_iter); - inbuf_val = Z_STRVAL_PP(inbuf_iter); - inbuf_len = Z_STRLEN_PP(inbuf_iter); - - ret = JString_append(os_tmp->_buf, inbuf_val, inbuf_len); - if (ret) { - ENCODE_BUF_EXCEPTION(); - goto do_clean; - } - } -#else - //PHP7 zend_string *zkey; zval *inbuf_iter; ulong num_key; @@ -533,12 +525,11 @@ PHP_METHOD(tup,encode) { } }ZEND_HASH_FOREACH_END(); -#endif } // 将mapwrapper进行uniattribute的encode使之成为符合需求的字符串 // 设置tup包初始化参数 - UniPacket* pack = UniPacket_new(); + UniPacket *pack = UniPacket_new(); pack->iVersion = iVersion; pack->cPacketType = (char) cPacketType; pack->iMessageType = iMessageType; @@ -558,52 +549,7 @@ PHP_METHOD(tup,encode) { // 如果设置了context if(NULL != contexts) { HashTable *contextsHt= Z_ARRVAL_P(contexts); -#if PHP_MAJOR_VERSION < 7 - for ( - zend_hash_internal_pointer_reset(contextsHt); - zend_hash_has_more_elements(contextsHt) == SUCCESS; - zend_hash_move_forward(contextsHt) - ) { - char *key; - uint keyLen; - zval ** contextsIter; - TarsOutputStream_reset(context_key_tmp); - TarsOutputStream_reset(context_value_tmp); - - - if (zend_hash_get_current_key_ex(contextsHt, &key, &keyLen, NULL, 0, NULL) == HASH_KEY_IS_STRING) { - if (zend_hash_get_current_data(contextsHt, (void **)&contextsIter) == FAILURE) { - continue; - } else { - char *contextVal; - uint32_t contextLen; - convert_to_string(*contextsIter); - contextVal = Z_STRVAL_PP(contextsIter); - contextLen = Z_STRLEN_PP(contextsIter); - - ret = TarsOutputStream_writeStringBuffer(context_key_tmp, key, strlen(key), 0); - if (ret) { - ENCODE_BUF_EXCEPTION(); - goto do_clean; - } - - ret = TarsOutputStream_writeStringBuffer(context_value_tmp, contextVal, contextLen, 1); - if (ret) { - ENCODE_BUF_EXCEPTION(); - goto do_clean; - } - - ret = JMapWrapper_put(pack->context, TarsOutputStream_getBuffer(context_key_tmp), TarsOutputStream_getLength(context_key_tmp), - TarsOutputStream_getBuffer(context_value_tmp), TarsOutputStream_getLength(context_value_tmp)); - if(ret) { - TUP_SET_CONTEXT_EXCEPTION(); - goto do_clean; - } - } - } - } -#else -//PHP7 + zend_string *zkey; zval *contextsIter; ulong num_key; @@ -622,8 +568,6 @@ PHP_METHOD(tup,encode) { convert_to_string(contextsIter); contextVal = Z_STRVAL_P(contextsIter); contextLen = Z_STRLEN_P(contextsIter); - TarsOutputStream_reset(context_key_tmp); - TarsOutputStream_reset(context_value_tmp); ret = TarsOutputStream_writeStringBuffer(context_key_tmp, key, strlen(key), 0); if (ret) { @@ -645,60 +589,13 @@ PHP_METHOD(tup,encode) { } } ZEND_HASH_FOREACH_END(); -#endif } // 如果设置了status if(NULL != statuses) { + TarsOutputStream_reset(context_key_tmp); + TarsOutputStream_reset(context_value_tmp); -#if PHP_MAJOR_VERSION < 7 - HashTable *statusesHt= Z_ARRVAL_P(statuses); - for ( - zend_hash_internal_pointer_reset(statusesHt); - zend_hash_has_more_elements(statusesHt) == SUCCESS; - zend_hash_move_forward(statusesHt) - ) { - char *key; - uint keyLen; - zval ** statusesIter; - TarsOutputStream_reset(context_key_tmp); - TarsOutputStream_reset(context_value_tmp); - - if (zend_hash_get_current_key_ex(statusesHt, &key, &keyLen, NULL, 0, NULL) == HASH_KEY_IS_STRING) { - if (zend_hash_get_current_data(statusesHt, (void **)&statusesIter) == FAILURE) { - continue; - } else { - char *statusVal; - uint32_t statusLen; - convert_to_string(*statusesIter); - statusVal = Z_STRVAL_PP(statusesIter); - statusLen = Z_STRLEN_PP(statusesIter); - - - ret = TarsOutputStream_writeStringBuffer(context_key_tmp, key, strlen(key), 0); - if (ret) { - ENCODE_BUF_EXCEPTION(); - goto do_clean; - } - - ret = TarsOutputStream_writeStringBuffer(context_value_tmp, statusVal, statusLen, 1); - if (ret) { - ENCODE_BUF_EXCEPTION(); - goto do_clean; - } - - ret = JMapWrapper_put(pack->status, TarsOutputStream_getBuffer(context_key_tmp), TarsOutputStream_getLength(context_key_tmp), - TarsOutputStream_getBuffer(context_value_tmp), TarsOutputStream_getLength(context_value_tmp)); - - if(ret) { - TUP_SET_STATUS_EXCEPTION(); - goto do_clean; - } - } - } - } -#else - //PHP7 zend_string *zkey; zval *statusesIter; ulong num_key; @@ -714,8 +611,6 @@ PHP_METHOD(tup,encode) { convert_to_string(statusesIter); statusVal = Z_STRVAL_P(statusesIter); statusLen = Z_STRLEN_P(statusesIter); - TarsOutputStream_reset(context_key_tmp); - TarsOutputStream_reset(context_value_tmp); ret = TarsOutputStream_writeStringBuffer(context_key_tmp, key, strlen(key), 0); if (ret) { @@ -738,7 +633,6 @@ PHP_METHOD(tup,encode) { } } ZEND_HASH_FOREACH_END(); -#endif } if (context_key_tmp) TarsOutputStream_del(&context_key_tmp); if (context_value_tmp) TarsOutputStream_del(&context_value_tmp); @@ -758,18 +652,6 @@ PHP_METHOD(tup,encode) { TarsOutputStream_reset(os_tmp); - /*ret = TarsOutputStream_writeShort(os_tmp, pack->iVersion, 1); if (TARS_SUCCESS != ret) goto do_clean; - ret = TarsOutputStream_writeChar(os_tmp, pack->cPacketType, 2); if (TARS_SUCCESS != ret) goto do_clean; - ret = TarsOutputStream_writeInt32(os_tmp, pack->iMessageType, 3); if (TARS_SUCCESS != ret) goto do_clean; - ret = TarsOutputStream_writeInt32(os_tmp, pack->iRequestId, 4); if (TARS_SUCCESS != ret)goto do_clean; - ret = TarsOutputStream_writeString(os_tmp, pack->sServantName, 5); if (TARS_SUCCESS != ret) goto do_clean; - ret = TarsOutputStream_writeString(os_tmp, pack->sFuncName, 6); if (TARS_SUCCESS != ret) goto do_clean; - ret = TarsOutputStream_writeVectorChar(os_tmp, pack->sBuffer, 7); if (TARS_SUCCESS != ret) goto do_clean; - ret = TarsOutputStream_writeInt32(os_tmp, pack->iTimeout, 8); if (TARS_SUCCESS != ret) goto do_clean; - ret = TarsOutputStream_writeMap(os_tmp, pack->context, 9); if (TARS_SUCCESS != ret) goto do_clean; - ret = TarsOutputStream_writeMap(os_tmp, pack->status, 10); if (TARS_SUCCESS != ret) goto do_clean; - */ - ret = UniPacket_encode(pack, os_tmp); if (TARS_SUCCESS != ret) { @@ -804,18 +686,23 @@ PHP_METHOD(tup,encodeRspPacket) { long iVersion,cPacketType,iMessageType, iRequestId, iRet; uint32_t outBuffLen; char *outBuff = NULL; - char * sResultDesc; - zend_size_t sResultDescLen; + char *sResultDesc; + size_t sResultDescLen; zval *inbuf_arr; - zval * statuses; + zval *statuses; int ret; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lllllsaa", &iVersion,&cPacketType,&iMessageType, - &iRequestId,&iRet,&sResultDesc,&sResultDescLen,&inbuf_arr,&statuses) == FAILURE) { - ZEND_WRONG_PARAM_COUNT(); - return ; - } + ZEND_PARSE_PARAMETERS_START(8, 8) + Z_PARAM_LONG(iVersion) + Z_PARAM_LONG(cPacketType) + Z_PARAM_LONG(iMessageType) + Z_PARAM_LONG(iRequestId) + Z_PARAM_LONG(iRet) + Z_PARAM_STRING(sResultDesc, sResultDescLen) + Z_PARAM_ARRAY(inbuf_arr) + Z_PARAM_ARRAY(statuses) + ZEND_PARSE_PARAMETERS_END(); /* 从buf数组中读出一个个的buffer,每个buffer就对应了输入的一个参数和它的值 / 这是一个map的结构打成char之后的成果,tup_unipackert要做的事情就是把它放入正确的 @@ -838,44 +725,6 @@ PHP_METHOD(tup,encodeRspPacket) { } - #if PHP_MAJOR_VERSION < 7 - HashTable *inbuf_ht = Z_ARRVAL_P(inbuf_arr); - for ( - zend_hash_internal_pointer_reset(inbuf_ht); - zend_hash_has_more_elements(inbuf_ht) == SUCCESS; - zend_hash_move_forward(inbuf_ht) - ) { - char *key; - uint key_len; - zval ** inbuf_iter; - long cur_tag; - - if (zend_hash_get_current_key_ex(inbuf_ht, &key, &key_len, &cur_tag, 0, NULL) == HASH_KEY_IS_STRING) { - if (zend_hash_get_current_data(inbuf_ht, (void **)&inbuf_iter) == FAILURE) { - continue; - } else { - cur_tag = atoi(key); - } - } - else { - if (zend_hash_get_current_data(inbuf_ht, (void **)&inbuf_iter) == FAILURE) - continue; - } - // 针对每一个buf,现在已经获取了key->buf这样的一个键值对 - char *inbuf_val; - uint32_t inbuf_len; - convert_to_string(*inbuf_iter); - inbuf_val = Z_STRVAL_PP(inbuf_iter); - inbuf_len = Z_STRLEN_PP(inbuf_iter); - - ret = JString_append(os_tmp->_buf, inbuf_val, inbuf_len); - if (ret) { - ENCODE_BUF_EXCEPTION(); - goto do_clean; - } - } - #else - //PHP7 zend_string *zkey; zval *inbuf_iter; ulong num_key; @@ -900,10 +749,9 @@ PHP_METHOD(tup,encodeRspPacket) { } }ZEND_HASH_FOREACH_END(); - #endif // 设置tup包初始化参数 - ResponsePacket* rsp_pack = ResponsePacket_new(); + ResponsePacket *rsp_pack = ResponsePacket_new(); // 拷贝进入sBuffer ret = JString_assign(rsp_pack->sBuffer, TarsOutputStream_getBuffer(os_tmp), TarsOutputStream_getLength(os_tmp)); @@ -916,7 +764,7 @@ PHP_METHOD(tup,encodeRspPacket) { rsp_pack->cPacketType = cPacketType; rsp_pack->iMessageType = iMessageType; rsp_pack->iRequestId = iRequestId; - rsp_pack->iRet = iRet; + rsp_pack->iRet = iRet; JString_assign(rsp_pack->sResultDesc, sResultDesc, sResultDescLen); TarsOutputStream *key_tmp = TarsOutputStream_new(); @@ -935,53 +783,6 @@ PHP_METHOD(tup,encodeRspPacket) { TarsOutputStream_reset(key_tmp); TarsOutputStream_reset(value_tmp); - #if PHP_MAJOR_VERSION < 7 - HashTable *statusesHt= Z_ARRVAL_P(statuses); - for ( - zend_hash_internal_pointer_reset(statusesHt); - zend_hash_has_more_elements(statusesHt) == SUCCESS; - zend_hash_move_forward(statusesHt) - ) { - char *key; - uint keyLen; - zval ** statusesIter; - long index; - - if (zend_hash_get_current_key_ex(statusesHt, &key, &keyLen, &index, 0, NULL) == HASH_KEY_IS_STRING) { - if (zend_hash_get_current_data(statusesHt, (void **)&statusesIter) == FAILURE) { - continue; - } else { - char *statusVal; - uint32_t statusLen; - convert_to_string(*statusesIter); - statusVal = Z_STRVAL_PP(statusesIter); - statusLen = Z_STRLEN_PP(statusesIter); - - - ret = TarsOutputStream_writeStringBuffer(key_tmp, key, strlen(key), 0); - if (ret) { - ENCODE_BUF_EXCEPTION(); - goto do_clean; - } - - ret = TarsOutputStream_writeStringBuffer(value_tmp, statusVal, statusLen, 1); - if (ret) { - ENCODE_BUF_EXCEPTION(); - goto do_clean; - } - - ret = JMapWrapper_put(rsp_pack->status, TarsOutputStream_getBuffer(key_tmp), TarsOutputStream_getLength(key_tmp), - TarsOutputStream_getBuffer(value_tmp), TarsOutputStream_getLength(value_tmp)); - - if(ret) { - TUP_SET_STATUS_EXCEPTION(); - goto do_clean; - } - } - } - } - #else - //PHP7 zend_string *zkey; zval *statusesIter; ulong num_key; @@ -1019,7 +820,6 @@ PHP_METHOD(tup,encodeRspPacket) { } } ZEND_HASH_FOREACH_END(); - #endif } if (key_tmp) TarsOutputStream_del(&key_tmp); if (value_tmp) TarsOutputStream_del(&value_tmp); @@ -1062,17 +862,17 @@ PHP_METHOD(tup,encodeRspPacket) { PHP_METHOD(tup,decode) { char *outBuff = NULL; - char * respBuffer; - zend_size_t respBufferLen; + char *respBuffer; + size_t respBufferLen; int ret; zval *ret_val; long iVersion = 3; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l",&respBuffer, - &respBufferLen,&iVersion) == FAILURE) { - ZEND_WRONG_PARAM_COUNT(); - return ; - } + ZEND_PARSE_PARAMETERS_START(1, 2) + Z_PARAM_STRING(respBuffer, respBufferLen) + Z_PARAM_OPTIONAL + Z_PARAM_LONG(iVersion) + ZEND_PARSE_PARAMETERS_END(); if(3 == iVersion) { UniPacket *unpack = UniPacket_new(); @@ -1082,7 +882,7 @@ PHP_METHOD(tup,decode) { // 获取返回码 int status; - JString * tmp; + JString *tmp; tmp = JString_new(); array_init(return_value); @@ -1213,16 +1013,15 @@ PHP_METHOD(tup,decodeReqPacket) { char *outBuff = NULL; - char * respBuffer; - zend_size_t respBufferLen; + char *respBuffer; + size_t respBufferLen; int ret; zval *ret_val; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s",&respBuffer, - &respBufferLen) == FAILURE) { - ZEND_WRONG_PARAM_COUNT(); - return ; - } + ZEND_PARSE_PARAMETERS_START(1, 1) + Z_PARAM_STRING(respBuffer, respBufferLen) + ZEND_PARSE_PARAMETERS_END(); + // requestPacket和UniPacket的结构完全一样,只是另一个别称 UniPacket *unpack = UniPacket_new(); @@ -1259,13 +1058,17 @@ PHP_METHOD(tup,decodeReqPacket) { if(3 == iVersion) { TarsOutputStream_writeMap(os,((UniAttribute *)unpack)->m_data,0); + len = TarsOutputStream_getLength(os); outBuff = TarsMalloc(len); memcpy(outBuff, TarsOutputStream_getBuffer(os), TarsOutputStream_getLength(os)); + my_add_assoc_stringl(return_value,"sBuffer",outBuff, len, 1); + if(outBuff) TarsFree(outBuff); + } else { len = JString_size(unpack->sBuffer); @@ -1275,69 +1078,14 @@ PHP_METHOD(tup,decodeReqPacket) { my_add_assoc_stringl(return_value,"sBuffer",outBuff, len, 1); if(outBuff) TarsFree(outBuff); - } - - // decode context - zval * context_zval; - ALLOC_INIT_ZVAL(context_zval); - array_init(context_zval); - - JMapWrapper * context = unpack->context; - int context_size = JMapWrapper_size(context); - - int index; - for (index = 0; index < context_size; ++index) - { - uint32_t context_key_len = JArray_getLength(context->first, index); - char * context_key = JArray_getPtr(context->first, index); - - uint32_t context_value_len = JArray_getLength(context->second, index); - char * context_value = JArray_getPtr(context->second, index); - - int context_key_len_unpacked = Tars_readStringLen(context_key); - char * context_key_unpacked = TarsMalloc(context_key_len_unpacked); - Tars_readString(context_key, context_key_unpacked); - - int context_value_len_unpacked = Tars_readStringLen(context_value); - char * context_value_unpacked = TarsMalloc(context_value_len_unpacked); - Tars_readString(context_value, context_value_unpacked); - - add_assoc_stringl_ex(context_zval, context_key_unpacked, context_key_len_unpacked, context_value_unpacked, context_value_len_unpacked); - } - add_assoc_zval(return_value, "context", context_zval); - - // decode status - zval * status_zval; - ALLOC_INIT_ZVAL(status_zval); - array_init(status_zval); - - JMapWrapper * status = unpack->status; - int status_size = JMapWrapper_size(status); - - for (index = 0; index < status_size; ++index) - { - uint32_t status_key_len = JArray_getLength(status->first, index); - char * status_key = JArray_getPtr(status->first, index); - - uint32_t status_value_len = JArray_getLength(status->second, index); - char * status_value = JArray_getPtr(status->second, index); - - int status_key_len_unpacked = Tars_readStringLen(status_key); - char * status_key_unpacked = TarsMalloc(status_key_len_unpacked); - Tars_readString(status_key, status_key_unpacked); - - int status_value_len_unpacked = Tars_readStringLen(status_value); - char * status_value_unpacked = TarsMalloc(status_value_len_unpacked); - Tars_readString(status_value, status_value_unpacked); - - add_assoc_stringl_ex(status_zval, status_key_unpacked, status_key_len_unpacked, status_value_unpacked, status_value_len_unpacked); } - add_assoc_zval(return_value, "status", status_zval); + if(os) TarsOutputStream_del(&os); if(unpack) UniPacket_del(&unpack); + return; } @@ -1367,7 +1115,6 @@ PHP_METHOD(tup, putChar) { /* {{{ TTUP::getChar(string $name, string $buf) */ PHP_METHOD(tup, getChar) { - Char b = 0; __TUP_GET(Char, &b); MY_RETURN_STRINGL(&b, 1, 1); @@ -1384,7 +1131,6 @@ PHP_METHOD(tup, putUInt8) { /* {{{ TTUP::getUInt8(string $name, string $buf) */ PHP_METHOD(tup, getUInt8) { - UInt8 i = 0; __TUP_GET(UInt8, &i); RETURN_LONG(i); @@ -1401,7 +1147,6 @@ PHP_METHOD(tup, putShort) { /* {{{ TTUP::getShort(string $name, string $buf) */ PHP_METHOD(tup, getShort) { - Short i = 0; __TUP_GET(Short, &i); RETURN_LONG(i); @@ -1418,7 +1163,6 @@ PHP_METHOD(tup, putUInt16) { /* {{{ TTUP::getUInt16(string $name, string $buf) */ PHP_METHOD(tup, getUInt16) { - UInt16 i = 0; __TUP_GET(UInt16, &i); RETURN_LONG(i); @@ -1435,7 +1179,6 @@ PHP_METHOD(tup, putInt32) { /* {{{ TTUP::getInt32(string $name, string $buf) */ PHP_METHOD(tup, getInt32) { - Int32 i = 0; __TUP_GET(Int32, &i); RETURN_LONG(i); @@ -1462,7 +1205,7 @@ PHP_METHOD(tup, putUInt32) { type i = 0; \ __TUP_GET(type, &i); \ if (i >= LONG_MIN && i <= LONG_MAX) { \ - RETURN_LONG(i); \ + RETURN_LONG(i); } else { \ char ll[32]; \ int len; \ @@ -1475,7 +1218,6 @@ PHP_METHOD(tup, putUInt32) { /* {{{ TTUP::getInt32(string $name, string $buf) */ PHP_METHOD(tup, getUInt32) { - __TUP_GET_BIG_INT(UInt32) } /* }}} */ @@ -1490,7 +1232,6 @@ PHP_METHOD(tup, putInt64) { /* {{{ TTUP::getInt32(string $name, string $buf) */ PHP_METHOD(tup, getInt64) { - __TUP_GET_BIG_INT(Int64) } /* }}} */ @@ -1505,7 +1246,6 @@ PHP_METHOD(tup, putDouble) { /* {{{ TTUP::getDouble(string $name, string $buf) */ PHP_METHOD(tup, getDouble) { - Double i = 0; __TUP_GET(Double, &i); RETURN_DOUBLE(i); @@ -1522,7 +1262,6 @@ PHP_METHOD(tup, putFloat) { /* {{{ TTUP::getFloat(string $name, string $buf) */ PHP_METHOD(tup, getFloat) { - Float i = 0; __TUP_GET(Float, &i); RETURN_DOUBLE(i); @@ -1532,20 +1271,21 @@ PHP_METHOD(tup, getFloat) { /* {{{ TTUP::putString(string $name, mixed $value) */ PHP_METHOD(tup, putString) { - - char * name, * buf = NULL; + char *name, *buf; int ret; - zend_size_t name_len; + size_t name_len; uint32_t len; - zval * value; - JString * js = NULL; - UniAttribute * att = NULL; + zval *value; + JString *js = NULL; + UniAttribute *att = NULL; long iVersion=3; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz|l", &name, &name_len, &value, &iVersion) == FAILURE) { - ZEND_WRONG_PARAM_COUNT(); - return ; - } + ZEND_PARSE_PARAMETERS_START(2, 3) + Z_PARAM_STRING(name, name_len) + Z_PARAM_ZVAL(value) + Z_PARAM_OPTIONAL + Z_PARAM_LONG(iVersion) + ZEND_PARSE_PARAMETERS_END(); js = JString_new(); if (!js) {MALLOC_EXCEPTION("String"); return;} @@ -1609,18 +1349,21 @@ do_clean : */ PHP_METHOD(tup, getString) { - char * name, *buf; - zend_size_t name_len, len; + char *name, *buf; + size_t name_len, len; int ret; - Bool is_require=false; - JString * js = NULL; - UniAttribute * att = NULL; + zend_bool is_require=0; + JString *js = NULL; + UniAttribute *att = NULL; long iVersion=3; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|bl", &name, &name_len, &buf, &len, &is_require, &iVersion) == FAILURE) { - ZEND_WRONG_PARAM_COUNT(); - return ; - } + ZEND_PARSE_PARAMETERS_START(2, 4) + Z_PARAM_STRING(name, name_len) + Z_PARAM_STRING(buf, len) + Z_PARAM_OPTIONAL + Z_PARAM_BOOL(is_require) + Z_PARAM_LONG(iVersion) + ZEND_PARSE_PARAMETERS_END(); att = UniAttribute_new(); if (!att) { @@ -1696,21 +1439,23 @@ do_clean : */ PHP_METHOD(tup, putVector) { - zval * clazz; - char * name, *buf = NULL; - zend_size_t name_len; + zval *clazz; + char *name, *buf = NULL; + size_t name_len; int ret; uint32_t len; - JArray * vct = NULL; - JString * js = NULL; + JArray *vct = NULL; + JString *js = NULL; long iVersion = 3; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sO|l", &name, &name_len, &clazz, tars_vector_ce, &iVersion) == FAILURE) { - ZEND_WRONG_PARAM_COUNT(); - return ; - } + ZEND_PARSE_PARAMETERS_START(2, 3) + Z_PARAM_STRING(name, name_len) + Z_PARAM_OBJECT_OF_CLASS(clazz, tars_vector_ce) + Z_PARAM_OPTIONAL + Z_PARAM_LONG(iVersion) + ZEND_PARSE_PARAMETERS_END(); - vector_wrapper * obj = Z_VECTOR_WRAPPER_P(clazz); + vector_wrapper *obj = Z_VECTOR_WRAPPER_P(clazz); if (!IS_VALID_TYPE(obj->t)) return TYPE_EXCEPTOIN(); if (IS_JSTRING(obj->t)) { @@ -1719,7 +1464,7 @@ PHP_METHOD(tup, putVector) { vct = obj->ctx->vct; } - UniAttribute * att = UniAttribute_new(); + UniAttribute *att = UniAttribute_new(); if (!att) { MALLOC_EXCEPTION("Vector"); return; @@ -1767,24 +1512,28 @@ do_clean : */ PHP_METHOD(tup, getVector) { - char * buf, * name; - zend_size_t len, name_len; + char *buf, *name; + size_t len, name_len; int ret; - zval * clazz, * ret_val = NULL; - JArray * vct; - JString * js; - Bool is_require = false; + zval *clazz, *ret_val = NULL; + JArray *vct; + JString *js; + zend_bool is_require = 0; long iVersion = 3; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sOs|bl", &name, &name_len, &clazz, tars_vector_ce, &buf, &len, &is_require, &iVersion) == FAILURE) { - ZEND_WRONG_PARAM_COUNT(); - return ; - } + ZEND_PARSE_PARAMETERS_START(3, 5) + Z_PARAM_STRING(name, name_len) + Z_PARAM_OBJECT_OF_CLASS(clazz, tars_vector_ce) + Z_PARAM_STRING(buf, len) + Z_PARAM_OPTIONAL + Z_PARAM_BOOL(is_require) + Z_PARAM_LONG(iVersion) + ZEND_PARSE_PARAMETERS_END(); - vector_wrapper * obj = Z_VECTOR_WRAPPER_P(clazz TSRMLS_CC); + vector_wrapper *obj = Z_VECTOR_WRAPPER_P(clazz); if (!IS_VALID_TYPE(obj->t)) return TYPE_EXCEPTOIN(); - UniAttribute * att = UniAttribute_new(); + UniAttribute *att = UniAttribute_new(); if (!att) { MALLOC_EXCEPTION("Vector"); goto do_clean; @@ -1928,23 +1677,25 @@ do_clean : /* {{{ TTUP::putMap */ PHP_METHOD(tup, putMap) { - zval * clazz; - char * name, *buf = NULL; + zval *clazz; + char *name, *buf = NULL; int ret; - zend_size_t name_len; + size_t name_len; uint32_t len; - JMapWrapper * container; + JMapWrapper *container; long iVersion=3; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sO|l", &name, &name_len, &clazz, tars_map_ce, &iVersion) == FAILURE) { - ZEND_WRONG_PARAM_COUNT(); - return ; - } + ZEND_PARSE_PARAMETERS_START(2, 3) + Z_PARAM_STRING(name, name_len) + Z_PARAM_OBJECT_OF_CLASS(clazz, tars_map_ce) + Z_PARAM_OPTIONAL + Z_PARAM_LONG(iVersion) + ZEND_PARSE_PARAMETERS_END(); - map_wrapper * obj = Z_MAP_WRAPPER_P(clazz TSRMLS_CC); + map_wrapper *obj = Z_MAP_WRAPPER_P(clazz); container = obj->ctx; - UniAttribute * att = UniAttribute_new(); + UniAttribute *att = UniAttribute_new(); if (!att) return MALLOC_EXCEPTION("Map"); if (3 == iVersion) { @@ -1983,19 +1734,23 @@ do_clean : */ PHP_METHOD(tup, getMap) { - zval * clazz, *ret_val = NULL; - char * buf, * name; - zend_size_t len, name_len; + zval *clazz, *ret_val = NULL; + char *buf, *name; + size_t len, name_len; int ret = 0; - JMapWrapper * container; - UniAttribute * att = NULL; - Bool is_require = false; + JMapWrapper *container; + UniAttribute *att = NULL; + zend_bool is_require = 0; long iVersion = 3; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sOs|bl", &name, &name_len, &clazz, tars_map_ce, &buf, &len, &is_require, &iVersion) == FAILURE) { - ZEND_WRONG_PARAM_COUNT(); - return ; - } + ZEND_PARSE_PARAMETERS_START(3, 5) + Z_PARAM_STRING(name, name_len) + Z_PARAM_OBJECT_OF_CLASS(clazz, tars_map_ce) + Z_PARAM_STRING(buf, len) + Z_PARAM_OPTIONAL + Z_PARAM_BOOL(is_require) + Z_PARAM_LONG(iVersion) + ZEND_PARSE_PARAMETERS_END(); att = UniAttribute_new(); if (!att) {ret = TARS_MALLOC_ERROR; goto do_clean; } @@ -2023,7 +1778,7 @@ PHP_METHOD(tup, getMap) { goto do_clean; } - map_wrapper * obj = Z_MAP_WRAPPER_P(clazz TSRMLS_CC); + map_wrapper *obj = Z_MAP_WRAPPER_P(clazz); container = obj->ctx; JMapWrapper_clear(container); @@ -2073,18 +1828,20 @@ do_clean : /* }}} */ PHP_METHOD(tup, putStruct) { - zval * clazz; - char * name, *buf = NULL; + zval *clazz; + char *name, *buf = NULL; int ret; - zend_size_t name_len; + size_t name_len; uint32_t len; - UniAttribute * att = NULL; + UniAttribute *att = NULL; long iVersion = 3; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sO|l", &name, &name_len, &clazz, tars_struct_ce, &iVersion) == FAILURE) { - ZEND_WRONG_PARAM_COUNT(); - return ; - } + ZEND_PARSE_PARAMETERS_START(2, 3) + Z_PARAM_STRING(name, name_len) + Z_PARAM_OBJECT_OF_CLASS(clazz, tars_struct_ce) + Z_PARAM_OPTIONAL + Z_PARAM_LONG(iVersion) + ZEND_PARSE_PARAMETERS_END(); att = UniAttribute_new(); if (!att) return MALLOC_EXCEPTION("Struct"); @@ -2131,18 +1888,22 @@ do_clean : + */ PHP_METHOD(tup, getStruct) { - char * buf, * name; - zend_size_t len, name_len; + char *buf, *name; + size_t len, name_len; int ret; - zval * clazz, *ret_val = NULL; - UniAttribute * attr = NULL; - Bool is_require = false; + zval *clazz, *ret_val = NULL; + UniAttribute *attr = NULL; + zend_bool is_require = 0; long iVersion=3; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sOs|bl", &name, &name_len, &clazz, tars_struct_ce, &buf, &len, &is_require, &iVersion) == FAILURE) { - ZEND_WRONG_PARAM_COUNT(); - return ; - } + ZEND_PARSE_PARAMETERS_START(3, 5) + Z_PARAM_STRING(name, name_len) + Z_PARAM_OBJECT_OF_CLASS(clazz, tars_struct_ce) + Z_PARAM_STRING(buf, len) + Z_PARAM_OPTIONAL + Z_PARAM_BOOL(is_require) + Z_PARAM_LONG(iVersion) + ZEND_PARSE_PARAMETERS_END(); // 解码 attr = UniAttribute_new();