Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 24 additions & 23 deletions system/include/emscripten/wire.h
Original file line number Diff line number Diff line change
Expand Up @@ -510,37 +510,38 @@ struct ret_val {
static constexpr int index = 0;
};

/*
namespace internal {

template <typename InputType, bool EnableWrapper>
struct RawPointerTransformer {
// Use decay to handle references to pointers e.g.(T*&)->(T*).
using DecayedType = std::decay_t<InputType>;
static constexpr bool ShouldWrap = EnableWrapper && std::is_pointer_v<DecayedType>;
using type = std::conditional_t<
ShouldWrap,
internal::AllowedRawPointer<std::remove_pointer_t<DecayedType>>,
InputType
>;
};

} // namespace internal

template<typename Slot>
struct allow_raw_pointer {
template<typename InputType, int Index>
struct Transform {
typedef typename std::conditional<
Index == Slot::index,
internal::AllowedRawPointer<typename std::remove_pointer<InputType>::type>,
InputType
>::type type;
};
struct Transform : internal::RawPointerTransformer<
InputType,
Index == Slot::index
> {};
};
*/

// allow all raw pointers
struct allow_raw_pointers {
template<typename InputType, int Index>
struct Transform {
// Use decay to handle references to pointers e.g.(T*&)->(T*).
typedef typename std::decay<InputType>::type DecayedType;
typedef typename std::conditional<
std::is_pointer<DecayedType>::value,
internal::AllowedRawPointer<typename std::remove_pointer<DecayedType>::type>,
InputType
>::type type;
};
};

// this is temporary until arg policies are reworked
template<typename Slot>
struct allow_raw_pointer : public allow_raw_pointers {
struct Transform : internal::RawPointerTransformer<
InputType,
true
> {};
};

struct async {
Expand Down
21 changes: 21 additions & 0 deletions test/embind/test_embind_allow_raw_pointer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <emscripten/bind.h>

using namespace emscripten;

class C {};

void onePointerArg(C* ptr) {}
void twoPointerArg(C* ptr1, C* ptr2) {}
void sandwich(int a, C* ptr1, int b) {}

C* pointerRet() { return nullptr; }
C* pointerRetPointerArg(C* ptr) { return nullptr; }

EMSCRIPTEN_BINDINGS(raw_pointers) {
class_<C>("C");
function("onePointerArg", &onePointerArg, allow_raw_pointer<arg<0>>());
function("twoPointerArg", &twoPointerArg, allow_raw_pointer<arg<0>>(), allow_raw_pointer<arg<1>>());
function("sandwich", &sandwich, allow_raw_pointer<arg<1>>());
function("pointerRet", &pointerRet, allow_raw_pointer<ret_val>());
function("pointerRetPointerArg", &pointerRetPointerArg, allow_raw_pointer<ret_val>(), allow_raw_pointer<arg<0>>());
}
12 changes: 12 additions & 0 deletions test/embind/test_embind_wrong_arg_allow.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include <emscripten/bind.h>

using namespace emscripten;

class C {};

void passThrough(int arg0, C* ptr) {}

EMSCRIPTEN_BINDINGS(raw_pointers) {
class_<C>("C");
function("passThrough", &passThrough, allow_raw_pointer<arg<0>>());
}
12 changes: 12 additions & 0 deletions test/embind/test_embind_wrong_ret_allow.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include <emscripten/bind.h>

using namespace emscripten;

class C {};

void passThrough(C* ptr) {}

EMSCRIPTEN_BINDINGS(raw_pointers) {
class_<C>("C");
function("passThrough", &passThrough, allow_raw_pointer<ret_val>());
}
5 changes: 5 additions & 0 deletions test/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -3362,10 +3362,15 @@ def test_embind_closure_no_dynamic_execution(self):
'val_invoke': ['embind/test_embind_no_raw_pointers_val_invoke.cpp'],
'val_call': ['embind/test_embind_no_raw_pointers_val_call.cpp'],
'val_new': ['embind/test_embind_no_raw_pointers_val_new.cpp'],
'wrong_ret_allow': ['embind/test_embind_wrong_ret_allow.cpp'],
'wrong_arg_allow': ['embind/test_embind_wrong_arg_allow.cpp'],
})
def test_embind_no_raw_pointers(self, filename):
self.assert_fail([EMCC, '-lembind', test_file(filename)], 'Implicitly binding raw pointers is illegal')

def test_embind_allow_raw_pointer(self):
self.emcc(test_file('embind/test_embind_allow_raw_pointer.cpp'), ['-lembind'])

@is_slow_test
@parameterized({
'': [],
Expand Down
Loading