Skip to content

Commit c2b4bd7

Browse files
committed
dedupe some xpconnect stuff.
1 parent 01ccfb5 commit c2b4bd7

File tree

2 files changed

+76
-144
lines changed

2 files changed

+76
-144
lines changed

build/jslib/x.js

Lines changed: 71 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,10 @@
1616
console: false, java: false, module: false, requirejsVars, navigator,
1717
document, importScripts, self, location, Components, FileUtils */
1818

19-
var requirejs, require, define;
19+
var requirejs, require, define, xpcUtil;
2020
(function (console, args, readFileFunc) {
2121
var fileName, env, fs, vm, path, exec, rhinoContext, dir, nodeRequire,
2222
nodeDefine, exists, reqMain, loadedOptimizedLib, existsForNode, Cc, Ci,
23-
cwd, normalize, xpfile,
2423
version = '2.1.4+',
2524
jsSuffixRegExp = /\.js$/,
2625
commandOption = '',
@@ -136,85 +135,89 @@ var requirejs, require, define;
136135
fileName = args[1];
137136
}
138137

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;
166154
}
167-
}
168-
return ary.join('/');
169-
};
170155

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('/');
178157

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+
}
205167
}
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+
}
208209
}
209210
}
210211
};
211212

213+
readFile = xpcUtil.readFile;
214+
212215
exec = function (string) {
213216
return eval(string);
214217
};
215218

216219
exists = function (fileName) {
217-
return xpfile(fileName).exists();
220+
return xpcUtil.xpfile(fileName).exists();
218221
};
219222

220223
//Define a console.log for easier logging. Don't

build/jslib/xpconnect/file.js

Lines changed: 5 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -6,54 +6,14 @@
66
//Helper functions to deal with file I/O.
77

88
/*jslint plusplus: false */
9-
/*global define, Components, FileUtils */
9+
/*global define, Components, xpcUtil */
1010

1111
define(['prim'], function (prim) {
1212
var file,
1313
Cc = Components.classes,
14-
Ci = Components.interfaces;
15-
16-
Components.utils['import']('resource://gre/modules/FileUtils.jsm');
17-
18-
function cwd() {
19-
return FileUtils.getFile("CurWorkD", []).path;
20-
}
21-
22-
//Remove . and .. from paths, normalize on front slashes
23-
function normalize(path) {
24-
//There has to be an easier way to do this.
25-
var i, part, ary,
26-
firstChar = path.charAt(0);
27-
28-
if (firstChar !== '/' &&
29-
firstChar !== '\\' &&
30-
path.indexOf(':') === -1) {
31-
//A relative path. Use the current working directory.
32-
path = cwd() + '/' + path;
33-
}
34-
35-
ary = path.replace(/\\/g, '/').split('/');
36-
37-
for (i = 0; i < ary.length; i += 1) {
38-
part = ary[i];
39-
if (part === '.') {
40-
ary.splice(i, 1);
41-
i -= 1;
42-
} else if (part === '..') {
43-
ary.splice(i - 1, 2);
44-
i -= 2;
45-
}
46-
}
47-
return ary.join('/');
48-
}
49-
50-
function xpfile(path) {
51-
try {
52-
return new FileUtils.File(normalize(path));
53-
} catch (e) {
54-
throw new Error(path + ' failed: ' + e);
55-
}
56-
}
14+
Ci = Components.interfaces,
15+
//Depends on xpcUtil which is set up in x.js
16+
xpfile = xpcUtil.xpfile;
5717

5818
function mkFullDir(dirObj) {
5919
//1 is DIRECTORY_TYPE, 511 is 0777 permissions
@@ -208,38 +168,7 @@ define(['prim'], function (prim) {
208168
return xpfile(from).moveTo(toFile.parent, toFile.leafName);
209169
},
210170

211-
readFile: function (/*String*/path, /*String?*/encoding) {
212-
//A file read function that can deal with BOMs
213-
encoding = encoding || "utf-8";
214-
215-
var inStream, convertStream,
216-
readData = {},
217-
fileObj = xpfile(path);
218-
219-
//XPCOM, you so crazy
220-
try {
221-
inStream = Cc['@mozilla.org/network/file-input-stream;1']
222-
.createInstance(Ci.nsIFileInputStream);
223-
inStream.init(fileObj, 1, 0, false);
224-
225-
convertStream = Cc['@mozilla.org/intl/converter-input-stream;1']
226-
.createInstance(Ci.nsIConverterInputStream);
227-
convertStream.init(inStream, encoding, inStream.available(),
228-
Ci.nsIConverterInputStream.DEFAULT_REPLACEMENT_CHARACTER);
229-
230-
convertStream.readString(inStream.available(), readData);
231-
return readData.value;
232-
} catch (e) {
233-
throw new Error((fileObj && fileObj.path || '') + ': ' + e);
234-
} finally {
235-
if (convertStream) {
236-
convertStream.close();
237-
}
238-
if (inStream) {
239-
inStream.close();
240-
}
241-
}
242-
},
171+
readFile: xpcUtil.readFile,
243172

244173
readFileAsync: function (path, encoding) {
245174
var d = prim();

0 commit comments

Comments
 (0)