Skip to content

Commit

Permalink
Process VLEN values
Browse files Browse the repository at this point in the history
  • Loading branch information
axelboc committed Aug 6, 2024
1 parent 6801969 commit 03a729a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
26 changes: 25 additions & 1 deletion src/hdf5_hl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,30 @@ function process_data(data: Uint8Array, metadata: Metadata, json_compatible: boo
return output_data;
}

else if (type === Module.H5T_class_t.H5T_VLEN.value) {
// Data buffer holds HDF5 `hvl_t` struct
// https://docs.hdfgroup.org/hdf5/v1_12/structhvl__t.html
const ptr_pairs = new Uint32Array(data.buffer); // both `size_t` length and pointer are 4-byte-long
const num_ptrs = ptr_pairs.length / 2;
const vlen_type = metadata.vlen_type as Metadata;
const { size } = vlen_type;

let output: (OutputData | JSONCompatibleOutputData)[] = [];
for (let p = 0; p < num_ptrs; p += 1) {
const length = ptr_pairs[p * 2]; // number of elements in this vlen array
const data_ptr = ptr_pairs[p * 2 + 1]; // pointer to this vlen array's data

// Read vlen array data from memory
const data_nbytes = length * size;
const data = Module.HEAPU8.slice(data_ptr, data_ptr + data_nbytes);

// Process this vlen array's data according to base datatype
output.push(process_data(data, { ...vlen_type, shape: [length] }, json_compatible));
}

output_data = output;
}

else {
known_type = false;
output_data = data;
Expand Down Expand Up @@ -948,7 +972,7 @@ export class Dataset extends HasAttrs {
let data = Module.HEAPU8.slice(data_ptr, data_ptr + nbytes);
processed = process_data(data, metadata, false);
} finally {
if (metadata.vlen) {
if (metadata.vlen || metadata.type === Module.H5T_class_t.H5T_VLEN.value) {
Module.reclaim_vlen_memory(this.file_id, this.path, "", BigInt(data_ptr));
}
Module._free(data_ptr);
Expand Down
10 changes: 10 additions & 0 deletions test/vlen_test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ async function vlen_test() {
vlen: false,
},
});

assert.deepEqual(f.get('int8_scalar').value, new Int8Array([0, 1]));
assert.deepEqual(
f.get('float32_oneD').value,
[
new Float32Array([0]),
new Float32Array([0, 1]),
new Float32Array([0, 1, 2])
]
);
}

export const tests = [
Expand Down

0 comments on commit 03a729a

Please sign in to comment.