summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBram Moolenaar <Bram@vim.org>2023-01-03 14:01:21 +0000
committerBram Moolenaar <Bram@vim.org>2023-01-03 14:01:21 +0000
commit46ab925937d04c208d905cfb50bd8ffcae11e466 (patch)
treea53164b0853374c62ccea3afb4b07314d8fad66c
parent36818a9daafbcb8e3b06be7b07f52b2d00a61746 (diff)
patch 9.0.1139: cannot create a new object in a compiled functionv9.0.1139
Problem: Cannot create a new object in a compiled function. Solution: Compile the instructins to create a new object.
-rw-r--r--src/testdir/test_vim9_class.vim10
-rw-r--r--src/version.c2
-rw-r--r--src/vim9expr.c43
-rw-r--r--src/vim9type.c5
4 files changed, 52 insertions, 8 deletions
diff --git a/src/testdir/test_vim9_class.vim b/src/testdir/test_vim9_class.vim
index b75e58c331..81076f8d32 100644
--- a/src/testdir/test_vim9_class.vim
+++ b/src/testdir/test_vim9_class.vim
@@ -394,9 +394,8 @@ def Test_class_object_compare()
END
v9.CheckScriptSuccess(class_lines + test_lines)
- # TODO: this does not work yet
- #v9.CheckScriptSuccess(
- # class_lines + ['def Test()'] + test_lines + ['enddef', 'Test()'])
+ v9.CheckScriptSuccess(
+ class_lines + ['def Test()'] + test_lines + ['enddef', 'Test()'])
for op in ['>', '>=', '<', '<=', '=~', '!~']
var op_lines = [
@@ -405,9 +404,8 @@ def Test_class_object_compare()
'echo i1 ' .. op .. ' i2',
]
v9.CheckScriptFailure(class_lines + op_lines, 'E1153: Invalid operation for object')
- # TODO: this does not work yet
- #v9.CheckScriptFailure(class_lines
- # + ['def Test()'] + op_lines + ['enddef', 'Test()'], 'E99:')
+ v9.CheckScriptFailure(class_lines
+ + ['def Test()'] + op_lines + ['enddef', 'Test()'], 'E1153: Invalid operation for object')
endfor
enddef
diff --git a/src/version.c b/src/version.c
index 485512192d..9e06cd4009 100644
--- a/src/version.c
+++ b/src/version.c
@@ -696,6 +696,8 @@ static char *(features[]) =
static int included_patches[] =
{ /* Add new patch number below this line */
/**/
+ 1139,
+/**/
1138,
/**/
1137,
diff --git a/src/vim9expr.c b/src/vim9expr.c
index c5897faf8b..5a6b110832 100644
--- a/src/vim9expr.c
+++ b/src/vim9expr.c
@@ -273,8 +273,47 @@ compile_class_object_index(cctx_T *cctx, char_u **arg, type_T *type)
class_T *cl = (class_T *)type->tt_member;
if (*name_end == '(')
{
- // TODO: method or function call
- emsg("compile_class_object_index(): object/class call not handled yet");
+ if (type->tt_type == VAR_CLASS)
+ {
+ garray_T *instr = &cctx->ctx_instr;
+ if (instr->ga_len > 0)
+ {
+ isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
+ if (isn->isn_type == ISN_LOADSCRIPT)
+ {
+ // The class was recognized as a script item. We only need
+ // to know what class it is, drop the instruction.
+ --instr->ga_len;
+ vim_free(isn->isn_arg.script.scriptref);
+ }
+ }
+
+ for (int i = 0; i < cl->class_class_function_count; ++i)
+ {
+ ufunc_T *fp = cl->class_class_functions[i];
+ // Use a separate pointer to avoid that ASAN complains about
+ // uf_name[] only being 4 characters.
+ char_u *ufname = (char_u *)fp->uf_name;
+ if (STRNCMP(name, ufname, len) == 0 && ufname[len] == NUL)
+ {
+ *arg = skipwhite(name_end + 1);
+ int argcount = 0;
+ if (compile_arguments(arg, cctx, &argcount,
+ CA_NOT_SPECIAL) == FAIL)
+ return FAIL;
+ return generate_CALL(cctx, fp, argcount);
+ }
+ }
+
+ semsg(_(e_method_not_found_on_class_str_str),
+ cl->class_name, name);
+ return FAIL;
+ }
+ else
+ {
+ // TODO: method call
+ emsg("compile_class_object_index(): object call not handled yet");
+ }
}
else if (type->tt_type == VAR_OBJECT)
{
diff --git a/src/vim9type.c b/src/vim9type.c
index 8ce5948163..c85c3f6c7d 100644
--- a/src/vim9type.c
+++ b/src/vim9type.c
@@ -581,6 +581,11 @@ typval2type_int(typval_T *tv, int copyID, garray_T *type_gap, int flags)
}
}
+ if (tv->v_type == VAR_CLASS)
+ member_type = (type_T *)tv->vval.v_class;
+ else if (tv->v_type == VAR_OBJECT && tv->vval.v_object != NULL)
+ member_type = (type_T *)tv->vval.v_object->obj_class;
+
type = get_type_ptr(type_gap);
if (type == NULL)
return NULL;