                 
         visitFunction: (n) ->
             try
+                new_mapping = deep_copy_object @currentMapping()
                 if n.ejs_env.closed.empty() and not n.ejs_env.nested_requires_env
                         n.body.body.unshift
                                 type: syntax.VariableDeclaration,

                                                expression: create_intrinsic "setSlot", [ (create_identifier env_name), (create_number_literal n.ejs_env.slot_mapping[param.name]), (create_string_literal param.name), (create_identifier param.name) ]
                                        }

                        new_mapping = deep_copy_object @currentMapping()
                        new_mapping["%slot_mapping"] = n.ejs_env.slot_mapping
                        ###
                        for mapped_id of n.ejs_env.slot_mapping

                # for a body, replace the body with a block statement
                # with a return statement returning that expression
                if n.expression
                        n.body = {
                                type: syntax.BlockStatement
                                body: [{
