protected virtual void EmitCtorForInstantiableClass() { var typeDef = this.Emitter.GetTypeDefinition(); string name = this.Emitter.Validator.GetCustomTypeName(typeDef, this.Emitter); if (name.IsEmpty()) { name = BridgeTypes.ToTypeScriptName(this.TypeInfo.Type, this.Emitter, false, true); } if (this.TypeInfo.Ctors.Count == 0) { this.Write("new "); this.WriteOpenCloseParentheses(); this.WriteColon(); this.Write(name); this.WriteSemiColon(); this.WriteNewLine(); } else if (this.TypeInfo.Ctors.Count == 1) { var ctor = this.TypeInfo.Ctors.First(); if (!ctor.HasModifier(Modifiers.Public)) { return; } XmlToJsDoc.EmitComment(this, ctor); this.Write("new "); this.EmitMethodParameters(ctor.Parameters, ctor); this.WriteColon(); this.Write(name); this.WriteSemiColon(); this.WriteNewLine(); } else { foreach (var ctor in this.TypeInfo.Ctors) { if (!ctor.HasModifier(Modifiers.Public)) { continue; } XmlToJsDoc.EmitComment(this, ctor); var ctorName = JS.Funcs.DCONSTRUCTOR; if (this.TypeInfo.Ctors.Count > 1 && ctor.Parameters.Count > 0) { var overloads = OverloadsCollection.Create(this.Emitter, ctor); ctorName = overloads.GetOverloadName(); } this.Write(ctorName); this.WriteColon(); this.BeginBlock(); this.WriteNew(); this.EmitMethodParameters(ctor.Parameters, ctor); this.WriteColon(); this.Write(name); this.WriteNewLine(); this.EndBlock(); this.WriteSemiColon(); this.WriteNewLine(); } } }
protected virtual void EmitIndexerMethod(IndexerDeclaration indexerDeclaration, IProperty prop, Accessor accessor, IMethod propAccessor, bool setter) { var isIgnore = propAccessor != null && this.Emitter.Validator.IsExternalType(propAccessor); if (!accessor.IsNull && this.Emitter.GetInline(accessor) == null && !isIgnore) { this.EnsureComma(); this.ResetLocals(); var prevMap = this.BuildLocalsMap(); var prevNamesMap = this.BuildLocalsNamesMap(); if (setter) { this.AddLocals(new ParameterDeclaration[] { new ParameterDeclaration { Name = "value" } }, accessor.Body); } else { this.AddLocals(new ParameterDeclaration[0], accessor.Body); } XmlToJsDoc.EmitComment(this, this.IndexerDeclaration, !setter); string accName = null; if (prop != null) { accName = this.Emitter.GetEntityNameFromAttr(prop, setter); if (string.IsNullOrEmpty(accName)) { var member_rr = (MemberResolveResult)this.Emitter.Resolver.ResolveNode(indexerDeclaration, this.Emitter); var overloads = OverloadsCollection.Create(this.Emitter, indexerDeclaration, setter); accName = overloads.GetOverloadName(false, Helpers.GetSetOrGet(setter), OverloadsCollection.ExcludeTypeParameterForDefinition(member_rr)); } } this.Write(accName); this.WriteColon(); this.WriteFunction(); var nm = Helpers.GetFunctionName(this.Emitter.AssemblyInfo.NamedFunctions, prop, this.Emitter, setter); if (nm != null) { this.Write(nm); } this.EmitMethodParameters(indexerDeclaration.Parameters, null, indexerDeclaration, setter); if (setter) { this.Write(", value)"); } this.WriteSpace(); var script = this.Emitter.GetScript(accessor); if (script == null) { if (YieldBlock.HasYield(accessor.Body)) { new GeneratorBlock(this.Emitter, accessor).Emit(); } else { accessor.Body.AcceptVisitor(this.Emitter); } } else { this.BeginBlock(); this.WriteLines(script); this.EndBlock(); } this.ClearLocalsMap(prevMap); this.ClearLocalsNamesMap(prevNamesMap); this.Emitter.Comma = true; } }
protected virtual bool WriteObject(string objectName, List <TypeConfigItem> members, string format, string interfaceFormat) { bool hasProperties = this.HasProperties(objectName, members); if (hasProperties && objectName != null && !this.IsObjectLiteral) { this.EnsureComma(); this.Write(objectName); this.WriteColon(); this.BeginBlock(); } bool isProperty = JS.Fields.PROPERTIES == objectName; bool isField = objectName == null; int count = 0; foreach (var member in members) { object constValue = null; bool isPrimitive = false; var primitiveExpr = member.Initializer as PrimitiveExpression; bool write = false; bool writeScript = false; if (primitiveExpr != null) { isPrimitive = true; constValue = primitiveExpr.Value; ResolveResult rr = null; if (member.VarInitializer != null) { rr = this.Emitter.Resolver.ResolveNode(member.VarInitializer, this.Emitter); } else { rr = this.Emitter.Resolver.ResolveNode(member.Entity, this.Emitter); } if (rr != null && rr.Type.Kind == TypeKind.Enum) { constValue = Helpers.GetEnumValue(this.Emitter, rr.Type, constValue); writeScript = true; } } if (constValue is RawValue) { constValue = constValue.ToString(); write = true; writeScript = false; } var isNull = member.Initializer.IsNull || member.Initializer is NullReferenceExpression; if (!isNull && !isPrimitive) { var constrr = this.Emitter.Resolver.ResolveNode(member.Initializer, this.Emitter); if (constrr != null && constrr.IsCompileTimeConstant) { isPrimitive = true; constValue = constrr.ConstantValue; if (constrr.Type.Kind == TypeKind.Enum) { constValue = Helpers.GetEnumValue(this.Emitter, constrr.Type, constrr.ConstantValue); } writeScript = true; } } var isNullable = false; if (isPrimitive && constValue is AstType) { var itype = this.Emitter.Resolver.ResolveNode((AstType)constValue, this.Emitter); if (NullableType.IsNullable(itype.Type)) { isNullable = true; } } string tpl = null; MemberResolveResult init_rr = null; if (isField && member.VarInitializer != null) { init_rr = this.Emitter.Resolver.ResolveNode(member.VarInitializer, this.Emitter) as MemberResolveResult; tpl = init_rr != null?this.Emitter.GetInline(init_rr.Member) : null; } bool written = false; if (!isNull && (!isPrimitive || constValue is AstType || tpl != null)) { string value = null; bool needContinue = false; string defValue = ""; if (!isPrimitive) { var oldWriter = this.SaveWriter(); this.NewWriter(); member.Initializer.AcceptVisitor(this.Emitter); value = this.Emitter.Output.ToString(); this.RestoreWriter(oldWriter); ResolveResult rr = null; AstType astType = null; if (member.VarInitializer != null) { rr = this.Emitter.Resolver.ResolveNode(member.VarInitializer, this.Emitter); } else { astType = member.Entity.ReturnType; rr = this.Emitter.Resolver.ResolveNode(member.Entity, this.Emitter); } constValue = Inspector.GetDefaultFieldValue(rr.Type, astType); if (rr.Type.Kind == TypeKind.Enum) { constValue = Helpers.GetEnumValue(this.Emitter, rr.Type, constValue); } isNullable = NullableType.IsNullable(rr.Type); needContinue = constValue is IType; writeScript = true; if (needContinue && !(member.Initializer is ObjectCreateExpression)) { defValue = " || " + Inspector.GetStructDefaultValue((IType)constValue, this.Emitter); } } else if (constValue is AstType) { value = isNullable ? "null" : Inspector.GetStructDefaultValue((AstType)constValue, this.Emitter); constValue = value; write = true; needContinue = !isProperty && !isNullable; } var name = member.GetName(this.Emitter); bool isValidIdentifier = Helpers.IsValidIdentifier(name); if (isProperty && isPrimitive) { constValue = "null"; if (this.IsObjectLiteral) { written = true; if (isValidIdentifier) { this.Write(string.Format("this.{0} = {1};", name, value)); } else { this.Write(string.Format("this[{0}] = {1};", AbstractEmitterBlock.ToJavaScript(name, this.Emitter), value)); } this.WriteNewLine(); } else { this.Injectors.Add(string.Format(name.StartsWith("\"") || !isValidIdentifier ? "this[{0}] = {1};" : "this.{0} = {1};", isValidIdentifier ? name : AbstractEmitterBlock.ToJavaScript(name, this.Emitter), value)); } } else { if (this.IsObjectLiteral) { written = true; if (isValidIdentifier) { this.Write(string.Format("this.{0} = {1};", name, value + defValue)); } else { this.Write(string.Format("this[{0}] = {1};", AbstractEmitterBlock.ToJavaScript(name, this.Emitter), value + defValue)); } this.WriteNewLine(); } else if (tpl != null) { if (!tpl.Contains("{0}")) { tpl = tpl + " = {0};"; } string v = null; if (!isNull && (!isPrimitive || constValue is AstType)) { v = value + defValue; } else { if (write) { v = constValue != null?constValue.ToString() : ""; } else if (writeScript) { v = AbstractEmitterBlock.ToJavaScript(constValue, this.Emitter); } else { var oldWriter = this.SaveWriter(); this.NewWriter(); member.Initializer.AcceptVisitor(this.Emitter); v = this.Emitter.Output.ToString(); this.RestoreWriter(oldWriter); } } tpl = tpl.Replace("{this}", "this").Replace("{0}", v); if (!tpl.EndsWith(";")) { tpl += ";"; } this.Injectors.Add(tpl); } else { if (isField && !isValidIdentifier) { this.Injectors.Add(string.Format("this[{0}] = {1};", name.StartsWith("\"") ? name : AbstractEmitterBlock.ToJavaScript(name, this.Emitter), value + defValue)); } else { this.Injectors.Add(string.Format(name.StartsWith("\"") ? interfaceFormat : format, name, value + defValue)); } } } if (needContinue || tpl != null) { continue; } } count++; if (written) { continue; } var mname = member.GetName(this.Emitter, true); bool isValid = Helpers.IsValidIdentifier(mname); if (!isValid) { if (this.IsObjectLiteral) { mname = "[" + AbstractEmitterBlock.ToJavaScript(mname, this.Emitter) + "]"; } else { mname = AbstractEmitterBlock.ToJavaScript(mname, this.Emitter); } } if (this.IsObjectLiteral) { this.WriteThis(); if (isValid) { this.WriteDot(); } this.Write(mname); this.Write(" = "); } else { this.EnsureComma(); XmlToJsDoc.EmitComment(this, member.Entity); this.Write(mname); this.WriteColon(); } bool close = false; if (isProperty) { var member_rr = this.Emitter.Resolver.ResolveNode(member.Entity, this.Emitter) as MemberResolveResult; if (member_rr != null) { var setterName = Helpers.GetPropertyRef(member_rr.Member, this.Emitter, true); var getterName = Helpers.GetPropertyRef(member_rr.Member, this.Emitter, false); string names = null; if (setterName != ("set" + mname) && getterName != ("get" + mname)) { names = string.Format("{{ getter:'{0}', setter: '{1}'", getterName, setterName); } else if (setterName != ("set" + mname)) { names = string.Format("{{ setter: '{0}'", setterName); } else if (getterName != ("get" + mname)) { names = string.Format("{{ getter: '{0}'", getterName); } if (names != null) { this.Write(names); this.Write(", value: "); close = true; } } } if (constValue is AstType) { if (isNullable) { this.Write("null"); } else { this.Write(Inspector.GetStructDefaultValue((AstType)constValue, this.Emitter)); } } else if (constValue is IType) { if (isNullable) { this.Write("null"); } else { this.Write(Inspector.GetStructDefaultValue((IType)constValue, this.Emitter)); } } else if (write) { this.Write(constValue); } else if (writeScript) { this.WriteScript(constValue); } else { member.Initializer.AcceptVisitor(this.Emitter); } if (close) { this.Write(" }"); } if (this.IsObjectLiteral) { this.WriteSemiColon(true); } this.Emitter.Comma = true; } if (count > 0 && objectName != null) { this.WriteNewLine(); this.EndBlock(); } return(count > 0); }
protected void VisitMethodDeclaration(MethodDeclaration methodDeclaration) { foreach (var attrSection in methodDeclaration.Attributes) { foreach (var attr in attrSection.Attributes) { var rr = this.Emitter.Resolver.ResolveNode(attr.Type, this.Emitter); if (rr.Type.FullName == "Bridge.InitAttribute") { int initPosition = 0; if (attr.HasArgumentList) { if (attr.Arguments.Any()) { var argExpr = attr.Arguments.First(); var argrr = this.Emitter.Resolver.ResolveNode(argExpr, this.Emitter); if (argrr.ConstantValue is int) { initPosition = (int)argrr.ConstantValue; } } } if (initPosition == 1) // Before { return; } } } } this.EnsureComma(); this.ResetLocals(); var prevMap = this.BuildLocalsMap(); var prevNamesMap = this.BuildLocalsNamesMap(); this.AddLocals(methodDeclaration.Parameters, methodDeclaration.Body); var typeDef = this.Emitter.GetTypeDefinition(); var overloads = OverloadsCollection.Create(this.Emitter, methodDeclaration); XmlToJsDoc.EmitComment(this, this.MethodDeclaration); if (overloads.HasOverloads) { string name = overloads.GetOverloadName(); this.Write(name); } else { this.Write(this.Emitter.GetEntityName(methodDeclaration)); } this.WriteColon(); if (methodDeclaration.TypeParameters.Count > 0) { this.WriteFunction(); this.EmitTypeParameters(methodDeclaration.TypeParameters, methodDeclaration); this.WriteSpace(); this.BeginBlock(); this.WriteReturn(true); this.Write("Bridge.fn.bind(this, "); } this.WriteFunction(); this.EmitMethodParameters(methodDeclaration.Parameters, methodDeclaration); this.WriteSpace(); var script = this.Emitter.GetScript(methodDeclaration); if (script == null) { if (methodDeclaration.HasModifier(Modifiers.Async)) { new AsyncBlock(this.Emitter, methodDeclaration).Emit(); } else { methodDeclaration.Body.AcceptVisitor(this.Emitter); } } else { this.BeginBlock(); foreach (var line in script) { this.Write(line); this.WriteNewLine(); } this.EndBlock(); } if (methodDeclaration.TypeParameters.Count > 0) { this.Write(");"); this.WriteNewLine(); this.EndBlock(); } this.ClearLocalsMap(prevMap); this.ClearLocalsNamesMap(prevNamesMap); this.Emitter.Comma = true; }
private void EmitCtorForInstantiableClass() { this.EmitInitMembers(); var baseType = this.Emitter.GetBaseTypeDefinition(); var typeDef = this.Emitter.GetTypeDefinition(); if (typeDef.IsValueType) { if (!typeDef.IsEnum) { this.TypeInfo.Ctors.Add(new ConstructorDeclaration { Modifiers = Modifiers.Public, Body = new BlockStatement() }); } } else { if (TypeInfo.Ctors.Count == 0 && isInitExists_) { this.TypeInfo.Ctors.Add(new ConstructorDeclaration { Modifiers = Modifiers.Public, Body = new BlockStatement() }); } } if (TypeInfo.Ctors.Count > 0) { var ctorInfos = this.TypeInfo.Ctors.Select(i => new CtorInfo(i, GetCtorName(i))).ToList(); ctorInfos.Sort((x, y) => x.ConstructorName.CompareTo(y.ConstructorName)); this.PushWriter("{0}"); this.Outdent(); foreach (var info in ctorInfos) { var ctor = info.ConstructorDeclaration; var ctorName = info.ConstructorName; TransformCtx.CurClassOtherMethodNames.Add(new TransformCtx.MethodInfo() { Name = info.CtorName, IsCtor = true }); if (info != ctorInfos.First()) { this.WriteNewLine(); } this.ResetLocals(); var prevMap = this.BuildLocalsMap(); var prevNamesMap = this.BuildLocalsNamesMap(); this.AddLocals(ctor.Parameters, ctor.Body); XmlToJsDoc.EmitComment(this, ctor); this.Write(info.CtorName); this.WriteEqualsSign(); this.WriteFunction(); this.EmitMethodParameters(ctor.Parameters, ctor); this.WriteSpace(); this.BeginFunctionBlock(); var requireNewLine = false; if (baseType != null && !this.Emitter.Validator.IsIgnoreType(baseType) || (ctor.Initializer != null && ctor.Initializer.ConstructorInitializerType == ConstructorInitializerType.This)) { if (requireNewLine) { this.WriteNewLine(); } this.EmitBaseConstructor(ctor, ctorName); requireNewLine = true; } else { if (isInitExists_) { this.Write("__init__(this)"); requireNewLine = true; } } if (ctor.Body.HasChildren) { var beginPosition = this.Emitter.Output.Length; if (requireNewLine) { this.WriteNewLine(); } this.ConvertParamsToReferences(ctor.Parameters); ctor.Body.AcceptChildren(this.Emitter); if (!this.Emitter.IsAsync) { this.EmitTempVars(beginPosition, true); } } else { if (requireNewLine) { this.WriteNewLine(); } } this.EndFunctionBlock(); this.ClearLocalsMap(prevMap); this.ClearLocalsNamesMap(prevNamesMap); } this.Indent(); string ctors = this.PopWriter(true); TransformCtx.CurClassOtherMethods.Add(ctors); this.EnsureComma(); this.Write("ctor".Ident()); this.WriteEqualsSign(); if (this.TypeInfo.Ctors.Count > 1) { this.WriteOpenBrace(); this.Indent(); this.WriteNewLine(); } foreach (var info in ctorInfos) { if (info != ctorInfos.First()) { this.WriteComma(true); } this.Write(info.CtorName); } if (this.TypeInfo.Ctors.Count > 1) { this.Outdent(); this.WriteNewLine(); this.WriteCloseBrace(); } this.Emitter.Comma = true; } }
protected virtual void EmitCtorForInstantiableClass() { var baseType = this.Emitter.GetBaseTypeDefinition(); var typeDef = this.Emitter.GetTypeDefinition(); var isObjectLiteral = this.Emitter.Validator.IsObjectLiteral(typeDef); var isPlainMode = this.Emitter.Validator.GetObjectCreateMode(typeDef) == 0; var ctorWrappers = isObjectLiteral ? new string[0] : this.EmitInitMembers().ToArray(); if (!this.TypeInfo.HasRealInstantiable(this.Emitter) && ctorWrappers.Length == 0) { return; } if (isObjectLiteral && isPlainMode) { return; } bool forceDefCtor = isObjectLiteral && this.Emitter.Validator.GetObjectCreateMode(typeDef) == 1 && this.TypeInfo.Ctors.Count == 0; if (typeDef.IsValueType || forceDefCtor || (this.TypeInfo.Ctors.Count == 0 && ctorWrappers.Length > 0)) { this.TypeInfo.Ctors.Add(new ConstructorDeclaration { Modifiers = Modifiers.Public, Body = new BlockStatement() }); } foreach (var ctor in this.TypeInfo.Ctors) { this.EnsureComma(); this.ResetLocals(); var prevMap = this.BuildLocalsMap(); var prevNamesMap = this.BuildLocalsNamesMap(); this.AddLocals(ctor.Parameters, ctor.Body); var ctorName = JS.Funcs.CONSTRUCTOR; if (this.TypeInfo.Ctors.Count > 1 && ctor.Parameters.Count > 0) { var overloads = OverloadsCollection.Create(this.Emitter, ctor); ctorName = overloads.GetOverloadName(); } XmlToJsDoc.EmitComment(this, ctor); this.Write(ctorName); this.WriteColon(); this.WriteFunction(); int pos = this.Emitter.Output.Length; this.EmitMethodParameters(ctor.Parameters, null, ctor); var ctorParams = this.Emitter.Output.ToString().Substring(pos); this.WriteSpace(); this.BeginBlock(); var len = this.Emitter.Output.Length; var requireNewLine = false; var noThisInvocation = ctor.Initializer == null || ctor.Initializer.IsNull || ctor.Initializer.ConstructorInitializerType == ConstructorInitializerType.Base; IWriterInfo oldWriter = null; if (ctorWrappers.Length > 0 && noThisInvocation) { oldWriter = this.SaveWriter(); this.NewWriter(); } this.ConvertParamsToReferences(ctor.Parameters); if (len != this.Emitter.Output.Length) { requireNewLine = true; } if (isObjectLiteral) { if (requireNewLine) { this.WriteNewLine(); } this.Write("var " + JS.Vars.D_THIS + " = "); var isBaseObjectLiteral = baseType != null && this.Emitter.Validator.IsObjectLiteral(baseType); if (isBaseObjectLiteral && baseType != null && (!this.Emitter.Validator.IsExternalType(baseType) || this.Emitter.Validator.IsBridgeClass(baseType)) || (ctor.Initializer != null && ctor.Initializer.ConstructorInitializerType == ConstructorInitializerType.This)) { this.EmitBaseConstructor(ctor, ctorName, true); } else if (isBaseObjectLiteral && baseType != null && ctor.Initializer != null && ctor.Initializer.ConstructorInitializerType == ConstructorInitializerType.Base) { this.EmitExternalBaseCtor(ctor, ref requireNewLine); } else { this.Write("{};"); } this.WriteNewLine(); string name = this.Emitter.Validator.GetCustomTypeName(typeDef, this.Emitter); if (name.IsEmpty()) { name = BridgeTypes.DefinitionToJsName(this.TypeInfo.Type, this.Emitter); } this.Write(JS.Vars.D_THIS + "." + JS.Funcs.GET_TYPE + " = function() { return " + name + "; };"); this.WriteNewLine(); this.Write("(function()"); this.BeginBlock(); requireNewLine = false; } if (noThisInvocation) { if (requireNewLine) { this.WriteNewLine(); } if (isObjectLiteral) { var fieldBlock = new FieldBlock(this.Emitter, this.TypeInfo, false, false, true); fieldBlock.Emit(); var properties = this.TypeInfo.InstanceProperties; var names = new List <string>(properties.Keys); foreach (var name in names) { var props = properties[name]; foreach (var prop in props) { var p = prop as PropertyDeclaration; if (p != null) { if (p.Getter.Body.IsNull && p.Setter.Body.IsNull) { continue; } this.Write(JS.Types.Object.DEFINEPROPERTY); this.WriteOpenParentheses(); this.Write("this, "); this.WriteScript(OverloadsCollection.Create(this.Emitter, p).GetOverloadName()); this.WriteComma(); this.Emitter.Comma = false; this.BeginBlock(); var block = new VisitorPropertyBlock(this.Emitter, p); block.EmitPropertyMethod(p, p.Getter, false, true); block.EmitPropertyMethod(p, p.Setter, true, true); this.EnsureComma(true); this.Write(JS.Fields.ENUMERABLE + ": true"); this.WriteNewLine(); this.EndBlock(); this.WriteCloseParentheses(); this.Write(";"); this.WriteNewLine(); } } } } else { this.Write("this." + JS.Funcs.INITIALIZE + "();"); requireNewLine = true; } } if (!isObjectLiteral) { if (baseType != null && (!this.Emitter.Validator.IsExternalType(baseType) || this.Emitter.Validator.IsBridgeClass(baseType)) || (ctor.Initializer != null && ctor.Initializer.ConstructorInitializerType == ConstructorInitializerType.This)) { if (requireNewLine) { this.WriteNewLine(); requireNewLine = false; } this.EmitBaseConstructor(ctor, ctorName, false); } else if (baseType != null && ctor.Initializer != null && ctor.Initializer.ConstructorInitializerType == ConstructorInitializerType.Base) { this.EmitExternalBaseCtor(ctor, ref requireNewLine); } } var script = this.Emitter.GetScript(ctor); if (script == null) { if (ctor.Body.HasChildren) { var beginPosition = this.Emitter.Output.Length; if (requireNewLine) { this.WriteNewLine(); } ctor.Body.AcceptChildren(this.Emitter); if (!this.Emitter.IsAsync) { this.EmitTempVars(beginPosition, true); } } else if (requireNewLine) { this.WriteNewLine(); } } else { if (requireNewLine) { this.WriteNewLine(); } this.WriteLines(script); } if (oldWriter != null) { this.WrapBody(oldWriter, ctorWrappers, ctorParams); } if (isObjectLiteral) { if (requireNewLine) { this.WriteNewLine(); } this.EndBlock(); this.Write(")." + JS.Funcs.CALL + "(" + JS.Vars.D_THIS + ");"); this.WriteNewLine(); this.Write("return " + JS.Vars.D_THIS + ";"); this.WriteNewLine(); } this.EndBlock(); this.Emitter.Comma = true; this.ClearLocalsMap(prevMap); this.ClearLocalsNamesMap(prevNamesMap); } }
protected virtual void EmitPropertyMethod(PropertyDeclaration propertyDeclaration, Accessor accessor, bool setter) { var memberResult = this.Emitter.Resolver.ResolveNode(propertyDeclaration, this.Emitter) as MemberResolveResult; if (memberResult != null && (memberResult.Member.Attributes.Any(a => a.AttributeType.FullName == "Bridge.FieldPropertyAttribute" || a.AttributeType.FullName == "Bridge.IgnoreAttribute" || a.AttributeType.FullName == "Bridge.ExternalAttribute") || (propertyDeclaration.Getter.IsNull && propertyDeclaration.Setter.IsNull))) { return; } if (!accessor.IsNull && this.Emitter.GetInline(accessor) == null) { this.EnsureComma(); this.ResetLocals(); var prevMap = this.BuildLocalsMap(); var prevNamesMap = this.BuildLocalsNamesMap(); if (setter) { this.AddLocals(new ParameterDeclaration[] { new ParameterDeclaration { Name = "value" } }, accessor.Body); } XmlToJsDoc.EmitComment(this, this.PropertyDeclaration); var overloads = OverloadsCollection.Create(this.Emitter, propertyDeclaration, setter); string name = overloads.GetOverloadName(); this.Write((setter ? "set" : "get") + name); this.WriteColon(); this.WriteFunction(); this.WriteOpenParentheses(); this.Write(setter ? "value" : ""); this.WriteCloseParentheses(); this.WriteSpace(); var script = this.Emitter.GetScript(accessor); if (script == null) { accessor.Body.AcceptVisitor(this.Emitter); } else { this.BeginBlock(); foreach (var line in script) { this.Write(line); this.WriteNewLine(); } this.EndBlock(); } this.ClearLocalsMap(prevMap); this.ClearLocalsNamesMap(prevNamesMap); this.Emitter.Comma = true; } }
protected void VisitMethodDeclaration(MethodDeclaration methodDeclaration) { foreach (var attrSection in methodDeclaration.Attributes) { foreach (var attr in attrSection.Attributes) { var rr = Emitter.Resolver.ResolveNode(attr.Type); if (rr.Type.FullName == "H5.ExternalAttribute") { return; } else if (rr.Type.FullName == "H5.InitAttribute") { InitPosition initPosition = InitPosition.After; if (attr.HasArgumentList) { if (attr.Arguments.Any()) { var argExpr = attr.Arguments.First(); var argrr = Emitter.Resolver.ResolveNode(argExpr); if (argrr.ConstantValue is int) { initPosition = (InitPosition)argrr.ConstantValue; } } } if (initPosition > 0) { return; } } } } EnsureComma(); ResetLocals(); var prevMap = BuildLocalsMap(); var prevNamesMap = BuildLocalsNamesMap(); AddLocals(methodDeclaration.Parameters, methodDeclaration.Body); var overloads = OverloadsCollection.Create(Emitter, methodDeclaration); XmlToJsDoc.EmitComment(this, MethodDeclaration); var isEntryPoint = Helpers.IsEntryPointMethod(Emitter, MethodDeclaration); var member_rr = (MemberResolveResult)Emitter.Resolver.ResolveNode(MethodDeclaration); string name = overloads.GetOverloadName(false, null, excludeTypeOnly: OverloadsCollection.ExcludeTypeParameterForDefinition(member_rr)); if (isEntryPoint) { Write(JS.Funcs.ENTRY_POINT_NAME); } else { Write(name); } WriteColon(); WriteFunction(); if (isEntryPoint) { Write(name); WriteSpace(); } else { var nm = Helpers.GetFunctionName(Emitter.AssemblyInfo.NamedFunctions, member_rr.Member, Emitter); if (nm != null) { Write(nm); WriteSpace(); } } EmitMethodParameters(methodDeclaration.Parameters, methodDeclaration.TypeParameters.Count > 0 && Helpers.IsIgnoreGeneric(methodDeclaration, Emitter) ? null : methodDeclaration.TypeParameters, methodDeclaration); WriteSpace(); var script = Emitter.GetScript(methodDeclaration); if (script == null) { if (YieldBlock.HasYield(methodDeclaration.Body)) { new GeneratorBlock(Emitter, methodDeclaration).Emit(); } else if (methodDeclaration.HasModifier(Modifiers.Async) || AsyncBlock.HasGoto(methodDeclaration.Body)) { new AsyncBlock(Emitter, methodDeclaration).Emit(); } else { methodDeclaration.Body.AcceptVisitor(Emitter); } } else { BeginBlock(); WriteLines(script); EndBlock(); } ClearLocalsMap(prevMap); ClearLocalsNamesMap(prevNamesMap); Emitter.Comma = true; }
protected virtual void EmitIndexerMethod(IndexerDeclaration indexerDeclaration, IProperty prop, Accessor accessor, IMethod propAccessor, bool setter) { var isIgnore = propAccessor != null && this.Emitter.Validator.IsIgnoreType(propAccessor); if (!accessor.IsNull && this.Emitter.GetInline(accessor) == null && !isIgnore) { this.EnsureComma(); this.ResetLocals(); var prevMap = this.BuildLocalsMap(); var prevNamesMap = this.BuildLocalsNamesMap(); if (setter) { this.AddLocals(new ParameterDeclaration[] { new ParameterDeclaration { Name = "value" } }, accessor.Body); } XmlToJsDoc.EmitComment(this, this.IndexerDeclaration); string accName = null; if (prop != null) { accName = this.Emitter.GetEntityNameFromAttr(prop, setter); if (string.IsNullOrEmpty(accName)) { var overloads = OverloadsCollection.Create(this.Emitter, indexerDeclaration, setter); accName = (setter ? "set" : "get") + overloads.GetOverloadName(); } } this.Write(accName); this.WriteColon(); this.WriteFunction(); this.EmitMethodParameters(indexerDeclaration.Parameters, null, indexerDeclaration, setter); if (setter) { this.Write(", value)"); } this.WriteSpace(); var script = this.Emitter.GetScript(accessor); if (script == null) { accessor.Body.AcceptVisitor(this.Emitter); } else { this.BeginBlock(); foreach (var line in script) { this.Write(line); this.WriteNewLine(); } this.EndBlock(); } this.ClearLocalsMap(prevMap); this.ClearLocalsNamesMap(prevNamesMap); this.Emitter.Comma = true; } }
protected virtual void EmitCtorForInstantiableClass() { this.EmitInitMembers(); if (!this.TypeInfo.HasInstantiable || this.Emitter.Plugins.HasConstructorInjectors(this)) { return; } var baseType = this.Emitter.GetBaseTypeDefinition(); var typeDef = this.Emitter.GetTypeDefinition(); if (typeDef.IsValueType) { this.TypeInfo.Ctors.Add(new ConstructorDeclaration { Modifiers = Modifiers.Public, Body = new BlockStatement() }); } foreach (var ctor in this.TypeInfo.Ctors) { this.EnsureComma(); this.ResetLocals(); var prevMap = this.BuildLocalsMap(); var prevNamesMap = this.BuildLocalsNamesMap(); this.AddLocals(ctor.Parameters, ctor.Body); var ctorName = "constructor"; if (this.TypeInfo.Ctors.Count > 1 && ctor.Parameters.Count > 0) { var overloads = OverloadsCollection.Create(this.Emitter, ctor); ctorName = overloads.GetOverloadName(); } XmlToJsDoc.EmitComment(this, ctor); this.Write(ctorName); this.WriteColon(); this.WriteFunction(); this.EmitMethodParameters(ctor.Parameters, ctor); this.WriteSpace(); this.BeginBlock(); var requireNewLine = false; if (baseType != null && (!this.Emitter.Validator.IsIgnoreType(baseType) || this.Emitter.Validator.IsBridgeClass(baseType)) || (ctor.Initializer != null && ctor.Initializer.ConstructorInitializerType == ConstructorInitializerType.This)) { if (requireNewLine) { this.WriteNewLine(); } this.EmitBaseConstructor(ctor, ctorName); requireNewLine = true; } var script = this.Emitter.GetScript(ctor); if (script == null) { if (ctor.Body.HasChildren) { var beginPosition = this.Emitter.Output.Length; if (requireNewLine) { this.WriteNewLine(); } this.ConvertParamsToReferences(ctor.Parameters); ctor.Body.AcceptChildren(this.Emitter); if (!this.Emitter.IsAsync) { this.EmitTempVars(beginPosition); } } } else { if (requireNewLine) { this.WriteNewLine(); } foreach (var line in script) { this.Write(line); this.WriteNewLine(); } } this.EndBlock(); this.Emitter.Comma = true; this.ClearLocalsMap(prevMap); this.ClearLocalsNamesMap(prevNamesMap); } }
protected override void DoEmit() { XmlToJsDoc.EmitComment(this, this.Emitter.Translator.EmitNode); string globalTarget = BridgeTypes.GetGlobalTarget(this.TypeInfo.Type.GetDefinition(), this.TypeInfo.TypeDeclaration); if (globalTarget != null) { this.CheckGlobalClass(); this.Emitter.NamedFunctions = new Dictionary <string, string>(); this.WriteTopInitMethods(); this.Write(JS.Types.Bridge.APPLY); this.WriteOpenParentheses(); this.Write(globalTarget); this.Write(", "); this.BeginBlock(); new MethodBlock(this.Emitter, this.TypeInfo, true).Emit(); this.EmitMetadata(); this.WriteNewLine(); this.EndBlock(); this.WriteCloseParentheses(); this.WriteSemiColon(); this.EmitAnonymousTypes(); this.EmitNamedFunctions(); this.WriteAfterInitMethods(); this.WriteNewLine(); } else { this.EmitClassHeader(); this.Emitter.NamedFunctions = new Dictionary <string, string>(); if (this.TypeInfo.TypeDeclaration.ClassType != ClassType.Interface) { MethodDeclaration entryPoint = null; if (this.TypeInfo.StaticMethods.Any(group => { return(group.Value.Any(method => { var result = Helpers.IsEntryPointMethod(this.Emitter, method); if (result) { entryPoint = method; } return result; })); })) { if (!entryPoint.Body.IsNull) { this.Emitter.VisitMethodDeclaration(entryPoint); } } this.EmitStaticBlock(); this.EmitInstantiableBlock(); } this.EmitClassEnd(); } }
protected void VisitMethodDeclaration(MethodDeclaration methodDeclaration) { foreach (var attrSection in methodDeclaration.Attributes) { foreach (var attr in attrSection.Attributes) { var rr = this.Emitter.Resolver.ResolveNode(attr.Type, this.Emitter); if (rr.Type.FullName == "Bridge.ExternalAttribute") { return; } else if (rr.Type.FullName == "Bridge.InitAttribute") { int initPosition = 0; if (attr.HasArgumentList) { if (attr.Arguments.Any()) { var argExpr = attr.Arguments.First(); var argrr = this.Emitter.Resolver.ResolveNode(argExpr, this.Emitter); if (argrr.ConstantValue is int) { initPosition = (int)argrr.ConstantValue; } } } if (initPosition > 0) { return; } } } } this.EnsureComma(); this.ResetLocals(); var prevMap = this.BuildLocalsMap(); var prevNamesMap = this.BuildLocalsNamesMap(); this.AddLocals(methodDeclaration.Parameters, methodDeclaration.Body); var overloads = OverloadsCollection.Create(this.Emitter, methodDeclaration); XmlToJsDoc.EmitComment(this, this.MethodDeclaration); string name = overloads.GetOverloadName(false, null, true); if (Helpers.IsEntryPointMethod(this.Emitter, methodDeclaration)) { name = JS.Fields.MAIN; } this.Write(name); this.WriteColon(); this.WriteFunction(); this.EmitMethodParameters(methodDeclaration.Parameters, methodDeclaration.TypeParameters.Count > 0 && Helpers.IsIgnoreGeneric(methodDeclaration, this.Emitter) ? null : methodDeclaration.TypeParameters, methodDeclaration); this.WriteSpace(); var script = this.Emitter.GetScript(methodDeclaration); if (script == null) { if (methodDeclaration.HasModifier(Modifiers.Async)) { new AsyncBlock(this.Emitter, methodDeclaration).Emit(); } else { methodDeclaration.Body.AcceptVisitor(this.Emitter); } } else { this.BeginBlock(); this.WriteLines(script); this.EndBlock(); } this.ClearLocalsMap(prevMap); this.ClearLocalsNamesMap(prevNamesMap); this.Emitter.Comma = true; }