From 18b0e148cd225057dc3feb387c5fdab43f7fcda1 Mon Sep 17 00:00:00 2001 From: Brian Cardarella Date: Tue, 12 Dec 2023 00:44:14 -0500 Subject: [PATCH] Building module using AST instead of strings Per conversation with Jose, he confirmed that we'll always run into truncation issues with using a String representation to compile. Using the AST to compile instead ensures this is avoided --- lib/live_view_native/templates.ex | 47 +++++++++++-------------------- 1 file changed, 17 insertions(+), 30 deletions(-) diff --git a/lib/live_view_native/templates.ex b/lib/live_view_native/templates.ex index f500e6d2..1c788b81 100644 --- a/lib/live_view_native/templates.ex +++ b/lib/live_view_native/templates.ex @@ -97,11 +97,7 @@ defmodule LiveViewNative.Templates do end defp dump_class_tree_bytecode(class_tree_map, caller) do - class_tree_map - |> generate_class_tree_module(caller) - |> Code.compile_string() - - :ok + generate_class_tree_module(class_tree_map, caller) end defp generate_class_tree_module(class_tree_map, caller) do @@ -109,33 +105,24 @@ defmodule LiveViewNative.Templates do module_name = generate_class_tree_module_name(template_module) branches = get_class_tree_branches(requires) - try do - :ets.new(:live_view_native_class_maps, [:public, :named_table]) - rescue - _ -> :already_defined + ast = quote location: :keep do + def class_tree(stylesheet_key) do + %{ + branches: unquote(branches), + contents: unquote(Macro.escape(class_tree_map))[stylesheet_key], + expanded_branches: [unquote(module_name)] + } || + %{ + branches: [], + contents: %{}, + expanded_branches: [unquote(module_name)] + } + end end - :ets.insert(:live_view_native_class_maps, {module_name, class_tree_map}) - - Macro.to_string( - quote location: :keep do - defmodule unquote(module_name) do - def class_tree(stylesheet_key) do - [_key, class_tree_map] = :ets.lookup(:live_view_native_class_maps, unquote(module_name)) - %{ - branches: unquote(branches), - contents: class_tree_map[stylesheet_key], - expanded_branches: [unquote(module_name)] - } || - %{ - branches: [], - contents: %{}, - expanded_branches: [unquote(module_name)] - } - end - end - end - ) + Module.create(module_name, ast, Macro.Env.location(__ENV__)) + + :ok end defp generate_class_tree_module_name(module) do