Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/wapython
Path: blob/main/core/dylink/src/wasm-export.ts
1067 views
1
/*
2
Given a list of names of functions and symbols, generate C code that define functions
3
that when called give a pointer to each of these.
4
5
For functions, this ensures that these named functions are all placed in the WASM
6
function table, which makes using them many orders of magnitude faster.
7
8
For symbols, it ensures that we have access to them at all (and quickly) so that
9
the GOTMemHandler (global offset table memory handler) can tell dynamic libraries
10
where the symbols are in memory.
11
*/
12
13
const MACRO = `
14
#ifndef WASM_EXPORT
15
#define WASM_EXPORT(x,y) __attribute__((visibility("default"))) void* __WASM_EXPORT__##x() { return &(y);}
16
#endif
17
`;
18
19
export default function wasmExport(names: string[]): string {
20
const v = Array.from(new Set(names)).sort();
21
return `${MACRO}\n
22
${v
23
.filter((func) => func.trim())
24
.map((func) => `WASM_EXPORT(${func},${func})`)
25
.join("\n")}`;
26
}
27
28
export function alias(name, value) {
29
return `WASM_EXPORT(${name},${value})\n`;
30
}
31
32