@@ -10,25 +10,45 @@ def setup(native_options)
10
10
11
11
list = Native . make_function_list ( Script . custom_functions . count )
12
12
13
- functs = FunctionWrapper . extend ( Script ::Functions )
14
- functs . options = @options
13
+ functions = FunctionWrapper . extend ( Script ::Functions )
14
+ functions . options = @options
15
15
16
16
Script . custom_functions . each_with_index do |custom_function , i |
17
- @callbacks [ custom_function ] = FFI ::Function . new ( :pointer , [ :pointer , :pointer ] ) do |s_args , cookie |
18
- length = Native . list_get_length ( s_args )
17
+ @callbacks [ custom_function ] = FFI ::Function . new ( :pointer , [ :pointer , :pointer ] ) do |native_argument_list , cookie |
18
+ native_argument_list_length = Native . list_get_length ( native_argument_list )
19
+ custom_function_arguments = [ ]
20
+ error_tag = nil
19
21
20
- v = Native . list_get_value ( s_args , 0 )
21
- v = Native . string_get_value ( v ) . dup
22
+ ( 0 ... native_argument_list_length ) . each do | i |
23
+ native_value = Native . list_get_value ( native_argument_list , i )
22
24
23
- s = Script ::String . new ( Script ::String . unquote ( v ) , Script ::String . type ( v ) )
25
+ case value_tag = Native . value_get_tag ( native_value )
26
+ when :sass_null
27
+ # no-op
28
+ when :sass_string
29
+ native_string = Native . string_get_value ( native_value )
30
+ argument = Script ::String . new ( Script ::String . unquote ( native_string ) , Script ::String . type ( native_string ) )
24
31
25
- value = functs . send ( custom_function , s )
32
+ custom_function_arguments << argument
33
+ else
34
+ error_tag = error ( "Sass argument of type #{ value_tag } unsupported" )
35
+ break
36
+ end
37
+ end
38
+
39
+ next error_tag if error_tag
26
40
27
- if value
28
- value = Script ::String . new ( Script ::String . unquote ( value . to_s ) , value . type )
29
- value . to_native
30
- else
31
- Script ::String . new ( "" ) . to_native
41
+ begin
42
+ value = functions . send ( custom_function , *custom_function_arguments )
43
+
44
+ if value
45
+ value = Script ::String . new ( Script ::String . unquote ( value . to_s ) , value . type )
46
+ value . to_native
47
+ else
48
+ Script ::String . new ( "" ) . to_native
49
+ end
50
+ rescue StandardError => exception
51
+ error ( exception . message )
32
52
end
33
53
end
34
54
@@ -48,6 +68,21 @@ def setup(native_options)
48
68
49
69
private
50
70
71
+ def error ( message )
72
+ value = Native ::SassValue . new
73
+ value [ :unknown ] = Native ::SassUnknown . new
74
+
75
+ error = Native ::SassError . new
76
+ error [ :tag ] = :sass_error
77
+
78
+ Native . error_set_message ( error , Native . native_string ( message ) )
79
+ $stderr. puts "[SassC::FunctionsHandler] #{ message } "
80
+
81
+ value [ :unknown ] [ :tag ] = :sass_error
82
+ value [ :error ] = error
83
+ value . pointer
84
+ end
85
+
51
86
class FunctionWrapper
52
87
class << self
53
88
attr_accessor :options
0 commit comments