Skip to content

Commit 7354bcb

Browse files
committed
allow passing functions directly
1 parent 8b6f415 commit 7354bcb

File tree

4 files changed

+31
-13
lines changed

4 files changed

+31
-13
lines changed

lib/sassc/engine.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class Engine
1616
def initialize(template, options = {})
1717
@template = template
1818
@options = options
19+
@functions = options.delete(:functions) || Script::Functions
1920
end
2021

2122
def render
@@ -37,7 +38,7 @@ def render
3738
Native.option_set_omit_source_map_url(native_options, true) if omit_source_map_url?
3839

3940
import_handler.setup(native_options)
40-
functions_handler.setup(native_options)
41+
functions_handler.setup(native_options, functions: @functions)
4142

4243
status = Native.compile_data_context(data_context)
4344

lib/sassc/functions_handler.rb

+8-8
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,24 @@ def initialize(options)
66
@options = options
77
end
88

9-
def setup(native_options)
9+
def setup(native_options, functions: Script::Functions)
1010
@callbacks = {}
1111
@function_names = {}
1212

13-
list = Native.make_function_list(Script.custom_functions.count)
13+
list = Native.make_function_list(Script.custom_functions(functions: functions).count)
1414

1515
# use an anonymous class wrapper to avoid mutations in a threaded environment
16-
functions = Class.new do
16+
functions_wrapper = Class.new do
1717
attr_accessor :options
18-
include Script::Functions
18+
include functions
1919
end.new
20-
functions.options = @options
20+
functions_wrapper.options = @options
2121

22-
Script.custom_functions.each_with_index do |custom_function, i|
22+
Script.custom_functions(functions: functions).each_with_index do |custom_function, i|
2323
@callbacks[custom_function] = FFI::Function.new(:pointer, [:pointer, :pointer]) do |native_argument_list, cookie|
2424
begin
2525
function_arguments = arguments_from_native_list(native_argument_list)
26-
result = functions.send(custom_function, *function_arguments)
26+
result = functions_wrapper.send(custom_function, *function_arguments)
2727
to_native_value(result)
2828
rescue StandardError => exception
2929
# This rescues any exceptions that occur either in value conversion
@@ -32,7 +32,7 @@ def setup(native_options)
3232
end
3333
end
3434

35-
@function_names[custom_function] = Script.formatted_function_name(custom_function)
35+
@function_names[custom_function] = Script.formatted_function_name(custom_function, functions: functions)
3636

3737
callback = Native.make_function(
3838
@function_names[custom_function],

lib/sassc/script.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
module SassC
44
module Script
55

6-
def self.custom_functions
7-
Functions.public_instance_methods
6+
def self.custom_functions(functions: Functions)
7+
functions.public_instance_methods
88
end
99

10-
def self.formatted_function_name(function_name)
11-
params = Functions.instance_method(function_name).parameters
10+
def self.formatted_function_name(function_name, functions: Functions)
11+
params = functions.instance_method(function_name).parameters
1212
params = params.map { |param_type, name| "$#{name}#{': null' if param_type == :opt}" }.join(", ")
1313
return "#{function_name}(#{params})"
1414
end

test/functions_test.rb

+17
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,17 @@ def test_concurrency
201201
end
202202
end
203203

204+
def test_pass_custom_functions_as_a_parameter
205+
out = Engine.new("div { url: test-function(); }", {functions: ExternalFunctions}).render
206+
assert_match /custom_function/, out
207+
end
208+
209+
def test_pass_incompatible_type_to_custom_functions
210+
assert_raises(TypeError) do
211+
Engine.new("div { url: test-function(); }", {functions: Class.new}).render
212+
end
213+
end
214+
204215
private
205216

206217
def assert_sass(sass, expected_css)
@@ -319,5 +330,11 @@ def returns_sass_list
319330

320331
end
321332

333+
module ExternalFunctions
334+
def test_function
335+
SassC::Script::Value::String.new("custom_function", :string)
336+
end
337+
end
338+
322339
end
323340
end

0 commit comments

Comments
 (0)