Skip to content

Commit f71a6ca

Browse files
add one level call_c_auto conversion support
1 parent 75ac7af commit f71a6ca

File tree

3 files changed

+57
-5
lines changed

3 files changed

+57
-5
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
namespace PHPtoCExt\Converter;
3+
4+
/**
5+
* Automatically convert c_function_auto api to corresponding c_function_call apis
6+
*/
7+
class CFunctionAutoConverter extends \PHPtoCExt\Converter
8+
{
9+
public function convert()
10+
{
11+
//first, find out all call_c_auto calls and find out the corresponding class context this c function call belongs to
12+
$classMap = $this->getClassMap();
13+
14+
foreach($classMap as $className => $classInfo) {
15+
$originalClassCode = implode("\n",array_slice($this->codeLines, $classInfo->startLine - 1, $classInfo->endLine - $classInfo->startLine + 1));
16+
foreach($classInfo->methodInfos as $methodPureName => $methodInfo) {
17+
for ($index = $methodInfo->startLine; $index <= $methodInfo->endLine; $index++) {
18+
if (preg_match("/call_c_auto\(.*\)/", $this->codeLines[$index], $matches)) {
19+
if(count($matches) == 1) {
20+
$codeLine = $this->codeLines[$index]; //store the original code line here
21+
$filteredCodeLine = str_replace(array("call_c_auto","(",")",";"),"",trim($codeLine));
22+
$lineComps = explode("=",$filteredCodeLine);
23+
$lineCompsCount = count($lineComps);
24+
$className[0] = "";
25+
$className = trim($className);
26+
$classNameComps = explode("\\", $className);
27+
//remove namespace
28+
array_shift($classNameComps);
29+
$className = implode("/",$classNameComps);
30+
if ($lineCompsCount == 1) { //this means we do not have return result variable
31+
$inputParamsStr = trim($lineComps[0]);
32+
if (strlen($inputParamsStr) == 0) {
33+
$this->searchAndReplace($filteredCodeLine,"call_c_function(\"$className.c\",\"$methodPureName\");");
34+
} else {
35+
$this->searchAndReplace($codeLine,"call_c_function(\"$className.c\",\"$methodPureName\",$inputParamsStr);");
36+
}
37+
} else if ($lineCompsCount == 2){ //this means we have input return result variable
38+
$returnVarStr = $lineComps[0];
39+
$inputParamsStr = $lineComps[1];
40+
if (strlen($inputParamsStr) == 0) {
41+
$this->searchAndReplace($filteredCodeLine,"$returnVarStr = call_c_function(\"$className.c\",\"$methodPureName\");");
42+
} else {
43+
$this->searchAndReplace($codeLine,"$returnVarStr = call_c_function(\"$className.c\",\"$methodPureName\",$inputParamsStr);");
44+
}
45+
46+
}
47+
}
48+
}
49+
}
50+
}
51+
}
52+
}
53+
}

src/PHPtoCExt/Converter/CFuntionCallConverter.php renamed to src/PHPtoCExt/Converter/CFunctionCallConverter.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,11 @@
66
*/
77
namespace PHPtoCExt\Converter;
88

9-
class CFuntionCallConverter extends \PHPtoCExt\Converter
9+
class CFunctionCallConverter extends \PHPtoCExt\Converter
1010
{
1111
public function convert()
1212
{
1313
//first, find out all call_c_function calls and find out the corresponding class context this c function call belongs to
14-
$cFunctionCallIndexes = array();
15-
1614
$classMap = $this->getClassMap();
1715

1816
$cSourceCodeMap = array();

src/PHPtoCExt/FileFilter.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,9 @@ public function filter()
4747
"\PHPtoCExt\Converter\ClassHierarchyFlatterningConverter",
4848
"\PHPtoCExt\Converter\StaticVarAutoDefineConverter",
4949
"\PHPtoCExt\Converter\SelfStaticConverter",
50-
"\PHPtoCExt\Converter\CodeReformatConverter", //reformat the code and get ready for the final conversion
51-
"\PHPtoCExt\Converter\CFuntionCallConverter", //convert c function calls
50+
"\PHPtoCExt\Converter\CodeReformatConverter", //reformat the code and get ready for the final conversion
51+
"\PHPtoCExt\Converter\CFunctionAutoConverter", //convert c_function_auto to c_function_call
52+
"\PHPtoCExt\Converter\CFunctionCallConverter", //convert c function calls
5253
);
5354

5455
$searches = array();

0 commit comments

Comments
 (0)