<commit_before>// Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import AxiomError from 'axiom/core/error';
import environment from 'axiom_shell/environment';

export var main = function(cx) {
  var onImportLoaded = function(resolve, reject, e) {
    var todoList = [];

    // There could be multiple x-axiom-plugin nodes in this import.
    var plugins = e.target.import.querySelectorAll('x-axiom-plugin');

    // Find them all, sanity check them, and add to a list of plugins to
    // process.
    for (var i = 0; i < plugins.length; i++) {
      var plugin = plugins[i];
      var desc = plugin.getAttribute('x-descriptor-module');
      if (!desc)
        console.stderr('Missing x-descriptor-module');

      var main = plugin.getAttribute('x-main-module');
      if (!main)
        console.stderr('Missing x-main-module');

      if (desc && main)
        todoList.push([desc, main]);
    }

    // Bail out if we don't find any valid plugins.
    if (!todoList.length)
      return reject(null);

    var nextTodo = function() {
      if (!todoList.length)
        return resolve(null);

      var todo = todoList.shift();
      var descriptor = environment.requireModule(todo[0])['default'];
      if (!descriptor) {
        return resolve(new AxiomError.Runtime(
            'No descriptor exported by: ' + todo[0]));
      }

      var module = environment.defineModule(descriptor);
      cx.stdout('New module: ' + module.moduleId + '\n');

      var main = environment.requireModule(todo[1])['default'];
      if (!main) {
        return resolve(new AxiomError.Runtime(
            'No main exported by: ' + todo[1]));
      }

      return main(module).then(function() {
          cx.stdout('Module ready.\n');
          return nextTodo();
        });
    };

    return nextTodo();
  };

  return new Promise(function(resolve, reject) {
      cx.ready();
      var url = cx.arg.url;
      if (!url)
        return reject(new AxiomError.Missing('url'));

      var link = document.createElement('link');
      link.rel = 'import';
      link.href = url;
      link.addEventListener('error', function(e) {
          cx.stdout('Error loading import: ' + e.target.href + '\n');
          link.parentNode.removeChild(link);
          return reject(null);
        });

      link.addEventListener('load',
                            onImportLoaded.bind(null, resolve, reject));<commit_msg>Pass source url to imported module.<commit_after>// Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import AxiomError from 'axiom/core/error';
import environment from 'axiom_shell/environment';

export var main = function(cx) {
  var onImportLoaded = function(resolve, reject, e) {
    var todoList = [];

    // There could be multiple x-axiom-plugin nodes in this import.
    var plugins = e.target.import.querySelectorAll('x-axiom-plugin');

    // Find them all, sanity check them, and add to a list of plugins to
    // process.
    for (var i = 0; i < plugins.length; i++) {
      var plugin = plugins[i];
      var desc = plugin.getAttribute('x-descriptor-module');
      if (!desc)
        console.stderr('Missing x-descriptor-module');

      var main = plugin.getAttribute('x-main-module');
      if (!main)
        console.stderr('Missing x-main-module');

      if (desc && main)
        todoList.push([desc, main]);
    }

    // Bail out if we don't find any valid plugins.
    if (!todoList.length)
      return reject(null);

    var nextTodo = function() {
      if (!todoList.length)
        return resolve(null);

      var todo = todoList.shift();
      var descriptor = environment.requireModule(todo[0])['default'];
      if (!descriptor) {
        return resolve(new AxiomError.Runtime(
            'No descriptor exported by: ' + todo[0]));
      }

      var module = environment.defineModule(descriptor);
      cx.stdout('New module: ' + module.moduleId + '\n');

      var main = environment.requireModule(todo[1])['default'];
      if (!main) {
        return resolve(new AxiomError.Runtime(
            'No main exported by: ' + todo[1]));
      }

      return main(module, { sourceUrl: e.target.import.URL }).then(function() {
          cx.stdout('Module ready.\n');
          return nextTodo();
        });
    };

    return nextTodo();
  };

  return new Promise(function(resolve, reject) {
      cx.ready();
      var url = cx.arg.url;
      if (!url)
        return reject(new AxiomError.Missing('url'));

      var link = document.createElement('link');
      link.rel = 'import';
      link.href = url;
      link.addEventListener('error', function(e) {
          cx.stdout('Error loading import: ' + e.target.href + '\n');
          link.parentNode.removeChild(link);
          return reject(null);
        });

      link.addEventListener('load',
                            onImportLoaded.bind(null, resolve, reject));
{'fraction_non_alphanumeric': 0.13479623824451412, 'fraction_numerical': 0.0035266457680250786, 'mean_word_length': 2.4633649932157393, 'pattern_counts': {'":': 0, '<': 5, '<?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': '2987159', 'n_tokens_mistral': 1476, 'n_tokens_neox': 1437, 'n_words': 413}