@@ -814,7 +814,6 @@ static int eval_isnamec(int c);
814814static int eval_isnamec1(int c);
815815static int get_var_tv(char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload);
816816static int handle_subscript(char_u **arg, typval_T *rettv, int evaluate, int verbose);
817- static typval_T *alloc_tv(void);
818817static typval_T *alloc_string_tv(char_u *string);
819818static void init_tv(typval_T *varp);
820819static long get_tv_number(typval_T *varp);
@@ -10065,6 +10064,9 @@ f_connect(typval_T *argvars, typval_T *rettv)
1006510064 int port;
1006610065 int json_mode = FALSE;
1006710066
10067+ /* default: fail */
10068+ rettv->vval.v_number = -1;
10069+
1006810070 address = get_tv_string(&argvars[0]);
1006910071 mode = get_tv_string_buf(&argvars[1], buf1);
1007010072 if (argvars[2].v_type != VAR_UNKNOWN)
@@ -10366,7 +10368,7 @@ get_channel_arg(typval_T *tv)
1036610368
1036710369 if (!channel_is_open(ch_idx))
1036810370 {
10369- EMSGN(_("E999 : not an open channel"), ch_idx);
10371+ EMSGN(_("E906 : not an open channel"), ch_idx);
1037010372 return -1;
1037110373 }
1037210374 return ch_idx;
@@ -14100,7 +14102,8 @@ f_jsondecode(typval_T *argvars, typval_T *rettv)
1410014102 reader.js_buf = get_tv_string(&argvars[0]);
1410114103 reader.js_eof = TRUE;
1410214104 reader.js_used = 0;
14103- json_decode(&reader, rettv);
14105+ if (json_decode(&reader, rettv) == FAIL)
14106+ EMSG(_(e_invarg));
1410414107}
1410514108
1410614109/*
@@ -16896,29 +16899,37 @@ send_common(typval_T *argvars, char_u *text, char *fun)
1689616899f_sendexpr(typval_T *argvars, typval_T *rettv)
1689716900{
1689816901 char_u *text;
16899- char_u *resp;
16900- typval_T typetv;
16902+ typval_T *listtv;
1690116903 int ch_idx;
16904+ int id;
1690216905
1690316906 /* return an empty string by default */
1690416907 rettv->v_type = VAR_STRING;
1690516908 rettv->vval.v_string = NULL;
1690616909
16907- text = json_encode_nr_expr(channel_get_id(), &argvars[1]);
16910+ id = channel_get_id();
16911+ text = json_encode_nr_expr(id, &argvars[1]);
1690816912 if (text == NULL)
1690916913 return;
1691016914
1691116915 ch_idx = send_common(argvars, text, "sendexpr");
1691216916 if (ch_idx >= 0)
1691316917 {
16914- /* TODO: read until the whole JSON message is received */
16915- /* TODO: only use the message with the right message ID */
16916- /* TODO: check sequence number */
16917- resp = channel_read_block(ch_idx);
16918- if (resp != NULL)
16918+ if (channel_read_json_block(ch_idx, id, &listtv) == OK)
1691916919 {
16920- channel_decode_json(resp, &typetv, rettv, NULL);
16921- vim_free(resp);
16920+ if (listtv->v_type == VAR_LIST)
16921+ {
16922+ list_T *list = listtv->vval.v_list;
16923+
16924+ if (list->lv_len == 2)
16925+ {
16926+ /* Move the item from the list and then change the type to
16927+ * avoid the value being freed. */
16928+ *rettv = list->lv_last->li_tv;
16929+ list->lv_last->li_tv.v_type = VAR_NUMBER;
16930+ }
16931+ }
16932+ clear_tv(listtv);
1692216933 }
1692316934 }
1692416935}
@@ -20923,7 +20934,7 @@ handle_subscript(
2092320934 * Allocate memory for a variable type-value, and make it empty (0 or NULL
2092420935 * value).
2092520936 */
20926- static typval_T *
20937+ typval_T *
2092720938alloc_tv(void)
2092820939{
2092920940 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
0 commit comments