[PHP5] Fix handling of double or float parameters with an integer default value.

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@9914 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Olly Betts 2007-08-25 13:53:27 +00:00
commit 437bc4d61e
2 changed files with 92 additions and 68 deletions

View file

@ -1,6 +1,10 @@
Version 1.3.32 (in progress) Version 1.3.32 (in progress)
============================ ============================
08/25/2007: olly
[PHP5] Fix handling of double or float parameters with an integer
default value.
08/25/2007: olly 08/25/2007: olly
[PHP5] Generate __isset() methods for setters for PHP 5.1 and later. [PHP5] Generate __isset() methods for setters for PHP 5.1 and later.

View file

@ -1225,9 +1225,6 @@ public:
// Is being shadow-wrap-thingied // Is being shadow-wrap-thingied
Printf(f->code, "{\n/* ALTERNATIVE Constructor, make an object wrapper */\n"); Printf(f->code, "{\n/* ALTERNATIVE Constructor, make an object wrapper */\n");
// Make object // Make object
String *shadowrettype = NewStringEmpty();
SwigToPhpType(d, iname, shadowrettype, (shadow && php_version == 4));
Printf(f->code, "zval *obj, *_cPtr;\n"); Printf(f->code, "zval *obj, *_cPtr;\n");
Printf(f->code, "MAKE_STD_ZVAL(obj);\n"); Printf(f->code, "MAKE_STD_ZVAL(obj);\n");
Printf(f->code, "MAKE_STD_ZVAL(_cPtr);\n"); Printf(f->code, "MAKE_STD_ZVAL(_cPtr);\n");
@ -1235,7 +1232,10 @@ public:
Printf(f->code, "INIT_ZVAL(*return_value);\n"); Printf(f->code, "INIT_ZVAL(*return_value);\n");
if (shadow && php_version == 4) { if (shadow && php_version == 4) {
String *shadowrettype = SwigToPhpType(d, iname, true);
Printf(f->code, "object_init_ex(obj,ptr_ce_swig_%s);\n", shadowrettype); Printf(f->code, "object_init_ex(obj,ptr_ce_swig_%s);\n", shadowrettype);
Delete(shadowrettype);
Printf(f->code, "add_property_zval(obj,\"" SWIG_PTR "\",_cPtr);\n"); Printf(f->code, "add_property_zval(obj,\"" SWIG_PTR "\",_cPtr);\n");
Printf(f->code, "*return_value=*obj;\n"); Printf(f->code, "*return_value=*obj;\n");
} else { } else {
@ -1512,80 +1512,97 @@ public:
Printf(pname, " or_%s", pname_cstr); Printf(pname, " or_%s", pname_cstr);
} }
} }
const char *value = GetChar(p, "value"); String *value = NewString(Getattr(p, "value"));
if (value) { if (Len(value)) {
/* Check that value is a valid constant in PHP (and adjust it if /* Check that value is a valid constant in PHP (and adjust it if
* necessary, or replace it with "?" if it's just not valid). */ * necessary, or replace it with "?" if it's just not valid). */
SwigType *type = Getattr(p, "type"); SwigType *type = Getattr(p, "type");
switch (SwigType_type(type)) { switch (SwigType_type(type)) {
case T_BOOL:{ case T_BOOL: {
if (strcmp(value, "true") == 0 || strcmp(value, "false") == 0) if (Strcmp(value, "true") == 0 || Strcmp(value, "false") == 0)
break; break;
char *p; char *p;
errno = 0; errno = 0;
int n = strtol(Char(value), &p, 0); int n = strtol(Char(value), &p, 0);
if (errno || *p) Clear(value);
value = "?"; if (errno || *p) {
else if (n) Append(value, "?");
value = "true"; } else if (n) {
else Append(value, "true");
value = "false"; } else {
Append(value, "false");
}
break; break;
} }
case T_CHAR: case T_CHAR:
case T_SCHAR: case T_SCHAR:
case T_SHORT: case T_SHORT:
case T_INT: case T_INT:
case T_LONG:{ case T_LONG: {
char *p; char *p;
errno = 0; errno = 0;
(void) strtol(Char(value), &p, 0); (void) strtol(Char(value), &p, 0);
if (errno || *p) if (errno || *p) {
value = "?"; Clear(value);
Append(value, "?");
}
break; break;
} }
case T_UCHAR: case T_UCHAR:
case T_USHORT: case T_USHORT:
case T_UINT: case T_UINT:
case T_ULONG:{ case T_ULONG: {
char *p; char *p;
errno = 0; errno = 0;
(void) strtoul(Char(value), &p, 0); (void) strtoul(Char(value), &p, 0);
if (errno || *p) if (errno || *p) {
value = "?"; Clear(value);
Append(value, "?");
}
break; break;
} }
case T_FLOAT: case T_FLOAT:
case T_DOUBLE:{ case T_DOUBLE:{
char *p; char *p;
errno = 0; errno = 0;
/* FIXME: strtod is locale dependent... */ /* FIXME: strtod is locale dependent... */
(void) strtod(Char(value), &p); (void) strtod(Char(value), &p);
if (errno || *p) if (errno || *p) {
value = "?"; Clear(value);
Append(value, "?");
} else {
if (strchr(Char(value), '.') == NULL) {
Insert(value, 0, (void *)"(double)");
}
}
break; break;
} }
case T_REFERENCE: case T_REFERENCE:
case T_USER: case T_USER:
case T_ARRAY: case T_ARRAY:
value = "?"; Clear(value);
break; Append(value, "?");
case T_STRING: break;
if (Len(value) < 2) { case T_STRING:
// How can a string (including "" be less than 2 characters?) if (Len(value) < 2) {
value = "?"; // How can a string (including "" be less than 2 characters?)
} else { Clear(value);
Append(value, "?");
} else {
const char *v = Char(value);
if (v[0] != '"' || v[Len(value) - 1] != '"') {
Clear(value);
Append(value, "?");
}
// Strings containing "$" require special handling, but we do
// that later.
}
break;
case T_VOID:
assert(false);
break;
case T_POINTER: {
const char *v = Char(value); const char *v = Char(value);
if (v[0] != '"' || v[Len(value) - 1] != '"')
value = "?";
// Strings containing "$" require special handling, but we do that later.
}
break;
case T_VOID:
assert(false);
break;
case T_POINTER:{
const char *v = value;
if (v[0] == '(') { if (v[0] == '(') {
// Handle "(void*)0", "(TYPE*)0", "(char*)NULL", etc. // Handle "(void*)0", "(TYPE*)0", "(char*)NULL", etc.
v += strcspn(v + 1, "*()") + 1; v += strcspn(v + 1, "*()") + 1;
@ -1596,21 +1613,28 @@ public:
} while (*v == '*'); } while (*v == '*');
if (*v++ == ')') { if (*v++ == ')') {
v += strspn(v, " \t"); v += strspn(v, " \t");
value = v; String * old = value;
value = NewString(v);
Delete(old);
} }
} }
} }
if (strcmp(value, "NULL") == 0 || strcmp(value, "0") == 0 || strcmp(value, "0L") == 0) { if (Strcmp(value, "NULL") == 0 ||
value = "null"; Strcmp(value, "0") == 0 ||
Strcmp(value, "0L") == 0) {
Clear(value);
Append(value, "null");
} else { } else {
value = "?"; Clear(value);
Append(value, "?");
} }
break; break;
} }
} }
if (!arg_values[argno]) { if (!arg_values[argno]) {
arg_values[argno] = NewString(value); arg_values[argno] = value;
value = NULL;
} else if (Cmp(arg_values[argno], value) != 0) { } else if (Cmp(arg_values[argno], value) != 0) {
// If a parameter has two different default values in // If a parameter has two different default values in
// different overloaded forms of the function, we can't // different overloaded forms of the function, we can't
@ -1633,6 +1657,7 @@ public:
Delete(arg_values[argno]); Delete(arg_values[argno]);
arg_values[argno] = NewString("?"); arg_values[argno] = NewString("?");
} }
Delete(value);
p = nextSibling(p); p = nextSibling(p);
++argno; ++argno;
} }
@ -2570,7 +2595,7 @@ public:
} }
void SwigToPhpType(SwigType *t, String_or_char *pname, String *php_type, int shadow_flag) { String * SwigToPhpType(SwigType *t, String_or_char *pname, int shadow_flag) {
String *ptype = 0; String *ptype = 0;
if (shadow_flag) { if (shadow_flag) {
@ -2580,12 +2605,10 @@ public:
ptype = PhpTypeFromTypemap((char *) "ptype", t, pname, (char *) ""); ptype = PhpTypeFromTypemap((char *) "ptype", t, pname, (char *) "");
} }
if (ptype) { if (ptype) return ptype;
Printf(php_type, "%s", ptype);
Delete(ptype); /* Map type here */
} else { switch (SwigType_type(t)) {
/* Map type here */
switch (SwigType_type(t)) {
case T_CHAR: case T_CHAR:
case T_SCHAR: case T_SCHAR:
case T_UCHAR: case T_UCHAR:
@ -2600,15 +2623,12 @@ public:
case T_BOOL: case T_BOOL:
case T_STRING: case T_STRING:
case T_VOID: case T_VOID:
Printf(php_type, "");
break; break;
case T_POINTER: case T_POINTER:
case T_REFERENCE: case T_REFERENCE:
case T_USER: case T_USER:
if (shadow_flag && is_shadow(t)) { if (shadow_flag && is_shadow(t)) {
Printf(php_type, Char(is_shadow(t))); return NewString(Char(is_shadow(t)));
} else {
Printf(php_type, "");
} }
break; break;
case T_ARRAY: case T_ARRAY:
@ -2617,10 +2637,10 @@ public:
default: default:
Printf(stderr, "SwigToPhpType: unhandled data type: %s\n", SwigType_str(t, 0)); Printf(stderr, "SwigToPhpType: unhandled data type: %s\n", SwigType_str(t, 0));
break; break;
}
} }
}
return NewStringEmpty();
}
String *PhpTypeFromTypemap(char *op, SwigType *t, String_or_char *pname, String_or_char *lname) { String *PhpTypeFromTypemap(char *op, SwigType *t, String_or_char *pname, String_or_char *lname) {
String *tms; String *tms;