def test_class_method_overload(): cls = cpp.cpp_class('MyTest') cls.qualname = "MyTest" method = cpp.cpp_method('method', params=[cpp.cpp_variable('myvar', cpp.cpp_type('MyTestClass', const=True))]) method1 = cpp.cpp_method('method', params=[cpp.cpp_variable('myvar', cpp.cpp_type('int', const=True))]) method2 = cpp.cpp_method('method', params=[cpp.cpp_variable('myvar', cpp.cpp_type('int', const=True)), cpp.cpp_variable('othervar', cpp.cpp_type('int', const=True))]) cls.public.append(method) method.parent = cls cls.public.append(method1) method1.parent = cls cls.public.append(method2) method2.parent = cls context = lua.lua_context_builder.bake([cls]) assert len(context.translated) == 1 t_cls = context.translated[0] assert t_cls assert t_cls.name == 'lua_munch_MyTest'
def test_class_base(): base = cpp.cpp_class('base') base.qualname = 'MyBaseClass' bmethod = cpp.cpp_method('base_method', params=[cpp.cpp_variable('variable', cpp.cpp_type('int'))]) bmethod.is_virtual = True base.public.append(bmethod) bmethod.parent = base cls = cpp.cpp_class('MyTest') cls.qualname = 'MyTestClass' cls.bases.append(base) method = cpp.cpp_method('method', params=[cpp.cpp_variable('myvar', cpp.cpp_type('MyTestClass', const=True))]) method1 = cpp.cpp_method('method', params=[cpp.cpp_variable('myvar', cpp.cpp_type('int', const=True))]) cls.public.append(method) method.parent = cls cls.public.append(method1) method1.parent = cls context = lua.lua_context_builder.bake([base, cls]) assert len(context.translated) == 2 t_cls = context.translated[1] assert t_cls assert t_cls.name == 'lua_munch_MyTest' print context.translated[0] print t_cls assert False
def init_translated_class(orig_class, context): assert 'translation' not in orig_class.__dict__ lua_cls = cpp.cpp_class('lua_munch_' + orig_class.identifier_name) luaReg = cpp.cpp_variable_array('lua_reg_' + orig_class.identifier_name, cpp.cpp_type('luaL_Reg', static=True)) logging.debug('baking methods for class: %s : %s' % (orig_class.name ,orig_class.public)) #we now bake all methods and subclasses from this class, but not preprocess them class_ctx = lua_context_builder.bake(orig_class.public, preprocess=False) for item in class_ctx.translated: if item.public: lua_cls.public.append(item) else: lua_cls.protected.append(item) if type(item) == cpp.cpp_method and not item.is_constructor and item.public: luaReg.expr.append('{ "%s" , lua_munch_%s::lua_munch_%s }' % (item.name, orig_class.name, item.name)) #the base methods are shims that only retrieve it's self and pass it down to the #base class for base in orig_class.bases: for item in base.public: if type(item) != cpp.cpp_method: continue method = cpp.cpp_method('lua_munch_' + item.name, static=True, returns=cpp.cpp_type('int'), params=[cpp.cpp_variable('L', cpp.cpp_type('lua_State', pointer=True))]) if item.parent: item.parameters.append(cpp.cpp_variable('lua_self', cpp.cpp_type(item.parent.name, pointer=True))) method.exprs.append('%s* lua_self = lua_munch_%s::get(L)' % (orig_class.qualname, orig_class.identifier_name)) method.exprs.append(cpp.cpp_return(cpp.cpp_method_call('lua_munch_%s::lua_munch_%s' % (item.parent.name, item.name), params=['L', 'lua_self']))) lua_cls.public.append(method) if not item.is_constructor: luaReg.expr.append('{ "%s" , lua_munch_%s::lua_munch_%s }' % (item.name, orig_class.name, item.name)) luaReg.expr.append('{ 0, 0 }') lua_cls.public.append(luaReg) orig_class.translation = lua_cls
def lua_preprocess(data, context): classes = filter(lambda item: type(item) == cpp.cpp_class, data) functions = filter(lambda item: type(item) == cpp.cpp_method and item.parent == None, data) other = filter(lambda item: type(item) != cpp.cpp_method and type(item) != cpp.cpp_class, data) del data[:] for item in classes: item.identifier_name = item.qualname.replace('::', '_') process_functions(item) fake_cls = cpp.cpp_class('Fake') fake_cls.public = functions process_functions(fake_cls) data += classes + fake_cls.public + other
def test_class(): cls = cpp.cpp_class('MyTest') cls.qualname = "MyTest" method = cpp.cpp_method('method', params=[cpp.cpp_variable('myvar', cpp.cpp_type('MyTestClass', const=True))]) cls.public.append(method) context = lua.lua_context_builder.bake([cls]) assert len(context.translated) == 1 t_cls = context.translated[0] assert t_cls assert t_cls.name == 'lua_munch_MyTest'