forked from ovr/StaticScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpp.mangler.ts
More file actions
41 lines (34 loc) · 1.39 KB
/
cpp.mangler.ts
File metadata and controls
41 lines (34 loc) · 1.39 KB
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
import * as ts from 'typescript';
function mangleParameters(parameters: ts.NodeArray<ts.ParameterDeclaration>): string {
if (parameters.length > 0) {
return parameters.map(
(parameter) => {
if (parameter.type) {
switch (parameter.type.kind) {
case ts.SyntaxKind.NumberKeyword:
return 'd';
case ts.SyntaxKind.StringKeyword:
return 'PKc';
case ts.SyntaxKind.BooleanKeyword:
return 'b';
default:
throw new Error(
`Unsupported mangling parameter type: ${parameter.type.kind}`
);
}
}
throw new Error('Unsupported mangling without type');
}
).join('');
}
return 'v';
}
export class CPPMangler {
static getMethodName(clazz: string, method: string, parameters: ts.NodeArray<ts.ParameterDeclaration>): string {
const name = clazz + '__' + method;
return '_Z' + name.length + name + mangleParameters(parameters);
}
static getFunctionName(name: string, parameters: ts.NodeArray<ts.ParameterDeclaration>): string {
return '_Z' + name.length + name + mangleParameters(parameters);
}
}