Skip to content

Commit

Permalink
work around for issue rendering cosmo template within pcall
Browse files Browse the repository at this point in the history
  • Loading branch information
leafo committed Mar 5, 2022
1 parent 4f97d5d commit 32c5b0a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
14 changes: 11 additions & 3 deletions sitegen/common.lua
Original file line number Diff line number Diff line change
Expand Up @@ -355,9 +355,17 @@ end
local render_cosmo
render_cosmo = function(template, context, line_offset)
local cosmo = require("sitegen.cosmo")
local success, output_or_err = pcall(function()
return cosmo.f(template)(context)
end)
local success, output_or_err
if _VERSION == "Lua 5.1" and not jit then
local s, render_fn = pcall(function()
return cosmo.f(template)
end)
success, output_or_err = s, s and render_fn(context) or render_fn
else
success, output_or_err = pcall(function()
return cosmo.f(template)(context)
end)
end
if success then
return output_or_err
end
Expand Down
11 changes: 9 additions & 2 deletions sitegen/common.moon
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,15 @@ highlight_line = (lines, line_no, context=2, highlight_color="%{bright}%{red}",
render_cosmo = (template, context, line_offset) ->
cosmo = require "sitegen.cosmo"

success, output_or_err = pcall ->
cosmo.f(template) context
-- unable to wrap cosmo rendering in a pcall due to: attempt to yield across metamethod/C-call boundary
success, output_or_err = if _VERSION == "Lua 5.1" and not jit
s, render_fn = pcall ->
cosmo.f template

s, s and render_fn(context) or render_fn
else
pcall ->
cosmo.f(template) context

if success
return output_or_err
Expand Down

0 comments on commit 32c5b0a

Please sign in to comment.