@holoscript/core 1.0.0-alpha.2 → 2.0.0

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.
Files changed (74) hide show
  1. package/package.json +2 -2
  2. package/src/HoloScript2DParser.js +227 -0
  3. package/src/HoloScript2DParser.ts +5 -0
  4. package/src/HoloScriptCodeParser.js +1102 -0
  5. package/src/HoloScriptCodeParser.ts +145 -20
  6. package/src/HoloScriptDebugger.js +458 -0
  7. package/src/HoloScriptParser.js +338 -0
  8. package/src/HoloScriptPlusParser.js +371 -0
  9. package/src/HoloScriptPlusParser.ts +543 -0
  10. package/src/HoloScriptRuntime.js +1399 -0
  11. package/src/HoloScriptRuntime.test.js +351 -0
  12. package/src/HoloScriptRuntime.ts +17 -3
  13. package/src/HoloScriptTypeChecker.js +356 -0
  14. package/src/__tests__/GraphicsServices.test.js +357 -0
  15. package/src/__tests__/GraphicsServices.test.ts +427 -0
  16. package/src/__tests__/HoloScriptPlusParser.test.js +317 -0
  17. package/src/__tests__/HoloScriptPlusParser.test.ts +392 -0
  18. package/src/__tests__/integration.test.js +336 -0
  19. package/src/__tests__/performance.bench.js +218 -0
  20. package/src/__tests__/type-checker.test.js +60 -0
  21. package/src/__tests__/type-checker.test.ts +73 -0
  22. package/src/index.js +217 -0
  23. package/src/index.ts +158 -18
  24. package/src/interop/Interoperability.js +413 -0
  25. package/src/interop/Interoperability.ts +494 -0
  26. package/src/logger.js +42 -0
  27. package/src/parser/EnhancedParser.js +205 -0
  28. package/src/parser/EnhancedParser.ts +251 -0
  29. package/src/parser/HoloScriptPlusParser.js +928 -0
  30. package/src/parser/HoloScriptPlusParser.ts +1089 -0
  31. package/src/runtime/HoloScriptPlusRuntime.js +674 -0
  32. package/src/runtime/HoloScriptPlusRuntime.ts +861 -0
  33. package/src/runtime/PerformanceTelemetry.js +323 -0
  34. package/src/runtime/PerformanceTelemetry.ts +467 -0
  35. package/src/runtime/RuntimeOptimization.js +361 -0
  36. package/src/runtime/RuntimeOptimization.ts +416 -0
  37. package/src/services/HololandGraphicsPipelineService.js +506 -0
  38. package/src/services/HololandGraphicsPipelineService.ts +662 -0
  39. package/src/services/PlatformPerformanceOptimizer.js +356 -0
  40. package/src/services/PlatformPerformanceOptimizer.ts +503 -0
  41. package/src/state/ReactiveState.js +427 -0
  42. package/src/state/ReactiveState.ts +572 -0
  43. package/src/tools/DeveloperExperience.js +376 -0
  44. package/src/tools/DeveloperExperience.ts +438 -0
  45. package/src/traits/AIDriverTrait.js +322 -0
  46. package/src/traits/AIDriverTrait.test.js +329 -0
  47. package/src/traits/AIDriverTrait.test.ts +357 -0
  48. package/src/traits/AIDriverTrait.ts +474 -0
  49. package/src/traits/LightingTrait.js +313 -0
  50. package/src/traits/LightingTrait.test.js +410 -0
  51. package/src/traits/LightingTrait.test.ts +462 -0
  52. package/src/traits/LightingTrait.ts +505 -0
  53. package/src/traits/MaterialTrait.js +194 -0
  54. package/src/traits/MaterialTrait.test.js +286 -0
  55. package/src/traits/MaterialTrait.test.ts +329 -0
  56. package/src/traits/MaterialTrait.ts +324 -0
  57. package/src/traits/RenderingTrait.js +356 -0
  58. package/src/traits/RenderingTrait.test.js +363 -0
  59. package/src/traits/RenderingTrait.test.ts +427 -0
  60. package/src/traits/RenderingTrait.ts +555 -0
  61. package/src/traits/VRTraitSystem.js +740 -0
  62. package/src/traits/VRTraitSystem.ts +1040 -0
  63. package/src/traits/VoiceInputTrait.js +284 -0
  64. package/src/traits/VoiceInputTrait.test.js +226 -0
  65. package/src/traits/VoiceInputTrait.test.ts +252 -0
  66. package/src/traits/VoiceInputTrait.ts +401 -0
  67. package/src/types/AdvancedTypeSystem.js +226 -0
  68. package/src/types/AdvancedTypeSystem.ts +494 -0
  69. package/src/types/HoloScriptPlus.d.ts +853 -0
  70. package/src/types.js +6 -0
  71. package/src/types.ts +96 -1
  72. package/tsconfig.json +1 -1
  73. package/tsup.config.d.ts +2 -0
  74. package/tsup.config.js +18 -0
@@ -0,0 +1,503 @@
1
+ /**
2
+ * Platform Performance Optimization System
3
+ *
4
+ * Provides adaptive quality and performance tuning for:
5
+ * - Mobile devices (battery/bandwidth constrained)
6
+ * - VR/AR platforms (latency-critical, 90 FPS required)
7
+ * - Desktop (quality-focused, high resolution)
8
+ *
9
+ * Features:
10
+ * - Automatic quality adjustment based on device
11
+ * - Performance profiling and analysis
12
+ * - Bottleneck detection and mitigation
13
+ * - Cross-platform benchmark testing
14
+ */
15
+
16
+ // ============================================================================
17
+ // Device Capabilities
18
+ // ============================================================================
19
+
20
+ export interface DeviceInfo {
21
+ platform: 'mobile' | 'vr' | 'desktop';
22
+ gpuVendor: string;
23
+ gpuModel: string;
24
+ gpuMemory: number; // MB
25
+ cpuCores: number;
26
+ ramTotal: number; // MB
27
+ screenResolution: { width: number; height: number };
28
+ refreshRate: number;
29
+ isLowPowerMode?: boolean;
30
+ }
31
+
32
+ export interface DeviceCapabilities {
33
+ maxTextureResolution: number;
34
+ supportsCompression: boolean;
35
+ compressionFormats: string[];
36
+ maxSimultaneousLights: number;
37
+ shadowsSupported: boolean;
38
+ computeShaderSupported: boolean;
39
+ rayTracingSupported: boolean;
40
+ estimatedMemory: number;
41
+ }
42
+
43
+ // ============================================================================
44
+ // Performance Profile
45
+ // ============================================================================
46
+
47
+ export interface PerformanceProfile {
48
+ device: DeviceInfo;
49
+ capabilities: DeviceCapabilities;
50
+ targetFPS: number;
51
+ qualityLevel: 'low' | 'medium' | 'high' | 'ultra';
52
+ adaptiveQuality: boolean;
53
+ fpsTarget: number;
54
+ fpsMin: number;
55
+ cpuBudget: number; // ms per frame
56
+ gpuBudget: number; // ms per frame
57
+ }
58
+
59
+ // ============================================================================
60
+ // Performance Benchmark
61
+ // ============================================================================
62
+
63
+ export interface BenchmarkResult {
64
+ testName: string;
65
+ platform: string;
66
+ fps: number;
67
+ gpuFrameTime: number; // ms
68
+ cpuFrameTime: number; // ms
69
+ gpuMemoryUsed: number; // MB
70
+ trianglesPerSecond: number;
71
+ drawCallsPerSecond: number;
72
+ qualityLevel: string;
73
+ passed: boolean;
74
+ }
75
+
76
+ // ============================================================================
77
+ // Adaptive Quality System
78
+ // ============================================================================
79
+
80
+ export interface AdaptiveQualitySettings {
81
+ enabled: boolean;
82
+ checkInterval: number; // ms
83
+ fpsDeltaThreshold: number; // FPS change to trigger adjustment
84
+ memoryThreshold: number; // % of max allowed
85
+ temperatureThreshold?: number; // celsius (mobile)
86
+ }
87
+
88
+ // ============================================================================
89
+ // Compression & Recommendations
90
+ // ============================================================================
91
+
92
+ export type CompressionFormat =
93
+ | 'none'
94
+ | 'lz4'
95
+ | 'zstd'
96
+ | 'bc1'
97
+ | 'bc7'
98
+ | 'astc'
99
+ | 'etc2'
100
+ | 'pvrtc';
101
+
102
+ export interface PerformanceRecommendation {
103
+ category: 'texture' | 'geometry' | 'shader' | 'memory' | 'rendering';
104
+ priority: 'low' | 'medium' | 'high' | 'critical';
105
+ recommendation: string;
106
+ expectedImprovement: number; // percentage
107
+ estimatedCost: string; // complexity of implementation
108
+ }
109
+
110
+ // ============================================================================
111
+ // Platform Performance Optimizer
112
+ // ============================================================================
113
+
114
+ export class PlatformPerformanceOptimizer {
115
+ private deviceInfo: DeviceInfo;
116
+ private capabilities: DeviceCapabilities;
117
+ private profile: PerformanceProfile;
118
+ private adaptiveSettings: AdaptiveQualitySettings;
119
+
120
+ private currentFPS: number = 60;
121
+ private frameHistory: number[] = [];
122
+ private lastAdaptTime: number = 0;
123
+
124
+ constructor(device: DeviceInfo) {
125
+ this.deviceInfo = device;
126
+ this.capabilities = this.detectCapabilities(device);
127
+ this.profile = this.createProfile(device);
128
+ this.adaptiveSettings = this.getAdaptiveSettings(device.platform);
129
+ }
130
+
131
+ /**
132
+ * Detect device capabilities
133
+ */
134
+ private detectCapabilities(device: DeviceInfo): DeviceCapabilities {
135
+ let maxTexture = 2048;
136
+ let supportsCompression = true;
137
+ let compressionFormats: string[] = ['dxt', 'basis'];
138
+ let maxLights = 4;
139
+ let shadowsSupported = true;
140
+ let computeSupported = false;
141
+ let rayTracingSupported = false;
142
+
143
+ // Mobile capabilities
144
+ if (device.platform === 'mobile') {
145
+ maxTexture = 512;
146
+ maxLights = 2;
147
+ shadowsSupported = false;
148
+ compressionFormats = ['astc', 'pvrtc'];
149
+
150
+ // Check for low memory
151
+ if (device.gpuMemory < 1024) {
152
+ maxTexture = 256;
153
+ maxLights = 1;
154
+ supportsCompression = true;
155
+ }
156
+ }
157
+
158
+ // VR capabilities
159
+ if (device.platform === 'vr') {
160
+ maxTexture = 2048;
161
+ maxLights = 4;
162
+ shadowsSupported = true;
163
+ compressionFormats = ['basis', 'dxt'];
164
+ computeSupported = true;
165
+ }
166
+
167
+ // Desktop capabilities
168
+ if (device.platform === 'desktop') {
169
+ maxTexture = 4096;
170
+ maxLights = 8;
171
+ shadowsSupported = true;
172
+ compressionFormats = ['none', 'dxt', 'basis'];
173
+ computeSupported = true;
174
+ rayTracingSupported = true;
175
+ }
176
+
177
+ return {
178
+ maxTextureResolution: maxTexture,
179
+ supportsCompression,
180
+ compressionFormats,
181
+ maxSimultaneousLights: maxLights,
182
+ shadowsSupported,
183
+ computeShaderSupported: computeSupported,
184
+ rayTracingSupported,
185
+ estimatedMemory: Math.min(device.gpuMemory, 2048),
186
+ };
187
+ }
188
+
189
+ /**
190
+ * Create performance profile for device
191
+ */
192
+ private createProfile(device: DeviceInfo): PerformanceProfile {
193
+ let qualityLevel: 'low' | 'medium' | 'high' | 'ultra' = 'high';
194
+ let fpsTarget = 60;
195
+ let fpsMin = 30;
196
+ let cpuBudget = 16.67; // ms for 60 FPS
197
+ let gpuBudget = 16.67; // ms for 60 FPS
198
+
199
+ // Mobile
200
+ if (device.platform === 'mobile') {
201
+ qualityLevel = 'low';
202
+ fpsTarget = 30;
203
+ fpsMin = 24;
204
+ cpuBudget = 33.33; // ms for 30 FPS
205
+ gpuBudget = 33.33; // ms for 30 FPS
206
+ }
207
+
208
+ // VR (more demanding)
209
+ if (device.platform === 'vr') {
210
+ qualityLevel = 'high';
211
+ fpsTarget = 90;
212
+ fpsMin = 75;
213
+ cpuBudget = 11.11; // ms for 90 FPS
214
+ gpuBudget = 11.11; // ms for 90 FPS
215
+ }
216
+
217
+ // Desktop (quality-focused)
218
+ if (device.platform === 'desktop') {
219
+ qualityLevel = 'ultra';
220
+ fpsTarget = device.refreshRate || 120;
221
+ fpsMin = 60;
222
+ cpuBudget = 1000 / fpsTarget;
223
+ gpuBudget = 1000 / fpsTarget;
224
+ }
225
+
226
+ return {
227
+ device,
228
+ capabilities: this.capabilities,
229
+ targetFPS: fpsTarget,
230
+ qualityLevel,
231
+ adaptiveQuality: device.platform === 'mobile' || device.platform === 'vr',
232
+ fpsTarget,
233
+ fpsMin,
234
+ cpuBudget,
235
+ gpuBudget,
236
+ };
237
+ }
238
+
239
+ /**
240
+ * Get adaptive quality settings for platform
241
+ */
242
+ private getAdaptiveSettings(platform: string): AdaptiveQualitySettings {
243
+ if (platform === 'mobile') {
244
+ return {
245
+ enabled: true,
246
+ checkInterval: 500, // Check every 500ms
247
+ fpsDeltaThreshold: 5, // Adjust if FPS changes by 5+
248
+ memoryThreshold: 80, // Adjust if above 80% memory
249
+ temperatureThreshold: 45, // Adjust if above 45C
250
+ };
251
+ }
252
+
253
+ if (platform === 'vr') {
254
+ return {
255
+ enabled: true,
256
+ checkInterval: 300, // Check every 300ms
257
+ fpsDeltaThreshold: 3, // Adjust if FPS changes by 3+
258
+ memoryThreshold: 85, // Adjust if above 85% memory
259
+ };
260
+ }
261
+
262
+ // Desktop
263
+ return {
264
+ enabled: false, // No adaptive quality needed
265
+ checkInterval: 1000,
266
+ fpsDeltaThreshold: 0,
267
+ memoryThreshold: 90,
268
+ };
269
+ }
270
+
271
+ /**
272
+ * Optimize for device - returns recommended rendering settings
273
+ */
274
+ optimizeForDevice(): any {
275
+ const { platform, gpuMemory, isLowPowerMode } = this.deviceInfo;
276
+ const quality = this.profile.qualityLevel;
277
+
278
+ const settings = {
279
+ quality,
280
+ platform,
281
+ textureResolution: this.capabilities.maxTextureResolution,
282
+ compression: this.selectCompression(platform),
283
+ maxLights: this.capabilities.maxSimultaneousLights,
284
+ shadowsEnabled: this.capabilities.shadowsSupported,
285
+ lodEnabled: true,
286
+ cullingEnabled: true,
287
+ instancingEnabled: true,
288
+ gpuMemoryBudget: gpuMemory > 2048 ? 1024 : gpuMemory / 2,
289
+ };
290
+
291
+ // Mobile low power adjustments
292
+ if (isLowPowerMode && platform === 'mobile') {
293
+ settings.quality = 'low';
294
+ settings.textureResolution = 256;
295
+ settings.compression = 'astc';
296
+ settings.maxLights = 1;
297
+ settings.shadowsEnabled = false;
298
+ settings.lodEnabled = true;
299
+ settings.cullingEnabled = true;
300
+ }
301
+
302
+ return settings;
303
+ }
304
+
305
+ /**
306
+ * Select best compression format for platform
307
+ */
308
+ private selectCompression(platform: string): string {
309
+ const formats = this.capabilities.compressionFormats;
310
+
311
+ if (platform === 'mobile') {
312
+ return formats.includes('astc') ? 'astc' : formats[0];
313
+ }
314
+
315
+ if (platform === 'vr') {
316
+ return formats.includes('basis') ? 'basis' : formats[0];
317
+ }
318
+
319
+ // Desktop
320
+ return formats.includes('none') ? 'none' : formats[0];
321
+ }
322
+
323
+ /**
324
+ * Update frame metrics for adaptive quality
325
+ */
326
+ updateFrameMetrics(fps: number, gpuMemoryUsed: number, gpuFrameTime: number): void {
327
+ this.currentFPS = fps;
328
+ this.frameHistory.push(fps);
329
+
330
+ // Keep only last 30 frames
331
+ if (this.frameHistory.length > 30) {
332
+ this.frameHistory.shift();
333
+ }
334
+
335
+ // Check if adaptation is needed
336
+ if (this.adaptiveSettings.enabled) {
337
+ const now = Date.now();
338
+ if (now - this.lastAdaptTime > this.adaptiveSettings.checkInterval) {
339
+ this.checkAndAdapt(fps, gpuMemoryUsed);
340
+ this.lastAdaptTime = now;
341
+ }
342
+ }
343
+ }
344
+
345
+ /**
346
+ * Check and adapt quality settings
347
+ */
348
+ private checkAndAdapt(fps: number, gpuMemoryUsed: number): void {
349
+ const avgFps = this.getAverageFPS();
350
+ const memoryPercent = (gpuMemoryUsed / this.profile.capabilities.estimatedMemory) * 100;
351
+
352
+ let shouldDegrade = false;
353
+ let shouldImprove = false;
354
+
355
+ // Check FPS
356
+ if (avgFps < this.profile.fpsMin) {
357
+ shouldDegrade = true;
358
+ } else if (avgFps > this.profile.fpsTarget + this.adaptiveSettings.fpsDeltaThreshold) {
359
+ shouldImprove = true;
360
+ }
361
+
362
+ // Check memory
363
+ if (memoryPercent > this.adaptiveSettings.memoryThreshold) {
364
+ shouldDegrade = true;
365
+ }
366
+
367
+ if (shouldDegrade) {
368
+ this.degradeQuality();
369
+ } else if (shouldImprove) {
370
+ this.improveQuality();
371
+ }
372
+ }
373
+
374
+ /**
375
+ * Degrade quality for better performance
376
+ */
377
+ private degradeQuality(): void {
378
+ switch (this.profile.qualityLevel) {
379
+ case 'ultra':
380
+ this.profile.qualityLevel = 'high';
381
+ break;
382
+ case 'high':
383
+ this.profile.qualityLevel = 'medium';
384
+ break;
385
+ case 'medium':
386
+ this.profile.qualityLevel = 'low';
387
+ break;
388
+ case 'low':
389
+ // Already at minimum
390
+ break;
391
+ }
392
+ }
393
+
394
+ /**
395
+ * Improve quality for better visuals
396
+ */
397
+ private improveQuality(): void {
398
+ switch (this.profile.qualityLevel) {
399
+ case 'low':
400
+ this.profile.qualityLevel = 'medium';
401
+ break;
402
+ case 'medium':
403
+ this.profile.qualityLevel = 'high';
404
+ break;
405
+ case 'high':
406
+ if (this.profile.device.platform === 'desktop') {
407
+ this.profile.qualityLevel = 'ultra';
408
+ }
409
+ break;
410
+ case 'ultra':
411
+ // Already at maximum
412
+ break;
413
+ }
414
+ }
415
+
416
+ /**
417
+ * Get average FPS from history
418
+ */
419
+ private getAverageFPS(): number {
420
+ if (this.frameHistory.length === 0) {
421
+ return this.currentFPS;
422
+ }
423
+
424
+ const sum = this.frameHistory.reduce((a, b) => a + b, 0);
425
+ return sum / this.frameHistory.length;
426
+ }
427
+
428
+ /**
429
+ * Run performance benchmark
430
+ */
431
+ async runBenchmark(
432
+ name: string,
433
+ renderFunc: (iterations: number) => Promise<{ fps: number; gpuTime: number; cpuTime: number; triangles: number; drawCalls: number }>
434
+ ): Promise<BenchmarkResult> {
435
+ const iterations = this.getBenchmarkIterations();
436
+
437
+ console.log(`Running benchmark: ${name} (${iterations} iterations)...`);
438
+
439
+ const result = await renderFunc(iterations);
440
+
441
+ const fps = result.fps;
442
+ const passed = fps >= this.profile.fpsMin;
443
+
444
+ return {
445
+ testName: name,
446
+ platform: this.deviceInfo.platform,
447
+ fps,
448
+ gpuFrameTime: result.gpuTime,
449
+ cpuFrameTime: result.cpuTime,
450
+ gpuMemoryUsed: 0, // Would be populated from actual measurement
451
+ trianglesPerSecond: result.triangles * fps,
452
+ drawCallsPerSecond: result.drawCalls * fps,
453
+ qualityLevel: this.profile.qualityLevel,
454
+ passed,
455
+ };
456
+ }
457
+
458
+ /**
459
+ * Get benchmark iterations based on platform
460
+ */
461
+ private getBenchmarkIterations(): number {
462
+ switch (this.deviceInfo.platform) {
463
+ case 'mobile':
464
+ return 100;
465
+ case 'vr':
466
+ return 200;
467
+ case 'desktop':
468
+ return 300;
469
+ }
470
+ }
471
+
472
+ /**
473
+ * Get current performance profile
474
+ */
475
+ getProfile(): PerformanceProfile {
476
+ return { ...this.profile };
477
+ }
478
+
479
+ /**
480
+ * Get optimization recommendations
481
+ */
482
+ getRecommendations(): string[] {
483
+ const recommendations: string[] = [];
484
+ const avgFps = this.getAverageFPS();
485
+
486
+ if (avgFps < this.profile.fpsMin) {
487
+ recommendations.push(`FPS below target (${Math.round(avgFps)} < ${this.profile.fpsMin})`);
488
+ recommendations.push('Consider reducing quality preset');
489
+ recommendations.push('Enable texture compression');
490
+ recommendations.push('Reduce shadow quality');
491
+ }
492
+
493
+ if (this.profile.device.isLowPowerMode) {
494
+ recommendations.push('Device in low power mode');
495
+ recommendations.push('Consider reducing quality');
496
+ recommendations.push('Disable shadows and lights');
497
+ }
498
+
499
+ return recommendations;
500
+ }
501
+ }
502
+
503
+ export default PlatformPerformanceOptimizer;