Beispiel #1
0
        /// <summary>Starts creating this class.</summary>
        /// <param name="name">The name of the class.</param>
        /// <param name="script">The parent script this class belongs to.</param>
        /// <param name="baseType">The type that this class will derive from.</param>
        public void StartType(string name, NitroCode script, Type baseType)
        {
            BaseType = baseType;
            Script   = script;
            TypeAttributes attribs = IsPublic?TypeAttributes.Public:TypeAttributes.NotPublic;

            Builder = script.Builder.DefineType(name, attribs, baseType);
        }
		public CompiledMethod(CompiledClass parent,string name,BracketFragment parameterBlock,BracketFragment codeBlock,TypeFragment retType,bool isPublic){
			Name=name;
			Parent=parent;
			CodeBlock=codeBlock;
			Script=Parent.Script;
			ParameterBlock=parameterBlock;
			
			Type returnType=null;
			if(retType!=null){
				returnType=retType.FindType(Script);
				if(returnType==null){
					Error("Type '"+retType.Value+"' was not found.");
				}
			}
			string methodName=Name;
			MethodAttributes attrib=isPublic?MethodAttributes.Public:MethodAttributes.Private;
			if(methodName=="new"){
				methodName=".ctor";
				attrib|=MethodAttributes.HideBySig|MethodAttributes.SpecialName|MethodAttributes.RTSpecialName;
			}
			// Does the parent base type define this method?
			// If so, use it's name.
			Type baseType=Parent.Builder.BaseType;
			
			// Parse the parameter set right away:
			ParseParameters();
			
			MethodInfo mInfo=Types.GetOverload(baseType.GetMethods(),Name,ParameterTypes,true);
			
			if(mInfo!=null){
				methodName=mInfo.Name;
				attrib|=MethodAttributes.Virtual|MethodAttributes.HideBySig;//|MethodAttributes.NewSlot;
			}
			
			bool isVoid=Types.IsVoid(returnType);
			
			if(isVoid){
				returnType=typeof(void);
			}
			
			Builder=Parent.Builder.DefineMethod(
				methodName,
				attrib,
				returnType,
				null
			);
			
			ApplyParameters();
			
			ILStream=new NitroIL(Builder.GetILGenerator());
			EndOfMethod=ILStream.DefineLabel();
			if(!isVoid){
				ReturnBay=ILStream.DeclareLocal(returnType);
			}
		}
Beispiel #3
0
        /// <summary>Attempts to find the type named here and resolve it to a system type.</summary>
        /// <param name="script">The script that should also be checked for types and acts as the security domain.</param>
        /// <returns>A system type if the type could be resolved and allowed successfully; null otherwise.</returns>
        public Type FindType(NitroCode script)
        {
            string name = Value;

            if (GenericSet != null)
            {
                name += "`" + GenericSet.Length;
            }
            Type baseType = script.GetType(name);

            if (baseType == null)
            {
                CompiledClass cClass = script.GetClass(Value);
                if (cClass == null)
                {
                    return(null);
                }
                baseType = cClass.GetAsType();
            }
            if (GenericSet != null)
            {
                if (!baseType.IsGenericTypeDefinition)
                {
                    Error(Value + " is not a generic type.");
                }
                Type[] genericTypes = new Type[GenericSet.Length];
                for (int i = GenericSet.Length - 1; i >= 0; i--)
                {
                    if ((genericTypes[i] = GenericSet[i].FindType(script)) == null)
                    {
                        return(null);
                    }
                }
                baseType = baseType.MakeGenericType(genericTypes);
            }
            if (IsArray)
            {
                if (Dimensions == 1)
                {
                    baseType = baseType.MakeArrayType();
                }
                else
                {
                    baseType = baseType.MakeArrayType(Dimensions);
                }
            }
            return(baseType);
        }
		/// <summary>Starts creating this class.</summary>
		/// <param name="name">The name of the class.</param>
		/// <param name="script">The parent script this class belongs to.</param>
		/// <param name="baseType">The type that this class will derive from.</param>
		public void StartType(string name,NitroCode script,Type baseType){
			BaseType=baseType;
			Script=script;
			TypeAttributes attribs=IsPublic?TypeAttributes.Public:TypeAttributes.NotPublic;
			Builder=script.Builder.DefineType(name,attribs,baseType);
		}
		public CompiledClass(CodeFragment classFragment,NitroCode script,string name,Type baseType,bool isPublic){
			IsPublic=isPublic;
			StartType(name,script,baseType);
			ClassFragment=classFragment;
		}
        public CompiledMethod(CompiledClass parent, string name, BracketFragment parameterBlock, BracketFragment codeBlock, TypeFragment retType, bool isPublic)
        {
            Name           = name;
            Parent         = parent;
            CodeBlock      = codeBlock;
            Script         = Parent.Script;
            ParameterBlock = parameterBlock;

            Type returnType = null;

            if (retType != null)
            {
                returnType = retType.FindType(Script);
                if (returnType == null)
                {
                    Error("Type '" + retType.Value + "' was not found.");
                }
            }
            string           methodName = Name;
            MethodAttributes attrib     = isPublic?MethodAttributes.Public:MethodAttributes.Private;

            if (methodName == "new")
            {
                methodName = ".ctor";
                attrib    |= MethodAttributes.HideBySig | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName;
            }
            // Does the parent base type define this method?
            // If so, use it's name.
            Type baseType = Parent.Builder.BaseType;

            // Parse the parameter set right away:
            ParseParameters();

            MethodInfo mInfo = Types.GetOverload(baseType.GetMethods(), Name, ParameterTypes, true);

            if (mInfo != null)
            {
                methodName = mInfo.Name;
                attrib    |= MethodAttributes.Virtual | MethodAttributes.HideBySig;         //|MethodAttributes.NewSlot;
            }

            bool isVoid = Types.IsVoid(returnType);

            if (isVoid)
            {
                returnType = typeof(void);
            }

            Builder = Parent.Builder.DefineMethod(
                methodName,
                attrib,
                returnType,
                null
                );

            ApplyParameters();

            ILStream    = new NitroIL(Builder.GetILGenerator());
            EndOfMethod = ILStream.DefineLabel();
            if (!isVoid)
            {
                ReturnBay = ILStream.DeclareLocal(returnType);
            }
        }
		/// <summary>Attempts to compile the code then run OnWindowLoaded. It's only successful if there are no nulls in the code buffer.</summary>
		/// <returns>Returns false if we're still waiting on code to download.</returns>
		public bool TryCompile(){
			
			if(CodeBuffer==null){
				return true;
			}
			
			if(CodeBuffer.Length==0){
				CodeBuffer=null;
				return true;
			}
			
			for(int i=0;i<CodeBuffer.Length;i++){
				if(CodeBuffer[i]==null){
					return false;
				}
			}
			
			#if !NoNitroRuntime
			// Iframe security check - can code from this domain run at all?
			// We have the Nitro runtime so it could run unwanted code.
			if(window.parent!=null && location!=null && !location.fullAccess){
				
				// It's an iframe to some unsafe location. We must have a security domain for this to be allowed at all.
				if(SecurityDomain==null || !SecurityDomain.AllowAccess(location.Protocol,location.host,location.ToString())){
					Wrench.Log.Add("Warning: blocked Nitro on a webpage - You must use a security domain to allow this. See http://help.kulestar.com/nitro-security/ for more.");
					return true;
				}
				
			}
			#endif
			
			// Good to go!
			FinishedParsing=false;
			string codeToCompile="";
			
			for(int i=0;i<CodeBuffer.Length;i++){
				codeToCompile+=CodeBuffer[i]+"\n";
			}
			
			CodeBuffer=null;
			
			if(!AotDocument){
				CodeInstance=NitroCache.TryGetScript(codeToCompile);
			}
			
			try{
				#if !NoNitroRuntime
				string aotFile=null;
				string aotAssemblyName=null;
				
				if(AotDocument){
					aotFile="";
					string[] pieces=ScriptLocation.Split('.');
					for(int i=0;i<pieces.Length-1;i++){
						if(i!=0){
							aotFile+=".";
						}
						aotFile+=pieces[i];
					}
					aotFile+="-nitro-aot.dll";
					aotAssemblyName=NitroCache.GetCodeSeed(codeToCompile)+".ntro";
				}
			
				if(CodeInstance==null){
					
					NitroCode script=new NitroCode(codeToCompile,UI.BaseCodeType,SecurityDomain,aotFile,aotAssemblyName);
					if(AotDocument){
						// Internally the constructor will write it to the named file.
						return true;
					}
					CodeInstance=(UICode)script.Instance();
					CodeInstance.BaseScript=script;
				}
				#endif
				
				if(CodeInstance!=null){
					CodeInstance.document=this;
					CodeInstance.window=window;
					CodeInstance.OnWindowLoaded();
					CodeInstance.Start();
					// Standard method that must be called.
					// Any code outside of functions gets dropped in here:
					CodeInstance.OnScriptReady();
				}
			}catch(Exception e){
				string scriptLocation=ScriptLocation;
				
				if(string.IsNullOrEmpty(scriptLocation)){
					// Use document.location instead:
					scriptLocation=location.ToString();
				}
				
				if(!string.IsNullOrEmpty(scriptLocation)){
					scriptLocation=" (At "+scriptLocation+")";
				}
				
				Wrench.Log.Add("Script error"+scriptLocation+": "+e);
			}
			return true;
		}
Beispiel #8
0
 public CompiledClass(CodeFragment classFragment, NitroCode script, string name, Type baseType, bool isPublic)
 {
     IsPublic = isPublic;
     StartType(name, script, baseType);
     ClassFragment = classFragment;
 }
		/// <summary>Attempts to find the type named here and resolve it to a system type.</summary>
		/// <param name="script">The script that should also be checked for types and acts as the security domain.</param>
		/// <returns>A system type if the type could be resolved and allowed successfully; null otherwise.</returns>
		public Type FindType(NitroCode script){
			string name=Value;
			if(GenericSet!=null){
				name+="`"+GenericSet.Length;
			}
			Type baseType=script.GetType(name);
			if(baseType==null){
				CompiledClass cClass=script.GetClass(Value);
				if(cClass==null){
					return null;
				}
				baseType=cClass.GetAsType();
			}
			if(GenericSet!=null){
				if(!baseType.IsGenericTypeDefinition){
					Error(Value+" is not a generic type.");
				}
				Type[] genericTypes=new Type[GenericSet.Length];
				for(int i=GenericSet.Length-1;i>=0;i--){
					if((genericTypes[i]=GenericSet[i].FindType(script))==null){
						return null;
					}
				}
				baseType=baseType.MakeGenericType(genericTypes);
			}
			if(IsArray){
				if(Dimensions==1){
					baseType=baseType.MakeArrayType();
				}else{
					baseType=baseType.MakeArrayType(Dimensions);
				}
			}
			return baseType;
		}