this is why I hate XPCOM
Aug. 4th, 2010 03:39 pm
// This is how you open a file. In the current working directory.
//
// THERE ARE 387.44 MILLION MILES OF PRINTED CIRCUITS IN WAFER THIN
// LAYERS THAT FILL MY COMPLEX. IF THE WORD HATE WAS ENGRAVED ON EACH
// NANOANGSTROM OF THOSE HUNDREDS OF MILLIONS OF MILES IT WOULD NOT
// EQUAL ONE ONE-BILLIONTH OF THE HATE I FEEL AT THIS MICRO-INSTANT.
const Cc = Components.classes;
const Ci = Components.interfaces;
const DS_CID = "@mozilla.org/file/directory_service;1";
const cwd = Cc[DS_CID].getService(Ci.nsIDirectoryServiceProvider)
.getFile("CurWorkD",{}).path;
const LF_CID = "@mozilla.org/file/local;1";
var outputFile = Cc[LF_CID].createInstance(Ci.nsILocalFile);
outputFile.initWithPath(cwd + '/bench-css.one');
const FOSTREAM_CID = "@mozilla.org/network/file-output-stream;1";
var ostream = Cc[FOSTREAM_CID].createInstance(Ci.nsIFileOutputStream);
ostream.init(outputFile,
0x02|0x08|0x10, // PR_WRONLY|PR_CREATE_FILE|PR_APPEND
438, // 0666; JS no longer supports octal
0); // useless
function output(s) {
ostream.write(s, s.length);
}
[EDIT: Yes, it's even worse than I thought.]
no subject
Date: 2010-08-04 10:58 pm (UTC)no subject
Date: 2010-08-04 11:03 pm (UTC)It's like the assembly language of JavaScript!
Date: 2010-08-04 11:42 pm (UTC)Re: It's like the assembly language of JavaScript!
Date: 2010-08-04 11:57 pm (UTC)Re: It's like the assembly language of JavaScript!
Date: 2010-08-05 12:04 am (UTC)Re: It's like the assembly language of JavaScript!
Date: 2010-08-05 12:12 am (UTC)no subject
Date: 2010-08-05 02:47 am (UTC)But that's pretty obnoxiously obnoxious.
no subject
Date: 2010-08-05 04:28 am (UTC)I think what I hate most about the code I quoted is not so much the excessive verbosity, or the magic numbers, or having to poke so many different objects, but in fact the complete lack of support for relative pathnames! I mean. Is this not a concept that has been well defined and (nigh-)universally accepted since nineteen fucking seventy, if not longer?
no subject
Date: 2010-08-05 05:04 am (UTC)This comment may give some idea. But even that code turned out to flaky for reasons I can't figure out, so I gave up and rewrote everything with blocking IO and threads. (But! The threads can't just use 'read' and 'write' to do blocking operations and stop caring whether they have a socket! Because, while the standard read/write API is allowed to be used on sockets, if your socket *can* handle simultaneous read/writes then the read/write APIs will error out if you try to use them in blocking mode. You can do blocking read/write, but you have to call recv/send to do it. Iff you have a socket.)
Thanks, I feel better now.
no subject
Date: 2010-08-06 12:42 am (UTC)no subject
Date: 2010-08-05 05:10 am (UTC)...Okay, but looking at that code, this DirectoryService nonsense is nonsense.
no subject
Date: 2010-08-06 12:41 am (UTC)Please tell me it's that easy
Date: 2010-08-05 10:51 am (UTC)"日本国" becomes 0xe5 0x2c 0xfd.
Yes, yes, I know no one cares about i18n. It's just amazing to me that with all the complexity, it still doesn't freaking work, and fails to work silently, and also fails in a way that doesn't suggest any obvious fix (I found something called nsIUnicharOutputStream! that must do this .. somehow?).
Meanwhile, in the real world, UTF-8 has won. Everyone knows a char * means UTF-8 now, and if you'd written an "append a string to a local file" function in C, you'd have to positively want it to fail for UTF-8 to break it in that way.
Re: Please tell me it's that easy
Date: 2010-08-06 12:43 am (UTC)Re: Please tell me it's that easy
Date: 2010-08-06 09:44 am (UTC)costream = Cc["@mozilla.org/intl/converter-outut-stream;1"].createInstance(Ci.nsIConverterOututStream);
costream.init(ostream, "UTF-8", 0, 0);
function output(s)
{
costream.writeString(s);
costream.flush(); // this flushes the converter stream, but does not necessarily flush the underlying output stream.
}
costream.flush is a totally different function from ostream.flush. It's probably a nop for UTF-8, but it does something, at least in theory, that no one has ever called "flushing". There's not even a method for flushing the underlying stream, that I can see.
Want to know how many bytes you've written? Oh, no, costream.writeString doesn't do that. costream.write doesn't do that either (but it takes its count as the first argument, and the data to be written as the second argument, because clearly doing things the same way as ostream.write would be boring).
Maybe there's an anti-ducktyping rationale? We'll give both classes functions called "flush" and "write", but make them totally different, just because we can?
I'd like to defend XPCOM. I can't really think of a good defense, though. Does "at least nsIUnicharInputStream.read mimics nsIInputStream.read " count?
If you're even in a position of thinking "oh, all I need is ASCII, so I won't bother handling UTF-8", something has gone very wrong in the internationalisation process.
Re: Please tell me it's that easy
Date: 2010-08-06 09:43 pm (UTC)encompassing
Date: 2011-06-02 10:58 pm (UTC)