aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xsrc/embind/embind.js12
-rwxr-xr-xsystem/include/emscripten/bind.h31
-rwxr-xr-xsystem/include/emscripten/wire.h2
-rwxr-xr-xtests/embind/embind.test.js6
-rw-r--r--tests/embind/embind_test.cpp10
5 files changed, 60 insertions, 1 deletions
diff --git a/src/embind/embind.js b/src/embind/embind.js
index e050d3c2..46fdeee4 100755
--- a/src/embind/embind.js
+++ b/src/embind/embind.js
@@ -1245,3 +1245,15 @@ function __embind_register_interface(
});
}
+function __embind_register_constant(name, type, value, destructor) {
+ name = Pointer_stringify(name);
+ whenDependentTypesAreResolved([], [type], function(type) {
+ type = type[0];
+ /*global console*/
+ //console.log('type', type);
+ //console.log('value', value);
+ Module[name] = type.fromWireType(value);
+ // todo: I need to natively destruct 'value' here
+ return [];
+ });
+}
diff --git a/system/include/emscripten/bind.h b/system/include/emscripten/bind.h
index 4ecd35e9..e767afaa 100755
--- a/system/include/emscripten/bind.h
+++ b/system/include/emscripten/bind.h
@@ -159,6 +159,11 @@ namespace emscripten {
const char* name,
GenericFunction constructor,
GenericFunction destructor);
+
+ void _embind_register_constant(
+ const char* name,
+ TYPEID constantType,
+ uintptr_t value);
}
}
}
@@ -1023,6 +1028,32 @@ namespace emscripten {
}
};
+ ////////////////////////////////////////////////////////////////////////////////
+ // CONSTANTS
+ ////////////////////////////////////////////////////////////////////////////////
+
+ namespace internal {
+ template<typename T>
+ uintptr_t asGenericValue(T t) {
+ return static_cast<uintptr_t>(t);
+ }
+
+ template<typename T>
+ uintptr_t asGenericValue(T* p) {
+ return reinterpret_cast<uintptr_t>(p);
+ }
+ }
+
+ template<typename ConstantType>
+ void constant(const char* name, const ConstantType& v) {
+ using namespace internal;
+ typedef BindingType<const ConstantType&> BT;
+ _embind_register_constant(
+ name,
+ TypeID<const ConstantType&>::get(),
+ asGenericValue(BindingType<const ConstantType&>::toWireType(v)));
+ }
+
namespace internal {
template<typename T>
class optional {
diff --git a/system/include/emscripten/wire.h b/system/include/emscripten/wire.h
index 0c0f5eb8..9c8cd096 100755
--- a/system/include/emscripten/wire.h
+++ b/system/include/emscripten/wire.h
@@ -122,7 +122,7 @@ namespace emscripten {
template<> \
struct BindingType<type> { \
typedef type WireType; \
- constexpr static WireType toWireType(const type& v) { \
+ constexpr static WireType toWireType(const type& v) { \
return v; \
} \
constexpr static type fromWireType(WireType v) { \
diff --git a/tests/embind/embind.test.js b/tests/embind/embind.test.js
index a577cd37..7bb3985f 100755
--- a/tests/embind/embind.test.js
+++ b/tests/embind/embind.test.js
@@ -1515,6 +1515,12 @@ module({
assert.equal('ValHolder$setVal', cm.ValHolder.prototype.setVal.name);
assert.equal('ValHolder$makeConst', cm.ValHolder.makeConst.name);
});
+
+ BaseFixture.extend("constants", function() {
+ assert.equal(10, cm.INT_CONSTANT);
+ assert.equal("some string", cm.STRING_CONSTANT);
+ //assert.deepEqual([1, 2, 3], cm.VALUE_TUPLE_CONSTANT);
+ });
});
/* global run_all_tests */
diff --git a/tests/embind/embind_test.cpp b/tests/embind/embind_test.cpp
index 0ae1801a..4d1b376b 100644
--- a/tests/embind/embind_test.cpp
+++ b/tests/embind/embind_test.cpp
@@ -1352,6 +1352,16 @@ int overloaded_function(int i, int j)
return 2;
}
+EMSCRIPTEN_BINDINGS(constants) {
+ constant("INT_CONSTANT", 10);
+ constant("STRING_CONSTANT", std::string("some string"));
+ TupleVector tv;
+ tv.x = 1;
+ tv.y = 2;
+ tv.z = 3;
+ constant("VALUE_TUPLE_CONSTANT", tv);
+}
+
EMSCRIPTEN_BINDINGS(tests) {
register_js_interface();