aboutsummaryrefslogtreecommitdiff
path: root/Lib/lua
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/lua')
-rw-r--r--Lib/lua/argcargv.i57
-rw-r--r--Lib/lua/lua.swg11
-rw-r--r--Lib/lua/lua_fnptr.i4
-rw-r--r--Lib/lua/luakw.swg2
-rw-r--r--Lib/lua/luarun.swg71
-rw-r--r--Lib/lua/luatypemaps.swg42
-rw-r--r--Lib/lua/std_auto_ptr.i39
-rw-r--r--Lib/lua/std_map.i4
-rw-r--r--Lib/lua/std_string.i19
-rw-r--r--Lib/lua/std_string_view.i50
-rw-r--r--Lib/lua/std_unique_ptr.i39
-rw-r--r--Lib/lua/swigmove.i18
-rw-r--r--Lib/lua/typemaps.i14
13 files changed, 308 insertions, 62 deletions
diff --git a/Lib/lua/argcargv.i b/Lib/lua/argcargv.i
new file mode 100644
index 000000000..605a24fd7
--- /dev/null
+++ b/Lib/lua/argcargv.i
@@ -0,0 +1,57 @@
+/* -------------------------------------------------------------
+ * SWIG library containing argc and argv multi-argument typemaps
+
+ Use it as follows:
+
+ %apply (int ARGC, char **ARGV) { (size_t argc, const char **argv) }
+ extern int mainApp(size_t argc, const char **argv);
+
+ then from lua:
+
+ args = { "arg0", "arg1" }
+ mainApp(args)
+
+ * ------------------------------------------------------------- */
+
+%{
+SWIGINTERN int SWIG_argv_size(lua_State* L, int index) {
+ int n=0;
+ while(1){
+ lua_rawgeti(L,index,n+1);
+ if (lua_isnil(L,-1))
+ break;
+ ++n;
+ lua_pop(L,1);
+ }
+ lua_pop(L,1);
+ return n;
+}
+%}
+
+%typemap(in) (int ARGC, char **ARGV) {
+ if (lua_istable(L,$input)) {
+ int i, size = SWIG_argv_size(L,$input);
+ $1 = ($1_ltype) size;
+ $2 = (char **) malloc((size+1)*sizeof(char *));
+ for (i = 0; i < size; i++) {
+ lua_rawgeti(L,$input,i+1);
+ if (lua_isnil(L,-1))
+ break;
+ $2[i] = (char *)lua_tostring(L, -1);
+ lua_pop(L,1);
+ }
+ $2[i]=NULL;
+ } else {
+ $1 = 0; $2 = 0;
+ lua_pushstring(L,"Expecting argv array");
+ lua_error(L);
+ }
+}
+
+%typemap(typecheck, precedence=SWIG_TYPECHECK_STRING_ARRAY) (int ARGC, char **ARGV) {
+ $1 = lua_istable(L,$input);
+}
+
+%typemap(freearg) (int ARGC, char **ARGV) {
+ free((char *) $2);
+}
diff --git a/Lib/lua/lua.swg b/Lib/lua/lua.swg
index 6db3add6e..4244435a5 100644
--- a/Lib/lua/lua.swg
+++ b/Lib/lua/lua.swg
@@ -18,8 +18,9 @@
* constants typemaps
* ----------------------------------------------------------------------------- */
// this basically adds to a table of constants
+/* Extra `(`...`)` here are to handle $value being e.g. `SizeOf< int,int >::size`. */
%typemap(consttab) int, unsigned int, short, unsigned short, long, unsigned long, unsigned char, signed char, bool, enum SWIGTYPE
- {SWIG_LUA_CONSTTAB_INT("$symname", $value)}
+ {SWIG_LUA_CONSTTAB_INT("$symname", ($value))}
%typemap(consttab) float, double
{SWIG_LUA_CONSTTAB_FLOAT("$symname", $value)}
@@ -151,7 +152,7 @@ The python/lua ones are great, but C++ ones I don't like
(mainly because I cannot get the stack trace out of it)
Therefore I have not bothered to try doing much in this
-Therefore currently its just enough to get a few test cases running ok
+Therefore currently it's just enough to get a few test cases running ok
note: if you wish to throw anything related to std::exception
use %include <std_except.i> instead
@@ -191,7 +192,7 @@ use %include <std_except.i> instead
Throwing object is a serious problem:
Assuming some code throws a 'FooBar'
There are a few options:
-- return a pointer to it: but its unclear how long this will last for.
+- return a pointer to it: but it's unclear how long this will last for.
- return a copy of it: but not all objects are copyable
(see exception_partial_info in the test suite for a case where you cannot do this)
- convert to a string & throw that
@@ -213,7 +214,7 @@ SWIG_fail;%}
// %apply SWIGTYPE EXCEPTION_BY_VAL {FooBar};
// %apply SWIGTYPE& EXCEPTION_BY_VAL {FooBar&}; // note: need & twice
%typemap(throws) SWIGTYPE EXCEPTION_BY_VAL
-%{SWIG_NewPointerObj(L,(void *)new $1_ltype(($1_ltype &) $1),$&1_descriptor,1);
+%{SWIG_NewPointerObj(L,(void *)new $1_ltype($1),$&1_descriptor,1);
SWIG_fail;%}
// similar for object reference
@@ -224,7 +225,7 @@ SWIG_fail;%}
// note: no support for object pointers
-// its not clear how long the pointer is valid for, therefore not supporting it
+// it's not clear how long the pointer is valid for, therefore not supporting it
/* -----------------------------------------------------------------------------
* extras
diff --git a/Lib/lua/lua_fnptr.i b/Lib/lua/lua_fnptr.i
index 481cfafa6..b4c663c53 100644
--- a/Lib/lua/lua_fnptr.i
+++ b/Lib/lua/lua_fnptr.i
@@ -17,7 +17,7 @@ note: it should be passed by value, not byref or as a pointer.
The SWIGLUA_FN holds a pointer to the lua_State, and the stack index where the function is held.
The macro SWIGLUA_FN_GET() will put a copy of the lua function at the top of the stack.
-After that its fairly simple to write the rest of the code (assuming know how to use lua),
+After that it's fairly simple to write the rest of the code (assuming know how to use lua),
just push the parameters, call the function and return the result.
int my_func(int a, int b, SWIGLUA_FN fn)
@@ -119,6 +119,6 @@ void swiglua_ref_get(SWIGLUA_REF* pref){
%{ swiglua_ref_set(&$1,L,$input); %}
%typemap(out) SWIGLUA_REF
-%{ if ($1.L!=0) {swiglua_ref_get(&$1);} else {lua_pushnil(L);}
+%{ if ($1.L!=0) {swiglua_ref_get(&$1);} else {lua_pushnil(L);}
SWIG_arg++; %}
diff --git a/Lib/lua/luakw.swg b/Lib/lua/luakw.swg
index fc2f92bfe..394e40053 100644
--- a/Lib/lua/luakw.swg
+++ b/Lib/lua/luakw.swg
@@ -2,7 +2,7 @@
Warnings for Lua keywords, built-in names and bad names.
*/
-#define LUAKW(x) %keywordwarn("'" `x` "' is a Lua keyword, renaming to 'c_" `x` "'", rename="c_%s") `x`
+#define LUAKW(x) %keywordwarn("'" `x` "' is a Lua keyword", rename="c_%s") `x`
#define LUABN(x) %namewarn(%warningmsg(SWIGWARN_PARSE_BUILTIN_NAME, "'" `x` "' conflicts with a basic function in Lua"), %$not %$ismember) `x`
/*
diff --git a/Lib/lua/luarun.swg b/Lib/lua/luarun.swg
index bd764d668..af6cd66f3 100644
--- a/Lib/lua/luarun.swg
+++ b/Lib/lua/luarun.swg
@@ -149,6 +149,20 @@ typedef struct swig_elua_entry {
# define lua_rawlen lua_objlen
#endif
+/* lua_tolstring() was added in Lua 5.1. It should be a little more
+ efficient than making two separate calls and it avoids problems with order
+ of evaluation so SWIG calls lua_tolstring() when it wants the length and
+ we provide a compatibility implementation for Lua 5.0. */
+#if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM < 501
+static const char *(lua_tolstring)(lua_State *L, int idx, size_t *len) {
+ /* Call lua_tostring() first as it may convert the value from number to
+ string. */
+ const char *result = lua_tostring(L, idx);
+ if (len) *len = lua_strlen(L, idx);
+ return result;
+}
+#endif
+
/* lua_pushglobaltable is the recommended "future-proof" way to get
the global table for Lua 5.2 and later. Here we define
@@ -307,7 +321,7 @@ typedef struct {
/* Contract support */
#define SWIG_contract_assert(expr, msg) \
- if (!(expr)) { SWIG_Lua_pusherrstring(L, (char *) msg); goto fail; } else
+ do { if (!(expr)) { SWIG_Lua_pusherrstring(L, (char *) msg); goto fail; } } while (0)
/* helper #defines */
@@ -819,6 +833,7 @@ SWIGINTERN int SWIG_Lua_class_do_get_item(lua_State *L, swig_type_info *type, i
int bases_search_result;
int substack_start = lua_gettop(L)-2;
assert(first_arg == substack_start+1);
+ (void)first_arg;
lua_checkstack(L,5);
assert(lua_isuserdata(L,-2)); /* just in case */
lua_getmetatable(L,-2); /* get the meta table */
@@ -826,7 +841,7 @@ SWIGINTERN int SWIG_Lua_class_do_get_item(lua_State *L, swig_type_info *type, i
/* NEW: looks for the __getitem() fn
this is a user provided get fn */
SWIG_Lua_get_table(L,"__getitem"); /* find the __getitem fn */
- if (lua_iscfunction(L,-1)) /* if its there */
+ if (lua_iscfunction(L,-1)) /* if it's there */
{ /* found it so call the fn & return its value */
lua_pushvalue(L,substack_start+1); /* the userdata */
lua_pushvalue(L,substack_start+2); /* the parameter */
@@ -857,6 +872,7 @@ SWIGINTERN int SWIG_Lua_class_do_get(lua_State *L, swig_type_info *type, int SW
int bases_search_result;
int substack_start = lua_gettop(L)-2;
assert(first_arg == substack_start+1);
+ (void)first_arg;
lua_checkstack(L,5);
assert(lua_isuserdata(L,-2)); /* just in case */
lua_getmetatable(L,-2); /* get the meta table */
@@ -883,7 +899,7 @@ SWIGINTERN int SWIG_Lua_class_do_get(lua_State *L, swig_type_info *type, int SW
lua_pushvalue(L,substack_start+2); /* key */
lua_rawget(L,-2); /* look for the fn */
lua_remove(L,-2); /* stack tidy, remove .fn table */
- if (lua_isfunction(L,-1)) /* note: if its a C function or lua function */
+ if (lua_isfunction(L,-1)) /* note: if it's a C function or lua function */
{ /* found it so return the fn & let lua call it */
lua_remove(L,-2); /* stack tidy, remove metatable */
if(ret)
@@ -966,7 +982,7 @@ SWIGINTERN int SWIG_Lua_class_do_set(lua_State *L, swig_type_info *type, int fi
/* NEW: looks for the __setitem() fn
this is a user provided set fn */
SWIG_Lua_get_table(L,"__setitem"); /* find the fn */
- if (lua_iscfunction(L,-1)) /* if its there */
+ if (lua_iscfunction(L,-1)) /* if it's there */
{ /* found it so call the fn & return its value */
lua_pushvalue(L,substack_start+1); /* the userdata */
lua_pushvalue(L,substack_start+2); /* the parameter */
@@ -1343,15 +1359,15 @@ SWIGINTERN void SWIG_Lua_add_class_instance_details(lua_State *L, swig_lua_clas
SWIGRUNTIME int SWIG_Lua_resolve_metamethod(lua_State *L); /*forward declaration*/
/* The real function that resolves a metamethod.
- * Function searches given class and all it's bases(recursively) for first instance of something that is
- * not equal to SWIG_Lua_resolve_metatmethod. (Almost always this 'something' is actual metamethod implementation
+ * Function searches given class and all its bases (recursively) for first instance of something that is
+ * not equal to SWIG_Lua_resolve_metamethod. (Almost always this 'something' is actual metamethod implementation
* and it is a SWIG-generated C function.). It returns value on the top of the L and there is no garbage below the
* answer.
* Returns 1 if found, 0 otherwise.
* clss is class which metatable we will search for method
* metamethod_name_idx is index in L where metamethod name (as string) lies
- * skip_check allows to skip searching metamethod in givel clss and immideatelly go to searching in bases. skip_check
- * is not caried to subsequent recursive calls - false is always passed. It is set to true only at first call from
+ * skip_check allows skipping searching metamethod in the given class and immediately going to searching in bases. skip_check
+ * is not carried to subsequent recursive calls - false is always passed. It is set to true only at first call from
* SWIG_Lua_resolve_metamethod
* */
SWIGINTERN int SWIG_Lua_do_resolve_metamethod(lua_State *L, const swig_lua_class *clss, int metamethod_name_idx,
@@ -1497,7 +1513,7 @@ SWIGINTERN void SWIG_Lua_add_class_user_metamethods(lua_State *L, swig_lua_class
}
}
- lua_pop(L,1); /* remove inheritable metatmethods table */
+ lua_pop(L,1); /* remove inheritable metamethods table */
/* Special handling for __tostring method */
lua_pushstring(L, "__tostring");
@@ -1757,6 +1773,7 @@ SWIGRUNTIME void SWIG_Lua_NewPointerObj(lua_State *L,void *ptr,swig_type_info *t
(if possible) */
SWIGRUNTIME int SWIG_Lua_ConvertPtr(lua_State *L,int index,void **ptr,swig_type_info *type,int flags)
{
+ int ret = SWIG_ERROR;
swig_lua_userdata *usr;
swig_cast_info *cast;
/* special case: lua nil => NULL pointer */
@@ -1765,33 +1782,49 @@ SWIGRUNTIME int SWIG_Lua_ConvertPtr(lua_State *L,int index,void **ptr,swig_type
*ptr=0;
return (flags & SWIG_POINTER_NO_NULL) ? SWIG_NullReferenceError : SWIG_OK;
}
+ if (lua_islightuserdata(L,index))
+ {
+ *ptr=lua_touserdata(L,index);
+ return (flags & SWIG_POINTER_NO_NULL) ? SWIG_NullReferenceError : SWIG_OK;
+ }
usr=(swig_lua_userdata*)lua_touserdata(L,index); /* get data */
if (usr)
{
+ if (((flags & SWIG_POINTER_RELEASE) == SWIG_POINTER_RELEASE) && !usr->own)
+ {
+ return SWIG_ERROR_RELEASE_NOT_OWNED;
+ }
if (flags & SWIG_POINTER_DISOWN) /* must disown the object */
{
- usr->own=0;
+ usr->own = 0;
}
if (!type) /* special cast void*, no casting fn */
{
*ptr=usr->ptr;
- return SWIG_OK; /* ok */
+ ret = SWIG_OK;
+ }
+ else
+ {
+ cast=SWIG_TypeCheck(usr->type->name,type); /* performs normal type checking */
+ if (cast)
+ {
+ int newmemory = 0;
+ *ptr=SWIG_TypeCast(cast,usr->ptr,&newmemory);
+ assert(!newmemory); /* newmemory handling not yet implemented */
+ ret = SWIG_OK;
+ }
}
- cast=SWIG_TypeCheckStruct(usr->type,type); /* performs normal type checking */
- if (cast)
+ if ((ret == SWIG_OK) && (flags & SWIG_POINTER_CLEAR))
{
- int newmemory = 0;
- *ptr=SWIG_TypeCast(cast,usr->ptr,&newmemory);
- assert(!newmemory); /* newmemory handling not yet implemented */
- return SWIG_OK; /* ok */
+ usr->ptr = 0;
}
}
- return SWIG_ERROR; /* error */
+ return ret;
}
SWIGRUNTIME void* SWIG_Lua_MustGetPtr(lua_State *L,int index,swig_type_info *type,int flags,
int argnum,const char *func_name){
- void *result;
+ void *result = 0;
if (!SWIG_IsOK(SWIG_ConvertPtr(L,index,&result,type,flags))){
luaL_error (L,"Error in %s, expected a %s at argument number %d\n",
func_name,(type && type->str)?type->str:"void*",argnum);
diff --git a/Lib/lua/luatypemaps.swg b/Lib/lua/luatypemaps.swg
index 8959f201e..7d23917ee 100644
--- a/Lib/lua/luatypemaps.swg
+++ b/Lib/lua/luatypemaps.swg
@@ -24,7 +24,7 @@
// additional check for unsigned numbers, to not permit negative input
%typemap(in,checkfn="lua_isnumber") unsigned int,
unsigned short, unsigned long, unsigned char
-%{SWIG_contract_assert((lua_tonumber(L,$input)>=0),"number must not be negative")
+%{SWIG_contract_assert((lua_tonumber(L,$input)>=0),"number must not be negative");
$1 = ($type)lua_tonumber(L, $input);%}
%typemap(out) int,short,long,
@@ -39,12 +39,12 @@ $1 = ($type)lua_tonumber(L, $input);%}
// SWIG assumes that this code will need a pointer to int to be passed in
// (this might be ok for objects by const ref, but not for numeric primitives)
// therefore we add a set of typemaps to fix this (for both in & out)
-%typemap(in,checkfn="lua_isnumber") const int&($basetype temp)
-%{ temp=($basetype)lua_tonumber(L,$input); $1=&temp;%}
+%typemap(in,checkfn="lua_isnumber") const int&($*1_ltype temp)
+%{ temp=($*1_ltype)lua_tonumber(L,$input); $1=&temp;%}
-%typemap(in,checkfn="lua_isnumber") const unsigned int&($basetype temp)
-%{SWIG_contract_assert((lua_tonumber(L,$input)>=0),"number must not be negative")
-temp=($basetype)lua_tonumber(L,$input); $1=&temp;%}
+%typemap(in,checkfn="lua_isnumber") const unsigned int&($*1_ltype temp)
+%{SWIG_contract_assert((lua_tonumber(L,$input)>=0),"number must not be negative");
+temp=($*1_ltype)lua_tonumber(L,$input); $1=&temp;%}
%typemap(out) const int&, const unsigned int&
%{ lua_pushnumber(L, (lua_Number) *$1); SWIG_arg++;%}
@@ -151,11 +151,17 @@ SWIGINTERN int SWIG_lua_isnilstring(lua_State *L, int idx) {
}
%}
-%typemap(in,checkfn="lua_isuserdata") SWIGTYPE&&
-%{
- if (!SWIG_IsOK(SWIG_ConvertPtr(L,$input,(void**)&$1,$descriptor,$disown))){
- SWIG_fail_ptr("$symname",$argnum,$descriptor);
+%typemap(in,checkfn="lua_isuserdata",fragment="<memory>") SWIGTYPE&& (void *argp = 0, int res = 0, std::unique_ptr<$*1_ltype> rvrdeleter) %{
+ res = SWIG_ConvertPtr(L, $input, &argp, $descriptor, SWIG_POINTER_RELEASE);
+ if (!SWIG_IsOK(res)) {
+ if (res == SWIG_ERROR_RELEASE_NOT_OWNED) {
+ lua_pushfstring(L, "Cannot release ownership as memory is not owned for argument $argnum of type '$1_type' in $symname"); SWIG_fail;
+ } else {
+ SWIG_fail_ptr("$symname", $argnum, $descriptor);
+ }
}
+ $1 = ($1_ltype)argp;
+ rvrdeleter.reset($1);
%}
// out is simple
@@ -217,7 +223,7 @@ $1=($1_ltype)&temp;%}
#ifdef __cplusplus
%typemap(out) SWIGTYPE
{
- $&1_ltype resultptr = new $1_ltype((const $1_ltype &) $1);
+ $&1_ltype resultptr = new $1_ltype($1);
SWIG_NewPointerObj(L,(void *) resultptr,$&1_descriptor,1); SWIG_arg++;
}
#else
@@ -237,22 +243,22 @@ $1=($1_ltype)&temp;%}
// therefore a special wrapping functions SWIG_ConvertMember() & SWIG_NewMemberObj() were written
%typemap(in,checkfn="lua_isuserdata") SWIGTYPE (CLASS::*)
%{
- if (!SWIG_IsOK(SWIG_ConvertMember(L,$input,(void*)(&$1),sizeof($type),$descriptor)))
+ if (!SWIG_IsOK(SWIG_ConvertMember(L,$input,(void*)(&$1),sizeof($1),$descriptor)))
SWIG_fail_ptr("$symname",$argnum,$descriptor);
%}
%typemap(out) SWIGTYPE (CLASS::*)
-%{
- SWIG_NewMemberObj(L,(void*)(&$1),sizeof($type),$descriptor); SWIG_arg++;
+%{
+ SWIG_NewMemberObj(L,(void*)(&$1),sizeof($1),$descriptor); SWIG_arg++;
%}
// void (must be empty without the SWIG_arg++)
-%typemap(out) void "";
+%typemap(out) void ""
/* void* is a special case
A function void fn(void*) should take any kind of pointer as a parameter (just like C/C++ does)
-but if its an output, then it should be wrapped like any other SWIG object (using default typemap)
+but if it's an output, then it should be wrapped like any other SWIG object (using default typemap)
*/
%typemap(in,checkfn="SWIG_isptrtype") void*
%{$1=($1_ltype)SWIG_MustGetPtr(L,$input,0,0,$argnum,"$symname");%}
@@ -285,7 +291,7 @@ parameters match which function
// unfortunately lua only considers one type of number
// so all numbers (int,float,double) match
-// you could add an advanced fn to get type & check if its integral
+// you could add an advanced fn to get type & check if it's integral
%typecheck(SWIG_TYPECHECK_INTEGER)
int, short, long,
unsigned int, unsigned short, unsigned long,
@@ -396,7 +402,7 @@ parameters match which function
* Specials
* ----------------------------------------------------------------------------- */
// swig::LANGUAGE_OBJ was added to allow containers of native objects
-// however its rather difficult to do this in lua, as you cannot hold pointers
+// however it's rather difficult to do this in lua, as you cannot hold pointers
// to native objects (they are held in the interpreter)
// therefore for now: just ignoring this feature
#ifdef __cplusplus
diff --git a/Lib/lua/std_auto_ptr.i b/Lib/lua/std_auto_ptr.i
new file mode 100644
index 000000000..b3b71d0f2
--- /dev/null
+++ b/Lib/lua/std_auto_ptr.i
@@ -0,0 +1,39 @@
+/* -----------------------------------------------------------------------------
+ * std_auto_ptr.i
+ *
+ * SWIG library file for handling std::auto_ptr.
+ * Memory ownership is passed from the std::auto_ptr C++ layer to the proxy
+ * class when returning a std::auto_ptr from a function.
+ * Memory ownership is passed from the proxy class to the std::auto_ptr in the
+ * C++ layer when passed as a parameter to a wrapped function.
+ * ----------------------------------------------------------------------------- */
+
+%define %auto_ptr(TYPE)
+%typemap(in, checkfn="SWIG_isptrtype", noblock=1) std::auto_ptr< TYPE > (void *argp = 0, int res = 0) {
+ res = SWIG_ConvertPtr(L, $input, &argp, $descriptor(TYPE *), SWIG_POINTER_RELEASE);
+ if (!SWIG_IsOK(res)) {
+ if (res == SWIG_ERROR_RELEASE_NOT_OWNED) {
+ lua_pushfstring(L, "Cannot release ownership as memory is not owned for argument $argnum of type 'TYPE *' in $symname"); SWIG_fail;
+ } else {
+ SWIG_fail_ptr("$symname", $argnum, $descriptor(TYPE *));
+ }
+ }
+ $1.reset((TYPE *)argp);
+}
+
+%typemap (out) std::auto_ptr< TYPE > %{
+ SWIG_NewPointerObj(L, $1.release(), $descriptor(TYPE *), SWIG_POINTER_OWN); SWIG_arg++;
+%}
+
+%typemap(typecheck, precedence=SWIG_TYPECHECK_POINTER, equivalent="TYPE *", noblock=1) std::auto_ptr< TYPE > {
+ void *vptr = 0;
+ int res = SWIG_ConvertPtr(L, $input, &vptr, $descriptor(TYPE *), 0);
+ $1 = SWIG_CheckState(res);
+}
+
+%template() std::auto_ptr< TYPE >;
+%enddef
+
+namespace std {
+ template <class T> class auto_ptr {};
+}
diff --git a/Lib/lua/std_map.i b/Lib/lua/std_map.i
index 773b6d0c3..19281ad7d 100644
--- a/Lib/lua/std_map.i
+++ b/Lib/lua/std_map.i
@@ -48,7 +48,11 @@ namespace std {
throw std::out_of_range("key not found");
}
void set(const K& key, const T& x) {
+%#ifdef __cpp_lib_map_try_emplace
+ (*self).insert_or_assign(key, x);
+%#else
(*self)[key] = x;
+%#endif
}
void del(const K& key) throw (std::out_of_range) {
std::map< K, T, C >::iterator i = self->find(key);
diff --git a/Lib/lua/std_string.i b/Lib/lua/std_string.i
index b95a8a4a2..795db71f2 100644
--- a/Lib/lua/std_string.i
+++ b/Lib/lua/std_string.i
@@ -43,19 +43,28 @@ but
Similarly for getting the string
$1 = (char*)lua_tostring(L, $input);
becomes
- $1.assign(lua_tostring(L,$input),lua_rawlen(L,$input));
-
-Not using: lua_tolstring() as this is only found in Lua 5.1 & not 5.0.2
+ size_t len;
+ const char *ptr = lua_tolstring(L, $input, &len);
+ $1.assign(ptr, len);
*/
%typemap(in,checkfn="lua_isstring") string
-%{$1.assign(lua_tostring(L,$input),lua_rawlen(L,$input));%}
+{
+ size_t len;
+ const char *ptr = lua_tolstring(L, $input, &len);
+ $1.assign(ptr, len);
+}
%typemap(out) string
%{ lua_pushlstring(L,$1.data(),$1.size()); SWIG_arg++;%}
%typemap(in,checkfn="lua_isstring") const string& ($*1_ltype temp)
-%{temp.assign(lua_tostring(L,$input),lua_rawlen(L,$input)); $1=&temp;%}
+{
+ size_t len;
+ const char *ptr = lua_tolstring(L, $input, &len);
+ temp.assign(ptr, len);
+ $1=&temp;
+}
%typemap(out) const string&
%{ lua_pushlstring(L,$1->data(),$1->size()); SWIG_arg++;%}
diff --git a/Lib/lua/std_string_view.i b/Lib/lua/std_string_view.i
new file mode 100644
index 000000000..5c8b6367b
--- /dev/null
+++ b/Lib/lua/std_string_view.i
@@ -0,0 +1,50 @@
+/* -----------------------------------------------------------------------------
+ * std_string_view.i
+ *
+ * std::string_view typemaps for LUA
+ * ----------------------------------------------------------------------------- */
+
+%{
+#include <string_view>
+%}
+
+namespace std {
+
+%naturalvar string_view;
+
+%typemap(in,checkfn="lua_isstring") string_view
+{
+ size_t len;
+ const char *ptr = lua_tolstring(L, $input, &len);
+ $1 = std::string_view(ptr, len);
+}
+
+%typemap(out) string_view
+%{ lua_pushlstring(L,$1.data(),$1.size()); SWIG_arg++;%}
+
+%typemap(in,checkfn="lua_isstring") const string_view& ($*1_ltype temp)
+{
+ size_t len;
+ const char *ptr = lua_tolstring(L, $input, &len);
+ temp = std::string_view(ptr, len);
+ $1=&temp;
+}
+
+%typemap(out) const string_view&
+%{ lua_pushlstring(L,$1->data(),$1->size()); SWIG_arg++;%}
+
+// for throwing of any kind of string_view, string_view ref's and string_view pointers
+// we convert all to lua strings
+%typemap(throws) string_view, string_view&, const string_view&
+%{ lua_pushlstring(L,$1.data(),$1.size()); SWIG_fail;%}
+
+%typemap(throws) string_view*, const string_view*
+%{ lua_pushlstring(L,$1->data(),$1->size()); SWIG_fail;%}
+
+%typecheck(SWIG_TYPECHECK_STRINGVIEW) string_view, const string_view& {
+ $1 = lua_isstring(L,$input);
+}
+
+class string_view;
+
+}
diff --git a/Lib/lua/std_unique_ptr.i b/Lib/lua/std_unique_ptr.i
new file mode 100644
index 000000000..ad08f3b0e
--- /dev/null
+++ b/Lib/lua/std_unique_ptr.i
@@ -0,0 +1,39 @@
+/* -----------------------------------------------------------------------------
+ * std_unique_ptr.i
+ *
+ * SWIG library file for handling std::unique_ptr.
+ * Memory ownership is passed from the std::unique_ptr C++ layer to the proxy
+ * class when returning a std::unique_ptr from a function.
+ * Memory ownership is passed from the proxy class to the std::unique_ptr in the
+ * C++ layer when passed as a parameter to a wrapped function.
+ * ----------------------------------------------------------------------------- */
+
+%define %unique_ptr(TYPE)
+%typemap(in, checkfn="SWIG_isptrtype", noblock=1) std::unique_ptr< TYPE > (void *argp = 0, int res = 0) {
+ res = SWIG_ConvertPtr(L, $input, &argp, $descriptor(TYPE *), SWIG_POINTER_RELEASE);
+ if (!SWIG_IsOK(res)) {
+ if (res == SWIG_ERROR_RELEASE_NOT_OWNED) {
+ lua_pushfstring(L, "Cannot release ownership as memory is not owned for argument $argnum of type 'TYPE *' in $symname"); SWIG_fail;
+ } else {
+ SWIG_fail_ptr("$symname", $argnum, $descriptor(TYPE *));
+ }
+ }
+ $1.reset((TYPE *)argp);
+}
+
+%typemap (out) std::unique_ptr< TYPE > %{
+ SWIG_NewPointerObj(L, $1.release(), $descriptor(TYPE *), SWIG_POINTER_OWN); SWIG_arg++;
+%}
+
+%typemap(typecheck, precedence=SWIG_TYPECHECK_POINTER, equivalent="TYPE *", noblock=1) std::unique_ptr< TYPE > {
+ void *vptr = 0;
+ int res = SWIG_ConvertPtr(L, $input, &vptr, $descriptor(TYPE *), 0);
+ $1 = SWIG_CheckState(res);
+}
+
+%template() std::unique_ptr< TYPE >;
+%enddef
+
+namespace std {
+ template <class T> class unique_ptr {};
+}
diff --git a/Lib/lua/swigmove.i b/Lib/lua/swigmove.i
new file mode 100644
index 000000000..d130e797a
--- /dev/null
+++ b/Lib/lua/swigmove.i
@@ -0,0 +1,18 @@
+/* -----------------------------------------------------------------------------
+ * swigmove.i
+ *
+ * Input typemaps library for implementing full move semantics when passing
+ * parameters by value.
+ * ----------------------------------------------------------------------------- */
+
+%typemap(in, checkfn="lua_isuserdata", noblock=1) SWIGTYPE MOVE (void *argp = 0, int res = 0) {
+ res = SWIG_ConvertPtr(L, $input, &argp, $&1_descriptor, SWIG_POINTER_RELEASE);
+ if (!SWIG_IsOK(res)) {
+ if (res == SWIG_ERROR_RELEASE_NOT_OWNED) {
+ lua_pushfstring(L, "Cannot release ownership as memory is not owned for argument $argnum of type '$1_type' in $symname"); SWIG_fail;
+ } else {
+ SWIG_fail_ptr("$symname", $argnum, $&1_descriptor);
+ }
+ }
+ SwigValueWrapper< $1_ltype >::reset($1, ($&1_type)argp);
+}
diff --git a/Lib/lua/typemaps.i b/Lib/lua/typemaps.i
index 8392e5bfa..68f6f6cca 100644
--- a/Lib/lua/typemaps.i
+++ b/Lib/lua/typemaps.i
@@ -28,7 +28,7 @@ The basic code looks like this:
However the code below is a mixture of #defines & such, so nowhere as easy to read
-To make you code work correctly its not just a matter of %including this file
+To make you code work correctly it's not just a matter of %including this file
You also have to give SWIG the hints on which to use where
eg
@@ -77,7 +77,7 @@ SWIG_NUMBER_TYPEMAP(enum SWIGTYPE);
// also for long longs's
SWIG_NUMBER_TYPEMAP(long long); SWIG_NUMBER_TYPEMAP(unsigned long long); SWIG_NUMBER_TYPEMAP(signed long long);
-// note we dont do char, as a char* is probably a string not a ptr to a single char
+// note we don't do char, as a char* is probably a string not a ptr to a single char
// similar for booleans
%typemap(in,checkfn="lua_isboolean") bool *INPUT(bool temp), bool &INPUT(bool temp)
@@ -188,16 +188,6 @@ int SWIG_read_NAME_num_array(lua_State* L,int index,TYPE *array,int size);
*/
-/* Reported that you don't need to check for NULL for delete & free
-There probably is some compiler that its not true for, so the code is left here just in case.
-#ifdef __cplusplus
-#define SWIG_ALLOC_ARRAY(TYPE,LEN) new TYPE[LEN]
-#define SWIG_FREE_ARRAY(PTR) if(PTR){delete[] PTR;}
-#else
-#define SWIG_ALLOC_ARRAY(TYPE,LEN) (TYPE *)malloc(LEN*sizeof(TYPE))
-#define SWIG_FREE_ARRAY(PTR) if(PTR){free(PTR);}
-#endif
-*/
%{
#ifdef __cplusplus /* generic alloc/dealloc fns*/
#define SWIG_ALLOC_ARRAY(TYPE,LEN) new TYPE[LEN]