wardite 0.4.3 → 0.5.0

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
  SHA256:
3
- metadata.gz: f1960efc493205587db2fbb0e21504247d120831417b081dc169c0b7c10fccaf
4
- data.tar.gz: cbdbf5e122fe118ccee7a615b184be2032b6000a043b3642a9b5336f6b0fbe19
3
+ metadata.gz: e3ca81e45a3da7f90566d465d5248bcac03981953e97c81681de900851348407
4
+ data.tar.gz: 2b0952f92a92bce4d54283868fd2565b9dbaa178cc1187b2a7f6feca6225fe57
5
5
  SHA512:
6
- metadata.gz: 207a1cd23ab5f25fdf9dd421ab42c70528be1987e57f41d656f47e205ee78ba321b9553364f988f6f039f87a77878e040075e690b00b97ace0bf91e35c07431f
7
- data.tar.gz: 944b8112be80c9c3b78dd7cdec01ac3c72f7e4e077196a315bfaa14ffeb4dfa6cdb868570ae0bc55fb4f07da858f968807fee8bb50d5846fa763dd7b82359f98
6
+ metadata.gz: 8a4db779ed117465fb2c981c0b9da39dcdd001e60abf00262d04353449b5b72ad433949d6c640458fe2b61b503e84180dbf3c7709d791b48f7ed2a5ad9dbe372
7
+ data.tar.gz: 7d00f04205f6ef10b21dee7067adf1f480a529cea4bf0caa404fd888547f101f3908f54863262333e2b363d2d17b28b4899ff12ddc5e226958eda975354fd954
@@ -311,7 +311,7 @@ module Wardite
311
311
  digits = target.memsize
312
312
  count = 0
313
313
  digits.times do |i|
314
- if (target.value & (1 << i)).zero?
314
+ if (target.value & (1 << i)).nonzero?
315
315
  count += 1
316
316
  end
317
317
  end
@@ -347,7 +347,13 @@ module Wardite
347
347
  if !right.is_a?(I32) || !left.is_a?(I32)
348
348
  raise EvalError, "maybe empty or invalid stack"
349
349
  end
350
- runtime.stack.push(I32(left.value_s / right.value_s))
350
+ result = left.value_s / right.value_s.to_f
351
+ iresult = (result >= 0 ? result.floor : result.ceil).to_i
352
+ if iresult >= (1 << (left.memsize - 1))
353
+ raise IntegerOverflow, "integer overflow"
354
+ end
355
+
356
+ runtime.stack.push(I32(iresult))
351
357
 
352
358
 
353
359
  when :i32_div_u
@@ -363,7 +369,13 @@ module Wardite
363
369
  if !right.is_a?(I32) || !left.is_a?(I32)
364
370
  raise EvalError, "maybe empty or invalid stack"
365
371
  end
366
- runtime.stack.push(I32(left.value_s % right.value_s))
372
+ result = left.value_s % right.value_s
373
+ if result > 0 && left.value_s < 0
374
+ result = result - right.value_s
375
+ elsif result < 0 && left.value_s > 0
376
+ result = result - right.value_s
377
+ end
378
+ runtime.stack.push(I32(result))
367
379
 
368
380
 
369
381
  when :i32_rem_u
@@ -403,8 +415,8 @@ module Wardite
403
415
  if !right.is_a?(I32) || !left.is_a?(I32)
404
416
  raise EvalError, "maybe empty or invalid stack"
405
417
  end
406
- value = left.value << right.value
407
- value %= (1 << right.memsize)
418
+ value = left.value << (right.value % right.memsize)
419
+ value %= 1 << right.memsize
408
420
 
409
421
  runtime.stack.push(I32(value))
410
422
 
@@ -414,7 +426,7 @@ module Wardite
414
426
  if !right.is_a?(I32) || !left.is_a?(I32)
415
427
  raise EvalError, "maybe empty or invalid stack"
416
428
  end
417
- value = left.value_s >> right.value
429
+ value = left.value_s >> (right.value % right.memsize)
418
430
  runtime.stack.push(I32(value))
419
431
 
420
432
 
@@ -423,7 +435,7 @@ module Wardite
423
435
  if !right.is_a?(I32) || !left.is_a?(I32)
424
436
  raise EvalError, "maybe empty or invalid stack"
425
437
  end
426
- value = left.value >> right.value
438
+ value = left.value >> (right.value % right.memsize)
427
439
  runtime.stack.push(I32(value))
428
440
 
429
441
 
@@ -432,9 +444,10 @@ module Wardite
432
444
  if !right.is_a?(I32) || !left.is_a?(I32)
433
445
  raise EvalError, "maybe empty or invalid stack"
434
446
  end
435
- rotated = left.value << right.value
436
- rest = left.value & (I32::I32_MAX << (right.memsize - right.value))
437
- value = rotated | (rest >> (right.memsize - right.value))
447
+ k = (right.value % right.memsize)
448
+ rotated = left.value << k
449
+ rest = left.value & (I32::I32_MAX << (right.memsize - k))
450
+ value = rotated | (rest >> (right.memsize - k))
438
451
  runtime.stack.push(I32(value))
439
452
 
440
453
 
@@ -443,9 +456,10 @@ module Wardite
443
456
  if !right.is_a?(I32) || !left.is_a?(I32)
444
457
  raise EvalError, "maybe empty or invalid stack"
445
458
  end
446
- rotated = left.value >> right.value
447
- rest = left.value & (I32::I32_MAX >> (right.memsize - right.value))
448
- value = rotated | (rest << (right.memsize - right.value))
459
+ k = (right.value % right.memsize)
460
+ rotated = left.value >> k
461
+ rest = left.value & (I32::I32_MAX >> (right.memsize - k))
462
+ value = rotated | (rest << (right.memsize - k))
449
463
  runtime.stack.push(I32(value))
450
464
 
451
465
 
@@ -368,7 +368,7 @@ module Wardite
368
368
  digits = target.memsize
369
369
  count = 0
370
370
  digits.times do |i|
371
- if (target.value & (1 << i)).zero?
371
+ if (target.value & (1 << i)).nonzero?
372
372
  count += 1
373
373
  end
374
374
  end
@@ -404,7 +404,13 @@ module Wardite
404
404
  if !right.is_a?(I64) || !left.is_a?(I64)
405
405
  raise EvalError, "maybe empty or invalid stack"
406
406
  end
407
- runtime.stack.push(I64(left.value_s / right.value_s))
407
+ result = left.value_s / right.value_s.to_f
408
+ iresult = (result >= 0 ? result.floor : result.ceil).to_i
409
+ if iresult >= (1 << (left.memsize - 1))
410
+ raise IntegerOverflow, "integer overflow"
411
+ end
412
+
413
+ runtime.stack.push(I64(iresult))
408
414
 
409
415
 
410
416
  when :i64_div_u
@@ -420,7 +426,13 @@ module Wardite
420
426
  if !right.is_a?(I64) || !left.is_a?(I64)
421
427
  raise EvalError, "maybe empty or invalid stack"
422
428
  end
423
- runtime.stack.push(I64(left.value_s % right.value_s))
429
+ result = left.value_s % right.value_s
430
+ if result > 0 && left.value_s < 0
431
+ result = result - right.value_s
432
+ elsif result < 0 && left.value_s > 0
433
+ result = result - right.value_s
434
+ end
435
+ runtime.stack.push(I64(result))
424
436
 
425
437
 
426
438
  when :i64_rem_u
@@ -460,8 +472,8 @@ module Wardite
460
472
  if !right.is_a?(I64) || !left.is_a?(I64)
461
473
  raise EvalError, "maybe empty or invalid stack"
462
474
  end
463
- value = left.value << right.value
464
- value %= (1 << right.memsize)
475
+ value = left.value << (right.value % right.memsize)
476
+ value %= 1 << right.memsize
465
477
 
466
478
  runtime.stack.push(I64(value))
467
479
 
@@ -471,7 +483,7 @@ module Wardite
471
483
  if !right.is_a?(I64) || !left.is_a?(I64)
472
484
  raise EvalError, "maybe empty or invalid stack"
473
485
  end
474
- value = left.value_s >> right.value
486
+ value = left.value_s >> (right.value % right.memsize)
475
487
  runtime.stack.push(I64(value))
476
488
 
477
489
 
@@ -480,7 +492,7 @@ module Wardite
480
492
  if !right.is_a?(I64) || !left.is_a?(I64)
481
493
  raise EvalError, "maybe empty or invalid stack"
482
494
  end
483
- value = left.value >> right.value
495
+ value = left.value >> (right.value % right.memsize)
484
496
  runtime.stack.push(I64(value))
485
497
 
486
498
 
@@ -489,9 +501,10 @@ module Wardite
489
501
  if !right.is_a?(I64) || !left.is_a?(I64)
490
502
  raise EvalError, "maybe empty or invalid stack"
491
503
  end
492
- rotated = left.value << right.value
493
- rest = left.value & (I64::I64_MAX << (right.memsize - right.value))
494
- value = rotated | (rest >> (right.memsize - right.value))
504
+ k = (right.value % right.memsize)
505
+ rotated = left.value << k
506
+ rest = left.value & (I64::I64_MAX << (right.memsize - k))
507
+ value = rotated | (rest >> (right.memsize - k))
495
508
  runtime.stack.push(I64(value))
496
509
 
497
510
 
@@ -500,9 +513,10 @@ module Wardite
500
513
  if !right.is_a?(I64) || !left.is_a?(I64)
501
514
  raise EvalError, "maybe empty or invalid stack"
502
515
  end
503
- rotated = left.value >> right.value
504
- rest = left.value & (I64::I64_MAX >> (right.memsize - right.value))
505
- value = rotated | (rest << (right.memsize - right.value))
516
+ k = (right.value % right.memsize)
517
+ rotated = left.value >> k
518
+ rest = left.value & (I64::I64_MAX >> (right.memsize - k))
519
+ value = rotated | (rest << (right.memsize - k))
506
520
  runtime.stack.push(I64(value))
507
521
 
508
522
 
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+ # rbs_inline: enabled
3
+ module Wardite
4
+ # basic error class.
5
+ class WebAssemblyError < StandardError
6
+ end
7
+
8
+ class IntegerOverflow < WebAssemblyError; end
9
+ end
@@ -2,5 +2,5 @@
2
2
  # rbs_inline: enabled
3
3
 
4
4
  module Wardite
5
- VERSION = "0.4.3" #: String
5
+ VERSION = "0.5.0" #: String
6
6
  end
data/lib/wardite.rb CHANGED
@@ -7,6 +7,7 @@ require_relative "wardite/const"
7
7
  require_relative "wardite/instruction"
8
8
  require_relative "wardite/value"
9
9
  require_relative "wardite/load"
10
+ require_relative "wardite/errors"
10
11
 
11
12
  module Wardite
12
13
  module Evaluator
@@ -1119,6 +1120,7 @@ module Wardite
1119
1120
  end
1120
1121
 
1121
1122
  @data += String.new("\0" * (delta * 64 * 1024))
1123
+ self.current = newsize
1122
1124
  prev
1123
1125
  end
1124
1126
 
data/scripts/gen_alu.rb CHANGED
@@ -480,7 +480,7 @@ module GenAlu
480
480
  digits = target.memsize
481
481
  count = 0
482
482
  digits.times do |i|
483
- if (target.value & (1 << i)).zero?
483
+ if (target.value & (1 << i)).nonzero?
484
484
  count += 1
485
485
  end
486
486
  end
@@ -520,7 +520,13 @@ module GenAlu
520
520
  if !right.is_a?(${CLASS}) || !left.is_a?(${CLASS})
521
521
  raise EvalError, "maybe empty or invalid stack"
522
522
  end
523
- runtime.stack.push(${CLASS}(left.value_s / right.value_s))
523
+ result = left.value_s / right.value_s.to_f
524
+ iresult = (result >= 0 ? result.floor : result.ceil).to_i
525
+ if iresult >= (1 << (left.memsize - 1))
526
+ raise IntegerOverflow, "integer overflow"
527
+ end
528
+
529
+ runtime.stack.push(${CLASS}(iresult))
524
530
  RUBY
525
531
 
526
532
  div_u: <<~RUBY,
@@ -547,7 +553,13 @@ module GenAlu
547
553
  if !right.is_a?(${CLASS}) || !left.is_a?(${CLASS})
548
554
  raise EvalError, "maybe empty or invalid stack"
549
555
  end
550
- runtime.stack.push(${CLASS}(left.value_s % right.value_s))
556
+ result = left.value_s % right.value_s
557
+ if result > 0 && left.value_s < 0
558
+ result = result - right.value_s
559
+ elsif result < 0 && left.value_s > 0
560
+ result = result - right.value_s
561
+ end
562
+ runtime.stack.push(${CLASS}(result))
551
563
  RUBY
552
564
 
553
565
  rem_u: <<~RUBY,
@@ -592,8 +604,8 @@ module GenAlu
592
604
  if !right.is_a?(${CLASS}) || !left.is_a?(${CLASS})
593
605
  raise EvalError, "maybe empty or invalid stack"
594
606
  end
595
- value = left.value << right.value
596
- value %= (1 << right.memsize)
607
+ value = left.value << (right.value % right.memsize)
608
+ value %= 1 << right.memsize
597
609
 
598
610
  runtime.stack.push(${CLASS}(value))
599
611
  RUBY
@@ -604,7 +616,7 @@ module GenAlu
604
616
  if !right.is_a?(${CLASS}) || !left.is_a?(${CLASS})
605
617
  raise EvalError, "maybe empty or invalid stack"
606
618
  end
607
- value = left.value_s >> right.value
619
+ value = left.value_s >> (right.value % right.memsize)
608
620
  runtime.stack.push(${CLASS}(value))
609
621
  RUBY
610
622
 
@@ -614,7 +626,7 @@ module GenAlu
614
626
  if !right.is_a?(${CLASS}) || !left.is_a?(${CLASS})
615
627
  raise EvalError, "maybe empty or invalid stack"
616
628
  end
617
- value = left.value >> right.value
629
+ value = left.value >> (right.value % right.memsize)
618
630
  runtime.stack.push(${CLASS}(value))
619
631
  RUBY
620
632
 
@@ -624,9 +636,10 @@ module GenAlu
624
636
  if !right.is_a?(${CLASS}) || !left.is_a?(${CLASS})
625
637
  raise EvalError, "maybe empty or invalid stack"
626
638
  end
627
- rotated = left.value << right.value
628
- rest = left.value & (${CLASS}::${CLASS}_MAX << (right.memsize - right.value))
629
- value = rotated | (rest >> (right.memsize - right.value))
639
+ k = (right.value % right.memsize)
640
+ rotated = left.value << k
641
+ rest = left.value & (${CLASS}::${CLASS}_MAX << (right.memsize - k))
642
+ value = rotated | (rest >> (right.memsize - k))
630
643
  runtime.stack.push(${CLASS}(value))
631
644
  RUBY
632
645
 
@@ -636,9 +649,10 @@ module GenAlu
636
649
  if !right.is_a?(${CLASS}) || !left.is_a?(${CLASS})
637
650
  raise EvalError, "maybe empty or invalid stack"
638
651
  end
639
- rotated = left.value >> right.value
640
- rest = left.value & (${CLASS}::${CLASS}_MAX >> (right.memsize - right.value))
641
- value = rotated | (rest << (right.memsize - right.value))
652
+ k = (right.value % right.memsize)
653
+ rotated = left.value >> k
654
+ rest = left.value & (${CLASS}::${CLASS}_MAX >> (right.memsize - k))
655
+ value = rotated | (rest << (right.memsize - k))
642
656
  runtime.stack.push(${CLASS}(value))
643
657
  RUBY
644
658
 
@@ -0,0 +1,12 @@
1
+ # Generated from lib/wardite/errors.rb with RBS::Inline
2
+
3
+ # frozen_string_literal: true
4
+ # rbs_inline: enabled
5
+ module Wardite
6
+ # basic error class.
7
+ class WebAssemblyError < StandardError
8
+ end
9
+
10
+ class IntegerOverflow < WebAssemblyError
11
+ end
12
+ end
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.3
4
+ version: 0.5.0
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-17 00:00:00.000000000 Z
11
+ date: 2024-11-24 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A pure-ruby webassembly runtime
14
14
  email:
@@ -49,6 +49,7 @@ files:
49
49
  - lib/wardite/alu_i64.generated.rb
50
50
  - lib/wardite/const.rb
51
51
  - lib/wardite/convert.generated.rb
52
+ - lib/wardite/errors.rb
52
53
  - lib/wardite/instruction.rb
53
54
  - lib/wardite/leb128.rb
54
55
  - lib/wardite/load.rb
@@ -66,6 +67,7 @@ files:
66
67
  - sig/generated/wardite/alu_i64.generated.rbs
67
68
  - sig/generated/wardite/const.rbs
68
69
  - sig/generated/wardite/convert.generated.rbs
70
+ - sig/generated/wardite/errors.rbs
69
71
  - sig/generated/wardite/instruction.rbs
70
72
  - sig/generated/wardite/leb128.rbs
71
73
  - sig/generated/wardite/load.rbs