Merge branch 'gsoc2017-php7-classes-via-c-api' of https://github.com/nihal95/swig into gsoc2017-php7-classes-via-c-api
This commit is contained in:
commit
d15fe23c5f
3 changed files with 82 additions and 64 deletions
|
|
@ -163,8 +163,9 @@ static void print_creation_free_wrapper(int item_index) {
|
|||
|
||||
Printf(s_header, "/* Garbage Collection Method for class %s */\n",class_name);
|
||||
Printf(s_header, "void %s_free_storage(zend_object *object) {\n",class_name);
|
||||
Printf(s_header, " if(!object)\n\t return;\n");
|
||||
Printf(s_header, " swig_object_wrapper *obj = (swig_object_wrapper *)php_fetch_object(object);\n");
|
||||
Printf(s_header, " swig_object_wrapper *obj = 0;\n\n");
|
||||
Printf(s_header, " if(!object)\n\t return;\n\n");
|
||||
Printf(s_header, " obj = (swig_object_wrapper *)php_fetch_object(object);\n\n");
|
||||
Printf(s_header, " if(!obj->newobject)\n\t return;\n");
|
||||
|
||||
if (need_free) {
|
||||
|
|
@ -1107,7 +1108,7 @@ public:
|
|||
/* Magic methods __set, __get, __isset is declared here in the extension.
|
||||
The flag variable is used to decide whether all variables are read or not.
|
||||
*/
|
||||
void magic_method_setter(Node *n, bool flag) {
|
||||
void magic_method_setter(Node *n, bool flag, String *baseClassExtend) {
|
||||
|
||||
if (magic_set == NULL) {
|
||||
magic_set = NewStringEmpty();
|
||||
|
|
@ -1115,6 +1116,9 @@ public:
|
|||
magic_isset = NewStringEmpty();
|
||||
}
|
||||
if (flag) {
|
||||
if (Cmp(baseClassExtend, "Exception") == 0 || !is_class_wrapped(baseClassExtend)) {
|
||||
baseClassExtend = NULL;
|
||||
}
|
||||
Wrapper *f = NewWrapper();
|
||||
// Need arg info set for __get magic function with one variable.
|
||||
String * arginfo_code = NewString("0");
|
||||
|
|
@ -1149,7 +1153,7 @@ public:
|
|||
Printf(f->code, "PHP_METHOD(%s,__set) {\n",class_name);
|
||||
|
||||
Printf(f->code, " swig_object_wrapper *arg = (swig_object_wrapper *)SWIG_Z_FETCH_OBJ_P(getThis());\n");
|
||||
Printf(f->code, " zval args[2];\n zend_string *arg2 = 0;\n\n");
|
||||
Printf(f->code, " zval args[2];\n zval tempZval;\n zend_string *arg2 = 0;\n\n");
|
||||
Printf(f->code, " if(ZEND_NUM_ARGS() != 2 || zend_get_parameters_array_ex(2, args) != SUCCESS) {\n");
|
||||
Printf(f->code, "\tWRONG_PARAM_COUNT;\n}\n\n");
|
||||
Printf(f->code, " if(!arg) SWIG_PHP_Error(E_ERROR, \"this pointer is NULL\");\n\n");
|
||||
|
|
@ -1157,13 +1161,17 @@ public:
|
|||
|
||||
Printf(f->code, "if (!arg2) {\n RETVAL_NULL();\n}\n",magic_set);
|
||||
Printf(f->code, "%s\n",magic_set);
|
||||
Printf(f->code, "\nelse if (strcmp(arg2->val,\"thisown\") == 0)\n{\n");
|
||||
Printf(f->code, "\nelse if (strcmp(arg2->val,\"thisown\") == 0) {\n");
|
||||
Printf(f->code, "arg->newobject = zval_get_long(&args[1]);\n}\n\n");
|
||||
Printf(f->code, "else {\nif (!arg->extras) {\n");
|
||||
Printf(f->code, "ALLOC_HASHTABLE(arg->extras);\nzend_hash_init(arg->extras, 0, NULL, ZVAL_PTR_DTOR, 0);\n}\n");
|
||||
Printf(f->code, "if (!zend_hash_find(arg->extras,arg2))\nzend_hash_add(arg->extras,arg2,&args[1]);\n");
|
||||
Printf(f->code, "else\nzend_hash_update(arg->extras,arg2,&args[1]);\n}\n\n");
|
||||
|
||||
Printf(f->code, "else {\n");
|
||||
if (baseClassExtend) {
|
||||
Printf(f->code, "PHP_MN(%s___set)(INTERNAL_FUNCTION_PARAM_PASSTHRU);\n}\n", baseClassExtend);
|
||||
} else {
|
||||
Printf(f->code, "if (!arg->extras) {\n");
|
||||
Printf(f->code, "ALLOC_HASHTABLE(arg->extras);\nzend_hash_init(arg->extras, 0, NULL, ZVAL_PTR_DTOR, 0);\n}\n");
|
||||
Printf(f->code, "if (!zend_hash_find(arg->extras,arg2))\nzend_hash_add(arg->extras,arg2,&args[1]);\n");
|
||||
Printf(f->code, "else\nzend_hash_update(arg->extras,arg2,&args[1]);\n}\n");
|
||||
}
|
||||
|
||||
Printf(f->code, "zend_string_release(arg2);\n\n");
|
||||
Printf(f->code, "thrown:\n");
|
||||
|
|
@ -1180,7 +1188,7 @@ public:
|
|||
Printf(f->code, "PHP_METHOD(%s,__get) {\n",class_name);
|
||||
|
||||
Printf(f->code, " swig_object_wrapper *arg = (swig_object_wrapper *)SWIG_Z_FETCH_OBJ_P(getThis());\n", class_name);
|
||||
Printf(f->code, " zval args[1];\n zend_string *arg2 = 0;\n\n");
|
||||
Printf(f->code, " zval args[1];\n zval tempZval;\n zend_string *arg2 = 0;\n\n");
|
||||
Printf(f->code, " if(ZEND_NUM_ARGS() != 1 || zend_get_parameters_array_ex(1, args) != SUCCESS) {\n");
|
||||
Printf(f->code, "\tWRONG_PARAM_COUNT;\n}\n\n");
|
||||
Printf(f->code, " if(!arg) SWIG_PHP_Error(E_ERROR, \"this pointer is NULL\");\n\n");
|
||||
|
|
@ -1188,12 +1196,16 @@ public:
|
|||
|
||||
Printf(f->code, "if (!arg2) {\n RETVAL_NULL();\n}\n",magic_set);
|
||||
Printf(f->code, "%s\n",magic_get);
|
||||
Printf(f->code, "\nelse if (strcmp(arg2->val,\"thisown\") == 0)\n{\n");
|
||||
Printf(f->code, "\nelse if (strcmp(arg2->val,\"thisown\") == 0) {\n");
|
||||
Printf(f->code, "if(arg->newobject) {\nRETVAL_LONG(1);\n}\nelse {\nRETVAL_LONG(0);\n}\n}\n\n");
|
||||
Printf(f->code, "else {\nif (!arg->extras) {\nRETVAL_NULL();\n}\n");
|
||||
Printf(f->code, "else {\nzval *zv = zend_hash_find(arg->extras,arg2);\n");
|
||||
Printf(f->code, "if (!zv)\nRETVAL_NULL();\nelse\nRETVAL_ZVAL(zv,1,ZVAL_PTR_DTOR);\n}\n}\n\n");
|
||||
|
||||
Printf(f->code, "else {\n");
|
||||
if (baseClassExtend) {
|
||||
Printf(f->code, "PHP_MN(%s___get)(INTERNAL_FUNCTION_PARAM_PASSTHRU);\n}\n", baseClassExtend);
|
||||
} else {
|
||||
Printf(f->code, "if (!arg->extras) {\nRETVAL_NULL();\n}\n");
|
||||
Printf(f->code, "else {\nzval *zv = zend_hash_find(arg->extras,arg2);\n");
|
||||
Printf(f->code, "if (!zv)\nRETVAL_NULL();\nelse\nRETVAL_ZVAL(zv,1,ZVAL_PTR_DTOR);\n}\n}\n");
|
||||
}
|
||||
|
||||
Printf(f->code, "zend_string_release(arg2);\n\n");
|
||||
Printf(f->code, "thrown:\n");
|
||||
|
|
@ -1210,7 +1222,7 @@ public:
|
|||
Printf(f->code, "PHP_METHOD(%s,__isset) {\n",class_name);
|
||||
|
||||
Printf(f->code, " swig_object_wrapper *arg = (swig_object_wrapper *)SWIG_Z_FETCH_OBJ_P(getThis());\n", class_name);
|
||||
Printf(f->code, " zval args[1];\n zend_string *arg2 = 0;\n\n");
|
||||
Printf(f->code, " zval args[1];\n zval tempZval;\n zend_string *arg2 = 0;\n\n");
|
||||
Printf(f->code, " int newSize = 1;\nchar *method_name = 0;\n\n");
|
||||
Printf(f->code, " if(ZEND_NUM_ARGS() != 1 || zend_get_parameters_array_ex(1, args) != SUCCESS) {\n");
|
||||
Printf(f->code, "\tWRONG_PARAM_COUNT;\n}\n\n");
|
||||
|
|
@ -1223,12 +1235,17 @@ public:
|
|||
Printf(magic_isset, "RETVAL_TRUE;\n}\n");
|
||||
|
||||
Printf(f->code, "if (!arg2) {\n RETVAL_FALSE;\n}\n",magic_set);
|
||||
Printf(f->code, "\nelse if (strcmp(arg2->val,\"thisown\") == 0)\n{\n");
|
||||
Printf(f->code, "\nelse if (strcmp(arg2->val,\"thisown\") == 0) {\n");
|
||||
Printf(f->code, "RETVAL_TRUE;\n}\n\n");
|
||||
Printf(f->code, "%s\n",magic_isset);
|
||||
Printf(f->code, "else {\nif (!arg->extras) {\nRETVAL_FALSE;\n}\n");
|
||||
Printf(f->code, "else {\nif (!zend_hash_find(arg->extras,arg2))\n");
|
||||
Printf(f->code, "RETVAL_FALSE;\nelse\nRETVAL_TRUE;\n}\n}\n\n");
|
||||
Printf(f->code, "else {\n");
|
||||
if (baseClassExtend) {
|
||||
Printf(f->code, "PHP_MN(%s___isset)(INTERNAL_FUNCTION_PARAM_PASSTHRU);\n}\n", baseClassExtend);
|
||||
} else {
|
||||
Printf(f->code, "if (!arg->extras) {\nRETVAL_FALSE;\n}\n");
|
||||
Printf(f->code, "else {\nif (!zend_hash_find(arg->extras,arg2))\n");
|
||||
Printf(f->code, "RETVAL_FALSE;\nelse\nRETVAL_TRUE;\n}\n}\n");
|
||||
}
|
||||
|
||||
Printf(f->code, "free(method_name);\nzend_string_release(arg2);\n\n");
|
||||
Printf(f->code, "thrown:\n");
|
||||
|
|
@ -1255,13 +1272,13 @@ public:
|
|||
|
||||
String *v_name = GetChar(n, "name");
|
||||
|
||||
Printf(magic_set, "\nelse if (strcmp(arg2->val,\"%s\") == 0)\n{\n",v_name);
|
||||
Printf(magic_set, "zval zv;\nZVAL_STRING(&zv, \"%s_set\");\n",v_name);
|
||||
Printf(magic_set, "CALL_METHOD_PARAM_1(zv, return_value, getThis(),args[1]);\n}\n\n");
|
||||
Printf(magic_set, "\nelse if (strcmp(arg2->val,\"%s\") == 0) {\n",v_name);
|
||||
Printf(magic_set, "ZVAL_STRING(&tempZval, \"%s_set\");\n",v_name);
|
||||
Printf(magic_set, "CALL_METHOD_PARAM_1(tempZval, return_value, getThis(),args[1]);\n}\n\n");
|
||||
|
||||
Printf(magic_get, "\nelse if (strcmp(arg2->val,\"%s\") == 0)\n{\n",v_name);
|
||||
Printf(magic_get, "zval zv;\nZVAL_STRING(&zv, \"%s_get\");\n",v_name);
|
||||
Printf(magic_get, "CALL_METHOD(zv, return_value, getThis());\n}\n");
|
||||
Printf(magic_get, "\nelse if (strcmp(arg2->val,\"%s\") == 0) {\n",v_name);
|
||||
Printf(magic_get, "ZVAL_STRING(&tempZval, \"%s_get\");\n",v_name);
|
||||
Printf(magic_get, "CALL_METHOD(tempZval, return_value, getThis());\n}\n");
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -1451,22 +1468,20 @@ public:
|
|||
int num_required = emit_num_required(l);
|
||||
numopt = num_arguments - num_required;
|
||||
|
||||
//if (wrapperType == directorconstructor)
|
||||
//num_arguments++;
|
||||
|
||||
if (num_arguments > 0) {
|
||||
String *args = NewStringEmpty();
|
||||
//if (wrapperType == directorconstructor)
|
||||
//Wrapper_add_local(f, "arg0", "zval * arg0;");
|
||||
Printf(args, "zval args[%d]", num_arguments);
|
||||
Wrapper_add_local(f, "tempPointer", "void *tempPointer = 0");
|
||||
if ((wrapperType == memberfn || wrapperType == membervar)) {
|
||||
num_arguments--; //To remove This Pointer
|
||||
Printf(args, "arg1 = (%s *)((SWIG_Z_FETCH_OBJ_P(getThis()))->ptr);\n", class_type);
|
||||
}
|
||||
Printf(args, "zval args[%d]", num_arguments);
|
||||
Wrapper_add_local(f, "args", args);
|
||||
Delete(args);
|
||||
args = NULL;
|
||||
}
|
||||
if (wrapperType == directorconstructor) {
|
||||
Wrapper_add_local(f, "arg0", "zval *arg0 = getThis()");
|
||||
}
|
||||
|
||||
// This generated code may be called:
|
||||
// 1) as an object method, or
|
||||
|
|
@ -1498,8 +1513,6 @@ public:
|
|||
}
|
||||
Printf(f->code, "WRONG_PARAM_COUNT;\n}\n\n");
|
||||
}
|
||||
if (wrapperType == directorconstructor)
|
||||
Printf(f->code, "zval * arg0 = getThis();\n \n");
|
||||
|
||||
/* Now convert from PHP to C variables */
|
||||
// At this point, argcount if used is the number of deliberately passed args
|
||||
|
|
@ -1757,7 +1770,7 @@ public:
|
|||
p += strlen(p) - 4;
|
||||
String *varname = Getattr(n, "membervariableHandler:sym:name");
|
||||
if (strcmp(p, "_get") == 0) {
|
||||
magic_method_setter(n,false);
|
||||
magic_method_setter(n, false, NULL);
|
||||
Setattr(shadow_get_vars, varname, Getattr(n, "type"));
|
||||
} else if (strcmp(p, "_set") == 0) {
|
||||
Setattr(shadow_set_vars, varname, iname);
|
||||
|
|
@ -2763,13 +2776,13 @@ done:
|
|||
Append(class_types, class_type);
|
||||
Append(class_need_free, "0");
|
||||
|
||||
Printf(s_oinit, "\nzend_class_entry SWIGTYPE_%s_internal_ce;\n", class_name);
|
||||
Printf(s_oinit, "\n{\n zend_class_entry SWIGTYPE_%s_internal_ce;\n", class_name);
|
||||
|
||||
// namespace code to introduce namespaces into wrapper classes.
|
||||
//if (nameSpace != NULL)
|
||||
//Printf(s_oinit, "INIT_CLASS_ENTRY(%s_internal_ce, \"%s\\\\%s\", class_%s_functions);\n", class_name, nameSpace ,class_name, class_name);
|
||||
//else
|
||||
Printf(s_oinit, "INIT_CLASS_ENTRY(SWIGTYPE_%s_internal_ce, \"%s\", class_%s_functions);\n", class_name, class_name, class_name);
|
||||
Printf(s_oinit, " INIT_CLASS_ENTRY(SWIGTYPE_%s_internal_ce, \"%s\", class_%s_functions);\n", class_name, class_name, class_name);
|
||||
|
||||
if (shadow) {
|
||||
char *rename = GetChar(n, "sym:name");
|
||||
|
|
@ -2816,14 +2829,14 @@ done:
|
|||
baseClassExtend = NewString(class_name);
|
||||
Append(baseClassExtend, "_Exception");
|
||||
|
||||
Printf(s_oinit, "zend_class_entry *SWIGTYPE_%s_ce = zend_lookup_class(zend_string_init(\"Exception\", sizeof(\"Exception\") - 1, 0));\n", baseClassExtend);
|
||||
Printf(s_oinit, " zend_class_entry *SWIGTYPE_%s_ce = zend_lookup_class(zend_string_init(\"Exception\", sizeof(\"Exception\") - 1, 0));\n", baseClassExtend);
|
||||
exceptionClassFlag = true;
|
||||
}
|
||||
|
||||
if (baseClassExtend && (exceptionClassFlag || is_class_wrapped(baseClassExtend))) {
|
||||
Printf(s_oinit, "SWIGTYPE_%s_ce = zend_register_internal_class_ex(&SWIGTYPE_%s_internal_ce, SWIGTYPE_%s_ce);\n", class_name , class_name, baseClassExtend);
|
||||
Printf(s_oinit, " SWIGTYPE_%s_ce = zend_register_internal_class_ex(&SWIGTYPE_%s_internal_ce, SWIGTYPE_%s_ce);\n", class_name , class_name, baseClassExtend);
|
||||
} else {
|
||||
Printf(s_oinit, "SWIGTYPE_%s_ce = zend_register_internal_class(&SWIGTYPE_%s_internal_ce);\n", class_name , class_name);
|
||||
Printf(s_oinit, " SWIGTYPE_%s_ce = zend_register_internal_class(&SWIGTYPE_%s_internal_ce);\n", class_name , class_name);
|
||||
}
|
||||
|
||||
{
|
||||
|
|
@ -2841,19 +2854,19 @@ done:
|
|||
String *interface = Getitem(interface_list, Iterator-1);
|
||||
String *interface_ce = NewStringEmpty();
|
||||
Printf(interface_ce, "php_%s_interface_ce_%d" , class_name , Iterator);
|
||||
Printf(s_oinit, "zend_class_entry *%s = zend_lookup_class(zend_string_init(\"%s\", sizeof(\"%s\") - 1, 0));\n", interface_ce , interface, interface);
|
||||
Printf(s_oinit, " zend_class_entry *%s = zend_lookup_class(zend_string_init(\"%s\", sizeof(\"%s\") - 1, 0));\n", interface_ce , interface, interface);
|
||||
Append(append_interface, interface_ce);
|
||||
Append(append_interface, " ");
|
||||
}
|
||||
Chop(append_interface);
|
||||
Replaceall(append_interface, " ", ",");
|
||||
Printf(s_oinit, "zend_class_implements(SWIGTYPE_%s_ce, %d, %s);\n", class_name, num_interfaces, append_interface);
|
||||
Printf(s_oinit, " zend_class_implements(SWIGTYPE_%s_ce, %d, %s);\n", class_name, num_interfaces, append_interface);
|
||||
}
|
||||
}
|
||||
|
||||
Printf(s_oinit, "SWIGTYPE_%s_ce->create_object = %s_object_new;\n", class_name, class_name);
|
||||
Printf(s_oinit, "memcpy(&%s_object_handlers,zend_get_std_object_handlers(), sizeof(zend_object_handlers));\n", class_name);
|
||||
Printf(s_oinit, "%s_object_handlers.clone_obj = NULL;\n\n", class_name);
|
||||
Printf(s_oinit, " SWIGTYPE_%s_ce->create_object = %s_object_new;\n", class_name, class_name);
|
||||
Printf(s_oinit, " memcpy(&%s_object_handlers,zend_get_std_object_handlers(), sizeof(zend_object_handlers));\n", class_name);
|
||||
Printf(s_oinit, " %s_object_handlers.clone_obj = NULL;\n}\n\n", class_name);
|
||||
|
||||
classnode = n;
|
||||
Language::classHandler(n);
|
||||
|
|
@ -3031,7 +3044,7 @@ done:
|
|||
Delete(shadow_get_vars);
|
||||
shadow_get_vars = NULL;
|
||||
}
|
||||
magic_method_setter(n,true);
|
||||
magic_method_setter(n, true, baseClassExtend);
|
||||
class_name = NULL;
|
||||
class_type = NULL;
|
||||
return SWIG_OK;
|
||||
|
|
@ -3166,12 +3179,17 @@ done:
|
|||
Printf(f->def, "/* to typecast and do the actual destruction */\n");
|
||||
Printf(f->def, "static void %s(zend_resource *res, const char *type_name) {\n", destructorname);
|
||||
|
||||
Printf(f->def, "\n\nif(%d) {\n", newClassObject ? 1 : 0);
|
||||
Printf(f->def, "return;\n}\n");
|
||||
Wrapper_add_localv(f, "value", "swig_object_wrapper *value = 0", NIL);
|
||||
Wrapper_add_localv(f, "ptr", "void *ptr = 0", NIL);
|
||||
Wrapper_add_localv(f, "newobject", "int newobject = 0", NIL);
|
||||
|
||||
Wrapper_add_localv(f, "value", "swig_object_wrapper *value=(swig_object_wrapper *) res->ptr", NIL);
|
||||
Wrapper_add_localv(f, "ptr", "void *ptr=value->ptr", NIL);
|
||||
Wrapper_add_localv(f, "newobject", "int newobject=value->newobject", NIL);
|
||||
|
||||
Printf(f->code, "if(%d) {\n", newClassObject ? 1 : 0);
|
||||
Printf(f->code, "return;\n}\n\n");
|
||||
|
||||
Printf(f->code, "value=(swig_object_wrapper *) res->ptr;\n");
|
||||
Printf(f->code, "ptr=value->ptr;\n");
|
||||
Printf(f->code, "newobject=value->newobject;\n\n");
|
||||
|
||||
emit_parameter_variables(l, f);
|
||||
emit_attach_parmmaps(l, f);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue