changeset 30103:55eeb7f0850b stable

store parent name in function object when caching parents in scope (bug #61105) * symscope.h, symscope.cc (symbol_scope_rep::cache_parent_fcn_names): Also store parent function name in function object. * bug-61105/bug-61105.tst, bug-61105/nested_test_1.m, bug-61105/nested_test_2.m, bug-61105/nested_test_3.m: New test files. * test/bug-61105/module.mk: New file. * test/module.mk: Update.
author John W. Eaton <jwe@octave.org>
date Wed, 01 Sep 2021 16:19:09 -0400
parents d3298a58049e
children 761210f338c7 6cc5315cdf2f
files libinterp/corefcn/symscope.cc libinterp/corefcn/symscope.h test/bug-61105/bug-61105.tst test/bug-61105/module.mk test/bug-61105/nested_test_1.m test/bug-61105/nested_test_2.m test/bug-61105/nested_test_3.m test/module.mk
diffstat 8 files changed, 85 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/corefcn/symscope.cc	Sat Aug 21 15:02:48 2021 +0200
+++ b/libinterp/corefcn/symscope.cc	Wed Sep 01 16:19:09 2021 -0400
@@ -197,6 +197,20 @@
   }
 
   void
+  symbol_scope_rep::cache_parent_fcn_names (const std::list<std::string>& names)
+  {
+    m_parent_fcn_names = names;
+
+    if (m_code && m_code->is_user_function ())
+      {
+        octave_user_function *fcn
+          = dynamic_cast<octave_user_function *> (m_code);
+
+        fcn->stash_parent_fcn_name (names.front ());
+      }
+  }
+
+  void
   symbol_scope_rep::set_parent (const std::shared_ptr<symbol_scope_rep>& parent)
   {
     m_parent = std::weak_ptr<symbol_scope_rep> (parent);
--- a/libinterp/corefcn/symscope.h	Sat Aug 21 15:02:48 2021 +0200
+++ b/libinterp/corefcn/symscope.h	Wed Sep 01 16:19:09 2021 -0400
@@ -262,10 +262,7 @@
       return m_parent_fcn_names;
     }
 
-    void cache_parent_fcn_names (const std::list<std::string>& names)
-    {
-      m_parent_fcn_names = names;
-    }
+    void cache_parent_fcn_names (const std::list<std::string>& names);
 
     octave_user_code *user_code (void) const { return m_code; }
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/bug-61105/bug-61105.tst	Wed Sep 01 16:19:09 2021 -0400
@@ -0,0 +1,32 @@
+########################################################################
+##
+## Copyright (C) 2021 The Octave Project Developers
+##
+## See the file COPYRIGHT.md in the top-level directory of this
+## distribution or <https://octave.org/copyright/>.
+##
+## This file is part of Octave.
+##
+## Octave is free software: you can redistribute it and/or modify it
+## under the terms of the GNU General Public License as published by
+## the Free Software Foundation, either version 3 of the License, or
+## (at your option) any later version.
+##
+## Octave is distributed in the hope that it will be useful, but
+## WITHOUT ANY WARRANTY; without even the implied warranty of
+## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+## GNU General Public License for more details.
+##
+## You should have received a copy of the GNU General Public License
+## along with Octave; see the file COPYING.  If not, see
+## <https://www.gnu.org/licenses/>.
+##
+########################################################################
+
+%!assert (nested_test_1 (2), 24)
+
+%!assert (nested_test_2 (), 24)
+
+%!test
+%!  fh = nested_test_3 ();
+%!  assert (fh (2), 24);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/bug-61105/module.mk	Wed Sep 01 16:19:09 2021 -0400
@@ -0,0 +1,7 @@
+bug_61105_TEST_FILES = \
+  %reldir%/bug-61105.tst \
+  %reldir%/nested_test_1.m \
+  %reldir%/nested_test_2.m \
+  %reldir%/nested_test_3.m
+
+TEST_FILES += $(bug_61105_TEST_FILES)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/bug-61105/nested_test_1.m	Wed Sep 01 16:19:09 2021 -0400
@@ -0,0 +1,15 @@
+function r = nested_test_1 (val)
+  a = 3;
+  b = 5;
+  c = 7;
+  function r = f1 (f, x)
+    r = f(x) + a;
+  endfunction
+  function r = f2 (y)
+    function r2 = f3 (z)
+      r2 = z^2 + b*y;
+    endfunction
+    r = f1 (@f3, y) + c;
+  endfunction
+  r = f2 (val);
+endfunction
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/bug-61105/nested_test_2.m	Wed Sep 01 16:19:09 2021 -0400
@@ -0,0 +1,8 @@
+function r = nested_test_2 ()
+  a = 3;
+  b = 5;
+  c = 7;
+  f1 = @(f, x) f(x) + a;
+  f2 = @(y) f1 (@(z) z^2 + b*y, y) + c;
+  r = f2 (2);
+endfunction
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/bug-61105/nested_test_3.m	Wed Sep 01 16:19:09 2021 -0400
@@ -0,0 +1,7 @@
+function fh = nested_test_3 ()
+  a = 3;
+  b = 5;
+  c = 7;
+  f1 = @(f, x) f(x) + a;
+  fh = @(y) f1 (@(z) z^2 + b*y, y) + c;
+endfunction
--- a/test/module.mk	Sat Aug 21 15:02:48 2021 +0200
+++ b/test/module.mk	Wed Sep 01 16:19:09 2021 -0400
@@ -91,6 +91,7 @@
 include %reldir%/bug-59704/module.mk
 include %reldir%/bug-59937/module.mk
 include %reldir%/bug-60237/module.mk
+include %reldir%/bug-61105/module.mk
 include %reldir%/class-concat/module.mk
 include %reldir%/classdef/module.mk
 include %reldir%/classdef-multiple-inheritance/module.mk