@@ -105,7 +105,8 @@ void _embind_register_function(
105105 const char * signature,
106106 GenericFunction invoker,
107107 GenericFunction function,
108- bool isAsync);
108+ bool isAsync,
109+ bool isNonnullReturn);
109110
110111void _embind_register_value_array (
111112 TYPEID tupleType,
@@ -182,7 +183,8 @@ void _embind_register_class_function(
182183 GenericFunction invoker,
183184 void * context,
184185 unsigned isPureVirtual,
185- bool isAsync);
186+ bool isAsync,
187+ bool isNonnullReturn);
186188
187189void _embind_register_class_property (
188190 TYPEID classType,
@@ -204,7 +206,8 @@ void _embind_register_class_class_function(
204206 const char * invokerSignature,
205207 GenericFunction invoker,
206208 GenericFunction method,
207- bool isAsync);
209+ bool isAsync,
210+ bool isNonnullReturn);
208211
209212void _embind_register_class_class_property (
210213 TYPEID classType,
@@ -338,6 +341,15 @@ struct pure_virtual {
338341 };
339342};
340343
344+ template <typename Slot>
345+ struct nonnull {
346+ static_assert (std::is_same<Slot, ret_val>::value, " Only nonnull return values are currently supported." );
347+ template <typename InputType, int Index>
348+ struct Transform {
349+ typedef InputType type;
350+ };
351+ };
352+
341353namespace return_value_policy {
342354
343355struct take_ownership : public allow_raw_pointers {};
@@ -380,6 +392,11 @@ struct isPolicy<emscripten::pure_virtual, Rest...> {
380392 static constexpr bool value = true ;
381393};
382394
395+ template <typename T, typename ... Rest>
396+ struct isPolicy <emscripten::nonnull<T>, Rest...> {
397+ static constexpr bool value = true ;
398+ };
399+
383400template <typename T, typename ... Rest>
384401struct isPolicy <T, Rest...> {
385402 static constexpr bool value = isPolicy<Rest...>::value;
@@ -428,6 +445,24 @@ struct isAsync<> {
428445 static constexpr bool value = false ;
429446};
430447
448+ template <typename ... Policies>
449+ struct isNonnullReturn ;
450+
451+ template <typename ... Rest>
452+ struct isNonnullReturn <nonnull<ret_val>, Rest...> {
453+ static constexpr bool value = true ;
454+ };
455+
456+ template <typename T, typename ... Rest>
457+ struct isNonnullReturn <T, Rest...> {
458+ static constexpr bool value = isNonnullReturn<Rest...>::value;
459+ };
460+
461+ template <>
462+ struct isNonnullReturn <> {
463+ static constexpr bool value = false ;
464+ };
465+
431466}
432467
433468// //////////////////////////////////////////////////////////////////////////////
@@ -640,7 +675,8 @@ void function(const char* name, ReturnType (*fn)(Args...), Policies...) {
640675 getSignature (invoke),
641676 reinterpret_cast <GenericFunction>(invoke),
642677 reinterpret_cast <GenericFunction>(fn),
643- isAsync<Policies...>::value);
678+ isAsync<Policies...>::value,
679+ isNonnullReturn<Policies...>::value);
644680}
645681
646682namespace internal {
@@ -1516,7 +1552,8 @@ struct RegisterClassMethod<ReturnType (ClassType::*)(Args...)> {
15161552 reinterpret_cast <GenericFunction>(invoke),
15171553 getContext (memberFunction),
15181554 isPureVirtual<Policies...>::value,
1519- isAsync<Policies...>::value);
1555+ isAsync<Policies...>::value,
1556+ isNonnullReturn<Policies...>::value);
15201557 }
15211558};
15221559
@@ -1545,7 +1582,8 @@ struct RegisterClassMethod<ReturnType (ClassType::*)(Args...) const> {
15451582 reinterpret_cast <GenericFunction>(invoke),
15461583 getContext (memberFunction),
15471584 isPureVirtual<Policies...>::value,
1548- isAsync<Policies...>::value);
1585+ isAsync<Policies...>::value,
1586+ isNonnullReturn<Policies...>::value);
15491587 }
15501588};
15511589
@@ -1573,7 +1611,8 @@ struct RegisterClassMethod<ReturnType (*)(ThisType, Args...)> {
15731611 reinterpret_cast <GenericFunction>(invoke),
15741612 getContext (function),
15751613 false ,
1576- isAsync<Policies...>::value);
1614+ isAsync<Policies...>::value,
1615+ isNonnullReturn<Policies...>::value);
15771616 }
15781617};
15791618
@@ -1601,7 +1640,8 @@ struct RegisterClassMethod<std::function<ReturnType (ThisType, Args...)>> {
16011640 reinterpret_cast <GenericFunction>(invoke),
16021641 getContext (function),
16031642 false ,
1604- isAsync<Policies...>::value);
1643+ isAsync<Policies...>::value,
1644+ isNonnullReturn<Policies...>::value);
16051645 }
16061646};
16071647
@@ -1623,7 +1663,8 @@ struct RegisterClassMethod<ReturnType (ThisType, Args...)> {
16231663 reinterpret_cast <GenericFunction>(invoke),
16241664 getContext (callable),
16251665 false ,
1626- isAsync<Policies...>::value);
1666+ isAsync<Policies...>::value,
1667+ isNonnullReturn<Policies...>::value);
16271668 }
16281669};
16291670
@@ -1752,7 +1793,7 @@ class class_ {
17521793 class_function (
17531794 " implement" ,
17541795 &wrapped_new<WrapperType*, WrapperType, val, ConstructorArgs...>,
1755- allow_raw_pointer<ret_val>())
1796+ allow_raw_pointer<ret_val>(), nonnull<ret_val>() )
17561797 .class_function (
17571798 " extend" ,
17581799 &wrapped_extend<WrapperType>)
@@ -1940,7 +1981,8 @@ class class_ {
19401981 getSignature (invoke),
19411982 reinterpret_cast <GenericFunction>(invoke),
19421983 reinterpret_cast <GenericFunction>(classMethod),
1943- isAsync<Policies...>::value);
1984+ isAsync<Policies...>::value,
1985+ isNonnullReturn<Policies...>::value);
19441986 return *this ;
19451987 }
19461988
0 commit comments