From 2e01533b23cfff7f90bf45c6608ee5112e57242a Mon Sep 17 00:00:00 2001 From: William S Fulton Date: Thu, 18 Dec 2014 06:37:49 +0000 Subject: [PATCH] Partial support for %constant and structs Test case is slightly modified from the test case in issue #250 Use of constant objects does not seem to work in Python - the type is SwigPyObject instead of constant_directive.Type1. --- Examples/test-suite/common.mk | 1 + .../java/constant_directive_runme.java | 22 +++++++++++++++++++ Source/Modules/csharp.cxx | 11 ---------- Source/Modules/d.cxx | 11 ---------- Source/Modules/java.cxx | 13 +---------- Source/Modules/modula3.cxx | 13 ----------- Source/Swig/cwrap.c | 9 +++++++- 7 files changed, 32 insertions(+), 48 deletions(-) create mode 100644 Examples/test-suite/java/constant_directive_runme.java diff --git a/Examples/test-suite/common.mk b/Examples/test-suite/common.mk index d4f34a707..314894598 100644 --- a/Examples/test-suite/common.mk +++ b/Examples/test-suite/common.mk @@ -143,6 +143,7 @@ CPP_TEST_CASES += \ class_scope_weird \ compactdefaultargs \ const_const_2 \ + constant_directive \ constant_pointers \ constover \ constructor_copy \ diff --git a/Examples/test-suite/java/constant_directive_runme.java b/Examples/test-suite/java/constant_directive_runme.java new file mode 100644 index 000000000..b5135a499 --- /dev/null +++ b/Examples/test-suite/java/constant_directive_runme.java @@ -0,0 +1,22 @@ +import constant_directive.*; + +public class constant_directive_runme { + + static { + try { + System.loadLibrary("constant_directive"); + } catch (UnsatisfiedLinkError e) { + System.err.println("Native code library failed to load. See the chapter on Dynamic Linking Problems in the SWIG Java documentation for help.\n" + e); + System.exit(1); + } + } + + public static void main(String argv[]) { + if (constant_directive.TYPE1_CONSTANT1.getVal() != 1) + throw new RuntimeException("fail"); + if (constant_directive.TYPE1_CONSTANT2.getVal() != 2) + throw new RuntimeException("fail"); + if (constant_directive.TYPE1_CONSTANT3.getVal() != 3) + throw new RuntimeException("fail"); + } +} diff --git a/Source/Modules/csharp.cxx b/Source/Modules/csharp.cxx index 39cab8f0a..3b1e03560 100644 --- a/Source/Modules/csharp.cxx +++ b/Source/Modules/csharp.cxx @@ -914,21 +914,10 @@ public: String *null_attribute = 0; // Now write code to make the function call if (!native_function_flag) { - if (Cmp(nodeType(n), "constant") == 0) { - // Wrapping a constant hack - Swig_save("functionWrapper", n, "wrap:action", NIL); - - // below based on Swig_VargetToFunction() - SwigType *ty = Swig_wrapped_var_type(Getattr(n, "type"), use_naturalvar_mode(n)); - Setattr(n, "wrap:action", NewStringf("%s = (%s)(%s);", Swig_cresult_name(), SwigType_lstr(ty, 0), Getattr(n, "value"))); - } Swig_director_emit_dynamic_cast(n, f); String *actioncode = emit_action(n); - if (Cmp(nodeType(n), "constant") == 0) - Swig_restore(n); - /* Return value if necessary */ if ((tm = Swig_typemap_lookup_out("out", n, Swig_cresult_name(), f, actioncode))) { canThrow(n, "out", n); diff --git a/Source/Modules/d.cxx b/Source/Modules/d.cxx index da31504e6..8546372ea 100644 --- a/Source/Modules/d.cxx +++ b/Source/Modules/d.cxx @@ -1701,21 +1701,10 @@ public: String *null_attribute = 0; // Now write code to make the function call if (!native_function_flag) { - if (Cmp(nodeType(n), "constant") == 0) { - // Wrapping a constant hack - Swig_save("functionWrapper", n, "wrap:action", NIL); - - // below based on Swig_VargetToFunction() - SwigType *ty = Swig_wrapped_var_type(Getattr(n, "type"), use_naturalvar_mode(n)); - Setattr(n, "wrap:action", NewStringf("%s = (%s) %s;", Swig_cresult_name(), SwigType_lstr(ty, 0), Getattr(n, "value"))); - } Swig_director_emit_dynamic_cast(n, f); String *actioncode = emit_action(n); - if (Cmp(nodeType(n), "constant") == 0) - Swig_restore(n); - /* Return value if necessary */ if ((tm = Swig_typemap_lookup_out("out", n, Swig_cresult_name(), f, actioncode))) { canThrow(n, "out", n); diff --git a/Source/Modules/java.cxx b/Source/Modules/java.cxx index 440fbf95a..82ecb41a4 100644 --- a/Source/Modules/java.cxx +++ b/Source/Modules/java.cxx @@ -1039,26 +1039,15 @@ public: } } + // Now write code to make the function call if (!native_function_flag) { - if (Cmp(nodeType(n), "constant") == 0) { - // Wrapping a constant hack - Swig_save("functionWrapper", n, "wrap:action", NIL); - // below based on Swig_VargetToFunction() - SwigType *ty = Swig_wrapped_var_type(Getattr(n, "type"), use_naturalvar_mode(n)); - Setattr(n, "wrap:action", NewStringf("%s = (%s)(%s);", Swig_cresult_name(), SwigType_lstr(ty, 0), Getattr(n, "value"))); - } - - // Now write code to make the function call Swig_director_emit_dynamic_cast(n, f); String *actioncode = emit_action(n); // Handle exception classes specified in the "except" feature's "throws" attribute addThrows(n, "feature:except", n); - if (Cmp(nodeType(n), "constant") == 0) - Swig_restore(n); - /* Return value if necessary */ if ((tm = Swig_typemap_lookup_out("out", n, Swig_cresult_name(), f, actioncode))) { addThrows(n, "tmap:out", n); diff --git a/Source/Modules/modula3.cxx b/Source/Modules/modula3.cxx index 2d6f26108..d9a0c922b 100644 --- a/Source/Modules/modula3.cxx +++ b/Source/Modules/modula3.cxx @@ -1407,25 +1407,12 @@ MODULA3(): } } - if (Cmp(nodeType(n), "constant") == 0) { - // Wrapping a constant hack - Swig_save("functionWrapper", n, "wrap:action", NIL); - - // below based on Swig_VargetToFunction() - SwigType *ty = Swig_wrapped_var_type(Getattr(n, "type"), use_naturalvar_mode(n)); - Setattr(n, "wrap:action", NewStringf("%s = (%s)(%s);", Swig_cresult_name(), SwigType_lstr(ty, 0), Getattr(n, "value"))); - } - Setattr(n, "wrap:name", wname); // Now write code to make the function call if (!native_function_flag) { String *actioncode = emit_action(n); - if (Cmp(nodeType(n), "constant") == 0) { - Swig_restore(n); - } - /* Return value if necessary */ String *tm; if ((tm = Swig_typemap_lookup_out("out", n, Swig_cresult_name(), f, actioncode))) { diff --git a/Source/Swig/cwrap.c b/Source/Swig/cwrap.c index c97640b08..9da4e0829 100644 --- a/Source/Swig/cwrap.c +++ b/Source/Swig/cwrap.c @@ -1614,7 +1614,14 @@ int Swig_VargetToFunction(Node *n, int flags) { Delete(mangled); Delete(sname); } else { - String *nname = SwigType_namestr(name); + String *nname = 0; + if (Equal(nodeType(n), "constant")) { + String *rawval = Getattr(n, "rawval"); + String *value = rawval ? rawval : Getattr(n, "value"); + nname = NewStringf("(%s)", value); + } else { + nname = SwigType_namestr(name); + } call = Swig_wrapped_var_assign(type, nname, varcref); cres = Swig_cresult(ty, Swig_cresult_name(), call); Setattr(n, "wrap:action", cres);