gogyou 0.2 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: be64eff572c8de86fa028cf2b0a3f43ced61f7fc
4
- data.tar.gz: 60ba38f8395e03b59c06053f915e255e151009ac
3
+ metadata.gz: c1f4c10b0e249b1b9897b5b1c317e0ef5e535830
4
+ data.tar.gz: c8ccad916b49f45e18d91bff8538cf1b7e5b6377
5
5
  SHA512:
6
- metadata.gz: 1b23fd8023dc3210c51228f0849a92aa541d4621a11e5ee4d57086e30458ede76d4b733cbbed1183667f5a2874964632c902e12017a8121aa17a91370124e4e8
7
- data.tar.gz: 08f775733ffe50d57ebbac3dc48c6bc2420dd112e8e6e5c8eb3bb3ae7dbaa7d91bb6fe607c2c1f38abbad56962bc9883f717fe132c1fafed942324a1220dd844
6
+ metadata.gz: b73dba83766abfce2f63ba2daad894fdf3b7560ed20bfa613d9d388aa707f053dac67f8133f6fe76002eb0958824d8b6cec23fc1a05b58228244d1ab0cfdb503
7
+ data.tar.gz: 93b5fb1d45270650ff47c10746ec94ddf0b44c59751b8f035af841902a0fc807f02e2c39820447ca9d055aeafa1abc4ee1ab78a9e2f72383e5d7db0a7e325fd1
@@ -141,7 +141,7 @@
141
141
  #
142
142
  module Gogyou
143
143
  Gogyou = self
144
- VERSION = Gem::Version.new("0.2")
144
+ VERSION = Gem::Version.new("0.2.1")
145
145
 
146
146
  require_relative "gogyou/typespec"
147
147
  require_relative "gogyou/mixin"
@@ -148,6 +148,9 @@ module Gogyou
148
148
  def self.define_accessors(accessorclass, model)
149
149
  accessorclass.class_eval do
150
150
  namecheck = {}
151
+ fieldsize = model.fields.size
152
+ define_method(:size__GOGYOU__, -> { fieldsize })
153
+ alias_method(:size, :size__GOGYOU__)
151
154
  model.fields.each do |field|
152
155
  name = field.name
153
156
  #raise NameError, "wrong field name - #{name}" unless name =~ /\A[A-Za-z_][A-Za-z_0-9]*\Z/
@@ -267,13 +270,13 @@ module Gogyou
267
270
  bytesize = type.bytesize
268
271
 
269
272
  if model.bytesize == 0
270
- check_index = ->(index) do
273
+ define_method(:check_index, ->(index) {
271
274
  index = index.to_i
272
275
  unless index >= 0 && index < self.size
273
276
  raise IndexError, "out of element size (index #{index} for 0 ... #{self.size})", caller(2)
274
277
  end
275
278
  index
276
- end
279
+ })
277
280
 
278
281
  define_method(:<<, ->(value) {
279
282
  raise TypeError, "immutable object (#<%s:0x%08X>)" % [self.class, __id__], caller(2) if frozen?
@@ -292,27 +295,31 @@ module Gogyou
292
295
  (buffer__GOGYOU__.bytesize - offset__GOGYOU__).align_floor(type.bytesize)
293
296
  })
294
297
  else
295
- check_index = ->(index) do
296
- index = index.to_i
297
- unless index >= 0 && (elements.nil? || index < elements)
298
- raise IndexError, "out of element size (index #{index} for 0 ... #{elements})", caller(2)
298
+ eval <<-EOS
299
+ def check_index(index)
300
+ index = index.to_i
301
+ unless index >= 0 && (#{elements.nil?} || index < #{elements})
302
+ raise IndexError, "out of element size (index \#{index} for 0 ... #{elements})", caller
303
+ end
304
+ index
299
305
  end
300
- index
301
- end
302
306
 
303
- eval <<-EOS
304
- def bytesize
307
+ def size
305
308
  #{elements}
306
309
  end
310
+
311
+ def bytesize
312
+ #{type.bytesize * elements}
313
+ end
307
314
  EOS
308
315
  end
309
316
 
310
317
  define_method(:to_s, -> {
311
- buffer__GOGYOU__.byteslice(offset__GOGYOU__, bytesize)
318
+ buffer__GOGYOU__.byteslice(offset__GOGYOU__, self.bytesize)
312
319
  })
313
320
 
314
321
  define_method(:[], ->(index) {
315
- v = type.aref(buffer__GOGYOU__, offset__GOGYOU__ + check_index.(index) * bytesize)
322
+ v = type.aref(buffer__GOGYOU__, offset__GOGYOU__ + check_index(index) * bytesize)
316
323
  v.infect_from(self, buffer) unless v.frozen?
317
324
  v.freeze if frozen? || buffer.frozen? || field.const?
318
325
  v
@@ -320,7 +327,7 @@ module Gogyou
320
327
 
321
328
  define_method(:[]=, ->(index, value) {
322
329
  raise TypeError, "immutable object (#<%s:0x%08X>)" % [self.class, __id__, index], caller(2) if frozen? or field.const?
323
- type.aset(buffer__GOGYOU__, offset__GOGYOU__ + check_index.(index) * bytesize, value)
330
+ type.aset(buffer__GOGYOU__, offset__GOGYOU__ + check_index(index) * bytesize, value)
324
331
  })
325
332
  end
326
333
  klass
@@ -7,7 +7,7 @@ module Gogyou
7
7
  module Object
8
8
  module Mixin
9
9
  def infect_from(*obj)
10
- obj.each { |o| taint if o.tainted?; untrust if o.untrusted? }
10
+ obj.each { |o| taint if o.tainted? }
11
11
  self
12
12
  end
13
13
  end
@@ -136,7 +136,6 @@ module Gogyou
136
136
 
137
137
  def swapbyte(bytesize)
138
138
  num = 0
139
- mask = 0xff
140
139
  bytesize.times do |i|
141
140
  num <<= 8
142
141
  num |= (self >> (i * 8)) & 0xff
@@ -117,15 +117,21 @@ end
117
117
  describe Gogyou::Accessor do
118
118
  it "fixed bytesize struct" do
119
119
  type = Gogyou.struct {
120
- int32_t :a, :b, 2, 2
120
+ char :a, 16
121
+ int32_t :b
122
+ int8_t :c
121
123
  }
122
124
 
123
- expect(type.bytesize).to eq 20
125
+ expect(type.bytesize).to eq 24
124
126
  expect(type.bytealign).to eq 4
125
127
  expect(type.extensible?).to eq false
126
128
 
127
- obj = type.new
128
- expect(obj.bytesize).to eq 20
129
+ obj = type.bind((?a .. ?z).to_a.join)
130
+ expect(obj.bytesize).to eq 24
131
+ expect(obj.size).to eq 3
132
+ expect(obj.a.bytesize).to eq 16
133
+ expect(obj.a[1]).to eq ?b.ord
134
+ expect(obj.a.to_s).to eq "abcdefghijklmnop"
129
135
  end
130
136
 
131
137
  it "extensibity struct" do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gogyou
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.2'
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - dearblue
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-09-06 00:00:00.000000000 Z
11
+ date: 2014-09-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -57,8 +57,8 @@ extra_rdoc_files:
57
57
  - lib/gogyou/accessor.rb
58
58
  - lib/gogyou/mixin.rb
59
59
  - lib/gogyou/model.rb
60
- - lib/gogyou/typespec.rb
61
60
  - lib/gogyou/primitives.rb
61
+ - lib/gogyou/typespec.rb
62
62
  files:
63
63
  - LICENSE.markdown
64
64
  - README.markdown