-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcompile.ts
More file actions
138 lines (117 loc) · 4.35 KB
/
compile.ts
File metadata and controls
138 lines (117 loc) · 4.35 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
import { RUNNER_TARGET_VERSION } from '@mimicprotocol/lib-ts/constants'
import { Command, Flags } from '@oclif/core'
import * as fs from 'fs'
import * as path from 'path'
import ManifestHandler from '../lib/ManifestHandler'
import { execBinCommand } from '../lib/packageManager'
import log from '../log'
import { FlagsType } from '../types'
import Functions, { DefaultFunctionConfig } from './functions'
export type CompileFlags = FlagsType<typeof Compile>
export default class Compile extends Command {
static override description = 'Compiles function'
static override examples = [
'<%= config.bin %> <%= command.id %> --function src/function.ts --build-directory ./build',
]
static override flags = {
...Functions.flags,
function: Flags.string({ char: 'f', description: 'Function to compile', default: DefaultFunctionConfig.function }),
manifest: Flags.string({ char: 'm', description: 'Manifest to validate', default: DefaultFunctionConfig.manifest }),
'build-directory': Flags.string({
char: 'b',
description: 'Output directory for compilation',
default: DefaultFunctionConfig['build-directory'],
}),
}
public async run(): Promise<void> {
const { flags } = await this.parse(Compile)
await Functions.runFunctions(this, flags, Compile.compile, 'compilation')
}
public static async compile(
cmd: Command,
{ function: functionDir, 'build-directory': buildDir, manifest: manifestDir }: CompileFlags
): Promise<void> {
const absFunctionFile = path.resolve(functionDir)
const absBuildDir = path.resolve(buildDir)
if (!fs.existsSync(absBuildDir)) fs.mkdirSync(absBuildDir, { recursive: true })
log.startAction('Verifying Manifest')
const manifest = ManifestHandler.load(cmd, manifestDir)
log.startAction('Compiling')
const wasmPath = path.join(absBuildDir, 'function.wasm')
const ascArgs = [
absFunctionFile,
'--target',
'release',
'--outFile',
wasmPath,
'--optimize',
'--exportRuntime',
'--transform',
'json-as/transform',
]
const result = execBinCommand('asc', ascArgs, process.cwd())
if (result.status !== 0) {
cmd.error('AssemblyScript compilation failed', {
code: 'BuildError',
suggestions: ['Check the AssemblyScript file'],
})
}
log.startAction('Injecting metadata')
const wasmBuffer = fs.readFileSync(wasmPath)
const metadata = {
runnerTarget: RUNNER_TARGET_VERSION,
}
const wasmWithMetadata = addCustomSection(wasmBuffer, 'mimic-metadata', JSON.stringify(metadata))
fs.writeFileSync(wasmPath, wasmWithMetadata)
log.startAction('Saving files')
fs.writeFileSync(path.join(absBuildDir, 'manifest.json'), JSON.stringify(manifest, null, 2))
log.stopAction()
console.log(`Build complete! Artifacts in ${absBuildDir}/`)
}
}
/**
* Add a custom section to a WASM binary
* @param wasmBuffer - The original WASM binary
* @param sectionName - Name of the custom section
* @param data - String data to store in the section
* @returns Modified WASM binary with the custom section
*/
function addCustomSection(wasmBuffer: Buffer, sectionName: string, data: string): Buffer {
const dataBuffer = Buffer.from(data, 'utf-8')
const nameBuffer = Buffer.from(sectionName, 'utf-8')
// WASM custom section format:
// - Section ID: 0 (custom section)
// - Section size (LEB128) - size of name length + name + data
// - Name length (LEB128)
// - Name bytes
// - Data bytes
const nameLengthBuffer = encodeLEB128(nameBuffer.length)
const sectionContentSize = nameLengthBuffer.length + nameBuffer.length + dataBuffer.length
const sectionSizeBuffer = encodeLEB128(sectionContentSize)
const customSection = Buffer.concat([
Buffer.from([0]), // Custom section ID
sectionSizeBuffer,
nameLengthBuffer,
nameBuffer,
dataBuffer,
])
// Insert after the WASM header (8 bytes: magic + version)
const headerSize = 8
return Buffer.concat([wasmBuffer.slice(0, headerSize), customSection, wasmBuffer.slice(headerSize)])
}
/**
* Encode an unsigned integer as LEB128 (Little Endian Base 128)
*/
function encodeLEB128(value: number): Buffer {
const bytes: number[] = []
while (true) {
let byte = value & 0x7f
value >>>= 7
if (value !== 0) {
byte |= 0x80
}
bytes.push(byte)
if (value === 0) break
}
return Buffer.from(bytes)
}