forked from AssemblyScript/assemblyscript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
241 lines (204 loc) · 7.36 KB
/
index.ts
File metadata and controls
241 lines (204 loc) · 7.36 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/**
* Low-level C-like compiler API.
* @module index
*//***/
import { Target, Feature } from "./common";
import { Compiler, Options } from "./compiler";
import { Decompiler } from "./decompiler";
import { IDLBuilder, TSDBuilder } from "./definitions";
import { DiagnosticMessage, DiagnosticCategory, formatDiagnosticMessage } from "./diagnostics";
import { Module } from "./module";
import { Program } from "./program";
// Options
/** Creates a new set of compiler options. */
export function newOptions(): Options {
return new Options();
}
/** Sets the `target` option. */
export function setTarget(options: Options, target: Target): void {
options.target = target;
}
/** Sets the `noAssert` option. */
export function setNoAssert(options: Options, noAssert: bool): void {
options.noAssert = noAssert;
}
/** Sets the `importMemory` option. */
export function setImportMemory(options: Options, importMemory: bool): void {
options.importMemory = importMemory;
}
/** Sets the `sharedMemory` option. */
export function setSharedMemory(options: Options, sharedMemory: i32): void {
options.sharedMemory = sharedMemory;
}
/** Sets the `importTable` option. */
export function setImportTable(options: Options, importTable: bool): void {
options.importTable = importTable;
}
/** Sets the `sourceMap` option. */
export function setSourceMap(options: Options, sourceMap: bool): void {
options.sourceMap = sourceMap;
}
/** Sets the `memoryBase` option. */
export function setMemoryBase(options: Options, memoryBase: u32): void {
options.memoryBase = memoryBase;
}
/** Sets a 'globalAliases' value. */
export function setGlobalAlias(options: Options, alias: string, name: string): void {
var globalAliases = options.globalAliases;
if (!globalAliases) options.globalAliases = globalAliases = new Map();
globalAliases.set(alias, name);
}
/** Sets the `explicitStart` option. */
export function setExplicitStart(options: Options, explicitStart: bool): void {
options.explicitStart = explicitStart;
}
/** Sets the `noUnsafe` option. */
export function setNoUnsafe(options: Options, noUnsafe: bool): void {
options.noUnsafe = noUnsafe;
}
/** Sign extension operations. */
export const FEATURE_SIGN_EXTENSION = Feature.SIGN_EXTENSION;
/** Mutable global imports and exports. */
export const FEATURE_MUTABLE_GLOBALS = Feature.MUTABLE_GLOBALS;
/** Non-trapping float to int conversion operations. */
export const FEATURE_NONTRAPPING_F2I = Feature.NONTRAPPING_F2I;
/** Bulk memory operations. */
export const FEATURE_BULK_MEMORY = Feature.BULK_MEMORY;
/** SIMD types and operations. */
export const FEATURE_SIMD = Feature.SIMD;
/** Threading and atomic operations. */
export const FEATURE_THREADS = Feature.THREADS;
/** Exception handling operations. */
export const FEATURE_EXCEPTION_HANDLING = Feature.EXCEPTION_HANDLING;
/** Tail call operations. */
export const FEATURE_TAIL_CALLS = Feature.TAIL_CALLS;
/** Reference types. */
export const FEATURE_REFERENCE_TYPES = Feature.REFERENCE_TYPES;
/** Enables a specific feature. */
export function enableFeature(options: Options, feature: Feature): void {
options.features |= feature;
}
/** Disables a specific feature. */
export function disableFeature(options: Options, feature: Feature): void {
options.features &= ~feature;
}
/** Gives the compiler a hint at the optimize levels that will be used later on. */
export function setOptimizeLevelHints(options: Options, optimizeLevel: i32, shrinkLevel: i32): void {
options.optimizeLevelHint = optimizeLevel;
options.shrinkLevelHint = shrinkLevel;
}
// Program
/** Creates a new Program. */
export function newProgram(options: Options): Program {
return new Program(options);
}
/** Obtains the next diagnostic message. Returns `null` once complete. */
export function nextDiagnostic(program: Program): DiagnosticMessage | null {
return program.diagnosticsOffset < program.diagnostics.length
? program.diagnostics[program.diagnosticsOffset++]
: null;
}
/** Obtains the source of the given file. */
export function getSource(program: Program, internalPath: string): string | null {
return program.getSource(internalPath);
}
/** Formats a diagnostic message to a string. */
export { formatDiagnosticMessage as formatDiagnostic };
/** Tests whether a diagnostic is informatory. */
export function isInfo(message: DiagnosticMessage): bool {
return message.category == DiagnosticCategory.INFO;
}
/** Tests whether a diagnostic is a warning. */
export function isWarning(message: DiagnosticMessage): bool {
return message.category == DiagnosticCategory.WARNING;
}
/** Tests whether a diagnostic is an error. */
export function isError(message: DiagnosticMessage): bool {
return message.category == DiagnosticCategory.ERROR;
}
// Parser
/** Parses a source file. If `parser` has been omitted a new one is created. */
export function parse(
/** Program reference. */
program: Program,
/** Source text of the file. */
text: string,
/** Normalized path of the file. */
path: string,
/** Whether this is an entry file. */
isEntry: bool = false
): void {
program.parser.parseFile(text, path, isEntry);
}
/** Obtains the next required file's path. Returns `null` once complete. */
export function nextFile(program: Program): string | null {
return program.parser.nextFile();
}
/** Obtains the path of the dependee of a given imported file. */
export function getDependee(program: Program, file: string): string | null {
return program.parser.getDependee(file);
}
// Compiler
/** Compiles the parsed sources to a module. */
export function compile(program: Program): Module {
program.parser.finish();
return new Compiler(program).compile();
}
/** Decompiles a module to its (low level) source. */
export function decompile(module: Module): string {
var decompiler = new Decompiler();
decompiler.decompile(module);
return decompiler.finish();
}
/** Builds WebIDL definitions for the specified program. */
export function buildIDL(program: Program): string {
return IDLBuilder.build(program);
}
/** Builds TypeScript definitions for the specified program. */
export function buildTSD(program: Program): string {
return TSDBuilder.build(program);
}
/** Builds a JSON file of a program's runtime type information. */
export function buildRTTI(program: Program): string {
var sb = new Array<string>();
sb.push("{\n \"names\": [\n");
for (let cls of program.managedClasses.values()) {
sb.push(" \"");
sb.push(cls.internalName);
sb.push("\",\n");
}
sb.push(" ],\n \"base\": [\n");
for (let cls of program.managedClasses.values()) {
let base = cls.base;
sb.push(" ");
sb.push(base ? base.id.toString() : "0");
sb.push(",\n");
}
sb.push(" ],\n \"flags\": [\n");
for (let cls of program.managedClasses.values()) {
sb.push(" ");
sb.push(cls.rttiFlags.toString());
sb.push(",\n");
}
sb.push(" ]\n}\n");
return sb.join("");
}
/** Prefix indicating a library file. */
export { LIBRARY_PREFIX } from "./common";
// Full API
export * from "./ast";
// export * from "./binary";
export * from "./common";
export * from "./compiler";
export * from "./decompiler";
export * from "./definitions";
export * from "./diagnosticMessages.generated";
export * from "./diagnostics";
export * from "./flow";
export * from "./module";
export * from "./parser";
export * from "./program";
export * from "./resolver";
export * from "./tokenizer";
export * from "./types";
export * from "./util/index";