Skip to content

Commit 9a5b420

Browse files
committed
[GR-29508] Don't patch nokogiri when using packaged libraries.
PullRequest: truffleruby/2434
2 parents 5474e16 + 46bd7dc commit 9a5b420

File tree

3 files changed

+42
-10
lines changed

3 files changed

+42
-10
lines changed

lib/truffle/truffle/cext_preprocessor.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,18 @@ def self.patch(file, contents, directory)
9191
directory.split('/').last(3).any? { |part| part =~ regexp } || file.split('/').last(2).any? { |part| part =~ regexp }
9292
end
9393
if matched
94+
# Generally we strip any trailing newlines and whitespce
95+
# from a patch, but on occasions we need to patch with a
96+
# preprocessor macro which _must_ end with a newline and
97+
# so requires that we preserve the trailing whitespace.
9498
patched_file[:patches].each do |patch|
95-
contents = contents.gsub(patch[:match], patch[:replacement].rstrip)
99+
last_line = patch[:replacement].rstrip.lines.last.lstrip
100+
contents = contents.gsub(patch[:match],
101+
if last_line && last_line.start_with?('#')
102+
patch[:replacement]
103+
else
104+
patch[:replacement].rstrip
105+
end)
96106
end
97107
end
98108
end

lib/truffle/truffle/patches/nokogiri_patches.rb

Lines changed: 18 additions & 8 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,14 +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: 'static VALUE to_array(VALUE self)'
44+
replacement: patch_for_system_libraries('static VALUE to_array(VALUE self)')
3545
},
3646
],
3747
'xslt_stylesheet.c' => [
3848
{ # It is not currently possible to pass var args from native
3949
# functions to sulong, so we work round the issue here.
4050
match: 'va_list args;',
41-
replacement: 'va_list args; rb_str_cat2(ctx, "Generic error"); return;'
51+
replacement: patch_for_system_libraries('va_list args; rb_str_cat2(ctx, "Generic error"); return;')
4252
}
4353
],
4454
'xml_document.c' => [
@@ -51,31 +61,31 @@ class NokogiriPatches
5161
{ # It is not currently possible to pass var args from native
5262
# functions to sulong, so we work round the issue here.
5363
match: /va_list args;[^}]*id_warning, 1, ruby_message\);/,
54-
replacement: 'rb_funcall(doc, id_warning, 1, NOKOGIRI_STR_NEW2("Warning."));'
64+
replacement: patch_for_system_libraries('rb_funcall(doc, id_warning, 1, NOKOGIRI_STR_NEW2("Warning."));')
5565
},
5666
{ # It is not currently possible to pass var args from native
5767
# functions to sulong, so we work round the issue here.
5868
match: /va_list args;[^}]*id_error, 1, ruby_message\);/,
59-
replacement: 'rb_funcall(doc, id_error, 1, NOKOGIRI_STR_NEW2("Warning."));'
69+
replacement: patch_for_system_libraries('rb_funcall(doc, id_error, 1, NOKOGIRI_STR_NEW2("Warning."));')
6070
}
6171
],
6272
'xml_xpath_context.c' => [
6373
{ # It is not currently possible to pass var args from native
6474
# functions to sulong, so we work round the issue here.
6575
match: 'va_list args;',
66-
replacement: 'va_list args; rb_raise(rb_eRuntimeError, "%s", "Exception:"); return;'
76+
replacement: patch_for_system_libraries('va_list args; rb_raise(rb_eRuntimeError, "%s", "Exception:"); return;')
6777
},
6878
{
6979
match: 'VALUE thing = Qnil;',
70-
replacement: "VALUE thing = Qnil;\n VALUE errors = rb_ary_new();"
80+
replacement: patch_for_system_libraries("VALUE thing = Qnil;\nVALUE errors = rb_ary_new();")
7181
},
7282
{
7383
match: 'xmlSetStructuredErrorFunc(NULL, Nokogiri_error_raise);',
74-
replacement: 'xmlSetStructuredErrorFunc(errors, Nokogiri_error_array_pusher);'
84+
replacement: patch_for_system_libraries('xmlSetStructuredErrorFunc(errors, Nokogiri_error_array_pusher);')
7585
},
7686
{
7787
match: 'if(xpath == NULL)',
78-
replacement: "if (RARRAY_LEN(errors) > 0) { rb_exc_raise(rb_ary_entry(errors, 0)); }\nif(xpath == NULL)"
88+
replacement: patch_for_system_libraries("if (RARRAY_LEN(errors) > 0) { rb_exc_raise(rb_ary_entry(errors, 0)); }\nif(xpath == NULL)")
7989
},
8090
],
8191
}

test/truffle/cexts/test-preprocess.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,19 @@ def test_patch(file, directory, input, expected)
4040
char * message;
4141
VALUE ruby_message;
4242
43-
rb_funcall(doc, id_warning, 1, NOKOGIRI_STR_NEW2("Warning."));
43+
#ifdef NOKOGIRI_PACKAGED_LIBRARIES
44+
va_list args;
45+
va_start(args, msg);
46+
vasprintf(&message, msg, args);
47+
va_end(args);
48+
49+
ruby_message = NOKOGIRI_STR_NEW2(message);
50+
vasprintf_free(message);
51+
rb_funcall(doc, id_warning, 1, ruby_message);
52+
#else
53+
rb_funcall(doc, id_warning, 1, NOKOGIRI_STR_NEW2("Warning."));
54+
#endif
55+
4456
}
4557
EOF
4658

0 commit comments

Comments
 (0)