MCPcopy Create free account
hub / github.com/emscripten-core/emscripten / create_tsd

Function create_tsd

tools/emscripten.py:679–721  ·  view source on GitHub ↗
(metadata, embind_tsd, bindgen_tsd)

Source from the content-addressed store, hash-verified

677
678
679def create_tsd(metadata, embind_tsd, bindgen_tsd):
680 out = '// TypeScript bindings for emscripten-generated code. Automatically generated at compile time.\n'
681 if settings.EXPORTED_RUNTIME_METHODS:
682 out += create_tsd_exported_runtime_methods(metadata)
683 # Manually generate definitions for any Wasm function exports.
684 out += 'interface WasmModule {\n'
685 for name, functype in metadata.function_exports.items():
686 mangled = asmjs_mangle(name)
687 should_export = settings.EXPORT_KEEPALIVE and mangled in settings.EXPORTED_FUNCTIONS
688 if not should_export:
689 continue
690 arguments = []
691 for index, type in enumerate(functype.params):
692 arguments.append(f"_{index}: {type_to_ts_type(type)}")
693 out += f' {mangled}({", ".join(arguments)}): '
694 assert len(functype.returns) <= 1, 'One return type only supported'
695 if functype.returns:
696 ret_ts_type = type_to_ts_type(functype.returns[0])
697 else:
698 ret_ts_type = 'void'
699 if settings.ASYNCIFY == 2 and any(fnmatch.fnmatch(name, pat) for pat in settings.ASYNCIFY_EXPORTS):
700 ret_ts_type = f'Promise<{ret_ts_type}>'
701 out += f'{ret_ts_type};\n'
702 out += '}\n'
703 out += f'\n{embind_tsd}'
704 # Combine all the various exports.
705 export_interfaces = 'WasmModule'
706 if settings.EXPORTED_RUNTIME_METHODS:
707 export_interfaces += ' & typeof RuntimeExports'
708 # Add in embind definitions.
709 if embind_tsd:
710 export_interfaces += ' & EmbindModule'
711 if settings.WASM_BINDGEN and bindgen_tsd:
712 for file_path in bindgen_tsd:
713 out += utils.read_file(file_path)
714 export_interfaces += ' & BindgenModule'
715 out += f'export type MainModule = {export_interfaces};\n'
716 if settings.MODULARIZE:
717 return_type = 'MainModule'
718 if settings.WASM_ASYNC_COMPILATION:
719 return_type = f'Promise<{return_type}>'
720 out += f'export default function MainModuleFactory (options?: unknown): {return_type};\n'
721 return out
722
723
724def create_asm_consts(metadata):

Callers

nothing calls this directly

Calls 5

asmjs_mangleFunction · 0.90
type_to_ts_typeFunction · 0.85
appendMethod · 0.80
joinMethod · 0.45

Tested by

no test coverage detected