Sha256: cd591035089ee2e1498844ad422d29386b1d5b661e5efc7fe419bc66584d0ba2

Contents?: true

Size: 1.87 KB

Versions: 3

Compression:

Stored size: 1.87 KB

Contents

module SassC
  module Script
    def self.custom_functions
      Functions.instance_methods.select do |function|
        Functions.public_method_defined?(function)
      end
    end

    def self.setup_custom_functions(options, sass_options)
      callbacks = {}

      list = Native.make_function_list(custom_functions.count)

      functs = Class.new.extend(Functions)
      def functs.options=(opts)
        @sass_options = opts
      end
      def functs.options
        @sass_options
      end
      functs.options = sass_options

      custom_functions.each_with_index do |custom_function, i|
        callbacks[custom_function] = FFI::Function.new(:pointer, [:pointer, :pointer]) do |s_args, cookie|
          length = Native.list_get_length(s_args)

          v = Native.list_get_value(s_args, 0)
          v = Native.string_get_value(v).dup

          s = String.new(String.unquote(v), String.type(v))

          value = functs.send(custom_function, s)

          if value
            value = String.new(String.unquote(value.to_s), value.type)
            value.to_native
          else
            String.new("").to_native
          end
        end

        callback = Native.make_function(
          formatted_function_name(custom_function),
          callbacks[custom_function],
          nil
        )

        Native::function_set_list_entry(list, i, callback)
      end

      Native::option_set_c_functions(options, list)

      status = yield

      callbacks

      status
    end

    def self.formatted_function_name(function_name)
      params = Functions.instance_method(function_name).parameters
      params = params.select { |param| param[0] == :req }
                     .map(&:first)
                     .map { |p| "$#{p}" }
                     .join(", ")
      "#{function_name}(#{params})"
    end
  end
end

require_relative "script/functions"
require_relative "script/string"

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
sassc-0.0.10 lib/sassc/script.rb
sassc-0.0.9 lib/sassc/script.rb
sassc-0.0.8 lib/sassc/script.rb