From 470ce2dd3110d3d740d2632744b517d4f87a28fd Mon Sep 17 00:00:00 2001 From: Zackery Spytz Date: Mon, 18 Feb 2019 04:23:40 -0700 Subject: [PATCH 1/4] [OCaml] Add missing INPUT, OUTPUT, and INOUT typemaps for primitives The typemaps are based on PHP's. --- Examples/test-suite/multivalue.i | 5 ++- Examples/test-suite/ocaml/Makefile.in | 6 +++ Examples/test-suite/ocaml/inout_runme.ml | 8 ++++ Examples/test-suite/ocaml/multivalue_runme.ml | 8 ++++ Lib/ocaml/ocaml.i | 4 -- Lib/ocaml/typecheck.i | 38 +++++++++++++++++++ Lib/ocaml/typemaps.i | 10 ----- 7 files changed, 64 insertions(+), 15 deletions(-) create mode 100644 Examples/test-suite/ocaml/inout_runme.ml create mode 100644 Examples/test-suite/ocaml/multivalue_runme.ml diff --git a/Examples/test-suite/multivalue.i b/Examples/test-suite/multivalue.i index f92e6c78b..d10613289 100644 --- a/Examples/test-suite/multivalue.i +++ b/Examples/test-suite/multivalue.i @@ -26,6 +26,10 @@ void divide_v(int a, int b, int *OUTPUT, int *OUTPUT); void divide_mv(int a, int b, int *OUTPUT, int *OUTPUT); +#else +void divide_l(int a, int b, int *OUTPUT, int *OUTPUT); +void divide_v(int a, int b, int *OUTPUT, int *OUTPUT); +void divide_mv(int a, int b, int *OUTPUT, int *OUTPUT); #endif %{ @@ -49,4 +53,3 @@ void divide_mv(int a, int b, int *quotient_p, int *remainder_p) } %} - diff --git a/Examples/test-suite/ocaml/Makefile.in b/Examples/test-suite/ocaml/Makefile.in index fd1daed15..775b1ea71 100644 --- a/Examples/test-suite/ocaml/Makefile.in +++ b/Examples/test-suite/ocaml/Makefile.in @@ -34,6 +34,12 @@ FAILING_C_TESTS = \ enums \ preproc_constants_c \ +CPP_TEST_CASES += \ + inout \ + +C_TEST_CASES += \ + multivalue \ + ml_runme = $(SCRIPTPREFIX)$*$(SCRIPTSUFFIX) run_testcase = \ diff --git a/Examples/test-suite/ocaml/inout_runme.ml b/Examples/test-suite/ocaml/inout_runme.ml new file mode 100644 index 000000000..767bb43e6 --- /dev/null +++ b/Examples/test-suite/ocaml/inout_runme.ml @@ -0,0 +1,8 @@ +open Swig +open Inout + +let _ = + assert (_AddOne1 '(1.) as float = 2.); + assert (_AddOne3 '(1, 1, 1) = C_list ['2.;'2.;'2.]); + assert (_AddOne1r '(1.) as float = 2.); +;; diff --git a/Examples/test-suite/ocaml/multivalue_runme.ml b/Examples/test-suite/ocaml/multivalue_runme.ml new file mode 100644 index 000000000..d7e19e7d8 --- /dev/null +++ b/Examples/test-suite/ocaml/multivalue_runme.ml @@ -0,0 +1,8 @@ +open Swig +open Multivalue + +let _ = + assert (_divide_l '(37, 5) = C_list ['7;'2]); + assert (_divide_v '(41, 7) = C_list ['5;'6]); + assert (_divide_mv '(91, 13) = C_list ['7;'0]); +;; diff --git a/Lib/ocaml/ocaml.i b/Lib/ocaml/ocaml.i index 96a36c8b2..0db48d348 100644 --- a/Lib/ocaml/ocaml.i +++ b/Lib/ocaml/ocaml.i @@ -33,10 +33,6 @@ %insert(classtemplate) "class.swg" -/* Definitions */ -#define SWIG_malloc(size) swig_malloc(size, FUNC_NAME) -#define SWIG_free(mem) free(mem) - /* Read in standard typemaps. */ %include %include diff --git a/Lib/ocaml/typecheck.i b/Lib/ocaml/typecheck.i index 2cc8dcbec..538b694d3 100644 --- a/Lib/ocaml/typecheck.i +++ b/Lib/ocaml/typecheck.i @@ -153,6 +153,44 @@ $1 = !caml_ptr_val_internal($input, &ptr, 0); } +%define INPUT_OUTPUT_INOUT_TYPEMAPS(type, c_to_ocaml, ocaml_to_c) +%typemap(in) type *INPUT(type temp), type &INPUT(type temp) { + temp = (type)ocaml_to_c($input); + $1 = &temp; +} +%typemap(typecheck) type *INPUT = type; +%typemap(typecheck) type &INPUT = type; + +%typemap(in, numinputs=0) type *OUTPUT($*1_ltype temp), type &OUTPUT($*1_ltype temp) "$1 = &temp;" +%typemap(argout) type *OUTPUT, type &OUTPUT { + swig_result = caml_list_append(swig_result, c_to_ocaml(*$1)); +} +%typemap(in) type *INOUT = type *INPUT; +%typemap(in) type &INOUT = type &INPUT; + +%typemap(argout) type *INOUT = type *OUTPUT; +%typemap(argout) type &INOUT = type &OUTPUT; + +%typemap(typecheck) type *INOUT = type; +%typemap(typecheck) type &INOUT = type; +%enddef + +INPUT_OUTPUT_INOUT_TYPEMAPS(bool, caml_val_bool, caml_long_val); +INPUT_OUTPUT_INOUT_TYPEMAPS(int, caml_val_int, caml_long_val); +INPUT_OUTPUT_INOUT_TYPEMAPS(long, caml_val_long, caml_long_val); +INPUT_OUTPUT_INOUT_TYPEMAPS(short, caml_val_int, caml_long_val); +INPUT_OUTPUT_INOUT_TYPEMAPS(char, caml_val_char, caml_long_val); +INPUT_OUTPUT_INOUT_TYPEMAPS(signed char, caml_val_char, caml_long_val); +INPUT_OUTPUT_INOUT_TYPEMAPS(float, caml_val_float, caml_double_val); +INPUT_OUTPUT_INOUT_TYPEMAPS(double, caml_val_double, caml_double_val); +INPUT_OUTPUT_INOUT_TYPEMAPS(unsigned int, caml_val_uint, caml_long_val); +INPUT_OUTPUT_INOUT_TYPEMAPS(unsigned long, caml_val_ulong, caml_long_val); +INPUT_OUTPUT_INOUT_TYPEMAPS(unsigned short, caml_val_ushort, caml_long_val); +INPUT_OUTPUT_INOUT_TYPEMAPS(unsigned char, caml_val_uchar, caml_long_val); +INPUT_OUTPUT_INOUT_TYPEMAPS(long long, caml_val_long, caml_long_val); +INPUT_OUTPUT_INOUT_TYPEMAPS(unsigned long long, caml_val_ulong, caml_long_val); +#undef INPUT_OUTPUT_INOUT_TYPEMAPS + /* ------------------------------------------------------------ * Exception handling * ------------------------------------------------------------ */ diff --git a/Lib/ocaml/typemaps.i b/Lib/ocaml/typemaps.i index a6c7ef47c..916b189d0 100644 --- a/Lib/ocaml/typemaps.i +++ b/Lib/ocaml/typemaps.i @@ -142,13 +142,6 @@ %typemap(directorout) C_NAME { $1 = OCAML_TO_C($input); } -%typemap(in) C_NAME *INPUT ($*1_ltype temp) { - temp = ($*1_ltype) OCAML_TO_C($input); - $1 = &temp; -} -%typemap(in,numinputs=0) C_NAME *OUTPUT ($*1_ltype temp) { - $1 = &temp; -} /* Out */ %typemap(out) C_NAME { $result = C_TO_OCAML($1); @@ -159,9 +152,6 @@ %typemap(varout) C_NAME & { $result = C_TO_OCAML($1); } -%typemap(argout) C_NAME *OUTPUT { - swig_result = caml_list_append(swig_result, C_TO_OCAML((long)*$1)); -} %typemap(out) C_NAME & { $result = C_TO_OCAML(*$1); } From b74b2189e6a3462a9dab0b299e82bc7086d2f077 Mon Sep 17 00:00:00 2001 From: Zackery Spytz Date: Mon, 18 Feb 2019 22:35:55 -0700 Subject: [PATCH 2/4] [OCaml] Rename ocaml.swg to ocamlrun.swg Rename ocamldec.swg to ocamlrundec.swg. --- Lib/ocaml/ocaml.i | 4 ++-- Lib/ocaml/{ocaml.swg => ocamlrun.swg} | 0 Lib/ocaml/{ocamldec.swg => ocamlrundec.swg} | 2 +- Source/Modules/ocaml.cxx | 4 ++-- 4 files changed, 5 insertions(+), 5 deletions(-) rename Lib/ocaml/{ocaml.swg => ocamlrun.swg} (100%) rename Lib/ocaml/{ocamldec.swg => ocamlrundec.swg} (99%) diff --git a/Lib/ocaml/ocaml.i b/Lib/ocaml/ocaml.i index 0db48d348..581f84d04 100644 --- a/Lib/ocaml/ocaml.i +++ b/Lib/ocaml/ocaml.i @@ -8,7 +8,7 @@ %insert(runtime) "swigrun.swg" /* Include headers */ -%insert(runtime) "ocamldec.swg" +%insert(runtime) "ocamlrundec.swg" /* Type registration */ %insert(init) "swiginit.swg" @@ -28,7 +28,7 @@ %} /*#ifndef SWIG_NOINCLUDE*/ -%insert(runtime) "ocaml.swg" +%insert(runtime) "ocamlrun.swg" /*#endif*/ %insert(classtemplate) "class.swg" diff --git a/Lib/ocaml/ocaml.swg b/Lib/ocaml/ocamlrun.swg similarity index 100% rename from Lib/ocaml/ocaml.swg rename to Lib/ocaml/ocamlrun.swg diff --git a/Lib/ocaml/ocamldec.swg b/Lib/ocaml/ocamlrundec.swg similarity index 99% rename from Lib/ocaml/ocamldec.swg rename to Lib/ocaml/ocamlrundec.swg index 0c2faf771..555f9a44f 100644 --- a/Lib/ocaml/ocamldec.swg +++ b/Lib/ocaml/ocamlrundec.swg @@ -1,5 +1,5 @@ /* ----------------------------------------------------------------------------- - * ocamldec.swg + * ocamlrundec.swg * * Ocaml runtime code -- declarations * ----------------------------------------------------------------------------- */ diff --git a/Source/Modules/ocaml.cxx b/Source/Modules/ocaml.cxx index 8b248bad2..27a001f85 100644 --- a/Source/Modules/ocaml.cxx +++ b/Source/Modules/ocaml.cxx @@ -1866,9 +1866,9 @@ public: } String *runtimeCode() { - String *s = Swig_include_sys("ocaml.swg"); + String *s = Swig_include_sys("ocamlrun.swg"); if (!s) { - Printf(stderr, "*** Unable to open 'ocaml.swg'\n"); + Printf(stderr, "*** Unable to open 'ocamlrun.swg'\n"); s = NewString(""); } return s; From 092c51c06e87a5b2a15efc1eb488414302a3e375 Mon Sep 17 00:00:00 2001 From: Zackery Spytz Date: Mon, 18 Feb 2019 22:42:20 -0700 Subject: [PATCH 3/4] [OCaml] Rename typemaps.i to ocaml.swg --- Lib/ocaml/ocaml.i | 2 +- Lib/ocaml/{typemaps.i => ocaml.swg} | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) rename Lib/ocaml/{typemaps.i => ocaml.swg} (99%) diff --git a/Lib/ocaml/ocaml.i b/Lib/ocaml/ocaml.i index 581f84d04..cc26d1859 100644 --- a/Lib/ocaml/ocaml.i +++ b/Lib/ocaml/ocaml.i @@ -35,7 +35,7 @@ /* Read in standard typemaps. */ %include -%include +%include %include %include %include diff --git a/Lib/ocaml/typemaps.i b/Lib/ocaml/ocaml.swg similarity index 99% rename from Lib/ocaml/typemaps.i rename to Lib/ocaml/ocaml.swg index 08a0c97c9..ac496bdba 100644 --- a/Lib/ocaml/typemaps.i +++ b/Lib/ocaml/ocaml.swg @@ -1,5 +1,5 @@ /* ----------------------------------------------------------------------------- - * typemaps.i + * ocaml.swg * * The Ocaml module handles all types uniformly via typemaps. Here * are the definitions. From 1eb9cd3211ef646aa4683dd976eb2d06d6826bcc Mon Sep 17 00:00:00 2001 From: Zackery Spytz Date: Mon, 18 Feb 2019 23:02:20 -0700 Subject: [PATCH 4/4] [OCaml] Move INPUT, OUTPUT, and INOUT typemaps to typemaps.i --- Examples/test-suite/multivalue.i | 1 + Lib/ocaml/typecheck.i | 38 --------------------------- Lib/ocaml/typemaps.i | 44 ++++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 38 deletions(-) create mode 100644 Lib/ocaml/typemaps.i diff --git a/Examples/test-suite/multivalue.i b/Examples/test-suite/multivalue.i index d10613289..3bc3471a1 100644 --- a/Examples/test-suite/multivalue.i +++ b/Examples/test-suite/multivalue.i @@ -27,6 +27,7 @@ void divide_v(int a, int b, int *OUTPUT, int *OUTPUT); void divide_mv(int a, int b, int *OUTPUT, int *OUTPUT); #else +%include "typemaps.i" void divide_l(int a, int b, int *OUTPUT, int *OUTPUT); void divide_v(int a, int b, int *OUTPUT, int *OUTPUT); void divide_mv(int a, int b, int *OUTPUT, int *OUTPUT); diff --git a/Lib/ocaml/typecheck.i b/Lib/ocaml/typecheck.i index 74d2727e1..0c0a600a0 100644 --- a/Lib/ocaml/typecheck.i +++ b/Lib/ocaml/typecheck.i @@ -172,44 +172,6 @@ %typecheck(SWIG_TYPECHECK_SWIGOBJECT) CAML_VALUE "$1 = 1;" -%define INPUT_OUTPUT_INOUT_TYPEMAPS(type, c_to_ocaml, ocaml_to_c) -%typemap(in) type *INPUT(type temp), type &INPUT(type temp) { - temp = (type)ocaml_to_c($input); - $1 = &temp; -} -%typemap(typecheck) type *INPUT = type; -%typemap(typecheck) type &INPUT = type; - -%typemap(in, numinputs=0) type *OUTPUT($*1_ltype temp), type &OUTPUT($*1_ltype temp) "$1 = &temp;" -%typemap(argout) type *OUTPUT, type &OUTPUT { - swig_result = caml_list_append(swig_result, c_to_ocaml(*$1)); -} -%typemap(in) type *INOUT = type *INPUT; -%typemap(in) type &INOUT = type &INPUT; - -%typemap(argout) type *INOUT = type *OUTPUT; -%typemap(argout) type &INOUT = type &OUTPUT; - -%typemap(typecheck) type *INOUT = type; -%typemap(typecheck) type &INOUT = type; -%enddef - -INPUT_OUTPUT_INOUT_TYPEMAPS(bool, caml_val_bool, caml_long_val); -INPUT_OUTPUT_INOUT_TYPEMAPS(int, caml_val_int, caml_long_val); -INPUT_OUTPUT_INOUT_TYPEMAPS(long, caml_val_long, caml_long_val); -INPUT_OUTPUT_INOUT_TYPEMAPS(short, caml_val_int, caml_long_val); -INPUT_OUTPUT_INOUT_TYPEMAPS(char, caml_val_char, caml_long_val); -INPUT_OUTPUT_INOUT_TYPEMAPS(signed char, caml_val_char, caml_long_val); -INPUT_OUTPUT_INOUT_TYPEMAPS(float, caml_val_float, caml_double_val); -INPUT_OUTPUT_INOUT_TYPEMAPS(double, caml_val_double, caml_double_val); -INPUT_OUTPUT_INOUT_TYPEMAPS(unsigned int, caml_val_uint, caml_long_val); -INPUT_OUTPUT_INOUT_TYPEMAPS(unsigned long, caml_val_ulong, caml_long_val); -INPUT_OUTPUT_INOUT_TYPEMAPS(unsigned short, caml_val_ushort, caml_long_val); -INPUT_OUTPUT_INOUT_TYPEMAPS(unsigned char, caml_val_uchar, caml_long_val); -INPUT_OUTPUT_INOUT_TYPEMAPS(long long, caml_val_long, caml_long_val); -INPUT_OUTPUT_INOUT_TYPEMAPS(unsigned long long, caml_val_ulong, caml_long_val); -#undef INPUT_OUTPUT_INOUT_TYPEMAPS - /* ------------------------------------------------------------ * Exception handling * ------------------------------------------------------------ */ diff --git a/Lib/ocaml/typemaps.i b/Lib/ocaml/typemaps.i new file mode 100644 index 000000000..39231e221 --- /dev/null +++ b/Lib/ocaml/typemaps.i @@ -0,0 +1,44 @@ +/* ---------------------------------------------------------------------------- + * typemaps.i + * + * These typemaps provide support for input/output arguments for C/C++ pointers + * and C++ references. +* ---------------------------------------------------------------------------- */ + +%define INPUT_OUTPUT_INOUT_TYPEMAPS(type, c_to_ocaml, ocaml_to_c) +%typemap(in) type *INPUT(type temp), type &INPUT(type temp) { + temp = (type)ocaml_to_c($input); + $1 = &temp; +} +%typemap(typecheck) type *INPUT = type; +%typemap(typecheck) type &INPUT = type; + +%typemap(in, numinputs=0) type *OUTPUT($*1_ltype temp), type &OUTPUT($*1_ltype temp) "$1 = &temp;" +%typemap(argout) type *OUTPUT, type &OUTPUT { + swig_result = caml_list_append(swig_result, c_to_ocaml(*$1)); +} +%typemap(in) type *INOUT = type *INPUT; +%typemap(in) type &INOUT = type &INPUT; + +%typemap(argout) type *INOUT = type *OUTPUT; +%typemap(argout) type &INOUT = type &OUTPUT; + +%typemap(typecheck) type *INOUT = type; +%typemap(typecheck) type &INOUT = type; +%enddef + +INPUT_OUTPUT_INOUT_TYPEMAPS(bool, caml_val_bool, caml_long_val); +INPUT_OUTPUT_INOUT_TYPEMAPS(int, caml_val_int, caml_long_val); +INPUT_OUTPUT_INOUT_TYPEMAPS(long, caml_val_long, caml_long_val); +INPUT_OUTPUT_INOUT_TYPEMAPS(short, caml_val_int, caml_long_val); +INPUT_OUTPUT_INOUT_TYPEMAPS(char, caml_val_char, caml_long_val); +INPUT_OUTPUT_INOUT_TYPEMAPS(signed char, caml_val_char, caml_long_val); +INPUT_OUTPUT_INOUT_TYPEMAPS(float, caml_val_float, caml_double_val); +INPUT_OUTPUT_INOUT_TYPEMAPS(double, caml_val_double, caml_double_val); +INPUT_OUTPUT_INOUT_TYPEMAPS(unsigned int, caml_val_uint, caml_long_val); +INPUT_OUTPUT_INOUT_TYPEMAPS(unsigned long, caml_val_ulong, caml_long_val); +INPUT_OUTPUT_INOUT_TYPEMAPS(unsigned short, caml_val_ushort, caml_long_val); +INPUT_OUTPUT_INOUT_TYPEMAPS(unsigned char, caml_val_uchar, caml_long_val); +INPUT_OUTPUT_INOUT_TYPEMAPS(long long, caml_val_long, caml_long_val); +INPUT_OUTPUT_INOUT_TYPEMAPS(unsigned long long, caml_val_ulong, caml_long_val); +#undef INPUT_OUTPUT_INOUT_TYPEMAPS