A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
"""Native wrappers for the engine's binding ENTRY POINTS.
The component half of the boundary has been generated from one reflection source
for a while (``native_bindings.py`` + ``embind.py``). The function half was not:
every entry point the SDK calls — ``renderer_begin``, ``renderer_submitAll``,
``uiLayout_update``, … — was declared once in C++ and then bound BY HAND, once in
embind for the web and once more in the native host. Which is why anything JS
drives (the frame, the UI layout, tilemaps, particles) worked on the web and had
to be rewritten in C++ to work on a device.
This generator removes the second hand-written copy. It reads the declarations in
``bindings/*.hpp`` — the same ones embind registers — and emits a wrapper per
function that marshals the arguments and calls it, written against the host's
engine surface (esjs.hpp) so it is the same whichever JS engine the host embeds. The BODY is shared: the
binding TU itself compiles natively (see cmake/ESEngineSources.cmake), so both
platforms run the same implementation; only the registration differs.
The mapping is small because the declarations are:
engine singletons ``ecs::Registry&`` / ``resource::ResourceManager&``: the
host has exactly one of each, so they are implicit and
consume no JS argument (see ``_IMPLICIT_REFS``)
scalars u32/i32/f32/f64/bool/int/float/double
``uintptr_t`` ptr an offset into the host's heap arena — the same thing it is
on the web, where the heap is wasm's linear memory (see
native/host/heap.hpp). A JS buffer handed straight across is
still accepted and copied in. Either way the binding's own
BoundarySpan validation runs, now against a real region.
strings ``const std::string&`` / ``const char*`` in, and either out:
a JS string, copied for the call. Not bulk data, so no heap
returns void / integer / float / bool / string, or a pointer WITH an
``@heapreturn`` byte count
Anything else — ``emscripten::val`` results, a typed pointer, a pointer return with
no ``@heapreturn`` — is SKIPPED and reported, never silently dropped: the skip list
is what tells you which entry points still need a hand-written binding.
"""
import re
from typing import List, NamedTuple, Optional, Tuple
# What the wrapper can marshal, and how each maps to a JS read.
_INT_TYPES = {'i8', 'i16', 'i32', 'i64', 'u8', 'u16', 'u32', 'u64',
'int', 'int32_t', 'uint32_t', 'size_t', 'long'}
_FLOAT_TYPES = {'f32', 'f64', 'float', 'double'}
_BOOL_TYPES = {'bool'}
_STRING = 'std::string'
# A C string, in or out. The module surfaces written as plain C use these where the
# C++ ones use std::string; both cross the boundary as a JS string, and neither needs
# the heap — a name or an animation id is not bulk data.
_CSTR = 'char*'
_VOID = 'void'
# A pointer-returning entry point hands back memory the MODULE owns (a static
# buffer, a vector's storage), which is not in the heap JS can read. The
# declaration says how many bytes it covers and the wrapper copies them into a
# stable heap slot, returning that offset — so the SDK reads the result exactly as
# it reads a wasm one, and nothing about the buffer's extent is guessed:
#
# // @heapreturn physics_getDynamicBodyCount() * PHYSICS_BODY_STRIDE_BYTES
# uintptr_t physics_getDynamicBodyTransforms();
#
# The expression is C++, evaluated in the generated TU right after the call — at
# GLOBAL scope, so any engine name in it needs qualifying (`esengine::f32`).
_HEAP_RETURN = re.compile(r'@heapreturn\s+(?P<expr>.+?)\s*$', re.MULTILINE)
# Reference parameters the host answers for itself: it runs exactly one registry
# and one resource manager, so these consume no JS argument and the wrapper passes
# the shim's accessor. Everything else taken by reference is a genuine object the
# boundary cannot marshal, and is skipped.
_IMPLICIT_REFS = {
'ecs::Registry&': 'esn_reg()',
'resource::ResourceManager&': 'esn_rm()',
}
# How a declaration spells each of them, matched on the type's last segment.
_IMPLICIT_SUFFIXES = {
'Registry': 'ecs::Registry&',
'ResourceManager': 'resource::ResourceManager&',
}
# `void name(args);` on one or more lines, inside the header's namespace. The return
# type may be const-qualified and may be a pointer (`const char* spine_getLastError()`)
# — without both, such a declaration matched nothing at all and was dropped without
# even reaching the skip list, which is the one thing this generator must never do.
_DECL = re.compile(
r'^(?P<ret>(?:const\s+)?[A-Za-z_][\w:]*(?:\s*\*)?)\s+(?P<name>[A-Za-z_]\w*)\s*\((?P<args>[^;{]*)\)\s*;',
re.MULTILINE | re.DOTALL,
)
class Param(NamedTuple):
cpp_type: str
name: str
class Function(NamedTuple):
name: str
ret: str
params: List[Param]
# The feature gates the declaration sits under (ES_ENABLE_SPINE, …). The
# wrapper carries them verbatim, so the generated TU tracks whatever the
# build enables instead of assuming a fixed feature set.
guards: Tuple[str, ...] = ()
# The C++ byte-count expression from an `@heapreturn` annotation, for an entry
# point that returns module-owned memory (see _HEAP_RETURN).
heap_return: Optional[str] = None
# C entry points (`extern "C"`) are not in namespace esengine, so the wrapper
# calls them unqualified.
extern_c: bool = False
# The declaration as the header spells it, so an alias can be declared with the
# exact same signature (see Alias).
decl: str = ''
class Alias(NamedTuple):
"""A second copy of a C surface, under another prefix.
Two implementations of ONE header can share a binary only if their symbols
differ: the 3D physics surface is answered by Jolt as ``physics3d_*`` and by
PhysX as ``physx3d_*`` (its TUs compiled with the rename header this emits).
The aliased wrappers are derived from the same declarations, so the second
backend cannot drift from the first — it is the same list under a new name, in
a TU of its own (generate_aliases) that is linked wherever the backend is.
"""
src: str # prefix in the header, e.g. 'physics3d_'
dst: str # prefix of the second implementation, e.g. 'physx3d_'
gate: str # the define the build sets where it links it, e.g. 'ES_ENABLE_PHYSX'
@staticmethod
def parse(spec: str) -> 'Alias':
# 'physics3d_=physx3d_:ES_ENABLE_PHYSX'
names, _, gate = spec.partition(':')
src, _, dst = names.partition('=')
if not src or not dst or not gate:
raise ValueError(f'alias "{spec}" is not <src>=<dst>:<GATE>')
return Alias(src, dst, gate)
def rename(self, text: str) -> str:
return re.sub(r'\b' + re.escape(self.src) + r'(?=\w)', self.dst, text)
class Skipped(NamedTuple):
name: str
reason: str
def _clean(t: str) -> str:
return ' '.join(t.replace('const', '').split()).strip()
def parse_header(text: str) -> Tuple[List[Function], List[Skipped]]:
"""Parse the declarations of one bindings header, in file order.
Declarations under an ``#ifdef __EMSCRIPTEN__`` are skipped outright: those
are the web-only entry points (they return an ``emscripten::val``), and the
native build does not even compile them.
"""
functions: List[Function] = []
skipped: List[Skipped] = []
# Walk the header keeping the preprocessor stack: __EMSCRIPTEN__ regions are
# dropped outright (web-only entry points the native build does not compile),
# every other gate is remembered and re-emitted around the wrapper.
kept: List[str] = []
guards_per_line: List[Tuple[str, ...]] = []
stack: List[Optional[str]] = [] # None = a web-only region being dropped
for line in text.splitlines():
token = line.strip()
if token.startswith('#ifdef ') or token.startswith('#ifndef ') or token.startswith('#if '):
name = token.split(None, 1)[1].strip() if ' ' in token else ''
stack.append(None if name == '__EMSCRIPTEN__' else name)
continue
if token.startswith('#endif'):
if stack:
stack.pop()
continue
if token.startswith('#else') or token.startswith('#elif'):
# An #else of a dropped region is kept-but-ungated, and vice versa;
# the bindings headers do not use it, so treat it as a plain region.
if stack:
stack[-1] = stack[-1] and f'!{stack[-1]}'
continue
if any(g is None for g in stack):
continue
kept.append(line)
guards_per_line.append(tuple(g for g in stack if g))
body = '\n'.join(kept)
# Offset → line index, so a match can be attributed to its guard stack.
line_starts: List[int] = []
offset = 0
for line in kept:
line_starts.append(offset)
offset += len(line) + 1
def guards_at(pos: int) -> Tuple[str, ...]:
lo, hi = 0, len(line_starts) - 1
while lo < hi:
mid = (lo + hi + 1) // 2
if line_starts[mid] <= pos:
lo = mid
else:
hi = mid - 1
return guards_per_line[lo] if guards_per_line else ()
# A header that opens `extern "C"` declares C entry points throughout (the
# standalone modules — physics, spine, video — are written that way), so their
# wrappers must call them unqualified.
extern_c = 'extern "C"' in body
for match in _DECL.finditer(body):
ret = _clean(match.group('ret'))
name = match.group('name')
if name in {'if', 'for', 'while', 'switch', 'return', 'namespace', 'class'}:
continue
heap_return = _heap_return_before(body, match.start())
params, reason = _parse_params(match.group('args'))
if reason is None and not _returnable(ret, heap_return):
reason = (f'returns {ret}; annotate it with `@heapreturn <bytes>` if it hands '
f'back module memory') if ret == 'uintptr_t' else f'returns {ret}'
if reason is not None:
skipped.append(Skipped(name, reason))
continue
functions.append(Function(name, ret, params, guards_at(match.start()),
heap_return, extern_c, match.group(0)))
return functions, skipped
def _heap_return_before(body: str, pos: int) -> Optional[str]:
"""The `@heapreturn` expression annotating the declaration at @p pos, if any.
Read from the comment block immediately above it — the annotation belongs next
to the declaration it describes, not in a table somewhere else.
"""
head = body[:pos].rstrip()
lines: List[str] = []
for line in reversed(head.splitlines()):
token = line.strip()
if not token.startswith('//') and not token.startswith('*') and not token.startswith('/*'):
break
lines.append(token)
match = _HEAP_RETURN.search('\n'.join(lines))
return match.group('expr').strip() if match else None
def _parse_params(args: str) -> Tuple[List[Param], Optional[str]]:
params: List[Param] = []
args = args.strip()
if not args or args == 'void':
return params, None
for raw in args.split(','):
decl = _clean(raw)
if not decl:
continue
parts = decl.replace('&', ' & ').split()
# `ecs::Registry ®istry` / `resource::ResourceManager &rm` → the host's
# one of each, no JS argument.
implicit = next((t for suffix, t in _IMPLICIT_SUFFIXES.items()
if parts[0].endswith(suffix)), None) if '&' in decl else None
if implicit:
params.append(Param(implicit, parts[-1]))
continue
# `const std::string&` — a JS string, read as UTF-8 for the call's duration.
if parts[0] == _STRING:
params.append(Param(_STRING, parts[-1]))
continue
# `const char*` — the same, handed over as a pointer.
if decl.replace(' ', '').startswith('char*'):
params.append(Param(_CSTR, parts[-1]))
continue
if '&' in decl or '*' in decl:
return params, f'takes {decl}'
cpp_type = ' '.join(parts[:-1]) if len(parts) > 1 else parts[0]
name = parts[-1] if len(parts) > 1 else f'arg{len(params)}'
cpp_type = _clean(cpp_type)
if cpp_type not in _INT_TYPES | _FLOAT_TYPES | _BOOL_TYPES | {'uintptr_t'}:
return params, f'takes {cpp_type}'
params.append(Param(cpp_type, name))
return params, None
def _returnable(ret: str, heap_return: Optional[str] = None) -> bool:
if ret == 'uintptr_t':
# Only with an annotation saying how much memory it covers.
return heap_return is not None
return (ret in (_VOID, _STRING, _CSTR)
or ret in _INT_TYPES | _FLOAT_TYPES | _BOOL_TYPES)
# The engine's short scalar aliases live in namespace esengine; the plain C++
# spellings do not and must stay unqualified.
_PLAIN = {'int', 'long', 'float', 'double', 'bool', 'size_t', 'uintptr_t',
'int32_t', 'uint32_t'}
def _qualify(cpp_type: str) -> str:
return cpp_type if cpp_type in _PLAIN else f'esengine::{cpp_type}'
class NativeFunctionsGenerator:
"""Emit ``es_<name>`` native wrappers for parsed binding declarations."""
def __init__(self, headers: List[Tuple[str, str]], shim_header: str = 'esn_shim.hpp',
aliases: Optional[List[Alias]] = None):
# headers: (include path as the generated TU should spell it, source text)
self.headers = headers
self.shim_header = shim_header
self.aliases = aliases or []
self.skipped: List[Skipped] = []
self.emitted: List[str] = []
@staticmethod
def _js_name(name: str) -> str:
"""The global the wrapper is registered under.
`es_` marks the boundary surface, so a C name that already carries it keeps
it — the video module's entry points are spelled `es_video_open`, and
`es_es_video_open` would read as a mistake on both sides of the boundary.
"""
return name if name.startswith('es_') else f'es_{name}'
def _wrapper(self, fn: Function) -> List[str]:
js_params = [p for p in fn.params if p.cpp_type not in _IMPLICIT_REFS]
out = [f'#ifdef {g}' for g in fn.guards]
out.append(f'static esjs::Val {self._js_name(fn.name)}(const esjs::Args& a) {{')
if js_params:
out.append(f' if (a.size() < {len(js_params)}) return esjs::undefined();')
out.append(' (void)a;')
call_args: List[str] = []
# (local, js argument index) per buffer argument, so a copied-in buffer is
# released after the call — see esn_argRelease.
ptr_releases: List[Tuple[str, int]] = []
js_index = 0
for param in fn.params:
if param.cpp_type in _IMPLICIT_REFS:
call_args.append(_IMPLICIT_REFS[param.cpp_type])
continue
local = f'a{js_index}'
if param.cpp_type == 'uintptr_t':
# A heap offset, the same as it is on the web — the host's arena
# plays the part of wasm's linear memory (see host/heap.hpp). A JS
# buffer handed straight across is copied in and released after the
# call. The binding's own BoundarySpan check runs either way, and
# now against a real region.
out.append(f' const uint32_t {local}_off = esn_argOffset(a[{js_index}]);')
out.append(f' const uintptr_t {local} = esn_heapAddr({local}_off);')
ptr_releases.append((local, js_index))
elif param.cpp_type == _CSTR:
# Owned by the wrapper for the duration of the call. A missing or
# non-string argument reads as empty, matching how cwrap coerces one.
out.append(f' const esjs::Utf8 {local}_c(a[{js_index}]);')
out.append(f' const char* {local} = {local}_c.c_str();')
elif param.cpp_type == _STRING:
# A JS string, owned by the wrapper for the call. An absent or
# non-string argument reads as empty rather than aborting the call,
# matching how embind coerces one.
out.append(f' const std::string {local} = esjs::toString(a[{js_index}]);')
elif param.cpp_type in _BOOL_TYPES:
out.append(f' const bool {local} = esjs::toBool(a[{js_index}]);')
elif param.cpp_type in _FLOAT_TYPES:
cpp = _qualify(param.cpp_type)
out.append(f' const {cpp} {local} = static_cast<{cpp}>(esjs::toNumber(a[{js_index}]));')
else:
cpp = _qualify(param.cpp_type)
out.append(f' const {cpp} {local} = static_cast<{cpp}>(esjs::toInt64(a[{js_index}]));')
call_args.append(local)
js_index += 1
scope = '' if fn.extern_c else 'esengine::'
call = f'{scope}{fn.name}({", ".join(call_args)})'
releases = [f' esn_argRelease(a[{index}], {local}_off);'
for local, index in ptr_releases]
if fn.ret == _VOID:
out.append(f' {call};')
out.extend(releases)
out.append(' return esjs::undefined();')
else:
# Hold the result, release the scratch, then convert — a wrapper must
# not leak the copy on the returning paths.
if fn.ret in _BOOL_TYPES:
out.append(f' const bool result = {call};')
out.extend(releases)
out.append(' return esjs::boolean(result);')
elif fn.ret in _FLOAT_TYPES:
out.append(f' const double result = static_cast<double>({call});')
out.extend(releases)
out.append(' return esjs::number(result);')
elif fn.ret == _STRING:
out.append(f' const std::string result = {call};')
out.extend(releases)
out.append(' return esjs::string(result);')
elif fn.ret == _CSTR:
# The module owns the storage (a static scratch string), so the value
# is copied into a JS string here rather than published to the heap.
# A null answers as empty, which is what UTF8ToString(0) reads as.
out.append(f' const char* result = {call};')
out.extend(releases)
out.append(' return esjs::string(result ? result : "");')
elif fn.ret == 'uintptr_t':
# Module-owned memory: copy `@heapreturn` bytes into this entry
# point's own heap slot and answer the offset, so the SDK reads the
# result the same way it reads a wasm pointer. One slot per entry
# point, so two consecutive readbacks cannot clobber each other.
out.append(' static EsnSlot slot;')
out.append(f' const uintptr_t src = {call};')
out.append(f' const size_t bytes = static_cast<size_t>({fn.heap_return});')
out.extend(releases)
out.append(' return esjs::uint32(esn_publish(slot, '
'reinterpret_cast<const void*>(src), bytes));')
else:
out.append(f' const int64_t result = static_cast<int64_t>({call});')
out.extend(releases)
out.append(' return esjs::int64(result);')
out.append('}')
out.extend('#endif' for _ in fn.guards)
out.append('')
return out
# ---- the TS half -------------------------------------------------------
# The same declarations, as the object a native host answers them through.
# The SDK's plugins call these by the module's own names, so a plugin reaches
# whichever core is present without knowing which (see ecs/engineApi.ts).
_TS_RETURN = {'void': 'void', 'bool': 'boolean', _STRING: 'string', _CSTR: 'string'}
@staticmethod
def _ts_type(cpp: str) -> str:
if cpp in _BOOL_TYPES:
return 'boolean'
if cpp in (_STRING, _CSTR):
return 'string'
if cpp in _IMPLICIT_REFS:
return 'unknown'
return 'number'
def _ts_signature(self, fn: Function) -> str:
params = ', '.join(f'{p.name}: {self._ts_type(p.cpp_type)}' for p in fn.params)
ret = self._TS_RETURN.get(fn.ret, 'number')
return f'{fn.name}?({params}): {ret};'
def generate_ts(self) -> str:
"""The native engine API: every generated entry point, callable by the
name the wasm module exposes it under."""
functions = self._functions()
lines = [
'// SPDX-License-Identifier: Apache-2.0',
'// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team',
'//',
'// Auto-generated by EHT (native_functions) — DO NOT EDIT.',
'//',
'// The engine entry points a native (embedded-Dawn) host binds, named as the',
'// wasm module names them — so an SDK plugin calls `api.uiLayout_update(...)`',
'// and reaches whichever core is present. Generated from the SAME bindings',
'// headers embind registers from, so the two surfaces cannot drift.',
'//',
'// Every member is optional: a core answers what it compiles. A `…Ptr`',
'// argument is an offset into the heap the core marshals through — wasm linear',
'// memory on the web, the host arena on a device — so a caller writes it the',
'// same way on both (see ecs/nativeHeap.ts).',
'',
'export interface NativeEngineApi {',
]
for fn in functions:
lines.append(f' {self._ts_signature(fn)}')
lines.append('}')
lines.append('')
lines.extend([
'/** Build the API over a host scope (the native host global object; a plain object',
' * in tests). A name the host did not bind simply stays absent. */',
'export function createNativeEngineApi(',
' scope: Record<string, unknown> = globalThis as unknown as Record<string, unknown>,',
'): NativeEngineApi {',
' const api: Record<string, unknown> = {};',
' const bind = (name: string, host: string, dropFirst: boolean): void => {',
' const fn = scope[host];',
' if (typeof fn !== \'function\') return;',
' const call = fn as (...a: unknown[]) => unknown;',
' api[name] = dropFirst',
' ? (_implicit: unknown, ...args: unknown[]) => call(...args)',
' : (...args: unknown[]) => call(...args);',
' };',
])
for fn in functions:
takes_implicit = any(p.cpp_type in _IMPLICIT_REFS for p in fn.params)
lines.append(f" bind('{fn.name}', '{self._js_name(fn.name)}', "
f"{str(takes_implicit).lower()});")
lines.extend([
' return api as NativeEngineApi;',
'}',
'',
])
return '\n'.join(lines)
def _functions(self) -> List[Function]:
out: List[Function] = []
for _, text in self.headers:
parsed, _ = parse_header(text)
out.extend(parsed)
return sorted(out, key=lambda f: f.name)
def generate(self) -> str:
lines = [
'// Auto-generated by EHT (native_functions) — DO NOT EDIT.',
'// Native wrappers for the engine binding entry points embind also registers,',
'// from the SAME declarations. The bodies live in the binding TUs, which this',
'// build compiles too — one implementation, two registration layers.',
f'#include "{self.shim_header}"',
'',
'#include <cstdint>',
'#include <string>',
'#include <vector>',
'',
]
functions: List[Function] = []
for include, text in self.headers:
lines.append(f'#include "{include}"')
parsed, skipped = parse_header(text)
functions.extend(parsed)
self.skipped.extend(skipped)
lines.append('')
for fn in sorted(functions, key=lambda f: f.name):
self.emitted.append(fn.name)
lines.extend(self._wrapper(fn))
lines.append('void esn_register_functions(esjs::Val global) {')
for fn in sorted(functions, key=lambda f: f.name):
js_arity = len([p for p in fn.params if p.cpp_type not in _IMPLICIT_REFS])
lines.extend(f'#ifdef {g}' for g in fn.guards)
js = self._js_name(fn.name)
lines.append(f' esjs::setFunction(global, "{js}", {js}, {js_arity});')
lines.extend('#endif' for _ in fn.guards)
lines.append('}')
lines.append('')
return '\n'.join(lines)
@staticmethod
def alias_register_name(alias: 'Alias') -> str:
"""The function the alias TU registers its wrappers through:
``esn_register_functions_physx3d`` for the ``physx3d_`` copy."""
return f'esn_register_functions_{alias.dst.rstrip("_")}'
def generate_aliases(self) -> str:
"""The aliased copies as a TU of their own, with their own registration
function (see alias_register_name).
Its own TU because it is linked where the second implementation is: into
the host on a desktop, into a separate library on Android (so an app that
did not pick that implementation does not carry it). Nothing here is
gated — the TU is compiled only where its implementation is linked."""
lines = [
'// Auto-generated by EHT (native_functions) — DO NOT EDIT.',
'// A second implementation of a C surface, wrapped from the SAME declarations',
'// as the first under its own prefix, and registered by its own function.',
f'#include "{self.shim_header}"',
'',
'#include <cstdint>',
'#include <string>',
'#include <vector>',
'',
]
functions: List[Function] = []
for include, text in self.headers:
parsed, _ = parse_header(text)
functions.extend(parsed)
for alias in self.aliases:
copies = [fn for fn in functions if fn.extern_c and fn.name.startswith(alias.src)]
if not copies:
raise ValueError(f'alias {alias.src}->{alias.dst}: no extern "C" entry point '
f'starts with {alias.src}')
renamed = sorted((fn._replace(
name=alias.rename(fn.name),
heap_return=alias.rename(fn.heap_return) if fn.heap_return else None,
decl=alias.rename(fn.decl),
) for fn in copies), key=lambda f: f.name)
# Declared here: the shared header declares only the original names.
lines.append('extern "C" {')
lines.extend(fn.decl for fn in renamed)
lines.append('}')
lines.append('')
for fn in renamed:
self.emitted.append(fn.name)
lines.extend(self._wrapper(fn))
lines.append(f'void {self.alias_register_name(alias)}(esjs::Val global) {{')
for fn in renamed:
js_arity = len([p for p in fn.params if p.cpp_type not in _IMPLICIT_REFS])
lines.extend(f'#ifdef {g}' for g in fn.guards)
js = self._js_name(fn.name)
lines.append(f' esjs::setFunction(global, "{js}", {js}, {js_arity});')
lines.extend('#endif' for _ in fn.guards)
lines.append('}')
lines.append('')
return '\n'.join(lines)
def generate_alias_header(self) -> str:
"""``#define <src>name <dst>name`` for every C name an alias renames — what
the second implementation's TUs are compiled with (forced include), so the
header they share declares and defines the renamed symbols. Every name the
header declares, the skipped ones too: a symbol left unrenamed would clash
with the first implementation at link time."""
lines = [
'// Auto-generated by EHT (native_functions) — DO NOT EDIT.',
'// Force-included into a second implementation of a C surface, so its',
'// symbols do not collide with the first one linked beside it.',
'#pragma once',
'',
]
for alias in self.aliases:
names = set()
for _, text in self.headers:
names.update(re.findall(r'\b(' + re.escape(alias.src) + r'\w+)\s*\(', text))
for name in sorted(names):
lines.append(f'#define {name} {alias.rename(name)}')
lines.append('')
return '\n'.join(lines)