Skip to content

new Uint8Array(n) with non-literal n returns a zero-length buffer #38

@proggeramlug

Description

@proggeramlug

Repro

function make(n: number) {
  const u = new Uint8Array(n);
  console.log('n=' + n + ' length=' + u.length);
}
make(100);
make(1024);

Expected

n=100 length=100
n=1024 length=1024

Actual (Perry 0.5.30)

n=100 length=0
n=1024 length=0

Likely cause

In crates/perry-codegen/src/expr.rs, Expr::Uint8ArrayNew has fast paths for the literal cases Expr::Integer(n) and Expr::Number(n) (routed to js_buffer_alloc), but the catch-all Some(e) => … arm falls through to js_uint8array_from_array, which treats the number as an ArrayHeader* pointer and silently yields a zero-length buffer.

Workaround

Buffer.alloc(n) works with computed/variable sizes. new Uint8Array(<literal>) works too.

Related

The pair for writes (u8[i] = v being a silent no-op) was a similar codegen stub in Expr::Uint8ArraySet; fixed separately.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions