File: import_handler.rb

package info (click to toggle)
ruby-sassc 2.4.0%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 416 kB
  • sloc: ruby: 2,277; makefile: 3
file content (50 lines) | stat: -rw-r--r-- 1,354 bytes parent folder | download | duplicates (3)
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
# frozen_string_literal: true

module SassC
  class ImportHandler
    def initialize(options)
      @importer = if options[:importer]
        options[:importer].new(options)
      else
        nil
      end
    end

    def setup(native_options)
      return unless @importer

      importer_callback = Native.make_importer(import_function, nil)

      list = Native.make_function_list(1)
      Native::function_set_list_entry(list, 0, importer_callback)

      Native.option_set_c_importers(native_options, list)
    end

    private

    def import_function
      @import_function ||= FFI::Function.new(:pointer, [:string, :pointer, :pointer]) do |path, importer_entry, compiler|
        last_import = Native::compiler_get_last_import(compiler)
        parent_path = Native::import_get_abs_path(last_import)

        imports = [*@importer.imports(path, parent_path)]
        imports_to_native(imports)
      end
    end

    def imports_to_native(imports)
      import_list = Native.make_import_list(imports.size)

      imports.each_with_index do |import, i|
        source = import.source ? Native.native_string(import.source) : nil
        source_map_path = nil

        entry = Native.make_import_entry(import.path, source, source_map_path)
        Native.import_set_list_entry(import_list, i, entry)
      end

      import_list
    end
  end
end