From e5af621a189f729d8c1513f739a6a085adf39842 Mon Sep 17 00:00:00 2001 From: Chad Austin Date: Mon, 1 Apr 2013 18:47:55 -0700 Subject: [PATCH] Generalize support for struct fields --- src/embind/embind.js | 29 +++++++++++--------- system/include/emscripten/bind.h | 45 ++++++++++++++++++++++++++------ tests/embind/embind_test.cpp | 10 ++++++- 3 files changed, 63 insertions(+), 21 deletions(-) diff --git a/src/embind/embind.js b/src/embind/embind.js index 3daa4b018..16e29d9e2 100755 --- a/src/embind/embind.js +++ b/src/embind/embind.js @@ -470,27 +470,32 @@ function __embind_register_struct( } function __embind_register_struct_field( - rawStructType, + structType, fieldName, - rawFieldType, - rawGetter, - rawSetter, - context + getterReturnType, + getter, + getterContext, + setterArgumentType, + setter, + setterContext ) { - var structType = requireRegisteredType(rawStructType, 'struct'); + structType = requireRegisteredType(structType, 'struct'); fieldName = Pointer_stringify(fieldName); - rawGetter = FUNCTION_TABLE[rawGetter]; - rawSetter = FUNCTION_TABLE[rawSetter]; + getter = FUNCTION_TABLE[getter]; + setter = FUNCTION_TABLE[setter]; + // TODO: test incomplete registration of value structs - whenDependentTypesAreResolved([], [rawFieldType], function(fieldType) { - fieldType = fieldType[0]; + whenDependentTypesAreResolved([], [getterReturnType, setterArgumentType], function(types) { + var getterReturnType = types[0]; + var setterArgumentType = types[1]; structType.fields[fieldName] = { read: function(ptr) { - return fieldType.fromWireType(rawGetter(context, ptr)); + return getterReturnType.fromWireType( + getter(getterContext, ptr)); }, write: function(ptr, o) { var destructors = []; - rawSetter(context, ptr, fieldType.toWireType(destructors, o)); + setter(setterContext, ptr, setterArgumentType.toWireType(destructors, o)); runDestructors(destructors); } }; diff --git a/system/include/emscripten/bind.h b/system/include/emscripten/bind.h index a5530744b..fe0c97119 100755 --- a/system/include/emscripten/bind.h +++ b/system/include/emscripten/bind.h @@ -71,25 +71,27 @@ namespace emscripten { void _embind_register_tuple_element( TYPEID tupleType, TYPEID getterReturnType, - GenericFunction staticGetter, + GenericFunction getter, void* getterContext, TYPEID setterArgumentType, - GenericFunction staticSetter, + GenericFunction setter, void* setterContext); void _embind_register_struct( TYPEID structType, - const char* name, + const char* fieldName, GenericFunction constructor, GenericFunction destructor); void _embind_register_struct_field( TYPEID structType, - const char* name, - TYPEID fieldType, + const char* fieldName, + TYPEID getterReturnType, GenericFunction getter, + void* getterContext, + TYPEID setterArgumentType, GenericFunction setter, - void* context); + void* setterContext); void _embind_register_smart_ptr( TYPEID pointerType, @@ -462,11 +464,38 @@ namespace emscripten { TypeID::get(), fieldName, TypeID::get(), - reinterpret_cast(&MemberAccess::getWire), - reinterpret_cast(&MemberAccess::setWire), + reinterpret_cast( + &MemberAccess::getWire), + getContext(field), + TypeID::get(), + reinterpret_cast( + &MemberAccess::setWire), getContext(field)); return *this; } + + template + value_struct& field( + const char* fieldName, + GetterReturnType (*getter)(const ClassType&), + void (*setter)(ClassType&, SetterArgumentType) + ) { + using namespace internal; + _embind_register_struct_field( + TypeID::get(), + fieldName, + TypeID::get(), + reinterpret_cast( + &MemberAccess + ::template propertyGet), + getContext(getter), + TypeID::get(), + reinterpret_cast( + &MemberAccess + ::template propertySet), + getContext(setter)); + return *this; + } }; //////////////////////////////////////////////////////////////////////////////// diff --git a/tests/embind/embind_test.cpp b/tests/embind/embind_test.cpp index e9feccfef..156c86808 100644 --- a/tests/embind/embind_test.cpp +++ b/tests/embind/embind_test.cpp @@ -758,6 +758,14 @@ struct StructVector { float x, y, z; }; +float readStructVectorZ(const StructVector& v) { + return v.z; +} + +void writeStructVectorZ(StructVector& v, float z) { + v.z = z; +} + StructVector emval_test_return_StructVector() { StructVector v; v.x = 1; @@ -1354,7 +1362,7 @@ EMSCRIPTEN_BINDINGS(tests) { value_struct("StructVector") .field("x", &StructVector::x) .field("y", &StructVector::y) - .field("z", &StructVector::z) + .field("z", &readStructVectorZ, &writeStructVectorZ) ; function("emval_test_return_StructVector", &emval_test_return_StructVector);