Created
September 12, 2023 15:08
-
-
Save gr33n7007h/ab3fe071a11d7a6817c3b508bad64d11 to your computer and use it in GitHub Desktop.
Enable FrozenCore monkey-patching bytecode
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
abort 'MJIT needs to be enabled' unless RubyVM::MJIT.enabled? | |
# make private constants available at top-level | |
# | |
constants = | |
Symbol.all_symbols.select do | |
RubyVM::MJIT.private_constant _1 rescue next | |
end | |
constants.each { Object.const_set _1, RubyVM::MJIT.const_get(_1) } | |
# dummy method to append iseq | |
# | |
def dummy = nil | |
# find the instructions we need | |
# | |
instructions = INSNS.values.select { _1.name =~ /\A(putspecialobject|leave)/ } | |
# get bytecode object | |
# | |
iseq = RubyVM::InstructionSequence.of method(:dummy) | |
# get pointer for iseq | |
# | |
iseq_ptr = C.rb_iseqw_to_iseq iseq | |
# build the method | |
# | |
iseq_ptr.body.iseq_encoded[0] = C.rb_vm_insn_encode instructions[0].bin | |
iseq_ptr.body.iseq_encoded[1] = 1 # FrozenCore enum type | |
iseq_ptr.body.iseq_encoded[2] = C.rb_vm_insn_encode instructions[1].bin | |
FC = dummy | |
p FC.singleton_methods | |
__END__ | |
=> [:"core#set_variable_alias", | |
:"core#undef_method", | |
:make_shareable, | |
:make_shareable_copy, | |
:"core#set_postexe", | |
:"core#hash_merge_ptr", | |
:"core#hash_merge_kwd", | |
:"core#raise", | |
:"core#sprintf", | |
:"core#set_method_alias", | |
:ensure_shareable, | |
:proc, | |
:lambda] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment