void FindsProperty(string code, Func <ClassDeclaration, bool> classFinder, Func <PropertyDeclaration, bool> propFinder) { CustomSwiftCompiler compiler = Utils.CompileSwift(code, moduleName: "CanFind"); var errors = new ErrorHandling(); ModuleInventory mi = ModuleInventory.FromFile(Path.Combine(compiler.DirectoryPath, "libCanFind.dylib"), errors); ModuleDeclaration mod = compiler.ReflectToModules(new string [] { compiler.DirectoryPath }, new string [] { compiler.DirectoryPath }, null, "CanFind") [0]; ClassDeclaration classDecl = mod.AllClasses.FirstOrDefault(classFinder); Assert.IsNotNull(classDecl, "null class"); PropertyDeclaration propDecl = classDecl.Members.OfType <PropertyDeclaration> ().FirstOrDefault(propFinder); Assert.IsNotNull(propDecl, "null property"); FunctionDeclaration getter = propDecl.GetGetter(); Assert.IsNotNull(getter, "null getter"); FunctionDeclaration setter = propDecl.GetSetter(); Assert.IsNotNull(setter, "null setter"); TLFunction tlgetter = XmlToTLFunctionMapper.ToTLFunction(getter, mi); Assert.IsNotNull(tlgetter, "null tlgetter"); TLFunction tlsetter = XmlToTLFunctionMapper.ToTLFunction(setter, mi); Assert.IsNotNull(tlsetter, "null tlsetter"); }
public void FindsPropertyGetterAndSetterFuncs() { string code = "public class Bar { public var x:Int = 0; }"; CustomSwiftCompiler compiler = Utils.CompileSwift(code, moduleName: "CanFind"); ModuleDeclaration mod = compiler.ReflectToModules(new string [] { compiler.DirectoryPath }, new string [] { compiler.DirectoryPath }, null, "CanFind") [0]; ClassDeclaration classDecl = mod.AllClasses.FirstOrDefault(cl => cl.Name == "Bar"); Assert.IsNotNull(classDecl); PropertyDeclaration propDecl = classDecl.Members.OfType <PropertyDeclaration> ().FirstOrDefault(p => p.Name == "x"); Assert.IsNotNull(propDecl); FunctionDeclaration getter = propDecl.GetGetter(); Assert.IsNotNull(getter); FunctionDeclaration setter = propDecl.GetSetter(); Assert.IsNotNull(setter); }
public static void AddPropertyGuard(PropertyDeclaration property, MethodBodyTransformationContext context, InstructionBlock block, InstructionWriter writer) { var propertyType = property.PropertyType; var methodBody = block.MethodBody; var sequence = block.AddInstructionSequence(null, NodePosition.After, null); if (sequence == null) return; var oldValueVariable = block.DefineLocalVariable(propertyType, string.Format("old{0}Value", property.Name)); var assets = GetTransformationAssets(property.Module); writer.AttachInstructionSequence(sequence); var isLocationBinding = CheckIfIsLocationBinding(methodBody,assets); if (isLocationBinding) { writer.AssignValue_LocalVariable(oldValueVariable , () => writer.Call_MethodOnTarget(property.GetGetter() , () => { //Load the instance parameter of the SetValue method //and convert it to the type writer.EmitInstruction(OpCodeNumber.Ldarg_1); //writer.EmitInstructionLoadIndirect(Assets.ObjectTypeSignature); writer.EmitInstructionType(OpCodeNumber.Ldobj, assets.ObjectTypeSignature); writer.EmitConvertFromObject(property.Parent); } ) ); //On the location binding the value parameter is at psotion 3 writer.EmitInstruction(OpCodeNumber.Ldarg_3); } else { writer.AssignValue_LocalVariable(oldValueVariable, () => writer.Get_PropertyValue(property)); //For a normal property the value parameter is at position 1 writer.EmitInstruction(OpCodeNumber.Ldarg_1); } if (propertyType.IsStruct()) { writer.EmitInstructionType(OpCodeNumber.Box, propertyType); } writer.Box_LocalVariableIfNeeded(oldValueVariable); var isPrimitive = propertyType.IsPrimitive(); if (isPrimitive) { writer.Compare_Primitives(); } else { //TODO: Try and use the equality operator when present writer.Compare_Objects(assets.ObjectEqualsMethod); } //writer.Leave_IfTrue(_context.LeaveBranchTarget); writer.Leave_IfTrue(context.LeaveBranchTarget); writer.DetachInstructionSequence(); }
void CompileProp(PropertyDeclaration propDecl, ProtocolDeclaration proto, CSInterface iface, CSUsingPackages use) { var getter = propDecl.GetGetter(); var setter = propDecl.GetSetter(); var publicProp = topLevelFunctionCompiler.CompileProperty(use, null, getter, setter, CSMethodKind.None); publicProp = new CSProperty(publicProp.PropType, CSMethodKind.None, publicProp.Name, CSVisibility.None, new CSCodeBlock(), CSVisibility.None, setter != null ? new CSCodeBlock() : null); ExportAttribute(getter.ObjCSelector).AttachBefore(publicProp); if (!propDecl.IsOptional) { kAbstractAttribute.AttachBefore(publicProp); } iface.Properties.Add(publicProp); }
public static void Get_PropertyValue(this InstructionWriter instructionWriter, PropertyDeclaration propertyDeclaration) { Call_MethodOnTarget(instructionWriter, propertyDeclaration.GetGetter(), () => instructionWriter.Get_This()); }