-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit adds a new primitive function `Primitive.send_delegate!` which emits a send instruction based on the first two parameters. It forces the send instruction to be an FCALL, thereby passing visibility checks. This allows us to write `Class#new` in Ruby and have it work for both BasicObject and regular objects. Here are the instructions for `Class#new`: ``` $ ./miniruby -e'puts RubyVM::InstructionSequence.of(Class.method(:new)).disasm' == disasm: #<ISeq:new@<internal:class>:2 (2,2)-(6,5)> local table (size: 5, argc: 0 [opts: 0, rest: 0, post: 0, block: 2, kw: -1@-1, kwrest: 1]) [ 5] "*"@0<Rest>[ 4] "**"@1<Kwrest>[ 3] "&"@2<Block>[ 2] "..."@3 [ 1] obj@4 0000 opt_invokebuiltin_delegate <builtin!rb_class_alloc2/0>, 0( 3)[LiCa] 0003 setlocal_WC_0 obj@4 0005 getlocal_WC_0 obj@4 ( 4)[Li] 0007 getlocal_WC_0 "*"@0 0009 splatarray true 0011 getlocal_WC_0 "**"@1 0013 getblockparamproxy "&"@2, 0 0016 splatkw 0017 send <calldata!mid:initialize, argc:2, ARGS_SPLAT|ARGS_SPLAT_MUT|ARGS_BLOCKARG|FCALL|KW_SPLAT>, nil 0020 pop 0021 getlocal_WC_0 obj@4 ( 5)[Li] 0023 leave ( 6)[Re] ``` Co-authored-by: John Hawthorn <jhawthorn@github.com>
- Loading branch information
1 parent
a6dac9b
commit 76ee75d
Showing
8 changed files
with
98 additions
and
5 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class Class | ||
def new(...) | ||
obj = Primitive.rb_class_alloc2 | ||
Primitive.send_delegate!(obj, :initialize, ...) | ||
obj | ||
end | ||
end | ||
|
||
class BasicObject | ||
def initialize | ||
end | ||
end |
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
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
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
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
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
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
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