Class: ASTTransform::Thunk

Inherits:
Node
  • Object
show all
Defined in:
lib/ast_transform/thunk.rb

Overview

The reordering primitive: an eagerly built wrapper node, spliced wherever the wrapped statements must EXECUTE — statement position or composed inside an expression (e.g. an assert_raises block body). Its body keeps its own source locations, and the lowering derives the wrapper's textual placement from them (see ThunkLowering), so the statements still emit on their original lines even though execution waits.

Children are [id, *body_statements] and the invariants are enforced here in initialize, which every construction path shares — s routing, the thunk helper, and Processor rebuilds (+updated+ re-initializes). Build thunks with TransformationHelper#thunk; reuse the same node to execute one body from several points (multiplexing).

Runtime semantics are near-transparent (proc lowering): return still returns from the enclosing method, and locals the body assigns stay method-scope. See ThunkLowering for the full contract.

Defined Under Namespace

Classes: Id

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Node

build, register, registry

Constructor Details

#initialize(type, children, properties = {}) ⇒ Thunk

Returns a new instance of Thunk.

Raises:

  • (ArgumentError)


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/ast_transform/thunk.rb', line 28

def initialize(type, children, properties = {})
  id, *body = children
  unless id.is_a?(ASTTransform::Thunk::Id)
    raise ArgumentError,
      "a Thunk's first child must be its #{Thunk::Id} (got #{id.class}); build thunks with the " \
        "thunk(*statements) helper"
  end
  raise ArgumentError, "a Thunk must wrap at least one statement" if body.empty?

  # Captured before super (which freezes the node); frozen so the shared array cannot be mutated out from under
  # +children+. Rebuilds via +updated+ re-run initialize, so the capture can never go stale.
  @id = id
  @body = body.freeze

  super
end

Instance Attribute Details

#bodyArray<Parser::AST::Node> (readonly)

Note:

Same as the Parser::AST::Node#children

Retrieves the Thunk's body.

Returns:

  • (Array<Parser::AST::Node>)

    The nodes forming the body of the Thunk.



53
54
55
# File 'lib/ast_transform/thunk.rb', line 53

def body
  @body
end

#idASTTransform::Thunk::Id (readonly)

Retrieves the Thunk's id.

Returns:



47
48
49
# File 'lib/ast_transform/thunk.rb', line 47

def id
  @id
end