File: gen_many_js_functions.py

package info (click to toggle)
emscripten 3.1.6~dfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 114,112 kB
  • sloc: ansic: 583,052; cpp: 391,943; javascript: 79,361; python: 54,180; sh: 49,997; pascal: 4,658; makefile: 3,426; asm: 2,191; lisp: 1,869; ruby: 488; cs: 142
file content (47 lines) | stat: -rw-r--r-- 1,933 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/env python3

# Copyright 2018 The Emscripten Authors.  All rights reserved.
# Emscripten is available under two separate licenses, the MIT license and the
# University of Illinois/NCSA Open Source License.  Both these licenses can be
# found in the LICENSE file.

import sys

NUM_FUNCS_TO_GENERATE = 1000


def func_name(i):
  return 'thisIsAFunctionWithVeryLongFunctionNameThatWouldBeGreatToBeMinifiedWhenImportingToAsmJsOrWasmSideCodeToCallOtherwiseCodeSizesWillBeLargeAndNetworkTransfersBecomeVerySlowThatUsersWillGoAwayAndVisitSomeOtherSiteInsteadAndThenWebAssemblyDeveloperIsSadOrEvenWorseNobodyNoticesButInternetPipesWillGetMoreCongestedWhichContributesToGlobalWarmingAndThenEveryoneElseWillBeSadAsWellEspeciallyThePolarBearsAndPenguinsJustThinkAboutThePenguins' + str(i + 1)


def generate_js_library_with_lots_of_functions(out_file):
  with open(out_file, 'w') as f:
    f.write('var FunctionsLibrary = {\n')

    for i in range(NUM_FUNCS_TO_GENERATE):
      f.write('  ' + func_name(i) + ': function() { return ' + str(i + 1) + '; },\n')

    f.write('}\n')
    f.write('mergeInto(LibraryManager.library, FunctionsLibrary);\n')


def generate_c_program_that_calls_js_library_with_lots_of_functions(out_file):
  with open(out_file, 'w') as f:
    f.write('#include <stdio.h>\n\n')

    for i in range(NUM_FUNCS_TO_GENERATE):
      f.write('int ' + func_name(i) + '(void);\n')

    f.write('\nint main() {\n')
    f.write('  int sum = 0;\n')

    for i in range(NUM_FUNCS_TO_GENERATE):
      f.write('  sum += ' + func_name(i) + '();\n')

    f.write('\n  printf("Sum of numbers from 1 to ' + str(NUM_FUNCS_TO_GENERATE) + ': %d (expected ' + str(int((NUM_FUNCS_TO_GENERATE * (NUM_FUNCS_TO_GENERATE + 1)) / 2)) + ')\\n", sum);\n')
    f.write('}\n')


if __name__ == '__main__':
  generate_js_library_with_lots_of_functions(sys.argv[1])
  generate_c_program_that_calls_js_library_with_lots_of_functions(sys.argv[2])