internal static void load_local_vars(ILGenerator ig, bool inFunction) { int n = 0; Type stack_frame = typeof(StackFrame); CodeGenerator.load_engine(inFunction, ig); ig.Emit(OpCodes.Call, typeof(VsaEngine).GetMethod("ScriptObjectStackTop")); ig.Emit(OpCodes.Castclass, stack_frame); ig.Emit(OpCodes.Ldfld, stack_frame.GetField("localVars")); object [] locals = TypeManager.CurrentLocals; n = locals != null ? locals.Length : 0; object local = null; for (int i = 0; i < n; i++) { local = locals [i]; if (local is LocalBuilder) { ig.Emit(OpCodes.Dup); ig.Emit(OpCodes.Ldc_I4, i); ig.Emit(OpCodes.Ldloc, (LocalBuilder)local); ig.Emit(OpCodes.Stelem_Ref); } } ig.Emit(OpCodes.Pop); }
internal override void Emit(EmitContext ec) { ILGenerator ig = ec.ig; TypeBuilder type = ec.type_builder; FieldBuilder field = type.DefineField(SemanticAnalyser.NextAnonymousRegExpObj, typeof(RegExpObject), FieldAttributes.Public | FieldAttributes.Static); Label label = ig.DefineLabel(); ig.Emit(OpCodes.Ldsfld, field); ig.Emit(OpCodes.Brtrue, label); CodeGenerator.load_engine(InFunction, ig); ig.Emit(OpCodes.Call, typeof(VsaEngine).GetMethod("GetOriginalRegExpConstructor")); ig.Emit(OpCodes.Ldstr, re); emit_flag(ig, flags.IndexOfAny(new char [] { IGNORE_CASE }) > -1); emit_flag(ig, flags.IndexOfAny(new char [] { GLOBAL }) > -1); emit_flag(ig, flags.IndexOfAny(new char [] { MULTI_LINE }) > -1); ig.Emit(OpCodes.Call, typeof(RegExpConstructor).GetMethod("Construct")); ig.Emit(OpCodes.Castclass, typeof(RegExpObject)); ig.Emit(OpCodes.Stsfld, field); ig.MarkLabel(label); ig.Emit(OpCodes.Ldsfld, field); }
internal override void Emit(EmitContext ec) { ILGenerator ig = ec.ig; Type t = typeof(object); bool in_function = InFunction; if (in_function) { local_builder = ig.DeclareLocal(t); } else { field_info = ec.type_builder.DefineField(mangle_id(id), t, FieldAttributes.Public | FieldAttributes.Static); } ig.BeginCatchBlock(typeof(Exception)); CodeGenerator.load_engine(in_function, ig); ig.Emit(OpCodes.Call, typeof(Try).GetMethod("JScriptExceptionValue")); if (in_function) { ig.Emit(OpCodes.Stloc, local_builder); } else { ig.Emit(OpCodes.Stsfld, field_info); } stms.Emit(ec); }
internal override void Emit(EmitContext ec) { ILGenerator ig = ec.ig; ig.Emit(OpCodes.Ldstr, name); CodeGenerator.load_engine(false, ig); ig.Emit(OpCodes.Call, GetType().GetMethod("JScriptImport")); }
internal static void emit_parents(bool inFunction, int lexical_difference, ILGenerator ig) { CodeGenerator.load_engine(inFunction, ig); ig.Emit(OpCodes.Call, typeof(VsaEngine).GetMethod("ScriptObjectStackTop")); for (int i = 0; i < lexical_difference; i++) { ig.Emit(OpCodes.Call, typeof(ScriptObject).GetMethod("GetParent")); } }
internal static void emit_get_default_this(ILGenerator ig, bool inFunction) { CodeGenerator.load_engine(inFunction, ig); ig.Emit(OpCodes.Call, typeof(VsaEngine).GetMethod("ScriptObjectStackTop")); Type iact_obj = typeof(IActivationObject); ig.Emit(OpCodes.Castclass, iact_obj); ig.Emit(OpCodes.Callvirt, iact_obj.GetMethod("GetDefaultThisObject")); }
internal void build_closure(EmitContext ec, string full_name, string encodedSource) { ILGenerator ig = ec.ig; string name = func_obj.name; Type t = ec.mod_builder.GetType(CodeGenerator.GetTypeName(Location.SourceName)); ig.Emit(OpCodes.Ldtoken, t); if (name != String.Empty) { ig.Emit(OpCodes.Ldstr, name); } else { ig.Emit(OpCodes.Ldstr, SemanticAnalyser.CurrentAnonymousMethod); } ig.Emit(OpCodes.Ldstr, full_name); func_obj.parameters.Emit(ec); build_local_fields(ig); ig.Emit(OpCodes.Ldc_I4_0); // FIXME: this hard coded for now. ig.Emit(OpCodes.Ldc_I4_0); // FIXME: this hard coded for now. ig.Emit(OpCodes.Ldstr, Decompiler.Decompile(encodedSource, 0, 0).Trim()); CodeGenerator.load_engine(InFunction, ig); ig.Emit(OpCodes.Call, typeof(FunctionExpression).GetMethod("JScriptFunctionExpression")); ig.Emit(OpCodes.Stloc, local_func); ig.Emit(OpCodes.Ldloc, local_func); ig.Emit(OpCodes.Newobj, typeof(Closure).GetConstructor(new Type [] { typeof(FunctionObject) })); if (name != String.Empty) { ig.Emit(OpCodes.Dup); if (parent == null || parent.GetType() == typeof(ScriptBlock)) { ig.Emit(OpCodes.Stsfld, field); } else if (parent != null && (parent.GetType() == typeof(FunctionDeclaration) || parent.GetType() == typeof(FunctionExpression))) { ig.Emit(OpCodes.Stloc, local_script_func); } } else { if (parent != null && (parent.GetType() == typeof(FunctionDeclaration) || parent.GetType() == typeof(FunctionExpression))) { ig.Emit(OpCodes.Dup); ig.Emit(OpCodes.Stloc, local_script_func); } } }
internal void build_closure(EmitContext ec, string full_name, string encodedSource) { ILGenerator ig = ec.ig; string name = func_obj.name; Type t = ec.mod_builder.GetType(CodeGenerator.GetTypeName(Location.SourceName)); ig.Emit(OpCodes.Ldtoken, t); ig.Emit(OpCodes.Ldstr, name); ig.Emit(OpCodes.Ldstr, full_name); func_obj.parameters.Emit(ec); build_local_fields(ig); // // If we have en eval method call, we have to // save the loca vars in the stack // if (SemanticAnalyser.MethodContainsEval(name) || SemanticAnalyser.MethodVarsUsedNested(name)) { ig.Emit(OpCodes.Ldc_I4_1); } else { ig.Emit(OpCodes.Ldc_I4_0); } ig.Emit(OpCodes.Ldc_I4_0); // FIXME: this hard coded for now. ig.Emit(OpCodes.Ldstr, Decompiler.Decompile(encodedSource, 0, 0).Trim()); ig.Emit(OpCodes.Ldnull); // FIXME: this hard coded for now. CodeGenerator.load_engine(InFunction, ig); ig.Emit(OpCodes.Call, typeof(FunctionDeclaration).GetMethod("JScriptFunctionDeclaration")); if (parent == null || parent.GetType() == typeof(ScriptBlock)) { ig.Emit(OpCodes.Stsfld, t.GetField(name)); } else { ig.Emit(OpCodes.Stloc, local_func); } }
internal override void Emit(EmitContext ec) { ILGenerator ig = ec.ig; bool varStm = lhs is VariableStatement; object var = null; if (varStm) { VariableStatement stm = (VariableStatement)lhs; ig.Emit(OpCodes.Ldnull); var = TypeManager.Get(((VariableDeclaration)stm.var_decls [0]).id); set_builder(ig, var); } if (obj != null) { obj.Emit(ec); } CodeGenerator.load_engine(InFunction, ig); Type convert = typeof(Convert); ig.Emit(OpCodes.Call, convert.GetMethod("ToForInObject")); ig.Emit(OpCodes.Call, typeof(ForIn).GetMethod("JScriptGetEnumerator")); Type ienumerator = typeof(IEnumerator); LocalBuilder iter = ig.DeclareLocal(ienumerator); LocalBuilder current = ig.DeclareLocal(typeof(object)); ig.Emit(OpCodes.Stloc, iter); Label init_loop = ig.DefineLabel(); Label move_next = ig.DefineLabel(); Label exit = ig.DefineLabel(); ig.Emit(OpCodes.Br, move_next); ig.MarkLabel(init_loop); if (body != null) { body.Emit(ec); } ig.MarkLabel(move_next); ig.Emit(OpCodes.Ldloc, iter); ig.Emit(OpCodes.Callvirt, ienumerator.GetMethod("MoveNext")); ig.Emit(OpCodes.Brfalse, exit); ig.Emit(OpCodes.Ldloc, iter); ig.Emit(OpCodes.Callvirt, ienumerator.GetProperty("Current").GetGetMethod()); ig.Emit(OpCodes.Stloc, current); ig.Emit(OpCodes.Ldloc, current); if (varStm) { set_builder(ig, var); } else { if (lhs is Expression) { AST ast = ((Expression)lhs).Last; if (ast is Identifier) { ((Identifier)ast).EmitStore(ec); } else { throw new NotImplementedException(); } } else { throw new NotImplementedException(); } } ig.Emit(OpCodes.Br, init_loop); ig.MarkLabel(exit); }