Add Javascript support for std::unique_ptr and std::auto_ptr
Equivalent to Ruby/Python implementations.
This commit is contained in:
parent
7556ed0889
commit
fe17296eb4
11 changed files with 358 additions and 6 deletions
|
|
@ -7,6 +7,10 @@ the issue number to the end of the URL: https://github.com/swig/swig/issues/
|
|||
Version 4.1.0 (in progress)
|
||||
===========================
|
||||
|
||||
2022-08-03: wsfulton
|
||||
[Javascript] Add support for std::unique_ptr in std_unique_ptr.i.
|
||||
Add support for std::auto_ptr in std_auto_ptr.i.
|
||||
|
||||
2022-08-02: wsfulton
|
||||
[Octave] Add support for std::unique_ptr in std_unique_ptr.i.
|
||||
Add support for std::auto_ptr in std_auto_ptr.i.
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
%module cpp11_std_unique_ptr
|
||||
|
||||
#if defined(SWIGCSHARP) || defined(SWIGJAVA) || defined(SWIGPYTHON) || defined(SWIGRUBY) || defined(SWIGPERL) || defined(SWIGTCL) || defined(SWIGOCTAVE)
|
||||
#if defined(SWIGCSHARP) || defined(SWIGJAVA) || defined(SWIGPYTHON) || defined(SWIGRUBY) || defined(SWIGPERL) || defined(SWIGTCL) || defined(SWIGOCTAVE) || defined(SWIGJAVASCRIPT)
|
||||
|
||||
%include "std_string.i"
|
||||
%include "std_unique_ptr.i"
|
||||
|
|
|
|||
101
Examples/test-suite/javascript/cpp11_std_unique_ptr_runme.js
Normal file
101
Examples/test-suite/javascript/cpp11_std_unique_ptr_runme.js
Normal file
|
|
@ -0,0 +1,101 @@
|
|||
var cpp11_std_unique_ptr = require("cpp11_std_unique_ptr");
|
||||
|
||||
var checkCount = function(expected_count) {
|
||||
actual_count = cpp11_std_unique_ptr.Klass.getTotal_count();
|
||||
if (actual_count != expected_count)
|
||||
throw new Error("Counts incorrect, expected:" + expected_count + " actual:" + actual_count);
|
||||
}
|
||||
|
||||
// unique_ptr as input
|
||||
{
|
||||
kin = new cpp11_std_unique_ptr.Klass("KlassInput");
|
||||
checkCount(1);
|
||||
s = cpp11_std_unique_ptr.takeKlassUniquePtr(kin);
|
||||
checkCount(0);
|
||||
if (s !== "KlassInput")
|
||||
throw new Error("Incorrect string: " + s);
|
||||
if (!cpp11_std_unique_ptr.is_nullptr(kin))
|
||||
throw new Error("is_nullptr failed");
|
||||
delete kin; // Should not fail, even though already deleted
|
||||
checkCount(0);
|
||||
}
|
||||
|
||||
{
|
||||
kin = new cpp11_std_unique_ptr.Klass("KlassInput");
|
||||
checkCount(1);
|
||||
s = cpp11_std_unique_ptr.takeKlassUniquePtr(kin);
|
||||
checkCount(0);
|
||||
if (s !== "KlassInput")
|
||||
throw new Error("Incorrect string: " + s);
|
||||
if (!cpp11_std_unique_ptr.is_nullptr(kin))
|
||||
throw new Error("is_nullptr failed");
|
||||
exception_thrown = false;
|
||||
try {
|
||||
cpp11_std_unique_ptr.takeKlassUniquePtr(kin);
|
||||
} catch (e) {
|
||||
if (!e.message.includes("cannot release ownership as memory is not owned"))
|
||||
throw new Error("incorrect exception message " + e.message);
|
||||
exception_thrown = true;
|
||||
}
|
||||
if (!exception_thrown)
|
||||
throw new Error("double usage of takeKlassUniquePtr should have been an error");
|
||||
delete kin; // Should not fail, even though already deleted
|
||||
checkCount(0);
|
||||
}
|
||||
|
||||
{
|
||||
kin = new cpp11_std_unique_ptr.Klass("KlassInput");
|
||||
exception_thrown = false;
|
||||
notowned = cpp11_std_unique_ptr.get_not_owned_ptr(kin);
|
||||
try {
|
||||
cpp11_std_unique_ptr.takeKlassUniquePtr(notowned);
|
||||
} catch (e) {
|
||||
if (!e.message.includes("cannot release ownership as memory is not owned"))
|
||||
throw new Error("incorrect exception message " + e.message);
|
||||
exception_thrown = true;
|
||||
}
|
||||
if (!exception_thrown)
|
||||
throw new Error("Should have thrown 'Cannot release ownership as memory is not owned' error");
|
||||
checkCount(1);
|
||||
// delete kin;
|
||||
// Above not deleting the C++ object(node v12) - can't reliably control GC
|
||||
cpp11_std_unique_ptr.takeKlassUniquePtr(kin);
|
||||
checkCount(0);
|
||||
}
|
||||
|
||||
{
|
||||
kini = new cpp11_std_unique_ptr.KlassInheritance("KlassInheritanceInput");
|
||||
checkCount(1);
|
||||
s = cpp11_std_unique_ptr.takeKlassUniquePtr(kini);
|
||||
checkCount(0);
|
||||
if (s !== "KlassInheritanceInput")
|
||||
throw new Error("Incorrect string: " + s);
|
||||
if (!cpp11_std_unique_ptr.is_nullptr(kini))
|
||||
throw new Error("is_nullptr failed");
|
||||
delete kini; // Should not fail, even though already deleted
|
||||
checkCount(0);
|
||||
}
|
||||
|
||||
// unique_ptr as output
|
||||
k1 = cpp11_std_unique_ptr.makeKlassUniquePtr("first");
|
||||
if (k1.getLabel() !== "first")
|
||||
throw new Error("wrong object label");
|
||||
|
||||
k2 = cpp11_std_unique_ptr.makeKlassUniquePtr("second");
|
||||
if (cpp11_std_unique_ptr.Klass.getTotal_count() != 2)
|
||||
throw new Error("number of objects should be 2");
|
||||
|
||||
// delete k1;
|
||||
// Above not deleting the C++ object(node v12), not sure why, use below as workaround
|
||||
cpp11_std_unique_ptr.takeKlassUniquePtr(k1);
|
||||
if (cpp11_std_unique_ptr.Klass.getTotal_count() != 1)
|
||||
throw new Error("number of objects should be 1");
|
||||
|
||||
if (k2.getLabel() !== "second")
|
||||
throw new Error("wrong object label");
|
||||
|
||||
// delete k2;
|
||||
// Above not deleting the C++ object(node v12) - can't reliably control GC
|
||||
cpp11_std_unique_ptr.takeKlassUniquePtr(k2);
|
||||
if (cpp11_std_unique_ptr.Klass.getTotal_count() != 0)
|
||||
throw new Error("no objects should be left");
|
||||
101
Examples/test-suite/javascript/li_std_auto_ptr_runme.js
Normal file
101
Examples/test-suite/javascript/li_std_auto_ptr_runme.js
Normal file
|
|
@ -0,0 +1,101 @@
|
|||
var li_std_auto_ptr = require("li_std_auto_ptr");
|
||||
|
||||
var checkCount = function(expected_count) {
|
||||
actual_count = li_std_auto_ptr.Klass.getTotal_count();
|
||||
if (actual_count != expected_count)
|
||||
throw new Error("Counts incorrect, expected:" + expected_count + " actual:" + actual_count);
|
||||
}
|
||||
|
||||
// auto_ptr as input
|
||||
{
|
||||
kin = new li_std_auto_ptr.Klass("KlassInput");
|
||||
checkCount(1);
|
||||
s = li_std_auto_ptr.takeKlassAutoPtr(kin);
|
||||
checkCount(0);
|
||||
if (s !== "KlassInput")
|
||||
throw new Error("Incorrect string: " + s);
|
||||
if (!li_std_auto_ptr.is_nullptr(kin))
|
||||
throw new Error("is_nullptr failed");
|
||||
delete kin; // Should not fail, even though already deleted
|
||||
checkCount(0);
|
||||
}
|
||||
|
||||
{
|
||||
kin = new li_std_auto_ptr.Klass("KlassInput");
|
||||
checkCount(1);
|
||||
s = li_std_auto_ptr.takeKlassAutoPtr(kin);
|
||||
checkCount(0);
|
||||
if (s !== "KlassInput")
|
||||
throw new Error("Incorrect string: " + s);
|
||||
if (!li_std_auto_ptr.is_nullptr(kin))
|
||||
throw new Error("is_nullptr failed");
|
||||
exception_thrown = false;
|
||||
try {
|
||||
li_std_auto_ptr.takeKlassAutoPtr(kin);
|
||||
} catch (e) {
|
||||
if (!e.message.includes("cannot release ownership as memory is not owned"))
|
||||
throw new Error("incorrect exception message " + e.message);
|
||||
exception_thrown = true;
|
||||
}
|
||||
if (!exception_thrown)
|
||||
throw new Error("double usage of takeKlassAutoPtr should have been an error");
|
||||
delete kin; // Should not fail, even though already deleted
|
||||
checkCount(0);
|
||||
}
|
||||
|
||||
{
|
||||
kin = new li_std_auto_ptr.Klass("KlassInput");
|
||||
exception_thrown = false;
|
||||
notowned = li_std_auto_ptr.get_not_owned_ptr(kin);
|
||||
try {
|
||||
li_std_auto_ptr.takeKlassAutoPtr(notowned);
|
||||
} catch (e) {
|
||||
if (!e.message.includes("cannot release ownership as memory is not owned"))
|
||||
throw new Error("incorrect exception message " + e.message);
|
||||
exception_thrown = true;
|
||||
}
|
||||
if (!exception_thrown)
|
||||
throw new Error("Should have thrown 'Cannot release ownership as memory is not owned' error");
|
||||
checkCount(1);
|
||||
// delete kin;
|
||||
// Above not deleting the C++ object(node v12) - can't reliably control GC
|
||||
li_std_auto_ptr.takeKlassAutoPtr(kin);
|
||||
checkCount(0);
|
||||
}
|
||||
|
||||
{
|
||||
kini = new li_std_auto_ptr.KlassInheritance("KlassInheritanceInput");
|
||||
checkCount(1);
|
||||
s = li_std_auto_ptr.takeKlassAutoPtr(kini);
|
||||
checkCount(0);
|
||||
if (s !== "KlassInheritanceInput")
|
||||
throw new Error("Incorrect string: " + s);
|
||||
if (!li_std_auto_ptr.is_nullptr(kini))
|
||||
throw new Error("is_nullptr failed");
|
||||
delete kini; // Should not fail, even though already deleted
|
||||
checkCount(0);
|
||||
}
|
||||
|
||||
// auto_ptr as output
|
||||
k1 = li_std_auto_ptr.makeKlassAutoPtr("first");
|
||||
if (k1.getLabel() !== "first")
|
||||
throw new Error("wrong object label");
|
||||
|
||||
k2 = li_std_auto_ptr.makeKlassAutoPtr("second");
|
||||
if (li_std_auto_ptr.Klass.getTotal_count() != 2)
|
||||
throw new Error("number of objects should be 2");
|
||||
|
||||
// delete k1;
|
||||
// Above not deleting the C++ object(node v12), not sure why, use below as workaround
|
||||
li_std_auto_ptr.takeKlassAutoPtr(k1);
|
||||
if (li_std_auto_ptr.Klass.getTotal_count() != 1)
|
||||
throw new Error("number of objects should be 1");
|
||||
|
||||
if (k2.getLabel() !== "second")
|
||||
throw new Error("wrong object label");
|
||||
|
||||
// delete k2;
|
||||
// Above not deleting the C++ object(node v12) - can't reliably control GC
|
||||
li_std_auto_ptr.takeKlassAutoPtr(k2);
|
||||
if (li_std_auto_ptr.Klass.getTotal_count() != 0)
|
||||
throw new Error("no objects should be left");
|
||||
|
|
@ -12,7 +12,7 @@
|
|||
#endif
|
||||
%}
|
||||
|
||||
#if defined(SWIGCSHARP) || defined(SWIGJAVA) || defined(SWIGPYTHON) || defined(SWIGRUBY) || defined(SWIGPERL) || defined(SWIGTCL) || defined(SWIGOCTAVE)
|
||||
#if defined(SWIGCSHARP) || defined(SWIGJAVA) || defined(SWIGPYTHON) || defined(SWIGRUBY) || defined(SWIGPERL) || defined(SWIGTCL) || defined(SWIGOCTAVE) || defined(SWIGJAVASCRIPT)
|
||||
|
||||
%include "std_string.i"
|
||||
%include "std_auto_ptr.i"
|
||||
|
|
|
|||
|
|
@ -135,8 +135,15 @@ SWIGRUNTIME int SWIG_JSC_ConvertInstancePtr(JSContextRef context, JSObjectRef ob
|
|||
}
|
||||
}
|
||||
|
||||
if (flags & SWIG_POINTER_DISOWN) {
|
||||
cdata->swigCMemOwn = false;
|
||||
if (((flags & SWIG_POINTER_RELEASE) == SWIG_POINTER_RELEASE) && !cdata->swigCMemOwn) {
|
||||
return SWIG_ERROR_RELEASE_NOT_OWNED;
|
||||
} else {
|
||||
if (flags & SWIG_POINTER_DISOWN) {
|
||||
cdata->swigCMemOwn = false;
|
||||
}
|
||||
if (flags & SWIG_POINTER_CLEAR) {
|
||||
cdata->swigCObject = 0;
|
||||
}
|
||||
}
|
||||
|
||||
return SWIG_OK;
|
||||
|
|
|
|||
33
Lib/javascript/jsc/std_auto_ptr.i
Normal file
33
Lib/javascript/jsc/std_auto_ptr.i
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
/* -----------------------------------------------------------------------------
|
||||
* 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, noblock=1) std::auto_ptr< TYPE > (void *argp = 0, int res = 0) {
|
||||
res = SWIG_ConvertPtr($input, &argp, $descriptor(TYPE *), SWIG_POINTER_RELEASE | %convertptr_flags);
|
||||
if (!SWIG_IsOK(res)) {
|
||||
if (res == SWIG_ERROR_RELEASE_NOT_OWNED) {
|
||||
%releasenotowned_fail(res, "TYPE *", $symname, $argnum);
|
||||
} else {
|
||||
%argument_fail(res, "TYPE *", $symname, $argnum);
|
||||
}
|
||||
}
|
||||
$1.reset((TYPE *)argp);
|
||||
}
|
||||
|
||||
%typemap (out) std::auto_ptr< TYPE > %{
|
||||
%set_output(SWIG_NewPointerObj($1.release(), $descriptor(TYPE *), SWIG_POINTER_OWN | %newpointer_flags));
|
||||
%}
|
||||
|
||||
%template() std::auto_ptr< TYPE >;
|
||||
%enddef
|
||||
|
||||
namespace std {
|
||||
template <class T> class auto_ptr {};
|
||||
}
|
||||
33
Lib/javascript/jsc/std_unique_ptr.i
Normal file
33
Lib/javascript/jsc/std_unique_ptr.i
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
/* -----------------------------------------------------------------------------
|
||||
* 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, noblock=1) std::unique_ptr< TYPE > (void *argp = 0, int res = 0) {
|
||||
res = SWIG_ConvertPtr($input, &argp, $descriptor(TYPE *), SWIG_POINTER_RELEASE | %convertptr_flags);
|
||||
if (!SWIG_IsOK(res)) {
|
||||
if (res == SWIG_ERROR_RELEASE_NOT_OWNED) {
|
||||
%releasenotowned_fail(res, "TYPE *", $symname, $argnum);
|
||||
} else {
|
||||
%argument_fail(res, "TYPE *", $symname, $argnum);
|
||||
}
|
||||
}
|
||||
$1.reset((TYPE *)argp);
|
||||
}
|
||||
|
||||
%typemap (out) std::unique_ptr< TYPE > %{
|
||||
%set_output(SWIG_NewPointerObj($1.release(), $descriptor(TYPE *), SWIG_POINTER_OWN | %newpointer_flags));
|
||||
%}
|
||||
|
||||
%template() std::unique_ptr< TYPE >;
|
||||
%enddef
|
||||
|
||||
namespace std {
|
||||
template <class T> class unique_ptr {};
|
||||
}
|
||||
|
|
@ -194,8 +194,15 @@ SWIGRUNTIME int SWIG_V8_ConvertInstancePtr(SWIGV8_OBJECT objRef, void **ptr, swi
|
|||
*ptr = cdata->swigCObject;
|
||||
}
|
||||
|
||||
if(flags & SWIG_POINTER_DISOWN) {
|
||||
cdata->swigCMemOwn = false;
|
||||
if (((flags & SWIG_POINTER_RELEASE) == SWIG_POINTER_RELEASE) && !cdata->swigCMemOwn) {
|
||||
return SWIG_ERROR_RELEASE_NOT_OWNED;
|
||||
} else {
|
||||
if (flags & SWIG_POINTER_DISOWN) {
|
||||
cdata->swigCMemOwn = false;
|
||||
}
|
||||
if (flags & SWIG_POINTER_CLEAR) {
|
||||
cdata->swigCObject = 0;
|
||||
}
|
||||
}
|
||||
return SWIG_OK;
|
||||
}
|
||||
|
|
|
|||
33
Lib/javascript/v8/std_auto_ptr.i
Normal file
33
Lib/javascript/v8/std_auto_ptr.i
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
/* -----------------------------------------------------------------------------
|
||||
* 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, noblock=1) std::auto_ptr< TYPE > (void *argp = 0, int res = 0) {
|
||||
res = SWIG_ConvertPtr($input, &argp, $descriptor(TYPE *), SWIG_POINTER_RELEASE | %convertptr_flags);
|
||||
if (!SWIG_IsOK(res)) {
|
||||
if (res == SWIG_ERROR_RELEASE_NOT_OWNED) {
|
||||
%releasenotowned_fail(res, "TYPE *", $symname, $argnum);
|
||||
} else {
|
||||
%argument_fail(res, "TYPE *", $symname, $argnum);
|
||||
}
|
||||
}
|
||||
$1.reset((TYPE *)argp);
|
||||
}
|
||||
|
||||
%typemap (out) std::auto_ptr< TYPE > %{
|
||||
%set_output(SWIG_NewPointerObj($1.release(), $descriptor(TYPE *), SWIG_POINTER_OWN | %newpointer_flags));
|
||||
%}
|
||||
|
||||
%template() std::auto_ptr< TYPE >;
|
||||
%enddef
|
||||
|
||||
namespace std {
|
||||
template <class T> class auto_ptr {};
|
||||
}
|
||||
33
Lib/javascript/v8/std_unique_ptr.i
Normal file
33
Lib/javascript/v8/std_unique_ptr.i
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
/* -----------------------------------------------------------------------------
|
||||
* 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, noblock=1) std::unique_ptr< TYPE > (void *argp = 0, int res = 0) {
|
||||
res = SWIG_ConvertPtr($input, &argp, $descriptor(TYPE *), SWIG_POINTER_RELEASE | %convertptr_flags);
|
||||
if (!SWIG_IsOK(res)) {
|
||||
if (res == SWIG_ERROR_RELEASE_NOT_OWNED) {
|
||||
%releasenotowned_fail(res, "TYPE *", $symname, $argnum);
|
||||
} else {
|
||||
%argument_fail(res, "TYPE *", $symname, $argnum);
|
||||
}
|
||||
}
|
||||
$1.reset((TYPE *)argp);
|
||||
}
|
||||
|
||||
%typemap (out) std::unique_ptr< TYPE > %{
|
||||
%set_output(SWIG_NewPointerObj($1.release(), $descriptor(TYPE *), SWIG_POINTER_OWN | %newpointer_flags));
|
||||
%}
|
||||
|
||||
%template() std::unique_ptr< TYPE >;
|
||||
%enddef
|
||||
|
||||
namespace std {
|
||||
template <class T> class unique_ptr {};
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue