Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@bnaya/objectbuffer

Package Overview
Dependencies
Maintainers
1
Versions
122
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@bnaya/objectbuffer - npm Package Compare versions

Comparing version 0.0.0-6858e67 to 0.0.0-738fce6

dist/internal/stringDecode.d.ts

32

dist/internal/api.js

@@ -23,3 +23,8 @@ import { initializeArrayBuffer } from "./store";

dataView,
allocator
allocator,
uint8: new Uint8Array(arrayBuffer),
uint16: new Uint16Array(arrayBuffer),
uint32: new Uint32Array(arrayBuffer),
float64: new Float64Array(arrayBuffer),
bigUint64: new BigUint64Array(arrayBuffer)
};

@@ -64,6 +69,12 @@ const start = objectSaver(externalArgsApiToExternalArgsApi(externalArgs), carrier, [], initialValue);

});
return createObjectWrapper(externalArgsApiToExternalArgsApi(externalArgs), {
const carrier = {
dataView,
allocator
}, dataView.getUint32(INITIAL_ENTRY_POINTER_TO_POINTER));
allocator,
uint8: new Uint8Array(arrayBuffer),
uint16: new Uint16Array(arrayBuffer),
uint32: new Uint32Array(arrayBuffer),
float64: new Float64Array(arrayBuffer),
bigUint64: new BigUint64Array(arrayBuffer)
};
return createObjectWrapper(externalArgsApiToExternalArgsApi(externalArgs), carrier, dataView.getUint32(INITIAL_ENTRY_POINTER_TO_POINTER));
}

@@ -95,7 +106,16 @@ /**

skipInitialization: true
}); // eslint-disable-next-line @typescript-eslint/ban-ts-ignore
});
const carrier = {
dataView: new DataView(newArrayBuffer),
allocator,
uint8: new Uint8Array(newArrayBuffer),
uint16: new Uint16Array(newArrayBuffer),
uint32: new Uint32Array(newArrayBuffer),
float64: new Float64Array(newArrayBuffer),
bigUint64: new BigUint64Array(newArrayBuffer)
}; // eslint-disable-next-line @typescript-eslint/ban-ts-ignore
// @ts-ignore
allocator.end = newArrayBuffer.byteLength;
getInternalAPI(objectBuffer).replaceCarrierContent(new DataView(newArrayBuffer), allocator);
getInternalAPI(objectBuffer).replaceCarrierContent(carrier);
}

@@ -102,0 +122,0 @@ export { sizeOf } from "./sizeOf";

10

dist/internal/arrayHelpers.d.ts
import { ArrayEntry, ExternalArgs, DataViewAndAllocatorCarrier } from "./interfaces";
export declare function arrayGetMetadata(externalArgs: ExternalArgs, dataView: DataView, pointerToArrayEntry: number): ArrayEntry;
export declare function arrayGetPointersToValueInIndex(externalArgs: ExternalArgs, dataView: DataView, pointerToArrayEntry: number, indexToGet: number): {
export declare function arrayGetMetadata(carrier: DataViewAndAllocatorCarrier, pointerToArrayEntry: number): ArrayEntry;
export declare function arrayGetPointersToValueInIndex(carrier: DataViewAndAllocatorCarrier, pointerToArrayEntry: number, indexToGet: number): {
pointer: number;

@@ -8,3 +8,3 @@ pointerToThePointer: number;

export declare function getFinalValueAtArrayIndex(externalArgs: ExternalArgs, dataViewCarrier: DataViewAndAllocatorCarrier, pointerToArrayEntry: number, indexToGet: number): any;
export declare function setValuePointerAtArrayIndex(externalArgs: ExternalArgs, dataView: DataView, pointerToArrayEntry: number, indexToSet: number, pointerToEntry: number): void;
export declare function setValuePointerAtArrayIndex(carrier: DataViewAndAllocatorCarrier, pointerToArrayEntry: number, indexToSet: number, pointerToEntry: number): void;
export declare function setValueAtArrayIndex(externalArgs: ExternalArgs, carrier: DataViewAndAllocatorCarrier, pointerToArrayEntry: number, indexToSet: number, value: any): void;

@@ -18,5 +18,5 @@ /**

*/
export declare function shrinkArray(externalArgs: ExternalArgs, dataView: DataView, pointerToArrayEntry: number, wishedLength: number): void;
export declare function shrinkArray(externalArgs: ExternalArgs, carrier: DataViewAndAllocatorCarrier, pointerToArrayEntry: number, wishedLength: number): void;
export declare function arraySort(externalArgs: ExternalArgs, dataViewCarrier: DataViewAndAllocatorCarrier, pointerToArrayEntry: number, sortComparator?: (a: any, b: any) => 1 | -1 | 0): void;
export declare function arrayReverse(externalArgs: ExternalArgs, dataView: DataView, pointerToArrayEntry: number): void;
export declare function arrayReverse(externalArgs: ExternalArgs, carrier: DataViewAndAllocatorCarrier, pointerToArrayEntry: number): void;
//# sourceMappingURL=arrayHelpers.d.ts.map

@@ -5,8 +5,8 @@ import { readEntry, writeEntry, writeValueInPtrToPtrAndHandleMemory } from "./store";

import { assertNonNull } from "./assertNonNull";
export function arrayGetMetadata(externalArgs, dataView, pointerToArrayEntry) {
const arrayEntry = readEntry(externalArgs, dataView, pointerToArrayEntry);
export function arrayGetMetadata(carrier, pointerToArrayEntry) {
const arrayEntry = readEntry(carrier, pointerToArrayEntry);
return arrayEntry;
}
export function arrayGetPointersToValueInIndex(externalArgs, dataView, pointerToArrayEntry, indexToGet) {
const metadata = arrayGetMetadata(externalArgs, dataView, pointerToArrayEntry); // out of bound
export function arrayGetPointersToValueInIndex(carrier, pointerToArrayEntry, indexToGet) {
const metadata = arrayGetMetadata(carrier, pointerToArrayEntry); // out of bound

@@ -18,3 +18,3 @@ if (indexToGet >= metadata.length) {

const pointerToThePointer = metadata.value + indexToGet * Uint32Array.BYTES_PER_ELEMENT;
const pointer = dataView.getUint32(pointerToThePointer);
const pointer = carrier.dataView.getUint32(pointerToThePointer);
return {

@@ -26,3 +26,3 @@ pointer,

export function getFinalValueAtArrayIndex(externalArgs, dataViewCarrier, pointerToArrayEntry, indexToGet) {
const pointers = arrayGetPointersToValueInIndex(externalArgs, dataViewCarrier.dataView, pointerToArrayEntry, indexToGet);
const pointers = arrayGetPointersToValueInIndex(dataViewCarrier, pointerToArrayEntry, indexToGet);

@@ -35,10 +35,10 @@ if (pointers === undefined) {

}
export function setValuePointerAtArrayIndex(externalArgs, dataView, pointerToArrayEntry, indexToSet, pointerToEntry) {
const pointers = arrayGetPointersToValueInIndex(externalArgs, dataView, pointerToArrayEntry, indexToSet);
export function setValuePointerAtArrayIndex(carrier, pointerToArrayEntry, indexToSet, pointerToEntry) {
const pointers = arrayGetPointersToValueInIndex(carrier, pointerToArrayEntry, indexToSet);
assertNonNull(pointers);
dataView.setUint32(pointers.pointerToThePointer, pointerToEntry);
carrier.dataView.setUint32(pointers.pointerToThePointer, pointerToEntry);
}
export function setValueAtArrayIndex(externalArgs, carrier, pointerToArrayEntry, indexToSet, value) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const pointers = arrayGetPointersToValueInIndex(externalArgs, carrier.dataView, pointerToArrayEntry, indexToSet);
const pointers = arrayGetPointersToValueInIndex(carrier, pointerToArrayEntry, indexToSet);
writeValueInPtrToPtrAndHandleMemory(externalArgs, carrier, pointers.pointerToThePointer, value);

@@ -51,3 +51,3 @@ }

export function extendArrayIfNeeded(externalArgs, carrier, pointerToArrayEntry, wishedLength) {
const metadata = arrayGetMetadata(externalArgs, carrier.dataView, pointerToArrayEntry);
const metadata = arrayGetMetadata(carrier, pointerToArrayEntry);

@@ -59,3 +59,3 @@ if (wishedLength > metadata.length) {

// no need to re-allocated, just push the length forward
writeEntry(externalArgs, carrier.dataView, pointerToArrayEntry, {
writeEntry(carrier, pointerToArrayEntry, {
type: ENTRY_TYPE.ARRAY,

@@ -74,5 +74,5 @@ refsCount: metadata.refsCount,

export function shrinkArray(externalArgs, dataView, pointerToArrayEntry, wishedLength) {
const metadata = arrayGetMetadata(externalArgs, dataView, pointerToArrayEntry);
writeEntry(externalArgs, dataView, pointerToArrayEntry, {
export function shrinkArray(externalArgs, carrier, pointerToArrayEntry, wishedLength) {
const metadata = arrayGetMetadata(carrier, pointerToArrayEntry);
writeEntry(carrier, pointerToArrayEntry, {
type: ENTRY_TYPE.ARRAY,

@@ -87,3 +87,3 @@ refsCount: metadata.refsCount,

function reallocateArray(externalArgs, carrier, pointerToArrayEntry, newAllocatedLength, newLength) {
const metadata = arrayGetMetadata(externalArgs, carrier.dataView, pointerToArrayEntry);
const metadata = arrayGetMetadata(carrier, pointerToArrayEntry);
const newArrayValueLocation = carrier.allocator.realloc(metadata.value, newAllocatedLength * Uint32Array.BYTES_PER_ELEMENT); // for (

@@ -102,3 +102,3 @@ // let memoryToCopyIndex = 0;

writeEntry(externalArgs, carrier.dataView, pointerToArrayEntry, {
writeEntry(carrier, pointerToArrayEntry, {
type: ENTRY_TYPE.ARRAY,

@@ -113,3 +113,3 @@ refsCount: metadata.refsCount,

export function arraySort(externalArgs, dataViewCarrier, pointerToArrayEntry, sortComparator = defaultCompareFunction) {
const metadata = arrayGetMetadata(externalArgs, dataViewCarrier.dataView, pointerToArrayEntry);
const metadata = arrayGetMetadata(dataViewCarrier, pointerToArrayEntry);
const pointersToValues = [...new Array(metadata.length).keys()].map(index => metadata.value + index * Uint32Array.BYTES_PER_ELEMENT).map(pointerToPointer => dataViewCarrier.dataView.getUint32(pointerToPointer));

@@ -151,4 +151,4 @@ const sortMe = pointersToValues.map(pointer => {

export function arrayReverse(externalArgs, dataView, pointerToArrayEntry) {
const metadata = arrayGetMetadata(externalArgs, dataView, pointerToArrayEntry);
export function arrayReverse(externalArgs, carrier, pointerToArrayEntry) {
const metadata = arrayGetMetadata(carrier, pointerToArrayEntry);

@@ -158,8 +158,8 @@ for (let i = 0; i < Math.floor(metadata.length / 2); i += 1) {

const a = arrayGetPointersToValueInIndex(externalArgs, dataView, pointerToArrayEntry, i); // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const a = arrayGetPointersToValueInIndex(carrier, pointerToArrayEntry, i); // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const b = arrayGetPointersToValueInIndex(externalArgs, dataView, pointerToArrayEntry, theOtherIndex);
setValuePointerAtArrayIndex(externalArgs, dataView, pointerToArrayEntry, i, b.pointer);
setValuePointerAtArrayIndex(externalArgs, dataView, pointerToArrayEntry, theOtherIndex, a.pointer);
const b = arrayGetPointersToValueInIndex(carrier, pointerToArrayEntry, theOtherIndex);
setValuePointerAtArrayIndex(carrier, pointerToArrayEntry, i, b.pointer);
setValuePointerAtArrayIndex(carrier, pointerToArrayEntry, theOtherIndex, a.pointer);
}
}

@@ -6,3 +6,3 @@ import { arrayGetMetadata, getFinalValueAtArrayIndex, shrinkArray, extendArrayIfNeeded, arrayGetPointersToValueInIndex, setValuePointerAtArrayIndex } from "./arrayHelpers";

export function arraySplice(externalArgs, dataViewCarrier, pointerToArrayEntry, startArg, deleteCountArg, ...itemsToAddArg) {
const metadata = arrayGetMetadata(externalArgs, dataViewCarrier.dataView, pointerToArrayEntry);
const metadata = arrayGetMetadata(dataViewCarrier, pointerToArrayEntry);
const calcedStart = calculateSpliceStart(metadata.length, startArg);

@@ -29,5 +29,5 @@ const calcedDeleteCount = calculateDeleteCount(metadata.length, calcedStart, deleteCountArg);

for (let writeValueToIndex = newLength - 1; writeValueToIndex >= calcedStart + itemCountChange; writeValueToIndex -= 1) {
const valueToCopyPointers = arrayGetPointersToValueInIndex(externalArgs, dataViewCarrier.dataView, pointerToArrayEntry, writeValueToIndex - itemCountChange);
const valueToCopyPointers = arrayGetPointersToValueInIndex(dataViewCarrier, pointerToArrayEntry, writeValueToIndex - itemCountChange);
assertNonNull(valueToCopyPointers);
setValuePointerAtArrayIndex(externalArgs, dataViewCarrier.dataView, pointerToArrayEntry, writeValueToIndex, valueToCopyPointers.pointer);
setValuePointerAtArrayIndex(dataViewCarrier, pointerToArrayEntry, writeValueToIndex, valueToCopyPointers.pointer);
dataViewCarrier.dataView.setUint32(valueToCopyPointers.pointerToThePointer, 0);

@@ -44,5 +44,5 @@ }

for (let writeValueToIndex = calcedStart + itemsToAddArg.length; writeValueToIndex < metadata.length + itemCountChange; writeValueToIndex += 1) {
const valueToCopyPointers = arrayGetPointersToValueInIndex(externalArgs, dataViewCarrier.dataView, pointerToArrayEntry, writeValueToIndex - itemCountChange);
const valueToCopyPointers = arrayGetPointersToValueInIndex(dataViewCarrier, pointerToArrayEntry, writeValueToIndex - itemCountChange);
assertNonNull(valueToCopyPointers);
setValuePointerAtArrayIndex(externalArgs, dataViewCarrier.dataView, pointerToArrayEntry, writeValueToIndex, valueToCopyPointers.pointer); // empty old array index, its still allocated!
setValuePointerAtArrayIndex(dataViewCarrier, pointerToArrayEntry, writeValueToIndex, valueToCopyPointers.pointer); // empty old array index, its still allocated!

@@ -63,3 +63,3 @@ dataViewCarrier.dataView.setUint32(valueToCopyPointers.pointerToThePointer, 0); // using that is wastefull

for (let i = 0; i < itemsToAddArg.length; i += 1) {
const valueToSetPointers = arrayGetPointersToValueInIndex(externalArgs, dataViewCarrier.dataView, pointerToArrayEntry, calcedStart + i);
const valueToSetPointers = arrayGetPointersToValueInIndex(dataViewCarrier, pointerToArrayEntry, calcedStart + i);
assertNonNull(valueToSetPointers);

@@ -70,3 +70,3 @@ writeValueInPtrToPtr(externalArgs, dataViewCarrier, valueToSetPointers.pointerToThePointer, itemsToAddArg[i]);

if (newLength < metadata.length) {
shrinkArray(externalArgs, dataViewCarrier.dataView, pointerToArrayEntry, newLength);
shrinkArray(externalArgs, dataViewCarrier, pointerToArrayEntry, newLength);
}

@@ -73,0 +73,0 @@

@@ -20,3 +20,3 @@ import { getFinalValueAtArrayIndex, arrayGetMetadata, setValueAtArrayIndex, arraySort, extendArrayIfNeeded, arrayReverse } from "./arrayHelpers";

if (p === "length") {
return arrayGetMetadata(this.externalArgs, this.carrier.dataView, this.entryPointer).length;
return arrayGetMetadata(this.carrier, this.entryPointer).length;
}

@@ -47,3 +47,3 @@

ownKeys() {
const length = arrayGetMetadata(this.externalArgs, this.carrier.dataView, this.entryPointer).length;
const length = arrayGetMetadata(this.carrier, this.entryPointer).length;
return [...new Array(length).keys(), "length"];

@@ -76,3 +76,3 @@ }

const length = arrayGetMetadata(this.externalArgs, this.carrier.dataView, this.entryPointer).length;
const length = arrayGetMetadata(this.carrier, this.entryPointer).length;

@@ -98,3 +98,3 @@ if (typeof p === "number") {

const currentLength = arrayGetMetadata(this.externalArgs, this.carrier.dataView, this.entryPointer).length;
const currentLength = arrayGetMetadata(this.carrier, this.entryPointer).length;

@@ -136,3 +136,3 @@ if (currentLength === value) {

index += 1;
length = arrayGetMetadata(this.externalArgs, this.carrier.dataView, this.entryPointer).length;
length = arrayGetMetadata(this.carrier, this.entryPointer).length;
} while (index < length);

@@ -148,3 +148,3 @@ }

index += 1;
length = arrayGetMetadata(this.externalArgs, this.carrier.dataView, this.entryPointer).length;
length = arrayGetMetadata(this.carrier, this.entryPointer).length;
} while (index < length);

@@ -160,3 +160,3 @@ }

index += 1;
length = arrayGetMetadata(this.externalArgs, this.carrier.dataView, this.entryPointer).length;
length = arrayGetMetadata(this.carrier, this.entryPointer).length;
} while (index < length);

@@ -178,3 +178,3 @@ }

reverse() {
arrayReverse(this.externalArgs, this.carrier.dataView, this.entryPointer);
arrayReverse(this.externalArgs, this.carrier, this.entryPointer);
return this;

@@ -193,3 +193,3 @@ } // no copy inside array is needed, so we can live with the built-in impl

this.splice(0, 0, ...elements);
return arrayGetMetadata(this.externalArgs, this.carrier.dataView, this.entryPointer).length;
return arrayGetMetadata(this.carrier, this.entryPointer).length;
}

@@ -196,0 +196,0 @@

import { ExternalArgs, DataViewAndAllocatorCarrier, InternalAPI, DateEntry, ArrayEntry, ObjectEntry, MapEntry, SetEntry } from "./interfaces";
import { IMemPool } from "@thi.ng/malloc";
export declare class BaseProxyTrap<T extends ObjectEntry | DateEntry | ArrayEntry | MapEntry | SetEntry> implements InternalAPI {

@@ -10,3 +9,3 @@ protected externalArgs: ExternalArgs;

getCarrier(): DataViewAndAllocatorCarrier;
replaceCarrierContent(dataView: DataView, allocator: IMemPool): void;
replaceCarrierContent(newCarrierContent: DataViewAndAllocatorCarrier): void;
getEntryPointer(): number;

@@ -17,3 +16,2 @@ getExternalArgs(): Readonly<{

arrayAdditionalAllocation: number;
minimumStringAllocation: number;
textDecoder: import("./textEncoderDecoderTypes").TextDecoder;

@@ -20,0 +18,0 @@ textEncoder: import("./textEncoderDecoderTypes").TextEncoder;

@@ -8,7 +8,7 @@ import { incrementRefCount, decrementRefCount, readEntry } from "./store";

this._entryPointer = _entryPointer;
incrementRefCount(this.externalArgs, this.carrier.dataView, this.entryPointer);
incrementRefCount(this.externalArgs, this.carrier, this.entryPointer);
}
destroy() {
const newRefCount = decrementRefCount(this.externalArgs, this.carrier.dataView, this.entryPointer);
const newRefCount = decrementRefCount(this.externalArgs, this.carrier, this.entryPointer);
this._entryPointer = 0;

@@ -22,5 +22,4 @@ return newRefCount;

replaceCarrierContent(dataView, allocator) {
this.carrier.dataView = dataView;
this.carrier.allocator = allocator;
replaceCarrierContent(newCarrierContent) {
Object.assign(this.carrier, newCarrierContent);
}

@@ -45,5 +44,5 @@

get entry() {
return readEntry(this.externalArgs, this.carrier.dataView, this.entryPointer);
return readEntry(this.carrier, this.entryPointer);
}
}

@@ -5,2 +5,6 @@ export declare const LOCK_OFFSET = 0;

export declare const MEM_POOL_START: number;
export declare const UNDEFINED_KNOWN_ADDRESS = 0;
export declare const NULL_KNOWN_ADDRESS = 1;
export declare const TRUE_KNOWN_ADDRESS = 2;
export declare const FALSE_KNOWN_ADDRESS = 3;
//# sourceMappingURL=consts.d.ts.map
export const LOCK_OFFSET = 0;
export const INITIAL_ENTRY_POINTER_TO_POINTER = LOCK_OFFSET + Uint32Array.BYTES_PER_ELEMENT;
export const INITIAL_ENTRY_POINTER_VALUE = INITIAL_ENTRY_POINTER_TO_POINTER + Uint32Array.BYTES_PER_ELEMENT;
export const MEM_POOL_START = INITIAL_ENTRY_POINTER_VALUE + Uint32Array.BYTES_PER_ELEMENT;
export const MEM_POOL_START = INITIAL_ENTRY_POINTER_VALUE + Uint32Array.BYTES_PER_ELEMENT;
export const UNDEFINED_KNOWN_ADDRESS = 0;
export const NULL_KNOWN_ADDRESS = 1;
export const TRUE_KNOWN_ADDRESS = 2;
export const FALSE_KNOWN_ADDRESS = 3;

@@ -49,3 +49,3 @@ import { readEntry, writeEntry } from "./store";

updateDateObjectForReuse() {
const entry = readEntry(this.externalArgs, this.carrier.dataView, this.entryPointer);
const entry = readEntry(this.carrier, this.entryPointer);
this.dateObjectForReuse.setTime(entry.value);

@@ -55,3 +55,3 @@ }

persistDateObject() {
writeEntry(this.externalArgs, this.carrier.dataView, this.entryPointer, {
writeEntry(this.carrier, this.entryPointer, {
type: ENTRY_TYPE.DATE,

@@ -58,0 +58,0 @@ refsCount: this.entry.refsCount,

import { getInternalAPI } from "./utils";
import { getCacheFor } from "./externalObjectsCache";
import { getAllLinkedAddresses } from "./getAllLinkedAddresses";
import { decrementRefCount } from "./store";
/**

@@ -16,8 +17,12 @@ * Dispose the given objectWrapper, and re-claim the memory

if (newRefCount === 0) {
const addressesToFree = getAllLinkedAddresses(internalApi.getExternalArgs(), internalApi.getCarrier().dataView, false, entryPointer);
const addressesToFree = getAllLinkedAddresses(internalApi.getCarrier(), false, entryPointer);
for (const address of addressesToFree) {
for (const address of addressesToFree.leafAddresses) {
internalApi.getCarrier().allocator.free(address);
}
for (const address of addressesToFree.arcAddresses) {
decrementRefCount(internalApi.getExternalArgs(), internalApi.getCarrier(), address);
}
return true;

@@ -24,0 +29,0 @@ }

export declare enum ENTRY_TYPE {
/**
* @deprecated
*/
UNDEFINED = 0,
/**
* @deprecated
*/
NULL = 1,

@@ -8,4 +14,10 @@ NUMBER = 2,

STRING = 5,
/**
* @deprecated
*/
BOOLEAN = 6,
OBJECT = 7,
/**
* @deprecated
*/
OBJECT_PROP = 8,

@@ -18,4 +30,4 @@ ARRAY = 9,

}
export declare const PRIMITIVE_TYPES: readonly [ENTRY_TYPE.NULL, ENTRY_TYPE.UNDEFINED, ENTRY_TYPE.NUMBER, ENTRY_TYPE.BIGINT_POSITIVE, ENTRY_TYPE.BIGINT_NEGATIVE, ENTRY_TYPE.BOOLEAN, ENTRY_TYPE.STRING];
export declare const isPrimitiveEntryType: (v: unknown) => v is ENTRY_TYPE.UNDEFINED | ENTRY_TYPE.NULL | ENTRY_TYPE.NUMBER | ENTRY_TYPE.BIGINT_POSITIVE | ENTRY_TYPE.BIGINT_NEGATIVE | ENTRY_TYPE.STRING | ENTRY_TYPE.BOOLEAN;
export declare const PRIMITIVE_TYPES: readonly [ENTRY_TYPE.NUMBER, ENTRY_TYPE.BIGINT_POSITIVE, ENTRY_TYPE.BIGINT_NEGATIVE, ENTRY_TYPE.STRING];
export declare const isPrimitiveEntryType: (v: unknown) => v is ENTRY_TYPE.NUMBER | ENTRY_TYPE.BIGINT_POSITIVE | ENTRY_TYPE.BIGINT_NEGATIVE | ENTRY_TYPE.STRING;
//# sourceMappingURL=entry-types.d.ts.map

@@ -21,3 +21,3 @@ import { createKnownTypeGuard } from "./utils";

export const PRIMITIVE_TYPES = [ENTRY_TYPE.NULL, ENTRY_TYPE.UNDEFINED, ENTRY_TYPE.NUMBER, ENTRY_TYPE.BIGINT_POSITIVE, ENTRY_TYPE.BIGINT_NEGATIVE, ENTRY_TYPE.BOOLEAN, ENTRY_TYPE.STRING];
export const PRIMITIVE_TYPES = [ENTRY_TYPE.NUMBER, ENTRY_TYPE.BIGINT_POSITIVE, ENTRY_TYPE.BIGINT_NEGATIVE, ENTRY_TYPE.STRING];
export const isPrimitiveEntryType = createKnownTypeGuard(PRIMITIVE_TYPES);

@@ -9,3 +9,4 @@ import { ENTRY_TYPE, isPrimitiveEntryType } from "./entry-types";

import { createMapWrapper } from "./mapWrapper";
import { createSetWrapper } from "./setWrapper"; // declare const FinalizationGroup: any;
import { createSetWrapper } from "./setWrapper";
import { UNDEFINED_KNOWN_ADDRESS, NULL_KNOWN_ADDRESS, TRUE_KNOWN_ADDRESS, FALSE_KNOWN_ADDRESS } from "./consts"; // declare const FinalizationGroup: any;
// declare const WeakRef: any;

@@ -21,12 +22,20 @@

export function entryToFinalJavaScriptValue(externalArgs, carrier, pointerToEntry) {
const valueEntry = readEntry(externalArgs, carrier.dataView, pointerToEntry);
if (pointerToEntry === UNDEFINED_KNOWN_ADDRESS) {
return undefined;
}
if (valueEntry.type === ENTRY_TYPE.NULL) {
if (pointerToEntry === NULL_KNOWN_ADDRESS) {
return null;
}
if (valueEntry.type === ENTRY_TYPE.UNDEFINED) {
return undefined;
if (pointerToEntry === TRUE_KNOWN_ADDRESS) {
return true;
}
if (pointerToEntry === FALSE_KNOWN_ADDRESS) {
return false;
}
const valueEntry = readEntry(carrier, pointerToEntry);
if (isPrimitiveEntryType(valueEntry.type)) {

@@ -56,11 +65,15 @@ return valueEntry.value;

function finalizer(memoryAddress, carrier, externalArgs) {
const newRefsCount = decrementRefCount(externalArgs, carrier.dataView, memoryAddress);
const newRefsCount = decrementRefCount(externalArgs, carrier, memoryAddress);
if (newRefsCount === 0) {
const freeUs = getAllLinkedAddresses(externalArgs, carrier.dataView, false, memoryAddress);
const freeUs = getAllLinkedAddresses(carrier, false, memoryAddress);
for (const address of freeUs) {
for (const address of freeUs.leafAddresses) {
carrier.allocator.free(address);
}
for (const address of freeUs.arcAddresses) {
decrementRefCount(externalArgs, carrier, address);
}
}
}

@@ -1,3 +0,7 @@

import { ExternalArgs } from "./interfaces";
export declare function getAllLinkedAddresses(externalArgs: ExternalArgs, dataView: DataView, ignoreRefCount: boolean, entryPointer: number): number[];
import { DataViewAndAllocatorCarrier } from "./interfaces";
export declare function getAllLinkedAddresses(carrier: DataViewAndAllocatorCarrier, ignoreRefCount: boolean, entryPointer: number): {
leafAddresses: number[];
arcAddresses: number[];
};
export declare function getObjectOrMapOrSetAddresses(carrier: DataViewAndAllocatorCarrier, ignoreRefCount: boolean, internalHashmapPointer: number, leafAddresses: number[], arcAddresses: number[]): void;
//# sourceMappingURL=getAllLinkedAddresses.d.ts.map
import { readEntry } from "./store";
import { ENTRY_TYPE } from "./entry-types";
import { hashMapGetPointersToFree } from "./hashmap/hashmap";
export function getAllLinkedAddresses(externalArgs, dataView, ignoreRefCount, entryPointer) {
const allAddresses = [];
getAllLinkedAddressesStep(externalArgs, dataView, ignoreRefCount, entryPointer, allAddresses);
return allAddresses;
import { isKnownAddressValuePointer } from "./utils";
export function getAllLinkedAddresses(carrier, ignoreRefCount, entryPointer) {
const leafAddresses = [];
const arcAddresses = [];
getAllLinkedAddressesStep(carrier, ignoreRefCount, entryPointer, leafAddresses, arcAddresses);
return {
leafAddresses,
arcAddresses
};
}
function getAllLinkedAddressesStep(externalArgs, dataView, ignoreRefCount, entryPointer, pushTo) {
if (entryPointer === 0) {
function getAllLinkedAddressesStep(carrier, ignoreRefCount, entryPointer, leafAddresses, arcAddresses) {
if (isKnownAddressValuePointer(entryPointer)) {
return;
}
const entry = readEntry(externalArgs, dataView, entryPointer);
const entry = readEntry(carrier, entryPointer);
switch (entry.type) {
case ENTRY_TYPE.UNDEFINED:
case ENTRY_TYPE.NULL:
case ENTRY_TYPE.BOOLEAN:
case ENTRY_TYPE.NUMBER:

@@ -25,9 +27,13 @@ case ENTRY_TYPE.STRING:

case ENTRY_TYPE.BIGINT_POSITIVE:
pushTo.push(entryPointer);
leafAddresses.push(entryPointer);
break;
case ENTRY_TYPE.OBJECT:
if (entry.refsCount < 2 || ignoreRefCount) {
pushTo.push(entryPointer);
getObjectAddresses(externalArgs, dataView, ignoreRefCount, entry, pushTo);
case ENTRY_TYPE.MAP:
case ENTRY_TYPE.SET:
if (entry.refsCount <= 1 || ignoreRefCount) {
leafAddresses.push(entryPointer);
getObjectOrMapOrSetAddresses(carrier, ignoreRefCount, entry.value, leafAddresses, arcAddresses);
} else {
arcAddresses.push(entryPointer);
}

@@ -38,13 +44,12 @@

case ENTRY_TYPE.ARRAY:
if (entry.refsCount < 2 || ignoreRefCount) {
pushTo.push(entryPointer);
pushTo.push(entry.value);
if (entry.refsCount <= 1 || ignoreRefCount) {
leafAddresses.push(entryPointer);
leafAddresses.push(entry.value);
for (let i = 0; i < entry.allocatedLength; i += 1) {
const valuePointer = dataView.getUint32(entry.value + i * Uint32Array.BYTES_PER_ELEMENT);
if (valuePointer !== 0) {
getAllLinkedAddressesStep(externalArgs, dataView, ignoreRefCount, valuePointer, pushTo);
}
const valuePointer = carrier.dataView.getUint32(entry.value + i * Uint32Array.BYTES_PER_ELEMENT);
getAllLinkedAddressesStep(carrier, ignoreRefCount, valuePointer, leafAddresses, arcAddresses);
}
} else {
arcAddresses.push(entryPointer);
}

@@ -55,4 +60,6 @@

case ENTRY_TYPE.DATE:
if (entry.refsCount < 2 || ignoreRefCount) {
pushTo.push(entryPointer);
if (entry.refsCount <= 1 || ignoreRefCount) {
leafAddresses.push(entryPointer);
} else {
arcAddresses.push(entryPointer);
}

@@ -69,12 +76,12 @@

function getObjectAddresses(externalArgs, dataView, ignoreRefCount, objectEntry, pushTo) {
export function getObjectOrMapOrSetAddresses(carrier, ignoreRefCount, internalHashmapPointer, leafAddresses, arcAddresses) {
const {
pointersToValuePointers,
pointers
} = hashMapGetPointersToFree(dataView, objectEntry.value);
pushTo.push(...pointers);
} = hashMapGetPointersToFree(carrier.dataView, internalHashmapPointer);
leafAddresses.push(...pointers);
for (const pointer of pointersToValuePointers) {
getAllLinkedAddressesStep(externalArgs, dataView, ignoreRefCount, dataView.getUint32(pointer), pushTo);
getAllLinkedAddressesStep(carrier, ignoreRefCount, carrier.dataView.getUint32(pointer), leafAddresses, arcAddresses);
}
}

@@ -10,12 +10,12 @@ import { DataViewAndAllocatorCarrier, ExternalArgs } from "../interfaces";

*/
export declare function hashMapInsertUpdate(externalArgs: ExternalArgs, { dataView, allocator }: DataViewAndAllocatorCarrier, mapPointer: number, externalKeyValue: number | string): number;
export declare function hashMapInsertUpdate(externalArgs: ExternalArgs, carrier: DataViewAndAllocatorCarrier, mapPointer: number, externalKeyValue: number | string): number;
/**
* @returns pointer of the pointer to the found node
*/
export declare function hashMapNodeLookup(externalArgs: ExternalArgs, dataView: DataView, mapPointer: number, externalKeyValue: number | string): number;
export declare function hashMapValueLookup(externalArgs: ExternalArgs, dataView: DataView, mapPointer: number, externalKeyValue: number | string): number;
export declare function hashMapNodeLookup(carrier: DataViewAndAllocatorCarrier, mapPointer: number, externalKeyValue: number | string): number;
export declare function hashMapValueLookup(carrier: DataViewAndAllocatorCarrier, mapPointer: number, externalKeyValue: number | string): number;
/**
* @returns the value pointer of the deleted key
*/
export declare function hashMapDelete(externalArgs: ExternalArgs, { dataView, allocator }: DataViewAndAllocatorCarrier, mapPointer: number, externalKeyValue: number | string): number;
export declare function hashMapDelete(carrier: DataViewAndAllocatorCarrier, mapPointer: number, externalKeyValue: number | string): number;
/**

@@ -22,0 +22,0 @@ *

@@ -27,23 +27,18 @@ import { MAP_MACHINE, NODE_MACHINE } from "./memoryLayout";

export function hashMapInsertUpdate(externalArgs, {
dataView,
allocator
}, mapPointer, externalKeyValue) {
const mapOperator = MAP_MACHINE.createOperator(dataView, mapPointer);
const keyEntry = primitiveValueToEntry({ ...externalArgs,
minimumStringAllocation: 0
}, externalKeyValue, 0); // allocate all possible needed memory upfront, so we won't oom in the middle of something
export function hashMapInsertUpdate(externalArgs, carrier, mapPointer, externalKeyValue) {
const mapOperator = MAP_MACHINE.createOperator(carrier.dataView, mapPointer);
const keyEntry = primitiveValueToEntry(externalKeyValue); // allocate all possible needed memory upfront, so we won't oom in the middle of something
// in case of overwrite, we will not need this memory
const memoryForNewNode = allocator.calloc(NODE_MACHINE.map.SIZE_OF);
const memoryForNewNode = carrier.allocator.calloc(NODE_MACHINE.map.SIZE_OF);
const memorySizeOfKey = sizeOfEntry(keyEntry);
const keyEntryMemory = allocator.calloc(memorySizeOfKey);
writeEntry(externalArgs, dataView, keyEntryMemory, keyEntry);
const keyEntryMemory = carrier.allocator.calloc(memorySizeOfKey);
writeEntry(carrier, keyEntryMemory, keyEntry);
const keyHeaderOverhead = keyEntry.type === ENTRY_TYPE.STRING ? 5 : 1;
const keyHashCode = hashCodeInPlace(dataView, mapOperator.get("CAPACITY"), // + 1 for the type of key
const keyHashCode = hashCodeInPlace(carrier.dataView, mapOperator.get("CAPACITY"), // + 1 for the type of key
keyEntryMemory + keyHeaderOverhead, memorySizeOfKey - keyHeaderOverhead);
let ptrToPtrToSaveTheNodeTo = mapOperator.get("ARRAY_POINTER") + keyHashCode * Uint32Array.BYTES_PER_ELEMENT;
const commonNodeOperator = NODE_MACHINE.createOperator(dataView, dataView.getUint32(ptrToPtrToSaveTheNodeTo)); // todo: share code with hashMapNodeLookup?
const commonNodeOperator = NODE_MACHINE.createOperator(carrier.dataView, carrier.dataView.getUint32(ptrToPtrToSaveTheNodeTo)); // todo: share code with hashMapNodeLookup?
while (commonNodeOperator.startAddress !== 0 && !compareStringOrNumberEntriesInPlace(dataView, commonNodeOperator.get("KEY_POINTER"), keyEntryMemory)) {
while (commonNodeOperator.startAddress !== 0 && !compareStringOrNumberEntriesInPlace(carrier.dataView, commonNodeOperator.get("KEY_POINTER"), keyEntryMemory)) {
ptrToPtrToSaveTheNodeTo = commonNodeOperator.pointerTo("NEXT_NODE_POINTER");

@@ -61,4 +56,4 @@ commonNodeOperator.startAddress = commonNodeOperator.get("NEXT_NODE_POINTER");

// we don't need the new memory
allocator.free(keyEntryMemory);
allocator.free(memoryForNewNode);
carrier.allocator.free(keyEntryMemory);
carrier.allocator.free(memoryForNewNode);
return commonNodeOperator.pointerTo("VALUE_POINTER");

@@ -68,7 +63,4 @@ } else {

commonNodeOperator.set("KEY_POINTER", keyEntryMemory);
commonNodeOperator.set("LINKED_LIST_ITEM_POINTER", linkedListItemInsert({
dataView,
allocator
}, mapOperator.get("LINKED_LIST_POINTER"), memoryForNewNode));
dataView.setUint32(ptrToPtrToSaveTheNodeTo, memoryForNewNode);
commonNodeOperator.set("LINKED_LIST_ITEM_POINTER", linkedListItemInsert(carrier, mapOperator.get("LINKED_LIST_POINTER"), memoryForNewNode));
carrier.dataView.setUint32(ptrToPtrToSaveTheNodeTo, memoryForNewNode);
mapOperator.set("LINKED_LIST_SIZE", mapOperator.get("LINKED_LIST_SIZE") + 1);

@@ -80,6 +72,3 @@

// });
hashMapRehash({
dataView,
allocator
}, mapOperator, mapOperator.get("CAPACITY") * 2);
hashMapRehash(carrier, mapOperator, mapOperator.get("CAPACITY") * 2);
}

@@ -94,10 +83,10 @@

export function hashMapNodeLookup(externalArgs, dataView, mapPointer, externalKeyValue) {
const mapMachine = MAP_MACHINE.createOperator(dataView, mapPointer);
const keyHashCode = hashCodeExternalValue(externalArgs, mapMachine.get("CAPACITY"), externalKeyValue);
export function hashMapNodeLookup(carrier, mapPointer, externalKeyValue) {
const mapMachine = MAP_MACHINE.createOperator(carrier.dataView, mapPointer);
const keyHashCode = hashCodeExternalValue(mapMachine.get("CAPACITY"), externalKeyValue);
let ptrToPtr = mapMachine.get("ARRAY_POINTER") + keyHashCode * Uint32Array.BYTES_PER_ELEMENT;
const node = NODE_MACHINE.createOperator(dataView, dataView.getUint32(ptrToPtr));
const node = NODE_MACHINE.createOperator(carrier.dataView, carrier.dataView.getUint32(ptrToPtr));
while (node.startAddress !== 0) {
const keyEntry = readEntry(externalArgs, dataView, node.get("KEY_POINTER"));
const keyEntry = readEntry(carrier, node.get("KEY_POINTER"));

@@ -114,4 +103,4 @@ if (keyEntry.value === externalKeyValue) {

}
export function hashMapValueLookup(externalArgs, dataView, mapPointer, externalKeyValue) {
const nodePtrToPtr = hashMapNodeLookup(externalArgs, dataView, mapPointer, externalKeyValue);
export function hashMapValueLookup(carrier, mapPointer, externalKeyValue) {
const nodePtrToPtr = hashMapNodeLookup(carrier, mapPointer, externalKeyValue);

@@ -122,3 +111,3 @@ if (nodePtrToPtr === 0) {

const node = NODE_MACHINE.createOperator(dataView, dataView.getUint32(nodePtrToPtr));
const node = NODE_MACHINE.createOperator(carrier.dataView, carrier.dataView.getUint32(nodePtrToPtr));
return node.pointerTo("VALUE_POINTER");

@@ -130,7 +119,4 @@ }

export function hashMapDelete(externalArgs, {
dataView,
allocator
}, mapPointer, externalKeyValue) {
const foundNodePtrToPtr = hashMapNodeLookup(externalArgs, dataView, mapPointer, externalKeyValue);
export function hashMapDelete(carrier, mapPointer, externalKeyValue) {
const foundNodePtrToPtr = hashMapNodeLookup(carrier, mapPointer, externalKeyValue);

@@ -141,14 +127,11 @@ if (foundNodePtrToPtr === 0) {

const nodeToDeletePointer = dataView.getUint32(foundNodePtrToPtr);
const nodeOperator = NODE_MACHINE.createOperator(dataView, nodeToDeletePointer);
const nodeToDeletePointer = carrier.dataView.getUint32(foundNodePtrToPtr);
const nodeOperator = NODE_MACHINE.createOperator(carrier.dataView, nodeToDeletePointer);
const valuePointer = nodeOperator.pointerTo("VALUE_POINTER");
linkedListItemRemove({
dataView,
allocator
}, nodeOperator.get("LINKED_LIST_ITEM_POINTER")); // remove node from bucket
linkedListItemRemove(carrier, nodeOperator.get("LINKED_LIST_ITEM_POINTER")); // remove node from bucket
dataView.setUint32(foundNodePtrToPtr, nodeOperator.get("NEXT_NODE_POINTER"));
allocator.free(nodeOperator.get("KEY_POINTER"));
allocator.free(nodeOperator.startAddress);
dataView.setUint32(mapPointer + MAP_MACHINE.map.LINKED_LIST_SIZE.bytesOffset, dataView.getUint32(mapPointer + MAP_MACHINE.map.LINKED_LIST_SIZE.bytesOffset) - 1);
carrier.dataView.setUint32(foundNodePtrToPtr, nodeOperator.get("NEXT_NODE_POINTER"));
carrier.allocator.free(nodeOperator.get("KEY_POINTER"));
carrier.allocator.free(nodeOperator.startAddress);
carrier.dataView.setUint32(mapPointer + MAP_MACHINE.map.LINKED_LIST_SIZE.bytesOffset, carrier.dataView.getUint32(mapPointer + MAP_MACHINE.map.LINKED_LIST_SIZE.bytesOffset) - 1);
return valuePointer;

@@ -234,15 +217,13 @@ }

function hashMapRehashInsert({
dataView
}, bucketsArrayPointer, arraySize, nodePointer) {
const nodeOperator = NODE_MACHINE.createOperator(dataView, nodePointer);
const keyInfo = getKeyStartLength(dataView, nodeOperator.get("KEY_POINTER"));
const keyHashCode = hashCodeInPlace(dataView, arraySize, keyInfo.start, keyInfo.length);
function hashMapRehashInsert(carrier, bucketsArrayPointer, arraySize, nodePointer) {
const nodeOperator = NODE_MACHINE.createOperator(carrier.dataView, nodePointer);
const keyInfo = getKeyStartLength(carrier.dataView, nodeOperator.get("KEY_POINTER"));
const keyHashCode = hashCodeInPlace(carrier.dataView, arraySize, keyInfo.start, keyInfo.length);
const bucket = keyHashCode % arraySize;
const bucketStartPointer = bucketsArrayPointer + bucket * Uint32Array.BYTES_PER_ELEMENT;
const prevFirstNodeInBucket = dataView.getUint32(bucketStartPointer);
dataView.setUint32(bucketStartPointer, nodePointer);
const prevFirstNodeInBucket = carrier.dataView.getUint32(bucketStartPointer);
carrier.dataView.setUint32(bucketStartPointer, nodePointer);
nodeOperator.set("NEXT_NODE_POINTER", prevFirstNodeInBucket); // // Add is first node in bucket
// if (nodeOperator.startAddress === 0) {
// dataView.setUint32(bucketStartPointer, nodePointer);
// carrier.dataView.setUint32(bucketStartPointer, nodePointer);
// return;

@@ -249,0 +230,0 @@ // }

@@ -1,4 +0,3 @@

import { ExternalArgs } from "../interfaces";
export declare function hashCodeInPlace(dataView: DataView, capacity: number, keyStart: number, keyBytesLength: number): number;
export declare function hashCodeExternalValue(externalArgs: ExternalArgs, capacity: number, value: string | number): number;
export declare function hashCodeExternalValue(capacity: number, value: string | number): number;
export declare function hashCodeEntry(dataView: DataView, capacity: number, pointer: number): number;

@@ -5,0 +4,0 @@ export declare function getKeyStartLength(dataView: DataView, keyPointer: number): {

import { ENTRY_TYPE } from "../entry-types";
import { stringEncodeInto } from "../stringEncodeInto";
export function hashCodeInPlace(dataView, capacity, keyStart, keyBytesLength) {

@@ -13,13 +14,14 @@ let h = 0 | 0; // const hashed: number[] = [];

}
export function hashCodeExternalValue(externalArgs, capacity, value) {
let dv;
export function hashCodeExternalValue(capacity, value) {
const ab = new ArrayBuffer(typeof value === "string" ? value.length * 3 : 8);
const dv = new DataView(ab);
let keyBytesLength = ab.byteLength;
if (typeof value === "string") {
dv = new DataView(externalArgs.textEncoder.encode(value).buffer);
keyBytesLength = stringEncodeInto(new Uint8Array(ab), 0, value);
} else {
dv = new DataView(new ArrayBuffer(8));
dv.setFloat64(0, value);
}
return hashCodeInPlace(dv, capacity, 0, dv.buffer.byteLength);
return hashCodeInPlace(dv, capacity, 0, keyBytesLength);
}

@@ -26,0 +28,0 @@ export function hashCodeEntry(dataView, capacity, pointer) {

import { ENTRY_TYPE } from "./entry-types";
import { TextDecoder, TextEncoder } from "./textEncoderDecoderTypes";
import { IMemPool } from "@thi.ng/malloc";
export declare type primitive = string | number | bigint | boolean | undefined | null;
export declare type Entry = NullEntry | NullUndefined | BooleanEntry | StringEntry | NumberEntry | BigIntPositiveEntry | BigIntNegativeEntry | ObjectEntry | ArrayEntry | DateEntry | MapEntry | SetEntry;
export interface NullEntry {
type: ENTRY_TYPE.NULL;
}
export interface NullUndefined {
type: ENTRY_TYPE.UNDEFINED;
}
export interface BooleanEntry {
type: ENTRY_TYPE.BOOLEAN;
value: boolean;
}
export declare type Entry = StringEntry | NumberEntry | BigIntPositiveEntry | BigIntNegativeEntry | ObjectEntry | ArrayEntry | DateEntry | MapEntry | SetEntry;
export interface StringEntry {

@@ -74,2 +63,7 @@ type: ENTRY_TYPE.STRING;

dataView: DataView;
uint8: Uint8Array;
uint16: Uint16Array;
uint32: Uint32Array;
float64: Float64Array;
bigUint64: BigUint64Array;
allocator: import("@thi.ng/malloc").IMemPool;

@@ -81,3 +75,2 @@ }

arrayAdditionalAllocation: number;
minimumStringAllocation: number;
textDecoder: TextDecoder;

@@ -90,3 +83,2 @@ textEncoder: TextEncoder;

arrayAdditionalAllocation?: number;
minimumStringAllocation?: number;
textDecoder: TextDecoder;

@@ -98,3 +90,3 @@ textEncoder: TextEncoder;

getCarrier(): Readonly<DataViewAndAllocatorCarrier>;
replaceCarrierContent(dataView: DataView, pool: IMemPool): void;
replaceCarrierContent(carrier: DataViewAndAllocatorCarrier): void;
getEntryPointer(): number;

@@ -101,0 +93,0 @@ destroy(): number;

@@ -1,2 +0,2 @@

import { deleteObjectPropertyEntryByKey, objectGet, objectSet } from "./objectWrapperHelpers";
import { deleteObjectPropertyEntryByKey, objectGet, objectSet, mapOrSetClear } from "./objectWrapperHelpers";
import { INTERNAL_API_SYMBOL } from "./symbols";

@@ -9,6 +9,3 @@ import { allocationsTransaction } from "./allocationsTransaction";

clear() {
// @todo impl using helper function with direct list access
for (const key of [...this.keys()]) {
this.delete(key);
}
mapOrSetClear(this.externalArgs, this.carrier, this.entryPointer);
}

@@ -89,3 +86,3 @@

return hashMapNodeLookup(this.externalArgs, this.carrier.dataView, this.entry.value, p) !== 0;
return hashMapNodeLookup(this.carrier, this.entry.value, p) !== 0;
}

@@ -92,0 +89,0 @@

@@ -29,3 +29,3 @@ import { getObjectPropertiesEntries, deleteObjectPropertyEntryByKey, objectGet, objectSet } from "./objectWrapperHelpers";

enumerate() {
const gotEntries = getObjectPropertiesEntries(this.externalArgs, this.carrier.dataView, this.entry.value);
const gotEntries = getObjectPropertiesEntries(this.carrier, this.entry.value);
return gotEntries.map(e => e.key);

@@ -35,3 +35,3 @@ }

ownKeys() {
const gotEntries = getObjectPropertiesEntries(this.externalArgs, this.carrier.dataView, this.entry.value);
const gotEntries = getObjectPropertiesEntries(this.carrier, this.entry.value);
return gotEntries.map(e => e.key);

@@ -60,3 +60,3 @@ }

return hashMapNodeLookup(this.externalArgs, this.carrier.dataView, this.entry.value, p) !== 0;
return hashMapNodeLookup(this.carrier, this.entry.value, p) !== 0;
}

@@ -63,0 +63,0 @@

import { ExternalArgs, DataViewAndAllocatorCarrier } from "./interfaces";
export declare function deleteObjectPropertyEntryByKey(externalArgs: ExternalArgs, { dataView, allocator }: DataViewAndAllocatorCarrier, hashmapPointer: number, keyToDeleteBy: string | number): boolean;
export declare function getObjectPropertiesEntries(externalArgs: ExternalArgs, dataView: DataView, hashmapPointer: number): Array<{
export declare function deleteObjectPropertyEntryByKey(externalArgs: ExternalArgs, carrier: DataViewAndAllocatorCarrier, hashmapPointer: number, keyToDeleteBy: string | number): boolean;
export declare function getObjectPropertiesEntries(carrier: DataViewAndAllocatorCarrier, hashmapPointer: number): Array<{
key: string | number;

@@ -9,2 +9,4 @@ valuePointer: number;

export declare function objectGet(externalArgs: ExternalArgs, carrier: DataViewAndAllocatorCarrier, entryPointer: number, key: string | number): any;
export declare function hashmapClearFree(externalArgs: ExternalArgs, carrier: DataViewAndAllocatorCarrier, hashmapPointer: number): void;
export declare function mapOrSetClear(externalArgs: ExternalArgs, carrier: DataViewAndAllocatorCarrier, mapOrSetPtr: number): void;
//# sourceMappingURL=objectWrapperHelpers.d.ts.map

@@ -1,52 +0,28 @@

import { readEntry, decrementRefCount, writeValueInPtrToPtrAndHandleMemory } from "./store";
import { readEntry, writeValueInPtrToPtrAndHandleMemory, handleArcForDeletedValuePointer, decrementRefCount, writeEntry } from "./store";
import { entryToFinalJavaScriptValue } from "./entryToFinalJavaScriptValue";
import { hashMapDelete, hashMapLowLevelIterator, hashMapNodePointerToKeyValue, hashMapInsertUpdate, hashMapValueLookup } from "./hashmap/hashmap";
import { getAllLinkedAddresses } from "./getAllLinkedAddresses";
export function deleteObjectPropertyEntryByKey(externalArgs, {
dataView,
allocator
}, hashmapPointer, keyToDeleteBy) {
const deletedValuePointerToPointer = hashMapDelete(externalArgs, {
dataView,
allocator
}, hashmapPointer, keyToDeleteBy);
const deletedValuePointer = dataView.getUint32(deletedValuePointerToPointer); // Nothing to delete here
import { hashMapDelete, hashMapLowLevelIterator, hashMapNodePointerToKeyValue, hashMapInsertUpdate, hashMapValueLookup, createHashMap } from "./hashmap/hashmap";
import { getObjectOrMapOrSetAddresses } from "./getAllLinkedAddresses";
export function deleteObjectPropertyEntryByKey(externalArgs, carrier, hashmapPointer, keyToDeleteBy) {
const deletedValuePointerToPointer = hashMapDelete(carrier, hashmapPointer, keyToDeleteBy); // no such key
if (deletedValuePointer === 0) {
if (deletedValuePointerToPointer === 0) {
return false;
} // handle memory free
if (deletedValuePointer !== 0) {
const existingValueEntry = readEntry(externalArgs, dataView, deletedValuePointer);
if (existingValueEntry && "refsCount" in existingValueEntry) {
const newRefCount = decrementRefCount(externalArgs, dataView, deletedValuePointer);
if (newRefCount === 0) {
const addressesToFree = getAllLinkedAddresses(externalArgs, dataView, false, deletedValuePointer);
for (const address of addressesToFree) {
allocator.free(address);
}
}
} else {
allocator.free(deletedValuePointer);
}
}
const deletedValuePointer = carrier.dataView.getUint32(deletedValuePointerToPointer);
handleArcForDeletedValuePointer(externalArgs, carrier, deletedValuePointer);
return true;
}
export function getObjectPropertiesEntries(externalArgs, dataView, hashmapPointer) {
export function getObjectPropertiesEntries(carrier, hashmapPointer) {
let iterator = 0;
const foundValues = [];
while (iterator = hashMapLowLevelIterator(dataView, hashmapPointer, iterator)) {
while (iterator = hashMapLowLevelIterator(carrier.dataView, hashmapPointer, iterator)) {
const {
valuePointer,
keyPointer
} = hashMapNodePointerToKeyValue(dataView, iterator);
const keyEntry = readEntry(externalArgs, dataView, keyPointer);
} = hashMapNodePointerToKeyValue(carrier.dataView, iterator);
const keyEntry = readEntry(carrier, keyPointer);
foundValues.push({
valuePointer: dataView.getUint32(valuePointer),
valuePointer: carrier.dataView.getUint32(valuePointer),
key: keyEntry.value

@@ -63,3 +39,3 @@ });

export function objectGet(externalArgs, carrier, entryPointer, key) {
const valuePointer = hashMapValueLookup(externalArgs, carrier.dataView, entryPointer, key);
const valuePointer = hashMapValueLookup(carrier, entryPointer, key);

@@ -71,2 +47,21 @@ if (valuePointer === 0) {

return entryToFinalJavaScriptValue(externalArgs, carrier, carrier.dataView.getUint32(valuePointer));
}
export function hashmapClearFree(externalArgs, carrier, hashmapPointer) {
const leafAddresses = [];
const arcAddresses = [];
getObjectOrMapOrSetAddresses(carrier, false, hashmapPointer, leafAddresses, arcAddresses);
for (const address of leafAddresses) {
carrier.allocator.free(address);
}
for (const address of arcAddresses) {
decrementRefCount(externalArgs, carrier, address);
}
}
export function mapOrSetClear(externalArgs, carrier, mapOrSetPtr) {
const entry = readEntry(carrier, mapOrSetPtr);
hashmapClearFree(externalArgs, carrier, entry.value);
entry.value = createHashMap(carrier, externalArgs.hashMapMinInitialCapacity);
writeEntry(carrier, mapOrSetPtr, entry);
}
import { ExternalArgs, DataViewAndAllocatorCarrier } from "./interfaces";
/**
* Returns pointer for the value
*/
export declare function saveValue(externalArgs: ExternalArgs, carrier: DataViewAndAllocatorCarrier, referencedPointers: number[], value: any): number;
//# sourceMappingURL=saveValue.d.ts.map

@@ -6,2 +6,7 @@ import { primitiveValueToEntry, isPrimitive, getOurPointerIfApplicable } from "./utils";

import { ENTRY_TYPE } from "./entry-types";
import { UNDEFINED_KNOWN_ADDRESS, NULL_KNOWN_ADDRESS, TRUE_KNOWN_ADDRESS, FALSE_KNOWN_ADDRESS } from "./consts";
/**
* Returns pointer for the value
*/
export function saveValue(externalArgs, carrier, referencedPointers, value) {

@@ -11,4 +16,20 @@ let valuePointer = 0;

if (value === undefined) {
return UNDEFINED_KNOWN_ADDRESS;
}
if (value === null) {
return NULL_KNOWN_ADDRESS;
}
if (value === true) {
return TRUE_KNOWN_ADDRESS;
}
if (value === false) {
return FALSE_KNOWN_ADDRESS;
}
if (isPrimitive(value)) {
const entry = primitiveValueToEntry(externalArgs, value, externalArgs.minimumStringAllocation);
const entry = primitiveValueToEntry(value);
valuePointer = appendEntry(externalArgs, carrier, entry);

@@ -15,0 +36,0 @@ } else if (maybeOurPointer = getOurPointerIfApplicable(value, carrier.dataView)) {

@@ -1,2 +0,2 @@

import { deleteObjectPropertyEntryByKey, objectSet } from "./objectWrapperHelpers";
import { deleteObjectPropertyEntryByKey, objectSet, mapOrSetClear } from "./objectWrapperHelpers";
import { INTERNAL_API_SYMBOL } from "./symbols";

@@ -9,6 +9,3 @@ import { allocationsTransaction } from "./allocationsTransaction";

clear() {
// @todo impl using helper function with direct list access
for (const key of [...this.keys()]) {
this.delete(key);
}
mapOrSetClear(this.externalArgs, this.carrier, this.entryPointer);
}

@@ -69,3 +66,3 @@

return hashMapNodeLookup(this.externalArgs, this.carrier.dataView, this.entry.value, p) !== 0;
return hashMapNodeLookup(this.carrier, this.entry.value, p) !== 0;
}

@@ -72,0 +69,0 @@

@@ -72,4 +72,11 @@ import { externalArgsApiToExternalArgsApi, isPrimitive, primitiveValueToEntry, align } from "./utils";

export function sizeOfValue(externalArgs, value) {
if (value === undefined || value === null || value === true || value === false) {
return {
memoryAllocated: 0,
numberOfAllocations: 0
};
}
if (isPrimitive(value)) {
const entry = primitiveValueToEntry(externalArgs, value, externalArgs.minimumStringAllocation);
const entry = primitiveValueToEntry(value);
return {

@@ -107,3 +114,3 @@ memoryAllocated: align(sizeOfEntry(entry)),

const hashMapNodesAllocationsSize = align(NODE_MACHINE.map.SIZE_OF) * keysArray.length;
const hashMapKeysSize = keysArray.map(k => sizeOfEntry(primitiveValueToEntry(externalArgs, k, 0))).reduce((p, c) => {
const hashMapKeysSize = keysArray.map(k => sizeOfEntry(primitiveValueToEntry(k))).reduce((p, c) => {
return p + align(c);

@@ -110,0 +117,0 @@ }, 0);

@@ -5,5 +5,5 @@ import { Entry, primitive, DataViewAndAllocatorCarrier } from "./interfaces";

export declare function sizeOfEntry(entry: Entry): number;
export declare function writeEntry(externalArgs: ExternalArgs, dataView: DataView, startingCursor: number, entry: Entry): void;
export declare function writeEntry({ dataView, uint8 }: DataViewAndAllocatorCarrier, startingCursor: number, entry: Entry): void;
export declare function appendEntry(externalArgs: ExternalArgs, carrier: DataViewAndAllocatorCarrier, entry: Entry): number;
export declare function readEntry(externalArgs: ExternalArgs, dataView: DataView, startingCursor: number): Entry;
export declare function readEntry(carrier: DataViewAndAllocatorCarrier, startingCursor: number): Entry;
export declare function canReuseMemoryOfEntry(entryA: Entry, value: primitive): boolean;

@@ -16,5 +16,5 @@ export declare function writeValueInPtrToPtr(externalArgs: ExternalArgs, carrier: DataViewAndAllocatorCarrier, ptrToPtr: number, value: any): {

export declare function writeValueInPtrToPtrAndHandleMemory(externalArgs: ExternalArgs, carrier: DataViewAndAllocatorCarrier, ptrToPtr: number, value: any): void;
export declare function incrementRefCount(externalArgs: ExternalArgs, dataView: DataView, entryPointer: number): number;
export declare function decrementRefCount(externalArgs: ExternalArgs, dataView: DataView, entryPointer: number): number;
export declare function getRefCount(externalArgs: ExternalArgs, dataView: DataView, entryPointer: number): number;
export declare function handleArcForDeletedValuePointer(externalArgs: ExternalArgs, carrier: DataViewAndAllocatorCarrier, deletedValuePointer: number): void;
export declare function incrementRefCount(externalArgs: ExternalArgs, carrier: DataViewAndAllocatorCarrier, entryPointer: number): number;
export declare function decrementRefCount(externalArgs: ExternalArgs, carrier: DataViewAndAllocatorCarrier, entryPointer: number): number;
export declare function getObjectPropPtrToPtr({ dataView }: DataViewAndAllocatorCarrier, pointerToEntry: number): {

@@ -21,0 +21,0 @@ valuePtrToPtr: number;

import { ENTRY_TYPE, isPrimitiveEntryType } from "./entry-types";
import { arrayBufferCopyTo, isPrimitive, primitiveValueToEntry, strByteLength } from "./utils";
import { isPrimitive, primitiveValueToEntry, isKnownAddressValuePointer } from "./utils";
import { BigInt64OverflowError } from "./exceptions";

@@ -7,2 +7,4 @@ import { INITIAL_ENTRY_POINTER_TO_POINTER, INITIAL_ENTRY_POINTER_VALUE } from "./consts";

import { getAllLinkedAddresses } from "./getAllLinkedAddresses";
import { stringEncodeInto } from "./stringEncodeInto";
import { stringDecode } from "./stringDecode";
const MAX_64_BIG_INT = BigInt("0xFFFFFFFFFFFFFFFF");

@@ -22,12 +24,2 @@ export function initializeArrayBuffer(arrayBuffer) {

switch (entry.type) {
case ENTRY_TYPE.UNDEFINED:
break;
case ENTRY_TYPE.NULL:
break;
case ENTRY_TYPE.BOOLEAN:
cursor += Uint8Array.BYTES_PER_ELEMENT;
break;
case ENTRY_TYPE.NUMBER:

@@ -40,3 +32,3 @@ cursor += Float64Array.BYTES_PER_ELEMENT;

cursor += Uint16Array.BYTES_PER_ELEMENT;
cursor += Math.max(strByteLength(entry.value), entry.allocatedBytes); // oh boy. i don't want to change it now, but no choice
cursor += entry.allocatedBytes; // oh boy. i don't want to change it now, but no choice
// @todo: this is incorrect? should be Math.max

@@ -83,3 +75,6 @@ // cursor += entry.allocatedBytes;

}
export function writeEntry(externalArgs, dataView, startingCursor, entry) {
export function writeEntry({
dataView,
uint8
}, startingCursor, entry) {
let cursor = startingCursor; // let writtenDataSizeInBytes = 0;

@@ -93,13 +88,2 @@ // write type

switch (entry.type) {
case ENTRY_TYPE.UNDEFINED:
break;
case ENTRY_TYPE.NULL:
break;
case ENTRY_TYPE.BOOLEAN:
dataView.setUint8(cursor, entry.value ? 1 : 0);
cursor += Uint8Array.BYTES_PER_ELEMENT;
break;
case ENTRY_TYPE.NUMBER:

@@ -111,12 +95,19 @@ dataView.setFloat64(cursor, entry.value);

case ENTRY_TYPE.STRING:
// eslint-disable-next-line no-case-declarations
const encodedString = externalArgs.textEncoder.encode(entry.value);
dataView.setUint16(cursor, encodedString.byteLength);
dataView.setUint16(cursor, entry.allocatedBytes);
cursor += Uint16Array.BYTES_PER_ELEMENT;
dataView.setUint16(cursor, entry.allocatedBytes);
cursor += Uint16Array.BYTES_PER_ELEMENT;
cursor += Uint16Array.BYTES_PER_ELEMENT; // const arr = new Uint8Array(entry.allocatedBytes);
// const writtenBytes1 = stringEncodeInto(arr, 0, entry.value);
// eslint-disable-next-line no-case-declarations
for (let i = 0; i < encodedString.length; i++) {
dataView.setUint8(cursor, encodedString[i]);
cursor += Uint8Array.BYTES_PER_ELEMENT;
const writtenBytes = stringEncodeInto(uint8, cursor, entry.value);
if (writtenBytes !== entry.allocatedBytes) {
// eslint-disable-next-line no-undef
console.warn({
value: entry.value,
writtenBytes,
allocatedBytes: entry.allocatedBytes
});
throw new Error("WTF???");
}

@@ -134,3 +125,3 @@

dataView.setBigUint64(cursor, entry.type === ENTRY_TYPE.BIGINT_NEGATIVE ? -entry.value : entry.value);
cursor += BigInt64Array.BYTES_PER_ELEMENT;
cursor += BigUint64Array.BYTES_PER_ELEMENT;
break;

@@ -174,8 +165,8 @@

const memoryAddress = carrier.allocator.calloc(size);
writeEntry(externalArgs, carrier.dataView, memoryAddress, entry);
writeEntry(carrier, memoryAddress, entry);
return memoryAddress;
}
export function readEntry(externalArgs, dataView, startingCursor) {
export function readEntry(carrier, startingCursor) {
let cursor = startingCursor;
const entryType = dataView.getUint8(cursor);
const entryType = carrier.dataView.getUint8(cursor);
cursor += Uint8Array.BYTES_PER_ELEMENT;

@@ -195,3 +186,3 @@ const entry = {

case ENTRY_TYPE.BOOLEAN:
entry.value = dataView.getUint8(cursor) !== 0;
entry.value = carrier.dataView.getUint8(cursor) !== 0;
cursor += Uint8Array.BYTES_PER_ELEMENT;

@@ -201,3 +192,3 @@ break;

case ENTRY_TYPE.NUMBER:
entry.value = dataView.getFloat64(cursor);
entry.value = carrier.dataView.getFloat64(cursor);
cursor += Float64Array.BYTES_PER_ELEMENT;

@@ -208,5 +199,5 @@ break;

// eslint-disable-next-line no-case-declarations
const stringLength = dataView.getUint16(cursor);
const stringLength = carrier.dataView.getUint16(cursor);
cursor += Uint16Array.BYTES_PER_ELEMENT;
entry.allocatedBytes = dataView.getUint16(cursor);
entry.allocatedBytes = carrier.dataView.getUint16(cursor);
cursor += Uint16Array.BYTES_PER_ELEMENT; // decode fails with zero length array

@@ -218,5 +209,5 @@

// eslint-disable-next-line no-case-declarations
const tempAB = new ArrayBuffer(stringLength);
arrayBufferCopyTo(dataView.buffer, cursor, stringLength, tempAB, 0);
entry.value = externalArgs.textDecoder.decode(tempAB);
// const tempAB = new ArrayBuffer(stringLength);
// arrayBufferCopyTo(dataView.buffer, cursor, stringLength, tempAB, 0);
entry.value = stringDecode(carrier.uint8, cursor, stringLength);
} else {

@@ -230,3 +221,3 @@ entry.value = "";

case ENTRY_TYPE.BIGINT_POSITIVE:
entry.value = dataView.getBigUint64(cursor);
entry.value = carrier.dataView.getBigUint64(cursor);
cursor += BigUint64Array.BYTES_PER_ELEMENT;

@@ -236,3 +227,3 @@ break;

case ENTRY_TYPE.BIGINT_NEGATIVE:
entry.value = -dataView.getBigUint64(cursor);
entry.value = -carrier.dataView.getBigUint64(cursor);
cursor += BigUint64Array.BYTES_PER_ELEMENT;

@@ -244,5 +235,5 @@ break;

case ENTRY_TYPE.SET:
entry.refsCount = dataView.getUint8(cursor);
entry.refsCount = carrier.dataView.getUint8(cursor);
cursor += Uint8Array.BYTES_PER_ELEMENT;
entry.value = dataView.getUint32(cursor);
entry.value = carrier.dataView.getUint32(cursor);
cursor += Uint32Array.BYTES_PER_ELEMENT;

@@ -252,9 +243,9 @@ break;

case ENTRY_TYPE.ARRAY:
entry.refsCount = dataView.getUint8(cursor);
entry.refsCount = carrier.dataView.getUint8(cursor);
cursor += Uint8Array.BYTES_PER_ELEMENT;
entry.value = dataView.getUint32(cursor);
entry.value = carrier.dataView.getUint32(cursor);
cursor += Uint32Array.BYTES_PER_ELEMENT;
entry.length = dataView.getUint32(cursor);
entry.length = carrier.dataView.getUint32(cursor);
cursor += Uint32Array.BYTES_PER_ELEMENT;
entry.allocatedLength = dataView.getUint32(cursor);
entry.allocatedLength = carrier.dataView.getUint32(cursor);
cursor += Uint32Array.BYTES_PER_ELEMENT;

@@ -264,5 +255,5 @@ break;

case ENTRY_TYPE.DATE:
entry.refsCount = dataView.getUint8(cursor);
entry.refsCount = carrier.dataView.getUint8(cursor);
cursor += Uint8Array.BYTES_PER_ELEMENT;
entry.value = dataView.getFloat64(cursor);
entry.value = carrier.dataView.getFloat64(cursor);
cursor += Float64Array.BYTES_PER_ELEMENT;

@@ -278,21 +269,16 @@ break;

export function canReuseMemoryOfEntry(entryA, value) {
const typeofTheValue = typeof value;
const typeofTheValue = typeof value; // number & bigint 64 are the same size
if (entryA.type === ENTRY_TYPE.BOOLEAN && typeofTheValue === "boolean") {
return true;
} // number & bigint 64 are the same size
if ((entryA.type === ENTRY_TYPE.BIGINT_NEGATIVE || entryA.type === ENTRY_TYPE.BIGINT_POSITIVE || entryA.type === ENTRY_TYPE.NUMBER) && (typeofTheValue === "bigint" || typeofTheValue === "number")) {
return true;
}
} // kill for strings for now
// if (
// entryA.type === ENTRY_TYPE.STRING &&
// typeofTheValue === "string" &&
// entryA.allocatedBytes >= strByteLength(value as string)
// ) {
// return true;
// }
if (entryA.type === ENTRY_TYPE.STRING && typeofTheValue === "string" && entryA.allocatedBytes >= strByteLength(value)) {
return true;
}
if ((entryA.type === ENTRY_TYPE.NULL || entryA.type === ENTRY_TYPE.UNDEFINED) && value === undefined || value === null) {
return true;
}
return false;

@@ -302,8 +288,7 @@ }

const existingEntryPointer = carrier.dataView.getUint32(ptrToPtr);
const existingValueEntry = readEntry(externalArgs, carrier.dataView, existingEntryPointer); // try to re use memory
const existingValueEntry = readEntry(carrier, existingEntryPointer); // try to re use memory
if (isPrimitive(value) && isPrimitiveEntryType(existingValueEntry.type) && canReuseMemoryOfEntry(existingValueEntry, value) && existingEntryPointer !== 0) {
const stringAllocatedBytes = existingValueEntry.type === ENTRY_TYPE.STRING ? existingValueEntry.allocatedBytes : 0;
const newEntry = primitiveValueToEntry(externalArgs, value, stringAllocatedBytes);
writeEntry(externalArgs, carrier.dataView, existingEntryPointer, newEntry);
if (!isKnownAddressValuePointer(existingEntryPointer) && isPrimitive(value) && isPrimitiveEntryType(existingValueEntry.type) && canReuseMemoryOfEntry(existingValueEntry, value)) {
const newEntry = primitiveValueToEntry(value);
writeEntry(carrier, existingEntryPointer, newEntry);
} else {

@@ -329,3 +314,3 @@ const referencedPointers = [];

for (const ptr of referencedPointers) {
incrementRefCount(externalArgs, carrier.dataView, ptr);
incrementRefCount(externalArgs, carrier, ptr);
}

@@ -335,10 +320,14 @@ }

if (existingValueEntry && "refsCount" in existingValueEntry) {
const newRefCount = decrementRefCount(externalArgs, carrier.dataView, existingEntryPointer);
const newRefCount = decrementRefCount(externalArgs, carrier, existingEntryPointer);
if (newRefCount === 0) {
const addressesToFree = getAllLinkedAddresses(externalArgs, carrier.dataView, false, existingEntryPointer);
const addressesToFree = getAllLinkedAddresses(carrier, false, existingEntryPointer);
for (const address of addressesToFree) {
for (const address of addressesToFree.leafAddresses) {
carrier.allocator.free(address);
}
for (const address of addressesToFree.arcAddresses) {
decrementRefCount(externalArgs, carrier, address);
}
}

@@ -349,19 +338,34 @@ } else {

}
export function incrementRefCount(externalArgs, dataView, entryPointer) {
const entry = readEntry(externalArgs, dataView, entryPointer);
export function handleArcForDeletedValuePointer(externalArgs, carrier, deletedValuePointer) {
// No memory to free/ARC
if (isKnownAddressValuePointer(deletedValuePointer)) {
return;
}
if ("refsCount" in entry) {
entry.refsCount += 1;
writeEntry(externalArgs, dataView, entryPointer, entry);
return entry.refsCount;
const existingValueEntry = readEntry(carrier, deletedValuePointer);
if (existingValueEntry && "refsCount" in existingValueEntry) {
const newRefCount = decrementRefCount(externalArgs, carrier, deletedValuePointer);
if (newRefCount === 0) {
const addressesToFree = getAllLinkedAddresses(carrier, false, deletedValuePointer);
for (const address of addressesToFree.leafAddresses) {
carrier.allocator.free(address);
}
for (const address of addressesToFree.arcAddresses) {
decrementRefCount(externalArgs, carrier, address);
}
}
} else {
carrier.allocator.free(deletedValuePointer);
}
throw new Error("unexpected");
}
export function decrementRefCount(externalArgs, dataView, entryPointer) {
const entry = readEntry(externalArgs, dataView, entryPointer);
export function incrementRefCount(externalArgs, carrier, entryPointer) {
const entry = readEntry(carrier, entryPointer);
if ("refsCount" in entry) {
entry.refsCount -= 1;
writeEntry(externalArgs, dataView, entryPointer, entry);
entry.refsCount += 1;
writeEntry(carrier, entryPointer, entry);
return entry.refsCount;

@@ -372,6 +376,8 @@ }

}
export function getRefCount(externalArgs, dataView, entryPointer) {
const entry = readEntry(externalArgs, dataView, entryPointer);
export function decrementRefCount(externalArgs, carrier, entryPointer) {
const entry = readEntry(carrier, entryPointer);
if ("refsCount" in entry) {
entry.refsCount -= 1;
writeEntry(carrier, entryPointer, entry);
return entry.refsCount;

@@ -378,0 +384,0 @@ }

@@ -14,3 +14,5 @@ export function arrayBuffer2HexArray(buffer, withByteNumber = false) {

}
import { OutOfMemoryError } from "./exceptions"; // extend pool and not monkey patch? need to think about it
import { MemPool } from "@thi.ng/malloc";
import { OutOfMemoryError } from "./exceptions";
import { MEM_POOL_START } from "./consts"; // extend pool and not monkey patch? need to think about it

@@ -82,2 +84,18 @@ export function recordAllocations(operation, pool) {

};
}
export function makeCarrier(arrayBuffer) {
const allocator = new MemPool({
buf: arrayBuffer,
start: MEM_POOL_START
});
const carrier = {
dataView: new DataView(arrayBuffer),
allocator,
uint8: new Uint8Array(arrayBuffer),
uint16: new Uint16Array(arrayBuffer),
uint32: new Uint32Array(arrayBuffer),
float64: new Float64Array(arrayBuffer),
bigUint64: new BigUint64Array(arrayBuffer)
};
return carrier;
}
import { primitive, Entry, ExternalArgs, InternalAPI, ExternalArgsApi } from "./interfaces";
export declare function isPrimitive(value: unknown): value is primitive;
export declare function primitiveValueToEntry(externalArgs: ExternalArgs, value: primitive, stringAllocatedBytes: number): Entry;
export declare function primitiveValueToEntry(value: primitive): Entry;
export declare function createKnownTypeGuard<T>(arr: ReadonlyArray<T>): (v: unknown) => v is T;

@@ -12,2 +12,3 @@ export declare function invariant(condition: boolean, message: string): void;

export declare function align(value: number, alignTo?: number): number;
export declare function isKnownAddressValuePointer(entryPointer: number): boolean;
//# sourceMappingURL=utils.d.ts.map
import { ENTRY_TYPE } from "./entry-types";
import { INTERNAL_API_SYMBOL } from "./symbols";
import { UNDEFINED_KNOWN_ADDRESS, NULL_KNOWN_ADDRESS, TRUE_KNOWN_ADDRESS, FALSE_KNOWN_ADDRESS } from "./consts";
const primitives = ["string", "number", "bigint", "boolean", "undefined"];

@@ -15,3 +16,3 @@ export function isPrimitive(value) {

}
export function primitiveValueToEntry(externalArgs, value, stringAllocatedBytes) {
export function primitiveValueToEntry(value) {
if (typeof value === "string") {

@@ -21,3 +22,3 @@ return {

value,
allocatedBytes: Math.max(externalArgs.textEncoder.encode(value).length, stringAllocatedBytes)
allocatedBytes: strByteLength(value)
};

@@ -40,21 +41,2 @@ }

if (typeof value === "boolean") {
return {
type: ENTRY_TYPE.BOOLEAN,
value
};
}
if (typeof value === "undefined") {
return {
type: ENTRY_TYPE.UNDEFINED
};
}
if (value === null) {
return {
type: ENTRY_TYPE.NULL
};
}
throw new Error("unexpected");

@@ -93,4 +75,3 @@ }

hashMapLoadFactor: p.hashMapLoadFactor ? p.hashMapLoadFactor : 0.75,
arrayAdditionalAllocation: p.arrayAdditionalAllocation ? p.arrayAdditionalAllocation : 0,
minimumStringAllocation: p.minimumStringAllocation ? p.minimumStringAllocation : 0
arrayAdditionalAllocation: p.arrayAdditionalAllocation ? p.arrayAdditionalAllocation : 0
};

@@ -117,2 +98,5 @@ }

return Math.ceil(value / alignTo) * alignTo;
}
export function isKnownAddressValuePointer(entryPointer) {
return entryPointer === UNDEFINED_KNOWN_ADDRESS || entryPointer === NULL_KNOWN_ADDRESS || entryPointer === TRUE_KNOWN_ADDRESS || entryPointer === FALSE_KNOWN_ADDRESS;
}
{
"name": "@bnaya/objectbuffer",
"description": "Object-like api, backed by an array buffer",
"version": "0.0.0-6858e67",
"version": "0.0.0-738fce6",
"main": "dist/objectbuffer.cjs.js",

@@ -39,16 +39,18 @@ "module": "dist/index.js",

"devDependencies": {
"@babel/cli": "^7.7.4",
"@babel/core": "^7.7.4",
"@babel/preset-env": "^7.7.4",
"@babel/cli": "^7.7.5",
"@babel/core": "^7.7.5",
"@babel/preset-env": "^7.7.6",
"@babel/preset-typescript": "^7.7.4",
"@types/jest": "^24.0.23",
"@types/node": "^12.12.14",
"@typescript-eslint/eslint-plugin": "^2.9.0",
"@typescript-eslint/parser": "^2.9.0",
"@types/benchmark": "^1.0.31",
"@types/jest": "^24.0.24",
"@types/node": "^12.12.21",
"@typescript-eslint/eslint-plugin": "^2.12.0",
"@typescript-eslint/parser": "^2.12.0",
"babel-loader": "^8.0.6",
"concurrently": "^5.0.0",
"core-js": "^3.4.5",
"benchmark": "^2.1.4",
"concurrently": "^5.0.2",
"core-js": "^3.5.0",
"eslint": "^6.7.2",
"eslint-config-prettier": "^6.7.0",
"eslint-plugin-prettier": "^3.1.1",
"eslint-plugin-prettier": "^3.1.2",
"gh-pages": "^2.1.1",

@@ -61,11 +63,11 @@ "html-webpack-plugin": "^3.2.0",

"rimraf": "^3.0.0",
"rollup": "^1.27.6",
"rollup": "^1.27.13",
"rollup-plugin-babel": "^4.3.3",
"rollup-plugin-node-resolve": "^5.2.0",
"typedoc": "npm:@gnd/typedoc",
"typedoc-plugin-markdown": "^2.2.11",
"typescript": "^3.7.2",
"webpack": "^4.41.2",
"typedoc-plugin-markdown": "^2.2.14",
"typescript": "^3.7.3",
"webpack": "^4.41.3",
"webpack-cli": "^3.3.10",
"webpack-dev-server": "^3.9.0",
"webpack-dev-server": "^3.10.0",
"worker-loader": "^2.0.0",

@@ -72,0 +74,0 @@ "yarn-deduplicate": "^1.1.1"

@@ -14,4 +14,3 @@ /* eslint-env jest */

textDecoder: new util.TextDecoder(),
arrayAdditionalAllocation: 0,
minimumStringAllocation: 0
arrayAdditionalAllocation: 0
});

@@ -18,0 +17,0 @@

@@ -13,4 +13,3 @@ /* eslint-env jest */

textDecoder: new util.TextDecoder(),
arrayAdditionalAllocation: 0,
minimumStringAllocation: 0
arrayAdditionalAllocation: 0
});

@@ -17,0 +16,0 @@

@@ -13,4 +13,3 @@ /* eslint-env jest */

textDecoder: new util.TextDecoder(),
arrayAdditionalAllocation: 0,
minimumStringAllocation: 0
arrayAdditionalAllocation: 0
});

@@ -17,0 +16,0 @@

@@ -82,7 +82,3 @@ /* eslint-env jest */

expect(objectBuffer.foo).toMatchInlineSnapshot(`
Map {
"2" => "b",
}
`);
expect(objectBuffer.foo).toMatchInlineSnapshot(`Map {}`);
});

@@ -89,0 +85,0 @@

@@ -13,4 +13,3 @@ /* eslint-env jest */

textDecoder: new util.TextDecoder(),
arrayAdditionalAllocation: 0,
minimumStringAllocation: 0
arrayAdditionalAllocation: 0
});

@@ -17,0 +16,0 @@

@@ -22,3 +22,2 @@ /* eslint-env jest */

// objectBuffer.foo = "b";
delete objectBuffer.foo;

@@ -29,2 +28,33 @@

});
test("Basic object with values", () => {
const input = {
a: 1,
b: true,
c: false,
d: null,
e: undefined,
foo: { a: 1, b: true, c: false, d: null, e: undefined }
};
const objectBuffer = createObjectBuffer<any>(externalArgs, 1024, input);
expect(memoryStats(objectBuffer).available).toMatchInlineSnapshot(`96`);
expect(input).toEqual(objectBuffer);
expect(objectBuffer).toMatchInlineSnapshot(`
Object {
"a": 1,
"b": true,
"c": false,
"d": null,
"e": undefined,
"foo": Object {
"a": 1,
"b": true,
"c": false,
"d": null,
"e": undefined,
},
}
`);
});
});

@@ -20,4 +20,3 @@ /* eslint-env jest */

textDecoder: new util.TextDecoder(),
arrayAdditionalAllocation: 0,
minimumStringAllocation: 0
arrayAdditionalAllocation: 0
});

@@ -24,0 +23,0 @@

@@ -14,4 +14,3 @@ /* eslint-env jest */

textDecoder: new util.TextDecoder(),
arrayAdditionalAllocation: 0,
minimumStringAllocation: 0
arrayAdditionalAllocation: 0
});

@@ -18,0 +17,0 @@

@@ -34,3 +34,3 @@ /* eslint-env jest */

objectBuffer.foo.add("b");
expect(memoryStats(objectBuffer).available).toMatchInlineSnapshot(`576`);
expect(memoryStats(objectBuffer).available).toMatchInlineSnapshot(`592`);
expect(objectBuffer.foo).toMatchInlineSnapshot(`

@@ -58,7 +58,7 @@ Set {

objectBuffer.foo.add("b");
expect(memoryStats(objectBuffer).available).toMatchInlineSnapshot(`576`);
expect(memoryStats(objectBuffer).available).toMatchInlineSnapshot(`592`);
objectBuffer.foo.delete(1);
expect(memoryStats(objectBuffer).available).toMatchInlineSnapshot(`576`);
expect(memoryStats(objectBuffer).available).toMatchInlineSnapshot(`592`);

@@ -79,3 +79,3 @@ expect(objectBuffer.foo).toMatchInlineSnapshot(`

objectBuffer.foo.add("b");
expect(memoryStats(objectBuffer).available).toMatchInlineSnapshot(`576`);
expect(memoryStats(objectBuffer).available).toMatchInlineSnapshot(`592`);

@@ -86,7 +86,3 @@ objectBuffer.foo.clear();

expect(objectBuffer.foo).toMatchInlineSnapshot(`
Set {
"b",
}
`);
expect(objectBuffer.foo).toMatchInlineSnapshot(`Set {}`);
});

@@ -101,3 +97,3 @@

expect(memoryStats(objectBuffer).available).toMatchInlineSnapshot(`576`);
expect(memoryStats(objectBuffer).available).toMatchInlineSnapshot(`592`);

@@ -104,0 +100,0 @@ expect(objectBuffer.foo).toMatchInlineSnapshot(`

@@ -6,3 +6,3 @@ /* eslint-env jest */

import { createObjectBuffer } from "../";
import { memoryStats } from "../internal/api";
import { memoryStats, disposeWrapperObject } from "../internal/api";
import { externalArgsApiToExternalArgsApi } from "../internal/utils";

@@ -15,4 +15,3 @@

textDecoder: new util.TextDecoder(),
arrayAdditionalAllocation: 0,
minimumStringAllocation: 0
arrayAdditionalAllocation: 0
});

@@ -42,2 +41,51 @@

});
test("basic 2", () => {
const objectBuffer = createObjectBuffer<any>(
externalArgs,
1024,
{ o: { a: { a: { v: [1] } } } },
{ useSharedArrayBuffer: true }
);
const prev1 = objectBuffer.o;
objectBuffer.o = undefined;
const prev2 = prev1.a;
const prev3 = prev1.a.a;
disposeWrapperObject(prev1);
// disposeWrapperObject(prev2);
// disposeWrapperObject(prev3);
// objectBuffer.arr = [{ bar: 666 }];
// objectBuffer.arr = [];
objectBuffer.o = undefined;
// disposeWrapperObject(objectBuffer.arr);
expect(prev2).toMatchInlineSnapshot(`
Object {
"a": Object {
"v": Array [
1,
],
},
}
`);
expect(prev3).toMatchInlineSnapshot(`
Object {
"v": Array [
1,
],
}
`);
expect(objectBuffer).toMatchInlineSnapshot(`
Object {
"o": undefined,
}
`);
expect(memoryStats(objectBuffer).used).toMatchInlineSnapshot(`608`);
});
});

@@ -30,3 +30,3 @@ /* eslint-disable */

arrayAdditionalAllocation: 0,
minimumStringAllocation: 0
});

@@ -33,0 +33,0 @@

@@ -12,4 +12,3 @@ import * as util from "util";

textDecoder: new util.TextDecoder(),
arrayAdditionalAllocation: 0,
minimumStringAllocation: 0
arrayAdditionalAllocation: 0
});

@@ -24,3 +23,3 @@

expect(initialFreeSpace).toMatchInlineSnapshot(`32`);
expect(initialFreeSpace).toMatchInlineSnapshot(`48`);

@@ -35,3 +34,3 @@ expect(() => {

expect(memoryStats(objectBuffer).available).toMatchInlineSnapshot(`32`);
expect(memoryStats(objectBuffer).available).toMatchInlineSnapshot(`48`);

@@ -38,0 +37,0 @@ expect(objectBuffer).toMatchInlineSnapshot(`

@@ -12,4 +12,3 @@ import * as util from "util";

textDecoder: new util.TextDecoder(),
arrayAdditionalAllocation: 0,
minimumStringAllocation: 0
arrayAdditionalAllocation: 0
});

@@ -16,0 +15,0 @@

@@ -16,4 +16,3 @@ /* eslint-env jest */

textDecoder: new util.TextDecoder(),
arrayAdditionalAllocation: 0,
minimumStringAllocation: 0
arrayAdditionalAllocation: 0
});

@@ -44,4 +43,3 @@

textDecoder: new util.TextDecoder(),
arrayAdditionalAllocation: 0,
minimumStringAllocation: 0
arrayAdditionalAllocation: 0
});

@@ -67,4 +65,3 @@ test("getUnderlyingArrayBuffer simple", () => {

textDecoder: new util.TextDecoder(),
arrayAdditionalAllocation: 0,
minimumStringAllocation: 0
arrayAdditionalAllocation: 0
});

@@ -71,0 +68,0 @@

import { initializeArrayBuffer } from "./store";
import { objectSaver } from "./objectSaver";
import { createObjectWrapper } from "./objectWrapper";
import { ExternalArgsApi } from "./interfaces";
import { ExternalArgsApi, DataViewAndAllocatorCarrier } from "./interfaces";
import {

@@ -45,3 +45,11 @@ arrayBufferCopyTo,

const carrier = { dataView, allocator };
const carrier: DataViewAndAllocatorCarrier = {
dataView,
allocator,
uint8: new Uint8Array(arrayBuffer),
uint16: new Uint16Array(arrayBuffer),
uint32: new Uint32Array(arrayBuffer),
float64: new Float64Array(arrayBuffer),
bigUint64: new BigUint64Array(arrayBuffer)
};

@@ -113,5 +121,15 @@ const start = objectSaver(

const carrier: DataViewAndAllocatorCarrier = {
dataView,
allocator,
uint8: new Uint8Array(arrayBuffer),
uint16: new Uint16Array(arrayBuffer),
uint32: new Uint32Array(arrayBuffer),
float64: new Float64Array(arrayBuffer),
bigUint64: new BigUint64Array(arrayBuffer)
};
return createObjectWrapper(
externalArgsApiToExternalArgsApi(externalArgs),
{ dataView, allocator },
carrier,
dataView.getUint32(INITIAL_ENTRY_POINTER_TO_POINTER)

@@ -151,2 +169,12 @@ );

const carrier: DataViewAndAllocatorCarrier = {
dataView: new DataView(newArrayBuffer),
allocator,
uint8: new Uint8Array(newArrayBuffer),
uint16: new Uint16Array(newArrayBuffer),
uint32: new Uint32Array(newArrayBuffer),
float64: new Float64Array(newArrayBuffer),
bigUint64: new BigUint64Array(newArrayBuffer)
};
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore

@@ -156,6 +184,3 @@ // @ts-ignore

getInternalAPI(objectBuffer).replaceCarrierContent(
new DataView(newArrayBuffer),
allocator
);
getInternalAPI(objectBuffer).replaceCarrierContent(carrier);
}

@@ -162,0 +187,0 @@

@@ -15,2 +15,3 @@ /* eslint-env jest */

import { externalArgsApiToExternalArgsApi } from "./utils";
import { makeCarrier } from "./testUtils";

@@ -20,4 +21,3 @@ const externalArgs: ExternalArgs = externalArgsApiToExternalArgsApi({

textDecoder: new util.TextDecoder(),
arrayAdditionalAllocation: 0,
minimumStringAllocation: 0
arrayAdditionalAllocation: 0
});

@@ -28,19 +28,10 @@

const arrayBuffer = new ArrayBuffer(256);
const dataView = new DataView(arrayBuffer);
const carrier = makeCarrier(arrayBuffer);
initializeArrayBuffer(arrayBuffer);
const allocator = new MemPool({
buf: arrayBuffer,
start: MEM_POOL_START
});
const arrayToSave = [1, 2];
const saverOutput = arraySaver(
externalArgs,
{ dataView, allocator },
[],
arrayToSave
);
const saverOutput = arraySaver(externalArgs, carrier, [], arrayToSave);
const metadata = arrayGetMetadata(externalArgs, dataView, saverOutput);
const metadata = arrayGetMetadata(carrier, saverOutput);

@@ -57,3 +48,3 @@ expect(metadata).toMatchInlineSnapshot(`

expect(allocator.stats().available).toMatchInlineSnapshot(`128`);
expect(carrier.allocator.stats().available).toMatchInlineSnapshot(`128`);
});

@@ -65,3 +56,3 @@

const dataView = new DataView(arrayBuffer);
const carrier = makeCarrier(arrayBuffer);
initializeArrayBuffer(arrayBuffer);

@@ -75,12 +66,7 @@ const allocator = new MemPool({

const saverOutput = arraySaver(
externalArgs,
{ dataView, allocator },
[],
arrayToSave
);
const saverOutput = arraySaver(externalArgs, carrier, [], arrayToSave);
const finalValue = getFinalValueAtArrayIndex(
externalArgs,
{ dataView, allocator },
carrier,
saverOutput,

@@ -97,3 +83,3 @@ 0

const dataView = new DataView(arrayBuffer);
const carrier = makeCarrier(arrayBuffer);
initializeArrayBuffer(arrayBuffer);

@@ -107,12 +93,7 @@ const allocator = new MemPool({

const saverOutput = arraySaver(
externalArgs,
{ dataView, allocator },
[],
arrayToSave
);
const saverOutput = arraySaver(externalArgs, carrier, [], arrayToSave);
const finalValue = getFinalValueAtArrayIndex(
externalArgs,
{ dataView, allocator },
carrier,
saverOutput,

@@ -129,3 +110,3 @@ 10

const dataView = new DataView(arrayBuffer);
const carrier = makeCarrier(arrayBuffer);
initializeArrayBuffer(arrayBuffer);

@@ -139,12 +120,7 @@ const allocator = new MemPool({

const saverOutput = arraySaver(
externalArgs,
{ dataView, allocator },
[],
arrayToSave
);
const saverOutput = arraySaver(externalArgs, carrier, [], arrayToSave);
const finalValue = getFinalValueAtArrayIndex(
externalArgs,
{ dataView, allocator },
carrier,
saverOutput,

@@ -162,3 +138,3 @@ 1

const dataView = new DataView(arrayBuffer);
const carrier = makeCarrier(arrayBuffer);
initializeArrayBuffer(arrayBuffer);

@@ -172,12 +148,7 @@ const allocator = new MemPool({

const saverOutput = arraySaver(
externalArgs,
{ dataView, allocator },
[],
arrayToSave
);
const saverOutput = arraySaver(externalArgs, carrier, [], arrayToSave);
setValueAtArrayIndex(
externalArgs,
{ dataView, allocator },
carrier,
saverOutput,

@@ -190,3 +161,3 @@ 1,

externalArgs,
{ dataView, allocator },
carrier,
saverOutput,

@@ -193,0 +164,0 @@ 1

@@ -16,11 +16,6 @@ import {

export function arrayGetMetadata(
externalArgs: ExternalArgs,
dataView: DataView,
carrier: DataViewAndAllocatorCarrier,
pointerToArrayEntry: number
) {
const arrayEntry = readEntry(
externalArgs,
dataView,
pointerToArrayEntry
) as ArrayEntry;
const arrayEntry = readEntry(carrier, pointerToArrayEntry) as ArrayEntry;

@@ -31,12 +26,7 @@ return arrayEntry;

export function arrayGetPointersToValueInIndex(
externalArgs: ExternalArgs,
dataView: DataView,
carrier: DataViewAndAllocatorCarrier,
pointerToArrayEntry: number,
indexToGet: number
) {
const metadata = arrayGetMetadata(
externalArgs,
dataView,
pointerToArrayEntry
);
const metadata = arrayGetMetadata(carrier, pointerToArrayEntry);

@@ -51,3 +41,3 @@ // out of bound

const pointer = dataView.getUint32(pointerToThePointer);
const pointer = carrier.dataView.getUint32(pointerToThePointer);

@@ -67,4 +57,3 @@ return {

const pointers = arrayGetPointersToValueInIndex(
externalArgs,
dataViewCarrier.dataView,
dataViewCarrier,
pointerToArrayEntry,

@@ -86,4 +75,3 @@ indexToGet

export function setValuePointerAtArrayIndex(
externalArgs: ExternalArgs,
dataView: DataView,
carrier: DataViewAndAllocatorCarrier,
pointerToArrayEntry: number,

@@ -94,4 +82,3 @@ indexToSet: number,

const pointers = arrayGetPointersToValueInIndex(
externalArgs,
dataView,
carrier,
pointerToArrayEntry,

@@ -103,3 +90,3 @@ indexToSet

dataView.setUint32(pointers.pointerToThePointer, pointerToEntry);
carrier.dataView.setUint32(pointers.pointerToThePointer, pointerToEntry);
}

@@ -116,4 +103,3 @@

const pointers = arrayGetPointersToValueInIndex(
externalArgs,
carrier.dataView,
carrier,
pointerToArrayEntry,

@@ -140,7 +126,3 @@ indexToSet

) {
const metadata = arrayGetMetadata(
externalArgs,
carrier.dataView,
pointerToArrayEntry
);
const metadata = arrayGetMetadata(carrier, pointerToArrayEntry);

@@ -158,3 +140,3 @@ if (wishedLength > metadata.length) {

// no need to re-allocated, just push the length forward
writeEntry(externalArgs, carrier.dataView, pointerToArrayEntry, {
writeEntry(carrier, pointerToArrayEntry, {
type: ENTRY_TYPE.ARRAY,

@@ -175,13 +157,9 @@ refsCount: metadata.refsCount,

externalArgs: ExternalArgs,
dataView: DataView,
carrier: DataViewAndAllocatorCarrier,
pointerToArrayEntry: number,
wishedLength: number
) {
const metadata = arrayGetMetadata(
externalArgs,
dataView,
pointerToArrayEntry
);
const metadata = arrayGetMetadata(carrier, pointerToArrayEntry);
writeEntry(externalArgs, dataView, pointerToArrayEntry, {
writeEntry(carrier, pointerToArrayEntry, {
type: ENTRY_TYPE.ARRAY,

@@ -202,7 +180,3 @@ refsCount: metadata.refsCount,

) {
const metadata = arrayGetMetadata(
externalArgs,
carrier.dataView,
pointerToArrayEntry
);
const metadata = arrayGetMetadata(carrier, pointerToArrayEntry);

@@ -227,3 +201,3 @@ const newArrayValueLocation = carrier.allocator.realloc(

writeEntry(externalArgs, carrier.dataView, pointerToArrayEntry, {
writeEntry(carrier, pointerToArrayEntry, {
type: ENTRY_TYPE.ARRAY,

@@ -243,7 +217,3 @@ refsCount: metadata.refsCount,

) {
const metadata = arrayGetMetadata(
externalArgs,
dataViewCarrier.dataView,
pointerToArrayEntry
);
const metadata = arrayGetMetadata(dataViewCarrier, pointerToArrayEntry);
const pointersToValues = [...new Array(metadata.length).keys()]

@@ -313,10 +283,6 @@ .map(index => metadata.value + index * Uint32Array.BYTES_PER_ELEMENT)

externalArgs: ExternalArgs,
dataView: DataView,
carrier: DataViewAndAllocatorCarrier,
pointerToArrayEntry: number
) {
const metadata = arrayGetMetadata(
externalArgs,
dataView,
pointerToArrayEntry
);
const metadata = arrayGetMetadata(carrier, pointerToArrayEntry);

@@ -327,13 +293,7 @@ for (let i = 0; i < Math.floor(metadata.length / 2); i += 1) {

// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const a = arrayGetPointersToValueInIndex(
externalArgs,
dataView,
pointerToArrayEntry,
i
)!;
const a = arrayGetPointersToValueInIndex(carrier, pointerToArrayEntry, i)!;
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const b = arrayGetPointersToValueInIndex(
externalArgs,
dataView,
carrier,
pointerToArrayEntry,

@@ -343,13 +303,6 @@ theOtherIndex

setValuePointerAtArrayIndex(
externalArgs,
dataView,
pointerToArrayEntry,
i,
b.pointer
);
setValuePointerAtArrayIndex(carrier, pointerToArrayEntry, i, b.pointer);
setValuePointerAtArrayIndex(
externalArgs,
dataView,
carrier,
pointerToArrayEntry,

@@ -356,0 +309,0 @@ theOtherIndex,

@@ -5,6 +5,4 @@ /* eslint-env jest */

import * as util from "util";
import { arrayBuffer2HexArray } from "./testUtils";
import { arrayBuffer2HexArray, makeCarrier } from "./testUtils";
import { arraySaver } from "./arraySaver";
import { MemPool } from "@thi.ng/malloc";
import { MEM_POOL_START } from "./consts";
import { externalArgsApiToExternalArgsApi } from "./utils";

@@ -23,17 +21,8 @@

const dataView = new DataView(arrayBuffer);
const carrier = makeCarrier(arrayBuffer);
initializeArrayBuffer(arrayBuffer);
const allocator = new MemPool({
buf: arrayBuffer,
start: MEM_POOL_START
});
const arrayToSave = [1, 2, 3];
const saverOutput = arraySaver(
externalArgs,
{ dataView, allocator },
[],
arrayToSave
);
const saverOutput = arraySaver(externalArgs, carrier, [], arrayToSave);

@@ -40,0 +29,0 @@ expect(saverOutput).toMatchInlineSnapshot(`224`);

@@ -10,2 +10,3 @@ /* eslint-env jest */

import { externalArgsApiToExternalArgsApi } from "./utils";
import { makeCarrier } from "./testUtils";

@@ -21,3 +22,3 @@ describe("arraySplice tests", () => {

const arrayBuffer = new ArrayBuffer(512);
const dataView = new DataView(arrayBuffer);
const carrier = makeCarrier(arrayBuffer);
initializeArrayBuffer(arrayBuffer);

@@ -31,12 +32,7 @@ const allocator = new MemPool({

const saverOutput = arraySaver(
externalArgs,
{ dataView, allocator },
[],
plainJSArray
);
const saverOutput = arraySaver(externalArgs, carrier, [], plainJSArray);
const arrayWrapper = createArrayWrapper(
{ ...externalArgs, arrayAdditionalAllocation: 3 },
{ dataView, allocator },
carrier,
saverOutput

@@ -101,3 +97,3 @@ );

const arrayBuffer = new ArrayBuffer(512);
const dataView = new DataView(arrayBuffer);
const carrier = makeCarrier(arrayBuffer);
initializeArrayBuffer(arrayBuffer);

@@ -111,12 +107,7 @@ const allocator = new MemPool({

const saverOutput = arraySaver(
externalArgs,
{ dataView, allocator },
[],
plainJSArray
);
const saverOutput = arraySaver(externalArgs, carrier, [], plainJSArray);
const arrayWrapper = createArrayWrapper(
{ ...externalArgs, arrayAdditionalAllocation: 3 },
{ dataView, allocator },
carrier,
saverOutput

@@ -158,3 +149,3 @@ );

const arrayBuffer = new ArrayBuffer(512);
const dataView = new DataView(arrayBuffer);
const carrier = makeCarrier(arrayBuffer);
initializeArrayBuffer(arrayBuffer);

@@ -168,12 +159,7 @@ const allocator = new MemPool({

const saverOutput = arraySaver(
externalArgs,
{ dataView, allocator },
[],
plainJSArray
);
const saverOutput = arraySaver(externalArgs, carrier, [], plainJSArray);
const arrayWrapper = createArrayWrapper(
{ ...externalArgs, arrayAdditionalAllocation: 3 },
{ dataView, allocator },
carrier,
saverOutput

@@ -224,3 +210,3 @@ );

const arrayBuffer = new ArrayBuffer(512);
const dataView = new DataView(arrayBuffer);
const carrier = makeCarrier(arrayBuffer);
initializeArrayBuffer(arrayBuffer);

@@ -234,12 +220,7 @@ const allocator = new MemPool({

const saverOutput = arraySaver(
externalArgs,
{ dataView, allocator },
[],
plainJSArray
);
const saverOutput = arraySaver(externalArgs, carrier, [], plainJSArray);
const arrayWrapper = createArrayWrapper(
{ ...externalArgs, arrayAdditionalAllocation: 3 },
{ dataView, allocator },
carrier,
saverOutput

@@ -308,3 +289,3 @@ );

const arrayBuffer = new ArrayBuffer(512);
const dataView = new DataView(arrayBuffer);
const carrier = makeCarrier(arrayBuffer);
initializeArrayBuffer(arrayBuffer);

@@ -318,12 +299,7 @@ const allocator = new MemPool({

const saverOutput = arraySaver(
externalArgs,
{ dataView, allocator },
[],
plainJSArray
);
const saverOutput = arraySaver(externalArgs, carrier, [], plainJSArray);
const arrayWrapper = createArrayWrapper(
{ ...externalArgs, arrayAdditionalAllocation: 3 },
{ dataView, allocator },
carrier,
saverOutput

@@ -393,3 +369,3 @@ );

const arrayBuffer = new ArrayBuffer(512);
const dataView = new DataView(arrayBuffer);
const carrier = makeCarrier(arrayBuffer);
initializeArrayBuffer(arrayBuffer);

@@ -403,12 +379,7 @@ const allocator = new MemPool({

const saverOutput = arraySaver(
externalArgs,
{ dataView, allocator },
[],
plainJSArray
);
const saverOutput = arraySaver(externalArgs, carrier, [], plainJSArray);
const arrayWrapper = createArrayWrapper(
{ ...externalArgs, arrayAdditionalAllocation: 3 },
{ dataView, allocator },
carrier,
saverOutput

@@ -468,3 +439,3 @@ );

const arrayBuffer = new ArrayBuffer(512);
const dataView = new DataView(arrayBuffer);
const carrier = makeCarrier(arrayBuffer);
initializeArrayBuffer(arrayBuffer);

@@ -478,12 +449,7 @@ const allocator = new MemPool({

const saverOutput = arraySaver(
externalArgs,
{ dataView, allocator },
[],
plainJSArray
);
const saverOutput = arraySaver(externalArgs, carrier, [], plainJSArray);
const arrayWrapper = createArrayWrapper(
{ ...externalArgs, arrayAdditionalAllocation: 3 },
{ dataView, allocator },
carrier,
saverOutput

@@ -549,3 +515,3 @@ );

const arrayBuffer = new ArrayBuffer(512);
const dataView = new DataView(arrayBuffer);
const carrier = makeCarrier(arrayBuffer);
initializeArrayBuffer(arrayBuffer);

@@ -559,12 +525,7 @@ const allocator = new MemPool({

const saverOutput = arraySaver(
externalArgs,
{ dataView, allocator },
[],
plainJSArray
);
const saverOutput = arraySaver(externalArgs, carrier, [], plainJSArray);
const arrayWrapper = createArrayWrapper(
{ ...externalArgs, arrayAdditionalAllocation: 3 },
{ dataView, allocator },
carrier,
saverOutput

@@ -630,3 +591,3 @@ );

const arrayBuffer = new ArrayBuffer(512);
const dataView = new DataView(arrayBuffer);
const carrier = makeCarrier(arrayBuffer);
initializeArrayBuffer(arrayBuffer);

@@ -640,12 +601,7 @@ const allocator = new MemPool({

const saverOutput = arraySaver(
externalArgs,
{ dataView, allocator },
[],
plainJSArray
);
const saverOutput = arraySaver(externalArgs, carrier, [], plainJSArray);
const arrayWrapper = createArrayWrapper(
{ ...externalArgs, arrayAdditionalAllocation: 3 },
{ dataView, allocator },
carrier,
saverOutput

@@ -652,0 +608,0 @@ );

@@ -22,7 +22,3 @@ import {

) {
const metadata = arrayGetMetadata(
externalArgs,
dataViewCarrier.dataView,
pointerToArrayEntry
);
const metadata = arrayGetMetadata(dataViewCarrier, pointerToArrayEntry);

@@ -78,4 +74,3 @@ const calcedStart = calculateSpliceStart(metadata.length, startArg);

const valueToCopyPointers = arrayGetPointersToValueInIndex(
externalArgs,
dataViewCarrier.dataView,
dataViewCarrier,
pointerToArrayEntry,

@@ -88,4 +83,3 @@ writeValueToIndex - itemCountChange

setValuePointerAtArrayIndex(
externalArgs,
dataViewCarrier.dataView,
dataViewCarrier,
pointerToArrayEntry,

@@ -115,4 +109,3 @@ writeValueToIndex,

const valueToCopyPointers = arrayGetPointersToValueInIndex(
externalArgs,
dataViewCarrier.dataView,
dataViewCarrier,
pointerToArrayEntry,

@@ -125,4 +118,3 @@ writeValueToIndex - itemCountChange

setValuePointerAtArrayIndex(
externalArgs,
dataViewCarrier.dataView,
dataViewCarrier,
pointerToArrayEntry,

@@ -154,4 +146,3 @@ writeValueToIndex,

const valueToSetPointers = arrayGetPointersToValueInIndex(
externalArgs,
dataViewCarrier.dataView,
dataViewCarrier,
pointerToArrayEntry,

@@ -172,8 +163,3 @@ calcedStart + i

if (newLength < metadata.length) {
shrinkArray(
externalArgs,
dataViewCarrier.dataView,
pointerToArrayEntry,
newLength
);
shrinkArray(externalArgs, dataViewCarrier, pointerToArrayEntry, newLength);
}

@@ -180,0 +166,0 @@

@@ -10,2 +10,3 @@ /* eslint-env jest */

import { externalArgsApiToExternalArgsApi } from "./utils";
import { makeCarrier } from "./testUtils";

@@ -22,5 +23,4 @@ describe("arrayWrapper tests", () => {

const arrayBuffer = new ArrayBuffer(256);
const dataView = new DataView(arrayBuffer);
const carrier = makeCarrier(arrayBuffer);
initializeArrayBuffer(arrayBuffer);
const allocator = new MemPool({

@@ -33,12 +33,7 @@ buf: arrayBuffer,

const saverOutput = arraySaver(
externalArgs,
{ dataView, allocator },
[],
arrayToSave
);
const saverOutput = arraySaver(externalArgs, carrier, [], arrayToSave);
const arrayWrapper: any = createArrayWrapper(
externalArgs,
{ dataView, allocator },
carrier,
saverOutput

@@ -60,3 +55,3 @@ );

const arrayBuffer = new ArrayBuffer(256);
const dataView = new DataView(arrayBuffer);
const carrier = makeCarrier(arrayBuffer);
initializeArrayBuffer(arrayBuffer);

@@ -67,15 +62,9 @@ const allocator = new MemPool({

});
const arrayToSave = ["a", "b", 1];
const saverOutput = arraySaver(
externalArgs,
{ dataView, allocator },
[],
arrayToSave
);
const saverOutput = arraySaver(externalArgs, carrier, [], arrayToSave);
const arrayWrapper = createArrayWrapper(
externalArgs,
{ dataView, allocator },
carrier,
saverOutput

@@ -91,3 +80,3 @@ );

const arrayBuffer = new ArrayBuffer(256);
const dataView = new DataView(arrayBuffer);
const carrier = makeCarrier(arrayBuffer);
initializeArrayBuffer(arrayBuffer);

@@ -98,15 +87,9 @@ const allocator = new MemPool({

});
const arrayToSave = ["a", "b", 1];
const saverOutput = arraySaver(
externalArgs,
{ dataView, allocator },
[],
arrayToSave
);
const saverOutput = arraySaver(externalArgs, carrier, [], arrayToSave);
const arrayWrapper = createArrayWrapper(
externalArgs,
{ dataView, allocator },
carrier,
saverOutput

@@ -137,3 +120,3 @@ );

const arrayBuffer = new ArrayBuffer(256);
const dataView = new DataView(arrayBuffer);
const carrier = makeCarrier(arrayBuffer);
initializeArrayBuffer(arrayBuffer);

@@ -144,15 +127,9 @@ const allocator = new MemPool({

});
const arrayToSave = ["a", "b", 1];
const saverOutput = arraySaver(
externalArgs,
{ dataView, allocator },
[],
arrayToSave
);
const saverOutput = arraySaver(externalArgs, carrier, [], arrayToSave);
const arrayWrapper = createArrayWrapper(
externalArgs,
{ dataView, allocator },
carrier,
saverOutput

@@ -181,21 +158,11 @@ );

const arrayBuffer = new ArrayBuffer(256);
const dataView = new DataView(arrayBuffer);
const carrier = makeCarrier(arrayBuffer);
initializeArrayBuffer(arrayBuffer);
const allocator = new MemPool({
buf: arrayBuffer,
start: MEM_POOL_START
});
const arrayToSave = ["a", "b", 1];
const saverOutput = arraySaver(
externalArgs,
{ dataView, allocator },
[],
arrayToSave
);
const saverOutput = arraySaver(externalArgs, carrier, [], arrayToSave);
const arrayWrapper: any = createArrayWrapper(
externalArgs,
{ dataView, allocator },
carrier,
saverOutput

@@ -217,9 +184,4 @@ );

const arrayBuffer = new ArrayBuffer(256);
const dataView = new DataView(arrayBuffer);
const carrier = makeCarrier(arrayBuffer);
initializeArrayBuffer(arrayBuffer);
const allocator = new MemPool({
buf: arrayBuffer,
start: MEM_POOL_START
});
const arrayToSave = ["a", "b", 1];

@@ -229,3 +191,3 @@

{ ...externalArgs, arrayAdditionalAllocation: 15 },
{ dataView, allocator },
carrier,
[],

@@ -237,3 +199,3 @@ arrayToSave

{ ...externalArgs, arrayAdditionalAllocation: 15 },
{ dataView, allocator },
carrier,
saverOutput

@@ -263,3 +225,3 @@ );

const arrayBuffer = new ArrayBuffer(256);
const dataView = new DataView(arrayBuffer);
const carrier = makeCarrier(arrayBuffer);
initializeArrayBuffer(arrayBuffer);

@@ -270,15 +232,9 @@ const allocator = new MemPool({

});
const arrayToSave = ["a", "b", 1];
const saverOutput = arraySaver(
externalArgs,
{ dataView, allocator },
[],
arrayToSave
);
const saverOutput = arraySaver(externalArgs, carrier, [], arrayToSave);
const arrayWrapper: any = createArrayWrapper(
{ ...externalArgs, arrayAdditionalAllocation: 3 },
{ dataView, allocator },
carrier,
saverOutput

@@ -310,3 +266,3 @@ );

const arrayBuffer = new ArrayBuffer(512);
const dataView = new DataView(arrayBuffer);
const carrier = makeCarrier(arrayBuffer);
initializeArrayBuffer(arrayBuffer);

@@ -320,12 +276,7 @@ const allocator = new MemPool({

const saverOutput = arraySaver(
externalArgs,
{ dataView, allocator },
[],
arrayToSave
);
const saverOutput = arraySaver(externalArgs, carrier, [], arrayToSave);
const arrayWrapper = createArrayWrapper(
{ ...externalArgs, arrayAdditionalAllocation: 3 },
{ dataView, allocator },
carrier,
saverOutput

@@ -349,3 +300,3 @@ );

expect(allocator.stats().available).toMatchInlineSnapshot(`152`);
expect(allocator.stats().available).toMatchInlineSnapshot(`184`);
});

@@ -355,3 +306,3 @@

const arrayBuffer = new ArrayBuffer(2048);
const dataView = new DataView(arrayBuffer);
const carrier = makeCarrier(arrayBuffer);
initializeArrayBuffer(arrayBuffer);

@@ -367,12 +318,7 @@ const allocator = new MemPool({

const saverOutput = arraySaver(
externalArgs,
{ dataView, allocator },
[],
arrayToSave
);
const saverOutput = arraySaver(externalArgs, carrier, [], arrayToSave);
const arrayWrapper = createArrayWrapper(
{ ...externalArgs, arrayAdditionalAllocation: 3 },
{ dataView, allocator },
carrier,
saverOutput

@@ -419,3 +365,3 @@ );

const arrayBuffer = new ArrayBuffer(512);
const dataView = new DataView(arrayBuffer);
const carrier = makeCarrier(arrayBuffer);
initializeArrayBuffer(arrayBuffer);

@@ -429,12 +375,7 @@ const allocator = new MemPool({

const saverOutput = arraySaver(
externalArgs,
{ dataView, allocator },
[],
arrayToSave
);
const saverOutput = arraySaver(externalArgs, carrier, [], arrayToSave);
const arrayWrapper = createArrayWrapper(
{ ...externalArgs, arrayAdditionalAllocation: 3 },
{ dataView, allocator },
carrier,
saverOutput

@@ -466,3 +407,3 @@ );

const arrayBuffer = new ArrayBuffer(512);
const dataView = new DataView(arrayBuffer);
const carrier = makeCarrier(arrayBuffer);
initializeArrayBuffer(arrayBuffer);

@@ -476,12 +417,7 @@ const allocator = new MemPool({

const saverOutput = arraySaver(
externalArgs,
{ dataView, allocator },
[],
arrayToSave
);
const saverOutput = arraySaver(externalArgs, carrier, [], arrayToSave);
const arrayWrapper = createArrayWrapper(
{ ...externalArgs, arrayAdditionalAllocation: 3 },
{ dataView, allocator },
carrier,
saverOutput

@@ -488,0 +424,0 @@ );

@@ -37,7 +37,3 @@ import {

if (p === "length") {
return arrayGetMetadata(
this.externalArgs,
this.carrier.dataView,
this.entryPointer
).length;
return arrayGetMetadata(this.carrier, this.entryPointer).length;
}

@@ -74,7 +70,3 @@

public ownKeys(): PropertyKey[] {
const length = arrayGetMetadata(
this.externalArgs,
this.carrier.dataView,
this.entryPointer
).length;
const length = arrayGetMetadata(this.carrier, this.entryPointer).length;

@@ -101,7 +93,3 @@ return [...new Array(length).keys(), "length"];

const length = arrayGetMetadata(
this.externalArgs,
this.carrier.dataView,
this.entryPointer
).length;
const length = arrayGetMetadata(this.carrier, this.entryPointer).length;

@@ -127,7 +115,4 @@ if (typeof p === "number") {

const currentLength = arrayGetMetadata(
this.externalArgs,
this.carrier.dataView,
this.entryPointer
).length;
const currentLength = arrayGetMetadata(this.carrier, this.entryPointer)
.length;

@@ -199,7 +184,3 @@ if (currentLength === value) {

length = arrayGetMetadata(
this.externalArgs,
this.carrier.dataView,
this.entryPointer
).length;
length = arrayGetMetadata(this.carrier, this.entryPointer).length;
} while (index < length);

@@ -217,7 +198,3 @@ }

length = arrayGetMetadata(
this.externalArgs,
this.carrier.dataView,
this.entryPointer
).length;
length = arrayGetMetadata(this.carrier, this.entryPointer).length;
} while (index < length);

@@ -240,7 +217,3 @@ }

length = arrayGetMetadata(
this.externalArgs,
this.carrier.dataView,
this.entryPointer
).length;
length = arrayGetMetadata(this.carrier, this.entryPointer).length;
} while (index < length);

@@ -269,3 +242,3 @@ }

public reverse() {
arrayReverse(this.externalArgs, this.carrier.dataView, this.entryPointer);
arrayReverse(this.externalArgs, this.carrier, this.entryPointer);
return this;

@@ -287,7 +260,3 @@ }

return arrayGetMetadata(
this.externalArgs,
this.carrier.dataView,
this.entryPointer
).length;
return arrayGetMetadata(this.carrier, this.entryPointer).length;
}

@@ -294,0 +263,0 @@

@@ -11,3 +11,2 @@ import {

} from "./interfaces";
import { IMemPool } from "@thi.ng/malloc";
import { incrementRefCount, decrementRefCount, readEntry } from "./store";

@@ -24,7 +23,3 @@ import { WrapperDestroyed } from "./exceptions";

) {
incrementRefCount(
this.externalArgs,
this.carrier.dataView,
this.entryPointer
);
incrementRefCount(this.externalArgs, this.carrier, this.entryPointer);
}

@@ -35,3 +30,3 @@

this.externalArgs,
this.carrier.dataView,
this.carrier,
this.entryPointer

@@ -48,5 +43,4 @@ );

public replaceCarrierContent(dataView: DataView, allocator: IMemPool) {
this.carrier.dataView = dataView;
this.carrier.allocator = allocator;
public replaceCarrierContent(newCarrierContent: DataViewAndAllocatorCarrier) {
Object.assign(this.carrier, newCarrierContent);
}

@@ -71,8 +65,4 @@

protected get entry(): T {
return readEntry(
this.externalArgs,
this.carrier.dataView,
this.entryPointer
) as T;
return readEntry(this.carrier, this.entryPointer) as T;
}
}

@@ -10,1 +10,6 @@ export const LOCK_OFFSET = 0;

INITIAL_ENTRY_POINTER_VALUE + Uint32Array.BYTES_PER_ELEMENT;
export const UNDEFINED_KNOWN_ADDRESS = 0;
export const NULL_KNOWN_ADDRESS = 1;
export const TRUE_KNOWN_ADDRESS = 2;
export const FALSE_KNOWN_ADDRESS = 3;

@@ -111,7 +111,3 @@ import {

private updateDateObjectForReuse() {
const entry = readEntry(
this.externalArgs,
this.carrier.dataView,
this.entryPointer
) as DateEntry;
const entry = readEntry(this.carrier, this.entryPointer) as DateEntry;

@@ -122,3 +118,3 @@ this.dateObjectForReuse.setTime(entry.value);

private persistDateObject() {
writeEntry(this.externalArgs, this.carrier.dataView, this.entryPointer, {
writeEntry(this.carrier, this.entryPointer, {
type: ENTRY_TYPE.DATE,

@@ -125,0 +121,0 @@ refsCount: this.entry.refsCount,

import { getInternalAPI } from "./utils";
import { getCacheFor } from "./externalObjectsCache";
import { getAllLinkedAddresses } from "./getAllLinkedAddresses";
import { decrementRefCount } from "./store";

@@ -19,4 +20,3 @@ /**

const addressesToFree = getAllLinkedAddresses(
internalApi.getExternalArgs(),
internalApi.getCarrier().dataView,
internalApi.getCarrier(),
false,

@@ -26,6 +26,14 @@ entryPointer

for (const address of addressesToFree) {
for (const address of addressesToFree.leafAddresses) {
internalApi.getCarrier().allocator.free(address);
}
for (const address of addressesToFree.arcAddresses) {
decrementRefCount(
internalApi.getExternalArgs(),
internalApi.getCarrier(),
address
);
}
return true;

@@ -32,0 +40,0 @@ }

import { createKnownTypeGuard } from "./utils";
export enum ENTRY_TYPE {
/**
* @deprecated
*/
UNDEFINED,
/**
* @deprecated
*/
NULL,

@@ -10,5 +16,10 @@ NUMBER,

STRING,
/**
* @deprecated
*/
BOOLEAN,
OBJECT,
// deprecated
/**
* @deprecated
*/
OBJECT_PROP,

@@ -23,8 +34,5 @@ ARRAY,

export const PRIMITIVE_TYPES = [
ENTRY_TYPE.NULL,
ENTRY_TYPE.UNDEFINED,
ENTRY_TYPE.NUMBER,
ENTRY_TYPE.BIGINT_POSITIVE,
ENTRY_TYPE.BIGINT_NEGATIVE,
ENTRY_TYPE.BOOLEAN,
ENTRY_TYPE.STRING

@@ -31,0 +39,0 @@ ] as const;

@@ -11,2 +11,8 @@ import { ExternalArgs, DataViewAndAllocatorCarrier } from "./interfaces";

import { createSetWrapper } from "./setWrapper";
import {
UNDEFINED_KNOWN_ADDRESS,
NULL_KNOWN_ADDRESS,
TRUE_KNOWN_ADDRESS,
FALSE_KNOWN_ADDRESS
} from "./consts";

@@ -29,12 +35,20 @@ // declare const FinalizationGroup: any;

) {
const valueEntry = readEntry(externalArgs, carrier.dataView, pointerToEntry);
if (pointerToEntry === UNDEFINED_KNOWN_ADDRESS) {
return undefined;
}
if (valueEntry.type === ENTRY_TYPE.NULL) {
if (pointerToEntry === NULL_KNOWN_ADDRESS) {
return null;
}
if (valueEntry.type === ENTRY_TYPE.UNDEFINED) {
return undefined;
if (pointerToEntry === TRUE_KNOWN_ADDRESS) {
return true;
}
if (pointerToEntry === FALSE_KNOWN_ADDRESS) {
return false;
}
const valueEntry = readEntry(carrier, pointerToEntry);
if (isPrimitiveEntryType(valueEntry.type)) {

@@ -79,20 +93,15 @@ return valueEntry.value;

) {
const newRefsCount = decrementRefCount(
externalArgs,
carrier.dataView,
memoryAddress
);
const newRefsCount = decrementRefCount(externalArgs, carrier, memoryAddress);
if (newRefsCount === 0) {
const freeUs = getAllLinkedAddresses(
externalArgs,
carrier.dataView,
false,
memoryAddress
);
const freeUs = getAllLinkedAddresses(carrier, false, memoryAddress);
for (const address of freeUs) {
for (const address of freeUs.leafAddresses) {
carrier.allocator.free(address);
}
for (const address of freeUs.arcAddresses) {
decrementRefCount(externalArgs, carrier, address);
}
}
}

@@ -5,7 +5,3 @@ /* eslint-env jest */

import { MemPool } from "@thi.ng/malloc";
import {
createObjectBuffer,
memoryStats,
getUnderlyingArrayBuffer
} from "./api";
import { createObjectBuffer, memoryStats } from "./api";
import { getAllLinkedAddresses } from "./getAllLinkedAddresses";

@@ -38,8 +34,12 @@ import { getInternalAPI, externalArgsApiToExternalArgsApi } from "./utils";

const dataView = new DataView(getUnderlyingArrayBuffer(objectBuffer));
// const a = objectBuffer.nestedObject;
// getInternalAPI(a).destroy();
// // a.toString();
const carrier = getInternalAPI(objectBuffer).getCarrier();
getInternalAPI(objectBuffer).destroy();
const linkedAddresses = getAllLinkedAddresses(
externalArgs,
dataView,
carrier,
false,

@@ -49,17 +49,16 @@ allocatedAddresses[allocatedAddresses.length - 1]

expect(linkedAddresses.slice().sort()).toEqual(
expect(linkedAddresses.leafAddresses.slice().sort()).toEqual(
allocatedAddresses.slice().sort()
);
expect(linkedAddresses.slice().sort()).toMatchInlineSnapshot(`
expect(linkedAddresses.leafAddresses.slice().sort())
.toMatchInlineSnapshot(`
Array [
1008,
1024,
1032,
1048,
1064,
1080,
1096,
1112,
1104,
112,
1136,
128,

@@ -81,24 +80,24 @@ 144,

448,
464,
472,
48,
488,
504,
520,
544,
568,
528,
552,
576,
592,
608,
616,
632,
648,
656,
672,
688,
704,
72,
808,
832,
856,
880,
792,
816,
840,
864,
904,
920,
936,
952,
960,
976,

@@ -130,8 +129,8 @@ 992,

Object {
"available": 904,
"used": 1144,
"available": 936,
"used": 1112,
}
`);
const dataView = new DataView(getUnderlyingArrayBuffer(objectBuffer));
const carrier = getInternalAPI(objectBuffer).getCarrier();

@@ -141,4 +140,3 @@ getInternalAPI(objectBuffer).destroy();

const linkedAddresses = getAllLinkedAddresses(
externalArgs,
dataView,
carrier,
false,

@@ -148,3 +146,3 @@ allocatedAddresses[allocatedAddresses.length - 1]

linkedAddresses.forEach(address => {
linkedAddresses.leafAddresses.forEach(address => {
pool.free(address);

@@ -151,0 +149,0 @@ });

import { readEntry } from "./store";
import { ExternalArgs, ObjectEntry } from "./interfaces";
import { DataViewAndAllocatorCarrier } from "./interfaces";
import { ENTRY_TYPE } from "./entry-types";
import { hashMapGetPointersToFree } from "./hashmap/hashmap";
import { isKnownAddressValuePointer } from "./utils";
export function getAllLinkedAddresses(
externalArgs: ExternalArgs,
dataView: DataView,
carrier: DataViewAndAllocatorCarrier,
ignoreRefCount: boolean,
entryPointer: number
) {
const allAddresses: number[] = [];
const leafAddresses: number[] = [];
const arcAddresses: number[] = [];
getAllLinkedAddressesStep(
externalArgs,
dataView,
carrier,
ignoreRefCount,
entryPointer,
allAddresses
leafAddresses,
arcAddresses
);
return allAddresses;
return { leafAddresses, arcAddresses };
}
function getAllLinkedAddressesStep(
externalArgs: ExternalArgs,
dataView: DataView,
carrier: DataViewAndAllocatorCarrier,
ignoreRefCount: boolean,
entryPointer: number,
pushTo: number[]
leafAddresses: number[],
arcAddresses: number[]
) {
if (entryPointer === 0) {
if (isKnownAddressValuePointer(entryPointer)) {
return;
}
const entry = readEntry(externalArgs, dataView, entryPointer);
const entry = readEntry(carrier, entryPointer);
switch (entry.type) {
case ENTRY_TYPE.UNDEFINED:
case ENTRY_TYPE.NULL:
case ENTRY_TYPE.BOOLEAN:
case ENTRY_TYPE.NUMBER:

@@ -45,15 +44,19 @@ case ENTRY_TYPE.STRING:

case ENTRY_TYPE.BIGINT_POSITIVE:
pushTo.push(entryPointer);
leafAddresses.push(entryPointer);
break;
case ENTRY_TYPE.OBJECT:
if (entry.refsCount < 2 || ignoreRefCount) {
pushTo.push(entryPointer);
getObjectAddresses(
externalArgs,
dataView,
case ENTRY_TYPE.MAP:
case ENTRY_TYPE.SET:
if (entry.refsCount <= 1 || ignoreRefCount) {
leafAddresses.push(entryPointer);
getObjectOrMapOrSetAddresses(
carrier,
ignoreRefCount,
entry,
pushTo
entry.value,
leafAddresses,
arcAddresses
);
} else {
arcAddresses.push(entryPointer);
}

@@ -64,20 +67,21 @@

case ENTRY_TYPE.ARRAY:
if (entry.refsCount < 2 || ignoreRefCount) {
pushTo.push(entryPointer);
pushTo.push(entry.value);
if (entry.refsCount <= 1 || ignoreRefCount) {
leafAddresses.push(entryPointer);
leafAddresses.push(entry.value);
for (let i = 0; i < entry.allocatedLength; i += 1) {
const valuePointer = dataView.getUint32(
const valuePointer = carrier.dataView.getUint32(
entry.value + i * Uint32Array.BYTES_PER_ELEMENT
);
if (valuePointer !== 0) {
getAllLinkedAddressesStep(
externalArgs,
dataView,
ignoreRefCount,
valuePointer,
pushTo
);
}
getAllLinkedAddressesStep(
carrier,
ignoreRefCount,
valuePointer,
leafAddresses,
arcAddresses
);
}
} else {
arcAddresses.push(entryPointer);
}

@@ -87,4 +91,6 @@ break;

case ENTRY_TYPE.DATE:
if (entry.refsCount < 2 || ignoreRefCount) {
pushTo.push(entryPointer);
if (entry.refsCount <= 1 || ignoreRefCount) {
leafAddresses.push(entryPointer);
} else {
arcAddresses.push(entryPointer);
}

@@ -100,25 +106,25 @@ break;

function getObjectAddresses(
externalArgs: ExternalArgs,
dataView: DataView,
export function getObjectOrMapOrSetAddresses(
carrier: DataViewAndAllocatorCarrier,
ignoreRefCount: boolean,
objectEntry: ObjectEntry,
pushTo: number[]
internalHashmapPointer: number,
leafAddresses: number[],
arcAddresses: number[]
) {
const { pointersToValuePointers, pointers } = hashMapGetPointersToFree(
dataView,
objectEntry.value
carrier.dataView,
internalHashmapPointer
);
pushTo.push(...pointers);
leafAddresses.push(...pointers);
for (const pointer of pointersToValuePointers) {
getAllLinkedAddressesStep(
externalArgs,
dataView,
carrier,
ignoreRefCount,
dataView.getUint32(pointer),
pushTo
carrier.dataView.getUint32(pointer),
leafAddresses,
arcAddresses
);
}
}
/* eslint-env jest */
import * as util from "util";
import { arrayBuffer2HexArray, recordAllocations } from "../testUtils";
import { MemPool } from "@thi.ng/malloc";
import { MEM_POOL_START } from "../consts";
import {
arrayBuffer2HexArray,
recordAllocations,
makeCarrier
} from "../testUtils";
import {
createHashMap,

@@ -28,22 +30,7 @@ hashMapInsertUpdate,

let ab = new ArrayBuffer(128);
let dataView = new DataView(ab);
let allocator = new MemPool({
buf: ab
});
let carrier: DataViewAndAllocatorCarrier = {
dataView,
allocator
};
let carrier: DataViewAndAllocatorCarrier = makeCarrier(ab);
function setABSize(size: number) {
ab = new ArrayBuffer(size);
dataView = new DataView(ab);
allocator = new MemPool({
buf: ab,
start: MEM_POOL_START
});
carrier = {
dataView,
allocator
};
carrier = makeCarrier(ab);
}

@@ -68,3 +55,3 @@

externalArgs,
{ dataView, allocator },
carrier,
mapPointer,

@@ -74,3 +61,3 @@ 3

dataView.setUint32(valuePointer, 5);
carrier.dataView.setUint32(valuePointer, 5);

@@ -85,3 +72,3 @@ expect(arrayBuffer2HexArray(ab, true)).toMatchSnapshot("after insert");

externalArgs,
{ dataView, allocator },
carrier,
mapPointer,

@@ -91,3 +78,3 @@ "abc"

dataView.setUint32(pointer, 6);
carrier.dataView.setUint32(pointer, 6);

@@ -102,15 +89,5 @@ expect(arrayBuffer2HexArray(ab, true)).toMatchSnapshot("after insert");

const pointer = hashMapInsertUpdate(
externalArgs,
{ dataView, allocator },
mapPointer,
key
);
const pointer = hashMapInsertUpdate(externalArgs, carrier, mapPointer, key);
const foundValuePointer = hashMapValueLookup(
externalArgs,
dataView,
mapPointer,
key
);
const foundValuePointer = hashMapValueLookup(carrier, mapPointer, key);

@@ -127,3 +104,3 @@ expect(pointer).toBe(foundValuePointer);

externalArgs,
{ dataView, allocator },
carrier,
mapPointer,

@@ -133,8 +110,3 @@ key

const foundValuePointer = hashMapValueLookup(
externalArgs,
dataView,
mapPointer,
key
);
const foundValuePointer = hashMapValueLookup(carrier, mapPointer, key);
expect(foundValuePointer).toBe(valuePointer);

@@ -150,3 +122,3 @@ });

externalArgs,
{ dataView, allocator },
carrier,
mapPointer,

@@ -158,8 +130,3 @@ key

const foundValuePointer = hashMapValueLookup(
externalArgs,
dataView,
mapPointer,
key
);
const foundValuePointer = hashMapValueLookup(carrier, mapPointer, key);

@@ -176,3 +143,3 @@ expect(foundValuePointer).toBe(firstValuePointer);

externalArgs,
{ dataView, allocator },
carrier,
mapPointer,

@@ -185,4 +152,3 @@ key

const foundValuePointer = hashMapValueLookup(
externalArgs,
dataView,
carrier,
mapPointer,

@@ -206,10 +172,5 @@ "Not a real key"

inserts.push(
hashMapInsertUpdate(
externalArgs,
{ dataView, allocator },
mapPointer,
value
)
hashMapInsertUpdate(externalArgs, carrier, mapPointer, value)
);
dataView.setUint32(inserts[inserts.length - 1], index);
carrier.dataView.setUint32(inserts[inserts.length - 1], index);
}

@@ -225,3 +186,3 @@

(iteratorToken = hashMapLowLevelIterator(
dataView,
carrier.dataView,
mapPointer,

@@ -231,9 +192,8 @@ iteratorToken

) {
values.push(hashMapNodePointerToKeyValue(dataView, iteratorToken));
values.push(
hashMapNodePointerToKeyValue(carrier.dataView, iteratorToken)
);
}
expect(
values.map(
v =>
(readEntry(externalArgs, dataView, v.keyPointer) as StringEntry).value
)
values.map(v => (readEntry(carrier, v.keyPointer) as StringEntry).value)
).toMatchInlineSnapshot(`

@@ -275,4 +235,6 @@ Array [

expect(hashMapSize(dataView, mapPointer)).toMatchInlineSnapshot(`0`);
const memAvailableAfterEachStep = [allocator.stats().available];
expect(hashMapSize(carrier.dataView, mapPointer)).toMatchInlineSnapshot(
`0`
);
const memAvailableAfterEachStep = [carrier.allocator.stats().available];

@@ -284,22 +246,21 @@ const input = [...new Array(26).keys()]

for (const [index, useThatAsKey] of input.entries()) {
dataView.setUint32(
hashMapInsertUpdate(
externalArgs,
{ dataView, allocator },
mapPointer,
useThatAsKey
),
carrier.dataView.setUint32(
hashMapInsertUpdate(externalArgs, carrier, mapPointer, useThatAsKey),
index
);
memAvailableAfterEachStep.push(allocator.stats().available);
memAvailableAfterEachStep.push(carrier.allocator.stats().available);
}
expect(hashMapSize(dataView, mapPointer)).toMatchInlineSnapshot(`26`);
expect(hashMapSize(carrier.dataView, mapPointer)).toMatchInlineSnapshot(
`26`
);
hashMapDelete(externalArgs, carrier, mapPointer, "a");
hashMapDelete(externalArgs, carrier, mapPointer, "b");
hashMapDelete(externalArgs, carrier, mapPointer, "c");
expect(hashMapSize(dataView, mapPointer)).toMatchInlineSnapshot(`23`);
memAvailableAfterEachStep.push(allocator.stats().available);
hashMapDelete(carrier, mapPointer, "a");
hashMapDelete(carrier, mapPointer, "b");
hashMapDelete(carrier, mapPointer, "c");
expect(hashMapSize(carrier.dataView, mapPointer)).toMatchInlineSnapshot(
`23`
);
memAvailableAfterEachStep.push(carrier.allocator.stats().available);

@@ -351,5 +312,5 @@ expect(memAvailableAfterEachStep).toMatchInlineSnapshot(`

const { allocations } = recordAllocations(() => {
hashmapPointer = createHashMap({ dataView, allocator });
hashmapPointer = createHashMap(carrier);
expect(allocator.stats().available).toMatchInlineSnapshot(`880`);
expect(carrier.allocator.stats().available).toMatchInlineSnapshot(`880`);

@@ -359,15 +320,10 @@ let toAdd: undefined | string;

while ((toAdd = inputCopy.pop()) !== undefined) {
dataView.setUint32(
hashMapInsertUpdate(
externalArgs,
{ dataView, allocator },
hashmapPointer,
toAdd
),
carrier.dataView.setUint32(
hashMapInsertUpdate(externalArgs, carrier, hashmapPointer, toAdd),
toAdd.charCodeAt(0)
);
}
}, allocator);
}, carrier.allocator);
const r = hashMapGetPointersToFree(dataView, hashmapPointer);
const r = hashMapGetPointersToFree(carrier.dataView, hashmapPointer);

@@ -430,3 +386,3 @@ expect(r).toMatchInlineSnapshot(`

r.pointersToValuePointers
.map(v => String.fromCharCode(dataView.getUint32(v)))
.map(v => String.fromCharCode(carrier.dataView.getUint32(v)))
.sort()

@@ -433,0 +389,0 @@ ).toEqual(input.sort());

@@ -65,24 +65,22 @@ import { MAP_MACHINE, NODE_MACHINE } from "./memoryLayout";

externalArgs: ExternalArgs,
{ dataView, allocator }: DataViewAndAllocatorCarrier,
carrier: DataViewAndAllocatorCarrier,
mapPointer: number,
externalKeyValue: number | string
) {
const mapOperator = MAP_MACHINE.createOperator(dataView, mapPointer);
const keyEntry = primitiveValueToEntry(
{ ...externalArgs, minimumStringAllocation: 0 },
externalKeyValue,
0
) as NumberEntry | StringEntry;
const mapOperator = MAP_MACHINE.createOperator(carrier.dataView, mapPointer);
const keyEntry = primitiveValueToEntry(externalKeyValue) as
| NumberEntry
| StringEntry;
// allocate all possible needed memory upfront, so we won't oom in the middle of something
// in case of overwrite, we will not need this memory
const memoryForNewNode = allocator.calloc(NODE_MACHINE.map.SIZE_OF);
const memoryForNewNode = carrier.allocator.calloc(NODE_MACHINE.map.SIZE_OF);
const memorySizeOfKey = sizeOfEntry(keyEntry);
const keyEntryMemory = allocator.calloc(memorySizeOfKey);
const keyEntryMemory = carrier.allocator.calloc(memorySizeOfKey);
writeEntry(externalArgs, dataView, keyEntryMemory, keyEntry);
writeEntry(carrier, keyEntryMemory, keyEntry);
const keyHeaderOverhead = keyEntry.type === ENTRY_TYPE.STRING ? 5 : 1;
const keyHashCode = hashCodeInPlace(
dataView,
carrier.dataView,
mapOperator.get("CAPACITY"),

@@ -99,4 +97,4 @@ // + 1 for the type of key

const commonNodeOperator = NODE_MACHINE.createOperator(
dataView,
dataView.getUint32(ptrToPtrToSaveTheNodeTo)
carrier.dataView,
carrier.dataView.getUint32(ptrToPtrToSaveTheNodeTo)
);

@@ -108,3 +106,3 @@

!compareStringOrNumberEntriesInPlace(
dataView,
carrier.dataView,
commonNodeOperator.get("KEY_POINTER"),

@@ -132,4 +130,4 @@ keyEntryMemory

// we don't need the new memory
allocator.free(keyEntryMemory);
allocator.free(memoryForNewNode);
carrier.allocator.free(keyEntryMemory);
carrier.allocator.free(memoryForNewNode);

@@ -143,3 +141,3 @@ return commonNodeOperator.pointerTo("VALUE_POINTER");

linkedListItemInsert(
{ dataView, allocator },
carrier,
mapOperator.get("LINKED_LIST_POINTER"),

@@ -150,3 +148,3 @@ memoryForNewNode

dataView.setUint32(ptrToPtrToSaveTheNodeTo, memoryForNewNode);
carrier.dataView.setUint32(ptrToPtrToSaveTheNodeTo, memoryForNewNode);

@@ -169,7 +167,3 @@ mapOperator.set(

// });
hashMapRehash(
{ dataView, allocator },
mapOperator,
mapOperator.get("CAPACITY") * 2
);
hashMapRehash(carrier, mapOperator, mapOperator.get("CAPACITY") * 2);
}

@@ -185,11 +179,9 @@

export function hashMapNodeLookup(
externalArgs: ExternalArgs,
dataView: DataView,
carrier: DataViewAndAllocatorCarrier,
mapPointer: number,
externalKeyValue: number | string
) {
const mapMachine = MAP_MACHINE.createOperator(dataView, mapPointer);
const mapMachine = MAP_MACHINE.createOperator(carrier.dataView, mapPointer);
const keyHashCode = hashCodeExternalValue(
externalArgs,
mapMachine.get("CAPACITY"),

@@ -204,12 +196,10 @@ externalKeyValue

const node = NODE_MACHINE.createOperator(
dataView,
dataView.getUint32(ptrToPtr)
carrier.dataView,
carrier.dataView.getUint32(ptrToPtr)
);
while (node.startAddress !== 0) {
const keyEntry = readEntry(
externalArgs,
dataView,
node.get("KEY_POINTER")
) as NumberEntry | StringEntry;
const keyEntry = readEntry(carrier, node.get("KEY_POINTER")) as
| NumberEntry
| StringEntry;

@@ -228,13 +218,7 @@ if (keyEntry.value === externalKeyValue) {

export function hashMapValueLookup(
externalArgs: ExternalArgs,
dataView: DataView,
carrier: DataViewAndAllocatorCarrier,
mapPointer: number,
externalKeyValue: number | string
) {
const nodePtrToPtr = hashMapNodeLookup(
externalArgs,
dataView,
mapPointer,
externalKeyValue
);
const nodePtrToPtr = hashMapNodeLookup(carrier, mapPointer, externalKeyValue);

@@ -246,4 +230,4 @@ if (nodePtrToPtr === 0) {

const node = NODE_MACHINE.createOperator(
dataView,
dataView.getUint32(nodePtrToPtr)
carrier.dataView,
carrier.dataView.getUint32(nodePtrToPtr)
);

@@ -258,4 +242,3 @@

export function hashMapDelete(
externalArgs: ExternalArgs,
{ dataView, allocator }: DataViewAndAllocatorCarrier,
carrier: DataViewAndAllocatorCarrier,
mapPointer: number,

@@ -265,4 +248,3 @@ externalKeyValue: number | string

const foundNodePtrToPtr = hashMapNodeLookup(
externalArgs,
dataView,
carrier,
mapPointer,

@@ -276,6 +258,6 @@ externalKeyValue

const nodeToDeletePointer = dataView.getUint32(foundNodePtrToPtr);
const nodeToDeletePointer = carrier.dataView.getUint32(foundNodePtrToPtr);
const nodeOperator = NODE_MACHINE.createOperator(
dataView,
carrier.dataView,
nodeToDeletePointer

@@ -286,16 +268,16 @@ );

linkedListItemRemove(
{ dataView, allocator },
nodeOperator.get("LINKED_LIST_ITEM_POINTER")
);
linkedListItemRemove(carrier, nodeOperator.get("LINKED_LIST_ITEM_POINTER"));
// remove node from bucket
dataView.setUint32(foundNodePtrToPtr, nodeOperator.get("NEXT_NODE_POINTER"));
carrier.dataView.setUint32(
foundNodePtrToPtr,
nodeOperator.get("NEXT_NODE_POINTER")
);
allocator.free(nodeOperator.get("KEY_POINTER"));
allocator.free(nodeOperator.startAddress);
carrier.allocator.free(nodeOperator.get("KEY_POINTER"));
carrier.allocator.free(nodeOperator.startAddress);
dataView.setUint32(
carrier.dataView.setUint32(
mapPointer + MAP_MACHINE.map.LINKED_LIST_SIZE.bytesOffset,
dataView.getUint32(
carrier.dataView.getUint32(
mapPointer + MAP_MACHINE.map.LINKED_LIST_SIZE.bytesOffset

@@ -436,3 +418,3 @@ ) - 1

function hashMapRehashInsert(
{ dataView }: DataViewAndAllocatorCarrier,
carrier: DataViewAndAllocatorCarrier,
bucketsArrayPointer: number,

@@ -442,7 +424,13 @@ arraySize: number,

) {
const nodeOperator = NODE_MACHINE.createOperator(dataView, nodePointer);
const keyInfo = getKeyStartLength(dataView, nodeOperator.get("KEY_POINTER"));
const nodeOperator = NODE_MACHINE.createOperator(
carrier.dataView,
nodePointer
);
const keyInfo = getKeyStartLength(
carrier.dataView,
nodeOperator.get("KEY_POINTER")
);
const keyHashCode = hashCodeInPlace(
dataView,
carrier.dataView,
arraySize,

@@ -457,4 +445,4 @@ keyInfo.start,

const prevFirstNodeInBucket = dataView.getUint32(bucketStartPointer);
dataView.setUint32(bucketStartPointer, nodePointer);
const prevFirstNodeInBucket = carrier.dataView.getUint32(bucketStartPointer);
carrier.dataView.setUint32(bucketStartPointer, nodePointer);
nodeOperator.set("NEXT_NODE_POINTER", prevFirstNodeInBucket);

@@ -464,3 +452,3 @@

// if (nodeOperator.startAddress === 0) {
// dataView.setUint32(bucketStartPointer, nodePointer);
// carrier.dataView.setUint32(bucketStartPointer, nodePointer);
// return;

@@ -467,0 +455,0 @@ // }

@@ -1,3 +0,3 @@

import { ExternalArgs } from "../interfaces";
import { ENTRY_TYPE } from "../entry-types";
import { stringEncodeInto } from "../stringEncodeInto";

@@ -25,15 +25,16 @@ export function hashCodeInPlace(

export function hashCodeExternalValue(
externalArgs: ExternalArgs,
capacity: number,
value: string | number
): number {
let dv: DataView;
const ab = new ArrayBuffer(typeof value === "string" ? value.length * 3 : 8);
const dv = new DataView(ab);
let keyBytesLength = ab.byteLength;
if (typeof value === "string") {
dv = new DataView(externalArgs.textEncoder.encode(value).buffer);
keyBytesLength = stringEncodeInto(new Uint8Array(ab), 0, value);
} else {
dv = new DataView(new ArrayBuffer(8));
dv.setFloat64(0, value);
}
return hashCodeInPlace(dv, capacity, 0, dv.buffer.byteLength);
return hashCodeInPlace(dv, capacity, 0, keyBytesLength);
}

@@ -40,0 +41,0 @@

import { ENTRY_TYPE } from "./entry-types";
import { TextDecoder, TextEncoder } from "./textEncoderDecoderTypes";
import { IMemPool } from "@thi.ng/malloc";

@@ -8,5 +7,2 @@ export type primitive = string | number | bigint | boolean | undefined | null;

export type Entry =
| NullEntry
| NullUndefined
| BooleanEntry
| StringEntry

@@ -22,15 +18,2 @@ | NumberEntry

export interface NullEntry {
type: ENTRY_TYPE.NULL;
}
export interface NullUndefined {
type: ENTRY_TYPE.UNDEFINED;
}
export interface BooleanEntry {
type: ENTRY_TYPE.BOOLEAN;
value: boolean;
}
export interface StringEntry {

@@ -105,2 +88,7 @@ type: ENTRY_TYPE.STRING;

dataView: DataView;
uint8: Uint8Array;
uint16: Uint16Array;
uint32: Uint32Array;
float64: Float64Array;
bigUint64: BigUint64Array;
allocator: import("@thi.ng/malloc").IMemPool;

@@ -113,3 +101,2 @@ }

arrayAdditionalAllocation: number;
minimumStringAllocation: number;
textDecoder: TextDecoder;

@@ -123,3 +110,2 @@ textEncoder: TextEncoder;

arrayAdditionalAllocation?: number;
minimumStringAllocation?: number;
textDecoder: TextDecoder;

@@ -132,5 +118,5 @@ textEncoder: TextEncoder;

getCarrier(): Readonly<DataViewAndAllocatorCarrier>;
replaceCarrierContent(dataView: DataView, pool: IMemPool): void;
replaceCarrierContent(carrier: DataViewAndAllocatorCarrier): void;
getEntryPointer(): number;
destroy(): number;
}
/* eslint-env jest */
import { arrayBuffer2HexArray, recordAllocations } from "../testUtils";
import { MemPool } from "@thi.ng/malloc";
import { MEM_POOL_START } from "../consts";
import {
arrayBuffer2HexArray,
recordAllocations,
makeCarrier
} from "../testUtils";

@@ -23,14 +25,7 @@ // eslint-disable-next-line @typescript-eslint/ban-ts-ignore

let ab = new ArrayBuffer(64);
let dataView = new DataView(ab);
let allocator = new MemPool({
buf: ab
});
let carrier = makeCarrier(ab);
function setABSize(size: number) {
ab = new ArrayBuffer(size);
dataView = new DataView(ab);
allocator = new MemPool({
buf: ab,
start: MEM_POOL_START
});
carrier = makeCarrier(ab);
}

@@ -45,11 +40,7 @@

const linkedListPointer = initLinkedList({ dataView, allocator });
const linkedListPointer = initLinkedList(carrier);
expect(arrayBuffer2HexArray(ab, true)).toMatchSnapshot();
const itemPointer = linkedListItemInsert(
{ dataView, allocator },
linkedListPointer,
7
);
const itemPointer = linkedListItemInsert(carrier, linkedListPointer, 7);

@@ -62,19 +53,11 @@ expect(itemPointer).toBe(64);

test("linkedList init & add 2 & delete 2", () => {
const linkedListPointer = initLinkedList({ dataView, allocator });
const linkedListPointer = initLinkedList(carrier);
// const copyToCompare = ab.slice(0);
expect(allocator.stats().available).toMatchInlineSnapshot(`56`);
expect(carrier.allocator.stats().available).toMatchInlineSnapshot(`56`);
const pointer1 = linkedListItemInsert(
{ dataView, allocator },
linkedListPointer,
7
);
const pointer2 = linkedListItemInsert(
{ dataView, allocator },
linkedListPointer,
8
);
const pointer1 = linkedListItemInsert(carrier, linkedListPointer, 7);
const pointer2 = linkedListItemInsert(carrier, linkedListPointer, 8);
expect(allocator.stats().available).toMatchInlineSnapshot(`24`);
expect(carrier.allocator.stats().available).toMatchInlineSnapshot(`24`);

@@ -84,4 +67,4 @@ expect(pointer1).toBe(64);

linkedListItemRemove({ dataView, allocator }, pointer2);
linkedListItemRemove({ dataView, allocator }, pointer1);
linkedListItemRemove(carrier, pointer2);
linkedListItemRemove(carrier, pointer1);

@@ -110,3 +93,3 @@ // fails on github action ??

expect(allocator.stats().available).toMatchInlineSnapshot(`56`);
expect(carrier.allocator.stats().available).toMatchInlineSnapshot(`56`);
});

@@ -116,11 +99,11 @@

setABSize(256);
const linkedListPointer = initLinkedList({ dataView, allocator });
const linkedListPointer = initLinkedList(carrier);
expect(allocator.stats().available).toMatchInlineSnapshot(`184`);
expect(carrier.allocator.stats().available).toMatchInlineSnapshot(`184`);
const itemsPointers = [
linkedListItemInsert({ dataView, allocator }, linkedListPointer, 7),
linkedListItemInsert({ dataView, allocator }, linkedListPointer, 6),
linkedListItemInsert({ dataView, allocator }, linkedListPointer, 5),
linkedListItemInsert({ dataView, allocator }, linkedListPointer, 4)
linkedListItemInsert(carrier, linkedListPointer, 7),
linkedListItemInsert(carrier, linkedListPointer, 6),
linkedListItemInsert(carrier, linkedListPointer, 5),
linkedListItemInsert(carrier, linkedListPointer, 4)
];

@@ -133,3 +116,3 @@

(iteratorPointer = linkedListLowLevelIterator(
dataView,
carrier.dataView,
linkedListPointer,

@@ -139,3 +122,3 @@ iteratorPointer

) {
values.push(linkedListGetValue(dataView, iteratorPointer));
values.push(linkedListGetValue(carrier.dataView, iteratorPointer));
}

@@ -152,9 +135,7 @@

expect(allocator.stats().available).toMatchInlineSnapshot(`120`);
expect(carrier.allocator.stats().available).toMatchInlineSnapshot(`120`);
itemsPointers.forEach(p =>
linkedListItemRemove({ dataView, allocator }, p)
);
itemsPointers.forEach(p => linkedListItemRemove(carrier, p));
expect(allocator.stats().available).toMatchInlineSnapshot(`184`);
expect(carrier.allocator.stats().available).toMatchInlineSnapshot(`184`);
});

@@ -164,11 +145,11 @@

setABSize(256);
const linkedListPointer = initLinkedList({ dataView, allocator });
const linkedListPointer = initLinkedList(carrier);
expect(allocator.stats().available).toMatchInlineSnapshot(`184`);
expect(carrier.allocator.stats().available).toMatchInlineSnapshot(`184`);
const itemsPointers = [
linkedListItemInsert({ dataView, allocator }, linkedListPointer, 7),
linkedListItemInsert({ dataView, allocator }, linkedListPointer, 6),
linkedListItemInsert({ dataView, allocator }, linkedListPointer, 5),
linkedListItemInsert({ dataView, allocator }, linkedListPointer, 4)
linkedListItemInsert(carrier, linkedListPointer, 7),
linkedListItemInsert(carrier, linkedListPointer, 6),
linkedListItemInsert(carrier, linkedListPointer, 5),
linkedListItemInsert(carrier, linkedListPointer, 4)
];

@@ -181,3 +162,3 @@

(iteratorPointer = linkedListLowLevelIterator(
dataView,
carrier.dataView,
linkedListPointer,

@@ -187,4 +168,4 @@ iteratorPointer

) {
values.push(linkedListGetValue(dataView, iteratorPointer));
linkedListItemRemove({ dataView, allocator }, iteratorPointer);
values.push(linkedListGetValue(carrier.dataView, iteratorPointer));
linkedListItemRemove(carrier, iteratorPointer);
}

@@ -207,9 +188,7 @@

expect(allocator.stats().available).toMatchInlineSnapshot(`152`);
expect(carrier.allocator.stats().available).toMatchInlineSnapshot(`152`);
itemsPointers.forEach(p =>
linkedListItemRemove({ dataView, allocator }, p)
);
itemsPointers.forEach(p => linkedListItemRemove(carrier, p));
expect(allocator.stats().available).toMatchInlineSnapshot(`184`);
expect(carrier.allocator.stats().available).toMatchInlineSnapshot(`184`);
});

@@ -219,12 +198,12 @@

setABSize(256);
const linkedListPointer = initLinkedList({ dataView, allocator });
const linkedListPointer = initLinkedList(carrier);
expect(allocator.stats().available).toMatchInlineSnapshot(`184`);
expect(carrier.allocator.stats().available).toMatchInlineSnapshot(`184`);
const regularSet = new Set([7, 6, 5, 4]);
const itemsPointers = [
linkedListItemInsert({ dataView, allocator }, linkedListPointer, 7),
linkedListItemInsert({ dataView, allocator }, linkedListPointer, 6),
linkedListItemInsert({ dataView, allocator }, linkedListPointer, 5),
linkedListItemInsert({ dataView, allocator }, linkedListPointer, 4)
linkedListItemInsert(carrier, linkedListPointer, 7),
linkedListItemInsert(carrier, linkedListPointer, 6),
linkedListItemInsert(carrier, linkedListPointer, 5),
linkedListItemInsert(carrier, linkedListPointer, 4)
];

@@ -237,3 +216,3 @@

(iteratorPointer = linkedListLowLevelIterator(
dataView,
carrier.dataView,
linkedListPointer,

@@ -243,4 +222,6 @@ iteratorPointer

) {
linkedLintResults.push(linkedListGetValue(dataView, iteratorPointer));
linkedListItemRemove({ dataView, allocator }, iteratorPointer);
linkedLintResults.push(
linkedListGetValue(carrier.dataView, iteratorPointer)
);
linkedListItemRemove(carrier, iteratorPointer);
}

@@ -270,9 +251,7 @@

expect(regularSetResults).toEqual(linkedLintResults);
expect(allocator.stats().available).toMatchInlineSnapshot(`152`);
expect(carrier.allocator.stats().available).toMatchInlineSnapshot(`152`);
itemsPointers.forEach(p =>
linkedListItemRemove({ dataView, allocator }, p)
);
itemsPointers.forEach(p => linkedListItemRemove(carrier, p));
expect(allocator.stats().available).toMatchInlineSnapshot(`184`);
expect(carrier.allocator.stats().available).toMatchInlineSnapshot(`184`);
});

@@ -282,9 +261,9 @@

setABSize(256);
const linkedListPointer = initLinkedList({ dataView, allocator });
const linkedListPointer = initLinkedList(carrier);
expect(allocator.stats().available).toMatchInlineSnapshot(`184`);
expect(carrier.allocator.stats().available).toMatchInlineSnapshot(`184`);
const itemsPointers = [
linkedListItemInsert({ dataView, allocator }, linkedListPointer, 7),
linkedListItemInsert({ dataView, allocator }, linkedListPointer, 6)
linkedListItemInsert(carrier, linkedListPointer, 7),
linkedListItemInsert(carrier, linkedListPointer, 6)
];

@@ -300,3 +279,3 @@

(iteratorPointer = linkedListLowLevelIterator(
dataView,
carrier.dataView,
linkedListPointer,

@@ -306,9 +285,5 @@ iteratorPointer

) {
values.push(linkedListGetValue(dataView, iteratorPointer));
values.push(linkedListGetValue(carrier.dataView, iteratorPointer));
if (c < toAdd.length) {
linkedListItemInsert(
{ dataView, allocator },
linkedListPointer,
toAdd[c++]
);
linkedListItemInsert(carrier, linkedListPointer, toAdd[c++]);
}

@@ -338,7 +313,5 @@ }

expect(allocator.stats().available).toMatchInlineSnapshot(`120`);
expect(carrier.allocator.stats().available).toMatchInlineSnapshot(`120`);
itemsPointers.forEach(p =>
linkedListItemRemove({ dataView, allocator }, p)
);
itemsPointers.forEach(p => linkedListItemRemove(carrier, p));

@@ -348,3 +321,3 @@ const iteratorPointerToFree = 0;

(iteratorPointer = linkedListLowLevelIterator(
dataView,
carrier.dataView,
linkedListPointer,

@@ -354,6 +327,6 @@ iteratorPointerToFree

) {
linkedListItemRemove({ dataView, allocator }, iteratorPointer);
linkedListItemRemove(carrier, iteratorPointer);
}
expect(allocator.stats().available).toMatchInlineSnapshot(`184`);
expect(carrier.allocator.stats().available).toMatchInlineSnapshot(`184`);
});

@@ -368,5 +341,5 @@

const { allocations } = recordAllocations(() => {
linkedListPointer = initLinkedList({ dataView, allocator });
linkedListPointer = initLinkedList(carrier);
expect(allocator.stats().available).toMatchInlineSnapshot(`64`);
expect(carrier.allocator.stats().available).toMatchInlineSnapshot(`64`);

@@ -376,7 +349,7 @@ let toAdd: undefined | number = 0;

while ((toAdd = toAddItemsCopy.pop()) !== undefined) {
linkedListItemInsert({ dataView, allocator }, linkedListPointer, toAdd);
linkedListItemInsert(carrier, linkedListPointer, toAdd);
}
}, allocator);
}, carrier.allocator);
const r = linkedListGetPointersToFree(dataView, linkedListPointer);
const r = linkedListGetPointersToFree(carrier.dataView, linkedListPointer);

@@ -383,0 +356,0 @@ expect(r).toMatchInlineSnapshot(`

@@ -10,3 +10,4 @@ import {

objectGet,
objectSet
objectSet,
mapOrSetClear
} from "./objectWrapperHelpers";

@@ -30,6 +31,3 @@

clear(): void {
// @todo impl using helper function with direct list access
for (const key of [...this.keys()]) {
this.delete(key);
}
mapOrSetClear(this.externalArgs, this.carrier, this.entryPointer);
}

@@ -153,10 +151,3 @@

return (
hashMapNodeLookup(
this.externalArgs,
this.carrier.dataView,
this.entry.value,
p
) !== 0
);
return hashMapNodeLookup(this.carrier, this.entry.value, p) !== 0;
}

@@ -163,0 +154,0 @@

@@ -7,5 +7,4 @@ /* eslint-env jest */

import { arraySaver } from "./arraySaver";
import { MemPool } from "@thi.ng/malloc";
import { MEM_POOL_START } from "./consts";
import { externalArgsApiToExternalArgsApi } from "./utils";
import { makeCarrier } from "./testUtils";

@@ -21,8 +20,4 @@ describe("pop it all", () => {

const arrayBuffer = new ArrayBuffer(512);
const dataView = new DataView(arrayBuffer);
initializeArrayBuffer(arrayBuffer);
const allocator = new MemPool({
buf: arrayBuffer,
start: MEM_POOL_START
});
const carrier = makeCarrier(arrayBuffer);

@@ -32,14 +27,5 @@ const arrayToSave = ["a", "b", -100];

const saverOutput = arraySaver(
externalArgs,
{ dataView, allocator },
[],
arrayToSave
);
const saverOutput = arraySaver(externalArgs, carrier, [], arrayToSave);
const arrayWrapper = createArrayWrapper(
externalArgs,
{ dataView, allocator },
saverOutput
);
const arrayWrapper = createArrayWrapper(externalArgs, carrier, saverOutput);

@@ -51,3 +37,3 @@ const sizeAfterEachPush: number[] = [];

arrayToCompare.push(i);
sizeAfterEachPush.push(allocator.stats().available);
sizeAfterEachPush.push(carrier.allocator.stats().available);
}

@@ -97,4 +83,4 @@

expect(arrayToCompare).toEqual(arrayWrapper);
expect(allocator.stats().available).toMatchInlineSnapshot(`48`);
expect(carrier.allocator.stats().available).toMatchInlineSnapshot(`48`);
});
});

@@ -5,6 +5,4 @@ /* eslint-env jest */

import * as util from "util";
import { arrayBuffer2HexArray } from "./testUtils";
import { arrayBuffer2HexArray, makeCarrier } from "./testUtils";
import { objectSaver } from "./objectSaver";
import { MemPool } from "@thi.ng/malloc";
import { MEM_POOL_START } from "./consts";
import { externalArgsApiToExternalArgsApi } from "./utils";

@@ -21,8 +19,4 @@

const arrayBuffer = new ArrayBuffer(1024);
const dataView = new DataView(arrayBuffer);
initializeArrayBuffer(arrayBuffer);
const allocator = new MemPool({
buf: arrayBuffer,
start: MEM_POOL_START
});
const carrier = makeCarrier(arrayBuffer);

@@ -33,2 +27,4 @@ const objectToSave = {

6: null,
10: true,
11: false,
ab: undefined,

@@ -41,15 +37,10 @@ abc: BigInt("100"),

const saverOutput = objectSaver(
externalArgs,
{ dataView, allocator },
[],
objectToSave
);
const saverOutput = objectSaver(externalArgs, carrier, [], objectToSave);
expect(saverOutput).toMatchInlineSnapshot(`800`);
expect(saverOutput).toMatchInlineSnapshot(`896`);
expect(arrayBuffer2HexArray(arrayBuffer, true)).toMatchSnapshot();
expect(allocator.stats().available).toMatchInlineSnapshot(`216`);
expect(carrier.allocator.stats().available).toMatchInlineSnapshot(`120`);
});
});
});

@@ -7,5 +7,4 @@ /* eslint-env jest */

import { createObjectWrapper } from "./objectWrapper";
import { MEM_POOL_START } from "./consts";
import { MemPool } from "@thi.ng/malloc";
import { externalArgsApiToExternalArgsApi } from "./utils";
import { makeCarrier } from "./testUtils";

@@ -21,8 +20,4 @@ describe("objectWrapper tests", () => {

const arrayBuffer = new ArrayBuffer(1024);
const dataView = new DataView(arrayBuffer);
initializeArrayBuffer(arrayBuffer);
const allocator = new MemPool({
buf: arrayBuffer,
start: MEM_POOL_START
});
const carrier = makeCarrier(arrayBuffer);

@@ -39,12 +34,7 @@ const objectToSave = {

const saverOutput = objectSaver(
externalArgs,
{ dataView, allocator },
[],
objectToSave
);
const saverOutput = objectSaver(externalArgs, carrier, [], objectToSave);
const objectWrapper: any = createObjectWrapper(
externalArgs,
{ dataView, allocator },
carrier,
saverOutput

@@ -69,3 +59,3 @@ );

expect(allocator.stats().top).toMatchInlineSnapshot(`736`);
expect(carrier.allocator.stats().top).toMatchInlineSnapshot(`704`);
});

@@ -75,9 +65,6 @@

const arrayBuffer = new ArrayBuffer(1024);
const dataView = new DataView(arrayBuffer);
initializeArrayBuffer(arrayBuffer);
const allocator = new MemPool({
buf: arrayBuffer,
start: MEM_POOL_START
});
const carrier = makeCarrier(arrayBuffer);
const objectToSave = {

@@ -93,12 +80,7 @@ a: 6,

const saverOutput = objectSaver(
externalArgs,
{ dataView, allocator },
[],
objectToSave
);
const saverOutput = objectSaver(externalArgs, carrier, [], objectToSave);
const objectWrapper: any = createObjectWrapper(
externalArgs,
{ dataView, allocator },
carrier,
saverOutput

@@ -110,3 +92,3 @@ );

expect(allocator.stats().top).toMatchInlineSnapshot(`736`);
expect(carrier.allocator.stats().top).toMatchInlineSnapshot(`704`);
});

@@ -116,9 +98,6 @@

const arrayBuffer = new ArrayBuffer(1024);
const dataView = new DataView(arrayBuffer);
initializeArrayBuffer(arrayBuffer);
const allocator = new MemPool({
buf: arrayBuffer,
start: MEM_POOL_START
});
const carrier = makeCarrier(arrayBuffer);
const objectToSave = {

@@ -134,12 +113,7 @@ a: 6,

const saverOutput = objectSaver(
externalArgs,
{ dataView, allocator },
[],
objectToSave
);
const saverOutput = objectSaver(externalArgs, carrier, [], objectToSave);
const objectWrapper = createObjectWrapper(
externalArgs,
{ dataView, allocator },
carrier,
saverOutput

@@ -162,3 +136,3 @@ );

expect(allocator.stats().top).toMatchInlineSnapshot(`736`);
expect(carrier.allocator.stats().top).toMatchInlineSnapshot(`728`);
});

@@ -168,9 +142,6 @@

const arrayBuffer = new ArrayBuffer(1024);
const dataView = new DataView(arrayBuffer);
initializeArrayBuffer(arrayBuffer);
const allocator = new MemPool({
buf: arrayBuffer,
start: MEM_POOL_START
});
const carrier = makeCarrier(arrayBuffer);
const objectToSave = {

@@ -186,12 +157,7 @@ a: 6,

const saverOutput = objectSaver(
externalArgs,
{ dataView, allocator },
[],
objectToSave
);
const saverOutput = objectSaver(externalArgs, carrier, [], objectToSave);
const objectWrapper = createObjectWrapper(
externalArgs,
{ dataView, allocator },
carrier,
saverOutput

@@ -215,3 +181,3 @@ );

expect(allocator.stats().top).toMatchInlineSnapshot(`832`);
expect(carrier.allocator.stats().top).toMatchInlineSnapshot(`800`);
});

@@ -221,9 +187,6 @@

const arrayBuffer = new ArrayBuffer(1024);
const dataView = new DataView(arrayBuffer);
initializeArrayBuffer(arrayBuffer);
const allocator = new MemPool({
buf: arrayBuffer,
start: MEM_POOL_START
});
const carrier = makeCarrier(arrayBuffer);
const objectToSave = {

@@ -239,12 +202,7 @@ a: 6,

const saverOutput = objectSaver(
externalArgs,
{ dataView, allocator },
[],
objectToSave
);
const saverOutput = objectSaver(externalArgs, carrier, [], objectToSave);
const objectWrapper = createObjectWrapper(
externalArgs,
{ dataView, allocator },
carrier,
saverOutput

@@ -266,5 +224,5 @@ );

expect(allocator.stats().top).toMatchInlineSnapshot(`736`);
expect(carrier.allocator.stats().top).toMatchInlineSnapshot(`704`);
});
});
});

@@ -51,4 +51,3 @@ import {

const gotEntries = getObjectPropertiesEntries(
this.externalArgs,
this.carrier.dataView,
this.carrier,
this.entry.value

@@ -62,4 +61,3 @@ );

const gotEntries = getObjectPropertiesEntries(
this.externalArgs,
this.carrier.dataView,
this.carrier,
this.entry.value

@@ -88,10 +86,3 @@ );

return (
hashMapNodeLookup(
this.externalArgs,
this.carrier.dataView,
this.entry.value,
p
) !== 0
);
return hashMapNodeLookup(this.carrier, this.entry.value, p) !== 0;
}

@@ -98,0 +89,0 @@

@@ -10,6 +10,5 @@ /* eslint-env jest */

} from "./objectWrapperHelpers";
import { MemPool } from "@thi.ng/malloc";
import { MEM_POOL_START } from "./consts";
import { externalArgsApiToExternalArgsApi } from "./utils";
import { ObjectEntry } from "./interfaces";
import { makeCarrier } from "./testUtils";

@@ -26,8 +25,3 @@ describe("objectWrapperHelpers tests", () => {

const arrayBuffer = new ArrayBuffer(1024);
const dataView = new DataView(arrayBuffer);
initializeArrayBuffer(arrayBuffer);
const allocator = new MemPool({
buf: arrayBuffer,
start: MEM_POOL_START
});
const carrier = makeCarrier(arrayBuffer);

@@ -44,20 +38,8 @@ const objectToSave = {

const saverOutput = objectSaver(
externalArgs,
{ dataView, allocator },
[],
objectToSave
);
const saverOutput = objectSaver(externalArgs, carrier, [], objectToSave);
const hashmapPointer = (readEntry(
externalArgs,
dataView,
saverOutput
) as ObjectEntry).value;
const hashmapPointer = (readEntry(carrier, saverOutput) as ObjectEntry)
.value;
const gotEntries = getObjectPropertiesEntries(
externalArgs,
dataView,
hashmapPointer
);
const gotEntries = getObjectPropertiesEntries(carrier, hashmapPointer);

@@ -68,19 +50,19 @@ expect(gotEntries).toMatchInlineSnapshot(`

"key": "5",
"valuePointer": 200,
"valuePointer": 0,
},
Object {
"key": "a",
"valuePointer": 272,
"valuePointer": 256,
},
Object {
"key": "kawabanga",
"valuePointer": 360,
"valuePointer": 1,
},
Object {
"key": "b",
"valuePointer": 432,
"valuePointer": 400,
},
Object {
"key": "nestedObject",
"valuePointer": 712,
"valuePointer": 680,
},

@@ -92,9 +74,6 @@ ]

const arrayBuffer = new ArrayBuffer(512);
const dataView = new DataView(arrayBuffer);
initializeArrayBuffer(arrayBuffer);
const allocator = new MemPool({
buf: arrayBuffer,
start: MEM_POOL_START
});
const carrier = makeCarrier(arrayBuffer);
const objectToSave = {

@@ -107,20 +86,12 @@ a: 6,

const saverOutput = objectSaver(
externalArgs,
{ dataView, allocator },
[],
objectToSave
);
const saverOutput = objectSaver(externalArgs, carrier, [], objectToSave);
expect(allocator.stats().available).toMatchInlineSnapshot(`56`);
expect(carrier.allocator.stats().available).toMatchInlineSnapshot(`88`);
const hashmapPointer = (readEntry(
externalArgs,
dataView,
saverOutput
) as ObjectEntry).value;
const hashmapPointer = (readEntry(carrier, saverOutput) as ObjectEntry)
.value;
deleteObjectPropertyEntryByKey(
externalArgs,
{ dataView, allocator },
carrier,
hashmapPointer,

@@ -130,9 +101,5 @@ "a"

expect(allocator.stats().available).toMatchInlineSnapshot(`136`);
expect(carrier.allocator.stats().available).toMatchInlineSnapshot(`168`);
const gotEntries = getObjectPropertiesEntries(
externalArgs,
dataView,
hashmapPointer
);
const gotEntries = getObjectPropertiesEntries(carrier, hashmapPointer);

@@ -147,7 +114,7 @@ expect(gotEntries).toMatchInlineSnapshot(`

"key": "c",
"valuePointer": 360,
"valuePointer": 0,
},
Object {
"key": "d",
"valuePointer": 432,
"valuePointer": 1,
},

@@ -160,9 +127,6 @@ ]

const arrayBuffer = new ArrayBuffer(512);
const dataView = new DataView(arrayBuffer);
initializeArrayBuffer(arrayBuffer);
const allocator = new MemPool({
buf: arrayBuffer,
start: MEM_POOL_START
});
const carrier = makeCarrier(arrayBuffer);
const objectToSave = {

@@ -175,20 +139,12 @@ a: 6,

const saverOutput = objectSaver(
externalArgs,
{ dataView, allocator },
[],
objectToSave
);
const saverOutput = objectSaver(externalArgs, carrier, [], objectToSave);
expect(allocator.stats().available).toMatchInlineSnapshot(`56`);
expect(carrier.allocator.stats().available).toMatchInlineSnapshot(`88`);
const hashmapPointer = (readEntry(
externalArgs,
dataView,
saverOutput
) as ObjectEntry).value;
const hashmapPointer = (readEntry(carrier, saverOutput) as ObjectEntry)
.value;
deleteObjectPropertyEntryByKey(
externalArgs,
{ dataView, allocator },
carrier,
hashmapPointer,

@@ -198,3 +154,3 @@ "d"

expect(getObjectPropertiesEntries(externalArgs, dataView, hashmapPointer))
expect(getObjectPropertiesEntries(carrier, hashmapPointer))
.toMatchInlineSnapshot(`

@@ -212,3 +168,3 @@ Array [

"key": "c",
"valuePointer": 360,
"valuePointer": 0,
},

@@ -218,3 +174,3 @@ ]

expect(allocator.stats().available).toMatchInlineSnapshot(`128`);
expect(carrier.allocator.stats().available).toMatchInlineSnapshot(`144`);
});

@@ -224,8 +180,4 @@

const arrayBuffer = new ArrayBuffer(512);
const dataView = new DataView(arrayBuffer);
initializeArrayBuffer(arrayBuffer);
const allocator = new MemPool({
buf: arrayBuffer,
start: MEM_POOL_START
});
const carrier = makeCarrier(arrayBuffer);

@@ -240,19 +192,10 @@ const objectToSave = {

const saverOutput = objectSaver(
externalArgs,
{ dataView, allocator },
[],
objectToSave
);
expect(allocator.stats().available).toMatchInlineSnapshot(`0`);
const saverOutput = objectSaver(externalArgs, carrier, [], objectToSave);
expect(carrier.allocator.stats().available).toMatchInlineSnapshot(`8`);
const hashmapPointer = readEntry(
externalArgs,
dataView,
saverOutput
) as ObjectEntry;
const hashmapPointer = readEntry(carrier, saverOutput) as ObjectEntry;
deleteObjectPropertyEntryByKey(
externalArgs,
{ dataView, allocator },
carrier,
hashmapPointer.value,

@@ -262,5 +205,4 @@ "c"

expect(
getObjectPropertiesEntries(externalArgs, dataView, hashmapPointer.value)
).toMatchInlineSnapshot(`
expect(getObjectPropertiesEntries(carrier, hashmapPointer.value))
.toMatchInlineSnapshot(`
Array [

@@ -277,7 +219,7 @@ Object {

"key": "d",
"valuePointer": 432,
"valuePointer": 1,
},
Object {
"key": "e",
"valuePointer": 0,
"valuePointer": 472,
},

@@ -287,5 +229,5 @@ ]

expect(allocator.stats().available).toMatchInlineSnapshot(`72`);
expect(carrier.allocator.stats().available).toMatchInlineSnapshot(`64`);
});
});
});

@@ -5,8 +5,12 @@ import {

StringEntry,
NumberEntry
NumberEntry,
MapEntry,
SetEntry
} from "./interfaces";
import {
readEntry,
writeValueInPtrToPtrAndHandleMemory,
handleArcForDeletedValuePointer,
decrementRefCount,
writeValueInPtrToPtrAndHandleMemory
writeEntry
} from "./store";

@@ -19,9 +23,10 @@ import { entryToFinalJavaScriptValue } from "./entryToFinalJavaScriptValue";

hashMapInsertUpdate,
hashMapValueLookup
hashMapValueLookup,
createHashMap
} from "./hashmap/hashmap";
import { getAllLinkedAddresses } from "./getAllLinkedAddresses";
import { getObjectOrMapOrSetAddresses } from "./getAllLinkedAddresses";
export function deleteObjectPropertyEntryByKey(
externalArgs: ExternalArgs,
{ dataView, allocator }: DataViewAndAllocatorCarrier,
carrier: DataViewAndAllocatorCarrier,
hashmapPointer: number,

@@ -31,4 +36,3 @@ keyToDeleteBy: string | number

const deletedValuePointerToPointer = hashMapDelete(
externalArgs,
{ dataView, allocator },
carrier,
hashmapPointer,

@@ -38,40 +42,13 @@ keyToDeleteBy

const deletedValuePointer = dataView.getUint32(deletedValuePointerToPointer);
// Nothing to delete here
if (deletedValuePointer === 0) {
// no such key
if (deletedValuePointerToPointer === 0) {
return false;
}
// handle memory free
if (deletedValuePointer !== 0) {
const existingValueEntry = readEntry(
externalArgs,
dataView,
deletedValuePointer
);
if (existingValueEntry && "refsCount" in existingValueEntry) {
const newRefCount = decrementRefCount(
externalArgs,
dataView,
deletedValuePointer
);
const deletedValuePointer = carrier.dataView.getUint32(
deletedValuePointerToPointer
);
if (newRefCount === 0) {
const addressesToFree = getAllLinkedAddresses(
externalArgs,
dataView,
false,
deletedValuePointer
);
handleArcForDeletedValuePointer(externalArgs, carrier, deletedValuePointer);
for (const address of addressesToFree) {
allocator.free(address);
}
}
} else {
allocator.free(deletedValuePointer);
}
}
return true;

@@ -81,4 +58,3 @@ }

export function getObjectPropertiesEntries(
externalArgs: ExternalArgs,
dataView: DataView,
carrier: DataViewAndAllocatorCarrier,
hashmapPointer: number

@@ -90,10 +66,14 @@ ): Array<{ key: string | number; valuePointer: number }> {

while (
(iterator = hashMapLowLevelIterator(dataView, hashmapPointer, iterator))
(iterator = hashMapLowLevelIterator(
carrier.dataView,
hashmapPointer,
iterator
))
) {
const { valuePointer, keyPointer } = hashMapNodePointerToKeyValue(
dataView,
carrier.dataView,
iterator
);
const keyEntry = readEntry(externalArgs, dataView, keyPointer) as
const keyEntry = readEntry(carrier, keyPointer) as
| StringEntry

@@ -103,3 +83,3 @@ | NumberEntry;

foundValues.push({
valuePointer: dataView.getUint32(valuePointer),
valuePointer: carrier.dataView.getUint32(valuePointer),
key: keyEntry.value

@@ -135,8 +115,3 @@ });

) {
const valuePointer = hashMapValueLookup(
externalArgs,
carrier.dataView,
entryPointer,
key
);
const valuePointer = hashMapValueLookup(carrier, entryPointer, key);

@@ -153,1 +128,40 @@ if (valuePointer === 0) {

}
export function hashmapClearFree(
externalArgs: ExternalArgs,
carrier: DataViewAndAllocatorCarrier,
hashmapPointer: number
) {
const leafAddresses: number[] = [];
const arcAddresses: number[] = [];
getObjectOrMapOrSetAddresses(
carrier,
false,
hashmapPointer,
leafAddresses,
arcAddresses
);
for (const address of leafAddresses) {
carrier.allocator.free(address);
}
for (const address of arcAddresses) {
decrementRefCount(externalArgs, carrier, address);
}
}
export function mapOrSetClear(
externalArgs: ExternalArgs,
carrier: DataViewAndAllocatorCarrier,
mapOrSetPtr: number
) {
const entry = readEntry(carrier, mapOrSetPtr) as MapEntry | SetEntry;
hashmapClearFree(externalArgs, carrier, entry.value);
entry.value = createHashMap(carrier, externalArgs.hashMapMinInitialCapacity);
writeEntry(carrier, mapOrSetPtr, entry);
}

@@ -11,3 +11,12 @@ import {

import { ENTRY_TYPE } from "./entry-types";
import {
UNDEFINED_KNOWN_ADDRESS,
NULL_KNOWN_ADDRESS,
TRUE_KNOWN_ADDRESS,
FALSE_KNOWN_ADDRESS
} from "./consts";
/**
* Returns pointer for the value
*/
export function saveValue(

@@ -22,8 +31,20 @@ externalArgs: ExternalArgs,

if (value === undefined) {
return UNDEFINED_KNOWN_ADDRESS;
}
if (value === null) {
return NULL_KNOWN_ADDRESS;
}
if (value === true) {
return TRUE_KNOWN_ADDRESS;
}
if (value === false) {
return FALSE_KNOWN_ADDRESS;
}
if (isPrimitive(value)) {
const entry = primitiveValueToEntry(
externalArgs,
value,
externalArgs.minimumStringAllocation
);
const entry = primitiveValueToEntry(value);
valuePointer = appendEntry(externalArgs, carrier, entry);

@@ -30,0 +51,0 @@ } else if (

@@ -9,3 +9,4 @@ import {

deleteObjectPropertyEntryByKey,
objectSet
objectSet,
mapOrSetClear
} from "./objectWrapperHelpers";

@@ -29,6 +30,3 @@

clear(): void {
// @todo impl using helper function with direct list access
for (const key of [...this.keys()]) {
this.delete(key);
}
mapOrSetClear(this.externalArgs, this.carrier, this.entryPointer);
}

@@ -125,10 +123,3 @@

return (
hashMapNodeLookup(
this.externalArgs,
this.carrier.dataView,
this.entry.value,
p
) !== 0
);
return hashMapNodeLookup(this.carrier, this.entry.value, p) !== 0;
}

@@ -135,0 +126,0 @@

@@ -37,7 +37,7 @@ /* eslint-env jest */

"calcedSize": 64,
"realSize": 48,
"realSize": 64,
}
`);
// ???
expect(r.calcedSize).toBe(r.realSize + 16);
expect(r.calcedSize).toBe(r.realSize);
});

@@ -54,7 +54,7 @@

test("undefined", () => {
expect(sizeOf(externalArgs, undefined)).toMatchInlineSnapshot(`16`);
expect(sizeOf(externalArgs, undefined)).toMatchInlineSnapshot(`0`);
});
test("null", () => {
expect(sizeOf(externalArgs, null)).toMatchInlineSnapshot(`16`);
expect(sizeOf(externalArgs, null)).toMatchInlineSnapshot(`0`);
});

@@ -65,3 +65,3 @@

sizeOf(externalArgs, ["a", { a: "u" }, null, undefined, 1])
).toMatchInlineSnapshot(`388`);
).toMatchInlineSnapshot(`356`);
});

@@ -68,0 +68,0 @@

@@ -121,8 +121,16 @@ import {

): CheckerResult {
if (
value === undefined ||
value === null ||
value === true ||
value === false
) {
return {
memoryAllocated: 0,
numberOfAllocations: 0
};
}
if (isPrimitive(value)) {
const entry = primitiveValueToEntry(
externalArgs,
value,
externalArgs.minimumStringAllocation
);
const entry = primitiveValueToEntry(value);

@@ -180,3 +188,3 @@ return {

const hashMapKeysSize = keysArray
.map(k => sizeOfEntry(primitiveValueToEntry(externalArgs, k, 0)))
.map(k => sizeOfEntry(primitiveValueToEntry(k)))
.reduce((p, c) => {

@@ -183,0 +191,0 @@ return p + align(c);

@@ -11,6 +11,4 @@ /* eslint-env jest */

import * as util from "util";
import { arrayBuffer2HexArray } from "./testUtils";
import { ObjectEntry, ExternalArgs } from "./interfaces";
import { MemPool } from "@thi.ng/malloc";
import { MEM_POOL_START } from "./consts";
import { arrayBuffer2HexArray, makeCarrier } from "./testUtils";
import { ObjectEntry } from "./interfaces";
import { externalArgsApiToExternalArgsApi } from "./utils";

@@ -20,6 +18,7 @@

test("initializeArrayBuffer", () => {
const arrayBuffer = new ArrayBuffer(20);
const arrayBuffer = new ArrayBuffer(64);
initializeArrayBuffer(arrayBuffer);
expect(arrayBuffer2HexArray(arrayBuffer, true)).toMatchInlineSnapshot(`
expect(arrayBuffer2HexArray(arrayBuffer.slice(0, 20), true))
.toMatchInlineSnapshot(`
Array [

@@ -52,20 +51,7 @@ "0:0x00",

describe("Store tests writeEntry", () => {
const externalArgs = externalArgsApiToExternalArgsApi({
textEncoder: new util.TextEncoder(),
textDecoder: new util.TextDecoder(),
arrayAdditionalAllocation: 20
});
test("writeEntry max number", () => {
const externalArgs: ExternalArgs = externalArgsApiToExternalArgsApi({
textEncoder: new util.TextEncoder(),
textDecoder: new util.TextDecoder(),
arrayAdditionalAllocation: 0,
minimumStringAllocation: 0
});
const arrayBuffer = new ArrayBuffer(64);
const carrier = makeCarrier(arrayBuffer);
const arrayBuffer = new ArrayBuffer(17);
const dataView = new DataView(arrayBuffer);
writeEntry(externalArgs, dataView, 8, {
writeEntry(carrier, 8, {
type: ENTRY_TYPE.NUMBER,

@@ -75,3 +61,4 @@ value: Number.MAX_VALUE

expect(arrayBuffer2HexArray(arrayBuffer)).toMatchInlineSnapshot(`
expect(arrayBuffer2HexArray(arrayBuffer.slice(0, 17)))
.toMatchInlineSnapshot(`
Array [

@@ -99,6 +86,6 @@ "0x00",

test("writeEntry min number", () => {
const arrayBuffer = new ArrayBuffer(17);
const dataView = new DataView(arrayBuffer);
const arrayBuffer = new ArrayBuffer(64);
const carrier = makeCarrier(arrayBuffer);
writeEntry(externalArgs, dataView, 8, {
writeEntry(carrier, 8, {
type: ENTRY_TYPE.NUMBER,

@@ -108,3 +95,4 @@ value: Number.MIN_VALUE

expect(arrayBuffer2HexArray(arrayBuffer)).toMatchInlineSnapshot(`
expect(arrayBuffer2HexArray(arrayBuffer.slice(0, 17)))
.toMatchInlineSnapshot(`
Array [

@@ -133,12 +121,13 @@ "0x00",

test("writeEntry string", () => {
const arrayBuffer = new ArrayBuffer(19);
const dataView = new DataView(arrayBuffer);
const arrayBuffer = new ArrayBuffer(64);
const carrier = makeCarrier(arrayBuffer);
writeEntry(externalArgs, dataView, 8, {
writeEntry(carrier, 8, {
type: ENTRY_TYPE.STRING,
value: "aא弟",
allocatedBytes: 0
allocatedBytes: 6
});
expect(arrayBuffer2HexArray(arrayBuffer)).toMatchInlineSnapshot(`
expect(arrayBuffer2HexArray(arrayBuffer.slice(0, 19)))
.toMatchInlineSnapshot(`
Array [

@@ -157,3 +146,3 @@ "0x00",

"0x00",
"0x00",
"0x06",
"0x61",

@@ -178,6 +167,6 @@ "0xd7",

test("readEntry max number", () => {
const arrayBuffer = new ArrayBuffer(17);
const dataView = new DataView(arrayBuffer);
const arrayBuffer = new ArrayBuffer(64);
const carrier = makeCarrier(arrayBuffer);
writeEntry(externalArgs, dataView, 8, {
writeEntry(carrier, 8, {
type: ENTRY_TYPE.NUMBER,

@@ -187,3 +176,3 @@ value: Number.MAX_VALUE

const redEntry = readEntry(externalArgs, dataView, 8);
const redEntry = readEntry(carrier, 8);

@@ -201,6 +190,6 @@ // expect(redBytesLength).toBe(writtenLength);

test("readEntry min number", () => {
const arrayBuffer = new ArrayBuffer(17);
const dataView = new DataView(arrayBuffer);
const arrayBuffer = new ArrayBuffer(64);
const carrier = makeCarrier(arrayBuffer);
writeEntry(externalArgs, dataView, 8, {
writeEntry(carrier, 8, {
type: ENTRY_TYPE.NUMBER,

@@ -210,3 +199,3 @@ value: Number.MIN_VALUE

const redEntry = readEntry(externalArgs, dataView, 8);
const redEntry = readEntry(carrier, 8);

@@ -222,16 +211,16 @@ expect(redEntry).toMatchInlineSnapshot(`

test("readEntry string", () => {
const arrayBuffer = new ArrayBuffer(12);
const dataView = new DataView(arrayBuffer);
const arrayBuffer = new ArrayBuffer(64);
const carrier = makeCarrier(arrayBuffer);
writeEntry(externalArgs, dataView, 0, {
writeEntry(carrier, 0, {
type: ENTRY_TYPE.STRING,
value: "aא弟",
allocatedBytes: 0
allocatedBytes: 6
});
const entry = readEntry(externalArgs, dataView, 0);
const entry = readEntry(carrier, 0);
expect(entry).toMatchInlineSnapshot(`
Object {
"allocatedBytes": 0,
"allocatedBytes": 6,
"type": 5,

@@ -244,6 +233,6 @@ "value": "aא弟",

test("readEntry BigInt", () => {
const arrayBuffer = new ArrayBuffer(20);
const dataView = new DataView(arrayBuffer);
const arrayBuffer = new ArrayBuffer(64);
const carrier = makeCarrier(arrayBuffer);
writeEntry(externalArgs, dataView, 0, {
writeEntry(carrier, 0, {
type: ENTRY_TYPE.BIGINT_POSITIVE,

@@ -253,3 +242,3 @@ value: BigInt("0b0" + "1".repeat(63))

const entry = readEntry(externalArgs, dataView, 0);
const entry = readEntry(carrier, 0);

@@ -265,6 +254,6 @@ expect(entry).toMatchInlineSnapshot(`

test("readEntry UBigInt", () => {
const arrayBuffer = new ArrayBuffer(16);
const dataView = new DataView(arrayBuffer);
const arrayBuffer = new ArrayBuffer(64);
const carrier = makeCarrier(arrayBuffer);
writeEntry(externalArgs, dataView, 0, {
writeEntry(carrier, 0, {
type: ENTRY_TYPE.BIGINT_POSITIVE,

@@ -274,3 +263,3 @@ value: BigInt("0b" + "1".repeat(64))

const entry = readEntry(externalArgs, dataView, 0);
const entry = readEntry(carrier, 0);

@@ -285,6 +274,6 @@ expect(entry).toMatchInlineSnapshot(`

test("ENTRY_TYPE.BIGINT_POSITIVE max value", () => {
const arrayBuffer = new ArrayBuffer(16);
const dataView = new DataView(arrayBuffer);
const arrayBuffer = new ArrayBuffer(64);
const carrier = makeCarrier(arrayBuffer);
writeEntry(externalArgs, dataView, 0, {
writeEntry(carrier, 0, {
type: ENTRY_TYPE.BIGINT_POSITIVE,

@@ -294,3 +283,3 @@ value: BigInt("0b" + "1".repeat(64))

expect(readEntry(externalArgs, dataView, 0)).toMatchInlineSnapshot(`
expect(readEntry(carrier, 0)).toMatchInlineSnapshot(`
Object {

@@ -304,6 +293,6 @@ "type": 3,

test("ENTRY_TYPE.BIGINT_NEGATIVE min value", () => {
const arrayBuffer = new ArrayBuffer(16);
const dataView = new DataView(arrayBuffer);
const arrayBuffer = new ArrayBuffer(64);
const carrier = makeCarrier(arrayBuffer);
writeEntry(externalArgs, dataView, 0, {
writeEntry(carrier, 0, {
type: ENTRY_TYPE.BIGINT_NEGATIVE,

@@ -313,3 +302,3 @@ value: -BigInt("0b" + "1".repeat(64))

expect(readEntry(externalArgs, dataView, 0)).toMatchInlineSnapshot(`
expect(readEntry(carrier, 0)).toMatchInlineSnapshot(`
Object {

@@ -323,7 +312,7 @@ "type": 4,

test("BigInt64 overflow error", () => {
const arrayBuffer = new ArrayBuffer(16);
const dataView = new DataView(arrayBuffer);
const arrayBuffer = new ArrayBuffer(64);
const carrier = makeCarrier(arrayBuffer);
expect(() => {
writeEntry(externalArgs, dataView, 0, {
writeEntry(carrier, 0, {
type: ENTRY_TYPE.BIGINT_POSITIVE,

@@ -337,4 +326,4 @@ value: BigInt("0b" + "1".repeat(65))

test("object entry", () => {
const arrayBuffer = new ArrayBuffer(8);
const dataView = new DataView(arrayBuffer);
const arrayBuffer = new ArrayBuffer(64);
const carrier = makeCarrier(arrayBuffer);

@@ -347,5 +336,5 @@ const entryToWrite: ObjectEntry = {

writeEntry(externalArgs, dataView, 0, entryToWrite);
writeEntry(carrier, 0, entryToWrite);
const entry = readEntry(externalArgs, dataView, 0);
const entry = readEntry(carrier, 0);

@@ -364,20 +353,13 @@ expect(entry).toMatchInlineSnapshot(`

test("appendEntry", () => {
const arrayBuffer = new ArrayBuffer(100);
const dataView = new DataView(arrayBuffer);
const arrayBuffer = new ArrayBuffer(96);
initializeArrayBuffer(arrayBuffer);
const allocator = new MemPool({
buf: arrayBuffer,
start: MEM_POOL_START
const carrier = makeCarrier(arrayBuffer);
const r1 = appendEntry(externalArgs, carrier, {
type: ENTRY_TYPE.STRING,
value: "im a string",
allocatedBytes: 11
});
const r1 = appendEntry(
externalArgs,
{ dataView, allocator },
{
type: ENTRY_TYPE.STRING,
value: "im a string",
allocatedBytes: 0
}
);
expect(r1).toMatchInlineSnapshot(`48`);

@@ -411,3 +393,3 @@

"23:0x00",
"24:0x64",
"24:0x60",
"25:0x00",

@@ -440,3 +422,3 @@ "26:0x00",

"51:0x00",
"52:0x00",
"52:0x0b",
"53:0x69",

@@ -485,19 +467,5 @@ "54:0x6d",

"95:0x00",
"96:0x00",
"97:0x00",
"98:0x00",
"99:0x00",
]
`);
const r2 = appendEntry(
externalArgs,
{ dataView, allocator },
{
type: ENTRY_TYPE.BOOLEAN,
value: true
}
);
expect(r2).toMatchInlineSnapshot(`72`);
expect(arrayBuffer2HexArray(arrayBuffer, true)).toMatchInlineSnapshot(`

@@ -521,11 +489,11 @@ Array [

"15:0x00",
"16:0x40",
"16:0x28",
"17:0x00",
"18:0x00",
"19:0x00",
"20:0x50",
"20:0x40",
"21:0x00",
"22:0x00",
"23:0x00",
"24:0x64",
"24:0x60",
"25:0x00",

@@ -558,3 +526,3 @@ "26:0x00",

"51:0x00",
"52:0x00",
"52:0x0b",
"53:0x69",

@@ -571,12 +539,12 @@ "54:0x6d",

"63:0x67",
"64:0x10",
"64:0x00",
"65:0x00",
"66:0x00",
"67:0x00",
"68:0x28",
"68:0x00",
"69:0x00",
"70:0x00",
"71:0x00",
"72:0x06",
"73:0x01",
"72:0x00",
"73:0x00",
"74:0x00",

@@ -604,6 +572,2 @@ "75:0x00",

"95:0x00",
"96:0x00",
"97:0x00",
"98:0x00",
"99:0x00",
]

@@ -610,0 +574,0 @@ `);

import { ENTRY_TYPE, isPrimitiveEntryType } from "./entry-types";
import { Entry, primitive, DataViewAndAllocatorCarrier } from "./interfaces";
import {
arrayBufferCopyTo,
isPrimitive,
primitiveValueToEntry,
strByteLength
isKnownAddressValuePointer
} from "./utils";

@@ -17,2 +16,4 @@ import { ExternalArgs } from "./interfaces";

import { getAllLinkedAddresses } from "./getAllLinkedAddresses";
import { stringEncodeInto } from "./stringEncodeInto";
import { stringDecode } from "./stringDecode";

@@ -42,12 +43,2 @@ const MAX_64_BIG_INT = BigInt("0xFFFFFFFFFFFFFFFF");

switch (entry.type) {
case ENTRY_TYPE.UNDEFINED:
break;
case ENTRY_TYPE.NULL:
break;
case ENTRY_TYPE.BOOLEAN:
cursor += Uint8Array.BYTES_PER_ELEMENT;
break;
case ENTRY_TYPE.NUMBER:

@@ -62,3 +53,3 @@ cursor += Float64Array.BYTES_PER_ELEMENT;

cursor += Math.max(strByteLength(entry.value), entry.allocatedBytes);
cursor += entry.allocatedBytes;

@@ -109,4 +100,3 @@ // oh boy. i don't want to change it now, but no choice

export function writeEntry(
externalArgs: ExternalArgs,
dataView: DataView,
{ dataView, uint8 }: DataViewAndAllocatorCarrier,
startingCursor: number,

@@ -125,13 +115,2 @@ entry: Entry

switch (entry.type) {
case ENTRY_TYPE.UNDEFINED:
break;
case ENTRY_TYPE.NULL:
break;
case ENTRY_TYPE.BOOLEAN:
dataView.setUint8(cursor, entry.value ? 1 : 0);
cursor += Uint8Array.BYTES_PER_ELEMENT;
break;
case ENTRY_TYPE.NUMBER:

@@ -143,7 +122,3 @@ dataView.setFloat64(cursor, entry.value);

case ENTRY_TYPE.STRING:
// eslint-disable-next-line no-case-declarations
const encodedString: Uint8Array = externalArgs.textEncoder.encode(
entry.value
);
dataView.setUint16(cursor, encodedString.byteLength);
dataView.setUint16(cursor, entry.allocatedBytes);
cursor += Uint16Array.BYTES_PER_ELEMENT;

@@ -154,5 +129,15 @@

for (let i = 0; i < encodedString.length; i++) {
dataView.setUint8(cursor, encodedString[i]);
cursor += Uint8Array.BYTES_PER_ELEMENT;
// const arr = new Uint8Array(entry.allocatedBytes);
// const writtenBytes1 = stringEncodeInto(arr, 0, entry.value);
// eslint-disable-next-line no-case-declarations
const writtenBytes = stringEncodeInto(uint8, cursor, entry.value);
if (writtenBytes !== entry.allocatedBytes) {
// eslint-disable-next-line no-undef
console.warn({
value: entry.value,
writtenBytes,
allocatedBytes: entry.allocatedBytes
});
throw new Error("WTF???");
}

@@ -174,3 +159,3 @@

);
cursor += BigInt64Array.BYTES_PER_ELEMENT;
cursor += BigUint64Array.BYTES_PER_ELEMENT;
break;

@@ -221,3 +206,3 @@

writeEntry(externalArgs, carrier.dataView, memoryAddress, entry);
writeEntry(carrier, memoryAddress, entry);

@@ -228,4 +213,3 @@ return memoryAddress;

export function readEntry(
externalArgs: ExternalArgs,
dataView: DataView,
carrier: DataViewAndAllocatorCarrier,
startingCursor: number

@@ -235,3 +219,3 @@ ): Entry {

const entryType: ENTRY_TYPE = dataView.getUint8(cursor);
const entryType: ENTRY_TYPE = carrier.dataView.getUint8(cursor);
cursor += Uint8Array.BYTES_PER_ELEMENT;

@@ -254,3 +238,3 @@

case ENTRY_TYPE.BOOLEAN:
entry.value = dataView.getUint8(cursor) !== 0;
entry.value = carrier.dataView.getUint8(cursor) !== 0;
cursor += Uint8Array.BYTES_PER_ELEMENT;

@@ -260,3 +244,3 @@ break;

case ENTRY_TYPE.NUMBER:
entry.value = dataView.getFloat64(cursor);
entry.value = carrier.dataView.getFloat64(cursor);
cursor += Float64Array.BYTES_PER_ELEMENT;

@@ -267,6 +251,6 @@ break;

// eslint-disable-next-line no-case-declarations
const stringLength = dataView.getUint16(cursor);
const stringLength = carrier.dataView.getUint16(cursor);
cursor += Uint16Array.BYTES_PER_ELEMENT;
entry.allocatedBytes = dataView.getUint16(cursor);
entry.allocatedBytes = carrier.dataView.getUint16(cursor);
cursor += Uint16Array.BYTES_PER_ELEMENT;

@@ -279,6 +263,6 @@

// eslint-disable-next-line no-case-declarations
const tempAB = new ArrayBuffer(stringLength);
arrayBufferCopyTo(dataView.buffer, cursor, stringLength, tempAB, 0);
// const tempAB = new ArrayBuffer(stringLength);
// arrayBufferCopyTo(dataView.buffer, cursor, stringLength, tempAB, 0);
entry.value = externalArgs.textDecoder.decode(tempAB);
entry.value = stringDecode(carrier.uint8, cursor, stringLength);
} else {

@@ -293,3 +277,3 @@ entry.value = "";

case ENTRY_TYPE.BIGINT_POSITIVE:
entry.value = dataView.getBigUint64(cursor);
entry.value = carrier.dataView.getBigUint64(cursor);
cursor += BigUint64Array.BYTES_PER_ELEMENT;

@@ -299,3 +283,3 @@ break;

case ENTRY_TYPE.BIGINT_NEGATIVE:
entry.value = -dataView.getBigUint64(cursor);
entry.value = -carrier.dataView.getBigUint64(cursor);
cursor += BigUint64Array.BYTES_PER_ELEMENT;

@@ -307,5 +291,5 @@ break;

case ENTRY_TYPE.SET:
entry.refsCount = dataView.getUint8(cursor);
entry.refsCount = carrier.dataView.getUint8(cursor);
cursor += Uint8Array.BYTES_PER_ELEMENT;
entry.value = dataView.getUint32(cursor);
entry.value = carrier.dataView.getUint32(cursor);
cursor += Uint32Array.BYTES_PER_ELEMENT;

@@ -315,9 +299,9 @@ break;

case ENTRY_TYPE.ARRAY:
entry.refsCount = dataView.getUint8(cursor);
entry.refsCount = carrier.dataView.getUint8(cursor);
cursor += Uint8Array.BYTES_PER_ELEMENT;
entry.value = dataView.getUint32(cursor);
entry.value = carrier.dataView.getUint32(cursor);
cursor += Uint32Array.BYTES_PER_ELEMENT;
entry.length = dataView.getUint32(cursor);
entry.length = carrier.dataView.getUint32(cursor);
cursor += Uint32Array.BYTES_PER_ELEMENT;
entry.allocatedLength = dataView.getUint32(cursor);
entry.allocatedLength = carrier.dataView.getUint32(cursor);
cursor += Uint32Array.BYTES_PER_ELEMENT;

@@ -327,5 +311,5 @@ break;

case ENTRY_TYPE.DATE:
entry.refsCount = dataView.getUint8(cursor);
entry.refsCount = carrier.dataView.getUint8(cursor);
cursor += Uint8Array.BYTES_PER_ELEMENT;
entry.value = dataView.getFloat64(cursor);
entry.value = carrier.dataView.getFloat64(cursor);
cursor += Float64Array.BYTES_PER_ELEMENT;

@@ -343,7 +327,2 @@ break;

const typeofTheValue = typeof value;
if (entryA.type === ENTRY_TYPE.BOOLEAN && typeofTheValue === "boolean") {
return true;
}
// number & bigint 64 are the same size

@@ -359,19 +338,11 @@ if (

if (
entryA.type === ENTRY_TYPE.STRING &&
typeofTheValue === "string" &&
entryA.allocatedBytes >= strByteLength(value as string)
) {
return true;
}
// kill for strings for now
// if (
// entryA.type === ENTRY_TYPE.STRING &&
// typeofTheValue === "string" &&
// entryA.allocatedBytes >= strByteLength(value as string)
// ) {
// return true;
// }
if (
((entryA.type === ENTRY_TYPE.NULL ||
entryA.type === ENTRY_TYPE.UNDEFINED) &&
value === undefined) ||
value === null
) {
return true;
}
return false;

@@ -387,27 +358,14 @@ }

const existingEntryPointer = carrier.dataView.getUint32(ptrToPtr);
const existingValueEntry = readEntry(
externalArgs,
carrier.dataView,
existingEntryPointer
);
const existingValueEntry = readEntry(carrier, existingEntryPointer);
// try to re use memory
if (
!isKnownAddressValuePointer(existingEntryPointer) &&
isPrimitive(value) &&
isPrimitiveEntryType(existingValueEntry.type) &&
canReuseMemoryOfEntry(existingValueEntry, value) &&
existingEntryPointer !== 0
canReuseMemoryOfEntry(existingValueEntry, value)
) {
const stringAllocatedBytes =
existingValueEntry.type === ENTRY_TYPE.STRING
? existingValueEntry.allocatedBytes
: 0;
const newEntry = primitiveValueToEntry(value);
const newEntry = primitiveValueToEntry(
externalArgs,
value,
stringAllocatedBytes
);
writeEntry(externalArgs, carrier.dataView, existingEntryPointer, newEntry);
writeEntry(carrier, existingEntryPointer, newEntry);
} else {

@@ -446,3 +404,3 @@ const referencedPointers: number[] = [];

for (const ptr of referencedPointers) {
incrementRefCount(externalArgs, carrier.dataView, ptr);
incrementRefCount(externalArgs, carrier, ptr);
}

@@ -454,3 +412,3 @@ }

externalArgs,
carrier.dataView,
carrier,
existingEntryPointer

@@ -461,4 +419,3 @@ );

const addressesToFree = getAllLinkedAddresses(
externalArgs,
carrier.dataView,
carrier,
false,

@@ -468,5 +425,9 @@ existingEntryPointer

for (const address of addressesToFree) {
for (const address of addressesToFree.leafAddresses) {
carrier.allocator.free(address);
}
for (const address of addressesToFree.arcAddresses) {
decrementRefCount(externalArgs, carrier, address);
}
}

@@ -478,29 +439,50 @@ } else {

export function incrementRefCount(
export function handleArcForDeletedValuePointer(
externalArgs: ExternalArgs,
dataView: DataView,
entryPointer: number
) {
const entry = readEntry(externalArgs, dataView, entryPointer);
carrier: DataViewAndAllocatorCarrier,
deletedValuePointer: number
): void {
// No memory to free/ARC
if (isKnownAddressValuePointer(deletedValuePointer)) {
return;
}
if ("refsCount" in entry) {
entry.refsCount += 1;
writeEntry(externalArgs, dataView, entryPointer, entry);
const existingValueEntry = readEntry(carrier, deletedValuePointer);
if (existingValueEntry && "refsCount" in existingValueEntry) {
const newRefCount = decrementRefCount(
externalArgs,
carrier,
deletedValuePointer
);
return entry.refsCount;
if (newRefCount === 0) {
const addressesToFree = getAllLinkedAddresses(
carrier,
false,
deletedValuePointer
);
for (const address of addressesToFree.leafAddresses) {
carrier.allocator.free(address);
}
for (const address of addressesToFree.arcAddresses) {
decrementRefCount(externalArgs, carrier, address);
}
}
} else {
carrier.allocator.free(deletedValuePointer);
}
throw new Error("unexpected");
}
export function decrementRefCount(
export function incrementRefCount(
externalArgs: ExternalArgs,
dataView: DataView,
carrier: DataViewAndAllocatorCarrier,
entryPointer: number
) {
const entry = readEntry(externalArgs, dataView, entryPointer);
const entry = readEntry(carrier, entryPointer);
if ("refsCount" in entry) {
entry.refsCount -= 1;
writeEntry(externalArgs, dataView, entryPointer, entry);
entry.refsCount += 1;
writeEntry(carrier, entryPointer, entry);

@@ -513,10 +495,13 @@ return entry.refsCount;

export function getRefCount(
export function decrementRefCount(
externalArgs: ExternalArgs,
dataView: DataView,
carrier: DataViewAndAllocatorCarrier,
entryPointer: number
) {
const entry = readEntry(externalArgs, dataView, entryPointer);
const entry = readEntry(carrier, entryPointer);
if ("refsCount" in entry) {
entry.refsCount -= 1;
writeEntry(carrier, entryPointer, entry);
return entry.refsCount;

@@ -523,0 +508,0 @@ }

@@ -23,4 +23,6 @@ export function arrayBuffer2HexArray(

import { IMemPool } from "@thi.ng/malloc";
import { IMemPool, MemPool } from "@thi.ng/malloc";
import { OutOfMemoryError } from "./exceptions";
import { DataViewAndAllocatorCarrier } from "./interfaces";
import { MEM_POOL_START } from "./consts";

@@ -96,1 +98,20 @@ // extend pool and not monkey patch? need to think about it

}
export function makeCarrier(arrayBuffer: ArrayBuffer) {
const allocator = new MemPool({
buf: arrayBuffer,
start: MEM_POOL_START
});
const carrier: DataViewAndAllocatorCarrier = {
dataView: new DataView(arrayBuffer),
allocator,
uint8: new Uint8Array(arrayBuffer),
uint16: new Uint16Array(arrayBuffer),
uint32: new Uint32Array(arrayBuffer),
float64: new Float64Array(arrayBuffer),
bigUint64: new BigUint64Array(arrayBuffer)
};
return carrier;
}

@@ -10,2 +10,8 @@ import {

import { INTERNAL_API_SYMBOL } from "./symbols";
import {
UNDEFINED_KNOWN_ADDRESS,
NULL_KNOWN_ADDRESS,
TRUE_KNOWN_ADDRESS,
FALSE_KNOWN_ADDRESS
} from "./consts";

@@ -32,7 +38,3 @@ const primitives = [

export function primitiveValueToEntry(
externalArgs: ExternalArgs,
value: primitive,
stringAllocatedBytes: number
): Entry {
export function primitiveValueToEntry(value: primitive): Entry {
if (typeof value === "string") {

@@ -42,6 +44,3 @@ return {

value,
allocatedBytes: Math.max(
externalArgs.textEncoder.encode(value).length,
stringAllocatedBytes
)
allocatedBytes: strByteLength(value)
};

@@ -67,21 +66,2 @@ }

if (typeof value === "boolean") {
return {
type: ENTRY_TYPE.BOOLEAN,
value
};
}
if (typeof value === "undefined") {
return {
type: ENTRY_TYPE.UNDEFINED
};
}
if (value === null) {
return {
type: ENTRY_TYPE.NULL
};
}
throw new Error("unexpected");

@@ -137,5 +117,2 @@ }

? p.arrayAdditionalAllocation
: 0,
minimumStringAllocation: p.minimumStringAllocation
? p.minimumStringAllocation
: 0

@@ -167,1 +144,10 @@ };

}
export function isKnownAddressValuePointer(entryPointer: number) {
return (
entryPointer === UNDEFINED_KNOWN_ADDRESS ||
entryPointer === NULL_KNOWN_ADDRESS ||
entryPointer === TRUE_KNOWN_ADDRESS ||
entryPointer === FALSE_KNOWN_ADDRESS
);
}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc