Class: Cheri::Explorer::RubyExplorer

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

Constant Summary collapse

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



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

def available_processors
  Runtime.runtime.available_processors
end

#configObject



549
550
551
# File 'lib/cheri/explorer/explorer.rb', line 549

def config
  Config::CONFIG  
end

#const_recs(parent_str = nil) ⇒ Object



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

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



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

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



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

def env
  ENV.to_a
end

#env_javaObject



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

def env_java
  ENV_JAVA.to_hash
end

#env_java_briefObject



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

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)


472
473
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
# File 'lib/cheri/explorer/explorer.rb', line 472

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



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

def free_memory
  Runtime.runtime.free_memory  
end

#global_varsObject

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



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

def global_vars
  result = []
  global_variables.each do |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



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

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

#jruby_versionObject



569
570
571
# File 'lib/cheri/explorer/explorer.rb', line 569

def jruby_version
  JRUBY_VERSION  
end

#max_memoryObject



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

def max_memory
  Runtime.runtime.max_memory  
end

#module_methods(name, id = nil) ⇒ Object



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

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



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

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



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

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



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

def object_space?
  Cheri::JRuby.object_space?  
end

#ruby_platformObject

TODO: aggregate values that will be displayed together



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

def ruby_platform
  RUBY_PLATFORM
end

#ruby_release_dateObject



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

def ruby_release_date
  RUBY_RELEASE_DATE  
end

#ruby_versionObject



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

def ruby_version
  RUBY_VERSION  
end

#security_managerObject



586
587
588
# File 'lib/cheri/explorer/explorer.rb', line 586

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

#simple_value(id, maxlen = 80) ⇒ Object



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

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



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

def total_memory
  Runtime.runtime.total_memory  
end