Inheritance: ScriptFunction
Beispiel #1
0
		public Closure (FunctionObject func)
		{
			this._prototype = func._prototype;
			this.method = func.method;
			this.vsa_engine = func.vsa_engine;
			this.func = func;
		}
Beispiel #2
0
 public Closure(FunctionObject func)
 {
     this._prototype = func._prototype;
     this.method     = func.method;
     this.vsa_engine = func.vsa_engine;
     this.func       = func;
 }
 internal StaticInitializer(Context context, Block body, FunctionScope own_scope) : base(context)
 {
     this.func                    = new FunctionObject(null, new ParameterDeclaration[0], null, body, own_scope, base.Globals.ScopeStack.Peek(), context, MethodAttributes.Static | MethodAttributes.Private);
     this.func.isMethod           = true;
     this.func.hasArgumentsObject = false;
     this.completion              = new Completion();
 }
Beispiel #4
0
 internal FunctionExpression(AST parent, string name,
                             FormalParameterList p,
                             string return_type, Block body, Location location)
     : base(parent, location)
 {
     func_obj = new FunctionObject(name, p, return_type, body, location);
 }
Beispiel #5
0
        internal void CheckOverloadsForDuplicates()
        {
            JSMemberField current = this;

            while (current != null)
            {
                FunctionObject func = current.value as FunctionObject;
                if (func == null)
                {
                    return;
                }
                for (JSMemberField next = current.nextOverload; next != null; next = next.nextOverload)
                {
                    FunctionObject f = (FunctionObject)next.value;
                    if (f.implementedIface != func.implementedIface)
                    {
                        continue;
                    }
                    if (Class.ParametersMatch(f.parameter_declarations, func.parameter_declarations))
                    {
                        func.funcContext.HandleError(JSError.DuplicateMethod);
                        f.funcContext.HandleError(JSError.DuplicateMethod);
                        break;
                    }
                }
                current = current.nextOverload;
            }
        }
Beispiel #6
0
 internal FunctionScope(ScriptObject parent, bool isMethod) : base(parent)
 {
     base.isKnownAtCompileTime = true;
     this.isMethod             = isMethod;
     this.mustSaveStackLocals  = false;
     if ((parent != null) && (parent is ActivationObject))
     {
         base.fast = ((ActivationObject)parent).fast;
     }
     else
     {
         base.fast = false;
     }
     this.returnVar                   = null;
     this.owner                       = null;
     this.isStatic                    = false;
     this.nested_functions            = null;
     this.fields_for_nested_functions = null;
     if (parent is FunctionScope)
     {
         this.ProvidesOuterScopeLocals = new SimpleHashtable(0x10);
     }
     else
     {
         this.ProvidesOuterScopeLocals = null;
     }
     this.closuresMightEscape = false;
 }
 public Closure(FunctionObject func) : this(func, null)
 {
     if (func.enclosing_scope is Microsoft.JScript.StackFrame)
     {
         this.enclosing_scope = func.enclosing_scope;
     }
 }
 internal FunctionScope(ScriptObject parent, bool isMethod) : base(parent)
 {
     base.isKnownAtCompileTime = true;
     this.isMethod = isMethod;
     this.mustSaveStackLocals = false;
     if ((parent != null) && (parent is ActivationObject))
     {
         base.fast = ((ActivationObject) parent).fast;
     }
     else
     {
         base.fast = false;
     }
     this.returnVar = null;
     this.owner = null;
     this.isStatic = false;
     this.nested_functions = null;
     this.fields_for_nested_functions = null;
     if (parent is FunctionScope)
     {
         this.ProvidesOuterScopeLocals = new SimpleHashtable(0x10);
     }
     else
     {
         this.ProvidesOuterScopeLocals = null;
     }
     this.closuresMightEscape = false;
 }
Beispiel #9
0
 public Closure(FunctionObject func) : this(func, null)
 {
     if (func.enclosing_scope is Microsoft.JScript.StackFrame)
     {
         this.enclosing_scope = func.enclosing_scope;
     }
 }
Beispiel #10
0
		internal FunctionExpression (AST parent, string name, 
					     FormalParameterList p,
					     string return_type, Block body, Location location)
			: base (parent, location)
		{
			func_obj = new FunctionObject (name, p, return_type, body, location);
		}
Beispiel #11
0
 internal StaticInitializer(Context context, Block body, FunctionScope own_scope)
   : base(context) {
   this.func = new FunctionObject(null, new ParameterDeclaration[0], null, body, own_scope, Globals.ScopeStack.Peek(), context, MethodAttributes.Private|MethodAttributes.Static);
   this.func.isMethod = true;
   this.func.hasArgumentsObject = false;
   this.completion = new Completion();
 }
Beispiel #12
0
 internal JSMemberField AddOverload(FunctionObject func, FieldAttributes attributeFlags){
   JSMemberField last = this;
   while (last.nextOverload != null) last = last.nextOverload;
   JSMemberField f = last.nextOverload = new JSMemberField((ClassScope)this.obj, this.Name, func, attributeFlags);
   f.type = this.type;
   return f;
 }
 public Closure(FunctionObject func)
     : this(func, null)
 {
     if (func.enclosing_scope is StackFrame)
     {
         this.enclosing_scope = func.enclosing_scope;
     }
 }
Beispiel #14
0
        public static Closure JScriptFunctionDeclaration(RuntimeTypeHandle handle, String name, String method_name, String[] formal_parameters,
                                                         JSLocalField[] fields, bool must_save_stack_locals, bool hasArgumentsObject, String text, Object declaringObject, VsaEngine engine)
        {
            Type           t    = Type.GetTypeFromHandle(handle);
            FunctionObject func = new FunctionObject(t, name, method_name, formal_parameters, fields, must_save_stack_locals, hasArgumentsObject, text, engine);

            return(new Closure(func, declaringObject));
        }
Beispiel #15
0
        public static FunctionObject JScriptFunctionExpression(RuntimeTypeHandle handle, String name, String method_name, String[] formal_params, JSLocalField[] fields,
                                                               bool must_save_stack_locals, bool hasArgumentsObject, String text, VsaEngine engine)
        {
            Type           t      = Type.GetTypeFromHandle(handle);
            FunctionObject result = new FunctionObject(t, name, method_name, formal_params, fields, must_save_stack_locals, hasArgumentsObject, text, engine);

            return(result);
        }
 internal JSVariableField AddNewField(String name, FieldAttributes attributeFlags, FunctionObject func){
   if (this.nested_functions == null){
     this.nested_functions = new ArrayList();
     this.fields_for_nested_functions = new ArrayList();
   }
   this.nested_functions.Add(func);
   JSVariableField result = this.AddNewField(name, (Object)func, attributeFlags);
   this.fields_for_nested_functions.Add(result);
   return result;
 }
Beispiel #17
0
 internal ArgumentsObject(ScriptObject parent, object[] arguments, FunctionObject function, Closure callee, ScriptObject scope, ArgumentsObject caller) : base(parent)
 {
     this.arguments    = arguments;
     this.formal_names = function.formal_parameters;
     this.scope        = scope;
     this.callee       = callee;
     this.caller       = caller;
     this.length       = arguments.Length;
     base.noExpando    = false;
 }
 internal ArgumentsObject(ScriptObject parent, Object[] arguments, FunctionObject function, Closure callee, ScriptObject scope, ArgumentsObject caller)
   : base(parent) {
   this.arguments = arguments;
   this.formal_names = function.formal_parameters;
   this.scope = scope;
   this.callee = callee;
   this.caller = caller;
   this.length = arguments.Length;
   this.noExpando = false;
 }
 internal JSFieldMethod(FieldInfo field, Object obj)
   : base(obj){ //The object is never used, but it is convenient to have the field on JSMethod.
   this.field = field;
   this.func = null;
   if (!field.IsLiteral)
     return;
   //If we get here, we know at compile time what function we are calling via the field. I.e. we're in fast mode.
   Object val = field is JSVariableField ? ((JSVariableField)field).value : field.GetValue(null);
   if (val is FunctionObject)
     this.func = (FunctionObject)val;
 }
 internal JSMemberField AddOverload(FunctionObject func, FieldAttributes attributeFlags)
 {
     JSMemberField nextOverload = this;
     while (nextOverload.nextOverload != null)
     {
         nextOverload = nextOverload.nextOverload;
     }
     JSMemberField field2 = nextOverload.nextOverload = new JSMemberField((ClassScope) base.obj, this.Name, func, attributeFlags);
     field2.type = base.type;
     return field2;
 }
 internal JSVariableField AddNewField(string name, FieldAttributes attributeFlags, FunctionObject func)
 {
     if (this.nested_functions == null)
     {
         this.nested_functions = new ArrayList();
         this.fields_for_nested_functions = new ArrayList();
     }
     this.nested_functions.Add(func);
     JSVariableField field = this.AddNewField(name, func, attributeFlags);
     this.fields_for_nested_functions.Add(field);
     return field;
 }
Beispiel #22
0
		public static FunctionObject JScriptFunctionExpression (RuntimeTypeHandle handle, string name,
									string methodName, string [] formalParams,
									JSLocalField [] fields, bool mustSaveStackLocals, 
									bool hasArgumentsObject, string text,
									VsaEngine engine)
		{
			MethodInfo method = engine.ScriptObjectStackTop ().GetType ().GetMethod (methodName);
			FunctionObject fun = new FunctionObject (method);
			fun.source = text;
			fun.vsa_engine = engine;
			return fun;
		}
 public static Closure JScriptFunctionDeclaration(RuntimeTypeHandle handle, string name, 
     string methodName, string [] formalParameters,
     JSLocalField [] fields, bool mustSaveStackLocals,
     bool hasArgumentsObjects, string text,
     Object declaringObject, VsaEngine engine)
 {
     FunctionObject f = new FunctionObject (name, null, null, null, null);
     f.source = text;
     MethodInfo method = engine.ScriptObjectStackTop ().GetType ().GetMethod (methodName);
     f.method = method;
     f.vsa_engine = engine;
     return new Closure (f);
 }
Beispiel #24
0
        public static FunctionObject JScriptFunctionExpression(RuntimeTypeHandle handle, string name,
                                                               string methodName, string [] formalParams,
                                                               JSLocalField [] fields, bool mustSaveStackLocals,
                                                               bool hasArgumentsObject, string text,
                                                               VsaEngine engine)
        {
            MethodInfo     method = engine.ScriptObjectStackTop().GetType().GetMethod(methodName);
            FunctionObject fun    = new FunctionObject(method);

            fun.source     = text;
            fun.vsa_engine = engine;
            return(fun);
        }
Beispiel #25
0
        internal JSMemberField AddOverload(FunctionObject func, FieldAttributes attributeFlags)
        {
            JSMemberField last = this;

            while (last.nextOverload != null)
            {
                last = last.nextOverload;
            }
            JSMemberField f = last.nextOverload = new JSMemberField((ClassScope)this.obj, this.Name, func, attributeFlags);

            f.type = this.type;
            return(f);
        }
Beispiel #26
0
 internal JSFieldMethod(FieldInfo field, object obj) : base(obj)
 {
     this.field = field;
     this.func  = null;
     if (field.IsLiteral)
     {
         object obj2 = (field is JSVariableField) ? ((JSVariableField)field).value : field.GetValue(null);
         if (obj2 is FunctionObject)
         {
             this.func = (FunctionObject)obj2;
         }
     }
 }
Beispiel #27
0
        internal JSMemberField AddOverload(FunctionObject func, FieldAttributes attributeFlags)
        {
            JSMemberField nextOverload = this;

            while (nextOverload.nextOverload != null)
            {
                nextOverload = nextOverload.nextOverload;
            }
            JSMemberField field2 = nextOverload.nextOverload = new JSMemberField((ClassScope)base.obj, this.Name, func, attributeFlags);

            field2.type = base.type;
            return(field2);
        }
 internal FunctionExpression(Context context, AST id, ParameterDeclaration[] formal_parameters, TypeExpression return_type, Block body, FunctionScope own_scope, FieldAttributes attributes)
   : base(context){
   if (attributes != (FieldAttributes)0){
     this.context.HandleError(JSError.SyntaxError);
     attributes = (FieldAttributes)0;
   }
   ScriptObject enclosingScope = Globals.ScopeStack.Peek();
   this.name = id.ToString();
   if (this.name.Length == 0)
     this.name = "anonymous "+(uniqueNumber++).ToString(CultureInfo.InvariantCulture);
   else
     this.AddNameTo(enclosingScope);
   this.func = new FunctionObject(this.name, formal_parameters, return_type, body, own_scope, enclosingScope, this.context, MethodAttributes.Static|MethodAttributes.Public);
 }
Beispiel #29
0
        private static bool TryGetNativeProperty(out object result, object obj, string key)
        {
            // * seems to be a wilcard inside member names
            if (key.IndexOf('*') != -1)
            {
                result = null;
                return(false);
            }
            key = MapToInternalName(key);

            if (obj is Closure)
            {
                obj = ((Closure)obj).func;
            }

            Type type = (obj is GlobalScope) ? typeof(GlobalObject) : obj.GetType();

            MemberInfo [] members = type.GetMember(key,
                                                   BindingFlags.FlattenHierarchy | BindingFlags.GetField | BindingFlags.GetProperty |
                                                   BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance);

            if (members.Length != 0)
            {
                MemberInfo member = members [0];

                switch (member.MemberType)
                {
                case MemberTypes.Field:
                    result = ((FieldInfo)member).GetValue(obj);
                    return(true);

                case MemberTypes.Property:
                    MethodInfo method = ((PropertyInfo)member).GetGetMethod();
                    result = method.Invoke(obj, new object [] { });
                    return(true);

                case MemberTypes.Method:
                    result = new FunctionObject((MethodInfo)member);
                    return(true);

                default:
                    Console.WriteLine("TryGetNativeProperty: Unsupported member type {0} for {1}.{2}",
                                      member.MemberType, obj, key);
                    break;
                }
            }

            result = null;
            return(false);
        }
        public static Closure JScriptFunctionDeclaration(RuntimeTypeHandle handle, string name,
                                                         string methodName, string [] formalParameters,
                                                         JSLocalField [] fields, bool mustSaveStackLocals,
                                                         bool hasArgumentsObjects, string text,
                                                         Object declaringObject, VsaEngine engine)
        {
            FunctionObject f = new FunctionObject(name, null, null, null, null);

            f.source = text;
            MethodInfo method = engine.ScriptObjectStackTop().GetType().GetMethod(methodName);

            f.method     = method;
            f.vsa_engine = engine;
            return(new Closure(f));
        }
Beispiel #31
0
 internal void CloseNestedFunctions(StackFrame sf)
 {
     if (this.nested_functions != null)
     {
         IEnumerator enumerator  = this.nested_functions.GetEnumerator();
         IEnumerator enumerator2 = this.fields_for_nested_functions.GetEnumerator();
         while (enumerator.MoveNext() && enumerator2.MoveNext())
         {
             FieldInfo      current = (FieldInfo)enumerator2.Current;
             FunctionObject func    = (FunctionObject)enumerator.Current;
             func.enclosing_scope = sf;
             current.SetValue(sf, new Closure(func));
         }
     }
 }
 internal Closure(FunctionObject func, Object declaringObject)
   : base(func.GetParent(), func.name, func.GetNumberOfFormalParameters()) {
   this.func = func;
   this.engine = func.engine;
   this.proto = new JSPrototypeObject(((ScriptObject)func.proto).GetParent(), this);
   this.enclosing_scope = this.engine.ScriptObjectStackTop();
   this.arguments = DBNull.Value;
   this.caller = DBNull.Value;
   this.declaringObject = declaringObject;
   this.noExpando = func.noExpando;
   if (func.isExpandoMethod){
     StackFrame sf = new StackFrame(new WithObject(this.enclosing_scope, declaringObject), new JSLocalField[0], new Object[0], null);
     this.enclosing_scope = sf;
     sf.closureInstance = declaringObject;
   }
 }
Beispiel #33
0
 internal Closure(FunctionObject func, object declaringObject) : base(func.GetParent(), func.name, func.GetNumberOfFormalParameters())
 {
     this.func            = func;
     base.engine          = func.engine;
     base.proto           = new JSPrototypeObject(((ScriptObject)func.proto).GetParent(), this);
     this.enclosing_scope = base.engine.ScriptObjectStackTop();
     this.arguments       = DBNull.Value;
     this.caller          = DBNull.Value;
     this.declaringObject = declaringObject;
     base.noExpando       = func.noExpando;
     if (func.isExpandoMethod)
     {
         Microsoft.JScript.StackFrame frame = new Microsoft.JScript.StackFrame(new WithObject(this.enclosing_scope, declaringObject), new JSLocalField[0], new object[0], null);
         this.enclosing_scope  = frame;
         frame.closureInstance = declaringObject;
     }
 }
Beispiel #34
0
        internal void CloseNestedFunctions(StackFrame sf)
        {
            if (this.nested_functions == null)
            {
                return;
            }
            IEnumerator funcs  = this.nested_functions.GetEnumerator();
            IEnumerator fields = this.fields_for_nested_functions.GetEnumerator();

            while (funcs.MoveNext() && fields.MoveNext())
            {
                FieldInfo      field = (FieldInfo)fields.Current;
                FunctionObject func  = (FunctionObject)funcs.Current;
                func.enclosing_scope = sf;
                field.SetValue(sf, new Closure(func));
            }
        }
Beispiel #35
0
        internal JSFieldMethod(FieldInfo field, Object obj)
            : base(obj) //The object is never used, but it is convenient to have the field on JSMethod.
        {
            this.field = field;
            this.func  = null;
            if (!field.IsLiteral)
            {
                return;
            }
            //If we get here, we know at compile time what function we are calling via the field. I.e. we're in fast mode.
            Object val = field is JSVariableField ? ((JSVariableField)field).value : field.GetValue(null);

            if (val is FunctionObject)
            {
                this.func = (FunctionObject)val;
            }
        }
        internal FunctionExpression(Context context, AST id, ParameterDeclaration[] formal_parameters, TypeExpression return_type, Block body, FunctionScope own_scope, FieldAttributes attributes) : base(context)
        {
            if (attributes != FieldAttributes.PrivateScope)
            {
                base.context.HandleError(JSError.SyntaxError);
                attributes = FieldAttributes.PrivateScope;
            }
            ScriptObject enclosingScope = base.Globals.ScopeStack.Peek();

            this.name = id.ToString();
            if (this.name.Length == 0)
            {
                this.name = "anonymous " + uniqueNumber++.ToString(CultureInfo.InvariantCulture);
            }
            else
            {
                this.AddNameTo(enclosingScope);
            }
            this.func = new FunctionObject(this.name, formal_parameters, return_type, body, own_scope, enclosingScope, base.context, MethodAttributes.Static | MethodAttributes.Public);
        }
 internal FunctionScope(ScriptObject parent, bool isMethod)
   : base(parent) {
   this.isKnownAtCompileTime = true;
   this.isMethod = isMethod;
   this.mustSaveStackLocals = false;
   if (parent != null && parent is ActivationObject)
     this.fast = ((ActivationObject)parent).fast;
   else
     this.fast = false;
   this.returnVar = null;
   this.owner = null; //Given its real value inside new FunctionObject
   this.isStatic = false; //Given its real value elsewhere
   this.nested_functions = null;
   this.fields_for_nested_functions = null;
   if (parent is FunctionScope)
     this.ProvidesOuterScopeLocals = new SimpleHashtable(16);
   else
     this.ProvidesOuterScopeLocals = null;
   this.closuresMightEscape = false;
 }
Beispiel #38
0
 internal void CheckOverloadsForDuplicates()
 {
     for (JSMemberField field = this; field != null; field = field.nextOverload)
     {
         FunctionObject obj2 = field.value as FunctionObject;
         if (obj2 == null)
         {
             return;
         }
         for (JSMemberField field2 = field.nextOverload; field2 != null; field2 = field2.nextOverload)
         {
             FunctionObject obj3 = (FunctionObject)field2.value;
             if ((obj3.implementedIface == obj2.implementedIface) && Class.ParametersMatch(obj3.parameter_declarations, obj2.parameter_declarations))
             {
                 obj2.funcContext.HandleError(JSError.DuplicateMethod);
                 obj3.funcContext.HandleError(JSError.DuplicateMethod);
                 break;
             }
         }
     }
 }
Beispiel #39
0
        internal override Object Invoke(Object obj, Object thisob, BindingFlags options, Binder binder, Object[] parameters, CultureInfo culture)
        {
            bool   construct = (options & BindingFlags.CreateInstance) != 0;
            bool   brackets  = (options & BindingFlags.GetProperty) != 0 && (options & BindingFlags.InvokeMethod) == 0;
            Object f         = this.func;

            if (f == null)
            {
                f = this.field.GetValue(this.obj);
            }
            FunctionObject func = f as FunctionObject;
            JSObject       jsOb = obj as JSObject;

            if (jsOb != null && func != null && func.isMethod && (func.attributes & MethodAttributes.Virtual) != 0 &&
                jsOb.GetParent() != func.enclosing_scope && ((ClassScope)func.enclosing_scope).HasInstance(jsOb))
            {
                LateBinding lb = new LateBinding(func.name);
                lb.obj = jsOb;
                return(lb.Call(parameters, construct, brackets, ((ScriptObject)this.obj).engine));
            }
            return(LateBinding.CallValue(f, parameters, construct, brackets, ((ScriptObject)this.obj).engine, thisob, binder, culture, null));
        }
Beispiel #40
0
        internal override object Invoke(object obj, object thisob, BindingFlags options, Binder binder, object[] parameters, CultureInfo culture)
        {
            bool   construct = (options & BindingFlags.CreateInstance) != BindingFlags.Default;
            bool   brackets  = ((options & BindingFlags.GetProperty) != BindingFlags.Default) && ((options & BindingFlags.InvokeMethod) == BindingFlags.Default);
            object func      = this.func;

            if (func == null)
            {
                func = this.field.GetValue(base.obj);
            }
            FunctionObject obj3 = func as FunctionObject;
            JSObject       ob   = obj as JSObject;

            if ((((ob != null) && (obj3 != null)) && (obj3.isMethod && ((obj3.attributes & MethodAttributes.Virtual) != MethodAttributes.PrivateScope))) && ((ob.GetParent() != obj3.enclosing_scope) && ((ClassScope)obj3.enclosing_scope).HasInstance(ob)))
            {
                LateBinding binding = new LateBinding(obj3.name)
                {
                    obj = ob
                };
                return(binding.Call(parameters, construct, brackets, ((ScriptObject)base.obj).engine));
            }
            return(LateBinding.CallValue(func, parameters, construct, brackets, ((ScriptObject)base.obj).engine, thisob, binder, culture, null));
        }
Beispiel #41
0
        internal ConstructorInfo[] GetAsConstructors(object proto)
        {
            JSMemberField nextOverload = this;
            int           num          = 0;

            while (nextOverload != null)
            {
                nextOverload = nextOverload.nextOverload;
                num++;
            }
            ConstructorInfo[] infoArray = new ConstructorInfo[num];
            nextOverload = this;
            num          = 0;
            while (nextOverload != null)
            {
                FunctionObject cons = (FunctionObject)nextOverload.value;
                cons.isConstructor = true;
                cons.proto         = proto;
                infoArray[num++]   = new JSConstructor(cons);
                nextOverload       = nextOverload.nextOverload;
            }
            return(infoArray);
        }
Beispiel #42
0
        internal ConstructorInfo[] GetAsConstructors(Object proto)
        {
            JSMemberField field = this;
            int           n     = 0;

            while (field != null)
            {
                field = field.nextOverload;
                n++;
            }
            ConstructorInfo[] result = new ConstructorInfo[n];
            field = this;
            n     = 0;
            while (field != null)
            {
                Debug.Assert(field.IsLiteral);
                FunctionObject func = (FunctionObject)field.value;
                func.isConstructor = true;
                func.proto         = proto;
                result[n++]        = new JSConstructor(func);
                field = field.nextOverload;
            }
            return(result);
        }
Beispiel #43
0
 public static Closure JScriptFunctionDeclaration(RuntimeTypeHandle handle, String name, String method_name, String[] formal_parameters, 
   JSLocalField[] fields, bool must_save_stack_locals, bool hasArgumentsObject, String text, Object declaringObject, VsaEngine engine){
   Type t = Type.GetTypeFromHandle(handle);
   FunctionObject func = new FunctionObject(t, name, method_name, formal_parameters, fields, must_save_stack_locals, hasArgumentsObject, text, engine);
   return new Closure(func, declaringObject);
 }
 public Closure(FunctionObject func)
   : this(func, null){
   if (func.enclosing_scope is StackFrame)
     this.enclosing_scope = func.enclosing_scope;
 }
Beispiel #45
0
 internal JSConstructor(FunctionObject cons)
 {
     this.cons = cons;
 }
 internal JSConstructor(FunctionObject cons){
   this.cons = cons;
 }
Beispiel #47
0
	public override Object Eval(VsaEngine engine)
#line 322 "./Nodes/JNode.tc"
	{
		// Get the scope to declare the function within.
		ScriptObject scope = engine.ScriptObjectStackTop();
	
		// Create a function object to wrap up this function.
		FunctionObject obj = new FunctionObject
			(EngineInstance.GetEngineInstance(engine).GetFunctionPrototype(),
			 this, scope);
		
		// javascript allows for anonymous functions
		if(name != null)
		{
			// Add the function to the scope.
			if(scope is IVariableAccess)
			{
				((IVariableAccess)scope).SetVariable(name, obj);
			}
			else
			{
				scope.Put(name, obj);
			}
		}
	
		// Return compiled function object
		return obj;
	}
Beispiel #48
0
      internal FunctionDeclaration(Context context, AST ifaceId, IdentifierLiteral id, ParameterDeclaration[] formal_parameters, TypeExpression return_type, 
                                   Block body, FunctionScope own_scope, FieldAttributes attributes, 
                                   bool isMethod, bool isGetter, bool isSetter, bool isAbstract, bool isFinal, CustomAttributeList customAttributes)
        : base(context) {
        MethodAttributes methodAttributes = (MethodAttributes)0;
        if ((attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Public)
          methodAttributes = MethodAttributes.Public;
        else if ((attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Private)
          methodAttributes = MethodAttributes.Private;
        else if ((attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Assembly)
          methodAttributes = MethodAttributes.Assembly;
        else if ((attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Family)
          methodAttributes = MethodAttributes.Family;
        else if ((attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.FamORAssem)
          methodAttributes = MethodAttributes.FamORAssem;
        else
          methodAttributes = MethodAttributes.Public;

        if ((attributes & FieldAttributes.Static) != 0 || !isMethod)
          methodAttributes |= MethodAttributes.Static;
        else
          methodAttributes |= MethodAttributes.Virtual | MethodAttributes.NewSlot;
        
        if (isAbstract)
          methodAttributes |= MethodAttributes.Abstract;
        if (isFinal)
          methodAttributes |= MethodAttributes.Final;
        
        this.name = id.ToString();
        this.isMethod = isMethod;
        if (ifaceId != null){
          if (isMethod){
            this.ifaceId = new TypeExpression(ifaceId);
            methodAttributes &= ~MethodAttributes.MemberAccessMask;
            methodAttributes |= MethodAttributes.Private|MethodAttributes.Final;
          }else{
            this.declaringObject = new Member(ifaceId.context, ifaceId, id);
            this.name = this.declaringObject.ToString();
          }
        }
        ScriptObject enclosingScope = Globals.ScopeStack.Peek();
        if (attributes == 0 && !isAbstract && !isFinal){
          if (enclosingScope is ClassScope)
            attributes |= FieldAttributes.Public;
        }else{
          if (!(enclosingScope is ClassScope)){
            this.context.HandleError(JSError.NotInsideClass);
            attributes = (FieldAttributes)0;
            methodAttributes = MethodAttributes.Public;
          }
        }
        if (enclosingScope is ActivationObject){
          this.inFastScope = ((ActivationObject)enclosingScope).fast;
          // if later on originalName != this.name this is a property getter/setter
          String originalName = this.name; 
          // mangle the name 
          if (isGetter){
            methodAttributes |= MethodAttributes.SpecialName;
            this.name = "get_" + this.name;
            if (return_type == null)
              return_type = new TypeExpression(new ConstantWrapper(Typeob.Object, context));
          }else if (isSetter){
            methodAttributes |= MethodAttributes.SpecialName;
            this.name = "set_" + this.name;
            return_type = new TypeExpression(new ConstantWrapper(Typeob.Void, context));
          }
          attributes &= FieldAttributes.FieldAccessMask;
          // create the function object
          this.func = new FunctionObject(this.name, formal_parameters, return_type, body, own_scope, enclosingScope, this.context, 
                                         methodAttributes, customAttributes, this.isMethod);
          if (this.declaringObject != null) return;
          // check whether the function name (possibly mangled) is in use already
          String fieldName = this.name;
          if (this.ifaceId != null) fieldName = ifaceId.ToString()+"."+fieldName;
          JSVariableField localField = (JSVariableField)((ActivationObject)enclosingScope).name_table[fieldName];
          if (localField != null && (!(localField is JSMemberField) || !(((JSMemberField)localField).value is FunctionObject) || this.func.isExpandoMethod)){
            if (originalName != this.name)
              localField.originalContext.HandleError(JSError.ClashWithProperty);
            else{
              id.context.HandleError(JSError.DuplicateName, this.func.isExpandoMethod);
              if (localField.value is FunctionObject)
                ((FunctionObject)localField.value).suppressIL = true;
            }
          }
          // create or update the proper field
          if (this.isMethod){
            if (!(localField is JSMemberField) || !(((JSMemberField)localField).value is FunctionObject) || originalName != this.name){
              this.field = ((ActivationObject)enclosingScope).AddNewField(fieldName, this.func, attributes|FieldAttributes.Literal);
              if (originalName == this.name) // if it is a property do not assign the type
                ((JSVariableField)this.field).type = new TypeExpression(new ConstantWrapper(Typeob.FunctionWrapper, this.context));
            }else
              this.field = ((JSMemberField)localField).AddOverload(this.func, attributes|FieldAttributes.Literal);
          }else if (enclosingScope is FunctionScope){
            if (this.inFastScope) attributes |= FieldAttributes.Literal;
            this.field = ((FunctionScope)enclosingScope).AddNewField(this.name, attributes, this.func);
            if (this.field is JSLocalField){
              JSLocalField locField = (JSLocalField)this.field;
              if (this.inFastScope){
                locField.type = new TypeExpression(new ConstantWrapper(Typeob.ScriptFunction, this.context));
                locField.attributeFlags |= FieldAttributes.Literal;
              }
              locField.debugOn = this.context.document.debugOn;
              locField.isDefined = true;
            }
          }else if (this.inFastScope){
            this.field = ((ActivationObject)enclosingScope).AddNewField(this.name, this.func, attributes|FieldAttributes.Literal);
            ((JSVariableField)this.field).type = new TypeExpression(new ConstantWrapper(Typeob.ScriptFunction, this.context));
            //Do not use typeof(Closure) for the field, since that has the arguments and callee properties, which are not
            //accessible in fast mode
          }else //enclosingScope is GlobalObject
            this.field = ((ActivationObject)enclosingScope).AddNewField(this.name, this.func, attributes|FieldAttributes.Static);
          ((JSVariableField)this.field).originalContext = context;
          
          // if it is a property create/update the PropertyInfo and assign the getter/setter
          if (originalName != this.name){
            String propertyFieldName = originalName;
            if (this.ifaceId != null) propertyFieldName = ifaceId.ToString()+"."+originalName;
            FieldInfo prop = (FieldInfo)((ClassScope)enclosingScope).name_table[propertyFieldName];
            if (prop != null){
              // check whether a property was defined already
              if (prop.IsLiteral){
                Object val = ((JSVariableField)prop).value;
                if (val is JSProperty)
                  this.enclosingProperty = (JSProperty)val;
              }
              if (this.enclosingProperty == null)
                id.context.HandleError(JSError.DuplicateName, true); // the matching name was not a property
            }
            if (this.enclosingProperty == null){
              this.enclosingProperty = new JSProperty(originalName);
              prop = ((ActivationObject)enclosingScope).AddNewField(propertyFieldName, this.enclosingProperty, attributes|FieldAttributes.Literal);
              ((JSMemberField)prop).originalContext = this.context;
            }else{
              if ((isGetter && this.enclosingProperty.getter != null) || (isSetter && this.enclosingProperty.setter != null))
                id.context.HandleError(JSError.DuplicateName, true); // duplicated setter or getter
            }
            if (isGetter)
              this.enclosingProperty.getter = new JSFieldMethod(this.field, enclosingScope);
            else
              this.enclosingProperty.setter = new JSFieldMethod(this.field, enclosingScope);
          }
        }else{ //Might get here if function declaration is inside of an eval.
          this.inFastScope = false;
          this.func = new FunctionObject(this.name, formal_parameters, return_type, body, own_scope, enclosingScope, this.context, MethodAttributes.Public, null, false);
          this.field = ((StackFrame)enclosingScope).AddNewField(this.name, new Closure(this.func), attributes|FieldAttributes.Static);
        }
      }
Beispiel #49
0
      private void CheckMatchingMethodForConsistency(MethodInfo matchingMethod, FunctionObject func, int i, int n){
        // return-type consistency - the return-type has to be the same
        IReflect rir = func.ReturnType(null);
        IReflect mrir = matchingMethod is JSFieldMethod ? ((JSFieldMethod)matchingMethod).func.ReturnType(null) : matchingMethod.ReturnType;
        if (!rir.Equals(mrir)){
          func.funcContext.HandleError(JSError.DifferentReturnTypeFromBase, func.name, true);
          return;
        }
        
        //Special treatment for methods that implement interface methods    
        if (func.implementedIface != null){
          func.implementedIfaceMethod = matchingMethod;
          this.superMembers[i] = func.name; //obliterate it so that it does not show up as unimplemented
          return;
        }

        // visibility consistency - the visibility specification has to be the same
        MethodAttributes visibility = func.attributes & MethodAttributes.MemberAccessMask;
        if ((matchingMethod.Attributes & MethodAttributes.MemberAccessMask) != visibility)
          //Allow Family to match FamORAssem
          if ((matchingMethod.Attributes & MethodAttributes.MemberAccessMask) != MethodAttributes.FamORAssem || visibility != MethodAttributes.Family)
            func.funcContext.HandleError(JSError.CannotChangeVisibility);

        // hiding, overriding and layout consistency
        // if i >= 0 after this, the base method is an overridden abstract method and steps should be taken to prevent a not implemented error
        if (func.noVersionSafeAttributeSpecified){ // current method does not specify any attribute (i.e. hide or override)
          if (this.Engine.versionSafe){
            //Give a message. The compiler option requires a method to say hide or override when there is a match.
            if ((matchingMethod.Attributes & MethodAttributes.Abstract) != 0){ // base is abstract
              func.funcContext.HandleError(JSError.HidesAbstractInBase, this.name + "." + func.name);
              func.attributes &= ~MethodAttributes.NewSlot;
              //Recover from error by overriding, it may be less bad than throwing a class load exception
            }else{ 
              func.funcContext.HandleError(JSError.NewNotSpecifiedInMethodDeclaration, this.IsInTheSameCompilationUnit(matchingMethod));
              i = -1;
            }
          }else{
            //No message, override if possible, otherwise hide
            if ((matchingMethod.Attributes & MethodAttributes.Virtual) == 0 || 
                (matchingMethod.Attributes & MethodAttributes.Final) != 0){ // base is non virtual or final, hide
              i = -1;
            }else{
              func.attributes &= ~MethodAttributes.NewSlot; //override
              if ((matchingMethod.Attributes & MethodAttributes.Abstract) == 0)
                i = -1;
            }
          }
        }else{ //Current method is marked override or hide
          if ((func.attributes & MethodAttributes.VtableLayoutMask) == MethodAttributes.ReuseSlot){ // current method specifies override
            if ((matchingMethod.Attributes & MethodAttributes.Virtual) == 0 || 
              (matchingMethod.Attributes & MethodAttributes.Final) != 0){ // base is non virtual or final, hide
              func.funcContext.HandleError(JSError.MethodInBaseIsNotVirtual);
              i = -1;
            }else{ 
              func.attributes &= ~MethodAttributes.NewSlot; //override
              if ((matchingMethod.Attributes & MethodAttributes.Abstract) == 0)
                i = -1;
            }
          }else{ // current method specifies hide
            Debug.Assert((func.attributes & MethodAttributes.VtableLayoutMask) == MethodAttributes.NewSlot);
            if ((matchingMethod.Attributes & MethodAttributes.Abstract) != 0){ // base is abstract
              func.funcContext.HandleError(JSError.HidesAbstractInBase, this.name + "." + func.name);
              func.attributes &= ~MethodAttributes.NewSlot;
              //Recover from error by overriding, it may be less bad than throwing a class load exception
            }else
              i = -1;
          }
        }

        if (i >= 0){
          //Overriding an abstract method. Take steps to prevent error messages.
          Debug.Assert((matchingMethod.Attributes & MethodAttributes.Abstract) != 0);
          this.superMembers[i] = func.name; //obliterate it so that it does not show up as unimplemented
          //Do likewise for any matching abstract members declared in less derived base classes
          for (int j = i+1; j < n; j++){ //Most derived class is always first
            MemberInfo mem = this.superMembers[j] as MemberInfo;
            if (mem == null) continue;
            if (mem.Name != matchingMethod.Name) break;
            MethodInfo meth2 = mem as MethodInfo;
            if (meth2 == null) continue;
            if (meth2.IsAbstract && Class.ParametersMatch(meth2.GetParameters(), matchingMethod.GetParameters())){
              IReflect rt = matchingMethod is JSFieldMethod ? ((JSFieldMethod)matchingMethod).ReturnIR() : matchingMethod.ReturnType;
              IReflect rt2 = meth2 is JSFieldMethod ? ((JSFieldMethod)meth2).ReturnIR() : meth2.ReturnType;
              if (rt == rt2) this.superMembers[j] = func.name;
            }
          }
        }
      }
Beispiel #50
0
      private void CheckMethodDeclarationConsistency(FunctionObject func){
        if (func.isStatic && !func.isExpandoMethod) return; //static methods do not clash with superclass methods
        if (func.isConstructor) return; //Constructors cannot clash with superclass members
        Object index = this.firstIndex[func.name];
        if (index == null){
          //There is no super class member with the same name as the function
          this.CheckThatMethodIsNotMarkedWithOverrideOrHide(func);
          if ((func.attributes & MethodAttributes.Final) != 0)
            func.attributes &= ~(MethodAttributes.Virtual|MethodAttributes.NewSlot|MethodAttributes.Final);
          return;
        }
        MemberInfo differentTypeOfMember = null;

        for (int i = (int)index, n = this.superMembers.Length; i < n; i++){
          MemberInfo member = this.superMembers[i] as MemberInfo;
          if (member == null) 
            //if we do not get a MemberInfo we have already processed this superclass member and it matches another member of the current class
            continue;
          if (!member.Name.Equals(func.name)) break;
          if (!this.CanSee(member)) continue;
          if (member.MemberType != MemberTypes.Method){
            //JScript does not allow overloading among different member types. 
            //Unless there is a superclass method with the same signature as the method, we have to give an error 
            differentTypeOfMember = member;
            continue;
          }
          if (func.isExpandoMethod){
            differentTypeOfMember = member;
            break;
          }
          MethodInfo supmeth = (MethodInfo)member;
          if (func.implementedIface != null){
            //Skip superclass methods that do not come from the appropriate interface
            if (supmeth is JSFieldMethod){
              if (((JSFieldMethod)supmeth).EnclosingScope() != func.implementedIface) continue;
            }else{
              if (supmeth.DeclaringType != func.implementedIface) continue;
            }
          }
          if (Class.ParametersMatch(supmeth.GetParameters(), func.parameter_declarations)){
            if (supmeth is JSWrappedMethod)
              supmeth = ((JSWrappedMethod)supmeth).method;
            if (func.noVersionSafeAttributeSpecified || (func.attributes & MethodAttributes.VtableLayoutMask) != MethodAttributes.NewSlot){
              //Check consistency of implicit or explicit override (a hiding method may be inconsistent)
              this.CheckMatchingMethodForConsistency(supmeth, func, i, n);
            }
            return;
          }
        }
        if (differentTypeOfMember != null){
          //Did not find a superclass method with the same signature, but did find a member with same name that was not a method
          //This is a no no for JScript unless the hide attribute has been specified
          if (func.noVersionSafeAttributeSpecified || 
            (func.attributes & MethodAttributes.VtableLayoutMask) != MethodAttributes.NewSlot && !func.isExpandoMethod){
            String supMemberName = this.GetFullNameFor(differentTypeOfMember);
            func.funcContext.HandleError(JSError.HidesParentMember, supMemberName, this.IsInTheSameCompilationUnit(differentTypeOfMember));
          }
          return;
        }
        //No matching method in superclass. Give an error if hide/override was specified
        this.CheckThatMethodIsNotMarkedWithOverrideOrHide(func);
        //Make final methods into non virtual
        if ((func.attributes & MethodAttributes.Final) != 0)
          func.attributes &= ~(MethodAttributes.Virtual|MethodAttributes.NewSlot|MethodAttributes.Final);
      }
Beispiel #51
0
 private void AllocateImplicitDefaultConstructor(){
   this.implicitDefaultConstructor = new FunctionObject(".ctor", new ParameterDeclaration[0], null, new Block(this.context), 
     new FunctionScope(this.classob, true), this.classob, this.context, MethodAttributes.Virtual|MethodAttributes.NewSlot|MethodAttributes.Public, null, true);
   this.implicitDefaultConstructor.isImplicitCtor = true; 
   this.implicitDefaultConstructor.isConstructor = true;  
   this.implicitDefaultConstructor.proto = this.classob;
 }
Beispiel #52
0
        internal FunctionDeclaration(Context context, AST ifaceId, IdentifierLiteral id, ParameterDeclaration[] formal_parameters, TypeExpression return_type,
                                     Block body, FunctionScope own_scope, FieldAttributes attributes,
                                     bool isMethod, bool isGetter, bool isSetter, bool isAbstract, bool isFinal, CustomAttributeList customAttributes)
            : base(context)
        {
            MethodAttributes methodAttributes = (MethodAttributes)0;

            if ((attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Public)
            {
                methodAttributes = MethodAttributes.Public;
            }
            else if ((attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Private)
            {
                methodAttributes = MethodAttributes.Private;
            }
            else if ((attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Assembly)
            {
                methodAttributes = MethodAttributes.Assembly;
            }
            else if ((attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Family)
            {
                methodAttributes = MethodAttributes.Family;
            }
            else if ((attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.FamORAssem)
            {
                methodAttributes = MethodAttributes.FamORAssem;
            }
            else
            {
                methodAttributes = MethodAttributes.Public;
            }

            if ((attributes & FieldAttributes.Static) != 0 || !isMethod)
            {
                methodAttributes |= MethodAttributes.Static;
            }
            else
            {
                methodAttributes |= MethodAttributes.Virtual | MethodAttributes.NewSlot;
            }

            if (isAbstract)
            {
                methodAttributes |= MethodAttributes.Abstract;
            }
            if (isFinal)
            {
                methodAttributes |= MethodAttributes.Final;
            }

            this.name     = id.ToString();
            this.isMethod = isMethod;
            if (ifaceId != null)
            {
                if (isMethod)
                {
                    this.ifaceId      = new TypeExpression(ifaceId);
                    methodAttributes &= ~MethodAttributes.MemberAccessMask;
                    methodAttributes |= MethodAttributes.Private | MethodAttributes.Final;
                }
                else
                {
                    this.declaringObject = new Member(ifaceId.context, ifaceId, id);
                    this.name            = this.declaringObject.ToString();
                }
            }
            ScriptObject enclosingScope = Globals.ScopeStack.Peek();

            if (attributes == 0 && !isAbstract && !isFinal)
            {
                if (enclosingScope is ClassScope)
                {
                    attributes |= FieldAttributes.Public;
                }
            }
            else
            {
                if (!(enclosingScope is ClassScope))
                {
                    this.context.HandleError(JSError.NotInsideClass);
                    attributes       = (FieldAttributes)0;
                    methodAttributes = MethodAttributes.Public;
                }
            }
            if (enclosingScope is ActivationObject)
            {
                this.inFastScope = ((ActivationObject)enclosingScope).fast;
                // if later on originalName != this.name this is a property getter/setter
                String originalName = this.name;
                // mangle the name
                if (isGetter)
                {
                    methodAttributes |= MethodAttributes.SpecialName;
                    this.name         = "get_" + this.name;
                    if (return_type == null)
                    {
                        return_type = new TypeExpression(new ConstantWrapper(Typeob.Object, context));
                    }
                }
                else if (isSetter)
                {
                    methodAttributes |= MethodAttributes.SpecialName;
                    this.name         = "set_" + this.name;
                    return_type       = new TypeExpression(new ConstantWrapper(Typeob.Void, context));
                }
                attributes &= FieldAttributes.FieldAccessMask;
                // create the function object
                this.func = new FunctionObject(this.name, formal_parameters, return_type, body, own_scope, enclosingScope, this.context,
                                               methodAttributes, customAttributes, this.isMethod);
                if (this.declaringObject != null)
                {
                    return;
                }
                // check whether the function name (possibly mangled) is in use already
                String fieldName = this.name;
                if (this.ifaceId != null)
                {
                    fieldName = ifaceId.ToString() + "." + fieldName;
                }
                JSVariableField localField = (JSVariableField)((ActivationObject)enclosingScope).name_table[fieldName];
                if (localField != null && (!(localField is JSMemberField) || !(((JSMemberField)localField).value is FunctionObject) || this.func.isExpandoMethod))
                {
                    if (originalName != this.name)
                    {
                        localField.originalContext.HandleError(JSError.ClashWithProperty);
                    }
                    else
                    {
                        id.context.HandleError(JSError.DuplicateName, this.func.isExpandoMethod);
                        if (localField.value is FunctionObject)
                        {
                            ((FunctionObject)localField.value).suppressIL = true;
                        }
                    }
                }
                // create or update the proper field
                if (this.isMethod)
                {
                    if (!(localField is JSMemberField) || !(((JSMemberField)localField).value is FunctionObject) || originalName != this.name)
                    {
                        this.field = ((ActivationObject)enclosingScope).AddNewField(fieldName, this.func, attributes | FieldAttributes.Literal);
                        if (originalName == this.name) // if it is a property do not assign the type
                        {
                            ((JSVariableField)this.field).type = new TypeExpression(new ConstantWrapper(Typeob.FunctionWrapper, this.context));
                        }
                    }
                    else
                    {
                        this.field = ((JSMemberField)localField).AddOverload(this.func, attributes | FieldAttributes.Literal);
                    }
                }
                else if (enclosingScope is FunctionScope)
                {
                    if (this.inFastScope)
                    {
                        attributes |= FieldAttributes.Literal;
                    }
                    this.field = ((FunctionScope)enclosingScope).AddNewField(this.name, attributes, this.func);
                    if (this.field is JSLocalField)
                    {
                        JSLocalField locField = (JSLocalField)this.field;
                        if (this.inFastScope)
                        {
                            locField.type            = new TypeExpression(new ConstantWrapper(Typeob.ScriptFunction, this.context));
                            locField.attributeFlags |= FieldAttributes.Literal;
                        }
                        locField.debugOn   = this.context.document.debugOn;
                        locField.isDefined = true;
                    }
                }
                else if (this.inFastScope)
                {
                    this.field = ((ActivationObject)enclosingScope).AddNewField(this.name, this.func, attributes | FieldAttributes.Literal);
                    ((JSVariableField)this.field).type = new TypeExpression(new ConstantWrapper(Typeob.ScriptFunction, this.context));
                    //Do not use typeof(Closure) for the field, since that has the arguments and callee properties, which are not
                    //accessible in fast mode
                }
                else //enclosingScope is GlobalObject
                {
                    this.field = ((ActivationObject)enclosingScope).AddNewField(this.name, this.func, attributes | FieldAttributes.Static);
                }
                ((JSVariableField)this.field).originalContext = context;

                // if it is a property create/update the PropertyInfo and assign the getter/setter
                if (originalName != this.name)
                {
                    String propertyFieldName = originalName;
                    if (this.ifaceId != null)
                    {
                        propertyFieldName = ifaceId.ToString() + "." + originalName;
                    }
                    FieldInfo prop = (FieldInfo)((ClassScope)enclosingScope).name_table[propertyFieldName];
                    if (prop != null)
                    {
                        // check whether a property was defined already
                        if (prop.IsLiteral)
                        {
                            Object val = ((JSVariableField)prop).value;
                            if (val is JSProperty)
                            {
                                this.enclosingProperty = (JSProperty)val;
                            }
                        }
                        if (this.enclosingProperty == null)
                        {
                            id.context.HandleError(JSError.DuplicateName, true); // the matching name was not a property
                        }
                    }
                    if (this.enclosingProperty == null)
                    {
                        this.enclosingProperty = new JSProperty(originalName);
                        prop = ((ActivationObject)enclosingScope).AddNewField(propertyFieldName, this.enclosingProperty, attributes | FieldAttributes.Literal);
                        ((JSMemberField)prop).originalContext = this.context;
                    }
                    else
                    {
                        if ((isGetter && this.enclosingProperty.getter != null) || (isSetter && this.enclosingProperty.setter != null))
                        {
                            id.context.HandleError(JSError.DuplicateName, true); // duplicated setter or getter
                        }
                    }
                    if (isGetter)
                    {
                        this.enclosingProperty.getter = new JSFieldMethod(this.field, enclosingScope);
                    }
                    else
                    {
                        this.enclosingProperty.setter = new JSFieldMethod(this.field, enclosingScope);
                    }
                }
            }
            else //Might get here if function declaration is inside of an eval.
            {
                this.inFastScope = false;
                this.func        = new FunctionObject(this.name, formal_parameters, return_type, body, own_scope, enclosingScope, this.context, MethodAttributes.Public, null, false);
                this.field       = ((StackFrame)enclosingScope).AddNewField(this.name, new Closure(this.func), attributes | FieldAttributes.Static);
            }
        }
 public static FunctionObject JScriptFunctionExpression(RuntimeTypeHandle handle, String name, String method_name, String[] formal_params, JSLocalField[] fields,
 bool must_save_stack_locals, bool hasArgumentsObject, String text, VsaEngine engine){
   Type t = Type.GetTypeFromHandle(handle);
   FunctionObject result = new FunctionObject(t, name, method_name, formal_params, fields, must_save_stack_locals, hasArgumentsObject, text, engine);
   return result;
 }
Beispiel #54
0
        private static bool TryGetNativeProperty(out object result, object obj, string key)
        {
            // * seems to be a wilcard inside member names
            if (key.IndexOf ('*') != -1) {
                result = null;
                return false;
            }
            key = MapToInternalName (key);

            if (obj is Closure)
                obj = ((Closure) obj).func;

            Type type = (obj is GlobalScope) ? typeof (GlobalObject) : obj.GetType ();
            MemberInfo [] members = type.GetMember (key,
                BindingFlags.FlattenHierarchy | BindingFlags.GetField | BindingFlags.GetProperty |
                BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance);

            if (members.Length != 0) {
                MemberInfo member = members [0];

                switch (member.MemberType) {
                case MemberTypes.Field:
                    result = ((FieldInfo) member).GetValue (obj);
                    return true;

                case MemberTypes.Property:
                    MethodInfo method = ((PropertyInfo) member).GetGetMethod ();
                    result = method.Invoke (obj, new object [] { });
                    return true;

                case MemberTypes.Method:
                    result = new FunctionObject ((MethodInfo) member);
                    return true;

                default:
                    Console.WriteLine ("TryGetNativeProperty: Unsupported member type {0} for {1}.{2}",
                        member.MemberType, obj, key);
                    break;
                }
            }

            result = null;
            return false;
        }
        public override MemberInfo[] GetMember(string name, BindingFlags bindingAttr)
        {
            MemberInfoList mems = new MemberInfoList();
            FieldInfo      elem = (FieldInfo)base.name_table[name];

            if (elem == null)
            {
                goto Label_0139;
            }
            if (elem.IsPublic)
            {
                if ((bindingAttr & BindingFlags.Public) != BindingFlags.Default)
                {
                    goto Label_0040;
                }
                goto Label_0139;
            }
            if ((bindingAttr & BindingFlags.NonPublic) == BindingFlags.Default)
            {
                goto Label_0139;
            }
Label_0040:
            if (!elem.IsLiteral)
            {
                goto Label_011E;
            }
            object obj2 = ((JSMemberField)elem).value;

            if (obj2 is FunctionObject)
            {
                FunctionObject obj3 = (FunctionObject)obj2;
                if (obj3.isConstructor)
                {
                    return(new MemberInfo[0]);
                }
                if (obj3.isExpandoMethod)
                {
                    if ((bindingAttr & BindingFlags.Instance) != BindingFlags.Default)
                    {
                        mems.Add(elem);
                    }
                }
                else
                {
                    ((JSMemberField)elem).AddOverloadedMembers(mems, this, bindingAttr | BindingFlags.DeclaredOnly);
                }
                goto Label_0139;
            }
            if (!(obj2 is JSProperty))
            {
                if (((obj2 is ClassScope) && ((bindingAttr & BindingFlags.Instance) != BindingFlags.Default)) && !((ClassScope)obj2).owner.isStatic)
                {
                    mems.Add(elem);
                    goto Label_0139;
                }
                goto Label_011E;
            }
            JSProperty property = (JSProperty)obj2;
            MethodInfo info2    = (property.getter != null) ? property.getter : property.setter;

            if (info2.IsStatic)
            {
                if ((bindingAttr & BindingFlags.Static) != BindingFlags.Default)
                {
                    goto Label_00EC;
                }
                goto Label_0139;
            }
            if ((bindingAttr & BindingFlags.Instance) == BindingFlags.Default)
            {
                goto Label_0139;
            }
Label_00EC:
            mems.Add(property);
            goto Label_0139;
Label_011E:
            if (elem.IsStatic)
            {
                if ((bindingAttr & BindingFlags.Static) != BindingFlags.Default)
                {
                    goto Label_0132;
                }
                goto Label_0139;
            }
            if ((bindingAttr & BindingFlags.Instance) == BindingFlags.Default)
            {
                goto Label_0139;
            }
Label_0132:
            mems.Add(elem);
Label_0139:
            if (((this.owner != null) && this.owner.isInterface) && ((bindingAttr & BindingFlags.DeclaredOnly) == BindingFlags.Default))
            {
                return(this.owner.GetInterfaceMember(name));
            }
            if ((base.parent != null) && ((bindingAttr & BindingFlags.DeclaredOnly) == BindingFlags.Default))
            {
                MemberInfo[] member = base.parent.GetMember(name, bindingAttr);
                if (member != null)
                {
                    foreach (MemberInfo info3 in member)
                    {
                        if (info3.MemberType == MemberTypes.Field)
                        {
                            elem = (FieldInfo)info3;
                            if ((!elem.IsStatic && !elem.IsLiteral) && !(elem is JSWrappedField))
                            {
                                elem = new JSWrappedField(elem, base.parent);
                            }
                            mems.Add(elem);
                        }
                        else
                        {
                            mems.Add(ScriptObject.WrapMember(info3, base.parent));
                        }
                    }
                }
            }
            return(mems.ToArray());
        }
 internal FunctionDeclaration(Context context, AST ifaceId, IdentifierLiteral id, ParameterDeclaration[] formal_parameters, TypeExpression return_type, Block body, FunctionScope own_scope, FieldAttributes attributes, bool isMethod, bool isGetter, bool isSetter, bool isAbstract, bool isFinal, CustomAttributeList customAttributes) : base(context)
 {
     this.completion = new Completion();
     MethodAttributes privateScope = MethodAttributes.PrivateScope;
     if ((attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Public)
     {
         privateScope = MethodAttributes.Public;
     }
     else if ((attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Private)
     {
         privateScope = MethodAttributes.Private;
     }
     else if ((attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Assembly)
     {
         privateScope = MethodAttributes.Assembly;
     }
     else if ((attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Family)
     {
         privateScope = MethodAttributes.Family;
     }
     else if ((attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.FamORAssem)
     {
         privateScope = MethodAttributes.FamORAssem;
     }
     else
     {
         privateScope = MethodAttributes.Public;
     }
     if (((attributes & FieldAttributes.Static) != FieldAttributes.PrivateScope) || !isMethod)
     {
         privateScope |= MethodAttributes.Static;
     }
     else
     {
         privateScope |= MethodAttributes.NewSlot | MethodAttributes.Virtual;
     }
     if (isAbstract)
     {
         privateScope |= MethodAttributes.Abstract;
     }
     if (isFinal)
     {
         privateScope |= MethodAttributes.Final;
     }
     this.name = id.ToString();
     this.isMethod = isMethod;
     if (ifaceId != null)
     {
         if (isMethod)
         {
             this.ifaceId = new TypeExpression(ifaceId);
             privateScope &= ~MethodAttributes.MemberAccessMask;
             privateScope |= MethodAttributes.Final | MethodAttributes.Private;
         }
         else
         {
             this.declaringObject = new Member(ifaceId.context, ifaceId, id);
             this.name = this.declaringObject.ToString();
         }
     }
     ScriptObject obj2 = base.Globals.ScopeStack.Peek();
     if (((attributes == FieldAttributes.PrivateScope) && !isAbstract) && !isFinal)
     {
         if (obj2 is ClassScope)
         {
             attributes |= FieldAttributes.Public;
         }
     }
     else if (!(obj2 is ClassScope))
     {
         base.context.HandleError(JSError.NotInsideClass);
         attributes = FieldAttributes.PrivateScope;
         privateScope = MethodAttributes.Public;
     }
     if (obj2 is ActivationObject)
     {
         this.inFastScope = ((ActivationObject) obj2).fast;
         string name = this.name;
         if (isGetter)
         {
             privateScope |= MethodAttributes.SpecialName;
             this.name = "get_" + this.name;
             if (return_type == null)
             {
                 return_type = new TypeExpression(new ConstantWrapper(Typeob.Object, context));
             }
         }
         else if (isSetter)
         {
             privateScope |= MethodAttributes.SpecialName;
             this.name = "set_" + this.name;
             return_type = new TypeExpression(new ConstantWrapper(Typeob.Void, context));
         }
         attributes &= FieldAttributes.FieldAccessMask;
         MethodAttributes attributes3 = privateScope & MethodAttributes.MemberAccessMask;
         if ((((privateScope & MethodAttributes.Virtual) != MethodAttributes.PrivateScope) && ((privateScope & MethodAttributes.Final) == MethodAttributes.PrivateScope)) && (((attributes3 == MethodAttributes.Private) || (attributes3 == MethodAttributes.Assembly)) || (attributes3 == MethodAttributes.FamANDAssem)))
         {
             privateScope |= MethodAttributes.CheckAccessOnOverride;
         }
         this.func = new FunctionObject(this.name, formal_parameters, return_type, body, own_scope, obj2, base.context, privateScope, customAttributes, this.isMethod);
         if (this.declaringObject == null)
         {
             string str2 = this.name;
             if (this.ifaceId != null)
             {
                 str2 = ifaceId.ToString() + "." + str2;
             }
             JSVariableField field = (JSVariableField) ((ActivationObject) obj2).name_table[str2];
             if ((field != null) && ((!(field is JSMemberField) || !(((JSMemberField) field).value is FunctionObject)) || this.func.isExpandoMethod))
             {
                 if (name != this.name)
                 {
                     field.originalContext.HandleError(JSError.ClashWithProperty);
                 }
                 else
                 {
                     id.context.HandleError(JSError.DuplicateName, this.func.isExpandoMethod);
                     if (field.value is FunctionObject)
                     {
                         ((FunctionObject) field.value).suppressIL = true;
                     }
                 }
             }
             if (this.isMethod)
             {
                 if ((!(field is JSMemberField) || !(((JSMemberField) field).value is FunctionObject)) || (name != this.name))
                 {
                     this.field = ((ActivationObject) obj2).AddNewField(str2, this.func, attributes | FieldAttributes.Literal);
                     if (name == this.name)
                     {
                         this.field.type = new TypeExpression(new ConstantWrapper(Typeob.FunctionWrapper, base.context));
                     }
                 }
                 else
                 {
                     this.field = ((JSMemberField) field).AddOverload(this.func, attributes | FieldAttributes.Literal);
                 }
             }
             else if (obj2 is FunctionScope)
             {
                 if (this.inFastScope)
                 {
                     attributes |= FieldAttributes.Literal;
                 }
                 this.field = ((FunctionScope) obj2).AddNewField(this.name, attributes, this.func);
                 if (this.field is JSLocalField)
                 {
                     JSLocalField field2 = (JSLocalField) this.field;
                     if (this.inFastScope)
                     {
                         field2.type = new TypeExpression(new ConstantWrapper(Typeob.ScriptFunction, base.context));
                         field2.attributeFlags |= FieldAttributes.Literal;
                     }
                     field2.debugOn = base.context.document.debugOn;
                     field2.isDefined = true;
                 }
             }
             else if (this.inFastScope)
             {
                 this.field = ((ActivationObject) obj2).AddNewField(this.name, this.func, attributes | FieldAttributes.Literal);
                 this.field.type = new TypeExpression(new ConstantWrapper(Typeob.ScriptFunction, base.context));
             }
             else
             {
                 this.field = ((ActivationObject) obj2).AddNewField(this.name, this.func, attributes | FieldAttributes.Static);
             }
             this.field.originalContext = context;
             if (name != this.name)
             {
                 string str3 = name;
                 if (this.ifaceId != null)
                 {
                     str3 = ifaceId.ToString() + "." + name;
                 }
                 FieldInfo info = (FieldInfo) ((ClassScope) obj2).name_table[str3];
                 if (info != null)
                 {
                     if (info.IsLiteral)
                     {
                         object obj3 = ((JSVariableField) info).value;
                         if (obj3 is JSProperty)
                         {
                             this.enclosingProperty = (JSProperty) obj3;
                         }
                     }
                     if (this.enclosingProperty == null)
                     {
                         id.context.HandleError(JSError.DuplicateName, true);
                     }
                 }
                 if (this.enclosingProperty == null)
                 {
                     this.enclosingProperty = new JSProperty(name);
                     ((JSMemberField) ((ActivationObject) obj2).AddNewField(str3, this.enclosingProperty, attributes | FieldAttributes.Literal)).originalContext = base.context;
                 }
                 else if ((isGetter && (this.enclosingProperty.getter != null)) || (isSetter && (this.enclosingProperty.setter != null)))
                 {
                     id.context.HandleError(JSError.DuplicateName, true);
                 }
                 if (isGetter)
                 {
                     this.enclosingProperty.getter = new JSFieldMethod(this.field, obj2);
                 }
                 else
                 {
                     this.enclosingProperty.setter = new JSFieldMethod(this.field, obj2);
                 }
             }
         }
     }
     else
     {
         this.inFastScope = false;
         this.func = new FunctionObject(this.name, formal_parameters, return_type, body, own_scope, obj2, base.context, MethodAttributes.Public, null, false);
         this.field = ((StackFrame) obj2).AddNewField(this.name, new Closure(this.func), attributes | FieldAttributes.Static);
     }
 }
Beispiel #57
0
 private void CheckThatMethodIsNotMarkedWithOverrideOrHide(FunctionObject func){
   if (func.noVersionSafeAttributeSpecified) return;
   //It is marked override or hide, give an appropriate error
   if ((func.attributes & MethodAttributes.VtableLayoutMask) == MethodAttributes.ReuseSlot) // current method specifies override
     func.funcContext.HandleError(JSError.NoMethodInBaseToOverride);
   else // current method specifies hide
     func.funcContext.HandleError(JSError.NoMethodInBaseToNew);
 }
Beispiel #58
0
        private void TranslateToILObjectForMember(ILGenerator il, Type obType, bool noValue, MemberInfo mem)
        {
            this.thereIsAnObjectOnTheStack = true;
            if (mem is IWrappedMember)
            {
                Object obj = ((IWrappedMember)mem).GetWrappedObject();
                if (obj is LenientGlobalObject)
                {
                    this.EmitILToLoadEngine(il);
                    il.Emit(OpCodes.Call, CompilerGlobals.getLenientGlobalObjectMethod);
                }
                else if (obj is Type || obj is ClassScope)
                {
                    if (obType.IsAssignableFrom(Typeob.Type))
                    {
                        (new ConstantWrapper(obj, null)).TranslateToIL(il, Typeob.Type);
                        return;
                    }
                    //this.name is the name of an instance member of the superclass of the class (or an outer class) in which this expression appears
                    //get class instance on stack
                    ScriptObject scope = Globals.ScopeStack.Peek();
                    while (scope is WithObject || scope is BlockScope)
                    {
                        scope = scope.GetParent();
                    }
                    if (scope is FunctionScope)
                    {
                        FunctionObject func = ((FunctionScope)scope).owner;
                        if (func.isMethod)
                        {
                            il.Emit(OpCodes.Ldarg_0);
                        }
                        else //Need to get the StackFrame.closureInstance on the stack
                        {
                            this.EmitILToLoadEngine(il);
                            il.Emit(OpCodes.Call, CompilerGlobals.scriptObjectStackTopMethod);
                            scope = this.Globals.ScopeStack.Peek();
                            while (scope is WithObject || scope is BlockScope)
                            {
                                il.Emit(OpCodes.Call, CompilerGlobals.getParentMethod);
                                scope = scope.GetParent();
                            }
                            il.Emit(OpCodes.Castclass, Typeob.StackFrame);
                            il.Emit(OpCodes.Ldfld, CompilerGlobals.closureInstanceField);
                        }
                    }
                    else if (scope is ClassScope)
                    {
                        il.Emit(OpCodes.Ldarg_0); //Inside instance initializer
                    }

                    //If dealing with a member of an outer class, get the outer class instance on the stack
                    scope = Globals.ScopeStack.Peek();
                    while (scope != null)
                    {
                        ClassScope csc = scope as ClassScope;
                        if (csc != null) //Might be dealing with a reference from within a nested class to an instance member of an outer class
                        {
                            if (csc.IsSameOrDerivedFrom(obType))
                            {
                                break;
                            }
                            il.Emit(OpCodes.Ldfld, csc.outerClassField);
                        }
                        scope = scope.GetParent();
                    }
                }
                else
                {
                    this.TranslateToILDefaultThisObject(il, this.lexLevel);
                    Convert.Emit(this, il, Typeob.Object, obType);
                }
            }
            else
            {
                ScriptObject scope = Globals.ScopeStack.Peek();
                while (scope is WithObject || scope is BlockScope)
                {
                    scope = scope.GetParent();
                }
                if (scope is FunctionScope)
                {
                    FunctionObject func = ((FunctionScope)scope).owner;
                    if (!func.isMethod) //Need to get the StackFrame.closureInstance on the stack
                    {
                        this.EmitILToLoadEngine(il);
                        il.Emit(OpCodes.Call, CompilerGlobals.scriptObjectStackTopMethod);
                        scope = this.Globals.ScopeStack.Peek();
                        while (scope is WithObject || scope is BlockScope)
                        {
                            il.Emit(OpCodes.Call, CompilerGlobals.getParentMethod);
                            scope = scope.GetParent();
                        }
                        il.Emit(OpCodes.Castclass, Typeob.StackFrame);
                        il.Emit(OpCodes.Ldfld, CompilerGlobals.closureInstanceField);
                        while (scope != null)
                        {
                            if (scope is ClassScope) //Might be dealing with a reference from within a nested class to an instance member of an outer class
                            {
                                ClassScope csc = (ClassScope)scope;
                                if (csc.IsSameOrDerivedFrom(obType))
                                {
                                    break;
                                }
                                il.Emit(OpCodes.Castclass, csc.GetTypeBuilder());
                                il.Emit(OpCodes.Ldfld, csc.outerClassField);
                            }
                            scope = scope.GetParent();
                        }
                        il.Emit(OpCodes.Castclass, obType);
                        return;
                    }
                }
                il.Emit(OpCodes.Ldarg_0);
                while (scope != null)
                {
                    if (scope is ClassScope) //Might be dealing with a reference from within a nested class to an instance member of an outer class
                    {
                        ClassScope csc = (ClassScope)scope;
                        if (csc.IsSameOrDerivedFrom(obType))
                        {
                            break;
                        }
                        il.Emit(OpCodes.Ldfld, csc.outerClassField);
                    }
                    scope = scope.GetParent();
                }
            }
        }
Beispiel #59
0
 internal Class(Context context, AST id, TypeExpression superTypeExpression, TypeExpression[] interfaces, Block body, 
   FieldAttributes attributes, bool isAbstract, bool isFinal, bool isStatic, bool isInterface, CustomAttributeList customAttributes)
   : base(context) {
   this.name = id.ToString();
   this.superTypeExpression = superTypeExpression;
   this.interfaces = interfaces;
   this.body = body;
   this.enclosingScope = (ScriptObject)Globals.ScopeStack.Peek(1);
   this.attributes = TypeAttributes.Class|TypeAttributes.Serializable;
   this.SetAccessibility(attributes);
   if (isAbstract)
     this.attributes |= TypeAttributes.Abstract;
   this.isAbstract = isAbstract || isInterface;
   this.isAlreadyPartiallyEvaluated = false;
   if (isFinal)
     this.attributes |= TypeAttributes.Sealed;
   if (isInterface)
     this.attributes |= TypeAttributes.Interface | TypeAttributes.Abstract;
   this.isCooked = false;
   this.cookedType = null;
   this.isExpando = false;
   this.isInterface = isInterface;
   this.isStatic = isStatic;
   this.needsEngine = !isInterface;
   this.validOn = (AttributeTargets)0;
   this.allowMultiple = true;
   this.classob = (ClassScope)Globals.ScopeStack.Peek();
   this.classob.name = this.name;
   this.classob.owner = this;
   this.implicitDefaultConstructor = null;
   if (!isInterface && !(this is EnumDeclaration))
     this.SetupConstructors();
   this.EnterNameIntoEnclosingScopeAndGetOwnField(id, isStatic);
   this.fields = this.classob.GetMemberFields();
   this.superClass = null;
   this.superIR = null;
   this.superMembers = null;
   this.firstIndex = null;
   this.fieldInitializer = null;
   this.customAttributes = customAttributes;
   this.clsCompliance = CLSComplianceSpec.NotAttributed;
   this.generateCodeForExpando = false;
   this.expandoItemProp = null;
   this.getHashTableMethod = null;
   this.getItem = null;
   this.setItem = null;
 }