~ubuntu-branches/ubuntu/vivid/emscripten/vivid

« back to all changes in this revision

Viewing changes to src/library_path.js

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2013-09-20 22:44:35 UTC
  • mfrom: (4.1.1 sid)
  • Revision ID: package-import@ubuntu.com-20130920224435-apuwj4fsl3fqv1a6
Tags: 1.5.6~20130920~6010666-1
* New snapshot release
* Update the list of supported architectures to the same as libv8
  (Closes: #723129)
* emlibtool has been removed from upstream.
* Fix warning syntax-error-in-dep5-copyright
* Refresh of the patches

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
mergeInto(LibraryManager.library, {
 
2
  $PATH__deps: ['$FS'],
 
3
  $PATH: {
 
4
    // split a filename into [root, dir, basename, ext], unix version
 
5
    // 'root' is just a slash, or nothing.
 
6
    splitPath: function(filename) {
 
7
      var splitPathRe = /^(\/?|)([\s\S]*?)((?:\.{1,2}|[^\/]+?|)(\.[^.\/]*|))(?:[\/]*)$/;
 
8
      return splitPathRe.exec(filename).slice(1);
 
9
    },
 
10
    normalizeArray: function(parts, allowAboveRoot) {
 
11
      // if the path tries to go above the root, `up` ends up > 0
 
12
      var up = 0;
 
13
      for (var i = parts.length - 1; i >= 0; i--) {
 
14
        var last = parts[i];
 
15
        if (last === '.') {
 
16
          parts.splice(i, 1);
 
17
        } else if (last === '..') {
 
18
          parts.splice(i, 1);
 
19
          up++;
 
20
        } else if (up) {
 
21
          parts.splice(i, 1);
 
22
          up--;
 
23
        }
 
24
      }
 
25
      // if the path is allowed to go above the root, restore leading ..s
 
26
      if (allowAboveRoot) {
 
27
        for (; up--; up) {
 
28
          parts.unshift('..');
 
29
        }
 
30
      }
 
31
      return parts;
 
32
    },
 
33
    normalize: function(path) {
 
34
      var isAbsolute = path.charAt(0) === '/',
 
35
          trailingSlash = path.substr(-1) === '/';
 
36
      // Normalize the path
 
37
      path = PATH.normalizeArray(path.split('/').filter(function(p) {
 
38
        return !!p;
 
39
      }), !isAbsolute).join('/');
 
40
      if (!path && !isAbsolute) {
 
41
        path = '.';
 
42
      }
 
43
      if (path && trailingSlash) {
 
44
        path += '/';
 
45
      }
 
46
      return (isAbsolute ? '/' : '') + path;
 
47
    },
 
48
    dirname: function(path) {
 
49
      var result = PATH.splitPath(path),
 
50
          root = result[0],
 
51
          dir = result[1];
 
52
      if (!root && !dir) {
 
53
        // No dirname whatsoever
 
54
        return '.';
 
55
      }
 
56
      if (dir) {
 
57
        // It has a dirname, strip trailing slash
 
58
        dir = dir.substr(0, dir.length - 1);
 
59
      }
 
60
      return root + dir;
 
61
    },
 
62
    basename: function(path, ext) {
 
63
      // EMSCRIPTEN return '/'' for '/', not an empty string
 
64
      if (path === '/') return '/';
 
65
      var f = PATH.splitPath(path)[2];
 
66
      if (ext && f.substr(-1 * ext.length) === ext) {
 
67
        f = f.substr(0, f.length - ext.length);
 
68
      }
 
69
      return f;
 
70
    },
 
71
    extname: function(path) {
 
72
      return PATH.splitPath(path)[3];
 
73
    },
 
74
    join: function() {
 
75
      var paths = Array.prototype.slice.call(arguments, 0);
 
76
      return PATH.normalize(paths.filter(function(p, index) {
 
77
        if (typeof p !== 'string') {
 
78
          throw new TypeError('Arguments to path.join must be strings');
 
79
        }
 
80
        return p;
 
81
      }).join('/'));
 
82
    },
 
83
    resolve: function() {
 
84
      var resolvedPath = '',
 
85
        resolvedAbsolute = false;
 
86
      for (var i = arguments.length - 1; i >= -1 && !resolvedAbsolute; i--) {
 
87
        var path = (i >= 0) ? arguments[i] : FS.cwd();
 
88
        // Skip empty and invalid entries
 
89
        if (typeof path !== 'string') {
 
90
          throw new TypeError('Arguments to path.resolve must be strings');
 
91
        } else if (!path) {
 
92
          continue;
 
93
        }
 
94
        resolvedPath = path + '/' + resolvedPath;
 
95
        resolvedAbsolute = path.charAt(0) === '/';
 
96
      }
 
97
      // At this point the path should be resolved to a full absolute path, but
 
98
      // handle relative paths to be safe (might happen when process.cwd() fails)
 
99
      resolvedPath = PATH.normalizeArray(resolvedPath.split('/').filter(function(p) {
 
100
        return !!p;
 
101
      }), !resolvedAbsolute).join('/');
 
102
      return ((resolvedAbsolute ? '/' : '') + resolvedPath) || '.';
 
103
    },
 
104
    relative: function(from, to) {
 
105
      from = PATH.resolve(from).substr(1);
 
106
      to = PATH.resolve(to).substr(1);
 
107
      function trim(arr) {
 
108
        var start = 0;
 
109
        for (; start < arr.length; start++) {
 
110
          if (arr[start] !== '') break;
 
111
        }
 
112
        var end = arr.length - 1;
 
113
        for (; end >= 0; end--) {
 
114
          if (arr[end] !== '') break;
 
115
        }
 
116
        if (start > end) return [];
 
117
        return arr.slice(start, end - start + 1);
 
118
      }
 
119
      var fromParts = trim(from.split('/'));
 
120
      var toParts = trim(to.split('/'));
 
121
      var length = Math.min(fromParts.length, toParts.length);
 
122
      var samePartsLength = length;
 
123
      for (var i = 0; i < length; i++) {
 
124
        if (fromParts[i] !== toParts[i]) {
 
125
          samePartsLength = i;
 
126
          break;
 
127
        }
 
128
      }
 
129
      var outputParts = [];
 
130
      for (var i = samePartsLength; i < fromParts.length; i++) {
 
131
        outputParts.push('..');
 
132
      }
 
133
      outputParts = outputParts.concat(toParts.slice(samePartsLength));
 
134
      return outputParts.join('/');
 
135
    }
 
136
  }
 
137
});
 
 
b'\\ No newline at end of file'