GNU Octave 7.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
ov-fcn-handle.cc
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 2003-2022 The Octave Project Developers
4//
5// See the file COPYRIGHT.md in the top-level directory of this
6// distribution or <https://octave.org/copyright/>.
7//
8// This file is part of Octave.
9//
10// Octave is free software: you can redistribute it and/or modify it
11// under the terms of the GNU General Public License as published by
12// the Free Software Foundation, either version 3 of the License, or
13// (at your option) any later version.
14//
15// Octave is distributed in the hope that it will be useful, but
16// WITHOUT ANY WARRANTY; without even the implied warranty of
17// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18// GNU General Public License for more details.
19//
20// You should have received a copy of the GNU General Public License
21// along with Octave; see the file COPYING. If not, see
22// <https://www.gnu.org/licenses/>.
23//
24////////////////////////////////////////////////////////////////////////
25
26#if defined (HAVE_CONFIG_H)
27# include "config.h"
28#endif
29
30#include <istream>
31#include <list>
32#include <ostream>
33#include <sstream>
34#include <string>
35#include <vector>
36
37#include "file-ops.h"
38#include "oct-locbuf.h"
39
40#include "defaults.h"
41#include "defun.h"
42#include "error.h"
43#include "errwarn.h"
44#include "file-stat.h"
45#include "input.h"
46#include "interpreter-private.h"
47#include "interpreter.h"
48#include "load-path.h"
49#include "oct-env.h"
50#include "oct-hdf5.h"
51#include "oct-map.h"
52#include "ov-base.h"
53#include "ov-cell.h"
54#include "ov-fcn-handle.h"
55#include "ov-usr-fcn.h"
56#include "parse.h"
57#include "pr-output.h"
58#include "pt-arg-list.h"
59#include "pt-assign.h"
60#include "pt-cmd.h"
61#include "pt-eval.h"
62#include "pt-exp.h"
63#include "pt-idx.h"
64#include "pt-misc.h"
65#include "pt-pr-code.h"
66#include "pt-stmt.h"
67#include "stack-frame.h"
68#include "syminfo.h"
69#include "symscope.h"
70#include "unwind-prot.h"
71#include "variables.h"
72
73#include "byte-swap.h"
74#include "ls-ascii-helper.h"
75#include "ls-hdf5.h"
76#include "ls-oct-text.h"
77#include "ls-oct-binary.h"
78#include "ls-utils.h"
79
80
82 "function handle",
83 "function_handle");
84
85const std::string octave_fcn_handle::anonymous ("@<anonymous>");
86
87OCTAVE_NAMESPACE_BEGIN
88
90 {
91 public:
92
93 invalid_fcn_handle (void) : base_fcn_handle ("<invalid>") { }
94
96
97 ~invalid_fcn_handle (void) = default;
98
100 {
101 return new invalid_fcn_handle (*this);
102 }
103
104 std::string type (void) const { return "<invalid>"; }
105
106 octave_value_list call (int nargout, const octave_value_list& args);
107 };
108
109 // Create a handle to an unnamed internal function. There will be no
110 // way to save and reload it. See, for example, the F__fltk_check__
111 // function in __init_fltk__.cc.
112
114 {
115 public:
116
118 : base_fcn_handle ("<internal>"), m_fcn (fcn)
119 { }
120
122
123 ~internal_fcn_handle (void) = default;
124
126 {
127 return new internal_fcn_handle (*this);
128 }
129
130 std::string type (void) const { return "<internal>"; }
131
132 bool is_internal (void) const { return true; }
133
134 octave_value_list call (int nargout, const octave_value_list& args);
135
136 // FIXME: These must go away. They don't do the right thing for
137 // scoping or overloads.
139 {
140 return m_fcn.function_value ();
141 }
142
144 {
145 return m_fcn.user_function_value ();
146 }
147
148 octave_value fcn_val (void) { return m_fcn; }
149
150 // Should be const.
151 octave_scalar_map info (void);
152
153 friend bool is_equal_to (const internal_fcn_handle& fh1,
154 const internal_fcn_handle& fh2);
155
156 private:
157
159 };
160
162 {
163 public:
164
165 // FIXME: octaveroot is temporary information used when loading
166 // handles. Can we avoid using it in the constructor?
167
168 simple_fcn_handle (const std::string& name = "",
169 const std::string& file = "",
170 const std::string& /*octaveroot*/ = "")
172 { }
173
174 simple_fcn_handle (const octave_value& fcn, const std::string& name)
175 : base_fcn_handle (name), m_fcn (fcn)
176 {
177 if (m_fcn.is_defined ())
178 {
180
181 if (oct_fcn)
182 m_file = oct_fcn->fcn_file_name ();
183 }
184 }
185
187
188 ~simple_fcn_handle (void) = default;
189
191 {
192 return new simple_fcn_handle (*this);
193 }
194
195 std::string type (void) const { return "simple"; }
196
197 bool is_simple (void) const { return true; }
198
199 octave_value_list call (int nargout, const octave_value_list& args);
200
201 // FIXME: These must go away. They don't do the right thing for
202 // scoping or overloads.
204
206
207 octave_value fcn_val (void);
208
209 // Should be const.
210 octave_scalar_map info (void);
211
212 bool save_ascii (std::ostream& os);
213
214 bool load_ascii (std::istream& is);
215
216 bool save_binary (std::ostream& os, bool save_as_floats);
217
218 bool load_binary (std::istream& is, bool swap, mach_info::float_format fmt);
219
220 bool save_hdf5 (octave_hdf5_id loc_hid, const char *name, bool save_as_floats);
221
222 bool load_hdf5 (octave_hdf5_id& group_hid, octave_hdf5_id& space_hid,
223 octave_hdf5_id& type_hid);
224
225 void print_raw (std::ostream& os, bool pr_as_read_syntax,
226 int current_print_indent_level) const;
227
228 friend bool is_equal_to (const simple_fcn_handle& fh1,
229 const simple_fcn_handle& fh2);
230
231 private:
232
234 };
235
237 {
238 public:
239
240 // FIXME: octaveroot is temporary information used when loading
241 // handles. Can we avoid using it in the constructor?
242
243 scoped_fcn_handle (const std::string& name = "",
244 const std::string& file = "",
245 const std::string& /*octaveroot*/ = "")
247 { }
248
249 scoped_fcn_handle (const octave_value& fcn, const std::string& name,
250 const std::list<std::string>& parentage);
251
253
254 ~scoped_fcn_handle (void) = default;
255
257 {
258 return new scoped_fcn_handle (*this);
259 }
260
261 std::string type (void) const { return "scopedfunction"; }
262
263 bool is_scoped (void) const { return true; }
264
265 octave_value_list call (int nargout, const octave_value_list& args);
266
267 // FIXME: These must go away. They don't do the right thing for
268 // scoping or overloads.
270 {
271 return m_fcn.function_value ();
272 }
273
275 {
276 return m_fcn.user_function_value ();
277 }
278
279 octave_value fcn_val (void) { return m_fcn; }
280
281 // Should be const.
282 octave_scalar_map info (void);
283
284 bool save_ascii (std::ostream& os);
285
286 bool load_ascii (std::istream& is);
287
288 bool save_binary (std::ostream& os, bool save_as_floats);
289
290 bool load_binary (std::istream& is, bool swap, mach_info::float_format fmt);
291
292 bool save_hdf5 (octave_hdf5_id loc_id, const char *name, bool save_as_floats);
293
294 bool load_hdf5 (octave_hdf5_id& group_hid, octave_hdf5_id& space_hid,
295 octave_hdf5_id& type_hid);
296
297 void print_raw (std::ostream&, bool pr_as_read_syntax,
298 int current_print_indent_level) const;
299
300 friend bool is_equal_to (const scoped_fcn_handle& fh1,
301 const scoped_fcn_handle& fh2);
302
303 protected:
304
305 void find_function (void);
306
307 // The function we are handling.
309
310 // List of parent function names. The first element is the name of
311 // m_fcn.
312 std::list<std::string> m_parentage;
313 };
314
316 {
317 public:
318
319 // FIXME: octaveroot is temporary information used when loading
320 // handles. Can we avoid using it in the constructor?
321
322 base_nested_fcn_handle (const std::string& name = "",
323 const std::string& file = "",
324 const std::string& /*octaveroot*/ = "")
326 { }
327
328 base_nested_fcn_handle (const octave_value& fcn, const std::string& name)
329 : base_fcn_handle (name), m_fcn (fcn)
330 { }
331
332 std::string type (void) const { return "nested"; }
333
335
336 bool is_nested (void) const { return true; }
337
338 // FIXME: These must go away. They don't do the right thing for
339 // scoping or overloads.
341 {
342 return m_fcn.function_value ();
343 }
344
346 {
347 return m_fcn.user_function_value ();
348 }
349
350 octave_value fcn_val (void) { return m_fcn; }
351
352 virtual octave_value workspace (void) const = 0;
353
354 // Should be const.
355 octave_scalar_map info (void);
356
357 bool save_ascii (std::ostream& os);
358
359 bool load_ascii (std::istream& is);
360
361 bool save_binary (std::ostream& os, bool save_as_floats);
363 bool load_binary (std::istream& is, bool swap, mach_info::float_format fmt);
364
365 bool save_hdf5 (octave_hdf5_id loc_id, const char *name, bool save_as_floats);
366
367 bool load_hdf5 (octave_hdf5_id& group_hid, octave_hdf5_id& space_hid,
368 octave_hdf5_id& type_hid);
369
370 void print_raw (std::ostream&, bool pr_as_read_syntax,
371 int current_print_indent_level) const;
372
373 protected:
374
375 // The function we are handling.
377 };
378
380 {
381 public:
382
383 // FIXME: octaveroot is temporary information used when loading
384 // handles. Can we avoid using it in the constructor?
385
386 nested_fcn_handle (const std::string& name = "",
387 const std::string& file = "",
388 const std::string& octaveroot = "")
389 : base_nested_fcn_handle (name, file, octaveroot)
390 { }
391
392 nested_fcn_handle (const octave_value& fcn, const std::string& name,
393 const std::shared_ptr<stack_frame>& stack_context)
395 {
396 m_stack_context->mark_closure_context ();
397 }
398
400
401 ~nested_fcn_handle (void) = default;
402
404
405 bool is_nested (const std::shared_ptr<stack_frame>& frame) const
406 {
407 return frame == m_stack_context;
408 }
409
411 {
412 return new nested_fcn_handle (*this);
413 }
414
416
417 octave_value_list call (int nargout, const octave_value_list& args);
418
419 octave_value workspace (void) const;
420
421 friend bool is_equal_to (const nested_fcn_handle& fh1,
422 const nested_fcn_handle& fh2);
423
424 std::shared_ptr<stack_frame> stack_context (void) const
425 {
426 return m_stack_context;
427 }
428
429 protected:
430
431 // Pointer to closure stack frames.
432 std::shared_ptr<stack_frame> m_stack_context;
433 };
434
436 {
437 public:
438
440 : base_nested_fcn_handle (nfh), m_stack_context (nfh.stack_context ())
441 { }
442
444
445 ~weak_nested_fcn_handle (void) = default;
446
448 {
449 return new weak_nested_fcn_handle (*this);
450 }
451
452 bool is_weak_nested (void) const { return true; }
453
454 octave_value_list call (int nargout, const octave_value_list& args);
455
456 octave_value workspace (void) const;
457
458 friend bool is_equal_to (const weak_nested_fcn_handle& fh1,
459 const weak_nested_fcn_handle& fh2);
460
461 protected:
462
463 // Pointer to closure stack frames.
464 std::weak_ptr<stack_frame> m_stack_context;
465 };
466
468 {
469 public:
470
471 // FIXME: octaveroot is temporary information used when loading
472 // handles. Can we avoid using it in the constructor?
473
474 class_simple_fcn_handle (const std::string& name,
475 const std::string& file,
476 const std::string& /*octaveroot*/)
478 { }
479
480 // FIXME: is the method name supposed to be just the method name or
481 // also contain the object name?
482
483 class_simple_fcn_handle (const std::string& class_nm,
484 const std::string& meth_nm);
485
487 const std::string& class_nm,
488 const std::string& meth_nm);
489
490 class_simple_fcn_handle (const octave_value& obj, const octave_value& fcn,
491 const std::string& class_nm,
492 const std::string& meth_nm);
493
495
496 ~class_simple_fcn_handle (void) = default;
497
499 {
500 return new class_simple_fcn_handle (*this);
501 }
502
503 std::string type (void) const { return "classsimple"; }
504
505 bool is_class_simple (void) const { return true; }
506
507 octave_value_list call (int nargout, const octave_value_list& args);
508
509 // FIXME: These must go away. They don't do the right thing for
510 // scoping or overloads.
512 {
513 // FIXME: Shouldn't the lookup rules here match those used in the
514 // call method?
515
516 if (m_fcn.is_defined ())
517 return m_fcn.function_value ();
518
519 symbol_table& symtab
520 = __get_symbol_table__ ("class_simple_fcn_handle::function_value");
521
522 // FIXME: is caching the correct thing to do?
523 // Cache this value so that the pointer will be valid as long as the
524 // function handle object is valid.
525
526 // FIXME: This should probably dispatch to the respective class method.
527 // But that breaks if a function handle is used in a class method with
528 // e.g. bsxfun with arguments of a different class (see bug #59661).
529 // m_fcn = symtab.find_method (m_name, m_dispatch_class);
531
532 return m_fcn.is_defined () ? m_fcn.function_value () : nullptr;
533 }
534
536 {
537 return m_fcn.user_function_value ();
538 }
539
540 octave_value fcn_val (void) { return m_fcn; }
541
542 // Should be const.
543 octave_scalar_map info (void);
544
545 std::string dispatch_class (void) const { return m_dispatch_class; }
546
547 bool save_ascii (std::ostream& os);
548
549 bool load_ascii (std::istream& is);
550
551 bool save_binary (std::ostream& os, bool save_as_floats);
552
553 bool load_binary (std::istream& is, bool swap, mach_info::float_format fmt);
554
555 bool save_hdf5 (octave_hdf5_id loc_id, const char *name, bool save_as_floats);
556
557 bool load_hdf5 (octave_hdf5_id& group_hid, octave_hdf5_id& space_hid,
558 octave_hdf5_id& type_hid);
559
560 void print_raw (std::ostream&, bool pr_as_read_syntax,
561 int current_print_indent_level) const;
562
563 friend bool is_equal_to (const class_simple_fcn_handle& fh1,
564 const class_simple_fcn_handle& fh2);
565
566 protected:
567
568 // The object containing the method we are handing.
570
571 // The method we are handling.
573
574 // Name of the class that m_fcn belongs to.
575 std::string m_dispatch_class;
576 };
577
578 // Handles to anonymous functions are similar to handles to nested
579 // functions. If they are created in a context that contains nested
580 // functions, then they store a link to the parent call stack frames
581 // that are active when they are created. These call stack frames
582 // (closure frames) provide access to variables needed by any nested
583 // functions that are called from the anonymous function. Anonymous
584 // functions also store a list of values from their parent scope
585 // corresponding to the symbols in the anonymous function. This list
586 // of values captures the variable values that are visible in the
587 // scope where they are created.
588 //
589 // Note that because handles to anonymous and nested functions capture
590 // call stack frames when they are created, they will cause deletion
591 // of the values in those frames to be deferred until the handles to
592 // the anonymous or nested functions are deleted.
593 //
594 // Would it be possible to avoid storing the closure frames for
595 // handles to anonymous functions if we can determine that the
596 // anonymous function has no unbound variables (or parameters, which
597 // could be handles to nested functions?) or if it is not created in a
598 // context that contains nested functions?
599 //
600 // Would it be possible to define anonymous functions as a special
601 // type of nested function object that also has an variable
602 // initialization list associated with it?
603
605 {
606 public:
607
608 static const std::string anonymous;
609
610 // Setting NAME here is a bit of a kluge to cope with a bad choice
611 // made to append the number of local variables to the @<anonymous>
612 // tag in the binary file format. See also the save_binary and
613 // load_binary functions.
614
615 base_anonymous_fcn_handle (const std::string& name = "")
617 { }
618
620 const stack_frame::local_vars_map& local_vars)
621 : base_fcn_handle (anonymous), m_fcn (fcn), m_local_vars (local_vars)
622 { }
623
625
627
628 std::string type (void) const { return "anonymous"; }
629
630 bool is_anonymous (void) const { return true; }
631
632 // FIXME: These must go away. They don't do the right thing for
633 // scoping or overloads.
635 {
636 return m_fcn.function_value ();
637 }
638
640 {
641 return m_fcn.user_function_value ();
642 }
643
644 octave_value fcn_val (void) { return m_fcn; }
645
646 virtual octave_value workspace (void) const = 0;
647
648 // Should be const.
649 octave_scalar_map info (void);
650
651 bool save_ascii (std::ostream& os);
652
653 bool load_ascii (std::istream& is);
654
655 bool save_binary (std::ostream& os, bool save_as_floats);
656
657 bool load_binary (std::istream& is, bool swap, mach_info::float_format fmt);
658
659 bool save_hdf5 (octave_hdf5_id loc_id, const char *name, bool save_as_floats);
660
661 bool load_hdf5 (octave_hdf5_id& group_hid, octave_hdf5_id& space_hid,
662 octave_hdf5_id& type_hid);
663
664 void print_raw (std::ostream&, bool pr_as_read_syntax,
665 int current_print_indent_level) const;
666
667 // Anonymous function handles are printed without a newline.
668 bool print_as_scalar (void) const { return false; }
669
670 bool parse (const std::string& fcn_text);
671
672 protected:
673
674 // The function we are handling.
676
677 // List of captured variable values for anonymous fucntions.
678 stack_frame::local_vars_map m_local_vars;
679 };
680
682 {
683 public:
684
686
687 // Setting NAME here is a bit of a kluge to cope with a bad choice
688 // made to append the number of local variables to the @<anonymous>
689 // tag in the binary file format. See also the save_binary and
690 // load_binary functions.
691
692 anonymous_fcn_handle (const std::string& name = "")
694 { }
695
697 const stack_frame::local_vars_map& local_vars,
698 const std::shared_ptr<stack_frame>& stack_context = std::shared_ptr<stack_frame> ());
699
701
702 ~anonymous_fcn_handle (void) = default;
703
705 {
706 return new anonymous_fcn_handle (*this);
707 }
708
710
711 octave_value_list call (int nargout, const octave_value_list& args);
712
713 octave_value workspace (void) const;
714
715 friend bool is_equal_to (const anonymous_fcn_handle& fh1,
716 const anonymous_fcn_handle& fh2);
717
718 std::shared_ptr<stack_frame> stack_context (void) const
719 {
720 return m_stack_context;
721 }
722
723 protected:
724
725 // Pointer to closure stack frames.
726 std::shared_ptr<stack_frame> m_stack_context;
727 };
728
730 {
731 public:
732
734
736 : base_anonymous_fcn_handle (afh), m_stack_context (afh.stack_context ())
737 { }
738
740
742
744 {
745 return new weak_anonymous_fcn_handle (*this);
746 }
747
748 bool is_weak_anonymous (void) const { return true; }
749
750 octave_value_list call (int nargout, const octave_value_list& args);
751
752 octave_value workspace (void) const;
753
754 friend bool is_equal_to (const weak_anonymous_fcn_handle& fh1,
755 const weak_anonymous_fcn_handle& fh2);
756
757 protected:
758
759 // Pointer to closure stack frames.
760 std::weak_ptr<stack_frame> m_stack_context;
761 };
762
763 extern bool is_equal_to (const anonymous_fcn_handle& fh1,
764 const anonymous_fcn_handle& fh2);
765
766 static void err_invalid_fcn_handle (const std::string& name)
767 {
768 error ("invalid function handle, unable to find function for @%s",
769 name.c_str ());
770 }
771
773 {
774 std::string type_str = type ();
775 error ("invalid conversion from %s handle to weak nestead handle",
776 type_str.c_str ());
777 }
778
780 {
781 std::string type_str = type ();
782 error ("invalid conversion from %s handle to weak anonymous handle",
783 type_str.c_str ());
784 }
785
787 base_fcn_handle::subsref (const std::string& type,
788 const std::list<octave_value_list>& idx,
789 int nargout)
790 {
791 octave_value_list retval;
792
793 switch (type[0])
794 {
795 case '(':
796 {
797 int tmp_nargout = (type.length () > 1 && nargout == 0) ? 1 : nargout;
798
799 retval = call (tmp_nargout, idx.front ());
800 }
801 break;
802
803 case '{':
804 case '.':
805 error ("function handle cannot be indexed with %c", type[0]);
806
807 default:
809 }
810
811 // FIXME: perhaps there should be an
812 // octave_value_list::next_subsref member function? See also
813 // octave_builtin::subsref.
814
815 if (idx.size () > 1)
816 retval = retval(0).next_subsref (nargout, type, idx);
817
818 return retval;
819 }
820
822 base_fcn_handle::convert_to_str_internal (bool, bool, char type) const
823 {
824 std::ostringstream buf;
825 print_raw (buf, true, 0);
826 return octave_value (buf.str (), type);
827 }
828
829 bool
831 {
832 unimplemented ("save", "text");
833
834 return true;
835 }
836
837 bool
839 {
840 unimplemented ("load", "text");
841
842 return true;
843 }
844
845 bool
846 base_fcn_handle::save_binary (std::ostream&, bool)
847 {
848 unimplemented ("save", "binary");
849
850 return true;
851 }
852
853 bool
855 {
856 unimplemented ("load", "binary");
857
858 return true;
859 }
860
861 bool
863 {
864 unimplemented ("save", "hdf5");
865
866 return true;
867 }
868
869 bool
871 {
872 unimplemented ("load", "hdf5");
873
874 return true;
875 }
876
877 void base_fcn_handle::warn_load (const char *file_type) const
878 {
879 std::string obj_type = type ();
880
882 ("Octave:load-save-unavailable",
883 "%s: loading %s files not available in this version of Octave",
884 obj_type.c_str (), file_type);
885 }
886
887 void base_fcn_handle::warn_save (const char *file_type) const
888 {
889 std::string obj_type = type ();
890
892 ("Octave:load-save-unavailable",
893 "%s: saving %s files not available in this version of Octave",
894 obj_type.c_str (), file_type);
895 }
896
897 void base_fcn_handle::unimplemented (const char *op, const char *fmt) const
898 {
899 std::string htype = type ();
900
901 warning ("%s for %s handles with %s format is not implemented",
902 op, htype.c_str (), fmt);
903 }
904
907 {
908 error ("invalid call to invalid function handle");
909 }
910
913 {
914 interpreter& interp = __get_interpreter__ ("internal_fcn_handle::call");
915
916 return interp.feval (m_fcn, args, nargout);
917 }
918
920 {
922
923 m.setfield ("function", fcn_name ());
924 m.setfield ("type", type ());
925 m.setfield ("file", "");
926
927 return m;
928 }
929
931 const internal_fcn_handle& fh2)
932 {
933 if (fh1.m_name == fh2.m_name
934 && fh1.m_fcn.is_defined () && fh2.m_fcn.is_defined ())
935 return fh1.m_fcn.is_copy_of (fh2.m_fcn);
936 else
937 return false;
938 }
939
942 {
943 // FIXME: if m_name has a '.' in the name, lookup first component. If
944 // it is a classdef meta object, then build TYPE and IDX arguments and
945 // make a subsref call using them.
946
947 interpreter& interp = __get_interpreter__ ("simple_fcn_handle::call");
948
949 octave_value fcn_to_call;
950
951 // The following code is similar to part of
952 // tree_evaluator::visit_index_expression but simpler because it
953 // handles a more restricted case.
954
955 symbol_table& symtab = interp.get_symbol_table ();
956
957 std::size_t pos = m_name.find ('.');
958
959 if (pos != std::string::npos)
960 {
961 // FIXME: check to see which of these cases actually work in
962 // Octave and Matlab. For the last two, assume handle is
963 // created before object is defined as an object.
964 //
965 // We can have one of
966 //
967 // pkg-list . fcn (args)
968 // pkg-list . cls . meth (args)
969 // class-name . method (args)
970 // class-name . static-method (args)
971 // object . method (args)
972 // object . static-method (args)
973
974 // Evaluate package elements until we find a function,
975 // classdef object, or classdef_meta object that is not a
976 // package. An object may only appear as the first element,
977 // then it must be followed directly by a function name.
978
979 std::size_t beg = 0;
980 std::size_t end = pos;
981
982 std::vector<std::string> idx_elts;
983
984 while (true)
985 {
986 end = m_name.find ('.', beg);
987
988 idx_elts.push_back (m_name.substr (beg, end-beg));
989
990 if (end == std::string::npos)
991 break;
992
993 beg = end+1;
994 }
995
996 std::size_t n_elts = idx_elts.size ();
997
998 bool have_object = false;
999 octave_value partial_expr_val;
1000
1001 // Lazy evaluation. The first element was not known to be defined
1002 // as an object in the scope where the handle was created. See if
1003 // there is a definition in the current scope.
1004
1005 partial_expr_val = interp.varval (idx_elts[0]);
1006
1007 if (partial_expr_val.is_defined ())
1008 {
1009 if (! partial_expr_val.is_classdef_object () || n_elts != 2)
1011
1012 have_object = true;
1013 }
1014 else
1015 partial_expr_val = symtab.find_function (idx_elts[0], ovl ());
1016
1017 std::string type;
1018 std::list<octave_value_list> arg_list;
1019
1020 for (std::size_t i = 1; i < n_elts; i++)
1021 {
1022 if (partial_expr_val.is_package ())
1023 {
1024 if (have_object)
1026
1027 type = ".";
1028 arg_list.push_back (ovl (idx_elts[i]));
1029
1030 try
1031 {
1032 // Silently ignore extra output values.
1033
1034 octave_value_list tmp_list
1035 = partial_expr_val.subsref (type, arg_list, 0);
1036
1037 partial_expr_val
1038 = tmp_list.length () ? tmp_list(0) : octave_value ();
1039
1040 if (partial_expr_val.is_cs_list ())
1042
1043 arg_list.clear ();
1044 }
1045 catch (const index_exception&)
1046 {
1048 }
1049 }
1050 else if (have_object || partial_expr_val.is_classdef_meta ())
1051 {
1052 // Object or class name must be the next to the last
1053 // element (it was the previous one, so if this is the
1054 // final element, it should be a classdef method,
1055 // but we'll let the classdef or classdef_meta subsref
1056 // function sort that out.
1057
1058 if (i != n_elts-1)
1060
1061 type = ".(";
1062 arg_list.push_back (ovl (idx_elts[i]));
1063 arg_list.push_back (args);
1064
1065 return partial_expr_val.subsref (type, arg_list, nargout);
1066 }
1067 else
1069 }
1070
1071 // If we get here, we must have a function to call.
1072
1073 if (! partial_expr_val.is_function ())
1075
1076 fcn_to_call = partial_expr_val;
1077 }
1078 else
1079 {
1080 // No "." in the name.
1081
1082 // Perform function lookup given current arguments. We'll need
1083 // to do this regardless of whether a function was found when
1084 // the handle was created.
1085
1086 octave_value ov_fcn = symtab.find_function (m_name, args);
1087
1088 if (m_fcn.is_defined ())
1089 {
1090 // A simple function was found when the handle was created.
1091 // Use that unless we find a class method to override it.
1092
1093 fcn_to_call = m_fcn;
1094
1095 if (ov_fcn.is_defined ())
1096 {
1097 octave_function *fcn = ov_fcn.function_value ();
1098
1099 std::string dispatch_class = fcn->dispatch_class ();
1100
1101 if (fcn->is_class_method ())
1102 {
1103 // Function found through lookup is a class method
1104 // so use it instead of the simple one found when
1105 // the handle was created.
1106
1107 fcn_to_call = ov_fcn;
1108 }
1109 }
1110 }
1111 else
1112 {
1113 // There was no simple function found when the handle was
1114 // created so use the one found here (if any).
1115
1116 fcn_to_call = ov_fcn;
1117 }
1118 }
1119
1120 if (! fcn_to_call.is_defined ())
1122
1123 return interp.feval (fcn_to_call, args, nargout);
1124 }
1125
1127 {
1128 // FIXME: Shouldn't the lookup rules here match those used in the
1129 // call method?
1130
1131 if (m_fcn.is_defined ())
1132 return m_fcn.function_value ();
1133
1134 symbol_table& symtab
1135 = __get_symbol_table__ ("simple_fcn_handle::function_value");
1136
1137 // FIXME: is caching the correct thing to do?
1138 // Cache this value so that the pointer will be valid as long as the
1139 // function handle object is valid.
1140
1142
1143 return m_fcn.is_defined () ? m_fcn.function_value () : nullptr;
1144 }
1145
1147 {
1148 // FIXME: Shouldn't the lookup rules here match those used in the
1149 // call method?
1150
1151 if (m_fcn.is_defined ())
1152 return m_fcn.user_function_value ();
1153
1154 symbol_table& symtab
1155 = __get_symbol_table__ ("simple_fcn_handle::user_function_value");
1156
1157 // FIXME: is caching the correct thing to do?
1158 // Cache this value so that the pointer will be valid as long as the
1159 // function handle object is valid.
1160
1161 m_fcn = symtab.find_user_function (m_name);
1162
1163 return m_fcn.is_defined () ? m_fcn.user_function_value () : nullptr;
1164 }
1165
1167 {
1168 if (m_fcn.is_defined ())
1169 return m_fcn;
1170
1171 symbol_table& symtab
1172 = __get_symbol_table__ ("simple_fcn_handle::user_function_value");
1173
1174 // FIXME: is caching the correct thing to do?
1175 // Cache this value so that the pointer will be valid as long as the
1176 // function handle object is valid.
1177
1178 m_fcn = symtab.find_user_function (m_name);
1179
1180 return m_fcn;
1181 }
1182
1184 {
1186
1187 m.setfield ("function", fcn_name ());
1188 m.setfield ("type", type ());
1189 // When is FILE defined for simple function handles?
1190 m.setfield ("file", file ());
1191
1192 return m;
1193 }
1194
1195 bool simple_fcn_handle::save_ascii (std::ostream& os)
1196 {
1197 os << "# octaveroot: " << config::octave_exec_home () << "\n";
1198
1199 std::string fnm = file ();
1200 if (! fnm.empty ())
1201 os << "# path: " << fnm << "\n";
1202
1203 os << "# subtype: " << type () << "\n";
1204
1205 os << m_name << "\n";
1206
1207 return true;
1208 }
1209
1210 bool simple_fcn_handle::load_ascii (std::istream& is)
1211 {
1212 // FIXME: If m_file is not empty, try to load the file and define
1213 // the function? Is it an error if that fails? Or should this job
1214 // always be deferred until the handle is used?
1215
1216 return is.good ();
1217 }
1218
1219 bool simple_fcn_handle::save_binary (std::ostream& os, bool)
1220 {
1221 std::ostringstream nmbuf;
1222
1223 // When is FILE defined for simple function handles?
1224 std::string fnm;
1225
1226 nmbuf << m_name << "@<simple>\n" << config::octave_exec_home ()
1227 << "\n" << fnm;
1228
1229 std::string buf_str = nmbuf.str ();
1230 int32_t tmp = buf_str.length ();
1231 os.write (reinterpret_cast<char *> (&tmp), 4);
1232 os.write (buf_str.c_str (), buf_str.length ());
1233
1234 return true;
1235 }
1236
1237 bool simple_fcn_handle::load_binary (std::istream& is, bool,
1239 {
1240 return is.good ();
1241 }
1242
1244 bool)
1245 {
1246#if defined (HAVE_HDF5)
1247
1248 bool retval = true;
1249
1250 octave_hdf5_id group_hid = -1;
1251#if defined (HAVE_HDF5_18)
1252 group_hid = H5Gcreate (loc_id, name, octave_H5P_DEFAULT, octave_H5P_DEFAULT,
1254#else
1255 group_hid = H5Gcreate (loc_id, name, 0);
1256#endif
1257 if (group_hid < 0)
1258 return false;
1259
1260 octave_hdf5_id space_hid, data_hid, type_hid;
1261 space_hid = data_hid = type_hid = -1;
1262
1263 // attach the type of the variable
1264 type_hid = H5Tcopy (H5T_C_S1);
1265 H5Tset_size (type_hid, m_name.length () + 1);
1266 if (type_hid < 0)
1267 {
1268 H5Gclose (group_hid);
1269 return false;
1270 }
1271
1272 OCTAVE_LOCAL_BUFFER (hsize_t, hdims, 2);
1273 hdims[0] = 0;
1274 hdims[1] = 0;
1275 space_hid = H5Screate_simple (0, hdims, nullptr);
1276 if (space_hid < 0)
1277 {
1278 H5Tclose (type_hid);
1279 H5Gclose (group_hid);
1280 return false;
1281 }
1282#if defined (HAVE_HDF5_18)
1283 data_hid = H5Dcreate (group_hid, "nm", type_hid, space_hid,
1286#else
1287 data_hid = H5Dcreate (group_hid, "nm", type_hid, space_hid,
1289#endif
1290 if (data_hid < 0
1291 || H5Dwrite (data_hid, type_hid, octave_H5S_ALL, octave_H5S_ALL,
1292 octave_H5P_DEFAULT, m_name.c_str ()) < 0)
1293 {
1294 H5Sclose (space_hid);
1295 H5Tclose (type_hid);
1296 H5Gclose (group_hid);
1297 return false;
1298 }
1299 H5Dclose (data_hid);
1300
1301 std::string octaveroot = config::octave_exec_home ();
1302
1303 // When is FILE defined for simple fucntion handles?
1304 std::string fpath;
1305
1306 H5Sclose (space_hid);
1307 hdims[0] = 1;
1308 hdims[1] = octaveroot.length ();
1309 space_hid = H5Screate_simple (0, hdims, nullptr);
1310 if (space_hid < 0)
1311 {
1312 H5Tclose (type_hid);
1313 H5Gclose (group_hid);
1314 return false;
1315 }
1316
1317 H5Tclose (type_hid);
1318 type_hid = H5Tcopy (H5T_C_S1);
1319 H5Tset_size (type_hid, octaveroot.length () + 1);
1320 octave_hdf5_id a_id;
1321#if defined (HAVE_HDF5_18)
1322 a_id = H5Acreate (group_hid, "OCTAVEROOT", type_hid, space_hid,
1324#else
1325 a_id = H5Acreate (group_hid, "OCTAVEROOT", type_hid, space_hid,
1327#endif
1328
1329 if (a_id >= 0)
1330 {
1331 retval = (H5Awrite (a_id, type_hid, octaveroot.c_str ()) >= 0);
1332
1333 H5Aclose (a_id);
1334 }
1335 else
1336 {
1337 H5Sclose (space_hid);
1338 H5Tclose (type_hid);
1339 H5Gclose (group_hid);
1340 return false;
1341 }
1342
1343 H5Sclose (space_hid);
1344 hdims[0] = 1;
1345 hdims[1] = fpath.length ();
1346 space_hid = H5Screate_simple (0, hdims, nullptr);
1347 if (space_hid < 0)
1348 {
1349 H5Tclose (type_hid);
1350 H5Gclose (group_hid);
1351 return false;
1352 }
1353
1354 H5Tclose (type_hid);
1355 type_hid = H5Tcopy (H5T_C_S1);
1356 H5Tset_size (type_hid, fpath.length () + 1);
1357
1358#if defined (HAVE_HDF5_18)
1359 a_id = H5Acreate (group_hid, "FILE", type_hid, space_hid,
1361#else
1362 a_id = H5Acreate (group_hid, "FILE", type_hid, space_hid, octave_H5P_DEFAULT);
1363#endif
1364
1365 if (a_id >= 0)
1366 {
1367 retval = (H5Awrite (a_id, type_hid, fpath.c_str ()) >= 0);
1368
1369 H5Aclose (a_id);
1370 }
1371 else
1372 retval = false;
1373
1374 H5Sclose (space_hid);
1375 H5Tclose (type_hid);
1376 H5Gclose (group_hid);
1377
1378 return retval;
1379
1380#else
1381
1382 octave_unused_parameter (loc_id);
1383 octave_unused_parameter (name);
1384
1385 warn_save ("hdf5");
1386
1387 return false;
1388
1389#endif
1390 }
1391
1393 octave_hdf5_id& space_hid,
1394 octave_hdf5_id& type_hid)
1395 {
1396#if defined (HAVE_HDF5)
1397
1398 unimplemented ("load", "hdf5");
1399
1400 octave_unused_parameter (group_hid);
1401 octave_unused_parameter (space_hid);
1402 octave_unused_parameter (type_hid);
1403
1404 return true;
1405
1406#else
1407
1408 octave_unused_parameter (group_hid);
1409 octave_unused_parameter (space_hid);
1410 octave_unused_parameter (type_hid);
1411
1412 return false;
1413
1414#endif
1415 }
1416
1417 void simple_fcn_handle::print_raw (std::ostream& os, bool pr_as_read_syntax,
1418 int current_print_indent_level) const
1419 {
1420 octave_print_internal (os, '@' + m_name, pr_as_read_syntax,
1421 current_print_indent_level);
1422 }
1423
1425 {
1426 if (fh1.m_name == fh2.m_name)
1427 {
1428 if (fh1.m_fcn.is_defined () && fh2.m_fcn.is_defined ())
1429 return fh1.m_fcn.is_copy_of (fh2.m_fcn);
1430
1431 if (fh1.m_fcn.is_undefined () && fh2.m_fcn.is_undefined ())
1432 return true;
1433 }
1434
1435 return false;
1436 }
1437
1439 const std::string& name,
1440 const std::list<std::string>& parentage)
1441 : base_fcn_handle (name), m_fcn (fcn), m_parentage (parentage)
1442 {
1443 // FIXME: should it be an error if FCN is undefined?
1444
1445 if (m_fcn.is_defined ())
1446 {
1447 octave_function *oct_fcn = m_fcn.function_value ();
1448
1449 if (oct_fcn)
1450 m_file = oct_fcn->fcn_file_name ();
1451 }
1452
1453 m_parentage.push_front (name);
1454 }
1455
1458 {
1459 // FIXME: we aren't really using the scope yet. Hmm.
1460
1461 interpreter& interp = __get_interpreter__ ("simple_fcn_handle::call");
1462
1463 if (! m_fcn.is_defined ())
1464 {
1465 // Try to find it?
1466
1467 find_function ();
1468 }
1469
1470 if (! m_fcn.is_defined ())
1472
1473 return interp.feval (m_fcn, args, nargout);
1474 }
1475
1477 {
1479
1480 m.setfield ("function", fcn_name ());
1481 m.setfield ("type", type ());
1482 m.setfield ("file", file ());
1483
1484 m.setfield ("parentage", Cell (m_parentage));
1485
1486 return m;
1487 }
1488
1489 bool scoped_fcn_handle::save_ascii (std::ostream& os)
1490 {
1491 os << "# octaveroot: " << config::octave_exec_home () << "\n";
1492
1493 std::string fnm = file ();
1494 if (! fnm.empty ())
1495 os << "# path: " << fnm << "\n";
1496
1497 os << "# subtype: " << type () << "\n";
1498
1499 os << m_name << "\n";
1500
1502 tmp.save_ascii (os);
1503
1504 return os.good ();
1505 }
1506
1507 bool scoped_fcn_handle::load_ascii (std::istream& is)
1508 {
1509 octave_cell ov_cell;
1510 ov_cell.load_ascii (is);
1511
1512 if (ov_cell.iscellstr ())
1513 {
1514 Array<std::string> cellstr_val = ov_cell.cellstr_value ();
1515
1516 for (octave_idx_type i = 0; i < cellstr_val.numel (); i++)
1517 m_parentage.push_back (cellstr_val(i));
1518 }
1519
1520 return is.good ();
1521 }
1522
1523 bool scoped_fcn_handle::save_binary (std::ostream& os, bool save_as_floats)
1524 {
1525 std::ostringstream nmbuf;
1526
1527 std::string fnm = file ();
1528
1529 nmbuf << m_name << "@<scopedfunction>\n" << config::octave_exec_home ()
1530 << "\n" << fnm;
1531
1532 std::string buf_str = nmbuf.str ();
1533 int32_t len = buf_str.length ();
1534 os.write (reinterpret_cast<char *> (&len), 4);
1535 os.write (buf_str.c_str (), buf_str.length ());
1536
1538 tmp.save_binary (os, save_as_floats);
1539
1540 return os.good ();
1541 }
1542
1543 bool scoped_fcn_handle::load_binary (std::istream& is, bool swap,
1545 {
1546 octave_cell ov_cell;
1547 ov_cell.load_binary (is, swap, fmt);
1548
1549 if (ov_cell.iscellstr ())
1550 {
1551 Array<std::string> cellstr_val = ov_cell.cellstr_value ();
1552
1553 for (octave_idx_type i = 0; i < cellstr_val.numel (); i++)
1554 m_parentage.push_back (cellstr_val(i));
1555 }
1556
1557 return is.good ();
1558 }
1559
1561 bool)
1562 {
1563#if defined (HAVE_HDF5)
1564
1565 unimplemented ("save", "hdf5");
1566
1567 // FIXME: save parentage.
1568
1569 octave_unused_parameter (loc_id);
1570 octave_unused_parameter (name);
1571
1572 return true;
1573
1574#else
1575
1576 octave_unused_parameter (loc_id);
1577 octave_unused_parameter (name);
1578
1579 warn_save ("hdf5");
1580
1581 return false;
1582
1583#endif
1584 }
1585
1587 octave_hdf5_id& space_hid,
1588 octave_hdf5_id& type_hid)
1589 {
1590#if defined (HAVE_HDF5)
1591
1592 unimplemented ("load", "hdf5");
1593
1594 // FIXME: load parentage.
1595
1596 octave_unused_parameter (group_hid);
1597 octave_unused_parameter (space_hid);
1598 octave_unused_parameter (type_hid);
1599
1600 return true;
1601
1602#else
1603
1604 octave_unused_parameter (group_hid);
1605 octave_unused_parameter (space_hid);
1606 octave_unused_parameter (type_hid);
1607
1608 return false;
1609
1610#endif
1611 }
1612
1613 void scoped_fcn_handle::print_raw (std::ostream& os,
1614 bool pr_as_read_syntax,
1615 int current_print_indent_level) const
1616 {
1617 octave_print_internal (os, '@' + m_name, pr_as_read_syntax,
1618 current_print_indent_level);
1619 }
1620
1622 {
1623 if (fh1.m_name == fh2.m_name
1624 && fh2.m_parentage == fh2.m_parentage
1625 && fh1.m_fcn.is_defined () && fh2.m_fcn.is_defined ())
1626 return fh1.m_fcn.is_copy_of (fh2.m_fcn);
1627 else
1628 return false;
1629 }
1630
1632 {
1633 // Since a scoped function is not visible by itself, try to load the
1634 // file named in m_file then find and define the scoped function.
1635 // It is not an error if this fails. We can report later that the
1636 // handle is invalid.
1637
1638 symbol_table& symtab
1639 = __get_symbol_table__ ("scoped_fcn_handle::find_function");
1640
1641 if (m_parentage.size () == 1)
1642 {
1643 std::string dir_name = sys::file_ops::dirname (m_file);
1644
1645 std::size_t pos = dir_name.find_last_of (sys::file_ops::dir_sep_chars ());
1646
1647 if (pos != std::string::npos)
1648 dir_name = dir_name.substr (0, pos);
1649 else if (dir_name == "private")
1650 dir_name = ".";
1651
1652 std::string fcn_name = m_parentage.front ();
1653
1654 // FIXME: Does dir_name need to be in the load path for this to work?
1655
1656 m_fcn = symtab.find_private_function (dir_name, m_name);
1657
1658 // FIXME: Verify that it is a private function?
1659 }
1660 else
1661 {
1662 std::string primary_parent_name = m_parentage.back ();
1663
1664 octave_value ov_parent_fcn
1665 = symtab.find_user_function (primary_parent_name);
1666
1667 if (ov_parent_fcn.is_defined ())
1668 {
1669 octave_user_function *fcn = ov_parent_fcn.user_function_value ();
1670
1671 if (fcn)
1672 {
1673 std::string file_name = fcn->fcn_file_name ();
1674
1675 std::string oct_home = config::octave_exec_home ();
1676
1677 if (file_name.substr (0, oct_home.size ()) == oct_home)
1678 file_name = file_name.substr (oct_home.size ());
1679
1680 octave_value subfun = fcn->find_subfunction (m_name);
1681
1682 if (subfun.is_defined ())
1683 m_fcn = subfun;
1684 }
1685 }
1686 }
1687 }
1688
1690 {
1692
1693 m.setfield ("function", fcn_name ());
1694 m.setfield ("type", type ());
1695 m.setfield ("file", "");
1696 m.setfield ("workspace", workspace ());
1697
1698 return m;
1699 }
1700
1701 // FIXME: For save, we need a way to save the (possibly shared)
1702 // workspace. For load, we need a way to load and link to the
1703 // (possibly shared) workspace that was saved.
1704 //
1705 // Since a nested function is not visible by itself, do we need to try
1706 // to load the file named in m_file then find and define the function?
1707 // Is it an error if that fails? Or should this job always be
1708 // deferred until the handle is used?
1709
1711 {
1712 unimplemented ("save", "text");
1713
1714 octave_unused_parameter (os);
1715
1716 return true;
1717 }
1718
1720 {
1721 unimplemented ("load", "text");
1722
1723 octave_unused_parameter (is);
1724
1725 return true;
1726 }
1727
1729 bool save_as_floats)
1730 {
1731 unimplemented ("save", "binary");
1732
1733 octave_unused_parameter (os);
1734 octave_unused_parameter (save_as_floats);
1735
1736 return true;
1737 }
1738
1739 bool base_nested_fcn_handle::load_binary (std::istream& is, bool swap,
1741 {
1742 unimplemented ("load", "binary");
1743
1744 octave_unused_parameter (is);
1745 octave_unused_parameter (swap);
1746 octave_unused_parameter (fmt);
1747
1748 return true;
1749 }
1750
1752 const char *name, bool)
1753 {
1754#if defined (HAVE_HDF5)
1755
1756 unimplemented ("save", "hdf5");
1757
1758 octave_unused_parameter (loc_id);
1759 octave_unused_parameter (name);
1760
1761 return true;
1762
1763#else
1764
1765 octave_unused_parameter (loc_id);
1766 octave_unused_parameter (name);
1767
1768 warn_save ("hdf5");
1769
1770 return false;
1771
1772#endif
1773 }
1774
1776 octave_hdf5_id& space_hid,
1777 octave_hdf5_id& type_hid)
1778 {
1779#if defined (HAVE_HDF5)
1780
1781 unimplemented ("load", "hdf5");
1782
1783 octave_unused_parameter (group_hid);
1784 octave_unused_parameter (space_hid);
1785 octave_unused_parameter (type_hid);
1786
1787 return true;
1788
1789#else
1790
1791 octave_unused_parameter (group_hid);
1792 octave_unused_parameter (space_hid);
1793 octave_unused_parameter (type_hid);
1794
1795 return false;
1796
1797#endif
1798 }
1799
1801 bool pr_as_read_syntax,
1802 int current_print_indent_level) const
1803 {
1804 octave_print_internal (os, '@' + m_name, pr_as_read_syntax,
1805 current_print_indent_level);
1806 }
1807
1809 {
1811 (new weak_nested_fcn_handle (*this)));
1812 }
1813
1816 {
1817 tree_evaluator& tw = __get_evaluator__ ("nested_fcn_handle::call");
1818
1820
1821 tw.push_stack_frame (oct_usr_fcn, m_stack_context);
1822
1823 unwind_action act ([&tw] () { tw.pop_stack_frame (); });
1824
1825 return oct_usr_fcn->execute (tw, nargout, args);
1826 }
1827
1829 {
1830 return m_stack_context->workspace ();
1831 }
1832
1834 {
1835 if (fh1.m_name == fh2.m_name
1836 && fh1.m_fcn.is_defined () && fh2.m_fcn.is_defined ())
1837 return fh1.m_fcn.is_copy_of (fh2.m_fcn);
1838 else
1839 return false;
1840 }
1841
1844 {
1845 tree_evaluator& tw = __get_evaluator__ ("weak_nested_fcn_handle::call");
1846
1848
1849 std::shared_ptr<stack_frame> frames = m_stack_context.lock ();
1850
1851 tw.push_stack_frame (oct_usr_fcn, frames);
1852
1853 unwind_action act ([&tw] () { tw.pop_stack_frame (); });
1854
1855 return oct_usr_fcn->execute (tw, nargout, args);
1856 }
1857
1859 {
1860 std::shared_ptr<stack_frame> frames = m_stack_context.lock ();
1861
1862 return frames ? frames->workspace () : octave_value ();
1863 }
1864
1866 const weak_nested_fcn_handle& fh2)
1867 {
1868 if (fh1.m_name == fh2.m_name
1869 && fh1.m_fcn.is_defined () && fh2.m_fcn.is_defined ())
1870 return fh1.m_fcn.is_copy_of (fh2.m_fcn);
1871 else
1872 return false;
1873 }
1874
1876 const std::string& meth_nm)
1877 : base_fcn_handle (meth_nm), m_obj (), m_fcn (),
1878 m_dispatch_class (class_nm)
1879 { }
1880
1882 const std::string& class_nm,
1883 const std::string& meth_nm)
1884 : base_fcn_handle (meth_nm), m_obj (), m_fcn (fcn),
1885 m_dispatch_class (class_nm)
1886 { }
1887
1889 const octave_value& fcn,
1890 const std::string& class_nm,
1891 const std::string& meth_nm)
1892 : base_fcn_handle (meth_nm), m_obj (obj), m_fcn (fcn),
1893 m_dispatch_class (class_nm)
1894 { }
1895
1898 {
1899 interpreter& interp = __get_interpreter__ ("class_simple_fcn_handle::call");
1900
1901 if (m_obj.is_defined ())
1902 {
1903 octave_value_list tmp_args = args;
1904 tmp_args.prepend (m_obj);
1905
1906 return interp.feval (m_fcn, tmp_args, nargout);
1907 }
1908
1909 // FIXME: is this the best approach? Should we be saving current
1910 // dispatch class and restoring that value instead of
1911 // unconditionally setting it to "" when we return from this
1912 // function?
1913
1914 tree_evaluator& tw = interp.get_evaluator ();
1915
1916 unwind_action act ([&tw] () { tw.set_dispatch_class (""); });
1917
1919
1920 if (m_fcn.is_defined ())
1921 return interp.feval (m_fcn, args, nargout);
1922
1923 return interp.feval (fcn_name (), args, nargout);
1924 }
1925
1927 {
1929
1930 m.setfield ("function", fcn_name ());
1931 m.setfield ("type", type ());
1932 m.setfield ("file", "");
1933 m.setfield ("class", dispatch_class ());
1934
1935 return m;
1936 }
1937
1938 // FIXME: Since a class method is not visible by itself, do we need to
1939 // try to load the file named in m_file then find and define the
1940 // function? Is it an error if that fails? Or should this job always
1941 // be deferred until the handle is used?
1942
1944 {
1945 unimplemented ("save", "text");
1946
1947 octave_unused_parameter (os);
1948
1949 return true;
1950 }
1951
1953 {
1954 unimplemented ("load", "text");
1955
1956 octave_unused_parameter (is);
1957
1958 return true;
1959 }
1960
1962 bool save_as_floats)
1963 {
1964 unimplemented ("save", "binary");
1965
1966 octave_unused_parameter (os);
1967 octave_unused_parameter (save_as_floats);
1968
1969 return true;
1970 }
1971
1972 bool class_simple_fcn_handle::load_binary (std::istream& is, bool swap,
1974 {
1975 unimplemented ("load", "binary");
1976
1977 octave_unused_parameter (is);
1978 octave_unused_parameter (swap);
1979 octave_unused_parameter (fmt);
1980
1981 return true;
1982 }
1983
1985 const char *name, bool)
1986 {
1987#if defined (HAVE_HDF5)
1988
1989 unimplemented ("save", "hdf5");
1990
1991 octave_unused_parameter (loc_id);
1992 octave_unused_parameter (name);
1993
1994 return true;
1995
1996#else
1997
1998 octave_unused_parameter (loc_id);
1999 octave_unused_parameter (name);
2000
2001 warn_save ("hdf5");
2002
2003 return false;
2004
2005#endif
2006 }
2007
2009 octave_hdf5_id& space_hid,
2010 octave_hdf5_id& type_hid)
2011 {
2012#if defined (HAVE_HDF5)
2013
2014 unimplemented ("load", "hdf5");
2015
2016 octave_unused_parameter (group_hid);
2017 octave_unused_parameter (space_hid);
2018 octave_unused_parameter (type_hid);
2019
2020 return true;
2021
2022#else
2023
2024 octave_unused_parameter (group_hid);
2025 octave_unused_parameter (space_hid);
2026 octave_unused_parameter (type_hid);
2027
2028 return false;
2029
2030#endif
2031 }
2032
2034 bool pr_as_read_syntax,
2035 int current_print_indent_level) const
2036 {
2037 octave_print_internal (os, '@' + m_name, pr_as_read_syntax,
2038 current_print_indent_level);
2039 }
2040
2042 const class_simple_fcn_handle& fh2)
2043 {
2044 // FIXME: Also need to check object values are equivalent?
2045
2046 if (fh1.m_name == fh2.m_name
2047 && fh1.m_fcn.is_defined () && fh2.m_fcn.is_defined ())
2048 return fh1.m_fcn.is_copy_of (fh2.m_fcn);
2049 else
2050 return false;
2051 }
2052
2053 const std::string base_anonymous_fcn_handle::anonymous ("@<anonymous>");
2054
2056 {
2058
2059 std::ostringstream buf;
2060 print_raw (buf, true, 0);
2061 m.setfield ("function", buf.str ());
2062
2063 m.setfield ("type", type ());
2064 m.setfield ("file", "");
2065 m.setfield ("workspace", workspace ());
2066 m.setfield ("within_file_path", "");
2067
2068 return m;
2069 }
2070
2072 {
2073 // FIXME: can we ensure that m_fcn is always defined?
2074
2075 if (m_fcn.is_undefined ())
2076 return false;
2077
2078 os << m_name << "\n";
2079
2080 print_raw (os, true, 0);
2081 os << "\n";
2082
2083 std::size_t varlen = m_local_vars.size ();
2084
2085 if (varlen > 0)
2086 {
2087 os << "# length: " << varlen << "\n";
2088
2089 for (const auto& nm_val : m_local_vars)
2090 {
2091 if (! save_text_data (os, nm_val.second, nm_val.first, false, 0))
2092 return ! os.fail ();
2093 }
2094 }
2095
2096 return true;
2097 }
2098
2100 {
2102
2103 std::string buf;
2104
2105 if (is)
2106 {
2107 // Get a line of text whitespace characters included, leaving
2108 // newline in the stream.
2109
2110 buf = octave::read_until_newline (is, true);
2111 }
2112
2113 std::streampos pos = is.tellg ();
2114
2115 // Set up temporary scope to use for evaluating the text that
2116 // defines the anonymous function.
2117
2118 interpreter& interp
2119 = __get_interpreter__ ("base_anonymous_fcn_handle::load_ascii");
2120
2121 tree_evaluator& tw = interp.get_evaluator ();
2122
2123 tw.push_dummy_scope (buf);
2124 unwind_action_safe restore_scope (&tree_evaluator::pop_scope, &tw);
2125
2126 octave_idx_type len = 0;
2127
2128 if (extract_keyword (is, "length", len, true) && len >= 0)
2129 {
2130 if (len > 0)
2131 {
2132 for (octave_idx_type i = 0; i < len; i++)
2133 {
2134 octave_value t2;
2135 bool dummy;
2136
2137 std::string name = read_text_data (is, "", dummy, t2, i);
2138
2139 if (! is)
2140 error ("load: failed to load anonymous function handle");
2141
2142 m_local_vars[name] = t2;
2143 }
2144 }
2145 }
2146 else
2147 {
2148 is.seekg (pos);
2149 is.clear ();
2150 }
2151
2152 if (is)
2153 return parse (buf);
2154
2155 return false;
2156 }
2157
2159 bool save_as_floats)
2160 {
2161 // FIXME: can we ensure that m_fcn is always defined?
2162
2163 if (m_fcn.is_undefined ())
2164 return false;
2165
2166 std::ostringstream nmbuf;
2167
2168 std::size_t varlen = m_local_vars.size ();
2169
2170 nmbuf << anonymous;
2171 if (varlen > 0)
2172 nmbuf << ' ' << varlen;
2173
2174 std::string buf_str = nmbuf.str ();
2175 int32_t tmp = buf_str.length ();
2176 os.write (reinterpret_cast<char *> (&tmp), 4);
2177 os.write (buf_str.c_str (), buf_str.length ());
2178
2179 std::ostringstream buf;
2180 print_raw (buf, true, 0);
2181 std::string stmp = buf.str ();
2182 tmp = stmp.length ();
2183 os.write (reinterpret_cast<char *> (&tmp), 4);
2184 os.write (stmp.c_str (), stmp.length ());
2185
2186 if (varlen > 0)
2187 {
2188 for (const auto& nm_val : m_local_vars)
2189 {
2190 if (! save_binary_data (os, nm_val.second, nm_val.first,
2191 "", 0, save_as_floats))
2192 return ! os.fail ();
2193 }
2194 }
2195
2196 return true;
2197 }
2198
2199 bool base_anonymous_fcn_handle::load_binary (std::istream& is, bool swap,
2201 {
2202 // Read extra characters in m_name as the number of local variable
2203 // values in this anonymous function.
2204
2205 octave_idx_type len = 0;
2206 std::size_t anl = anonymous.length ();
2207 if (m_name.length () > anl)
2208 {
2209 std::istringstream nm_is (m_name.substr (anl));
2210 nm_is >> len;
2211
2212 // Anonymous functons don't have names. We just used this
2213 // string as temporary storage to pass the number of local
2214 // variable values.
2215
2216 m_name = "";
2217 }
2218
2219 int32_t tmp;
2220
2221 if (! is.read (reinterpret_cast<char *> (&tmp), 4))
2222 return false;
2223 if (swap)
2224 swap_bytes<4> (&tmp);
2225
2226 OCTAVE_LOCAL_BUFFER (char, ctmp2, tmp+1);
2227 // is.get (ctmp2, tmp+1, 0); caused is.eof () to be true though
2228 // effectively not reading over file end
2229 is.read (ctmp2, tmp);
2230 ctmp2[tmp] = 0;
2231
2232 // Set up temporary scope to use for evaluating the text that
2233 // defines the anonymous function.
2234
2235 interpreter& interp
2236 = __get_interpreter__ ("base_anonymous_fcn_handle::load_binary");
2237
2238 tree_evaluator& tw = interp.get_evaluator ();
2239
2240 tw.push_dummy_scope (ctmp2);
2241 unwind_action_safe restore_scope (&tree_evaluator::pop_scope, &tw);
2242
2243 if (len > 0)
2244 {
2245 for (octave_idx_type i = 0; i < len; i++)
2246 {
2247 octave_value t2;
2248 bool dummy;
2249 std::string doc;
2250
2251 std::string name
2252 = read_binary_data (is, swap, fmt, "", dummy, t2, doc);
2253
2254 if (! is)
2255 error ("load: failed to load anonymous function handle");
2256
2257 m_local_vars[name] = t2;
2258 }
2259 }
2260
2261 if (is)
2262 return parse (ctmp2);
2263
2264 return false;
2265 }
2266
2268 const char *name,
2269 bool save_as_floats)
2270 {
2271#if defined (HAVE_HDF5)
2272
2273 bool retval = true;
2274
2275 octave_hdf5_id group_hid = -1;
2276#if defined (HAVE_HDF5_18)
2277 group_hid = H5Gcreate (loc_id, name, octave_H5P_DEFAULT, octave_H5P_DEFAULT,
2279#else
2280 group_hid = H5Gcreate (loc_id, name, 0);
2281#endif
2282 if (group_hid < 0)
2283 return false;
2284
2285 octave_hdf5_id space_hid, data_hid, type_hid;
2286 space_hid = data_hid = type_hid = -1;
2287
2288 // attach the type of the variable
2289 type_hid = H5Tcopy (H5T_C_S1);
2290 H5Tset_size (type_hid, m_name.length () + 1);
2291 if (type_hid < 0)
2292 {
2293 H5Gclose (group_hid);
2294 return false;
2295 }
2296
2297 OCTAVE_LOCAL_BUFFER (hsize_t, hdims, 2);
2298 hdims[0] = 0;
2299 hdims[1] = 0;
2300 space_hid = H5Screate_simple (0, hdims, nullptr);
2301 if (space_hid < 0)
2302 {
2303 H5Tclose (type_hid);
2304 H5Gclose (group_hid);
2305 return false;
2306 }
2307#if defined (HAVE_HDF5_18)
2308 data_hid = H5Dcreate (group_hid, "nm", type_hid, space_hid,
2311#else
2312 data_hid = H5Dcreate (group_hid, "nm", type_hid, space_hid,
2314#endif
2315 if (data_hid < 0
2316 || H5Dwrite (data_hid, type_hid, octave_H5S_ALL, octave_H5S_ALL,
2317 octave_H5P_DEFAULT, m_name.c_str ()) < 0)
2318 {
2319 H5Sclose (space_hid);
2320 H5Tclose (type_hid);
2321 H5Gclose (group_hid);
2322 return false;
2323 }
2324 H5Dclose (data_hid);
2325
2326 std::ostringstream buf;
2327 print_raw (buf, true, 0);
2328 std::string stmp = buf.str ();
2329
2330 // attach the type of the variable
2331 H5Tset_size (type_hid, stmp.length () + 1);
2332 if (type_hid < 0)
2333 {
2334 H5Sclose (space_hid);
2335 H5Gclose (group_hid);
2336 return false;
2337 }
2338
2339#if defined (HAVE_HDF5_18)
2340 data_hid = H5Dcreate (group_hid, "fcn", type_hid, space_hid,
2343#else
2344 data_hid = H5Dcreate (group_hid, "fcn", type_hid, space_hid,
2346#endif
2347 if (data_hid < 0
2348 || H5Dwrite (data_hid, type_hid, octave_H5S_ALL, octave_H5S_ALL,
2349 octave_H5P_DEFAULT, stmp.c_str ()) < 0)
2350 {
2351 H5Sclose (space_hid);
2352 H5Tclose (type_hid);
2353 H5Gclose (group_hid);
2354 return false;
2355 }
2356
2357 H5Dclose (data_hid);
2358
2359 std::size_t varlen = m_local_vars.size ();
2360
2361 if (varlen > 0)
2362 {
2363 octave_hdf5_id as_id = H5Screate (H5S_SCALAR);
2364
2365 if (as_id >= 0)
2366 {
2367 octave_hdf5_id a_id;
2368#if defined (HAVE_HDF5_18)
2369 a_id = H5Acreate (group_hid, "SYMBOL_TABLE", H5T_NATIVE_IDX, as_id,
2371
2372#else
2373 a_id = H5Acreate (group_hid, "SYMBOL_TABLE", H5T_NATIVE_IDX, as_id,
2375#endif
2376
2377 if (a_id >= 0)
2378 {
2379 retval = (H5Awrite (a_id, H5T_NATIVE_IDX, &varlen) >= 0);
2380
2381 H5Aclose (a_id);
2382 }
2383 else
2384 retval = false;
2385
2386 H5Sclose (as_id);
2387 }
2388 else
2389 retval = false;
2390#if defined (HAVE_HDF5_18)
2391 data_hid = H5Gcreate (group_hid, "symbol table",
2393#else
2394 data_hid = H5Gcreate (group_hid, "symbol table", 0);
2395#endif
2396 if (data_hid < 0)
2397 {
2398 H5Sclose (space_hid);
2399 H5Tclose (type_hid);
2400 H5Gclose (group_hid);
2401 return false;
2402 }
2403
2404 for (const auto& nm_val : m_local_vars)
2405 {
2406 if (! add_hdf5_data (data_hid, nm_val.second, nm_val.first,
2407 "", false, save_as_floats))
2408 break;
2409 }
2410
2411 H5Gclose (data_hid);
2412 }
2413
2414 H5Sclose (space_hid);
2415 H5Tclose (type_hid);
2416 H5Gclose (group_hid);
2417
2418 return retval;
2419
2420#else
2421
2422 octave_unused_parameter (loc_id);
2423 octave_unused_parameter (name);
2424 octave_unused_parameter (save_as_floats);
2425
2426 warn_save ("hdf5");
2427
2428 return false;
2429
2430#endif
2431 }
2432
2434 octave_hdf5_id& space_hid,
2435 octave_hdf5_id& type_hid)
2436 {
2437#if defined (HAVE_HDF5)
2438
2439 bool success = true;
2440
2441#if defined (HAVE_HDF5_18)
2442 octave_hdf5_id data_hid = H5Dopen (group_hid, "fcn", octave_H5P_DEFAULT);
2443#else
2444 octave_hdf5_id data_hid = H5Dopen (group_hid, "fcn");
2445#endif
2446
2447 if (data_hid < 0)
2448 {
2449 H5Sclose (space_hid);
2450 H5Tclose (type_hid);
2451 H5Gclose (group_hid);
2452 return false;
2453 }
2454
2455 H5Tclose (type_hid);
2456 type_hid = H5Dget_type (data_hid);
2457 octave_hdf5_id type_class_hid = H5Tget_class (type_hid);
2458
2459 if (type_class_hid != H5T_STRING)
2460 {
2461 H5Sclose (space_hid);
2462 H5Tclose (type_hid);
2463 H5Dclose (data_hid);
2464 H5Gclose (group_hid);
2465 return false;
2466 }
2467
2468 H5Sclose (space_hid);
2469 space_hid = H5Dget_space (data_hid);
2470 hsize_t rank = H5Sget_simple_extent_ndims (space_hid);
2471
2472 if (rank != 0)
2473 {
2474 H5Sclose (space_hid);
2475 H5Tclose (type_hid);
2476 H5Dclose (data_hid);
2477 H5Gclose (group_hid);
2478 return false;
2479 }
2480
2481 int slen = H5Tget_size (type_hid);
2482 if (slen < 0)
2483 {
2484 H5Sclose (space_hid);
2485 H5Tclose (type_hid);
2486 H5Dclose (data_hid);
2487 H5Gclose (group_hid);
2488 return false;
2489 }
2490
2491 OCTAVE_LOCAL_BUFFER (char, fcn_tmp, slen);
2492
2493 // create datatype for (null-terminated) string to read into:
2494 octave_hdf5_id st_id = H5Tcopy (H5T_C_S1);
2495 H5Tset_size (st_id, slen);
2496
2497 if (H5Dread (data_hid, st_id, octave_H5S_ALL, octave_H5S_ALL,
2498 octave_H5P_DEFAULT, fcn_tmp)
2499 < 0)
2500 {
2501 H5Tclose (st_id);
2502 H5Sclose (space_hid);
2503 H5Tclose (type_hid);
2504 H5Dclose (data_hid);
2505 H5Gclose (group_hid);
2506 return false;
2507 }
2508 H5Tclose (st_id);
2509 H5Dclose (data_hid);
2510
2511 octave_idx_type len = 0;
2512
2513 // we have to pull some shenanigans here to make sure
2514 // HDF5 doesn't print out all sorts of error messages if we
2515 // call H5Aopen for a non-existing attribute
2516
2517 H5E_auto_t err_func;
2518 void *err_func_data;
2519
2520 // turn off error reporting temporarily, but save the error
2521 // reporting function:
2522#if defined (HAVE_HDF5_18)
2523 H5Eget_auto (octave_H5E_DEFAULT, &err_func, &err_func_data);
2524 H5Eset_auto (octave_H5E_DEFAULT, nullptr, nullptr);
2525#else
2526 H5Eget_auto (&err_func, &err_func_data);
2527 H5Eset_auto (nullptr, nullptr);
2528#endif
2529
2530 octave_hdf5_id attr_id = H5Aopen_name (group_hid, "SYMBOL_TABLE");
2531
2532 if (attr_id >= 0)
2533 {
2534 if (H5Aread (attr_id, H5T_NATIVE_IDX, &len) < 0)
2535 success = false;
2536
2537 H5Aclose (attr_id);
2538 }
2539
2540 // restore error reporting:
2541#if defined (HAVE_HDF5_18)
2542 H5Eset_auto (octave_H5E_DEFAULT, err_func, err_func_data);
2543#else
2544 H5Eset_auto (err_func, err_func_data);
2545#endif
2546
2547 // Set up temporary scope to use for evaluating the text that
2548 // defines the anonymous function.
2549
2550 interpreter& interp
2551 = __get_interpreter__ ("base_anonymous_fcn_handle::load_hdf5");
2552
2553 tree_evaluator& tw = interp.get_evaluator ();
2554
2555 tw.push_dummy_scope (fcn_tmp);
2556 unwind_action_safe restore_scope (&tree_evaluator::pop_scope, &tw);
2557
2558 if (len > 0 && success)
2559 {
2560 hsize_t num_obj = 0;
2561#if defined (HAVE_HDF5_18)
2562 data_hid = H5Gopen (group_hid, "symbol table", octave_H5P_DEFAULT);
2563#else
2564 data_hid = H5Gopen (group_hid, "symbol table");
2565#endif
2566 H5Gget_num_objs (data_hid, &num_obj);
2567 H5Gclose (data_hid);
2568
2569 if (num_obj != static_cast<hsize_t> (len))
2570 error ("load: failed to load anonymous function handle");
2571
2572 hdf5_callback_data dsub;
2573 int current_item = 0;
2574 for (octave_idx_type i = 0; i < len; i++)
2575 {
2576 if (hdf5_h5g_iterate (group_hid, "symbol table", &current_item,
2577 &dsub) <= 0)
2578 error ("load: failed to load anonymous function handle");
2579
2580 m_local_vars[dsub.name] = dsub.tc;
2581 }
2582 }
2583
2584 if (success)
2585 return parse (fcn_tmp);
2586
2587 return false;
2588
2589#else
2590
2591 octave_unused_parameter (group_hid);
2592 octave_unused_parameter (space_hid);
2593 octave_unused_parameter (type_hid);
2594
2595 return false;
2596
2597#endif
2598 }
2599
2600 void base_anonymous_fcn_handle::print_raw (std::ostream& os, bool, int) const
2601 {
2602 tree_print_code tpc (os);
2603
2605
2606 if (! f)
2607 error ("invalid anonymous function handle");
2608
2609 os << "@";
2610
2611 // The parameter list should always be valid for anonymous
2612 // functions, so we should always call accept for it, and it will
2613 // print the parens for us.
2614
2615 tree_parameter_list *p = f->parameter_list ();
2616
2617 if (p)
2618 p->accept (tpc);
2619
2620 os << " ";
2621
2622 tree_statement_list *b = f->body ();
2623
2624 assert (b->length () == 1);
2625
2626 tree_statement *s = b->front ();
2627
2628 if (! s)
2629 error ("invalid anonymous function handle");
2630
2631 assert (s->is_expression ());
2632
2633 tree_expression *e = s->expression ();
2634
2635 if (! e)
2636 error ("invalid anonymous function handle");
2637
2638 tpc.print_fcn_handle_body (e);
2639 }
2640
2641 bool base_anonymous_fcn_handle::parse (const std::string& fcn_text)
2642 {
2643 // FIXME: If evaluation of the string gives us an anonymous function
2644 // handle object, then why extract the function and create a new
2645 // anonymous function object? Why not just attach the workspace
2646 // values to the object returned by eval_string? This code is also is
2647 // duplicated in read_mat5_binary_element in ls-mat5.cc.
2648
2649 interpreter& interp
2650 = __get_interpreter__ ("base_anonymous_fcn_handle::parse");
2651
2652 // Set up temporary scope to use for evaluating the text that defines
2653 // the anonymous function so that we don't pick up values of random
2654 // variables that might be in the current scope.
2655
2656 tree_evaluator& tw = interp.get_evaluator ();
2657 tw.push_dummy_scope ("read_mat5_binary_element");
2658
2659 unwind_action act ([&tw] () { tw.pop_scope (); });
2660
2661 int parse_status;
2662 octave_value anonymous_fcn_hdl
2663 = interp.eval_string (fcn_text, true, parse_status);
2664
2665 if (parse_status != 0)
2666 return false;
2667
2668 octave_fcn_handle *fh = anonymous_fcn_hdl.fcn_handle_value ();
2669
2670 if (! fh)
2671 return false;
2672
2673 m_fcn = fh->fcn_val ();
2674
2676
2677 if (uf)
2678 {
2679 symbol_scope uf_scope = uf->scope ();
2680
2681 if (uf_scope)
2682 uf_scope.cache_name (m_name);
2683 }
2684
2685 return true;
2686 }
2687
2689 const stack_frame::local_vars_map& local_vars,
2690 const std::shared_ptr<stack_frame>& stack_context)
2691 : base_anonymous_fcn_handle (fcn, local_vars),
2692 m_stack_context (stack_context)
2693 { }
2694
2696 {
2698 (new weak_anonymous_fcn_handle (*this)));
2699 }
2700
2703 {
2704 tree_evaluator& tw = __get_evaluator__ ("anonymous_fcn_handle::call");
2705
2707
2709
2710 unwind_action act ([&tw] () { tw.pop_stack_frame (); });
2711
2712 return oct_usr_fcn->execute (tw, nargout, args);
2713 }
2714
2716 {
2717 octave_scalar_map local_vars_map;
2718
2719 for (const auto& nm_val : m_local_vars)
2720 local_vars_map.assign (nm_val.first, nm_val.second);
2721
2722 // FIXME: it would be more convenient if stack_frame::workspace
2723 // returned a Cell object directly instead of a Cell in an
2724 // octave_value object.
2725
2726 Cell cell_frames;
2727
2728 if (m_stack_context)
2729 {
2730 octave_value ov_frames = m_stack_context->workspace ();
2731 cell_frames = ov_frames.cell_value ();
2732 }
2733
2734 octave_idx_type num_frames = cell_frames.numel ();
2735 // FIXME: It seems there should be a simple way to concatenate cells...
2736 Cell retval = Cell (num_frames+1, 1);
2737 retval(0) = m_local_vars;
2738 for (octave_idx_type i = 0; i < num_frames; i++)
2739 retval(i+1) = cell_frames(i);
2740
2741 return retval;
2742 }
2743
2745 const anonymous_fcn_handle& fh2)
2746 {
2747 if (fh1.m_fcn.is_defined () && fh2.m_fcn.is_defined ())
2748 return fh1.m_fcn.is_copy_of (fh2.m_fcn);
2749 else
2750 return false;
2751 }
2752
2755 {
2756 tree_evaluator& tw = __get_evaluator__ ("anonymous_fcn_handle::call");
2757
2759
2760 std::shared_ptr<stack_frame> frames = m_stack_context.lock ();
2761
2762 tw.push_stack_frame (oct_usr_fcn, m_local_vars, frames);
2763
2764 unwind_action act ([&tw] () { tw.pop_stack_frame (); });
2765
2766 return oct_usr_fcn->execute (tw, nargout, args);
2767 }
2768
2770 {
2771 octave_scalar_map local_vars_map;
2772
2773 for (const auto& nm_val : m_local_vars)
2774 local_vars_map.assign (nm_val.first, nm_val.second);
2775
2776 // FIXME: it would be more convenient if stack_frame::workspace
2777 // returned a Cell object directly instead of a Cell in an
2778 // octave_value object.
2779
2780 std::shared_ptr<stack_frame> frames = m_stack_context.lock ();
2781
2782 Cell cell_frames;
2783
2784 if (frames)
2785 {
2786 octave_value ov_frames = frames->workspace ();
2787 cell_frames = ov_frames.cell_value ();
2788 }
2789
2790 octave_idx_type num_frames = cell_frames.numel ();
2791
2792 // FIXME: It seems there should be a simple way to concatenate
2793 // cells...
2794 Cell retval = Cell (num_frames+1, 1);
2795 retval(0) = m_local_vars;
2796 for (octave_idx_type i = 0; i < num_frames; i++)
2797 retval(i+1) = cell_frames(i);
2798
2799 return retval;
2800 }
2801
2803 const weak_anonymous_fcn_handle& fh2)
2804 {
2805 if (fh1.m_name == fh2.m_name
2806 && fh1.m_fcn.is_defined () && fh2.m_fcn.is_defined ())
2807 return fh1.m_fcn.is_copy_of (fh2.m_fcn);
2808 else
2809 return false;
2810 }
2811
2812OCTAVE_NAMESPACE_END
2813
2815 : octave_base_value (), m_rep (new octave::invalid_fcn_handle ())
2816{ }
2817
2819 : octave_base_value (), m_rep (new octave::internal_fcn_handle (fcn))
2820{ }
2821
2823 : octave_base_value (), m_rep (new octave::simple_fcn_handle (name))
2824{ }
2825
2827 const std::string& name)
2828 : octave_base_value (), m_rep (new octave::simple_fcn_handle (fcn, name))
2829{ }
2830
2831octave_fcn_handle::octave_fcn_handle (const std::string& class_nm,
2832 const std::string& meth_nm)
2833 : octave_base_value (),
2834 m_rep (new octave::class_simple_fcn_handle (class_nm, meth_nm))
2835{ }
2836
2838 const std::string& class_nm,
2839 const std::string& meth_nm)
2840 : octave_base_value (),
2841 m_rep (new octave::class_simple_fcn_handle (fcn, class_nm, meth_nm))
2842{ }
2843
2845 const octave_value& fcn,
2846 const std::string& class_nm,
2847 const std::string& meth_nm)
2848 : octave_base_value (),
2849 m_rep (new octave::class_simple_fcn_handle (obj, fcn, class_nm, meth_nm))
2850{ }
2851
2853 const std::string& name,
2854 const std::list<std::string>& parentage)
2855 : octave_base_value (),
2856 m_rep (new octave::scoped_fcn_handle (fcn, name, parentage))
2857{ }
2858
2860 const std::string& name,
2861 const std::shared_ptr<octave::stack_frame>& stack_context)
2862 : octave_base_value (),
2863 m_rep (new octave::nested_fcn_handle (fcn, name, stack_context))
2864{ }
2865
2867 const octave::stack_frame::local_vars_map& local_vars,
2868 const std::shared_ptr<octave::stack_frame>& stack_context)
2869 : octave_base_value (),
2870 m_rep (new octave::anonymous_fcn_handle (fcn, local_vars, stack_context))
2871{ }
2872
2873octave_fcn_handle::octave_fcn_handle (octave::base_fcn_handle *rep)
2874 : octave_base_value (), m_rep (rep)
2875{ }
2876
2878 : octave_base_value (fh)
2879{
2880 m_rep.reset (fh.m_rep->clone ());
2881}
2882
2885{
2886 static dim_vector dv (1, 1);
2887 return dv;
2888}
2889
2890bool
2892{
2893 return m_rep->save_ascii (os);
2894}
2895
2896bool
2898{
2899 std::shared_ptr<octave::base_fcn_handle> new_rep;
2900
2901 // Read enough to detect type then create new rep object and dispatch
2902 // to finish loading object.
2903
2904 std::streampos pos = is.tellg ();
2905
2906 std::string octaveroot = extract_keyword (is, "octaveroot", true);
2907 if (octaveroot.empty ())
2908 {
2909 is.seekg (pos);
2910 is.clear ();
2911 }
2912
2913 pos = is.tellg ();
2914
2915 std::string fpath = extract_keyword (is, "path", true);
2916 if (fpath.empty ())
2917 {
2918 is.seekg (pos);
2919 is.clear ();
2920 }
2921
2922 if (! (octaveroot.empty () || fpath.empty ()))
2923 {
2924 std::size_t len = octaveroot.size ();
2925 if (octaveroot == fpath.substr (0, len))
2926 fpath = octave::config::octave_exec_home () + fpath.substr (len);
2927 }
2928
2929 pos = is.tellg ();
2930
2931 std::string subtype = extract_keyword (is, "subtype", true);
2932 if (subtype.empty ())
2933 {
2934 is.seekg (pos);
2935 is.clear ();
2936
2937 // We have a legacy file that can contain either an anonymous
2938 // function or a simple function handle.
2939
2940 std::string name;
2941 is >> name;
2942
2943 if (name == anonymous)
2944 new_rep.reset (new octave::anonymous_fcn_handle ());
2945 else
2946 new_rep.reset (new octave::simple_fcn_handle (name, fpath, octaveroot));
2947 }
2948 else
2949 {
2950 // Load individual function handle types.
2951
2952 if (subtype == "simple")
2953 {
2954 std::string name;
2955 is >> name;
2956
2957 new_rep.reset (new octave::simple_fcn_handle (name, fpath, octaveroot));
2958 }
2959 else if (subtype == "scopedfunction")
2960 {
2961 std::string name;
2962 is >> name;
2963
2964 new_rep.reset (new octave::scoped_fcn_handle (name, fpath, octaveroot));
2965 }
2966 else if (subtype == "anonymous")
2967 new_rep.reset (new octave::anonymous_fcn_handle ());
2968 else if (subtype == "nested")
2969 {
2970 std::string name;
2971 is >> name;
2972
2973 new_rep.reset (new octave::nested_fcn_handle (name, fpath, octaveroot));
2974 }
2975 else if (subtype == "classsimple")
2976 {
2977 std::string name;
2978 is >> name;
2979
2980 new_rep.reset (new octave::class_simple_fcn_handle (name, fpath, octaveroot));
2981 }
2982 }
2983
2984 if (! new_rep)
2985 return false;
2986
2987 if (! new_rep->load_ascii (is))
2988 return false;
2989
2990 m_rep = new_rep;
2991
2992 return true;
2993}
2994
2995bool
2996octave_fcn_handle::save_binary (std::ostream& os, bool save_as_floats)
2997{
2998 return m_rep->save_binary (os, save_as_floats);
2999}
3000
3001bool
3002octave_fcn_handle::load_binary (std::istream& is, bool swap,
3004{
3005 // Read enough to detect type then create new rep object and dispatch
3006 // to finish loading object.
3007
3008 int32_t tmp;
3009 if (! is.read (reinterpret_cast<char *> (&tmp), 4))
3010 return false;
3011 if (swap)
3012 swap_bytes<4> (&tmp);
3013
3014 OCTAVE_LOCAL_BUFFER (char, ctmp1, tmp+1);
3015 // is.get (ctmp1, tmp+1, 0); caused is.eof () to be true though
3016 // effectively not reading over file end
3017 is.read (ctmp1, tmp);
3018 ctmp1[tmp] = 0;
3019 std::string name (ctmp1);
3020
3021 if (! is)
3022 return false;
3023
3024 std::shared_ptr<octave::base_fcn_handle> new_rep;
3025
3026 std::size_t anl = anonymous.length ();
3027
3028 if (name.length () >= anl && name.substr (0, anl) == anonymous)
3029 {
3030 // Even with extra info stored in the function name, anonymous
3031 // functions look the same. Note that NAME here may have the
3032 // number of local variables appended. We decode that inside the
3033 // load_binary function.
3034
3035 new_rep.reset (new octave::anonymous_fcn_handle (name));
3036 }
3037 else
3038 {
3039 // Unpack extra info stored with the function name and load
3040 // individual function handle types.
3041 // FIXME: is there a better way?
3042
3043 std::string octaveroot;
3044 std::string fpath;
3045 std::string subtype = "simple";
3046
3047 if (name.find_first_of ('\n') != std::string::npos)
3048 {
3049 std::size_t pos1 = name.find_first_of ('\n');
3050 std::size_t pos2 = name.find_first_of ('\n', pos1 + 1);
3051 octaveroot = name.substr (pos1 + 1, pos2 - pos1 - 1);
3052 fpath = name.substr (pos2 + 1);
3053 name = name.substr (0, pos1);
3054 }
3055
3056 std::size_t pos1 = name.find ('@');
3057 if (pos1 != std::string::npos)
3058 {
3059 if (name[pos1+1] == '<')
3060 {
3061 std::size_t pos2 = name.find ('>', pos1 + 2);
3062
3063 if (pos2 != std::string::npos)
3064 subtype = name.substr (pos1 + 2, pos2 - pos1 - 2);
3065 }
3066
3067 name = name.substr (0, pos1);
3068 }
3069
3070 // Anonymous should have been handled above so it is not in the
3071 // following list.
3072
3073 if (subtype == "simple")
3074 new_rep.reset (new octave::simple_fcn_handle (name, fpath, octaveroot));
3075 else if (subtype == "scopedfunction")
3076 new_rep.reset (new octave::scoped_fcn_handle (name, fpath, octaveroot));
3077 else if (subtype == "nested")
3078 new_rep.reset (new octave::nested_fcn_handle (name, fpath, octaveroot));
3079 else if (subtype == "classsimple")
3080 new_rep.reset (new octave::class_simple_fcn_handle (name, fpath, octaveroot));
3081 }
3082
3083 if (! new_rep)
3084 return false;
3085
3086 if (! new_rep->load_binary (is, swap, fmt))
3087 return false;
3088
3089 m_rep = new_rep;
3090
3091 return true;
3092}
3093
3094bool
3096 bool save_as_floats)
3097{
3098 return m_rep->save_hdf5 (loc_id, name, save_as_floats);
3099}
3100
3101bool
3102octave_fcn_handle::load_hdf5 (octave_hdf5_id loc_id, const char *name_arg)
3103{
3104#if defined (HAVE_HDF5)
3105
3106#if defined (HAVE_HDF5_18)
3107 octave_hdf5_id group_hid = H5Gopen (loc_id, name_arg, octave_H5P_DEFAULT);
3108#else
3109 octave_hdf5_id group_hid = H5Gopen (loc_id, name_arg);
3110#endif
3111 if (group_hid < 0)
3112 return false;
3113
3114#if defined (HAVE_HDF5_18)
3115 octave_hdf5_id data_hid = H5Dopen (group_hid, "nm", octave_H5P_DEFAULT);
3116#else
3117 octave_hdf5_id data_hid = H5Dopen (group_hid, "nm");
3118#endif
3119
3120 if (data_hid < 0)
3121 {
3122 H5Gclose (group_hid);
3123 return false;
3124 }
3125
3126 octave_hdf5_id type_hid = H5Dget_type (data_hid);
3127 octave_hdf5_id type_class_hid = H5Tget_class (type_hid);
3128
3129 if (type_class_hid != H5T_STRING)
3130 {
3131 H5Tclose (type_hid);
3132 H5Dclose (data_hid);
3133 H5Gclose (group_hid);
3134 return false;
3135 }
3136
3137 octave_hdf5_id space_hid = H5Dget_space (data_hid);
3138 hsize_t rank = H5Sget_simple_extent_ndims (space_hid);
3139
3140 if (rank != 0)
3141 {
3142 H5Sclose (space_hid);
3143 H5Tclose (type_hid);
3144 H5Dclose (data_hid);
3145 H5Gclose (group_hid);
3146 return false;
3147 }
3148
3149 int slen = H5Tget_size (type_hid);
3150 if (slen < 0)
3151 {
3152 H5Sclose (space_hid);
3153 H5Tclose (type_hid);
3154 H5Dclose (data_hid);
3155 H5Gclose (group_hid);
3156 return false;
3157 }
3158
3159 OCTAVE_LOCAL_BUFFER (char, nm_tmp, slen);
3160
3161 // create datatype for (null-terminated) string to read into:
3162 octave_hdf5_id st_id = H5Tcopy (H5T_C_S1);
3163 H5Tset_size (st_id, slen);
3164
3165 if (H5Dread (data_hid, st_id, octave_H5S_ALL, octave_H5S_ALL,
3166 octave_H5P_DEFAULT, nm_tmp)
3167 < 0)
3168 {
3169 H5Tclose (st_id);
3170 H5Sclose (space_hid);
3171 H5Tclose (type_hid);
3172 H5Dclose (data_hid);
3173 H5Gclose (group_hid);
3174 return false;
3175 }
3176 H5Tclose (st_id);
3177 H5Dclose (data_hid);
3178
3179 std::string name (nm_tmp);
3180
3181 std::shared_ptr<octave::base_fcn_handle> new_rep;
3182
3183 if (name == anonymous)
3184 {
3185 // Even with extra info stored in the function name, anonymous
3186 // functions look the same.
3187
3188 new_rep.reset (new octave::anonymous_fcn_handle ());
3189 }
3190 else
3191 {
3192 // Unpack extra info stored with the function name and load
3193 // individual function handle types.
3194 // FIXME: is there a better way?
3195
3196 std::string octaveroot;
3197 std::string fpath;
3198 std::string subtype = "simple";
3199
3200 if (name.find_first_of ('\n') != std::string::npos)
3201 {
3202 std::size_t pos1 = name.find_first_of ('\n');
3203 std::size_t pos2 = name.find_first_of ('\n', pos1 + 1);
3204 octaveroot = name.substr (pos1 + 1, pos2 - pos1 - 1);
3205 fpath = name.substr (pos2 + 1);
3206 name = name.substr (0, pos1);
3207 }
3208
3209 std::size_t pos1 = name.find ('@');
3210 if (pos1 != std::string::npos)
3211 {
3212 if (name[pos1+1] == '<')
3213 {
3214 std::size_t pos2 = name.find ('>', pos1 + 2);
3215
3216 if (pos2 != std::string::npos)
3217 subtype = name.substr (pos1 + 2, pos2 - pos1 - 2);
3218 }
3219
3220 name = name.substr (0, pos1);
3221 }
3222
3223 // Anonymous should have been handled above so it is not in the
3224 // following list.
3225
3226 if (subtype == "simple")
3227 new_rep.reset (new octave::simple_fcn_handle (name, fpath, octaveroot));
3228 else if (subtype == "scopedfunction")
3229 new_rep.reset (new octave::scoped_fcn_handle (name, fpath, octaveroot));
3230 else if (subtype == "nested")
3231 new_rep.reset (new octave::nested_fcn_handle (name, fpath, octaveroot));
3232 else if (subtype == "classsimple")
3233 new_rep.reset (new octave::class_simple_fcn_handle (name, fpath, octaveroot));
3234 }
3235
3236 bool status = false;
3237
3238 if (new_rep && new_rep->load_hdf5 (group_hid, space_hid, type_hid))
3239 {
3240 m_rep = new_rep;
3241 status = true;
3242 }
3243
3244 // FIXME: manage these with an unwind_action object?
3245
3246 H5Tclose (type_hid);
3247 H5Sclose (space_hid);
3248 H5Gclose (group_hid);
3249
3250 return status;
3251
3252#else
3253
3254 octave_unused_parameter (loc_id);
3255 octave_unused_parameter (name_arg);
3256
3257 warn_load ("hdf5");
3258
3259 return false;
3260
3261#endif
3262}
3263
3264/*
3265%!test <*33857>
3266%! a = 2;
3267%! f = @(x) a + x;
3268%! g = @(x) 2 * x;
3269%! hm = @version;
3270%! hdld = @svd;
3271%! hbi = @log2;
3272%! f2 = f;
3273%! g2 = g;
3274%! hm2 = hm;
3275%! hdld2 = hdld;
3276%! hbi2 = hbi;
3277%! modes = {"-text", "-binary"};
3278%! if (isfield (__octave_config_info__, "HAVE_HDF5")
3279%! && __octave_config_info__ ("HAVE_HDF5"))
3280%! modes(end+1) = "-hdf5";
3281%! endif
3282%! for i = 1:numel (modes)
3283%! mode = modes{i};
3284%! nm = tempname ();
3285%! unwind_protect
3286%! f2 (1);
3287%! save (mode, nm, "f2", "g2", "hm2", "hdld2", "hbi2");
3288%! clear f2 g2 hm2 hdld2 hbi2
3289%! load (nm);
3290%! assert (f (2), f2 (2));
3291%! assert (g (2), g2 (2));
3292%! assert (g (3), g2 (3));
3293%! unlink (nm);
3294%! save (mode, nm, "f2", "g2", "hm2", "hdld2", "hbi2");
3295%! unwind_protect_cleanup
3296%! unlink (nm);
3297%! end_unwind_protect
3298%! endfor
3299*/
3300
3301/*
3302%!function fcn_handle_save_recurse (n, mode, nm, f2, g2, hm2, hdld2, hbi2)
3303%! if (n == 0)
3304%! save (mode, nm, "f2", "g2", "hm2", "hdld2", "hbi2");
3305%! else
3306%! fcn_handle_save_recurse (n - 1, mode, nm, f2, g2, hm2, hdld2, hbi2);
3307%! endif
3308%!endfunction
3309%!function [f2, g2, hm2, hdld2, hbi2] = fcn_handle_load_recurse (n, nm)
3310%! if (n == 0)
3311%! load (nm);
3312%! else
3313%! [f2, g2, hm2, hdld2, hbi2] = fcn_handle_load_recurse (n - 1, nm);
3314%! endif
3315%!endfunction
3316
3317%!test <*35876>
3318%! a = 2;
3319%! f = @(x) a + x;
3320%! g = @(x) 2 * x;
3321%! hm = @version;
3322%! hdld = @svd;
3323%! hbi = @log2;
3324%! f2 = f;
3325%! g2 = g;
3326%! hm2 = hm;
3327%! hdld2 = hdld;
3328%! hbi2 = hbi;
3329%! modes = {"-text", "-binary"};
3330%! if (isfield (__octave_config_info__, "HAVE_HDF5")
3331%! && __octave_config_info__ ("HAVE_HDF5"))
3332%! modes(end+1) = "-hdf5";
3333%! endif
3334%! for i = 1:numel (modes)
3335%! mode = modes{i};
3336%! nm = tempname ();
3337%! unwind_protect
3338%! fcn_handle_save_recurse (2, mode, nm, f2, g2, hm2, hdld2, hbi2);
3339%! clear f2 g2 hm2 hdld2 hbi2
3340%! [f2, f2, hm2, hdld2, hbi2] = fcn_handle_load_recurse (2, nm);
3341%! load (nm);
3342%! assert (f (2), f2 (2));
3343%! assert (g (2), g2 (2));
3344%! assert (g (3), g2 (3));
3345%! unlink (nm);
3346%! fcn_handle_save_recurse (2, mode, nm, f2, g2, hm2, hdld2, hbi2);
3347%! unwind_protect_cleanup
3348%! unlink (nm);
3349%! end_unwind_protect
3350%! endfor
3351*/
3352
3353void
3354octave_fcn_handle::print (std::ostream& os, bool pr_as_read_syntax)
3355{
3356 print_raw (os, pr_as_read_syntax);
3357 newline (os);
3358}
3359
3360void
3361octave_fcn_handle::print_raw (std::ostream& os, bool pr_as_read_syntax) const
3362{
3363 m_rep->print_raw (os, pr_as_read_syntax, current_print_indent_level ());
3364}
3365
3366bool
3368{
3369 // FIXME: Maybe there is a better way? Possibly by using typeid or
3370 // typeindex?
3371
3372 // Don't include invalid_fcn_handle in the list of types to compare.
3373 // Consider them to be like NaN values so comparisons between any two
3374 // invalid handles are always false.
3375
3376 if (fh1.is_internal () && fh2.is_internal ())
3377 return is_equal_to (*dynamic_cast<octave::internal_fcn_handle *> (fh1.get_rep ()),
3378 *dynamic_cast<octave::internal_fcn_handle *> (fh2.get_rep ()));
3379 else if (fh1.is_simple () && fh2.is_simple ())
3380 return is_equal_to (*dynamic_cast<octave::simple_fcn_handle *> (fh1.get_rep ()),
3381 *dynamic_cast<octave::simple_fcn_handle *> (fh2.get_rep ()));
3382 else if (fh1.is_scoped () && fh2.is_scoped ())
3383 return is_equal_to (*dynamic_cast<octave::scoped_fcn_handle *> (fh1.get_rep ()),
3384 *dynamic_cast<octave::scoped_fcn_handle *> (fh2.get_rep ()));
3385 else if (fh1.is_nested () && fh2.is_nested ())
3386 return is_equal_to (*dynamic_cast<octave::nested_fcn_handle *> (fh1.get_rep ()),
3387 *dynamic_cast<octave::nested_fcn_handle *> (fh2.get_rep ()));
3388 else if (fh1.is_class_simple () && fh2.is_class_simple ())
3389 return is_equal_to (*dynamic_cast<octave::class_simple_fcn_handle *> (fh1.get_rep ()),
3390 *dynamic_cast<octave::class_simple_fcn_handle *> (fh2.get_rep ()));
3391 else if (fh1.is_anonymous () && fh2.is_anonymous ())
3392 return is_equal_to (*dynamic_cast<octave::anonymous_fcn_handle *> (fh1.get_rep ()),
3393 *dynamic_cast<octave::anonymous_fcn_handle *> (fh2.get_rep ()));
3394 else
3395 return false;
3396}
3397
3398OCTAVE_NAMESPACE_BEGIN
3399
3400 // DEPRECATED in Octave 6.
3401
3403 make_fcn_handle (interpreter& interp, const std::string& nm)
3404 {
3405 tree_evaluator& tw = interp.get_evaluator ();
3406
3407 return tw.make_fcn_handle (nm);
3408 }
3409
3410DEFUN (functions, args, ,
3411 doc: /* -*- texinfo -*-
3412@deftypefn {} {@var{s} =} functions (@var{fcn_handle})
3413Return a structure containing information about the function handle
3414@var{fcn_handle}.
3415
3416The structure @var{s} always contains these three fields:
3417
3418@table @asis
3419@item function
3420The function name. For an anonymous function (no name) this will be the
3421actual function definition.
3422
3423@item type
3424Type of the function.
3425
3426@table @asis
3427@item anonymous
3428The function is anonymous.
3429
3430@item private
3431The function is private.
3432
3433@item overloaded
3434The function overloads an existing function.
3435
3436@item simple
3437The function is a built-in or m-file function.
3438
3439@item subfunction
3440The function is a subfunction within an m-file.
3441@end table
3442
3443@item nested
3444The function is nested.
3445
3446@item file
3447The m-file that will be called to perform the function. This field is empty
3448for anonymous and built-in functions.
3449@end table
3450
3451In addition, some function types may return more information in additional
3452fields.
3453
3454@strong{Warning:} @code{functions} is provided for debugging purposes only.
3455Its behavior may change in the future and programs should not depend on any
3456particular output format.
3457
3458@seealso{func2str, str2func}
3459@end deftypefn */)
3460{
3461 if (args.length () != 1)
3462 print_usage ();
3463
3464 octave_fcn_handle *fh = args(0).xfcn_handle_value ("functions: FCN_HANDLE argument must be a function handle object");
3465
3466 return ovl (fh->info ());
3467}
3468
3469DEFUN (func2str, args, ,
3470 doc: /* -*- texinfo -*-
3471@deftypefn {} {} func2str (@var{fcn_handle})
3472Return a string containing the name of the function referenced by the
3473function handle @var{fcn_handle}.
3474@seealso{str2func, functions}
3475@end deftypefn */)
3476{
3477 if (args.length () != 1)
3478 print_usage ();
3479
3480 octave_fcn_handle *fh = args(0).xfcn_handle_value ("func2str: FCN_HANDLE argument must be a function handle object");
3481
3482 if (! fh)
3483 error ("func2str: FCN_HANDLE must be a valid function handle");
3484
3485 octave_value retval;
3486
3487 std::string fh_nm = fh->fcn_name ();
3488
3489 if (fh->is_anonymous ())
3490 {
3491 std::ostringstream buf;
3492
3493 fh->print_raw (buf);
3494
3495 retval = buf.str ();
3496 }
3497 else
3498 retval = fh_nm;
3499
3500 return retval;
3501}
3502
3503DEFMETHOD (str2func, interp, args, ,
3504 doc: /* -*- texinfo -*-
3505@deftypefn {} {} str2func (@var{fcn_name})
3506Return a function handle constructed from the string @var{fcn_name}.
3507
3508Previous versions of Octave accepted an optional second argument,
3509@qcode{"global"}, that caused str2func to ignore locally visible
3510functions. This option is no longer supported.
3511@seealso{func2str, functions}
3512@end deftypefn */)
3513{
3514 int nargin = args.length ();
3515
3516 if (nargin < 1 || nargin > 2)
3517 print_usage ();
3518
3519 std::string nm
3520 = args(0).xstring_value ("str2func: FCN_NAME must be a string");
3521
3522 if (nm.empty ())
3523 error ("str2func: invalid function name");
3524
3525 if (nm[0] == '@')
3526 {
3527 // Unlike the anonymous_fcn_handle::parse method, don't set up
3528 // temporary scope to use for evaluating the text that defines
3529 // the anonymous function. Here we want
3530 //
3531 // str2fun ("@(args) expr")
3532 //
3533 // to behave the same as if
3534 //
3535 // @(args) expr
3536 //
3537 // were evaluated in the current scope.
3538
3539 int parse_status;
3540 octave_value afh = interp.eval_string (nm, true, parse_status);
3541
3542 if (parse_status == 0)
3543 return afh;
3544 }
3545 else
3546 {
3547 if (nargin == 2)
3548 warning_with_id ("Octave:str2func-global-argument",
3549 "str2func: second argument ignored");
3550
3551 tree_evaluator& tw = interp.get_evaluator ();
3552
3553 return tw.make_fcn_handle (nm);
3554 }
3555
3556 return ovl ();
3557}
3558
3559/*
3560%!test
3561%! f = str2func ("<");
3562%! assert (class (f), "function_handle");
3563%! assert (func2str (f), "lt");
3564%! assert (f (1, 2), true);
3565%! assert (f (2, 1), false);
3566
3567%!test
3568%! f = str2func ("@(x) sin (x)");
3569%! assert (func2str (f), "@(x) sin (x)");
3570%! assert (f (0:3), sin (0:3));
3571
3572%!error <FCN_NAME must be a string> str2func ({"sin"})
3573*/
3574
3575/*
3576%!function y = __testrecursionfunc (f, x, n)
3577%! if (nargin < 3)
3578%! n = 0;
3579%! endif
3580%! if (n > 2)
3581%! y = f (x);
3582%! else
3583%! n++;
3584%! y = __testrecursionfunc (@(x) f (2*x), x, n);
3585%! endif
3586%!endfunction
3587%!
3588%!assert (__testrecursionfunc (@(x) x, 1), 8)
3589*/
3590
3591DEFUN (is_function_handle, args, ,
3592 doc: /* -*- texinfo -*-
3593@deftypefn {} {} is_function_handle (@var{x})
3594Return true if @var{x} is a function handle.
3595@seealso{isa, typeinfo, class, functions}
3596@end deftypefn */)
3597{
3598 if (args.length () != 1)
3599 print_usage ();
3600
3601 return ovl (args(0).is_function_handle ());
3602}
3603
3604/*
3605%!shared fh
3606%! fh = @(x) x;
3607
3608%!assert (is_function_handle (fh))
3609%!assert (! is_function_handle ({fh}))
3610%!assert (! is_function_handle (1))
3611
3612%!error is_function_handle ()
3613%!error is_function_handle (1, 2)
3614*/
3615
3616/*
3617%!test
3618%! f = @(t) eval ('2*t');
3619%! assert (f (21), 42);
3620*/
3621
3622/*
3623%!test <*58389>
3624%! s = "x";
3625%! a.(s) = [e, pi];
3626%! f = @(x) a.(s)(x);
3627%! assert (f(1), e);
3628%! assert (f(2), pi);
3629%! assert (f([2,1]), [pi, e]);
3630*/
3631
3632/*
3633%!function r = __f (g, i)
3634%! r = g(i);
3635%!endfunction
3636%!test
3637%! x = [1,2;3,4];
3638%! assert (__f (@(i) x(:,i), 1), [1;3]);
3639*/
3640
3641OCTAVE_NAMESPACE_END
void swap_bytes< 4 >(void *ptr)
Definition: byte-swap.h:63
octave_idx_type numel(void) const
Number of elements in the array.
Definition: Array.h:411
Definition: Cell.h:43
octave_value make_weak_anonymous_handle(void) const
anonymous_fcn_handle(const anonymous_fcn_handle &)=default
octave_value workspace(void) const
anonymous_fcn_handle * clone(void) const
friend bool is_equal_to(const anonymous_fcn_handle &fh1, const anonymous_fcn_handle &fh2)
std::shared_ptr< stack_frame > stack_context(void) const
std::shared_ptr< stack_frame > m_stack_context
octave_value_list call(int nargout, const octave_value_list &args)
anonymous_fcn_handle(const std::string &name="")
~anonymous_fcn_handle(void)=default
bool load_hdf5(octave_hdf5_id &group_hid, octave_hdf5_id &space_hid, octave_hdf5_id &type_hid)
octave_user_function * user_function_value(bool=false)
void print_raw(std::ostream &, bool pr_as_read_syntax, int current_print_indent_level) const
bool save_hdf5(octave_hdf5_id loc_id, const char *name, bool save_as_floats)
bool load_ascii(std::istream &is)
bool save_ascii(std::ostream &os)
octave_scalar_map info(void)
std::string type(void) const
bool load_binary(std::istream &is, bool swap, mach_info::float_format fmt)
bool parse(const std::string &fcn_text)
base_anonymous_fcn_handle(const octave_value &fcn, const stack_frame::local_vars_map &local_vars)
virtual octave_value workspace(void) const =0
octave_value fcn_val(void)
bool save_binary(std::ostream &os, bool save_as_floats)
stack_frame::local_vars_map m_local_vars
static const std::string anonymous
base_anonymous_fcn_handle(const std::string &name="")
bool is_anonymous(void) const
bool print_as_scalar(void) const
base_anonymous_fcn_handle(const base_anonymous_fcn_handle &)=default
octave_function * function_value(bool=false)
~base_anonymous_fcn_handle(void)=default
virtual bool save_binary(std::ostream &os, bool save_as_floats)
virtual bool save_hdf5(octave_hdf5_id loc_id, const char *name, bool save_as_floats)
virtual octave_value make_weak_anonymous_handle(void) const
std::string m_name
void unimplemented(const char *op, const char *fmt) const
std::string fcn_name(void) const
Definition: ov-fcn-handle.h:92
virtual bool save_ascii(std::ostream &os)
virtual std::string type(void) const =0
void warn_save(const char *file_type) const
virtual bool load_binary(std::istream &is, bool swap, mach_info::float_format fmt)
virtual octave_value make_weak_nested_handle(void) const
virtual bool load_hdf5(octave_hdf5_id &group_hid, octave_hdf5_id &space_hid, octave_hdf5_id &type_hid)
virtual octave_value_list call(int nargout, const octave_value_list &args)=0
void warn_load(const char *file_type) const
virtual bool is_nested(void) const
Definition: ov-fcn-handle.h:73
virtual bool load_ascii(std::istream &is)
octave_value_list subsref(const std::string &type, const std::list< octave_value_list > &idx, int nargout)
std::string file(void) const
Definition: ov-fcn-handle.h:94
octave_value convert_to_str_internal(bool pad, bool force, char type) const
std::string m_file
virtual void print_raw(std::ostream &, bool, int) const
octave_function * function_value(bool=false)
void print_raw(std::ostream &, bool pr_as_read_syntax, int current_print_indent_level) const
octave_scalar_map info(void)
bool load_binary(std::istream &is, bool swap, mach_info::float_format fmt)
base_nested_fcn_handle(const std::string &name="", const std::string &file="", const std::string &="")
bool save_binary(std::ostream &os, bool save_as_floats)
std::string type(void) const
bool is_nested(void) const
bool save_ascii(std::ostream &os)
bool load_hdf5(octave_hdf5_id &group_hid, octave_hdf5_id &space_hid, octave_hdf5_id &type_hid)
base_nested_fcn_handle(const octave_value &fcn, const std::string &name)
octave_user_function * user_function_value(bool=false)
virtual octave_value workspace(void) const =0
bool load_ascii(std::istream &is)
bool save_hdf5(octave_hdf5_id loc_id, const char *name, bool save_as_floats)
octave_value fcn_val(void)
octave_user_function * user_function_value(bool=false)
octave_value_list call(int nargout, const octave_value_list &args)
std::string type(void) const
friend bool is_equal_to(const class_simple_fcn_handle &fh1, const class_simple_fcn_handle &fh2)
bool is_class_simple(void) const
bool load_binary(std::istream &is, bool swap, mach_info::float_format fmt)
std::string dispatch_class(void) const
octave_function * function_value(bool=false)
~class_simple_fcn_handle(void)=default
class_simple_fcn_handle * clone(void) const
bool save_binary(std::ostream &os, bool save_as_floats)
class_simple_fcn_handle(const std::string &name, const std::string &file, const std::string &)
bool save_hdf5(octave_hdf5_id loc_id, const char *name, bool save_as_floats)
class_simple_fcn_handle(const class_simple_fcn_handle &)=default
void print_raw(std::ostream &, bool pr_as_read_syntax, int current_print_indent_level) const
octave_scalar_map info(void)
bool save_ascii(std::ostream &os)
bool load_hdf5(octave_hdf5_id &group_hid, octave_hdf5_id &space_hid, octave_hdf5_id &type_hid)
octave_value fcn_val(void)
bool load_ascii(std::istream &is)
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:94
octave_value_list call(int nargout, const octave_value_list &args)
internal_fcn_handle * clone(void) const
~internal_fcn_handle(void)=default
internal_fcn_handle(const octave_value &fcn)
octave_user_function * user_function_value(bool=false)
bool is_internal(void) const
internal_fcn_handle(const internal_fcn_handle &)=default
octave_scalar_map info(void)
octave_value fcn_val(void)
friend bool is_equal_to(const internal_fcn_handle &fh1, const internal_fcn_handle &fh2)
octave_function * function_value(bool=false)
std::string type(void) const
octave_value varval(const std::string &name) const
tree_evaluator & get_evaluator(void)
octave_value_list eval_string(const std::string &eval_str, bool silent, int &parse_status, int nargout)
symbol_table & get_symbol_table(void)
Definition: interpreter.h:296
octave_value_list feval(const char *name, const octave_value_list &args=octave_value_list(), int nargout=0)
Evaluate an Octave function (built-in or interpreted) and return the list of result values.
~invalid_fcn_handle(void)=default
octave_value_list call(int nargout, const octave_value_list &args)
std::string type(void) const
invalid_fcn_handle(const invalid_fcn_handle &)=default
invalid_fcn_handle * clone(void) const
friend bool is_equal_to(const nested_fcn_handle &fh1, const nested_fcn_handle &fh2)
nested_fcn_handle(const std::string &name="", const std::string &file="", const std::string &octaveroot="")
nested_fcn_handle(const octave_value &fcn, const std::string &name, const std::shared_ptr< stack_frame > &stack_context)
bool is_nested(const std::shared_ptr< stack_frame > &frame) const
octave_value_list call(int nargout, const octave_value_list &args)
octave_value workspace(void) const
nested_fcn_handle * clone(void) const
std::shared_ptr< stack_frame > stack_context(void) const
std::shared_ptr< stack_frame > m_stack_context
octave_value make_weak_nested_handle(void) const
nested_fcn_handle(const nested_fcn_handle &)=default
~nested_fcn_handle(void)=default
std::map< std::string, octave_value > local_vars_map
Definition: stack-frame.h:112
void cache_name(const std::string &name)
Definition: symscope.h:576
int current_print_indent_level(void) const
Definition: ov-base.h:893
OCTINTERP_API void newline(std::ostream &os) const
Definition: ov-base.cc:1383
OCTINTERP_API void warn_load(const char *type) const
Definition: ov-base.cc:1152
bool load_ascii(std::istream &is)
Definition: ov-cell.cc:798
Array< std::string > cellstr_value(void) const
Definition: ov-cell.cc:636
bool load_binary(std::istream &is, bool swap, octave::mach_info::float_format fmt)
Definition: ov-cell.cc:930
bool iscellstr(void) const
Definition: ov-cell.cc:431
static const std::string anonymous
std::string fcn_name(void) const
void print_raw(std::ostream &os, bool pr_as_read_syntax=false) const
octave_scalar_map info(void)
bool is_simple(void) const
std::shared_ptr< octave::base_fcn_handle > m_rep
bool load_hdf5(octave_hdf5_id loc_id, const char *name)
bool is_internal(void) const
bool load_ascii(std::istream &is)
bool is_class_simple(void) const
octave_value fcn_val(void)
bool is_anonymous(void) const
dim_vector dims(void) const
bool is_scoped(void) const
bool save_binary(std::ostream &os, bool save_as_floats)
bool save_ascii(std::ostream &os)
bool save_hdf5(octave_hdf5_id loc_id, const char *name, bool save_as_floats)
bool is_nested(void) const
octave::base_fcn_handle * get_rep(void) const
bool load_binary(std::istream &is, bool swap, octave::mach_info::float_format fmt)
void print(std::ostream &os, bool pr_as_read_syntax=false)
std::string dispatch_class(void) const
Definition: ov-fcn.h:149
virtual std::string fcn_file_name(void) const
Definition: ov-fcn.h:78
bool is_class_method(const std::string &cname="") const
Definition: ov-fcn.h:116
void setfield(const std::string &key, const octave_value &val)
Definition: oct-map.cc:190
void assign(const std::string &k, const octave_value &val)
Definition: oct-map.h:238
std::string fcn_file_name(void) const
Definition: ov-usr-fcn.h:106
octave::symbol_scope scope(void)
Definition: ov-usr-fcn.h:93
octave_value_list execute(octave::tree_evaluator &tw, int nargout=0, const octave_value_list &args=octave_value_list())
Definition: ov-usr-fcn.cc:492
octave_value find_subfunction(const std::string &subfuns) const
Definition: ov-usr-fcn.cc:418
octave_idx_type length(void) const
Definition: ovl.h:113
octave_value_list & prepend(const octave_value &val)
Definition: ovl.cc:80
bool is_function(void) const
Definition: ov.h:822
bool is_classdef_object(void) const
Definition: ov.h:700
bool is_package(void) const
Definition: ov.h:706
octave_value subsref(const std::string &type, const std::list< octave_value_list > &idx)
Definition: ov.h:525
OCTINTERP_API octave_function * function_value(bool silent=false) const
OCTINTERP_API octave_fcn_handle * fcn_handle_value(bool silent=false) const
OCTINTERP_API octave_user_function * user_function_value(bool silent=false) const
bool is_cs_list(void) const
Definition: ov.h:715
bool is_copy_of(const octave_value &val) const
Definition: ov.h:1482
bool save_binary(std::ostream &os, bool save_as_floats)
Definition: ov.h:1491
bool is_defined(void) const
Definition: ov.h:637
bool save_ascii(std::ostream &os)
Definition: ov.h:1487
Cell cell_value(void) const
bool is_classdef_meta(void) const
Definition: ov.h:697
bool is_undefined(void) const
Definition: ov.h:640
friend bool is_equal_to(const scoped_fcn_handle &fh1, const scoped_fcn_handle &fh2)
void find_function(void)
void print_raw(std::ostream &, bool pr_as_read_syntax, int current_print_indent_level) const
bool save_binary(std::ostream &os, bool save_as_floats)
scoped_fcn_handle(const scoped_fcn_handle &)=default
octave_value m_fcn
~scoped_fcn_handle(void)=default
scoped_fcn_handle * clone(void) const
bool load_ascii(std::istream &is)
octave_value_list call(int nargout, const octave_value_list &args)
bool load_binary(std::istream &is, bool swap, mach_info::float_format fmt)
octave_value fcn_val(void)
std::string type(void) const
octave_function * function_value(bool=false)
bool load_hdf5(octave_hdf5_id &group_hid, octave_hdf5_id &space_hid, octave_hdf5_id &type_hid)
octave_scalar_map info(void)
scoped_fcn_handle(const std::string &name="", const std::string &file="", const std::string &="")
bool save_hdf5(octave_hdf5_id loc_id, const char *name, bool save_as_floats)
bool is_scoped(void) const
octave_user_function * user_function_value(bool=false)
bool save_ascii(std::ostream &os)
std::list< std::string > m_parentage
friend bool is_equal_to(const simple_fcn_handle &fh1, const simple_fcn_handle &fh2)
std::string type(void) const
octave_function * function_value(bool)
bool is_simple(void) const
octave_user_function * user_function_value(bool)
simple_fcn_handle(const std::string &name="", const std::string &file="", const std::string &="")
simple_fcn_handle(const octave_value &fcn, const std::string &name)
void print_raw(std::ostream &os, bool pr_as_read_syntax, int current_print_indent_level) const
simple_fcn_handle * clone(void) const
bool save_binary(std::ostream &os, bool save_as_floats)
bool save_hdf5(octave_hdf5_id loc_hid, const char *name, bool save_as_floats)
octave_value m_fcn
octave_value fcn_val(void)
bool save_ascii(std::ostream &os)
bool load_binary(std::istream &is, bool swap, mach_info::float_format fmt)
bool load_hdf5(octave_hdf5_id &group_hid, octave_hdf5_id &space_hid, octave_hdf5_id &type_hid)
simple_fcn_handle(const simple_fcn_handle &)=default
octave_value_list call(int nargout, const octave_value_list &args)
bool load_ascii(std::istream &is)
octave_scalar_map info(void)
~simple_fcn_handle(void)=default
octave_value find_private_function(const std::string &dir_name, const std::string &name)
Definition: symtab.cc:99
octave_value find_function(const std::string &name, const symbol_scope &search_scope=symbol_scope())
Definition: symtab.cc:249
octave_value find_user_function(const std::string &name)
Definition: symtab.cc:288
void set_dispatch_class(const std::string &class_name)
Definition: pt-eval.cc:2553
void pop_stack_frame(void)
Definition: pt-eval.cc:2471
void push_dummy_scope(const std::string &name)
Definition: pt-eval.cc:2639
void pop_scope(void)
Definition: pt-eval.cc:2646
octave_value make_fcn_handle(const std::string &nm)
Definition: pt-eval.cc:1604
void push_stack_frame(const symbol_scope &scope)
Definition: pt-eval.cc:2443
weak_anonymous_fcn_handle(const anonymous_fcn_handle &afh)
weak_anonymous_fcn_handle * clone(void) const
bool is_weak_anonymous(void) const
std::weak_ptr< stack_frame > m_stack_context
octave_value workspace(void) const
octave_value_list call(int nargout, const octave_value_list &args)
friend bool is_equal_to(const weak_anonymous_fcn_handle &fh1, const weak_anonymous_fcn_handle &fh2)
~weak_anonymous_fcn_handle(void)=default
weak_anonymous_fcn_handle(const weak_anonymous_fcn_handle &)=default
weak_nested_fcn_handle(const nested_fcn_handle &nfh)
std::weak_ptr< stack_frame > m_stack_context
octave_value workspace(void) const
bool is_weak_nested(void) const
friend bool is_equal_to(const weak_nested_fcn_handle &fh1, const weak_nested_fcn_handle &fh2)
weak_nested_fcn_handle(const weak_nested_fcn_handle &)=default
~weak_nested_fcn_handle(void)=default
weak_nested_fcn_handle * clone(void) const
octave_value_list call(int nargout, const octave_value_list &args)
const octave_hdf5_id octave_H5P_DEFAULT
const octave_hdf5_id octave_H5E_DEFAULT
const octave_hdf5_id octave_H5S_ALL
OCTINTERP_API void print_usage(void)
Definition: defun-int.h:72
#define DEFMETHOD(name, interp_name, args_name, nargout_name, doc)
Macro to define a builtin method.
Definition: defun.h:111
#define DEFUN(name, args_name, nargout_name, doc)
Macro to define a builtin function.
Definition: defun.h:56
void warning(const char *fmt,...)
Definition: error.cc:1055
void warning_with_id(const char *id, const char *fmt,...)
Definition: error.cc:1070
void error(const char *fmt,...)
Definition: error.cc:980
#define panic_impossible()
Definition: error.h:411
QString name
F77_RET_T const F77_DBLE const F77_DBLE * f
std::string read_until_newline(std::istream &is, bool keep_newline)
void skip_preceeding_newline(std::istream &is)
octave_hdf5_err hdf5_h5g_iterate(octave_hdf5_id loc_id, const char *name, int *idx, void *operator_data)
Definition: ls-hdf5.cc:1061
bool add_hdf5_data(octave_hdf5_id loc_id, const octave_value &tc, const std::string &name, const std::string &doc, bool mark_global, bool save_as_floats)
Definition: ls-hdf5.cc:1407
std::string read_binary_data(std::istream &is, bool swap, octave::mach_info::float_format fmt, const std::string &filename, bool &global, octave_value &tc, std::string &doc)
bool save_binary_data(std::ostream &os, const octave_value &tc, const std::string &name, const std::string &doc, bool mark_global, bool save_as_floats)
std::string read_text_data(std::istream &is, const std::string &filename, bool &global, octave_value &tc, octave_idx_type count, const bool do_name_validation)
Definition: ls-oct-text.cc:287
std::string extract_keyword(std::istream &is, const char *keyword, const bool next_only)
Definition: ls-oct-text.cc:84
bool save_text_data(std::ostream &os, const octave_value &val_arg, const std::string &name, bool mark_global, int precision)
Definition: ls-oct-text.cc:363
std::string octave_exec_home(void)
Definition: defaults.cc:164
std::string dirname(const std::string &path)
Definition: file-ops.cc:358
interpreter & __get_interpreter__(const std::string &who)
tree_evaluator & __get_evaluator__(const std::string &who)
symbol_table & __get_symbol_table__(const std::string &who)
int64_t octave_hdf5_id
#define H5T_NATIVE_IDX
Definition: oct-hdf5.h:42
#define OCTAVE_LOCAL_BUFFER(T, buf, size)
Definition: oct-locbuf.h:44
return octave_value(v1.char_array_value() . concat(v2.char_array_value(), ra_idx),((a1.is_sq_string()||a2.is_sq_string()) ? '\'' :'"'))
#define DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA(t, n, c)
Definition: ov-base.h:222
static void err_invalid_fcn_handle(const std::string &name)
OCTAVE_NAMESPACE_BEGIN octave_value make_fcn_handle(interpreter &interp, const std::string &nm)
bool is_equal_to(const anonymous_fcn_handle &fh1, const anonymous_fcn_handle &fh2)
octave_value_list ovl(const OV_Args &... args)
Construct an octave_value_list with less typing.
Definition: ovl.h:211
void octave_print_internal(std::ostream &os, const float_display_format &fmt, bool d, bool pr_as_read_syntax)
Definition: pr-output.cc:1762
static std::string dir_sep_chars
Definition: shared-fcns.h:96
octave_value tc
Definition: ls-hdf5.h:115
std::string name
Definition: ls-hdf5.h:109
F77_RET_T len
Definition: xerbla.cc:61