<commit_before>function ccdump_wrapper(options)
{
  this.options = options;
  this.requireConfig = {
    baseUrl: options.ccdump_path + "/content",
  };
}

ccdump_wrapper.prototype = {
  options: null,
  requireConfig: null,
  analyzer: null,
  graphSerializer: null,
  _loaded: false,
  _ran: false,

  load_ccdump: function load_ccdump(callback)
  {
    if (this._loaded) {
      callback && callback(this);
      return;
    }

    let loaderJs = this.options.ccdump_path + "/content/loader.js";
    let global = this.options.global;
    Services.scriptloader.loadSubScript(loaderJs, global, "utf8");

    global.require(this.requireConfig,
      ["app/analyzer", "app/graphSerializer"],
      function _onRequireCcdump(mAnalyzer, mGraphSerializer) {
        this.analyzer = new mAnalyzer();
        this.graphSerializer = mGraphSerializer;
        this._loaded = true;
        callback && callback(this);
      }.bind(this)
    );
  },

  run: function ccdump_run(callback)
  {
    let listener = {
      onProgress: function() { },
      onFinished: function() {
        this._ran = true;
        callback && callback(this);
      }.bind(this),
    };

    this.load_ccdump(function _run() {
      this.analyzer.run(listener);
    }.bind(this));
  },

  save: function ccdump_save(filePath, callback)
  {
    function doSave() {
      let file = Cc["@mozilla.org/file/local;1"].
                 createInstance(Ci.nsILocalFile);
      file.initWithPath(filePath);

      let fs = Cc["@mozilla.org/network/file-output-stream;1"].
               createInstance(Ci.nsIFileOutputStream);
      let modeFlags = 0x02 | 0x08 | 0x20;
      fs.init(file, modeFlags, 420 /* 0644 */, fs.DEFER_OPEN);

      let converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"].
                      createInstance(Ci.nsIScriptableUnicodeConverter);
      converter.charset = "UTF-8";
      let input = converter.convertToInputStream(this.toJSON());

      NetUtil.asyncCopy(input, fs, function _onAsyncCopy(aStatus) {
        if (!Components.isSuccessCode(aStatus)) {
          Cu.reportError("failed to save ccdump file " + filePath);
        }

        callback && callback(aStatus);
      });
    }

    if (this._ran) {
      doSave.call(this);
    } else {
      this.run(doSave.bind(this));
    }
  },

  toJSON: function ccdump_toJSON()
  {
    return this.graphSerializer.toJSON(this.analyzer);
  },

  clear: function ccdump_clear()
  {
    this.analyzer.clear();
    this._ran = false;
  },
};<commit_msg>style changes as requested by Honza<commit_after>/* See license.txt for terms of usage */

function ccdump_wrapper(options)
{
    this.options = options;
    this.requireConfig = {
        baseUrl: options.ccdump_path + "/content",
    };
}

ccdump_wrapper.prototype = {
    options: null,
    requireConfig: null,
    analyzer: null,
    graphSerializer: null,
    _loaded: false,
    _ran: false,

    load_ccdump: function load_ccdump(callback)
    {
        if (this._loaded)
        {
            callback && callback(this);
            return;
        }

        let loaderJs = this.options.ccdump_path + "/content/loader.js";
        let global = this.options.global;
        Services.scriptloader.loadSubScript(loaderJs, global, "utf8");

        global.require(this.requireConfig,
            ["app/analyzer", "app/graphSerializer"],
            function _onRequireCcdump(mAnalyzer, mGraphSerializer)
            {
                this.analyzer = new mAnalyzer();
                this.graphSerializer = mGraphSerializer;
                this._loaded = true;
                callback && callback(this);
            }.bind(this));
    },

    run: function ccdump_run(callback)
    {
        let listener = {
            onProgress: function() { },
            onFinished: function()
            {
                this._ran = true;
                callback && callback(this);
            }.bind(this),
        };

        this.load_ccdump(this.analyzer.run.bind(this.analyzer, listener));
    },

    save: function ccdump_save(filePath, callback)
    {
        function doSave()
        {
            let file = Cc["@mozilla.org/file/local;1"].
                       createInstance(Ci.nsILocalFile);
            file.initWithPath(filePath);

            let fs = Cc["@mozilla.org/network/file-output-stream;1"].
                     createInstance(Ci.nsIFileOutputStream);
            let modeFlags = 0x02 | 0x08 | 0x20;
            fs.init(file, modeFlags, 420 /* 0644 */, fs.DEFER_OPEN);

            let converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"].
                            createInstance(Ci.nsIScriptableUnicodeConverter);
            converter.charset = "UTF-8";
            let input = converter.convertToInputStream(this.toJSON());

            NetUtil.asyncCopy(input, fs, function _onAsyncCopy(aStatus)
            {
                if (!Components.isSuccessCode(aStatus)) {
                    Cu.reportError("failed to save ccdump file " + filePath);
                }

                callback && callback(aStatus);
            });
        }

        if (this._ran)
        {
            doSave.call(this);
        }
        else
        {
            this.run(doSave.bind(this));
        }
    },

    toJSON: function ccdump_toJSON()
    {
        return this.graphSerializer.toJSON(this.analyzer);
    },

    clear: function ccdump_clear()
    {
        this.analyzer.clear();
        this._ran = false;
    },
};

// vim:set tw=80 sw=4 sts=4 et:
{'fraction_non_alphanumeric': 0.12236363636363637, 'fraction_numerical': 0.008, 'mean_word_length': 2.1044018058690743, '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': '5748567', 'n_tokens_mistral': 1718, 'n_tokens_neox': 1630, 'n_words': 289}