Skip to content

Commit

Permalink
Fix: don't move implementations to Fiber, only the hashes
Browse files Browse the repository at this point in the history
  • Loading branch information
ysbaddaden committed Jan 22, 2025
1 parent f36a036 commit 73c8b3f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 27 deletions.
32 changes: 8 additions & 24 deletions src/fiber.cr
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,8 @@ class Fiber
@timeout_select_action = nil

# Additional cleanup (avoid stale references)
@exec_recursive = nil
@exec_recursive_clone = nil
@exec_recursive_hash = nil
@exec_recursive_clone_hash = nil

@alive = false
{% unless flag?(:interpreted) %}
Expand Down Expand Up @@ -338,31 +338,15 @@ class Fiber

# :nodoc:
#
# Protects `Reference#inspect` and `Reference#pretty_print` against recursion.
def exec_recursive(object_id : UInt64, method : Symbol, &)
# NOTE: can't use `Set` because of prelude require order
hash = (@exec_recursive ||= Hash({UInt64, Symbol}, Nil).new)
key = {object_id, method}
hash.put(key, nil) do
yield
hash.delete(key)
return true
end
false
# See `Reference#exec_recursive` for details.
def exec_recursive_hash
@exec_recursive_hash ||= Hash({UInt64, Symbol}, Nil).new
end

# :nodoc:
#
# Protects `Reference#clone` implementations against recursion. See
# `Reference#exec_recursive_clone` for more details.
def exec_recursive_clone(object_id : UInt64, &)
# NOTE: can't use `Set` because of prelude require order
hash = (@exec_recursive_clone ||= Hash(UInt64, UInt64).new)
clone_object_id = hash[object_id]?
unless clone_object_id
clone_object_id = yield(hash).object_id
hash.delete(object_id)
end
Pointer(Void).new(clone_object_id)
# See `Reference#exec_recursive_clone` for details.
def exec_recursive_clone_hash
@exec_recursive_clone_hash ||= Hash(UInt64, UInt64).new
end
end
20 changes: 17 additions & 3 deletions src/reference.cr
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,15 @@ class Reference
end

private def exec_recursive(method, &)
Fiber.current.exec_recursive(object_id, method) { yield }
# NOTE: can't use `Set` because of prelude require order
hash = Fiber.current.exec_recursive_hash
key = {object_id, method}
hash.put(key, nil) do
yield
hash.delete(key)
return true
end
false
end

# Helper method to perform clone by also checking recursiveness.
Expand All @@ -199,7 +207,13 @@ class Reference
# end
# ```
private def exec_recursive_clone(&)
pointer = Fiber.current.exec_recursive_clone(object_id) { |hash| yield hash }
pointer.as(self)
# NOTE: can't use `Set` because of prelude require order
hash = Fiber.current.exec_recursive_clone_hash
clone_object_id = hash[object_id]?
unless clone_object_id
clone_object_id = yield(hash).object_id
hash.delete(object_id)
end
Pointer(Void).new(clone_object_id).as(self)
end
end

0 comments on commit 73c8b3f

Please sign in to comment.