Module: Latexmath::NativeRecursionGuard

Included in:
Latexmath
Defined in:
lib/latexmath/parser/native_recursion_guard.rb

Overview

Parsanol::Native.raise_native_parse_error translates a native failure. While Latexmath's own native call runs on this thread, this hook turns a recursion-limit failure into NativeRecursionLimit; every other failure, and every other caller of Parsanol, is unaffected. (The same guard as amath's.)

Constant Summary collapse

MESSAGE =
/\A(?:Maximum recursion depth \(\d+\) exceeded|Recursion limit exceeded\b)/
KEY =
:latexmath_native_recursion_guard

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.installed? ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/latexmath/parser/native_recursion_guard.rb', line 27

def self.installed?
  Parsanol::Native.singleton_class.ancestors.include?(self)
end

.watch ⇒ Object



19
20
21
22
23
24
25
# File 'lib/latexmath/parser/native_recursion_guard.rb', line 19

def self.watch
  outer = Thread.current[KEY]
  Thread.current[KEY] = true
  yield
ensure
  Thread.current[KEY] = outer
end

Instance Method Details

#raise_native_parse_error(error, grammar, input) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/latexmath/parser/native_recursion_guard.rb', line 31

def raise_native_parse_error(error, grammar, input)
  if Thread.current[KEY] && error.message.match?(MESSAGE)
    raise NativeRecursionLimit, error.message.lines.first.strip, cause: nil
  end

  super
end