Skip to content

Commit 6ed61e5

Browse files
committed
doc(readme) Explain the Instance.globals API.
1 parent 3a1b026 commit 6ed61e5

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

Diff for: README.md

+28-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ $ ruby simple.rb
7676

7777
## The `Instance` class
7878

79-
Instantiates a WebAssembly module represented by bytes, and calls exported functions on it:
79+
Instantiates a WebAssembly module represented by bytes, and calls
80+
exported functions on it:
8081

8182
```ruby
8283
require "wasmer"
@@ -93,19 +94,45 @@ result = instance.exports.sum 1, 2
9394
puts result # 3
9495
```
9596

97+
### Exported functions
98+
9699
All exported functions are accessible on the `exports`
97100
getter. Arguments of these functions are automatically casted to
98101
WebAssembly values.
99102

103+
### Exported memory
104+
100105
The `memory` getter exposes the `Memory` class representing the memory
101106
of that particular instance, e.g.:
102107

103108
```ruby
104109
view = instance.memory.uint8_view
105110
```
106111

112+
`Instance.memory` throws an exception if no memory is exported.
113+
107114
See below for more information.
108115

116+
### Exported globals
117+
118+
The `globals` getter exposes the `ExportedGlobal` class represented an
119+
exported global variable, e.g.:
120+
121+
```ruby
122+
# Get the `x` global.
123+
x = instance.globals.x
124+
125+
# Check whether the global is mutable.
126+
assert x.mutable
127+
assert_equal x.value, 7
128+
129+
# Update its value.
130+
x.value = 42
131+
132+
# Tada!
133+
assert_equal x.value, 42
134+
```
135+
109136
## The `Memory` class
110137

111138
A WebAssembly instance has its own memory, represented by the `Memory`

0 commit comments

Comments
 (0)