1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
|
////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2012-2021 The Octave Project Developers
//
// See the file COPYRIGHT.md in the top-level directory of this
// distribution or <https://octave.org/copyright/>.
//
// This file is part of Octave.
//
// Octave is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Octave is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Octave; see the file COPYING. If not, see
// <https://www.gnu.org/licenses/>.
//
////////////////////////////////////////////////////////////////////////
#if defined (HAVE_CONFIG_H)
# include "config.h"
#endif
#include <algorithm>
#include <iomanip>
#include "cdef-class.h"
#include "cdef-manager.h"
#include "cdef-utils.h"
#include "errwarn.h"
#include "interpreter-private.h"
#include "interpreter.h"
#include "load-path.h"
#include "ov-builtin.h"
#include "ov-classdef.h"
#include "ov-fcn-handle.h"
#include "ov-usr-fcn.h"
#include "parse.h"
#include "pt-assign.h"
#include "pt-classdef.h"
#include "pt-eval.h"
#include "pt-idx.h"
#include "pt-misc.h"
#include "pt-stmt.h"
#include "pt-walk.h"
namespace octave
{
static bool
is_method_executing (const octave_value& ov, const cdef_object& obj)
{
tree_evaluator& tw = __get_evaluator__ ("is_method_executing");
octave_function *stack_fcn = tw.current_function ();
octave_function *method_fcn = ov.function_value (true);
// Does the top of the call stack match our target function?
if (stack_fcn && stack_fcn == method_fcn)
{
octave_user_function *uf = method_fcn->user_function_value (true);
// We can only check the context object for user-function (not builtin),
// where we have access to the parameters (arguments and return values).
// That's ok as there's no need to call this function for builtin
// methods.
if (uf)
{
// At this point, the method is executing, but we still need to
// check the context object for which the method is executing. For
// methods, it's the first argument of the function; for ctors, it
// is the first return value.
tree_parameter_list *pl = uf->is_classdef_constructor ()
? uf->return_list ()
: uf->parameter_list ();
if (pl && pl->size () > 0)
{
tree_decl_elt *elt = pl->front ();
octave_value arg0 = tw.evaluate (elt);
if (arg0.is_defined () && arg0.type_name () == "object")
{
cdef_object arg0_obj = to_cdef (arg0);
return obj.is (arg0_obj);
}
}
}
}
return false;
}
octave_value
cdef_property::cdef_property_rep::get_value (const cdef_object& obj,
bool do_check_access,
const std::string& who) const
{
octave_value retval;
if (do_check_access && ! check_get_access ())
err_property_access (who, false);
if (! obj.is_constructed ())
{
cdef_class cls (to_cdef (get ("DefiningClass")));
if (! obj.is_partially_constructed_for (cls))
error ("cannot reference properties of class '%s' for non-constructed object",
cls.get_name ().c_str ());
}
octave_value get_fcn = get ("GetMethod");
// FIXME: should check whether we're already in get accessor method
if (get_fcn.isempty () || is_method_executing (get_fcn, obj))
retval = obj.get (get ("Name").string_value ());
else
{
octave_value_list args;
args(0) = to_ov (obj);
args = feval (get_fcn, args, 1);
retval = args(0);
}
return retval;
}
octave_value
cdef_property::cdef_property_rep::get_value (bool do_check_access,
const std::string& who) const
{
if (do_check_access && ! check_get_access ())
err_property_access (who, false);
return get ("DefaultValue");
}
bool
cdef_property::cdef_property_rep::is_recursive_set (const cdef_object& /* obj */) const
{
// FIXME: implement
return false;
}
OCTAVE_NORETURN void
cdef_property::cdef_property_rep::err_property_access
(const std::string& from, bool is_set) const
{
octave_value acc = get (is_set ? "SetAccess" : "GetAccess");
std::string acc_s;
if (acc.is_string ())
acc_s = acc.string_value ();
else
acc_s = "class-restricted";
if (is_set)
error ("%s: property '%s' has %s access and cannot be set in this context",
from.c_str (), get_name ().c_str (), acc_s.c_str ());
else
error ("%s: property '%s' has %s access and cannot be obtained in this context",
from.c_str (), get_name ().c_str (), acc_s.c_str ());
}
void
cdef_property::cdef_property_rep::set_value (cdef_object& obj,
const octave_value& val,
bool do_check_access,
const std::string& who)
{
if (do_check_access && ! check_set_access ())
err_property_access (who, true);
if (! obj.is_constructed ())
{
cdef_class cls (to_cdef (get ("DefiningClass")));
if (! obj.is_partially_constructed_for (cls))
error ("cannot reference properties of class '%s' for non-constructed object",
cls.get_name ().c_str ());
}
octave_value set_fcn = get ("SetMethod");
if (set_fcn.isempty () || is_method_executing (set_fcn, obj))
obj.put (get ("Name").string_value (), val);
else
{
octave_value_list args;
args(0) = to_ov (obj);
args(1) = val;
args = feval (set_fcn, args, 1);
if (args.length () > 0 && args(0).is_defined ())
{
if (args (0).is_classdef_object ())
{
cdef_object new_obj = to_cdef (args(0));
obj = new_obj;
}
else
::warning ("set-method of property '%s' returned a non-classdef object",
get_name ().c_str ());
}
}
}
bool
cdef_property::cdef_property_rep::check_get_access (void) const
{
cdef_class cls (to_cdef (get ("DefiningClass")));
return check_access (cls, get ("GetAccess"), "", get_name (), false);
return false;
}
bool
cdef_property::cdef_property_rep::check_set_access (void) const
{
cdef_class cls (to_cdef (get ("DefiningClass")));
return check_access (cls, get ("SetAccess"), "", get_name (), true);
return false;
}
}
|