#42 Native builtin functions implementation

Closed
opened 3 years ago by belliash · 3 comments
belliash commented 3 years ago
Owner

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'];"\ "}"
belliash added the
enhancement
label 3 years ago
belliash commented 3 years ago
Poster
Owner

Left:

  • scandir()
  • glob()
  • tmpfile()
  • array_unshift()
  • array_merge_recursive()
Left: * scandir() * glob() * tmpfile() * array_unshift() * array_merge_recursive()
likoski commented 3 years ago
Owner

There is globbing function already implemented in PH7 Engine, however directory handling routines are implemented as ph7_io_stream and they results are returned internally to the VM. We might need to implement them as a part of VFS. At least I dont know the way to use ph7_io_stream. I know the result is save in pCtx->pRet and it is not a big deal to take its value, but it can be tricky to overload the builtin function return value afterwards. The implementation of xOpenDir() and xReadDir() will be needed for scandir() implementation too.

There is globbing function already implemented in PH7 Engine, however directory handling routines are implemented as ph7_io_stream and they results are returned internally to the VM. We might need to implement them as a part of VFS. At least I dont know the way to use ph7_io_stream. I know the result is save in pCtx->pRet and it is not a big deal to take its value, but it can be tricky to overload the builtin function return value afterwards. The implementation of xOpenDir() and xReadDir() will be needed for scandir() implementation too.
belliash self-assigned this 2 years ago
belliash removed their assignment 2 years ago
belliash self-assigned this 2 years ago
belliash commented 2 years ago
Poster
Owner

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.
belliash closed this issue 2 years ago
Sign in to join this conversation.
No Milestone
No Assignees
2 Participants
Notifications
Due Date

No due date set.

Dependencies

This issue currently doesn't have any dependencies.

Loading…
There is no content yet.