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
27 changes: 0 additions & 27 deletions system/include/emscripten/bind.h
Original file line number Diff line number Diff line change
Expand Up @@ -1919,35 +1919,8 @@ class_<std::map<K, V, Compare, Allocator>> register_map(const char* name) {
;
}

////////////////////////////////////////////////////////////////////////////////
// std::optional
////////////////////////////////////////////////////////////////////////////////

namespace internal {
template <typename T>
struct BindingType<std::optional<T>> {
using ValBinding = BindingType<val>;
using WireType = ValBinding::WireType;

template<typename ReturnPolicy = void>
static WireType toWireType(std::optional<T> value, rvp::default_tag) {
if (value) {
return ValBinding::toWireType(val(*value, allow_raw_pointers()), rvp::default_tag{});
}
return ValBinding::toWireType(val::undefined(), rvp::default_tag{});
}


static std::optional<T> fromWireType(WireType value) {
val optional = val::take_ownership(value);
if (optional.isUndefined()) {
return {};
}
return optional.as<T>();
}
};
} // end namespace internal


////////////////////////////////////////////////////////////////////////////////
// ENUMS
Expand Down
24 changes: 24 additions & 0 deletions system/include/emscripten/val.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <vector>
#include <type_traits>
#include <pthread.h>
#include <optional>
#if __cplusplus >= 202002L
#include <coroutine>
#include <exception>
Expand Down Expand Up @@ -815,6 +816,29 @@ struct BindingType<T, typename std::enable_if<std::is_base_of<val, T>::value &&
}
};

template <typename T>
struct BindingType<std::optional<T>> {
using ValBinding = BindingType<val>;
using WireType = ValBinding::WireType;

template<typename ReturnPolicy = void>
static WireType toWireType(std::optional<T> value, rvp::default_tag) {
if (value) {
return ValBinding::toWireType(val(*value, allow_raw_pointers()), rvp::default_tag{});
}
return ValBinding::toWireType(val::undefined(), rvp::default_tag{});
}


static std::optional<T> fromWireType(WireType value) {
val optional = val::take_ownership(value);
if (optional.isUndefined()) {
return {};
}
return optional.as<T>();
}
};

}

template <typename T, typename... Policies>
Expand Down
14 changes: 14 additions & 0 deletions test/embind/test_optional_val_lib.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <emscripten/val.h>
#include <optional>
#include <string>

// This file deliberately does NOT include <emscripten/bind.h>

class MyType {
public:
void RunCallback(emscripten::val callback);
};

void MyType::RunCallback(emscripten::val cb) {
cb(std::make_optional(std::string{"Hey"}));
}
30 changes: 30 additions & 0 deletions test/embind/test_optional_val_main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <emscripten/bind.h>
#include <emscripten/val.h>
#include <string>
#include <iostream>

using namespace emscripten;

class MyType {
public:
void RunCallback(emscripten::val callback);
};

int main() {
EM_ASM(
let value = new Module.MyType();
value.RunCallback((e) => {
console.log("Received: " + e);
if (e !== "Hey") throw "Expected 'Hey', got " + e;
});
);
std::cout << "done" << std::endl;
}

EMSCRIPTEN_BINDINGS(my_module) {
register_optional<std::string>();

class_<MyType>("MyType")
.constructor<>()
.function("RunCallback", &MyType::RunCallback);
}
9 changes: 9 additions & 0 deletions test/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -14471,6 +14471,15 @@ def test_embind_no_exceptions(self):
create_file('a.cpp', '#define try\n#define catch if (0)\n#include <emscripten/bind.h>')
self.run_process([EMXX, '-fno-exceptions', '-std=c++23', '-lembind', 'a.cpp'])

def test_embind_optional_val_no_bind(self):
# Ensure passing std::optional to emscripten::val works if <emscripten/bind.h>
# was not included in the compilation unit using val.
self.run_process([EMXX,'-lembind',
test_file('embind/test_optional_val_main.cpp'),
test_file('embind/test_optional_val_lib.cpp')])
output = self.run_js('a.out.js')
self.assertContained('done', output)

def test_no_pthread(self):
self.do_runf('hello_world.c', cflags=['-pthread', '-no-pthread'])
self.assertExists('hello_world.js')
Expand Down