/// <summary>Create function compiled from Function(...) constructor.</summary> /// <remarks>Create function compiled from Function(...) constructor.</remarks> internal static Rhino.InterpretedFunction CreateFunction(Context cx, Scriptable scope, InterpreterData idata, object staticSecurityDomain) { Rhino.InterpretedFunction f; f = new Rhino.InterpretedFunction(idata, staticSecurityDomain); f.InitScriptFunction(cx, scope); return f; }
// fixupTable[i] = (label_index << 32) | fixup_site // ECF_ or Expression Context Flags constants: for now only TAIL public virtual InterpreterData Compile(CompilerEnvirons compilerEnv, ScriptNode tree, string encodedSource, bool returnFunction) { this.compilerEnv = compilerEnv; new NodeTransformer().Transform(tree); if (returnFunction) { scriptOrFn = tree.GetFunctionNode(0); } else { scriptOrFn = tree; } itsData = new InterpreterData(compilerEnv.GetLanguageVersion(), scriptOrFn.GetSourceName(), encodedSource, ((AstRoot)tree).IsInStrictMode()); itsData.topLevel = true; if (returnFunction) { GenerateFunctionICode(); } else { GenerateICodeFromTree(scriptOrFn); } return itsData; }
private InterpretedFunction(InterpreterData idata, object staticSecurityDomain) { this.idata = idata; // Always get Context from the current thread to // avoid security breaches via passing mangled Context instances // with bogus SecurityController Context cx = Context.GetContext(); SecurityController sc = cx.GetSecurityController(); object dynamicDomain; if (sc != null) { dynamicDomain = sc.GetDynamicSecurityDomain(staticSecurityDomain); } else { if (staticSecurityDomain != null) { throw new ArgumentException(); } dynamicDomain = null; } this.securityController = sc; this.securityDomain = dynamicDomain; }
internal static string GetEncodedSource(InterpreterData idata) { if (idata.encodedSource == null) { return null; } return Sharpen.Runtime.Substring(idata.encodedSource, idata.encodedSourceStart, idata.encodedSourceEnd); }
internal static int[] GetLineNumbers(InterpreterData data) { UintMap presentLines = new UintMap(); byte[] iCode = data.itsICode; int iCodeLength = iCode.Length; for (int pc = 0; pc != iCodeLength; ) { int bytecode = iCode[pc]; int span = BytecodeSpan(bytecode); if (bytecode == Icode_LINE) { if (span != 3) { Kit.CodeBug(); } int line = GetIndex(iCode, pc + 1); presentLines.Put(line, 0); } pc += span; } return presentLines.GetKeys(); }
internal static void DumpICode(InterpreterData idata) { return; byte[] iCode = idata.itsICode; int iCodeLength = iCode.Length; string[] strings = idata.itsStringTable; TextWriter @out = System.Console.Out; @out.WriteLine("ICode dump, for " + idata.itsName + ", length = " + iCodeLength); @out.WriteLine("MaxStack = " + idata.itsMaxStack); int indexReg = 0; for (int pc = 0; pc < iCodeLength; ) { @out.Flush(); @out.Write(" [" + pc + "] "); int token = iCode[pc]; int icodeLength = BytecodeSpan(token); string tname = Icode.BytecodeName(token); int old_pc = pc; ++pc; switch (token) { default: { if (icodeLength != 1) { Kit.CodeBug(); } @out.WriteLine(tname); break; } case Icode_GOSUB: case Token.GOTO: case Token.IFEQ: case Token.IFNE: case Icode_IFEQ_POP: case Icode_LEAVEDQ: { int newPC = pc + GetShort(iCode, pc) - 1; @out.WriteLine(tname + " " + newPC); pc += 2; break; } case Icode_VAR_INC_DEC: case Icode_NAME_INC_DEC: case Icode_PROP_INC_DEC: case Icode_ELEM_INC_DEC: case Icode_REF_INC_DEC: { int incrDecrType = iCode[pc]; @out.WriteLine(tname + " " + incrDecrType); ++pc; break; } case Icode_CALLSPECIAL: { int callType = iCode[pc] & unchecked((int)(0xFF)); bool isNew = (iCode[pc + 1] != 0); int line = GetIndex(iCode, pc + 2); @out.WriteLine(tname + " " + callType + " " + isNew + " " + indexReg + " " + line); pc += 4; break; } case Token.CATCH_SCOPE: { bool afterFisrtFlag = (iCode[pc] != 0); @out.WriteLine(tname + " " + afterFisrtFlag); ++pc; break; } case Token.REGEXP: { @out.WriteLine(tname + " " + idata.itsRegExpLiterals[indexReg]); break; } case Token.OBJECTLIT: case Icode_SPARE_ARRAYLIT: { @out.WriteLine(tname + " " + idata.literalIds[indexReg]); break; } case Icode_CLOSURE_EXPR: case Icode_CLOSURE_STMT: { @out.WriteLine(tname + " " + idata.itsNestedFunctions[indexReg]); break; } case Token.CALL: case Icode_TAIL_CALL: case Token.REF_CALL: case Token.NEW: { @out.WriteLine(tname + ' ' + indexReg); break; } case Token.THROW: case Token.YIELD: case Icode_GENERATOR: case Icode_GENERATOR_END: { int line = GetIndex(iCode, pc); @out.WriteLine(tname + " : " + line); pc += 2; break; } case Icode_SHORTNUMBER: { int value = GetShort(iCode, pc); @out.WriteLine(tname + " " + value); pc += 2; break; } case Icode_INTNUMBER: { int value = GetInt(iCode, pc); @out.WriteLine(tname + " " + value); pc += 4; break; } case Token.NUMBER: { double value = idata.itsDoubleTable[indexReg]; @out.WriteLine(tname + " " + value); break; } case Icode_LINE: { int line = GetIndex(iCode, pc); @out.WriteLine(tname + " : " + line); pc += 2; break; } case Icode_REG_STR1: { string str = strings[unchecked((int)(0xFF)) & iCode[pc]]; @out.WriteLine(tname + " \"" + str + '"'); ++pc; break; } case Icode_REG_STR2: { string str = strings[GetIndex(iCode, pc)]; @out.WriteLine(tname + " \"" + str + '"'); pc += 2; break; } case Icode_REG_STR4: { string str = strings[GetInt(iCode, pc)]; @out.WriteLine(tname + " \"" + str + '"'); pc += 4; break; } case Icode_REG_IND_C0: { indexReg = 0; @out.WriteLine(tname); break; } case Icode_REG_IND_C1: { indexReg = 1; @out.WriteLine(tname); break; } case Icode_REG_IND_C2: { indexReg = 2; @out.WriteLine(tname); break; } case Icode_REG_IND_C3: { indexReg = 3; @out.WriteLine(tname); break; } case Icode_REG_IND_C4: { indexReg = 4; @out.WriteLine(tname); break; } case Icode_REG_IND_C5: { indexReg = 5; @out.WriteLine(tname); break; } case Icode_REG_IND1: { indexReg = unchecked((int)(0xFF)) & iCode[pc]; @out.WriteLine(tname + " " + indexReg); ++pc; break; } case Icode_REG_IND2: { indexReg = GetIndex(iCode, pc); @out.WriteLine(tname + " " + indexReg); pc += 2; break; } case Icode_REG_IND4: { indexReg = GetInt(iCode, pc); @out.WriteLine(tname + " " + indexReg); pc += 4; break; } case Icode_GETVAR1: case Icode_SETVAR1: case Icode_SETCONSTVAR1: { indexReg = iCode[pc]; @out.WriteLine(tname + " " + indexReg); ++pc; break; } } if (old_pc + icodeLength != pc) { Kit.CodeBug(); } } int[] table = idata.itsExceptionTable; if (table != null) { @out.WriteLine("Exception handlers: " + table.Length / EXCEPTION_SLOT_SIZE); for (int i = 0; i != table.Length; i += EXCEPTION_SLOT_SIZE) { int tryStart = table[i + EXCEPTION_TRY_START_SLOT]; int tryEnd = table[i + EXCEPTION_TRY_END_SLOT]; int handlerStart = table[i + EXCEPTION_HANDLER_SLOT]; int type = table[i + EXCEPTION_TYPE_SLOT]; int exceptionLocal = table[i + EXCEPTION_LOCAL_SLOT]; int scopeLocal = table[i + EXCEPTION_SCOPE_SLOT]; @out.WriteLine(" tryStart=" + tryStart + " tryEnd=" + tryEnd + " handlerStart=" + handlerStart + " type=" + (type == 0 ? "catch" : "finally") + " exceptionLocal=" + exceptionLocal); } } @out.Flush(); }
public object Compile(CompilerEnvirons compilerEnv, ScriptNode tree, string encodedSource, bool returnFunction) { CodeGenerator cgen = new CodeGenerator(); itsData = cgen.Compile(compilerEnv, tree, encodedSource, returnFunction); return itsData; }
private void GenerateNestedFunctions() { int functionCount = scriptOrFn.GetFunctionCount(); if (functionCount == 0) { return; } InterpreterData[] array = new InterpreterData[functionCount]; for (int i = 0; i != functionCount; i++) { FunctionNode fn = scriptOrFn.GetFunctionNode(i); CodeGenerator gen = new CodeGenerator(); gen.compilerEnv = compilerEnv; gen.scriptOrFn = fn; gen.itsData = new InterpreterData(itsData); gen.GenerateFunctionICode(); array[i] = gen.itsData; } itsData.itsNestedFunctions = array; }
/// <summary>Create script from compiled bytecode.</summary> /// <remarks>Create script from compiled bytecode.</remarks> internal static Rhino.InterpretedFunction CreateScript(InterpreterData idata, object staticSecurityDomain) { Rhino.InterpretedFunction f; f = new Rhino.InterpretedFunction(idata, staticSecurityDomain); return f; }
private InterpretedFunction(Rhino.InterpretedFunction parent, int index) { this.idata = parent.idata.itsNestedFunctions[index]; this.securityController = parent.securityController; this.securityDomain = parent.securityDomain; }