@@ -99,9 +99,11 @@ shared struct SharedBox {
99
99
let sharedBox = new SharedBox ();
100
100
let sharedBox2 = new SharedBox ();
101
101
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
+ }
105
107
106
108
// can programmatically test if a value can be shared
107
109
assert (Reflect .canBeShared (sharedBox2));
@@ -110,16 +112,20 @@ assert(!Reflect.canBeShared({}));
110
112
let worker = new Worker (' worker.js' );
111
113
worker .postMessage ({ sharedBox });
112
114
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
+ }
115
119
```
116
120
117
121
``` javascript
118
122
// worker.js
119
123
onmessage = function (e ) {
120
124
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
+ }
123
129
};
124
130
```
125
131
@@ -135,25 +141,31 @@ Shared Arrays are a fixed-length arrays that may be shared across agents. They a
135
141
136
142
``` javascript
137
143
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
+ }
142
150
}
143
151
144
152
let worker = new Worker (' worker_array.js' );
145
153
worker .postMessage ({ sharedArray });
146
154
147
- sharedArray[0 ] = " main" ;
148
- console .log (sharedArray[0 ]);
155
+ unsafe {
156
+ sharedArray[0 ] = " main" ;
157
+ console .log (sharedArray[0 ]);
158
+ }
149
159
```
150
160
151
161
``` javascript
152
162
// worker_array.js
153
163
onmessage = function (e ) {
154
164
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
+ }
157
169
};
158
170
```
159
171
@@ -281,13 +293,13 @@ let worker = new Worker('worker_mutex.js');
281
293
worker .postMessage ({ point });
282
294
283
295
// assume this agent can block
284
- {
296
+ unsafe {
285
297
using lock = Atomics .Mutex .lock (point .mutex );
286
298
point .x = " main" ;
287
299
point .y = " main" ;
288
300
}
289
301
290
- {
302
+ unsafe {
291
303
using lock = Atomics .Mutex .lock (point .mutex );
292
304
console .log (point .x , point .y );
293
305
}
@@ -297,7 +309,7 @@ worker.postMessage({ point });
297
309
// worker_mutex.js
298
310
onmessage = function (e ) {
299
311
let point = e .data .point ;
300
- {
312
+ unsafe {
301
313
using lock = Atomics .Mutex .lock (point .mutex );
302
314
point .x = " worker" ;
303
315
point .y = " worker" ;
0 commit comments