Inheritance: ObjectInstance
示例#1
0
        //     INITIALIZATION
        //_________________________________________________________________________________________

        /// <summary>
        /// Creates a new String object.
        /// </summary>
        /// <param name="prototype"> The next object in the prototype chain. </param>
        internal StringConstructor(ObjectInstance prototype)
            : base(prototype, __STUB__Construct, __STUB__Call)
        {
            // Initialize the constructor properties.
            var properties = GetDeclarativeProperties(Engine);

            InitializeConstructorProperties(properties, "String", 1, StringInstance.CreatePrototype(Engine, this));
            InitializeProperties(properties);
        }
示例#2
0
        public FunctionInstance Construct(params string[] argumentsAndBody)
        {
            // Passing no arguments results in an empty function.
            if (argumentsAndBody.Length == 0)
            {
                return(new UserDefinedFunction(this.InstancePrototype, "anonymous", new string[0], string.Empty));
            }

            // Split any comma-delimited names.
            var argumentNames = new List <string>();

            for (int i = 0; i < argumentsAndBody.Length - 1; i++)
            {
                var splitNames = argumentsAndBody[i].Split(',');
                if (splitNames.Length > 1 || StringInstance.Trim(splitNames[0]) != string.Empty)
                {
                    for (int j = 0; j < splitNames.Length; j++)
                    {
                        // Trim any whitespace from the start and end of the argument name.
                        string argumentName = StringInstance.Trim(splitNames[j]);
                        if (argumentName == string.Empty)
                        {
                            throw new JavaScriptException(this.Engine, "SyntaxError", "Unexpected ',' in argument");
                        }

                        // Check the name is valid and resolve any escape sequences.
                        argumentName = Compiler.Lexer.ResolveIdentifier(this.Engine, argumentName);
                        if (argumentName == null)
                        {
                            throw new JavaScriptException(this.Engine, "SyntaxError", "Expected identifier");
                        }
                        splitNames[j] = argumentName;
                    }
                }
                argumentNames.AddRange(splitNames);
            }

            // Create a new function.
            return(new UserDefinedFunction(this.InstancePrototype, "anonymous", argumentNames, argumentsAndBody[argumentsAndBody.Length - 1]));
        }