Module: Generator
- Defined in:
- lib/rubysol/generator.rb
Overview
“centralize”
meta-programming magic (code generation) in module here - why? why not?
find a better name e.g Magic? Pilot? or CodeGen or Metagen or ???
Defined Under Namespace
Class Method Summary collapse
-
._demodulize(path) ⇒ Object
helpers.
-
.getter_function(contract_class, name, type) ⇒ Object
add public getters helpers.
- .global_function(contract_class) ⇒ Object
- .mapping_getter_function(contract_class, name, type) ⇒ Object
-
.typed_function(contract_class, name, inputs:) ⇒ Object
to rename to generate_typed or generate_wrapped or ?? typed_method??.
- .typed_library_function(contract_class, name, inputs:) ⇒ Object
Class Method Details
._demodulize(path) ⇒ Object
helpers
12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/rubysol/generator.rb', line 12 def self._demodulize( path ) ## turn class.name into a symbol cutting off all namepspaces (::) ## e.g. Contracts::ERC20 => ERC20 ## ## note: ActiveSupport::String#demodulize is the same (for string) path = path.to_s if i = path.rindex('::') path[(i+2)..-1] else path end end |
.getter_function(contract_class, name, type) ⇒ Object
add public getters helpers
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 |
# File 'lib/rubysol/generator.rb', line 231 def self.getter_function( contract_class, name, type ) ## note: make sure name is always a symbol name = name.to_sym if type.mapping? mapping_getter_function( contract_class, name, type ) elsif type.array? puts "[debug] auto-generate public array getter - #{name} : #{type}:" ## auto-add/register sig(nature) in here - why? why not? ## contract_class.sig( name, ## [Types::Typed::UIntType.instance], ## :view, returns: type.sub_type ) ## auto-add/register sig(nature) in here - why? why not? ## fix-fix-fix - check if outputs is an array (or still single type?) contract_class.sigs[ name ] = { inputs: [typeof(Types::UInt)], outputs: [typeof(type.sub_type)], options: [:view,:public] } contract_class.sigs_exclude << name contract_class.class_eval do ## note: hack: must use kwargs for now!!! index: (not index) for now define_method name do |index:| puts "[debug] call public (state) array getter for #{name} : #{type} with index #{index} (#{contract_class.name})" puts "[debug] self -> #{self}" value = instance_variable_get( :"@#{name}" ) value[index] end end # class_evel ## auto-add typed wrapper!!!!! typed_function( contract_class, name, inputs: [typeof(Types::UInt)] ) else puts "[debug] auto-generate public getter - #{name} : #{type}:" ## auto-add/register sig(nature) in here - why? why not? ## fix-fix-fix - check if outputs is an array (or still single type?) contract_class.sigs[ name ] = { inputs: [], outputs: [typeof(type)], options: [:view, :public] } contract_class.sigs_exclude << name contract_class.class_eval do define_method name do puts "[debug] call public (state) getter for #{name} : #{type} (#{contract_class.name})" puts "[debug] self -> #{self}" value = instance_variable_get( :"@#{name}" ) value end end # class_eval ## auto-add typed wrapper!!!!! typed_function( contract_class, name, inputs: [] ) end # if ## puts "after - instance_methods:" ## pp contract_class.instance_methods( false ) end |
.global_function(contract_class) ⇒ Object
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 |
# File 'lib/rubysol/generator.rb', line 367 def self.global_function( contract_class ) ## todo/check: check if method exists already - why? why not? contract_name = _demodulize( contract_class.name ).to_sym ## use module_eval if exists? why? why not? ## example: ## def ERC20( address ) ## klass = ERC20 ## puts "==> calling #{klass.name}( #{address.pretty_print_inspect })" ## obj = klass.at( address ) ## raise ArgumentError, "no #{klass.name} contract @ addreess #{address} found; sorry" if obj.nil? ## puts " bingo! #{obj.class.name} (#{obj.class.parent_contracts}) contract found @ #{address}" ## obj ## end Globals.class_eval do define_method contract_name do |address| klass = contract_class puts "==> calling #{klass.name}( #{address.pretty_print_inspect })" obj = klass.at( address ) raise ArgumentError, "no #{klass.name} contract @ address #{address} found; sorry" if obj.nil? puts " bingo! #{obj.class.name} (#{obj.class.parent_contracts}) contract found @ #{address}" obj end end end |
.mapping_getter_function(contract_class, name, type) ⇒ Object
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 |
# File 'lib/rubysol/generator.rb', line 296 def self.mapping_getter_function( contract_class, name, type ) ## note: make sure name is always a symbol name = name.to_sym index = 0 current_type = type sig_args = [] while current_type.mapping? do sig_args << current_type.key_type current_type = current_type.value_type index += 1 end puts "[debug] auto-generate public mapping getter - #{name} : #{type} (#{contract_class.name}):" puts "sig_args:" pp sig_args ## auto-add/register sig(nature) in here - why? why not? ## contract_class.sig( name, sig_args, :view, returns: current_type) puts " index: #{index}" # {:arg0=>:address} # index: 1 ## check - if sig_args is alreay array of types (or typedclasses???) sig_args = sig_args.map {|sig_arg| typeof(sig_arg) } contract_class.sigs[ name ] = { inputs: sig_args, outputs: [typeof(current_type)], options: [:view, :public] } contract_class.sigs_exclude << name puts contract_class.sigs[ name ].pretty_print_inspect contract_class.class_eval do ## note: hack: must use kwargs for now!!! arg0, arg1, arg2, for now define_method name do |arg0:, arg1: nil, arg2: nil, arg3: nil| puts "[debug] call public (state) mapping getter for #{name} : #{type} - index: #{index} (#{contract_class.name})" puts "[debug] self -> #{self}" args = [arg0, arg1, arg2, arg3] puts "[debug] args -> #{args.pretty_print_inspect}" ## raise ArgumentError, "expected #{index} argument(s); got #{args.size}" if args.size != index value = instance_variable_get( "@#{name}" ) (0...index).each do |i| value = value[ args[i] ] end value end end # class_eval ## auto-add typed wrapper!!!!! typed_function( contract_class, name, inputs: sig_args ) end |
.typed_function(contract_class, name, inputs:) ⇒ Object
to rename to generate_typed or generate_wrapped or ??
typed_method??
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/rubysol/generator.rb', line 104 def self.typed_function( contract_class, name, inputs: ) ## note: must? find matching method in class ## todo/fix: use methods( false) if available (do NOT look-up in subclasses or such) ## note: make sure name and contract_name is always a symbol name = name.to_sym contract_name = _demodulize( contract_class.name ).to_sym exists = contract_class.instance_methods( false ).include?( name ) if !exists = "[ERRRO] no method #{name} found for sig in class #{contract_class.name}" puts ## fix: change to NoMethodError - exists? raise NameError, end ## note: method lookup via method needs an object / INSTANCE ## NOT working with class only!!!! ## m = contract_class.method( name ) ## puts " bingo! #{name} - #{m.owner}" puts " bingo! #{name}" ## avoid recursive circle ## exlcude method from method_add automagic wrapping/generation contract_class.sigs_exclude << :"__#{contract_name}__#{name}_unsafe" contract_class.sigs_exclude << :"__#{contract_name}__#{name}" contract_class.sigs_exclude << name ## ## use :name_raw instead of :name_unsafe - why? why not? ## rewire ## alias_method :name_unsafe, :name ## alias_method :name, :name_typed contract_class.class_eval do define_method :"__#{contract_name}__#{name}" do |*args_unsafe,**kwargs_unsafe| puts "==> calling __#{contract_name}__#{name} (class #{contract_class.name})" m = method( :"__#{contract_name}__#{name}_unsafe" ) kwargs = Function.params( m, inputs, *args_unsafe, **kwargs_unsafe ) ret = m.call( **kwargs ) ## todo/fix: ## check returns type / value too ret end ## note: must add class/contract name (via prefix) here!! ## rename (unsafe/untyped) method alias_method :"__#{contract_name}__#{name}_unsafe", name alias_method name, :"__#{contract_name}__#{name}" ## if name == :constructor ### add ERC20() or such ## alias_method contract_name, :"__#{contract_name}__#{name}" ## end ## - use leading underscore - why? why not? e.g. _ERC20() ## as alternative to NOT conflict with global conversion function - why? why not? end end |
.typed_library_function(contract_class, name, inputs:) ⇒ Object
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/rubysol/generator.rb', line 168 def self.typed_library_function( contract_class, name, inputs: ) ## note: must? find matching method in class ## todo/fix: use methods( false) if available (do NOT look-up in subclasses or such) ## todo - make sure contract_class is a module (not a class) !!!! ## note: make sure name and contract_name is always a symbol name = name.to_sym contract_name = _demodulize( contract_class.name ).to_sym exists = contract_class.instance_methods( false ).include?( name ) if !exists = "[ERRRO] no method #{name} found for sig in module #{contract_class.name}" puts ## fix: change to NoMethodError - exists? raise NameError, end ## note: method lookup via method needs an object / INSTANCE ## NOT working with class only!!!! ## m = contract_class.method( name ) ## puts " bingo! #{name} - #{m.owner}" puts " bingo! #{name}" ## avoid recursive circle ## exlcude method from method_add automagic wrapping/generation contract_class.sigs_exclude << :"#{name}_unsafe" contract_class.sigs_exclude << name ## ## use :name_raw instead of :name_unsafe - why? why not? ## rewire ## alias_method :name_unsafe, :name ## alias_method :name, :name_typed contract_class.class_eval do alias_method :"#{name}_unsafe", name define_method name do |*args_unsafe,**kwargs_unsafe| puts "==> calling #{contract_name}.#{name} (module #{contract_class.name})" m = method( :"#{name}_unsafe" ) kwargs = Function.params( m, inputs, *args_unsafe, **kwargs_unsafe ) ret = m.call( **kwargs ) ## todo/fix: ## check returns type / value too ret end ## make into module functions!!! module_function name module_function :"#{name}_unsafe" end end |