/
greet.rb
54 lines (38 loc) 路 1.2 KB
/
greet.rb
1
2
3
4
5
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# coding: utf-8
$LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
require "wasmer"
# Instantiates the module.
file = File.expand_path "greet.wasm", File.dirname(__FILE__)
bytes = IO.read file, mode: "rb"
instance = Wasmer::Instance.new bytes
# Set the subject to greet.
subject = "Wasmer 馃拵".bytes
length_of_subject = subject.length
# Allocate memory for the subject, and get a pointer to it.
input_pointer = instance.exports.allocate length_of_subject
# Write the subject into the memory.
memory = instance.memory.uint8_view input_pointer
for nth in 0..length_of_subject - 1
memory[nth] = subject[nth]
end
# C-string terminates by NULL.
memory[length_of_subject] = 0
# Run the `greet` function. Give the pointer to the subject.
output_pointer = instance.exports.greet input_pointer
# Read the result of the `greet` function.
memory = instance.memory.uint8_view output_pointer
output = ""
nth = 0
while true
char = memory[nth]
if 0 == char
break
end
output += char.chr
nth += 1
end
length_of_output = nth
puts output
# Deallocate the subject, and the output.
instance.exports.deallocate(input_pointer, length_of_subject)
instance.exports.deallocate(output_pointer, length_of_output)