Author: cesar
Date: 2005-04-14 02:16:51 -0400 (Thu, 14 Apr 2005)
New Revision: 42983
Modified:
trunk/mcs/class/Microsoft.JScript/Microsoft.JScript/ChangeLog
trunk/mcs/class/Microsoft.JScript/Microsoft.JScript/CodeGenerator.cs
trunk/mcs/class/Microsoft.JScript/Microsoft.JScript/Eval.cs
trunk/mcs/class/Microsoft.JScript/Microsoft.JScript/FunctionDeclaration.cs
trunk/mcs/class/Microsoft.JScript/Microsoft.JScript/IdentificationTable.cs
trunk/mcs/class/Microsoft.JScript/Microsoft.JScript/SemanticAnalizer.cs
trunk/mcs/class/Microsoft.JScript/Microsoft.JScript/TypeManager.cs
trunk/mcs/class/Microsoft.JScript/Microsoft.JScript/ast.cs
trunk/mcs/class/Microsoft.JScript/Microsoft.JScript/expression.cs
Log:
2005-04-14 Cesar Lopez Nataren <[EMAIL PROTECTED]>
* SemanticAnalizer.cs: Added a Hashtable that indicates which
methods need to generate proper code for handling the local vars
in the StackFrame. Added method AddMethodWithEval, MethodContainsEval.
* Eval.cs: Back to M.J.
* ast.cs: locals from abstract class Function is now an array of
objects not just AST's.
* CodeGenerator.cs: Clean up fall_true, better using recursion :)
Added static method load_local_vars. At method ft_emit_equality do
Emit the code of the ast. Added method load_local_vars.
* FunctionDeclaration.cs: Update call to TypeManager methods to
new API. Added a check for invocations to 'eval' inside functions
so StackFrame IL code gets generated. Build the function's closure
take the need for the StackFrame into account.
* TypeManager.cs: renamed IdentificationTable locals to
local_script_functions, AddLocal to AddLocalScriptFunction,
GetLocal to GetLocalScriptFunction. Added method CurrentLocals.
* IdentificationTable.cs: Method CurrentLocals now returns
an array of object's not AST's.
* expression.cs: (class Call) Check if we have a call to 'eval',
if so we must keep up the StackFrame which contains the local
vars. We generate special code that handles the StackFrame with
the local vars in order to keep them in sync. When a function
contains an invocation to 'eval' even when such function is a
function declared in the code we do not perform a direct method
call to it when invoked instead a call through late binding gets
performed. Added methods IsEval, set_local_vars. Implement 'eval'
from JScript!
Modified: trunk/mcs/class/Microsoft.JScript/Microsoft.JScript/ChangeLog
===================================================================
--- trunk/mcs/class/Microsoft.JScript/Microsoft.JScript/ChangeLog
2005-04-14 06:13:52 UTC (rev 42982)
+++ trunk/mcs/class/Microsoft.JScript/Microsoft.JScript/ChangeLog
2005-04-14 06:16:51 UTC (rev 42983)
@@ -1,3 +1,40 @@
+2005-04-14 Cesar Lopez Nataren <[EMAIL PROTECTED]>
+
+ * SemanticAnalizer.cs: Added a Hashtable that indicates which
+ methods need to generate proper code for handling the local vars
+ in the StackFrame. Added method AddMethodWithEval, MethodContainsEval.
+
+ * Eval.cs: Back to M.J.
+
+ * ast.cs: locals from abstract class Function is now an array of
+ objects not just AST's.
+
+ * CodeGenerator.cs: Clean up fall_true, better using recursion :)
+ Added static method load_local_vars. At method ft_emit_equality do
+ Emit the code of the ast. Added method load_local_vars.
+
+ * FunctionDeclaration.cs: Update call to TypeManager methods to
+ new API. Added a check for invocations to 'eval' inside functions
+ so StackFrame IL code gets generated. Build the function's closure
+ take the need for the StackFrame into account.
+
+ * TypeManager.cs: renamed IdentificationTable locals to
+ local_script_functions, AddLocal to AddLocalScriptFunction,
+ GetLocal to GetLocalScriptFunction. Added method CurrentLocals.
+
+ * IdentificationTable.cs: Method CurrentLocals now returns
+ an array of object's not AST's.
+
+ * expression.cs: (class Call) Check if we have a call to 'eval',
+ if so we must keep up the StackFrame which contains the local
+ vars. We generate special code that handles the StackFrame with
+ the local vars in order to keep them in sync. When a function
+ contains an invocation to 'eval' even when such function is a
+ function declared in the code we do not perform a direct method
+ call to it when invoked instead a call through late binding gets
+ performed. Added methods IsEval, set_local_vars. Implement 'eval'
+ from JScript!
+
2005-04-10 Cesar Lopez Nataren <[EMAIL PROTECTED]>
* StackTrace.cs: Back to M.J namespace.
Modified: trunk/mcs/class/Microsoft.JScript/Microsoft.JScript/CodeGenerator.cs
===================================================================
--- trunk/mcs/class/Microsoft.JScript/Microsoft.JScript/CodeGenerator.cs
2005-04-14 06:13:52 UTC (rev 42982)
+++ trunk/mcs/class/Microsoft.JScript/Microsoft.JScript/CodeGenerator.cs
2005-04-14 06:16:51 UTC (rev 42983)
@@ -326,6 +326,7 @@
ILGenerator ig = ec.ig;
Equality eq = ast as Equality;
+ eq.Emit (ec);
switch (eq.op) {
case JSToken.NotEqual:
ig.Emit (OpCodes.Brtrue, lbl);
@@ -341,17 +342,17 @@
Type type = ast.GetType ();
if (type == typeof (Expression)) {
- Expression exp = ast as Expression;
- exp.Emit (ec);
- AST last_exp = (AST) exp.exprs [exp.exprs.Count
- 1];
-
- if (last_exp is Binary)
- ft_binary_recursion (ec, last_exp, lbl);
- else if (last_exp is Equality)
- ft_emit_equality (ec, last_exp, lbl);
+ Expression exp = ast as Expression;
+ AST last_exp = last_exp = (AST) exp.exprs
[exp.exprs.Count - 1];
+ if (exp.exprs.Count >= 2)
+ exp.Emit (ec);
+ fall_true (ec, last_exp, lbl);
} else if (type == typeof (Binary))
ft_binary_recursion (ec, ast, lbl);
- else
+ else if (type == typeof (Equality)) {
+ Console.WriteLine ("about to call
ft_emit_equality");
+ ft_emit_equality (ec, ast, lbl);
+ } else
emit_default_case (ec, ast, OpCodes.Brfalse,
lbl);
}
@@ -492,5 +493,32 @@
{
return TypeManager.defined_in_current_scope (id);
}
+
+ 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);
+ }
}
}
Modified: trunk/mcs/class/Microsoft.JScript/Microsoft.JScript/Eval.cs
===================================================================
--- trunk/mcs/class/Microsoft.JScript/Microsoft.JScript/Eval.cs 2005-04-14
06:13:52 UTC (rev 42982)
+++ trunk/mcs/class/Microsoft.JScript/Microsoft.JScript/Eval.cs 2005-04-14
06:16:51 UTC (rev 42983)
@@ -4,6 +4,7 @@
// Author: Cesar Octavio Lopez Nataren
//
// (C) 2003, Cesar Octavio Lopez Nataren, <[EMAIL PROTECTED]>
+// (C) 2005, Novell Inc, (http://novell.com)
//
//
@@ -30,7 +31,7 @@
using System;
using Microsoft.JScript.Vsa;
-namespace Microsoft.JScript.Tmp {
+namespace Microsoft.JScript {
public class Eval : AST {
Modified:
trunk/mcs/class/Microsoft.JScript/Microsoft.JScript/FunctionDeclaration.cs
===================================================================
--- trunk/mcs/class/Microsoft.JScript/Microsoft.JScript/FunctionDeclaration.cs
2005-04-14 06:13:52 UTC (rev 42982)
+++ trunk/mcs/class/Microsoft.JScript/Microsoft.JScript/FunctionDeclaration.cs
2005-04-14 06:16:51 UTC (rev 42983)
@@ -111,7 +111,7 @@
FieldAttributes.Public |
FieldAttributes.Static);
else {
local_func = ig.DeclareLocal (typeof
(Microsoft.JScript.ScriptFunction));
- TypeManager.AddLocal (name, local_func);
+ TypeManager.AddLocalScriptFunction (name,
local_func);
}
build_closure (ec, full_name);
}
@@ -122,6 +122,10 @@
ILGenerator old_ig = ec.ig;
ec.ig = this.ig;
func_obj.body.Emit (ec);
+
+ if (SemanticAnalyser.MethodContainsEval (func_obj.name))
+ CodeGenerator.load_local_vars (ec.ig, true);
+
this.ig.Emit (OpCodes.Ret);
ec.ig = old_ig;
TypeManager.EndScope ();
@@ -141,8 +145,16 @@
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 (func_obj.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.Ldc_I4_0); // FIXME: this hard coded
for now.
ig.Emit (OpCodes.Ldstr,
"STRING_REPRESENTATION_OF_THE_FUNCTION"); // FIXME
ig.Emit (OpCodes.Ldnull); // FIXME: this hard coded for
now.
@@ -158,7 +170,7 @@
internal void build_local_fields (ILGenerator ig)
{
- AST e;
+ object e;
int n;
if (locals == null)
Modified:
trunk/mcs/class/Microsoft.JScript/Microsoft.JScript/IdentificationTable.cs
===================================================================
--- trunk/mcs/class/Microsoft.JScript/Microsoft.JScript/IdentificationTable.cs
2005-04-14 06:13:52 UTC (rev 42982)
+++ trunk/mcs/class/Microsoft.JScript/Microsoft.JScript/IdentificationTable.cs
2005-04-14 06:16:51 UTC (rev 42983)
@@ -237,11 +237,10 @@
// delete the latest scope mark
//
marks = marks.Tail;
-
current_locals.Pop ();
}
- internal AST [] CurrentLocals {
+ internal object [] CurrentLocals {
get {
Stack stack = new Stack ();
Symbol _top = top;
@@ -253,7 +252,7 @@
}
if (stack.Count == 0)
return null;
- AST [] locals = new AST [stack.Count];
+ object [] locals = new object [stack.Count];
stack.CopyTo (locals, 0);
return locals;
}
Modified:
trunk/mcs/class/Microsoft.JScript/Microsoft.JScript/SemanticAnalizer.cs
===================================================================
--- trunk/mcs/class/Microsoft.JScript/Microsoft.JScript/SemanticAnalizer.cs
2005-04-14 06:13:52 UTC (rev 42982)
+++ trunk/mcs/class/Microsoft.JScript/Microsoft.JScript/SemanticAnalizer.cs
2005-04-14 06:16:51 UTC (rev 42983)
@@ -44,7 +44,7 @@
private static Hashtable obj_ctrs;
private static Hashtable prototypes;
-
+ internal static Hashtable methods_with_eval = new Hashtable ();
//
// Type to GlobalObject
//
@@ -188,5 +188,18 @@
throw new Exception ("jsObj can't be null");
return (Type) prototypes [jsObj.GetType ()];
}
+
+ internal static void AddMethodWithEval (string name)
+ {
+ object contained = methods_with_eval [name];
+ if (contained == null)
+ methods_with_eval.Add (name, true);
+ }
+
+ internal static bool MethodContainsEval (string name)
+ {
+ object val = methods_with_eval [name];
+ return val != null;
+ }
}
}
Modified: trunk/mcs/class/Microsoft.JScript/Microsoft.JScript/TypeManager.cs
===================================================================
--- trunk/mcs/class/Microsoft.JScript/Microsoft.JScript/TypeManager.cs
2005-04-14 06:13:52 UTC (rev 42982)
+++ trunk/mcs/class/Microsoft.JScript/Microsoft.JScript/TypeManager.cs
2005-04-14 06:16:51 UTC (rev 42983)
@@ -17,24 +17,24 @@
internal class TypeManager {
static IdentificationTable infos;
- static IdentificationTable locals;
+ static IdentificationTable local_script_functions;
static TypeManager ()
{
infos = new IdentificationTable ();
- locals = new IdentificationTable ();
+ local_script_functions = new IdentificationTable ();
}
internal static void BeginScope ()
{
infos.BeginScope ();
- locals.BeginScope ();
+ local_script_functions.BeginScope ();
}
internal static void EndScope ()
{
infos.EndScope ();
- locals.EndScope ();
+ local_script_functions.EndScope ();
}
internal static void Add (string name, object o)
@@ -42,9 +42,9 @@
infos.Enter (Symbol.CreateSymbol (name), o);
}
- internal static void AddLocal (string name, object o)
+ internal static void AddLocalScriptFunction (string name,
object o)
{
- locals.Enter (Symbol.CreateSymbol (name), o);
+ local_script_functions.Enter (Symbol.CreateSymbol
(name), o);
}
internal static object Get (string name)
@@ -52,9 +52,9 @@
return infos.Get (Symbol.CreateSymbol (name));
}
- internal static object GetLocal (string name)
+ internal static object GetLocalScriptFunction (string name)
{
- return locals.Get (Symbol.CreateSymbol (name));
+ return local_script_functions.Get (Symbol.CreateSymbol
(name));
}
internal static void Set (string name, object o)
@@ -69,5 +69,9 @@
return Get (id);
return null;
}
+
+ internal static object [] CurrentLocals {
+ get { return infos.CurrentLocals; }
+ }
}
}
Modified: trunk/mcs/class/Microsoft.JScript/Microsoft.JScript/ast.cs
===================================================================
--- trunk/mcs/class/Microsoft.JScript/Microsoft.JScript/ast.cs 2005-04-14
06:13:52 UTC (rev 42982)
+++ trunk/mcs/class/Microsoft.JScript/Microsoft.JScript/ast.cs 2005-04-14
06:16:51 UTC (rev 42983)
@@ -106,11 +106,10 @@
internal FunctionObject func_obj;
internal JSFunctionAttributeEnum func_type;
- internal AST [] locals;
+ internal object [] locals;
internal LocalBuilder local_func;
protected bool not_void_return = false;
-
protected ILGenerator ig;
internal bool CheckThis {
Modified: trunk/mcs/class/Microsoft.JScript/Microsoft.JScript/expression.cs
===================================================================
--- trunk/mcs/class/Microsoft.JScript/Microsoft.JScript/expression.cs
2005-04-14 06:13:52 UTC (rev 42982)
+++ trunk/mcs/class/Microsoft.JScript/Microsoft.JScript/expression.cs
2005-04-14 06:16:51 UTC (rev 42983)
@@ -496,7 +496,7 @@
internal Call (AST parent, AST exp)
{
- this.parent = parent;
+ this.parent = parent;
this.member_exp = exp;
this.args = new Args ();
}
@@ -532,6 +532,16 @@
if (!built_in.IsFunction)
throw new Exception
("error JS5002 A: function expected.");
if (IsGlobalObjectMethod
(built_in)) {
+ //
+ // If a method contains
an eval invocation
+ // we must generate
proper code for accessing
+ // the local vars in
the StackFrame.
+ //
+ if (((Identifier)
member_exp).name.Value == "eval") {
+ Function
cont_func = GetContainerFunction;
+ if (cont_func
!= null)
+
SemanticAnalyser.AddMethodWithEval (cont_func.func_obj.name);
+ }
args.params_info =
built_in.Parameters;
n = built_in.NumOfArgs;
}
@@ -578,17 +588,39 @@
emit_print_stm (ec);
return;
} else if (IsGlobalObjectMethod (binding)) {
+ bool eval = IsEval (binding);
+ bool in_func = InFunction;
+
+ if (eval && in_func)
+ CodeGenerator.load_local_vars
(ec.ig, in_func);
+
args.Emit (ec);
+
+ if (eval)
+ CodeGenerator.load_engine
(in_func, ec.ig);
+
member_exp.Emit (ec);
+
+ if (eval && no_effect && in_func)
+ ec.ig.Emit (OpCodes.Pop);
+
+ if (eval && in_func)
+ set_local_vars (ec.ig);
} else if (IsConstructorProperty (binding)) {
member_exp.Emit (ec);
EmitBuiltInArgs (ec);
EmitInvoke (ec);
}
} else if (bind_type == typeof (FunctionDeclaration) ||
bind_type == typeof (FunctionExpression)) {
- MethodBuilder method = (MethodBuilder)
TypeManager.Get ((binding as Function).func_obj.name);
- emit_func_call (method, ec);
- return;
+ Function function = binding as Function;
+ MethodBuilder method = (MethodBuilder)
TypeManager.Get (function.func_obj.name);
+
+ if (SemanticAnalyser.MethodContainsEval
(function.func_obj.name))
+ emit_late_call (ec);
+ else {
+ emit_func_call (method, ec);
+ return;
+ }
} else if (binding is MemberInfo) {
MemberInfo minfo = (MemberInfo) binding;
MemberTypes member_type = minfo.MemberType;
@@ -876,6 +908,40 @@
return false;
}
}
+
+ bool IsEval (object binding)
+ {
+ if (binding == null || binding.GetType () != typeof
(BuiltIn))
+ return false;
+ BuiltIn bind = (BuiltIn) binding;
+ return bind.Name == "eval";
+ }
+
+ private void set_local_vars (ILGenerator ig)
+ {
+ 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.Ldelem_Ref);
+ ig.Emit (OpCodes.Stloc, (LocalBuilder)
local);
+ }
+ }
+ }
}
interface IAccesible {
@@ -1044,7 +1110,7 @@
if (bind is MethodBuilder) {
TypeBuilder type = ec.type_builder;
if (binding.InFunction) {
- LocalBuilder local_meth =
(LocalBuilder) TypeManager.GetLocal (binding.func_obj.name);
+ LocalBuilder local_meth =
(LocalBuilder) TypeManager.GetLocalScriptFunction (binding.func_obj.name);
ec.ig.Emit (OpCodes.Ldloc,
local_meth);
} else {
FieldInfo method =
type.GetField (binding.func_obj.name);
@@ -1662,9 +1728,9 @@
/* function properties of the Global Object */
case "eval":
- throw new NotImplementedException ();
- case "parseInt":
-
+ ig.Emit (OpCodes.Call, typeof (Eval).GetMethod
("JScriptEvaluate"));
+ break;
+ case "parseInt":
ig.Emit (OpCodes.Call, go.GetMethod
("parseInt"));
ig.Emit (OpCodes.Box, typeof (Double));
break;
_______________________________________________
Mono-patches maillist - [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches