#class InterfaceBase def exposeInterface(klass,box,definition,*args) klassName=klass.to_s #args=args.map{|a|"'"+a.to_s+"'"}.join(",") name=definition.name iname=klassName+name api=klass.interfaceApi.map{|f| "def self.#{f}(*s) begin @@i.#{f}(*s) rescue Object=>e BasicInterface::puts e end end" }.join("\n") $myargs = args s=" module #{iname} @@i=#{klassName}.new(*$myargs) #{api} end" #pp s eval(s,binding) klass.class_eval("@@interface=#{iname} def interface @@interface end ") $myargs = nil #pp iname #exit s="box.ref(#{iname})" pp s eval(s) s="box.eval('#{klassName}=#{iname}')" eval(s) end class Class def interfaceApi(*s) @@interfaces||=[] @@interfaces << self @@api||=[] s.each{|f| @@api << f } @@api end def self.interfaceClasses @@interfaces.uniq end end class BasicInterface interfaceApi :test, :puts, :pp def test puts "TEST" end def puts(*s) Kernel::puts(*s) end def pp(*s) self.puts s.inspect end end class FSInterface def initialize(dir) @dir=dir end interfaceApi :save, :load def save(filename,content) raise AccessViolation unless filename=~/^[a-zA-Z_\.0-9]*$/ p=File.join(@dir,filename) puts "saving ",p File.open(p,"w") do |f| f.puts content end end def load(filename) raise AccessViolation unless filename=~/^[a-zA-Z_\.0-9]*$/ p=File.join(@dir,filename) #puts "loading ",p File.open(p) do |f| f.read end end def self.create(box,definition,server) exposeInterface(self,box,definition,"data/packages/#{definition.name}") end end class RequireInterface interfaceApi :loadPrg def initialize(path) @path=path end def loadPrg(filename) raise AccessViolation unless filename=~/^[a-zA-Z_\.0-9]*$/ p=File.join(@path,filename) File.open(p) do |f| f.read end end def self.create(box,definition,server) exposeInterface(self,box,definition,"packages/#{definition.name}") end end class OtherPackage def initialize(pkgname) @pkgname=pkgname end def method_missing(*s) #pp s PackageInterface::run(@pkgname,*s) end end class PackageInterface interfaceApi :package, :run def initialize(server) @server=server end def run(*x) pp x @server.interface(x[0]).send(*x[1..-1]) end def package(name) pp "PACKAGE:",name,interface OtherPackage.new(name) #nil end def self.create(box,definition,server) box.eval("class OtherPackage def initialize(interface,pkgname) @interface=interface @pkgname=pkgname end def method_missing(*s) PackageInterface::run(@pkgname,*s) end end ") exposeInterface(self,box,definition,server) end end #INTERFACES=[BasicInterface,FSInterface]