Skip to content

Commit

Permalink
Merge pull request #2423 from natalie-lang/file-type
Browse files Browse the repository at this point in the history
Add actual File object type
  • Loading branch information
seven1m authored Dec 26, 2024
2 parents 4a5309e + 68b0ccc commit 38e0d7f
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 4 deletions.
5 changes: 4 additions & 1 deletion include/natalie/file_object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ namespace Natalie {
class FileObject : public IoObject {
public:
FileObject()
: IoObject { GlobalEnv::the()->Object()->const_fetch("File"_s)->as_class() } { }
: FileObject { GlobalEnv::the()->Object()->const_fetch("File"_s)->as_class() } { }

FileObject(ClassObject *klass)
: IoObject { Object::Type::File, klass } { }

Value initialize(Env *, Args &&, Block *);

Expand Down
3 changes: 3 additions & 0 deletions include/natalie/io_object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ class IoObject : public Object {
IoObject(ClassObject *klass)
: Object { Object::Type::Io, klass } { }

IoObject(Type type, ClassObject *klass)
: Object { type, klass } { }

IoObject(int fileno)
: Object { Object::Type::Io, GlobalEnv::the()->Object()->const_fetch("IO"_s)->as_class() }
, m_sync { fileno == STDERR_FILENO } {
Expand Down
3 changes: 2 additions & 1 deletion include/natalie/object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ class Object : public Cell {
bool is_float() const { return m_type == Type::Float; }
bool is_hash() const { return m_type == Type::Hash; }
bool is_integer() const { return m_type == Type::Integer; }
bool is_io() const { return m_type == Type::Io; }
bool is_io() const { return m_type == Type::Io || m_type == Type::File; }
bool is_file() const { return m_type == Type::File; }
bool is_file_stat() const { return m_type == Type::FileStat; }
bool is_match_data() const { return m_type == Type::MatchData; }
bool is_proc() const { return m_type == Type::Proc; }
Expand Down
1 change: 1 addition & 0 deletions include/natalie/object_type.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ enum class ObjectType {
Exception,
False,
Fiber,
File,
FileStat,
Float,
Hash,
Expand Down
2 changes: 1 addition & 1 deletion src/natalie.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ Env *build_top_env() {
IO->include_once(env, Enumerable);
IoObject::build_constants(env, IO);

ClassObject *File = IO->subclass(env, "File");
ClassObject *File = IO->subclass(env, "File", Object::Type::File);
Object->const_set("File"_s, File);
File->include_once(env, Enumerable);

Expand Down
6 changes: 5 additions & 1 deletion src/object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ Value Object::create(Env *env, ClassObject *klass) {
obj = new IoObject { klass };
break;

case Object::Type::File:
obj = new FileObject { klass };
break;

case Object::Type::MatchData:
obj = new MatchDataObject { klass };
break;
Expand Down Expand Up @@ -355,7 +359,7 @@ const IoObject *Object::as_io() const {
}

FileObject *Object::as_file() {
assert(is_io());
assert(is_file());
return static_cast<FileObject *>(this);
}

Expand Down

0 comments on commit 38e0d7f

Please sign in to comment.