| 1 |
// Start of wrapperclasses.js |
| 2 |
|
| 3 |
class FutharkArray { |
| 4 |
constructor(ctx, ptr, type_name, dim, heap, fshape, fvalues, ffree) { |
| 5 |
this.ctx = ctx; |
| 6 |
this.ptr = ptr; |
| 7 |
this.type_name = type_name; |
| 8 |
this.dim = dim; |
| 9 |
this.heap = heap; |
| 10 |
this.fshape = fshape; |
| 11 |
this.fvalues = fvalues; |
| 12 |
this.ffree = ffree; |
| 13 |
this.valid = true; |
| 14 |
} |
| 15 |
|
| 16 |
validCheck() { |
| 17 |
if (!this.valid) { |
| 18 |
throw "Using freed memory" |
| 19 |
} |
| 20 |
} |
| 21 |
|
| 22 |
futharkType() { |
| 23 |
return this.type_name; |
| 24 |
} |
| 25 |
|
| 26 |
free() { |
| 27 |
this.validCheck(); |
| 28 |
this.ffree(this.ctx.ctx, this.ptr); |
| 29 |
this.valid = false; |
| 30 |
} |
| 31 |
|
| 32 |
shape() { |
| 33 |
this.validCheck(); |
| 34 |
var s = this.fshape(this.ctx.ctx, this.ptr) >> 3; |
| 35 |
return Array.from(this.ctx.wasm.HEAP64.subarray(s, s + this.dim)); |
| 36 |
} |
| 37 |
|
| 38 |
toTypedArray(dims = this.shape()) { |
| 39 |
this.validCheck(); |
| 40 |
console.assert(dims.length === this.dim, "dim=%s,dims=%s", this.dim, dims.toString()); |
| 41 |
var length = Number(dims.reduce((a, b) => a * b)); |
| 42 |
var v = this.fvalues(this.ctx.ctx, this.ptr) / this.heap.BYTES_PER_ELEMENT; |
| 43 |
return this.heap.subarray(v, v + length); |
| 44 |
} |
| 45 |
|
| 46 |
toArray() { |
| 47 |
this.validCheck(); |
| 48 |
var dims = this.shape(); |
| 49 |
var ta = this.toTypedArray(dims); |
| 50 |
return (function nest(offs, ds) { |
| 51 |
var d0 = Number(ds[0]); |
| 52 |
if (ds.length === 1) { |
| 53 |
return Array.from(ta.subarray(offs, offs + d0)); |
| 54 |
} else { |
| 55 |
var d1 = Number(ds[1]); |
| 56 |
return Array.from(Array(d0), (x,i) => nest(offs + i * d1, ds.slice(1))); |
| 57 |
} |
| 58 |
})(0, dims); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
class FutharkOpaque { |
| 63 |
constructor(ctx, ptr, ffree) { |
| 64 |
this.ctx = ctx; |
| 65 |
this.ptr = ptr; |
| 66 |
this.ffree = ffree; |
| 67 |
this.valid = true; |
| 68 |
} |
| 69 |
|
| 70 |
validCheck() { |
| 71 |
if (!this.valid) { |
| 72 |
throw "Using freed memory" |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
free() { |
| 77 |
this.validCheck(); |
| 78 |
this.ffree(this.ctx.ctx, this.ptr); |
| 79 |
this.valid = false; |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
// End of wrapperclasses.js |