<commit_before>/**
 * Copyright (c) 2015 Teem2 LLC
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
/** Provides support for getter and setter functions on an object.
    
    Events:
        None
    
    Attributes:
        earlyAttrs:array An array of attribute names that will be set first.
        lateAttrs:array An array of attribute names that will be set last.
*/
define(function(require, exports, module) {
    var dr = require('$LIB/dr/dr.js'),
        JS = require('$LIB/jsclass.js'),
        acorn = require('$LIB/acorn.js'),
        evaluator = require('$SPRITE/expressionresolver.js');
    
    module.exports = dr.AccessorSupport = new JS.Module('AccessorSupport', {
        // Class Methods and Attributes ////////////////////////////////////////////
        extend: {
            /** Generate a setter name for an attribute.
                @returns string */
            generateSetterName: function(attrName) {
                return this.SETTER_NAMES[attrName] || (this.SETTER_NAMES[attrName] = this.generateName(attrName, 'set_'));
            },
            
            /** Generate a getter name for an attribute.
                @returns string */
            generateGetterName: function(attrName) {
                return this.GETTER_NAMES[attrName] || (this.GETTER_NAMES[attrName] = this.generateName(attrName, 'get_'));
            },
            
            /** Generate a config name for an attribute.
                @returns string */
            generateConfigAttrName: function(attrName) {
                return this.CONFIG_ATTR_NAMES[attrName] || (this.CONFIG_ATTR_NAMES[attrName] = this.generateName(attrName, '__cfg_'));
            },
            
            /** Generate a constraint function name for an attribute.
                @returns string */
            generateConstraintFunctionName: function(attrName) {
                return this.CONSTRAINT_FUNCTION_NAMES[attrName] || (this.CONSTRAINT_FUNCTION_NAMES[attrName] = this.generateName(attrName, '__fnc_'));
            },
            
            /** Generates a method name by capitalizing the attrName and
                prepending the prefix.
                @returns string */
            generateName: function(attrName, prefix) {
                return prefix + attrName;
            },
            
            /** Caches getter names. */
            GETTER_NAMES:{},
            
            /** Caches setter names. */
            SETTER_NAMES:{},
            
            /** Caches config attribute names. */
            CONFIG_ATTR_NAMES:{},
            
            /** Caches constraint function names. */
            CONSTRAINT_FUNCTION_NAMES:{},
            
            CONSTRAINTS: {
                BINDINGS:{},
                SCOPES: null,
                PROPERTY_BINDINGS: {
                    MemberExpression: function(n, parent) {
                        // avoid binding to CallExpressions whose parent is a 
                        // function call, e.g. Math.round(...) shouldn't attempt 
                        // to bind to 'round' on Math
                        if (parent.node.type !== 'CallExpression' || parent.sub !== 'callee') {
                            dr.AccessorSupport.CONSTRAINTS.SCOPES.push({
                                binding:acorn.stringify(n.object),
                                property:n.property.name
                            });
                        }
                        return true;
                    }
                },
                
                __registeredConstraints: [],
                __lockCount: 0,
                incrementLockCount: function() {
                    ++this.__lockCount;
                },
                decrementLockCount: function() {
                    if (--this.__lockCount === 0) {
                        // Bind constraints
                        var rcs = this.__registeredConstraints;
                        while (rcs.length) rcs.shift().bindConstraint(rcs.shift(), rcs.shift(), true);
                    }
                },
                registerConstraint: function(node, attrName, expression) {
                    if (this.__lockCount === 0) {
                        // Bind immediatly if no nodes are being instantiated.
                        node.bindConstraint(attrName, expression);
                    } else {
                        this.__registeredConstraints.push(node, attrName, expression);
                    }
                }
            },
            
            coerce: function(attrName, value, type, defaultValue, scope) {
                switch (type) {
                    case 'number':
                        var newValue = parseFloat(value);
                        if (isNaN(newValue)) {
                            if (defaultValue !== undefined) {
                                value = defaultValue;
                            } else {
                                dr.dumpStack("NaN encountered parsing value as number with no default: " + value);
                            }
                        } else {<commit_msg>Better error message when number attr has undefined default.<commit_after>/**
 * Copyright (c) 2015 Teem2 LLC
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
/** Provides support for getter and setter functions on an object.
    
    Events:
        None
    
    Attributes:
        earlyAttrs:array An array of attribute names that will be set first.
        lateAttrs:array An array of attribute names that will be set last.
*/
define(function(require, exports, module) {
    var dr = require('$LIB/dr/dr.js'),
        JS = require('$LIB/jsclass.js'),
        acorn = require('$LIB/acorn.js'),
        evaluator = require('$SPRITE/expressionresolver.js');
    
    module.exports = dr.AccessorSupport = new JS.Module('AccessorSupport', {
        // Class Methods and Attributes ////////////////////////////////////////////
        extend: {
            /** Generate a setter name for an attribute.
                @returns string */
            generateSetterName: function(attrName) {
                return this.SETTER_NAMES[attrName] || (this.SETTER_NAMES[attrName] = this.generateName(attrName, 'set_'));
            },
            
            /** Generate a getter name for an attribute.
                @returns string */
            generateGetterName: function(attrName) {
                return this.GETTER_NAMES[attrName] || (this.GETTER_NAMES[attrName] = this.generateName(attrName, 'get_'));
            },
            
            /** Generate a config name for an attribute.
                @returns string */
            generateConfigAttrName: function(attrName) {
                return this.CONFIG_ATTR_NAMES[attrName] || (this.CONFIG_ATTR_NAMES[attrName] = this.generateName(attrName, '__cfg_'));
            },
            
            /** Generate a constraint function name for an attribute.
                @returns string */
            generateConstraintFunctionName: function(attrName) {
                return this.CONSTRAINT_FUNCTION_NAMES[attrName] || (this.CONSTRAINT_FUNCTION_NAMES[attrName] = this.generateName(attrName, '__fnc_'));
            },
            
            /** Generates a method name by capitalizing the attrName and
                prepending the prefix.
                @returns string */
            generateName: function(attrName, prefix) {
                return prefix + attrName;
            },
            
            /** Caches getter names. */
            GETTER_NAMES:{},
            
            /** Caches setter names. */
            SETTER_NAMES:{},
            
            /** Caches config attribute names. */
            CONFIG_ATTR_NAMES:{},
            
            /** Caches constraint function names. */
            CONSTRAINT_FUNCTION_NAMES:{},
            
            CONSTRAINTS: {
                BINDINGS:{},
                SCOPES: null,
                PROPERTY_BINDINGS: {
                    MemberExpression: function(n, parent) {
                        // avoid binding to CallExpressions whose parent is a 
                        // function call, e.g. Math.round(...) shouldn't attempt 
                        // to bind to 'round' on Math
                        if (parent.node.type !== 'CallExpression' || parent.sub !== 'callee') {
                            dr.AccessorSupport.CONSTRAINTS.SCOPES.push({
                                binding:acorn.stringify(n.object),
                                property:n.property.name
                            });
                        }
                        return true;
                    }
                },
                
                __registeredConstraints: [],
                __lockCount: 0,
                incrementLockCount: function() {
                    ++this.__lockCount;
                },
                decrementLockCount: function() {
                    if (--this.__lockCount === 0) {
                        // Bind constraints
                        var rcs = this.__registeredConstraints;
                        while (rcs.length) rcs.shift().bindConstraint(rcs.shift(), rcs.shift(), true);
                    }
                },
                registerConstraint: function(node, attrName, expression) {
                    if (this.__lockCount === 0) {
                        // Bind immediatly if no nodes are being instantiated.
                        node.bindConstraint(attrName, expression);
                    } else {
                        this.__registeredConstraints.push(node, attrName, expression);
                    }
                }
            },
            
            coerce: function(attrName, value, type, defaultValue, scope) {
                switch (type) {
                    case 'number':
                        var newValue = parseFloat(value);
                        if (isNaN(newValue)) {
                            if (defaultValue !== undefined) {
                                value = defaultValue;
                            } else {
                                dr.dumpStack("NaN encountered parsing value as number with no default. Attr: " + attrName);
                                value = NaN;
                            }
                        } else {
{'fraction_non_alphanumeric': 0.09112206155586074, 'fraction_numerical': 0.0012925115114306486, 'mean_word_length': 1.475504899020196, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6312013', 'n_tokens_mistral': 2967, 'n_tokens_neox': 2775, 'n_words': 889}