goscript 0.0.35 → 0.0.36

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,116 @@
1
+ import * as $ from "@goscript/builtin/builtin.js";
2
+ // firstStoreInProgress is a placeholder value used during the first store operation
3
+ const firstStoreInProgress = Symbol('firstStoreInProgress');
4
+ export class Value {
5
+ get v() {
6
+ return this._fields.v.value;
7
+ }
8
+ set v(value) {
9
+ this._fields.v.value = value;
10
+ }
11
+ _fields;
12
+ constructor(init) {
13
+ this._fields = {
14
+ v: $.varRef(init?.v ?? null)
15
+ };
16
+ }
17
+ clone() {
18
+ const cloned = new Value();
19
+ cloned._fields = {
20
+ v: $.varRef(this._fields.v.value)
21
+ };
22
+ return cloned;
23
+ }
24
+ // Load returns the value set by the most recent Store.
25
+ // It returns nil if there has been no call to Store for this Value.
26
+ Load() {
27
+ const v = this;
28
+ // For JavaScript, we can simplify this since we're single-threaded
29
+ // Just return the stored value directly
30
+ return v._fields.v.value;
31
+ }
32
+ // Store sets the value of the [Value] v to val.
33
+ // All calls to Store for a given Value must use values of the same concrete type.
34
+ // Store of an inconsistent type panics, as does Store(nil).
35
+ Store(val) {
36
+ const v = this;
37
+ if (val == null) {
38
+ $.panic("sync/atomic: store of nil value into Value");
39
+ }
40
+ // For JavaScript, store the value directly
41
+ v._fields.v.value = val;
42
+ }
43
+ // Swap stores new into Value and returns the previous value. It returns nil if
44
+ // the Value is empty.
45
+ //
46
+ // All calls to Swap for a given Value must use values of the same concrete
47
+ // type. Swap of an inconsistent type panics, as does Swap(nil).
48
+ Swap(_new) {
49
+ const v = this;
50
+ if (_new == null) {
51
+ $.panic("sync/atomic: swap of nil value into Value");
52
+ }
53
+ // For JavaScript, swap the values directly
54
+ const old = v._fields.v.value;
55
+ v._fields.v.value = _new;
56
+ return old;
57
+ }
58
+ // CompareAndSwap executes the compare-and-swap operation for the [Value].
59
+ //
60
+ // All calls to CompareAndSwap for a given Value must use values of the same
61
+ // concrete type. CompareAndSwap of an inconsistent type panics, as does
62
+ // CompareAndSwap(old, nil).
63
+ CompareAndSwap(old, _new) {
64
+ const v = this;
65
+ if (_new == null) {
66
+ $.panic("sync/atomic: compare and swap of nil value into Value");
67
+ }
68
+ // For JavaScript, compare and swap directly
69
+ if (v._fields.v.value === old) {
70
+ v._fields.v.value = _new;
71
+ return true;
72
+ }
73
+ return false;
74
+ }
75
+ // Register this type with the runtime type system
76
+ static __typeInfo = $.registerStructType('Value', new Value(), [{ name: "Load", args: [], returns: [{ type: { kind: $.TypeKind.Interface, methods: [] } }] }, { name: "Store", args: [{ name: "val", type: { kind: $.TypeKind.Interface, methods: [] } }], returns: [] }, { name: "Swap", args: [{ name: "new", type: { kind: $.TypeKind.Interface, methods: [] } }], returns: [{ type: { kind: $.TypeKind.Interface, methods: [] } }] }, { name: "CompareAndSwap", args: [{ name: "old", type: { kind: $.TypeKind.Interface, methods: [] } }, { name: "new", type: { kind: $.TypeKind.Interface, methods: [] } }], returns: [{ type: { kind: $.TypeKind.Basic, name: "boolean" } }] }], Value, { "v": { kind: $.TypeKind.Interface, methods: [] } });
77
+ }
78
+ class efaceWords {
79
+ get typ() {
80
+ return this._fields.typ.value;
81
+ }
82
+ set typ(value) {
83
+ this._fields.typ.value = value;
84
+ }
85
+ get data() {
86
+ return this._fields.data.value;
87
+ }
88
+ set data(value) {
89
+ this._fields.data.value = value;
90
+ }
91
+ _fields;
92
+ constructor(init) {
93
+ this._fields = {
94
+ typ: $.varRef(init?.typ ?? null),
95
+ data: $.varRef(init?.data ?? null)
96
+ };
97
+ }
98
+ clone() {
99
+ const cloned = new efaceWords();
100
+ cloned._fields = {
101
+ typ: $.varRef(this._fields.typ.value),
102
+ data: $.varRef(this._fields.data.value)
103
+ };
104
+ return cloned;
105
+ }
106
+ // Register this type with the runtime type system
107
+ static __typeInfo = $.registerStructType('efaceWords', new efaceWords(), [], efaceWords, { "data": { kind: $.TypeKind.Basic, name: "Pointer" }, "typ": { kind: $.TypeKind.Basic, name: "Pointer" } });
108
+ }
109
+ // Runtime functions for pinning/unpinning (no-ops in JavaScript)
110
+ export function runtime_procPin() {
111
+ return 0; // No-op in JavaScript
112
+ }
113
+ export function runtime_procUnpin() {
114
+ // No-op in JavaScript
115
+ }
116
+ //# sourceMappingURL=value.gs.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"value.gs.js","sourceRoot":"","sources":["../../../../gs/sync/atomic/value.gs.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,CAAC,MAAM,8BAA8B,CAAC;AAQlD,oFAAoF;AACpF,MAAM,oBAAoB,GAAG,MAAM,CAAC,sBAAsB,CAAC,CAAC;AAE5D,MAAM,OAAO,KAAK;IACjB,IAAW,CAAC;QACX,OAAO,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,KAAK,CAAA;IAC5B,CAAC;IACD,IAAW,CAAC,CAAC,KAAiB;QAC7B,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,KAAK,GAAG,KAAK,CAAA;IAC7B,CAAC;IAEM,OAAO,CAEb;IAED,YAAY,IAAgC;QAC3C,IAAI,CAAC,OAAO,GAAG;YACd,CAAC,EAAE,CAAC,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC,IAAI,IAAI,CAAC;SAC5B,CAAA;IACF,CAAC;IAEM,KAAK;QACX,MAAM,MAAM,GAAG,IAAI,KAAK,EAAE,CAAA;QAC1B,MAAM,CAAC,OAAO,GAAG;YAChB,CAAC,EAAE,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,KAAK,CAAC;SACjC,CAAA;QACD,OAAO,MAAM,CAAA;IACd,CAAC;IAED,uDAAuD;IACvD,oEAAoE;IAC7D,IAAI;QACV,MAAM,CAAC,GAAG,IAAI,CAAA;QACd,mEAAmE;QACnE,wCAAwC;QACxC,OAAO,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,KAAK,CAAA;IACzB,CAAC;IAED,gDAAgD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I,UAAU,EAAE,EAChB,EAAE,EACF,UAAU,EACV,EAAC,MAAM,EAAE,EAAE,IAAI,EAAE,CAAC,CAAC,QAAQ,CAAC,KAAK,EAAE,IAAI,EAAE,SAAS,EAAE,EAAE,KAAK,EAAE,EAAE,IAAI,EAAE,CAAC,CAAC,QAAQ,CAAC,KAAK,EAAE,IAAI,EAAE,SAAS,EAAE,EAAC,CAC1G,CAAC;;AAGH,iEAAiE;AACjE,MAAM,UAAU,eAAe;IAC9B,OAAO,CAAC,CAAC,CAAC,sBAAsB;AACjC,CAAC;AAED,MAAM,UAAU,iBAAiB;IAChC,sBAAsB;AACvB,CAAC"}
@@ -9,3 +9,4 @@ export declare function Slice(_ptr: Pointer, _len: IntegerType): any[];
9
9
  export declare function SliceData(_slice: any[]): Pointer;
10
10
  export declare function String(_ptr: Pointer, _len: IntegerType): string;
11
11
  export declare function StringData(_str: string): Pointer;
12
+ export declare function Pointer(value: any): Pointer;
@@ -41,4 +41,9 @@ export function String(_ptr, _len) {
41
41
  export function StringData(_str) {
42
42
  throw new Error('unsafe.StringData is not supported in JavaScript/TypeScript: direct memory access is not available in JavaScript');
43
43
  }
44
+ // Pointer converts a value to an unsafe.Pointer for atomic operations
45
+ // In JavaScript/TypeScript, this is just a pass-through function
46
+ export function Pointer(value) {
47
+ return value;
48
+ }
44
49
  //# sourceMappingURL=unsafe.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"unsafe.js","sourceRoot":"","sources":["../../../gs/unsafe/unsafe.ts"],"names":[],"mappings":"AAAA,oFAAoF;AACpF,2FAA2F;AAC3F,mCAAmC;AAYnC,uEAAuE;AACvE,4DAA4D;AAC5D,MAAM,UAAU,OAAO,CAAC,SAAwB;IAC9C,MAAM,IAAI,KAAK,CACb,sHAAsH,CACvH,CAAA;AACH,CAAC;AAED,8EAA8E;AAC9E,4DAA4D;AAC5D,MAAM,UAAU,QAAQ,CAAC,SAAwB;IAC/C,MAAM,IAAI,KAAK,CACb,sIAAsI,CACvI,CA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MAAM,UAAU,MAAM,CAAC,SAAwB;IAC7C,MAAM,IAAI,KAAK,CACb,gHAAgH,CACjH,CAAA;AACH,CAAC;AAED,sDAAsD;AACtD,+DAA+D;AAC/D,MAAM,UAAU,GAAG,CAAC,IAAa,EAAE,IAAiB;IAClD,MAAM,IAAI,KAAK,CACb,yGAAyG,CAC1G,CAAA;AACH,CAAC;AAED,6DAA6D;AAC7D,4DAA4D;AAC5D,MAAM,UAAU,KAAK,CAAC,IAAa,EAAE,IAAiB;IACpD,MAAM,IAAI,KAAK,CACb,6GAA6G,CAC9G,CAAA;AACH,CAAC;AAED,mEAAmE;AACnE,4DAA4D;AAC5D,MAAM,UAAU,SAAS,CAAC,MAAa;IACrC,MAAM,IAAI,KAAK,CACb,iHAAiH,CAClH,CAAA;AACH,CAAC;AAED,oEAAoE;AACpE,4DAA4D;AAC5D,MAAM,UAAU,MAAM,CAAC,IAAa,EAAE,IAAiB;IACrD,MAAM,IAAI,KAAK,CACb,8GAA8G,CAC/G,CAAA;AACH,CAAC;AAED,kEAAkE;AAClE,4DAA4D;AAC5D,MAAM,UAAU,UAAU,CAAC,IAAY;IACrC,MAAM,IAAI,KAAK,CACb,kHAAkH,CACnH,CAAA;AACH,CAAC;AAED,sEAAsE;AACtE,iEAAiE;AACjE,MAAM,UAAU,OAAO,CAAC,KAAU;IAChC,OAAO,KAAK,CAAC;AACf,CAAC"}
package/gs/maps/iter.ts CHANGED
@@ -58,7 +58,7 @@ export function Insert<K extends $.Comparable, V>(
58
58
 
59
59
  // Collect collects key-value pairs from seq into a new map
60
60
  // and returns it.
61
- export function Collect<K extends $.Comparable, V extends any>(
61
+ export function Collect<K extends $.Comparable, V>(
62
62
  seq: iter.Seq2<K, V>,
63
63
  ): Map<K, V> {
64
64
  let m = $.makeMap<K, V>()
@@ -46,8 +46,8 @@ describe('Erfinv', () => {
46
46
  })
47
47
 
48
48
  it('should handle very small values', () => {
49
- expect(Erfinv(1e-10)).toBeCloseTo(8.862269254527579e-11, 14)
50
- expect(Erfinv(-1e-10)).toBeCloseTo(-8.862269254527579e-11, 14)
49
+ expect(Erfinv(1e-10)).toBeCloseTo(8.862269254527579e-11, 14) //eslint-disable-line no-loss-of-precision
50
+ expect(Erfinv(-1e-10)).toBeCloseTo(-8.862269254527579e-11, 14) //eslint-disable-line no-loss-of-precision
51
51
  })
52
52
 
53
53
  it('should handle values close to boundaries', () => {
@@ -1,6 +1,6 @@
1
1
  import { describe, it, expect } from 'vitest';
2
2
  import { FMA } from './fma.gs.js';
3
- import { Inf, NaN, IsNaN, IsInf } from './bits.gs.js';
3
+ import { Inf, NaN as GoNaN, IsNaN } from './bits.gs.js';
4
4
 
5
5
  describe('FMA', () => {
6
6
  it('should compute fused multiply-add correctly for basic cases', () => {
@@ -35,10 +35,10 @@ describe('FMA', () => {
35
35
  });
36
36
 
37
37
  it('should handle NaN cases', () => {
38
- expect(IsNaN(FMA(NaN(), 2, 3))).toBe(true);
39
- expect(IsNaN(FMA(2, NaN(), 3))).toBe(true);
40
- expect(IsNaN(FMA(2, 3, NaN()))).toBe(true);
41
- expect(IsNaN(FMA(NaN(), NaN(), NaN()))).toBe(true);
38
+ expect(IsNaN(FMA(GoNaN(), 2, 3))).toBe(true);
39
+ expect(IsNaN(FMA(2, GoNaN(), 3))).toBe(true);
40
+ expect(IsNaN(FMA(2, 3, GoNaN()))).toBe(true);
41
+ expect(IsNaN(FMA(GoNaN(), GoNaN(), GoNaN()))).toBe(true);
42
42
  });
43
43
 
44
44
  it('should handle special infinity and zero combinations', () => {
@@ -1,7 +1,7 @@
1
1
  import { describe, it, expect } from 'vitest';
2
2
  import { Ldexp, ldexp } from './ldexp.gs.js';
3
3
  import { Frexp } from './frexp.gs.js';
4
- import { Inf, NaN, IsNaN, IsInf } from './bits.gs.js';
4
+ import { Inf, NaN as GoNaN, IsNaN } from './bits.gs.js';
5
5
 
6
6
  describe('Ldexp', () => {
7
7
  it('should compute frac × 2^exp correctly for basic cases', () => {
@@ -47,9 +47,9 @@ describe('Ldexp', () => {
47
47
  });
48
48
 
49
49
  it('should handle NaN cases', () => {
50
- expect(IsNaN(Ldexp(NaN(), 5))).toBe(true);
51
- expect(IsNaN(Ldexp(NaN(), -5))).toBe(true);
52
- expect(IsNaN(Ldexp(NaN(), 0))).toBe(true);
50
+ expect(IsNaN(Ldexp(GoNaN(), 5))).toBe(true);
51
+ expect(IsNaN(Ldexp(GoNaN(), -5))).toBe(true);
52
+ expect(IsNaN(Ldexp(GoNaN(), 0))).toBe(true);
53
53
  });
54
54
 
55
55
  it('should be the inverse of Frexp', () => {
@@ -123,6 +123,6 @@ describe('ldexp (lowercase)', () => {
123
123
  // Test special cases
124
124
  expect(ldexp(Inf(1), 5)).toBe(Ldexp(Inf(1), 5));
125
125
  expect(ldexp(Inf(-1), 5)).toBe(Ldexp(Inf(-1), 5));
126
- expect(IsNaN(ldexp(NaN(), 5))).toBe(IsNaN(Ldexp(NaN(), 5)));
126
+ expect(IsNaN(ldexp(GoNaN(), 5))).toBe(IsNaN(Ldexp(GoNaN(), 5)));
127
127
  });
128
128
  });
@@ -94,8 +94,8 @@ describe('Lgamma', () => {
94
94
  it('should satisfy reflection formula for negative values', () => {
95
95
  // For non-integer x: Lgamma(x) + Lgamma(1-x) = log(π/sin(πx))
96
96
  const x = 0.3
97
- const [lgamma_x, sign_x] = Lgamma(x)
98
- const [lgamma_1_minus_x, sign_1_minus_x] = Lgamma(1 - x)
97
+ const [lgamma_x, _sign_x] = Lgamma(x)
98
+ const [lgamma_1_minus_x, _sign_1_minus_x] = Lgamma(1 - x)
99
99
  const expected = Math.log(Math.PI / Math.sin(Math.PI * x))
100
100
  expect(lgamma_x + lgamma_1_minus_x).toBeCloseTo(expected, 5)
101
101
  })
@@ -0,0 +1,276 @@
1
+ import * as $ from "@goscript/builtin/builtin.js";
2
+
3
+ import * as unsafe from "@goscript/unsafe/index.js"
4
+
5
+ // Type alias for uintptr (pointer-sized unsigned integer)
6
+ export type uintptr = number;
7
+ export type Pointer = any;
8
+
9
+ // SwapInt32 atomically stores new into *addr and returns the previous *addr value.
10
+ // Consider using the more ergonomic and less error-prone [Int32.Swap] instead.
11
+ //
12
+ //go:noescape
13
+ export function SwapInt32(addr: $.VarRef<number> | null, _new: number): number {
14
+ if (!addr) return 0;
15
+ let old = addr.value;
16
+ addr.value = _new;
17
+ return old;
18
+ }
19
+
20
+ // SwapUint32 atomically stores new into *addr and returns the previous *addr value.
21
+ // Consider using the more ergonomic and less error-prone [Uint32.Swap] instead.
22
+ //
23
+ //go:noescape
24
+ export function SwapUint32(addr: $.VarRef<number> | null, _new: number): number {
25
+ if (!addr) return 0;
26
+ let old = addr.value;
27
+ addr.value = _new;
28
+ return old;
29
+ }
30
+
31
+ // SwapUintptr atomically stores new into *addr and returns the previous *addr value.
32
+ // Consider using the more ergonomic and less error-prone [Uintptr.Swap] instead.
33
+ //
34
+ //go:noescape
35
+ export function SwapUintptr(addr: $.VarRef<uintptr> | null, _new: uintptr): uintptr {
36
+ if (!addr) return 0;
37
+ let old = addr.value;
38
+ addr.value = _new;
39
+ return old;
40
+ }
41
+
42
+ // SwapPointer atomically stores new into *addr and returns the previous *addr value.
43
+ // Consider using the more ergonomic and less error-prone [Pointer.Swap] instead.
44
+ export function SwapPointer(addr: $.VarRef<Pointer> | null, _new: Pointer): Pointer {
45
+ if (!addr) return null;
46
+ let old = addr.value;
47
+ addr.value = _new;
48
+ return old;
49
+ }
50
+
51
+ // CompareAndSwapInt32 executes the compare-and-swap operation for an int32 value.
52
+ // Consider using the more ergonomic and less error-prone [Int32.CompareAndSwap] instead.
53
+ //
54
+ //go:noescape
55
+ export function CompareAndSwapInt32(addr: $.VarRef<number> | null, old: number, _new: number): boolean {
56
+ if (!addr) return false;
57
+ if (addr.value === old) {
58
+ addr.value = _new;
59
+ return true;
60
+ }
61
+ return false;
62
+ }
63
+
64
+ // CompareAndSwapUint32 executes the compare-and-swap operation for a uint32 value.
65
+ // Consider using the more ergonomic and less error-prone [Uint32.CompareAndSwap] instead.
66
+ //
67
+ //go:noescape
68
+ export function CompareAndSwapUint32(addr: $.VarRef<number> | null, old: number, _new: number): boolean {
69
+ if (!addr) return false;
70
+ if (addr.value === old) {
71
+ addr.value = _new;
72
+ return true;
73
+ }
74
+ return false;
75
+ }
76
+
77
+ // CompareAndSwapUintptr executes the compare-and-swap operation for a uintptr value.
78
+ // Consider using the more ergonomic and less error-prone [Uintptr.CompareAndSwap] instead.
79
+ //
80
+ //go:noescape
81
+ export function CompareAndSwapUintptr(addr: $.VarRef<uintptr> | null, old: uintptr, _new: uintptr): boolean {
82
+ if (!addr) return false;
83
+ if (addr.value === old) {
84
+ addr.value = _new;
85
+ return true;
86
+ }
87
+ return false;
88
+ }
89
+
90
+ // CompareAndSwapPointer executes the compare-and-swap operation for a unsafe.Pointer value.
91
+ // Consider using the more ergonomic and less error-prone [Pointer.CompareAndSwap] instead.
92
+ export function CompareAndSwapPointer(addr: $.VarRef<Pointer> | null, old: Pointer, _new: Pointer): boolean {
93
+ if (!addr) return false;
94
+ if (addr.value === old) {
95
+ addr.value = _new;
96
+ return true;
97
+ }
98
+ return false;
99
+ }
100
+
101
+ // AddInt32 atomically adds delta to *addr and returns the new value.
102
+ // Consider using the more ergonomic and less error-prone [Int32.Add] instead.
103
+ //
104
+ //go:noescape
105
+ export function AddInt32(addr: $.VarRef<number> | null, delta: number): number {
106
+ if (!addr) return 0;
107
+ addr.value = (addr.value + delta) | 0; // Use bitwise OR to ensure 32-bit signed integer
108
+ return addr.value;
109
+ }
110
+
111
+ // AddUint32 atomically adds delta to *addr and returns the new value.
112
+ // To subtract a signed positive constant value c from x, do AddUint32(&x, ^uint32(c-1)).
113
+ // In particular, to decrement x, do AddUint32(&x, ^uint32(0)).
114
+ // Consider using the more ergonomic and less error-prone [Uint32.Add] instead.
115
+ //
116
+ //go:noescape
117
+ export function AddUint32(addr: $.VarRef<number> | null, delta: number): number {
118
+ if (!addr) return 0;
119
+ addr.value = (addr.value + delta) >>> 0; // Use unsigned right shift to ensure 32-bit unsigned integer
120
+ return addr.value;
121
+ }
122
+
123
+ // AddUintptr atomically adds delta to *addr and returns the new value.
124
+ // Consider using the more ergonomic and less error-prone [Uintptr.Add] instead.
125
+ //
126
+ //go:noescape
127
+ export function AddUintptr(addr: $.VarRef<uintptr> | null, delta: uintptr): uintptr {
128
+ if (!addr) return 0;
129
+ addr.value = (addr.value + delta) >>> 0; // Use unsigned right shift for uintptr
130
+ return addr.value;
131
+ }
132
+
133
+ // AndInt32 atomically performs a bitwise AND operation on *addr using the bitmask provided as mask
134
+ // and returns the old value.
135
+ // Consider using the more ergonomic and less error-prone [Int32.And] instead.
136
+ //
137
+ //go:noescape
138
+ export function AndInt32(addr: $.VarRef<number> | null, mask: number): number {
139
+ if (!addr) return 0;
140
+ let old = addr.value;
141
+ addr.value = (addr.value & mask) | 0; // Use bitwise OR to ensure 32-bit signed integer
142
+ return old;
143
+ }
144
+
145
+ // AndUint32 atomically performs a bitwise AND operation on *addr using the bitmask provided as mask
146
+ // and returns the old value.
147
+ // Consider using the more ergonomic and less error-prone [Uint32.And] instead.
148
+ //
149
+ //go:noescape
150
+ export function AndUint32(addr: $.VarRef<number> | null, mask: number): number {
151
+ if (!addr) return 0;
152
+ let old = addr.value;
153
+ addr.value = (addr.value & mask) >>> 0; // Use unsigned right shift to ensure 32-bit unsigned integer
154
+ return old;
155
+ }
156
+
157
+ // AndUintptr atomically performs a bitwise AND operation on *addr using the bitmask provided as mask
158
+ // and returns the old value.
159
+ // Consider using the more ergonomic and less error-prone [Uintptr.And] instead.
160
+ //
161
+ //go:noescape
162
+ export function AndUintptr(addr: $.VarRef<uintptr> | null, mask: uintptr): uintptr {
163
+ if (!addr) return 0;
164
+ let old = addr.value;
165
+ addr.value = (addr.value & mask) >>> 0; // Use unsigned right shift for uintptr
166
+ return old;
167
+ }
168
+
169
+ // OrInt32 atomically performs a bitwise OR operation on *addr using the bitmask provided as mask
170
+ // and returns the old value.
171
+ // Consider using the more ergonomic and less error-prone [Int32.Or] instead.
172
+ //
173
+ //go:noescape
174
+ export function OrInt32(addr: $.VarRef<number> | null, mask: number): number {
175
+ if (!addr) return 0;
176
+ let old = addr.value;
177
+ addr.value = (addr.value | mask) | 0; // Use bitwise OR to ensure 32-bit signed integer
178
+ return old;
179
+ }
180
+
181
+ // OrUint32 atomically performs a bitwise OR operation on *addr using the bitmask provided as mask
182
+ // and returns the old value.
183
+ // Consider using the more ergonomic and less error-prone [Uint32.Or] instead.
184
+ //
185
+ //go:noescape
186
+ export function OrUint32(addr: $.VarRef<number> | null, mask: number): number {
187
+ if (!addr) return 0;
188
+ let old = addr.value;
189
+ addr.value = (addr.value | mask) >>> 0; // Use unsigned right shift to ensure 32-bit unsigned integer
190
+ return old;
191
+ }
192
+
193
+ // OrUintptr atomically performs a bitwise OR operation on *addr using the bitmask provided as mask
194
+ // and returns the old value.
195
+ // Consider using the more ergonomic and less error-prone [Uintptr.Or] instead.
196
+ //
197
+ //go:noescape
198
+ export function OrUintptr(addr: $.VarRef<uintptr> | null, mask: uintptr): uintptr {
199
+ if (!addr) return 0;
200
+ let old = addr.value;
201
+ addr.value = (addr.value | mask) >>> 0; // Use unsigned right shift for uintptr
202
+ return old;
203
+ }
204
+
205
+ // LoadInt32 atomically loads *addr.
206
+ // Consider using the more ergonomic and less error-prone [Int32.Load] instead.
207
+ //
208
+ //go:noescape
209
+ export function LoadInt32(addr: $.VarRef<number> | null): number {
210
+ if (!addr) return 0;
211
+ return addr.value;
212
+ }
213
+
214
+ // LoadUint32 atomically loads *addr.
215
+ // Consider using the more ergonomic and less error-prone [Uint32.Load] instead.
216
+ //
217
+ //go:noescape
218
+ export function LoadUint32(addr: $.VarRef<number> | null): number {
219
+ if (!addr) return 0;
220
+ return addr.value;
221
+ }
222
+
223
+ // LoadUintptr atomically loads *addr.
224
+ // Consider using the more ergonomic and less error-prone [Uintptr.Load] instead.
225
+ //
226
+ //go:noescape
227
+ export function LoadUintptr(addr: $.VarRef<uintptr> | null): uintptr {
228
+ if (!addr) return 0;
229
+ return addr.value;
230
+ }
231
+
232
+ // LoadPointer atomically loads *addr.
233
+ // Consider using the more ergonomic and less error-prone [Pointer.Load] instead.
234
+ export function LoadPointer(addr: $.VarRef<Pointer> | null): Pointer {
235
+ if (!addr) return null;
236
+ return addr.value;
237
+ }
238
+
239
+ // StoreInt32 atomically stores val into *addr.
240
+ // Consider using the more ergonomic and less error-prone [Int32.Store] instead.
241
+ //
242
+ //go:noescape
243
+ export function StoreInt32(addr: $.VarRef<number> | null, val: number): void {
244
+ if (addr) {
245
+ addr.value = val | 0; // Use bitwise OR to ensure 32-bit signed integer
246
+ }
247
+ }
248
+
249
+ // StoreUint32 atomically stores val into *addr.
250
+ // Consider using the more ergonomic and less error-prone [Uint32.Store] instead.
251
+ //
252
+ //go:noescape
253
+ export function StoreUint32(addr: $.VarRef<number> | null, val: number): void {
254
+ if (addr) {
255
+ addr.value = val >>> 0; // Use unsigned right shift to ensure 32-bit unsigned integer
256
+ }
257
+ }
258
+
259
+ // StoreUintptr atomically stores val into *addr.
260
+ // Consider using the more ergonomic and less error-prone [Uintptr.Store] instead.
261
+ //
262
+ //go:noescape
263
+ export function StoreUintptr(addr: $.VarRef<uintptr> | null, val: uintptr): void {
264
+ if (addr) {
265
+ addr.value = val >>> 0; // Use unsigned right shift for uintptr
266
+ }
267
+ }
268
+
269
+ // StorePointer atomically stores val into *addr.
270
+ // Consider using the more ergonomic and less error-prone [Pointer.Store] instead.
271
+ export function StorePointer(addr: $.VarRef<Pointer> | null, val: Pointer): void {
272
+ if (addr) {
273
+ addr.value = val;
274
+ }
275
+ }
276
+