pigpigyyy / Yuescript

A Moonscript dialect compiles to Lua.
http://yuescript.org
MIT License
452 stars 39 forks source link

Bug: yue segfaults with macros #61

Open shadmansaleh opened 3 years ago

shadmansaleh commented 3 years ago

I tried yue on android with termux

It compiles and works decently except . When the script has macros it segfaults when while compiling to lua

Backtrace:

Thread 2 "yue" received signal SIGSEGV, Segmentation fault.
[Switching to LWP 620]
__gnu_Unwind_Resume (ucbp=0x2, entry_vrs=0xf6e59dd4)
    at /buildbot/src/android/gcc/toolchain/build/../gcc/gcc-4.9/libgcc/unwind-arm-common.inc:482
482     /buildbot/src/android/gcc/toolchain/build/../gcc/gcc-4.9/libgcc/unwind-arm-common.inc: (undocumented errno 2).
(gdb) bt
#0  __gnu_Unwind_Resume (ucbp=0x2, entry_vrs=0xf6e59dd4)
    at /buildbot/src/android/gcc/toolchain/build/../gcc/gcc-4.9/libgcc/unwind-arm-common.inc:482
#1  0xf734ed00 in ___Unwind_Resume ()
   from /apex/com.android.runtime/lib/bionic/libc.so
#2  0xaadbc7ac in yue::YueCompilerImpl::isMacroChain(parserlib::ChainValue_t*) const ()
#3  0xaadb45c0 in yue::YueCompilerImpl::transformBlock(parserlib::Block_t*, std::__ndk1::list<std::__ndk1::basic_string<char, std::__ndk1::char_traits<char>, std::__ndk1::allocator<char> >, std::__ndk1::allocator<std::__ndk1::basic_string<char, std::__ndk1::char_traits<char>, std::__ndk1::allocator<char> > > >&, yue::YueCompilerImpl::ExpUsage, parserlib::ExpList_t*, bool) ()
#4  0xaadb08ac in yue::YueCompilerImpl::compile(std::__ndk1::basic_string_view<char, std::__ndk1::char_traits<char> >, yue::YueConfig const&) ()
#5  0xaadb05a0 in yue::YueCompiler::compile(std::__ndk1::basic_string_view<char, std::__ndk1::char_traits<char> >, yue::YueConfig const&) ()
#6  0xaad9e348 in std::__ndk1::__async_func<main::$_4>::operator()() ()
#7  0xaad9df24 in std::__ndk1::__async_assoc_state<std::__ndk1::tuple<int, std::__ndk1::basic_string<char, std::__ndk1::char_traits<char>, std::__ndk1::allocator<char> >, std::__ndk1::basic_string<char, std::__ndk1::char_traits<char>, std::__ndk1::allocator<char> > >, std::__ndk1::__async_func<main::$_4> >::__execute() ()
#8  0xaada0090 in void* std::__ndk1::__thread_proxy<std::__ndk1::tuple<std::__ndk1::unique_ptr<std::__ndk1::__thread_struct, std::__ndk1::default_delete<std::__ndk1::__thread_struct> >, void (std::__ndk1::__async_assoc_state<std::__ndk1::tuple<int, std::__ndk1::basic_string<char, std::__ndk1::char_traits<char>, std::__ndk1::allocator<char> >, std::__ndk1::basic_string<char, std::__ndk1::char_traits<char>, std::__ndk1::allocator<char> > >, std::__ndk1::__async_func<main::$_4> >::*)(), std::__ndk1::__async_assoc_state<std::__ndk1::tuple<int, std::__ndk1::basic_string<char, std::__ndk1::char_traits<char>, std::__ndk1::allocator<char> >, std::__ndk1::basic_string<char, std::__ndk1::char_traits<char>, std::__ndk1::allocator<char> > >, std::__ndk1::__async_func<main::$_4> >*> >(void*) ()
#9  0xf7348b84 in __pthread_start(void*) ()
   from /apex/com.android.runtime/lib/bionic/libc.so
#10 0xf7302724 in __start_thread ()
   from /apex/com.android.runtime/lib/bionic/libc.so
#11 0x00000000 in ?? ()
Backtrace stopped: previous frame identical to this frame (corrupt stack?)

(gdb) thread 1
[Switching to thread 1 (LWP 4392)]
#0  0xf72727c8 in syscall ()
   from /apex/com.android.runtime/lib/bionic/libc.so
(gdb) bt
#0  0xf72727c8 in syscall ()
   from /apex/com.android.runtime/lib/bionic/libc.so
#1  0xf7277b2a in __futex_wait_ex(void volatile*, bool, int, bool, timespec const*) () from /apex/com.android.runtime/lib/bionic/libc.so
#2  0xf72be2f0 in pthread_cond_wait ()
   from /apex/com.android.runtime/lib/bionic/libc.so
#3  0xf73b60be in std::__ndk1::condition_variable::wait(std::__ndk1::unique_lock<std::__ndk1::mutex>&) ()
   from /data/data/com.termux/files/usr/lib/libc++_shared.so
#4  0xf73b788a in std::__ndk1::__assoc_sub_state::__sub_wait(std::__ndk1::unique_lock<std::__ndk1::mutex>&) ()
   from /data/data/com.termux/files/usr/lib/libc++_shared.so
#5  0xaac103c4 in std::__ndk1::__assoc_state<std::__ndk1::tuple<int, std::__ndk1::basic_string<char, std::__ndk1::char_traits<char>, std::__ndk1::allocator<char> >, std::__ndk1::basic_string<char, std::__ndk1::char_traits<char>, std::__ndk1::allocator<char> > > >::move() ()
#6  0xaabfaebc in main ()

Any idea why ?

It was compiled with clang-12 linked to bionic libc. I tried it on arch with gcc there it worked fine . Not sure if it's an issue with clang or android .

Yue version: 0.7.15 (built with make release on main branch) lua version 5.4 (compiled by yue, Though I have lua5.1 and luajit installed on system)

Also thanks for yuescript :)

pigpigyyy commented 3 years ago

Can you post the codes with macro you've tried ? I'm using Yuescript in termux for android with archlinux and it works fine too. More info is needed to locate the problem.

shadmansaleh commented 3 years ago

You're right . I had a syntax error . I though I was running same code on arch :P. But I shouldn't crash

macro hello = ()->
  "print 'hello'"

$hello

I had ! missing after $hello on arch it reports error correctly

4: macro expression must be followed by arguments list
$hello
^

But on termux It crashes.

Since you're using on termux can you tell me how you compiled on termux ? It's possible I did something wrong while compiling .

For me:

  1. Cloned the repo
  2. removed -stdc++fs from LINK_FLAGS (as it's gcc only)
  3. ran make

Later when I was having errors I applied tmpname patch fot liblua on embaded lua source tree . But same result .

shadmansaleh commented 3 years ago

Also I you don't mind . A bit off topic but couldn't find anywere else to ask. How to do recursive macros ?

like this?

macro m1 = ()->
  ""
macro m2 = ()->
  "$m1!"

$m2!
pigpigyyy commented 3 years ago

I'm using termux with AnLinux which can help installing other Linux systems without root requirement for Android and not running any compiler directly in termux environment. And then I installed ArchLinux in termux using termux environment as a common ArchLinux system to install Yuescript with luarocks.

pigpigyyy commented 3 years ago

Syntax error issue seems not to be in the Yuescript C++ codes. I might try compiling Yuescript in termux directly someday. Yes, you are right about doing recursive macros.

macro m1 = ()-> "#{1 + 1}"
macro m2 = ()-> "$m1! + 3"
print $m2!

Will compiles to:

return print((2 + 3))

And you may join the discord group to talk about any thing related to Yuescript any time.

shadmansaleh commented 3 years ago

Thanks . I don't use discord for yet . Maybe I'll join someday . if I find any solution to or the reason of segfault I'll let you know.

I was actually trying yuescript as means to scripting in neovim . And having a wierd behaviour with macros that's why I was asking about recursve macros

I had this:

macro auc = (event, patern, cmd)->
  if patern::match('^[\'"](.*)[\'"]')
    patern = patern::match('^[\'"](.*)[\'"]')

  "vim.cmd 'autocmd #{event} #{patern} #{cmd}'"

macro au = (event, patern, cmd)->
  "$auc #{event}, #{patern}, #{cmd}"

-- $au BufEnter, '*', :echo "hi" --failed to expand macro as block: syntax error
$au BufEnter, '*', ':echo "hi"'
-- $au BufEnter, '*', 'echo "hi"' --failed to expand macro as block: syntax error
$au BufEnter, '*', echo "hi"

And it compiled to:

vim.cmd({
    ['autocmd BufEnter * '] = echo(("hi")(''))
});
return vim.cmd('autocmd BufEnter * echo "hi"')

But what I need is

return vim.cmd('autocmd BufEnter * :echo "hi"')

I just can't seem to get the : . Am I doing this wrong or is this an unwanted behavior ? Is there a way to get the output I'm looking for ? The one where it interpreted ':echo "hi"' as table totaly surprised me .

Is there a equavelent of moonc -X in yue ?

Are there some docs on yue.so ?

When you import macros where does yue look for the scripts ? Does it have a separate set of directories like package.path or package.cpath Or use same as lua?

What's a block marco and when is it allowed ? I'm getting lua macro can only be placed where block macro is allowed when running lua macro example from docs . And yeah this error also segfaults in termux . Basically any error handling macro is a segfault.

What does implicit object mean?

lots of questions :P . I'm really enjoying the language . But unlike commonly used language there's really no other place I can get help from . I'm piling them here for now . So you can answer them when you have time :) Also have joined the discord channel.