Class: Gap::CFunc

Inherits:
Object
  • Object
show all
Defined in:
lib/gap50/gap/cfunc.rb

Constant Summary collapse

DEFAULT_GEN =
CFunc.new

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sam = Gap::Main) ⇒ CFunc

Returns a new instance of CFunc.



3
4
5
# File 'lib/gap50/gap/cfunc.rb', line 3

def initialize(sam = Gap::Main)
    @sam = sam
end

Class Method Details

.gen(name, args, text) ⇒ Object



40
41
42
# File 'lib/gap50/gap/cfunc.rb', line 40

def self.gen(name, args, text)
    DEFAULT_GEN.gen name, args, text
end

.genlib(name, text) ⇒ Object



44
45
46
# File 'lib/gap50/gap/cfunc.rb', line 44

def self.genlib(name, text)
    DEFAULT_GEN.genlib name, text
end

Instance Method Details

#gen(name, args, text) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/gap50/gap/cfunc.rb', line 7

def gen(name, args, text)
    md5 = Gap::MD5.hexdigest(text)
    cpp = _file name + "_" + md5, ".cpp"
    outdll = _file name + "_" + md5, ".dll"
    dll = name + "_" + md5 + ".dll"
    output = @sam.genfile dll do
        @sam.writefile cpp, 
            %{extern "C" int __stdcall #{name}(#{args}) {
#{text}
}}
        system "g++ #{cpp} -shared -static -s -m32 -o #{outdll} -Wl,-add-stdcall-alias"
        open(outdll, "rb") do |f| f.read end
    end
    Gap::DLL.new(dll)[name]
end

#genlib(name, text) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/gap50/gap/cfunc.rb', line 23

def genlib(name, text)
    md5 = Gap::MD5.hexdigest(text)
    cpp = _file name + "_" + md5, ".cpp"
    outdll = _file name + "_" + md5, ".dll"
    dll = name + "_" + md5 + ".dll"
    output = @sam.genfile dll do
        @sam.writefile cpp, 
            %{#define GAPI(type) extern "C" type __stdcall
#{text}
}
        system "g++ #{cpp} -shared -static -s -m32 -o #{outdll} -Wl,-add-stdcall-alias"
        open(outdll, "rb") do |f| f.read end
    end
    Gap::DLL.new(dll)
end