Class: ComponentizeAny::WatGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/componentize_any/wat_generator.rb

Instance Method Summary collapse

Constructor Details

#initializeWatGenerator

Returns a new instance of WatGenerator.



2
3
4
# File 'lib/componentize_any/wat_generator.rb', line 2

def initialize
  @buf = ""
end

Instance Method Details

#parse_wit(wit) ⇒ Object



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
# File 'lib/componentize_any/wat_generator.rb', line 32

def parse_wit(wit)
  export = wit.exports[0]
  export_package = wit.packages[wit.exports[0].package_name]
  export_interface = export_package.interfaces[export.interface]
  export_func = export_interface.members.values[0] #: ComponentizeAny::Witty::Member

  @buf << "    (component\n      (core module) ;; empty\n      (core instance $m (;0;) (instantiate 0))\n      (type (;0;) (result))\n      (type $main_t \#{to_func_type_wat(export_func.args, export_func.return_value)})\n      (alias core export $m \"\#{export_func.counterpart}\" (core func (;0;)))\n      (func $main (;0;) (type $main_t) (canon lift (core func 0)))\n      (component $C (;0;)\n        (type (;0;) (result))\n        (type $main_t \#{to_func_type_wat(export_func.args, export_func.return_value)})\n        (import \"main\" (func $f (;0;) (type $main_t)))\n        (export (;1;) \"run\" (func $f))\n      )\n      (instance $c (;0;) (instantiate $C\n          (with \"main\" (func $main))\n      ))\n      (export (;1;) \"\#{export.full_name}\" (instance $c))\n    )\n  WAT\nend\n"

#to_func_type_wat(args, return_value) ⇒ Object



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
# File 'lib/componentize_any/wat_generator.rb', line 6

def to_func_type_wat(args, return_value)
  arg_types = args.map.with_index do |arg, i|
    case arg
    when :s32, :s64, :u32, :u64, :f32, :f64
      "(param \"arg#{i}\" #{arg.to_s})"
    else
      raise "unknown type: #{arg}"
    end
  end
  return_type = case return_value
  when :i32
    "i32"
  when :i64
    "i64"
  when :f32
    "f32"
  when :f64
    "f64"
  when :result
    "0" # result is predefined
  else
    raise "unknown type: #{return_value}"
  end
  "(func #{arg_types.join(' ')} (result #{return_type}))"
end

#to_sObject



60
61
62
# File 'lib/componentize_any/wat_generator.rb', line 60

def to_s
  @buf
end