private void GenerateProperties(dynamic klass, ObjectValue value, Dictionary<string, PropertySpecializer> specializers) {
            if (klass.ContainsKey("properties")) {
                foreach (var prop in klass["properties"]) {
                    string propName = prop["name"];
                    string desc = ParseDocumentation(prop["desc"]);

                    string textRaw = "";
                    if (prop.ContainsKey("textRaw")) {
                        textRaw = prop["textRaw"];
                    }

                    PropertySpecializer specializer;
                    AnalysisValue propValue = null;
                    if (specializers != null &&
                        specializers.TryGetValue(propName, out specializer)) {
                        propValue = specializer.Specialize(value.ProjectEntry, propName);
                    } else if (desc.IndexOf("<code>Boolean</code>") != -1) {
                        propValue = value.ProjectEntry.Analyzer._trueInst;
                    } else if (desc.IndexOf("<code>Number</code>") != -1) {
                        propValue = value.ProjectEntry.Analyzer._zeroIntValue;
                    } else if (desc.IndexOf("<code>Readable Stream</code>") != -1) {
                        propValue = _readableStream;
                    } else if (desc.IndexOf("<code>Writable Stream</code>") != -1 || textRaw == "process.stderr") {
                        propValue = _writableStream;
                    } else if (!String.IsNullOrWhiteSpace(textRaw)) {
                        int start, end;
                        if ((start = textRaw.IndexOf('{')) != -1 && (end = textRaw.IndexOf('}')) != -1 &&
                            start < end) {
                            string typeName = textRaw.Substring(start, end - start);
                            switch (typeName) {
                                case "Boolean":
                                    propValue = value.ProjectEntry.Analyzer._trueInst;
                                    break;
                                case "Number":
                                    propValue = value.ProjectEntry.Analyzer._zeroIntValue;
                                    break;
                            }
                        }
                    }

                    if (propValue == null) {
                        propValue = new BuiltinObjectValue(value.ProjectEntry);
                    }

                    value.Add(
                        new MemberAddInfo(
                            propName,
                            propValue,
                            desc,
                            true
                        )
                    );
                }
            }
        }
        private void GenerateGlobal(ObjectValue exports, dynamic klass) {
            string name = FixClassName((string)klass["name"]);
            ObjectValue value = new BuiltinObjectValue(
                exports.ProjectEntry,
                ParseDocumentation((string)klass["desc"])
            );

            exports.Add(name, value.Proxy);

            if (klass.ContainsKey("methods")) {
                foreach (var method in klass["methods"]) {
                    GenerateMethod(
                        value,
                        null,
                        method
                    );
                }
            }

            Dictionary<string, PropertySpecializer> specializers;
            _propertySpecializations.TryGetValue(name, out specializers);

            GenerateProperties(klass, value, specializers);
        }
Exemplo n.º 3
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 void GenerateClass(ObjectValue exports, dynamic klass) {
            string className = (string)klass["name"];

            List<OverloadResult> overloads = new List<OverloadResult>();
            var fixedClassName = FixClassName(className);
            dynamic signatures;
            if (klass.TryGetValue("signatures", out signatures)) {
                foreach (var sig in signatures) {
                    var parameters = GetParameters(sig["params"]);
                    var doc = ParseDocumentation((string)sig["desc"]);

                    overloads.Add(new SimpleOverloadResult(fixedClassName, doc, parameters));
                }
            }
            BuiltinFunctionValue klassValue = new ClassBuiltinFunctionValue(
                exports.ProjectEntry,
                fixedClassName,
                overloads.ToArray(),
                ParseDocumentation((string)klass["desc"])
            );

            exports.Add(klassValue);

            if (klass.ContainsKey("methods")) {
                var prototype = (PrototypeValue)klassValue.Descriptors["prototype"].Values.Types.First().Value;
                Dictionary<string, FunctionSpecializer> classSpecializations;
                _classSpecializations.TryGetValue(className, out classSpecializations);
                foreach (var method in klass["methods"]) {
                    GenerateMethod(
                        prototype,
                        classSpecializations,
                        method
                    );
                }
            }
            if (klass.ContainsKey("classMethods")) {
                foreach (var method in klass["classMethods"]) {
                    GenerateMethod(
                        klassValue,
                        null,
                        method
                    );
                }
            }

            if (klass.ContainsKey("properties")) {
                var prototype = (PrototypeValue)klassValue.Descriptors["prototype"].Values.Types.First().Value;

                GenerateProperties(klass, prototype, null);
            }
        }
Exemplo n.º 5
0
        private BuiltinFunctionValue ObjectFunction(out ObjectValue objectPrototype, out BuiltinFunctionValue getOwnPropertyDescriptor) {
            var builtinEntry = _analyzer._builtinEntry;

            objectPrototype = new BuiltinObjectPrototypeValue(builtinEntry) {
                SpecializedFunction(
                    "__defineGetter__",
                    DefineGetter,
                    "Creates a getter method for the given property name",
                    Parameter("sprop", "The property name"),
                    Parameter("fun", "The function to be invoked")
                ),   
                BuiltinFunction(
                    "__lookupGetter__",
                    "Gets the getter function for the given property name",
                    Parameter("sprop", "The property name")
                ),   
                SpecializedFunction(
                    "__defineSetter__",
                    DefineSetter,
                    "Creates a setter method for the given property name",
                    Parameter("sprop", "The property name"),
                    Parameter("fun", "The function to be invoked")
                ),   
                BuiltinFunction(
                    "__lookupSetter__",
                    "Gets the setter function for the given property name",
                    Parameter("sprop", "The property name")
                ),   

                BuiltinFunction("constructor"),   
                ReturningFunction("toString", _analyzer._emptyStringValue),   
                ReturningFunction("toLocaleString", _analyzer._emptyStringValue),   
                BuiltinFunction("valueOf"),   
                ReturningFunction("hasOwnProperty", _analyzer._trueInst),   
                ReturningFunction(
                    "isPrototypeOf",
                    _analyzer._trueInst,
                    "Determines whether an object exists in another object's prototype chain.",
                    Parameter("v", "Another object whose prototype chain is to be checked.")
                ),   
                ReturningFunction(
                    "propertyIsEnumerable",
                    _analyzer._trueInst,
                    "Determines whether a specified property is enumerable.",
                    Parameter("v", "A property name.")
                ),   
            };

            getOwnPropertyDescriptor = SpecializedFunction(
                "getOwnPropertyDescriptor",
                GetOwnPropertyDescriptor,
                @"Gets the own property descriptor of the specified object. 
An own property descriptor is one that is defined directly on the object and is not inherited from the object's prototype. ",
                Parameter("o", "Object that contains the property."),
                Parameter("p", "Name of the property.")
            );

            return new SpecializedFunctionValue(
                builtinEntry, 
                "Object", 
                NewObject,
                null,
                objectPrototype) { 
                BuiltinFunction(
                    "getPrototypeOf",
                    "Returns the prototype of an object."
                ),
                getOwnPropertyDescriptor,
                BuiltinFunction(
                    "getOwnPropertyNames",
                    @"Returns the names of the own properties of an object. The own properties of an object are those that are defined directly 
on that object, and are not inherited from the object's prototype. The properties of an object include both fields (objects) and functions.",
                    Parameter("o", "Object that contains the own properties.")

                ),
                BuiltinFunction(
                    "create",
                    "Creates an object that has the specified prototype, and that optionally contains specified properties.",
                    Parameter("o", "Object to use as a prototype. May be null"),
                    Parameter("properties", "JavaScript object that contains one or more property descriptors.")
                ),
                SpecializedFunction(
                    "defineProperty",
                    DefineProperty,
                    "Adds a property to an object, or modifies attributes of an existing property.",
                    Parameter("o", "Object on which to add or modify the property. This can be a native JavaScript object (that is, a user-defined object or a built in object) or a DOM object."),
                    Parameter("p", "The property name."),
                    Parameter("attributes", "Descriptor for the property. It can be for a data property or an accessor property.")
                ),
                SpecializedFunction(
                    "defineProperties", 
                    DefineProperties,
                    "Adds one or more properties to an object, and/or modifies attributes of existing properties.",
                    Parameter("o", "Object on which to add or modify the properties. This can be a native JavaScript object or a DOM object."),
                    Parameter("properties", "JavaScript object that contains one or more descriptor objects. Each descriptor object describes a data property or an accessor property.")
                ),
                BuiltinFunction(
                    "seal",
                    "Prevents the modification of attributes of existing properties, and prevents the addition of new properties.",
                    Parameter("o", "Object on which to lock the attributes.")
                ),
                BuiltinFunction(
                    "freeze",
                    "Prevents the modification of existing property attributes and values, and prevents the addition of new properties.",
                    Parameter("o", "Object on which to lock the attributes.")
                ),
                BuiltinFunction(
                    "preventExtensions",
                    "Prevents the addition of new properties to an object.",
                    Parameter("o", "Object to make non-extensible.")
                ),
                ReturningFunction(
                    "isSealed",
                    _analyzer._trueInst,
                    "Returns true if existing property attributes cannot be modified in an object and new properties cannot be added to the object.",
                    Parameter("o", "Object to test. ")
                ),
                ReturningFunction(
                    "isFrozen",
                    _analyzer._trueInst,
                    "Returns true if existing property attributes and values cannot be modified in an object, and new properties cannot be added to the object.",
                    Parameter("o", "Object to test.")
                ),
                ReturningFunction(
                    "isExtensible",
                    _analyzer._trueInst,
                    "Returns a value that indicates whether new properties can be added to an object.",
                    Parameter("o", "Object to test.")
                ),
                SpecializedFunction(
                    "keys",
                    ObjectKeys,
                    "Returns the names of the enumerable properties and methods of an object.",
                    Parameter("o", "Object that contains the properties and methods. This can be an object that you created or an existing Document Object Model (DOM) object.")
                ),
                ReturningFunction("is", _analyzer._trueInst),
            };
        }