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

Implement remaining File methods. #177

Merged
merged 2 commits into from
Mar 31, 2018

Conversation

faultyserver
Copy link
Member

This PR implements a majority of the File API exposed by Crystal. To do this cleanly, it also introduces a new NativeLib.passthrough macro that should help write native bindings more easily and consistently.

Some methods have issues with this process that lead to them needing special treatment. They are marked with TODO comments describing the bugs.

An overview of what's been added:

  • each_line(filename, encoding = nil, invalid = nil, chomp = true)
  • read_lines(filename, encoding = nil, invalid = nil, chomp = true) : Array(String) (implemented as .lines)
  • join(*parts) : String
  • read(filename, encoding = nil, invalid = nil) : String
  • write(filename, content, perm = DEFAULT_CREATE_MODE, encoding = nil, invalid = nil)
  • basename(path) : String
  • chmod(path, mode : Int)
  • chown(path, uid : Int? = -1, gid : Int = -1, follow_symlinks = false)
  • delete(path)
  • directory?(path) : Bool`
  • dirname(path) : String
  • empty?(path) : Bool`
  • executable?(path) : Bool`
  • exists?(path) : Bool`
  • expand_path(path, dir = nil) : String
  • extname(filename) : String
  • file?(path) : Bool`
  • link(old_path, new_path)
  • readable?(path) : Bool`
  • real_path(path) : String
  • rename(old_filename, new_filename)
  • size(filename) : UInt64
  • symlink(old_path, new_path)
  • symlink?(path) : Bool`
  • touch(filename : String, time : Time = Time.now)
  • writable?(path) : Bool`

All of these are static methods on File. Notably left out of these additions are the stat methods, as they return a Stat object and would need to be transformed into some other Myst object before being returned to the caller. This may be done in the future, but is not needed as of now.

I'll also admit that the testing for these methods is lacking. Most of this is because the methods are direct passthroughs and thus testing them would be repeating the testing done by Crystal itself. However, adding tests for edge cases and argument validity would still be beneficial.

Crystal's `File` provides a lot of functionality that can be directly exposed in Myst. To make that nicer to work with, this commit also adds a `NativeLib.passthrough` macro that takes a "method definition" and automatically wraps it in a method that matches what the Myst interpreter expects for native functions.
@faultyserver faultyserver added this to the Next milestone Mar 30, 2018
@faultyserver faultyserver merged commit fd26570 into myst-lang:master Mar 31, 2018
@faultyserver faultyserver deleted the nativelib/file branch March 31, 2018 15:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant