Commit Graph

295 Commits

Author SHA1 Message Date
William S Fulton 28d92ed341 [JavaScript] Fix the v8 and node engines for v8 14.x (Node.js 26)
v8 14.1 added an EmbedderDataTypeTag to the v8::Object aligned internal field
pointer accessors and 14.2 removed the versions without one, so the generated
code did not compile with Node.js 26. The new SWIGV8_ALIGNED_POINTER_GET and
SWIGV8_ALIGNED_POINTER_SET macros select between them, switching at 14.2 as
14.1 only has the tagged setter.

The default tag is used rather than a SWIG specific one. The tag used when
setting must match the tag used when getting, and modules pass proxy objects
between each other, so a SWIG specific tag would stop a module wrapped by an
older SWIG being read by one wrapped by a newer SWIG.

v8 14.3 similarly added a type tag to v8::External and deprecated the versions
without one, which is fatal as the examples are compiled with -Werror.
SWIGV8_EXTERNAL_NEW now selects between them and the new SWIGV8_EXTERNAL_VALUE
macro does the same for reading the value back.

Node.js 26 is added to the CI test matrix for both the node and napi engines.

The minimum supported v8 version is unchanged at 7.4.

Assisted-by: Claude Code (Opus 5)
2026-08-01 09:42:44 +01:00
William S Fulton dce0f9012b [JavaScript] Fix the v8 and node engines for v8 13.x (Node.js 24)
v8 12.6 deprecated FunctionCallbackInfo::Holder() and 13.5 removed it, so the
generated code did not compile with Node.js 24. This() has been available for
as long as SWIG has supported v8 and is the documented replacement, so it is
used unconditionally via the new SWIGV8_ARGS_THIS macro.

PropertyCallbackInfo needs a version check as its Holder() replacement,
HolderV2(), was only added in v8 12.7 - SWIGV8_INFO_HOLDER handles this.

v8 13.3 added Utf8LengthV2 and WriteUtf8V2 and deprecated Utf8Length and
WriteUtf8, which is fatal as the examples are compiled with -Werror. The
existing SWIGV8_UTF8_LENGTH and SWIGV8_WRITE_UTF8 macros now select between
them. The buffer size passed to SWIGV8_WRITE_UTF8 now always includes room
for the NUL and the returned count includes the NUL, matching WriteUtf8V2
with WriteFlags::kNullTerminate.

The ARGC/ARGV typemap relied on the old behaviour of passing a buffer size
excluding the NUL. With WriteUtf8V2 that silently truncates the last
character while still returning the expected count, so the length check
would have passed for a corrupted string.

Node.js 24 is added to the CI test matrix for both the node and napi engines.

The minimum supported v8 version is unchanged at 7.4.

Assisted-by: Claude Code (Opus 5)
2026-08-01 09:42:44 +01:00
William S Fulton de573cda78 Pass SWIG_FromCharPtrAndSize and friends on to the C compiler
A bare #define at the start of a line in a brace delimited %fragment body is
consumed by the SWIG preprocessor and never reaches the generated wrapper. The
macro therefore only exists at SWIG level and code that SWIG does not macro
expand, such as a typemap delimited with %{ ... %}, is left referring to an
undefined name. Use %#define so the definition is emitted for the C compiler.

Fixes SWIG_FromCharPtrAndSize for Python, SWIG_FromBinaryCharPtrAndSize for
Octave, Perl, R and Ruby, SWIG_ToUint8Array and SWIG_FromUint8Array for
JavaScript, and the SWIG_AsVal_* and SWIG_From_* macros in the Scilab char,
short, signed char, unsigned char, unsigned long and long long files.

Lib/scilab/scipointer.swg is deliberately left alone. Its fragments are never
requested by any typemap, so they are only emitted as SWIG preprocessor
definitions; switching them to %#define drops SWIG_ConvertPtr and
SWIG_NewPointerObj from the generated code entirely. Fixing those needs the
definitions moving to a runtime insert, as the other target languages do.

Add a common test case charptr_fragment. It uses a %{ ... %} delimited typemap
so the macro name survives into the wrapper, which only compiles when the
library defines the macro for the C compiler. The target languages without
these macros wrap the same functions using their default typemaps. Verified
that the generated Python module fails to load before this change with an
undefined symbol for SWIG_FromCharPtrAndSize.

Follows on from #3522.

Assisted-by: Claude Code (Opus 5)
2026-07-31 00:10:40 +01:00
William S Fulton 5d3539131c Use the canonical capitalisation of Unified Typemap Library 2026-07-23 17:30:59 +01:00
Erez Geva cd569d122a cdata.i changes and new test li_cdata_bytes
Fix guile cdata.

Add SWIG_BINSTR flag
- Add SWIG_FromBinCharPtrAndSize with the new flag
  To add languages that use Lib/cdata.i.
- python use binary string by using the new SWIG_BINSTR
  in SWIG_AsCharPtrAndSize and SWIG_FromBinCharPtrAndSize.

Languages that were changed to uses list
- Tcl use list of integers.
- scilab use list of uint8.
  And support passing list of numbers to C.

Add support to JavaScript
- use Uint8Array instead of strings.
- Add li_cdata_carrays. tests.
- Add li_cdata_bytes tests.
- Use Debian node-addon-api package location.
- napi folder location to Examples/test-suite/javascript/Makefile.in.
- Update documentation.

Update cdata.i documentation

This new cdata test focus on:
- Ensure we can receive proper data from C and pass proper data back to C.
- Use all possoble byte values , i.e. the full range of 0 to 255
  and ensure values 128 to 255 do not pass Unicode transformation (UTF-8/16).
- Ensure zero is a valid value and not a string null termination
  nor a modified UTF-8 which transform U+0000 to 0xC0 0x80.
- Check mutability of the cdata object.

Signed-off-by: Erez Geva <ErezGeva2@gmail.com>
2026-05-25 13:16:09 +01:00
Julien Schueller 84ff343b89
Fix -Wextra-semi warnings (#3372) 2026-03-18 18:53:05 +00:00
Olly Betts 491994c836 Use NULL instead of SWIG_NULLPTR
This was added in an attempt to avoid warnings from a static analysis
tool which wants us to use nullptr instead of NULL, but we can't do so
unconditionally while continuing to support C++98.

SWIG_NULLPTR is ugly, verbose and non-standard and NULL is still
standard C++ so we've decided to just use NULL instead.  Users of tools
which don't like NULL can just suppress this warning for SWIG-generated
code.

Closes #3165
Closes #3166
2025-07-02 08:43:32 +01:00
Thanh Vu Le 935f9222a3 [Javascript] Use SetNativeDataProperty()
Use SetNativeDataProperty() instead of SetAccessor() which fixes
compatibility with recent v8 versions.

Closes #3185
Fixes #3138
2025-06-09 16:11:29 +12:00
William S Fulton 36ef36dcb0 Add <errno.h> fragment for errno usage 2024-11-22 19:07:10 +00:00
Olly Betts 7274c3f9b7 Make minimum supported v8 version 7.4
This is the oldest we are able to test in CI, and only then via it being
the version use by the oldest node.js we can test in CI.  We have no
direct testing with v8 in CI currently, and it seems hard to
v8_shell.cxx no longer building with more recent v8 versions.

See #2537
2024-09-19 08:25:44 +12:00
William S Fulton 9bf4842002 C++ reference errors when passing in a 'NULL' change of behaviour.
Most languages now use "NullReferenceError" in the error message
where they previously used "ValueError". Also exception changes:

Guile:    "swig-null-reference-error" instead of "swig-value-error"
MzScheme: "swig-null-reference-error" instead of "swig-value-error"
PHP:      zend_ce_type_error instead of zend_ce_value_error
Python:   Consistently raises TypeError instead of a mix of ValueError
          and TypeError.
Ruby:     Consistently raises NullReferenceError instead of a mix of
          ArgumentError and NullReferenceErrorError.

The consistent raising of a TypeError instead of ValueError for Python
ensures that incorrectly passing 'None' into a C++ reference argument
will correctly convert the error into a NotImplemented error for
the rich comparisons implementations per PEP 207. Fixes #2987

Note that the li_constraints checking implementation for the NONNULL
typemap for pointers also makes the same error change from
SWIG_ValueError to SWIG_NullReferenceError.

The D typemaps use SWIG_DNullReferenceException instead of
SWIG_DIllegalArgumentException, although this ultimately has no change
as the same D Exception is still thrown.
2024-09-14 13:03:36 +01:00
Olly Betts 494a782714 Fix some source comments 2024-07-19 09:28:33 +12:00
Vadim Zeitlin 474a936b15 Move javascriptkw.swg file to Lib/javascript directory
This file is JS-specific and should be in JS subdirectory, for
consistency with all the other languages.
2024-07-18 22:27:52 +02:00
Vadim Zeitlin 628e356f8b Avoid duplicating std::vector<> declaration for JavaScript
Use a helper macro abstracting the difference between the generic
version and the bool specialization of std::vector<> instead of
duplicating the entire class declaration.

No real changes, but this will help with future modifications by
allowing to make them only once instead of twice.

Don't pass separate REFERENCE to JS macro defining std::vector

Just use "CTYPE&" for both the generic case and bool specialization,
it is still wrong (as std::vector<bool>::reference is a proxy type
different from this one), but not worse than before and it's simpler to
have 2 arguments in SWIG_STD_VECTOR_INTERNAL() macro rather than 3.
2024-06-25 07:54:00 +01: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
William S Fulton 2d1944a407 Fix JavaScript _wrap_getCPtr on 64-bit Windows
error: cast from 'void*' to 'long int' loses precision

Closes #2869
2024-04-12 18:34:02 +01:00
William S Fulton 2f194c10a8 Remove some locator strings in generated code
For Javascript, MzScheme, Python, Ruby.
Removes the vast majority of the /*@SWIG:...*/ locator strings in the
generated wrappers for these 4 languages to help with reproducible builds.

Issue #202
2024-04-12 08:12:44 +01: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 e76952e355 Support NULL being passed into char* in typemaps
Missing functionality for Javascript, MzScheme, Octave.
Tested in follow on commit in cpp11_std_unique_ptr testcase.
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
Olly Betts 3a437b2017 Remove deprecated map and vector specialisation macros
These have done nothing except emit a warning saying they are no
longer required for a long time.

One exception is guile and mzscheme still define non-trivial
specialize_std_map_on_* - it seems they were missed for some
reason, but the same improved machinery which meant these macros
are no longer required for other target languages should apply
to them too.
2023-12-03 09:55:35 +13:00
Olly Betts a8091575c1 Fix typos in comments and docs 2023-11-30 17:07:16 +13:00
Olly Betts 3ce0174a0c Fix random doubled spaces in code 2023-11-17 09:49:36 +13:00
Olly Betts 77d6365255 Remove reintroduced reference to BOTH
I removed BOTH in 779fa45601 - it looks
like this reference is from a change merged after that based on the code
before the removal.
2023-10-25 09:37:56 +13:00
William S Fulton 76fdf2c355 std::map wrappers and non-default constructible
Fix and test Guile, MzScheme, Java, Javascript
Octave is also failing.
2023-09-15 19:17:34 +01: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
Momtchil Momtchev ceed54758a Javascript Node-API support added
Closes #2545
2023-06-28 07:24:51 +01:00
Olly Betts 779fa45601 Remove deprecated BOTH typemap rule
This has been a deprecated alias for INOUT since 2002 (SWIG 1.3.14).
2023-06-15 15:07:04 +12:00
Momtchil Momtchev fd3898367a bring in the `size_t` `cmalloc` in JS
The default `cmalloc` uses an `int` argument
2023-05-31 14:11:13 +02:00
Momtchil Momtchev 721a4808b8 remove hacks and implement default arguments 2023-05-31 13:22:35 +02:00
Olly Betts 0c40ac59f1 Merge branch 'v8-packed-obj' 2023-05-30 17:49:49 +12:00
Momtchil Momtchev b83fc1fda6 remove the check from free 2023-05-29 22:13:43 +02:00
Momtchil Momtchev 05fd4878fe reset the persistent in the weak callback 2023-05-29 22:12:32 +02:00
Momtchil Momtchev de3c2fea95 fix JSC too 2023-05-29 21:02:42 +02:00
Momtchil Momtchev 2ccf3c3050 fix member pointers 2023-05-29 20:17:31 +02:00
Momtchil Momtchev 92dfc5de2e first batch of tests 2023-05-29 12:39:51 +02:00
Olly Betts eaefa489c5 [js] argcargv.i: Allow empty array or empty string 2023-05-22 14:08:13 +12:00
Olly Betts fcdd63dba5 Remove redundant NULL checks before free()
See 7ec2f89fe2
2023-05-20 16:17:00 +12:00
Erez Geva bdc2190cde Add argc and argv multi-argument typemap to Java, Guile, JavaScript, Scilab and C#
Fixes #2552

Signed-off-by: Erez Geva <ErezGeva2@gmail.com>
2023-05-20 16:17:00 +12:00
Olly Betts 1f5ff2e6a5 Allow using snprintf() instead of sprintf() in wrappers
We aim to produce code that works with C90 or C++98 so we can't
assume snprintf() is available, but it almost always is (even
on systems from before it was standardised) so having a way to
use it is helpful.

Enable this automatically if the compiler claims conformance
with at least C90 or C++98 and check SWIG_HAVE_SNPRINTF to allow
turning on manually, but disable if SWIG_NO_SNPRINTF if defined.

The fallback is to call sprintf() without a buffer size check -
checking after the call is really shutting the stable door after
the horse has bolted, and most of our uses either have a fixed maximum
possible size or dynamically allocate a buffer that's large enough.

Fixes: #2502 (sprintf deprecation warnings on macos)
Fixes: #2548
2023-04-27 10:08:57 +12:00
William S Fulton bba072d95d Merge branch 'fix-cast'
* fix-cast:
  add tests for new casting behavior
  skip tests when value is out of range
  refactor integers JS testcase to avoid repeating code
  Return uint64_t as double if is bigger than uint32_t
  Use SWIG_TypeCast in SWIG_V8_ConvertInstancePtr if types don't match (#3)
2022-10-14 19:19:13 +01:00
William S Fulton d32943b2a9 Javascript v8 object to string exceptions improvement 2022-09-19 09:09:29 +01:00
William S Fulton 00190c484f Javascript, Octave, R - Improve exceptions for %catches
and exception specifications for native types.

Now the raised exception contains the string value as the exception
message instead of just the C/C++ type of the exception.

R exceptions were completely swallowed beforehand
2022-09-19 09:09:29 +01: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