patch 8.0.0268: may get ml_get error when :luado deletes lines

Problem:    May get ml_get error when :luado deletes lines or switches to
            another buffer. (Nikolai Pavlov, issue #1421)
Solution:   Check the buffer and line every time.
diff --git a/src/if_lua.c b/src/if_lua.c
index b77a3cd..ad8ee1b 100644
--- a/src/if_lua.c
+++ b/src/if_lua.c
@@ -1716,6 +1716,8 @@
     const char *s = (const char *) eap->arg;
     luaL_Buffer b;
     size_t len;
+    buf_T *was_curbuf = curbuf;
+
     if (lua_init() == FAIL) return;
     if (u_save(eap->line1 - 1, eap->line2 + 1) == FAIL)
     {
@@ -1739,6 +1741,10 @@
     lua_replace(L, -2); /* function -> body */
     for (l = eap->line1; l <= eap->line2; l++)
     {
+	/* Check the line number, the command my have deleted lines. */
+	if (l > curbuf->b_ml.ml_line_count)
+	    break;
+
 	lua_pushvalue(L, -1); /* function */
 	luaV_pushline(L, curbuf, l); /* current line as arg */
 	lua_pushinteger(L, l); /* current line number as arg */
@@ -1747,6 +1753,9 @@
 	    luaV_emsg(L);
 	    break;
 	}
+	/* Catch the command switching to another buffer. */
+	if (curbuf != was_curbuf)
+	    break;
 	if (lua_isstring(L, -1)) /* update line? */
 	{
 #ifdef HAVE_SANDBOX