-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsharedarraybuffer_final.js
More file actions
68 lines (56 loc) · 2.12 KB
/
Copy pathsharedarraybuffer_final.js
File metadata and controls
68 lines (56 loc) · 2.12 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
try {
console.log("Testing SharedArrayBuffer...");
let sab = new SharedArrayBuffer(1024);
// Check byteLength
if (sab.byteLength !== 1024) {
throw new Error("byteLength is incorrect: " + sab.byteLength);
}
console.log("byteLength OK");
// Check prototype
if (!(sab instanceof SharedArrayBuffer)) {
throw new Error("instanceof SharedArrayBuffer failed");
}
console.log("instanceof OK");
if (sab instanceof ArrayBuffer) {
// SharedArrayBuffer is NOT an ArrayBuffer
throw new Error("SharedArrayBuffer should not be instanceof ArrayBuffer");
}
console.log("Not instanceof ArrayBuffer OK");
// Check usage with TypedArray
let i32 = new Int32Array(sab);
if (i32.byteLength !== 1024) {
throw new Error("TypedArray byteLength incorrect: " + i32.byteLength);
}
console.log("Int32Array on SAB OK");
// Check Atomics
Atomics.store(i32, 0, 42);
let val = Atomics.load(i32, 0);
if (val !== 42) {
throw new Error("Atomics store/load failed: " + val);
}
console.log("Atomics basic OK");
// Check Atomics.wait (timeout)
// Value at index 0 is 42. verification:
if (Atomics.load(i32, 0) !== 42) {
throw new Error("Value check failed before wait");
}
console.log("Testing wait (timeout expected)...");
let initialTime = Date.now();
let waitResult = Atomics.wait(i32, 0, 42, 100); // 100ms timeout
let duration = Date.now() - initialTime;
// Note: implementation might not be high precision, but should be > 0.
console.log("Wait result:", waitResult, "Duration:", duration);
if (waitResult !== "timed-out") {
throw new Error("Atomics.wait expected 'timed-out', got: " + waitResult);
}
// Check Atomics.notify (0 waiters)
let notifyResult = Atomics.notify(i32, 0, 1);
if (notifyResult !== 0) {
throw new Error("Atomics.notify expected 0, got: " + notifyResult);
}
console.log("Atomics wait/notify OK");
console.log("SharedArrayBuffer Final Test Passed!");
} catch (e) {
console.log("Test Failed:");
console.log(e);
}