Skip to content

Commit 46bd7dc

Browse files
committed
Refactor Nokogiri patch generation.
1 parent 971cdcd commit 46bd7dc

File tree

1 file changed

+18
-58
lines changed

1 file changed

+18
-58
lines changed

lib/truffle/truffle/patches/nokogiri_patches.rb

Lines changed: 18 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@ class NokogiriPatches
2323
xmlDocPtr doc = (xmlDocPtr)c;
2424
EOF
2525

26+
def self.patch_for_system_libraries(replacement)
27+
<<-EOF
28+
#ifdef NOKOGIRI_PACKAGED_LIBRARIES
29+
\\&
30+
#else
31+
#{replacement}
32+
#endif
33+
EOF
34+
end
35+
2636
PATCHES = {
2737
gem: 'nokogiri',
2838
patches: {
@@ -31,26 +41,14 @@ class NokogiriPatches
3141
# is called with. This works on MRI but causes an error in
3242
# TruffleRuby.
3343
match: 'static VALUE to_array(VALUE self, VALUE rb_node)',
34-
replacement: <<-EOF
35-
#ifdef NOKOGIRI_PACKAGED_LIBRARIES
36-
static VALUE to_array(VALUE self, VALUE rb_node)
37-
#else
38-
static VALUE to_array(VALUE self)
39-
#endif
40-
EOF
44+
replacement: patch_for_system_libraries('static VALUE to_array(VALUE self)')
4145
},
4246
],
4347
'xslt_stylesheet.c' => [
4448
{ # It is not currently possible to pass var args from native
4549
# functions to sulong, so we work round the issue here.
4650
match: 'va_list args;',
47-
replacement: <<-EOF
48-
#ifdef NOKOGIRI_PACKAGED_LIBRARIES
49-
va_list args;
50-
#else
51-
va_list args; rb_str_cat2(ctx, "Generic error"); return;
52-
#endif
53-
EOF
51+
replacement: patch_for_system_libraries('va_list args; rb_str_cat2(ctx, "Generic error"); return;')
5452
}
5553
],
5654
'xml_document.c' => [
@@ -63,69 +61,31 @@ class NokogiriPatches
6361
{ # It is not currently possible to pass var args from native
6462
# functions to sulong, so we work round the issue here.
6563
match: /va_list args;[^}]*id_warning, 1, ruby_message\);/,
66-
replacement: <<-EOF
67-
#ifdef NOKOGIRI_PACKAGED_LIBRARIES
68-
\\&
69-
#else
70-
rb_funcall(doc, id_warning, 1, NOKOGIRI_STR_NEW2("Warning."));
71-
#endif
72-
EOF
64+
replacement: patch_for_system_libraries('rb_funcall(doc, id_warning, 1, NOKOGIRI_STR_NEW2("Warning."));')
7365
},
7466
{ # It is not currently possible to pass var args from native
7567
# functions to sulong, so we work round the issue here.
7668
match: /va_list args;[^}]*id_error, 1, ruby_message\);/,
77-
replacement: <<-EOF
78-
#ifdef NOKOGIRI_PACKAGED_LIBRARIES
79-
\\&
80-
#else
81-
rb_funcall(doc, id_error, 1, NOKOGIRI_STR_NEW2("Warning."));
82-
#endif
83-
EOF
69+
replacement: patch_for_system_libraries('rb_funcall(doc, id_error, 1, NOKOGIRI_STR_NEW2("Warning."));')
8470
}
8571
],
8672
'xml_xpath_context.c' => [
8773
{ # It is not currently possible to pass var args from native
8874
# functions to sulong, so we work round the issue here.
8975
match: 'va_list args;',
90-
replacement: <<-EOF
91-
#ifdef NOKOGIRI_PACKAGED_LIBRARIES
92-
va_list args;
93-
#else
94-
va_list args; rb_raise(rb_eRuntimeError, "%s", "Exception:"); return;
95-
#endif
96-
EOF
76+
replacement: patch_for_system_libraries('va_list args; rb_raise(rb_eRuntimeError, "%s", "Exception:"); return;')
9777
},
9878
{
9979
match: 'VALUE thing = Qnil;',
100-
replacement: <<-EOF
101-
#ifdef NOKOGIRI_PACKAGED_LIBRARIES
102-
VALUE thing = Qnil;
103-
#else
104-
VALUE thing = Qnil;
105-
VALUE errors = rb_ary_new();
106-
#endif
107-
EOF
80+
replacement: patch_for_system_libraries("VALUE thing = Qnil;\nVALUE errors = rb_ary_new();")
10881
},
10982
{
11083
match: 'xmlSetStructuredErrorFunc(NULL, Nokogiri_error_raise);',
111-
replacement: <<-EOF
112-
#ifdef NOKOGIRI_PACKAGED_LIBRARIES
113-
xmlSetStructuredErrorFunc(NULL, Nokogiri_error_raise);
114-
#else
115-
xmlSetStructuredErrorFunc(errors, Nokogiri_error_array_pusher);
116-
#endif
117-
EOF
84+
replacement: patch_for_system_libraries('xmlSetStructuredErrorFunc(errors, Nokogiri_error_array_pusher);')
11885
},
11986
{
12087
match: 'if(xpath == NULL)',
121-
replacement: <<-EOF
122-
#ifdef NOKOGIRI_PACKAGED_LIBRARIES
123-
if(xpath == NULL)
124-
#else
125-
if (RARRAY_LEN(errors) > 0) { rb_exc_raise(rb_ary_entry(errors, 0)); }
126-
if(xpath == NULL)
127-
#endif
128-
EOF
88+
replacement: patch_for_system_libraries("if (RARRAY_LEN(errors) > 0) { rb_exc_raise(rb_ary_entry(errors, 0)); }\nif(xpath == NULL)")
12989
},
13090
],
13191
}

0 commit comments

Comments
 (0)