|
| 1 | +// Copyright (c) the go-embedded-ruby/ruby authors |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: BSD-3-Clause |
| 4 | + |
| 5 | +package vm |
| 6 | + |
| 7 | +import ( |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/go-embedded-ruby/ruby/internal/object" |
| 11 | +) |
| 12 | + |
| 13 | +// TestSetDupClone pins Set#dup / #clone to MRI 4.0.10 (#866): Set defines no dup |
| 14 | +// or clone of its own, only initialize_copy (set.c Init_Set), so the copy goes |
| 15 | +// through rb_obj_dup / rb_obj_clone and keeps the subclass, the instance |
| 16 | +// variables, the singleton (clone only) and a user initialize_copy hook. The |
| 17 | +// expected values are MRI's output for the same programs. |
| 18 | +func TestSetDupClone(t *testing.T) { |
| 19 | + cases := []struct{ src, want string }{ |
| 20 | + {`sub = Class.new(Set); s = sub.new([1, 2]); s.instance_variable_set(:@tag, 7) |
| 21 | +d = s.dup; c = s.clone |
| 22 | +p [d.class == sub, d.instance_variable_get(:@tag), d.to_a, c.class == sub, c.instance_variable_get(:@tag), c.to_a]`, |
| 23 | + "[true, 7, [1, 2], true, 7, [1, 2]]"}, |
| 24 | + // the copy is independent of the original. |
| 25 | + {`s = Set[1, 2]; d = s.dup; d << 3; p [s.size, d.size]`, "[2, 3]"}, |
| 26 | + // compare_by_identity survives both, as set_copy copies the table type. |
| 27 | + {`t = Set[1].compare_by_identity; p [t.dup.compare_by_identity?, t.clone.compare_by_identity?]`, "[true, true]"}, |
| 28 | + // clone copies the singleton class, dup does not. |
| 29 | + {`s = Set[1]; def s.hello = :hi |
| 30 | +p [(s.clone.hello rescue :none), (s.dup.hello rescue :none)]`, "[:hi, :none]"}, |
| 31 | + // a subclass hook runs, and super fills the members. |
| 32 | + {`k = Class.new(Set) { def initialize_copy(o) = (super; @copied = true) } |
| 33 | +d = k.new([1]).dup; p [d.instance_variable_get(:@copied), d.to_a]`, "[true, [1]]"}, |
| 34 | + // a hook that skips super leaves the copy empty: the members arrive only |
| 35 | + // through Set#initialize_copy. |
| 36 | + {`k = Class.new(Set) { def initialize_copy(o) = nil }; p k.new([1]).dup.to_a`, "[]"}, |
| 37 | + {`p Set.private_instance_methods(false).include?(:initialize_copy)`, "true"}, |
| 38 | + // set_i_initialize_copy: a self-copy is a no-op returning self ... |
| 39 | + {`s = Set[1, 2]; p [s.send(:initialize_copy, s).equal?(s), s.to_a]`, "[true, [1, 2]]"}, |
| 40 | + // ... returned BEFORE the iteration check, so it is allowed inside #each. |
| 41 | + {`s = Set[1, 2]; s.each { s.send(:initialize_copy, s) }; p s.to_a`, "[1, 2]"}, |
| 42 | + // ... a replace during an iteration is refused ... |
| 43 | + {`s = Set[1] |
| 44 | +begin; s.each { s.send(:initialize_copy, Set[9]) }; rescue => e; p [e.class, e.message]; end`, |
| 45 | + `[RuntimeError, "cannot replace set during iteration"]`}, |
| 46 | + // ... a non-Set source fails TypedData_Get_Struct ... |
| 47 | + {`begin; Set[1].send(:initialize_copy, [1]); rescue => e; p [e.class, e.message]; end`, |
| 48 | + `[TypeError, "wrong argument type Array (expected set)"]`}, |
| 49 | + // ... and a call replaces the members. |
| 50 | + {`p Set[1].send(:initialize_copy, Set[5, 6]).to_a`, "[5, 6]"}, |
| 51 | + } |
| 52 | + for _, c := range cases { |
| 53 | + if got := runSrc(t, c.src); got != c.want { |
| 54 | + t.Fatalf("src=%q got=%q want=%q", c.src, got, c.want) |
| 55 | + } |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +// TestDupCopiesGenericIvars: init_copy runs rb_copy_generic_ivar on every dup and |
| 60 | +// clone, so a value whose ivars live in the generic table (Array, String, Hash, |
| 61 | +// Set) keeps them, in order, and the copy's table is its own. |
| 62 | +func TestDupCopiesGenericIvars(t *testing.T) { |
| 63 | + cases := []struct{ src, want string }{ |
| 64 | + {`r = [[1], +"s", {a: 1}, Set[1]].map { |o| o.instance_variable_set(:@x, 1); [o.dup.instance_variable_get(:@x), o.clone.instance_variable_get(:@x)] } |
| 65 | +p r`, "[[1, 1], [1, 1], [1, 1], [1, 1]]"}, |
| 66 | + {`a = [1]; a.instance_variable_set(:@x, 1); a.instance_variable_set(:@y, 2) |
| 67 | +d = a.dup; d.instance_variable_set(:@x, 9); p [a.instance_variable_get(:@x), d.instance_variables]`, "[1, [:@x, :@y]]"}, |
| 68 | + {`p [1].dup.instance_variables`, "[]"}, |
| 69 | + } |
| 70 | + for _, c := range cases { |
| 71 | + if got := runSrc(t, c.src); got != c.want { |
| 72 | + t.Fatalf("src=%q got=%q want=%q", c.src, got, c.want) |
| 73 | + } |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +// TestCopyGenericIvarsToImmediate: an immediate has no table, so a copy onto one |
| 78 | +// is dropped rather than dereferencing a missing entry. |
| 79 | +func TestCopyGenericIvarsToImmediate(t *testing.T) { |
| 80 | + src := object.NewArrayFromSlice(nil) |
| 81 | + setIvar(src, "@x", object.IntValue(1)) |
| 82 | + func() { |
| 83 | + defer func() { |
| 84 | + if r := recover(); r != nil { |
| 85 | + t.Fatalf("copy onto an immediate must be dropped, got panic: %v", r) |
| 86 | + } |
| 87 | + }() |
| 88 | + copyGenericIvars(src, object.IntValue(7)) |
| 89 | + }() |
| 90 | + if got := getIvar(src, "@x"); got != object.IntValue(1) { |
| 91 | + t.Fatalf("source ivar changed: %v", got) |
| 92 | + } |
| 93 | +} |
0 commit comments