Port PHP5 binding generator to Python 3

This commit is contained in:
Benjamin Dauvergne 2014-12-03 02:02:31 +01:00
parent a231eaff33
commit b73f8f3ce5
4 changed files with 254 additions and 251 deletions

View File

@ -19,9 +19,9 @@
# along with this program; if not, see <http://www.gnu.org/licenses/>.
import os
from wrapper_source import WrapperSource
from wrapper_header import WrapperHeader
from php_code import PhpCode
from php5.wrapper_source import WrapperSource
from php5.wrapper_header import WrapperHeader
from php5.php_code import PhpCode
class Binding:
def __init__(self, binding_data):

View File

@ -20,6 +20,7 @@
import re
import sys
import six
from utils import *
@ -39,7 +40,7 @@ class PhpCode:
self.generate_footer()
def generate_header(self):
print >> self.fd, '''\
six.print_('''\
<?php
/* this file has been generated automatically; do not edit */
@ -100,7 +101,7 @@ function lassoGetRequestTypeFromSoapMsg($mesg) {
function lassoRegisterIdWsf2DstService($prefix, $href) {
lasso_register_idwsf2_dst_service($prefix, $href);
}
'''
''', file=self.fd)
def generate_class(self, klass):
class_name = klass.name
@ -110,27 +111,27 @@ function lassoRegisterIdWsf2DstService($prefix, $href) {
else:
inheritence = ' extends LassoObject'
print >> self.fd, '/**'
print >> self.fd, ' * @package Lasso'
print >> self.fd, ' */'
print >> self.fd, 'class %(class_name)s%(inheritence)s {' % locals()
six.print_('/**', file=self.fd)
six.print_(' * @package Lasso', file=self.fd)
six.print_(' */', file=self.fd)
six.print_('class %(class_name)s%(inheritence)s {' % locals(), file=self.fd)
if klass.members or klass.methods:
self.generate_constructors(klass)
self.generate_getters_and_setters(klass)
self.generate_methods(klass)
print >> self.fd, '}'
print >> self.fd, ''
six.print_('}', file=self.fd)
six.print_('', file=self.fd)
# Add a special class to get an object instance without initialising
print >> self.fd, '/**'
print >> self.fd, ' * @package Lasso'
print >> self.fd, ' */'
print >> self.fd, 'class %(class_name)sNoInit extends %(class_name)s {' % locals()
print >> self.fd, ' public function __construct() {}'
print >> self.fd, '}'
print >> self.fd, ''
six.print_('/**', file=self.fd)
six.print_(' * @package Lasso', file=self.fd)
six.print_(' */', file=self.fd)
six.print_('class %(class_name)sNoInit extends %(class_name)s {' % locals(), file=self.fd)
six.print_(' public function __construct() {}', file=self.fd)
six.print_('}', file=self.fd)
six.print_('', file=self.fd)
def generate_constructors(self, klass):
method_prefix = format_as_underscored(klass.name) + '_'
@ -155,13 +156,13 @@ function lassoRegisterIdWsf2DstService($prefix, $href) {
c_args = ', '.join(c_args)
# XXX: could check $this->_cptr->typename to see if it got the
# right class type
print >> self.fd, ' public $_cptr = null;'
print >> self.fd, ''
print >> self.fd, ' public function __construct(%s) {' % php_args
print >> self.fd, ' $this->_cptr = %s(%s);' % (m.name, c_args)
print >> self.fd, ' if (is_null($this->_cptr)) { throw new Exception("Constructor for ', klass.name, ' failed "); }'
print >> self.fd, ' }'
print >> self.fd, ''
six.print_(' public $_cptr = null;', file=self.fd)
six.print_('', file=self.fd)
six.print_(' public function __construct(%s) {' % php_args, file=self.fd)
six.print_(' $this->_cptr = %s(%s);' % (m.name, c_args), file=self.fd)
six.print_(' if (is_null($this->_cptr)) { throw new Exception("Constructor for ', klass.name, ' failed "); }', file=self.fd)
six.print_(' }', file=self.fd)
six.print_('', file=self.fd)
elif name.startswith(method_prefix) and m.args \
and clean_type(unconstify(m.args[0][0])) != klass.name:
@ -188,10 +189,10 @@ function lassoRegisterIdWsf2DstService($prefix, $href) {
c_args.append('$%s' % arg_name)
php_args = ', '.join(php_args)
c_args = ', '.join(c_args)
print >>self.fd, ' public static function %s(%s) {' % (php_name, php_args)
print >>self.fd, ' return cptrToPhp(%s(%s));' % (m.name, c_args)
print >>self.fd, ' }'
print >>self.fd, ''
six.print_(' public static function %s(%s) {' % (php_name, php_args), file=self.fd)
six.print_(' return cptrToPhp(%s(%s));' % (m.name, c_args), file=self.fd)
six.print_(' }', file=self.fd)
six.print_('', file=self.fd)
@ -199,49 +200,49 @@ function lassoRegisterIdWsf2DstService($prefix, $href) {
d = { 'type': arg_type(m), 'name': format_as_camelcase(arg_name(m)),
'docstring': self.get_docstring_return_type(arg_type(m)), 'class': c.name }
print >> self.fd, ''' /**'
six.print_(''' /**', file=self.fd)
* @return %(docstring)s
*/
protected function get_%(name)s() {''' % d
print >> self.fd, ' $t = %(class)s_%(name)s_get($this->_cptr);' % d
protected function get_%(name)s() {''' % d, file=self.fd)
six.print_(' $t = %(class)s_%(name)s_get($this->_cptr);' % d, file=self.fd)
if is_object(m):
print >> self.fd, ' $t = cptrToPhp($t);'
six.print_(' $t = cptrToPhp($t);', file=self.fd)
elif (is_glist(m) or is_hashtable(m)) and is_object(element_type(m)):
print >> self.fd, ' foreach ($t as $key => $item) {'
print >> self.fd, ' $t[$key] = cptrToPhp($item);'
print >> self.fd, ' }'
six.print_(' foreach ($t as $key => $item) {', file=self.fd)
six.print_(' $t[$key] = cptrToPhp($item);', file=self.fd)
six.print_(' }', file=self.fd)
elif is_hashtable(m) or (is_glist(m) and (is_cstring(element_type(m)) \
or is_xml_node(element_type(m)))) or is_int(m, self.binding_data) \
or is_boolean(m) or is_cstring(m) or is_xml_node(m):
pass
else:
raise Exception('Cannot generate a Php getter %s.%s' % (c,m))
print >> self.fd, ' return $t;'
print >>self.fd, ' }'
six.print_(' return $t;', file=self.fd)
six.print_(' }', file=self.fd)
def generate_setter(self, c, m):
d = { 'type': arg_type(m), 'name': format_as_camelcase(arg_name(m)),
'docstring': self.get_docstring_return_type(arg_type(m)), 'class': c.name }
print >> self.fd, ' protected function set_%(name)s($value) {' % d
six.print_(' protected function set_%(name)s($value) {' % d, file=self.fd)
if is_object(m):
print >> self.fd, ' $value = $value->_cptr;'
six.print_(' $value = $value->_cptr;', file=self.fd)
elif (is_glist(m) or is_hashtable(m)) and is_object(element_type(m)):
print >> self.fd, ' $array = array();'
print >> self.fd, ' if (!is_null($value)) {'
print >> self.fd, ' foreach ($value as $key => $item) {'
print >> self.fd, ' $array[$key] = $item->_cptr;'
print >> self.fd, ' }'
print >> self.fd, ' }'
print >> self.fd, ' $value = $array;'
six.print_(' $array = array();', file=self.fd)
six.print_(' if (!is_null($value)) {', file=self.fd)
six.print_(' foreach ($value as $key => $item) {', file=self.fd)
six.print_(' $array[$key] = $item->_cptr;', file=self.fd)
six.print_(' }', file=self.fd)
six.print_(' }', file=self.fd)
six.print_(' $value = $array;', file=self.fd)
elif is_hashtable(m) or (is_glist(m) and (is_cstring(element_type(m)) \
or is_xml_node(element_type(m)))) or is_int(m, self.binding_data) \
or is_boolean(m) or is_cstring(m) or is_xml_node(m):
pass
else:
raise Exception('Cannot generate a Php setter %s.%s' % (c,m))
print >> self.fd, ' %(class)s_%(name)s_set($this->_cptr, $value);' % d
print >> self.fd, ' }'
print >> self.fd, ''
six.print_(' %(class)s_%(name)s_set($this->_cptr, $value);' % d, file=self.fd)
six.print_(' }', file=self.fd)
six.print_('', file=self.fd)
def generate_getters_and_setters(self, klass):
for m in klass.members:
@ -269,27 +270,27 @@ function lassoRegisterIdWsf2DstService($prefix, $href) {
mname = re.match(r'lasso_.*_get_(\w+)', meth_name).group(1)
mname = format_as_camelcase(mname)
print >> self.fd, ' /**'
print >> self.fd, ' * @return %s' % self.get_docstring_return_type(m.return_type)
print >> self.fd, ' */'
print >> self.fd, ' protected function get_%s() {' % mname
six.print_(' /**', file=self.fd)
six.print_(' * @return %s' % self.get_docstring_return_type(m.return_type), file=self.fd)
six.print_(' */', file=self.fd)
six.print_(' protected function get_%s() {' % mname, file=self.fd)
if self.is_object(m.return_type):
print >> self.fd, ' $cptr = %s($this->_cptr);' % meth_name
print >> self.fd, ' if (! is_null($cptr)) {'
print >> self.fd, ' return cptrToPhp($cptr);'
print >> self.fd, ' }'
print >> self.fd, ' return null;'
six.print_(' $cptr = %s($this->_cptr);' % meth_name, file=self.fd)
six.print_(' if (! is_null($cptr)) {', file=self.fd)
six.print_(' return cptrToPhp($cptr);', file=self.fd)
six.print_(' }', file=self.fd)
six.print_(' return null;', file=self.fd)
else:
print >> self.fd, ' return %s($this->_cptr);' % meth_name
print >> self.fd, ' }'
six.print_(' return %s($this->_cptr);' % meth_name, file=self.fd)
six.print_(' }', file=self.fd)
if setter:
print >> self.fd, ' protected function set_%s($value) {' % mname
six.print_(' protected function set_%s($value) {' % mname, file=self.fd)
if self.is_object(m.return_type):
print >> self.fd, ' %s($this->_cptr, $value->_cptr);' % setter.name
six.print_(' %s($this->_cptr, $value->_cptr);' % setter.name, file=self.fd)
else:
print >> self.fd, ' %s($this->_cptr, $value);' % setter.name
print >> self.fd, ' }'
print >> self.fd, ''
six.print_(' %s($this->_cptr, $value);' % setter.name, file=self.fd)
six.print_(' }', file=self.fd)
six.print_('', file=self.fd)
# second pass on methods, real methods
method_prefix = format_as_underscored(klass.name) + '_'
@ -354,26 +355,26 @@ function lassoRegisterIdWsf2DstService($prefix, $href) {
c_args = ''
if m.docstring:
print >> self.fd, self.generate_docstring(m, mname, 4)
print >> self.fd, ' public function %s(%s) {' % (
format_underscore_as_camelcase(mname), php_args)
six.print_(self.generate_docstring(m, mname, 4), file=self.fd)
six.print_(' public function %s(%s) {' % (
format_underscore_as_camelcase(mname), php_args), file=self.fd)
if m.return_type == 'void':
print >> self.fd, ' %s($this->_cptr%s);' % (cname, c_args)
six.print_(' %s($this->_cptr%s);' % (cname, c_args), file=self.fd)
elif is_rc(m.return_type):
print >> self.fd, ' $rc = %s($this->_cptr%s);' % (cname, c_args)
print >> self.fd, ' if ($rc == 0) {'
print >> self.fd, ' return 0;'
print >> self.fd, ' } else if ($rc > 0) {' # recoverable error
print >> self.fd, ' return $rc;'
print >> self.fd, ' } else if ($rc < 0) {' # unrecoverable error
print >> self.fd, ' LassoError::throw_on_rc($rc);'
print >> self.fd, ' }'
six.print_(' $rc = %s($this->_cptr%s);' % (cname, c_args), file=self.fd)
six.print_(' if ($rc == 0) {', file=self.fd)
six.print_(' return 0;', file=self.fd)
six.print_(' } else if ($rc > 0) {', file=self.fd) # recoverable error
six.print_(' return $rc;', file=self.fd)
six.print_(' } else if ($rc < 0) {', file=self.fd) # unrecoverable error
six.print_(' LassoError::throw_on_rc($rc);', file=self.fd)
six.print_(' }', file=self.fd)
else:
print >> self.fd, ' return %s($this->_cptr%s);' % (cname, c_args)
print >> self.fd, ' }'
print >> self.fd, ''
six.print_(' return %s($this->_cptr%s);' % (cname, c_args), file=self.fd)
six.print_(' }', file=self.fd)
six.print_('', file=self.fd)
print >> self.fd, ''
six.print_('', file=self.fd)
def generate_docstring(self, func, method_name, indent):
docstring = func.docstring.orig_docstring
@ -433,12 +434,12 @@ function lassoRegisterIdWsf2DstService($prefix, $href) {
cat = exc_cat.attrib.get('name')
done_cats.append(cat)
parent_cat = exc_cat.attrib.get('parent', '')
print >> self.fd, '''\
six.print_('''\
/**
* @package Lasso
*/
class Lasso%sError extends Lasso%sError {}
''' % (cat, parent_cat)
''' % (cat, parent_cat), file=self.fd)
exceptions_dict = {}
@ -458,35 +459,35 @@ class Lasso%sError extends Lasso%sError {}
else:
parent_cat = ''
print >> self.fd, '''\
six.print_('''\
/**
* @package Lasso
*/
class Lasso%sError extends Lasso%sError {}
''' % (cat, parent_cat)
''' % (cat, parent_cat), file=self.fd)
if detail not in exceptions_dict:
print >> self.fd, '''\
six.print_('''\
/**
* @package Lasso
*/
class Lasso%sError extends Lasso%sError {
protected $code = %s;
}
''' % (detail, cat, c[1])
''' % (detail, cat, c[1]), file=self.fd)
exceptions_dict[detail] = c[1]
print >> self.fd, '''\
six.print_('''\
/**
* @package Lasso
*/
class LassoError extends Exception {
private static $exceptions_dict = array('''
private static $exceptions_dict = array(''', file=self.fd)
for k, v in exceptions_dict.items():
print >> self.fd, ' %s => "Lasso%sError",' % (v, k)
six.print_(' %s => "Lasso%sError",' % (v, k), file=self.fd)
print >> self.fd, '''\
six.print_('''\
);
public static function throw_on_rc($rc) {
@ -497,9 +498,9 @@ class LassoError extends Exception {
throw new $exception(strError($rc), $rc);
}
}
'''
''', file=self.fd)
def generate_footer(self):
print >> self.fd, '''\
?>'''
six.print_('''\
?>''', file=self.fd)

View File

@ -17,6 +17,7 @@
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.
import six
class WrapperHeader:
def __init__(self, binding_data, fd, functions_list):
@ -31,7 +32,7 @@ class WrapperHeader:
def generate_header(self):
# FIXME: Get the current version and name
print >> self.fd, '''\
six.print_('''\
/* this file has been generated automatically; do not edit */
#include "../../config.h"
@ -46,18 +47,18 @@ class WrapperHeader:
PHP_MINIT_FUNCTION(lasso);
PHP_MSHUTDOWN_FUNCTION(lasso);
'''
''', file=self.fd)
def generate_functions_list(self):
for m in self.functions_list:
print >> self.fd, 'PHP_FUNCTION(%s);' % m
print >> self.fd, ''
six.print_('PHP_FUNCTION(%s);' % m, file=self.fd)
six.print_('', file=self.fd)
def generate_footer(self):
print >> self.fd, '''\
six.print_('''\
extern zend_module_entry lasso_module_entry;
#define phpext_lasso_ptr &lasso_module_entry
#endif
'''
''', file=self.fd)

View File

@ -20,6 +20,7 @@
import sys
import os
import six
from utils import *
@ -52,43 +53,43 @@ class WrapperSource:
self.functions_list.append('lasso_init')
self.functions_list.append('lasso_shutdown')
print >> self.fd, '''\
six.print_('''\
/* this file has been generated automatically; do not edit */
'''
''', file=self.fd)
print >> self.fd, open(os.path.join(self.src_dir,'wrapper_source_top.c')).read()
six.print_(open(os.path.join(self.src_dir,'wrapper_source_top.c')).read(), file=self.fd)
for h in self.binding_data.headers:
print >> self.fd, '#include <%s>' % h
print >> self.fd, ''
six.print_('#include <%s>' % h, file=self.fd)
six.print_('', file=self.fd)
print >> self.fd, '''\
six.print_('''\
PHP_MINIT_FUNCTION(lasso)
{
le_lasso_server = zend_register_list_destructors_ex(php_gobject_generic_destructor, NULL, PHP_LASSO_SERVER_RES_NAME, module_number);
lasso_init();
'''
''', file=self.fd)
def generate_constants(self):
print >> self.fd, ' /* Constants (both enums and defines) */'
six.print_(' /* Constants (both enums and defines) */', file=self.fd)
for c in self.binding_data.constants:
if c[0] == 'i':
print >> self.fd, ' REGISTER_LONG_CONSTANT("%s", %s, CONST_CS|CONST_PERSISTENT);' % (c[1], c[1])
six.print_(' REGISTER_LONG_CONSTANT("%s", %s, CONST_CS|CONST_PERSISTENT);' % (c[1], c[1]), file=self.fd)
elif c[0] == 's':
print >> self.fd, ' REGISTER_STRING_CONSTANT("%s", (char*) %s, CONST_CS|CONST_PERSISTENT);' % (c[1], c[1])
six.print_(' REGISTER_STRING_CONSTANT("%s", (char*) %s, CONST_CS|CONST_PERSISTENT);' % (c[1], c[1]), file=self.fd)
elif c[0] == 'b':
print >> self.fd, '''\
six.print_('''\
#ifdef %s
REGISTER_LONG_CONSTANT("%s", 1, CONST_CS|CONST_PERSISTENT);
#else
REGISTER_LONG_CONSTANT("%s", 0, CONST_CS|CONST_PERSISTENT);
#endif''' % (c[1], c[1], c[1])
#endif''' % (c[1], c[1], c[1]), file=self.fd)
else:
print >> sys.stderr, 'E: unknown constant type: %r' % c[0]
print >> self.fd, ''
six.print_('E: unknown constant type: %r' % c[0], file=sys.stderr)
six.print_('', file=self.fd)
def generate_middle(self):
print >> self.fd, '''\
six.print_('''\
return SUCCESS;
}
@ -98,7 +99,7 @@ PHP_MSHUTDOWN_FUNCTION(lasso)
return SUCCESS;
}
'''
''', file=self.fd)
def set_zval(self, zval_name, c_variable, type, free = False):
'''Emit code to set a zval* of name zval_name, from the value of the C variable called c_variable type, type.
@ -106,21 +107,21 @@ PHP_MSHUTDOWN_FUNCTION(lasso)
# first we free the previous value
p = (zval_name, c_variable)
q = { 'zval_name' : zval_name, 'c_variable' : c_variable }
print >> self.fd, ' zval_dtor(%s);' % zval_name
six.print_(' zval_dtor(%s);' % zval_name, file=self.fd)
if is_pointer(type):
print >> self.fd, ' if (! %s) {' % c_variable
print >> self.fd, ' ZVAL_NULL(%s);' % zval_name
print >> self.fd, ' } else {'
six.print_(' if (! %s) {' % c_variable, file=self.fd)
six.print_(' ZVAL_NULL(%s);' % zval_name, file=self.fd)
six.print_(' } else {', file=self.fd)
if is_int(type, self.binding_data):
print >> self.fd, ' ZVAL_LONG(%s, %s);' % p
six.print_(' ZVAL_LONG(%s, %s);' % p, file=self.fd)
elif is_boolean(type):
print >> self.fd, ' ZVAL_BOOL(%s, %s);' % p
six.print_(' ZVAL_BOOL(%s, %s);' % p, file=self.fd)
elif is_cstring(type):
print >> self.fd, ' ZVAL_STRING(%s, (char*)%s, 1);' % p
six.print_(' ZVAL_STRING(%s, (char*)%s, 1);' % p, file=self.fd)
if free and not is_const(type):
print >> self.fd, 'g_free(%s)' % c_variable
six.print_('g_free(%s)' % c_variable, file=self.fd)
elif arg_type(type) == 'xmlNode*':
print >> self.fd, '''\
six.print_('''\
{
char* xmlString = get_string_from_xml_node(%(c_variable)s);
if (xmlString) {
@ -129,7 +130,7 @@ PHP_MSHUTDOWN_FUNCTION(lasso)
ZVAL_NULL(%(zval_name)s);
}
}
''' % q
''' % q, file=self.fd)
elif is_glist(type):
elem_type = make_arg(element_type(type))
if not arg_type(elem_type):
@ -145,27 +146,27 @@ PHP_MSHUTDOWN_FUNCTION(lasso)
free_function = 'g_list_free(%(c_variable)s);'
else:
raise Exception('unknown element-type: ' + repr(type))
print >> self.fd, ' %s((GList*)%s, &%s);' % (function, c_variable, zval_name)
six.print_(' %s((GList*)%s, &%s);' % (function, c_variable, zval_name), file=self.fd)
if free:
print >> self.fd, ' ', free_function % q
six.print_(' ', free_function % q, file=self.fd)
elif is_object(type):
print >> self.fd, '''\
six.print_('''\
if (G_IS_OBJECT(%(c_variable)s)) {
PhpGObjectPtr *obj = PhpGObjectPtr_New(G_OBJECT(%(c_variable)s));
ZEND_REGISTER_RESOURCE(%(zval_name)s, obj, le_lasso_server);
} else {
ZVAL_NULL(%(zval_name)s);
}''' % q
}''' % q, file=self.fd)
if free:
print >> self.fd, '''\
six.print_('''\
if (%(c_variable)s) {
g_object_unref(%(c_variable)s); // If constructor ref is off by one'
}''' % q
}''' % q, file=self.fd)
else:
raise Exception('unknown type: ' + repr(type) + unconstify(arg_type(type)))
if is_pointer(type):
print >> self.fd, ' }'
six.print_(' }', file=self.fd)
@ -174,20 +175,20 @@ PHP_MSHUTDOWN_FUNCTION(lasso)
return
if is_boolean(arg):
print >> self.fd, ' RETVAL_BOOL(return_c_value);'
six.print_(' RETVAL_BOOL(return_c_value);', file=self.fd)
elif is_int(arg, self.binding_data):
print >> self.fd, ' RETVAL_LONG(return_c_value);'
six.print_(' RETVAL_LONG(return_c_value);', file=self.fd)
elif is_cstring(arg):
print >> self.fd, '''\
six.print_('''\
if (return_c_value) {
RETVAL_STRING((char*)return_c_value, 1);
} else {
RETVAL_NULL();
}'''
}''', file=self.fd)
if free:
print >> self.fd, ' free(return_c_value);'
six.print_(' free(return_c_value);', file=self.fd)
elif is_xml_node(arg):
print >> self.fd, '''\
six.print_('''\
{
char* xmlString = get_string_from_xml_node(return_c_value);
if (xmlString) {
@ -196,54 +197,54 @@ PHP_MSHUTDOWN_FUNCTION(lasso)
RETVAL_NULL();
}
}
'''
''', file=self.fd)
if free:
print >> self.fd, ' lasso_release_xml_node(return_c_value);'
six.print_(' lasso_release_xml_node(return_c_value);', file=self.fd)
elif is_glist(arg):
el_type = element_type(arg)
if is_cstring(el_type):
print >> self.fd, '''\
six.print_('''\
set_array_from_list_of_strings((GList*)return_c_value, &return_value);
'''
''', file=self.fd)
if free:
print >> self.fd, ' lasso_release_list_of_strings(return_c_value);'
six.print_(' lasso_release_list_of_strings(return_c_value);', file=self.fd)
elif is_xml_node(el_type):
print >> self.fd, '''\
six.print_('''\
set_array_from_list_of_xmlnodes((GList*)return_c_value, &return_value);
'''
''', file=self.fd)
if free or is_transfer_full(arg):
print >> self.fd, ' lasso_release_list_of_xml_node(return_c_value);'
six.print_(' lasso_release_list_of_xml_node(return_c_value);', file=self.fd)
elif is_object(el_type):
print >> self.fd, '''\
six.print_('''\
set_array_from_list_of_objects((GList*)return_c_value, &return_value);
'''
''', file=self.fd)
if free:
print >> self.fd, ' lasso_release_list_of_gobjects(return_c_value);'
six.print_(' lasso_release_list_of_gobjects(return_c_value);', file=self.fd)
else:
raise Exception('cannot return value for %s' % (arg,))
elif is_hashtable(arg):
el_type = element_type(arg)
if is_object(el_type):
print >> self.fd, '''\
six.print_('''\
set_array_from_hashtable_of_objects(return_c_value, &return_value);
'''
''', file=self.fd)
else:
if not is_cstring(arg):
print >>sys.stderr, 'W: %s has no explicit string annotation' % (arg,)
print >> self.fd, '''\
six.print_('''\
set_array_from_hashtable_of_strings(return_c_value, &return_value);
'''
''', file=self.fd)
elif is_object(arg):
print >> self.fd, '''\
six.print_('''\
if (return_c_value) {
PhpGObjectPtr *self;
self = PhpGObjectPtr_New(G_OBJECT(return_c_value));
ZEND_REGISTER_RESOURCE(return_value, self, le_lasso_server);
} else {
RETVAL_NULL();
}'''
}''', file=self.fd)
if free:
print >> self.fd, ' lasso_release_gobject(return_c_value);'
six.print_(' lasso_release_gobject(return_c_value);', file=self.fd)
else:
raise Exception('cannot return value for %s' % (arg,))
@ -255,26 +256,26 @@ PHP_MSHUTDOWN_FUNCTION(lasso)
else:
name = m.name
self.functions_list.append(name)
print >> self.fd, '''PHP_FUNCTION(%s)
{''' % name
six.print_('''PHP_FUNCTION(%s)
{''' % name, file=self.fd)
parse_tuple_format = []
parse_tuple_args = []
for arg in m.args:
if is_out(arg):
print >> self.fd, ' zval *php_out_%s = NULL;' % arg_name(arg)
print >> self.fd, ' %s %s;' % (var_type(arg), arg_name(arg))
six.print_(' zval *php_out_%s = NULL;' % arg_name(arg), file=self.fd)
six.print_(' %s %s;' % (var_type(arg), arg_name(arg)), file=self.fd)
parse_tuple_format.append('z!')
parse_tuple_args.append('&php_out_%s' % arg_name(arg))
elif is_cstring(arg):
parse_tuple_format.append('s!')
parse_tuple_args.append('&%s_str, &%s_len' % (arg_name(arg), arg_name(arg)))
print >> self.fd, ' %s %s = NULL;' % ('char*', arg_name(arg))
print >> self.fd, ' %s %s_str = NULL;' % ('char*', arg_name(arg))
print >> self.fd, ' %s %s_len = 0;' % ('int', arg_name(arg))
six.print_(' %s %s = NULL;' % ('char*', arg_name(arg)), file=self.fd)
six.print_(' %s %s_str = NULL;' % ('char*', arg_name(arg)), file=self.fd)
six.print_(' %s %s_len = 0;' % ('int', arg_name(arg)), file=self.fd)
elif is_int(arg, self.binding_data) or is_boolean(arg):
parse_tuple_format.append('l')
parse_tuple_args.append('&%s' % arg_name(arg))
print >> self.fd, ' %s %s;' % ('long', arg_name(arg))
six.print_(' %s %s;' % ('long', arg_name(arg)), file=self.fd)
elif is_time_t_pointer(arg):
parse_tuple_format.append('l')
parse_tuple_args.append('&%s' % (arg_name(arg),))
@ -282,59 +283,59 @@ PHP_MSHUTDOWN_FUNCTION(lasso)
elif is_xml_node(arg):
parse_tuple_format.append('s!')
parse_tuple_args.append('&%s_str, &%s_len' % (arg_name(arg), arg_name(arg)))
print >> self.fd, ' %s %s = NULL;' % ('xmlNode*', arg_name(arg))
print >> self.fd, ' %s %s_str = NULL;' % ('char*', arg_name(arg))
print >> self.fd, ' %s %s_len = 0;' % ('int', arg_name(arg))
six.print_(' %s %s = NULL;' % ('xmlNode*', arg_name(arg)), file=self.fd)
six.print_(' %s %s_str = NULL;' % ('char*', arg_name(arg)), file=self.fd)
six.print_(' %s %s_len = 0;' % ('int', arg_name(arg)), file=self.fd)
elif is_glist(arg):
parse_tuple_format.append('a!')
parse_tuple_args.append('&zval_%s' % arg_name(arg))
print >> self.fd, ' %s zval_%s = NULL;' % ('zval*', arg_name(arg))
print >> self.fd, ' %s %s = NULL;' % ('GList*', arg_name(arg))
six.print_(' %s zval_%s = NULL;' % ('zval*', arg_name(arg)), file=self.fd)
six.print_(' %s %s = NULL;' % ('GList*', arg_name(arg)), file=self.fd)
elif is_object(arg):
parse_tuple_format.append('r')
parse_tuple_args.append('&zval_%s' % arg_name(arg))
print >> self.fd, ' %s %s = NULL;' % (arg_type(arg), arg_name(arg))
print >> self.fd, ' %s zval_%s = NULL;' % ('zval*', arg_name(arg))
print >> self.fd, ' %s cvt_%s = NULL;' % ('PhpGObjectPtr*', arg_name(arg))
six.print_(' %s %s = NULL;' % (arg_type(arg), arg_name(arg)), file=self.fd)
six.print_(' %s zval_%s = NULL;' % ('zval*', arg_name(arg)), file=self.fd)
six.print_(' %s cvt_%s = NULL;' % ('PhpGObjectPtr*', arg_name(arg)), file=self.fd)
else:
raise Exception('Unsupported type %s %s' % (arg, m))
if m.return_type:
print >> self.fd, ' %s return_c_value;' % m.return_type
six.print_(' %s return_c_value;' % m.return_type, file=self.fd)
if m.return_type is not None and self.is_object(m.return_arg):
print >> self.fd, ' G_GNUC_UNUSED PhpGObjectPtr *self;'
print >> self.fd, ''
six.print_(' G_GNUC_UNUSED PhpGObjectPtr *self;', file=self.fd)
six.print_('', file=self.fd)
parse_tuple_args = ', '.join(parse_tuple_args)
if parse_tuple_args:
parse_tuple_args = ', ' + parse_tuple_args
print >> self.fd, '''\
six.print_('''\
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "%s"%s) == FAILURE) {
RETURN_FALSE;
}
''' % (''.join(parse_tuple_format), parse_tuple_args)
''' % (''.join(parse_tuple_format), parse_tuple_args), file=self.fd)
for f, arg in zip(parse_tuple_format, m.args):
if is_out(arg):
continue
elif is_xml_node(arg):
print >> self.fd, '''\
%(name)s = get_xml_node_from_string(%(name)s_str);''' % {'name': arg[1]}
six.print_('''\
%(name)s = get_xml_node_from_string(%(name)s_str);''' % {'name': arg[1]}, file=self.fd)
elif f.startswith('s'):
print >> self.fd, '''\
%(name)s = %(name)s_str;''' % {'name': arg[1]}
six.print_('''\
%(name)s = %(name)s_str;''' % {'name': arg[1]}, file=self.fd)
elif f.startswith('r'):
print >> self.fd, ' ZEND_FETCH_RESOURCE(cvt_%s, PhpGObjectPtr *, &zval_%s, -1, PHP_LASSO_SERVER_RES_NAME, le_lasso_server);' % (arg[1], arg[1])
print >> self.fd, ' %s = (%s)cvt_%s->obj;' % (arg[1], arg[0], arg[1])
six.print_(' ZEND_FETCH_RESOURCE(cvt_%s, PhpGObjectPtr *, &zval_%s, -1, PHP_LASSO_SERVER_RES_NAME, le_lasso_server);' % (arg[1], arg[1]), file=self.fd)
six.print_(' %s = (%s)cvt_%s->obj;' % (arg[1], arg[0], arg[1]), file=self.fd)
elif f.startswith('a'):
el_type = element_type(arg)
if is_cstring(el_type):
print >> self.fd, ' %(name)s = get_list_from_array_of_strings(zval_%(name)s);' % {'name': arg[1]}
six.print_(' %(name)s = get_list_from_array_of_strings(zval_%(name)s);' % {'name': arg[1]}, file=self.fd)
elif is_object(el_type):
print >> self.fd, ' %(name)s = get_list_from_array_of_objects(zval_%(name)s);' % {'name': arg[1]}
six.print_(' %(name)s = get_list_from_array_of_objects(zval_%(name)s);' % {'name': arg[1]}, file=self.fd)
else:
print >> sys.stderr, 'E: In %(function)s arg %(name)s is of type GList<%(elem)s>' % { 'function': m.name, 'name': arg[1], 'elem': el_type }
six.print_('E: In %(function)s arg %(name)s is of type GList<%(elem)s>' % { 'function': m.name, 'name': arg[1], 'elem': el_type }, file=sys.stderr)
elif f == 'l':
pass
else:
@ -342,17 +343,17 @@ PHP_MSHUTDOWN_FUNCTION(lasso)
if m.return_type is not None:
print >> self.fd, ' return_c_value = ',
six.print_(' return_c_value = ', file=self.fd)
if 'new' in m.name:
print >> self.fd, '(%s)' % m.return_type,
six.print_('(%s)' % m.return_type, file=self.fd)
else:
print >> self.fd, ' ',
six.print_(' ', file=self.fd)
def special(x):
if is_time_t_pointer(x):
return '%(name)s ? &%(name)s : NULL' % { 'name': arg_name(x) }
else:
return ref_name(x)
print >> self.fd, '%s(%s);' % (m.name, ', '.join([special(x) for x in m.args]))
six.print_('%s(%s);' % (m.name, ', '.join([special(x) for x in m.args])), file=self.fd)
# Free the converted arguments
for f, arg in zip(parse_tuple_format, m.args):
@ -363,21 +364,21 @@ PHP_MSHUTDOWN_FUNCTION(lasso)
self.set_zval('php_out_%s' % argname, argname, unref_type(arg), free = free)
pass
elif argtype == 'xmlNode*':
print >> self.fd, ' xmlFree(%s);' % argname
six.print_(' xmlFree(%s);' % argname, file=self.fd)
elif f.startswith('a'):
el_type = element_type(arg)
if is_cstring(el_type):
print >> self.fd, ' if (%(name)s) {' % { 'name': arg[1] }
print >> self.fd, ' free_glist(&%(name)s,(GFunc)free);' % { 'name': arg[1] }
print >> self.fd, ' }'
six.print_(' if (%(name)s) {' % { 'name': arg[1] }, file=self.fd)
six.print_(' free_glist(&%(name)s,(GFunc)free);' % { 'name': arg[1] }, file=self.fd)
six.print_(' }', file=self.fd)
try:
self.return_value(m.return_arg, is_transfer_full(m.return_arg, default=True))
except:
raise Exception('Cannot return value for function %s' % m)
print >> self.fd, '}'
print >> self.fd, ''
six.print_('}', file=self.fd)
six.print_('', file=self.fd)
def generate_members(self, c):
for m in c.members:
@ -390,40 +391,40 @@ PHP_MSHUTDOWN_FUNCTION(lasso)
type = arg_type(m)
function_name = '%s_%s_get' % (klassname, format_as_camelcase(name))
print >> self.fd, '''PHP_FUNCTION(%s)
{''' % function_name
six.print_('''PHP_FUNCTION(%s)
{''' % function_name, file=self.fd)
self.functions_list.append(function_name)
print >> self.fd, ' %s return_c_value;' % type
print >> self.fd, ' %s* this;' % klassname
print >> self.fd, ' zval* zval_this;'
print >> self.fd, ' PhpGObjectPtr *cvt_this;'
print >> self.fd, ''
print >> self.fd, '''\
six.print_(' %s return_c_value;' % type, file=self.fd)
six.print_(' %s* this;' % klassname, file=self.fd)
six.print_(' zval* zval_this;', file=self.fd)
six.print_(' PhpGObjectPtr *cvt_this;', file=self.fd)
six.print_('', file=self.fd)
six.print_('''\
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zval_this) == FAILURE) {
RETURN_FALSE;
}
ZEND_FETCH_RESOURCE(cvt_this, PhpGObjectPtr *, &zval_this, -1, PHP_LASSO_SERVER_RES_NAME, le_lasso_server);
this = (%s*)cvt_this->obj;
''' % (klassname)
print >> self.fd, ' return_c_value = (%s)this->%s;' % (type, name)
''' % (klassname), file=self.fd)
six.print_(' return_c_value = (%s)this->%s;' % (type, name), file=self.fd)
self.return_value(m)
print >> self.fd, '}'
print >> self.fd, ''
six.print_('}', file=self.fd)
six.print_('', file=self.fd)
def generate_setter(self, c, m):
klassname = c.name
name = arg_name(m)
type = arg_type(m)
function_name = '%s_%s_set' % (klassname, format_as_camelcase(name))
print >> self.fd, '''PHP_FUNCTION(%s)
{''' % function_name
six.print_('''PHP_FUNCTION(%s)
{''' % function_name, file=self.fd)
self.functions_list.append(function_name)
print >> self.fd, ' %s* this;' % klassname
print >> self.fd, ' zval* zval_this;'
print >> self.fd, ' PhpGObjectPtr *cvt_this;'
six.print_(' %s* this;' % klassname, file=self.fd)
six.print_(' zval* zval_this;', file=self.fd)
six.print_(' PhpGObjectPtr *cvt_this;', file=self.fd)
# FIXME: This bloc should be factorised
parse_tuple_format = ''
@ -432,93 +433,93 @@ PHP_MSHUTDOWN_FUNCTION(lasso)
# arg_type = arg_type.replace('const ', '')
parse_tuple_format += 's'
parse_tuple_args.append('&%s_str, &%s_len' % (name, name))
print >> self.fd, ' %s %s_str = NULL;' % ('char*', name)
print >> self.fd, ' %s %s_len = 0;' % ('int', name)
six.print_(' %s %s_str = NULL;' % ('char*', name), file=self.fd)
six.print_(' %s %s_len = 0;' % ('int', name), file=self.fd)
elif is_int(m, self.binding_data) or is_boolean(m):
parse_tuple_format += 'l'
parse_tuple_args.append('&%s' % name)
print >> self.fd, ' %s %s;' % ('long', name)
six.print_(' %s %s;' % ('long', name), file=self.fd)
# Must also handle lists of Objects
elif is_glist(m) or is_hashtable(m):
parse_tuple_format += 'a'
parse_tuple_args.append('&zval_%s' % name)
print >> self.fd, ' %s zval_%s;' % ('zval*', name)
six.print_(' %s zval_%s;' % ('zval*', name), file=self.fd)
elif is_object(m):
parse_tuple_format += 'r'
parse_tuple_args.append('&zval_%s' % name)
print >> self.fd, ' %s zval_%s = NULL;' % ('zval*', name)
print >> self.fd, ' %s cvt_%s = NULL;' % ('PhpGObjectPtr*', name)
six.print_(' %s zval_%s = NULL;' % ('zval*', name), file=self.fd)
six.print_(' %s cvt_%s = NULL;' % ('PhpGObjectPtr*', name), file=self.fd)
else:
raise Exception('Cannot make a setter for %s.%s' % (c,m))
if parse_tuple_args:
parse_tuple_arg = parse_tuple_args[0]
else:
print >> self.fd, '}'
print >> self.fd, ''
six.print_('}', file=self.fd)
six.print_('', file=self.fd)
return
print >> self.fd, ''
print >> self.fd, '''\
six.print_('', file=self.fd)
six.print_('''\
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r%s", &zval_this, %s) == FAILURE) {
return;
}
''' % (parse_tuple_format, parse_tuple_arg)
''' % (parse_tuple_format, parse_tuple_arg), file=self.fd)
# Get 'this' object
print >> self.fd, '''\
six.print_('''\
ZEND_FETCH_RESOURCE(cvt_this, PhpGObjectPtr *, &zval_this, -1, PHP_LASSO_SERVER_RES_NAME, le_lasso_server);
this = (%s*)cvt_this->obj;
''' % klassname
''' % klassname, file=self.fd)
# Set new value
d = { 'name': name, 'type': type }
if is_int(m, self.binding_data) or is_boolean(m):
print >> self.fd, ' this->%s = %s;' % (name, name)
six.print_(' this->%s = %s;' % (name, name), file=self.fd)
elif is_cstring(m):
print >> self.fd, ' lasso_assign_string(this->%(name)s, %(name)s_str);' % d
six.print_(' lasso_assign_string(this->%(name)s, %(name)s_str);' % d, file=self.fd)
elif is_xml_node(m):
print >> self.fd, ' lasso_assign_new_xml_node(this->%(name)s, get_xml_node_from_string(%(name)s_str));' % d
six.print_(' lasso_assign_new_xml_node(this->%(name)s, get_xml_node_from_string(%(name)s_str));' % d, file=self.fd)
elif is_glist(m):
el_type = element_type(m)
if is_cstring(el_type):
print >> self.fd, ' lasso_assign_new_list_of_strings(this->%(name)s, get_list_from_array_of_strings(zval_%(name)s));' % d
six.print_(' lasso_assign_new_list_of_strings(this->%(name)s, get_list_from_array_of_strings(zval_%(name)s));' % d, file=self.fd)
elif is_xml_node(el_type):
print >> self.fd, ' lasso_assign_new_list_of_xml_node(this->%(name)s, get_list_from_array_of_xmlnodes(zval_%(name)s))' % d
six.print_(' lasso_assign_new_list_of_xml_node(this->%(name)s, get_list_from_array_of_xmlnodes(zval_%(name)s))' % d, file=self.fd)
elif is_object(el_type):
print >> self.fd, ' lasso_assign_new_list_of_gobjects(this->%(name)s, get_list_from_array_of_objects(zval_%(name)s));' % d
six.print_(' lasso_assign_new_list_of_gobjects(this->%(name)s, get_list_from_array_of_objects(zval_%(name)s));' % d, file=self.fd)
else:
raise Exception('Cannot create C setter for %s.%s' % (c,m))
elif is_hashtable(m):
el_type = element_type(m)
print >> self.fd, '''\
six.print_('''\
{
GHashTable *oldhash = this->%(name)s;''' % d
GHashTable *oldhash = this->%(name)s;''' % d, file=self.fd)
if is_object(el_type):
print >>self.fd, ' this->%(name)s = get_hashtable_from_array_of_objects(zval_%(name)s);' % d
six.print_(' this->%(name)s = get_hashtable_from_array_of_objects(zval_%(name)s);' % d, file=self.fd)
else:
print >>self.fd, ' this->%(name)s = get_hashtable_from_array_of_strings(zval_%(name)s);' % d
print >> self.fd, ' g_hash_table_destroy(oldhash);'
print >> self.fd, ' }'
six.print_(' this->%(name)s = get_hashtable_from_array_of_strings(zval_%(name)s);' % d, file=self.fd)
six.print_(' g_hash_table_destroy(oldhash);', file=self.fd)
six.print_(' }', file=self.fd)
elif is_object(m):
print >> self.fd, ' ZEND_FETCH_RESOURCE(cvt_%(name)s, PhpGObjectPtr*, &zval_%(name)s, -1, PHP_LASSO_SERVER_RES_NAME, le_lasso_server);' % d
print >> self.fd, ' lasso_assign_gobject(this->%(name)s, cvt_%(name)s->obj);' % d
six.print_(' ZEND_FETCH_RESOURCE(cvt_%(name)s, PhpGObjectPtr*, &zval_%(name)s, -1, PHP_LASSO_SERVER_RES_NAME, le_lasso_server);' % d, file=self.fd)
six.print_(' lasso_assign_gobject(this->%(name)s, cvt_%(name)s->obj);' % d, file=self.fd)
print >> self.fd, '}'
print >> self.fd, ''
six.print_('}', file=self.fd)
six.print_('', file=self.fd)
def generate_functions_list(self):
print >> self.fd, '''\
static zend_function_entry lasso_functions[] = {'''
six.print_('''\
static zend_function_entry lasso_functions[] = {''', file=self.fd)
for m in self.functions_list:
print >> self.fd, ' PHP_FE(%s, NULL)' % m
print >> self.fd, '''\
six.print_(' PHP_FE(%s, NULL)' % m, file=self.fd)
six.print_('''\
{NULL, NULL, NULL, 0, 0}
};
'''
''', file=self.fd)
def generate_footer(self):
print >> self.fd, '''\
six.print_('''\
zend_module_entry lasso_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
STANDARD_MODULE_HEADER,
@ -535,5 +536,5 @@ zend_module_entry lasso_module_entry = {
#endif
STANDARD_MODULE_PROPERTIES
};
'''
''', file=self.fd)