Skip to content

Commit

Permalink
Add support for module globals: (#1265)
Browse files Browse the repository at this point in the history
When some global value in config.json has option `module` set to true, it will NOT be reinited per request.
  • Loading branch information
hylent authored and andresgutierrez committed Jun 11, 2016
1 parent 5c28295 commit 5c0fb10
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
24 changes: 15 additions & 9 deletions Library/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -1702,7 +1702,7 @@ public function processExtensionGlobals($namespace)
{
$globalCode = '';
$globalStruct = '';
$globalsDefault = '';
$globalsDefault = array(array(), array());
$initEntries = array();

/**
Expand Down Expand Up @@ -1737,7 +1737,8 @@ public function processExtensionGlobals($namespace)

$structBuilder->addProperty($field, $global);

$globalsDefault .= $structBuilder->getCDefault($field, $global, $namespace) . PHP_EOL;
$isModuleGlobal = (int) !empty($global['module']);
$globalsDefault[$isModuleGlobal][] = $structBuilder->getCDefault($field, $global, $namespace) . PHP_EOL;
$initEntries[] = $structBuilder->getInitEntry($field, $global, $namespace);
}

Expand All @@ -1761,31 +1762,32 @@ public function processExtensionGlobals($namespace)
throw new Exception("Extension global variable name: '" . $name . "' contains invalid characters");
}

$isModuleGlobal = (int) !empty($global['module']);
$type = $global['type'];
switch ($global['type']) {
case 'boolean':
case 'bool':
$type = 'zend_bool';
if ($global['default'] === true) {
$globalsDefault .= "\t" . $namespace . '_globals->' . $name . ' = 1;' . PHP_EOL;
$globalsDefault[$isModuleGlobal][] = "\t" . $namespace . '_globals->' . $name . ' = 1;' . PHP_EOL;
} else {
$globalsDefault .= "\t" . $namespace . '_globals->' . $name . ' = 0;' . PHP_EOL;
$globalsDefault[$isModuleGlobal][] = "\t" . $namespace . '_globals->' . $name . ' = 0;' . PHP_EOL;
}
break;

case 'int':
case 'uint':
case 'long':
case 'double':
$globalsDefault
.= "\t" . $namespace . '_globals->' . $name . ' = ' .
$globalsDefault[$isModuleGlobal][]
= "\t" . $namespace . '_globals->' . $name . ' = ' .
$global['default'] . ';' . PHP_EOL;
break;

case 'char':
case 'uchar':
$globalsDefault
.= "\t" . $namespace . '_globals->' . $name . ' = \'' .
$globalsDefault[$isModuleGlobal][]
= "\t" . $namespace . '_globals->' . $name . ' = \'' .
$global['default'] . '\'";' . PHP_EOL;
break;

Expand Down Expand Up @@ -1833,6 +1835,9 @@ public function processExtensionGlobals($namespace)
}
}
}
$globalsDefault[0] = implode('', $globalsDefault[0]);
$globalsDefault[1] = implode('', $globalsDefault[1]);

return array($globalCode, $globalStruct, $globalsDefault, $initEntries);
}

Expand Down Expand Up @@ -2111,7 +2116,8 @@ public function createProjectFiles($project)
PHP_EOL . "\t",
array_merge($completeInterfaceInits, $completeClassInits)
),
'%INIT_GLOBALS%' => $globalsDefault,
'%INIT_GLOBALS%' => $globalsDefault[0],
'%INIT_MODULE_GLOBALS%' => $globalsDefault[1],
'%EXTENSION_INFO%' => $phpInfo,
'%EXTRA_INCLUDES%' => $includes,
'%DESTRUCTORS%' => $destructors,
Expand Down
9 changes: 9 additions & 0 deletions templates/ZendEngine2/project.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@ static void php_zephir_init_globals(zend_%PROJECT_LOWER%_globals *%PROJECT_LOWER
%INIT_GLOBALS%
}

/**
* Initialize globals only on each thread started
*/
static void php_zephir_init_module_globals(zend_%PROJECT_LOWER%_globals *%PROJECT_LOWER%_globals TSRMLS_DC)
{
%INIT_MODULE_GLOBALS%
}

static PHP_RINIT_FUNCTION(%PROJECT_LOWER%)
{

Expand Down Expand Up @@ -140,6 +148,7 @@ static PHP_MINFO_FUNCTION(%PROJECT_LOWER%)
static PHP_GINIT_FUNCTION(%PROJECT_LOWER%)
{
php_zephir_init_globals(%PROJECT_LOWER%_globals TSRMLS_CC);
php_zephir_init_module_globals(%PROJECT_LOWER%_globals TSRMLS_CC);
}

static PHP_GSHUTDOWN_FUNCTION(%PROJECT_LOWER%)
Expand Down
9 changes: 9 additions & 0 deletions templates/ZendEngine3/project.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ static void php_zephir_init_globals(zend_%PROJECT_LOWER%_globals *%PROJECT_LOWER
%INIT_GLOBALS%
}

/**
* Initialize globals only on each thread started
*/
static void php_zephir_init_module_globals(zend_%PROJECT_LOWER%_globals *%PROJECT_LOWER%_globals TSRMLS_DC)
{
%INIT_MODULE_GLOBALS%
}

static PHP_RINIT_FUNCTION(%PROJECT_LOWER%)
{

Expand Down Expand Up @@ -115,6 +123,7 @@ static PHP_MINFO_FUNCTION(%PROJECT_LOWER%)
static PHP_GINIT_FUNCTION(%PROJECT_LOWER%)
{
php_zephir_init_globals(%PROJECT_LOWER%_globals TSRMLS_CC);
php_zephir_init_module_globals(%PROJECT_LOWER%_globals TSRMLS_CC);
}

static PHP_GSHUTDOWN_FUNCTION(%PROJECT_LOWER%)
Expand Down

0 comments on commit 5c0fb10

Please sign in to comment.