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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
|
// config.js - Configuration system for Baba Yaga engine
import fs from 'fs';
import path from 'path';
import { ValidationError } from './error.js';
/**
* Configuration class for Baba Yaga engine with validation and defaults
*/
export class BabaYagaConfig {
constructor(options = {}) {
// Performance settings
this.maxRecursionDepth = this.validateNumber(options.maxRecursionDepth, 1000, 1, 100000, 'maxRecursionDepth');
this.maxExecutionTime = this.validateNumber(options.maxExecutionTime, 30000, 100, 300000, 'maxExecutionTime');
this.maxMemoryUsage = this.validateNumber(options.maxMemoryUsage, 100_000_000, 1000000, 1_000_000_000, 'maxMemoryUsage');
// Input validation limits
this.maxSourceLength = this.validateNumber(options.maxSourceLength, 10_000_000, 1000, 100_000_000, 'maxSourceLength');
this.maxASTDepth = this.validateNumber(options.maxASTDepth, 1000, 10, 10000, 'maxASTDepth');
this.maxIdentifierLength = this.validateNumber(options.maxIdentifierLength, 255, 1, 1000, 'maxIdentifierLength');
this.maxStringLength = this.validateNumber(options.maxStringLength, 1_000_000, 1000, 10_000_000, 'maxStringLength');
this.maxListLength = this.validateNumber(options.maxListLength, 100_000, 100, 10_000_000, 'maxListLength');
this.maxTableSize = this.validateNumber(options.maxTableSize, 10_000, 10, 1_000_000, 'maxTableSize');
// Feature flags
this.enableOptimizations = this.validateBoolean(options.enableOptimizations, false, 'enableOptimizations'); // Disabled by default due to lexer bug
this.enableTypeChecking = this.validateBoolean(options.enableTypeChecking, true, 'enableTypeChecking');
this.enableDebugMode = this.validateBoolean(options.enableDebugMode, false, 'enableDebugMode');
this.enableProfiling = this.validateBoolean(options.enableProfiling, false, 'enableProfiling');
this.strictMode = this.validateBoolean(options.strictMode, false, 'strictMode');
// Security settings
this.allowUnsafeOperations = this.validateBoolean(options.allowUnsafeOperations, true, 'allowUnsafeOperations');
this.sandboxMode = this.validateBoolean(options.sandboxMode, false, 'sandboxMode');
this.allowedBuiltins = this.validateArray(options.allowedBuiltins, this.getDefaultBuiltins(), 'allowedBuiltins');
this.allowedCharacters = options.allowedCharacters instanceof RegExp
? options.allowedCharacters
: /^[\x20-\x7E\s\n\r\t]*$/; // Printable ASCII + whitespace
// Error handling
this.verboseErrors = this.validateBoolean(options.verboseErrors, true, 'verboseErrors');
this.maxErrorSuggestions = this.validateNumber(options.maxErrorSuggestions, 3, 0, 10, 'maxErrorSuggestions');
this.includeStackTrace = this.validateBoolean(options.includeStackTrace, true, 'includeStackTrace');
// Output settings
this.outputFormat = this.validateString(options.outputFormat, 'pretty', ['pretty', 'json', 'minimal'], 'outputFormat');
this.colorOutput = this.validateBoolean(options.colorOutput, true, 'colorOutput');
this.showTimings = this.validateBoolean(options.showTimings, false, 'showTimings');
// Custom extensions
this.customBuiltins = this.validateMap(options.customBuiltins, new Map(), 'customBuiltins');
this.plugins = this.validateArray(options.plugins, [], 'plugins');
// Host integration
this.hostInterface = options.hostInterface || null;
this.ioHandlers = this.validateObject(options.ioHandlers, {}, 'ioHandlers');
// Cache settings
this.enableCaching = this.validateBoolean(options.enableCaching, false, 'enableCaching');
this.cacheDirectory = this.validateString(options.cacheDirectory, '.baba-cache', null, 'cacheDirectory');
// Development settings
this.repl = this.validateObject(options.repl, {
historySize: 1000,
multilineMode: true,
autoComplete: true,
showTypes: false
}, 'repl');
// Freeze configuration to prevent accidental modification
if (options.freeze !== false) {
Object.freeze(this);
}
}
/**
* Validate and normalize a number option
*/
validateNumber(value, defaultValue, min, max, name) {
if (value === undefined || value === null) {
return defaultValue;
}
if (typeof value !== 'number' || isNaN(value)) {
throw new ValidationError(`Configuration option '${name}' must be a number, got: ${typeof value}`);
}
if (value < min || value > max) {
throw new ValidationError(`Configuration option '${name}' must be between ${min} and ${max}, got: ${value}`);
}
return value;
}
/**
* Validate and normalize a boolean option
*/
validateBoolean(value, defaultValue, name) {
if (value === undefined || value === null) {
return defaultValue;
}
if (typeof value !== 'boolean') {
throw new ValidationError(`Configuration option '${name}' must be a boolean, got: ${typeof value}`);
}
return value;
}
/**
* Validate and normalize a string option
*/
validateString(value, defaultValue, allowedValues, name) {
if (value === undefined || value === null) {
return defaultValue;
}
if (typeof value !== 'string') {
throw new ValidationError(`Configuration option '${name}' must be a string, got: ${typeof value}`);
}
if (allowedValues && !allowedValues.includes(value)) {
throw new ValidationError(`Configuration option '${name}' must be one of: ${allowedValues.join(', ')}, got: ${value}`);
}
return value;
}
/**
* Validate and normalize an array option
*/
validateArray(value, defaultValue, name) {
if (value === undefined || value === null) {
return defaultValue;
}
if (!Array.isArray(value)) {
throw new ValidationError(`Configuration option '${name}' must be an array, got: ${typeof value}`);
}
return [...value]; // Create a copy
}
/**
* Validate and normalize an object option
*/
validateObject(value, defaultValue, name) {
if (value === undefined || value === null) {
return defaultValue;
}
if (typeof value !== 'object' || Array.isArray(value)) {
throw new ValidationError(`Configuration option '${name}' must be an object, got: ${typeof value}`);
}
return { ...defaultValue, ...value }; // Merge with defaults
}
/**
* Validate and normalize a Map option
*/
validateMap(value, defaultValue, name) {
if (value === undefined || value === null) {
return defaultValue;
}
if (!(value instanceof Map)) {
if (typeof value === 'object' && !Array.isArray(value)) {
// Convert object to Map
return new Map([...defaultValue.entries(), ...Object.entries(value)]);
} else {
throw new ValidationError(`Configuration option '${name}' must be a Map or object, got: ${typeof value}`);
}
}
return new Map([...defaultValue.entries(), ...value.entries()]);
}
/**
* Get default built-in functions
*/
getDefaultBuiltins() {
return [
// Higher-order functions
'map', 'filter', 'reduce', 'length',
// List operations
'append', 'prepend', 'concat', 'update', 'removeAt', 'slice',
// Table operations
'set', 'remove', 'merge', 'keys', 'values',
// String operations
'str.concat', 'str.split', 'str.join', 'str.length', 'str.substring',
'str.replace', 'str.trim', 'str.upper', 'str.lower',
// Math operations
'math.abs', 'math.sign', 'math.floor', 'math.ceil', 'math.round', 'math.trunc',
'math.min', 'math.max', 'math.clamp', 'math.pow', 'math.sqrt', 'math.exp', 'math.log',
'math.sin', 'math.cos', 'math.tan', 'math.asin', 'math.acos', 'math.atan', 'math.atan2',
'math.deg', 'math.rad', 'math.random', 'math.randomInt',
// IO operations
'io.out', 'io.in', 'io.emit', 'io.listen',
// Introspection
'shape'
];
}
/**
* Create configuration from JSON file
*/
static fromFile(configPath) {
try {
const fullPath = path.resolve(configPath);
const configData = fs.readFileSync(fullPath, 'utf8');
const parsed = JSON.parse(configData);
return new BabaYagaConfig(parsed);
} catch (error) {
if (error.code === 'ENOENT') {
throw new ValidationError(`Configuration file not found: ${configPath}`);
} else if (error instanceof SyntaxError) {
throw new ValidationError(`Invalid JSON in configuration file: ${error.message}`);
} else {
throw new ValidationError(`Failed to load configuration: ${error.message}`);
}
}
}
/**
* Create configuration from environment variables
*/
static fromEnvironment(prefix = 'BABA_') {
const options = {};
for (const [key, value] of Object.entries(process.env)) {
if (key.startsWith(prefix)) {
const configKey = key.slice(prefix.length).toLowerCase()
.replace(/_([a-z])/g, (_, letter) => letter.toUpperCase());
// Try to parse as JSON, fall back to string
try {
options[configKey] = JSON.parse(value);
} catch {
options[configKey] = value;
}
}
}
return new BabaYagaConfig(options);
}
/**
* Create development configuration
*/
static development() {
return new BabaYagaConfig({
enableDebugMode: true,
enableProfiling: true,
verboseErrors: true,
showTimings: true,
strictMode: false,
maxRecursionDepth: 500, // Lower for development
maxExecutionTime: 10000, // 10 seconds
repl: {
historySize: 10000,
multilineMode: true,
autoComplete: true,
showTypes: true
}
});
}
/**
* Create production configuration
*/
static production() {
return new BabaYagaConfig({
enableDebugMode: false,
enableProfiling: false,
verboseErrors: false,
showTimings: false,
strictMode: true,
enableOptimizations: true,
sandboxMode: true,
allowUnsafeOperations: false,
maxRecursionDepth: 2000,
maxExecutionTime: 5000, // 5 seconds
enableCaching: true
});
}
/**
* Create testing configuration
*/
static testing() {
return new BabaYagaConfig({
enableDebugMode: true,
verboseErrors: true,
strictMode: true,
maxRecursionDepth: 100, // Low for testing
maxExecutionTime: 1000, // 1 second
maxSourceLength: 100000, // Smaller for tests
includeStackTrace: true
});
}
/**
* Create sandbox configuration for untrusted code
*/
static sandbox() {
return new BabaYagaConfig({
sandboxMode: true,
allowUnsafeOperations: false,
strictMode: true,
maxRecursionDepth: 100,
maxExecutionTime: 1000,
maxSourceLength: 10000,
maxASTDepth: 50,
maxListLength: 1000,
maxTableSize: 100,
allowedBuiltins: [
'map', 'filter', 'reduce',
'str.length', 'str.substring',
'math.abs', 'math.min', 'math.max', 'math.floor', 'math.ceil'
]
});
}
/**
* Merge with another configuration
*/
merge(otherConfig) {
if (!(otherConfig instanceof BabaYagaConfig)) {
otherConfig = new BabaYagaConfig(otherConfig);
}
const merged = {};
// Copy all properties from both configs
for (const key of Object.keys(this)) {
merged[key] = otherConfig.hasOwnProperty(key) ? otherConfig[key] : this[key];
}
return new BabaYagaConfig({ ...merged, freeze: false });
}
/**
* Export configuration to JSON
*/
toJSON() {
const config = {};
for (const [key, value] of Object.entries(this)) {
if (value instanceof Map) {
config[key] = Object.fromEntries(value.entries());
} else if (value instanceof RegExp) {
config[key] = value.source;
} else {
config[key] = value;
}
}
return config;
}
/**
* Save configuration to file
*/
saveToFile(filePath) {
const config = this.toJSON();
const json = JSON.stringify(config, null, 2);
try {
fs.writeFileSync(filePath, json, 'utf8');
} catch (error) {
throw new ValidationError(`Failed to save configuration: ${error.message}`);
}
}
/**
* Validate configuration consistency
*/
validate() {
const errors = [];
// Check for logical inconsistencies
if (this.maxASTDepth > this.maxRecursionDepth * 2) {
errors.push('maxASTDepth should not be more than twice maxRecursionDepth');
}
if (this.maxExecutionTime < 100) {
errors.push('maxExecutionTime should be at least 100ms');
}
if (this.sandboxMode && this.allowUnsafeOperations) {
errors.push('sandboxMode and allowUnsafeOperations cannot both be true');
}
if (this.enableCaching && !this.cacheDirectory) {
errors.push('cacheDirectory must be specified when enableCaching is true');
}
if (errors.length > 0) {
throw new ValidationError(`Configuration validation failed:\n - ${errors.join('\n - ')}`);
}
return true;
}
/**
* Get a summary of the current configuration
*/
summary() {
return {
performance: {
maxRecursionDepth: this.maxRecursionDepth,
maxExecutionTime: this.maxExecutionTime,
maxMemoryUsage: this.maxMemoryUsage,
enableOptimizations: this.enableOptimizations
},
security: {
sandboxMode: this.sandboxMode,
allowUnsafeOperations: this.allowUnsafeOperations,
strictMode: this.strictMode,
allowedBuiltinsCount: this.allowedBuiltins.length
},
limits: {
maxSourceLength: this.maxSourceLength,
maxASTDepth: this.maxASTDepth,
maxListLength: this.maxListLength,
maxTableSize: this.maxTableSize
},
features: {
enableDebugMode: this.enableDebugMode,
enableProfiling: this.enableProfiling,
enableTypeChecking: this.enableTypeChecking,
enableCaching: this.enableCaching
}
};
}
}
|