示例#1
0
        /// <summary>
        /// eventListener.emit(eventName, args...)
        ///
        /// Returns bool indicating if event was raised.
        /// </summary>
        private static IAnalysisSet EventEmitterEmit(FunctionValue func, Node node, AnalysisUnit unit, IAnalysisSet @this, IAnalysisSet[] args)
        {
            if (args.Length >= 1)
            {
                if (@this != null)
                {
                    foreach (var thisArg in @this)
                    {
                        ExpandoValue expando = @thisArg.Value as ExpandoValue;
                        if (expando != null)
                        {
                            foreach (var arg in args[0])
                            {
                                var strValue = arg.Value.GetStringValue();
                                if (strValue != null)
                                {
                                    VariableDef events;
                                    if (expando.TryGetMetadata <VariableDef>(new EventListenerKey(strValue), out events))
                                    {
                                        foreach (var type in events.GetTypesNoCopy(unit))
                                        {
                                            type.Call(node, unit, @this, args.Skip(1).ToArray());
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            return(func.ProjectState._trueInst.Proxy);
        }
示例#2
0
        /// <summary>
        /// this.addListener(name, handler)
        ///
        /// returns the event listener
        /// </summary>
        private static IAnalysisSet EventEmitterAddListener(FunctionValue func, Node node, AnalysisUnit unit, IAnalysisSet @this, IAnalysisSet[] args)
        {
            if (args.Length >= 2 && args[1].Count > 0)
            {
                if (@this != null)
                {
                    foreach (var thisArg in @this)
                    {
                        ExpandoValue expando = @thisArg.Value as ExpandoValue;
                        if (expando != null)
                        {
                            foreach (var arg in args[0])
                            {
                                var strValue = arg.Value.GetStringValue();
                                if (strValue != null)
                                {
                                    var         key = new EventListenerKey(strValue);
                                    VariableDef events;
                                    if (!expando.TryGetMetadata(key, out events))
                                    {
                                        expando.SetMetadata(key, events = new VariableDef());
                                    }

                                    events.AddTypes(unit, args[1]);
                                }
                            }
                        }
                    }
                }
            }
            return(@this ?? AnalysisSet.Empty);
        }
 public BuiltinFunctionValue(ProjectEntry projectEntry,
     string name,
     string documentation = null,
     ExpandoValue prototype = null,
     params ParameterResult[] signature)
     : this(projectEntry, name, new[] { new SimpleOverloadResult(name, documentation, signature) }, documentation, prototype) {
 }
        public BuiltinFunctionValue(ProjectEntry projectEntry,
            string name,
            OverloadResult[] overloads,
            string documentation = null,
            ExpandoValue prototype = null)
            : base(projectEntry, prototype, name) {
            _name = name;
            _documentation = documentation;
            _overloads = overloads;

            Add("length", projectEntry.Analyzer.GetConstant(1.0).Proxy);
            Add("name", projectEntry.Analyzer.GetConstant(name).Proxy);
            Add("arguments", projectEntry.Analyzer._nullInst.Proxy);
            Add("caller", projectEntry.Analyzer._nullInst.Proxy);

            projectEntry.Analyzer.AnalysisValueCreated(typeof(BuiltinFunctionValue));
        }
        internal FunctionValue(ProjectEntry projectEntry, ExpandoValue prototype = null, string name = null)
            : base(projectEntry, projectEntry.Analyzer._functionPrototype) {
            if (prototype == null) {
                string description = null;
#if DEBUG
                if (String.IsNullOrWhiteSpace(Name ?? name)) {
                    if (AnalysisUnit != null) {
                        var loc = Locations.First();
                        description = "prototype object of " + AnalysisUnit.FullName + " " + loc.FilePath + "(" + loc.Column + ")";
                    }
                    else {
                        description = "prototype object of <unknown objects>";
                    }
                }
                else {
                    description = "prototype object of " + (Name ?? name);
                }
#endif
                prototype = new PrototypeValue(ProjectEntry, this, description: description);
            }
            Add("prototype", prototype.Proxy);
            prototype.Add("constructor", this.Proxy);
            _instance = new InstanceValue(ProjectEntry, prototype, name);
        }
        private void GenerateMethod(ExpandoValue exports, Dictionary<string, FunctionSpecializer> specialMethods, dynamic method) {
            string methodName = (string)method["name"];

            ObjectValue returnValue = null;
            if (methodName.StartsWith("create") && methodName.Length > 6) {
                string klassName = methodName.Substring(6);
                PropertyDescriptorValue propDesc;
                if (exports.Descriptors.TryGetValue(klassName, out propDesc) && propDesc.Values != null) {
                    var types = propDesc.Values.Types;
                    if (types != null && types.Count == 1) {
                        var type = types.First().Value;
                        if (type is FunctionValue) {
                            returnValue = ((FunctionValue)type)._instance;
                        }
                    }
                }
            }

            foreach (var sig in method["signatures"]) {
                BuiltinFunctionValue function;
                FunctionSpecializer specialMethod;
                if (specialMethods != null &&
                    specialMethods.TryGetValue(methodName, out specialMethod)) {
                    function = specialMethod.Specialize(
                        exports.ProjectEntry,
                        methodName,
                        ParseDocumentation((string)method["desc"]),
                        returnValue,
                        GetParameters(sig["params"])
                    );
                } else if (returnValue != null) {
                    function = new ReturningFunctionValue(
                        exports.ProjectEntry,
                        methodName,
                        returnValue.SelfSet,
                        ParseDocumentation((string)method["desc"]),
                        GetParameters(sig["params"])
                    );
                } else {
                    function = new BuiltinFunctionValue(
                        exports.ProjectEntry,
                        methodName,
                        ParseDocumentation((string)method["desc"]),
                        null,
                        GetParameters(sig["params"])
                    );
                }

                exports.Add(function);
            }
        }
示例#7
0
        private void GenerateMethod(ExpandoValue exports, Dictionary <string, FunctionSpecializer> specialMethods, dynamic method)
        {
            string methodName = (string)method["name"];

            ObjectValue returnValue = null;

            if (methodName.StartsWith("create") && methodName.Length > 6)
            {
                string klassName = methodName.Substring(6);
                PropertyDescriptorValue propDesc;
                if (exports.Descriptors.TryGetValue(klassName, out propDesc) && propDesc.Values != null)
                {
                    var types = propDesc.Values.Types;
                    if (types != null && types.Count == 1)
                    {
                        var type = types.First().Value;
                        if (type is FunctionValue)
                        {
                            returnValue = ((FunctionValue)type)._instance;
                        }
                    }
                }
            }

            foreach (var sig in method["signatures"])
            {
                BuiltinFunctionValue function;
                FunctionSpecializer  specialMethod;
                if (specialMethods != null &&
                    specialMethods.TryGetValue(methodName, out specialMethod))
                {
                    function = specialMethod.Specialize(
                        exports.ProjectEntry,
                        methodName,
                        ParseDocumentation((string)method["desc"]),
                        returnValue,
                        GetParameters(sig["params"])
                        );
                }
                else if (returnValue != null)
                {
                    function = new ReturningFunctionValue(
                        exports.ProjectEntry,
                        methodName,
                        returnValue.SelfSet,
                        ParseDocumentation((string)method["desc"]),
                        GetParameters(sig["params"])
                        );
                }
                else
                {
                    function = new BuiltinFunctionValue(
                        exports.ProjectEntry,
                        methodName,
                        ParseDocumentation((string)method["desc"]),
                        null,
                        GetParameters(sig["params"])
                        );
                }

                exports.Add(function);
            }
        }
示例#8
0
        private BuiltinFunctionValue ArrayFunction(out ExpandoValue arrayPrototype) {
            var builtinEntry = _analyzer._builtinEntry;

            return new SpecializedFunctionValue(
                builtinEntry,
                "Array",
                NewArray,
                null,
                arrayPrototype = new BuiltinObjectValue(builtinEntry) {
                        SpecializedFunction(
                            "concat",
                            ArrayConcat,
                            "Combines two or more arrays.",
                            Parameter("item"),
                            Parameter("item...")
                        ),
                        BuiltinFunction("constructor"),
                        ReturningFunction(
                            "every",
                            _analyzer._trueInst,
                            "Determines whether all the members of an array satisfy the specified test.",
                            Parameter("callbackfn", "A function that accepts up to three arguments. The every method calls the callbackfn function for each element in array1 until the callbackfn returns false, or until the end of the array."),
                            Parameter("thisArg", "An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.", isOptional:true)
                        ),
                        BuiltinFunction(
                            "filter",
                            "Returns the elements of an array that meet the condition specified in a callback function.",
                            Parameter("callbackfn", "A function that accepts up to three arguments. The filter method calls the callbackfn function one time for each element in the array."),
                            Parameter("thisArg", "An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.", isOptional:true)
                        ),
                        SpecializedFunction(
                            "forEach", 
                            ArrayForEach,
                            "Performs the specified action for each element in an array.",
                            Parameter("callbackfn", "A function that accepts up to three arguments. forEach calls the callbackfn function one time for each element in the array."),
                            Parameter("thisArg", "An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.")
                        ),
                        ReturningFunction(
                            "indexOf",
                            _analyzer._zeroIntValue,
                            "Returns the index of the first occurrence of a value in an array.",
                            Parameter("searchElement", "The value to locate in the array."),
                            Parameter("fromIndex", "The array index at which to begin the search. If fromIndex is omitted, the search starts at index 0.", isOptional: true)
                        ),
                        ReturningFunction(
                            "join",
                            _analyzer._emptyStringValue,
                            "Adds all the elements of an array separated by the specified separator string.",
                            Parameter("separator", "A string used to separate one element of an array from the next in the resulting String. If omitted, the array elements are separated with a comma.", isOptional: true)
                        ),
                        ReturningFunction(
                            "lastIndexOf",
                            _analyzer._zeroIntValue,
                            "Returns the index of the last occurrence of a specified value in an array.",
                            Parameter("searchElement", "The value to locate in the array."),
                            Parameter("fromIndex", "The array index at which to begin the search. If fromIndex is omitted, the search starts at the last index in the array.", isOptional: true)
                        ),
                        BuiltinProperty(
                            "length", 
                            _analyzer._zeroIntValue,
                            "Gets or sets the length of the array. This is a number one higher than the highest element defined in an array."
                        ),
                        BuiltinFunction(
                            "map",
                            "Calls a defined callback function on each element of an array, and returns an array that contains the results.",
                            Parameter("callbackfn", "A function that accepts up to three arguments. The map method calls the callbackfn function one time for each element in the array."),
                            Parameter("thisArg", "An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.")
                        ),
                        SpecializedFunction(
                            "pop",
                            ArrayPopFunction,
                            "Removes the last element from an array and returns it"
                        ),                        
                        SpecializedFunction(
                            "push",
                            ArrayPushFunction,
                            "Appends new elements to an array, and returns the new length of the array.",
                            Parameter("item", "New element of the Array."),
                            Parameter("item...",  "New element of the Array.", isOptional: true)
                        ),
                        BuiltinFunction(
                            "reduce",
                            "Calls the specified callback function for all the elements in an array. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.",
                            Parameter("callbackfn", "A function that accepts up to four arguments. The reduce method calls the callbackfn function one time for each element in the array."),
                            Parameter("initialValue", "If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value.", isOptional: true)
                        ),
                        BuiltinFunction(
                            "reduceRight",
                            "Calls the specified callback function for all the elements in an array, in descending order. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.",
                            Parameter("callbackfn", "A function that accepts up to four arguments. The reduceRight method calls the callbackfn function one time for each element in the array."),
                            Parameter("initialValue", "If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value.", isOptional: true)
                        ),
                        BuiltinFunction(
                            "reverse", 
                            "Reverses the elements in an Array."
                        ),
                        BuiltinFunction(
                            "shift", 
                            "Removes the first element from an array and returns it."
                        ),
                        SpecializedFunction(
                            "slice",
                            ArraySliceFunction,
                            "Returns a section of an array.",
                            Parameter("start", "The beginning of the specified portion of the array.", isOptional: true),
                            Parameter("end", "end The end of the specified portion of the array.", isOptional: true)
                        ),
                        ReturningFunction(
                            "some",
                            _analyzer._trueInst,
                            "Determines whether the specified callback function returns true for any element of an array.",
                            Parameter("callbackfn", "A function that accepts up to three arguments. The some method calls the callbackfn function for each element in array1 until the callbackfn returns true, or until the end of the array."),
                            Parameter("thisArg", "An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.", isOptional:true)
                        ),
                        BuiltinFunction(
                            "sort",
                            "Sorts an array.",
                            Parameter("compareFn", "The function used to determine the order of the elements. If omitted, the elements are sorted in ascending, ASCII character order.")
                        ),
                        BuiltinFunction(
                            "splice", 
                            "Removes elements from an array and, if necessary, inserts new elements in their place, returning the deleted elements.",
                            Parameter("start", "The zero-based location in the array from which to start removing elements.")
                        ),
                        BuiltinFunction("toLocaleString"),
                        ReturningFunction(
                            "toString", 
                            _analyzer._emptyStringValue,
                            "Returns a string representation of an array."
                        ),
                        BuiltinFunction(
                            "unshift",
                            "Inserts new elements at the start of an array.",
                            Parameter("item...", "Element to insert at the start of the Array.")
                        ),
                }) { 
                new ReturningFunctionValue(builtinEntry, "isArray", _analyzer._falseInst.Proxy)
            };
        }
示例#9
0
 public Globals(ObjectValue globalObject, AnalysisValue numberPrototype, AnalysisValue stringPrototype, AnalysisValue booleanPrototype, AnalysisValue functionPrototype, FunctionValue arrayFunction, ObjectValue objectPrototype, BuiltinFunctionValue requireFunction, ExpandoValue arrayPrototype, BuiltinFunctionValue objectGetOwnPropertyDescriptor) {
     GlobalObject = globalObject;
     NumberPrototype = numberPrototype;
     StringPrototype = stringPrototype;
     BooleanPrototype = booleanPrototype;
     FunctionPrototype = functionPrototype;
     ArrayFunction = arrayFunction;
     ObjectPrototype = objectPrototype;
     RequireFunction = requireFunction;
     ArrayPrototype = arrayPrototype;
     ObjectGetOwnPropertyDescriptor = objectGetOwnPropertyDescriptor;
 }
 public SpecializedFunctionValue(ProjectEntry projectEntry, string name, CallDelegate func, OverloadResult[] overloads, string documentation = null, ExpandoValue prototype = null)
     : base(projectEntry, name, overloads, documentation, prototype)
 {
     _func = func;
 }
 public SpecializedFunctionValue(ProjectEntry projectEntry, string name, CallDelegate func, string documentation = null, ExpandoValue prototype = null, params ParameterResult[] signature)
     : base(projectEntry, name, documentation, prototype, signature) {
     _func = func;
 }