1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
|
// Start of values.js
var futharkPrimtypes =
new Set([
'i8',
'i16',
'i32',
'i64',
'u8',
'u16',
'u32',
'u64',
'f16',
'f32',
'f64',
'bool']);
var typToType = { ' i8' : Int8Array ,
' i16' : Int16Array ,
' i32' : Int32Array ,
' i64' : BigInt64Array ,
' u8' : Uint8Array ,
' u16' : Uint16Array ,
' u32' : Uint32Array ,
' u64' : BigUint64Array ,
' f16' : Uint16Array ,
' f32' : Float32Array ,
' f64' : Float64Array ,
'bool' : Uint8Array
};
function binToStringArray(buff, array) {
for (var i = 0; i < array.length; i++) {
array[i] = buff[i];
}
}
function fileToBuff(fname) {
var readline = require('readline');
var fs = require('fs');
var buff = fs.readFileSync(fname);
return buff;
}
var typToSize = {
"bool" : 1,
" u8" : 1,
" i8" : 1,
" u16" : 2,
" i16" : 2,
" u32" : 4,
" i32" : 4,
" f16" : 2,
" f32" : 4,
" u64" : 8,
" i64" : 8,
" f64" : 8,
}
function toU8(ta) {
return new Uint8Array(ta.buffer, ta.byteOffset, ta.byteLength);
}
function construct_binary_value(v, typ) {
var dims;
var payload_bytes;
var filler;
if (v instanceof FutharkOpaque) {
throw "Opaques are not supported";
} else if (v instanceof FutharkArray) {
var t = v.futharkType();
var ftype = " ".slice(t.length) + t;
var shape = v.shape();
var ta = v.toTypedArray(shape);
var da = new BigInt64Array(shape);
dims = shape.length;
payload_bytes = da.byteLength + ta.byteLength;
filler = (bytes) => {
bytes.set(toU8(da), 7);
bytes.set(toU8(ta), 7 + da.byteLength);
}
} else {
var ftype = " ".slice(typ.length) + typ;
dims = 0;
payload_bytes = typToSize[ftype];
filler = (bytes) => {
var scalar = new (typToType[ftype])([v]);
bytes.set(toU8(scalar), 7);
}
}
var total_bytes = 7 + payload_bytes;
var bytes = new Uint8Array(total_bytes);
bytes[0] = Buffer.from('b').readUInt8();
bytes[1] = 2;
bytes[2] = dims;
for (var i = 0; i < 4; i++) {
bytes[3+i] = ftype.charCodeAt(i);
}
filler(bytes);
return Buffer.from(bytes);
}
class Reader {
constructor(f) {
this.f = f;
this.buff = fileToBuff(f);
}
read_bin_array(num_dim, typ) {
var u8_array = new Uint8Array(num_dim * 8);
binToStringArray(this.buff.slice(0, num_dim * 8), u8_array);
var shape = new BigInt64Array(u8_array.buffer);
var length = shape[0];
for (var i = 1; i < shape.length; i++) {
length = length * shape[i];
}
length = Number(length);
var dbytes = typToSize[typ];
var u8_data = new Uint8Array(length * dbytes);
binToStringArray(this.buff.slice(num_dim * 8, num_dim * 8 + dbytes * length), u8_data);
var data = new (typToType[typ])(u8_data.buffer);
var tmp_buff = this.buff.slice(num_dim * 8, num_dim * 8 + dbytes * length);
this.buff = this.buff.slice(num_dim * 8 + dbytes * length);
return [shape, data];
}
read_bin_scalar(typ) {
var size = typToSize[typ];
var u8_array = new Uint8Array(size);
binToStringArray(this.buff, u8_array);
var array = new (typToType[typ])(u8_array.buffer);
this.buff = this.buff.slice(u8_array.length); // Update buff to be unread part of the string
return array[0];
}
skip_spaces() {
while (this.buff.length > 0 && this.buff.slice(0, 1).toString().trim() == "") {
this.buff = this.buff.slice(1);
}
}
read_binary(typename, dim) {
// Skip leading white space
while (this.buff.slice(0, 1).toString().trim() == "") {
this.buff = this.buff.slice(1);
}
if (this.buff[0] != 'b'.charCodeAt(0)) {
throw "Not in binary format"
}
var version = this.buff[1];
if (version != 2) {
throw "Not version 2";
}
var num_dim = this.buff[2];
var typ = this.buff.slice(3, 7);
this.buff = this.buff.slice(7);
var exp_typ = "[]".repeat(dim) + typename;
var given_typ = "[]".repeat(num_dim) + typ.toString().trim();
console.log(exp_typ);
console.log(given_typ);
if (exp_typ !== given_typ) {
throw ("Expected type : " + exp_typ + ", Actual type : " + given_typ);
}
if (num_dim === 0) {
return this.read_bin_scalar(typ);
} else {
return this.read_bin_array(num_dim, typ);
}
}
get_buff() {
return this.buff;
}
}
// Function is redudant but is helpful for keeping consistent with python implementation
function skip_spaces(reader) {
reader.skip_spaces();
}
function read_value(typename, reader) {
var typ = typename;
var dim = 0;
while (typ.slice(0, 2) === "[]") {
dim = dim + 1;
typ = typ.slice(2);
}
if (!futharkPrimtypes.has(typ)) {
throw ("Unkown type: " + typ);
}
var val = reader.read_binary(typ, dim);
return val;
}
// End of values.js
|