patch 8.2.1739: Vim9: crash when compiling a manually defined function

Problem:    Vim9: crash when compiling a manually defined function. (Antony
            Scriven)
Solution:   Check that the script ID is positive. (closes #7012)
diff --git a/src/testdir/test_vim9_script.vim b/src/testdir/test_vim9_script.vim
index 57d36d9..4a4047f 100644
--- a/src/testdir/test_vim9_script.vim
+++ b/src/testdir/test_vim9_script.vim
@@ -2755,7 +2755,7 @@
     exe 'set rtp^=' .. getcwd() .. '/Xruntime'
     call crash#func()
     call writefile(['ok'], 'Xdidit')
-    qall
+    qall!
   END
   writefile(lines, 'Xscript')
   RunVim([], [], '-S Xscript')
@@ -2817,7 +2817,7 @@
 def Test_invalid_sid()
   assert_fails('func <SNR>1234_func', 'E123:')
 
-  if RunVim([], ['wq Xdidit'], '+"func <SNR>1_func"')
+  if RunVim([], ['wq! Xdidit'], '+"func <SNR>1_func"')
     assert_equal([], readfile('Xdidit'))
   endif
   delete('Xdidit')
@@ -2831,6 +2831,27 @@
   CheckDefAndScriptSuccess(lines)
 enddef
 
+def Test_define_func_at_command_line()
+  # run in a separate Vim instance to avoid the script context
+  let lines =<< trim END
+    func CheckAndQuit()
+      call assert_fails('call Afunc()', 'E117: Unknown function: Bfunc')
+      call writefile(['errors: ' .. string(v:errors)], 'Xdidcmd')
+    endfunc
+  END
+  writefile([''], 'Xdidcmd')
+  writefile(lines, 'XcallFunc')
+  let buf = RunVimInTerminal('-S XcallFunc', #{rows: 6})
+  # define Afunc() on the command line
+  term_sendkeys(buf, ":def Afunc()\<CR>Bfunc()\<CR>enddef\<CR>")
+  term_sendkeys(buf, ":call CheckAndQuit()\<CR>")
+  WaitForAssert({-> assert_equal(['errors: []'], readfile('Xdidcmd'))})
+
+  call StopVimInTerminal(buf)
+  delete('XcallFunc')
+  delete('Xdidcmd')
+enddef
+
 " Keep this last, it messes up highlighting.
 def Test_substitute_cmd()
   new
diff --git a/src/version.c b/src/version.c
index 0b3c5f5..374f5a1 100644
--- a/src/version.c
+++ b/src/version.c
@@ -751,6 +751,8 @@
 static int included_patches[] =
 {   /* Add new patch number below this line */
 /**/
+    1739,
+/**/
     1738,
 /**/
     1737,
diff --git a/src/vim9compile.c b/src/vim9compile.c
index 6ed166d..5d0ccb6 100644
--- a/src/vim9compile.c
+++ b/src/vim9compile.c
@@ -277,9 +277,12 @@
 lookup_script(char_u *name, size_t len, int vim9script)
 {
     int		    cc;
-    hashtab_T	    *ht = &SCRIPT_VARS(current_sctx.sc_sid);
+    hashtab_T	    *ht;
     dictitem_T	    *di;
 
+    if (current_sctx.sc_sid <= 0)
+	return FAIL;
+    ht = &SCRIPT_VARS(current_sctx.sc_sid);
     if (vim9script && !script_is_vim9())
 	return FAIL;
     cc = name[len];