Class: Wardite::Runtime
- Inherits:
-
Object
- Object
- Wardite::Runtime
- Defined in:
- lib/wardite.rb
Instance Attribute Summary collapse
-
#call_stack ⇒ Object
: Array.
-
#instance ⇒ Object
readonly
: Instance.
-
#stack ⇒ Object
: Array.
Instance Method Summary collapse
- #call(name, args) ⇒ Object
- #call_index(idx, args) ⇒ Object
- #callable?(name) ⇒ Boolean
- #drained_stack(finish) ⇒ Object
- #eval_insn(frame, insn) ⇒ Object
- #execute! ⇒ Object
-
#initialize(inst) ⇒ Runtime
constructor
A new instance of Runtime.
- #invoke_external(external_function) ⇒ Object
- #invoke_internal(wasm_function) ⇒ Object
- #method_missing(name, *args) ⇒ Object
- #push_frame(wasm_function) ⇒ Object
- #respond_to?(name) ⇒ Boolean
Constructor Details
#initialize(inst) ⇒ Runtime
Returns a new instance of Runtime.
675 676 677 678 679 |
# File 'lib/wardite.rb', line 675 def initialize(inst) @stack = [] @call_stack = [] @instance = inst end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object
923 924 925 926 927 928 929 |
# File 'lib/wardite.rb', line 923 def method_missing(name, *args) if callable? name call(name, args) else super end end |
Instance Attribute Details
#call_stack ⇒ Object
: Array
670 671 672 |
# File 'lib/wardite.rb', line 670 def call_stack @call_stack end |
#instance ⇒ Object (readonly)
: Instance
672 673 674 |
# File 'lib/wardite.rb', line 672 def instance @instance end |
Instance Method Details
#call(name, args) ⇒ Object
690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 |
# File 'lib/wardite.rb', line 690 def call(name, args) if !callable?(name) raise ::NoMethodError, "function #{name} not found" end kind, fn = @instance.exports[name.to_s] if kind != 0 raise ::NoMethodError, "#{name} is not a function" end if fn.callsig.size != args.size raise ArgumentError, "unmatch arg size" end args.each do |arg| stack.push arg end case fn when WasmFunction invoke_internal(fn) when ExternalFunction invoke_external(fn) else raise GenericError, "registered pointer is not to a function" end end |
#call_index(idx, args) ⇒ Object
718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 |
# File 'lib/wardite.rb', line 718 def call_index(idx, args) fn = self.instance.store[idx] if !fn # TODO: own error NoFunctionError raise ::NoMethodError, "func #{idx} not found" end if fn.callsig.size != args.size raise ArgumentError, "unmatch arg size" end args.each do |arg| stack.push arg end case fn when WasmFunction invoke_internal(fn) when ExternalFunction invoke_external(fn) else raise GenericError, "registered pointer is not to a function" end end |
#callable?(name) ⇒ Boolean
683 684 685 |
# File 'lib/wardite.rb', line 683 def callable?(name) !! @instance.exports[name.to_s] end |
#drained_stack(finish) ⇒ Object
911 912 913 914 915 916 917 918 |
# File 'lib/wardite.rb', line 911 def drained_stack(finish) drained = stack[0...finish] if ! drained $stderr.puts "warning: state of stack: #{stack.inspect}" raise EvalError, "stack too short" end return drained end |
#eval_insn(frame, insn) ⇒ Object
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 |
# File 'lib/wardite.rb', line 821 def eval_insn(frame, insn) case insn.code when :local_get idx = insn.operand[0] if !idx.is_a?(Integer) raise EvalError, "[BUG] invalid type of operand" end local = frame.locals[idx] if !local raise EvalError, "local not found" end stack.push(local) when :local_set idx = insn.operand[0] if !idx.is_a?(Integer) raise EvalError, "[BUG] invalid type of operand" end value = stack.pop if !value raise EvalError, "value should be pushed" end frame.locals[idx] = value when :i32_add right, left = stack.pop, stack.pop if !right.is_a?(Integer) || !left.is_a?(Integer) raise EvalError, "maybe empty stack" end stack.push(left + right) when :i32_const const = insn.operand[0] if !const.is_a?(Integer) raise EvalError, "[BUG] invalid type of operand" end stack.push(const) when :i32_store _align = insn.operand[0] # TODO: alignment support? offset = insn.operand[1] raise EvalError, "[BUG] invalid type of operand" if !offset.is_a?(Integer) value = stack.pop addr = stack.pop if !value.is_a?(Integer) || !addr.is_a?(Integer) raise EvalError, "maybe stack too short" end at = addr + offset data_end = at + 4 # sizeof(i32) memory = self.instance.store.memories[0] || raise("[BUG] no memory") memory.data[at...data_end] = [value].pack("I") when :call idx = insn.operand[0] raise EvalError, "[BUG] local operand not found" if !idx.is_a?(Integer) fn = self.instance.store.funcs[idx] case fn when WasmFunction push_frame(fn) when ExternalFunction ret = invoke_external(fn) self.stack.push ret if ret else raise GenericError, "got a non-function pointer" end when :end old_frame = call_stack.pop if !old_frame raise EvalError, "maybe empty call stack" end # unwind the stacks if old_frame.arity > 0 if old_frame.arity > 1 raise ::NotImplementedError, "return artiy >= 2 not yet supported ;;" end value = stack.pop if !value raise EvalError, "cannot obtain return value" end self.stack = drained_stack(old_frame.sp) stack.push value else self.stack = drained_stack(old_frame.sp) end end end |
#execute! ⇒ Object
803 804 805 806 807 808 809 810 811 812 813 814 815 816 |
# File 'lib/wardite.rb', line 803 def execute! loop do cur_frame = self.call_stack.last #: Frame if !cur_frame break end cur_frame.pc += 1 insn = cur_frame.body[cur_frame.pc] if !insn break end eval_insn(cur_frame, insn) end end |
#invoke_external(external_function) ⇒ Object
790 791 792 793 794 795 796 797 798 799 800 |
# File 'lib/wardite.rb', line 790 def invoke_external(external_function) local_start = stack.size - external_function.callsig.size args = stack[local_start..] if !args raise LoadError, "stack too short" end self.stack = drained_stack(local_start) proc = external_function.callable proc[self.instance.store, args] end |
#invoke_internal(wasm_function) ⇒ Object
769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 |
# File 'lib/wardite.rb', line 769 def invoke_internal(wasm_function) arity = wasm_function.retsig.size push_frame(wasm_function) execute! if arity > 0 if arity > 1 raise ::NotImplementedError, "return artiy >= 2 not yet supported ;;" end if self.stack.empty? raise "[BUG] stack empry" end v = self.stack.pop return v end return nil end |
#push_frame(wasm_function) ⇒ Object
743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 |
# File 'lib/wardite.rb', line 743 def push_frame(wasm_function) local_start = stack.size - wasm_function.callsig.size locals = stack[local_start..] if !locals raise LoadError, "stack too short" end self.stack = drained_stack(local_start) wasm_function.locals_type.each_with_index do |typ, i| case typ when :i32, :u32 # locals.push Local::I32(typ, 0)... locals.push 0 else $stderr.puts "warning: unknown type #{typ.inspect}. default to Object" locals.push Object.new end end arity = wasm_function.retsig.size frame = Frame.new(-1, stack.size, wasm_function.body, arity, locals) self.call_stack.push(frame) end |
#respond_to?(name) ⇒ Boolean
933 934 935 |
# File 'lib/wardite.rb', line 933 def respond_to? name callable?(name) || super end |