Class: MilkTea::ModuleLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/milk_tea/core/module_loader.rb,
lib/milk_tea/core/module_loader/errors.rb

Defined Under Namespace

Classes: ImportResolution, ImportResolutionError, Program

Constant Summary collapse

PLATFORM_SUFFIXES =
{
  "linux" => :linux,
  "windows" => :windows,
  "wasm" => :wasm,
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(module_roots: [MilkTea.root], package_graph: nil, shared_cache: nil, source_overrides: nil, platform: nil) ⇒ ModuleLoader

Returns a new instance of ModuleLoader.



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
# File 'lib/milk_tea/core/module_loader.rb', line 93

def initialize(module_roots: [MilkTea.root], package_graph: nil, shared_cache: nil, source_overrides: nil, platform: nil)
  @module_roots = module_roots.map { |root| File.expand_path(root.to_s) }
  @ast_cache = {}
  @parse_cache = {}
  @analysis_cache = {}
  @collecting_analysis_cache = {}
  @collecting_path_errors = {}
  @checking_paths = []
  @platform = self.class.normalize_platform_name(platform)
  @package_graph = package_graph
  @package_manifest_cache = {}
  @shared_cache = shared_cache # Hash or nil; mutated in-place to persist across calls
  @source_overrides = normalize_source_overrides(source_overrides)

  @path_resolver = ModulePathResolver.new(
    module_roots: @module_roots,
    platform: @platform,
    package_graph: @package_graph,
    source_overrides: @source_overrides,
    package_manifest_cache: @package_manifest_cache,
  )
  @binder = ModuleBinder.new
  @async_runtime_installer = AsyncRuntimeInstaller.new(
    resolve_module_path: @path_resolver.method(:resolve_module_path),
    check_block: ->(path, collecting) { collecting ? check_path_collecting_errors(path) : check_path(path) },
    bind_block: @binder.method(:module_binding),
  )
  @prelude_installer = PreludeInstaller.new(
    resolve_module_path: @path_resolver.method(:resolve_module_path),
    check_block: ->(path, collecting) { collecting ? check_path_collecting_errors(path) : check_path(path) },
    bind_block: @binder.method(:module_binding),
  )
end

Class Method Details

.check_file(path, platform: nil) ⇒ Object



22
23
24
# File 'lib/milk_tea/core/module_loader.rb', line 22

def self.check_file(path, platform: nil)
  new(platform:).check_file(path)
end

.check_program(path, platform: nil) ⇒ Object



26
27
28
# File 'lib/milk_tea/core/module_loader.rb', line 26

def self.check_program(path, platform: nil)
  new(platform:).check_program(path)
end

.default_host_platformObject



89
90
91
# File 'lib/milk_tea/core/module_loader.rb', line 89

def self.default_host_platform
  MilkTea.host_platform
end

.effective_platform_for_path(path, platform_override: nil, host_platform: nil) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/milk_tea/core/module_loader.rb', line 54

def self.effective_platform_for_path(path, platform_override: nil, host_platform: nil)
  normalized_override = normalize_platform_name(platform_override)
  return normalized_override if normalized_override

  suffix_platform = platform_suffix_for_path(path)
  return suffix_platform if suffix_platform

  manifest_platform = PackageManifest.load(path).platform
  return manifest_platform if manifest_platform

  normalize_platform_name(host_platform || default_host_platform)
rescue PackageManifestError
  normalize_platform_name(host_platform || default_host_platform)
end

.load_file(path, platform: nil) ⇒ Object



18
19
20
# File 'lib/milk_tea/core/module_loader.rb', line 18

def self.load_file(path, platform: nil)
  new(platform:).load_file(path)
end

.normalize_platform_name(value) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/milk_tea/core/module_loader.rb', line 30

def self.normalize_platform_name(value)
  return nil if value.nil? || value.to_s.strip.empty?

  case value.to_s.strip.downcase
  when "linux"
    :linux
  when "windows", "win", "win32"
    :windows
  when "wasm", "web", "html5", "browser"
    :wasm
  when "darwin", "macos", "osx"
    :darwin
  else
    raise ArgumentError, "unknown platform #{value}; expected linux|windows|wasm|darwin"
  end
end

.platform_suffix_for_path(path) ⇒ Object



47
48
49
50
51
52
# File 'lib/milk_tea/core/module_loader.rb', line 47

def self.platform_suffix_for_path(path)
  match = File.basename(path.to_s).match(/\.(linux|windows|wasm)\.mt\z/)
  return nil unless match

  PLATFORM_SUFFIXES.fetch(match[1])
end

.resolve_source_path(path, platform: nil, error_class: nil) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/milk_tea/core/module_loader.rb', line 69

def self.resolve_source_path(path, platform: nil, error_class: nil)
  expanded_path = File.expand_path(path.to_s)
  normalized_platform = platform.nil? ? nil : normalize_platform_name(platform)
  pinned_platform = platform_suffix_for_path(expanded_path)

  if pinned_platform
    if normalized_platform && normalized_platform != pinned_platform
      raise_platform_conflict!(expanded_path, pinned_platform, normalized_platform, error_class:)
    end
    return expanded_path
  end

  return expanded_path unless normalized_platform && expanded_path.end_with?(".mt")

  variant_path = expanded_path.sub(/\.mt\z/, ".#{normalized_platform}.mt")
  return variant_path if File.file?(variant_path)

  expanded_path
end

Instance Method Details

#build_global_import_index(ast) ⇒ Object



264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/milk_tea/core/module_loader.rb', line 264

def build_global_import_index(ast)
  index = {}
  current_imports = ast.imports.map { |import| import.path.to_s }.to_set

  @analysis_cache.each_value do |analysis|
    next unless analysis
    next unless analysis.module_name

    mod_name = analysis.module_name.to_s
    next if current_imports.include?(mod_name)
    next if mod_name == ast.module_name.to_s

    types = analysis.respond_to?(:types) ? analysis.types : {}
    types.each_key do |type_name|
      type_str = type_name.to_s
      index[type_str] ||= []
      index[type_str] << mod_name unless index[type_str].include?(mod_name)
    end
  end

  index
end

#check_file(path) ⇒ Object



132
133
134
# File 'lib/milk_tea/core/module_loader.rb', line 132

def check_file(path)
  check_program(path).root_analysis
end

#check_level_parallel(paths) ⇒ Object



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/milk_tea/core/module_loader.rb', line 230

def check_level_parallel(paths)
  threads = paths.map do |resolved_path|
    Thread.new do
      Thread.current[:resolved_path] = resolved_path
      begin
        analysis = check_path(resolved_path)
        Thread.current[:analysis] = analysis
      rescue ModuleLoadError, PackageLockError, SemanticError => e
        Thread.current[:error] = e
      end
    end
  end

  threads.each(&:join)

  paths.zip(threads).each do |resolved_path, t|
    raise t[:error] if t[:error]
  end
end

#check_program(path) ⇒ Object



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
# File 'lib/milk_tea/core/module_loader.rb', line 136

def check_program(path)
  Types::Registry.reset!
  requested_path = File.expand_path(path)
  previous_platform = @platform
  @platform ||= self.class.platform_suffix_for_path(requested_path)
  root_path = self.class.resolve_source_path(requested_path, platform: @platform, error_class: ModuleLoadError)

  check_program_parallel(root_path)

  root_analysis = @analysis_cache.fetch(root_path)
  analyses_by_module_name = @analysis_cache.each_value.each_with_object({}) do |analysis, modules|
    next unless analysis.module_name

    modules[analysis.module_name] = analysis
  end

  Program.new(
    root_path:,
    root_analysis:,
    analyses_by_path: @analysis_cache.dup.freeze,
    analyses_by_module_name: analyses_by_module_name.freeze,
  )
ensure
  @platform = previous_platform
end

#check_program_parallel(root_path) ⇒ Object



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
# File 'lib/milk_tea/core/module_loader.rb', line 162

def check_program_parallel(root_path)
  # Phase 1: Parse all transitive modules (sequential)
  parse_all(root_path)

  # Phase 2: Build dependency graph from parsed ASTs
  graph = {}
  @parse_cache.each_key do |resolved_path|
    ast = @parse_cache[resolved_path]
    deps = ast.imports.map do |import|
      @path_resolver.resolve_module_path(import.path.to_s, importer_path: resolved_path, importer_module_name: ast.module_name.to_s)
    end
    graph[resolved_path] = deps
  end

  # Phase 3: Topological sort into independent levels
  levels = topo_sort_levels(graph)

  # Phase 4: Check each level (parallel within level, sequential across levels)
  levels.each do |level_paths|
    if level_paths.length == 1
      check_path(level_paths.first)
    else
      check_level_parallel(level_paths)
    end
  end
end

#collecting_path_errorsObject

Errors collected per analyzed import path during collecting-mode checks. Populated by #check_path_collecting_errors; used by the CLI check command to surface errors in a single file's imported modules (otherwise only reported when that module is checked directly).



320
321
322
# File 'lib/milk_tea/core/module_loader.rb', line 320

def collecting_path_errors
  @collecting_path_errors
end

#imported_modules_for_ast(ast, importer_path: nil) ⇒ Object



250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/milk_tea/core/module_loader.rb', line 250

def imported_modules_for_ast(ast, importer_path: nil)
  modules = {}

  ast.imports.each do |import|
    import_path = @path_resolver.resolve_module_path(import.path.to_s, importer_path:, importer_module_name: ast.module_name.to_s)
    import_analysis = check_path(import_path)
    modules[import.path.to_s] = @binder.module_binding(import_analysis)
  end

  @async_runtime_installer.install_async_runtime_dependency!(ast, modules, importer_path:, collecting_errors: false)
  @prelude_installer.install_prelude_modules!(ast, modules, importer_path:, collecting_errors: false)
  modules.freeze
end

#imported_modules_for_ast_collecting_errors(ast, importer_path: nil) ⇒ Object



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
# File 'lib/milk_tea/core/module_loader.rb', line 287

def imported_modules_for_ast_collecting_errors(ast, importer_path: nil)
  modules = {}
  errors = []

  ast.imports.each do |import|
    begin
      import_path = @path_resolver.resolve_module_path(import.path.to_s, importer_path:, importer_module_name: ast.module_name.to_s)
      import_analysis = check_path_collecting_errors(import_path)
      modules[import.path.to_s] = @binder.module_binding(import_analysis)
    rescue ModuleLoadError, PackageLockError, SemanticError => e
      errors << ImportResolutionError.new(import:, error: e)
    end
  end

  begin
    @async_runtime_installer.install_async_runtime_dependency!(ast, modules, importer_path:, collecting_errors: true)
  rescue ModuleLoadError, PackageLockError => e
    errors << ImportResolutionError.new(import: nil, error: e)
  end

  begin
    @prelude_installer.install_prelude_modules!(ast, modules, importer_path:, collecting_errors: true)
  rescue ModuleLoadError, PackageLockError => e
    errors << ImportResolutionError.new(import: nil, error: e)
  end

  ImportResolution.new(modules: modules.freeze, errors: errors.freeze)
end

#load_file(path) ⇒ Object



127
128
129
130
# File 'lib/milk_tea/core/module_loader.rb', line 127

def load_file(path)
  resolved_path = self.class.resolve_source_path(path, platform: @platform, error_class: ModuleLoadError)
  @ast_cache[resolved_path] ||= parse_file(resolved_path)
end

#parse_all(resolved_path) ⇒ Object



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/milk_tea/core/module_loader.rb', line 189

def parse_all(resolved_path)
  return if @parse_cache.key?(resolved_path)

  if @checking_paths.include?(resolved_path)
    raise ModuleLoadError.new("cyclic import detected", path: resolved_path)
  end

  @checking_paths << resolved_path
  ast = load_file(resolved_path)
  @parse_cache[resolved_path] = ast

  ast.imports.each do |import|
    import_path = @path_resolver.resolve_module_path(import.path.to_s, importer_path: resolved_path, importer_module_name: ast.module_name.to_s)
    parse_all(import_path)
  end
ensure
  @checking_paths.pop if @checking_paths.last == resolved_path
end

#topo_sort_levels(graph) ⇒ Object



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/milk_tea/core/module_loader.rb', line 208

def topo_sort_levels(graph)
  in_degree = {}
  graph.each_key { |node| in_degree[node] = 0 }
  graph.each_value do |deps|
    deps.each { |dep| in_degree[dep] = (in_degree[dep] || 0) + 1 }
  end

  levels = []
  remaining = graph.keys.to_set
  until remaining.empty?
    level = remaining.select { |node| (in_degree[node] || 0) == 0 }
    break if level.empty?

    levels << level
    level.each do |node|
      remaining.delete(node)
      (graph[node] || []).each { |dep| in_degree[dep] -= 1 }
    end
  end
  levels
end