Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] ArrayBuffer does not have length property #110

Closed
jtenner opened this issue Jun 20, 2019 · 3 comments
Closed

[Bug] ArrayBuffer does not have length property #110

jtenner opened this issue Jun 20, 2019 · 3 comments
Assignees
Labels
Bug 🐛 Something isn't working
Milestone

Comments

@jtenner
Copy link
Contributor

jtenner commented Jun 20, 2019

There is an issue with the toHaveLength() assertion that should work with ArrayBuffers.

ERROR TS2339: Property 'length' does not exist on type '~lib/arraybuffer/ArrayBuffer'.

       length = actual.length;
                       ~~~~~~
 in node_modules/as-pect/assembly/internal/comparison/lengthComparison.ts(34,22)

Way to reproduce:

expect<ArrayBuffer>(buffer).toHaveLength(100, "Some expectation.");
@jtenner jtenner added the Bug 🐛 Something isn't working label Jun 20, 2019
@jtenner jtenner added this to the 1.3.0 milestone Jun 20, 2019
@jtenner jtenner self-assigned this Jun 20, 2019
@willemneal
Copy link
Collaborator

This is because ArrayBuffer doesn't have a length property any more as that is meant for different views with various element widths. Use byteLength or cast it as a Uint8Array.

@sealed export class ArrayBuffer {

  static isView<T>(value: T): bool {
    if (value) {
      if (value instanceof Int8Array) return true;
      if (value instanceof Uint8Array) return true;
      if (value instanceof Uint8ClampedArray) return true;
      if (value instanceof Int16Array) return true;
      if (value instanceof Uint16Array) return true;
      if (value instanceof Int32Array) return true;
      if (value instanceof Uint32Array) return true;
      if (value instanceof Int64Array) return true;
      if (value instanceof Uint64Array) return true;
      if (value instanceof Float32Array) return true;
      if (value instanceof Float64Array) return true;
      if (value instanceof DataView) return true;
    }
    return false;
  }

  constructor(length: i32) {
    if (<u32>length > <u32>BLOCK_MAXSIZE) throw new RangeError(E_INVALIDLENGTH);
    var buffer = __alloc(<usize>length, idof<ArrayBuffer>());
    memory.fill(buffer, 0, <usize>length);
    return changetype<ArrayBuffer>(buffer); // retains
  }

  get byteLength(): i32 {
    return changetype<BLOCK>(changetype<usize>(this) - BLOCK_OVERHEAD).rtSize;
  }

  slice(begin: i32 = 0, end: i32 = BLOCK_MAXSIZE): ArrayBuffer {
    var length = this.byteLength;
    begin = begin < 0 ? max(length + begin, 0) : min(begin, length);
    end   = end   < 0 ? max(length + end  , 0) : min(end  , length);
    var outSize = <usize>max(end - begin, 0);
    var out = __alloc(outSize, idof<ArrayBuffer>());
    memory.copy(out, changetype<usize>(this) + <usize>begin, outSize);
    return changetype<ArrayBuffer>(out); // retains
  }

  toString(): string {
    return "[object ArrayBuffer]";
  }
}

@jtenner
Copy link
Contributor Author

jtenner commented Jun 20, 2019

I'm aware of this. Have you seen the lengthComparison function in the comparison folder?

@jtenner
Copy link
Contributor Author

jtenner commented Jun 22, 2019

Closed via AssemblyScript/assemblyscript#681

@jtenner jtenner closed this as completed Jun 22, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug 🐛 Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants