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.



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

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



458
459
460
461
462
463
464
465
# File 'lib/mirah/jvm/source_generator/builder.rb', line 458

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.



346
347
348
# File 'lib/mirah/jvm/source_generator/builder.rb', line 346

def klass
  @klass
end

#nameObject

Returns the value of attribute name.



346
347
348
# File 'lib/mirah/jvm/source_generator/builder.rb', line 346

def name
  @name
end

#outObject

Returns the value of attribute out.



346
347
348
# File 'lib/mirah/jvm/source_generator/builder.rb', line 346

def out
  @out
end

#typeObject

Returns the value of attribute type.



346
347
348
# File 'lib/mirah/jvm/source_generator/builder.rb', line 346

def type
  @type
end

Instance Method Details

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



404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
# File 'lib/mirah/jvm/source_generator/builder.rb', line 404

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



454
455
456
# File 'lib/mirah/jvm/source_generator/builder.rb', line 454

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

#labelObject



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

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

#ldc_class(type) ⇒ Object



450
451
452
# File 'lib/mirah/jvm/source_generator/builder.rb', line 450

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

#ldc_double(value) ⇒ Object



442
443
444
# File 'lib/mirah/jvm/source_generator/builder.rb', line 442

def ldc_double(value)
  print value
end

#ldc_float(value) ⇒ Object



438
439
440
# File 'lib/mirah/jvm/source_generator/builder.rb', line 438

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

#ldc_long(value) ⇒ Object



446
447
448
# File 'lib/mirah/jvm/source_generator/builder.rb', line 446

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

#local?(name) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#push_int(value) ⇒ Object



434
435
436
# File 'lib/mirah/jvm/source_generator/builder.rb', line 434

def push_int(value)
  print value
end

#returns_void?Boolean

Returns:

  • (Boolean)


400
401
402
# File 'lib/mirah/jvm/source_generator/builder.rb', line 400

def returns_void?
  type.nil? || type.void?
end

#startObject



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

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



395
396
397
398
# File 'lib/mirah/jvm/source_generator/builder.rb', line 395

def stop
  dedent
  puts "}"
end

#tmp(type, &block) ⇒ Object



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

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