Module: Library::ClassMethods

Defined in:
lib/rubysol/library.rb

Instance Method Summary collapse

Instance Method Details

#enum(class_name, *args) ⇒ Object



18
19
20
21
# File 'lib/rubysol/library.rb', line 18

def enum( class_name, *args )
  typedclass = Types::Enum.new( class_name, *args, scope: self  )
  typedclass
end

#method_added(name) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/rubysol/library.rb', line 86

def method_added( name )

  if sigs_exclude.include?( name )
     puts "--- skip method added hook >#{name}< - found in sigs exclude"
     return ## do nothing; 
  else
     puts "==> method added hook >#{name}<... processing..."
  end

  ## pp name
  ## pp name.class.name

  name = name.to_sym  ## note: make sure name is ALWAYS a symbol

  ## note:  method lookup via method needs an object / INSTANCE
  ##             NOT working with class only!!!!
 
  # m = method( name )
  # pp m.name
  # pp m.parameters
  # pp m

  raise "no unnamed sig(nature) on stack for method >#{name}< in module >#{self.name}<; sorry"   if sigs_unnamed.size == 0
  sig_unnamed = sigs_unnamed.pop  
  puts "    using sig_unnamed: #{sig_unnamed.inspect}"
  

  @sigs ||= {}
  raise "duplicate method sig(nature) for method >#{name}< in module >#{self.name}<; sorry"   if @sigs.has_key?( name )
  @sigs[ name ] = sig_unnamed



  puts "    generate typed_library_function >#{name}<"
  Generator.typed_library_function( self, name, 
                                      inputs: sig_unnamed[ :inputs ] )  

  puts "<== method added hook >#{name}< done."                               
end

#sig(args = [], *options, returns: nil) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/rubysol/library.rb', line 44

def sig( args=[], *options, returns: nil )

  puts "[debug] add sig args: #{args.inspect}, options: #{options.inspect}, returns: #{returns.inspect}"
  ## use inputs for args) and outputs for returns  - why? why not?

  ## check if include explicit visibility in options
  if  options.include?( :public ) ||
      options.include?( :private ) ||
      options.include?( :internal )
      # do nothing / pass-along as is
  else
      # auto-add default up-front - :public or :internal if name starting with underscore (_)
      visibility =  name.start_with?( '_' )  ? :internal : :public
      options.unshift( visibility )
  end

  ####  
  #  auto-convert args (inputs), returns (outputs) to type (defs)
  args = args.map { |value| typeof( value ) }

  ###
  ## note: turn returns into an array - empty if nil, etc.
  ##        always wrap into array
  returns =  if returns.nil?
                  []
             elsif returns.is_a?( ::Array ) 
                  returns 
             else  ## assume single type
                  [returns]  
             end  

  returns = returns.map { |value| typeof( value ) }


  @sigs_unnamed ||= [] 
  @sigs_unnamed.push( { inputs:  args,
                        outputs: returns,
                        options: options } )
end

#sigsObject

note: sig machinery with method_added MUST come last here



25
26
27
# File 'lib/rubysol/library.rb', line 25

def sigs
  @sigs ||= {}
end

#sigs_excludeObject

ignore this methods;

do NOT (auto-)generate wrapper method popping (unnamed) sig from stack!!!


35
36
37
38
39
40
# File 'lib/rubysol/library.rb', line 35

def sigs_exclude
  ## note: always exclude  globals
  ##        get or can get changed via runtime modules (simulacrm) and such!!!
  ##  e.g. msg.sender, block.timestamp, tx.origin, etc. 
  @sigs_exclude ||= [:block, :tx, :msg]
end

#sigs_unnamedObject

unnamed sigs stack



29
30
31
# File 'lib/rubysol/library.rb', line 29

def sigs_unnamed   ## unnamed sigs stack 
  @sigs_unnamed ||= []
end

#struct(class_name, **attributes) ⇒ Object



13
14
15
16
# File 'lib/rubysol/library.rb', line 13

def struct( class_name, **attributes )
  typedclass = Types::Struct.new( class_name, scope: self, **attributes )
  typedclass  
end