File size: 3,850 Bytes
4e7644b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * moduleRegistry.ts
 *
 * Runtime module registry for plugin system monkey-patching
 *
 * How it works:
 * 1. Host app calls moduleRegistry.register() at startup to register all @patchable modules
 * 2. Plugins access and modify module exports via window.QwenPaw.modules
 * 3. Host code accesses modules via moduleRegistry.get/call to ensure using plugin-modified versions
 */

export interface ModuleRegistry {
  /**
   * Register a module (called by generated registerHostModules())
   */
  register(key: string, module: Record<string, unknown>): void;

  /**
   * Get a module export value (for const/let/var types)
   */
  get(moduleKey: string, exportName: string): unknown;

  /**
   * Call a module export function (for function/class types)
   */
  call(moduleKey: string, exportName: string, ...args: unknown[]): unknown;

  /**
   * Get all registered module keys
   */
  keys(): string[];

  /**
   * Get entire module object (for plugin use)
   */
  getModule(key: string): Record<string, unknown> | undefined;
}

class ModuleRegistryImpl implements ModuleRegistry {
  private modules = new Map<string, Record<string, unknown>>();

  register(key: string, module: Record<string, unknown>): void {
    // Safely copy module exports, avoiding ES Module namespace special properties
    const safeCopy: Record<string, unknown> = {};

    try {
      // Only copy enumerable own properties
      for (const exportName of Object.keys(module)) {
        try {
          const descriptor = Object.getOwnPropertyDescriptor(
            module,
            exportName,
          );
          if (descriptor && descriptor.enumerable) {
            // Read the current value (works for both plain properties and getters)
            safeCopy[exportName] = module[exportName];
          }
        } catch (e) {
          // Skip inaccessible properties
          if (console && console.warn) {
            console.warn(
              `[moduleRegistry] Cannot copy property ${exportName} from ${key}:`,
              e,
            );
          }
        }
      }

      this.modules.set(key, safeCopy);
    } catch (err) {
      if (console && console.error) {
        console.error(
          `[moduleRegistry] Failed to register module: ${key}`,
          err,
        );
      }
    }
  }

  get(moduleKey: string, exportName: string): unknown {
    const mod = this.modules.get(moduleKey);
    if (!mod) {
      console.warn(`[moduleRegistry] Module not found: ${moduleKey}`);
      return undefined;
    }
    return mod[exportName];
  }

  call(moduleKey: string, exportName: string, ...args: unknown[]): unknown {
    const fn = this.get(moduleKey, exportName);
    if (typeof fn !== "function") {
      console.error(
        `[moduleRegistry] Export "${exportName}" in "${moduleKey}" is not callable`,
      );
      return undefined;
    }
    return fn(...args);
  }

  keys(): string[] {
    return Array.from(this.modules.keys());
  }

  getModule(key: string): Record<string, unknown> | undefined {
    return this.modules.get(key);
  }

  /**
   * Get all modules (for window.QwenPaw.modules)
   */
  getAllModules(): Record<string, Record<string, unknown>> {
    const result: Record<string, Record<string, unknown>> = {};
    for (const [key, mod] of this.modules) {
      result[key] = mod;
    }
    return result;
  }
}

export const moduleRegistry = new ModuleRegistryImpl();

// Expose to window.QwenPaw.modules (for plugin use)
// Set during initialization
if (typeof window !== "undefined") {
  if (!window.QwenPaw) {
    (window as any).QwenPaw = {};
  }

  // Use Proxy for dynamic access, ensuring plugins always get latest module state
  Object.defineProperty(window.QwenPaw, "modules", {
    get() {
      return (moduleRegistry as any).getAllModules();
    },
    configurable: true,
    enumerable: true,
  });
}