diff src/typval.c @ 32130:ec0d5bd9083c v9.0.1396

patch 9.0.1396: sort(list, 'N') does not work in Vim9 script context Commit: https://github.com/vim/vim/commit/be19d78c3d44221cbc38fbb5bac19302345c1def Author: Bram Moolenaar <Bram@vim.org> Date: Thu Mar 9 22:06:49 2023 +0000 patch 9.0.1396: sort(list, 'N') does not work in Vim9 script context Problem: sort(list, 'N') does not work in Vim9 script context. Solution: Convert string to number without giving an error. (closes https://github.com/vim/vim/issues/12061)
author Bram Moolenaar <Bram@vim.org>
date Thu, 09 Mar 2023 23:15:04 +0100
parents 39f4126d2a0d
children e4851934751a
line wrap: on
line diff
--- a/src/typval.c
+++ b/src/typval.c
@@ -188,7 +188,11 @@ init_tv(typval_T *varp)
 }
 
     static varnumber_T
-tv_get_bool_or_number_chk(typval_T *varp, int *denote, int want_bool)
+tv_get_bool_or_number_chk(
+	typval_T    *varp,
+	int	    *denote,
+	int	    want_bool,
+	int	    vim9_string_error) // in Vim9 using a string is an error
 {
     varnumber_T	n = 0L;
 
@@ -210,7 +214,7 @@ tv_get_bool_or_number_chk(typval_T *varp
 	    emsg(_(e_using_funcref_as_number));
 	    break;
 	case VAR_STRING:
-	    if (in_vim9script())
+	    if (vim9_string_error && in_vim9script())
 	    {
 		emsg_using_string_as(varp, !want_bool);
 		break;
@@ -287,10 +291,22 @@ tv_get_number(typval_T *varp)
     return tv_get_number_chk(varp, &error);	// return 0L on error
 }
 
+/*
+ * Like tv_get_numbe() but in Vim9 script do convert a number in a string to a
+ * number without giving an error.
+ */
+    varnumber_T
+tv_to_number(typval_T *varp)
+{
+    int		error = FALSE;
+
+    return tv_get_bool_or_number_chk(varp, &error, FALSE, FALSE);
+}
+
     varnumber_T
 tv_get_number_chk(typval_T *varp, int *denote)
 {
-    return tv_get_bool_or_number_chk(varp, denote, FALSE);
+    return tv_get_bool_or_number_chk(varp, denote, FALSE, TRUE);
 }
 
 /*
@@ -300,7 +316,7 @@ tv_get_number_chk(typval_T *varp, int *d
     varnumber_T
 tv_get_bool(typval_T *varp)
 {
-    return tv_get_bool_or_number_chk(varp, NULL, TRUE);
+    return tv_get_bool_or_number_chk(varp, NULL, TRUE, TRUE);
 }
 
 /*
@@ -310,7 +326,7 @@ tv_get_bool(typval_T *varp)
     varnumber_T
 tv_get_bool_chk(typval_T *varp, int *denote)
 {
-    return tv_get_bool_or_number_chk(varp, denote, TRUE);
+    return tv_get_bool_or_number_chk(varp, denote, TRUE, TRUE);
 }
 
     static float_T