MCPcopy Create free account
hub / github.com/W4G1/multithreading / arraySet

Method arraySet

src/lib/json_buffer.ts:1411–1476  ·  view source on GitHub ↗
(target: Pointer, index: number, value: any)

Source from the content-addressed store, hash-verified

1409 };
1410
1411 private arraySet(target: Pointer, index: number, value: any) {
1412 this.resolvePtr(target.__ptr);
1413 if (index < this.scratchLen) {
1414 const offset = this.scratchStart + index * 8;
1415 this.u32[offset >> 2] = TYPE_NULL;
1416 this.u32[(offset + 4) >> 2] = 0;
1417 }
1418
1419 const valResult = this.writeValue(value);
1420 const valHandle = { __ptr: valResult.payload };
1421 const isValPtr = valResult.type >= TYPE_NUMBER;
1422
1423 if (isValPtr) {
1424 this.tempRoots.push({ handle: valHandle, type: valResult.type });
1425 }
1426
1427 try {
1428 this.resolvePtr(target.__ptr);
1429 const ptr = this.scratchPtr;
1430 const cap = this.scratchCap;
1431 const len = this.scratchLen;
1432
1433 if (index >= cap) {
1434 const newCap = Math.max(cap * 2, index + 1);
1435 const newByteSize = 12 + newCap * 8;
1436
1437 const newPtr = this.alloc(newByteSize);
1438
1439 this.resolvePtr(target.__ptr);
1440 const oldDataStart = this.scratchStart;
1441
1442 const idx = newPtr >> 2;
1443 this.u32[idx] = TYPE_ARRAY;
1444 this.u32[idx + 1] = newCap;
1445 const newLen = Math.max(len, index + 1);
1446 this.u32[idx + 2] = newLen;
1447
1448 const oldByteLen = len * 8;
1449 this.u8.set(
1450 this.u8.subarray(oldDataStart, oldDataStart + oldByteLen),
1451 newPtr + 12,
1452 );
1453
1454 const pIdx = this.scratchPtr >> 2;
1455 this.u32[pIdx] = TYPE_MOVED;
1456 this.u32[pIdx + 1] = newPtr;
1457
1458 const offset = newPtr + 12 + index * 8;
1459 const oIdx = offset >> 2;
1460 this.u32[oIdx] = valResult.type;
1461 this.u32[oIdx + 1] = valHandle.__ptr;
1462 return;
1463 }
1464
1465 const offset = ptr + 12 + index * 8;
1466 const oIdx = offset >> 2;
1467 this.u32[oIdx] = valResult.type;
1468 this.u32[oIdx + 1] = valHandle.__ptr;

Callers 3

writeValueMethod · 0.95
writeInitialMethod · 0.95

Calls 3

resolvePtrMethod · 0.95
writeValueMethod · 0.95
allocMethod · 0.95

Tested by

no test coverage detected