Generalize support for struct fields

This commit is contained in:
Chad Austin 2013-04-01 18:47:55 -07:00 коммит произвёл Jukka Jylänki
Родитель de48fdc5b0
Коммит e5af621a18
3 изменённых файлов: 63 добавлений и 21 удалений

Просмотреть файл

@ -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);
}
};

Просмотреть файл

@ -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<ClassType>::get(),
fieldName,
TypeID<FieldType>::get(),
reinterpret_cast<GenericFunction>(&MemberAccess<ClassType, FieldType>::getWire),
reinterpret_cast<GenericFunction>(&MemberAccess<ClassType, FieldType>::setWire),
reinterpret_cast<GenericFunction>(
&MemberAccess<ClassType, FieldType>::getWire),
getContext(field),
TypeID<FieldType>::get(),
reinterpret_cast<GenericFunction>(
&MemberAccess<ClassType, FieldType>::setWire),
getContext(field));
return *this;
}
template<typename GetterReturnType, typename SetterArgumentType>
value_struct& field(
const char* fieldName,
GetterReturnType (*getter)(const ClassType&),
void (*setter)(ClassType&, SetterArgumentType)
) {
using namespace internal;
_embind_register_struct_field(
TypeID<ClassType>::get(),
fieldName,
TypeID<GetterReturnType>::get(),
reinterpret_cast<GenericFunction>(
&MemberAccess<ClassType, GetterReturnType>
::template propertyGet<GetterReturnType(*)(const ClassType&)>),
getContext(getter),
TypeID<SetterArgumentType>::get(),
reinterpret_cast<GenericFunction>(
&MemberAccess<ClassType, SetterArgumentType>
::template propertySet<void(*)(ClassType&, SetterArgumentType)>),
getContext(setter));
return *this;
}
};
////////////////////////////////////////////////////////////////////////////////

Просмотреть файл

@ -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>("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);