/// <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 #2
0
        protected override void Compile(string codeToCompile)
        {
            if (!AotDocument)
            {
                CodeInstance = NitroCache.TryGetScript(codeToCompile);
            }

            try{
                                #if !NoNitroRuntime
                string aotFile         = null;
                string aotAssemblyName = null;

                if (AotDocument)
                {
                    aotFile = "";
                    string[] pieces = htmlDocument.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;
                    }
                    CodeInstance            = (UICode)script.Instance();
                    CodeInstance.BaseScript = script;
                }
                                #endif

                if (CodeInstance != null)
                {
                    CodeInstance.document = htmlDocument;
                    CodeInstance.window   = htmlDocument.window;

                    // Trigger an event to say Nitro is about to start:
                    Dom.Event e = new Dom.Event("scriptenginestart");
                    htmlDocument.dispatchEvent(e);


                    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 = htmlDocument.ScriptLocation;

                if (string.IsNullOrEmpty(scriptLocation))
                {
                    // Use document.basepath instead:
                    scriptLocation = Document.basepath.ToString();
                }

                if (!string.IsNullOrEmpty(scriptLocation))
                {
                    scriptLocation = " (At " + scriptLocation + ")";
                }

                Dom.Log.Add("Script error" + scriptLocation + ": " + e);
            }
        }