wardite 0.4.1 → 0.4.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '02079b17ada50d21f00ea34c1830452eea21ff12a2fb6e9c5903d07cdca53d3a'
4
- data.tar.gz: 9ab24caf2cc630593ce32ab652c8044a621f67ddce1cb70f75a1cd9e767b13e2
3
+ metadata.gz: f1960efc493205587db2fbb0e21504247d120831417b081dc169c0b7c10fccaf
4
+ data.tar.gz: cbdbf5e122fe118ccee7a615b184be2032b6000a043b3642a9b5336f6b0fbe19
5
5
  SHA512:
6
- metadata.gz: 88054ffba3884002ea3ff7c63b21a38ff41a93f2e641ec7b82cf39f47e7df7672c5c6412f4efd1ca9feb6e222aaaadf98e67a40810c8ed213046df348aac8f79
7
- data.tar.gz: 1c5b8765bff6ae42eb00c696efd8af2c6b892911d52fc68a1124fb3804419f121f886ba416b6b39b3183afa22e1f6aadf730681c004f254796c5e6934ab39da1
6
+ metadata.gz: 207a1cd23ab5f25fdf9dd421ab42c70528be1987e57f41d656f47e205ee78ba321b9553364f988f6f039f87a77878e040075e690b00b97ace0bf91e35c07431f
7
+ data.tar.gz: 944b8112be80c9c3b78dd7cdec01ac3c72f7e4e077196a315bfaa14ffeb4dfa6cdb868570ae0bc55fb4f07da858f968807fee8bb50d5846fa763dd7b82359f98
data/README.md CHANGED
@@ -43,7 +43,7 @@ path = ARGV[0]
43
43
  method = ARGV[1]
44
44
  args = ARGV[2..-1] || []
45
45
 
46
- instance = Wardite::new(path: path);
46
+ instance = Wardite::new(path: path)
47
47
  if !method && instance.runtime.respond_to?(:_start)
48
48
  instance.runtime._start
49
49
  else
@@ -58,7 +58,7 @@ module Wardite
58
58
  runtime.stack.push(to)
59
59
 
60
60
 
61
- when :i32_extend_8_s
61
+ when :i32_extend8_s
62
62
  from = runtime.stack.pop
63
63
  raise EvalError, "maybe empty or invalid stack" if !from.is_a?(I32)
64
64
  to = from.extendN_s(to: :i32, from: :i8)
@@ -66,7 +66,7 @@ module Wardite
66
66
  runtime.stack.push(to)
67
67
 
68
68
 
69
- when :i32_extend_16_s
69
+ when :i32_extend16_s
70
70
  from = runtime.stack.pop
71
71
  raise EvalError, "maybe empty or invalid stack" if !from.is_a?(I32)
72
72
  to = from.extendN_s(to: :i32, from: :i16)
@@ -178,7 +178,7 @@ module Wardite
178
178
  runtime.stack.push(to)
179
179
 
180
180
 
181
- when :i64_extend_8_s
181
+ when :i64_extend8_s
182
182
  from = runtime.stack.pop
183
183
  raise EvalError, "maybe empty or invalid stack" if !from.is_a?(I32)
184
184
  to = from.extendN_s(to: :i64, from: :i8)
@@ -186,7 +186,7 @@ module Wardite
186
186
  runtime.stack.push(to)
187
187
 
188
188
 
189
- when :i64_extend_16_s
189
+ when :i64_extend16_s
190
190
  from = runtime.stack.pop
191
191
  raise EvalError, "maybe empty or invalid stack" if !from.is_a?(I32)
192
192
  to = from.extendN_s(to: :i64, from: :i16)
@@ -194,7 +194,7 @@ module Wardite
194
194
  runtime.stack.push(to)
195
195
 
196
196
 
197
- when :i64_extend_32_s
197
+ when :i64_extend32_s
198
198
  from = runtime.stack.pop
199
199
  raise EvalError, "maybe empty or invalid stack" if !from.is_a?(I32)
200
200
  to = from.extendN_s(to: :i64, from: :i32)
data/lib/wardite/load.rb CHANGED
@@ -193,7 +193,7 @@ module Wardite
193
193
 
194
194
  attr_accessor :kind #: Integer
195
195
 
196
- attr_accessor :func_index #: Integer
196
+ attr_accessor :index #: Integer
197
197
  end
198
198
 
199
199
  attr_accessor :exports #: Hash[String, ExportDesc]
@@ -840,7 +840,7 @@ module Wardite
840
840
  dest.add_desc do |desc|
841
841
  desc.name = name
842
842
  desc.kind = kind
843
- desc.func_index = index
843
+ desc.index = index
844
844
  end
845
845
  end
846
846
 
data/lib/wardite/value.rb CHANGED
@@ -237,6 +237,10 @@ module Wardite
237
237
  def inspect
238
238
  "I32(#{value_s})"
239
239
  end
240
+
241
+ def ==(other)
242
+ return self.class == other.class && self.value == other.value
243
+ end
240
244
  end
241
245
 
242
246
  class I64
@@ -400,6 +404,10 @@ module Wardite
400
404
  def inspect
401
405
  "I64(#{@value})"
402
406
  end
407
+
408
+ def ==(other)
409
+ return self.class == other.class && self.value == other.value
410
+ end
403
411
  end
404
412
 
405
413
  class F32
@@ -601,6 +609,10 @@ module Wardite
601
609
  def inspect
602
610
  "F32(#{@value})"
603
611
  end
612
+
613
+ def ==(other)
614
+ return self.class == other.class && self.value == other.value
615
+ end
604
616
  end
605
617
 
606
618
  class F64
@@ -800,5 +812,9 @@ module Wardite
800
812
  def inspect
801
813
  "F64(#{@value})"
802
814
  end
815
+
816
+ def ==(other)
817
+ return self.class == other.class && self.value == other.value
818
+ end
803
819
  end
804
820
  end
@@ -2,5 +2,5 @@
2
2
  # rbs_inline: enabled
3
3
 
4
4
  module Wardite
5
- VERSION = "0.4.1" #: String
5
+ VERSION = "0.4.3" #: String
6
6
  end
data/lib/wardite.rb CHANGED
@@ -258,10 +258,13 @@ module Wardite
258
258
  if !callable?(name)
259
259
  raise ::NoMethodError, "function #{name} not found"
260
260
  end
261
- kind, fn = @instance.exports[name.to_s]
261
+ kind, fn = @instance.exports.mappings[name.to_s]
262
262
  if kind != 0
263
263
  raise ::NoMethodError, "#{name} is not a function"
264
264
  end
265
+ if !fn.is_a?(WasmFunction) && !fn.is_a?(ExternalFunction)
266
+ raise ::NoMethodError, "#{name} is not a function"
267
+ end
265
268
  if fn.callsig.size != args.size
266
269
  raise ArgumentError, "unmatch arg size"
267
270
  end
@@ -310,20 +313,28 @@ module Wardite
310
313
  end
311
314
  self.stack = drained_stack(local_start)
312
315
 
313
- wasm_function.locals_type.each_with_index do |typ, i|
314
- case typ
315
- when :i32, :u32
316
- locals.push I32(0)
317
- when :i64, :u64
318
- locals.push I64(0)
319
- else
320
- $stderr.puts "warning: unknown type #{typ.inspect}. default to I32"
321
- locals.push I32(0)
316
+ wasm_function.locals_count.each_with_index do |count, i|
317
+ typ = wasm_function.locals_type[i]
318
+ count.times do
319
+ case typ
320
+ when :i32, :u32
321
+ locals.push I32(0)
322
+ when :i64, :u64
323
+ locals.push I64(0)
324
+ when :f32
325
+ locals.push F32(0.0)
326
+ when :f64
327
+ locals.push F64(0.0)
328
+ else
329
+ $stderr.puts "warning: unknown type #{typ.inspect}. default to I32"
330
+ locals.push I32(0)
331
+ end
322
332
  end
323
333
  end
324
334
 
325
335
  arity = wasm_function.retsig.size
326
336
  frame = Frame.new(-1, stack.size, wasm_function.body, arity, locals)
337
+ frame.findex = wasm_function.findex
327
338
  self.call_stack.push(frame)
328
339
  end
329
340
 
@@ -593,7 +604,8 @@ module Wardite
593
604
  end
594
605
  local = frame.locals[idx]
595
606
  if !local
596
- raise EvalError, "local not found"
607
+ # require "irb"; binding.irb
608
+ raise EvalError, "local not found, idx = #{idx}"
597
609
  end
598
610
  stack.push(local)
599
611
 
@@ -718,10 +730,12 @@ module Wardite
718
730
  end
719
731
 
720
732
  rescue => e
721
- require "pp"
722
- $stderr.puts "instance:::\n#{self.instance.pretty_inspect}"
723
- $stderr.puts "frame:::\n#{frame.pretty_inspect}"
724
- $stderr.puts "stack:::\n#{stack.pretty_inspect}"
733
+ if ENV["DEBUG"]
734
+ require "pp"
735
+ $stderr.puts "instance:::\n#{self.instance.pretty_inspect}"
736
+ $stderr.puts "frame:::\n#{frame.pretty_inspect}"
737
+ $stderr.puts "stack:::\n#{stack.pretty_inspect}"
738
+ end
725
739
  raise e
726
740
  end
727
741
 
@@ -884,6 +898,8 @@ module Wardite
884
898
 
885
899
  attr_accessor :locals #: Array[wasmValue]
886
900
 
901
+ attr_accessor :findex #: Integer
902
+
887
903
  # @rbs pc: Integer
888
904
  # @rbs sp: Integer
889
905
  # @rbs body: Array[Op]
@@ -897,6 +913,8 @@ module Wardite
897
913
  @arity = arity
898
914
  @locals = locals
899
915
  @labels = []
916
+
917
+ @findex = 0
900
918
  end
901
919
  end
902
920
 
@@ -970,6 +988,8 @@ module Wardite
970
988
  retsig = type_section.defined_results[sigindex]
971
989
  codes = code_section.func_codes[findex]
972
990
  wasm_function = WasmFunction.new(callsig, retsig, codes)
991
+ idx = self.funcs.size
992
+ wasm_function.findex = idx
973
993
  self.funcs << wasm_function
974
994
  end
975
995
  end
@@ -1204,8 +1224,11 @@ module Wardite
1204
1224
  end
1205
1225
  end
1206
1226
 
1227
+ # @rbs!
1228
+ # type exportHandle = WasmFunction | ExternalFunction | Table | Global | Memory
1229
+
1207
1230
  class Exports
1208
- attr_accessor :mappings #: Hash[String, [Integer, WasmFunction|ExternalFunction]]
1231
+ attr_accessor :mappings #: Hash[String, [Integer, exportHandle]]
1209
1232
 
1210
1233
  # @rbs export_section: ExportSection
1211
1234
  # @rbs store: Store
@@ -1213,15 +1236,36 @@ module Wardite
1213
1236
  def initialize(export_section, store)
1214
1237
  @mappings = {}
1215
1238
  export_section.exports.each_pair do |name, desc|
1216
- # TODO: introduce map by kind
1217
- @mappings[name] = [desc.kind, store.funcs[desc.func_index]]
1239
+ case desc.kind
1240
+ when 0x0
1241
+ @mappings[name] = [desc.kind, store.funcs[desc.index]]
1242
+ when 0x1
1243
+ @mappings[name] = [desc.kind, store.tables[desc.index]]
1244
+ when 0x2
1245
+ @mappings[name] = [desc.kind, store.memories[desc.index]]
1246
+ when 0x3
1247
+ @mappings[name] = [desc.kind, store.globals[desc.index]]
1248
+ else
1249
+ end
1218
1250
  end
1219
1251
  end
1220
1252
 
1221
1253
  # @rbs name: String
1222
- # @rbs return: [Integer, WasmFunction|ExternalFunction]
1254
+ # @rbs return: exportHandle|nil
1223
1255
  def [](name)
1224
- @mappings[name]
1256
+ @mappings[name]&.[](1)
1257
+ end
1258
+
1259
+ def respond_to?(name)
1260
+ !!self[name.to_s] || super
1261
+ end
1262
+
1263
+ def method_missing(name, *_args)
1264
+ if self[name.to_s]
1265
+ self[name.to_s]
1266
+ else
1267
+ super
1268
+ end
1225
1269
  end
1226
1270
  end
1227
1271
 
@@ -1233,6 +1277,8 @@ module Wardite
1233
1277
 
1234
1278
  attr_accessor :code_body #: CodeSection::CodeBody
1235
1279
 
1280
+ attr_accessor :findex #: Integer
1281
+
1236
1282
  # @rbs callsig: Array[Symbol]
1237
1283
  # @rbs retsig: Array[Symbol]
1238
1284
  # @rbs code_body: CodeSection::CodeBody
@@ -1242,6 +1288,7 @@ module Wardite
1242
1288
  @retsig = retsig
1243
1289
 
1244
1290
  @code_body = code_body
1291
+ @findex = 0 # for debug
1245
1292
  end
1246
1293
 
1247
1294
  # @rbs return: Array[Op]
data/scripts/gen_conv.rb CHANGED
@@ -46,7 +46,7 @@ module GenConv
46
46
  elsif method =~ /^extendN_(u|s)$/
47
47
  suffix = $1
48
48
  from_size = from.to_s.scan(/\d+/).join
49
- symbol = "#{to.to_s}_extend_#{from_size}_#{suffix}"
49
+ symbol = "#{to.to_s}_extend#{from_size}_#{suffix}"
50
50
  extra_kargs = ", from: :#{from.to_s}"
51
51
  elsif method.end_with?("_s") or method.end_with?("_u")
52
52
  core = method.sub(/_(s|u)$/, "")
@@ -136,7 +136,7 @@ module Wardite
136
136
 
137
137
  attr_accessor kind: Integer
138
138
 
139
- attr_accessor func_index: Integer
139
+ attr_accessor index: Integer
140
140
  end
141
141
 
142
142
  attr_accessor exports: Hash[String, ExportDesc]
@@ -115,6 +115,8 @@ module Wardite
115
115
 
116
116
  # I32#inspect shows signed value for convinience
117
117
  def inspect: () -> untyped
118
+
119
+ def ==: (untyped other) -> untyped
118
120
  end
119
121
 
120
122
  class I64
@@ -196,6 +198,8 @@ module Wardite
196
198
 
197
199
  # I64#inspect shows signed value
198
200
  def inspect: () -> untyped
201
+
202
+ def ==: (untyped other) -> untyped
199
203
  end
200
204
 
201
205
  class F32
@@ -278,6 +282,8 @@ module Wardite
278
282
  def trunc_sat_s: (to: Symbol) -> wasmValue
279
283
 
280
284
  def inspect: () -> untyped
285
+
286
+ def ==: (untyped other) -> untyped
281
287
  end
282
288
 
283
289
  class F64
@@ -358,5 +364,7 @@ module Wardite
358
364
  def trunc_sat_s: (to: Symbol) -> wasmValue
359
365
 
360
366
  def inspect: () -> untyped
367
+
368
+ def ==: (untyped other) -> untyped
361
369
  end
362
370
  end
@@ -175,6 +175,8 @@ module Wardite
175
175
 
176
176
  attr_accessor locals: Array[wasmValue]
177
177
 
178
+ attr_accessor findex: Integer
179
+
178
180
  # @rbs pc: Integer
179
181
  # @rbs sp: Integer
180
182
  # @rbs body: Array[Op]
@@ -313,8 +315,10 @@ module Wardite
313
315
  def result_size: () -> Integer
314
316
  end
315
317
 
318
+ type exportHandle = WasmFunction | ExternalFunction | Table | Global | Memory
319
+
316
320
  class Exports
317
- attr_accessor mappings: Hash[String, [ Integer, WasmFunction | ExternalFunction ]]
321
+ attr_accessor mappings: Hash[String, [ Integer, exportHandle ]]
318
322
 
319
323
  # @rbs export_section: ExportSection
320
324
  # @rbs store: Store
@@ -322,8 +326,12 @@ module Wardite
322
326
  def initialize: (ExportSection export_section, Store store) -> void
323
327
 
324
328
  # @rbs name: String
325
- # @rbs return: [Integer, WasmFunction|ExternalFunction]
326
- def []: (String name) -> [ Integer, WasmFunction | ExternalFunction ]
329
+ # @rbs return: exportHandle|nil
330
+ def []: (String name) -> (exportHandle | nil)
331
+
332
+ def respond_to?: (untyped name) -> untyped
333
+
334
+ def method_missing: (untyped name, *untyped _args) -> untyped
327
335
  end
328
336
 
329
337
  # TODO: common interface btw. WasmFunction and ExternalFunction?
@@ -334,6 +342,8 @@ module Wardite
334
342
 
335
343
  attr_accessor code_body: CodeSection::CodeBody
336
344
 
345
+ attr_accessor findex: Integer
346
+
337
347
  # @rbs callsig: Array[Symbol]
338
348
  # @rbs retsig: Array[Symbol]
339
349
  # @rbs code_body: CodeSection::CodeBody
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wardite
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.4.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Uchio Kondo
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-11-10 00:00:00.000000000 Z
11
+ date: 2024-11-17 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A pure-ruby webassembly runtime
14
14
  email: