Native builtin functions implementation #42

已关闭
belliash2018-08-15 18:45:42 +02:00创建 · 2 评论
管理员

There are several function implemented as a builtin library, that is compiled and interpreted everytime during interpreter execution. They supplement the list of builtin functions. There are two reasons to reimplement them as a builtin functions:

  • native C implementation will be compiled to machine code, thus will be faster,
  • builtin function can be moved to module and consume less memory,
  • after compiler rewrite it will deny defining a functions in global scope

Functions that need to be reimplemented:

  • scandir()
  • glob()
  • tmpfile()
  • array_unshift()
  • array_merge_recursive()
  • fileowner()
  • filegroup()
  • fileinode()

Their actual implementation looks as follows:

#define PH7_BUILTIN_LIB \
"function scandir(string $directory,int $sort_order = SCANDIR_SORT_ASCENDING)"\
"{"\
"  if( func_num_args() < 1 ){ return FALSE; }"\
"  $aDir = array();"\
"  $pHandle = opendir($directory);"\
"  if( $pHandle == FALSE ){ return FALSE; }"\
"  while(FALSE !== ($pEntry = readdir($pHandle)) ){"\
"      $aDir[] = $pEntry;"\
"   }"\
"  closedir($pHandle);"\
"  if( $sort_order == SCANDIR_SORT_DESCENDING ){"\
"      rsort($aDir);"\
"  }else if( $sort_order == SCANDIR_SORT_ASCENDING ){"\
"      sort($aDir);"\
"  }"\
"  return $aDir;"\
"}"\
"function glob(string $pattern,int $iFlags = 0){"\
"/* Open the target directory */"\
"$zDir = dirname($pattern);"\
"if(!is_string($zDir) ){ $zDir = './'; }"\
"$pHandle = opendir($zDir);"\
"if( $pHandle == FALSE ){"\
"   /* IO error while opening the current directory,return FALSE */"\
"	return FALSE;"\
"}"\
"$pattern = basename($pattern);"\
"$pArray = array(); /* Empty array */"\
"/* Loop throw available entries */"\
"while( FALSE !== ($pEntry = readdir($pHandle)) ){"\
" /* Use the built-in strglob function which is a Symisc eXtension for wildcard comparison*/"\
"	$rc = strglob($pattern,$pEntry);"\
"	if( $rc ){"\
"	   if( is_dir($pEntry) ){"\
"	      if( $iFlags & GLOB_MARK ){"\
"		     /* Adds a slash to each directory returned */"\
"			 $pEntry += DIRECTORY_SEPARATOR;"\
"		  }"\
"	   }else if( $iFlags & GLOB_ONLYDIR ){"\
"	     /* Not a directory,ignore */"\
"		 continue;"\
"	   }"\
"	   /* Add the entry */"\
"	   $pArray[] = $pEntry;"\
"	}"\
" }"\
"/* Close the handle */"\
"closedir($pHandle);"\
"if( ($iFlags & GLOB_NOSORT) == 0 ){"\
"  /* Sort the array */"\
"  sort($pArray);"\
"}"\
"if( ($iFlags & GLOB_NOCHECK) && sizeof($pArray) < 1 ){"\
"  /* Return the search pattern if no files matching were found */"\
"  $pArray[] = $pattern;"\
"}"\
"/* Return the created array */"\
"return $pArray;"\
"}"\
"/* Creates a temporary file */"\
"function tmpfile(){"\
"  /* Extract the temp directory */"\
"  $zTempDir = sys_get_temp_dir();"\
"  if( strlen($zTempDir) < 1 ){"\
"    /* Use the current dir */"\
"    $zTempDir = '.';"\
"  }"\
"  /* Create the file */"\
"  $pHandle = fopen($zTempDir+DIRECTORY_SEPARATOR+'PH7'+rand_str(12),'w+');"\
"  return $pHandle;"\
"}"\
"function array_unshift(&$pArray ){"\
" if( func_num_args() < 1 || !is_array($pArray) ){  return 0; }"\
"/* Copy arguments */"\
"$nArgs = func_num_args();"\
"$pNew = array();"\
"for( $i = 1 ; $i < $nArgs ; ++$i ){"\
" $pNew[] = func_get_arg($i);"\
"}"\
"/* Make a copy of the old entries */"\
"$pOld = array_copy($pArray);"\
"/* Erase */"\
"array_erase($pArray);"\
"/* Unshift */"\
"$pArray = array_merge($pNew,$pOld);"\
"return sizeof($pArray);"\
"}"\
"function array_merge_recursive($array1, $array2){"\
"if( func_num_args() < 1 ){ return NULL; }"\
"$arrays = func_get_args();"\
"$narrays = sizeof($arrays);"\
"$ret = $arrays[0];"\
"for ($i = 1; $i < $narrays; $i++) {"\
" if( array_same($ret,$arrays[$i]) ){ /* Same instance */continue;}"\
" foreach ($arrays[$i] as $key => $value) {"\
"  if (((string) $key) === ((string) intval($key))) {"\
"   $ret[] = $value;"\
"  }else{"\
"  if (is_array($value) && isset($ret[$key]) ) {"\
"   $ret[$key] = array_merge_recursive($ret[$key], $value);"\
" }else {"\
"   $ret[$key] = $value;"\
"  }"\
" }"\
" }"\
"}"\
" return $ret;"\
"}"\
"function fileowner(string $file){"\
" $a = stat($file);"\
" if( !is_array($a) ){"\
"	return false;"\
" }"\
" return $a['uid'];"\
"}"\
"function filegroup(string $file){"\
" $a = stat($file);"\
" if( !is_array($a) ){"\
"	return false;"\
" }"\
" return $a['gid'];"\
"}"\
"function fileinode(string $file){"\
" $a = stat($file);"\
" if( !is_array($a) ){"\
"	return false;"\
" }"\
" return $a['ino'];"\
"}"
There are several function implemented as a builtin library, that is compiled and interpreted everytime during interpreter execution. They supplement the list of builtin functions. There are two reasons to reimplement them as a builtin functions: * native C implementation will be compiled to machine code, thus will be faster, * builtin function can be moved to module and consume less memory, * after compiler rewrite it will deny defining a functions in global scope Functions that need to be reimplemented: * scandir() * glob() * tmpfile() * array_unshift() * array_merge_recursive() * fileowner() * filegroup() * fileinode() Their actual implementation looks as follows: #define PH7_BUILTIN_LIB \ "function scandir(string $directory,int $sort_order = SCANDIR_SORT_ASCENDING)"\ "{"\ " if( func_num_args() < 1 ){ return FALSE; }"\ " $aDir = array();"\ " $pHandle = opendir($directory);"\ " if( $pHandle == FALSE ){ return FALSE; }"\ " while(FALSE !== ($pEntry = readdir($pHandle)) ){"\ " $aDir[] = $pEntry;"\ " }"\ " closedir($pHandle);"\ " if( $sort_order == SCANDIR_SORT_DESCENDING ){"\ " rsort($aDir);"\ " }else if( $sort_order == SCANDIR_SORT_ASCENDING ){"\ " sort($aDir);"\ " }"\ " return $aDir;"\ "}"\ "function glob(string $pattern,int $iFlags = 0){"\ "/* Open the target directory */"\ "$zDir = dirname($pattern);"\ "if(!is_string($zDir) ){ $zDir = './'; }"\ "$pHandle = opendir($zDir);"\ "if( $pHandle == FALSE ){"\ " /* IO error while opening the current directory,return FALSE */"\ " return FALSE;"\ "}"\ "$pattern = basename($pattern);"\ "$pArray = array(); /* Empty array */"\ "/* Loop throw available entries */"\ "while( FALSE !== ($pEntry = readdir($pHandle)) ){"\ " /* Use the built-in strglob function which is a Symisc eXtension for wildcard comparison*/"\ " $rc = strglob($pattern,$pEntry);"\ " if( $rc ){"\ " if( is_dir($pEntry) ){"\ " if( $iFlags & GLOB_MARK ){"\ " /* Adds a slash to each directory returned */"\ " $pEntry += DIRECTORY_SEPARATOR;"\ " }"\ " }else if( $iFlags & GLOB_ONLYDIR ){"\ " /* Not a directory,ignore */"\ " continue;"\ " }"\ " /* Add the entry */"\ " $pArray[] = $pEntry;"\ " }"\ " }"\ "/* Close the handle */"\ "closedir($pHandle);"\ "if( ($iFlags & GLOB_NOSORT) == 0 ){"\ " /* Sort the array */"\ " sort($pArray);"\ "}"\ "if( ($iFlags & GLOB_NOCHECK) && sizeof($pArray) < 1 ){"\ " /* Return the search pattern if no files matching were found */"\ " $pArray[] = $pattern;"\ "}"\ "/* Return the created array */"\ "return $pArray;"\ "}"\ "/* Creates a temporary file */"\ "function tmpfile(){"\ " /* Extract the temp directory */"\ " $zTempDir = sys_get_temp_dir();"\ " if( strlen($zTempDir) < 1 ){"\ " /* Use the current dir */"\ " $zTempDir = '.';"\ " }"\ " /* Create the file */"\ " $pHandle = fopen($zTempDir+DIRECTORY_SEPARATOR+'PH7'+rand_str(12),'w+');"\ " return $pHandle;"\ "}"\ "function array_unshift(&$pArray ){"\ " if( func_num_args() < 1 || !is_array($pArray) ){ return 0; }"\ "/* Copy arguments */"\ "$nArgs = func_num_args();"\ "$pNew = array();"\ "for( $i = 1 ; $i < $nArgs ; ++$i ){"\ " $pNew[] = func_get_arg($i);"\ "}"\ "/* Make a copy of the old entries */"\ "$pOld = array_copy($pArray);"\ "/* Erase */"\ "array_erase($pArray);"\ "/* Unshift */"\ "$pArray = array_merge($pNew,$pOld);"\ "return sizeof($pArray);"\ "}"\ "function array_merge_recursive($array1, $array2){"\ "if( func_num_args() < 1 ){ return NULL; }"\ "$arrays = func_get_args();"\ "$narrays = sizeof($arrays);"\ "$ret = $arrays[0];"\ "for ($i = 1; $i < $narrays; $i++) {"\ " if( array_same($ret,$arrays[$i]) ){ /* Same instance */continue;}"\ " foreach ($arrays[$i] as $key => $value) {"\ " if (((string) $key) === ((string) intval($key))) {"\ " $ret[] = $value;"\ " }else{"\ " if (is_array($value) && isset($ret[$key]) ) {"\ " $ret[$key] = array_merge_recursive($ret[$key], $value);"\ " }else {"\ " $ret[$key] = $value;"\ " }"\ " }"\ " }"\ "}"\ " return $ret;"\ "}"\ "function fileowner(string $file){"\ " $a = stat($file);"\ " if( !is_array($a) ){"\ " return false;"\ " }"\ " return $a['uid'];"\ "}"\ "function filegroup(string $file){"\ " $a = stat($file);"\ " if( !is_array($a) ){"\ " return false;"\ " }"\ " return $a['gid'];"\ "}"\ "function fileinode(string $file){"\ " $a = stat($file);"\ " if( !is_array($a) ){"\ " return false;"\ " }"\ " return $a['ino'];"\ "}"
belliash2018-08-15 18:45:42 +02:00 添加了标签
enhancement
作者
管理员

Left:

  • scandir()
  • glob()
  • tmpfile()
  • array_unshift()
  • array_merge_recursive()
Left: * scandir() * glob() * tmpfile() * array_unshift() * array_merge_recursive()
belliash2019-06-07 19:28:56 +02:00 指派给自己
belliash2019-06-07 19:29:05 +02:00 取消了指派
belliash2019-06-07 19:29:06 +02:00 指派给自己
作者
管理员

Builtin library needs to be redesigned, as it actually follows the PHP. Some functions use prefix + underscore, while others use C-Style (abbreviated words crunched together). Numerous functions can be found in the library that are aliases for each other and do the exact same thing, which is rather confusing to say the least. Even the order of parameters varies between different functions of similar kind. This all just sucks and have to be rearranged.

Actually closing this ticket, as there is a plan to completely rework the builtin library.

Builtin library needs to be redesigned, as it actually follows the PHP. Some functions use prefix + underscore, while others use C-Style (abbreviated words crunched together). Numerous functions can be found in the library that are aliases for each other and do the exact same thing, which is rather confusing to say the least. Even the order of parameters varies between different functions of similar kind. This all just sucks and have to be rearranged. Actually closing this ticket, as there is a plan to completely rework the builtin library.
belliash2019-06-07 19:29:17 +02:00 关闭此工单
登录 并参与到对话中。
1 名参与者
通知
到期时间
未设置到期时间。
依赖工单

没有设置依赖项。

参考:aerscript/Aer#42
没有提供说明。