-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for Newspeak's Files #190
Conversation
Thanks for the PR! Haven't looked at the code yet. So, just some basic first questions: The file gives us access to byte buffers, or what exactly is Are you already using it for something? Would it be possible to use the provided changes to change TestRunner to pick up tests automatically? At the moment, we are hardcoding them, which is of course not exactly ideal. |
The buffer is a SOMns Array (long), reading stuff as a string is currently not supported, for that streams would be necessary. I think it should be possible to pick up test automatically, streams would help |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are a couple of things, I'd prefer to see changed I think:
- return values, self/this is often preferable to nil
- block dispatch nodes would be better than hardcoded invokes
- please remove dead code
- tests better integrated with unit tests
and a few other things
tests/files/test.sh
Outdated
@@ -0,0 +1,29 @@ | |||
#!/bin/bash |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why did you chose to run those tests separately and not as unit tests with the other things in TestRunner?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hm, right, i didn't end up needing batch magic for cleanup
} catch (FileNotFoundException e) { | ||
fail.getMethod().invoke(new Object[] {fail, e.toString()}); | ||
} | ||
/* |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please don't commit dead code
private final File f; | ||
|
||
public static void setSOMClass(final SClass cls) { | ||
// assert fileDescriptorClass == null || cls == null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No dead code please
core-lib/Files.ns
Outdated
@@ -0,0 +1,657 @@ | |||
class Files usingVmMirror: vmMirror usingPlatform: platform = Value( | |||
(* This code was derived by converting the Strongtalk File classes to Newspeak, which is why the Sun Microsystems copyright and BSD license below applies. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this an unedited copy of the file?
If not, could you first have a commit with the original, and then the necessary changes?
Makes it much easier to track divergence.
Also, any chance you could improve the formatting of the file for human readers? (ideally in a separate commit)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The File API was split into multiple files and classes which I merged, so I don't think committing changes on top of the newspeak original would make sense. Also the formatting was worse. I am going to change that comment to say it's based on NewSpeak File classes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then please carefully attribute the source.
We need to be able to at least find the original files, ideally with an URL into the original repo for each, including version information.
core-lib/Files.ns
Outdated
public simpleName ^ <String> = ( | ||
^elements last name | ||
) | ||
(* |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No dead code please (also in other places)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's a public method accessible by users (which isn't used internally)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
writeStream
looks like it is in a comment, so, it's not accessible?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I misinterpreted the comment, only the (* of the comment was visible. I decided to keep the stream based methods as comments for the case that streams are implemented in the future. Those methods could be useful for reading text from files.
try { | ||
if (mode.getString().equals("read")) { | ||
raf = new RandomAccessFile(f, "r"); | ||
} else { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, what does Newspeak do if you pass in #foobar
? or nil
, or whatever for mode
? here we get crashes, or if it is any symbol, we get write access. Might be better to have an exception or call the failure block?
open = true; | ||
} | ||
|
||
public void closeFile() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, you got java doc on the next method, but not here?
|
||
public void write(final int nBytes, final long position, final SBlock fail) { | ||
if (!open) { | ||
fail.getMethod().invoke(new Object[] {fail, "File not open"}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
block node please, also the other cases.
byte[] buff = new byte[bufferSize]; | ||
|
||
for (int i = 0; i < bufferSize; i++) { | ||
// punish users for using this loophole |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ehm, no?! Throw a proper language level exception, please.
raf.close(); | ||
open = false; | ||
} catch (IOException e) { | ||
e.printStackTrace(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A Smalltalk exception would be much nicer
e555f58
to
c7fa31f
Compare
Adds support for file access similar to Newspeak, the major difference in functionality is the lack of streams. - FileDescriptor used to read/write files, a integrated buffer is used to store read data and data to write. - FilePath: represents a single file or directory and provides some operations (e.g. copy, move, size) - FilePattern: represents a set of files or directories in the form of a Path with wildcards (*, ?) and provides operations (e.g. pathsDo: , delete). Example: ``` f:: FilePath for: ('/directory/file'). //create a filepath. d:: f open: #readwrite ifFail: [:r|]. //returns file descriptor. d buffer at: 1 put: 127. d write: 1 at: 0 ifFail: [:r|]. //write 01111111 at the files beginning. ``` Code based on: https://bitbucket.org/newspeaklanguage/newspeak/src/38a47c705f1a1ab3359f2a58b79e8c728bfb218f/Files.ns https://bitbucket.org/newspeaklanguage/newspeak/src/38a47c705f1a1ab3359f2a58b79e8c728bfb218f/Win32Files.ns
Ok, I did some more polishing, moving test files a bit around, fix formatting a little. Nothing much of consequence. @daumayr if you don't have any further things, I'll merge it. |
@Richard-Roberts ah, perhaps interesting for you to have a brief look. This adds file support based on Newspeak. Might be useful for Grace. |
@smarr you can merge it, the broken tests are already broken in dev, i'll take a look at that separately. |
Looks nice, thanks @daumayr. Early next year I hope to get our self-hosting Grace compiler running on SOMns, these changes will be useful for getting that going! |
Adds support for file access similar to Newspeak, the major difference in functionality is the lack of streams.