<commit_before>// ---------- Loader ----------
  /*
   * Spec Differences
   * - Added ensureRegistered entry cache argument to avoid repeated lookups
   * - metadata sent through ensureRegistered and all requestHOOK calls
   * - Error entry checking and saving added to requestHOOK calls
   */

  // 3. Loader Objects

  // Loader class
  function Loader() {
    this._loader = {
      loaderObj: this,

      resolve: undefined,
      fetch: undefined,
      translate: undefined,
      instantiate: undefined,

      registry: {},
      newRegistry: createRegistry(), //this is temporary until Registry is ready to be used
      // Realm not implemented
    };
  }

  // States
  var FETCH = 0;
  var TRANSLATE = 1;
  var INSTANTIATE = 2;
  var INSTANTIATE_ALL = 3;
  var LINK = 4;
  var READY = 5;

  // 3.3.2
  Loader.prototype['import'] = function(name, referrer) {
    var loader = this._loader;
    var metadata = {};
    return Promise.resolve()
    .then(function() {
      return loader.resolve.call(loader.loaderObj, name, referrer, metadata);
    })
    ['catch'](function(err) {
      throw addToError(err, 'Resolving ' + name + (referrer ? ', ' + referrer : ''));
    })
    .then(function(key) {
      return requestReady(loader, key, metadata);
    });
  };

  // 3.3.3
  Loader.prototype.resolve = function(name, referrer, metadata) {
    var loader = this._loader;
    return loader.resolve.call(loader.loaderObj, name, referrer, metadata || {});
  };

  // 3.3.4
  // For eg ready, <script type="module" src="${key}"></script>
  Loader.prototype.load = function(key, stage, metadata) {
    var loader = this._loader;
    
    if (stage == 'fetch')
      return requestFetch(loader, key, metadata);
    
    else if (stage == 'translate')
      return requestTranslate(loader, key, metadata);
    
    else if (stage == 'instantiate')
      return requestInstantiateAll(loader, key, metadata)
      .then(function(entry) {
        if (!(entry.module instanceof Module))
          return entry.module;
      });
    
    else if (stage == 'link')
      return requestLink(loader, key, metadata).then(function() {})
    
    else if (!stage || stage == 'ready')
      return requestReady(loader, key, metadata)
      .then(function(entry) {
        // this is ok because we use plain modules throughout
        return entry.module;
      });

    else
      throw new TypeError('Invalid stage ' + stage);
  };

  // 3.3.5
  Object.defineProperty(Loader.prototype, 'registry', {
      get: function() {
          if (typeof this !== 'object')
              throw new TypeError('this must be a Loader');
          // uncomment when Realm is implemented
          // if (!this._loader.realm)
          //     throw new TypeError('A Loader must have a realm');

          if (!(this._loader.newRegistry instanceof RegistryPrototype))
              throw new TypeError('invalid registry -- must be created during Loader constructor');
          return this._loader.newRegistry;
      }
  });

  // No longer in spec
  // For eg fetch, <script type="module">${value}</script>, key = anon
  Loader.prototype.provide = function(key, stage, value, metadata) {
    var loader = this._loader;

    var entry = ensureRegistered(loader, key, metadata);

    if (stage == 'fetch') {
      if (entry.state > FETCH)
        throw new TypeError(key + ' has already been fetched.');
      fulfillFetch(loader, entry, value);
    }
    else if (stage == 'translate') {
      if (entry.state > TRANSLATE)
        throw new TypeError(key + ' has already been translated.');
      fulfillTranslate(loader, entry, value);
    }
    else if (stage == 'instantiate') {
      if (entry.state > INSTANTIATE)
        throw new TypeError(key + ' has already been instantiated.');
      fulfillFetch(loader, entry, undefined);
      fulfillTranslate(loader, entry, undefined);
      // NB error propogation
      entry.translate.then(function(source) {
        loadTranspilerThenFulfillInstantiate(loader, entry, value, source);
      });
    }
    else
      throw new TypeError('Invalid stage ' + stage);
  };

  // TODO: the Loader no longer has the hook property
  // loader.hook('resolve') -> returns resolve hook
  // loader.hook('resolve', fn) -> sets resolve hook
  var hooks = ['resolve', 'fetch', 'translate', 'instantiate'];
  Loader.prototype.hook = function(name, value) {
    var loader = this._loader;
    if (indexOf.call(hooks, name) == -1)
      throw new TypeError(name + ' is not a valid hook.');
    if (value)
      loader[name] = value;
    else
      return loader[name];
  };

  // 4. Registry Objects
  // For now, registry objects are a work in progress that don't fully integrate into the rest of the code base

  // 4.1.1
  function createRegistry() {
    var registry = new RegistryPrototype();
    registry._registry = {};
    if (Map)
      registry._registry.registryMap = new Map();
    else
      registry._registry.registryMap = {};
    // 4.4.2
    if (__global.Symbol && __global.Symbol.iterator)
      registry[__global.Symbol.iterator] = RegistryPrototype.prototype.entries;
    return registry;
  }

  // 4.2
  function Registry() {
    throw new Error('A registry may only be created when creating a loader');
  }

  // 4.3.1
  Registry.prototype = RegistryPrototype;

  // 4.4 (reason for a separate constructor explained at 4.3.1)
  function RegistryPrototype() {}

  // 4.4.1
  Registry.prototype.constructor = Registry;

  // 4.4.2 is inlined in 4.1.1

  // 4.4.3
  RegistryPrototype.prototype.entries = function() {
    if (typeof this !== 'object')
      throw new TypeError('cannot get entries of a non-registry');
    if (Map) {
      // native iterator
      return this._registry.registryMap.entries();
    } else {
      // polyfilled iterator
      var keys = Object.keys(this._registry.registryMap);
      var keyIndex = 0;
      var instance = this;
      return {
        next: function() {
          if (keyIndex < keys.length - 1) {
            return {
              value: [keys[keyIndex], instance._registry.registryMap[keys[keyIndex++]]],
              done: false
            };
          } else {
            return {
              value: undefined,
              done: true
            };
          }
        }
      };
    }
  }

  // 4.4.4
  RegistryPrototype.prototype.keys = function() {
    if (typeof this !== 'object')
      throw new TypeError('invalid registry');
    if (Map) {
      // native iterator
      return this._registry.registryMap.keys();
    } else {
      // polyfilled iterator
      var keys = Object.keys(this._registry.registryMap);
      var keyIndex = 0;
      return {
        next: function() {
          if (keyIndex < keys.length - 1) {
            return {
              value: keys[keyIndex++],
              done: false
            };
          } else {
            return {
              value: undefined,
              done: true
            };
          }
        }
      };
    }
  }

  // 4.4.5
  RegistryPrototype.prototype.values = function() {
    if (typeof this !== 'object')
      throw new TypeError('invalid registry');
    if (Map) {
      // native iterator
      return this._registry.registryMap.values();
    } else {
      // polyfilled iterator
      var keys = Object.keys(this._registry.registryMap);
      var keyIndex = 0;
      var instance = this;
      return {
        next: function() {
          if (keyIndex < keys.length - 1) {
            return {
              value: instance._registry.registryMap[keys[keyIndex++]],
              done: false
            };
          } else {
            return {
              value: undefined,
              done: true
            };
          }
        }
      };
    }
  }

  // 4.4.6
  RegistryPrototype.prototype.get = function(key) {
    if (typeof this !== 'object')
      throw new TypeError('invalid registry');
    if (Map)
      return this._registry.registryMap.get(key);
    else
      return this._registry.registryMap[key];
  }

  // 4.4.7
  RegistryPrototype.prototype.set = function(key, value) {
    if (typeof this !== 'object')
      throw new TypeError('invalid registry');
    if (Map)
      this._registry.registryMap.set(key, value);
    else
      this._registry.registryMap[key] = value;

    return this;
  }

  // 4.4.8
  RegistryPrototype.prototype.has = function(key) {
    if (typeof this !== 'object')
      throw new TypeError('invalid registry');
    if (Map)
      return this._registry.registryMap.has(key);
    else
      return this._registry.registryMap.hasOwnProperty(key);
  }

  // 4.4.9
  RegistryPrototype.prototype.delete = function(key) {
    if (typeof this !== 'object')
      throw new TypeError('invalid registry');
    if (Map)
      return this._registry.registryMap.delete(key);
    else {
      var hadProperty = this._registry.registryMap.hasOwnProperty(key);
      delete this._registry.registryMap[key];
      return hadProperty;
    }
  }

  // 4.1.1 - TODO out of date
  function getCurrentStage(entry) {
    if (typeof entry !== 'object')
      throw new TypeError('entry is not an object');
    return entry.pipeline[0];<commit_msg>Fixing reference to Map so that it doesn't throw exceptions in old browsers<commit_after>// ---------- Loader ----------
  /*
   * Spec Differences
   * - Added ensureRegistered entry cache argument to avoid repeated lookups
   * - metadata sent through ensureRegistered and all requestHOOK calls
   * - Error entry checking and saving added to requestHOOK calls
   */

  // 3. Loader Objects

  // Loader class
  function Loader() {
    this._loader = {
      loaderObj: this,

      resolve: undefined,
      fetch: undefined,
      translate: undefined,
      instantiate: undefined,

      registry: {},
      newRegistry: createRegistry(), //this is temporary until Registry is ready to be used
      // Realm not implemented
    };
  }

  // States
  var FETCH = 0;
  var TRANSLATE = 1;
  var INSTANTIATE = 2;
  var INSTANTIATE_ALL = 3;
  var LINK = 4;
  var READY = 5;

  // 3.3.2
  Loader.prototype['import'] = function(name, referrer) {
    var loader = this._loader;
    var metadata = {};
    return Promise.resolve()
    .then(function() {
      return loader.resolve.call(loader.loaderObj, name, referrer, metadata);
    })
    ['catch'](function(err) {
      throw addToError(err, 'Resolving ' + name + (referrer ? ', ' + referrer : ''));
    })
    .then(function(key) {
      return requestReady(loader, key, metadata);
    });
  };

  // 3.3.3
  Loader.prototype.resolve = function(name, referrer, metadata) {
    var loader = this._loader;
    return loader.resolve.call(loader.loaderObj, name, referrer, metadata || {});
  };

  // 3.3.4
  // For eg ready, <script type="module" src="${key}"></script>
  Loader.prototype.load = function(key, stage, metadata) {
    var loader = this._loader;
    
    if (stage == 'fetch')
      return requestFetch(loader, key, metadata);
    
    else if (stage == 'translate')
      return requestTranslate(loader, key, metadata);
    
    else if (stage == 'instantiate')
      return requestInstantiateAll(loader, key, metadata)
      .then(function(entry) {
        if (!(entry.module instanceof Module))
          return entry.module;
      });
    
    else if (stage == 'link')
      return requestLink(loader, key, metadata).then(function() {})
    
    else if (!stage || stage == 'ready')
      return requestReady(loader, key, metadata)
      .then(function(entry) {
        // this is ok because we use plain modules throughout
        return entry.module;
      });

    else
      throw new TypeError('Invalid stage ' + stage);
  };

  // 3.3.5
  Object.defineProperty(Loader.prototype, 'registry', {
      get: function() {
          if (typeof this !== 'object')
              throw new TypeError('this must be a Loader');
          // uncomment when Realm is implemented
          // if (!this._loader.realm)
          //     throw new TypeError('A Loader must have a realm');

          if (!(this._loader.newRegistry instanceof RegistryPrototype))
              throw new TypeError('invalid registry -- must be created during Loader constructor');
          return this._loader.newRegistry;
      }
  });

  // No longer in spec
  // For eg fetch, <script type="module">${value}</script>, key = anon
  Loader.prototype.provide = function(key, stage, value, metadata) {
    var loader = this._loader;

    var entry = ensureRegistered(loader, key, metadata);

    if (stage == 'fetch') {
      if (entry.state > FETCH)
        throw new TypeError(key + ' has already been fetched.');
      fulfillFetch(loader, entry, value);
    }
    else if (stage == 'translate') {
      if (entry.state > TRANSLATE)
        throw new TypeError(key + ' has already been translated.');
      fulfillTranslate(loader, entry, value);
    }
    else if (stage == 'instantiate') {
      if (entry.state > INSTANTIATE)
        throw new TypeError(key + ' has already been instantiated.');
      fulfillFetch(loader, entry, undefined);
      fulfillTranslate(loader, entry, undefined);
      // NB error propogation
      entry.translate.then(function(source) {
        loadTranspilerThenFulfillInstantiate(loader, entry, value, source);
      });
    }
    else
      throw new TypeError('Invalid stage ' + stage);
  };

  // TODO: the Loader no longer has the hook property
  // loader.hook('resolve') -> returns resolve hook
  // loader.hook('resolve', fn) -> sets resolve hook
  var hooks = ['resolve', 'fetch', 'translate', 'instantiate'];
  Loader.prototype.hook = function(name, value) {
    var loader = this._loader;
    if (indexOf.call(hooks, name) == -1)
      throw new TypeError(name + ' is not a valid hook.');
    if (value)
      loader[name] = value;
    else
      return loader[name];
  };

  // 4. Registry Objects
  // For now, registry objects are a work in progress that don't fully integrate into the rest of the code base

  // 4.1.1
  function createRegistry() {
    var registry = new RegistryPrototype();
    registry._registry = {};
    if (__global.Map)
      registry._registry.registryMap = new __global.Map();
    else
      registry._registry.registryMap = {};
    // 4.4.2
    if (__global.Symbol && __global.Symbol.iterator)
      registry[__global.Symbol.iterator] = RegistryPrototype.prototype.entries;
    return registry;
  }

  // 4.2
  function Registry() {
    throw new Error('A registry may only be created when creating a loader');
  }

  // 4.3.1
  Registry.prototype = RegistryPrototype;

  // 4.4 (reason for a separate constructor explained at 4.3.1)
  function RegistryPrototype() {}

  // 4.4.1
  Registry.prototype.constructor = Registry;

  // 4.4.2 is inlined in 4.1.1

  // 4.4.3
  RegistryPrototype.prototype.entries = function() {
    if (typeof this !== 'object')
      throw new TypeError('cannot get entries of a non-registry');
    if (__global.Map) {
      // native iterator
      return this._registry.registryMap.entries();
    } else {
      // polyfilled iterator
      var keys = Object.keys(this._registry.registryMap);
      var keyIndex = 0;
      var instance = this;
      return {
        next: function() {
          if (keyIndex < keys.length - 1) {
            return {
              value: [keys[keyIndex], instance._registry.registryMap[keys[keyIndex++]]],
              done: false
            };
          } else {
            return {
              value: undefined,
              done: true
            };
          }
        }
      };
    }
  }

  // 4.4.4
  RegistryPrototype.prototype.keys = function() {
    if (typeof this !== 'object')
      throw new TypeError('invalid registry');
    if (__global.Map) {
      // native iterator
      return this._registry.registryMap.keys();
    } else {
      // polyfilled iterator
      var keys = Object.keys(this._registry.registryMap);
      var keyIndex = 0;
      return {
        next: function() {
          if (keyIndex < keys.length - 1) {
            return {
              value: keys[keyIndex++],
              done: false
            };
          } else {
            return {
              value: undefined,
              done: true
            };
          }
        }
      };
    }
  }

  // 4.4.5
  RegistryPrototype.prototype.values = function() {
    if (typeof this !== 'object')
      throw new TypeError('invalid registry');
    if (__global.Map) {
      // native iterator
      return this._registry.registryMap.values();
    } else {
      // polyfilled iterator
      var keys = Object.keys(this._registry.registryMap);
      var keyIndex = 0;
      var instance = this;
      return {
        next: function() {
          if (keyIndex < keys.length - 1) {
            return {
              value: instance._registry.registryMap[keys[keyIndex++]],
              done: false
            };
          } else {
            return {
              value: undefined,
              done: true
            };
          }
        }
      };
    }
  }

  // 4.4.6
  RegistryPrototype.prototype.get = function(key) {
    if (typeof this !== 'object')
      throw new TypeError('invalid registry');
    if (__global.Map)
      return this._registry.registryMap.get(key);
    else
      return this._registry.registryMap[key];
  }

  // 4.4.7
  RegistryPrototype.prototype.set = function(key, value) {
    if (typeof this !== 'object')
      throw new TypeError('invalid registry');
    if (__global.Map)
      this._registry.registryMap.set(key, value);
    else
      this._registry.registryMap[key] = value;

    return this;
  }

  // 4.4.8
  RegistryPrototype.prototype.has = function(key) {
    if (typeof this !== 'object')
      throw new TypeError('invalid registry');
    if (__global.Map)
      return this._registry.registryMap.has(key);
    else
      return this._registry.registryMap.hasOwnProperty(key);
  }

  // 4.4.9
  RegistryPrototype.prototype.delete = function(key) {
    if (typeof this !== 'object')
      throw new TypeError('invalid registry');
    if (__global.Map)
      return this._registry.registryMap.delete(key);
    else {
      var hadProperty = this._registry.registryMap.hasOwnProperty(key);
      delete this._registry.registryMap[key];
      return hadProperty;
    }
  }

  // 4.1.1 - TODO out of date
  function getCurrentStage(entry) {
    if (typeof entry !== 'object')
      throw new TypeError('entry is not an object');
    return entry.pipeline[0];
{'fraction_non_alphanumeric': 0.12542685240392434, 'fraction_numerical': 0.008347335898964714, 'mean_word_length': 2.2075799721835883, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 21, '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': '4460389', 'n_tokens_mistral': 5376, 'n_tokens_neox': 5215, 'n_words': 1445}