Skip to content

Commit 38b83c0

Browse files
committed
Update README to use unsafe blocks
1 parent 61a03b4 commit 38b83c0

File tree

1 file changed

+30
-18
lines changed

1 file changed

+30
-18
lines changed

README.md

+30-18
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,11 @@ shared struct SharedBox {
9999
let sharedBox = new SharedBox();
100100
let sharedBox2 = new SharedBox();
101101

102-
sharedBox.x = 42; // x is declared and rhs is primitive
103-
sharedBox.x = sharedBox2; // x is declared and rhs is shared
104-
assertThrows(() => { sharedBox.x = {}; }) // rhs is not a shared struct
102+
unsafe {
103+
sharedBox.x = 42; // x is declared and rhs is primitive
104+
sharedBox.x = sharedBox2; // x is declared and rhs is shared
105+
assertThrows(() => { sharedBox.x = {}; }) // rhs is not a shared struct
106+
}
105107

106108
// can programmatically test if a value can be shared
107109
assert(Reflect.canBeShared(sharedBox2));
@@ -110,16 +112,20 @@ assert(!Reflect.canBeShared({}));
110112
let worker = new Worker('worker.js');
111113
worker.postMessage({ sharedBox });
112114

113-
sharedBox.x = "main"; // x is declared and rhs is primitive
114-
console.log(sharedBox.x);
115+
unsafe {
116+
sharedBox.x = "main"; // x is declared and rhs is primitive
117+
console.log(sharedBox.x);
118+
}
115119
```
116120

117121
```javascript
118122
// worker.js
119123
onmessage = function(e) {
120124
let sharedBox = e.data.sharedBox;
121-
sharedBox.x = "worker"; // x is declared and rhs is primitive
122-
console.log(sharedBox.x);
125+
unsafe {
126+
sharedBox.x = "worker"; // x is declared and rhs is primitive
127+
console.log(sharedBox.x);
128+
}
123129
};
124130
```
125131

@@ -135,25 +141,31 @@ Shared Arrays are a fixed-length arrays that may be shared across agents. They a
135141

136142
```javascript
137143
let sharedArray = new SharedArray(100);
138-
assert(sharedArray.length === 100);
139-
for (i = 0; i < sharedArray.length; i++) {
140-
// like shared structs, all elements are initialized to undefined
141-
assert(sharedArray[i] === undefined);
144+
unsafe {
145+
assert(sharedArray.length === 100);
146+
for (i = 0; i < sharedArray.length; i++) {
147+
// like shared structs, all elements are initialized to undefined
148+
assert(sharedArray[i] === undefined);
149+
}
142150
}
143151

144152
let worker = new Worker('worker_array.js');
145153
worker.postMessage({ sharedArray });
146154

147-
sharedArray[0] = "main";
148-
console.log(sharedArray[0]);
155+
unsafe {
156+
sharedArray[0] = "main";
157+
console.log(sharedArray[0]);
158+
}
149159
```
150160

151161
```javascript
152162
// worker_array.js
153163
onmessage = function(e) {
154164
let sharedArray = e.data.sharedArray;
155-
sharedArray[0] = "worker";
156-
console.log(sharedArray[0]);
165+
unsafe {
166+
sharedArray[0] = "worker";
167+
console.log(sharedArray[0]);
168+
}
157169
};
158170
```
159171

@@ -281,13 +293,13 @@ let worker = new Worker('worker_mutex.js');
281293
worker.postMessage({ point });
282294

283295
// assume this agent can block
284-
{
296+
unsafe {
285297
using lock = Atomics.Mutex.lock(point.mutex);
286298
point.x = "main";
287299
point.y = "main";
288300
}
289301

290-
{
302+
unsafe {
291303
using lock = Atomics.Mutex.lock(point.mutex);
292304
console.log(point.x, point.y);
293305
}
@@ -297,7 +309,7 @@ worker.postMessage({ point });
297309
// worker_mutex.js
298310
onmessage = function(e) {
299311
let point = e.data.point;
300-
{
312+
unsafe {
301313
using lock = Atomics.Mutex.lock(point.mutex);
302314
point.x = "worker";
303315
point.y = "worker";

0 commit comments

Comments
 (0)