Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4 noet: |
| 2 | * |
| 3 | * VIM - Vi IMproved by Bram Moolenaar |
| 4 | * |
| 5 | * Do ":help uganda" in Vim to read copying and usage conditions. |
| 6 | * Do ":help credits" in Vim to see a list of people who contributed. |
| 7 | * See README.txt for an overview of the Vim source code. |
| 8 | */ |
| 9 | |
| 10 | /* |
| 11 | * vim9class.c: Vim9 script class support |
| 12 | */ |
| 13 | |
| 14 | #define USING_FLOAT_STUFF |
| 15 | #include "vim.h" |
| 16 | |
| 17 | #if defined(FEAT_EVAL) || defined(PROTO) |
| 18 | |
| 19 | // When not generating protos this is included in proto.h |
| 20 | #ifdef PROTO |
| 21 | # include "vim9.h" |
| 22 | #endif |
| 23 | |
| 24 | /* |
| 25 | * Handle ":class" and ":abstract class" up to ":endclass". |
| 26 | */ |
| 27 | void |
| 28 | ex_class(exarg_T *eap) |
| 29 | { |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 30 | if (!current_script_is_vim9() |
| 31 | || (cmdmod.cmod_flags & CMOD_LEGACY) |
| 32 | || !getline_equal(eap->getline, eap->cookie, getsourceline)) |
| 33 | { |
| 34 | emsg(_(e_class_can_only_be_defined_in_vim9_script)); |
| 35 | return; |
| 36 | } |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 37 | |
| 38 | char_u *arg = eap->arg; |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 39 | int is_abstract = eap->cmdidx == CMD_abstract; |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 40 | if (is_abstract) |
| 41 | { |
| 42 | if (STRNCMP(arg, "class", 5) != 0 || !VIM_ISWHITE(arg[5])) |
| 43 | { |
| 44 | semsg(_(e_invalid_argument_str), arg); |
| 45 | return; |
| 46 | } |
| 47 | arg = skipwhite(arg + 5); |
| 48 | } |
| 49 | |
| 50 | if (!ASCII_ISUPPER(*arg)) |
| 51 | { |
| 52 | semsg(_(e_class_name_must_start_with_uppercase_letter_str), arg); |
| 53 | return; |
| 54 | } |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 55 | char_u *name_end = find_name_end(arg, NULL, NULL, FNE_CHECK_START); |
| 56 | if (!IS_WHITE_OR_NUL(*name_end)) |
| 57 | { |
| 58 | semsg(_(e_white_space_required_after_class_name_str), arg); |
| 59 | return; |
| 60 | } |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 61 | |
| 62 | // TODO: |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 63 | // generics: <Tkey, Tentry> |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 64 | // extends SomeClass |
| 65 | // implements SomeInterface |
| 66 | // specifies SomeInterface |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 67 | // check nothing follows |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 68 | |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 69 | // TODO: handle "is_export" if it is set |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 70 | |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 71 | garray_T type_list; // list of pointers to allocated types |
| 72 | ga_init2(&type_list, sizeof(type_T *), 10); |
| 73 | |
| 74 | // Growarray with object members declared in the class. |
| 75 | garray_T objmembers; |
| 76 | ga_init2(&objmembers, sizeof(objmember_T), 10); |
| 77 | |
| 78 | // Growarray with object methods declared in the class. |
| 79 | garray_T objmethods; |
| 80 | ga_init2(&objmethods, sizeof(ufunc_T), 10); |
| 81 | |
| 82 | /* |
| 83 | * Go over the body of the class until "endclass" is found. |
| 84 | */ |
| 85 | char_u *theline = NULL; |
| 86 | int success = FALSE; |
| 87 | for (;;) |
| 88 | { |
| 89 | vim_free(theline); |
| 90 | theline = eap->getline(':', eap->cookie, 0, GETLINE_CONCAT_ALL); |
| 91 | if (theline == NULL) |
| 92 | break; |
| 93 | char_u *line = skipwhite(theline); |
| 94 | |
| 95 | // TODO: |
| 96 | // class members (public, read access, private): |
| 97 | // static varname |
| 98 | // public static varname |
| 99 | // static _varname |
| 100 | // |
| 101 | // constructors: |
| 102 | // def new() |
| 103 | // enddef |
| 104 | // def newOther() |
| 105 | // enddef |
| 106 | // |
| 107 | // methods (object, class, generics): |
| 108 | // def someMethod() |
| 109 | // enddef |
| 110 | // static def someMethod() |
| 111 | // enddef |
| 112 | // def <Tval> someMethod() |
| 113 | // enddef |
| 114 | // static def <Tval> someMethod() |
| 115 | // enddef |
| 116 | |
| 117 | char_u *p = line; |
| 118 | if (checkforcmd(&p, "endclass", 4)) |
| 119 | { |
| 120 | if (STRNCMP(line, "endclass", 8) != 0) |
| 121 | semsg(_(e_command_cannot_be_shortened_str), line); |
| 122 | else if (*p == '|' || !ends_excmd2(line, p)) |
| 123 | semsg(_(e_trailing_characters_str), p); |
Bram Moolenaar | 98aeb21 | 2022-12-08 22:09:14 +0000 | [diff] [blame] | 124 | else |
| 125 | success = TRUE; |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 126 | break; |
| 127 | } |
| 128 | |
| 129 | // "this.varname" |
| 130 | // "this._varname" |
| 131 | // TODO: |
| 132 | // "public this.varname" |
| 133 | if (STRNCMP(line, "this", 4) == 0) |
| 134 | { |
| 135 | if (line[4] != '.' || !eval_isnamec1(line[5])) |
| 136 | { |
| 137 | semsg(_(e_invalid_object_member_declaration_str), line); |
| 138 | break; |
| 139 | } |
| 140 | char_u *varname = line + 5; |
| 141 | char_u *varname_end = to_name_end(varname, FALSE); |
| 142 | |
| 143 | char_u *colon = skipwhite(varname_end); |
| 144 | // TODO: accept initialization and figure out type from it |
| 145 | if (*colon != ':') |
| 146 | { |
| 147 | emsg(_(e_type_or_initialization_required)); |
| 148 | break; |
| 149 | } |
| 150 | if (VIM_ISWHITE(*varname_end)) |
| 151 | { |
| 152 | semsg(_(e_no_white_space_allowed_before_colon_str), varname); |
| 153 | break; |
| 154 | } |
| 155 | if (!VIM_ISWHITE(colon[1])) |
| 156 | { |
| 157 | semsg(_(e_white_space_required_after_str_str), ":", varname); |
| 158 | break; |
| 159 | } |
| 160 | |
| 161 | char_u *type_arg = skipwhite(colon + 1); |
| 162 | type_T *type = parse_type(&type_arg, &type_list, TRUE); |
| 163 | if (type == NULL) |
| 164 | break; |
| 165 | |
| 166 | if (ga_grow(&objmembers, 1) == FAIL) |
| 167 | break; |
| 168 | objmember_T *m = ((objmember_T *)objmembers.ga_data) |
| 169 | + objmembers.ga_len; |
| 170 | m->om_name = vim_strnsave(varname, varname_end - varname); |
| 171 | m->om_type = type; |
| 172 | ++objmembers.ga_len; |
| 173 | } |
| 174 | |
| 175 | else |
| 176 | { |
| 177 | semsg(_(e_not_valid_command_in_class_str), line); |
| 178 | break; |
| 179 | } |
| 180 | } |
| 181 | vim_free(theline); |
| 182 | |
| 183 | if (success) |
| 184 | { |
| 185 | class_T *cl = ALLOC_CLEAR_ONE(class_T); |
| 186 | if (cl == NULL) |
| 187 | goto cleanup; |
| 188 | cl->class_refcount = 1; |
| 189 | cl->class_name = vim_strnsave(arg, name_end - arg); |
| 190 | |
| 191 | // Members are used by the new() function, add them here. |
| 192 | cl->class_obj_member_count = objmembers.ga_len; |
Bram Moolenaar | 98aeb21 | 2022-12-08 22:09:14 +0000 | [diff] [blame] | 193 | cl->class_obj_members = objmembers.ga_len == 0 ? NULL |
| 194 | : ALLOC_MULT(objmember_T, objmembers.ga_len); |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 195 | if (cl->class_name == NULL |
Bram Moolenaar | 98aeb21 | 2022-12-08 22:09:14 +0000 | [diff] [blame] | 196 | || (objmembers.ga_len > 0 && cl->class_obj_members == NULL)) |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 197 | { |
| 198 | vim_free(cl->class_name); |
| 199 | vim_free(cl->class_obj_members); |
| 200 | vim_free(cl); |
| 201 | goto cleanup; |
| 202 | } |
| 203 | mch_memmove(cl->class_obj_members, objmembers.ga_data, |
| 204 | sizeof(objmember_T) * objmembers.ga_len); |
| 205 | vim_free(objmembers.ga_data); |
| 206 | |
| 207 | int have_new = FALSE; |
| 208 | for (int i = 0; i < objmethods.ga_len; ++i) |
| 209 | if (STRCMP((((ufunc_T *)objmethods.ga_data) + i)->uf_name, |
| 210 | "new") == 0) |
| 211 | { |
| 212 | have_new = TRUE; |
| 213 | break; |
| 214 | } |
| 215 | if (!have_new) |
| 216 | { |
| 217 | // No new() method was defined, add the default constructor. |
| 218 | garray_T fga; |
| 219 | ga_init2(&fga, 1, 1000); |
| 220 | ga_concat(&fga, (char_u *)"new("); |
| 221 | for (int i = 0; i < cl->class_obj_member_count; ++i) |
| 222 | { |
| 223 | if (i > 0) |
| 224 | ga_concat(&fga, (char_u *)", "); |
| 225 | ga_concat(&fga, (char_u *)"this."); |
| 226 | objmember_T *m = cl->class_obj_members + i; |
| 227 | ga_concat(&fga, (char_u *)m->om_name); |
| 228 | } |
| 229 | ga_concat(&fga, (char_u *)")\nenddef\n"); |
| 230 | ga_append(&fga, NUL); |
| 231 | |
| 232 | exarg_T fea; |
| 233 | CLEAR_FIELD(fea); |
| 234 | fea.cmdidx = CMD_def; |
| 235 | fea.cmd = fea.arg = fga.ga_data; |
| 236 | |
| 237 | garray_T lines_to_free; |
| 238 | ga_init2(&lines_to_free, sizeof(char_u *), 50); |
| 239 | |
| 240 | ufunc_T *nf = define_function(&fea, NULL, &lines_to_free, cl); |
| 241 | |
| 242 | ga_clear_strings(&lines_to_free); |
| 243 | vim_free(fga.ga_data); |
| 244 | |
| 245 | if (nf != NULL && ga_grow(&objmethods, 1) == OK) |
| 246 | { |
| 247 | ((ufunc_T **)objmethods.ga_data)[objmethods.ga_len] = nf; |
| 248 | ++objmethods.ga_len; |
| 249 | |
| 250 | nf->uf_flags |= FC_NEW; |
| 251 | nf->uf_class = cl; |
| 252 | nf->uf_ret_type = get_type_ptr(&type_list); |
| 253 | if (nf->uf_ret_type != NULL) |
| 254 | { |
| 255 | nf->uf_ret_type->tt_type = VAR_OBJECT; |
| 256 | nf->uf_ret_type->tt_member = (type_T *)cl; |
| 257 | nf->uf_ret_type->tt_argcount = 0; |
| 258 | nf->uf_ret_type->tt_args = NULL; |
| 259 | } |
| 260 | cl->class_new_func = nf; |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | cl->class_obj_method_count = objmethods.ga_len; |
| 265 | cl->class_obj_methods = ALLOC_MULT(ufunc_T *, objmethods.ga_len); |
| 266 | if (cl->class_obj_methods == NULL) |
| 267 | { |
| 268 | vim_free(cl->class_name); |
| 269 | vim_free(cl->class_obj_members); |
| 270 | vim_free(cl->class_obj_methods); |
| 271 | vim_free(cl); |
| 272 | goto cleanup; |
| 273 | } |
| 274 | mch_memmove(cl->class_obj_methods, objmethods.ga_data, |
| 275 | sizeof(ufunc_T *) * objmethods.ga_len); |
| 276 | vim_free(objmethods.ga_data); |
| 277 | |
| 278 | cl->class_type.tt_type = VAR_CLASS; |
| 279 | cl->class_type.tt_member = (type_T *)cl; |
| 280 | cl->class_type_list = type_list; |
| 281 | |
| 282 | // TODO: |
| 283 | // - Add the methods to the class |
| 284 | // - array with ufunc_T pointers |
| 285 | // - Fill hashtab with object members and methods |
| 286 | // - Generate the default new() method, if needed. |
| 287 | // Later: |
| 288 | // - class members |
| 289 | // - class methods |
| 290 | |
| 291 | // Add the class to the script-local variables. |
| 292 | typval_T tv; |
| 293 | tv.v_type = VAR_CLASS; |
| 294 | tv.vval.v_class = cl; |
| 295 | set_var_const(cl->class_name, current_sctx.sc_sid, |
| 296 | NULL, &tv, FALSE, ASSIGN_DECL, 0); |
| 297 | return; |
| 298 | } |
| 299 | |
| 300 | cleanup: |
| 301 | for (int i = 0; i < objmembers.ga_len; ++i) |
| 302 | { |
| 303 | objmember_T *m = ((objmember_T *)objmembers.ga_data) + i; |
| 304 | vim_free(m->om_name); |
| 305 | } |
| 306 | ga_clear(&objmembers); |
| 307 | |
| 308 | ga_clear(&objmethods); |
| 309 | clear_type_list(&type_list); |
| 310 | } |
| 311 | |
| 312 | /* |
| 313 | * Find member "name" in class "cl" and return its type. |
| 314 | * When not found t_any is returned. |
| 315 | */ |
| 316 | type_T * |
| 317 | class_member_type( |
| 318 | class_T *cl, |
| 319 | char_u *name, |
| 320 | char_u *name_end, |
| 321 | int *member_idx) |
| 322 | { |
| 323 | *member_idx = -1; // not found (yet) |
| 324 | size_t len = name_end - name; |
| 325 | |
| 326 | for (int i = 0; i < cl->class_obj_member_count; ++i) |
| 327 | { |
| 328 | objmember_T *m = cl->class_obj_members + i; |
| 329 | if (STRNCMP(m->om_name, name, len) == 0 && m->om_name[len] == NUL) |
| 330 | { |
| 331 | *member_idx = i; |
| 332 | return m->om_type; |
| 333 | } |
| 334 | } |
| 335 | return &t_any; |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | /* |
| 339 | * Handle ":interface" up to ":endinterface". |
| 340 | */ |
| 341 | void |
| 342 | ex_interface(exarg_T *eap UNUSED) |
| 343 | { |
| 344 | // TODO |
| 345 | } |
| 346 | |
| 347 | /* |
| 348 | * Handle ":enum" up to ":endenum". |
| 349 | */ |
| 350 | void |
| 351 | ex_enum(exarg_T *eap UNUSED) |
| 352 | { |
| 353 | // TODO |
| 354 | } |
| 355 | |
| 356 | /* |
| 357 | * Handle ":type". |
| 358 | */ |
| 359 | void |
| 360 | ex_type(exarg_T *eap UNUSED) |
| 361 | { |
| 362 | // TODO |
| 363 | } |
| 364 | |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 365 | /* |
| 366 | * Evaluate what comes after a class: |
| 367 | * - class member: SomeClass.varname |
| 368 | * - class method: SomeClass.SomeMethod() |
| 369 | * - class constructor: SomeClass.new() |
| 370 | * - object member: someObject.varname |
| 371 | * - object method: someObject.SomeMethod() |
| 372 | * |
| 373 | * "*arg" points to the '.'. |
| 374 | * "*arg" is advanced to after the member name or method call. |
| 375 | * |
| 376 | * Returns FAIL or OK. |
| 377 | */ |
| 378 | int |
| 379 | class_object_index( |
| 380 | char_u **arg, |
| 381 | typval_T *rettv, |
| 382 | evalarg_T *evalarg, |
| 383 | int verbose UNUSED) // give error messages |
| 384 | { |
| 385 | // int evaluate = evalarg != NULL |
| 386 | // && (evalarg->eval_flags & EVAL_EVALUATE); |
| 387 | |
| 388 | if (VIM_ISWHITE((*arg)[1])) |
| 389 | { |
| 390 | semsg(_(e_no_white_space_allowed_after_str_str), ".", *arg); |
| 391 | return FAIL; |
| 392 | } |
| 393 | |
| 394 | ++*arg; |
| 395 | char_u *name = *arg; |
| 396 | char_u *name_end = find_name_end(name, NULL, NULL, FNE_CHECK_START); |
| 397 | if (name_end == name) |
| 398 | return FAIL; |
| 399 | size_t len = name_end - name; |
| 400 | |
| 401 | class_T *cl = rettv->v_type == VAR_CLASS ? rettv->vval.v_class |
| 402 | : rettv->vval.v_object->obj_class; |
| 403 | if (*name_end == '(') |
| 404 | { |
| 405 | for (int i = 0; i < cl->class_obj_method_count; ++i) |
| 406 | { |
| 407 | ufunc_T *fp = cl->class_obj_methods[i]; |
| 408 | if (STRNCMP(name, fp->uf_name, len) == 0 && fp->uf_name[len] == NUL) |
| 409 | { |
| 410 | typval_T argvars[MAX_FUNC_ARGS + 1]; |
| 411 | int argcount = 0; |
| 412 | |
| 413 | char_u *argp = name_end; |
| 414 | int ret = get_func_arguments(&argp, evalarg, 0, |
| 415 | argvars, &argcount); |
| 416 | if (ret == FAIL) |
| 417 | return FAIL; |
| 418 | |
| 419 | funcexe_T funcexe; |
| 420 | CLEAR_FIELD(funcexe); |
| 421 | funcexe.fe_evaluate = TRUE; |
| 422 | |
Bram Moolenaar | d28d7b9 | 2022-12-08 20:42:00 +0000 | [diff] [blame] | 423 | // Clear the class or object after calling the function, in |
| 424 | // case the refcount is one. |
| 425 | typval_T tv_tofree = *rettv; |
| 426 | rettv->v_type = VAR_UNKNOWN; |
| 427 | |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 428 | // Call the user function. Result goes into rettv; |
| 429 | // TODO: pass the object |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 430 | int error = call_user_func_check(fp, argcount, argvars, |
| 431 | rettv, &funcexe, NULL); |
| 432 | |
Bram Moolenaar | d28d7b9 | 2022-12-08 20:42:00 +0000 | [diff] [blame] | 433 | // Clear the previous rettv and the arguments. |
| 434 | clear_tv(&tv_tofree); |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 435 | for (int idx = 0; idx < argcount; ++idx) |
| 436 | clear_tv(&argvars[idx]); |
| 437 | |
| 438 | if (error != FCERR_NONE) |
| 439 | { |
| 440 | user_func_error(error, printable_func_name(fp), |
| 441 | funcexe.fe_found_var); |
| 442 | return FAIL; |
| 443 | } |
| 444 | *arg = argp; |
| 445 | return OK; |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | semsg(_(e_method_not_found_on_class_str_str), cl->class_name, name); |
| 450 | } |
| 451 | |
| 452 | else if (rettv->v_type == VAR_OBJECT) |
| 453 | { |
| 454 | for (int i = 0; i < cl->class_obj_member_count; ++i) |
| 455 | { |
| 456 | objmember_T *m = &cl->class_obj_members[i]; |
| 457 | if (STRNCMP(name, m->om_name, len) == 0 && m->om_name[len] == NUL) |
| 458 | { |
| 459 | // The object only contains a pointer to the class, the member |
| 460 | // values array follows right after that. |
| 461 | object_T *obj = rettv->vval.v_object; |
| 462 | typval_T *tv = (typval_T *)(obj + 1) + i; |
| 463 | copy_tv(tv, rettv); |
| 464 | object_unref(obj); |
| 465 | |
| 466 | *arg = name_end; |
| 467 | return OK; |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | semsg(_(e_member_not_found_on_object_str_str), cl->class_name, name); |
| 472 | } |
| 473 | |
| 474 | // TODO: class member |
| 475 | |
| 476 | return FAIL; |
| 477 | } |
| 478 | |
| 479 | /* |
| 480 | * Make a copy of an object. |
| 481 | */ |
| 482 | void |
| 483 | copy_object(typval_T *from, typval_T *to) |
| 484 | { |
| 485 | *to = *from; |
| 486 | if (to->vval.v_object != NULL) |
| 487 | ++to->vval.v_object->obj_refcount; |
| 488 | } |
| 489 | |
| 490 | /* |
| 491 | * Free an object. |
| 492 | */ |
| 493 | static void |
| 494 | object_clear(object_T *obj) |
| 495 | { |
| 496 | class_T *cl = obj->obj_class; |
| 497 | |
| 498 | // the member values are just after the object structure |
| 499 | typval_T *tv = (typval_T *)(obj + 1); |
| 500 | for (int i = 0; i < cl->class_obj_member_count; ++i) |
| 501 | clear_tv(tv + i); |
| 502 | |
Bram Moolenaar | d28d7b9 | 2022-12-08 20:42:00 +0000 | [diff] [blame] | 503 | // Remove from the list headed by "first_object". |
| 504 | object_cleared(obj); |
| 505 | |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 506 | vim_free(obj); |
Bram Moolenaar | d28d7b9 | 2022-12-08 20:42:00 +0000 | [diff] [blame] | 507 | class_unref(cl); |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | /* |
| 511 | * Unreference an object. |
| 512 | */ |
| 513 | void |
| 514 | object_unref(object_T *obj) |
| 515 | { |
| 516 | if (obj != NULL && --obj->obj_refcount <= 0) |
| 517 | object_clear(obj); |
| 518 | } |
| 519 | |
| 520 | /* |
| 521 | * Make a copy of a class. |
| 522 | */ |
| 523 | void |
| 524 | copy_class(typval_T *from, typval_T *to) |
| 525 | { |
| 526 | *to = *from; |
| 527 | if (to->vval.v_class != NULL) |
| 528 | ++to->vval.v_class->class_refcount; |
| 529 | } |
| 530 | |
| 531 | /* |
| 532 | * Unreference a class. Free it when the reference count goes down to zero. |
| 533 | */ |
| 534 | void |
Bram Moolenaar | d28d7b9 | 2022-12-08 20:42:00 +0000 | [diff] [blame] | 535 | class_unref(class_T *cl) |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 536 | { |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 537 | if (cl != NULL && --cl->class_refcount <= 0) |
| 538 | { |
| 539 | vim_free(cl->class_name); |
| 540 | |
| 541 | for (int i = 0; i < cl->class_obj_member_count; ++i) |
| 542 | { |
| 543 | objmember_T *m = &cl->class_obj_members[i]; |
| 544 | vim_free(m->om_name); |
| 545 | } |
| 546 | vim_free(cl->class_obj_members); |
| 547 | |
| 548 | vim_free(cl->class_obj_methods); |
| 549 | |
| 550 | if (cl->class_new_func != NULL) |
| 551 | func_ptr_unref(cl->class_new_func); |
| 552 | |
| 553 | clear_type_list(&cl->class_type_list); |
| 554 | |
| 555 | vim_free(cl); |
| 556 | } |
| 557 | } |
| 558 | |
Bram Moolenaar | d28d7b9 | 2022-12-08 20:42:00 +0000 | [diff] [blame] | 559 | static object_T *first_object = NULL; |
| 560 | |
| 561 | /* |
| 562 | * Call this function when an object has been created. It will be added to the |
| 563 | * list headed by "first_object". |
| 564 | */ |
| 565 | void |
| 566 | object_created(object_T *obj) |
| 567 | { |
| 568 | if (first_object != NULL) |
| 569 | { |
| 570 | obj->obj_next_used = first_object; |
| 571 | first_object->obj_prev_used = obj; |
| 572 | } |
| 573 | first_object = obj; |
| 574 | } |
| 575 | |
| 576 | /* |
| 577 | * Call this function when an object has been cleared and is about to be freed. |
| 578 | * It is removed from the list headed by "first_object". |
| 579 | */ |
| 580 | void |
| 581 | object_cleared(object_T *obj) |
| 582 | { |
| 583 | if (obj->obj_next_used != NULL) |
| 584 | obj->obj_next_used->obj_prev_used = obj->obj_prev_used; |
| 585 | if (obj->obj_prev_used != NULL) |
| 586 | obj->obj_prev_used->obj_next_used = obj->obj_next_used; |
| 587 | else if (first_object == obj) |
| 588 | first_object = obj->obj_next_used; |
| 589 | } |
| 590 | |
| 591 | /* |
| 592 | * Go through the list of all objects and free items without "copyID". |
| 593 | */ |
| 594 | int |
| 595 | object_free_nonref(int copyID) |
| 596 | { |
| 597 | int did_free = FALSE; |
| 598 | object_T *next_obj; |
| 599 | |
| 600 | for (object_T *obj = first_object; obj != NULL; obj = next_obj) |
| 601 | { |
| 602 | next_obj = obj->obj_next_used; |
| 603 | if ((obj->obj_copyID & COPYID_MASK) != (copyID & COPYID_MASK)) |
| 604 | { |
| 605 | // Free the object and items it contains. |
| 606 | object_clear(obj); |
| 607 | did_free = TRUE; |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | return did_free; |
| 612 | } |
| 613 | |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 614 | |
| 615 | #endif // FEAT_EVAL |