Skip to content

Commit

Permalink
Optimize away a to_s call for = scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
k0kubun committed Dec 10, 2022
1 parent af17ff7 commit 2017600
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 13 deletions.
4 changes: 1 addition & 3 deletions lib/haml/compiler/script_compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,8 @@ def compile_script_assign(var, node, &block)
def compile_script_result(result, node)
if !node.value[:escape_html] && node.value[:preserve]
result = find_and_preserve(result)
else
result = "(#{result}).to_s"
end
[:escape, node.value[:escape_html], [:dynamic, result]]
[:escapeany, node.value[:escape_html], [:dynamic, result]]
end

def find_and_preserve(code)
Expand Down
10 changes: 6 additions & 4 deletions lib/haml/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
require 'haml/compiler'
require 'haml/html'
require 'haml/string_splitter'
require 'haml/escapable'
require 'haml/force_escapable'
require 'haml/escape'
require 'haml/escape_any'
require 'haml/force_escape'
require 'haml/dynamic_merger'
require 'haml/ambles'
require 'haml/whitespace'
Expand All @@ -32,8 +33,9 @@ class Engine < Temple::Engine
use HTML
use StringSplitter
filter :StaticAnalyzer
use Escapable
use ForceEscapable
use Escape
use EscapeAny
use ForceEscape
filter :ControlFlow
use Ambles
filter :MultiFlattener
Expand Down
2 changes: 1 addition & 1 deletion lib/haml/escapable.rb → lib/haml/escape.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
require 'haml/util'

module Haml
class Escapable < Temple::Filters::Escapable
class Escape < Temple::Filters::Escapable
def initialize(opts = {})
super
@escape_code = options[:escape_code] ||
Expand Down
21 changes: 21 additions & 0 deletions lib/haml/escape_any.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true
require 'haml/escape'

module Haml
# This module allows Temple::Filter to dispatch :fescape on `#compile`.
module EscapeanyDispathcer
def on_escapeany(flag, exp)
[:escapeany, flag, compile(exp)]
end
end
::Temple::Filter.include EscapeanyDispathcer

# Unlike Haml::Escape, this calls to_s when not escaped.
class EscapeAny < Escape
alias_method :on_escapeany, :on_escape

def on_dynamic(value)
[:dynamic, @escape ? @escape_code % value : "(#{value}).to_s"]
end
end
end
10 changes: 5 additions & 5 deletions lib/haml/force_escapable.rb → lib/haml/force_escape.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true
require 'haml/escapable'
require 'haml/escape'

module Haml
# This module allows Temple::Filter to dispatch :fescape on `#compile`.
Expand All @@ -10,8 +10,8 @@ def on_fescape(flag, exp)
end
::Temple::Filter.include FescapeDispathcer

# Unlike Haml::Escapable, this escapes value even if it's html_safe.
class ForceEscapable < Escapable
# Unlike Haml::Escape, this escapes value even if it's html_safe.
class ForceEscape < Escape
def initialize(opts = {})
super
@escape_code = options[:escape_code] || "::Haml::Util.escape_html((%s))"
Expand All @@ -20,8 +20,8 @@ def initialize(opts = {})

alias_method :on_fescape, :on_escape

# ForceEscapable doesn't touch :escape expression.
# This method is not used if it's inserted after Haml::Escapable.
# ForceEscape doesn't touch :escape expression.
# This method is not used if it's inserted after Haml::Escape.
def on_escape(flag, exp)
[:escape, flag, compile(exp)]
end
Expand Down

0 comments on commit 2017600

Please sign in to comment.