From 91b10c0b77c5408cbbbc2fc6d678ee92b2d4b28a Mon Sep 17 00:00:00 2001 From: Matt Valentine-House Date: Mon, 18 Sep 2023 21:50:33 +0100 Subject: [YARP] Implement ConstantTargetNode --- test/yarp/compiler_test.rb | 14 ++++++++++++++ yarp/yarp_compiler.c | 6 ++++++ 2 files changed, 20 insertions(+) diff --git a/test/yarp/compiler_test.rb b/test/yarp/compiler_test.rb index b8a636d984..c6b7fed1eb 100644 --- a/test/yarp/compiler_test.rb +++ b/test/yarp/compiler_test.rb @@ -103,6 +103,20 @@ module YARP test_yarp_eval("class YARP::CompilerTest; @@yct = 0; @@yct += 1; end") end + def test_ConstantTargetNode + # We don't call test_yarp_eval directly in this case becuase we + # don't want to assign the constant mutliple times if we run + # with `--repeat-count` + # Instead, we eval manually here, and remove the constant to + constant_names = ["YCT", "YCT2"] + source = "#{constant_names.join(",")} = 1" + yarp_eval = RubyVM::InstructionSequence.compile_yarp(source).eval + assert_equal yarp_eval, 1 + constant_names.map { |name| + Object.send(:remove_const, name) + } + end + def test_ConstantWriteNode # We don't call test_yarp_eval directly in this case becuase we # don't want to assign the constant mutliple times if we run diff --git a/yarp/yarp_compiler.c b/yarp/yarp_compiler.c index 400583130a..2a8ebe5403 100644 --- a/yarp/yarp_compiler.c +++ b/yarp/yarp_compiler.c @@ -994,6 +994,12 @@ yp_compile_node(rb_iseq_t *iseq, const yp_node_t *node, LINK_ANCHOR *const ret, return; } + case YP_CONSTANT_TARGET_NODE: { + yp_constant_target_node_t *constant_write_node = (yp_constant_target_node_t *) node; + ADD_INSN1(ret, &dummy_line_node, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE)); + ADD_INSN1(ret, &dummy_line_node, setconstant, ID2SYM(yp_constant_id_lookup(compile_context, constant_write_node->name))); + return; + } case YP_CONSTANT_WRITE_NODE: { yp_constant_write_node_t *constant_write_node = (yp_constant_write_node_t *) node; YP_COMPILE_NOT_POPPED(constant_write_node->value); -- cgit v1.2.3