mirror of
				https://github.com/neovim/neovim.git
				synced 2025-11-04 01:34:25 +00:00 
			
		
		
		
	feat(eval/method): partially port v8.1.1954
Does not include listener_*() functions. js_*() functions are N/A. json_encode() and json_decode() didn't include tests; add some anyway (to json_functions_spec.lua). test_lua.vim isn't included yet, so add tests to luaeval_spec.lua.
This commit is contained in:
		@@ -1204,7 +1204,7 @@ next method: >
 | 
			
		||||
	mylist->filter(filterexpr)->map(mapexpr)->sort()->join()
 | 
			
		||||
<
 | 
			
		||||
Example of using a lambda: >
 | 
			
		||||
	GetPercentage->{x -> x * 100}()->printf('%d%%')
 | 
			
		||||
	GetPercentage()->{x -> x * 100}()->printf('%d%%')
 | 
			
		||||
<
 | 
			
		||||
When using -> the |expr7| operators will be applied first, thus: >
 | 
			
		||||
	-1.234->string()
 | 
			
		||||
@@ -6384,6 +6384,9 @@ json_decode({expr})					*json_decode()*
 | 
			
		||||
		recommended and the only one required to be supported.
 | 
			
		||||
		Non-UTF-8 characters are an error.
 | 
			
		||||
 | 
			
		||||
		Can also be used as a |method|: >
 | 
			
		||||
			ReadObject()->json_decode()
 | 
			
		||||
 | 
			
		||||
json_encode({expr})					*json_encode()*
 | 
			
		||||
		Convert {expr} into a JSON string.  Accepts
 | 
			
		||||
		|msgpack-special-dict| as the input.  Will not convert
 | 
			
		||||
@@ -6396,6 +6399,9 @@ json_encode({expr})					*json_encode()*
 | 
			
		||||
		or special escapes like "\t", other are dumped as-is.
 | 
			
		||||
		|Blob|s are converted to arrays of the individual bytes.
 | 
			
		||||
 | 
			
		||||
		Can also be used as a |method|: >
 | 
			
		||||
			GetObject()->json_encode()
 | 
			
		||||
 | 
			
		||||
keys({dict})						*keys()*
 | 
			
		||||
		Return a |List| with all the keys of {dict}.  The |List| is in
 | 
			
		||||
		arbitrary order.  Also see |items()| and |values()|.
 | 
			
		||||
@@ -6459,6 +6465,10 @@ libcall({libname}, {funcname}, {argument})
 | 
			
		||||
		object code must be compiled as position-independent ('PIC').
 | 
			
		||||
		Examples: >
 | 
			
		||||
			:echo libcall("libc.so", "getenv", "HOME")
 | 
			
		||||
 | 
			
		||||
<		Can also be used as a |method|, where the base is passed as
 | 
			
		||||
		the argument to the called function: >
 | 
			
		||||
			GetValue()->libcall("libc.so", "getenv")
 | 
			
		||||
<
 | 
			
		||||
							*libcallnr()*
 | 
			
		||||
libcallnr({libname}, {funcname}, {argument})
 | 
			
		||||
@@ -6468,6 +6478,10 @@ libcallnr({libname}, {funcname}, {argument})
 | 
			
		||||
			:echo libcallnr("/usr/lib/libc.so", "getpid", "")
 | 
			
		||||
			:call libcallnr("libc.so", "printf", "Hello World!\n")
 | 
			
		||||
			:call libcallnr("libc.so", "sleep", 10)
 | 
			
		||||
<
 | 
			
		||||
		Can also be used as a |method|, where the base is passed as
 | 
			
		||||
		the argument to the called function: >
 | 
			
		||||
			GetValue()->libcallnr("libc.so", "printf")
 | 
			
		||||
<
 | 
			
		||||
							*line()*
 | 
			
		||||
line({expr})	The result is a Number, which is the line number of the file
 | 
			
		||||
@@ -6493,6 +6507,9 @@ line({expr})	The result is a Number, which is the line number of the file
 | 
			
		||||
			line(".")		line number of the cursor
 | 
			
		||||
			line("'t")		line number of mark t
 | 
			
		||||
			line("'" . marker)	line number of mark marker
 | 
			
		||||
<
 | 
			
		||||
		Can also be used as a |method|: >
 | 
			
		||||
			GetValue()->line()
 | 
			
		||||
 | 
			
		||||
line2byte({lnum})					*line2byte()*
 | 
			
		||||
		Return the byte count from the start of the buffer for line
 | 
			
		||||
@@ -6507,6 +6524,9 @@ line2byte({lnum})					*line2byte()*
 | 
			
		||||
		|getline()|.  When {lnum} is invalid -1 is returned.
 | 
			
		||||
		Also see |byte2line()|, |go| and |:goto|.
 | 
			
		||||
 | 
			
		||||
		Can also be used as a |method|: >
 | 
			
		||||
			GetLnum()->line2byte()
 | 
			
		||||
 | 
			
		||||
lispindent({lnum})					*lispindent()*
 | 
			
		||||
		Get the amount of indent for line {lnum} according the lisp
 | 
			
		||||
		indenting rules, as with 'lisp'.
 | 
			
		||||
@@ -6514,6 +6534,9 @@ lispindent({lnum})					*lispindent()*
 | 
			
		||||
		relevant.  {lnum} is used just like in |getline()|.
 | 
			
		||||
		When {lnum} is invalid, -1 is returned.
 | 
			
		||||
 | 
			
		||||
		Can also be used as a |method|: >
 | 
			
		||||
			GetLnum()->lispindent()
 | 
			
		||||
 | 
			
		||||
list2str({list} [, {utf8}])				*list2str()*
 | 
			
		||||
		Convert each number in {list} to a character string can
 | 
			
		||||
		concatenate them all.  Examples: >
 | 
			
		||||
@@ -6528,6 +6551,9 @@ list2str({list} [, {utf8}])				*list2str()*
 | 
			
		||||
		With utf-8 composing characters work as expected: >
 | 
			
		||||
			list2str([97, 769])	returns "á"
 | 
			
		||||
<
 | 
			
		||||
		Can also be used as a |method|: >
 | 
			
		||||
			GetList()->list2str()
 | 
			
		||||
 | 
			
		||||
localtime()						*localtime()*
 | 
			
		||||
		Return the current time, measured as seconds since 1st Jan
 | 
			
		||||
		1970.  See also |strftime()|, |strptime()| and |getftime()|.
 | 
			
		||||
@@ -6562,6 +6588,9 @@ luaeval({expr}[, {expr}])
 | 
			
		||||
		Evaluate Lua expression {expr} and return its result converted
 | 
			
		||||
		to Vim data structures. See |lua-eval| for more details.
 | 
			
		||||
 | 
			
		||||
		Can also be used as a |method|: >
 | 
			
		||||
			GetExpr()->luaeval()
 | 
			
		||||
 | 
			
		||||
map({expr1}, {expr2})					*map()*
 | 
			
		||||
		{expr1} must be a |List|, |Blob| or |Dictionary|.
 | 
			
		||||
		Replace each item in {expr1} with the result of evaluating
 | 
			
		||||
 
 | 
			
		||||
@@ -220,21 +220,21 @@ return {
 | 
			
		||||
    jobstop={args=1},
 | 
			
		||||
    jobwait={args={1, 2}},
 | 
			
		||||
    join={args={1, 2}, base=1},
 | 
			
		||||
    json_decode={args=1},
 | 
			
		||||
    json_encode={args=1},
 | 
			
		||||
    json_decode={args=1, base=1},
 | 
			
		||||
    json_encode={args=1, base=1},
 | 
			
		||||
    keys={args=1, base=1},
 | 
			
		||||
    last_buffer_nr={},  -- obsolete
 | 
			
		||||
    len={args=1, base=1},
 | 
			
		||||
    libcall={args=3},
 | 
			
		||||
    libcallnr={args=3},
 | 
			
		||||
    line={args={1, 2}},
 | 
			
		||||
    line2byte={args=1},
 | 
			
		||||
    lispindent={args=1},
 | 
			
		||||
    list2str={args={1, 2}},
 | 
			
		||||
    libcall={args=3, base=3},
 | 
			
		||||
    libcallnr={args=3, base=3},
 | 
			
		||||
    line={args={1, 2}, base=1},
 | 
			
		||||
    line2byte={args=1, base=1},
 | 
			
		||||
    lispindent={args=1, base=1},
 | 
			
		||||
    list2str={args={1, 2}, base=1},
 | 
			
		||||
    localtime={},
 | 
			
		||||
    log={args=1, base=1, func="float_op_wrapper", data="&log"},
 | 
			
		||||
    log10={args=1, base=1, func="float_op_wrapper", data="&log10"},
 | 
			
		||||
    luaeval={args={1, 2}},
 | 
			
		||||
    luaeval={args={1, 2}, base=1},
 | 
			
		||||
    map={args=2, base=1},
 | 
			
		||||
    maparg={args={1, 4}},
 | 
			
		||||
    mapcheck={args={1, 3}},
 | 
			
		||||
 
 | 
			
		||||
@@ -88,7 +88,7 @@ func Test_argadd_empty_curbuf()
 | 
			
		||||
  argadd Xargadd
 | 
			
		||||
  call assert_equal(curbuf, bufnr('%'))
 | 
			
		||||
  call assert_equal('', bufname('%'))
 | 
			
		||||
  call assert_equal(1, line('$'))
 | 
			
		||||
  call assert_equal(1, '$'->line())
 | 
			
		||||
  rew
 | 
			
		||||
  call assert_notequal(curbuf, '%'->bufnr())
 | 
			
		||||
  call assert_equal('Xargadd', '%'->bufname())
 | 
			
		||||
 
 | 
			
		||||
@@ -905,7 +905,7 @@ func Test_byte2line_line2byte()
 | 
			
		||||
  call assert_equal([-1, -1, 1, 1, 2, 2, 2, 3, 3, -1],
 | 
			
		||||
  \                 map(range(-1, 8), 'v:val->byte2line()'))
 | 
			
		||||
  call assert_equal([-1, -1, 1, 3, 6, 8, -1],
 | 
			
		||||
  \                 map(range(-1, 5), 'line2byte(v:val)'))
 | 
			
		||||
  \                 map(range(-1, 5), 'v:val->line2byte()'))
 | 
			
		||||
 | 
			
		||||
  set fileformat=dos
 | 
			
		||||
  call assert_equal([-1, -1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, -1],
 | 
			
		||||
@@ -1505,17 +1505,17 @@ func Test_libcall_libcallnr()
 | 
			
		||||
  endif
 | 
			
		||||
 | 
			
		||||
  if has('win32')
 | 
			
		||||
    call assert_equal($USERPROFILE, libcall(libc, 'getenv', 'USERPROFILE'))
 | 
			
		||||
    call assert_equal($USERPROFILE, 'USERPROFILE'->libcall(libc, 'getenv'))
 | 
			
		||||
  else
 | 
			
		||||
    call assert_equal($HOME, libcall(libc, 'getenv', 'HOME'))
 | 
			
		||||
    call assert_equal($HOME, 'HOME'->libcall(libc, 'getenv'))
 | 
			
		||||
  endif
 | 
			
		||||
 | 
			
		||||
  " If function returns NULL, libcall() should return an empty string.
 | 
			
		||||
  call assert_equal('', libcall(libc, 'getenv', 'X_ENV_DOES_NOT_EXIT'))
 | 
			
		||||
 | 
			
		||||
  " Test libcallnr() with string and integer argument.
 | 
			
		||||
  call assert_equal(4, libcallnr(libc, 'strlen', 'abcd'))
 | 
			
		||||
  call assert_equal(char2nr('A'), libcallnr(libc, 'toupper', char2nr('a')))
 | 
			
		||||
  call assert_equal(4, 'abcd'->libcallnr(libc, 'strlen'))
 | 
			
		||||
  call assert_equal(char2nr('A'), char2nr('a')->libcallnr(libc, 'toupper'))
 | 
			
		||||
 | 
			
		||||
  call assert_fails("call libcall(libc, 'Xdoesnotexist_', '')", 'E364:')
 | 
			
		||||
  call assert_fails("call libcallnr(libc, 'Xdoesnotexist_', '')", 'E364:')
 | 
			
		||||
 
 | 
			
		||||
@@ -43,6 +43,9 @@ func Test_lisp_indent()
 | 
			
		||||
	      \ ',@body',
 | 
			
		||||
	      \ '(princ "</a>")))'
 | 
			
		||||
	      \ ])
 | 
			
		||||
  call assert_equal(7, lispindent(2))
 | 
			
		||||
  call assert_equal(5, 6->lispindent())
 | 
			
		||||
 | 
			
		||||
  set lisp
 | 
			
		||||
  set lispwords&
 | 
			
		||||
  let save_copt = &cpoptions
 | 
			
		||||
 
 | 
			
		||||
@@ -111,7 +111,7 @@ func Test_list2str_str2list_utf8()
 | 
			
		||||
  let s = "\u304b\u3099\u3044"
 | 
			
		||||
  let l = [0x304b, 0x3099, 0x3044]
 | 
			
		||||
  call assert_equal(l, str2list(s, 1))
 | 
			
		||||
  call assert_equal(s, list2str(l, 1))
 | 
			
		||||
  call assert_equal(s, l->list2str(1))
 | 
			
		||||
  if &enc ==# 'utf-8'
 | 
			
		||||
    call assert_equal(str2list(s), str2list(s, 1))
 | 
			
		||||
    call assert_equal(list2str(l), list2str(l, 1))
 | 
			
		||||
 
 | 
			
		||||
@@ -86,14 +86,15 @@ describe('luaeval()', function()
 | 
			
		||||
  -- meaningful later.
 | 
			
		||||
 | 
			
		||||
  it('correctly evaluates scalars', function()
 | 
			
		||||
    -- Also test method call (->) syntax
 | 
			
		||||
    eq(1, funcs.luaeval('1'))
 | 
			
		||||
    eq(0, eval('type(luaeval("1"))'))
 | 
			
		||||
    eq(0, eval('"1"->luaeval()->type()'))
 | 
			
		||||
 | 
			
		||||
    eq(1.5, funcs.luaeval('1.5'))
 | 
			
		||||
    eq(5, eval('type(luaeval("1.5"))'))
 | 
			
		||||
    eq(5, eval('"1.5"->luaeval()->type()'))
 | 
			
		||||
 | 
			
		||||
    eq("test", funcs.luaeval('"test"'))
 | 
			
		||||
    eq(1, eval('type(luaeval("\'test\'"))'))
 | 
			
		||||
    eq(1, eval('"\'test\'"->luaeval()->type()'))
 | 
			
		||||
 | 
			
		||||
    eq('', funcs.luaeval('""'))
 | 
			
		||||
    eq('\000', funcs.luaeval([['\0']]))
 | 
			
		||||
 
 | 
			
		||||
@@ -168,7 +168,8 @@ describe('json_decode() function', function()
 | 
			
		||||
  end)
 | 
			
		||||
 | 
			
		||||
  it('parses floating-point numbers', function()
 | 
			
		||||
    eq('100000.0', eval('string(json_decode("100000.0"))'))
 | 
			
		||||
    -- Also test method call (->) syntax
 | 
			
		||||
    eq('100000.0', eval('"100000.0"->json_decode()->string()'))
 | 
			
		||||
    eq(100000.5, funcs.json_decode('100000.5'))
 | 
			
		||||
    eq(-100000.5, funcs.json_decode('-100000.5'))
 | 
			
		||||
    eq(-100000.5e50, funcs.json_decode('-100000.5e50'))
 | 
			
		||||
@@ -549,11 +550,12 @@ describe('json_encode() function', function()
 | 
			
		||||
  end)
 | 
			
		||||
 | 
			
		||||
  it('dumps floats', function()
 | 
			
		||||
    eq('0.0', eval('json_encode(0.0)'))
 | 
			
		||||
    -- Also test method call (->) syntax
 | 
			
		||||
    eq('0.0', eval('0.0->json_encode()'))
 | 
			
		||||
    eq('10.5', funcs.json_encode(10.5))
 | 
			
		||||
    eq('-10.5', funcs.json_encode(-10.5))
 | 
			
		||||
    eq('-1.0e-5', funcs.json_encode(-1e-5))
 | 
			
		||||
    eq('1.0e50', eval('json_encode(1.0e50)'))
 | 
			
		||||
    eq('1.0e50', eval('1.0e50->json_encode()'))
 | 
			
		||||
  end)
 | 
			
		||||
 | 
			
		||||
  it('fails to dump NaN and infinite values', function()
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user