summaryrefslogtreecommitdiff
path: root/scripts/spidermonkify.py
blob: 992e7d48dbc8d59707c3022a1227c643a0a26c21 (plain)
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
'''
A bunch of hackish fixups for testing of SpiderMonkey support. We should
get rid of these ASAP.
'''

import os
import sys
import math
import shutil
import subprocess

import emscripten

binaryen_root = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))

js_target = sys.argv[1]
wast_target = sys.argv[2]

wasm_target = wast_target[:-5] + '.wasm'

base_wast_target = os.path.basename(wast_target)
base_wasm_target = os.path.basename(wasm_target)


def fix(js, before, after):
  assert js.count(before) == 1
  return js.replace(before, after)

# fix up js
js = open(js_target).read()
# send a buffer into wasm methods, not a typed array
js = fix(js,
         'instance = Wasm.instantiateModule(binary, info)',
         'instance = Wasm.instantiateModule(binary.buffer, info)')
# use the wasm, not wast
js = js.replace('"' + base_wast_target + '"', '"' + base_wasm_target + '"')
js = js.replace("'" + base_wast_target + "'", "'" + base_wasm_target + "'")
open(js_target, 'w').write(js)
shutil.copyfile(wast_target + '.mappedGlobals', wasm_target + '.mappedGlobals')

# lower cases, spidermonkey has no support for them
temp = wast_target + '.temp'
subprocess.check_call([os.path.join(binaryen_root, 'bin', 'binaryen-shell'),
                       wast_target, '--lower-case', '--print'],
                      stdout=open(temp, 'w'))
shutil.copyfile(temp, wast_target)

# fix up wast
wast = open(wast_target).read()
# tableswitch => br_table
wast = wast.replace('(tableswitch', '(br_table')
# memory to page sizes
PAGE_SIZE = 64 * 1024
memory_start = wast.find('(memory') + 1
memory_end = wast.find(')', memory_start)
memory = wast[memory_start:memory_end]
parts = memory.split(' ')
parts[1] = str(int(math.ceil(float(parts[1]) / PAGE_SIZE)))
if len(parts) == 3:
  parts[2] = str(int(math.ceil(float(parts[2]) / PAGE_SIZE)))
wast = wast[:memory_start] + ' '.join(parts) + \
       wast[memory_end:memory_end + 1] + \
       ' (export "memory" memory) ' + wast[memory_end + 1:]
open(wast_target, 'w').write(wast)

# convert to binary using spidermonkey
subprocess.check_call(emscripten.shared.SPIDERMONKEY_ENGINE + ['-e',
  'os.file.writeTypedArrayToFile("%s", new Uint8Array(wasmTextToBinary(os.file.readFile("%s"))))' % (wasm_target, wast_target)
])