Skip to content

Commit

Permalink
Upstream truffleruby patches needed when using system libraries
Browse files Browse the repository at this point in the history
* Adapt tests for that configuration.
  • Loading branch information
eregon committed Oct 12, 2022
1 parent b629646 commit d5e129f
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 7 deletions.
3 changes: 3 additions & 0 deletions ext/nokogiri/nokogiri.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ xmlNodePtr xmlLastElementChild(xmlNodePtr parent);
# endif
#endif

#if defined(TRUFFLERUBY) && !defined(NOKOGIRI_PACKAGED_LIBRARIES)
# define TRUFFLERUBY_NOKOGIRI_SYSTEM_LIBRARIES
#endif

NOKOPUBVAR VALUE mNokogiri ;
NOKOPUBVAR VALUE mNokogiriGumbo ;
Expand Down
12 changes: 12 additions & 0 deletions ext/nokogiri/xml_sax_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -203,10 +203,16 @@ warning_func(void *ctx, const char *msg, ...)
VALUE doc = rb_iv_get(self, "@document");
VALUE rb_message;

#ifdef TRUFFLERUBY_NOKOGIRI_SYSTEM_LIBRARIES
/* It is not currently possible to pass var args from native
functions to sulong, so we work around the issue here. */
rb_message = rb_sprintf("warning_func: %s", msg);
#else
va_list args;
va_start(args, msg);
rb_message = rb_vsprintf(msg, args);
va_end(args);
#endif

rb_funcall(doc, id_warning, 1, rb_message);
}
Expand All @@ -219,10 +225,16 @@ error_func(void *ctx, const char *msg, ...)
VALUE doc = rb_iv_get(self, "@document");
VALUE rb_message;

#ifdef TRUFFLERUBY_NOKOGIRI_SYSTEM_LIBRARIES
/* It is not currently possible to pass var args from native
functions to sulong, so we work around the issue here. */
rb_message = rb_sprintf("error_func: %s", msg);
#else
va_list args;
va_start(args, msg);
rb_message = rb_vsprintf(msg, args);
va_end(args);
#endif

rb_funcall(doc, id_error, 1, rb_message);
}
Expand Down
17 changes: 17 additions & 0 deletions ext/nokogiri/xml_xpath_context.c
Original file line number Diff line number Diff line change
Expand Up @@ -298,10 +298,16 @@ xpath_generic_exception_handler(void *ctx, const char *msg, ...)
{
VALUE rb_message;

#ifdef TRUFFLERUBY_NOKOGIRI_SYSTEM_LIBRARIES
/* It is not currently possible to pass var args from native
functions to sulong, so we work around the issue here. */
rb_message = rb_sprintf("xpath_generic_exception_handler: %s", msg);
#else
va_list args;
va_start(args, msg);
rb_message = rb_vsprintf(msg, args);
va_end(args);
#endif

rb_exc_raise(rb_exc_new3(rb_eRuntimeError, rb_message));
}
Expand Down Expand Up @@ -336,7 +342,12 @@ evaluate(int argc, VALUE *argv, VALUE self)
}

xmlResetLastError();
#ifdef TRUFFLERUBY_NOKOGIRI_SYSTEM_LIBRARIES
VALUE errors = rb_ary_new();
xmlSetStructuredErrorFunc(errors, Nokogiri_error_array_pusher);
#else
xmlSetStructuredErrorFunc(NULL, Nokogiri_error_raise);
#endif

/* For some reason, xmlXPathEvalExpression will blow up with a generic error */
/* when there is a non existent function. */
Expand All @@ -346,6 +357,12 @@ evaluate(int argc, VALUE *argv, VALUE self)
xmlSetStructuredErrorFunc(NULL, NULL);
xmlSetGenericErrorFunc(NULL, NULL);

#ifdef TRUFFLERUBY_NOKOGIRI_SYSTEM_LIBRARIES
if (RARRAY_LEN(errors) > 0) {
rb_exc_raise(rb_ary_entry(errors, 0));
}
#endif

if (xpath == NULL) {
xmlErrorPtr error = xmlGetLastError();
rb_exc_raise(Nokogiri_wrap_xml_syntax_error(error));
Expand Down
6 changes: 6 additions & 0 deletions ext/nokogiri/xslt_stylesheet.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,16 @@ xslt_generic_error_handler(void *ctx, const char *msg, ...)
{
VALUE message;

#ifdef TRUFFLERUBY_NOKOGIRI_SYSTEM_LIBRARIES
/* It is not currently possible to pass var args from native
functions to sulong, so we work around the issue here. */
message = rb_sprintf("xslt_generic_error_handler: %s", msg);
#else
va_list args;
va_start(args, msg);
message = rb_vsprintf(msg, args);
va_end(args);
#endif

rb_str_concat((VALUE)ctx, message);
}
Expand Down
4 changes: 4 additions & 0 deletions test/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ def skip_unless_libxml2_patch(patch_name)
def skip_unless_jruby(msg = "this test should only run with jruby")
skip(msg) unless Nokogiri.jruby?
end

def truffleruby_system_libraries?
RUBY_ENGINE == "truffleruby" and !Nokogiri::PACKAGED_LIBRARIES
end
end

class TestBenchmark < Minitest::BenchSpec
Expand Down
13 changes: 9 additions & 4 deletions test/test_xslt_transforms.rb
Original file line number Diff line number Diff line change
Expand Up @@ -349,10 +349,15 @@ def test_non_html_xslt_transform
exception = assert_raises(RuntimeError) do
xslt.transform(doc)
end
assert_match(
/xmlXPathCompOpEval: function decimal not found|java.lang.NoSuchMethodException.*decimal/,
exception.message,
)
if truffleruby_system_libraries?
assert_equal(
"xslt_generic_error_handler: xmlXPathCompOpEval: function %s not found\nxslt_generic_error_handler: %s",
exception.message)
else
assert_match(
/xmlXPathCompOpEval: function decimal not found|java.lang.NoSuchMethodException.*decimal/,
exception.message)
end
end

describe "DEFAULT_XSLT parse options" do
Expand Down
6 changes: 5 additions & 1 deletion test/xml/sax/test_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,11 @@ def call_parse_io_with_encoding(encoding)
XML
parser.parse(xml)
refute_empty(parser.document.warnings)
assert_match(/URI .* is not absolute/, parser.document.warnings.first)
if truffleruby_system_libraries?
assert_equal('warning_func: %s', parser.document.warnings.first)
else
assert_match(/URI .* is not absolute/, parser.document.warnings.first)
end
end
end
end
Expand Down
12 changes: 10 additions & 2 deletions test/xml/test_entity_reference.rb
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,11 @@ def setup
html = File.read(xml_document)
@parser.parse(html)
refute_nil @parser.document.errors
assert_equal ["Entity 'bar' not defined"], @parser.document.errors.map(&:to_s).map(&:strip)
if truffleruby_system_libraries?
assert_equal ["error_func: %s"], @parser.document.errors.map(&:to_s).map(&:strip)
else
assert_equal ["Entity 'bar' not defined"], @parser.document.errors.map(&:to_s).map(&:strip)
end
end

test_relative_and_absolute_path :test_more_sax_entity_reference do
Expand All @@ -206,7 +210,11 @@ def setup
]
@parser.parse(html)
refute_nil @parser.document.errors
assert_equal ["Entity 'bar' not defined"], @parser.document.errors.map(&:to_s).map(&:strip)
if truffleruby_system_libraries?
assert_equal ["error_func: %s"], @parser.document.errors.map(&:to_s).map(&:strip)
else
assert_equal ["Entity 'bar' not defined"], @parser.document.errors.map(&:to_s).map(&:strip)
end
end
end

Expand Down

0 comments on commit d5e129f

Please sign in to comment.