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
65 changes: 65 additions & 0 deletions internal/vm/autoload.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,35 @@ func (vm *VM) autoloadPathFor(cls *RClass, name string, inherit bool) object.Val
return object.NilV
}

// autoloadVisible reports whether a pending autoload of name on c makes the
// constant answer "defined" to `defined?` and #const_defined?. It does NOT,
// while the registered feature is already loaded or is being loaded right now:
// MRI resolves an autoload entry through check_autoload_required, which calls
// rb_feature_provided(file, &loading) and treats an entry whose feature is
// loading as having no autoload left to run (ruby/ruby v3_4_0 variable.c
// check_autoload_required / autoload_defined_p / rb_autoload_at_p). That is
// what makes a file required DIRECTLY, while an autoload for it is registered,
// see its own constant as undefined:
//
// MZ.autoload :K, path
// require path # inside the file: defined?(MZ::K) is nil,
// # MZ.const_defined?(:K) is false, MZ.autoload?(:K) is nil,
// # but MZ.constants(false) still includes :K
//
// The constant list is deliberately NOT filtered this way — MRI keeps the
// reserved entry in Module#constants throughout — so only the two "is it
// defined?" questions consult this.
func (vm *VM) autoloadVisible(c *RClass, name string) bool {
if c == nil || c.autoloads == nil {
return false
}
path, ok := c.autoloads[name]
if !ok {
return false
}
return !vm.featureLoaded(path)
}

// registerAutoloadOn records (or replaces) a pending autoload for name on cls.
// When the constant is already defined in cls's own table the registration is
// dropped, matching MRI where autoload of a defined constant is inert. A fresh
Expand Down Expand Up @@ -172,9 +201,45 @@ func (vm *VM) tryAutoload(cls *RClass, name string) bool {
// Kernel#require is what runs — ruby/spec exercises exactly that.
vm.send(vm.main, "require", []object.Value{object.NewString(path)}, nil)
done = true
vm.warnAutoloadDidNotDefine(cls, name, path)
return true
}

// warnAutoloadDidNotDefine emits MRI's diagnostic for an autoload whose file
// completed without defining the constant in the module the autoload was
// registered on — the case where lookup then carries on into the enclosing
// lexical and ancestor scopes. ruby/ruby v3_4_0 variable.c autoload_try_load:
//
// // After we loaded the feature, if the constant is not defined, we remove
// // it completely:
// rb_const_entry_t *ce = rb_const_lookup(arguments->module, arguments->name);
// if (!ce || UNDEF_P(ce->value)) {
// result = Qfalse;
// rb_const_remove(arguments->module, arguments->name);
// if (arguments->module == rb_cObject) {
// rb_warning("Expected %"PRIsVALUE" to define %"PRIsVALUE" but it didn't", …);
// } else {
// rb_warning("Expected %"PRIsVALUE" to define %"PRIsVALUE"::%"PRIsVALUE" but it didn't", …);
// }
// }
//
// It goes through rb_warning, not rb_warn, so only $VERBOSE == true shows it —
// the same gate warnCircularRequire applies — and it is emitted at most once
// per registration because the caller has already retired the entry.
func (vm *VM) warnAutoloadDidNotDefine(cls *RClass, name, path string) {
if _, defined := cls.consts[name]; defined {
return
}
if v, ok := vm.globals["$VERBOSE"].(object.Bool); !ok || !bool(v) {
return
}
qualified := name
if cls != vm.cObject {
qualified = vm.moduleToSStr(cls) + "::" + name
}
vm.curStderr().writeStr("warning: Expected " + path + " to define " + qualified + " but it didn't\n")
}

// featureLoaded reports whether path names a file that has already been loaded,
// resolved the way require would resolve it. $LOADED_FEATURES is the authority
// (doRequire records each file there and featureDropped retires a cache entry
Expand Down
9 changes: 4 additions & 5 deletions internal/vm/defined.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (vm *VM) hasScopedConst(cls *RClass, name string) bool {
continue
}
}
if _, ok := c.autoloads[name]; ok {
if vm.autoloadVisible(c, name) {
return true
}
}
Expand All @@ -37,19 +37,18 @@ func (vm *VM) hasScopedConst(cls *RClass, name string) bool {
// require — used by defined? to report a registered-but-unloaded constant.
func (vm *VM) autoloadPending(cref *RClass, name string) bool {
for _, c := range vm.nesting(cref) {
if _, ok := c.autoloads[name]; ok {
if vm.autoloadVisible(c, name) {
return true
}
}
if cref != nil {
for _, c := range vm.ancestors(cref) {
if _, ok := c.autoloads[name]; ok {
if vm.autoloadVisible(c, name) {
return true
}
}
}
_, ok := vm.cObject.autoloads[name]
return ok
return vm.autoloadVisible(vm.cObject, name)
}

// gvarDefined reports whether a global variable is set. User globals live in
Expand Down
6 changes: 3 additions & 3 deletions internal/vm/module_residuals.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ func (vm *VM) constSegDefined(mod *RClass, name string, inherit, isFirst bool) (
if v, ok := mod.consts[name]; ok {
return v, true
}
return object.NilVal(), hasAutoload(mod, name)
return object.NilVal(), vm.autoloadVisible(mod, name)
}
if v, ok := vm.constInAncestors(mod, name); ok {
return v, true
Expand All @@ -266,7 +266,7 @@ func (vm *VM) constSegDefined(mod *RClass, name string, inherit, isFirst bool) (
if v, ok := vm.cObject.consts[name]; ok {
return v, true
}
if hasAutoload(vm.cObject, name) {
if vm.autoloadVisible(vm.cObject, name) {
return object.NilVal(), true
}
}
Expand Down Expand Up @@ -296,7 +296,7 @@ func (vm *VM) autoloadPendingInAncestors(mod *RClass, name string) bool {
if (c == vm.cObject || c == vm.cBasicObject) && mod != vm.cObject && mod != vm.cBasicObject {
continue
}
if hasAutoload(c, name) {
if vm.autoloadVisible(c, name) {
return true
}
}
Expand Down
53 changes: 49 additions & 4 deletions internal/vm/pack.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,20 +81,65 @@ func (vm *VM) registerPackUnpack() {
}
return object.NewStringBytesEnc(out, enc)
})
vm.cString.define("unpack", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value {
vm.cString.define("unpack", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value {
data := self.(*object.String).Bytes()
return object.NewArrayFromSlice(unpackElems(data, vm.packFormat(args)))
fmtStr := vm.packFormat(args)
return object.NewArrayFromSlice(unpackElems(data[vm.unpackOffset(args, len(data)):], fmtStr))
})
vm.cString.define("unpack1", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value {
vm.cString.define("unpack1", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value {
data := self.(*object.String).Bytes()
elems := unpackElems(data, vm.packFormat(args))
fmtStr := vm.packFormat(args)
elems := unpackElems(data[vm.unpackOffset(args, len(data)):], fmtStr)
if len(elems) == 0 {
return object.NilV
}
return elems[0]
})
}

// unpackOffset reads the `offset:` keyword of String#unpack / #unpack1 and
// returns the BYTE offset unpacking starts from, validating it the way MRI's
// pack_unpack_internal does (ruby/ruby v3_4_0 pack.c):
//
// if (offset < 0) rb_raise(rb_eArgError, "offset can't be negative");
// len = RSTRING_LEN(str);
// if (offset > len) rb_raise(rb_eArgError, "offset outside of string");
//
// The bound is EXCLUSIVE of the error only at offset == len: that is accepted
// and leaves nothing to unpack, so "a".unpack("C", offset: 1) is [nil] and
// "a".unpack1("C", offset: 1) is nil, while offset 2 raises. The offset counts
// BYTES, not characters, so "\u0608".unpack1("C", offset: 1) reads the second
// byte of the two-byte character.
//
// No keyword at all yields 0. Any keyword other than :offset is rejected the
// way rb_get_kwargs does.
func (vm *VM) unpackOffset(args []object.Value, byteLen int) int {
h, ok := trailingHash(args)
if !ok || len(args) < 2 {
return 0
}
off := object.Value(nil)
for _, k := range h.Keys {
sym, isSym := k.(object.Symbol)
if isSym && string(sym) == "offset" {
off, _ = h.Get(k)
continue
}
raise("ArgumentError", "unknown keyword: %s", k.Inspect())
}
if off == nil || object.IsNil(off) {
return 0
}
n := vm.toIntCoerce(off)
if n < 0 {
raise("ArgumentError", "offset can't be negative")
}
if n > int64(byteLen) {
raise("ArgumentError", "offset outside of string")
}
return int(n)
}

// packFormat extracts the mandatory format argument, which is a String or any
// object that responds to #to_str.
func (vm *VM) packFormat(args []object.Value) string {
Expand Down
3 changes: 3 additions & 0 deletions internal/vm/refinement.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ func (vm *VM) registerRefinements() {
return self.(*RClass).refinedClass
})

// Refinement#import_methods (see refinement_import.go).
vm.installRefinementImport()

// Module#refinements returns this module's own refinement modules (not those
// of included modules).
vm.cModule.define("refinements", func(_ *VM, self object.Value, _ []object.Value, _ *Proc) object.Value {
Expand Down
148 changes: 148 additions & 0 deletions internal/vm/refinement_import.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
// Copyright (c) the go-embedded-ruby/ruby authors
//
// SPDX-License-Identifier: BSD-3-Clause

package vm

import (
"sort"

"github.com/go-embedded-ruby/ruby/internal/object"
)

// Refinement#import_methods — copying Ruby-defined instance methods of one or
// more plain modules INTO a refinement, so they behave as if they had been
// written in the refine block itself.
//
// MRI (ruby/ruby v3_4_0 eval.c refinement_import_methods):
//
// rb_check_arity(argc, 1, UNLIMITED_ARGUMENTS);
// for (i = 0; i < argc; i++) {
// Check_Type(argv[i], T_MODULE);
// if (RCLASS_SUPER(argv[i])) {
// rb_warn("%"PRIsVALUE" has ancestors, but Refinement#import_methods "
// "doesn't import their methods", rb_class_path(argv[i]));
// }
// }
// arg.cref = rb_vm_cref_replace_with_duplicated_cref();
// arg.refinement = refinement;
// for (i = 0; i < argc; i++) { … rb_id_table_foreach(m_tbl, …); }
// return refinement;
//
// and refinement_import_methods_i, which does the copying:
//
// if (me->def->type != VM_METHOD_TYPE_ISEQ) {
// rb_raise(rb_eArgError, "Can't import method which is not defined with "
// "Ruby code: %"PRIsVALUE"#%"PRIsVALUE, …);
// }
// rb_cref_t *new_cref = rb_vm_cref_dup_without_refinements(me->def->body.iseq.cref);
// CREF_REFINEMENTS_SET(new_cref, CREF_REFINEMENTS(arg->cref));
// rb_add_method_iseq(arg->refinement, key, me->def->body.iseq.iseqptr,
// new_cref, METHOD_ENTRY_VISI(me));
//
// Four consequences the ruby/spec suite pins, each reproduced below:
//
// - Every argument is type-checked BEFORE anything is copied, so a bad
// argument late in the list imports nothing at all; but the
// "not defined with Ruby code" check happens DURING the copy, so the
// modules listed before the offending one keep their imports.
// - Only the module's OWN instance method table is walked: neither its
// included/prepended modules (hence the warning) nor its class methods.
// - The copy keeps its source cref for constants but takes the refinement's
// refinements, so an imported method sees the other imported methods and
// the rest of the refine block. Here the copy keeps the source method's
// lexScope (rbgo's cref for constant lookup) and takes the refinement as
// its owner, which is what this VM's refinement dispatch keys on.
// - Visibility is carried over with the method.

// installRefinementImport defines Refinement#import_methods. It is private, as
// MRI defines it, so it is reached as a bare call with the refinement as self —
// which is exactly how a refine block body invokes it.
func (vm *VM) installRefinementImport() {
m := &Method{
name: "import_methods",
native: refinementImportMethods,
owner: vm.cRefinement,
vis: visPrivate,
}
vm.cRefinement.methods["import_methods"] = m
}

// refinementImportMethods is the Refinement#import_methods body.
func refinementImportMethods(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value {
r, ok := self.(*RClass)
if !ok || !r.isRefinement {
raise("TypeError", "wrong argument type %s (expected Refinement)", vm.classOf(self).name)
}
if len(args) == 0 {
raise("ArgumentError", "wrong number of arguments (given 0, expected 1+)")
}
// Pass 1: Check_Type every argument first, so one bad argument leaves the
// refinement untouched, then warn for each module that has ancestors
// (RCLASS_SUPER non-NULL for a module means it includes or prepends one).
mods := make([]*RClass, len(args))
for i, a := range args {
mod, isMod := a.(*RClass)
if !isMod || !mod.isModule {
raise("TypeError", "wrong argument type %s (expected Module)", vm.classOf(a).name)
}
mods[i] = mod
}
for _, mod := range mods {
if len(mod.includes) > 0 || len(mod.prepends) > 0 {
vm.rbWarn("warning: %s has ancestors, but Refinement#import_methods doesn't import their methods",
vm.moduleToSStr(mod))
}
}
// Pass 2: copy. Modules are walked in argument order, so a name defined in
// several of them ends up holding the LAST one's definition.
for _, mod := range mods {
for _, name := range sortedMethodNames(mod.methods) {
src := mod.methods[name]
if src.undefined {
continue
}
if !definedWithRubyCode(src) {
raise("ArgumentError", "Can't import method which is not defined with Ruby code: %s#%s",
vm.moduleToSStr(mod), name)
}
cp := *src
// The refinement owns the copy: this VM reads a frame's refinement
// scope off the running method's owner, so an imported method that
// calls another imported method (or anything else the refine block
// defines) resolves it through the refinement, as MRI's
// CREF_REFINEMENTS_SET arranges.
cp.owner = r
// Constants keep resolving where the method was WRITTEN
// (rb_vm_cref_dup_without_refinements preserves the source cref), so
// pin lexScope to the source module unless the source already carried
// an explicit one.
if cp.lexScope == nil {
cp.lexScope = src.owner
}
r.methods[name] = &cp
}
}
return r
}

// definedWithRubyCode reports whether m is VM_METHOD_TYPE_ISEQ — a method
// written as Ruby source with `def`. A native (C-function) method, an
// attr_reader/writer and a define_method block body are all other types, which
// refinement_import_methods_i refuses.
func definedWithRubyCode(m *Method) bool {
return m.iseq != nil && m.native == nil && m.proc == nil && m.attrKind == 0
}

// sortedMethodNames returns tbl's keys in a stable order. MRI's
// rb_id_table_foreach has no defined order; sorting makes which method of a
// module is reported in the "not defined with Ruby code" ArgumentError — and
// how far the import got before raising — reproducible run to run.
func sortedMethodNames(tbl map[string]*Method) []string {
out := make([]string, 0, len(tbl))
for k := range tbl {
out = append(out, k)
}
sort.Strings(out)
return out
}
Loading
Loading