File: wasm.py

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (103 lines) | stat: -rw-r--r-- 2,694 bytes parent folder | download | duplicates (13)
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
#!/usr/bin/env python3

# 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.

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)