forked from jarikomppa/soloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_rpgmaker.py
1280 lines (1158 loc) · 31.4 KB
/
gen_rpgmaker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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
119
120
121
122
123
124
125
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
""" SoLoud RPGMaker ruby wrapper generator """
import soloud_codegen
fo = open("../glue/rpgmaker_soloud.rb", "w")
# module SoLoudImporter
# extend DL::Importer
# dlload 'soloud_x86.dll'
#
# extern "char * foo(int, float)"
#
# CONSTANT = 23
# end
#
SOLOUD_TYPES = []
for x in soloud_codegen.soloud_type:
SOLOUD_TYPES.append(x + " *")
def pythonize_camelcase(origstr):
""" Turns camelCase into underscore_style """
ret = ""
for letter in origstr:
if letter.isupper():
ret += '_' + letter.lower()
else:
ret += letter
# kludge, because calc_f_f_t is silly.
ret = ret.replace("_f_f_t", "_fft")
# kludge for 3d calls
ret = ret.replace("3d", "_3d")
return ret
def has_ex_variant(funcname):
""" Checks if this function has an "Ex" variant """
if funcname[-2::] == "Ex":
# Already an Ex..
return False
for func in soloud_codegen.soloud_func:
if func[1] == (funcname + "Ex"):
return True
return False
fo.write("""
#===============================================================================
# SoLoud Wrapper for RPG Maker
#-----------------------------------------------------------------------------
# Author : Gabriel "Gab!" Teles
# License : ZLib/LibPNG
# Website : https://www.mundorpgmaker.com.br/
#-----------------------------------------------------------------------------
# Only joins "DL/*.rb" to SoLoud Ruby Wrapper in order to run on RPG Maker.
# SoLoud wrapper for Ruby
# This file is autogenerated; any changes will be overwritten
#===============================================================================
module DL
SEM = Mutex.new
def set_callback_internal(proc_entry, addr_entry, argc, ty, abi = nil, &cbp)
raise(ArgumentError, "arity should not be less than 0.") if (argc < 0)
addr = nil
SEM.synchronize{
ary = proc_entry[ty]
(0...MAX_CALLBACK).each{|n|
idx = (n * DLSTACK_SIZE) + argc
if( ary[idx].nil? )
ary[idx] = cbp
addr = addr_entry[ty][idx]
break
end
}
}
addr
end
def set_cdecl_callback(ty, argc, &cbp)
set_callback_internal(CdeclCallbackProcs, CdeclCallbackAddrs, argc, ty, &cbp)
end
def set_stdcall_callback(ty, argc, &cbp)
set_callback_internal(StdcallCallbackProcs, StdcallCallbackAddrs, argc, ty, &cbp)
end
def remove_callback_internal(proc_entry, addr_entry, addr, ctype = nil)
index = nil
if( ctype )
addr_entry[ctype].each_with_index{|xaddr, idx|
if( xaddr == addr )
index = idx
end
}
else
addr_entry.each{|ty,entry|
entry.each_with_index{|xaddr, idx|
if( xaddr == addr )
index = idx
end
}
}
end
if( index and proc_entry[ctype][index] )
proc_entry[ctype][index] = nil
return true
else
return false
end
end
def remove_cdecl_callback(addr, ctype = nil)
remove_callback_internal(CdeclCallbackProcs, CdeclCallbackAddrs, addr, ctype)
end
def remove_stdcall_callback(addr, ctype = nil)
remove_callback_internal(StdcallCallbackProcs, StdcallCallbackAddrs, addr, ctype)
end
alias set_callback set_cdecl_callback
alias remove_callback remove_cdecl_callback
class Stack
def self.[](*types)
new(types)
end
def initialize(types)
parse_types(types)
end
def size()
@size
end
def types()
@types
end
def pack(ary)
case SIZEOF_VOIDP
when SIZEOF_LONG
ary.pack(@template).unpack('l!*')
when SIZEOF_LONG_LONG
ary.pack(@template).unpack('q*')
else
raise(RuntimeError, "sizeof(void*)?")
end
end
def unpack(ary)
case SIZEOF_VOIDP
when SIZEOF_LONG
ary.pack('l!*').unpack(@template)
when SIZEOF_LONG_LONG
ary.pack('q*').unpack(@template)
else
raise(RuntimeError, "sizeof(void*)?")
end
end
private
def align(addr, align)
d = addr % align
if( d == 0 )
addr
else
addr + (align - d)
end
end
ALIGN_MAP = {
TYPE_VOIDP => ALIGN_VOIDP,
TYPE_CHAR => ALIGN_VOIDP,
TYPE_SHORT => ALIGN_VOIDP,
TYPE_INT => ALIGN_VOIDP,
TYPE_LONG => ALIGN_VOIDP,
TYPE_FLOAT => ALIGN_FLOAT,
TYPE_DOUBLE => ALIGN_DOUBLE,
}
PACK_MAP = {
TYPE_VOIDP => ((SIZEOF_VOIDP == SIZEOF_LONG_LONG)? "q" : "l!"),
TYPE_CHAR => "c",
TYPE_SHORT => "s!",
TYPE_INT => "i!",
TYPE_LONG => "l!",
TYPE_FLOAT => "f",
TYPE_DOUBLE => "d",
}
SIZE_MAP = {
TYPE_VOIDP => SIZEOF_VOIDP,
TYPE_CHAR => SIZEOF_CHAR,
TYPE_SHORT => SIZEOF_SHORT,
TYPE_INT => SIZEOF_INT,
TYPE_LONG => SIZEOF_LONG,
TYPE_FLOAT => SIZEOF_FLOAT,
TYPE_DOUBLE => SIZEOF_DOUBLE,
}
if defined?(TYPE_LONG_LONG)
ALIGN_MAP[TYPE_LONG_LONG] = ALIGN_LONG_LONG
PACK_MAP[TYPE_LONG_LONG] = "q"
SIZE_MAP[TYPE_LONG_LONG] = SIZEOF_LONG_LONG
end
def parse_types(types)
@types = types
@template = ""
addr = 0
types.each{|t|
addr = add_padding(addr, ALIGN_MAP[t])
@template << PACK_MAP[t]
addr += SIZE_MAP[t]
}
addr = add_padding(addr, ALIGN_MAP[SIZEOF_VOIDP])
if( addr % SIZEOF_VOIDP == 0 )
@size = addr / SIZEOF_VOIDP
else
@size = (addr / SIZEOF_VOIDP) + 1
end
end
def add_padding(addr, align)
orig_addr = addr
addr = align(orig_addr, align)
d = addr - orig_addr
if( d > 0 )
@template << "x#{d}"
end
addr
end
end
module PackInfo
ALIGN_MAP = {
TYPE_VOIDP => ALIGN_VOIDP,
TYPE_CHAR => ALIGN_CHAR,
TYPE_SHORT => ALIGN_SHORT,
TYPE_INT => ALIGN_INT,
TYPE_LONG => ALIGN_LONG,
TYPE_FLOAT => ALIGN_FLOAT,
TYPE_DOUBLE => ALIGN_DOUBLE,
-TYPE_CHAR => ALIGN_CHAR,
-TYPE_SHORT => ALIGN_SHORT,
-TYPE_INT => ALIGN_INT,
-TYPE_LONG => ALIGN_LONG,
}
PACK_MAP = {
TYPE_VOIDP => ((SIZEOF_VOIDP == SIZEOF_LONG_LONG) ? "q" : "l!"),
TYPE_CHAR => "c",
TYPE_SHORT => "s!",
TYPE_INT => "i!",
TYPE_LONG => "l!",
TYPE_FLOAT => "f",
TYPE_DOUBLE => "d",
-TYPE_CHAR => "c",
-TYPE_SHORT => "s!",
-TYPE_INT => "i!",
-TYPE_LONG => "l!",
}
SIZE_MAP = {
TYPE_VOIDP => SIZEOF_VOIDP,
TYPE_CHAR => SIZEOF_CHAR,
TYPE_SHORT => SIZEOF_SHORT,
TYPE_INT => SIZEOF_INT,
TYPE_LONG => SIZEOF_LONG,
TYPE_FLOAT => SIZEOF_FLOAT,
TYPE_DOUBLE => SIZEOF_DOUBLE,
-TYPE_CHAR => SIZEOF_CHAR,
-TYPE_SHORT => SIZEOF_SHORT,
-TYPE_INT => SIZEOF_INT,
-TYPE_LONG => SIZEOF_LONG,
}
if defined?(TYPE_LONG_LONG)
ALIGN_MAP[TYPE_LONG_LONG] = ALIGN_MAP[-TYPE_LONG_LONG] = ALIGN_LONG_LONG
PACK_MAP[TYPE_LONG_LONG] = PACK_MAP[-TYPE_LONG_LONG] = "q"
SIZE_MAP[TYPE_LONG_LONG] = SIZE_MAP[-TYPE_LONG_LONG] = SIZEOF_LONG_LONG
end
def align(addr, align)
d = addr % align
if( d == 0 )
addr
else
addr + (align - d)
end
end
module_function :align
end
class Packer
include PackInfo
def self.[](*types)
new(types)
end
def initialize(types)
parse_types(types)
end
def size()
@size
end
def pack(ary)
case SIZEOF_VOIDP
when SIZEOF_LONG
ary.pack(@template)
when SIZEOF_LONG_LONG
ary.pack(@template)
else
raise(RuntimeError, "sizeof(void*)?")
end
end
def unpack(ary)
case SIZEOF_VOIDP
when SIZEOF_LONG
ary.join().unpack(@template)
when SIZEOF_LONG_LONG
ary.join().unpack(@template)
else
raise(RuntimeError, "sizeof(void*)?")
end
end
private
def parse_types(types)
@template = ""
addr = 0
types.each{|t|
orig_addr = addr
if( t.is_a?(Array) )
addr = align(orig_addr, ALIGN_MAP[TYPE_VOIDP])
else
addr = align(orig_addr, ALIGN_MAP[t])
end
d = addr - orig_addr
if( d > 0 )
@template << "x#{d}"
end
if( t.is_a?(Array) )
@template << (PACK_MAP[t[0]] * t[1])
addr += (SIZE_MAP[t[0]] * t[1])
else
@template << PACK_MAP[t]
addr += SIZE_MAP[t]
end
}
addr = align(addr, ALIGN_MAP[TYPE_VOIDP])
@size = addr
end
end
module ValueUtil
def unsigned_value(val, ty)
case ty.abs
when TYPE_CHAR
[val].pack("c").unpack("C")[0]
when TYPE_SHORT
[val].pack("s!").unpack("S!")[0]
when TYPE_INT
[val].pack("i!").unpack("I!")[0]
when TYPE_LONG
[val].pack("l!").unpack("L!")[0]
when TYPE_LONG_LONG
[val].pack("q").unpack("Q")[0]
else
val
end
end
def signed_value(val, ty)
case ty.abs
when TYPE_CHAR
[val].pack("C").unpack("c")[0]
when TYPE_SHORT
[val].pack("S!").unpack("s!")[0]
when TYPE_INT
[val].pack("I!").unpack("i!")[0]
when TYPE_LONG
[val].pack("L!").unpack("l!")[0]
when TYPE_LONG_LONG
[val].pack("Q").unpack("q")[0]
else
val
end
end
def wrap_args(args, tys, funcs, &block)
result = []
tys ||= []
args.each_with_index{|arg, idx|
result.push(wrap_arg(arg, tys[idx], funcs, &block))
}
result
end
def wrap_arg(arg, ty, funcs = [], &block)
funcs ||= []
case arg
when nil
return 0
when CPtr
return arg.to_i
when IO
case ty
when TYPE_VOIDP
return CPtr[arg].to_i
else
return arg.to_i
end
when Function
if( block )
arg.bind_at_call(&block)
funcs.push(arg)
elsif !arg.bound?
raise(RuntimeError, "block must be given.")
end
return arg.to_i
when String
if( ty.is_a?(Array) )
return arg.unpack('C*')
else
case SIZEOF_VOIDP
when SIZEOF_LONG
return [arg].pack("p").unpack("l!")[0]
when SIZEOF_LONG_LONG
return [arg].pack("p").unpack("q")[0]
else
raise(RuntimeError, "sizeof(void*)?")
end
end
when Float, Integer
return arg
when Array
if( ty.is_a?(Array) ) # used only by struct
case ty[0]
when TYPE_VOIDP
return arg.collect{|v| Integer(v)}
when TYPE_CHAR
if( arg.is_a?(String) )
return val.unpack('C*')
end
end
return arg
else
return arg
end
else
if( arg.respond_to?(:to_ptr) )
return arg.to_ptr.to_i
else
begin
return Integer(arg)
rescue
raise(ArgumentError, "unknown argument type: #{arg.class}")
end
end
end
end
end
class Function < Object
include DL
include ValueUtil
def initialize cfunc, argtypes, abi = nil, &block
@cfunc = cfunc
@stack = Stack.new(argtypes.collect{|ty| ty.abs})
if( @cfunc.ctype < 0 )
@cfunc.ctype = @cfunc.ctype.abs
@unsigned = true
else
@unsigned = false
end
if block_given?
bind(&block)
end
end
def to_i()
@cfunc.to_i
end
def name
@cfunc.name
end
def call(*args, &block)
funcs = []
_args = wrap_args(args, @stack.types, funcs, &block)
r = @cfunc.call(@stack.pack(_args))
funcs.each{|f| f.unbind_at_call()}
return wrap_result(r)
end
def wrap_result(r)
case @cfunc.ctype
when TYPE_VOIDP
r = CPtr.new(r)
else
if( @unsigned )
r = unsigned_value(r, @cfunc.ctype)
end
end
r
end
def bind(&block)
if( !block )
raise(RuntimeError, "block must be given.")
end
if( @cfunc.ptr == 0 )
cb = Proc.new{|*args|
ary = @stack.unpack(args)
@stack.types.each_with_index{|ty, idx|
case ty
when TYPE_VOIDP
ary[idx] = CPtr.new(ary[idx])
end
}
r = block.call(*ary)
wrap_arg(r, @cfunc.ctype, [])
}
case @cfunc.calltype
when :cdecl
@cfunc.ptr = set_cdecl_callback(@cfunc.ctype, @stack.size, &cb)
when :stdcall
@cfunc.ptr = set_stdcall_callback(@cfunc.ctype, @stack.size, &cb)
else
raise(RuntimeError, "unsupported calltype: #{@cfunc.calltype}")
end
if( @cfunc.ptr == 0 )
raise(RuntimeException, "can't bind C function.")
end
end
end
def unbind()
if( @cfunc.ptr != 0 )
case @cfunc.calltype
when :cdecl
remove_cdecl_callback(@cfunc.ptr, @cfunc.ctype)
when :stdcall
remove_stdcall_callback(@cfunc.ptr, @cfunc.ctype)
else
raise(RuntimeError, "unsupported calltype: #{@cfunc.calltype}")
end
@cfunc.ptr = 0
end
end
def bound?()
@cfunc.ptr != 0
end
def bind_at_call(&block)
bind(&block)
end
def unbind_at_call()
end
end
class TempFunction < Function
def bind_at_call(&block)
bind(&block)
end
def unbind_at_call()
unbind()
end
end
class CarriedFunction < Function
def initialize(cfunc, argtypes, n)
super(cfunc, argtypes)
@carrier = []
@index = n
@mutex = Mutex.new
end
def create_carrier(data)
ary = []
userdata = [ary, data]
@mutex.lock()
@carrier.push(userdata)
return dlwrap(userdata)
end
def bind_at_call(&block)
userdata = @carrier[-1]
userdata[0].push(block)
bind{|*args|
ptr = args[@index]
if( !ptr )
raise(RuntimeError, "The index of userdata should be lower than #{args.size}.")
end
userdata = dlunwrap(Integer(ptr))
args[@index] = userdata[1]
userdata[0][0].call(*args)
}
@mutex.unlock()
end
end
class CStruct
def CStruct.entity_class()
CStructEntity
end
end
class CUnion
def CUnion.entity_class()
CUnionEntity
end
end
module CStructBuilder
def create(klass, types, members)
new_class = Class.new(klass){
define_method(:initialize){|addr|
@entity = klass.entity_class.new(addr, types)
@entity.assign_names(members)
}
define_method(:to_ptr){ @entity }
define_method(:to_i){ @entity.to_i }
members.each{|name|
define_method(name){ @entity[name] }
define_method(name + "="){|val| @entity[name] = val }
}
}
size = klass.entity_class.size(types)
new_class.module_eval(<<-EOS, __FILE__, __LINE__+1)
def new_class.size()
#{size}
end
def new_class.malloc()
addr = DL.malloc(#{size})
new(addr)
end
EOS
return new_class
end
module_function :create
end
class CStructEntity < CPtr
include PackInfo
include ValueUtil
def CStructEntity.malloc(types, func = nil)
addr = DL.malloc(CStructEntity.size(types))
CStructEntity.new(addr, types, func)
end
def CStructEntity.size(types)
offset = 0
max_align = 0
types.each_with_index{|t,i|
orig_offset = offset
if( t.is_a?(Array) )
align = PackInfo::ALIGN_MAP[t[0]]
offset = PackInfo.align(orig_offset, align)
size = offset - orig_offset
offset += (PackInfo::SIZE_MAP[t[0]] * t[1])
else
align = PackInfo::ALIGN_MAP[t]
offset = PackInfo.align(orig_offset, align)
size = offset - orig_offset
offset += PackInfo::SIZE_MAP[t]
end
if (max_align < align)
max_align = align
end
}
offset = PackInfo.align(offset, max_align)
offset
end
def initialize(addr, types, func = nil)
set_ctypes(types)
super(addr, @size, func)
end
def assign_names(members)
@members = members
end
def set_ctypes(types)
@ctypes = types
@offset = []
offset = 0
max_align = 0
types.each_with_index{|t,i|
orig_offset = offset
if( t.is_a?(Array) )
align = ALIGN_MAP[t[0]]
else
align = ALIGN_MAP[t]
end
offset = PackInfo.align(orig_offset, align)
@offset[i] = offset
if( t.is_a?(Array) )
offset += (SIZE_MAP[t[0]] * t[1])
else
offset += SIZE_MAP[t]
end
if (max_align < align)
max_align = align
end
}
offset = PackInfo.align(offset, max_align)
@size = offset
end
def [](name)
idx = @members.index(name)
if( idx.nil? )
raise(ArgumentError, "no such member: #{name}")
end
ty = @ctypes[idx]
if( ty.is_a?(Array) )
r = super(@offset[idx], SIZE_MAP[ty[0]] * ty[1])
else
r = super(@offset[idx], SIZE_MAP[ty.abs])
end
packer = Packer.new([ty])
val = packer.unpack([r])
case ty
when Array
case ty[0]
when TYPE_VOIDP
val = val.collect{|v| CPtr.new(v)}
end
when TYPE_VOIDP
val = CPtr.new(val[0])
else
val = val[0]
end
if( ty.is_a?(Integer) && (ty < 0) )
return unsigned_value(val, ty)
elsif( ty.is_a?(Array) && (ty[0] < 0) )
return val.collect{|v| unsigned_value(v,ty[0])}
else
return val
end
end
def []=(name, val)
idx = @members.index(name)
if( idx.nil? )
raise(ArgumentError, "no such member: #{name}")
end
ty = @ctypes[idx]
packer = Packer.new([ty])
val = wrap_arg(val, ty, [])
buff = packer.pack([val].flatten())
super(@offset[idx], buff.size, buff)
if( ty.is_a?(Integer) && (ty < 0) )
return unsigned_value(val, ty)
elsif( ty.is_a?(Array) && (ty[0] < 0) )
return val.collect{|v| unsigned_value(v,ty[0])}
else
return val
end
end
def to_s()
super(@size)
end
end
class CUnionEntity < CStructEntity
include PackInfo
def CUnionEntity.malloc(types, func=nil)
addr = DL.malloc(CUnionEntity.size(types))
CUnionEntity.new(addr, types, func)
end
def CUnionEntity.size(types)
size = 0
types.each_with_index{|t,i|
if( t.is_a?(Array) )
tsize = PackInfo::SIZE_MAP[t[0]] * t[1]
else
tsize = PackInfo::SIZE_MAP[t]
end
if( tsize > size )
size = tsize
end
}
end
def set_ctypes(types)
@ctypes = types
@offset = []
@size = 0
types.each_with_index{|t,i|
@offset[i] = 0
if( t.is_a?(Array) )
size = SIZE_MAP[t[0]] * t[1]
else
size = SIZE_MAP[t]
end
if( size > @size )
@size = size
end
}
end
end
module CParser
def parse_struct_signature(signature, tymap=nil)
if( signature.is_a?(String) )
signature = signature.split(/\s*,\s*/)
end
mems = []
tys = []
signature.each{|msig|
tks = msig.split(/\s+(\*)?/)
ty = tks[0..-2].join(" ")
member = tks[-1]
case ty
when /\[(\d+)\]/
n = $1.to_i
ty.gsub!(/\s*\[\d+\]/,"")
ty = [ty, n]
when /\[\]/
ty.gsub!(/\s*\[\]/, "*")
end
case member
when /\[(\d+)\]/
ty = [ty, $1.to_i]
member.gsub!(/\s*\[\d+\]/,"")
when /\[\]/
ty = ty + "*"
member.gsub!(/\s*\[\]/, "")
end
mems.push(member)
tys.push(parse_ctype(ty,tymap))
}
return tys, mems
end
def parse_signature(signature, tymap=nil)
tymap ||= {}
signature = signature.gsub(/\s+/, " ").strip
case signature
when /^([\w@\*\s]+)\(([\w\*\s\,\[\]]*)\)$/
ret = $1
(args = $2).strip!
ret = ret.split(/\s+/)
args = args.split(/\s*,\s*/)
func = ret.pop
if( func =~ /^\*/ )
func.gsub!(/^\*+/,"")
ret.push("*")
end
ret = ret.join(" ")
return [func, parse_ctype(ret, tymap), args.collect{|arg| parse_ctype(arg, tymap)}]
else
raise(RuntimeError,"can't parse the function prototype: #{signature}")
end
end
def parse_ctype(ty, tymap=nil)
tymap ||= {}
case ty
when Array
return [parse_ctype(ty[0], tymap), ty[1]]
when "void"
return TYPE_VOID
when "char"
return TYPE_CHAR
when "unsigned char"
return -TYPE_CHAR
when "short"
return TYPE_SHORT
when "unsigned short"
return -TYPE_SHORT
when "int"
return TYPE_INT
when "unsigned int", 'uint'
return -TYPE_INT
when "long"
return TYPE_LONG
when "unsigned long"
return -TYPE_LONG
when "long long"
if( defined?(TYPE_LONG_LONG) )
return TYPE_LONG_LONG
else
raise(RuntimeError, "unsupported type: #{ty}")
end
when "unsigned long long"
if( defined?(TYPE_LONG_LONG) )
return -TYPE_LONG_LONG
else
raise(RuntimeError, "unsupported type: #{ty}")
end
when "float"
return TYPE_FLOAT
when "double"
return TYPE_DOUBLE
when /\*/, /\[\s*\]/
return TYPE_VOIDP
else
if( tymap[ty] )
return parse_ctype(tymap[ty], tymap)
else
raise(DLError, "unknown type: #{ty}")
end
end
end
end
class CompositeHandler
def initialize(handlers)
@handlers = handlers
end
def handlers()
@handlers
end
def sym(symbol)
@handlers.each{|handle|
if( handle )
begin
addr = handle.sym(symbol)
return addr
rescue DLError
end
end
}
return nil
end
def [](symbol)
sym(symbol)
end
end
module Importer
include DL
include CParser
extend Importer
def dlload(*libs)
handles = libs.collect{|lib|
case lib
when nil
nil
when Handle
lib
when Importer
lib.handlers
else
begin
DL.dlopen(lib)
rescue DLError
raise(DLError, "can't load #{lib}")
end
end
}.flatten()
@handler = CompositeHandler.new(handles)
@func_map = {}
@type_alias = {}
end
def typealias(alias_type, orig_type)
@type_alias[alias_type] = orig_type
end
def sizeof(ty)
case ty
when String
ty = parse_ctype(ty, @type_alias).abs()
case ty
when TYPE_CHAR
return SIZEOF_CHAR
when TYPE_SHORT
return SIZEOF_SHORT
when TYPE_INT
return SIZEOF_INT