|
| 1 | +// TBD: Moving lib/parse here might make sense where the compiler has to evaluate WASM binaries on |
| 2 | +// the fly, like when importing other WASM files through `import`. Module integration hasn't been |
| 3 | +// specified, yet, though, and it somewhat conflicts with our dependency on Binaryen. |
| 4 | + |
| 5 | +/** WebAssembly section ids. */ |
| 6 | +export enum SectionId { |
| 7 | + /** A custom section with an explicit name. */ |
| 8 | + Custom = 0, |
| 9 | + /** Function types section. */ |
| 10 | + Type = 1, |
| 11 | + /** Imports section. */ |
| 12 | + Import = 2, |
| 13 | + /** Functions section. */ |
| 14 | + Function = 3, |
| 15 | + /** Tables section. */ |
| 16 | + Table = 4, |
| 17 | + /** Memories section. */ |
| 18 | + Memory = 5, |
| 19 | + /** Globals section. */ |
| 20 | + Global = 6, |
| 21 | + /** Exports section. */ |
| 22 | + Export = 7, |
| 23 | + /** Start function section. */ |
| 24 | + Start = 8, |
| 25 | + /** Table element section. */ |
| 26 | + Element = 9, |
| 27 | + /** Function code section. */ |
| 28 | + Code = 10, |
| 29 | + /** Memory segments section. */ |
| 30 | + Data = 11 |
| 31 | +} |
| 32 | + |
| 33 | +/** WebAssembly external kinds. */ |
| 34 | +export enum ExternalKind { |
| 35 | + /** External function. */ |
| 36 | + Function = 0, |
| 37 | + /** External table. */ |
| 38 | + Table = 1, |
| 39 | + /** External memory. */ |
| 40 | + Memory = 2, |
| 41 | + /** External global. */ |
| 42 | + Global = 3 |
| 43 | +} |
| 44 | + |
| 45 | +/** WebAssembly name section kinds. */ |
| 46 | +export enum NameKind { |
| 47 | + /** Module name. */ |
| 48 | + Module = 0, |
| 49 | + /** Function name. */ |
| 50 | + Function = 1, |
| 51 | + /** Local name. */ |
| 52 | + Local = 2, |
| 53 | + |
| 54 | + // see: https://github.com/WebAssembly/design/pull/1064 |
| 55 | + |
| 56 | + /** Label name. */ |
| 57 | + Label = 3, |
| 58 | + /** Function type name. */ |
| 59 | + Type = 4, |
| 60 | + /** Table name. */ |
| 61 | + Table = 5, |
| 62 | + /** Memory name. */ |
| 63 | + Memory = 6, |
| 64 | + /** Global variable name. */ |
| 65 | + Global = 7 |
| 66 | +} |
| 67 | + |
| 68 | +/** WebAssembly types. */ |
| 69 | +export enum Type { |
| 70 | + i32 = 0x7f, |
| 71 | + i64 = 0x7e, |
| 72 | + f32 = 0x7d, |
| 73 | + f64 = 0x7c, |
| 74 | + anyfunc = 0x70, |
| 75 | + func = 0x60, |
| 76 | + none = 0x40 |
| 77 | +} |
| 78 | + |
| 79 | +/** WebAssembly opcodes. */ |
| 80 | +export enum Op { |
| 81 | + unreachable = 0x00, |
| 82 | + nop = 0x01, |
| 83 | + block = 0x02, |
| 84 | + loop = 0x03, |
| 85 | + if_ = 0x04, |
| 86 | + else_ = 0x05, |
| 87 | + end = 0x0b, |
| 88 | + br = 0x0c, |
| 89 | + br_if = 0x0d, |
| 90 | + br_table = 0x0e, |
| 91 | + return_ = 0x0f, |
| 92 | + call = 0x10, |
| 93 | + call_indirect = 0x11, |
| 94 | + drop = 0x1a, |
| 95 | + select = 0x1b, |
| 96 | + get_local = 0x20, |
| 97 | + set_local = 0x21, |
| 98 | + tee_local = 0x22, |
| 99 | + get_global = 0x23, |
| 100 | + set_global = 0x24, |
| 101 | + i32_load = 0x28, |
| 102 | + i64_load = 0x29, |
| 103 | + f32_load = 0x2a, |
| 104 | + f64_load = 0x2b, |
| 105 | + i32_load8_s = 0x2c, |
| 106 | + i32_load8_u = 0x2d, |
| 107 | + i32_load16_s = 0x2e, |
| 108 | + i32_load16_u = 0x2f, |
| 109 | + i64_load8_s = 0x30, |
| 110 | + i64_load8_u = 0x31, |
| 111 | + i64_load16_s = 0x32, |
| 112 | + i64_load16_u = 0x33, |
| 113 | + i64_load32_s = 0x34, |
| 114 | + i64_load32_u = 0x35, |
| 115 | + i32_store = 0x36, |
| 116 | + i64_store = 0x37, |
| 117 | + f32_store = 0x38, |
| 118 | + f64_store = 0x39, |
| 119 | + i32_store8 = 0x3a, |
| 120 | + i32_store16 = 0x3b, |
| 121 | + i64_store8 = 0x3c, |
| 122 | + i64_store16 = 0x3d, |
| 123 | + i64_store32 = 0x3e, |
| 124 | + current_memory = 0x3f, |
| 125 | + grow_memory = 0x40, |
| 126 | + i32_const = 0x41, |
| 127 | + i64_const = 0x42, |
| 128 | + f32_const = 0x43, |
| 129 | + f64_const = 0x44, |
| 130 | + i32_eqz = 0x45, |
| 131 | + i32_eq = 0x46, |
| 132 | + i32_ne = 0x47, |
| 133 | + i32_lt_s = 0x48, |
| 134 | + i32_lt_u = 0x49, |
| 135 | + i32_gt_s = 0x4a, |
| 136 | + i32_gt_u = 0x4b, |
| 137 | + i32_le_s = 0x4c, |
| 138 | + i32_le_u = 0x4d, |
| 139 | + i32_ge_s = 0x4e, |
| 140 | + i32_ge_u = 0x4f, |
| 141 | + i64_eqz = 0x50, |
| 142 | + i64_eq = 0x51, |
| 143 | + i64_ne = 0x52, |
| 144 | + i64_lt_s = 0x53, |
| 145 | + i64_lt_u = 0x54, |
| 146 | + i64_gt_s = 0x55, |
| 147 | + i64_gt_u = 0x56, |
| 148 | + i64_le_s = 0x57, |
| 149 | + i64_le_u = 0x58, |
| 150 | + i64_ge_s = 0x59, |
| 151 | + i64_ge_u = 0x5a, |
| 152 | + f32_eq = 0x5b, |
| 153 | + f32_ne = 0x5c, |
| 154 | + f32_lt = 0x5d, |
| 155 | + f32_gt = 0x5e, |
| 156 | + f32_le = 0x5f, |
| 157 | + f32_ge = 0x60, |
| 158 | + f64_eq = 0x61, |
| 159 | + f64_ne = 0x62, |
| 160 | + f64_lt = 0x63, |
| 161 | + f64_gt = 0x64, |
| 162 | + f64_le = 0x65, |
| 163 | + f64_ge = 0x66, |
| 164 | + i32_clz = 0x67, |
| 165 | + i32_ctz = 0x68, |
| 166 | + i32_popcnt = 0x69, |
| 167 | + i32_add = 0x6a, |
| 168 | + i32_sub = 0x6b, |
| 169 | + i32_mul = 0x6c, |
| 170 | + i32_div_s = 0x6d, |
| 171 | + i32_div_u = 0x6e, |
| 172 | + i32_rem_s = 0x6f, |
| 173 | + i32_rem_u = 0x70, |
| 174 | + i32_and = 0x71, |
| 175 | + i32_or = 0x72, |
| 176 | + i32_xor = 0x73, |
| 177 | + i32_shl = 0x74, |
| 178 | + i32_shr_s = 0x75, |
| 179 | + i32_shr_u = 0x76, |
| 180 | + i32_rotl = 0x77, |
| 181 | + i32_rotr = 0x78, |
| 182 | + i64_clz = 0x79, |
| 183 | + i64_ctz = 0x7a, |
| 184 | + i64_popcnt = 0x7b, |
| 185 | + i64_add = 0x7c, |
| 186 | + i64_sub = 0x7d, |
| 187 | + i64_mul = 0x7e, |
| 188 | + i64_div_s = 0x7f, |
| 189 | + i64_div_u = 0x80, |
| 190 | + i64_rem_s = 0x81, |
| 191 | + i64_rem_u = 0x82, |
| 192 | + i64_and = 0x83, |
| 193 | + i64_or = 0x84, |
| 194 | + i64_xor = 0x85, |
| 195 | + i64_shl = 0x86, |
| 196 | + i64_shr_s = 0x87, |
| 197 | + i64_shr_u = 0x88, |
| 198 | + i64_rotl = 0x89, |
| 199 | + i64_rotr = 0x8a, |
| 200 | + f32_abs = 0x8b, |
| 201 | + f32_neg = 0x8c, |
| 202 | + f32_ceil = 0x8d, |
| 203 | + f32_floor = 0x8e, |
| 204 | + f32_trunc = 0x8f, |
| 205 | + f32_nearest = 0x90, |
| 206 | + f32_sqrt = 0x91, |
| 207 | + f32_add = 0x92, |
| 208 | + f32_sub = 0x93, |
| 209 | + f32_mul = 0x94, |
| 210 | + f32_div = 0x95, |
| 211 | + f32_min = 0x96, |
| 212 | + f32_max = 0x97, |
| 213 | + f32_copysign = 0x98, |
| 214 | + f64_abs = 0x99, |
| 215 | + f64_neg = 0x9a, |
| 216 | + f64_ceil = 0x9b, |
| 217 | + f64_floor = 0x9c, |
| 218 | + f64_trunc = 0x9d, |
| 219 | + f64_nearest = 0x9e, |
| 220 | + f64_sqrt = 0x9f, |
| 221 | + f64_add = 0xa0, |
| 222 | + f64_sub = 0xa1, |
| 223 | + f64_mul = 0xa2, |
| 224 | + f64_div = 0xa3, |
| 225 | + f64_min = 0xa4, |
| 226 | + f64_max = 0xa5, |
| 227 | + f64_copysign = 0xa6, |
| 228 | + i32_wrap_i64 = 0xa7, |
| 229 | + i32_trunc_s_f32 = 0xa8, |
| 230 | + i32_trunc_u_f32 = 0xa9, |
| 231 | + i32_trunc_s_f64 = 0xaa, |
| 232 | + i32_trunc_u_f64 = 0xab, |
| 233 | + i64_extend_s_i32 = 0xac, |
| 234 | + i64_extend_u_i32 = 0xad, |
| 235 | + i64_trunc_s_f32 = 0xae, |
| 236 | + i64_trunc_u_f32 = 0xaf, |
| 237 | + i64_trunc_s_f64 = 0xb0, |
| 238 | + i64_trunc_u_f64 = 0xb1, |
| 239 | + f32_convert_s_i32 = 0xb2, |
| 240 | + f32_convert_u_i32 = 0xb3, |
| 241 | + f32_convert_s_i64 = 0xb4, |
| 242 | + f32_convert_u_i64 = 0xb5, |
| 243 | + f32_demote_f64 = 0xb6, |
| 244 | + f64_convert_s_i32 = 0xb7, |
| 245 | + f64_convert_u_i32 = 0xb8, |
| 246 | + f64_convert_s_i64 = 0xb9, |
| 247 | + f64_convert_u_i64 = 0xba, |
| 248 | + f64_promote_f32 = 0xbb, |
| 249 | + i32_reinterpret_f32 = 0xbc, |
| 250 | + i64_reinterpret_f64 = 0xbd, |
| 251 | + f32_reinterpret_i32 = 0xbe, |
| 252 | + f64_reinterpret_i64 = 0xbf |
| 253 | +} |
| 254 | + |
| 255 | +enum ReaderState { |
| 256 | + HEADER |
| 257 | +} |
| 258 | + |
| 259 | +/** WebAssembly binary reader. */ |
| 260 | +export class Reader { // TODO |
| 261 | + |
| 262 | + /** Buffer being read. */ |
| 263 | + private buffer: Uint8Array; |
| 264 | + /** Current read offset. */ |
| 265 | + private offset: u32; |
| 266 | + /** Total length. */ |
| 267 | + private length: u32; |
| 268 | + /** Current state. */ |
| 269 | + private state: ReaderState; |
| 270 | + |
| 271 | + /** Constructs a new binary reader. */ |
| 272 | + constructor(totalLength: u32, initialChunk: Uint8Array) { |
| 273 | + this.buffer = initialChunk; |
| 274 | + this.offset = 0; |
| 275 | + this.length = totalLength; |
| 276 | + this.state = ReaderState.HEADER; |
| 277 | + } |
| 278 | + |
| 279 | + /** Provides a chunk of data. */ |
| 280 | + next(chunk: Uint8Array): void { |
| 281 | + if (!chunk.length) return; |
| 282 | + // var current = this.buffer; |
| 283 | + // var offset = this.offset; |
| 284 | + // var buffer = new Uint8Array((current.length - offset) + chunk.length); |
| 285 | + // buffer.set(current.subarray(offset), 0); |
| 286 | + // buffer.set(chunk, (current.length - offset)); |
| 287 | + // this.buffer = buffer; |
| 288 | + // this.length -= this.offset; |
| 289 | + // this.offset = 0; |
| 290 | + unreachable(); |
| 291 | + } |
| 292 | + |
| 293 | + finish(): void { |
| 294 | + unreachable(); |
| 295 | + } |
| 296 | +} |
0 commit comments