|
16 | 16 | console: false, java: false, module: false, requirejsVars, navigator, |
17 | 17 | document, importScripts, self, location, Components, FileUtils */ |
18 | 18 |
|
19 | | -var requirejs, require, define; |
| 19 | +var requirejs, require, define, xpcUtil; |
20 | 20 | (function (console, args, readFileFunc) { |
21 | 21 | var fileName, env, fs, vm, path, exec, rhinoContext, dir, nodeRequire, |
22 | 22 | nodeDefine, exists, reqMain, loadedOptimizedLib, existsForNode, Cc, Ci, |
23 | | - cwd, normalize, xpfile, |
24 | 23 | version = '2.1.4+', |
25 | 24 | jsSuffixRegExp = /\.js$/, |
26 | 25 | commandOption = '', |
@@ -136,85 +135,89 @@ var requirejs, require, define; |
136 | 135 | fileName = args[1]; |
137 | 136 | } |
138 | 137 |
|
139 | | - cwd = function () { |
140 | | - return FileUtils.getFile("CurWorkD", []).path; |
141 | | - }; |
142 | | - |
143 | | - //Remove . and .. from paths, normalize on front slashes |
144 | | - normalize = function (path) { |
145 | | - //There has to be an easier way to do this. |
146 | | - var i, part, ary, |
147 | | - firstChar = path.charAt(0); |
148 | | - |
149 | | - if (firstChar !== '/' && |
150 | | - firstChar !== '\\' && |
151 | | - path.indexOf(':') === -1) { |
152 | | - //A relative path. Use the current working directory. |
153 | | - path = cwd() + '/' + path; |
154 | | - } |
155 | | - |
156 | | - ary = path.replace(/\\/g, '/').split('/'); |
157 | | - |
158 | | - for (i = 0; i < ary.length; i += 1) { |
159 | | - part = ary[i]; |
160 | | - if (part === '.') { |
161 | | - ary.splice(i, 1); |
162 | | - i -= 1; |
163 | | - } else if (part === '..') { |
164 | | - ary.splice(i - 1, 2); |
165 | | - i -= 2; |
| 138 | + xpcUtil = { |
| 139 | + cwd: function () { |
| 140 | + return FileUtils.getFile("CurWorkD", []).path; |
| 141 | + }, |
| 142 | + |
| 143 | + //Remove . and .. from paths, normalize on front slashes |
| 144 | + normalize: function (path) { |
| 145 | + //There has to be an easier way to do this. |
| 146 | + var i, part, ary, |
| 147 | + firstChar = path.charAt(0); |
| 148 | + |
| 149 | + if (firstChar !== '/' && |
| 150 | + firstChar !== '\\' && |
| 151 | + path.indexOf(':') === -1) { |
| 152 | + //A relative path. Use the current working directory. |
| 153 | + path = xpcUtil.cwd() + '/' + path; |
166 | 154 | } |
167 | | - } |
168 | | - return ary.join('/'); |
169 | | - }; |
170 | 155 |
|
171 | | - xpfile = function (path) { |
172 | | - try { |
173 | | - return new FileUtils.File(normalize(path)); |
174 | | - } catch (e) { |
175 | | - throw new Error(path + ' failed: ' + e); |
176 | | - } |
177 | | - }; |
| 156 | + ary = path.replace(/\\/g, '/').split('/'); |
178 | 157 |
|
179 | | - readFile = function (/*String*/path, /*String?*/encoding) { |
180 | | - //A file read function that can deal with BOMs |
181 | | - encoding = encoding || "utf-8"; |
182 | | - |
183 | | - var inStream, convertStream, |
184 | | - readData = {}, |
185 | | - fileObj = xpfile(path); |
186 | | - |
187 | | - //XPCOM, you so crazy |
188 | | - try { |
189 | | - inStream = Cc['@mozilla.org/network/file-input-stream;1'] |
190 | | - .createInstance(Ci.nsIFileInputStream); |
191 | | - inStream.init(fileObj, 1, 0, false); |
192 | | - |
193 | | - convertStream = Cc['@mozilla.org/intl/converter-input-stream;1'] |
194 | | - .createInstance(Ci.nsIConverterInputStream); |
195 | | - convertStream.init(inStream, encoding, inStream.available(), |
196 | | - Ci.nsIConverterInputStream.DEFAULT_REPLACEMENT_CHARACTER); |
197 | | - |
198 | | - convertStream.readString(inStream.available(), readData); |
199 | | - return readData.value; |
200 | | - } catch (e) { |
201 | | - throw new Error((fileObj && fileObj.path || '') + ': ' + e); |
202 | | - } finally { |
203 | | - if (convertStream) { |
204 | | - convertStream.close(); |
| 158 | + for (i = 0; i < ary.length; i += 1) { |
| 159 | + part = ary[i]; |
| 160 | + if (part === '.') { |
| 161 | + ary.splice(i, 1); |
| 162 | + i -= 1; |
| 163 | + } else if (part === '..') { |
| 164 | + ary.splice(i - 1, 2); |
| 165 | + i -= 2; |
| 166 | + } |
205 | 167 | } |
206 | | - if (inStream) { |
207 | | - inStream.close(); |
| 168 | + return ary.join('/'); |
| 169 | + }, |
| 170 | + |
| 171 | + xpfile: function (path) { |
| 172 | + try { |
| 173 | + return new FileUtils.File(xpcUtil.normalize(path)); |
| 174 | + } catch (e) { |
| 175 | + throw new Error(path + ' failed: ' + e); |
| 176 | + } |
| 177 | + }, |
| 178 | + |
| 179 | + readFile: function (/*String*/path, /*String?*/encoding) { |
| 180 | + //A file read function that can deal with BOMs |
| 181 | + encoding = encoding || "utf-8"; |
| 182 | + |
| 183 | + var inStream, convertStream, |
| 184 | + readData = {}, |
| 185 | + fileObj = xpcUtil.xpfile(path); |
| 186 | + |
| 187 | + //XPCOM, you so crazy |
| 188 | + try { |
| 189 | + inStream = Cc['@mozilla.org/network/file-input-stream;1'] |
| 190 | + .createInstance(Ci.nsIFileInputStream); |
| 191 | + inStream.init(fileObj, 1, 0, false); |
| 192 | + |
| 193 | + convertStream = Cc['@mozilla.org/intl/converter-input-stream;1'] |
| 194 | + .createInstance(Ci.nsIConverterInputStream); |
| 195 | + convertStream.init(inStream, encoding, inStream.available(), |
| 196 | + Ci.nsIConverterInputStream.DEFAULT_REPLACEMENT_CHARACTER); |
| 197 | + |
| 198 | + convertStream.readString(inStream.available(), readData); |
| 199 | + return readData.value; |
| 200 | + } catch (e) { |
| 201 | + throw new Error((fileObj && fileObj.path || '') + ': ' + e); |
| 202 | + } finally { |
| 203 | + if (convertStream) { |
| 204 | + convertStream.close(); |
| 205 | + } |
| 206 | + if (inStream) { |
| 207 | + inStream.close(); |
| 208 | + } |
208 | 209 | } |
209 | 210 | } |
210 | 211 | }; |
211 | 212 |
|
| 213 | + readFile = xpcUtil.readFile; |
| 214 | + |
212 | 215 | exec = function (string) { |
213 | 216 | return eval(string); |
214 | 217 | }; |
215 | 218 |
|
216 | 219 | exists = function (fileName) { |
217 | | - return xpfile(fileName).exists(); |
| 220 | + return xpcUtil.xpfile(fileName).exists(); |
218 | 221 | }; |
219 | 222 |
|
220 | 223 | //Define a console.log for easier logging. Don't |
|
0 commit comments