@holoscript/core 1.0.0-alpha.2 → 2.0.1

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,494 @@
1
+ /**
2
+ * @holoscript/core Interoperability
3
+ *
4
+ * TypeScript module resolution, proper import/export handling,
5
+ * async/await support, error boundaries
6
+ */
7
+
8
+ import * as path from 'path';
9
+ import * as fs from 'fs';
10
+
11
+ /**
12
+ * Module resolver for TypeScript/JavaScript integration
13
+ */
14
+ export class ModuleResolver {
15
+ private cache: Map<string, any> = new Map();
16
+ private resolvedPaths: Map<string, string> = new Map();
17
+ private basePath: string;
18
+
19
+ constructor(basePath: string = process.cwd()) {
20
+ this.basePath = basePath;
21
+ }
22
+
23
+ /**
24
+ * Resolve module path
25
+ */
26
+ resolveModule(modulePath: string, fromPath?: string): string {
27
+ const cacheKey = `${modulePath}:${fromPath || 'root'}`;
28
+
29
+ if (this.resolvedPaths.has(cacheKey)) {
30
+ return this.resolvedPaths.get(cacheKey)!;
31
+ }
32
+
33
+ const resolved = this.performResolve(modulePath, fromPath);
34
+ this.resolvedPaths.set(cacheKey, resolved);
35
+ return resolved;
36
+ }
37
+
38
+ /**
39
+ * Perform actual module resolution
40
+ */
41
+ private performResolve(modulePath: string, fromPath?: string): string {
42
+ // Handle built-in modules
43
+ if (this.isBuiltinModule(modulePath)) {
44
+ return modulePath;
45
+ }
46
+
47
+ // Handle relative imports
48
+ if (modulePath.startsWith('.')) {
49
+ const basePath = fromPath ? path.dirname(fromPath) : this.basePath;
50
+ const resolved = path.resolve(basePath, modulePath);
51
+ return this.findModuleFile(resolved);
52
+ }
53
+
54
+ // Handle node_modules
55
+ if (modulePath.startsWith('@')) {
56
+ // Scoped package
57
+ return this.resolveNodeModule(modulePath);
58
+ }
59
+
60
+ // Standard package
61
+ return this.resolveNodeModule(modulePath);
62
+ }
63
+
64
+ /**
65
+ * Check if built-in module
66
+ */
67
+ private isBuiltinModule(modulePath: string): boolean {
68
+ const builtins = ['fs', 'path', 'crypto', 'util', 'stream', 'events'];
69
+ return builtins.includes(modulePath);
70
+ }
71
+
72
+ /**
73
+ * Resolve from node_modules
74
+ */
75
+ private resolveNodeModule(modulePath: string): string {
76
+ let current = this.basePath;
77
+
78
+ while (current !== path.dirname(current)) {
79
+ const nodeModulesPath = path.join(current, 'node_modules', modulePath);
80
+
81
+ if (fs.existsSync(nodeModulesPath)) {
82
+ return nodeModulesPath;
83
+ }
84
+
85
+ // Try package.json main field
86
+ const packageJsonPath = path.join(nodeModulesPath, 'package.json');
87
+
88
+ if (fs.existsSync(packageJsonPath)) {
89
+ const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'));
90
+ return this.findModuleFile(path.join(nodeModulesPath, packageJson.main || 'index.js'));
91
+ }
92
+
93
+ current = path.dirname(current);
94
+ }
95
+
96
+ throw new Error(`Module not found: ${modulePath}`);
97
+ }
98
+
99
+ /**
100
+ * Find module file with various extensions
101
+ */
102
+ private findModuleFile(basePath: string): string {
103
+ const extensions = ['.ts', '.tsx', '.js', '.jsx', '.json'];
104
+
105
+ // Try exact file
106
+ if (fs.existsSync(basePath)) {
107
+ return basePath;
108
+ }
109
+
110
+ // Try with extensions
111
+ for (const ext of extensions) {
112
+ const withExt = basePath + ext;
113
+ if (fs.existsSync(withExt)) {
114
+ return withExt;
115
+ }
116
+ }
117
+
118
+ // Try index
119
+ const indexPath = path.join(basePath, 'index');
120
+ for (const ext of extensions) {
121
+ const withExt = indexPath + ext;
122
+ if (fs.existsSync(withExt)) {
123
+ return withExt;
124
+ }
125
+ }
126
+
127
+ throw new Error(`Cannot resolve file: ${basePath}`);
128
+ }
129
+
130
+ /**
131
+ * Load module (with caching)
132
+ */
133
+ loadModule(modulePath: string, fromPath?: string): any {
134
+ const resolved = this.resolveModule(modulePath, fromPath);
135
+ const cacheKey = resolved;
136
+
137
+ if (this.cache.has(cacheKey)) {
138
+ return this.cache.get(cacheKey);
139
+ }
140
+
141
+ // For now, return module info (real implementation would execute)
142
+ const module = {
143
+ path: resolved,
144
+ exports: {},
145
+ };
146
+
147
+ this.cache.set(cacheKey, module);
148
+ return module;
149
+ }
150
+
151
+ /**
152
+ * Clear cache
153
+ */
154
+ clearCache(): void {
155
+ this.cache.clear();
156
+ this.resolvedPaths.clear();
157
+ }
158
+ }
159
+
160
+ /**
161
+ * Named export/import handler
162
+ */
163
+ export class ExportImportHandler {
164
+ private exports: Map<string, Map<string, any>> = new Map();
165
+
166
+ /**
167
+ * Define named export
168
+ */
169
+ defineExport(modulePath: string, name: string, value: any): void {
170
+ if (!this.exports.has(modulePath)) {
171
+ this.exports.set(modulePath, new Map());
172
+ }
173
+
174
+ this.exports.get(modulePath)!.set(name, value);
175
+ }
176
+
177
+ /**
178
+ * Get named export
179
+ */
180
+ getExport(modulePath: string, name: string): any {
181
+ const moduleExports = this.exports.get(modulePath);
182
+
183
+ if (!moduleExports) {
184
+ throw new Error(`Module not found: ${modulePath}`);
185
+ }
186
+
187
+ if (!moduleExports.has(name)) {
188
+ throw new Error(`Named export '${name}' not found in ${modulePath}`);
189
+ }
190
+
191
+ return moduleExports.get(name);
192
+ }
193
+
194
+ /**
195
+ * Get all exports from module
196
+ */
197
+ getAllExports(modulePath: string): Record<string, any> {
198
+ const moduleExports = this.exports.get(modulePath);
199
+
200
+ if (!moduleExports) {
201
+ return {};
202
+ }
203
+
204
+ const result: Record<string, any> = {};
205
+
206
+ for (const [name, value] of moduleExports) {
207
+ result[name] = value;
208
+ }
209
+
210
+ return result;
211
+ }
212
+
213
+ /**
214
+ * Check if export exists
215
+ */
216
+ hasExport(modulePath: string, name: string): boolean {
217
+ return this.exports.has(modulePath) && this.exports.get(modulePath)!.has(name);
218
+ }
219
+ }
220
+
221
+ /**
222
+ * Async function handler
223
+ */
224
+ export class AsyncFunctionHandler {
225
+ /**
226
+ * Wrap async function for HoloScript
227
+ */
228
+ wrapAsyncFunction(fn: (...args: any[]) => Promise<any>): (...args: any[]) => Promise<any> {
229
+ return async (...args: any[]) => {
230
+ try {
231
+ return await fn(...args);
232
+ } catch (error) {
233
+ throw this.normalizeError(error);
234
+ }
235
+ };
236
+ }
237
+
238
+ /**
239
+ * Check if function is async
240
+ */
241
+ isAsync(fn: any): boolean {
242
+ if (typeof fn !== 'function') {
243
+ return false;
244
+ }
245
+
246
+ const fnStr = fn.toString().trim();
247
+ return fnStr.startsWith('async ') || fn.constructor.name === 'AsyncFunction';
248
+ }
249
+
250
+ /**
251
+ * Convert callback to promise
252
+ */
253
+ callbackToPromise(
254
+ fn: (callback: (err: any, result: any) => void) => void
255
+ ): Promise<any> {
256
+ return new Promise((resolve, reject) => {
257
+ fn((err: any, result: any) => {
258
+ if (err) {
259
+ reject(err);
260
+ } else {
261
+ resolve(result);
262
+ }
263
+ });
264
+ });
265
+ }
266
+
267
+ /**
268
+ * Normalize error from async function
269
+ */
270
+ private normalizeError(error: any): Error {
271
+ if (error instanceof Error) {
272
+ return error;
273
+ }
274
+
275
+ if (typeof error === 'string') {
276
+ return new Error(error);
277
+ }
278
+
279
+ return new Error(JSON.stringify(error));
280
+ }
281
+ }
282
+
283
+ /**
284
+ * Error boundary for isolation
285
+ */
286
+ export class ErrorBoundary {
287
+ private errors: Error[] = [];
288
+ private onError?: (error: Error) => void;
289
+
290
+ constructor(onError?: (error: Error) => void) {
291
+ this.onError = onError;
292
+ }
293
+
294
+ /**
295
+ * Wrap function with error boundary
296
+ */
297
+ wrap<T extends (...args: any[]) => any>(fn: T): T {
298
+ return ((...args: any[]) => {
299
+ try {
300
+ const result = fn(...args);
301
+
302
+ // Handle async functions
303
+ if (result instanceof Promise) {
304
+ return result.catch((error: any) => {
305
+ this.handleError(error);
306
+ throw error;
307
+ });
308
+ }
309
+
310
+ return result;
311
+ } catch (error: any) {
312
+ this.handleError(error);
313
+ throw error;
314
+ }
315
+ }) as T;
316
+ }
317
+
318
+ /**
319
+ * Wrap async function with error boundary
320
+ */
321
+ wrapAsync<T extends (...args: any[]) => Promise<any>>(fn: T): T {
322
+ return (async (...args: any[]) => {
323
+ try {
324
+ return await fn(...args);
325
+ } catch (error: any) {
326
+ this.handleError(error);
327
+ throw error;
328
+ }
329
+ }) as T;
330
+ }
331
+
332
+ /**
333
+ * Execute function in boundary
334
+ */
335
+ execute<T>(fn: () => T): T {
336
+ try {
337
+ return fn();
338
+ } catch (error: any) {
339
+ this.handleError(error);
340
+ throw error;
341
+ }
342
+ }
343
+
344
+ /**
345
+ * Execute async function in boundary
346
+ */
347
+ async executeAsync<T>(fn: () => Promise<T>): Promise<T> {
348
+ try {
349
+ return await fn();
350
+ } catch (error: any) {
351
+ this.handleError(error);
352
+ throw error;
353
+ }
354
+ }
355
+
356
+ /**
357
+ * Handle error
358
+ */
359
+ private handleError(error: any): void {
360
+ const normalized = this.normalizeError(error);
361
+ this.errors.push(normalized);
362
+
363
+ if (this.onError) {
364
+ this.onError(normalized);
365
+ }
366
+ }
367
+
368
+ /**
369
+ * Get errors
370
+ */
371
+ getErrors(): Error[] {
372
+ return [...this.errors];
373
+ }
374
+
375
+ /**
376
+ * Clear errors
377
+ */
378
+ clearErrors(): void {
379
+ this.errors = [];
380
+ }
381
+
382
+ /**
383
+ * Normalize error
384
+ */
385
+ private normalizeError(error: any): Error {
386
+ if (error instanceof Error) {
387
+ return error;
388
+ }
389
+
390
+ if (typeof error === 'string') {
391
+ return new Error(error);
392
+ }
393
+
394
+ return new Error(JSON.stringify(error));
395
+ }
396
+ }
397
+
398
+ /**
399
+ * TypeScript type loader
400
+ */
401
+ export class TypeScriptTypeLoader {
402
+ /**
403
+ * Load types from TypeScript declaration file
404
+ */
405
+ loadTypes(filePath: string): Map<string, any> {
406
+ const types = new Map<string, any>();
407
+
408
+ // In real implementation, would parse .d.ts file
409
+ // For now, return empty map
410
+ return types;
411
+ }
412
+
413
+ /**
414
+ * Convert TypeScript type to HoloScript type
415
+ */
416
+ convertType(tsType: any): any {
417
+ // Basic type mapping
418
+ const mapping: Record<string, string> = {
419
+ string: 'text',
420
+ number: 'numeric',
421
+ boolean: 'logical',
422
+ void: 'void',
423
+ any: 'dynamic',
424
+ unknown: 'dynamic',
425
+ null: 'null',
426
+ undefined: 'void',
427
+ };
428
+
429
+ if (typeof tsType === 'string') {
430
+ return mapping[tsType] || tsType;
431
+ }
432
+
433
+ if (Array.isArray(tsType)) {
434
+ return {
435
+ kind: 'array',
436
+ elementType: this.convertType(tsType[0]),
437
+ };
438
+ }
439
+
440
+ if (typeof tsType === 'object') {
441
+ return {
442
+ kind: 'object',
443
+ properties: Object.entries(tsType).reduce(
444
+ (acc, [key, val]) => {
445
+ acc[key] = this.convertType(val);
446
+ return acc;
447
+ },
448
+ {} as Record<string, any>
449
+ ),
450
+ };
451
+ }
452
+
453
+ return tsType;
454
+ }
455
+ }
456
+
457
+ /**
458
+ * Unified interop context
459
+ */
460
+ export class InteropContext {
461
+ private moduleResolver: ModuleResolver;
462
+ private exportImportHandler: ExportImportHandler;
463
+ private asyncHandler: AsyncFunctionHandler;
464
+ private errorBoundary: ErrorBoundary;
465
+ private typeLoader: TypeScriptTypeLoader;
466
+
467
+ constructor(basePath?: string) {
468
+ this.moduleResolver = new ModuleResolver(basePath);
469
+ this.exportImportHandler = new ExportImportHandler();
470
+ this.asyncHandler = new AsyncFunctionHandler();
471
+ this.errorBoundary = new ErrorBoundary();
472
+ this.typeLoader = new TypeScriptTypeLoader();
473
+ }
474
+
475
+ getModuleResolver(): ModuleResolver {
476
+ return this.moduleResolver;
477
+ }
478
+
479
+ getExportImportHandler(): ExportImportHandler {
480
+ return this.exportImportHandler;
481
+ }
482
+
483
+ getAsyncHandler(): AsyncFunctionHandler {
484
+ return this.asyncHandler;
485
+ }
486
+
487
+ getErrorBoundary(): ErrorBoundary {
488
+ return this.errorBoundary;
489
+ }
490
+
491
+ getTypeLoader(): TypeScriptTypeLoader {
492
+ return this.typeLoader;
493
+ }
494
+ }
package/src/logger.js ADDED
@@ -0,0 +1,42 @@
1
+ /**
2
+ * @holoscript/core Logger
3
+ *
4
+ * Simple pluggable logger for HoloScript
5
+ */
6
+ class NoOpLogger {
7
+ debug() { }
8
+ info() { }
9
+ warn() { }
10
+ error() { }
11
+ }
12
+ class ConsoleLogger {
13
+ debug(message, meta) {
14
+ console.debug(`[HoloScript:DEBUG] ${message}`, meta ?? '');
15
+ }
16
+ info(message, meta) {
17
+ console.info(`[HoloScript:INFO] ${message}`, meta ?? '');
18
+ }
19
+ warn(message, meta) {
20
+ console.warn(`[HoloScript:WARN] ${message}`, meta ?? '');
21
+ }
22
+ error(message, meta) {
23
+ console.error(`[HoloScript:ERROR] ${message}`, meta ?? '');
24
+ }
25
+ }
26
+ let currentLogger = new NoOpLogger();
27
+ export function setHoloScriptLogger(logger) {
28
+ currentLogger = logger;
29
+ }
30
+ export function enableConsoleLogging() {
31
+ currentLogger = new ConsoleLogger();
32
+ }
33
+ export function resetLogger() {
34
+ currentLogger = new NoOpLogger();
35
+ }
36
+ export const logger = {
37
+ debug: (msg, meta) => currentLogger.debug(msg, meta),
38
+ info: (msg, meta) => currentLogger.info(msg, meta),
39
+ warn: (msg, meta) => currentLogger.warn(msg, meta),
40
+ error: (msg, meta) => currentLogger.error(msg, meta),
41
+ };
42
+ export { NoOpLogger, ConsoleLogger };