Skip to content

Commit

Permalink
Support Object#instance_variables.
Browse files Browse the repository at this point in the history
  • Loading branch information
sisshiki1969 committed Nov 5, 2023
1 parent 3676288 commit 5ad5690
Showing 1 changed file with 36 additions and 16 deletions.
52 changes: 36 additions & 16 deletions monoruby/src/builtins/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub(super) fn init(globals: &mut Globals) {
globals.define_builtin_func(OBJECT_CLASS, "instance_variable_defined?", iv_defined);
globals.define_builtin_func(OBJECT_CLASS, "instance_variable_set", iv_set);
globals.define_builtin_func(OBJECT_CLASS, "instance_variable_get", iv_get);
globals.define_builtin_func(OBJECT_CLASS, "instance_variables", iv);
globals.define_builtin_func(OBJECT_CLASS, "to_s", to_s);
globals.define_builtin_func(OBJECT_CLASS, "method", method);
globals.define_builtin_func(OBJECT_CLASS, "system", system);
Expand Down Expand Up @@ -274,6 +275,22 @@ fn iv_get(_vm: &mut Executor, globals: &mut Globals, lfp: LFP, arg: Arg) -> Resu
Ok(v)
}

///
/// ### Object#instance_variables
///
/// - instance_variables -> [Symbol]
///
/// [https://docs.ruby-lang.org/ja/latest/method/Object/i/instance_variables.html]
#[monoruby_builtin]
fn iv(_vm: &mut Executor, globals: &mut Globals, lfp: LFP, _: Arg) -> Result<Value> {
MonorubyErr::check_number_of_arguments(lfp.arg_len(), 0)?;
let iter = globals
.get_ivars(lfp.self_val())
.into_iter()
.map(|(id, _)| Value::symbol(id));
Ok(Value::array_from_iter(iter))
}

fn prepare_command_arg(input: &str) -> (String, Vec<String>) {
let mut args = vec![];
let include_meta = input.contains([
Expand Down Expand Up @@ -583,27 +600,30 @@ mod test {
run_test2(
r#"a=Object.new; a.instance_variable_set("@i", 42); a.instance_variable_defined?(:@i)"#,
);
run_test2(
r#"a=Object.new; a.instance_variable_set("@i", 42); a.instance_variable_defined?(:@j)"#,
);
run_test2(
r#"a=Object.new; a.instance_variable_set("@i", 42); a.instance_variable_get(:@i)"#,
);
// TDDO: Object#instace_variables must return Array of Symbols,
// ordered by the time of definition of the instance variables.
run_test2(
r#"
20000.times do
a=Object.new;
a.instance_variable_set("@a", 0);
a.instance_variable_set("@b", 1);
a.instance_variable_set("@c", 2);
a.instance_variable_set("@d", 3);
a.instance_variable_set("@e", 4);
a.instance_variable_set("@f", 5);
a.instance_variable_set("@g", 6);
a.instance_variable_set("@h", 7);
a.instance_variable_set("@i", 8);
a.instance_variable_set("@j", 9);
a.instance_variable_set("@k", 10);
a.instance_variable_set("@l", 11);
a.inspect;
end
a=Object.new;
a.instance_variable_set("@b", 1);
a.instance_variable_set("@e", 4);
a.instance_variable_set("@c", 2);
a.instance_variable_set("@j", 9);
a.instance_variable_set("@d", 3);
a.instance_variable_set("@a", 0);
a.instance_variable_set("@g", 6);
a.instance_variable_set("@l", 11);
a.instance_variable_set("@h", 7);
a.instance_variable_set("@i", 8);
a.instance_variable_set("@f", 5);
a.instance_variable_set("@k", 10);
a.instance_variables.sort
"#,
);
}
Expand Down

0 comments on commit 5ad5690

Please sign in to comment.