1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 1996-2021 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 "error.h"
31 #include "errwarn.h"
32 #include "interpreter-private.h"
33 #include "interpreter.h"
34 #include "ov-builtin.h"
35 #include "ov.h"
36 #include "ovl.h"
37 #include "profiler.h"
38 #include "unwind-prot.h"
39 
40 
41 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_builtin,
42                                      "built-in function",
43                                      "built-in function");
44 
45 octave_value_list
execute(octave::tree_evaluator & tw,int nargout,const octave_value_list & args)46 octave_builtin::execute (octave::tree_evaluator& tw, int nargout,
47                          const octave_value_list& args)
48 {
49   octave_value_list retval;
50 
51   if (args.has_magic_colon ())
52     error ("invalid use of colon in function argument list");
53 
54   octave::profiler& profiler = tw.get_profiler ();
55 
56   octave::profiler::enter<octave_builtin> block (profiler, *this);
57 
58   if (f)
59     retval = (*f) (args, nargout);
60   else
61     {
62       octave::interpreter& interp
63         = octave::__get_interpreter__ ("octave_builtin::call");
64 
65       retval = (*m) (interp, args, nargout);
66     }
67 
68   // Do not allow null values to be returned from functions.
69   // FIXME: perhaps true builtins should be allowed?
70 
71   retval.make_storable_values ();
72 
73   // Fix the case of a single undefined value.
74   // This happens when a compiled function uses
75   //
76   //   octave_value retval;
77   //
78   // instead of
79   //
80   //   octave_value_list retval;
81   //
82   // the idiom is very common, so we solve that here.
83 
84   if (retval.length () == 1 && retval.xelem (0).is_undefined ())
85     retval.clear ();
86 
87   return retval;
88 }
89 
90 octave::jit_type *
to_jit(void) const91 octave_builtin::to_jit (void) const
92 {
93   return jtype;
94 }
95 
96 void
stash_jit(octave::jit_type & type)97 octave_builtin::stash_jit (octave::jit_type& type)
98 {
99   jtype = &type;
100 }
101 
102 octave_builtin::fcn
function(void) const103 octave_builtin::function (void) const
104 {
105   return f;
106 }
107 
108 octave_builtin::meth
method(void) const109 octave_builtin::method (void) const
110 {
111   return m;
112 }
113 
114 void
push_dispatch_class(const std::string & dispatch_type)115 octave_builtin::push_dispatch_class (const std::string& dispatch_type)
116 {
117   dispatch_classes.insert (dispatch_type);
118 }
119 
120 bool
handles_dispatch_class(const std::string & dispatch_type) const121 octave_builtin::handles_dispatch_class (const std::string& dispatch_type) const
122 {
123   return dispatch_classes.find (dispatch_type) != dispatch_classes.end ();
124 }
125