annotate src/vim9class.c @ 31447:8cb31f7ac9e2 v9.0.1056

patch 9.0.1056: leaking memory when disassembling an object method Commit: https://github.com/vim/vim/commit/eb53350c02f620305e931ffd2ac611cc2b1a0ce9 Author: Bram Moolenaar <Bram@vim.org> Date: Wed Dec 14 15:06:11 2022 +0000 patch 9.0.1056: leaking memory when disassembling an object method Problem: Leaking memory when disassembling an object method. Solution: Free the typval of the class.
author Bram Moolenaar <Bram@vim.org>
date Wed, 14 Dec 2022 16:15:04 +0100
parents 9ae3720f9bd9
children 5ef28f5ff357
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1 /* vi:set ts=8 sts=4 sw=4 noet:
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2 *
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
3 * VIM - Vi IMproved by Bram Moolenaar
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
4 *
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
5 * Do ":help uganda" in Vim to read copying and usage conditions.
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
6 * Do ":help credits" in Vim to see a list of people who contributed.
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
7 * See README.txt for an overview of the Vim source code.
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
8 */
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
9
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
10 /*
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
11 * vim9class.c: Vim9 script class support
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
12 */
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
13
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
14 #define USING_FLOAT_STUFF
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
15 #include "vim.h"
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
16
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
17 #if defined(FEAT_EVAL) || defined(PROTO)
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
18
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
19 // When not generating protos this is included in proto.h
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
20 #ifdef PROTO
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
21 # include "vim9.h"
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
22 #endif
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
23
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
24 /*
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
25 * Handle ":class" and ":abstract class" up to ":endclass".
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
26 */
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
27 void
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
28 ex_class(exarg_T *eap)
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
29 {
31396
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
30 if (!current_script_is_vim9()
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
31 || (cmdmod.cmod_flags & CMOD_LEGACY)
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
32 || !getline_equal(eap->getline, eap->cookie, getsourceline))
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
33 {
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
34 emsg(_(e_class_can_only_be_defined_in_vim9_script));
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
35 return;
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
36 }
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
37
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
38 char_u *arg = eap->arg;
31396
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
39 int is_abstract = eap->cmdidx == CMD_abstract;
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
40 if (is_abstract)
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
41 {
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
42 if (STRNCMP(arg, "class", 5) != 0 || !VIM_ISWHITE(arg[5]))
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
43 {
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
44 semsg(_(e_invalid_argument_str), arg);
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
45 return;
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
46 }
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
47 arg = skipwhite(arg + 5);
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
48 }
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
49
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
50 if (!ASCII_ISUPPER(*arg))
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
51 {
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
52 semsg(_(e_class_name_must_start_with_uppercase_letter_str), arg);
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
53 return;
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
54 }
31396
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
55 char_u *name_end = find_name_end(arg, NULL, NULL, FNE_CHECK_START);
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
56 if (!IS_WHITE_OR_NUL(*name_end))
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
57 {
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
58 semsg(_(e_white_space_required_after_class_name_str), arg);
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
59 return;
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
60 }
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
61
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
62 // TODO:
31396
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
63 // generics: <Tkey, Tentry>
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
64 // extends SomeClass
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
65 // implements SomeInterface
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
66 // specifies SomeInterface
31396
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
67 // check nothing follows
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
68
31396
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
69 // TODO: handle "is_export" if it is set
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
70
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
71 garray_T type_list; // list of pointers to allocated types
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
72 ga_init2(&type_list, sizeof(type_T *), 10);
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
73
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
74 // Growarray with object members declared in the class.
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
75 garray_T objmembers;
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
76 ga_init2(&objmembers, sizeof(objmember_T), 10);
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
77
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
78 // Growarray with object methods declared in the class.
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
79 garray_T objmethods;
31416
f088f1d97eee patch 9.0.1041: cannot define a method in a class
Bram Moolenaar <Bram@vim.org>
parents: 31408
diff changeset
80 ga_init2(&objmethods, sizeof(ufunc_T *), 10);
31396
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
81
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
82 /*
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
83 * Go over the body of the class until "endclass" is found.
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
84 */
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
85 char_u *theline = NULL;
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
86 int success = FALSE;
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
87 for (;;)
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
88 {
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
89 vim_free(theline);
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
90 theline = eap->getline(':', eap->cookie, 0, GETLINE_CONCAT_ALL);
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
91 if (theline == NULL)
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
92 break;
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
93 char_u *line = skipwhite(theline);
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
94
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
95 // TODO:
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
96 // class members (public, read access, private):
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
97 // static varname
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
98 // public static varname
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
99 // static _varname
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
100
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
101 char_u *p = line;
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
102 if (checkforcmd(&p, "endclass", 4))
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
103 {
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
104 if (STRNCMP(line, "endclass", 8) != 0)
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
105 semsg(_(e_command_cannot_be_shortened_str), line);
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
106 else if (*p == '|' || !ends_excmd2(line, p))
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
107 semsg(_(e_trailing_characters_str), p);
31408
c82cb53474ee patch 9.0.1037: lalloc(0) error for a class without members
Bram Moolenaar <Bram@vim.org>
parents: 31404
diff changeset
108 else
c82cb53474ee patch 9.0.1037: lalloc(0) error for a class without members
Bram Moolenaar <Bram@vim.org>
parents: 31404
diff changeset
109 success = TRUE;
31396
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
110 break;
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
111 }
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
112
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
113 // "this.varname"
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
114 // "this._varname"
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
115 // TODO:
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
116 // "public this.varname"
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
117 if (STRNCMP(line, "this", 4) == 0)
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
118 {
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
119 if (line[4] != '.' || !eval_isnamec1(line[5]))
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
120 {
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
121 semsg(_(e_invalid_object_member_declaration_str), line);
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
122 break;
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
123 }
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
124 char_u *varname = line + 5;
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
125 char_u *varname_end = to_name_end(varname, FALSE);
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
126
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
127 char_u *colon = skipwhite(varname_end);
31443
9ae3720f9bd9 patch 9.0.1054: object member can't get type from initializer
Bram Moolenaar <Bram@vim.org>
parents: 31441
diff changeset
128 char_u *type_arg = colon;
9ae3720f9bd9 patch 9.0.1054: object member can't get type from initializer
Bram Moolenaar <Bram@vim.org>
parents: 31441
diff changeset
129 type_T *type = NULL;
9ae3720f9bd9 patch 9.0.1054: object member can't get type from initializer
Bram Moolenaar <Bram@vim.org>
parents: 31441
diff changeset
130 if (*colon == ':')
9ae3720f9bd9 patch 9.0.1054: object member can't get type from initializer
Bram Moolenaar <Bram@vim.org>
parents: 31441
diff changeset
131 {
9ae3720f9bd9 patch 9.0.1054: object member can't get type from initializer
Bram Moolenaar <Bram@vim.org>
parents: 31441
diff changeset
132 if (VIM_ISWHITE(*varname_end))
9ae3720f9bd9 patch 9.0.1054: object member can't get type from initializer
Bram Moolenaar <Bram@vim.org>
parents: 31441
diff changeset
133 {
9ae3720f9bd9 patch 9.0.1054: object member can't get type from initializer
Bram Moolenaar <Bram@vim.org>
parents: 31441
diff changeset
134 semsg(_(e_no_white_space_allowed_before_colon_str),
9ae3720f9bd9 patch 9.0.1054: object member can't get type from initializer
Bram Moolenaar <Bram@vim.org>
parents: 31441
diff changeset
135 varname);
9ae3720f9bd9 patch 9.0.1054: object member can't get type from initializer
Bram Moolenaar <Bram@vim.org>
parents: 31441
diff changeset
136 break;
9ae3720f9bd9 patch 9.0.1054: object member can't get type from initializer
Bram Moolenaar <Bram@vim.org>
parents: 31441
diff changeset
137 }
9ae3720f9bd9 patch 9.0.1054: object member can't get type from initializer
Bram Moolenaar <Bram@vim.org>
parents: 31441
diff changeset
138 if (!VIM_ISWHITE(colon[1]))
9ae3720f9bd9 patch 9.0.1054: object member can't get type from initializer
Bram Moolenaar <Bram@vim.org>
parents: 31441
diff changeset
139 {
9ae3720f9bd9 patch 9.0.1054: object member can't get type from initializer
Bram Moolenaar <Bram@vim.org>
parents: 31441
diff changeset
140 semsg(_(e_white_space_required_after_str_str), ":",
9ae3720f9bd9 patch 9.0.1054: object member can't get type from initializer
Bram Moolenaar <Bram@vim.org>
parents: 31441
diff changeset
141 varname);
9ae3720f9bd9 patch 9.0.1054: object member can't get type from initializer
Bram Moolenaar <Bram@vim.org>
parents: 31441
diff changeset
142 break;
9ae3720f9bd9 patch 9.0.1054: object member can't get type from initializer
Bram Moolenaar <Bram@vim.org>
parents: 31441
diff changeset
143 }
9ae3720f9bd9 patch 9.0.1054: object member can't get type from initializer
Bram Moolenaar <Bram@vim.org>
parents: 31441
diff changeset
144 type_arg = skipwhite(colon + 1);
9ae3720f9bd9 patch 9.0.1054: object member can't get type from initializer
Bram Moolenaar <Bram@vim.org>
parents: 31441
diff changeset
145 type = parse_type(&type_arg, &type_list, TRUE);
9ae3720f9bd9 patch 9.0.1054: object member can't get type from initializer
Bram Moolenaar <Bram@vim.org>
parents: 31441
diff changeset
146 if (type == NULL)
9ae3720f9bd9 patch 9.0.1054: object member can't get type from initializer
Bram Moolenaar <Bram@vim.org>
parents: 31441
diff changeset
147 break;
9ae3720f9bd9 patch 9.0.1054: object member can't get type from initializer
Bram Moolenaar <Bram@vim.org>
parents: 31441
diff changeset
148 }
9ae3720f9bd9 patch 9.0.1054: object member can't get type from initializer
Bram Moolenaar <Bram@vim.org>
parents: 31441
diff changeset
149
9ae3720f9bd9 patch 9.0.1054: object member can't get type from initializer
Bram Moolenaar <Bram@vim.org>
parents: 31441
diff changeset
150 char_u *expr_start = skipwhite(type_arg);
9ae3720f9bd9 patch 9.0.1054: object member can't get type from initializer
Bram Moolenaar <Bram@vim.org>
parents: 31441
diff changeset
151 char_u *expr_end = expr_start;
9ae3720f9bd9 patch 9.0.1054: object member can't get type from initializer
Bram Moolenaar <Bram@vim.org>
parents: 31441
diff changeset
152 if (type == NULL && *expr_start != '=')
31396
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
153 {
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
154 emsg(_(e_type_or_initialization_required));
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
155 break;
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
156 }
31443
9ae3720f9bd9 patch 9.0.1054: object member can't get type from initializer
Bram Moolenaar <Bram@vim.org>
parents: 31441
diff changeset
157
9ae3720f9bd9 patch 9.0.1054: object member can't get type from initializer
Bram Moolenaar <Bram@vim.org>
parents: 31441
diff changeset
158 if (*expr_start == '=')
31396
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
159 {
31443
9ae3720f9bd9 patch 9.0.1054: object member can't get type from initializer
Bram Moolenaar <Bram@vim.org>
parents: 31441
diff changeset
160 if (!VIM_ISWHITE(expr_start[-1]) || !VIM_ISWHITE(expr_start[1]))
9ae3720f9bd9 patch 9.0.1054: object member can't get type from initializer
Bram Moolenaar <Bram@vim.org>
parents: 31441
diff changeset
161 {
9ae3720f9bd9 patch 9.0.1054: object member can't get type from initializer
Bram Moolenaar <Bram@vim.org>
parents: 31441
diff changeset
162 semsg(_(e_white_space_required_before_and_after_str_at_str),
9ae3720f9bd9 patch 9.0.1054: object member can't get type from initializer
Bram Moolenaar <Bram@vim.org>
parents: 31441
diff changeset
163 "=", type_arg);
9ae3720f9bd9 patch 9.0.1054: object member can't get type from initializer
Bram Moolenaar <Bram@vim.org>
parents: 31441
diff changeset
164 break;
9ae3720f9bd9 patch 9.0.1054: object member can't get type from initializer
Bram Moolenaar <Bram@vim.org>
parents: 31441
diff changeset
165 }
9ae3720f9bd9 patch 9.0.1054: object member can't get type from initializer
Bram Moolenaar <Bram@vim.org>
parents: 31441
diff changeset
166 expr_start = skipwhite(expr_start + 1);
31396
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
167
31443
9ae3720f9bd9 patch 9.0.1054: object member can't get type from initializer
Bram Moolenaar <Bram@vim.org>
parents: 31441
diff changeset
168 expr_end = expr_start;
9ae3720f9bd9 patch 9.0.1054: object member can't get type from initializer
Bram Moolenaar <Bram@vim.org>
parents: 31441
diff changeset
169 evalarg_T evalarg;
9ae3720f9bd9 patch 9.0.1054: object member can't get type from initializer
Bram Moolenaar <Bram@vim.org>
parents: 31441
diff changeset
170 fill_evalarg_from_eap(&evalarg, eap, FALSE);
31447
8cb31f7ac9e2 patch 9.0.1056: leaking memory when disassembling an object method
Bram Moolenaar <Bram@vim.org>
parents: 31443
diff changeset
171 skip_expr(&expr_end, NULL);
31396
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
172
31443
9ae3720f9bd9 patch 9.0.1054: object member can't get type from initializer
Bram Moolenaar <Bram@vim.org>
parents: 31441
diff changeset
173 if (type == NULL)
9ae3720f9bd9 patch 9.0.1054: object member can't get type from initializer
Bram Moolenaar <Bram@vim.org>
parents: 31441
diff changeset
174 {
9ae3720f9bd9 patch 9.0.1054: object member can't get type from initializer
Bram Moolenaar <Bram@vim.org>
parents: 31441
diff changeset
175 // No type specified, use the type of the initializer.
9ae3720f9bd9 patch 9.0.1054: object member can't get type from initializer
Bram Moolenaar <Bram@vim.org>
parents: 31441
diff changeset
176 typval_T tv;
9ae3720f9bd9 patch 9.0.1054: object member can't get type from initializer
Bram Moolenaar <Bram@vim.org>
parents: 31441
diff changeset
177 tv.v_type = VAR_UNKNOWN;
9ae3720f9bd9 patch 9.0.1054: object member can't get type from initializer
Bram Moolenaar <Bram@vim.org>
parents: 31441
diff changeset
178 char_u *expr = expr_start;
9ae3720f9bd9 patch 9.0.1054: object member can't get type from initializer
Bram Moolenaar <Bram@vim.org>
parents: 31441
diff changeset
179 int res = eval0(expr, &tv, eap, &evalarg);
9ae3720f9bd9 patch 9.0.1054: object member can't get type from initializer
Bram Moolenaar <Bram@vim.org>
parents: 31441
diff changeset
180
9ae3720f9bd9 patch 9.0.1054: object member can't get type from initializer
Bram Moolenaar <Bram@vim.org>
parents: 31441
diff changeset
181 if (res == OK)
9ae3720f9bd9 patch 9.0.1054: object member can't get type from initializer
Bram Moolenaar <Bram@vim.org>
parents: 31441
diff changeset
182 type = typval2type(&tv, get_copyID(), &type_list,
9ae3720f9bd9 patch 9.0.1054: object member can't get type from initializer
Bram Moolenaar <Bram@vim.org>
parents: 31441
diff changeset
183 TVTT_DO_MEMBER);
9ae3720f9bd9 patch 9.0.1054: object member can't get type from initializer
Bram Moolenaar <Bram@vim.org>
parents: 31441
diff changeset
184 if (type == NULL)
9ae3720f9bd9 patch 9.0.1054: object member can't get type from initializer
Bram Moolenaar <Bram@vim.org>
parents: 31441
diff changeset
185 {
9ae3720f9bd9 patch 9.0.1054: object member can't get type from initializer
Bram Moolenaar <Bram@vim.org>
parents: 31441
diff changeset
186 semsg(_(e_cannot_get_object_member_type_from_initializer_str),
9ae3720f9bd9 patch 9.0.1054: object member can't get type from initializer
Bram Moolenaar <Bram@vim.org>
parents: 31441
diff changeset
187 expr_start);
9ae3720f9bd9 patch 9.0.1054: object member can't get type from initializer
Bram Moolenaar <Bram@vim.org>
parents: 31441
diff changeset
188 clear_evalarg(&evalarg, NULL);
9ae3720f9bd9 patch 9.0.1054: object member can't get type from initializer
Bram Moolenaar <Bram@vim.org>
parents: 31441
diff changeset
189 break;
9ae3720f9bd9 patch 9.0.1054: object member can't get type from initializer
Bram Moolenaar <Bram@vim.org>
parents: 31441
diff changeset
190 }
9ae3720f9bd9 patch 9.0.1054: object member can't get type from initializer
Bram Moolenaar <Bram@vim.org>
parents: 31441
diff changeset
191 }
9ae3720f9bd9 patch 9.0.1054: object member can't get type from initializer
Bram Moolenaar <Bram@vim.org>
parents: 31441
diff changeset
192 clear_evalarg(&evalarg, NULL);
31424
e31fc75f6aff patch 9.0.1045: in a class object members cannot be initialized
Bram Moolenaar <Bram@vim.org>
parents: 31418
diff changeset
193 }
31443
9ae3720f9bd9 patch 9.0.1054: object member can't get type from initializer
Bram Moolenaar <Bram@vim.org>
parents: 31441
diff changeset
194 if (!valid_declaration_type(type))
9ae3720f9bd9 patch 9.0.1054: object member can't get type from initializer
Bram Moolenaar <Bram@vim.org>
parents: 31441
diff changeset
195 break;
31424
e31fc75f6aff patch 9.0.1045: in a class object members cannot be initialized
Bram Moolenaar <Bram@vim.org>
parents: 31418
diff changeset
196
31396
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
197 if (ga_grow(&objmembers, 1) == FAIL)
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
198 break;
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
199 objmember_T *m = ((objmember_T *)objmembers.ga_data)
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
200 + objmembers.ga_len;
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
201 m->om_name = vim_strnsave(varname, varname_end - varname);
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
202 m->om_type = type;
31424
e31fc75f6aff patch 9.0.1045: in a class object members cannot be initialized
Bram Moolenaar <Bram@vim.org>
parents: 31418
diff changeset
203 if (expr_end > expr_start)
e31fc75f6aff patch 9.0.1045: in a class object members cannot be initialized
Bram Moolenaar <Bram@vim.org>
parents: 31418
diff changeset
204 m->om_init = vim_strnsave(expr_start, expr_end - expr_start);
31396
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
205 ++objmembers.ga_len;
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
206 }
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
207
31416
f088f1d97eee patch 9.0.1041: cannot define a method in a class
Bram Moolenaar <Bram@vim.org>
parents: 31408
diff changeset
208 // constructors:
f088f1d97eee patch 9.0.1041: cannot define a method in a class
Bram Moolenaar <Bram@vim.org>
parents: 31408
diff changeset
209 // def new()
f088f1d97eee patch 9.0.1041: cannot define a method in a class
Bram Moolenaar <Bram@vim.org>
parents: 31408
diff changeset
210 // enddef
f088f1d97eee patch 9.0.1041: cannot define a method in a class
Bram Moolenaar <Bram@vim.org>
parents: 31408
diff changeset
211 // def newOther()
f088f1d97eee patch 9.0.1041: cannot define a method in a class
Bram Moolenaar <Bram@vim.org>
parents: 31408
diff changeset
212 // enddef
f088f1d97eee patch 9.0.1041: cannot define a method in a class
Bram Moolenaar <Bram@vim.org>
parents: 31408
diff changeset
213 // methods:
f088f1d97eee patch 9.0.1041: cannot define a method in a class
Bram Moolenaar <Bram@vim.org>
parents: 31408
diff changeset
214 // def someMethod()
f088f1d97eee patch 9.0.1041: cannot define a method in a class
Bram Moolenaar <Bram@vim.org>
parents: 31408
diff changeset
215 // enddef
f088f1d97eee patch 9.0.1041: cannot define a method in a class
Bram Moolenaar <Bram@vim.org>
parents: 31408
diff changeset
216 // TODO:
f088f1d97eee patch 9.0.1041: cannot define a method in a class
Bram Moolenaar <Bram@vim.org>
parents: 31408
diff changeset
217 // static def someMethod()
f088f1d97eee patch 9.0.1041: cannot define a method in a class
Bram Moolenaar <Bram@vim.org>
parents: 31408
diff changeset
218 // enddef
f088f1d97eee patch 9.0.1041: cannot define a method in a class
Bram Moolenaar <Bram@vim.org>
parents: 31408
diff changeset
219 // def <Tval> someMethod()
f088f1d97eee patch 9.0.1041: cannot define a method in a class
Bram Moolenaar <Bram@vim.org>
parents: 31408
diff changeset
220 // enddef
f088f1d97eee patch 9.0.1041: cannot define a method in a class
Bram Moolenaar <Bram@vim.org>
parents: 31408
diff changeset
221 // static def <Tval> someMethod()
f088f1d97eee patch 9.0.1041: cannot define a method in a class
Bram Moolenaar <Bram@vim.org>
parents: 31408
diff changeset
222 // enddef
f088f1d97eee patch 9.0.1041: cannot define a method in a class
Bram Moolenaar <Bram@vim.org>
parents: 31408
diff changeset
223 else if (checkforcmd(&p, "def", 3))
f088f1d97eee patch 9.0.1041: cannot define a method in a class
Bram Moolenaar <Bram@vim.org>
parents: 31408
diff changeset
224 {
f088f1d97eee patch 9.0.1041: cannot define a method in a class
Bram Moolenaar <Bram@vim.org>
parents: 31408
diff changeset
225 exarg_T ea;
f088f1d97eee patch 9.0.1041: cannot define a method in a class
Bram Moolenaar <Bram@vim.org>
parents: 31408
diff changeset
226 garray_T lines_to_free;
f088f1d97eee patch 9.0.1041: cannot define a method in a class
Bram Moolenaar <Bram@vim.org>
parents: 31408
diff changeset
227
f088f1d97eee patch 9.0.1041: cannot define a method in a class
Bram Moolenaar <Bram@vim.org>
parents: 31408
diff changeset
228 CLEAR_FIELD(ea);
f088f1d97eee patch 9.0.1041: cannot define a method in a class
Bram Moolenaar <Bram@vim.org>
parents: 31408
diff changeset
229 ea.cmd = line;
f088f1d97eee patch 9.0.1041: cannot define a method in a class
Bram Moolenaar <Bram@vim.org>
parents: 31408
diff changeset
230 ea.arg = p;
f088f1d97eee patch 9.0.1041: cannot define a method in a class
Bram Moolenaar <Bram@vim.org>
parents: 31408
diff changeset
231 ea.cmdidx = CMD_def;
f088f1d97eee patch 9.0.1041: cannot define a method in a class
Bram Moolenaar <Bram@vim.org>
parents: 31408
diff changeset
232 ea.getline = eap->getline;
f088f1d97eee patch 9.0.1041: cannot define a method in a class
Bram Moolenaar <Bram@vim.org>
parents: 31408
diff changeset
233 ea.cookie = eap->cookie;
f088f1d97eee patch 9.0.1041: cannot define a method in a class
Bram Moolenaar <Bram@vim.org>
parents: 31408
diff changeset
234
f088f1d97eee patch 9.0.1041: cannot define a method in a class
Bram Moolenaar <Bram@vim.org>
parents: 31408
diff changeset
235 ga_init2(&lines_to_free, sizeof(char_u *), 50);
f088f1d97eee patch 9.0.1041: cannot define a method in a class
Bram Moolenaar <Bram@vim.org>
parents: 31408
diff changeset
236 ufunc_T *uf = define_function(&ea, NULL, &lines_to_free, TRUE);
f088f1d97eee patch 9.0.1041: cannot define a method in a class
Bram Moolenaar <Bram@vim.org>
parents: 31408
diff changeset
237 ga_clear_strings(&lines_to_free);
f088f1d97eee patch 9.0.1041: cannot define a method in a class
Bram Moolenaar <Bram@vim.org>
parents: 31408
diff changeset
238
f088f1d97eee patch 9.0.1041: cannot define a method in a class
Bram Moolenaar <Bram@vim.org>
parents: 31408
diff changeset
239 // TODO: how about errors?
f088f1d97eee patch 9.0.1041: cannot define a method in a class
Bram Moolenaar <Bram@vim.org>
parents: 31408
diff changeset
240 if (uf != NULL && ga_grow(&objmethods, 1) == OK)
f088f1d97eee patch 9.0.1041: cannot define a method in a class
Bram Moolenaar <Bram@vim.org>
parents: 31408
diff changeset
241 {
31424
e31fc75f6aff patch 9.0.1045: in a class object members cannot be initialized
Bram Moolenaar <Bram@vim.org>
parents: 31418
diff changeset
242 if (STRNCMP(uf->uf_name, "new", 3) == 0)
e31fc75f6aff patch 9.0.1045: in a class object members cannot be initialized
Bram Moolenaar <Bram@vim.org>
parents: 31418
diff changeset
243 uf->uf_flags |= FC_NEW;
e31fc75f6aff patch 9.0.1045: in a class object members cannot be initialized
Bram Moolenaar <Bram@vim.org>
parents: 31418
diff changeset
244
31416
f088f1d97eee patch 9.0.1041: cannot define a method in a class
Bram Moolenaar <Bram@vim.org>
parents: 31408
diff changeset
245 ((ufunc_T **)objmethods.ga_data)[objmethods.ga_len] = uf;
f088f1d97eee patch 9.0.1041: cannot define a method in a class
Bram Moolenaar <Bram@vim.org>
parents: 31408
diff changeset
246 ++objmethods.ga_len;
f088f1d97eee patch 9.0.1041: cannot define a method in a class
Bram Moolenaar <Bram@vim.org>
parents: 31408
diff changeset
247 }
f088f1d97eee patch 9.0.1041: cannot define a method in a class
Bram Moolenaar <Bram@vim.org>
parents: 31408
diff changeset
248 }
f088f1d97eee patch 9.0.1041: cannot define a method in a class
Bram Moolenaar <Bram@vim.org>
parents: 31408
diff changeset
249
31396
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
250 else
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
251 {
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
252 semsg(_(e_not_valid_command_in_class_str), line);
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
253 break;
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
254 }
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
255 }
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
256 vim_free(theline);
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
257
31447
8cb31f7ac9e2 patch 9.0.1056: leaking memory when disassembling an object method
Bram Moolenaar <Bram@vim.org>
parents: 31443
diff changeset
258 class_T *cl = NULL;
31396
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
259 if (success)
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
260 {
31447
8cb31f7ac9e2 patch 9.0.1056: leaking memory when disassembling an object method
Bram Moolenaar <Bram@vim.org>
parents: 31443
diff changeset
261 cl = ALLOC_CLEAR_ONE(class_T);
31396
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
262 if (cl == NULL)
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
263 goto cleanup;
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
264 cl->class_refcount = 1;
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
265 cl->class_name = vim_strnsave(arg, name_end - arg);
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
266
31396
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
267 // Members are used by the new() function, add them here.
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
268 cl->class_obj_member_count = objmembers.ga_len;
31408
c82cb53474ee patch 9.0.1037: lalloc(0) error for a class without members
Bram Moolenaar <Bram@vim.org>
parents: 31404
diff changeset
269 cl->class_obj_members = objmembers.ga_len == 0 ? NULL
c82cb53474ee patch 9.0.1037: lalloc(0) error for a class without members
Bram Moolenaar <Bram@vim.org>
parents: 31404
diff changeset
270 : ALLOC_MULT(objmember_T, objmembers.ga_len);
31396
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
271 if (cl->class_name == NULL
31408
c82cb53474ee patch 9.0.1037: lalloc(0) error for a class without members
Bram Moolenaar <Bram@vim.org>
parents: 31404
diff changeset
272 || (objmembers.ga_len > 0 && cl->class_obj_members == NULL))
31396
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
273 goto cleanup;
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
274 mch_memmove(cl->class_obj_members, objmembers.ga_data,
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
275 sizeof(objmember_T) * objmembers.ga_len);
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
276 vim_free(objmembers.ga_data);
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
277
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
278 int have_new = FALSE;
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
279 for (int i = 0; i < objmethods.ga_len; ++i)
31416
f088f1d97eee patch 9.0.1041: cannot define a method in a class
Bram Moolenaar <Bram@vim.org>
parents: 31408
diff changeset
280 if (STRCMP(((ufunc_T **)objmethods.ga_data)[i]->uf_name,
31396
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
281 "new") == 0)
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
282 {
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
283 have_new = TRUE;
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
284 break;
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
285 }
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
286 if (!have_new)
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
287 {
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
288 // No new() method was defined, add the default constructor.
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
289 garray_T fga;
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
290 ga_init2(&fga, 1, 1000);
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
291 ga_concat(&fga, (char_u *)"new(");
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
292 for (int i = 0; i < cl->class_obj_member_count; ++i)
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
293 {
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
294 if (i > 0)
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
295 ga_concat(&fga, (char_u *)", ");
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
296 ga_concat(&fga, (char_u *)"this.");
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
297 objmember_T *m = cl->class_obj_members + i;
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
298 ga_concat(&fga, (char_u *)m->om_name);
31441
e572ff386670 patch 9.0.1053: default constructor arguments are not optional
Bram Moolenaar <Bram@vim.org>
parents: 31424
diff changeset
299 ga_concat(&fga, (char_u *)" = v:none");
31396
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
300 }
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
301 ga_concat(&fga, (char_u *)")\nenddef\n");
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
302 ga_append(&fga, NUL);
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
303
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
304 exarg_T fea;
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
305 CLEAR_FIELD(fea);
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
306 fea.cmdidx = CMD_def;
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
307 fea.cmd = fea.arg = fga.ga_data;
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
308
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
309 garray_T lines_to_free;
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
310 ga_init2(&lines_to_free, sizeof(char_u *), 50);
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
311
31416
f088f1d97eee patch 9.0.1041: cannot define a method in a class
Bram Moolenaar <Bram@vim.org>
parents: 31408
diff changeset
312 ufunc_T *nf = define_function(&fea, NULL, &lines_to_free, TRUE);
31396
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
313
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
314 ga_clear_strings(&lines_to_free);
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
315 vim_free(fga.ga_data);
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
316
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
317 if (nf != NULL && ga_grow(&objmethods, 1) == OK)
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
318 {
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
319 ((ufunc_T **)objmethods.ga_data)[objmethods.ga_len] = nf;
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
320 ++objmethods.ga_len;
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
321
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
322 nf->uf_flags |= FC_NEW;
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
323 nf->uf_ret_type = get_type_ptr(&type_list);
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
324 if (nf->uf_ret_type != NULL)
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
325 {
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
326 nf->uf_ret_type->tt_type = VAR_OBJECT;
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
327 nf->uf_ret_type->tt_member = (type_T *)cl;
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
328 nf->uf_ret_type->tt_argcount = 0;
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
329 nf->uf_ret_type->tt_args = NULL;
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
330 }
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
331 }
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
332 }
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
333
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
334 cl->class_obj_method_count = objmethods.ga_len;
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
335 cl->class_obj_methods = ALLOC_MULT(ufunc_T *, objmethods.ga_len);
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
336 if (cl->class_obj_methods == NULL)
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
337 goto cleanup;
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
338 mch_memmove(cl->class_obj_methods, objmethods.ga_data,
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
339 sizeof(ufunc_T *) * objmethods.ga_len);
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
340 vim_free(objmethods.ga_data);
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
341
31416
f088f1d97eee patch 9.0.1041: cannot define a method in a class
Bram Moolenaar <Bram@vim.org>
parents: 31408
diff changeset
342 // Set the class pointer on all the object methods.
f088f1d97eee patch 9.0.1041: cannot define a method in a class
Bram Moolenaar <Bram@vim.org>
parents: 31408
diff changeset
343 for (int i = 0; i < objmethods.ga_len; ++i)
f088f1d97eee patch 9.0.1041: cannot define a method in a class
Bram Moolenaar <Bram@vim.org>
parents: 31408
diff changeset
344 {
f088f1d97eee patch 9.0.1041: cannot define a method in a class
Bram Moolenaar <Bram@vim.org>
parents: 31408
diff changeset
345 ufunc_T *fp = cl->class_obj_methods[i];
f088f1d97eee patch 9.0.1041: cannot define a method in a class
Bram Moolenaar <Bram@vim.org>
parents: 31408
diff changeset
346 fp->uf_class = cl;
f088f1d97eee patch 9.0.1041: cannot define a method in a class
Bram Moolenaar <Bram@vim.org>
parents: 31408
diff changeset
347 fp->uf_flags |= FC_OBJECT; // TODO: not for class method
f088f1d97eee patch 9.0.1041: cannot define a method in a class
Bram Moolenaar <Bram@vim.org>
parents: 31408
diff changeset
348 }
f088f1d97eee patch 9.0.1041: cannot define a method in a class
Bram Moolenaar <Bram@vim.org>
parents: 31408
diff changeset
349
31396
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
350 cl->class_type.tt_type = VAR_CLASS;
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
351 cl->class_type.tt_member = (type_T *)cl;
31416
f088f1d97eee patch 9.0.1041: cannot define a method in a class
Bram Moolenaar <Bram@vim.org>
parents: 31408
diff changeset
352 cl->class_object_type.tt_type = VAR_OBJECT;
f088f1d97eee patch 9.0.1041: cannot define a method in a class
Bram Moolenaar <Bram@vim.org>
parents: 31408
diff changeset
353 cl->class_object_type.tt_member = (type_T *)cl;
31396
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
354 cl->class_type_list = type_list;
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
355
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
356 // TODO:
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
357 // - Add the methods to the class
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
358 // - array with ufunc_T pointers
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
359 // - Fill hashtab with object members and methods
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
360 // - Generate the default new() method, if needed.
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
361 // Later:
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
362 // - class members
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
363 // - class methods
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
364
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
365 // Add the class to the script-local variables.
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
366 typval_T tv;
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
367 tv.v_type = VAR_CLASS;
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
368 tv.vval.v_class = cl;
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
369 set_var_const(cl->class_name, current_sctx.sc_sid,
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
370 NULL, &tv, FALSE, ASSIGN_DECL, 0);
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
371 return;
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
372 }
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
373
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
374 cleanup:
31447
8cb31f7ac9e2 patch 9.0.1056: leaking memory when disassembling an object method
Bram Moolenaar <Bram@vim.org>
parents: 31443
diff changeset
375 if (cl != NULL)
8cb31f7ac9e2 patch 9.0.1056: leaking memory when disassembling an object method
Bram Moolenaar <Bram@vim.org>
parents: 31443
diff changeset
376 {
8cb31f7ac9e2 patch 9.0.1056: leaking memory when disassembling an object method
Bram Moolenaar <Bram@vim.org>
parents: 31443
diff changeset
377 vim_free(cl->class_name);
8cb31f7ac9e2 patch 9.0.1056: leaking memory when disassembling an object method
Bram Moolenaar <Bram@vim.org>
parents: 31443
diff changeset
378 vim_free(cl->class_obj_members);
8cb31f7ac9e2 patch 9.0.1056: leaking memory when disassembling an object method
Bram Moolenaar <Bram@vim.org>
parents: 31443
diff changeset
379 vim_free(cl->class_obj_methods);
8cb31f7ac9e2 patch 9.0.1056: leaking memory when disassembling an object method
Bram Moolenaar <Bram@vim.org>
parents: 31443
diff changeset
380 vim_free(cl);
8cb31f7ac9e2 patch 9.0.1056: leaking memory when disassembling an object method
Bram Moolenaar <Bram@vim.org>
parents: 31443
diff changeset
381 }
8cb31f7ac9e2 patch 9.0.1056: leaking memory when disassembling an object method
Bram Moolenaar <Bram@vim.org>
parents: 31443
diff changeset
382
31396
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
383 for (int i = 0; i < objmembers.ga_len; ++i)
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
384 {
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
385 objmember_T *m = ((objmember_T *)objmembers.ga_data) + i;
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
386 vim_free(m->om_name);
31424
e31fc75f6aff patch 9.0.1045: in a class object members cannot be initialized
Bram Moolenaar <Bram@vim.org>
parents: 31418
diff changeset
387 vim_free(m->om_init);
31396
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
388 }
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
389 ga_clear(&objmembers);
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
390
31416
f088f1d97eee patch 9.0.1041: cannot define a method in a class
Bram Moolenaar <Bram@vim.org>
parents: 31408
diff changeset
391 for (int i = 0; i < objmethods.ga_len; ++i)
f088f1d97eee patch 9.0.1041: cannot define a method in a class
Bram Moolenaar <Bram@vim.org>
parents: 31408
diff changeset
392 {
f088f1d97eee patch 9.0.1041: cannot define a method in a class
Bram Moolenaar <Bram@vim.org>
parents: 31408
diff changeset
393 ufunc_T *uf = ((ufunc_T **)objmethods.ga_data)[i];
f088f1d97eee patch 9.0.1041: cannot define a method in a class
Bram Moolenaar <Bram@vim.org>
parents: 31408
diff changeset
394 func_clear_free(uf, FALSE);
f088f1d97eee patch 9.0.1041: cannot define a method in a class
Bram Moolenaar <Bram@vim.org>
parents: 31408
diff changeset
395 }
31396
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
396 ga_clear(&objmethods);
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
397 clear_type_list(&type_list);
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
398 }
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
399
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
400 /*
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
401 * Find member "name" in class "cl" and return its type.
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
402 * When not found t_any is returned.
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
403 */
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
404 type_T *
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
405 class_member_type(
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
406 class_T *cl,
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
407 char_u *name,
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
408 char_u *name_end,
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
409 int *member_idx)
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
410 {
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
411 *member_idx = -1; // not found (yet)
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
412 size_t len = name_end - name;
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
413
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
414 for (int i = 0; i < cl->class_obj_member_count; ++i)
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
415 {
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
416 objmember_T *m = cl->class_obj_members + i;
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
417 if (STRNCMP(m->om_name, name, len) == 0 && m->om_name[len] == NUL)
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
418 {
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
419 *member_idx = i;
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
420 return m->om_type;
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
421 }
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
422 }
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
423 return &t_any;
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
424 }
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
425
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
426 /*
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
427 * Handle ":interface" up to ":endinterface".
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
428 */
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
429 void
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
430 ex_interface(exarg_T *eap UNUSED)
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
431 {
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
432 // TODO
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
433 }
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
434
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
435 /*
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
436 * Handle ":enum" up to ":endenum".
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
437 */
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
438 void
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
439 ex_enum(exarg_T *eap UNUSED)
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
440 {
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
441 // TODO
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
442 }
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
443
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
444 /*
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
445 * Handle ":type".
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
446 */
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
447 void
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
448 ex_type(exarg_T *eap UNUSED)
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
449 {
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
450 // TODO
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
451 }
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
452
31396
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
453 /*
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
454 * Evaluate what comes after a class:
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
455 * - class member: SomeClass.varname
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
456 * - class method: SomeClass.SomeMethod()
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
457 * - class constructor: SomeClass.new()
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
458 * - object member: someObject.varname
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
459 * - object method: someObject.SomeMethod()
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
460 *
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
461 * "*arg" points to the '.'.
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
462 * "*arg" is advanced to after the member name or method call.
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
463 *
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
464 * Returns FAIL or OK.
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
465 */
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
466 int
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
467 class_object_index(
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
468 char_u **arg,
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
469 typval_T *rettv,
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
470 evalarg_T *evalarg,
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
471 int verbose UNUSED) // give error messages
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
472 {
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
473 // int evaluate = evalarg != NULL
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
474 // && (evalarg->eval_flags & EVAL_EVALUATE);
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
475
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
476 if (VIM_ISWHITE((*arg)[1]))
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
477 {
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
478 semsg(_(e_no_white_space_allowed_after_str_str), ".", *arg);
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
479 return FAIL;
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
480 }
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
481
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
482 ++*arg;
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
483 char_u *name = *arg;
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
484 char_u *name_end = find_name_end(name, NULL, NULL, FNE_CHECK_START);
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
485 if (name_end == name)
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
486 return FAIL;
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
487 size_t len = name_end - name;
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
488
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
489 class_T *cl = rettv->v_type == VAR_CLASS ? rettv->vval.v_class
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
490 : rettv->vval.v_object->obj_class;
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
491 if (*name_end == '(')
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
492 {
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
493 for (int i = 0; i < cl->class_obj_method_count; ++i)
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
494 {
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
495 ufunc_T *fp = cl->class_obj_methods[i];
31418
462508d04341 patch 9.0.1042: ASAN gives false alarm about array access.
Bram Moolenaar <Bram@vim.org>
parents: 31416
diff changeset
496 // Use a separate pointer to avoid that ASAN complains about
462508d04341 patch 9.0.1042: ASAN gives false alarm about array access.
Bram Moolenaar <Bram@vim.org>
parents: 31416
diff changeset
497 // uf_name[] only being 4 characters.
462508d04341 patch 9.0.1042: ASAN gives false alarm about array access.
Bram Moolenaar <Bram@vim.org>
parents: 31416
diff changeset
498 char_u *ufname = (char_u *)fp->uf_name;
462508d04341 patch 9.0.1042: ASAN gives false alarm about array access.
Bram Moolenaar <Bram@vim.org>
parents: 31416
diff changeset
499 if (STRNCMP(name, ufname, len) == 0 && ufname[len] == NUL)
31396
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
500 {
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
501 typval_T argvars[MAX_FUNC_ARGS + 1];
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
502 int argcount = 0;
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
503
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
504 char_u *argp = name_end;
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
505 int ret = get_func_arguments(&argp, evalarg, 0,
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
506 argvars, &argcount);
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
507 if (ret == FAIL)
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
508 return FAIL;
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
509
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
510 funcexe_T funcexe;
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
511 CLEAR_FIELD(funcexe);
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
512 funcexe.fe_evaluate = TRUE;
31416
f088f1d97eee patch 9.0.1041: cannot define a method in a class
Bram Moolenaar <Bram@vim.org>
parents: 31408
diff changeset
513 if (rettv->v_type == VAR_OBJECT)
f088f1d97eee patch 9.0.1041: cannot define a method in a class
Bram Moolenaar <Bram@vim.org>
parents: 31408
diff changeset
514 {
f088f1d97eee patch 9.0.1041: cannot define a method in a class
Bram Moolenaar <Bram@vim.org>
parents: 31408
diff changeset
515 funcexe.fe_object = rettv->vval.v_object;
f088f1d97eee patch 9.0.1041: cannot define a method in a class
Bram Moolenaar <Bram@vim.org>
parents: 31408
diff changeset
516 ++funcexe.fe_object->obj_refcount;
f088f1d97eee patch 9.0.1041: cannot define a method in a class
Bram Moolenaar <Bram@vim.org>
parents: 31408
diff changeset
517 }
31396
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
518
31404
60b1d266548d patch 9.0.1035: object members are not being marked as used
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
519 // Clear the class or object after calling the function, in
60b1d266548d patch 9.0.1035: object members are not being marked as used
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
520 // case the refcount is one.
60b1d266548d patch 9.0.1035: object members are not being marked as used
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
521 typval_T tv_tofree = *rettv;
60b1d266548d patch 9.0.1035: object members are not being marked as used
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
522 rettv->v_type = VAR_UNKNOWN;
60b1d266548d patch 9.0.1035: object members are not being marked as used
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
523
31396
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
524 // Call the user function. Result goes into rettv;
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
525 int error = call_user_func_check(fp, argcount, argvars,
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
526 rettv, &funcexe, NULL);
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
527
31404
60b1d266548d patch 9.0.1035: object members are not being marked as used
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
528 // Clear the previous rettv and the arguments.
60b1d266548d patch 9.0.1035: object members are not being marked as used
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
529 clear_tv(&tv_tofree);
31396
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
530 for (int idx = 0; idx < argcount; ++idx)
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
531 clear_tv(&argvars[idx]);
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
532
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
533 if (error != FCERR_NONE)
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
534 {
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
535 user_func_error(error, printable_func_name(fp),
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
536 funcexe.fe_found_var);
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
537 return FAIL;
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
538 }
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
539 *arg = argp;
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
540 return OK;
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
541 }
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
542 }
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
543
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
544 semsg(_(e_method_not_found_on_class_str_str), cl->class_name, name);
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
545 }
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
546
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
547 else if (rettv->v_type == VAR_OBJECT)
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
548 {
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
549 for (int i = 0; i < cl->class_obj_member_count; ++i)
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
550 {
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
551 objmember_T *m = &cl->class_obj_members[i];
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
552 if (STRNCMP(name, m->om_name, len) == 0 && m->om_name[len] == NUL)
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
553 {
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
554 // The object only contains a pointer to the class, the member
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
555 // values array follows right after that.
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
556 object_T *obj = rettv->vval.v_object;
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
557 typval_T *tv = (typval_T *)(obj + 1) + i;
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
558 copy_tv(tv, rettv);
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
559 object_unref(obj);
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
560
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
561 *arg = name_end;
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
562 return OK;
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
563 }
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
564 }
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
565
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
566 semsg(_(e_member_not_found_on_object_str_str), cl->class_name, name);
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
567 }
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
568
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
569 // TODO: class member
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
570
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
571 return FAIL;
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
572 }
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
573
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
574 /*
31424
e31fc75f6aff patch 9.0.1045: in a class object members cannot be initialized
Bram Moolenaar <Bram@vim.org>
parents: 31418
diff changeset
575 * If "arg" points to a class or object method, return it.
e31fc75f6aff patch 9.0.1045: in a class object members cannot be initialized
Bram Moolenaar <Bram@vim.org>
parents: 31418
diff changeset
576 * Otherwise return NULL.
e31fc75f6aff patch 9.0.1045: in a class object members cannot be initialized
Bram Moolenaar <Bram@vim.org>
parents: 31418
diff changeset
577 */
e31fc75f6aff patch 9.0.1045: in a class object members cannot be initialized
Bram Moolenaar <Bram@vim.org>
parents: 31418
diff changeset
578 ufunc_T *
e31fc75f6aff patch 9.0.1045: in a class object members cannot be initialized
Bram Moolenaar <Bram@vim.org>
parents: 31418
diff changeset
579 find_class_func(char_u **arg)
e31fc75f6aff patch 9.0.1045: in a class object members cannot be initialized
Bram Moolenaar <Bram@vim.org>
parents: 31418
diff changeset
580 {
e31fc75f6aff patch 9.0.1045: in a class object members cannot be initialized
Bram Moolenaar <Bram@vim.org>
parents: 31418
diff changeset
581 char_u *name = *arg;
e31fc75f6aff patch 9.0.1045: in a class object members cannot be initialized
Bram Moolenaar <Bram@vim.org>
parents: 31418
diff changeset
582 char_u *name_end = find_name_end(name, NULL, NULL, FNE_CHECK_START);
e31fc75f6aff patch 9.0.1045: in a class object members cannot be initialized
Bram Moolenaar <Bram@vim.org>
parents: 31418
diff changeset
583 if (name_end == name || *name_end != '.')
e31fc75f6aff patch 9.0.1045: in a class object members cannot be initialized
Bram Moolenaar <Bram@vim.org>
parents: 31418
diff changeset
584 return NULL;
e31fc75f6aff patch 9.0.1045: in a class object members cannot be initialized
Bram Moolenaar <Bram@vim.org>
parents: 31418
diff changeset
585
e31fc75f6aff patch 9.0.1045: in a class object members cannot be initialized
Bram Moolenaar <Bram@vim.org>
parents: 31418
diff changeset
586 size_t len = name_end - name;
e31fc75f6aff patch 9.0.1045: in a class object members cannot be initialized
Bram Moolenaar <Bram@vim.org>
parents: 31418
diff changeset
587 typval_T tv;
e31fc75f6aff patch 9.0.1045: in a class object members cannot be initialized
Bram Moolenaar <Bram@vim.org>
parents: 31418
diff changeset
588 tv.v_type = VAR_UNKNOWN;
e31fc75f6aff patch 9.0.1045: in a class object members cannot be initialized
Bram Moolenaar <Bram@vim.org>
parents: 31418
diff changeset
589 if (eval_variable(name, len, 0, &tv, NULL, EVAL_VAR_NOAUTOLOAD) == FAIL)
e31fc75f6aff patch 9.0.1045: in a class object members cannot be initialized
Bram Moolenaar <Bram@vim.org>
parents: 31418
diff changeset
590 return NULL;
e31fc75f6aff patch 9.0.1045: in a class object members cannot be initialized
Bram Moolenaar <Bram@vim.org>
parents: 31418
diff changeset
591 if (tv.v_type != VAR_CLASS && tv.v_type != VAR_OBJECT)
31447
8cb31f7ac9e2 patch 9.0.1056: leaking memory when disassembling an object method
Bram Moolenaar <Bram@vim.org>
parents: 31443
diff changeset
592 goto fail_after_eval;
31424
e31fc75f6aff patch 9.0.1045: in a class object members cannot be initialized
Bram Moolenaar <Bram@vim.org>
parents: 31418
diff changeset
593
e31fc75f6aff patch 9.0.1045: in a class object members cannot be initialized
Bram Moolenaar <Bram@vim.org>
parents: 31418
diff changeset
594 class_T *cl = tv.v_type == VAR_CLASS ? tv.vval.v_class
e31fc75f6aff patch 9.0.1045: in a class object members cannot be initialized
Bram Moolenaar <Bram@vim.org>
parents: 31418
diff changeset
595 : tv.vval.v_object->obj_class;
e31fc75f6aff patch 9.0.1045: in a class object members cannot be initialized
Bram Moolenaar <Bram@vim.org>
parents: 31418
diff changeset
596 if (cl == NULL)
31447
8cb31f7ac9e2 patch 9.0.1056: leaking memory when disassembling an object method
Bram Moolenaar <Bram@vim.org>
parents: 31443
diff changeset
597 goto fail_after_eval;
31424
e31fc75f6aff patch 9.0.1045: in a class object members cannot be initialized
Bram Moolenaar <Bram@vim.org>
parents: 31418
diff changeset
598 char_u *fname = name_end + 1;
e31fc75f6aff patch 9.0.1045: in a class object members cannot be initialized
Bram Moolenaar <Bram@vim.org>
parents: 31418
diff changeset
599 char_u *fname_end = find_name_end(fname, NULL, NULL, FNE_CHECK_START);
e31fc75f6aff patch 9.0.1045: in a class object members cannot be initialized
Bram Moolenaar <Bram@vim.org>
parents: 31418
diff changeset
600 if (fname_end == fname)
31447
8cb31f7ac9e2 patch 9.0.1056: leaking memory when disassembling an object method
Bram Moolenaar <Bram@vim.org>
parents: 31443
diff changeset
601 goto fail_after_eval;
31424
e31fc75f6aff patch 9.0.1045: in a class object members cannot be initialized
Bram Moolenaar <Bram@vim.org>
parents: 31418
diff changeset
602 len = fname_end - fname;
e31fc75f6aff patch 9.0.1045: in a class object members cannot be initialized
Bram Moolenaar <Bram@vim.org>
parents: 31418
diff changeset
603
e31fc75f6aff patch 9.0.1045: in a class object members cannot be initialized
Bram Moolenaar <Bram@vim.org>
parents: 31418
diff changeset
604 for (int i = 0; i < cl->class_obj_method_count; ++i)
e31fc75f6aff patch 9.0.1045: in a class object members cannot be initialized
Bram Moolenaar <Bram@vim.org>
parents: 31418
diff changeset
605 {
e31fc75f6aff patch 9.0.1045: in a class object members cannot be initialized
Bram Moolenaar <Bram@vim.org>
parents: 31418
diff changeset
606 ufunc_T *fp = cl->class_obj_methods[i];
e31fc75f6aff patch 9.0.1045: in a class object members cannot be initialized
Bram Moolenaar <Bram@vim.org>
parents: 31418
diff changeset
607 // Use a separate pointer to avoid that ASAN complains about
e31fc75f6aff patch 9.0.1045: in a class object members cannot be initialized
Bram Moolenaar <Bram@vim.org>
parents: 31418
diff changeset
608 // uf_name[] only being 4 characters.
e31fc75f6aff patch 9.0.1045: in a class object members cannot be initialized
Bram Moolenaar <Bram@vim.org>
parents: 31418
diff changeset
609 char_u *ufname = (char_u *)fp->uf_name;
e31fc75f6aff patch 9.0.1045: in a class object members cannot be initialized
Bram Moolenaar <Bram@vim.org>
parents: 31418
diff changeset
610 if (STRNCMP(fname, ufname, len) == 0 && ufname[len] == NUL)
31447
8cb31f7ac9e2 patch 9.0.1056: leaking memory when disassembling an object method
Bram Moolenaar <Bram@vim.org>
parents: 31443
diff changeset
611 {
8cb31f7ac9e2 patch 9.0.1056: leaking memory when disassembling an object method
Bram Moolenaar <Bram@vim.org>
parents: 31443
diff changeset
612 clear_tv(&tv);
31424
e31fc75f6aff patch 9.0.1045: in a class object members cannot be initialized
Bram Moolenaar <Bram@vim.org>
parents: 31418
diff changeset
613 return fp;
31447
8cb31f7ac9e2 patch 9.0.1056: leaking memory when disassembling an object method
Bram Moolenaar <Bram@vim.org>
parents: 31443
diff changeset
614 }
31424
e31fc75f6aff patch 9.0.1045: in a class object members cannot be initialized
Bram Moolenaar <Bram@vim.org>
parents: 31418
diff changeset
615 }
e31fc75f6aff patch 9.0.1045: in a class object members cannot be initialized
Bram Moolenaar <Bram@vim.org>
parents: 31418
diff changeset
616
31447
8cb31f7ac9e2 patch 9.0.1056: leaking memory when disassembling an object method
Bram Moolenaar <Bram@vim.org>
parents: 31443
diff changeset
617 fail_after_eval:
8cb31f7ac9e2 patch 9.0.1056: leaking memory when disassembling an object method
Bram Moolenaar <Bram@vim.org>
parents: 31443
diff changeset
618 clear_tv(&tv);
31424
e31fc75f6aff patch 9.0.1045: in a class object members cannot be initialized
Bram Moolenaar <Bram@vim.org>
parents: 31418
diff changeset
619 return NULL;
e31fc75f6aff patch 9.0.1045: in a class object members cannot be initialized
Bram Moolenaar <Bram@vim.org>
parents: 31418
diff changeset
620 }
e31fc75f6aff patch 9.0.1045: in a class object members cannot be initialized
Bram Moolenaar <Bram@vim.org>
parents: 31418
diff changeset
621
e31fc75f6aff patch 9.0.1045: in a class object members cannot be initialized
Bram Moolenaar <Bram@vim.org>
parents: 31418
diff changeset
622 /*
31396
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
623 * Make a copy of an object.
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
624 */
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
625 void
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
626 copy_object(typval_T *from, typval_T *to)
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
627 {
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
628 *to = *from;
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
629 if (to->vval.v_object != NULL)
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
630 ++to->vval.v_object->obj_refcount;
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
631 }
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
632
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
633 /*
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
634 * Free an object.
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
635 */
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
636 static void
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
637 object_clear(object_T *obj)
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
638 {
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
639 class_T *cl = obj->obj_class;
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
640
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
641 // the member values are just after the object structure
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
642 typval_T *tv = (typval_T *)(obj + 1);
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
643 for (int i = 0; i < cl->class_obj_member_count; ++i)
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
644 clear_tv(tv + i);
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
645
31404
60b1d266548d patch 9.0.1035: object members are not being marked as used
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
646 // Remove from the list headed by "first_object".
60b1d266548d patch 9.0.1035: object members are not being marked as used
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
647 object_cleared(obj);
60b1d266548d patch 9.0.1035: object members are not being marked as used
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
648
31396
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
649 vim_free(obj);
31404
60b1d266548d patch 9.0.1035: object members are not being marked as used
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
650 class_unref(cl);
31396
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
651 }
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
652
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
653 /*
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
654 * Unreference an object.
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
655 */
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
656 void
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
657 object_unref(object_T *obj)
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
658 {
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
659 if (obj != NULL && --obj->obj_refcount <= 0)
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
660 object_clear(obj);
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
661 }
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
662
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
663 /*
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
664 * Make a copy of a class.
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
665 */
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
666 void
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
667 copy_class(typval_T *from, typval_T *to)
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
668 {
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
669 *to = *from;
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
670 if (to->vval.v_class != NULL)
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
671 ++to->vval.v_class->class_refcount;
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
672 }
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
673
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
674 /*
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
675 * Unreference a class. Free it when the reference count goes down to zero.
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
676 */
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
677 void
31404
60b1d266548d patch 9.0.1035: object members are not being marked as used
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
678 class_unref(class_T *cl)
31396
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
679 {
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
680 if (cl != NULL && --cl->class_refcount <= 0)
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
681 {
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
682 vim_free(cl->class_name);
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
683
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
684 for (int i = 0; i < cl->class_obj_member_count; ++i)
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
685 {
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
686 objmember_T *m = &cl->class_obj_members[i];
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
687 vim_free(m->om_name);
31424
e31fc75f6aff patch 9.0.1045: in a class object members cannot be initialized
Bram Moolenaar <Bram@vim.org>
parents: 31418
diff changeset
688 vim_free(m->om_init);
31396
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
689 }
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
690 vim_free(cl->class_obj_members);
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
691
31416
f088f1d97eee patch 9.0.1041: cannot define a method in a class
Bram Moolenaar <Bram@vim.org>
parents: 31408
diff changeset
692 for (int i = 0; i < cl->class_obj_method_count; ++i)
f088f1d97eee patch 9.0.1041: cannot define a method in a class
Bram Moolenaar <Bram@vim.org>
parents: 31408
diff changeset
693 {
f088f1d97eee patch 9.0.1041: cannot define a method in a class
Bram Moolenaar <Bram@vim.org>
parents: 31408
diff changeset
694 ufunc_T *uf = cl->class_obj_methods[i];
f088f1d97eee patch 9.0.1041: cannot define a method in a class
Bram Moolenaar <Bram@vim.org>
parents: 31408
diff changeset
695 func_clear_free(uf, FALSE);
f088f1d97eee patch 9.0.1041: cannot define a method in a class
Bram Moolenaar <Bram@vim.org>
parents: 31408
diff changeset
696 }
31396
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
697 vim_free(cl->class_obj_methods);
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
698
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
699 clear_type_list(&cl->class_type_list);
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
700
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
701 vim_free(cl);
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
702 }
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
703 }
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
704
31404
60b1d266548d patch 9.0.1035: object members are not being marked as used
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
705 static object_T *first_object = NULL;
60b1d266548d patch 9.0.1035: object members are not being marked as used
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
706
60b1d266548d patch 9.0.1035: object members are not being marked as used
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
707 /*
60b1d266548d patch 9.0.1035: object members are not being marked as used
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
708 * Call this function when an object has been created. It will be added to the
60b1d266548d patch 9.0.1035: object members are not being marked as used
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
709 * list headed by "first_object".
60b1d266548d patch 9.0.1035: object members are not being marked as used
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
710 */
60b1d266548d patch 9.0.1035: object members are not being marked as used
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
711 void
60b1d266548d patch 9.0.1035: object members are not being marked as used
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
712 object_created(object_T *obj)
60b1d266548d patch 9.0.1035: object members are not being marked as used
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
713 {
60b1d266548d patch 9.0.1035: object members are not being marked as used
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
714 if (first_object != NULL)
60b1d266548d patch 9.0.1035: object members are not being marked as used
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
715 {
60b1d266548d patch 9.0.1035: object members are not being marked as used
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
716 obj->obj_next_used = first_object;
60b1d266548d patch 9.0.1035: object members are not being marked as used
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
717 first_object->obj_prev_used = obj;
60b1d266548d patch 9.0.1035: object members are not being marked as used
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
718 }
60b1d266548d patch 9.0.1035: object members are not being marked as used
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
719 first_object = obj;
60b1d266548d patch 9.0.1035: object members are not being marked as used
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
720 }
60b1d266548d patch 9.0.1035: object members are not being marked as used
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
721
60b1d266548d patch 9.0.1035: object members are not being marked as used
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
722 /*
60b1d266548d patch 9.0.1035: object members are not being marked as used
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
723 * Call this function when an object has been cleared and is about to be freed.
60b1d266548d patch 9.0.1035: object members are not being marked as used
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
724 * It is removed from the list headed by "first_object".
60b1d266548d patch 9.0.1035: object members are not being marked as used
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
725 */
60b1d266548d patch 9.0.1035: object members are not being marked as used
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
726 void
60b1d266548d patch 9.0.1035: object members are not being marked as used
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
727 object_cleared(object_T *obj)
60b1d266548d patch 9.0.1035: object members are not being marked as used
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
728 {
60b1d266548d patch 9.0.1035: object members are not being marked as used
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
729 if (obj->obj_next_used != NULL)
60b1d266548d patch 9.0.1035: object members are not being marked as used
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
730 obj->obj_next_used->obj_prev_used = obj->obj_prev_used;
60b1d266548d patch 9.0.1035: object members are not being marked as used
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
731 if (obj->obj_prev_used != NULL)
60b1d266548d patch 9.0.1035: object members are not being marked as used
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
732 obj->obj_prev_used->obj_next_used = obj->obj_next_used;
60b1d266548d patch 9.0.1035: object members are not being marked as used
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
733 else if (first_object == obj)
60b1d266548d patch 9.0.1035: object members are not being marked as used
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
734 first_object = obj->obj_next_used;
60b1d266548d patch 9.0.1035: object members are not being marked as used
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
735 }
60b1d266548d patch 9.0.1035: object members are not being marked as used
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
736
60b1d266548d patch 9.0.1035: object members are not being marked as used
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
737 /*
60b1d266548d patch 9.0.1035: object members are not being marked as used
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
738 * Go through the list of all objects and free items without "copyID".
60b1d266548d patch 9.0.1035: object members are not being marked as used
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
739 */
60b1d266548d patch 9.0.1035: object members are not being marked as used
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
740 int
60b1d266548d patch 9.0.1035: object members are not being marked as used
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
741 object_free_nonref(int copyID)
60b1d266548d patch 9.0.1035: object members are not being marked as used
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
742 {
60b1d266548d patch 9.0.1035: object members are not being marked as used
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
743 int did_free = FALSE;
60b1d266548d patch 9.0.1035: object members are not being marked as used
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
744 object_T *next_obj;
60b1d266548d patch 9.0.1035: object members are not being marked as used
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
745
60b1d266548d patch 9.0.1035: object members are not being marked as used
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
746 for (object_T *obj = first_object; obj != NULL; obj = next_obj)
60b1d266548d patch 9.0.1035: object members are not being marked as used
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
747 {
60b1d266548d patch 9.0.1035: object members are not being marked as used
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
748 next_obj = obj->obj_next_used;
60b1d266548d patch 9.0.1035: object members are not being marked as used
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
749 if ((obj->obj_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
60b1d266548d patch 9.0.1035: object members are not being marked as used
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
750 {
60b1d266548d patch 9.0.1035: object members are not being marked as used
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
751 // Free the object and items it contains.
60b1d266548d patch 9.0.1035: object members are not being marked as used
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
752 object_clear(obj);
60b1d266548d patch 9.0.1035: object members are not being marked as used
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
753 did_free = TRUE;
60b1d266548d patch 9.0.1035: object members are not being marked as used
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
754 }
60b1d266548d patch 9.0.1035: object members are not being marked as used
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
755 }
60b1d266548d patch 9.0.1035: object members are not being marked as used
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
756
60b1d266548d patch 9.0.1035: object members are not being marked as used
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
757 return did_free;
60b1d266548d patch 9.0.1035: object members are not being marked as used
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
758 }
60b1d266548d patch 9.0.1035: object members are not being marked as used
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
759
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
760
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
761 #endif // FEAT_EVAL