Skip to content

Commit

Permalink
Convert C++ std::shared_ptr(NULL) to JS null (#85)
Browse files Browse the repository at this point in the history
* convert C++ std::shared_ptr(NULL) to JS null

* update the changelog
  • Loading branch information
mmomtchev authored Oct 16, 2024
1 parent 86d02b5 commit 5e338e9
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 11 deletions.
4 changes: 4 additions & 0 deletions CHANGES-JSE
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ the issue number to the end of the URL: https://github.com/swig/swig/issues/
Version 5.0.6
===========================

2024-10-16: mmomtchev
[JavaScript] Fix [mmomtchev/swig#84](https://github.com/mmomtchev/swig/issues/84),
`std::shared_ptr` typemaps do not work with `NULL` values

2024-10-16: mmomtchev
[JavaScript] Use TypeScript type branding for representing C/C++
opaque objects and allow `null` values
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public:

%}

%template() std::vector<std::shared_ptr<C> >;
%template(c_array) std::vector<std::shared_ptr<C> >;

%inline %{

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
var cpp11_shared_ptr_nullptr_in_containers = require('cpp11_shared_ptr_nullptr_in_containers');

const a = /* await */(cpp11_shared_ptr_nullptr_in_containers.ret_vec_c_shared_ptr());
if (!(a instanceof cpp11_shared_ptr_nullptr_in_containers.c_array))
throw new Error('Did not receive an array');

if (/* await */(a.size()) !== 3)
throw new Error('Expected 3 elements');


const values = [0, null, 2];
for (const i in values) {
const el = /* await */(a.get(+i));
if (values[i] === null) {
if (el !== null)
throw new Error(`${i} element is not null`);
} else {
if (/* await */(el.get_m()) !== values[i])
throw new Error(`${i} element is not ${values[i]}`);
}
}
24 changes: 14 additions & 10 deletions Lib/javascript/napi/std_shared_ptr.i
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,24 @@
// (the *& trick is needed because $1 is a SwigValueWrapper)
%typemap(out) std::shared_ptr<CONST TYPE> {
%set_output(SWIG_NewPointerObj(const_cast<TYPE *>($1.get()), $descriptor(TYPE *), SWIG_POINTER_OWN | %newpointer_flags));
auto *owner = new std::shared_ptr<CONST TYPE>(*&$1);
auto finalizer = new SWIG_NAPI_Finalizer([owner](){
delete owner;
});
SWIG_NAPI_SetFinalizer(env, $result, finalizer);
if (!$result.IsNull()) {
auto *owner = new std::shared_ptr<CONST TYPE>(*&$1);
auto finalizer = new SWIG_NAPI_Finalizer([owner](){
delete owner;
});
SWIG_NAPI_SetFinalizer(env, $result, finalizer);
}
}

%typemap(out) std::shared_ptr<CONST TYPE> & {
%set_output(SWIG_NewPointerObj(const_cast<TYPE *>($1->get()), $descriptor(TYPE *), $owner | %newpointer_flags));
auto owner = new std::shared_ptr<CONST TYPE>(*$1);
auto finalizer = new SWIG_NAPI_Finalizer([owner](){
delete owner;
});
SWIG_NAPI_SetFinalizer(env, $result, finalizer);
if (!$result.IsNull()) {
auto owner = new std::shared_ptr<CONST TYPE>(*$1);
auto finalizer = new SWIG_NAPI_Finalizer([owner](){
delete owner;
});
SWIG_NAPI_SetFinalizer(env, $result, finalizer);
}
}

%typemap(in, numinputs=0) std::shared_ptr<CONST TYPE> &OUTPUT {
Expand Down

0 comments on commit 5e338e9

Please sign in to comment.