Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

repl wanted #79

Closed
ghost opened this issue May 1, 2017 · 26 comments
Closed

repl wanted #79

ghost opened this issue May 1, 2017 · 26 comments

Comments

@ghost
Copy link

ghost commented May 1, 2017

Feature Request

repl needed to write & test script more easily

That is, each time I wanna do something, for example have a look at path.join(), I have to write a simple statement to a file then use xmake lua to run it. That's troublesome. A repl like other script languages have is good-to-use

I tried to impl it by myself but it has trouble with sandbox

@waruqi
Copy link
Member

waruqi commented May 1, 2017

This is a very nice feature, I also wanted to add this feature before. I saw the implementation of luajit.c, It seems not very complicated, I will attempt to implement it in this week if everything goes well.

@ghost
Copy link
Author

ghost commented May 1, 2017

👍

@waruqi
Copy link
Member

waruqi commented May 2, 2017

I have finished this feature on linux/macosx. (not yet tested it on windows)

You can fetch dev branch and run xmake lua to run interactive commands. for example:

$ xmake lua

> import("core.project.task")
> task.run("hello")
>
> print(path.join("/tmp", "xxx"))
>
> = 1+2+3 / 4
>
> a = 1
> print(a)
>
> for _, v in pairs({1, 2, 3}) do
>> print(v)
>> end

@ghost
Copy link
Author

ghost commented May 2, 2017

> 1
stdin:1: unexpected symbol near '1'
> a=1
> a
>> 

I think a single val should be printed directly

like python:

>>> 1
1
>>> a=1
>>> a
1

@ghost
Copy link
Author

ghost commented May 2, 2017

Anyway 👍

@waruqi
Copy link
Member

waruqi commented May 3, 2017

@titansnow I agree with your point, but I don't know how to distinguish them better in lua.

So you need add prefix character '=' to eval expr now.

> = 1 + 3 +5
9
> = 1
1

Or you can help me improve it. interactive.c:168

@ghost
Copy link
Author

ghost commented May 3, 2017

I'd like to look into it 😃

@ghost
Copy link
Author

ghost commented May 3, 2017

I found some info on stackoverflow that might be workable

@ghost
Copy link
Author

ghost commented May 3, 2017

lua 5.3 uses pre-check to know whether a statement could return

  const char *retline = lua_pushfstring(L, "return %s;", line);
  int status = luaL_loadbuffer(L, retline, strlen(retline), "=stdin");
  if (status == LUA_OK) {
    // ...

at lua.c:334 (5.3.4)

if could return then add return otherwise not add return

@waruqi
Copy link
Member

waruqi commented May 3, 2017

@titansnow I tried this way before, but this method can't handle some function calls well. for example:

> import("core.project.task")
> task.run("xxx")  <------ will failed, task not found

These function calls can also be returned, but return import("...") cannot affect the scope of the outside

@ghost
Copy link
Author

ghost commented May 3, 2017

@waruqi That's too bad 😞 I couldn't figure out why it will be so -- return break scope? Maybe lua is a magic language that makes me 🌀

@waruqi
Copy link
Member

waruqi commented May 3, 2017

@titansnow And it's hard to distinguish whether you want to print the return value. Any function call that has a return value will be printed

for example:

function test()
    -- do some thing

    -- ok
    return true
end

then, we run it using return test()

> test()
true      -- maybe I don't want to see this return value

@ghost
Copy link
Author

ghost commented May 3, 2017

I think now there are two thing needed to be done:

  • in multiline input, I need a cancel way. <C-C>?

  • the readline library itself is common in posix system but the headers might be not. On my ubuntu, unless libreadline-dev is installed, there's no header readline/*

    there are two solutions:

    • add require of readline development files
    • do not include the header. Declare them manually:
      diff --git a/core/src/xmake/sandbox/interactive.c b/core/src/xmake/sandbox/interactive.c
      index 339ab9cc..da3c7a8e 100644
      --- a/core/src/xmake/sandbox/interactive.c
      +++ b/core/src/xmake/sandbox/interactive.c
      @@ -41,8 +41,8 @@
        */
       #include "prefix.h"
       #ifndef TB_CONFIG_OS_WINDOWS
      -#   include <readline/readline.h>
      -#   include <readline/history.h>
      +    extern char* readline   (const char*);
      +    extern void  add_history(const char*);
       #endif
      
       /* //////////////////////////////////////////////////////////////////////////////////////

@waruqi
Copy link
Member

waruqi commented May 4, 2017

@titansnow It try loading return expr first now. You can try it.

> 1 + 2
3

> a = 1
> a
1

But you need assign var manually when call import now.

> task = import("core.project.task")
> task.run("hello")

@waruqi
Copy link
Member

waruqi commented May 4, 2017

@titansnow

  • in multiline input, I need a cancel way. ?

Use the signal to cancel multiline, this way is too much trouble and can not handle the cross-platform well.

So you can simply input q to cancel multiline input, for example:

> for _, v in ipairs({1, 2}) do
>> print(v)
>> q             <--  cancel multiline and clear previous input
> 1 + 2
3
  • the readline library itself is common in posix system but the headers might be not. On my ubuntu, unless libreadline-dev is installed, there's no header readline/*

I fixed it

+    extern char* readline   (const char*);
+    extern void  add_history(const char*);

@ghost
Copy link
Author

ghost commented May 4, 2017

👍

@ghost
Copy link
Author

ghost commented May 6, 2017

A very bad message: I tested carefully again, found that although libreadline has installed, xmake couldn't build successfully even declare functions manually, libreadline-dev is required. And libreadline-dev is not one of build-essential package group. So that install scripts must be modified to add this requirement @waruqi

@waruqi
Copy link
Member

waruqi commented May 6, 2017

@titansnow But it passed on ci.Can you give me some build error info? I try to fix it and detect libreadline . If this library not found, I will continue to uses fgets when building.

@ghost
Copy link
Author

ghost commented May 6, 2017

Travis CI

@ghost
Copy link
Author

ghost commented May 6, 2017

Three solutions:

  • fallback to fgets (reduce user experience)
  • install this library via package manager if not found (troublesome)
  • bundle readline (impossible because license)

@waruqi
Copy link
Member

waruqi commented May 6, 2017

ok, I will fix it in recent days.

@waruqi
Copy link
Member

waruqi commented May 6, 2017

@titansnow I want to use the following two solutions:

  1. Try detect libreadline.a before building, I will use fgets if readline not found. (It done)
  2. Modify get.sh to install this library via package manager if not found (Can you help me modify it?)

@ghost
Copy link
Author

ghost commented May 6, 2017

👌

@ghost ghost mentioned this issue May 7, 2017
@waruqi
Copy link
Member

waruqi commented May 7, 2017

@titansnow Thanks! 😸

@ghost
Copy link
Author

ghost commented May 8, 2017

$ xmake --version
XMake v2.1.4.201705081849, The Make-like Build Utility based on Lua
Copyright (C) 2015-2016 Ruki Wang, tboox.org, xmake.io
Copyright (C) 2005-2015 Mike Pall, luajit.org
$ xmake lua
> os.execv('git',{'--version'})
git version 2.11.0
> os.runv
function: 0xb71601e8
> os.runv('git',{'--version'})
/usr/local/share/xmake/core/base/os.lua:569: runv(git --version) failed(-1)!
stack traceback:
	[C]: in function 'error'
	/usr/local/share/xmake/core/base/os.lua:569: in function 'raise'
	/usr/local/share/xmake/core/sandbox/modules/os.lua:245: in function </usr/local/share/xmake/core/sandbox/modules/os.lua:237>
	[C]: in function 'interactive'
	...ake/core/sandbox/modules/import/core/sandbox/sandbox.lua:51: in function 'interactive'
	/usr/local/share/xmake/plugins/lua/xmake.lua:60: in function </usr/local/share/xmake/plugins/lua/xmake.lua:32>
	[C]: in function 'load'
	/usr/local/share/xmake/core/project/task.lua:423: in function 'run'
	/usr/local/share/xmake/core/main.lua:188: in function </usr/local/share/xmake/core/main.lua:151>
> 

?

@waruqi
Copy link
Member

waruqi commented May 8, 2017

@titansnow I have fixed it.

@ghost ghost closed this as completed May 9, 2017
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant