Sha256: 48f42f5ba982a2e28bafb09e49c335c593f387922fa834658f84001cf420b907

Contents?: true

Size: 1.38 KB

Versions: 3

Compression:

Stored size: 1.38 KB

Contents

module SassC
  class FunctionsHandler
    def initialize(options)
      @options = options
    end

    def setup(native_options)
      callbacks = {}

      list = Native.make_function_list(Script.custom_functions.count)

      functs = FunctionWrapper.extend(Script::Functions)
      functs.options = @options

      Script.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 = Script::String.new(Script::String.unquote(v), Script::String.type(v))

          value = functs.send(custom_function, s)

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

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

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

      Native::option_set_c_functions(native_options, list)
    end

    private

    class FunctionWrapper
      class << self
        attr_accessor :options
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
sassc-1.1.0 lib/sassc/functions_handler.rb
sassc-1.0.0 lib/sassc/functions_handler.rb
sassc-0.0.11 lib/sassc/functions_handler.rb