Exemple #1
0
		internal override void Emit (EmitContext ec)
		{
			ILGenerator ig = ec.ig;
			ig.Emit (OpCodes.Ldstr, str);
			if (no_effect)
				ig.Emit (OpCodes.Pop);
		}
Exemple #2
0
		internal override void Emit (EmitContext ec)
		{
			if (InFunction)
				ec.ig.Emit (OpCodes.Ldarg_0);
			else
				CodeGenerator.emit_get_default_this (ec.ig, InFunction);
		}
Exemple #3
0
		internal void create_closure (EmitContext ec)
		{
			string name = func_obj.name;
			string full_name;
			TypeBuilder type = ec.type_builder;
			ILGenerator ig = ec.ig;			
		
			if (prefix == String.Empty) 
				full_name = name;
			else 
				full_name = prefix + "." + name;

			MethodBuilder method_builder = type.DefineMethod (full_name, func_obj.attr, HandleReturnType,
								  func_obj.params_types ());
			MethodBuilder tmp = (MethodBuilder) TypeManager.Get (name);


			if (tmp == null)
				TypeManager.Add (name, method_builder);
			else 
				TypeManager.Set (name, method_builder);

			set_custom_attr (method_builder);
			this.ig = method_builder.GetILGenerator ();

			if (parent == null || parent.GetType () == typeof (ScriptBlock))
				type.DefineField (name, typeof (Microsoft.JScript.ScriptFunction),
						  FieldAttributes.Public | FieldAttributes.Static);
			else {
				local_func = ig.DeclareLocal (typeof (Microsoft.JScript.ScriptFunction));
				TypeManager.AddLocalScriptFunction (name, local_func);
			}
			build_closure (ec, full_name, func_obj.source);
		}
        void ICanModifyContext.EmitDecls(EmitContext ec)
        {
            object var;

            if ((var = CodeGenerator.variable_defined_in_current_scope (id)) != null) {
                Type t = var.GetType ();
                if (t == typeof (FieldBuilder))
                    field_info = (FieldBuilder) var;
                else if (t == typeof (LocalBuilder))
                    local_builder = (LocalBuilder) var;
                return;
            }

            ILGenerator ig = ec.ig;
            if (parent == null || (parent.GetType () != typeof (FunctionDeclaration)
                           && parent.GetType () != typeof (FunctionExpression))) {
                FieldBuilder field_builder;
                TypeBuilder type_builder  = ec.type_builder;

                field_builder = type_builder.DefineField (id, this.type, FieldAttributes.Public | FieldAttributes.Static);
                TypeManager.Add (id, field_builder);
                field_info = field_builder;
            } else {
                local_builder = ig.DeclareLocal (type);
                TypeManager.Add (id, local_builder);
            }
        }
Exemple #5
0
		internal override void Emit (EmitContext ec)
		{
			int i, size = var_decls.Count;

			for (i = 0; i < size; i++)
				((VariableDeclaration) var_decls [i]).Emit (ec);
		}
Exemple #6
0
		internal override void Emit (EmitContext ec)
		{
			ILGenerator ig = ec.ig;
			expression.Emit (ec);
			ig.Emit (OpCodes.Call, typeof (Throw).GetMethod ("JScriptThrow"));
			ig.Emit (OpCodes.Throw);
		}
Exemple #7
0
 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"));
 }
Exemple #8
0
 internal override void Emit(EmitContext ec)
 {
     if (label == String.Empty) {
         ec.ig.Emit (OpCodes.Br, ec.LoopEnd);
         return;
     }
     ec.ig.Emit (OpCodes.Br, (binding as Labelled).EndAddrs);
 }
Exemple #9
0
		void ICanModifyContext.EmitDecls (EmitContext ec)
		{
			if (true_stm is ICanModifyContext)
				((ICanModifyContext) true_stm).EmitDecls (ec);

			if (false_stm is ICanModifyContext)
				((ICanModifyContext) false_stm).EmitDecls (ec);
		}
Exemple #10
0
		void ICanModifyContext.EmitDecls (EmitContext ec)
		{
			if (lhs is ICanModifyContext)
				((ICanModifyContext) lhs).EmitDecls (ec);

			if (body is ICanModifyContext)
				((ICanModifyContext) body).EmitDecls (ec);
		}
Exemple #11
0
        internal override void Emit(EmitContext ec)
        {
            ILGenerator ig = ec.ig;

            if (Value)
                ig.Emit (OpCodes.Ldc_I4_1);
            else
                ig.Emit (OpCodes.Ldc_I4_0);

            if (no_effect)
                ig.Emit (OpCodes.Pop);
        }
Exemple #12
0
        void ICanModifyContext.EmitDecls(EmitContext ec)
        {
            if (guarded_block is ICanModifyContext)
                ((ICanModifyContext) guarded_block).EmitDecls (ec);

            foreach (AST ast in catch_blocks)
                if (ast is ICanModifyContext)
                    ((ICanModifyContext) ast).EmitDecls (ec);

            if (finally_block is ICanModifyContext)
                ((ICanModifyContext) finally_block).EmitDecls (ec);
        }
Exemple #13
0
 void ICanModifyContext.EmitDecls(EmitContext ec)
 {
     //
     // Emit variable declarations and function's closure first
     // because of posible free occurrences inside a method.
     //
     foreach (AST ast in elems)
         if (ast is FunctionDeclaration)
             ((FunctionDeclaration) ast).create_closure (ec);
         else if (ast is ICanModifyContext)
             ((ICanModifyContext) ast).EmitDecls (ec);
 }
Exemple #14
0
		internal override void Emit (EmitContext ec)
		{
			int n = elems.Count;
			object e;

			//
			// Emit the rest of expressions and statements.
			//
			for (int i = 0; i < n; i++) {
				e = elems [i];
				((AST) e).Emit (ec);
			}
		}
Exemple #15
0
		internal override void Emit (EmitContext ec)
		{
			int i = 0;
			ILGenerator ig = ec.ig;
			ArrayList exps = elems.elems;
			FieldInfo missing = null;
			if (skip_count != 0)
				missing = typeof (Missing).GetField ("Value");
			ig.Emit (OpCodes.Ldc_I4, elems.Size);
			ig.Emit (OpCodes.Newarr, typeof (object));
			foreach (AST ast in exps) {
				ig.Emit (OpCodes.Dup);
 				ig.Emit (OpCodes.Ldc_I4, i);
				if (ast != null) {
					ast.Emit (ec);
					CodeGenerator.EmitBox (ig, ast);
				} else 
					ig.Emit (OpCodes.Ldsfld, missing);
 				ig.Emit (OpCodes.Stelem_Ref);
				i++;
			}
 			ig.Emit (OpCodes.Call, typeof (Globals).GetMethod ("ConstructArrayLiteral"));
		}
Exemple #16
0
        internal override void Emit(EmitContext ec)
        {
            ILGenerator ig = ec.ig;
            if (exp != null)
                exp.Emit (ec);

            ig.Emit (OpCodes.Ldarg_0);
            ig.Emit (OpCodes.Ldfld, typeof (ScriptObject).GetField ("engine"));
            ig.Emit (OpCodes.Call, typeof (With).GetMethod ("JScriptWith"));
            ig.Emit (OpCodes.Pop);

            ig.BeginExceptionBlock ();

            if (stm != null)
                stm.Emit (ec);

            ig.BeginFinallyBlock ();
            ig.Emit (OpCodes.Ldarg_0);
            ig.Emit (OpCodes.Ldfld, typeof (ScriptObject).GetField ("engine"));
            ig.Emit (OpCodes.Call, typeof (VsaEngine).GetMethod ("PopScriptObject"));
            ig.Emit (OpCodes.Pop);

            ig.EndExceptionBlock ();
        }
Exemple #17
0
 internal override void Emit(EmitContext ec)
 {
     IntConstant.EmitInt (ec.ig, Value);
 }
Exemple #18
0
        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);
        }
Exemple #19
0
 internal override void Emit(EmitContext ec)
 {
     ec.ig.Emit (OpCodes.Ldstr, property_name);
     exp.Emit (ec);
     CodeGenerator.EmitBox (ec.ig, exp);
 }
Exemple #20
0
        internal override void Emit(EmitContext ec)
        {
            ILGenerator ig = ec.ig;

            ig.Emit (OpCodes.Ldarg_0);
            ig.Emit (OpCodes.Ldfld, typeof (ScriptObject).GetField ("engine"));
            ig.Emit (OpCodes.Call, typeof (Microsoft.JScript.Vsa.VsaEngine).GetMethod ("GetOriginalObjectConstructor"));
            ig.Emit (OpCodes.Call, typeof (ObjectConstructor).GetMethod ("ConstructObject"));

            foreach (ObjectLiteralItem item in elems) {
                ig.Emit (OpCodes.Dup);
                item.Emit (ec);
                ig.Emit (OpCodes.Call, typeof (JSObject).GetMethod ("SetMemberValue2"));
            }
            if (no_effect)
                ig.Emit (OpCodes.Pop);
        }
Exemple #21
0
        internal override void Emit(EmitContext ec)
        {
            ILGenerator ig = ec.ig;

                        EmitLong (ig, Value);
        }
Exemple #22
0
        internal override void Emit(EmitContext ec)
        {
            AST         tmp;
            ILGenerator ig        = ec.ig;
            Label       old_begin = ec.LoopBegin;
            Label       old_end   = ec.LoopEnd;
            Label       back      = ig.DefineLabel();
            Label       forward   = ig.DefineLabel();


            /* emit init expr */
            tmp = exprs [0];
            if (tmp != null)
            {
                tmp.Emit(ec);
            }

            ec.LoopBegin = ig.DefineLabel();
            ec.LoopEnd   = ig.DefineLabel();

            ig.MarkLabel(back);
            ig.MarkLabel(ec.LoopBegin);

            /* emit condition */
            tmp = exprs [1];
            if (tmp != null)
            {
                tmp.Emit(ec);
            }

            if (tmp != null && tmp is Expression)
            {
                ArrayList t = ((Expression)tmp).exprs;
                AST       a = (AST)t [t.Count - 1];
                if (a is Equality)
                {
                    ig.Emit(OpCodes.Brfalse, forward);
                }
                else if (a is Relational)
                {
                    Relational rel = (Relational)a;
                    ig.Emit(OpCodes.Ldc_I4_0);
                    ig.Emit(OpCodes.Conv_R8);

                    if (rel.op == JSToken.GreaterThan)
                    {
                        ig.Emit(OpCodes.Ble, forward);
                    }
                    else
                    {
                        ig.Emit(OpCodes.Bge, forward);
                    }
                }
            }
            /* emit stms */
            if (stms != null)
            {
                stms.Emit(ec);
            }

            if (parent.GetType() == typeof(Labelled))
            {
                ig.MarkLabel((parent as Labelled).InitAddrs);
            }

            tmp = exprs [2];
            /* emit increment */
            if (tmp != null)
            {
                tmp.Emit(ec);
            }

            ig.Emit(OpCodes.Br, back);
            ig.MarkLabel(forward);
            ig.MarkLabel(ec.LoopEnd);

            ec.LoopBegin = old_begin;
            ec.LoopEnd   = old_end;
        }
Exemple #23
0
 internal override void Emit(EmitContext ec)
 {
 }
 void ICanModifyContext.EmitDecls(EmitContext ec)
 {
     ((ICanModifyContext) func_obj.body).EmitDecls (ec);
 }
        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)
        {
            TypeManager.BeginScope ();
            ILGenerator old_ig = ec.ig;
            ec.ig = this.ig;

            ((ICanModifyContext) func_obj.body).EmitDecls (ec);
            func_obj.body.Emit (ec);

            string func_name = func_obj.name;

            if (SemanticAnalyser.MethodContainsEval (func_name))
                CodeGenerator.load_local_vars (ec.ig, true);
            else {
                VariableDeclaration decl = SemanticAnalyser.OutterScopeVar (func_name);
                if (decl == null) {
                    decl = SemanticAnalyser.VarUsedNested (func_name);
                    if (decl != null)
                        CodeGenerator.load_local_vars (ec.ig, InFunction);
                } else
                    CodeGenerator.locals_to_stack_frame (ec.ig, decl.lexical_depth, lexical_depth - decl.lexical_depth, true);
            }
            this.ig.Emit (OpCodes.Ret);
            ec.ig = old_ig;
            TypeManager.EndScope ();
        }
Exemple #27
0
 internal override void Emit(EmitContext ec)
 {
     ec.ig.Emit (OpCodes.Ldc_R4, Value);
 }
Exemple #28
0
		void ICanModifyContext.EmitDecls (EmitContext ec)
		{
			foreach (VariableDeclaration var_decl in var_decls)
				((ICanModifyContext) var_decl).EmitDecls (ec);
		}
Exemple #29
0
		//
		// Here the actual IL code generation happens.
		//
		internal abstract void Emit (EmitContext ec);
Exemple #30
0
 void ICanModifyContext.EmitDecls(EmitContext ec)
 {
     if (stm is ICanModifyContext)
         ((ICanModifyContext) stm).EmitDecls (ec);
 }
Exemple #31
0
		internal override void Emit (EmitContext ec)
		{
			ILGenerator ig = ec.ig;
			
			ig.BeginExceptionBlock ();

			if (guarded_block != null)
				guarded_block.Emit (ec);

			if (catch_blocks != null && catch_blocks.Count > 0) {
				foreach (Catch c in catch_blocks)
					c.Emit (ec);
			}	       	
			if (finally_block != null) {
				ig.BeginFinallyBlock ();
				finally_block.Emit (ec);
			}
			ig.EndExceptionBlock ();
		}
Exemple #32
0
		internal override void Emit (EmitContext ec)
		{
			throw new NotImplementedException ();
		}
Exemple #33
0
        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);
        }