Examples - flaupretre/php-ext-gen GitHub Wiki

Examples are contained in the 'examples' subdirectory.

Function definition files

A simple 'bridge' function (taken from the newt extension) :

#PHP prototype:
#    int newt_centered_window(int $width, int $height [, string $title=null ])
#
# No need for an explicit default value for 'title', as our default for string
# is a NULL pointer and this is the same in newt.
#---------------------------------------------------------------------------- 

arguments:
  width:
	type: int
  height:
	type: int
  title:
	type: string
	optional: true 

{% block user_body %}
EG_RETURN_INT(newtCenteredWindow((long)(EG_IVAL(width))
	,(long)(EG_IVAL(height)),EG_STRVAL(title)));
{% endblock %}

A minimal function taking no argument and always returning null:

# void newt_bell()
# This function has no metadata to define, as it has no argument

{% block body %}
newt_Bell();
{% endblock %}

Passing arg by ref : Here is a sample function taking 2 arguments of type 'float', one by ref, and the second by value. It replaces the first argument with the product of both args.

#PHP: void dummy_mult(&$var1, $var2)

arguments:
  var1:
    type: float
    byref: true
  var2:
    type: float

{% block body %}
/* Could be written in one line with no intermediate var... */
eg_float product;

product = EG_FVAL(var1) * EG_FVAL(var2);
EG_ARG_SET_FLOAT(var1, product); // set var1 returned value
{% endblock %}

An example of a resource definition file. This one is taken from xmlrpc. The file is named 'resource.xmlrpc_server.c'.

Note that this extension does not create persistent resources. So, we don't have to check for the 'persistent' flag when freeing data.

display_string: "xmlrpc server"

#------ Data ---------------

{% block user_resource_struct %}
zval* method_map;
zval* introspection_map;
XMLRPC_SERVER server_ptr;
{% endblock %}

{# ----- Destructor ------ #}

{% user_resource_dtor %}
XMLRPC_ServerDestroy(ptr->server_ptr);

zval_dtor(ptr->method_map);
FREE_ZVAL(ptr->method_map);

zval_dtor(ptr->introspection_map);
FREE_ZVAL(ptr->introspection_map);
{% endblock %}

Now, the resource creation function :

# resource xmlrpc_server_create(void)
#Creates an xmlrpc server

{% block user_body %}
zval *method_map, *introspection_map;
EG_RESOURCE_STRUCT(xmlrpc_server) *server;

if (return_value_used) {
	server = EG_RESOURCE_ALLOC(xmlrpc_server,0);

	MAKE_STD_ZVAL(method_map);
	array_init(method_map);
	server->method_map = method_map;

	MAKE_STD_ZVAL(introspection_map);
	array_init(introspection_map);
	server->introspection_map = introspection_map;
	
	server->server_ptr = XMLRPC_ServerCreate();

	XMLRPC_ServerRegisterIntrospectionCallback(server->server_ptr, php_xmlrpc_introspection_callback);

	/* store for later use */

	EG_RETVAL_RESOURCE(EG_RESOURCE_REGISTER(server));
}
{% endblock %}

and the resource destruction function :

# bool xmlrpc_server_destroy(resource server)
# Destroys server resources

arguments:
  server:
	type: resource(xmlrpc_server)

{% block user_body %}
	ZEND_RESULT_CODE status;

	if (!EG_ARG_RESOURCE_IS_VALID(server)) EG_EXCEPTION_ABORT("Invalid resource");

	status=EG_RESOURCE_DELETE(EG_RESOURCE(server));
	EG_RETVAL_BOOL(status==SUCCESS);
{% endblock %}