Skip to content

Commit

Permalink
add one level call_c_auto conversion support
Browse files Browse the repository at this point in the history
  • Loading branch information
jimthunderbird committed Mar 2, 2015
1 parent 75ac7af commit f71a6ca
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 5 deletions.
53 changes: 53 additions & 0 deletions src/PHPtoCExt/Converter/CFunctionAutoConverter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php
namespace PHPtoCExt\Converter;

/**
* Automatically convert c_function_auto api to corresponding c_function_call apis
*/
class CFunctionAutoConverter extends \PHPtoCExt\Converter
{
public function convert()
{
//first, find out all call_c_auto calls and find out the corresponding class context this c function call belongs to
$classMap = $this->getClassMap();

foreach($classMap as $className => $classInfo) {
$originalClassCode = implode("\n",array_slice($this->codeLines, $classInfo->startLine - 1, $classInfo->endLine - $classInfo->startLine + 1));
foreach($classInfo->methodInfos as $methodPureName => $methodInfo) {
for ($index = $methodInfo->startLine; $index <= $methodInfo->endLine; $index++) {
if (preg_match("/call_c_auto\(.*\)/", $this->codeLines[$index], $matches)) {
if(count($matches) == 1) {
$codeLine = $this->codeLines[$index]; //store the original code line here
$filteredCodeLine = str_replace(array("call_c_auto","(",")",";"),"",trim($codeLine));
$lineComps = explode("=",$filteredCodeLine);
$lineCompsCount = count($lineComps);
$className[0] = "";
$className = trim($className);
$classNameComps = explode("\\", $className);
//remove namespace
array_shift($classNameComps);
$className = implode("/",$classNameComps);
if ($lineCompsCount == 1) { //this means we do not have return result variable
$inputParamsStr = trim($lineComps[0]);
if (strlen($inputParamsStr) == 0) {
$this->searchAndReplace($filteredCodeLine,"call_c_function(\"$className.c\",\"$methodPureName\");");
} else {
$this->searchAndReplace($codeLine,"call_c_function(\"$className.c\",\"$methodPureName\",$inputParamsStr);");
}
} else if ($lineCompsCount == 2){ //this means we have input return result variable
$returnVarStr = $lineComps[0];
$inputParamsStr = $lineComps[1];
if (strlen($inputParamsStr) == 0) {
$this->searchAndReplace($filteredCodeLine,"$returnVarStr = call_c_function(\"$className.c\",\"$methodPureName\");");
} else {
$this->searchAndReplace($codeLine,"$returnVarStr = call_c_function(\"$className.c\",\"$methodPureName\",$inputParamsStr);");
}

}
}
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@
*/
namespace PHPtoCExt\Converter;

class CFuntionCallConverter extends \PHPtoCExt\Converter
class CFunctionCallConverter extends \PHPtoCExt\Converter
{
public function convert()
{
//first, find out all call_c_function calls and find out the corresponding class context this c function call belongs to
$cFunctionCallIndexes = array();

$classMap = $this->getClassMap();

$cSourceCodeMap = array();
Expand Down
5 changes: 3 additions & 2 deletions src/PHPtoCExt/FileFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ public function filter()
"\PHPtoCExt\Converter\ClassHierarchyFlatterningConverter",
"\PHPtoCExt\Converter\StaticVarAutoDefineConverter",
"\PHPtoCExt\Converter\SelfStaticConverter",
"\PHPtoCExt\Converter\CodeReformatConverter", //reformat the code and get ready for the final conversion
"\PHPtoCExt\Converter\CFuntionCallConverter", //convert c function calls
"\PHPtoCExt\Converter\CodeReformatConverter", //reformat the code and get ready for the final conversion
"\PHPtoCExt\Converter\CFunctionAutoConverter", //convert c_function_auto to c_function_call
"\PHPtoCExt\Converter\CFunctionCallConverter", //convert c function calls
);

$searches = array();
Expand Down

0 comments on commit f71a6ca

Please sign in to comment.