Example #1
0
        private object CompileImpl(IScriptable scope, StreamReader sourceReader, string sourceString, string sourceName, int lineno, object securityDomain, bool returnFunction, Interpreter compiler, ErrorReporter compilationErrorReporter)
        {
            if (securityDomain != null && securityController == null) {
                throw new ArgumentException ("securityDomain should be null if setSecurityController() was never called");
            }

            // One of sourceReader or sourceString has to be null
            if (!(sourceReader == null ^ sourceString == null))
                Context.CodeBug ();
            // scope should be given if and only if compiling function
            if (!(scope == null ^ returnFunction))
                Context.CodeBug ();

            CompilerEnvirons compilerEnv = new CompilerEnvirons ();
            compilerEnv.initFromContext (this);
            if (compilationErrorReporter == null) {
                compilationErrorReporter = compilerEnv.getErrorReporter ();
            }

            if (m_Debugger != null) {
                if (sourceReader != null) {
                    sourceString = sourceReader.ReadToEnd ();
                    sourceReader = null;
                }
            }

            Parser p = new Parser (compilerEnv, compilationErrorReporter);
            if (returnFunction) {
                p.calledByCompileFunction = true;
            }
            ScriptOrFnNode tree;
            if (sourceString != null) {
                tree = p.Parse (sourceString, sourceName, lineno);
            }
            else {
                tree = p.Parse (sourceReader, sourceName, lineno);
            }
            if (returnFunction) {
                if (!(tree.FunctionCount == 1 && tree.FirstChild != null && tree.FirstChild.Type == Token.FUNCTION)) {
                    // TODO: the check just look for the first child
                    // TODO: and allows for more nodes after it for compatibility
                    // TODO: with sources like function() {};;;
                    throw new ArgumentException ("compileFunction only accepts source with single JS function: " + sourceString);
                }
            }

            if (compiler == null) {
                compiler = new Interpreter ();
                //compiler = new Compiler();
            }

            string encodedSource = p.EncodedSource;

            object bytecode = compiler.Compile (compilerEnv, tree, encodedSource, returnFunction);

            if (m_Debugger != null) {
                if (sourceString == null)
                    Context.CodeBug ();
                if (bytecode is DebuggableScript) {
                    DebuggableScript dscript = (DebuggableScript)bytecode;
                    NotifyDebugger (this, dscript, sourceString);
                }
                else {
                    throw new ApplicationException ("NOT SUPPORTED");
                }
            }

            object result;
            if (returnFunction) {
                result = compiler.CreateFunctionObject (this, scope, bytecode, securityDomain);
            }
            else {
                result = compiler.CreateScriptObject (bytecode, securityDomain);
            }

            return result;
        }
Example #2
0
 internal IFunction CompileFunction(IScriptable scope, string source, Interpreter compiler, ErrorReporter compilationErrorReporter, string sourceName, int lineno, object securityDomain)
 {
     return (IFunction)CompileImpl (scope, null, source, sourceName, lineno, securityDomain, true, compiler, compilationErrorReporter);
 }
Example #3
0
 internal IScript CompileString(string source, Interpreter compiler, ErrorReporter compilationErrorReporter, string sourceName, int lineno, object securityDomain)
 {
     return (IScript)CompileImpl (null, null, source, sourceName, lineno, securityDomain, false, compiler, compilationErrorReporter);
 }
        void generateNestedFunctions ()
        {
            int functionCount = scriptOrFn.FunctionCount;
            if (functionCount == 0)
                return;

            InterpreterData [] array = new InterpreterData [functionCount];
            for (int i = 0; i != functionCount; i++) {
                FunctionNode def = scriptOrFn.getFunctionNode (i);
                Interpreter jsi = new Interpreter ();
                jsi.compilerEnv = compilerEnv;
                jsi.scriptOrFn = def;
                jsi.itsData = new InterpreterData (itsData);
                jsi.generateFunctionICode ();
                array [i] = jsi.itsData;
            }
            itsData.itsNestedFunctions = array;
        }