• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Bnaya / objectbuffer / 1cc46d885c9a8c4464d8420dc6a68afe83ac9811

pending completion
1cc46d885c9a8c4464d8420dc6a68afe83ac9811

push

github

Bnaya Peretz
wip

458 of 552 branches covered (82.97%)

Branch coverage included in aggregate %.

1249 of 1361 relevant lines covered (91.77%)

277.66 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

64.0
/src/internal/mapWrapper.ts
1
import type { ExternalArgs, GlobalCarrier, InternalAPI } from "./interfaces";
2
import {
3
  deleteObjectPropertyEntryByKey,
4
  objectGet,
5
  objectSet,
6
  mapOrSetClear,
7
} from "./objectWrapperHelpers";
8

9
import { INTERNAL_API_SYMBOL } from "./symbols";
10
import { BaseProxyTrap } from "./BaseProxyTrap";
11
import {
12
  hashMapNodeLookup,
13
  hashMapSize,
14
  hashmapNodesPointerIterator,
15
  hashMapNodePointerToKey,
16
  hashMapNodePointerToValue,
17
} from "./hashmap/hashmap";
18
import { entryToFinalJavaScriptValue } from "./entryToFinalJavaScriptValue";
19
import { object_pointerToHashMap_get } from "./generatedStructs";
20

21
export class MapWrapper<K extends string | number, V>
22
  extends BaseProxyTrap
23
  implements Map<K, V>
24
{
25
  clear(): void {
26
    mapOrSetClear(this.externalArgs, this.carrier, this.entryPointer);
27
  }
28

29
  forEach(
30
    callbackfn: (value: V, key: K, map: Map<K, V>) => void,
31
    thisArg?: any
32
  ): void {
33
    for (const pair of this.entries()) {
34
      callbackfn.call(thisArg || null, pair[1], pair[0], this);
4✔
35
    }
36
  }
37

38
  get size(): number {
39
    return hashMapSize(
40
      this.carrier.heap,
41
      object_pointerToHashMap_get(this.carrier.heap, this.entryPointer)
42
    );
43
  }
44

45
  [Symbol.iterator](): IterableIterator<[K, V]> {
46
    return this.entries();
47
  }
48

49
  *entries(): IterableIterator<[K, V]> {
50
    for (const nodePointer of hashmapNodesPointerIterator(
51
      this.carrier.heap,
52
      object_pointerToHashMap_get(this.carrier.heap, this.entryPointer)
53
    )) {
54
      const valuePointer = hashMapNodePointerToValue(nodePointer);
55
      const keyPointer = hashMapNodePointerToKey(
56
        this.carrier.heap,
57
        nodePointer
58
      );
59

60
      yield [
61
        entryToFinalJavaScriptValue(
62
          this.externalArgs,
63
          this.carrier,
64
          keyPointer
65
        ),
66
        entryToFinalJavaScriptValue(
67
          this.externalArgs,
68
          this.carrier,
69
          this.carrier.heap.u32[valuePointer / Uint32Array.BYTES_PER_ELEMENT]
70
        ),
71
      ];
72
    }
73
  }
74

75
  *keys(): IterableIterator<K> {
76
    for (const nodePointer of hashmapNodesPointerIterator(
77
      this.carrier.heap,
78
      object_pointerToHashMap_get(this.carrier.heap, this.entryPointer)
79
    )) {
80
      const t = hashMapNodePointerToKey(this.carrier.heap, nodePointer);
81

82
      yield entryToFinalJavaScriptValue(this.externalArgs, this.carrier, t);
83
    }
84
  }
85

86
  *values(): IterableIterator<V> {
87
    for (const nodePointer of hashmapNodesPointerIterator(
88
      this.carrier.heap,
89
      object_pointerToHashMap_get(this.carrier.heap, this.entryPointer)
90
    )) {
91
      const valuePointer = hashMapNodePointerToValue(nodePointer);
92

93
      yield entryToFinalJavaScriptValue(
94
        this.externalArgs,
95
        this.carrier,
96
        this.carrier.heap.u32[valuePointer / Uint32Array.BYTES_PER_ELEMENT]
97
      );
98
    }
99
  }
100

101
  get [Symbol.toStringTag]() {
102
    return Map.prototype[Symbol.toStringTag];
103
  }
104

105
  get [INTERNAL_API_SYMBOL](): InternalAPI {
106
    return this;
107
  }
108

109
  static get [Symbol.species]() {
110
    return Map;
111
  }
112

113
  public get(p: string | number) {
114
    if (!(typeof p === "string" || typeof p === "number")) {
×
115
      return undefined;
116
    }
117

118
    return objectGet(
119
      this.externalArgs,
120
      this.carrier,
121
      object_pointerToHashMap_get(this.carrier.heap, this.entryPointer),
122
      p
123
    );
124
  }
125

126
  public delete(p: string | number): boolean {
127
    if (!(typeof p === "string" || typeof p === "number")) {
3!
128
      return false;
129
    }
130

131
    return deleteObjectPropertyEntryByKey(
132
      this.carrier,
133
      object_pointerToHashMap_get(this.carrier.heap, this.entryPointer),
134
      p
135
    );
136
  }
137

138
  public has(p: string | number) {
139
    if (!(typeof p === "string" || typeof p === "number")) {
4!
140
      return false;
141
    }
142

143
    return (
144
      hashMapNodeLookup(
145
        this.carrier.heap,
146
        object_pointerToHashMap_get(this.carrier.heap, this.entryPointer),
147
        p
148
      ) !== 0
149
    );
150
  }
151

152
  public set(p: string | number, value: any) {
153
    if (!(typeof p === "string" || typeof p === "number")) {
15!
154
      return this;
155
    }
156

157
    objectSet(
158
      this.externalArgs,
159
      this.carrier,
160
      object_pointerToHashMap_get(this.carrier.heap, this.entryPointer),
161
      p,
162
      value
163
    );
164

165
    return this;
166
  }
167
}
168

169
export function createMapWrapper<K extends string | number, V>(
170
  externalArgs: ExternalArgs,
171
  globalCarrier: GlobalCarrier,
172
  entryPointer: number
173
): Map<K, V> {
174
  return new MapWrapper<K, V>(externalArgs, globalCarrier, entryPointer);
175
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc