class VirtualModule::JuliaSourceProvider

Constants

EXT

Public Instance Methods

compile(vars=nil, type_info=nil, params=nil, script=nil, auto_binding=nil) click to toggle source
# File lib/virtual_module.rb, line 446
    def compile(vars=nil, type_info=nil, params=nil, script=nil, auto_binding=nil)
      @compiled_lib =
        File.read(File.dirname(__FILE__)+"/virtual_module/bridge.jl") + ";" +
        load_packages.join(";\n") + @transpiler.call(@source.join(";\n"))
      if !vars.nil? && !type_info.nil? && !params.nil? && !script.nil? && !auto_binding.nil?
        @compiled_lib += <<EOS
  function ___convert_type(name, typename, params)
    if length(findin(#{type_info[:params].keys.map{|e| e.to_s}},[name]))>0
      if typename=="FloatArray"
        convert(Array{Float64,1}, params[name])
      elseif typename=="IntArray"
        convert(Array{Int64,1}, params[name])
      end
    else
      params[name]
    end
  end

  function vm_builtin_eval_func(params)
    #{vars.map{|e| e.to_s + '=___convert_type("'+e.to_s+'","'+(type_info[:params][e]||"")+'", params[1])'}.join(";")}
    ##{vars.map{|e| 'println("'+e.to_s+'=", typeof('+e.to_s+'))' }.join(";")}
    ___evaluated = (#{@transpiler.call(script)})

    #{vars.map{|e| 'params[1]["'+e.to_s+'"]='+e.to_s }.join(";") if auto_binding}

    (___evaluated,#{auto_binding ? "params[1]" : "-1" })
  end
EOS
      end
    end
generate_message(input_queue_path, receiver, name, *args, **kwargs) click to toggle source
# File lib/virtual_module.rb, line 477
def generate_message(input_queue_path, receiver, name, *args, **kwargs)
  script, params = ["", ""]
  if args.count + kwargs.count > 0
    gen_driver = ->(arg, input_queue_path, i, type, param_name){
      val = case arg.class.to_s
        when "Module" then (
          (table_index = arg.___proxy_object_transmitter.get_index(@builder.object_id)).nil? ?
            "deserialize(fp)" :
            "object_lookup_table[#{table_index}]"
          )
        when "Symbol" then "convert(Symbol, unpack(readall(fp)))"
        else "unpack(readall(fp))"
      end
      script += "#{param_name} =open( \"#{input_queue_path}.#{i}.#{type}\", \"r\" ) do fp; #{val}; end;"
    }
    conv_kwargs = KwargsConverter.new("Dict{Symbol,Any}()", ->(k){"kwargs[:#{k}]"}, ";kwargs...")
    script, params = prepare_params(input_queue_path, gen_driver, conv_kwargs, name, *args, **kwargs)
  end
  callee = "#{name}"
  if !receiver.nil?
    if receiver[0..5]=="\xC1VMOBJ"
      script += "receiver=object_lookup_table[#{receiver[6..-1]}];"
    else
      File.write("#{input_queue_path}_serialized", receiver)
      script += "receiver =open( \"#{input_queue_path}_serialized\", \"r\" ) do fp; deserialize(fp); end;"
    end
    if name==:[]
      callee = "receiver"
    else
      callee = "receiver.#{name}"
    end
  end
  script += "#{callee}#{params};"
end
lib_script(ipc=nil) click to toggle source
# File lib/virtual_module.rb, line 432
    def lib_script(ipc=nil)
      if ipc!=:rpc
        @compiled_lib
      else
        <<EOS
import MsgPackRpcServer
module RemoteFunctions
#{@compiled_lib}
end
MsgPackRpcServer.run(parse(ARGS[1]), RemoteFunctions)
EOS
      end
    end
load_packages() click to toggle source
# File lib/virtual_module.rb, line 374
def load_packages
  @pkgs.map{|e| "import #{e}"}
end
main_loop(input_queue_path, output_queue_path, lib_script=nil) click to toggle source
# File lib/virtual_module.rb, line 390
    def main_loop(input_queue_path, output_queue_path, lib_script=nil)
      <<EOS
using MsgPack
object_lookup_table = Dict()
while true
  try
    source = open( "#{input_queue_path}", "r" ) do fp
      readall(fp)
    end
    if source[1]=='\n'
      open( "#{output_queue_path}", "w" ) do fp
        serialize(fp, object_lookup_table[parse(Int,source[2:length(source)])])
      end
    else
      result = eval(parse(source))
      open( "#{output_queue_path}", "w" ) do fp
        try
          write(fp,pack(result))
        catch
          object_lookup_table[object_id(result)] = result
          write(fp, 0xc1)
          write(fp, "VMOBJ")
          write(fp, string(object_id(result)))
        end
      end
    end
  catch err
    print(typeof(err))
    if !isa(err, InterruptException)
      open( "#{output_queue_path}", "w" ) do fp
        write(fp, 0xc1)
        write(fp, "VMERR")
        write(fp, string(err))
      end
    else
      exit
    end
  end
end
EOS
    end
to_a() click to toggle source
# File lib/virtual_module.rb, line 378
def to_a
  :tuple
end
to_s() click to toggle source
# File lib/virtual_module.rb, line 382
def to_s
  :string
end
vclass() click to toggle source
# File lib/virtual_module.rb, line 386
def vclass
  :summary
end