Просмотр исходного кода

Move all for loop logic to erlydtl_runtime (#167)

Andreas Stenius 11 лет назад
Родитель
Сommit
b9ec861c2d
3 измененных файлов с 17 добавлено и 18 удалено
  1. 1 0
      NEWS.md
  2. 8 15
      src/erlydtl_beam_compiler.erl
  3. 8 3
      src/erlydtl_runtime.erl

+ 1 - 0
NEWS.md

@@ -6,3 +6,4 @@ Standards](http://www.gnu.org/prep/standards/html_node/NEWS-File.html#NEWS-File)
 
 ## master (upcoming release)
 
+* Fixed issue with generated code for `for` loops (#167).

+ 8 - 15
src/erlydtl_beam_compiler.erl

@@ -1336,21 +1336,14 @@ for_loop_ast(IteratorList, LoopValue, IsReversed, Contents,
 
     {ParentLoop, TreeWalker3} = resolve_reserved_variable('forloop', TreeWalker2),
 
-    %% call for loop (wrapped in a fun to contain the variable L)
-    {{?Q(["fun() ->",
-          "  case erlydtl_runtime:forloop(",
-          "    fun (_@Vars, _@Counters) ->",
-          "      {_@IteratorVars} = if is_tuple(_@Vars), size(_@Vars) == _@IteratorCount@ -> _@Vars;",
-          "                            _@___ifclauses -> _",
-          "                         end,",
-          "      {_@LoopBodyAst, erlydtl_runtime:increment_counter_stats(_@Counters)}",
-          "    end,",
-          "    _@LoopValueAst0, _@ParentLoop)",
-          "  of",
-          "    empty -> _@EmptyContentsAst;",
-          "    {L, _} -> L",
-          "  end",
-          "end()"],
+    {{?Q(["erlydtl_runtime:forloop(",
+          "  fun (_@Vars, _@Counters) ->",
+          "    {_@IteratorVars} = if is_tuple(_@Vars), size(_@Vars) == _@IteratorCount@ -> _@Vars;",
+          "                          _@___ifclauses -> _",
+          "                       end,",
+          "    {_@LoopBodyAst, erlydtl_runtime:increment_counter_stats(_@Counters)}",
+          "  end,",
+          "  _@LoopValueAst0, _@ParentLoop, _@EmptyContentsAst)"],
          [{ifclauses, if IteratorCount > 1 ->
                               ?Q(["() when is_list(_@Vars), length(_@Vars) == _@IteratorCount@ ->",
                                   "  list_to_tuple(_@Vars);",

+ 8 - 3
src/erlydtl_runtime.erl

@@ -361,13 +361,18 @@ increment_counter_stats([{counter, Counter}, {counter0, Counter0}, {revcounter,
      {first, false}, {last, RevCounter0 =:= 1},
      {parentloop, Parent}].
 
-forloop(_Fun, [], _Parent) -> empty;
-forloop(Fun, Values, Parent) ->
+forloop(_Fun, [], _Parent, Default) -> Default;
+forloop(Fun, Values, Parent, _Default) ->
     push_ifchanged_context(),
-    Result = lists:mapfoldl(Fun, init_counter_stats(Values, Parent), Values),
+    {Result, _Acc} = lists:mapfoldl(Fun, init_counter_stats(Values, Parent), Values),
     pop_ifchanged_context(),
     Result.
 
+%% keep old version for backwards compatibility..
+forloop(_Fun, [], _Parent) -> empty;
+forloop(Fun, Values, Parent) ->
+    {forloop(Fun, Values, Parent, undefined), undefined}.
+
 push_ifchanged_context() ->
     IfChangedContextStack = case get(?IFCHANGED_CONTEXT_VARIABLE) of
                                 undefined -> [];