Class: Mirah::JavaSource::MethodBuilder

Inherits:
Object
  • Object
show all
Includes:
Helper
Defined in:
lib/mirah/jvm/source_generator/builder.rb

Direct Known Subclasses

JsniMethodBuilder

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helper

#annotate, #annotation_value, #block, #dedent, #indent, #init_value, #print, #puts

Constructor Details

#initialize(cls, options) ⇒ MethodBuilder

Returns a new instance of MethodBuilder.



346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
# File 'lib/mirah/jvm/source_generator/builder.rb', line 346

def initialize(cls, options)
  @class = cls
  @klass = cls
  @compiler = cls.compiler
  @out = Output.new
  @visibility = options[:visibility]
  @name = options[:name]
  @type = options[:return]
  @typename = @type && @type.to_source
  @locals = {}
  @args = options[:args].map do |arg|
    unless arg.kind_of? Array
      arg = [arg.inferred_type, arg.name]
    end
    @locals[arg[1]] = arg[0]
    arg
  end
  @static = options[:static] ? ' static' : nil
  @abstract = options[:abstract] && ' abstract'
  @exceptions = options[:exceptions] || []
  @temps = 0
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



452
453
454
455
456
457
458
459
# File 'lib/mirah/jvm/source_generator/builder.rb', line 452

def method_missing(name, *args)
  if name.to_s =~ /.const_(m)?(\d)/
    print '-' if $1
    print $2
  else
    super
  end
end

Instance Attribute Details

#klassObject

Returns the value of attribute klass.



344
345
346
# File 'lib/mirah/jvm/source_generator/builder.rb', line 344

def klass
  @klass
end

#nameObject

Returns the value of attribute name.



344
345
346
# File 'lib/mirah/jvm/source_generator/builder.rb', line 344

def name
  @name
end

#outObject

Returns the value of attribute out.



344
345
346
# File 'lib/mirah/jvm/source_generator/builder.rb', line 344

def out
  @out
end

#typeObject

Returns the value of attribute type.



344
345
346
# File 'lib/mirah/jvm/source_generator/builder.rb', line 344

def type
  @type
end

Instance Method Details

#declare_local(type, name, initialize = true) ⇒ Object



398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
# File 'lib/mirah/jvm/source_generator/builder.rb', line 398

def declare_local(type, name, initialize=true)
  unless @locals[name]
    if initialize
      print "#{type.to_source} #{name} = "
      if block_given?
        yield self
      else
        print init_value(type)
      end
      puts ';'
    end
    @locals[name] = type
  end
  name
end

#instanceof(type) ⇒ Object



448
449
450
# File 'lib/mirah/jvm/source_generator/builder.rb', line 448

def instanceof(type)
  print " instanceof #{type.to_source}"
end

#labelObject



423
424
425
426
# File 'lib/mirah/jvm/source_generator/builder.rb', line 423

def label
  @temps += 1
  "label#{@temps}"
end

#ldc_class(type) ⇒ Object



444
445
446
# File 'lib/mirah/jvm/source_generator/builder.rb', line 444

def ldc_class(type)
  print "#{type.to_source}.class"
end

#ldc_double(value) ⇒ Object



436
437
438
# File 'lib/mirah/jvm/source_generator/builder.rb', line 436

def ldc_double(value)
  print value
end

#ldc_float(value) ⇒ Object



432
433
434
# File 'lib/mirah/jvm/source_generator/builder.rb', line 432

def ldc_float(value)
  print "(float)#{value}"
end

#ldc_long(value) ⇒ Object



440
441
442
# File 'lib/mirah/jvm/source_generator/builder.rb', line 440

def ldc_long(value)
  print "#{value}L"
end

#local?(name) ⇒ Boolean

Returns:

  • (Boolean)


414
415
416
# File 'lib/mirah/jvm/source_generator/builder.rb', line 414

def local?(name)
  !!@locals[name]
end

#push_int(value) ⇒ Object



428
429
430
# File 'lib/mirah/jvm/source_generator/builder.rb', line 428

def push_int(value)
  print value
end

#startObject



369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
# File 'lib/mirah/jvm/source_generator/builder.rb', line 369

def start
  print "#{@visibility}#{@static}#{@abstract} #{@typename} #{@name}("
  @args.each_with_index do |(type, name), i|
    print ', ' unless i == 0
    print "#{type.to_source} #{name}"
  end
  print ')'
  unless @exceptions.empty?
    print ' throws '
    @exceptions.each_with_index do |exception, i|
      print ', ' unless i == 0
      print exception.to_source
    end
  end
  if @abstract
    puts ";"
    def self.puts(*args); end
    def self.print(*args); end
  else
    puts " {"
  end
  indent
end

#stopObject



393
394
395
396
# File 'lib/mirah/jvm/source_generator/builder.rb', line 393

def stop
  dedent
  puts "}"
end

#tmp(type, &block) ⇒ Object



418
419
420
421
# File 'lib/mirah/jvm/source_generator/builder.rb', line 418

def tmp(type, &block)
  @temps += 1
  declare_local(type, "temp$#{@temps}", &block)
end