Skip to content

Commit

Permalink
Merge pull request #2420 from natalie-lang/no-more-ub-static-cast
Browse files Browse the repository at this point in the history
  • Loading branch information
seven1m authored Dec 24, 2024
2 parents ef0f16f + ed147b4 commit bc6d69c
Show file tree
Hide file tree
Showing 9 changed files with 20 additions and 96 deletions.
1 change: 0 additions & 1 deletion include/natalie.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@
#include "natalie/native_profiler.hpp"
#include "natalie/nil_object.hpp"
#include "natalie/object.hpp"
#include "natalie/parser_object.hpp"
#include "natalie/proc_object.hpp"
#include "natalie/process_module.hpp"
#include "natalie/random_object.hpp"
Expand Down
3 changes: 3 additions & 0 deletions include/natalie/env_object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ namespace Natalie {

class EnvObject : public Object {
public:
EnvObject()
: Object { Object::Type::Env, GlobalEnv::the()->Object() } { }

Value assoc(Env *, Value name);
Value clear(Env *);
Value clone(Env *);
Expand Down
6 changes: 3 additions & 3 deletions include/natalie/object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ class Object : public Cell {
bool is_complex() const { return m_type == Type::Complex; }
bool is_dir() const { return m_type == Type::Dir; }
bool is_encoding() const { return m_type == Type::Encoding; }
bool is_env() const { return m_type == Type::Env; }
bool is_exception() const { return m_type == Type::Exception; }
bool is_float() const { return m_type == Type::Float; }
bool is_hash() const { return m_type == Type::Hash; }
Expand Down Expand Up @@ -165,6 +166,8 @@ class Object : public Cell {
const DirObject *as_dir() const;
EncodingObject *as_encoding();
const EncodingObject *as_encoding() const;
EnvObject *as_env();
const EnvObject *as_env() const;
ExceptionObject *as_exception();
const ExceptionObject *as_exception() const;
FalseObject *as_false();
Expand Down Expand Up @@ -232,9 +235,6 @@ class Object : public Cell {
MatchDataObject *as_match_data_or_raise(Env *);
StringObject *as_string_or_raise(Env *);

EnvObject *as_env_object_for_method_binding();
ParserObject *as_parser_object_for_method_binding();

SymbolObject *to_symbol(Env *, Conversion);
SymbolObject *to_instance_variable_name(Env *);

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 @@ -12,6 +12,7 @@ enum class ObjectType {
Encoding,
Enumerator,
EnumeratorArithmeticSequence,
Env,
Exception,
False,
Fiber,
Expand Down
18 changes: 0 additions & 18 deletions include/natalie/parser_object.hpp

This file was deleted.

14 changes: 1 addition & 13 deletions lib/natalie/compiler/binding_gen.rb
Original file line number Diff line number Diff line change
Expand Up @@ -336,23 +336,11 @@ def klass_arg
'klass->as_class()' if pass_klass
end

SPECIAL_CLASSES_WITHOUT_DEDICATED_TYPES = %w[KernelModule]

def special_case_class?
SPECIAL_CLASSES_WITHOUT_DEDICATED_TYPES.include?(cpp_class)
end

def as_type(value)
if cpp_class == 'Object'
value
elsif special_case_class?
raise 'should not reach here'
elsif %w[EnvObject ParserObject].include?(cpp_class)
# TODO
underscored = cpp_class.gsub(/([a-z])([A-Z])/, '\1_\2').downcase
"#{value}->as_#{underscored}_for_method_binding()"
else
underscored = cpp_class.sub(/Object|Object/, '').gsub(/([a-z])([A-Z])/, '\1_\2').gsub('::', '_').downcase
underscored = cpp_class.sub(/Object/, '').gsub(/([a-z])([A-Z])/, '\1_\2').gsub('::', '_').downcase
"#{value}->as_#{underscored}()"
end
end
Expand Down
2 changes: 1 addition & 1 deletion src/natalie.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ Env *build_top_env() {
GlobalEnv::the()->global_set_read_hook(env, "$+"_s, true, GlobalVariableAccessHooks::ReadHooks::last_match_last_group);
GlobalEnv::the()->global_set_read_hook(env, "$&"_s, true, GlobalVariableAccessHooks::ReadHooks::last_match_to_s);

Value ENV = new Natalie::Object {};
Value ENV = new Natalie::EnvObject {};
Object->const_set("ENV"_s, ENV);
ENV->extend_once(env, Enumerable);

Expand Down
19 changes: 11 additions & 8 deletions src/object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ Value Object::create(Env *env, ClassObject *klass) {

case Object::Type::Binding:
case Object::Type::Encoding:
case Object::Type::Env:
case Object::Type::False:
case Object::Type::Float:
case Object::Type::Integer:
Expand Down Expand Up @@ -273,6 +274,16 @@ const EncodingObject *Object::as_encoding() const {
return static_cast<const EncodingObject *>(this);
}

EnvObject *Object::as_env() {
assert(is_env());
return static_cast<EnvObject *>(this);
}

const EnvObject *Object::as_env() const {
assert(is_env());
return static_cast<const EnvObject *>(this);
}

ExceptionObject *Object::as_exception() {
assert(is_exception());
return static_cast<ExceptionObject *>(this);
Expand Down Expand Up @@ -523,14 +534,6 @@ const VoidPObject *Object::as_void_p() const {
return static_cast<const VoidPObject *>(this);
}

EnvObject *Object::as_env_object_for_method_binding() {
return static_cast<EnvObject *>(this);
}

ParserObject *Object::as_parser_object_for_method_binding() {
return static_cast<ParserObject *>(this);
}

ArrayObject *Object::as_array_or_raise(Env *env) {
if (!is_array())
env->raise("TypeError", "{} can't be coerced into Array", m_klass->inspect_str());
Expand Down
52 changes: 0 additions & 52 deletions src/parser_object.cpp

This file was deleted.

0 comments on commit bc6d69c

Please sign in to comment.