<commit_before>"use strict";
var _ = require("underscore");
var Generator = require("./generators").Generator;

var defaultIndent = "    ";

exports.ModuleGenerator = (function () {
    function CommonJs (usage) {
        this.usage = usage;
        this.indent = 0;
    }
    CommonJs.prototype = new Generator();
    
    var builtins = {
            "object": "JSONObject",
            "array": "JSONArray",
            "int": "int",
            "float": "float",
            "double": "double",
            "short": "short",
            "boolean": "boolean",
            "string": "String"
    };
    
    var builtinImportables = {
            "object": "org.json.JSONObject",
            "array": "org.json.JSONArray"
    };
    
    CommonJs.prototype.importLines = function (importsArray) {
       return [];
    };

    CommonJs.prototype.packageLines = function (classObject) {
        // NOP;
       var className = classObject.name,
           superClass = classObject.superClass;
       var lines = ["function " + className + " () {"];
       if (!classObject.properties || classObject.properties.length === 0) {
           lines.push(defaultIndent + "// No properties declared");
       }
       _.each(classObject.properties, function (p) {
           var typeName = typeString(p.type),
               paramName = p.name, 
               value = p["default"];
           if (_.isUndefined(value)) {
               value = "null";
           } else {
               value = JSON.stringify(value);
           }
           
           lines.push(defaultIndent + "this." + paramName + " = " + value + "; // " + typeName);
           
       });
       
       lines.push("}");
       if (classObject.superClass) {
           lines.push("var " + superClass + " = require(\"" + superClass + "\");");
           lines.push(className + ".prototype = new " + superClass + "();");
       }
       lines.push("module.exports = " + className + ";");
       lines.push("var instance = " + className + ".prototype;");
       
       return lines;
    };
    
    function typeString (type) {
        var builtin = builtins[type];
        
        return builtin ? builtin : type.name;
    }
    
    function docsLines(docs, params, suppressIndent) {
        if (_.isUndefined(suppressIndent) && _.isBoolean(params)) {
            suppressIndent = params;
            params = null;
        }
        suppressIndent = true;
        var indent = suppressIndent ? "" : defaultIndent;
        
        var lines = [],
            comment = false;
        
        if (docs) {
            comment = true;
            lines.push(indent + "/**");
            lines.push(indent + " * " + docs);
        }

        if (params) {
            _.each(params, function (param) {
                if (!comment) {
                    lines.push(indent + "/**");
                    comment = true;
                }
                
                var paramLine = [" * @param", param.name, "{@link " + typeString(param.type) + "}"];
                if (param.docs) {
                    paramLine.push(param.docs);
                }
                lines.push(indent + paramLine.join(" "));
            });
        }
        
        if (comment) {
            lines.push(indent + " */");
        }
        
        return lines;
    }
    
    
    CommonJs.prototype.interfaceDeclLines = function (classObject) {
        return [];
    };    
    
    CommonJs.prototype.propertyLines = function (properties) {
        // NOP
        return [];
    };
    
    CommonJs.prototype.methodLine = function (sig) {
        var lines = [];
        
        var comment = false;

        lines.push(docsLines(sig.docs, sig.params));
        
        var namedArgs = _.filter(sig.name.split(":"), function (t) {
            return t;
        });
        
        var isFirst = true;
        var methodName = _.map(namedArgs, function (s) {
            if (isFirst) {
                isFirst = false;
                return s;
            }
            return s[0].toUpperCase() + s.substring(1);
        }).join("");
        
        var params  = _.map(sig.params, function (p) {
            return p.name;
        });        
        lines.push("instance." + methodName + " = function (" + params.join(", ") + ") {");
        var theComment = this.usage !== 'request' ? "// AUTOGENERATED, COPY DON'T EDIT" : "// AUTOGENERATED, REPLACE NOT EDIT";
        lines.push(defaultIndent + theComment);
        lines.push(defaultIndent + "throw new Error(\"" + methodName + " is unimplemented\");");
        lines.push("};")
        
        return _.flatten(lines);
    };
    
    CommonJs.prototype.methodLines = function (sigArray) {
        var self = this;
        var allLines = self.role === "javascript" ? [
            "instance.onLoad = function (nativeObject) {};",  " ",
            "instance.onResume = function () {};", " ",
            "instance.onPause = function () {};", " ",
            "instance.onUnload = function () {};", " "
                        ] : [];
         _.each(sigArray, function (sig) {
            var line = self.methodLine(sig);
            allLines.push(line);
            allLines.push(" ");
            
        });
        return allLines;
    };
    
    
    CommonJs.prototype.footer = function () {
        return "";
    };
    
    return CommonJs;
}());

exports.getOutputDirectory = function (nodeModule, kirinInfo, platformBlock) {
    return nodeModule.directory + "/stubs";
};

exports.generateFiles = function (filePrefix, classOrder, fileMap, nodeModule) {
    fileMap = fileMap || {};
    var generator = new exports.ModuleGenerator(),
        path = require("path");
    
    if (!filePrefix) {
        throw new Error("An idlOutput is needed in package.json");
    }
    
    _.each(classOrder, function (irClass) {
        if (!irClass) {
            return;
        }
        if (irClass.role === "javascript") {            
            var lines = generator.lines(irClass),
                content = lines.join("\n"),
                filename = irClass.name + ".stub.js";
            
            fileMap[path.join(filePrefix, filename)] = content;
            nodeModule.addFile(path.join(filePrefix, filename));
        }
    });
    
    return fileMap;
};<commit_msg>Generate stub modules and proper request objects.<commit_after>"use strict";
var _ = require("underscore");
var Generator = require("./generators").Generator;

var defaultIndent = "    ";

exports.ModuleGenerator = (function () {
    function CommonJs (usage) {
        this.usage = usage;
        this.indent = 0;
    }
    CommonJs.prototype = new Generator();
    
    var builtins = {
            "object": "JSONObject",
            "array": "JSONArray",
            "int": "int",
            "float": "float",
            "double": "double",
            "short": "short",
            "boolean": "boolean",
            "string": "String"
    };
    
    var builtinImportables = {
            "object": "org.json.JSONObject",
            "array": "org.json.JSONArray"
    };
    
    CommonJs.prototype.importLines = function (importsArray) {
       return [];
    };

    CommonJs.prototype.packageLines = function (classObject) {
        // NOP;
       var className = classObject.name,
           superClass = classObject.superClass;
       var lines = ["function " + className + " () {"];
       if (!classObject.properties || classObject.properties.length === 0) {
           lines.push(defaultIndent + "// No properties declared");
       }
       _.each(classObject.properties, function (p) {
           var typeName = typeString(p.type),
               paramName = p.name, 
               value = p["default"];
           if (_.isUndefined(value)) {
               value = "null";
           } else {
               value = JSON.stringify(value);
           }
           
           lines.push(defaultIndent + "this." + paramName + " = " + value + "; // " + typeName);
           
       });
       
       lines.push("}");
       if (classObject.superClass) {
           lines.push("var " + superClass + " = require(\"" + superClass + "\");");
           lines.push(className + ".prototype = new " + superClass + "();");
       }
       lines.push("module.exports = " + className + ";");
       lines.push("var instance = " + className + ".prototype;");
       
       return lines;
    };
    
    function typeString (type) {
        var builtin = builtins[type];
        
        return builtin ? builtin : type.name;
    }
    
    function docsLines(docs, params, suppressIndent) {
        if (_.isUndefined(suppressIndent) && _.isBoolean(params)) {
            suppressIndent = params;
            params = null;
        }
        suppressIndent = true;
        var indent = suppressIndent ? "" : defaultIndent;
        
        var lines = [],
            comment = false;
        
        if (docs) {
            comment = true;
            lines.push(indent + "/**");
            lines.push(indent + " * " + docs);
        }

        if (params) {
            _.each(params, function (param) {
                if (!comment) {
                    lines.push(indent + "/**");
                    comment = true;
                }
                
                var paramLine = [" * @param", param.name, "{@link " + typeString(param.type) + "}"];
                if (param.docs) {
                    paramLine.push(param.docs);
                }
                lines.push(indent + paramLine.join(" "));
            });
        }
        
        if (comment) {
            lines.push(indent + " */");
        }
        
        return lines;
    }
    
    
    CommonJs.prototype.interfaceDeclLines = function (classObject) {
        return [];
    };    
    
    CommonJs.prototype.propertyLines = function (properties) {
        // NOP
        return [];
    };
    
    CommonJs.prototype.methodLine = function (sig) {
        var lines = [];
        
        var comment = false;

        lines.push(docsLines(sig.docs, sig.params));
        
        var namedArgs = _.filter(sig.name.split(":"), function (t) {
            return t;
        });
        
        var isFirst = true;
        var methodName = _.map(namedArgs, function (s) {
            if (isFirst) {
                isFirst = false;
                return s;
            }
            return s[0].toUpperCase() + s.substring(1);
        }).join("");
        
        var params  = _.map(sig.params, function (p) {
            return p.name;
        });        
        lines.push("instance." + methodName + " = function (" + params.join(", ") + ") {");
        var theComment = this.usage !== 'request' ? "// AUTOGENERATED, COPY DON'T EDIT" : "// AUTOGENERATED, REPLACE NOT EDIT";
        lines.push(defaultIndent + theComment);
        lines.push(defaultIndent + "throw new Error(\"" + methodName + " is unimplemented\");");
        lines.push("};")
        
        return _.flatten(lines);
    };
    
    CommonJs.prototype.methodLines = function (sigArray) {
        var self = this;
        var allLines = self.role === "javascript" ? [
            "instance.onLoad = function (nativeObject) {};",  " ",
            "instance.onResume = function () {};", " ",
            "instance.onPause = function () {};", " ",
            "instance.onUnload = function () {};", " "
                        ] : [];
         _.each(sigArray, function (sig) {
            var line = self.methodLine(sig);
            allLines.push(line);
            allLines.push(" ");
            
        });
        return allLines;
    };
    
    
    CommonJs.prototype.footer = function () {
        return "";
    };
    
    return CommonJs;
}());

exports.getOutputDirectory = function (nodeModule, kirinInfo, platformBlock) {
    return nodeModule.directory;
};

exports.generateFiles = function (filePrefix, classOrder, fileMap, nodeModule) {
    fileMap = fileMap || {};
    var generator = new exports.ModuleGenerator(),
        path = require("path");
    
    if (!filePrefix) {
        throw new Error("An idlOutput is needed in package.json");
    }
    
    _.each(classOrder, function (irClass) {
        if (!irClass) {
            return;
        }
        if (irClass.role === "javascript") {            
            var lines = generator.lines(irClass),
                content = lines.join("\n"),
                filename = "stubs/" + irClass.name + ".stub.js";
            
            fileMap[path.join(filePrefix, filename)] = content;
            // this is wrong! addFile breaks immutability
            nodeModule.addFile(path.join(filePrefix, filename));
        } else if (irClass.role === "request") {
            var lines = generator.lines(irClass),
                content = lines.join("\n"),
                filename = irClass.name + ".js";
        
            fileMap[path.join(filePrefix, filename)] = content;
           // nodeModule.addFile(path.join(filePrefix, filename));            
        }
    });
    
    return fileMap;
};
{'fraction_non_alphanumeric': 0.14246323529411764, 'fraction_numerical': 0.0006127450980392157, 'mean_word_length': 1.571287908625443, 'pattern_counts': {'":': 22, '<': 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': '2468737', 'n_tokens_mistral': 3588, 'n_tokens_neox': 3547, 'n_words': 717}