I propose that simplify this to
open(path, [options])
where path is a string and the optional options argument is an object
like {read: Boolean, write: Boolean, append: Boolean, update: Boolean,
binary: Boolean, charset: String, delimiter: String} plus any other
flags/options that may make sense but _not_ the file path (i.e. this
is about the function signature, not what options we should support).
Calling open without the options argument would return a text reader
stream.
What do you think?
Hannes
ServerJS: x: exclusive lock
PHP: x: ... If the file already exists, the *fopen()* call will fail by
returning *FALSE* and generating an error of level *E_WARNING*. ...
This is equivalent to specifying /O_EXCL|O_CREAT/ flags for the
underlying /open(2)/ system call.
To be frank. http://draft.monkeyscript.org/api/io/File.html#open the
MonkeyScript treats x like PHP does, not like ServerJS does. Why? NSPR
supports the |PR_CREATE_FILE | ||PR_EXCL| but not locking right on open,
and ServerJS's omits |PR_CREATE_FILE | ||PR_EXCL.|
~Daniel Friesen (Dantman, Nadir-Seen-Fire) [http://daniel.friesen.name]
You might want to re-read that portion of the ServerJS spec. It was
changed to reflect the C, and therefore PHP, behavior as Wes Garland
clarified. The original verbiage was a mistake on my part in
understanding the intent of the flag and knowledge of the underlying
C.
Kris Kowal
Yes, it certainly would.
> I propose that simplify this to
>
> open(path, [options])
>
> where path is a string and the optional options argument is an object
> like {read: Boolean, write: Boolean, append: Boolean, update: Boolean,
> binary: Boolean, charset: String, delimiter: String} plus any other
> flags/options that may make sense but _not_ the file path (i.e. this
> is about the function signature, not what options we should support).
> Calling open without the options argument would return a text reader
> stream.
>
> What do you think?
+1, but I still want to be able to do the following:
open(path)
open(path, "w")
read(path, "b")
write(path, content, "b")
To that end, would it be sufferable to permit the "options" to be
coerced from an fopen-like String?
And on the very edge of desirability:
open(path, "w", {charset: "UTF-8"})
write(path, content, "b", {charset: "UTF-8"})
To this end, would it be permissible for open, read, and write, to
accept a variadic list of options objects, any of which coercible from
an fopen-like String, with right-to-left precedence, that is, updated
from left to right?
open(path, [options, …])
read(path, [options, …])
write(path, content, [options …])
?
Kris Kowal
Sorry, this example didn't make sense. Imagine:
> write(path, content, "x", {charset: "UTF-8"})
Kris Kowal
This strikes me as needlessly complex. Think about how you would
document this, and it starts to get hairy. If you want to specify a
charset and a mode, then pass it in the options dict/hash. Simple, and
easy to remember:
open(path, [mode]);
open(path, options);
No special cases. No complexity, almost no extra work on part of the
API user.
-ash
+1.
(flags=mode, mode=access; This get's confusing when some people use mode
for open mode and in other cases mode refers to permissions)
Though my own open without the options object will still be a full
File.open(path, flags, [encoding=File.systemEncoding], [mode], [block]);
ie: While the API for my File object is different I aim for a level of
compatibility with ServerJS in things like what .read() returns so that
the ServerJS stuff in my environment (ie: banana('serverjs');) can be
implemented as simple wrappers around the MonkeyScript stuff.
>
> 2009/5/31 Ash Berlin <ash_g...@firemirror.com>:
>>
>>
>> This strikes me as needlessly complex. Think about how you would
>> document this, and it starts to get hairy. If you want to specify a
>> charset and a mode, then pass it in the options dict/hash. Simple,
>> and
>> easy to remember:
>>
>> open(path, [mode]);
>> open(path, options);
>
> Just to be sure, that means that the second argument can either be a
> mode string, or an options object?
Yes, that was my intent. Path is always required. 2nd arg is an
optional mode (a string) or a options dictionary.
>
> If we support open mode strings, should we also support them within
> the options object, e.g.:
>
> open(path, {mode: "r", charset: "UTF-8"});
>
> Hannes
If you like. I've not thought about if it would be easier to have it
spelt out in terms of X different flags or not. I'll leave that up to
you (and to list which mode strings we accept)
I've updated the wiki to reflect these ideas:
https://wiki.mozilla.org/ServerJS/Filesystem_API/A#Files
; open(path String, mode|options)
: returns a stream object that supports an appropriate interface for
the given options and mode, which include reading, writing, updating,
byte, character, unbuffered, buffered, and line buffered streams.
More details follow in the [#Stream] section of this document.
* path
* mode: "rwa+bxc", or
* options
** mode String
** charset String
** read Boolean
** write Boolean
** append Boolean
** update Boolean
** binary Boolean
** exclusive Boolean
** canonical Boolean
; read(path String, [mode|options])
: opens, reads, and closes a file, returning its content.
; write(path String, content String|Binary, [mode|options])
: opens, writes, flushes, and closes a file with the given content.
If the content is a ByteArray or ByteString, the binary mode is
implied.
Please continue if there are outstanding issues. For example, whether
we require "canonical" IO or not is another issue, also whether to
rectify history's wrongs and make it clear that "canonical" IO means
"non-blocking".
Kris Kowal
I think at the very least canonical and exclusive need some actual
explanation to avoid confusion.
exclusive feels a little ambiguous between exclusive lock and don't create.
wait, update? was that read+write or something else?
Ditto. Thats what I thought too. This absolutely needs changing. What
API does the "c" char in the mode come from? I've never come across it
before.
Also we need to specify where the precedence is when mode and any of
the boolean flags are specified. i.e.:
open(path, {mode: "rb", binary: false, write: true, read: false }) //
WTF does this do.
Thats partly why I'm not sure having mode in the options is needed.
>
> I think at the very least canonical and exclusive need some actual
> explanation to avoid confusion.
> exclusive feels a little ambiguous between exclusive lock and don't
> create.
>
> wait, update? was that read+write or something else?
>
> ~Daniel Friesen (Dantman, Nadir-Seen-Fire) [http://
> daniel.friesen.name]
Yeah - what is update?
-ash
>
> Wes's interpretation of canonical is the one intended. Please forget
> my statement about non-blocking.
Ah that one. It's been a while since I've done stuff at that low a
level.
Okay so does canonical make sense in text mode, or should it only be
possible with binary?
Also is it uses often enough that it is needed in the mode string?
And no one has yet suggested how we behave when the mode strings and
the flags contradict each other - which one takes effect?
-ash
Ah that one. It's been a while since I've done stuff at that low a
level.
Okay so does canonical make sense in text mode, or should it only be
possible with binary?
Also is it uses often enough that it is needed in the mode string?
And no one has yet suggested how we behave when the mode strings and
the flags contradict each other - which one takes effect?
Java
(AFAIK) just doesn't go this low level and even C isn't entirely
consistent between *NIX systems.
Last time I looked, unbuffered stdin was not possible to get hold of in
Java.
Eclipse is different - key presses etc come is as Events through which ever
windowing platform (Swing, or what ever it is) rather than as a stdin
stream.
I think Mark Porter might have a good case - this isn't possibly to do
portably, and also isn't something that people care about very often. If
they need this, then it should probably be an extra C/'native' module that
sets this.
Thoughts?
Well, NSPR doesn't even support canonical. std(in/out/err) is separate
from normal file handles.
So I can say that even if ServerJS supports canonical I won't be
supporting it in MonkeyScript, even in the ServerJS wrapper.
I'm thinking that we should drop the "canonical" mode. It was
ill-advised on my part. CANON is usually set with another function
(ioctl?) anyway, and it's by far the more common case to have a tty
stream that you want to set to canonical mode than it is to directly
open one, like open("/dev/tty", "ubc"). I think we should at least
postpone the issue, as it can be tacked on later.
Kris Kowal
I made that statement because as far as I know, I don't even see any
current implementations that support canonical, and from the looks of it
getting someone to write support for that wouldn't fit cost to benefit.
Ya, some random C/C++ programmer could throw a patch the way of the
project, but the price to ask someone to write that chunk of code
doesn't seam worth it.
> Wes
>
> --
> Wesley W. Garland
> Director, Product Development
> PageMail, Inc.
> +1 613 542 2787 x 102
NSPR is a platform abstraction library. And a perfectly reasonable one
for using toe build an IO module with.
If it doesn't support canonical handling of a stream then should we
really be forcing the programmer of an implementation to write a pile of
extra code taking platform differences into account, for a feature that
only a few libraries are likely to even use?
I made that statement because as far as I know, I don't even see any
current implementations that support canonical, and from the looks of it
getting someone to write support for that wouldn't fit cost to benefit.
Ya, some random C/C++ programmer could throw a patch the way of the
project, but the price to ask someone to write that chunk of code
doesn't seam worth it.
Question, canonical on file.open;
If we're putting canonical in the mode, what are we expecting it to work on?
What abstraction layers are able to support canonical mode, where?
Is this something that belongs generically in open, or is this something
which only applies to stdin (in which case, didn't we put stdin at
system.stdin rather than file.open?).
Since this is stdin stuff, I'd vote for:
system.stdin.canonical;
Or whatever property names you want to come up with for the options.
As for portability and Windows. Would closing the stdin and re-opening
it in the other mode in the background if it is changed suffice?
If so I think that would probably be the best option.
In the long term, raw IO can be emulated using lower-level calls like
_kbhit(), as I recall.
Again, I'm advising that we defer discussion of
canonical/non-canonical IO, and removing the "c" mode character from
the specification. Canonical IO is not generally supported by flags
to "open", and is relegated to the obscurata of other functions.
Kris Kowal
+1 on dropping canonical, I don't think this could be implemented in
java without resorting to native code. For the purpose of implementing
line editing features, the best approach IMO is to use an existing
library such as readline or jline.
2009/6/2 Ash Berlin <ash_g...@firemirror.com>:
>
> Also we need to specify where the precedence is when mode and any of
> the boolean flags are specified. i.e.:
>
> open(path, {mode: "rb", binary: false, write: true, read: false }) //
> WTF does this do.
>
> Thats partly why I'm not sure having mode in the options is needed.
>
Yes, I also think that having both a "mode" and
"read"|"write"|"update"... properties in the options object is
redundant and potentially dangerous, so I'm for not supporting
options.mode.
Regarding the question how to deal with conflicting mode/option flags,
I suggest to demand throwing a TypeError, like ES5 defines for
conflicting property descriptors.
>>
>> wait, update? was that read+write or something else?
>>
>> ~Daniel Friesen (Dantman, Nadir-Seen-Fire) [http://
>> daniel.friesen.name]
>
> Yeah - what is update?
>
It returns an Updater object that provides read + write + seek:
https://wiki.mozilla.org/ServerJS/Filesystem_API/A#.2AReader.2C_.2AUpdater
Hannes