Commit Graph

224 Commits

Author SHA1 Message Date
William S Fulton 5124908ed5 [Lua] Remove unused director helper functions
SWIG_Lua_director_has_method() and SWIG_Lua_director_call_method() in
director.swg are not referenced by the generated code or anywhere else. The
generated director methods look up the method override themselves, via
SWIG_Lua_get_director_override(), and make the lua_pcall() inline.

Assisted-by: Claude Code (Opus 4.8)
2026-07-14 20:16:23 +01:00
William S Fulton 7201136121 [Lua] Add director support for Lua 5.1 and LuaJIT
Directors store the table of Lua method overrides for an object in the
userdata's uservalue. Uservalues were added in Lua 5.2, so directors did not
compile at all for Lua 5.1 or LuaJIT, failing with:

  error: 'lua_getuservalue' was not declared in this scope
  error: 'lua_setuservalue' was not declared in this scope

Uservalues replaced the userdata environments of Lua 5.1, so use the
environment functions, lua_getfenv and lua_setfenv, when wrapping for Lua 5.1.

The two are not quite interchangeable and simply defining lua_getuservalue to
lua_getfenv is subtly wrong. A new userdata has no uservalue (nil) in Lua 5.2
and later, but in Lua 5.1 it inherits the environment of the C function
creating it, which turns out to be Lua's package table, shared by every
userdata the module creates. The "is there a table of overrides yet?" test
would then always succeed and every object's overrides would be written into
that one shared table - overrides set on one object would be seen by all other
objects and the package table would be corrupted. So the tables created for
overrides are marked with a key that is not a valid C++ method name, and an
unmarked environment is reported as nil to match Lua 5.2.

The override lookup was inlined into every generated director method, so it is
now generated as a call to a new SWIG_Lua_get_director_override() in
director.swg instead. This puts the uservalue handling in one place and makes
the generated code smaller.

The Lua examples and the whole Lua test-suite, including all the director
tests, now pass with Lua 5.1, and continue to pass with Lua 5.3. A director
wrapper also compiles against LuaJIT's headers.

Assisted-by: Claude Code (Opus 4.8)
2026-07-14 20:16:20 +01:00
William S Fulton 294c944c10 [Lua] Restore Lua 5.1 and LuaJIT compatibility for non-director wrapping
the new Lua directors feature uses lua_setuservalue, added in Lua 5.2.
Removing the LUA_VERSION_NUM-guarded compatibility defines for lua_rawlen,
lua_pushglobaltable and lua_rawsetp/lua_rawgetp from Lib/lua/luarun.swg.

LuaJIT implements the Lua 5.1 API and, per its own documentation, is
"API+ABI-compatible with Lua 5.1, which prevents implementing features
that would otherwise break the Lua/C API and ABI" (https://luajit.org/extensions.html,
"Extensions from Lua 5.2" section) - so it never defines these Lua 5.2 additions.
Noticed when rebuilding obs-studio's LuaJIT-based Lua bindings against SWIG master.

Restore those compatibility defines, scoped to 5.1 and later only (the old
Lua 5.0-only shims are not restored).

Also revert an incidental ok!=LUA_OK comparison introduced by #3394
back to ok!=0, since LUA_OK isn't defined pre-5.2 either and luaL_dostring()
already returns 0 on success on every Lua version.

Fix the examples and test-suite where they themselves used Lua 5.2 only
features: LUA_OK in Examples/lua/embed/embed.c, _ENV in helpers.lua, and
string.format("%c", ...) in li_cdata_bytes, which yields an empty string for a
NUL byte in Lua 5.1 as strlen() is applied to the formatted result, so
string.char() is used instead.

Directors continue to require Lua 5.2 or later and are unaffected by this
change; the Lua 5.1 director test failures are addressed separately.

Assisted-by: Claude Code (Sonnet 5)
2026-07-14 20:15:39 +01:00
William S Fulton 3dda830ac9 Add char *& string typemaps to all languages and char_strings runme parity
A 'char *&' (a reference to a char pointer) was only marshalled as a string by
C#, D, Go, Java and PHP; every other language treated it as an opaque char **
pointer.  Add char *& string typemaps to the languages that were missing them,
so a char *& function argument, return value or variable is marshalled as a
string in every target language.  SWIG's const reference stripping means this
also covers char *const&.

- Lib/typemaps/strings.swg: add Char *& to the %typemaps_string in, freearg,
  out, typecheck and director typemaps.  This gives char *& to the languages
  that use the Unified Typemap Library (Python, Ruby, Perl, Tcl, Octave, Scilab,
  R and JavaScript).  The shared in typemap now casts &buf to $1_ltype so that a
  const char * reference hidden behind a typedef also compiles.
- Lib/lua/luatypemaps.swg, Lib/guile/typemaps.i, Lib/ocaml/ocaml.swg: these
  define their own char * string typemaps; give each char *& and const char *&
  in/out/freearg typemaps too.
- Lib/r/rtype.swg: the C wrapper returned the char *& string correctly but the R
  proxy wrapped it as an undefined _p_p_char S4 class; add char *& to the
  scoerceout char list so it is returned as a plain character value.

char_strings.i is now exercised by a runme in every target language, all testing
the same set of functions (get/set/pingpong/global variables and all four char *&
functions), giving complete char *& typemap coverage and testing.

Where a language genuinely cannot support part of the char array portion of the
test (a char[] global has no varin typemap in most scripting languages, a char[16]
parameter is bounds checked, and Guile/OCaml reject char[] parameters), that one
assertion is adapted or skipped with an inline comment; the char *& coverage is
complete everywhere.

Assisted-by: Claude Code (Opus 4.8)
2026-07-09 21:55:58 +01:00
William S Fulton 445905e2cd Cosmetic fixes for Lua 5.0/5.1 drop
- configure.ac: fix "Bellow" -> "below" in the unsupported version warning
- Doc/Manual/Lua.html: fix grammar in the eLua status sentence
- Lib/lua/luarun.swg: collapse a two-line comment to one line

Assisted-by: Claude Opus 4.7
2026-05-30 17:24:17 +01:00
Erez Geva 74ff929bd4 Remove Lua 5.0 and Lua 5.1 and Lua options
- configure.ac
  - Use minimum version 5.2 for Lua
  - Add lua 5.5, it was released in dec 2025
  - Syntax improvements
- GHA CI
  - Remove 'default' Lua version, it is confusing.
    Just select the version explicitly.
  - Add Lua 5.4 test, we need to test all supported versions 5.2, 5.3 and 5.4
    Lua 5.5 is not available on Ubuntu yet.
- Lua documentation Doc/Manual/Lua.html
  - Update version to 5.2
  - Remove mention to older SWIG version,
    whom wishes to go to the very old history can look on CHANGES.
  - Add references on missing operators in Lua.
- _ENV exist from Lua 5.2.
- Remove old compatibility code from Lib/lua/luarun.swg.
- Remove LUA_VERSION_NUM from Lua C wrapping code.
  It was used for old Lua 5.0 and Lua 5.1.
- Remove _VERSION from Lua tests.
  Note: _VERSION remains a keyword and developers may use it in interface files!
- Remove Example/lua/import.lua and Examples/test-suite/lua/import.lua
  as they are not used or needed any longer.
  - Remove old Lua import.
- Add Examples/test-suite/lua/helpers.lua
  - include it in all tests in test-suite.
  - Add catch "undefined" global variables enable function.
  - Add import_to_globs function.
    To add module functions, variables and classes to global scope.
- Remove the -squash-bases option as it is broken
  The der.new_func from the example
  in the document,  "29.3.18.3 Inheritance",
  pass from base object to derived object
  regardless of using the flag or not.
- Remove -no-old-metatable-bindings as it is deprecated in the last 12 years

Signed-off-by: Erez Geva <ErezGeva2@gmail.com>
2026-05-30 17:24:17 +01:00
William S Fulton a1e619b7cb argcargv / OOM-protection follow-ups
- [Lua] Fix off-by-one when nil terminates the table early: use $1 = i
  (not i + 1) to match the count of strings actually copied, and pop
  the nil from the Lua stack before breaking.

- [Python] In SWIG_AsCharPtrAndSize (pystrings.swg) decref bytes / obj
  on the SWIG_MemoryError early returns added by the previous commits,
  so an allocation failure no longer leaks the temporary PyObject.

- [Octave, Python] In argcargv.i free already-allocated string entries
  and the array itself before failing on a per-element OOM (Python uses
  the existing break path so the freearg typemap performs the cleanup).

- [Perl, PHP, Tcl] Drop unreachable `goto fail` after SWIG_croak /
  SWIG_PHP_Error / SWIG_exception_fail, all of which already invoke
  SWIG_fail. Compilers warning on unreachable code complained.

- Standardise the OOM error wording across languages and fix the
  3- and 5-space indentation introduced by the previous commits.

- Update Examples/perl5/xmlstring/xmlstring.i to the new
  %typemaps_string signature (it picks up the missing WarningLeakMsg
  argument too) and add a SWIG_NewCopyXMLChArray fragment.

- CHANGES.current: document the API breakages introduced by removing
  %new_copy_array, removing %typemaps_string_alloc and extending
  %typemaps_string, including the actual error text users will see.

Assisted-by: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-19 21:01:30 +01:00
Erez Geva d051fe9775 Add protection to allocations in `argcargv` typemap
Signed-off-by: Erez Geva <ErezGeva2@gmail.com>
2026-05-19 21:01:30 +01:00
Christophe Calmejane ae916b4bb6 Add support for shared_ptr for Lua
- Added lua smart pointers typemaps (boost and std)
- Handling nullptr case for typecheck without memory allocation
- Handling feature:smartptr
- Remove redundant shared_ptr testcase entries in lua test-suite
- Added unit tests for lua smart pointers

Closes #3387
2026-03-26 08:37:00 +00:00
Christophe Calmejane 9a60f9573f
Task/std unordered map set lua (#3386)
* Added lua unit tests for std::unordered_map std::unordered_set

* Added lua typemaps for std::unordered_map and std::unordered_set
2026-03-23 19:46:49 +00:00
Christophe Calmejane 95904761fd
Support for std::array and std::set for lua (#3305)
Added std::array and std::set for lua
2026-03-05 21:51:51 +00:00
William S Fulton f66873434c Lua directors cleanup/tweaks
Consistency changes with other target languages.
Minor code cleanup and remove warnings.
Minor documentation edits.
2025-12-12 18:54:53 +00:00
Christophe Calmejane 9d45588941 Added directors support for lua 2025-12-12 07:16:24 +00:00
Erez Geva 0c613c7f06 string-length typemaps typecheck
Use SWIG_TYPECHECK_STRING as typemaps represent string.
Add typecheck to Go, Java, C# and D languages.

Signed-off-by: Erez Geva <ErezGeva2@gmail.com>
2025-08-11 08:28:02 +01:00
Erez Geva 2bc9ede0c1 Add typecheck.
Add test with char_binary_rev_len 2 constructors.

Signed-off-by: Goh, Wei Sheng <wei.sheng.goh@intel.com>
2025-08-11 08:28:02 +01:00
Erez Geva cb68446ece Fix Length & string reverse order typemap.
And add it to missing languages: C#, Java, Lua, PHP.

Signed-off-by: Erez Geva <ErezGeva2@gmail.com>
2025-07-28 17:07:42 +02:00
Armin Brauns 390844a7d2 Fix "unsigned long long" being interpreted as "signed long long" in Lua 2024-12-05 15:37:42 +00:00
William S Fulton ba9b0a35ab Merge branch 'char_binary_java_fix-tidyup'
* char_binary_java_fix-tidyup:
  Move SWIGStringWithLengthHelper to csharphead.swg
  cdata whitespace/cosmetic fixups
  cdata doc updates
  Rename `typemaps/cdata_struct.swg` to `typemaps/cdata_begin.swg`. And `typemaps/cdata.swg` to `typemaps/cdata.swg`. Move `cdata_apply.swg` content to `typemaps/cdata.swg`.
  Group the C# marshalling of STRING-LENGTH typemap  into C# class named SWIGStringWithLengthHelper.
  Leave Length & string reverse order typemap in typemaps/strings.swg
  Support old C# as "LPUTF8Str" was add in 2017.
  Improve documentation. Follow @wsfulton reviews.
  Use a dummy for MzScheme and untested OCaml cdate. To prevent compilation error.
  Further fixing follow reviews.
  Reorganise raw data typemap,  so typemaps folder contain only common part. Improve document.
  Inline SWIG_string_to_utf8_bytes SWIG_utf8_bytes_to_string code
  Fixes of STRING/BYTES LENGTH typemaps.

 Conflicts:
	CHANGES.current
2024-06-13 10:53:19 +01:00
Erez Geva 300e5e313b Rename `typemaps/cdata_struct.swg` to `typemaps/cdata_begin.swg`.
And `typemaps/cdata.swg` to `typemaps/cdata.swg`.
Move `cdata_apply.swg` content to `typemaps/cdata.swg`.

Signed-off-by: Erez Geva <ErezGeva2@gmail.com>
2024-06-07 08:19:20 +01:00
Erez Geva 2d703e4b82 Leave Length & string reverse order typemap in typemaps/strings.swg
where they were before.
2024-06-07 08:19:20 +01:00
William S Fulton 426cdedbcc Add missing SWIGTYPE MOVE typecheck typemaps
Removes warning 472:
./../cpp11_move_only.i:78: Warning 472: Overloaded method ConstructorTester::ConstructorTester(MovableCopyable) with no explicit typecheck typemap for arg 0 of type 'MoveOnly'
./../cpp11_move_only.i:78: Warning 472: Dispatching calls to this method may not work correctly, see the 'Typemaps and Overloading' section in Typemaps chapter of the documentation
2024-03-07 08:28:06 +00:00
William S Fulton 24a66e6125 Add const std::unique_ptr & input typemaps 2024-03-06 21:46:58 +00:00
William S Fulton 846b40793e Add non-const std::unique_ptr & input typemaps 2024-03-06 21:46:58 +00:00
William S Fulton 5712ce6464 std::unique_ptr return by reference typemaps added 2024-03-06 21:46:58 +00:00
William S Fulton 3f1e40d2f4 Add std::unique_ptr && output typemaps
Move semantics are not supported by default.
They behave as if a lvalue reference was returned.
2024-03-06 21:46:58 +00:00
William S Fulton ae22a97f1b Movable std::unique_ptr - add std::unique_ptr && typemaps
Closes #2650
2024-03-06 21:46:58 +00:00
Erez Geva affbd5893d Reorganise raw data typemap,
so typemaps folder contain only common part.
Improve document.

Signed-off-by: Erez Geva <ErezGeva2@gmail.com>
2024-02-29 11:58:05 +01:00
Erez Geva 1bf59e0bbc Fixes of STRING/BYTES LENGTH typemaps.
Fix Java STRING LENGTH typemap.
Use string type in static typed languages (Java, C#, D and Go).

Add BYTES LENGTH typemap and apply it for binary data.
Use byte type in static typed languages.

Add li_cdata_cpp, li_cdata and char_binary
 tests for most of languages(apart from R and experimental).

Fix the director_binary_string test and add it to C#, D, Go,
 Perl, PHP, Python, Ruby and octave.

Update documents.

Signed-off-by: Erez Geva <ErezGeva2@gmail.com>
2024-02-29 02:09:09 +01:00
William S Fulton 521d43d071 Lua Visual C++ C4100 warning fix
Closes #2760
2024-01-16 08:46:48 +00:00
Olly Betts 3ce0174a0c Fix random doubled spaces in code 2023-11-17 09:49:36 +13:00
Christophe Calmejane f7d9f5e931 Using #ifdef instead of #if to prevent warnings 2023-07-04 09:18:20 +02:00
Christophe Calmejane 2b583e9d19 Fix for #2625
Using c++17 insert_or_assign for std::map when available.
2023-07-04 09:18:20 +02:00
Olly Betts aa2a7f258e Fix passing a Lua number for C++ std::string
Order of evaluation of C++ function arguments is not defined, and if
lua_rawlen() was called before lua_tostring() then it would return 0
(because the value was still a number) and an empty string would be
passed.

The same issue affects std::string_view too, but that's not been in
a release yet.

The new testcases fail without the fix with GCC 12.2 on x86-64 Linux.
2023-05-30 17:54:19 +12:00
Olly Betts fcf0d7deab Tweak comment formatting 2023-05-23 09:20:31 +12:00
Olly Betts 6085a9661e Initial support for std::string_view
So far C#, Java, Lua and PHP are supported.

Closes: #2540
See #1567
2023-05-08 15:56:37 +12:00
Olly Betts 589373309d [Lua] Alternative SWIG_LUA_CONSTTAB_INT fix
SWIG_LUA_CONSTTAB_INT can be called with a constant expression
containing commas (e.g. `SizeOf< int,int >::size`).  This was
addressed in b13f584258 by making
its second argument variadic, but (a) this doesn't work with
our baseline of C90/C++98 and (b) the variadic syntax used is
a GCC extension which doesn't work with all compilers (e.g. MSVC).

We can solve this without needing variadic macros by simply wrapping
this argument in an extra pair of parentheses when calling.
2023-04-27 10:03:29 +12:00
Olly Betts 240f2572ae Revert "Lua variadic templates sizeof... constants fix"
This reverts commit b13f584258.
2023-04-27 10:03:29 +12:00
William S Fulton b13f584258 Lua variadic templates sizeof... constants fix 2022-12-22 22:07:45 +00:00
Olly Betts a5bc48afea [Lua] Fix type resolution between SWIG-wrapped modules
See #2126
2022-10-20 10:14:59 +13:00
William S Fulton 46f7501d94 Cleanup SWIG_VERSION definition
Add Swig_obligatory_macros which must be called by each
target language to define SWIG_VERSION correctly
in the generated code, as well as the language specific
macro SWIGXXX where XXX is the target language name.

Drop the #ifdef SWIGXXX that was previously generated -
I can't see the point of this and if users are defining
this macro somehow, then users will need to change this

Closes #1050
2022-10-13 19:47:43 +01:00
Olly Betts 4ac3c87a29 Sort out predefined SWIG-specific macros
Ensure that SWIG_VERSION is defined both at SWIG-time and in the
generated C/C++ wrapper code (it was only defined in the wrapper
for some target languages previously).

SWIGGO and SWIGJAVASCRIPT are now defined in the generated wrappers
to match behaviour for all other target languages.

Stop defining SWIGVERSION in the wrapper.  This only happened as a
side-effect of how SWIG_VERSION was defined but was never documented and
is redundant.

The new testcase also checks that SWIG is defined at SWIG-time but not
in the generated wrapper, and that exactly one of a list of
target-language specific macros is defined.

Fixes #1050
2022-10-05 12:40:15 +13:00
William S Fulton e97181ebc0 Add missing typecheck typemaps for std::auto_ptr and std::unique_ptr
To fix overloading when using these types.
2022-09-17 10:23:51 +01:00
William S Fulton dad7c93ca0 Provide SWIGTYPE MOVE typemaps in swigmove.i
For implementing full move semantics when passing parameters by value.
Based on SWIGTYPE && and std::unique_ptr typemaps which implement move
semantics.

Added for all languages, but untested for: Go, Ocaml, R, Scilab (and
unlikely to be fully functional for same reasons as for std::unique_ptr
support).

Issue #999
2022-09-16 08:36:25 +01:00
William S Fulton 7934561874 Test/fixes to handle NULL pointer for unique_ptr/auto_ptr
Also add missing unique_ptr tests for Lua and Racket.
2022-08-31 19:42:55 +01:00
William S Fulton e139a36511 SWIGTYPE && input typemaps now assume object has been moved
Replicated Java implementation.

Fully implemented for:
- C#
- D
- Guile
- Javascript (UTL)
- Lua
- MzScheme
- Octave (UTL)
- Perl (UTL)
- PHP
- Python (UTL)
- Ruby (UTL)
- Tcl (UTL)

PHP std::auto_ptr std::unique_ptr minor tweaks and testcase corrections
2022-08-31 19:40:14 +01:00
William S Fulton c10a84c775 Cosmetic stray semi-colon removal after %typemap using quotes 2022-08-31 19:40:13 +01:00
William S Fulton 41fddf61ec Add Lua support for std::unique_ptr and std::auto_ptr
Equivalent to Python/Ruby implementations.
2022-08-11 21:38:21 +01:00
William S Fulton 319442a8c4 More move semantics improvements
More removal of casts in the out typemaps when copying objects to enable
C++ compilers to possibly make use of move semantics.
2022-07-04 11:19:50 +01:00
William S Fulton bf36bf7d8a Movable and move-only types supported in "out" typemaps.
Enhance SWIGTYPE "out" typemaps to use std::move when copying
objects, thereby making use of move semantics when wrapping a function returning
by value if the returned type supports move semantics.

Wrapping functions that return move only types 'by value' now work out the box
without having to provide custom typemaps.

The implementation removed all casts in the "out" typemaps to allow the compiler to
appropriately choose calling a move constructor, where possible, otherwise a copy
constructor. The implementation alsoand required modifying SwigValueWrapper to
change a cast operator from:

  SwigValueWrapper::operator T&() const;

to

  #if __cplusplus >=201103L
    SwigValueWrapper::operator T&&() const;
  #else
    SwigValueWrapper::operator T&() const;
  #endif

This is not backwards compatible for C++11 and later when using the valuewrapper feature
if a cast is explicitly being made in user supplied "out" typemaps. Suggested change
in custom "out" typemaps for C++11 and later code:

1. Try remove the cast altogether to let the compiler use an appropriate implicit cast.
2. Change the cast, for example, from static_cast<X &> to static_cast<X &&>, using the
   __cplusplus macro if all versions of C++ need to be supported.

Issue #999
Closes #1044

More about the commit:
Added some missing "varout" typemaps for Ocaml which was falling back to
use "out" typemaps as they were missing.

Ruby std::set fix for SwigValueWrapper C++11 changes.
2022-06-30 17:26:48 +01:00
William S Fulton 6860e2bc03 Document argc argv library 2022-05-15 19:12:39 +01:00