wasm.py 2.68 KB
Newer Older
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
#!/usr/bin/env python

# Copyright 2019 the V8 project authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be found
# in the LICENSE file.

import io
import math
import struct
import sys

CUSTOM_SECTION_ID = 0
FUNCTION_SECTION_ID = 3

def peek_uint8(fin):
  bs = fin.peek(1)[:1]
  if len(bs) != 1:
    return None, bs
  return ord(bs[0]), bs

def read_uint8(fin):
  value, bs = peek_uint8(fin)
  fin.read(len(bs))
  return value, bs

def peek_uint32(fin):
  bs = fin.peek(4)[:4]
  if len(bs) != 4:
    return None, bs
  return ord(bs[0]) | ord(bs[1]) << 8 | ord(bs[2]) << 16 | ord(bs[3]) << 24, bs

def read_uint32(fin):
  value, bs = peek_uint32(fin)
  fin.read(len(bs))
  return value, bs

def peek_varuintN(fin):
  value = 0
  shift = 0
  n = 1
  while True:
    bs = fin.peek(n)[:n]
    if len(bs) < n:
      return None, bs
    b = ord(bs[-1])
    value |= (b & 0x7F) << shift;
    if (b & 0x80) == 0x00:
      return value, bs
    shift += 7;
    n += 1

def read_varuintN(fin):
  value, bs = peek_varuintN(fin)
  fin.read(len(bs))
  return value, bs

def to_varuintN(value):
  bs = ""
  while True:
    b = value & 0x7F
    value >>= 7
    if (value != 0x00):
      b |= 0x80
    bs += chr(b)
    if value == 0x00:
      return bs

def write_varuintN(value, fout):
  bs = to_varuintN(value)
  fout.write(bs)
  return bs

def peek_magic_number(fin, expected_magic_number=0x6d736100):
  magic_number, bs = peek_uint32(fin)
  assert magic_number == expected_magic_number, "unexpected magic number"
  return magic_number, bs

def read_magic_number(fin, expected_magic_number=0x6d736100):
  magic_number, bs = peek_magic_number(fin, expected_magic_number)
  fin.read(len(bs))
  return magic_number, bs

def peek_version(fin, expected_version=1):
  version, bs = peek_uint32(fin)
  assert version == expected_version, "unexpected version"
  return version, bs

def read_version(fin, expected_version=1):
  version, bs = peek_version(fin, expected_version)
  fin.read(len(bs))
  return version, bs

def write_custom_section(fout, section_name_bs, payload_bs):
  section_name_length_bs = to_varuintN(len(section_name_bs))
  payload_length_bs = to_varuintN(len(section_name_bs) \
      + len(section_name_length_bs) + len(payload_bs))
  section_id_bs = to_varuintN(CUSTOM_SECTION_ID)
  fout.write(section_id_bs)
  fout.write(payload_length_bs)
  fout.write(section_name_length_bs)
  fout.write(section_name_bs)
  fout.write(payload_bs)

def write_compilation_hints_section(fout, hints_bs):
  num_compilation_hints_bs = to_varuintN(len(hints_bs))
  section_name_bs = b"compilationHints"
  payload_bs = num_compilation_hints_bs + hints_bs
  write_custom_section(fout, section_name_bs, payload_bs)