Skip to content

Commit

Permalink
Use Optional for Module methods
Browse files Browse the repository at this point in the history
  • Loading branch information
seven1m committed Feb 21, 2025
1 parent 38bec76 commit 6c1350f
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 55 deletions.
8 changes: 4 additions & 4 deletions include/natalie/kernel_module.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,11 @@ class KernelModule {
static bool is_frozen(Value self) { return self.is_frozen(); }
static Value loop(Env *env, Value self, Block *block);
static Value method(Env *env, Value self, Value name);
static Value methods(Env *env, Value self, Value regular_val);
static Value methods(Env *env, Value self, Optional<Value> regular_val);
static bool neqtilde(Env *env, Value self, Value other);
static Value private_methods(Env *env, Value self, Value recur = nullptr);
static Value protected_methods(Env *env, Value self, Value recur = nullptr);
static Value public_methods(Env *env, Value self, Value recur = nullptr);
static Value private_methods(Env *env, Value self, Optional<Value> recur = {});
static Value protected_methods(Env *env, Value self, Optional<Value> recur = {});
static Value public_methods(Env *env, Value self, Optional<Value> recur = {});
static Value remove_instance_variable(Env *env, Value self, Value name_val);
static bool respond_to_missing(Env *, Value, Value, Value) { return false; }
static bool respond_to_method(Env *, Value, Value, Value);
Expand Down
20 changes: 10 additions & 10 deletions include/natalie/module_object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,15 @@ class ModuleObject : public Object {
Value const_find(Env *, SymbolObject *, ConstLookupSearchMode = ConstLookupSearchMode::Strict, ConstLookupFailureMode = ConstLookupFailureMode::ConstMissing);

Value const_get(SymbolObject *) const;
Value const_get(Env *, Value, Value = nullptr);
Value const_get(Env *, Value, Optional<Value> = {});
Value const_fetch(SymbolObject *) const;
Value const_set(SymbolObject *, Value);
Value const_set(SymbolObject *, MethodFnPtr, StringObject *);
Value const_set(Env *, Value, Value);

void remove_const(SymbolObject *);
Value remove_const(Env *, Value);
Value constants(Env *, Value) const;
Value constants(Env *, Optional<Value>) const;
Value const_missing(Env *, Value);

void make_method_alias(Env *, SymbolObject *, SymbolObject *);
Expand Down Expand Up @@ -101,10 +101,10 @@ class ModuleObject : public Object {
bool class_variable_defined(Env *, Value);
Value class_variable_get(Env *, Value);
Value class_variable_set(Env *, Value, Value);
ArrayObject *class_variables(Value = nullptr) const;
ArrayObject *class_variables(Optional<Value> = {}) const;
Value remove_class_variable(Env *, Value);

Value define_method(Env *, Value, Value, Block *);
Value define_method(Env *, Value, Optional<Value>, Block *);
SymbolObject *define_method(Env *, SymbolObject *, MethodFnPtr, int);
SymbolObject *define_method(Env *, SymbolObject *, Block *);
SymbolObject *undefine_method(Env *, SymbolObject *);
Expand All @@ -118,11 +118,11 @@ class ModuleObject : public Object {
Value instance_method(Env *, Value);
Value public_instance_method(Env *, Value);

Value instance_methods(Env *, Value, std::function<bool(MethodVisibility)>);
Value instance_methods(Env *, Value);
Value private_instance_methods(Env *, Value);
Value protected_instance_methods(Env *, Value);
Value public_instance_methods(Env *, Value);
Value instance_methods(Env *, Optional<Value>, std::function<bool(MethodVisibility)>);
Value instance_methods(Env *, Optional<Value>);
Value private_instance_methods(Env *, Optional<Value>);
Value protected_instance_methods(Env *, Optional<Value>);
Value public_instance_methods(Env *, Optional<Value>);

ArrayObject *ancestors(Env *);
bool ancestors_includes(Env *, ModuleObject *);
Expand Down Expand Up @@ -163,7 +163,7 @@ class ModuleObject : public Object {
Value private_constant(Env *, Args &&);
Value public_constant(Env *, Args &&);

bool const_defined(Env *, Value, Value = nullptr);
bool const_defined(Env *, Value, Optional<Value> = {});
Value alias_method(Env *, Value, Value);
Value remove_method(Env *, Args &&);
Value undef_method(Env *, Args &&);
Expand Down
26 changes: 13 additions & 13 deletions lib/natalie/compiler/binding_gen.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1035,12 +1035,12 @@ def generate_name
gen.static_binding_as_instance_method('Kernel', 'kind_of?', 'KernelModule', 'is_a', argc: 1, pass_env: true, pass_block: false, return_type: :bool)
gen.static_binding_as_instance_method('Kernel', 'loop', 'KernelModule', 'loop', argc: 0, pass_env: true, pass_block: true, return_type: :Object)
gen.static_binding_as_instance_method('Kernel', 'method', 'KernelModule', 'method', argc: 1, pass_env: true, pass_block: false, return_type: :Object)
gen.static_binding_as_instance_method('Kernel', 'methods', 'KernelModule', 'methods', argc: 0..1, pass_env: true, pass_block: false, return_type: :Object)
gen.static_binding_as_instance_method('Kernel', 'methods', 'KernelModule', 'methods', argc: 0..1, pass_env: true, pass_block: false, return_type: :Object, pass_null: false)
gen.static_binding_as_instance_method('Kernel', 'nil?', 'KernelModule', 'is_nil', argc: 0, pass_env: false, pass_block: false, return_type: :bool)
gen.static_binding_as_instance_method('Kernel', 'object_id', 'Object', 'object_id', argc: 0, pass_env: false, pass_block: false, return_type: :int)
gen.static_binding_as_instance_method('Kernel', 'private_methods', 'KernelModule', 'private_methods', argc: 0..1, pass_env: true, pass_block: false, return_type: :Object)
gen.static_binding_as_instance_method('Kernel', 'protected_methods', 'KernelModule', 'protected_methods', argc: 0..1, pass_env: true, pass_block: false, return_type: :Object)
gen.static_binding_as_instance_method('Kernel', 'public_methods', 'KernelModule', 'public_methods', argc: 0..1, pass_env: true, pass_block: false, return_type: :Object)
gen.static_binding_as_instance_method('Kernel', 'private_methods', 'KernelModule', 'private_methods', argc: 0..1, pass_env: true, pass_block: false, return_type: :Object, pass_null: false)
gen.static_binding_as_instance_method('Kernel', 'protected_methods', 'KernelModule', 'protected_methods', argc: 0..1, pass_env: true, pass_block: false, return_type: :Object, pass_null: false)
gen.static_binding_as_instance_method('Kernel', 'public_methods', 'KernelModule', 'public_methods', argc: 0..1, pass_env: true, pass_block: false, return_type: :Object, pass_null: false)
gen.static_binding_as_instance_method('Kernel', 'remove_instance_variable', 'KernelModule', 'remove_instance_variable', argc: 1, pass_env: true, pass_block: false, return_type: :Object)
gen.static_binding_as_instance_method('Kernel', 'singleton_class', 'Object', 'singleton_class', argc: 0, pass_env: true, pass_block: false, return_type: :Object)
gen.static_binding_as_instance_method('Kernel', 'tap', 'KernelModule', 'tap', argc: 0, pass_env: true, pass_block: true, return_type: :Object)
Expand Down Expand Up @@ -1102,20 +1102,20 @@ def generate_name
gen.binding('Module', 'class_variable_defined?', 'ModuleObject', 'class_variable_defined', argc: 1, pass_env: true, pass_block: false, return_type: :bool)
gen.binding('Module', 'class_variable_get', 'ModuleObject', 'class_variable_get', argc: 1, pass_env: true, pass_block: false, return_type: :Object)
gen.binding('Module', 'class_variable_set', 'ModuleObject', 'class_variable_set', argc: 2, pass_env: true, pass_block: false, return_type: :Object)
gen.binding('Module', 'class_variables', 'ModuleObject', 'class_variables', argc: 0..1, pass_env: false, pass_block: false, return_type: :Object)
gen.binding('Module', 'const_defined?', 'ModuleObject', 'const_defined', argc: 1..2, pass_env: true, pass_block: false, return_type: :bool)
gen.binding('Module', 'const_get', 'ModuleObject', 'const_get', argc: 1..2, pass_env: true, pass_block: false, return_type: :Object)
gen.binding('Module', 'class_variables', 'ModuleObject', 'class_variables', argc: 0..1, pass_env: false, pass_block: false, return_type: :Object, pass_null: false)
gen.binding('Module', 'const_defined?', 'ModuleObject', 'const_defined', argc: 1..2, pass_env: true, pass_block: false, return_type: :bool, pass_null: false)
gen.binding('Module', 'const_get', 'ModuleObject', 'const_get', argc: 1..2, pass_env: true, pass_block: false, return_type: :Object, pass_null: false)
gen.binding('Module', 'const_missing', 'ModuleObject', 'const_missing', argc: 1, pass_env: true, pass_block: false, return_type: :Object)
gen.binding('Module', 'const_set', 'ModuleObject', 'const_set', argc: 2, pass_env: true, pass_block: false, return_type: :Object)
gen.binding('Module', 'constants', 'ModuleObject', 'constants', argc: 0..1, pass_env: true, pass_block: false, return_type: :Object)
gen.binding('Module', 'define_method', 'ModuleObject', 'define_method', argc: 1..2, pass_env: true, pass_block: true, return_type: :Object)
gen.binding('Module', 'constants', 'ModuleObject', 'constants', argc: 0..1, pass_env: true, pass_block: false, return_type: :Object, pass_null: false)
gen.binding('Module', 'define_method', 'ModuleObject', 'define_method', argc: 1..2, pass_env: true, pass_block: true, return_type: :Object, pass_null: false)
gen.binding('Module', 'deprecate_constant', 'ModuleObject', 'deprecate_constant', argc: :any, pass_env: true, pass_block: false, return_type: :Object)
gen.binding('Module', 'extend_object', 'ModuleObject', 'extend_object', argc: 1, pass_env: true, pass_block: false, return_type: :Object, visibility: :private)
gen.binding('Module', 'include', 'ModuleObject', 'include', argc: 1.., pass_env: true, pass_block: false, return_type: :Object)
gen.binding('Module', 'include?', 'ModuleObject', 'does_include_module', argc: 1, pass_env: true, pass_block: false, return_type: :bool)
gen.binding('Module', 'included_modules', 'ModuleObject', 'included_modules', argc: 0, pass_env: true, pass_block: false, return_type: :Object)
gen.binding('Module', 'instance_method', 'ModuleObject', 'instance_method', argc: 1, pass_env: true, pass_block: false, return_type: :Object)
gen.binding('Module', 'instance_methods', 'ModuleObject', 'instance_methods', argc: 0..1, pass_env: true, pass_block: false, return_type: :Object)
gen.binding('Module', 'instance_methods', 'ModuleObject', 'instance_methods', argc: 0..1, pass_env: true, pass_block: false, return_type: :Object, pass_null: false)
gen.binding('Module', 'method_defined?', 'ModuleObject', 'is_method_defined', argc: 1, pass_env: true, pass_block: false, return_type: :bool)
gen.binding('Module', 'module_eval', 'ModuleObject', 'module_eval', argc: 0, pass_env: true, pass_block: true, aliases: ['class_eval'], return_type: :Object)
gen.binding('Module', 'module_exec', 'ModuleObject', 'module_exec', argc: :any, pass_env: true, pass_block: true, aliases: ['class_exec'], return_type: :Object)
Expand All @@ -1125,13 +1125,13 @@ def generate_name
gen.binding('Module', 'private', 'ModuleObject', 'private_method', argc: :any, pass_env: true, pass_block: false, return_type: :Object)
gen.binding('Module', 'private_class_method', 'ModuleObject', 'private_class_method', argc: :any, pass_env: true, pass_block: false, return_type: :Object)
gen.binding('Module', 'private_constant', 'ModuleObject', 'private_constant', argc: :any, pass_env: true, pass_block: false, return_type: :Object)
gen.binding('Module', 'private_instance_methods', 'ModuleObject', 'private_instance_methods', argc: 0..1, pass_env: true, pass_block: false, return_type: :Object)
gen.binding('Module', 'private_instance_methods', 'ModuleObject', 'private_instance_methods', argc: 0..1, pass_env: true, pass_block: false, return_type: :Object, pass_null: false)
gen.binding('Module', 'protected', 'ModuleObject', 'protected_method', argc: :any, pass_env: true, pass_block: false, return_type: :Object)
gen.binding('Module', 'protected_instance_methods', 'ModuleObject', 'protected_instance_methods', argc: 0..1, pass_env: true, pass_block: false, return_type: :Object)
gen.binding('Module', 'protected_instance_methods', 'ModuleObject', 'protected_instance_methods', argc: 0..1, pass_env: true, pass_block: false, return_type: :Object, pass_null: false)
gen.binding('Module', 'public', 'ModuleObject', 'public_method', argc: :any, pass_env: true, pass_block: false, return_type: :Object)
gen.binding('Module', 'public_class_method', 'ModuleObject', 'public_class_method', argc: :any, pass_env: true, pass_block: false, return_type: :Object)
gen.binding('Module', 'public_constant', 'ModuleObject', 'public_constant', argc: :any, pass_env: true, pass_block: false, return_type: :Object)
gen.binding('Module', 'public_instance_methods', 'ModuleObject', 'public_instance_methods', argc: 0..1, pass_env: true, pass_block: false, return_type: :Object)
gen.binding('Module', 'public_instance_methods', 'ModuleObject', 'public_instance_methods', argc: 0..1, pass_env: true, pass_block: false, return_type: :Object, pass_null: false)
gen.binding('Module', 'public_instance_method', 'ModuleObject', 'public_instance_method', argc: 1, pass_env: true, pass_block: false, return_type: :Object)
gen.binding('Module', 'remove_class_variable', 'ModuleObject', 'remove_class_variable', argc: 1, pass_env: true, pass_block: false, return_type: :Object)
gen.binding('Module', 'remove_const', 'ModuleObject', 'remove_const', argc: 1, pass_env: true, pass_block: false, return_type: :Object, visibility: :private)
Expand Down
22 changes: 11 additions & 11 deletions src/kernel_module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1059,17 +1059,17 @@ Value KernelModule::method(Env *env, Value self, Value name) {
return new MethodObject { self, method_info.method() };
}

Value KernelModule::methods(Env *env, Value self, Value regular_val) {
bool regular = regular_val ? regular_val.is_truthy() : true;
Value KernelModule::methods(Env *env, Value self, Optional<Value> regular_val) {
bool regular = regular_val ? regular_val.value().is_truthy() : true;
if (regular) {
if (self->singleton_class()) {
return self->singleton_class()->instance_methods(env, TrueObject::the());
return self->singleton_class()->instance_methods(env, Value(TrueObject::the()));
} else {
return self.klass()->instance_methods(env, TrueObject::the());
return self.klass()->instance_methods(env, Value(TrueObject::the()));
}
}
if (self->singleton_class())
return self->singleton_class()->instance_methods(env, FalseObject::the());
return self->singleton_class()->instance_methods(env, Value(FalseObject::the()));
else
return new ArrayObject {};
}
Expand All @@ -1078,23 +1078,23 @@ bool KernelModule::neqtilde(Env *env, Value self, Value other) {
return self.send(env, "=~"_s, { other }).is_falsey();
}

Value KernelModule::private_methods(Env *env, Value self, Value recur) {
Value KernelModule::private_methods(Env *env, Value self, Optional<Value> recur) {
if (self->singleton_class())
return self->singleton_class()->private_instance_methods(env, TrueObject::the());
return self->singleton_class()->private_instance_methods(env, Value(TrueObject::the()));
else
return self.klass()->private_instance_methods(env, recur);
}

Value KernelModule::protected_methods(Env *env, Value self, Value recur) {
Value KernelModule::protected_methods(Env *env, Value self, Optional<Value> recur) {
if (self->singleton_class())
return self->singleton_class()->protected_instance_methods(env, TrueObject::the());
return self->singleton_class()->protected_instance_methods(env, Value(TrueObject::the()));
else
return self.klass()->protected_instance_methods(env, recur);
}

Value KernelModule::public_methods(Env *env, Value self, Value recur) {
Value KernelModule::public_methods(Env *env, Value self, Optional<Value> recur) {
if (self.singleton_class())
return self.singleton_class()->public_instance_methods(env, TrueObject::the());
return self.singleton_class()->public_instance_methods(env, Value(TrueObject::the()));
else
return self.klass()->public_instance_methods(env, recur);
}
Expand Down
Loading

0 comments on commit 6c1350f

Please sign in to comment.