private static IAnalysisSet CloneSpecializationImpl(FunctionValue func, Node node, AnalysisUnit unit, IAnalysisSet @this, IAnalysisSet[] args) {
            if (args.Length > 0) {
                return args[0];
            }

            return AnalysisSet.Empty;            
        }
예제 #2
0
 private static IAnalysisSet ArrayForEach(FunctionValue func, Node node, AnalysisUnit unit, IAnalysisSet @this, IAnalysisSet[] args) {
     // prevent Array.forEach from calling Array.forEach recursively
     if (!_inForEach) {
         try {
             _inForEach = true;
             if (args.Length >= 1 && @this != null) {
                 foreach (var value in @this) {
                     ArrayValue arr = value.Value as ArrayValue;
                     if (arr != null) {
                         arr.ForEach(node, unit, @this, args);
                     }
                 }
             }
         } finally {
             _inForEach = false;
         }
     }
     return unit.Analyzer._undefined.Proxy;
 }
예제 #3
0
 private static IAnalysisSet ArraySliceFunction(FunctionValue func, Node node, AnalysisUnit unit, IAnalysisSet @this, IAnalysisSet[] args) {
     IAnalysisSet res = AnalysisSet.Empty;
     if (args.Length >= 1 && @this != null) {
         foreach (var thisValue in @this) {
             ArrayValue arr = thisValue.Value as ArrayValue;
             if (arr != null) {
                 res = res.Add(thisValue);
             }
         }
     }
     return res;
 }
예제 #4
0
 private static IAnalysisSet ArrayPushFunction(FunctionValue func, Node node, AnalysisUnit unit, IAnalysisSet @this, IAnalysisSet[] args) {
     if (args.Length >= 1 && @this != null) {                
         foreach (var thisValue in @this) {
             ArrayValue arr = thisValue.Value as ArrayValue;
             if (arr != null) {
                 arr.PushValue(node, unit, args[0]);
             }
         }
     }
     return unit.Analyzer._zeroIntValue.SelfSet;
 }
예제 #5
0
        private static IAnalysisSet NewArray(FunctionValue func, Node node, AnalysisUnit unit, IAnalysisSet @this, IAnalysisSet[] args) {
            IAnalysisSet value;
            if (!unit._env.GlobalEnvironment.TryGetNodeValue(NodeEnvironmentKind.ArrayCall, node, out value)) {
                var arrValue = new ArrayValue(
                    new[] { new TypedDef() },
                    unit.ProjectEntry,
                    node
                );
                value = arrValue.SelfSet;
                unit._env.GlobalEnvironment.AddNodeValue(NodeEnvironmentKind.ArrayCall, node, arrValue.SelfSet);
            }

            return value;
        }
예제 #6
0
 private static IAnalysisSet StringToLowerCase(FunctionValue func, Node node, AnalysisUnit unit, IAnalysisSet @this, IAnalysisSet[] args) {
     IAnalysisSet res = AnalysisSet.Empty;
     if (@this != null) {
         foreach (var arg in @this) {
             StringValue str = arg.Value as StringValue;
             if (str != null) {
                 res = res.Union(unit.Analyzer.GetConstant(str._value.ToLower(CultureInfo.CurrentCulture)).SelfSet);
             }
         }
     }
     if (res.Count == 0) {
         return unit.Analyzer._emptyStringValue.SelfSet;
     }
     return res;
 }
예제 #7
0
        private static IAnalysisSet DefineProperties(FunctionValue func, Node node, AnalysisUnit unit, IAnalysisSet @this, IAnalysisSet[] args) {
            // object, {propName: {desc}, ...}
            if (args.Length >= 2) {
                foreach (var obj in args[0]) {
                    ExpandoValue target = obj.Value as ExpandoValue;
                    if (target != null) {
                        foreach (var properties in args[1]) {
                            ExpandoValue propsObj = properties.Value as ExpandoValue;
                            if (propsObj != null) {
                                propsObj.AddDescriptorDependency(unit);

                                foreach (var propName in propsObj.Descriptors.Keys) {
                                    var definingProperty = propsObj.Get(node, unit, propName);
                                    foreach (var propValue in definingProperty) {
                                        target.AddProperty(
                                            node,
                                            unit,
                                            propName,
                                            propValue.Value
                                        );
                                    }
                                }
                            }
                        }
                    }
                }
            }
            if (args.Length > 0) {
                return args[0];
            }
            return AnalysisSet.Empty;
        }
예제 #8
0
 private static IAnalysisSet GetOwnPropertyDescriptor(FunctionValue func, Node node, AnalysisUnit unit, IAnalysisSet @this, IAnalysisSet[] args) {
     // Object.getOwnPropertyDescriptor(object, name)
     // returns an object like:
     //  {
     //      get:<function>,
     //      set:<function>,
     //      value:<object>,
     //      writable: boolean,
     //      enumerable: boolean,
     //      configurable: boolean
     // }
     if (args.Length >= 2) {
         IAnalysisSet res = AnalysisSet.Empty;
         foreach (var nameValue in args[1]) {
             string nameStr = nameValue.Value.GetStringValue();
             if (nameStr != null) {
                 foreach (var value in args[0]) {
                     var propDesc = value.Value.GetProperty(node, unit, nameStr) as PropertyDescriptorValue;
                     if (propDesc != null) {
                         res = res.Union(propDesc.SelfSet);
                     }
                 }
             }
         }
         return res;
     }
     return AnalysisSet.Empty;
 }
 private static IAnalysisSet WrapFunctionSpecializationImpl(FunctionValue func, Node node, AnalysisUnit unit, IAnalysisSet @this, IAnalysisSet[] args) {
     // just return the original unwrapped function for intellisense purposes...
     if (args.Length >= 1) {
         return args[0];
     }
     return AnalysisSet.Empty;
 }
 private static IAnalysisSet AssignSpecializationImpl(FunctionValue func, Node node, AnalysisUnit unit, IAnalysisSet @this, IAnalysisSet[] args) {
     return AnalysisSet.Empty;
 }
        private static IAnalysisSet MergeSpecializationImpl(FunctionValue func, Node node, AnalysisUnit unit, IAnalysisSet @this, IAnalysisSet[] args) {
            if (args.Length >= 2) {
                foreach (var targetValue in args[0]) {
                    var target = targetValue.Value as ExpandoValue;
                    if (target == null) {
                        continue;
                    }
                    if (args[1].Count < unit.Analyzer.Limits.MaxMergeTypes) {
                        foreach (var sourceValue in args[1]) {
                            var source = sourceValue.Value as ExpandoValue;
                            if (source == null) {
                                continue;
                            }

                            target.AddLinkedValue(unit, source);
                        }
                    }
                }

            }
            return AnalysisSet.Empty;
        }
        /// <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 && args[0].Count < unit.Analyzer.Limits.MaxEvents) {
                            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;
        }
        /// <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;
        }
        private static IAnalysisSet UtilInherits(FunctionValue func, Node node, AnalysisUnit unit, IAnalysisSet @this, IAnalysisSet[] args) {
            // function inherits(ctor, superCtor)
            // sets ctor.super_ to superCtor
            // sets ctor.prototype = {copy of superCtor.prototype}
            if (args.Length >= 2) {
                args[0].SetMember(node, unit, "super_", args[1]);

                IAnalysisSet prototypeValue;
                InheritsPrototypeValue copied;
                var prototype = args[1].Get(node, unit, "prototype");
                if (!unit.DeclaringModuleEnvironment.TryGetNodeValue(NodeEnvironmentKind.InheritsPrototypeValue, node, out prototypeValue)) {
                    copied = new InheritsPrototypeValue(unit.ProjectEntry, prototype);
                    unit.DeclaringModuleEnvironment.AddNodeValue(NodeEnvironmentKind.InheritsPrototypeValue, node, copied.Proxy);
                } else {
                    copied = (InheritsPrototypeValue)prototypeValue.First().Value;
                    copied.AddPrototypes(prototype);
                }

                args[0].SetMember(node, unit, "prototype", copied.Proxy);
            }
            return AnalysisSet.Empty;
        }
        private static IAnalysisSet FsReadDirSync(FunctionValue func, Node node, AnalysisUnit unit, IAnalysisSet @this, IAnalysisSet[] args) {
            var call = node as CallNode;
            if (call != null && call.Arguments.Length == 1) {
                var ee = new ExpressionEvaluator(unit);
                IAnalysisSet arraySet;
                ReadDirSyncArrayValue array;
                if (!unit.GetDeclaringModuleEnvironment().TryGetNodeValue(NodeEnvironmentKind.ArrayValue, call, out arraySet)) {
                    array = new ReadDirSyncArrayValue(
                        unit.ProjectEntry,
                        node
                    );
                    arraySet = array.SelfSet;
                    unit.GetDeclaringModuleEnvironment().AddNodeValue(NodeEnvironmentKind.ArrayValue, call, arraySet);
                } else {
                    array = (ReadDirSyncArrayValue)arraySet.First().Value;
                }

                foreach (var path in ee.MergeStringLiterals(call.Arguments[0])) {
                    array.AddDirectoryMembers(unit, path);
                }

                return array.SelfSet;
            }
            return AnalysisSet.Empty;
        }
예제 #16
0
 private static IAnalysisSet DefineGetter(FunctionValue func, Node node, AnalysisUnit unit, IAnalysisSet @this, IAnalysisSet[] args) {
     if (@this != null && args.Length >= 2) {
         foreach (var thisVal in @this) {
             ExpandoValue expando = thisVal.Value as ExpandoValue;
             if (expando != null) {
                 foreach (var name in args[0]) {
                     var nameStr = name.Value.GetStringValue();
                     if (nameStr != null) {
                         expando.DefineGetter(unit, nameStr, args[1]);
                         // call the function w/ our this arg...
                         args[1].Call(node, unit, thisVal, Analyzer.ExpressionEvaluator.EmptySets);
                     }
                 }
             }
         }
     }
     return unit.Analyzer._undefined.Proxy;
 }
        private static IAnalysisSet BackboneExtendSpecializationImpl(FunctionValue func, Node node, AnalysisUnit unit, IAnalysisSet @this, IAnalysisSet[] args) {
            IAnalysisSet res = AnalysisSet.Empty;
            BackboneExtendFunctionValue value;
            if (!unit._env.GlobalEnvironment.TryGetNodeValue(NodeEnvironmentKind.ExtendCall, node, out res)) {
                value = new BackboneExtendFunctionValue(
                    unit.ProjectEntry
                );
                res = value.SelfSet;
                unit._env.GlobalEnvironment.AddNodeValue(
                    NodeEnvironmentKind.ExtendCall, 
                    node, 
                    value.SelfSet
                );
            } else {
                value = (BackboneExtendFunctionValue)res.First().Value;
            }

            if (@this != null) {
                value._instance.SetMember(
                    node,
                    unit,
                    "__proto__",
                    @this.Construct(node, unit, args)
                );
            }

            if (args.Length > 0) {
                if (args[0].Count < unit.Analyzer.Limits.MaxMergeTypes) {
                    foreach (var protoProps in args[0]) {
                        ExpandoValue expandoProto = protoProps.Value as ExpandoValue;
                        if (expandoProto != null) {
                            value._prototype.AddLinkedValue(unit, expandoProto);
                        }
                    }
                }
            }

            if (args.Length > 1) {
                if (args[1].Count < unit.Analyzer.Limits.MaxMergeTypes) {
                    foreach (var protoProps in args[1]) {
                        ExpandoValue expandoProto = protoProps.Value as ExpandoValue;
                        if (expandoProto != null) {
                            value.AddLinkedValue(unit, expandoProto);
                        }
                    }
                }
            }

            return res;
        }
예제 #18
0
        private static IAnalysisSet DefineProperty(FunctionValue func, Node node, AnalysisUnit unit, IAnalysisSet @this, IAnalysisSet[] args) {
            // object, name, property desc

            if (args.Length >= 3) {
                foreach (var obj in args[0]) {
                    ExpandoValue expando = obj.Value as ExpandoValue;
                    if (expando != null) {
                        foreach (var name in args[1]) {
                            string propName = name.Value.GetStringValue();
                            if (propName != null) {
                                foreach (var desc in args[2]) {
                                    expando.AddProperty(node, unit, propName, desc.Value);
                                }
                            }
                        }
                    }
                }
            }
            if (args.Length > 0) {
                return args[0];
            }
            return AnalysisSet.Empty;
        }
        private static IAnalysisSet ExtendSpecializationImpl(FunctionValue func, Node node, AnalysisUnit unit, IAnalysisSet @this, IAnalysisSet[] args) {
            if (args.Length >= 1) {
                var obj = args[0];
                if (args.Length >= 2) {
                    foreach (var targetValue in obj) {
                        var target = targetValue.Value as ExpandoValue;
                        if (target == null || target is BuiltinObjectPrototypeValue) {
                            continue;
                        }

                        for (int i = 1; i < args.Length; i++) {
                            var curArg = args[i];

                            if (curArg.Count < unit.Analyzer.Limits.MaxMergeTypes) {
                                foreach (var sourceValue in curArg) {
                                    var source = sourceValue.Value as ExpandoValue;
                                    if (source == null) {
                                        continue;
                                    }

                                    target.AddLinkedValue(unit, source);
                                }
                            }
                        }
                    }
                }
                return obj;
            }
            return AnalysisSet.Empty;
        }
예제 #20
0
        private static IAnalysisSet Require(FunctionValue func, Node node, AnalysisUnit unit, IAnalysisSet @this, IAnalysisSet[] args) {
            CallNode call = (CallNode)node;
            IAnalysisSet res = AnalysisSet.Empty;
            // we care a lot about require analysis and people do some pretty
            // crazy dynamic things for require calls.  If we let our normal
            // analysis and specialized function handle it we won't get things
            // like handling './' + somePath.  So we'll go ahead and handle
            // some special cases here...
            if (IsSpecialRequire(unit, call, ref res)) {
                return res;
            }

            if (args.Length > 0) {
                foreach (var arg in args[0]) {
                    var moduleName = arg.Value.GetStringValue();
                    if (moduleName != null) {
                        res = res.Union(
                            unit.Analyzer.Modules.RequireModule(
                                node,
                                unit,
                                moduleName, 
                                unit.DeclaringModuleEnvironment.Name
                            )
                        );
                    }
                }
            }
            return res;
        }
 private static IAnalysisSet SetProtoSpecializationImpl(FunctionValue func, Node node, AnalysisUnit unit, IAnalysisSet @this, IAnalysisSet[] args) {
     if (args.Length >= 2) {
         args[0].SetMember(node, unit, "__proto__", args[1]);
     }
     return AnalysisSet.Empty;
 }
예제 #22
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;
 }
 private static IAnalysisSet ObjectKeysSpecializationImpl(FunctionValue func, Node node, AnalysisUnit unit, IAnalysisSet @this, IAnalysisSet[] args) {
     if (args.Length >= 1) {
         return unit.Analyzer._arrayFunction._instance.Proxy;
     }
     return AnalysisSet.Empty;
 }
예제 #24
0
        private static IAnalysisSet NewObject(FunctionValue func, Node node, AnalysisUnit unit, IAnalysisSet @this, IAnalysisSet[] args) {
            IAnalysisSet value;
            if (!unit._env.GlobalEnvironment.TryGetNodeValue(NodeEnvironmentKind.ObjectCall, node, out value)) {
                var objValue = new ObjectLiteralValue(
                    unit.ProjectEntry,
                    node
                );
                value = objValue.SelfSet;
                unit._env.GlobalEnvironment.AddNodeValue(NodeEnvironmentKind.ObjectCall, node, objValue.SelfSet);
            }

            return value;
        }
 private static IAnalysisSet CreateSpecializationImpl(FunctionValue func, Node node, AnalysisUnit unit, IAnalysisSet @this, IAnalysisSet[] args) {
     if (args.Length >= 1) {
         // fake out copy by just returning the
         // original input object
         return args[0];
     }
     return AnalysisSet.Empty;
 }
예제 #26
0
 private static IAnalysisSet ArrayPopFunction(FunctionValue func, Node node, AnalysisUnit unit, IAnalysisSet @this, IAnalysisSet[] args) {
     IAnalysisSet res = AnalysisSet.Empty;
     if (@this != null) {
         foreach (var thisValue in @this) {
             ArrayValue arr = thisValue.Value as ArrayValue;
             if (arr != null) {
                 res = res.Union(arr.PopValue(node, unit));
             }
         }
     }
     return res;
 }
예제 #27
0
        private static IAnalysisSet ObjectKeys(FunctionValue func, Node node, AnalysisUnit unit, IAnalysisSet @this, IAnalysisSet[] args) {
            ArrayValue arrValue;
            IAnalysisSet value;
            if (!unit._env.GlobalEnvironment.TryGetNodeValue(NodeEnvironmentKind.ObjectKeysArray, node, out value)) {
                arrValue = new ArrayValue(
                    new[] { new TypedDef() },
                    unit.ProjectEntry,
                    node
                );
                unit._env.GlobalEnvironment.AddNodeValue(NodeEnvironmentKind.ObjectKeysArray, node, arrValue.SelfSet);
            } else {
                arrValue = (ArrayValue)value.First().Value;
            }

            IAnalysisSet res = AnalysisSet.Empty;
            if (args.Length >= 1) {
                if (args[0].Count < unit.Analyzer.Limits.MaxObjectKeysTypes) {
                    arrValue.IndexTypes[0].AddTypes(
                        unit,
                        args[0].GetEnumerationValues(node, unit)
                    );
                }
            }
            return arrValue.SelfSet;
        }
예제 #28
0
 private static IAnalysisSet ArrayConcat(FunctionValue func, Node node, AnalysisUnit unit, IAnalysisSet @this, IAnalysisSet[] args) {
     // supporting actual concat would be nice, but could lead to infinite
     // analysis.  So we just return the existing array for now.
     return @this;
 }
예제 #29
0
 private static IAnalysisSet DefineSetter(FunctionValue func, Node node, AnalysisUnit unit, IAnalysisSet @this, IAnalysisSet[] args) {
     if (@this != null && args.Length >= 2) {
         foreach (var thisVal in @this) {
             ExpandoValue expando = thisVal.Value as ExpandoValue;
             if (expando != null) {
                 foreach (var name in args[0]) {
                     var nameStr = name.Value.GetStringValue();
                     if (nameStr != null) {
                         expando.DefineSetter(unit, nameStr, args[1]);
                     }
                 }
             }
         }
     }
     return unit.Analyzer._undefined.Proxy;
 }
예제 #30
0
 private static IAnalysisSet ApplyFunction(FunctionValue func, Node node, AnalysisUnit unit, IAnalysisSet @this, IAnalysisSet[] args) {
     var res = AnalysisSet.Empty;
     if (@this != null && args.Length > 0) {
         foreach (var value in @this) {
             if (args.Length > 1) {
                 foreach (var arg in args[1]) {
                     res = res.Union(value.Call(node, unit, args[0], arg.Value.GetIndices(node, unit)));
                 }
             } else {
                 res = res.Union(value.Call(node, unit, args[0], ExpressionEvaluator.EmptySets));
             }
         }
     }
     return res;
 }
예제 #31
0
 public PrototypeValue(ProjectEntry projectEntry, FunctionValue function, string description = null)
     : base(projectEntry, description: description)
 {
     _function = function;
     projectEntry.Analyzer.AnalysisValueCreated(typeof(PrototypeValue));
 }