Class: Cheri::Explorer::RubyExplorer

Inherits:
Object
  • Object
show all
Defined in:
lib/cheri/explorer/explorer.rb

Constant Summary collapse

DeprecatedVars =
["$="]
System =
::Java::JavaLang::System
Runtime =
::Java::JavaLang::Runtime
JDate =
::Java::JavaUtil::Date
JVersion =
'java.version'.freeze
JVendor =
'java.vendor'.freeze
RtName =
'java.runtime.name'.freeze
RtVersion =
'java.runtime.version'.freeze
VmName =
'java.vm.name'.freeze
VmVersion =
'java.vm.version'.freeze
VmVendor =
'java.vm.vendor'.freeze

Instance Method Summary collapse

Instance Method Details

#available_processorsObject



591
592
593
# File 'lib/cheri/explorer/explorer.rb', line 591

def available_processors
  Runtime.runtime.available_processors
end

#configObject



551
552
553
# File 'lib/cheri/explorer/explorer.rb', line 551

def config
  Config::CONFIG  
end

#const_recs(parent_str = nil) ⇒ Object



383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
# File 'lib/cheri/explorer/explorer.rb', line 383

def const_recs(parent_str=nil)
  parent_str = nil if 'Module' == parent_str
  result = []
  if parent_str
    raise Cheri.type_error(parent_str,String) unless String === parent_str
    # make sure we don't have anything executable before we eval it
    raise ArgumentError,"Invalid constant "+parent_str unless /^([A-Z])((\w|::[A-Z])*)$/.match parent_str
    parent_str = parent_str[2,parent_str.length-2] if parent_str.rindex('::',0)
    if (parent = eval("::#{parent_str}") rescue nil) &&
        ((parent.respond_to?(:constants) && (consts = parent.constants rescue nil)) ||
         (parent.respond_to?(:__constants__) && (consts = parent.__constants__ rescue nil)))
      consts.each do |c|
        if (ec = (eval("::#{parent_str}::#{c}") rescue nil))
          result << ConstRec.new(parent_str,c,ec)
        end
      end
    end
  else
    Module.constants.each do |c|
      if (ec = (eval("::#{c}") rescue nil))
        result << ConstRec.new(nil,c,ec)
      end
    end
  end
  result
end

#constantsObject



373
374
375
376
377
378
379
380
381
382
# File 'lib/cheri/explorer/explorer.rb', line 373

def constants
  result = []
  begin
    Module.constants.each do |c|
      result << ([c,eval("::#{c}.class").to_s,eval("::#{c}.to_s")] rescue [c.to_s,'???', '???'])
    end
  rescue
  end
  result
end

#envObject



345
346
347
# File 'lib/cheri/explorer/explorer.rb', line 345

def env
  ENV.to_a
end

#env_javaObject



574
575
576
# File 'lib/cheri/explorer/explorer.rb', line 574

def env_java
  ENV_JAVA.to_hash
end

#env_java_briefObject



577
578
579
580
581
582
583
584
585
586
587
# File 'lib/cheri/explorer/explorer.rb', line 577

def env_java_brief
  {
    JVersion => ENV_JAVA[JVersion],
    JVendor => ENV_JAVA[JVendor],
    RtName => ENV_JAVA[RtName],
    RtVersion => ENV_JAVA[RtVersion],
    VmName => ENV_JAVA[VmName],
    VmVersion => ENV_JAVA[VmVersion],
    VmVendor => ENV_JAVA[VmVendor],
  }
end

#find(sargs) ⇒ Object

Raises:

  • (ArgumentError)


474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
# File 'lib/cheri/explorer/explorer.rb', line 474

def find(sargs)
  return unless sargs && defined?ObjectSpace
  raise Cheri.type_error(sargs,SearchArgs) unless SearchArgs === sargs
  sclazz = sargs.clazz
  raise Cheri.type_error(sclazz,String) unless String === sclazz
  # make sure we don't have anything executable before we eval it
  raise ArgumentError,"Invalid class "+ sclazz unless /^([A-Z])((\w|::[A-Z])*)$/.match sclazz
  if Module === (clazz = (eval("::#{sclazz}") rescue nil))
    GC.start if sargs.gc
    result = []
    if (vars = sargs.vars) && !vars.empty?
      # just supporting one var name and/or value for now
      sname = vars[0].name
      raise ArgumentError,"Invalid instance variable" if sname && !(Symbol === sname)
      sval = vars[0].value
      sval = nil if String === sval && sval.empty?
      rx = Regexp === sval
    else
      sname = nil
      sval = nil
    end
    if sname && sval
      ObjectSpace.each_object(clazz) do |o|
        if Fixnum === (id = o.__id__ rescue nil) &&
            #(o.respond_to?(:instance_variable_get) rescue nil) &&
            (iv = o.instance_variable_get(sname) rescue nil) &&
            (String === iv || String === (iv = iv.to_s rescue nil)) &&
            (rx ? (iv =~ sval rescue nil) : (iv.index(sval) rescue nil))
          result << id
        end
      end
    elsif sname
      sname = sname.to_s
      ObjectSpace.each_object(clazz) do |o|
        if Fixnum === (id = o.__id__ rescue nil) &&
            #(o.respond_to?(:instance_variables) rescue nil) &&
            Array === (ivs = o.instance_variables rescue nil) &&
            (ivs.include?(sname) rescue nil)
          result << id
        end
      end
    elsif sval
      # note: _not_ calling inspect here to avoid circular reference trap;
      # examining (potentially) each instance var instead
      ObjectSpace.each_object(clazz) do |o|
        if Fixnum === (id = o.__id__ rescue nil)
          if (String === (os = o) || String === (os = o.to_s rescue nil)) &&
              (rx ? (os =~ sval rescue nil) : (os.index(sval) rescue nil))
            result << id
          elsif Array === (ivs = o.instance_variables rescue nil)
            hit = nil
            ivs.each do |ivn|
              if (iv = o.instance_variable_get(ivn.to_sym) rescue nil) &&
                  (String === iv || String === (iv = iv.to_s rescue nil)) &&
                  (rx ? (iv =~ sval rescue nil) : (iv.index(sval) rescue nil))
                result << id unless hit
                hit = true
                # this doesn't appear to be breaking?
                break # TODO: check for LocalJumpError
              end
            end
          end
        end
      end
    else
      ObjectSpace.each_object(clazz) do |o|
        if Fixnum === (id = o.__id__ rescue nil)
          result << id
        end
      end
    end
    result
  else
    []
  end
end

#free_memoryObject



594
595
596
# File 'lib/cheri/explorer/explorer.rb', line 594

def free_memory
  Runtime.runtime.free_memory  
end

#global_varsObject

TODO: this doesn’t make much sense, needs to be per thread



349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
# File 'lib/cheri/explorer/explorer.rb', line 349

def global_vars
  result = []
  global_variables.each do |v|
    next if DeprecatedVars.include?v
    ev = eval(v) rescue '???'
    if Array === ev
      eva = []
      ev.each do |e|
        eva << e.to_s        
      end
      ev = eva
    elsif Hash === ev
      eva = []
    ev.each_pair do |k,e|
      eva << [k.to_s,e.to_s]      
    end
      ev = eva
    else
      ev = ev.to_s 
    end
    result << [v,ev]
  end
  result
end

#jruby_start_timeObject



603
604
605
# File 'lib/cheri/explorer/explorer.rb', line 603

def jruby_start_time
  @start_time ||= JDate.new(Cheri::JRuby.start_time).to_string
end

#jruby_versionObject



571
572
573
# File 'lib/cheri/explorer/explorer.rb', line 571

def jruby_version
  JRUBY_VERSION  
end

#max_memoryObject



600
601
602
# File 'lib/cheri/explorer/explorer.rb', line 600

def max_memory
  Runtime.runtime.max_memory  
end

#module_methods(name, id = nil) ⇒ Object



410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
# File 'lib/cheri/explorer/explorer.rb', line 410

def module_methods(name,id=nil)
  if id && defined?ObjectSpace
    raise Cheri.type_error(id,Fixnum) unless Fixnum === id
    # doing this in two steps to work around JRuby bug (JRUBY-1125)
    mod = ObjectSpace._id2ref(id)
    if Module === mod
      return MethodRec.new(mod)
    end
  elsif name
    raise Cheri.type_error(name,String) unless String === name
    return if /#/.match name
    # make sure we don't have anything executable before we eval it
    raise ArgumentError,"Invalid module "+name unless /^([A-Z])((\w|::[A-Z])*)$/.match name
    if Module === (mod = eval("::#{name}") rescue nil)
      return MethodRec.new(mod)       
    end
  end
  nil
end

#object(id) ⇒ Object



430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
# File 'lib/cheri/explorer/explorer.rb', line 430

def object(id)
  return unless id && defined?ObjectSpace
  raise Cheri.type_error(id,Fixnum) unless Fixnum === id
  # doing this in two steps to work around JRuby bug (JRUBY-1125)
  obj = ObjectSpace._id2ref(id)
  if obj && !(Fixnum === obj)
    if Module === obj && (name = obj.name rescue nil)
      if (ix = name.rindex('::'))
        parent = name[0,ix]
        name = name[ix+2,name.length - ix - 2]
      else
        parent = nil
      end
      ConstRec.new(parent,name,obj)
    else
      ObjectRec.new(obj)
    end
  else
    nil
  end
end

#object_methods(id) ⇒ Object



452
453
454
455
456
457
458
459
460
# File 'lib/cheri/explorer/explorer.rb', line 452

def object_methods(id)
  return unless id && defined?ObjectSpace
  raise Cheri.type_error(id,Fixnum) unless Fixnum === id
  # doing this in two steps to work around JRuby bug (JRUBY-1125)
  obj = ObjectSpace._id2ref(id)
  if obj && !(Fixnum === obj)
    return MethodRec.new(obj)    
  end
end

#object_space?Boolean

Returns:

  • (Boolean)


606
607
608
# File 'lib/cheri/explorer/explorer.rb', line 606

def object_space?
  Cheri::JRuby.object_space?  
end

#ruby_platformObject

TODO: aggregate values that will be displayed together



336
337
338
# File 'lib/cheri/explorer/explorer.rb', line 336

def ruby_platform
  RUBY_PLATFORM
end

#ruby_release_dateObject



342
343
344
# File 'lib/cheri/explorer/explorer.rb', line 342

def ruby_release_date
  RUBY_RELEASE_DATE  
end

#ruby_versionObject



339
340
341
# File 'lib/cheri/explorer/explorer.rb', line 339

def ruby_version
  RUBY_VERSION  
end

#security_managerObject



588
589
590
# File 'lib/cheri/explorer/explorer.rb', line 588

def security_manager
  System.security_manager ? System.security_manager.to_string : nil
end

#simple_value(id, maxlen = 80) ⇒ Object



462
463
464
465
466
467
468
469
470
471
472
# File 'lib/cheri/explorer/explorer.rb', line 462

def simple_value(id,maxlen=80)
  return unless id && defined?ObjectSpace
  raise Cheri.type_error(id,Fixnum) unless Fixnum === id
  # doing this in two steps to work around JRuby bug (JRUBY-1125)
  obj = ObjectSpace._id2ref(id)
  if obj && !(Fixnum === obj)
    obj.to_s[0,maxlen] rescue 'Unreadable'
  else
    nil
  end
end

#total_memoryObject



597
598
599
# File 'lib/cheri/explorer/explorer.rb', line 597

def total_memory
  Runtime.runtime.total_memory  
end