Skip to content

Instantly share code, notes, and snippets.

@TestSubjector
Last active November 22, 2020 17:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TestSubjector/ae983d300a3c55ecf44896bfe20a75c6 to your computer and use it in GitHub Desktop.
Save TestSubjector/ae983d300a3c55ecf44896bfe20a75c6 to your computer and use it in GitHub Desktop.
How deepcopying in Lua may not be enough
local node = parser:entry_stat(lex)
local diff_node = copy(node, getmetatable(node))
print(node.node_type) -- returns ast.unspecialized.top.Task
print(diff_node.node_type) -- returns ast.unspecialized.top.Task
print(node.node_type == ast.unspecialized.top.Task) -- returns False
print(diff_node.node_type == ast.unspecialized.top.Task) -- returns False
for k, v in pairs(ast.unspecialized.top.Task) do
print(k, " ", v)
end
-- #Output1
-- print_custom false
-- parent ast.unspecialized.top
-- expected_field_set table: 0x7feecb918720
-- memoize_cache false
-- print_collapsed false
-- expected_fields {span,annotations,name,params,return_type_expr,effect_exprs,body}
-- print_hidden false
-- name Task
-- children {}
for k, v in pairs(node.node_type) do
print(k, " ", v)
end
-- #Output2
-- print_custom false
-- parent ast.unspecialized.top
-- expected_field_set table: 0x7feecb918720
-- memoize_cache false
-- print_collapsed false
-- expected_fields {span,annotations,name,params,return_type_expr,effect_exprs,body}
-- print_hidden false
-- name Task
-- children {}
for k, v in pairs(diff_node.node_type) do
print(k, " ", v)
end
-- #Output3
-- print_custom false
-- parent ast.unspecialized.top
-- expected_field_set table: 0x7feec8cbce48
-- children {}
-- print_collapsed false
-- expected_fields {span,annotations,name,params,return_type_expr,effect_exprs,body}
-- print_hidden false
-- name Task
-- memoize_cache false
-- #Output3 differs from #Output1 & #Output2, even with a deepcopy.
-- Look deeper into the metables, or change evaluation conditions for such cases.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment