Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

embind: Add support for std::nullptr_t #18661

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
17 changes: 17 additions & 0 deletions src/embind/embind.js
Original file line number Diff line number Diff line change
@@ -675,6 +675,23 @@ var LibraryEmbind = {
return this['fromWireType']({{{ makeGetValue('pointer', '0', 'i32') }}});
},

_embind_register_std_nullptr_t__sig: 'vpppDn',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be vpp.

_embind_register_std_nullptr_t__deps: ['$registerType'],
_embind_register_std_nullptr_t: function(rawType, name) {

registerType(rawType, {
name: name,
'fromWireType': function (value) {
return null;
},
'toWireType': function (destructors, value) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think null is a valid wasm type. This should probably be 0.

return null;
},
'argPackAdvance': 8,
destructorFunction: null, // This type does not need a destructor
});
},

_embind_register_std_string__sig: 'vpp',
_embind_register_std_string__deps: [
'$readLatin1String', '$registerType',
4 changes: 4 additions & 0 deletions system/include/emscripten/bind.h
Original file line number Diff line number Diff line change
@@ -78,6 +78,10 @@ void _embind_register_float(
const char* name,
size_t size);

void _embind_register_std_nullptr_t(
TYPEID nullptrType,
const char* name);

void _embind_register_std_string(
TYPEID stringType,
const char* name);
2 changes: 2 additions & 0 deletions system/lib/embind/bind.cpp
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@
#endif
#include <algorithm>
#include <climits>
#include <cstddef>
#include <emscripten/emscripten.h>
#include <emscripten/wire.h>
#include <limits>
@@ -142,6 +143,7 @@ EMSCRIPTEN_BINDINGS(builtin) {
register_float<float>("float");
register_float<double>("double");

_embind_register_std_nullptr_t(TypeID<std::nullptr_t>::get(), "std::nullptr_t");
_embind_register_std_string(TypeID<std::string>::get(), "std::string");
_embind_register_std_string(
TypeID<std::basic_string<unsigned char>>::get(), "std::basic_string<unsigned char>");
4 changes: 4 additions & 0 deletions test/embind/embind.test.js
Original file line number Diff line number Diff line change
@@ -565,6 +565,10 @@ module({
assert.equal(undefined, cm.emval_test_return_void());
});

test("nullptr_t return converts to null", function() {
assert.equal(null, cm.emval_test_return_null());
});

test("booleans can be marshalled", function() {
assert.equal(false, cm.emval_test_not(true));
assert.equal(true, cm.emval_test_not(false));
5 changes: 5 additions & 0 deletions test/embind/embind_test.cpp
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@
// University of Illinois/NCSA Open Source License. Both these licenses can be
// found in the LICENSE file.

#include <cstddef>
#include <string>
#include <malloc.h>
#include <functional>
@@ -74,6 +75,10 @@ val emval_test_passthrough(val v) {
void emval_test_return_void() {
}

std::nullptr_t emval_test_return_nullptr_t() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why would a function have a return type of std::nullptr_t. I've seen it used for overloading with parameters but not this?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also don't see this function bound anywhere. e.g. look at how emval_test_not works.

return nullptr;
}

bool emval_test_not(bool b) {
return !b;
}