Attempt to support truffleruby

This commit is contained in:
Takashi Kokubun 2019-09-08 16:07:54 +09:00
parent a345008ff2
commit 9b5b1f09a3
No known key found for this signature in database
GPG Key ID: 6FFC433B12EE23DD
21 changed files with 49 additions and 17 deletions

View File

@ -1,5 +1,6 @@
#include <ruby.h>
#include <ruby/encoding.h>
#ifndef TRUFFLERUBY
#include "hescape.h"
#include "string.h"
@ -551,3 +552,4 @@ Init_hamlit(void)
rb_const_set(mAttributeBuilder, id_space, rb_obj_freeze(rb_str_new_cstr(" ")));
rb_const_set(mAttributeBuilder, id_underscore, rb_obj_freeze(rb_str_new_cstr("_")));
}
#endif

View File

@ -21,7 +21,7 @@ Gem::Specification.new do |spec|
if /java/ === RUBY_PLATFORM
spec.platform = 'java'
else
elsif RUBY_ENGINE != 'truffleruby'
spec.extensions = ['ext/hamlit/extconf.rb']
spec.required_ruby_version = '>= 2.1.0'
end

View File

@ -9,7 +9,9 @@ module Hamlit::AttributeBuilder
itemscope allowfullscreen default inert sortable
truespeed typemustmatch download].freeze
if /java/ === RUBY_PLATFORM # JRuby
# Java extension is not implemented for JRuby yet.
# TruffleRuby does not implement `rb_ary_sort_bang`, etc.
if /java/ === RUBY_PLATFORM || RUBY_ENGINE == 'truffleruby'
class << self
def build(escape_attrs, quote, format, object_ref, *hashes)
hashes << Hamlit::ObjectRef.parse(object_ref) if object_ref

View File

@ -14,7 +14,9 @@ module Hamlit
def compile(node)
hashes = []
return runtime_compile(node) if node.value[:object_ref] != :nil
if node.value[:object_ref] != :nil || !Ripper.respond_to?(:lex) # No Ripper.lex in truffleruby
return runtime_compile(node)
end
node.value[:attributes_hashes].each do |attribute_str|
hash = AttributeParser.parse(attribute_str)
return runtime_compile(node) unless hash

View File

@ -11,6 +11,10 @@ module Hamlit
end
def compile(node, &block)
unless Ripper.respond_to?(:lex) # No Ripper.lex in truffleruby
return dynamic_compile(node, &block)
end
no_children = node.children.empty?
case
when no_children && node.value[:escape_interpolation]

View File

@ -28,8 +28,10 @@ module Hamlit
nil
when node.value[:parse]
return compile_interpolated_plain(node) if node.value[:escape_interpolation]
return delegate_optimization(node) if RubyExpression.string_literal?(node.value[:value])
return delegate_optimization(node) if Temple::StaticAnalyzer.static?(node.value[:value])
if Ripper.respond_to?(:lex) # No Ripper.lex in truffleruby
return delegate_optimization(node) if RubyExpression.string_literal?(node.value[:value])
return delegate_optimization(node) if Temple::StaticAnalyzer.static?(node.value[:value])
end
var = @identity.generate
[:multi,
@ -53,6 +55,10 @@ module Hamlit
# We should handle interpolation here to escape only interpolated values.
def compile_interpolated_plain(node)
unless Ripper.respond_to?(:lex) # No Ripper.lex in truffleruby
return [:multi, [:escape, node.value[:escape_interpolation], [:dynamic, "%Q[#{node.value[:value]}]"]], [:newline]]
end
temple = [:multi]
StringSplitter.compile(node.value[:value]).each do |type, value|
case type

View File

@ -25,8 +25,10 @@ module Hamlit
use Parser
use Compiler
use HTML
use StringSplitter
filter :StaticAnalyzer
if Ripper.respond_to?(:lex) # No Ripper.lex in truffleruby
use StringSplitter
filter :StaticAnalyzer
end
use Escapable
use ForceEscapable
filter :ControlFlow

View File

@ -5,6 +5,9 @@ module Hamlit
class Filters
class Plain < Base
def compile(node)
unless Ripper.respond_to?(:lex)
raise NotImplementedError.new('This platform does not have Ripper.lex required for :plain filter')
end
text = node.value[:text]
text = text.rstrip unless ::Hamlit::HamlUtil.contains_interpolation?(text) # for compatibility
[:multi, *compile_plain(text)]

BIN
lib/hamlit/hamlit.su Normal file

Binary file not shown.

View File

@ -1,7 +1,9 @@
# frozen_string_literal: true
module Hamlit
module Utils
if /java/ === RUBY_PLATFORM # JRuby
# Java extension is not implemented for JRuby yet.
# TruffleRuby does not implement `rb_ary_sort_bang`, etc.
if /java/ === RUBY_PLATFORM || RUBY_ENGINE == 'truffleruby'
require 'cgi/escape'
def self.escape_html(html)

View File

@ -2105,4 +2105,4 @@ HTML
assert_equal expected.encoding, actual.encoding
assert_equal expected, actual
end
end
end if RUBY_ENGINE != 'truffleruby' # truffleruby cannot run Haml

View File

@ -119,7 +119,7 @@ class FiltersTest < Haml::TestCase
assert_haml_ugly(":plain\n \#{'<script>evil</script>'}")
end
end
end if RUBY_ENGINE != 'truffleruby' # truffleruby does not implement Ripper.lex
class ErbFilterTest < Haml::TestCase
test "multiline expressions should work" do; skip

View File

@ -1107,4 +1107,4 @@ world</p>}
assert_equal haml_result, hamlit_result
end
end
end
end if RUBY_ENGINE != 'truffleruby' # truffleruby cannot run Haml

View File

@ -97,5 +97,5 @@ describe Hamlit::AttributeParser do
it { assert_parse({ 'foo' => 'bar(a, b)', 'hoge' => 'piyo(a, b,)' }, 'foo: bar(a, b), hoge: piyo(a, b,),') }
it { assert_parse({ 'foo' => 'bar(a, b)', 'hoge' => 'piyo(a, b,)' }, ' { foo: bar(a, b), hoge: piyo(a, b,), } ') }
end
end
end if RUBY_ENGINE != 'truffleruby' # truffleruby doesn't have Ripper.lex
end

View File

@ -114,6 +114,9 @@ describe Hamlit::Engine do
if /java/ === RUBY_PLATFORM
skip 'maybe due to Ripper of JRuby'
end
if RUBY_ENGINE == 'truffleruby'
skip 'truffleruby raises NoMethodError'
end
assert_raises ArgumentError do
render_hamlit("%div{ nil }")

View File

@ -22,5 +22,5 @@ describe Hamlit::Filters do
#{'<script>'}
HAML
end
end
end if RUBY_ENGINE != 'truffleruby' # truffleruby doesn't have Ripper.lex
end

View File

@ -244,4 +244,4 @@ describe Hamlit::Engine do
end
end unless /java/ === RUBY_PLATFORM # execjs is not working with Travis JRuby environment
end
end
end if RUBY_ENGINE != 'truffleruby' # negetive line numbers are broken in truffleruby

View File

@ -44,4 +44,4 @@ describe 'optimization' do
assert_equal true, compiled_code(haml).include?(%|value='hello|)
end
end
end
end if RUBY_ENGINE != 'truffleruby' # truffleruby does not implement major Ripper features

View File

@ -41,4 +41,4 @@ describe Hamlit::RubyExpression do
it { assert_literal(false, %Q|''\n''|) }
end
end
end
end if RUBY_ENGINE != 'truffleruby' # truffleruby doesn't have Ripper.sexp

View File

@ -47,5 +47,5 @@ describe Hamlit::StringSplitter do
end
end
end
end
end if RUBY_ENGINE != 'truffleruby' # truffleruby doesn't have Ripper.lex
end

View File

@ -47,6 +47,9 @@ module RenderHelper
end
def assert_haml(haml, options = {})
if RUBY_ENGINE == 'truffleruby'
skip 'truffleruby cannot run Haml'
end
expected = render_haml(haml, options)
actual = render_hamlit(haml, options)
assert_equal expected, actual
@ -64,6 +67,9 @@ class Haml::TestCase < BASE_TEST_CLASS
end
def assert_haml_ugly(text, options = {})
if RUBY_ENGINE == 'truffleruby'
skip 'truffleruby cannot run Haml'
end
haml_base = { escape_html: true, escape_attrs: true }
hamlit_base = { escape_html: true }
scope = options.delete(:scope) || Object.new