#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_ini.h"
#include "php_hello.h"
ZEND_DECLARE_MODULE_GLOBALS(hello)
static function_entry hello_functions[] = {
PHP_FE(hello_world, NULL)
PHP_FE(hello_long, NULL)
PHP_FE(hello_double, NULL)
PHP_FE(hello_bool, NULL)
PHP_FE(hello_null, NULL)
PHP_FE(hello_greetme, NULL)
PHP_FE(hello_add, NULL)
PHP_FE(hello_dump, NULL)
PHP_FE(hello_array, NULL)
PHP_FE(hello_array_strings, NULL)
PHP_FE(hello_array_walk, NULL)
PHP_FE(hello_array_value, NULL)
PHP_FE(hello_get_global_var, NULL)
PHP_FE(hello_set_local_var, NULL)
{NULL, NULL, NULL}
};
zend_module_entry hello_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
STANDARD_MODULE_HEADER,
#endif
PHP_HELLO_WORLD_EXTNAME,
hello_functions,
PHP_MINIT(hello),
PHP_MSHUTDOWN(hello),
PHP_RINIT(hello),
NULL,
NULL,
#if ZEND_MODULE_API_NO >= 20010901
PHP_HELLO_WORLD_VERSION,
#endif
STANDARD_MODULE_PROPERTIES
};
#ifdef COMPILE_DL_HELLO
ZEND_GET_MODULE(hello)
#endif
PHP_INI_BEGIN()
PHP_INI_ENTRY("hello.greeting", "Hello World", PHP_INI_ALL, NULL)
STD_PHP_INI_ENTRY("hello.direction", "1", PHP_INI_ALL, OnUpdateBool, direction, zend_hello_globals, hello_globals)
PHP_INI_END()
static void php_hello_init_globals(zend_hello_globals *hello_globals)
{
hello_globals->direction = 1;
}
PHP_RINIT_FUNCTION(hello)
{
HELLO_G(counter) = 0;
return SUCCESS;
}
PHP_MINIT_FUNCTION(hello)
{
ZEND_INIT_MODULE_GLOBALS(hello, php_hello_init_globals, NULL);
REGISTER_INI_ENTRIES();
return SUCCESS;
}
PHP_MSHUTDOWN_FUNCTION(hello)
{
UNREGISTER_INI_ENTRIES();
return SUCCESS;
}
PHP_FUNCTION(hello_world)
{
RETURN_STRING("Hello World", 1);
}
PHP_FUNCTION(hello_long)
{
if (HELLO_G(direction)) {
HELLO_G(counter)++;
} else {
HELLO_G(counter)--;
}
RETURN_LONG(HELLO_G(counter));
}
PHP_FUNCTION(hello_double)
{
RETURN_DOUBLE(3.1415926535);
}
PHP_FUNCTION(hello_bool)
{
RETURN_BOOL(1);
}
PHP_FUNCTION(hello_null)
{
RETURN_NULL();
}
PHP_FUNCTION(hello_greetme)
{
zval *zname;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &zname) == FAILURE) {
RETURN_NULL();
}
convert_to_string(zname);
php_printf("Hello ");
PHPWRITE(Z_STRVAL_P(zname), Z_STRLEN_P(zname));
php_printf("
");
RETURN_TRUE;
}
PHP_FUNCTION(hello_add)
{
long a;
double b;
zend_bool return_long = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ld|b", &a, &b, &return_long) == FAILURE) {
RETURN_NULL();
}
if (return_long) {
RETURN_LONG(a + b);
} else {
RETURN_DOUBLE(a + b);
}
}
PHP_FUNCTION(hello_dump)
{
zval *uservar;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &uservar) == FAILURE) {
RETURN_NULL();
}
switch (Z_TYPE_P(uservar)) {
case IS_NULL:
php_printf("NULL
");
break;
case IS_BOOL:
php_printf("Boolean: %s
", Z_LVAL_P(uservar) ? "TRUE" : "FALSE");
break;
case IS_LONG:
php_printf("Long: %ld
", Z_LVAL_P(uservar));
break;
case IS_DOUBLE:
php_printf("Double: %f
", Z_DVAL_P(uservar));
break;
case IS_STRING:
php_printf("String: ");
PHPWRITE(Z_STRVAL_P(uservar), Z_STRLEN_P(uservar));
php_printf("
");
break;
case IS_RESOURCE:
php_printf("Resource
");
break;
case IS_ARRAY:
php_printf("Array
");
break;
case IS_OBJECT:
php_printf("Object
");
break;
default:
php_printf("Unknown
");
}
RETURN_TRUE;
}
PHP_FUNCTION(hello_array)
{
char *mystr;
zval *mysubarray;
array_init(return_value);
add_index_long(return_value, 42, 123);
add_next_index_string(return_value, "I should now be found at index 43", 1);
add_next_index_stringl(return_value, "I'm at 44!", 10, 1);
mystr = estrdup("Forty Five");
add_next_index_string(return_value, mystr, 0);
add_assoc_double(return_value, "pi", 3.1415926535);
ALLOC_INIT_ZVAL(mysubarray);
array_init(mysubarray);
add_next_index_string(mysubarray, "hello", 1);
php_printf("mysubarray->refcount = %d
", mysubarray->refcount);
mysubarray->refcount = 2;
php_printf("mysubarray->refcount = %d
", mysubarray->refcount);
add_assoc_zval(return_value, "subarray", mysubarray);
php_printf("mysubarray->refcount = %d
", mysubarray->refcount);
}
PHP_FUNCTION(hello_array_strings)
{
zval *arr, **data;
HashTable *arr_hash;
HashPosition pointer;
int array_count;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &arr) == FAILURE) {
RETURN_NULL();
}
arr_hash = Z_ARRVAL_P(arr);
array_count = zend_hash_num_elements(arr_hash);
php_printf("The array passed contains %d elements
", array_count);
for(zend_hash_internal_pointer_reset_ex(arr_hash, &pointer); zend_hash_get_current_data_ex(arr_hash, (void**) &data, &pointer) == SUCCESS; zend_hash_move_forward_ex(arr_hash, &pointer)) {
zval temp;
char *key;
int key_len;
long index;
if (zend_hash_get_current_key_ex(arr_hash, &key, &key_len, &index, 0, &pointer) == HASH_KEY_IS_STRING) {
PHPWRITE(key, key_len);
} else {
php_printf("%ld", index);
}
php_printf(" => ");
temp = **data;
zval_copy_ctor(&temp);
convert_to_string(&temp);
PHPWRITE(Z_STRVAL(temp), Z_STRLEN(temp));
php_printf("
");
zval_dtor(&temp);
}
RETURN_TRUE;
}
static int php_hello_array_walk(zval **element TSRMLS_DC)
{
zval temp;
temp = **element;
zval_copy_ctor(&temp);
convert_to_string(&temp);
PHPWRITE(Z_STRVAL(temp), Z_STRLEN(temp));
php_printf("
");
zval_dtor(&temp);
return ZEND_HASH_APPLY_KEEP;
}
static int php_hello_array_walk_arg(zval **element, char *greeting TSRMLS_DC)
{
php_printf("%s", greeting);
php_hello_array_walk(element TSRMLS_CC);
return ZEND_HASH_APPLY_KEEP;
}
static int php_hello_array_walk_args(zval **element, int num_args, var_list args, zend_hash_key *hash_key)
{
char *prefix = va_arg(args, char*);
char *suffix = va_arg(args, char*);
TSRMLS_FETCH();
php_printf("%s", prefix);
php_hello_array_walk(element TSRMLS_CC);
php_printf("%s
", suffix);
return ZEND_HASH_APPLY_KEEP;
}
PHP_FUNCTION(hello_array_walk)
{
zval *zarray;
int print_newline = 1;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &zarray) == FAILURE) {
RETURN_NULL();
}
zend_hash_apply(Z_ARRVAL_P(zarray), (apply_func_t)php_hello_array_walk TSRMLS_CC);
zend_hash_internal_pointer_reset(Z_ARRVAL_P(zarray));
zend_hash_apply_with_argument(Z_ARRVAL_P(zarray), (apply_func_arg_t)php_hello_array_walk_arg, "Hello " TSRMLS_CC);
zend_hash_apply_with_arguments(Z_ARRVAL_P(zarray), (apply_func_args_t)php_hello_array_walk_args, 2, "Hello ", "Welcome to my extension!");
RETURN_TRUE;
}
PHP_FUNCTION(hello_array_value)
{
zval *zarray, *zoffset, **zvalue;
long index = 0;
char *key = NULL;
int key_len = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "az", &zarray, &zoffset) == FAILURE) {
RETURN_NULL();
}
switch (Z_TYPE_P(zoffset)) {
case IS_NULL:
index = 0;
break;
case IS_DOUBLE:
index = (long)Z_DVAL_P(zoffset);
break;
case IS_BOOL:
case IS_LONG:
case IS_RESOURCE:
index = Z_LVAL_P(zoffset);
break;
case IS_STRING:
key = Z_STRVAL_P(zoffset);
key_len = Z_STRLEN_P(zoffset);
break;
case IS_ARRAY:
key = "Array";
key_len = sizeof("Array") - 1;
break;
case IS_OBJECT:
key = "Object";
key_len = sizeof("Object") - 1;
break;
default:
key = "Unknown";
key_len = sizeof("Unknown") - 1;
}
if (key && zend_hash_find(Z_ARRVAL_P(zarray), key, key_len + 1, (void**)&zvalue) == FAILURE) {
php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Undefined index: %s", key);
RETURN_NULL();
} else if (!key && zend_hash_index_find(Z_ARRVAL_P(zarray), index, (void**)&zvalue) == FAILURE) {
php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Undefined index: %ld", index);
RETURN_NULL();
}
*return_value = **zvalue;
zval_copy_ctor(return_value);
}
PHP_FUNCTION(hello_get_global_var)
{
char *varname;
int varname_len;
zval **varvalue;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &varname, &varname_len) == FAILURE) {
RETURN_NULL();
}
if (zend_hash_find(&EG(symbol_table), varname, varname_len + 1, (void**)&varvalue) == FAILURE) {
php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Undefined variable: %s", varname);
RETURN_NULL();
}
*return_value = **varvalue;
zval_copy_ctor(return_value);
}
PHP_FUNCTION(hello_set_local_var)
{
zval *newvar;
char *varname;
int varname_len;
zval *value;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz", &varname, &varname_len, &value) == FAILURE) {
RETURN_NULL();
}
ALLOC_INIT_ZVAL(newvar);
*newvar = *value;
zval_copy_ctor(newvar);
zend_hash_add(EG(active_symbol_table), varname, varname_len + 1, &newvar, sizeof(zval*), NULL);
RETURN_TRUE;
}
December 6, 2010 at 8:12 pm
Got that part working with some guessing. At least it appears to be working, I’m brand new to zend also and hence using the tutorial too.
The prototype for php_hello_array_walk_args should be:
static int php_hello_array_walk_args(zval **element TSRMLS_DC, int num_args, va_list args, zend_hash_key *hash_key)
remove the TSRMLS_FETCH line
The call to zend_hash_apply_with_arguments should be:
zend_hash_apply_with_arguments(Z_ARRVAL_P(zarray) TSRMLS_CC,(apply_func_args_t)php_hello_array_walk_args, 2, "Hello ", "Welcome to my extension!");
The trick was looking through the warnings even when it compiles successfully.
May 11, 2010 at 7:20 pm
I copied this code exactly and attempted to make it.
Compile problems:
1. The aforementioned "var_list" to "va_list" typo is one problem.
2. Another is that lines 211, 212, 213, and 216 refer to mysubarray->refcount, to which the compiler complains "’zval’ has no member named ‘refcount’".
3. Furthermore, where did those lines come from? Nowhere in the tutorial is this code given.
Segfaults:
Commenting out the lines mentioned in #2 and fixing the typo in #1 allows the code to compile (with warnings). Then running:
$ php -r ‘hello_array_walk(array(1,2,3));’
yields:
1 2 3 Hello 1 Hello 2 Hello 3 Segmentation fault (core dumped)
Running gdb on the core shows that zend_hash_apply_with_arguments() was the offending call (line 308). Commenting out this line seems to create a perfect extension.
I wish I knew enough to suggest a solution, but hey, I’m reading the tutorial. Any fixes to this code will be greatly appreciated. Thank you!
May 11, 2010 at 6:51 pm
The following code from the sanity check is nowhere in the tutorial so far:
#ifdef COMPILE_DL_HELLO
ZEND_GET_MODULE(hello)
#endif
What’s it do?