Skip to content

Commit

Permalink
Don't ignore ref count, but zero & restore it
Browse files Browse the repository at this point in the history
  • Loading branch information
Bnaya committed Apr 10, 2020
1 parent a10c867 commit b819698
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/internal/objectWrapperHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import {
handleArcForDeletedValuePointer,
decrementRefCount,
writeEntry,
incrementRefCount,
setRefCount,
} from "./store";
import { entryToFinalJavaScriptValue } from "./entryToFinalJavaScriptValue";
import {
Expand Down Expand Up @@ -148,9 +150,12 @@ export function mapOrSetClear(
) {
const entry = readEntry(carrier, mapOrSetPtr) as MapEntry | SetEntry;

// we fake the entry refCount as zero so getAllLinkedAddresses will visit what's needed
const prevCount = setRefCount(carrier, mapOrSetPtr, 0);

const { leafAddresses, arcAddresses } = getAllLinkedAddresses(
carrier,
true,
false,
mapOrSetPtr
);

Expand All @@ -174,6 +179,9 @@ export function mapOrSetClear(

// hashmapClearFree(externalArgs, carrier, entry.value);

// Restore real ref count
setRefCount(carrier, mapOrSetPtr, prevCount);

entry.value = createHashMap(carrier, externalArgs.hashMapMinInitialCapacity);

writeEntry(carrier, mapOrSetPtr, entry);
Expand Down
27 changes: 27 additions & 0 deletions src/internal/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,33 @@ export function decrementRefCount(
throw new Error("unexpected");
}

export function getRefCount(carrier: GlobalCarrier, entryPointer: number) {
const entry = readEntry(carrier, entryPointer);

if ("refsCount" in entry) {
return entry.refsCount;
}

throw new Error("unexpected");
}

export function setRefCount(
carrier: GlobalCarrier,
entryPointer: number,
newRefCount: number
) {
const entry = readEntry(carrier, entryPointer);

if ("refsCount" in entry) {
const prevCount = entry.refsCount;
entry.refsCount = newRefCount;
writeEntry(carrier, entryPointer, entry);
return prevCount;
}

throw new Error("unexpected");
}

// export function getObjectPropPtrToPtr(
// { dataView }: GlobalCarrier,
// pointerToEntry: number
Expand Down

0 comments on commit b819698

Please sign in to comment.