Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,15 @@ block keyword params, begin-less rescue/ensure, and `!~` — on top of the
pre-existing core (full object model, metaprogramming, Fiber/Thread, Marshal,
regexp, the scientific bindings, and the js/wasm target) detailed below.

Ruby **4.0**'s implicit-parameter API is implemented:
`Binding#implicit_parameters`, `#implicit_parameter_get` and
`#implicit_parameter_defined?` read a block's `it` or `_1`..`_N` — the current
scope's only, never a parent's or a nested one's, which is the single flag MRI
distinguishes them by (`proc.c` v4.0.9:688). An implicit parameter is not a
local variable, so these are the way to reach one. `Thread#raise` takes 4.0's
`cause:` keyword too, chaining from the **caller's** `$!` rather than the
target thread's.

**Performance:** a 6-runtime comparative suite ([BENCHMARKS.md](BENCHMARKS.md))
pits rbgo and rbgo+AOT against MRI, MRI+YJIT, JRuby and TruffleRuby. The rbgo
interpreter runs ~3–6× MRI on compute and at parity on I/O-bound work;
Expand Down
25 changes: 23 additions & 2 deletions internal/bytecode/bytecode.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,8 +360,29 @@ type ISeq struct {
BlockSlot int // slot of the &block param, or -1
NumLocals int // total local slots (params first, then assigns)
Locals []string // local-variable names by slot (for Binding); "" for anonymous slots
Children []*ISeq // nested ISeqs (method bodies / class bodies defined here)
Super string // for a class body: the superclass name ("" → Object)

// ImplicitParams names the slots holding a block's IMPLICIT parameters —
// `it`, or `_1`..`_N` — by slot, "" elsewhere. It is a SECOND channel beside
// Locals because the two answer different questions and 4.0 separated them:
// an implicit parameter is not a local variable (it is absent from
// Binding#local_variables and unreachable by #local_variable_get) and yet it
// has a name and a value, which Binding#implicit_parameters,
// #implicit_parameter_get and #implicit_parameter_defined? return.
//
// MRI keeps them in the same local table under IDs the program cannot spell:
// `it` under idItImplicit rather than idIt, and the numbered parameters under
// IDs rb_numparam_id_p recognises (proc.c v4.0.9:680-783). rbgo already
// blanked Locals for `it` to the same end; this records what was blanked
// instead of discarding it.
//
// It is DENSE and starts at slot 0: a block whose parameters the parser
// synthesised has no anonymous slot among them, so there is no gap to
// represent. Empty or nil for every other scope, and a slot past its end is
// an ordinary local.
ImplicitParams []string

Children []*ISeq // nested ISeqs (method bodies / class bodies defined here)
Super string // for a class body: the superclass name ("" → Object)

// Lines is the source map: which source line each instruction came from,
// COMPRESSED to one entry per line CHANGE and kept sorted by PC.
Expand Down
65 changes: 48 additions & 17 deletions internal/compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,20 @@ type builder struct {
curLine int
lastLine int
firstLine int

// implicitParams records, by slot, the name of a block's IMPLICIT parameter
// — `it` or `_1`..`_N` — and "" elsewhere. See bytecode.ISeq.ImplicitParams:
// it keeps the names the public local table must not carry, instead of
// discarding them as blanking alone did.
implicitParams []string
}

// markImplicitParam records slot i as holding the implicit parameter `name`.
func (b *builder) markImplicitParam(i int, name string) {
for len(b.implicitParams) <= i {
b.implicitParams = append(b.implicitParams, "")
}
b.implicitParams[i] = name
}

func newBuilder(name string, params []string) *builder {
Expand Down Expand Up @@ -232,23 +246,24 @@ func (b *builder) localLookup(name string) (int, bool) {

func (b *builder) build() *bytecode.ISeq {
return &bytecode.ISeq{
Name: b.name,
Insns: b.insns,
Consts: b.consts,
Names: b.names,
Params: b.params,
NumRequired: b.numRequired,
SplatIndex: b.splatIndex,
PostCount: b.postCount,
KwNames: b.kwNames,
KwRequired: b.kwRequired,
KwRestSlot: b.kwRestSlot,
BlockSlot: b.blockSlot,
NumLocals: len(b.locals),
Locals: b.locals,
Children: b.children,
Lines: b.lines,
FirstLine: b.firstLine,
Name: b.name,
Insns: b.insns,
Consts: b.consts,
Names: b.names,
Params: b.params,
NumRequired: b.numRequired,
SplatIndex: b.splatIndex,
PostCount: b.postCount,
KwNames: b.kwNames,
KwRequired: b.kwRequired,
KwRestSlot: b.kwRestSlot,
BlockSlot: b.blockSlot,
NumLocals: len(b.locals),
Locals: b.locals,
ImplicitParams: b.implicitParams,
Children: b.children,
Lines: b.lines,
FirstLine: b.firstLine,
}
}

Expand Down Expand Up @@ -2193,11 +2208,27 @@ func blockParamsAreImplicit(blk *ast.Block) bool {
// under `ruby_version_is ""..."4.0"` and pins the hiding only from 4.0. Hiding
// them here would be answering a question rbgo has not yet said it is answering;
// it belongs with the RUBY_VERSION bump.
//
// ⛔ BOTH KINDS ARE RECORDED, hidden or not. Ruby 4.0 adds
// Binding#implicit_parameters, #implicit_parameter_get and
// #implicit_parameter_defined? (proc.c v4.0.9:680-783), which answer for `it`
// AND for `_1`..`_N`. That is the other half of taking `_1` out of
// #local_variables: the implicit parameters do not disappear, they move to an
// API of their own. Blanking alone threw the name away and left nothing for the
// new methods to read, so the name goes into ImplicitParams first — a channel
// the public local table does not expose and the version bump does not have to
// invent.
func hideImplicitBlockParams(b *builder, blk *ast.Block, positionals []string) {
if !blockParamsAreImplicit(blk) {
return
}
// ⛔ EVERY positional here is implicit, so the list is DENSE and recorded
// without a guard. A `name == ""` arm looked prudent and was unreachable:
// blockParamsAreImplicit already means the parser synthesised the whole
// parameter list, so there is no anonymous slot among them. The coverage
// gate named it, which is the right way to find out.
for i, name := range positionals {
b.markImplicitParam(i, name)
if name != "it" {
continue
}
Expand Down
112 changes: 112 additions & 0 deletions internal/vm/binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,18 @@ type Binding struct {
// fresh block-less scope, it continues the captured one.
block *Proc

// implicit maps an IMPLICIT parameter's name — `it`, or `_1`..`_N` — to its
// slot in env, the binding's own (depth 0) scope. Only depth 0: MRI reads
// these with get_local_variable_ptr(&env, lid, FALSE), where the final
// argument is "search the parent scopes" and is TRUE for every local-variable
// method and FALSE for every implicit-parameter one (proc.c v4.0.9:688, 734,
// 782). That single flag is the whole of "ignores a numbered parameter
// defined in a parent scope", which ruby/spec pins four times over.
//
// nil for a scope with none, which is every scope that is not a block whose
// parameters the parser synthesised.
implicit map[string]int

// method is the __method__/__callee__ pair of the frame the binding was
// captured in, so eval'd code reports the enclosing method. MRI reaches the
// same answer from the binding's own frame (rb_vm_frame_method_entry on the
Expand Down Expand Up @@ -267,6 +279,9 @@ func (vm *VM) registerBinding() {
names: append([]string(nil), b.names...),
locs: append([]bindLoc(nil), b.locs...),
added: append([]string(nil), b.added...),
// Shared by value, not by reference: nothing mutates the map after
// capture, but a copy keeps that true if anything ever does.
implicit: copyImplicit(b.implicit),
}
}
cBinding.define("dup", bindingDup)
Expand Down Expand Up @@ -305,6 +320,103 @@ func (vm *VM) registerBinding() {
cBinding.define("local_variable_defined?", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value {
return object.Bool(self.(*Binding).slotOf(vm.bindingVarName(args[0])) >= 0)
})

// Ruby 4.0's implicit-parameter trio (proc.c v4.0.9:680-783). They are the
// other half of taking `_1` out of #local_variables: the implicit parameters
// do not vanish, they move to an API of their own.
//
// bind_implicit_parameters answers [:it] when `it` is in scope and the
// numbered parameters otherwise — never a mixture, because a block cannot
// have both.
cBinding.define("implicit_parameters", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value {
if len(args) != 0 {
raise("ArgumentError", "wrong number of arguments (given %d, expected 0)", len(args))
}
return self.(*Binding).implicitParameterNames()
})
cBinding.define("implicit_parameter_get", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value {
if len(args) != 1 {
raise("ArgumentError", "wrong number of arguments (given %d, expected 1)", len(args))
}
b := self.(*Binding)
name := vm.implicitParamName(args[0])
slot, ok := b.implicit[name]
if !ok {
raise("NameError", "implicit parameter '%s' is not defined for %s", name, b.ToS())
}
return b.env.slots[slot]
})
cBinding.define("implicit_parameter_defined?", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value {
if len(args) != 1 {
raise("ArgumentError", "wrong number of arguments (given %d, expected 1)", len(args))
}
b := self.(*Binding)
_, ok := b.implicit[vm.implicitParamName(args[0])]
return object.Bool(ok)
})
}

// implicitParamName coerces the argument of #implicit_parameter_get /
// #implicit_parameter_defined? and REFUSES a name that is not an implicit
// parameter's at all.
//
// ⛔ THE TWO REFUSALS ARE DIFFERENT AND BOTH ARE PINNED. MRI checks the shape of
// the NAME before it looks for a value (proc.c v4.0.9:726, 775):
//
// :a -> NameError "'a' is not an implicit parameter" — never could be
// :_1 -> NameError "implicit parameter '_1' is not defined for …" — could be, is not
//
// Collapsing them would pass "not defined" for a name no scope could ever hold.
// The TypeError for a non-String/Symbol is bindingVarName's, which is already
// MRI's message.
func (vm *VM) implicitParamName(v object.Value) string {
name := vm.bindingVarName(v)
if !isImplicitParamName(name) {
raise("NameError", "'%s' is not an implicit parameter", name)
}
return name
}

func copyImplicit(m map[string]int) map[string]int {
if m == nil {
return nil
}
out := make(map[string]int, len(m))
for k, v := range m {
out[k] = v
}
return out
}

// isImplicitParamName is MRI's rb_implicit_param_p: `it`, or `_` followed by a
// single digit 1..9. `_0` and `_10` are ordinary locals.
func isImplicitParamName(name string) bool {
if name == "it" {
return true
}
return len(name) == 2 && name[0] == '_' && name[1] >= '1' && name[1] <= '9'
}

// implicitParameterNames is bind_implicit_parameters (proc.c v4.0.9:680): [:it]
// when `it` is this scope's implicit parameter, otherwise the numbered ones in
// order. A block has one kind or the other and never both — `proc { |x| _1 }` is
// a syntax error — so the `it` arm returns before the numbered ones are looked
// at, as MRI's does.
func (b *Binding) implicitParameterNames() object.Value {
if _, ok := b.implicit["it"]; ok {
return object.NewArray(object.Symbol("it"))
}
var elems []object.Value
// Ascending by name, which for `_1`..`_9` is ascending by slot too. MRI
// builds the list from the local table in order for the same reason.
for d := byte('1'); d <= '9'; d++ {
n := string([]byte{'_', d})
if _, ok := b.implicit[n]; !ok {
break
}
elems = append(elems, object.Symbol(n))
}
return object.NewArrayFromSlice(elems)
}

// localVariableNames is the Symbol array Binding#local_variables and
Expand Down
Loading
Loading