Repository navigation
Expand file tree
/
Copy pathbuild.js
More file actions
233 lines (196 loc) · 6.84 KB
/
Copy pathbuild.js
File metadata and controls
233 lines (196 loc) · 6.84 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
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const { spawnSync } = require('child_process');
function ensureDir(dir) {
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
}
function compileWatToWasm(watFile, wasmFile) {
console.log(`📝 Compiling ${watFile} to ${wasmFile}...`);
try {
const result = spawnSync('wasm-tools', ['parse', watFile, '-o', wasmFile], {
stdio: 'inherit',
cwd: process.cwd()
});
if (result.error) {
console.error(`✗ Error running wasm-tools:`, result.error.message);
return false;
}
if (result.status === 0) {
console.log(`✓ Successfully compiled ${watFile}`);
return true;
} else {
console.error(`✗ Compilation failed for ${watFile} with exit code ${result.status}`);
return false;
}
} catch (error) {
console.error(`✗ Failed to compile ${watFile}:`, error.message);
return false;
}
}
function dumpWasm(wasmFile) {
try {
console.log(`🔍 Analyzing ${wasmFile}...`);
const dumpFile = wasmFile.replace('.wasm', '.dump');
const result = spawnSync('wasm-tools', ['dump', wasmFile], {
cwd: process.cwd()
});
if (result.error) {
console.error(`✗ Error running wasm-tools for analysis:`, result.error.message);
return false;
}
if (result.status === 0) {
fs.writeFileSync(dumpFile, result.stdout.toString(), 'utf8');
console.log(`✓ Generated analysis dump: ${dumpFile}`);
return true;
} else {
console.error(`✗ Failed to analyze ${wasmFile}`);
return false;
}
} catch (error) {
console.error(`✗ Failed to analyze ${wasmFile}:`, error.message);
return false;
}
}
function validateWasmFile(wasmFile) {
try {
console.log(`✅ Validating ${wasmFile}...`);
const result = spawnSync('wasm-tools', ['validate', wasmFile], {
stdio: 'inherit',
cwd: process.cwd()
});
if (result.error) {
console.error(`✗ Error running wasm-tools for validation:`, result.error.message);
return false;
}
if (result.status === 0) {
console.log(`✓ ${wasmFile} is valid`);
return true;
} else {
console.error(`✗ Validation failed for ${wasmFile}`);
return false;
}
} catch (error) {
console.error(`✗ Failed to validate ${wasmFile}:`, error.message);
return false;
}
}
function copyFile(src, dest) {
try {
if (!fs.existsSync(src)) {
console.error(`✗ Source file not found: ${src}`);
return false;
}
fs.copyFileSync(src, dest);
console.log(`✓ Copied ${src} to ${dest}`);
return true;
} catch (error) {
console.error(`✗ Failed to copy ${src} to ${dest}:`, error.message);
return false;
}
}
function main() {
console.log('🚀 Building Catalyst...\n');
const outputDir = './dist';
ensureDir(outputDir);
// Compile WAT files to WASM
const watFiles = ['catalyst.wat'];
let allSuccess = true;
for (const watFile of watFiles) {
if (!fs.existsSync(watFile)) {
console.error(`✗ WAT file not found: ${watFile}`);
allSuccess = false;
continue;
}
const wasmFile = path.join(outputDir, watFile.replace('.wat', '.wasm'));
if (!compileWatToWasm(watFile, wasmFile)) {
allSuccess = false;
continue;
}
if (!dumpWasm(wasmFile)) {
allSuccess = false;
continue;
}
if (!validateWasmFile(wasmFile)) {
allSuccess = false;
continue;
}
}
// Copy JavaScript files
const jsFiles = ['catalyst.js'];
for (const jsFile of jsFiles) {
const destFile = path.join(outputDir, jsFile);
if (!copyFile(jsFile, destFile)) {
allSuccess = false;
}
}
const testHtmlSrc = 'test.html';
const testHtmlDest = path.join(outputDir, 'test.html');
if (!copyFile(testHtmlSrc, testHtmlDest)) {
console.error(`✗ Failed to copy test.html - make sure ${testHtmlSrc} exists`);
allSuccess = false;
}
// Copy .htaccess file for cross-origin isolation (high-resolution timers)
const htaccessSrc = '.htaccess';
const htaccessDest = path.join(outputDir, '.htaccess');
if (!copyFile(htaccessSrc, htaccessDest)) {
console.error(`✗ Failed to copy .htaccess - cross-origin isolation will not work`);
allSuccess = false;
}
// Copy keys file for LLM API access (if it exists)
const keysSrc = 'keys';
const keysDest = path.join(outputDir, 'keys');
if (fs.existsSync(keysSrc)) {
if (!copyFile(keysSrc, keysDest)) {
console.error(`✗ Failed to copy keys file - LLM optimization will not work`);
// Don't fail the build for missing keys - it's optional
}
} else {
console.log(`⚠️ Keys file not found - LLM optimization will be disabled`);
console.log(`💡 Create a 'keys' file with: openai=sk-proj-... or anthropic=sk-ant-api03-...`);
}
// Create package info
const packageInfo = {
name: 'catalyst',
version: '3.0.0',
description: 'Catalyst Smalltalk virtual machine',
phase: 3,
features: [],
buildDate: new Date().toISOString(),
files: {
wasm: watFiles.map(f => f.replace('.wat', '.wasm')),
javascript: jsFiles,
test: 'test.html',
config: '.htaccess'
}
};
fs.writeFileSync(
path.join(outputDir, 'package-info.json'),
JSON.stringify(packageInfo, null, 2),
'utf8'
);
console.log(`✓ Created package-info.json`);
if (allSuccess) {
console.log('\n🎉 Phase 3 build completed successfully!');
console.log('\nTo test the method translation:');
console.log('1. Start a web server: python -m http.server 8000');
console.log('2. Open: http://localhost:8000/dist/test.html');
console.log('3. Click "Run (3 benchmark) with translation" to execute the basic example');
console.log('4. Use "Run Multiple Times" to trigger method translation (threshold: 1000 invocations)');
} else {
console.log('\n❌ Build completed with errors');
process.exit(1);
}
}
// Only run main if this is the main module
if (require.main === module) {
main();
}
module.exports = {
compileWatToWasm,
dumpWasm,
validateWasmFile,
copyFile
};