예제 #1
0
        /// <summary>
        /// Constructs an instance of the class with the specified parameters.
        /// </summary>
        /// <param name="args">The parameters passed to the constructor.</param>
        /// <returns>The created instance of the class.</returns>
        public override IScriptObject Construct(List <IScriptObject> args)
        {
            var ins = new InstanceObject(this);

            if (Constructor != null)
            {
                Constructor.Invoke(ins, args);
            }
            return(ins);
        }
예제 #2
0
        /// <summary>
        /// Invokes the constructor on the specified target with the specified parameters and returns the value.
        /// </summary>
        /// <param name="target">The target on which the constructor is invoked.</param>
        /// <param name="args">The parameters passed to the constructor.</param>
        /// <exception cref="ArgumentException"><paramref name="args"/> is invalid.</exception>
        public void Invoke(InstanceObject target, List <IScriptObject> args)
        {
            try
            {
                FunctionHelper.CheckArguments(Parameters, args);
            }
            catch (ArgumentException)
            {
                throw new ArgumentException(
                          string.Format(ExceptionResource.ConstructorParemetersNotMatch,
                                        Class.Name));
            }
            MethodContext context = new MethodContext(Class, target, Closure);

            FunctionHelper.SetArguments(context, Parameters, args);
            if (Class.Super != null && Class.Super.Constructor != null)
            {
                var list = new List <IScriptObject>();
                if (Parameters != null)
                {
                    foreach (var p in SuperCall)
                    {
                        list.Add(context.EvaluateExpression(p));
                    }
                }
                Class.Super.Constructor.Invoke(target, list);
            }
            IScriptObject value;

            foreach (var field in Class.FieldInitializers)
            {
                if (field.Value != null)
                {
                    value = Closure.EvaluateExpression(field.Value);
                }
                else
                {
                    value = ScriptNull.Instance;
                }
                if (target.Fields.ContainsKey(field.Key))
                {
                    target.Fields.Add(field.Key, value);
                }
                else
                {
                    target.Fields[field.Key] = value;
                }
            }
            if (Statements != null)
            {
                context.ExecuteStatements(Statements);
            }
        }