Skip to content
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

Merged
merged 1 commit into from
Nov 6, 2017
Merged

Add support for Newspeak's Files #190

merged 1 commit into from
Nov 6, 2017

Conversation

daumayr
Copy link
Contributor

@daumayr daumayr commented Oct 11, 2017

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).
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.

@smarr
Copy link
Owner

smarr commented Oct 11, 2017

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 buffer? An integer array?
Any way to read stuff as a string?

Are you already using it for something?
Or do you plan to use it for something specific?

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.

@daumayr
Copy link
Contributor Author

daumayr commented Oct 11, 2017

The buffer is a SOMns Array (long), reading stuff as a string is currently not supported, for that streams would be necessary.
Currently it is used in the Tests only, when expanding replay to deal with external communication I am going to make a benchmark that uses it.

I think it should be possible to pick up test automatically, streams would help

Copy link
Owner

@smarr smarr left a 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

@@ -0,0 +1,29 @@
#!/bin/bash
Copy link
Owner

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?

Copy link
Contributor Author

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()});
}
/*
Copy link
Owner

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;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No dead code please

@@ -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.
Copy link
Owner

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)

Copy link
Contributor Author

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.

Copy link
Owner

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.

public simpleName ^ <String> = (
^elements last name
)
(*
Copy link
Owner

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)

Copy link
Contributor Author

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)

Copy link
Owner

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?

Copy link
Contributor Author

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 {
Copy link
Owner

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() {
Copy link
Owner

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"});
Copy link
Owner

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
Copy link
Owner

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();
Copy link
Owner

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

Repository owner deleted a comment Oct 17, 2017
Repository owner deleted a comment Oct 17, 2017
@smarr smarr changed the title Adapt Newspeak Files Add support for Newspeak's Files Nov 6, 2017
@smarr smarr force-pushed the FileIO branch 2 times, most recently from e555f58 to c7fa31f Compare November 6, 2017 18:28
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
@smarr
Copy link
Owner

smarr commented Nov 6, 2017

Ok, I did some more polishing, moving test files a bit around, fix formatting a little. Nothing much of consequence.
I squashed the changes, too, since the history didn't reflect anything useful.

@daumayr if you don't have any further things, I'll merge it.

@smarr
Copy link
Owner

smarr commented Nov 6, 2017

@Richard-Roberts ah, perhaps interesting for you to have a brief look. This adds file support based on Newspeak. Might be useful for Grace.

@daumayr
Copy link
Contributor Author

daumayr commented Nov 6, 2017

@smarr you can merge it, the broken tests are already broken in dev, i'll take a look at that separately.

@ghost
Copy link

ghost commented Nov 6, 2017

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!

@smarr smarr added the enhancement Improves the implementation with something noteworthy label Nov 6, 2017
@smarr smarr added this to the v0.6.0 - Black Diamonds milestone Nov 6, 2017
@smarr smarr merged commit bbd0841 into smarr:dev Nov 6, 2017
@smarr smarr deleted the FileIO branch November 6, 2017 22:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Improves the implementation with something noteworthy
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants