@5ive-tech/sdk 1.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +279 -0
- package/dist/FiveSDK.d.ts +336 -0
- package/dist/FiveSDK.js +395 -0
- package/dist/accounts/index.d.ts +254 -0
- package/dist/accounts/index.js +543 -0
- package/dist/assets/vm/dummy.file +0 -0
- package/dist/assets/vm/five_vm_wasm.d.ts +762 -0
- package/dist/assets/vm/five_vm_wasm.js +3754 -0
- package/dist/assets/vm/five_vm_wasm_bg.js +3307 -0
- package/dist/assets/vm/five_vm_wasm_bg.wasm +0 -0
- package/dist/assets/vm/five_vm_wasm_bg.wasm.d.ts +247 -0
- package/dist/assets/vm/package.json +11 -0
- package/dist/bin/gen-types.d.ts +2 -0
- package/dist/bin/gen-types.js +35 -0
- package/dist/compiler/BytecodeCompiler.d.ts +83 -0
- package/dist/compiler/BytecodeCompiler.js +379 -0
- package/dist/config/ConfigManager.d.ts +13 -0
- package/dist/config/ConfigManager.js +27 -0
- package/dist/config/ProgramIdResolver.d.ts +62 -0
- package/dist/config/ProgramIdResolver.js +104 -0
- package/dist/crypto/index.d.ts +211 -0
- package/dist/crypto/index.js +451 -0
- package/dist/encoding/ParameterEncoder.d.ts +31 -0
- package/dist/encoding/ParameterEncoder.js +278 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.js +28 -0
- package/dist/lib/bytecode-encoder.d.ts +62 -0
- package/dist/lib/bytecode-encoder.js +281 -0
- package/dist/logging/index.d.ts +9 -0
- package/dist/logging/index.js +10 -0
- package/dist/metadata/index.d.ts +213 -0
- package/dist/metadata/index.js +296 -0
- package/dist/modules/accounts.d.ts +60 -0
- package/dist/modules/accounts.js +275 -0
- package/dist/modules/deploy.d.ts +90 -0
- package/dist/modules/deploy.js +1118 -0
- package/dist/modules/execute.d.ts +90 -0
- package/dist/modules/execute.js +649 -0
- package/dist/modules/fees.d.ts +14 -0
- package/dist/modules/fees.js +112 -0
- package/dist/modules/namespaces.d.ts +39 -0
- package/dist/modules/namespaces.js +190 -0
- package/dist/modules/state-diff.d.ts +35 -0
- package/dist/modules/state-diff.js +342 -0
- package/dist/modules/vm-state.d.ts +7 -0
- package/dist/modules/vm-state.js +44 -0
- package/dist/program/AccountResolver.d.ts +67 -0
- package/dist/program/AccountResolver.js +134 -0
- package/dist/program/BorshSchemaGenerator.d.ts +8 -0
- package/dist/program/BorshSchemaGenerator.js +57 -0
- package/dist/program/FiveProgram.d.ts +144 -0
- package/dist/program/FiveProgram.js +282 -0
- package/dist/program/FunctionBuilder.d.ts +114 -0
- package/dist/program/FunctionBuilder.js +347 -0
- package/dist/program/ProgramAccount.d.ts +38 -0
- package/dist/program/ProgramAccount.js +170 -0
- package/dist/program/TypeGenerator.d.ts +90 -0
- package/dist/program/TypeGenerator.js +195 -0
- package/dist/program/index.d.ts +24 -0
- package/dist/program/index.js +21 -0
- package/dist/project/config.d.ts +5 -0
- package/dist/project/config.js +33 -0
- package/dist/project/toml.d.ts +6 -0
- package/dist/project/toml.js +43 -0
- package/dist/project/workspace.d.ts +160 -0
- package/dist/project/workspace.js +73 -0
- package/dist/testing/AccountMetaGenerator.d.ts +121 -0
- package/dist/testing/AccountMetaGenerator.js +261 -0
- package/dist/testing/AccountTestFixture.d.ts +211 -0
- package/dist/testing/AccountTestFixture.js +530 -0
- package/dist/testing/OnChainAccountManager.d.ts +81 -0
- package/dist/testing/OnChainAccountManager.js +260 -0
- package/dist/testing/StateSerializer.d.ts +65 -0
- package/dist/testing/StateSerializer.js +330 -0
- package/dist/testing/TestDiscovery.d.ts +79 -0
- package/dist/testing/TestDiscovery.js +274 -0
- package/dist/testing/TestRunner.d.ts +117 -0
- package/dist/testing/TestRunner.js +346 -0
- package/dist/testing/index.d.ts +14 -0
- package/dist/testing/index.js +13 -0
- package/dist/types.d.ts +356 -0
- package/dist/types.js +32 -0
- package/dist/utils/abi.d.ts +31 -0
- package/dist/utils/abi.js +92 -0
- package/dist/utils/transaction.d.ts +5 -0
- package/dist/utils/transaction.js +48 -0
- package/dist/validation/InputValidator.d.ts +142 -0
- package/dist/validation/InputValidator.js +332 -0
- package/dist/validation/index.d.ts +4 -0
- package/dist/validation/index.js +4 -0
- package/dist/wasm/compiler/AbiLogic.d.ts +4 -0
- package/dist/wasm/compiler/AbiLogic.js +37 -0
- package/dist/wasm/compiler/AnalysisLogic.d.ts +6 -0
- package/dist/wasm/compiler/AnalysisLogic.js +61 -0
- package/dist/wasm/compiler/CompilationLogic.d.ts +10 -0
- package/dist/wasm/compiler/CompilationLogic.js +431 -0
- package/dist/wasm/compiler/FiveCompiler.d.ts +48 -0
- package/dist/wasm/compiler/FiveCompiler.js +183 -0
- package/dist/wasm/compiler/InfoLogic.d.ts +6 -0
- package/dist/wasm/compiler/InfoLogic.js +24 -0
- package/dist/wasm/compiler/OptimizationLogic.d.ts +2 -0
- package/dist/wasm/compiler/OptimizationLogic.js +13 -0
- package/dist/wasm/compiler/ValidationLogic.d.ts +7 -0
- package/dist/wasm/compiler/ValidationLogic.js +26 -0
- package/dist/wasm/compiler/index.d.ts +2 -0
- package/dist/wasm/compiler/index.js +2 -0
- package/dist/wasm/compiler/types.d.ts +8 -0
- package/dist/wasm/compiler/types.js +1 -0
- package/dist/wasm/compiler/utils.d.ts +8 -0
- package/dist/wasm/compiler/utils.js +75 -0
- package/dist/wasm/index.d.ts +9 -0
- package/dist/wasm/index.js +12 -0
- package/dist/wasm/instance.d.ts +1 -0
- package/dist/wasm/instance.js +26 -0
- package/dist/wasm/loader.d.ts +7 -0
- package/dist/wasm/loader.js +112 -0
- package/dist/wasm/vm.d.ts +33 -0
- package/dist/wasm/vm.js +250 -0
- package/package.json +59 -0
|
@@ -0,0 +1,431 @@
|
|
|
1
|
+
import { readFile, writeFile } from "fs/promises";
|
|
2
|
+
import { createCompilerError, extractMetrics, extractAbi } from "./utils.js";
|
|
3
|
+
export async function compile(ctx, source, options) {
|
|
4
|
+
if (!ctx.compiler) {
|
|
5
|
+
throw createCompilerError("Compiler not initialized");
|
|
6
|
+
}
|
|
7
|
+
const startTime = Date.now();
|
|
8
|
+
const metricsFormat = options?.metricsFormat || "json";
|
|
9
|
+
try {
|
|
10
|
+
let result;
|
|
11
|
+
let WasmCompilationOptions = ctx.WasmCompilationOptions;
|
|
12
|
+
// Use unified compilation method (module already loaded in initialize)
|
|
13
|
+
if (!WasmCompilationOptions && ctx.wasmModuleRef) {
|
|
14
|
+
WasmCompilationOptions = ctx.wasmModuleRef.WasmCompilationOptions;
|
|
15
|
+
}
|
|
16
|
+
const includeMetrics = options?.includeMetrics || Boolean(options?.metricsOutput) || Boolean(options?.metricsFormat);
|
|
17
|
+
const errorFormat = options?.errorFormat || "terminal";
|
|
18
|
+
const comprehensiveMetrics = options?.comprehensiveMetrics || Boolean(options?.metricsOutput);
|
|
19
|
+
const compilationOptions = new WasmCompilationOptions()
|
|
20
|
+
.with_mode(options?.target || "deployment")
|
|
21
|
+
.with_optimization_level(options?.optimizationLevel || "production")
|
|
22
|
+
.with_v2_preview(true)
|
|
23
|
+
.with_constraint_cache(false)
|
|
24
|
+
.with_enhanced_errors(true)
|
|
25
|
+
.with_metrics(includeMetrics)
|
|
26
|
+
.with_comprehensive_metrics(comprehensiveMetrics)
|
|
27
|
+
.with_metrics_format(metricsFormat)
|
|
28
|
+
.with_error_format(errorFormat);
|
|
29
|
+
result = ctx.compiler.compile(source, compilationOptions);
|
|
30
|
+
if (result.compiler_errors && result.compiler_errors.length > 0) {
|
|
31
|
+
ctx.logger.debug(`WASM returned ${result.compiler_errors.length} compiler errors`);
|
|
32
|
+
for (const error of result.compiler_errors) {
|
|
33
|
+
ctx.logger.debug(`Compiler error details:`, error);
|
|
34
|
+
try {
|
|
35
|
+
const message = error.message ? error.message : "No message";
|
|
36
|
+
const code = error.code ? error.code : "No code";
|
|
37
|
+
const severity = error.severity ? error.severity : "Unknown";
|
|
38
|
+
const category = error.category ? error.category : "Unknown";
|
|
39
|
+
ctx.logger.error(`Detailed error: [${code}] ${severity} - ${message}`);
|
|
40
|
+
ctx.logger.error(`Category: ${category}`);
|
|
41
|
+
if (error.location) {
|
|
42
|
+
const location = error.location;
|
|
43
|
+
ctx.logger.error(`Location: line ${location.line}, column ${location.column}`);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
catch (e) {
|
|
47
|
+
ctx.logger.debug(`Failed to extract error details:`, e);
|
|
48
|
+
ctx.logger.debug(`Raw error object:`, error);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
const metricsPayload = extractMetrics(result, metricsFormat);
|
|
53
|
+
if (result.success && result.bytecode) {
|
|
54
|
+
return {
|
|
55
|
+
success: true,
|
|
56
|
+
bytecode: result.bytecode,
|
|
57
|
+
abi: extractAbi(result, ctx.logger),
|
|
58
|
+
metadata: result.metadata,
|
|
59
|
+
metrics: metricsPayload,
|
|
60
|
+
metricsReport: metricsPayload,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
return {
|
|
65
|
+
success: false,
|
|
66
|
+
errors: result.compiler_errors || [],
|
|
67
|
+
metadata: result.metadata,
|
|
68
|
+
metrics: metricsPayload,
|
|
69
|
+
metricsReport: metricsPayload,
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
catch (error) {
|
|
74
|
+
if (options && options.debug) { // Check if debug option is available, though not strictly in CompilationOptions type used here as any
|
|
75
|
+
// Actually 'options' is any in signature
|
|
76
|
+
}
|
|
77
|
+
// Logic from original code had specialized try/catch for WASM error inside compileFile
|
|
78
|
+
// but here in compile it just throws
|
|
79
|
+
throw createCompilerError(`Compilation error: ${error instanceof Error ? error.message : "Unknown error"}`, error);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
export async function compileModules(ctx, mainSource, modules, options) {
|
|
83
|
+
if (!ctx.compiler) {
|
|
84
|
+
throw createCompilerError("Compiler not initialized");
|
|
85
|
+
}
|
|
86
|
+
const startTime = Date.now();
|
|
87
|
+
try {
|
|
88
|
+
let WasmCompilationOptions = ctx.WasmCompilationOptions;
|
|
89
|
+
if (!WasmCompilationOptions && ctx.wasmModuleRef) {
|
|
90
|
+
WasmCompilationOptions = ctx.wasmModuleRef.WasmCompilationOptions;
|
|
91
|
+
}
|
|
92
|
+
const metricsFormat = options?.metricsFormat || "json";
|
|
93
|
+
const includeMetrics = options?.includeMetrics || Boolean(options?.metricsOutput);
|
|
94
|
+
const errorFormat = options?.errorFormat || "terminal";
|
|
95
|
+
const comprehensiveMetrics = options?.comprehensiveMetrics || false;
|
|
96
|
+
const compilationOptions = new WasmCompilationOptions()
|
|
97
|
+
.with_mode(options?.target || "deployment")
|
|
98
|
+
.with_optimization_level(options?.optimizationLevel || "production")
|
|
99
|
+
.with_v2_preview(true)
|
|
100
|
+
.with_constraint_cache(false)
|
|
101
|
+
.with_enhanced_errors(true)
|
|
102
|
+
.with_metrics(includeMetrics)
|
|
103
|
+
.with_comprehensive_metrics(comprehensiveMetrics)
|
|
104
|
+
.with_metrics_format(metricsFormat)
|
|
105
|
+
.with_error_format(errorFormat);
|
|
106
|
+
const result = ctx.compiler.compile_multi(mainSource, modules, compilationOptions);
|
|
107
|
+
const metricsPayload = extractMetrics(result, metricsFormat);
|
|
108
|
+
if (result.success && result.bytecode) {
|
|
109
|
+
return {
|
|
110
|
+
success: true,
|
|
111
|
+
bytecode: result.bytecode,
|
|
112
|
+
abi: extractAbi(result, ctx.logger),
|
|
113
|
+
metadata: result.metadata,
|
|
114
|
+
metrics: metricsPayload,
|
|
115
|
+
metricsReport: metricsPayload,
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
else {
|
|
119
|
+
return {
|
|
120
|
+
success: false,
|
|
121
|
+
errors: result.compiler_errors || [],
|
|
122
|
+
metadata: result.metadata,
|
|
123
|
+
metrics: metricsPayload,
|
|
124
|
+
metricsReport: metricsPayload,
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
catch (error) {
|
|
129
|
+
throw createCompilerError(`Compilation error: ${error instanceof Error ? error.message : "Unknown error"}`, error);
|
|
130
|
+
}
|
|
131
|
+
finally {
|
|
132
|
+
const compilationTime = Date.now() - startTime;
|
|
133
|
+
ctx.logger.debug(`Multi-file compilation completed in ${compilationTime}ms`);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
export async function compileFile(ctx, options) {
|
|
137
|
+
if (!ctx.compiler) {
|
|
138
|
+
throw createCompilerError("Compiler not initialized");
|
|
139
|
+
}
|
|
140
|
+
const startTime = Date.now();
|
|
141
|
+
const metricsFormat = options.metricsFormat || "json";
|
|
142
|
+
try {
|
|
143
|
+
const sourceCode = await readFile(options.sourceFile, "utf8");
|
|
144
|
+
ctx.logger.debug(`Compiling source file: ${options.sourceFile}`);
|
|
145
|
+
let result;
|
|
146
|
+
try {
|
|
147
|
+
let WasmCompilationOptions = ctx.WasmCompilationOptions;
|
|
148
|
+
if (!WasmCompilationOptions && ctx.wasmModuleRef) {
|
|
149
|
+
WasmCompilationOptions = ctx.wasmModuleRef.WasmCompilationOptions;
|
|
150
|
+
}
|
|
151
|
+
const includeMetrics = options.includeMetrics || Boolean(options.metricsOutput) || Boolean(options.metricsFormat);
|
|
152
|
+
const errorFormat = options.errorFormat || "terminal";
|
|
153
|
+
const comprehensiveMetrics = options.comprehensiveMetrics || Boolean(options.metricsOutput);
|
|
154
|
+
const compilationOptions = new WasmCompilationOptions()
|
|
155
|
+
.with_mode(options.target || "deployment")
|
|
156
|
+
.with_optimization_level(options.optimizationLevel || "production")
|
|
157
|
+
.with_v2_preview(true)
|
|
158
|
+
.with_constraint_cache(options.enable_constraint_cache !== false)
|
|
159
|
+
.with_enhanced_errors(true)
|
|
160
|
+
.with_metrics(includeMetrics)
|
|
161
|
+
.with_comprehensive_metrics(comprehensiveMetrics)
|
|
162
|
+
.with_metrics_format(metricsFormat)
|
|
163
|
+
.with_error_format(errorFormat)
|
|
164
|
+
.with_source_file(options.sourceFile);
|
|
165
|
+
result = ctx.compiler.compile(sourceCode, compilationOptions);
|
|
166
|
+
if (result.compiler_errors && result.compiler_errors.length > 0) {
|
|
167
|
+
ctx.logger.debug(`WASM returned ${result.compiler_errors.length} compiler errors`);
|
|
168
|
+
for (const error of result.compiler_errors) {
|
|
169
|
+
ctx.logger.debug(`Compiler error details:`, error);
|
|
170
|
+
try {
|
|
171
|
+
const message = error.message ? error.message : "No message";
|
|
172
|
+
const code = error.code ? error.code : "No code";
|
|
173
|
+
const severity = error.severity ? error.severity : "Unknown";
|
|
174
|
+
const category = error.category ? error.category : "Unknown";
|
|
175
|
+
ctx.logger.error(`Detailed error: [${code}] ${severity} - ${message}`);
|
|
176
|
+
ctx.logger.error(`Category: ${category}`);
|
|
177
|
+
if (error.location) {
|
|
178
|
+
const location = error.location;
|
|
179
|
+
ctx.logger.error(`Location: line ${location.line}, column ${location.column}`);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
catch (e) {
|
|
183
|
+
ctx.logger.debug(`Failed to extract error details:`, e);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
catch (wasmError) {
|
|
189
|
+
ctx.logger.error("WASM compiler threw exception:", wasmError);
|
|
190
|
+
result = {
|
|
191
|
+
success: false,
|
|
192
|
+
bytecode: null,
|
|
193
|
+
bytecode_size: 0,
|
|
194
|
+
compilation_time: 0,
|
|
195
|
+
compiler_errors: [
|
|
196
|
+
{
|
|
197
|
+
code: "E9999",
|
|
198
|
+
severity: "error",
|
|
199
|
+
category: "wasm_exception",
|
|
200
|
+
message: wasmError instanceof Error
|
|
201
|
+
? wasmError.message
|
|
202
|
+
: String(wasmError),
|
|
203
|
+
description: "WASM compiler threw an exception",
|
|
204
|
+
location: null,
|
|
205
|
+
suggestions: [],
|
|
206
|
+
},
|
|
207
|
+
],
|
|
208
|
+
error_count: 1,
|
|
209
|
+
warning_count: 0,
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
if (!result.success &&
|
|
213
|
+
result.compiler_errors &&
|
|
214
|
+
result.compiler_errors.length > 0) {
|
|
215
|
+
ctx.logger.debug(`Enhanced Error Count: ${result.error_count}`);
|
|
216
|
+
try {
|
|
217
|
+
const terminalOutput = result.format_all_terminal
|
|
218
|
+
? result.format_all_terminal()
|
|
219
|
+
: null;
|
|
220
|
+
if (terminalOutput) {
|
|
221
|
+
ctx.logger.debug("Terminal formatted errors:", terminalOutput);
|
|
222
|
+
}
|
|
223
|
+
const jsonOutput = result.format_all_json
|
|
224
|
+
? result.format_all_json()
|
|
225
|
+
: null;
|
|
226
|
+
if (jsonOutput) {
|
|
227
|
+
ctx.logger.debug("JSON formatted errors:", jsonOutput);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
catch (formatError) {
|
|
231
|
+
ctx.logger.debug("Failed to get formatted errors:", formatError);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
const compilationTime = Date.now() - startTime;
|
|
235
|
+
const metricsPayload = extractMetrics(result, metricsFormat);
|
|
236
|
+
let convertedErrors = [];
|
|
237
|
+
if (result.compiler_errors && result.compiler_errors.length > 0) {
|
|
238
|
+
try {
|
|
239
|
+
const jsonErrors = result.format_all_json
|
|
240
|
+
? result.format_all_json()
|
|
241
|
+
: null;
|
|
242
|
+
if (jsonErrors) {
|
|
243
|
+
const parsedErrors = JSON.parse(jsonErrors);
|
|
244
|
+
convertedErrors = parsedErrors.map((error) => ({
|
|
245
|
+
type: "enhanced",
|
|
246
|
+
...error,
|
|
247
|
+
code: error.code || "E0000",
|
|
248
|
+
severity: error.severity || "error",
|
|
249
|
+
category: error.category || "compilation",
|
|
250
|
+
message: error.message || "Unknown error",
|
|
251
|
+
}));
|
|
252
|
+
}
|
|
253
|
+
else {
|
|
254
|
+
convertedErrors = [
|
|
255
|
+
{
|
|
256
|
+
type: "enhanced",
|
|
257
|
+
code: "E0004",
|
|
258
|
+
severity: "error",
|
|
259
|
+
category: "compilation",
|
|
260
|
+
message: "InvalidScript",
|
|
261
|
+
description: "The script contains syntax or semantic errors",
|
|
262
|
+
location: undefined,
|
|
263
|
+
suggestions: [],
|
|
264
|
+
},
|
|
265
|
+
];
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
catch (parseError) {
|
|
269
|
+
ctx.logger.debug("Failed to parse JSON errors from WASM:", parseError);
|
|
270
|
+
convertedErrors = [
|
|
271
|
+
{
|
|
272
|
+
type: "enhanced",
|
|
273
|
+
code: "E0004",
|
|
274
|
+
severity: "error",
|
|
275
|
+
category: "compilation",
|
|
276
|
+
message: "InvalidScript",
|
|
277
|
+
description: "Compilation failed with enhanced error system",
|
|
278
|
+
location: undefined,
|
|
279
|
+
suggestions: [],
|
|
280
|
+
},
|
|
281
|
+
];
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
const compilationResult = {
|
|
285
|
+
success: result.success,
|
|
286
|
+
bytecode: result.bytecode ? new Uint8Array(result.bytecode) : undefined,
|
|
287
|
+
abi: result.abi || undefined,
|
|
288
|
+
errors: convertedErrors,
|
|
289
|
+
warnings: convertedErrors.filter((e) => e.severity === "warning") || [],
|
|
290
|
+
metrics: {
|
|
291
|
+
compilationTime: result.compilation_time || compilationTime,
|
|
292
|
+
bytecodeSize: result.bytecode_size || 0,
|
|
293
|
+
memoryUsed: 0,
|
|
294
|
+
optimizationTime: 0,
|
|
295
|
+
instructionCount: 0,
|
|
296
|
+
functionCount: 0,
|
|
297
|
+
},
|
|
298
|
+
metricsReport: metricsPayload,
|
|
299
|
+
};
|
|
300
|
+
if (options.outputFile && compilationResult.bytecode) {
|
|
301
|
+
await writeFile(options.outputFile, compilationResult.bytecode);
|
|
302
|
+
ctx.logger.debug(`Bytecode written to: ${options.outputFile}`);
|
|
303
|
+
}
|
|
304
|
+
if (options.abiOutputFile && compilationResult.abi) {
|
|
305
|
+
await writeFile(options.abiOutputFile, JSON.stringify(compilationResult.abi, null, 2));
|
|
306
|
+
ctx.logger.debug(`ABI written to: ${options.abiOutputFile}`);
|
|
307
|
+
}
|
|
308
|
+
ctx.logger.debug(`Compilation completed in ${compilationTime}ms`);
|
|
309
|
+
return compilationResult;
|
|
310
|
+
}
|
|
311
|
+
catch (error) {
|
|
312
|
+
const compilationTime = Date.now() - startTime;
|
|
313
|
+
return {
|
|
314
|
+
success: false,
|
|
315
|
+
errors: [
|
|
316
|
+
{
|
|
317
|
+
type: "runtime",
|
|
318
|
+
message: error instanceof Error ? error.message : String(error),
|
|
319
|
+
sourceLocation: options.sourceFile,
|
|
320
|
+
},
|
|
321
|
+
],
|
|
322
|
+
warnings: [],
|
|
323
|
+
metrics: {
|
|
324
|
+
compilationTime,
|
|
325
|
+
memoryUsed: 0,
|
|
326
|
+
optimizationTime: 0,
|
|
327
|
+
bytecodeSize: 0,
|
|
328
|
+
instructionCount: 0,
|
|
329
|
+
functionCount: 0,
|
|
330
|
+
},
|
|
331
|
+
};
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
export async function compileWithDiscovery(ctx, entryPoint, options) {
|
|
335
|
+
if (!ctx.compiler) {
|
|
336
|
+
throw createCompilerError("Compiler not initialized");
|
|
337
|
+
}
|
|
338
|
+
try {
|
|
339
|
+
let WasmCompilationOptions = ctx.WasmCompilationOptions;
|
|
340
|
+
if (!WasmCompilationOptions && ctx.wasmModuleRef) {
|
|
341
|
+
WasmCompilationOptions = ctx.wasmModuleRef.WasmCompilationOptions;
|
|
342
|
+
}
|
|
343
|
+
const metricsFormat = options?.metricsFormat || "json";
|
|
344
|
+
const includeMetrics = options?.includeMetrics || Boolean(options?.metricsOutput);
|
|
345
|
+
const errorFormat = options?.errorFormat || "terminal";
|
|
346
|
+
const comprehensiveMetrics = options?.comprehensiveMetrics || false;
|
|
347
|
+
const compilationOptions = new WasmCompilationOptions()
|
|
348
|
+
.with_mode(options?.target || "deployment")
|
|
349
|
+
.with_optimization_level(options?.optimizationLevel || "production")
|
|
350
|
+
.with_v2_preview(true)
|
|
351
|
+
.with_constraint_cache(false)
|
|
352
|
+
.with_enhanced_errors(true)
|
|
353
|
+
.with_metrics(includeMetrics)
|
|
354
|
+
.with_comprehensive_metrics(comprehensiveMetrics)
|
|
355
|
+
.with_metrics_format(metricsFormat)
|
|
356
|
+
.with_error_format(errorFormat);
|
|
357
|
+
const result = ctx.compiler.compileMultiWithDiscovery(entryPoint, compilationOptions);
|
|
358
|
+
const metricsPayload = extractMetrics(result, metricsFormat);
|
|
359
|
+
if (result.success && result.bytecode) {
|
|
360
|
+
return {
|
|
361
|
+
success: true,
|
|
362
|
+
bytecode: result.bytecode,
|
|
363
|
+
abi: result.abi,
|
|
364
|
+
metadata: result.metadata,
|
|
365
|
+
metrics: metricsPayload,
|
|
366
|
+
metricsReport: metricsPayload,
|
|
367
|
+
};
|
|
368
|
+
}
|
|
369
|
+
else {
|
|
370
|
+
return {
|
|
371
|
+
success: false,
|
|
372
|
+
errors: result.compiler_errors || [],
|
|
373
|
+
metadata: result.metadata,
|
|
374
|
+
metrics: metricsPayload,
|
|
375
|
+
metricsReport: metricsPayload,
|
|
376
|
+
};
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
catch (error) {
|
|
380
|
+
throw createCompilerError(`Compilation error: ${error instanceof Error ? error.message : "Unknown error"}`, error);
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
export async function compileModulesExplicit(ctx, moduleFiles, entryPoint, options) {
|
|
384
|
+
if (!ctx.compiler) {
|
|
385
|
+
throw createCompilerError("Compiler not initialized");
|
|
386
|
+
}
|
|
387
|
+
try {
|
|
388
|
+
let WasmCompilationOptions = ctx.WasmCompilationOptions;
|
|
389
|
+
if (!WasmCompilationOptions && ctx.wasmModuleRef) {
|
|
390
|
+
WasmCompilationOptions = ctx.wasmModuleRef.WasmCompilationOptions;
|
|
391
|
+
}
|
|
392
|
+
const metricsFormat = options?.metricsFormat || "json";
|
|
393
|
+
const includeMetrics = options?.includeMetrics || Boolean(options?.metricsOutput);
|
|
394
|
+
const errorFormat = options?.errorFormat || "terminal";
|
|
395
|
+
const comprehensiveMetrics = options?.comprehensiveMetrics || false;
|
|
396
|
+
const compilationOptions = new WasmCompilationOptions()
|
|
397
|
+
.with_mode(options?.target || "deployment")
|
|
398
|
+
.with_optimization_level(options?.optimizationLevel || "production")
|
|
399
|
+
.with_v2_preview(true)
|
|
400
|
+
.with_constraint_cache(false)
|
|
401
|
+
.with_enhanced_errors(true)
|
|
402
|
+
.with_metrics(includeMetrics)
|
|
403
|
+
.with_comprehensive_metrics(comprehensiveMetrics)
|
|
404
|
+
.with_metrics_format(metricsFormat)
|
|
405
|
+
.with_error_format(errorFormat);
|
|
406
|
+
const result = ctx.compiler.compileModules(moduleFiles, entryPoint, compilationOptions);
|
|
407
|
+
const metricsPayload = extractMetrics(result, metricsFormat);
|
|
408
|
+
if (result.success && result.bytecode) {
|
|
409
|
+
return {
|
|
410
|
+
success: true,
|
|
411
|
+
bytecode: result.bytecode,
|
|
412
|
+
abi: result.abi,
|
|
413
|
+
metadata: result.metadata,
|
|
414
|
+
metrics: metricsPayload,
|
|
415
|
+
metricsReport: metricsPayload,
|
|
416
|
+
};
|
|
417
|
+
}
|
|
418
|
+
else {
|
|
419
|
+
return {
|
|
420
|
+
success: false,
|
|
421
|
+
errors: result.compiler_errors || [],
|
|
422
|
+
metadata: result.metadata,
|
|
423
|
+
metrics: metricsPayload,
|
|
424
|
+
metricsReport: metricsPayload,
|
|
425
|
+
};
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
catch (error) {
|
|
429
|
+
throw createCompilerError(`Compilation error: ${error instanceof Error ? error.message : "Unknown error"}`, error);
|
|
430
|
+
}
|
|
431
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Five Compiler WASM Integration
|
|
3
|
+
*
|
|
4
|
+
* Real integration with Five VM WASM bindings for DSL compilation,
|
|
5
|
+
* ABI generation, and bytecode optimization.
|
|
6
|
+
*/
|
|
7
|
+
import { CompilationOptions, CompilationResult, Logger } from "../../types.js";
|
|
8
|
+
export declare class FiveCompiler {
|
|
9
|
+
private compiler;
|
|
10
|
+
private logger;
|
|
11
|
+
private initialized;
|
|
12
|
+
constructor(logger: Logger);
|
|
13
|
+
/**
|
|
14
|
+
* Initialize the compiler with real Five VM WASM module
|
|
15
|
+
* The Node.js target (from wasm-pack --target nodejs) is pre-initialized
|
|
16
|
+
*/
|
|
17
|
+
initialize(): Promise<void>;
|
|
18
|
+
private getContext;
|
|
19
|
+
compile(source: string, options?: any): Promise<any>;
|
|
20
|
+
compileModules(mainSource: string, modules: Array<{
|
|
21
|
+
name: string;
|
|
22
|
+
source: string;
|
|
23
|
+
}>, options?: any): Promise<any>;
|
|
24
|
+
compileFile(options: CompilationOptions): Promise<CompilationResult>;
|
|
25
|
+
compileWithDiscovery(entryPoint: string, options?: any): Promise<any>;
|
|
26
|
+
compileModulesExplicit(moduleFiles: string[], entryPoint: string, options?: any): Promise<any>;
|
|
27
|
+
generateABI(sourceCode: string): Promise<any>;
|
|
28
|
+
extractAccountDefinitions(sourceCode: string): Promise<any>;
|
|
29
|
+
extractFunctionSignatures(sourceCode: string): Promise<any>;
|
|
30
|
+
validateSource(sourceCode: string): Promise<{
|
|
31
|
+
valid: boolean;
|
|
32
|
+
errors: string[];
|
|
33
|
+
warnings: string[];
|
|
34
|
+
}>;
|
|
35
|
+
validateAccountConstraints(sourceCode: string, functionName: string, accounts: any[]): Promise<any>;
|
|
36
|
+
optimizeBytecode(bytecode: Uint8Array): Promise<Uint8Array>;
|
|
37
|
+
analyzeBytecode(bytecode: Uint8Array): Promise<any>;
|
|
38
|
+
analyzeInstructionAt(bytecode: Uint8Array, offset: number): Promise<any>;
|
|
39
|
+
getBytecodeStats(bytecode: Uint8Array): Promise<any>;
|
|
40
|
+
getOpcodeUsage(sourceCode: string): Promise<any>;
|
|
41
|
+
getOpcodeAnalysis(sourceCode: string): Promise<any>;
|
|
42
|
+
getCompilerInfo(): {
|
|
43
|
+
version: string;
|
|
44
|
+
features: string[];
|
|
45
|
+
};
|
|
46
|
+
discoverModules(entryPoint: string): Promise<string[]>;
|
|
47
|
+
isReady(): boolean;
|
|
48
|
+
}
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Five Compiler WASM Integration
|
|
3
|
+
*
|
|
4
|
+
* Real integration with Five VM WASM bindings for DSL compilation,
|
|
5
|
+
* ABI generation, and bytecode optimization.
|
|
6
|
+
*/
|
|
7
|
+
import { ConfigManager } from "../../config/ConfigManager.js";
|
|
8
|
+
import * as CompilationLogic from "./CompilationLogic.js";
|
|
9
|
+
import * as AnalysisLogic from "./AnalysisLogic.js";
|
|
10
|
+
import * as AbiLogic from "./AbiLogic.js";
|
|
11
|
+
import * as ValidationLogic from "./ValidationLogic.js";
|
|
12
|
+
import * as OptimizationLogic from "./OptimizationLogic.js";
|
|
13
|
+
import * as InfoLogic from "./InfoLogic.js";
|
|
14
|
+
import { createCompilerError } from "./utils.js";
|
|
15
|
+
// Global refs to loaded modules
|
|
16
|
+
let wasmModuleRef = null;
|
|
17
|
+
let FiveCompilerWasm;
|
|
18
|
+
let FiveVMWasm;
|
|
19
|
+
let BytecodeAnalyzer;
|
|
20
|
+
let WasmCompilationOptions;
|
|
21
|
+
export class FiveCompiler {
|
|
22
|
+
constructor(logger) {
|
|
23
|
+
this.compiler = null;
|
|
24
|
+
this.initialized = false;
|
|
25
|
+
this.logger = logger;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Initialize the compiler with real Five VM WASM module
|
|
29
|
+
* The Node.js target (from wasm-pack --target nodejs) is pre-initialized
|
|
30
|
+
*/
|
|
31
|
+
async initialize() {
|
|
32
|
+
try {
|
|
33
|
+
// Try multiple candidate locations (mirrors vm.ts logic)
|
|
34
|
+
const cfg = await ConfigManager.getInstance().get();
|
|
35
|
+
const prefer = cfg.wasm?.loader || "auto";
|
|
36
|
+
const configured = Array.isArray(cfg.wasm?.modulePaths)
|
|
37
|
+
? cfg.wasm.modulePaths
|
|
38
|
+
: [];
|
|
39
|
+
const nodeCandidates = [
|
|
40
|
+
"../../five_vm_wasm.cjs",
|
|
41
|
+
"../five_vm_wasm.cjs",
|
|
42
|
+
"../../five_vm_wasm.js",
|
|
43
|
+
"../five_vm_wasm.js",
|
|
44
|
+
];
|
|
45
|
+
const bundlerCandidates = [
|
|
46
|
+
"../../assets/vm/five_vm_wasm.cjs",
|
|
47
|
+
"../assets/vm/five_vm_wasm.cjs",
|
|
48
|
+
"../../assets/vm/five_vm_wasm.js",
|
|
49
|
+
"../assets/vm/five_vm_wasm.js",
|
|
50
|
+
];
|
|
51
|
+
let candidates = [];
|
|
52
|
+
candidates.push(...configured);
|
|
53
|
+
if (prefer === "node") {
|
|
54
|
+
candidates.push(...nodeCandidates);
|
|
55
|
+
}
|
|
56
|
+
else if (prefer === "bundler") {
|
|
57
|
+
candidates.push(...bundlerCandidates);
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
candidates.push(...nodeCandidates, ...bundlerCandidates);
|
|
61
|
+
}
|
|
62
|
+
let wasmModule = null;
|
|
63
|
+
const tried = [];
|
|
64
|
+
for (const candidate of candidates) {
|
|
65
|
+
try {
|
|
66
|
+
// eslint-disable-next-line no-await-in-loop
|
|
67
|
+
const mod = await import(candidate);
|
|
68
|
+
// The Node.js target (wasm-pack --target nodejs) is pre-initialized
|
|
69
|
+
// No need to call initSync() - just check if it has the expected exports
|
|
70
|
+
if (mod &&
|
|
71
|
+
(mod.WasmFiveCompiler || mod.FiveCompilerWasm) &&
|
|
72
|
+
mod.FiveVMWasm) {
|
|
73
|
+
wasmModule = mod;
|
|
74
|
+
break;
|
|
75
|
+
}
|
|
76
|
+
if (!(mod.WasmFiveCompiler || mod.FiveCompilerWasm)) {
|
|
77
|
+
tried.push({ path: candidate, error: "Missing WasmFiveCompiler export" });
|
|
78
|
+
}
|
|
79
|
+
else if (!mod.FiveVMWasm) {
|
|
80
|
+
tried.push({ path: candidate, error: "Missing FiveVMWasm export" });
|
|
81
|
+
}
|
|
82
|
+
else {
|
|
83
|
+
tried.push({ path: candidate, error: "Module loaded but invalid" });
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
catch (e) {
|
|
87
|
+
tried.push({ path: candidate, error: e });
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
if (!wasmModule) {
|
|
91
|
+
const attempted = tried
|
|
92
|
+
.map((t) => ` - ${t.path}: ${t.error instanceof Error ? t.error.message : String(t.error)}`)
|
|
93
|
+
.join("\n");
|
|
94
|
+
throw new Error(`Failed to load WASM compiler module. Attempted:\n${attempted}`);
|
|
95
|
+
}
|
|
96
|
+
wasmModuleRef = wasmModule;
|
|
97
|
+
FiveCompilerWasm =
|
|
98
|
+
wasmModule.WasmFiveCompiler || wasmModule.FiveCompilerWasm;
|
|
99
|
+
FiveVMWasm = wasmModule.FiveVMWasm;
|
|
100
|
+
BytecodeAnalyzer = wasmModule.BytecodeAnalyzer;
|
|
101
|
+
WasmCompilationOptions = wasmModule.WasmCompilationOptions;
|
|
102
|
+
// Initialize the compiler instance
|
|
103
|
+
this.compiler = new FiveCompilerWasm();
|
|
104
|
+
this.initialized = true;
|
|
105
|
+
// WASM compiler initialized silently
|
|
106
|
+
}
|
|
107
|
+
catch (error) {
|
|
108
|
+
throw createCompilerError(`Five VM WASM modules not found. Please run "npm run build:wasm" to build the required WebAssembly modules. Error: ${error}`, error);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
getContext() {
|
|
112
|
+
return {
|
|
113
|
+
compiler: this.compiler,
|
|
114
|
+
wasmModuleRef,
|
|
115
|
+
WasmCompilationOptions,
|
|
116
|
+
BytecodeAnalyzer,
|
|
117
|
+
logger: this.logger,
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
// --- Compilation ---
|
|
121
|
+
async compile(source, options) {
|
|
122
|
+
return CompilationLogic.compile(this.getContext(), source, options);
|
|
123
|
+
}
|
|
124
|
+
async compileModules(mainSource, modules, options) {
|
|
125
|
+
return CompilationLogic.compileModules(this.getContext(), mainSource, modules, options);
|
|
126
|
+
}
|
|
127
|
+
async compileFile(options) {
|
|
128
|
+
return CompilationLogic.compileFile(this.getContext(), options);
|
|
129
|
+
}
|
|
130
|
+
async compileWithDiscovery(entryPoint, options) {
|
|
131
|
+
return CompilationLogic.compileWithDiscovery(this.getContext(), entryPoint, options);
|
|
132
|
+
}
|
|
133
|
+
async compileModulesExplicit(moduleFiles, entryPoint, options) {
|
|
134
|
+
return CompilationLogic.compileModulesExplicit(this.getContext(), moduleFiles, entryPoint, options);
|
|
135
|
+
}
|
|
136
|
+
// --- ABI & Extraction ---
|
|
137
|
+
async generateABI(sourceCode) {
|
|
138
|
+
return AbiLogic.generateABI(this.getContext(), sourceCode);
|
|
139
|
+
}
|
|
140
|
+
async extractAccountDefinitions(sourceCode) {
|
|
141
|
+
return AbiLogic.extractAccountDefinitions(this.getContext(), sourceCode);
|
|
142
|
+
}
|
|
143
|
+
async extractFunctionSignatures(sourceCode) {
|
|
144
|
+
return AbiLogic.extractFunctionSignatures(this.getContext(), sourceCode);
|
|
145
|
+
}
|
|
146
|
+
// --- Validation ---
|
|
147
|
+
async validateSource(sourceCode) {
|
|
148
|
+
return ValidationLogic.validateSource(this.getContext(), sourceCode);
|
|
149
|
+
}
|
|
150
|
+
async validateAccountConstraints(sourceCode, functionName, accounts) {
|
|
151
|
+
return ValidationLogic.validateAccountConstraints(this.getContext(), sourceCode, functionName, accounts);
|
|
152
|
+
}
|
|
153
|
+
// --- Optimization ---
|
|
154
|
+
async optimizeBytecode(bytecode) {
|
|
155
|
+
return OptimizationLogic.optimizeBytecode(this.getContext(), bytecode);
|
|
156
|
+
}
|
|
157
|
+
// --- Analysis ---
|
|
158
|
+
async analyzeBytecode(bytecode) {
|
|
159
|
+
return AnalysisLogic.analyzeBytecode(this.getContext(), bytecode);
|
|
160
|
+
}
|
|
161
|
+
async analyzeInstructionAt(bytecode, offset) {
|
|
162
|
+
return AnalysisLogic.analyzeInstructionAt(this.getContext(), bytecode, offset);
|
|
163
|
+
}
|
|
164
|
+
async getBytecodeStats(bytecode) {
|
|
165
|
+
return AnalysisLogic.getBytecodeStats(this.getContext(), bytecode);
|
|
166
|
+
}
|
|
167
|
+
async getOpcodeUsage(sourceCode) {
|
|
168
|
+
return AnalysisLogic.getOpcodeUsage(this.getContext(), sourceCode);
|
|
169
|
+
}
|
|
170
|
+
async getOpcodeAnalysis(sourceCode) {
|
|
171
|
+
return AnalysisLogic.getOpcodeAnalysis(this.getContext(), sourceCode);
|
|
172
|
+
}
|
|
173
|
+
// --- Info ---
|
|
174
|
+
getCompilerInfo() {
|
|
175
|
+
return InfoLogic.getCompilerInfo(this.getContext());
|
|
176
|
+
}
|
|
177
|
+
async discoverModules(entryPoint) {
|
|
178
|
+
return InfoLogic.discoverModules(this.getContext(), entryPoint);
|
|
179
|
+
}
|
|
180
|
+
isReady() {
|
|
181
|
+
return this.initialized && this.compiler !== null;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { CompilationContext } from "./types.js";
|
|
2
|
+
export declare function getCompilerInfo(ctx: CompilationContext): {
|
|
3
|
+
version: string;
|
|
4
|
+
features: string[];
|
|
5
|
+
};
|
|
6
|
+
export declare function discoverModules(ctx: CompilationContext, entryPoint: string): Promise<string[]>;
|