[guile] Improve constant wrapping

Allow wrapping anything with a `varout` typemap as a constant.

See #3034
This commit is contained in:
Olly Betts 2024-10-25 08:41:55 +13:00
parent acd508f4b9
commit 9187aaf22c
5 changed files with 34 additions and 23 deletions

View File

@ -7,6 +7,10 @@ the issue number to the end of the URL: https://github.com/swig/swig/issues/
Version 4.4.0 (in progress) Version 4.4.0 (in progress)
=========================== ===========================
2024-10-25: olly
[Guile] Allow wrapping anything with a `varout` typemap as a
constant.
2024-10-24: olly 2024-10-24: olly
[Perl] https://sourceforge.net/p/swig/bugs/1134/ Ensure C++ [Perl] https://sourceforge.net/p/swig/bugs/1134/ Ensure C++
local variables get destroyed before throwing a Perl exception. local variables get destroyed before throwing a Perl exception.

View File

@ -3,10 +3,8 @@
// %constant and struct // %constant and struct
#ifdef SWIGGUILE #ifdef SWIGGUILE
// Suppress warnings for function pointer constants which SWIG/Guile doesn't // Suppress warning for function pointer constant which SWIG/Guile doesn't
// currently handle. // currently handle.
%warnfilter(SWIGWARN_TYPEMAP_CONST_UNDEF) TYPE1_CONSTANT1;
%warnfilter(SWIGWARN_TYPEMAP_CONST_UNDEF) TYPE1_CONSTANT2;
%warnfilter(SWIGWARN_TYPEMAP_CONST_UNDEF) TYPE1CFPTR1DEF_CONSTANT1; %warnfilter(SWIGWARN_TYPEMAP_CONST_UNDEF) TYPE1CFPTR1DEF_CONSTANT1;
#endif #endif

View File

@ -1,12 +1,6 @@
%module li_std_string %module li_std_string
%include <std_string.i> %include <std_string.i>
#ifdef SWIGGUILE
// Suppress warnings for constants SWIG/Guile doesn't currently handle.
%warnfilter(SWIGWARN_TYPEMAP_CONST_UNDEF) MY_STRING;
%warnfilter(SWIGWARN_TYPEMAP_CONST_UNDEF) MY_STRING_2;
#endif
#if defined(SWIGLUA) || defined(SWIGPHP) || defined(SWIGUTL) #if defined(SWIGLUA) || defined(SWIGPHP) || defined(SWIGUTL)
%apply std::string& INPUT { std::string &input } %apply std::string& INPUT { std::string &input }
%apply std::string& INOUT { std::string &inout } %apply std::string& INOUT { std::string &inout }

View File

@ -61,4 +61,11 @@
(if (not (string=? (get-null (stdstring-empty)) "non-null")) (if (not (string=? (get-null (stdstring-empty)) "non-null"))
(error "get-null stdstring-empty test")) (error "get-null stdstring-empty test"))
(if (not (string=? (aString) "something"))
(error "aString test"))
(if (not (string=? (MY-STRING) ""))
(error "MY-STRING test"))
(if (not (string=? (MY-STRING-2) "OK"))
(error "MY-STRING-2 test"))
(exit 0) (exit 0)

View File

@ -12,6 +12,7 @@
* ----------------------------------------------------------------------------- */ * ----------------------------------------------------------------------------- */
#include "swigmod.h" #include "swigmod.h"
#include <assert.h>
#include <ctype.h> #include <ctype.h>
// Note string broken in half for compilers that can't handle long strings // Note string broken in half for compilers that can't handle long strings
@ -1093,8 +1094,7 @@ public:
int assignable = !is_immutable(n); int assignable = !is_immutable(n);
if (1 || (SwigType_type(t) != T_USER) || (is_a_pointer(t))) { {
Printf(f->def, "static SCM\n%s(SCM s_0)\n{\n", var_name); Printf(f->def, "static SCM\n%s(SCM s_0)\n{\n", var_name);
/* Define the scheme name in C. This define is used by several Guile /* Define the scheme name in C. This define is used by several Guile
@ -1111,6 +1111,8 @@ public:
/* Printv(f->code,tm,"\n",NIL); */ /* Printv(f->code,tm,"\n",NIL); */
emit_action_code(n, f->code, tm); emit_action_code(n, f->code, tm);
} else { } else {
// The fake variable constantWrapper() creates is immutable.
assert(!GetFlag(n, "guile:reallywrappingaconstant"));
throw_unhandled_guile_type_error(t); throw_unhandled_guile_type_error(t);
} }
Printf(f->code, "}\n"); Printf(f->code, "}\n");
@ -1123,6 +1125,12 @@ public:
/* Printv(f->code,tm,"\n",NIL); */ /* Printv(f->code,tm,"\n",NIL); */
emit_action_code(n, f->code, tm); emit_action_code(n, f->code, tm);
} else { } else {
if (GetFlag(n, "guile:reallywrappingaconstant")) {
Delete(var_name);
Delete(proc_name);
DelWrapper(f);
return SWIG_ERROR;
}
throw_unhandled_guile_type_error(t); throw_unhandled_guile_type_error(t);
} }
Printf(f->code, "\nreturn gswig_result;\n"); Printf(f->code, "\nreturn gswig_result;\n");
@ -1241,9 +1249,6 @@ public:
Delete(signature2); Delete(signature2);
Delete(doc); Delete(doc);
} }
} else {
Swig_warning(WARN_TYPEMAP_VAR_UNDEF, input_file, line_number, "Unsupported variable type %s (ignored).\n", SwigType_str(t, 0));
} }
Delete(var_name); Delete(var_name);
Delete(proc_name); Delete(proc_name);
@ -1286,12 +1291,6 @@ public:
proc_name = NewString(iname); proc_name = NewString(iname);
Replaceall(proc_name, "_", "-"); Replaceall(proc_name, "_", "-");
if ((SwigType_type(nctype) == T_USER) && (!is_a_pointer(nctype))) {
Swig_warning(WARN_TYPEMAP_CONST_UNDEF, input_file, line_number, "Unsupported constant value.\n");
Delete(var_name);
DelWrapper(f);
return SWIG_NOWRAP;
}
// See if there's a typemap // See if there's a typemap
if ((tm = Swig_typemap_lookup("constant", n, name, 0))) { if ((tm = Swig_typemap_lookup("constant", n, name, 0))) {
@ -1301,7 +1300,8 @@ public:
// Create variable and assign it a value // Create variable and assign it a value
Printf(f_header, "static %s = (%s)(%s);\n", SwigType_str(type, var_name), SwigType_str(type, 0), value); Printf(f_header, "static %s = (%s)(%s);\n", SwigType_str(type, var_name), SwigType_str(type, 0), value);
} }
{ int result = SWIG_OK;
if (Len(nctype) > 0) {
/* Hack alert: will cleanup later -- Dave */ /* Hack alert: will cleanup later -- Dave */
Node *nn = NewHash(); Node *nn = NewHash();
Setfile(nn, Getfile(n)); Setfile(nn, Getfile(n));
@ -1313,14 +1313,22 @@ public:
if (constasvar) { if (constasvar) {
SetFlag(nn, "feature:constasvar"); SetFlag(nn, "feature:constasvar");
} }
variableWrapper(nn); SetFlag(nn, "guile:reallywrappingaconstant");
if (variableWrapper(nn) == SWIG_ERROR) {
Swig_warning(WARN_TYPEMAP_CONST_UNDEF, input_file, line_number, "Unsupported constant value.\n");
result = SWIG_NOWRAP;
}
Delete(nn); Delete(nn);
} else {
Swig_warning(WARN_TYPEMAP_CONST_UNDEF, input_file, line_number, "Unsupported constant value.\n");
result = SWIG_NOWRAP;
} }
Delete(var_name); Delete(var_name);
Delete(nctype); Delete(nctype);
Delete(proc_name); Delete(proc_name);
DelWrapper(f); DelWrapper(f);
return SWIG_OK; return result;
} }
/* ------------------------------------------------------------ /* ------------------------------------------------------------