private writeStmtSpecificType()

in javascript/packages/fury/lib/gen/map.ts [256:336]


  private writeStmtSpecificType(accessor: string) {
    const k = this.scope.uniqueName("k");
    const v = this.scope.uniqueName("v");
    const keyHeader = (this.keyGenerator.needToWriteRef() ? MapFlags.TRACKING_REF : 0);
    const valueHeader = (this.valueGenerator.needToWriteRef() ? MapFlags.TRACKING_REF : 0);
    const typeId = (this.keyGenerator.getTypeId()! << 8) | (this.valueGenerator.getTypeId()!);
    const lastKeyIsNull = this.scope.uniqueName("lastKeyIsNull");
    const lastValueIsNull = this.scope.uniqueName("lastValueIsNull");
    const chunkSize = this.scope.uniqueName("chunkSize");
    const chunkSizeOffset = this.scope.uniqueName("chunkSizeOffset");
    const keyRef = this.scope.uniqueName("keyRef");
    const valueRef = this.scope.uniqueName("valueRef");

    return `
      ${this.builder.writer.varInt32(`${accessor}.size`)}
      let ${lastKeyIsNull} = false;
      let ${lastValueIsNull} = false;
      let ${chunkSize} = 0;
      let ${chunkSizeOffset} = 0;

      for (const [${k}, ${v}] of ${accessor}.entries()) {
        let keyIsNull = ${k} === null || ${k} === undefined;
        let valueIsNull = ${v} === null || ${v} === undefined;

        if (${lastKeyIsNull} !== keyIsNull || ${lastValueIsNull} !== valueIsNull || ${chunkSize} === 0 || ${chunkSize} === 255) {
          if (${chunkSize} > 0) {
            ${this.builder.writer.setUint8Position(chunkSizeOffset, chunkSize)};
            ${chunkSize} = 0;
          }
          ${chunkSizeOffset} = ${this.builder.writer.getCursor()}
          ${
            this.builder.writer.uint16(
              `(((${keyHeader} | (keyIsNull ? ${MapFlags.HAS_NULL} : 0)) << 4) | (${valueHeader} | (valueIsNull ? ${MapFlags.HAS_NULL} : 0))) << 8`
            )
          }
          ${this.builder.writer.uint32(typeId)};

          ${lastKeyIsNull} = keyIsNull;
          ${lastValueIsNull} = valueIsNull;
        }
        if (keyIsNull) {
          ${this.builder.writer.uint8(RefFlags.NullFlag)}
        }
        ${this.keyGenerator.needToWriteRef()
? `
            const ${keyRef} = ${this.builder.referenceResolver.existsWriteObject(v)};
            if (${keyRef} !== undefined) {
              ${this.builder.writer.uint8(RefFlags.RefFlag)};
              ${this.builder.writer.uint16(keyRef)};
            } else {
              ${this.builder.writer.uint8(RefFlags.RefValueFlag)};
              ${this.keyGenerator.toWriteEmbed(k, true)}
            }
        `
: this.keyGenerator.toWriteEmbed(k, true)}
       

        if (valueIsNull) {
          ${this.builder.writer.uint8(RefFlags.NullFlag)}
        }
        ${this.valueGenerator.needToWriteRef()
? `
            const ${valueRef} = ${this.builder.referenceResolver.existsWriteObject(v)};
            if (${valueRef} !== undefined) {
              ${this.builder.writer.uint8(RefFlags.RefFlag)};
              ${this.builder.writer.uint16(valueRef)};
            } else {
              ${this.builder.writer.uint8(RefFlags.RefValueFlag)};
              ${this.valueGenerator.toWriteEmbed(v, true)};
            }
        `
: this.valueGenerator.toWriteEmbed(v, true)}
        

        ${chunkSize}++;
      }
      if (${chunkSize} > 0) {
        ${this.builder.writer.setUint8Position(chunkSizeOffset, chunkSize)};
      }
    `;
  }