Class: FxmlJitInfo

Inherits:
Object
  • Object
show all
Includes:
JRubyFX
Defined in:
lib/fxmlloader/fxml_jit_info.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_name, jit_settings = 1, outfile = nil, cache_dir = nil, validate = true, opts = nil) ⇒ FxmlJitInfo

Returns a new instance of FxmlJitInfo.


47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/fxmlloader/fxml_jit_info.rb', line 47

def initialize(file_name, jit_settings=1, outfile=nil, cache_dir=nil,validate = true, opts = nil)
  @file_name = file_name
  @no_write = (opts && opts[:no_write]) || false
  if @file_name.start_with? "file:"
    @file_name = @file_name.gsub(/^file\:/, '')
  elsif @file_name.start_with? "jar:"
    @no_write = true
  elsif @file_name.start_with? "compoundjar:"
    @no_write = true
  end
  @jit_settings = jit_settings
  @run_count = 0
  @opts = opts
  @outfile = if @outfile
    outfile
  else
    cache_dir = cache_dir || File.join(File.dirname(@file_name), ".jrubyfx_cache")
    FileUtils.mkpath(cache_dir) unless File.directory?(cache_dir) or @no_write
    @f_hash = Digest::SHA1.hexdigest(File.basename @file_name)
    File.join(cache_dir, "#{@f_hash}.rb")
  end
  if File.exist?(@outfile) && !(opts && opts[:force])
    @compiled = self.class.load_aot(@file_name, @outfile, validate)
    if @compiled
      dputs "got #{file_name} from cache"
    end
  end
end

Instance Attribute Details

#file_nameObject

TODO: store jit settings in here instead of $RB_* variables


46
47
48
# File 'lib/fxmlloader/fxml_jit_info.rb', line 46

def file_name
  @file_name
end

#jit_settingsObject

TODO: store jit settings in here instead of $RB_* variables


46
47
48
# File 'lib/fxmlloader/fxml_jit_info.rb', line 46

def jit_settings
  @jit_settings
end

#raw_codeObject

TODO: store jit settings in here instead of $RB_* variables


46
47
48
# File 'lib/fxmlloader/fxml_jit_info.rb', line 46

def raw_code
  @raw_code
end

Class Method Details

.hash(file) ⇒ Object


32
33
34
# File 'lib/fxmlloader/fxml_jit_info.rb', line 32

def self.hash(file)
  Digest::SHA1.hexdigest(File.read file)
end

.load_aot(file, cache, validate = true) ⇒ Object


35
36
37
38
39
40
41
42
43
44
# File 'lib/fxmlloader/fxml_jit_info.rb', line 35

def self.load_aot(file, cache, validate = true)
  if validate
    hash = hash(file)
    f_hash = File.open(cache, "r", &:readline).strip
    return false if "# #{hash} encoding: utf-8" != f_hash
  end
  hash = Digest::SHA1.hexdigest(File.basename file)
  require cache
  JRubyFX::GeneratedAssets.const_get("AOT#{hash}").new rescue nil
end

Instance Method Details

#__build_via_jit(__local_fxml_controller, __local_namespace, __local_jruby_ext) ⇒ Object


88
89
90
# File 'lib/fxmlloader/fxml_jit_info.rb', line 88

def __build_via_jit(__local_fxml_controller, __local_namespace, __local_jruby_ext)
  @compiled.__build_via_jit(__local_fxml_controller, __local_namespace, __local_jruby_ext)
end

#compile(code = @raw_code) ⇒ Object


91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/fxmlloader/fxml_jit_info.rb', line 91

def compile(code=@raw_code)
  @raw_code = code
  # TODO: begin rescue end
  full_code =  <<METHOD_DEF
  def __build_via_jit(__local_fxml_controller, __local_namespace, __local_jruby_ext)
    __local_fx_id_setter = lambda do |name, __i|
      __local_namespace[name] = __i
      __local_fxml_controller.instance_variable_set(("@\#{name}").to_sym, __i)
    end
#{code}
  end
METHOD_DEF
  ;#)
  unless @no_write
    begin
      jit_aot_cache(full_code)
    rescue
      p $!
      jit_no_cache(full_code)
    end
  else
    jit_no_cache(full_code)
  end
  if @opts && @opts[:compiled_hook]
    @opts[:compiled_hook].call(@outfile)
  end

end

#compiled?Boolean

Returns:

  • (Boolean)

82
83
84
# File 'lib/fxmlloader/fxml_jit_info.rb', line 82

def compiled?
  !!@compiled
end

#decompileObject


85
86
87
# File 'lib/fxmlloader/fxml_jit_info.rb', line 85

def decompile
  @compiled = nil
end

#hashObject


75
76
77
# File 'lib/fxmlloader/fxml_jit_info.rb', line 75

def hash
  FxmlJitInfo.hash(@file_name)
end

#jit_aot_cache(full_code) ⇒ Object


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
# File 'lib/fxmlloader/fxml_jit_info.rb', line 126

def jit_aot_cache(full_code)
  File.open(@outfile, "w") do |f|
    hash = hash()
    f << <<AOT
# #{hash} encoding: utf-8
# @@ 1

########################### DO NOT MODIFY THIS FILE ###########################
#       This file was automatically generated by JRubyFX-fxmlloader on        #
# #{Time.now} for #{@file_name}
########################### DO NOT MODIFY THIS FILE ###########################

module JRubyFX
module GeneratedAssets
  class AOT#{@f_hash}
    include JRubyFX
    #{full_code}
    def hash
      #{hash.inspect}
    end
    def compiled?
      true
    end
  end
end
end
AOT
  end
  puts "saved #{@outfile} to cache"
  require @outfile
  @compiled = JRubyFX::GeneratedAssets.const_get("AOT#{@f_hash}").new rescue nil
end

#jit_no_cache(full_code) ⇒ Object


120
121
122
123
124
# File 'lib/fxmlloader/fxml_jit_info.rb', line 120

def jit_no_cache(full_code)
  dputs "JIT only, no aot for #{@file_name}"
  self.instance_eval full_code
  @compiled = true
end

#should_jit?Boolean

Returns:

  • (Boolean)

78
79
80
81
# File 'lib/fxmlloader/fxml_jit_info.rb', line 78

def should_jit?
  return false if @jit_settings == :no_jit || compiled?
  return true if (@run_count += 1) >= @jit_settings
end