Beispiel #1
0
	inline void include_path(lua_State *L,const char * path)
	{
		std::ostringstream stream;

		stream << "package.path = package.path .. \";" << path << "/?.lua\"";

		dostring(L,stream.str().c_str());
	}
Beispiel #2
0
static int handle_luainit (lua_State *L) {
  const char *init = getenv("LUA_INIT");
  if (init == NULL) return 0;  /* status OK */
  else if (init[0] == '@')
    return dofile(L, init+1);
  else
    return dostring(L, init, "=LUA_INIT");
}
Beispiel #3
0
int main(int argc,char* argv[]) {
  lua_State *L;
  int ok;
  printf("[C] Welcome to the simple embedded lua example\n");
  printf("[C] We are in C\n");
  printf("[C] opening a lua state & loading the libraries\n");
  L=lua_open();
  luaopen_base(L);
  luaopen_string(L);
  luaopen_math(L);
  printf("[C] now loading the SWIG wrapped library\n");
  luaopen_example(L);
  printf("[C] all looks ok\n");
  printf("\n");
  if (argc != 2 || argv[1] == NULL || strlen(argv[1]) == 0) {
    printf("[C] ERROR: no lua file given on command line\n");
    exit(3);
  }
  printf("[C] let's load the file '%s'\n", argv[1]);
  printf("[C] any lua code in this file will be executed\n");
  if (luaL_loadfile(L, argv[1]) || lua_pcall(L, 0, 0, 0)) {
    printf("[C] ERROR: cannot run lua file: %s",lua_tostring(L, -1));
    exit(3);
  }
  printf("[C] We are now back in C, all looks ok\n");
  printf("\n");
  printf("[C] let's call the function 'do_tests()'\n");
  ok=dostring(L,"do_tests()");
  printf("[C] We are back in C, the dostring() function returned %d\n",ok);
  printf("\n");
  printf("[C] Let's call lua again, but create an error\n");
  ok=dostring(L,"no_such_function()");
  printf("[C] We are back in C, the dostring() function returned %d\n",ok);
  printf("[C] it should also have returned 1 and printed an error message\n");
  printf("\n");
  printf("[C] Let's call lua again, calling the greeting function\n");
  ok=dostring(L,"call_greeting()");
  printf("[C] This was C=>Lua=>C (getting a bit complex)\n");
  printf("\n");
  printf("[C] all finished, closing the lua state\n");
  lua_close(L);
  return 0;
}
Beispiel #4
0
static void iuplua_openlibs (lua_State *L) {
  lua_pushliteral(L, LUA_COPYRIGHT);
  lua_setglobal(L, "_COPYRIGHT");  /* set global _COPYRIGHT */

#ifdef USE_STATIC
  /* disable require */
  dostring(L, "function require() end ", "static_require");

  /* iuplua initialization */
  iuplua_open(L);

#ifdef IUPLUA_IMGLIB
  luaopen_iupluaimglib(L);
#endif
#ifdef IUPLUA_TUIO
  iuptuiolua_open(L);
#endif
#ifdef IUPLUA_WEB
  iupweblua_open(L);
#endif
#ifdef IUPLUA_SCINTILLA
  iup_scintillalua_open(L);
#endif

/* luaopen_lfs(L); */

#ifndef IUPLUA_NO_GL
  iupgllua_open(L);
  iupglcontrolslua_open(L);
#ifdef USE_LUAGL
  luaopen_luagl(L);
#endif
#endif
#ifndef IUPLUA_NO_CD
  iupcontrolslua_open(L);
  iupmatrixexlua_open(L);
  iup_plotlua_open(L);
  cdlua_open(L);
  cdluaiup_open(L);
  cdInitContextPlus();
#endif
#ifndef IUPLUA_NO_IM
  iupimlua_open(L);
  imlua_open(L);
  imlua_open_process(L);
#endif
#ifndef IUPLUA_NO_IM
#ifndef IUPLUA_NO_CD
  cdluaim_open(L);
#endif
#endif
#endif
}
Beispiel #5
0
	inline void include_cpath(lua_State *L,const char * path)
	{
		std::ostringstream stream;

#ifdef WIN32
		stream << "package.cpath = package.cpath .. \";" << path << "/?.dll\"";
#else
		stream << "package.cpath = package.cpath .. \";" << path << "/?.so\"";
#endif //WIN32

		dostring(L,stream.str().c_str());
	}
Beispiel #6
0
static int handle_luainit (lua_State *L) {
  const char *name = "=" LUA_INITVERSION;
  const char *init = NULL;
  if (init == NULL) {
    name = "=" LUA_INIT;
	init = NULL;
  }
  if (init == NULL) return LUA_OK;
  else if (init[0] == '@')
    return dofile(L, init+1);
  else
    return dostring(L, init, name);
}
Beispiel #7
0
/**
   \brief Parse a block of Lua code. If as_expr is true, then
   it appends the string "return " in front of the script.
*/
void parser_imp::parse_script(bool as_expr) {
    m_last_script_pos = mk_pair(m_scanner.get_script_block_line(), m_scanner.get_script_block_pos());
    if (!m_script_state)
        throw exception("failed to execute Lua script, parser does not have a Lua interpreter");
    std::string script_code = m_scanner.get_str_val();
    if (as_expr) {
        script_code = "return " + script_code;
    }
    next();
    using_script([&](lua_State * L) {
            dostring(L, script_code.c_str());
        });
}
Beispiel #8
0
static int handle_luainit (lua_State *L) {
  const char *name = "=" LUA_INITVERSION;
  const char *init = getenv(name + 1);
  if (init == NULL) {
    name = "=" LUA_INIT;
    init = getenv(name + 1);  /* try alternative name */
  }
  if (init == NULL) return LUA_OK;
  else if (init[0] == '@')
    return dofile(L, init+1);
  else
    return dostring(L, init, name);
}
Beispiel #9
0
static int handle_luainit(lua_State *L)
{
#if LJ_TARGET_CONSOLE
  const char *init = NULL;
#else
  const char *init = getenv(LUA_INIT);
#endif
  if (init == NULL)
    return 0;  /* status OK */
  else if (init[0] == '@')
    return dofile(L, init+1);
  else
    return dostring(L, init, "=" LUA_INIT);
}
Beispiel #10
0
	bool lua_context::execute(const variant& value, game_logic::formula_callable* callable)
	{
		bool res = false;
		if(callable) {
			set_self_callable(*callable);
		}
		if(value.is_string()) {
			res = dostring("", value.as_string());
		} else {
			lua_compiled_ptr compiled = value.try_convert<lua_compiled>();
			ASSERT_LOG(compiled != NULL, "FATAL: object given couldn't be converted to type 'lua_compiled'");
			res = compiled->run(context_ptr());
		}
		return res;
	}
Beispiel #11
0
static int handle_luainit (lua_State *L) {
  const char *name = "=" LUA_INITVERSION;
  // getenv not supported on Windows Store
  //const char *init = getenv(name + 1);
  const char *init = NULL;
  if (init == NULL) {
    name = "=" LUA_INIT;
	// getenv not supported on Windows Store
	//init = getenv(name + 1);  /* try alternative name */
    init = NULL;  /* try alternative name */
  }
  if (init == NULL) return LUA_OK;
  else if (init[0] == '@')
    return dofile(L, init+1);
  else
    return dostring(L, init, name);
}
Beispiel #12
0
/*
** Processes options 'e' and 'l', which involve running Lua code.
** Returns 0 if some code raises an error.
*/
static int runargs (lua_State *L, char **argv, int n) {
  int i;
  for (i = 1; i < n; i++) {
    int option = argv[i][1];
    lua_assert(argv[i][0] == '-');  /* already checked */
    if (option == 'e' || option == 'l') {
      int status;
      const char *extra = argv[i] + 2;  /* both options need an argument */
      if (*extra == '\0') extra = argv[++i];
      lua_assert(extra != NULL);
      status = (option == 'e')
               ? dostring(L, extra, "=(command line)")
               : dolibrary(L, extra);
      if (status != LUA_OK) return 0;
    }
  }
  return 1;
}
Beispiel #13
0
/*---------------------------------------------------------------------*/
void 
process_str_request(lua_State *L, void *argv)
{
	TRACE_LUA_FUNC_START();
	int read_size, total_read, rc;
	char client_msg[LUA_MAXINPUT];
	int client_sock = *((int *)argv);
	total_read = rc = 0;
	
	/* Receive message from client */
	while ((read_size = recv(client_sock, 
				 &client_msg[total_read],
				 LUA_MAXINPUT - total_read, 0)) > 0) {
		total_read += read_size;
		if ((unsigned)(total_read >= LUA_MAXINPUT) || 
		    client_msg[total_read - 1] == REMOTE_LUA_CMD_DELIM) {
			client_msg[total_read - 1] = '\0';
			break;
		}
	}
	/* 
	 * if total_read == 0, 
	 * then this means that the client closed conn. 
	 *
	 * Otherwise process the lua command
	 */
	if (total_read != 0) {
		char delim = REMOTE_LUA_CMD_DELIM;
		register_lua_procs(L);
		load_startup(L);
		setvbuf(stdout, NULL, _IONBF, BUFSIZ);
		/* redirect stdout and stderr to client_sock */
		dup2(client_sock, 1);
		dup2(client_sock, 2);
		/* redirect command output to the client */
		dostring(L, client_msg, "init");
		rc = write(client_sock, &delim, 1);
		/* redirect stdout and stderr back to /dev/null */
		dup2(0, 1);
		dup2(0, 2);
	}
	TRACE_LUA_FUNC_END();
}
Beispiel #14
0
static int runargs(lua_State *L, char **argv, int n)
{
    int i;
    for (i = 1; i < n; i++) {
        if (argv[i] == NULL) continue;
        lua_assert(argv[i][0] == '-');
        switch (argv[i][1]) {  /* option */
        case 'e': {
            const char *chunk = argv[i] + 2;
            if (*chunk == '\0') chunk = argv[++i];
            lua_assert(chunk != NULL);
            if (dostring(L, chunk, "=(command line)") != 0)
                return 1;
            break;
        }
        case 'l': {
            const char *filename = argv[i] + 2;
            if (*filename == '\0') filename = argv[++i];
            lua_assert(filename != NULL);
            if (dolibrary(L, filename))
                return 1;  /* stop if file fails */
            break;
        }
        case 'j': {  /* LuaJIT extension */
            const char *cmd = argv[i] + 2;
            if (*cmd == '\0') cmd = argv[++i];
            lua_assert(cmd != NULL);
            if (dojitcmd(L, cmd))
                return 1;
            break;
        }
        case 'O':  /* LuaJIT extension */
            if (dojitopt(L, argv[i] + 2))
                return 1;
            break;
        case 'b':  /* LuaJIT extension */
            return dobytecode(L, argv+i);
        default:
            break;
        }
    }
    return 0;
}
Beispiel #15
0
/*---------------------------------------------------------------------*/
static int
handle_luainit(lua_State *L)
{
	TRACE_LUA_FUNC_START();
	
        const char *init = getenv(LUA_INIT);
	
        if (init == NULL) {
		TRACE_LUA_FUNC_END();
		return 0; /* status OK */
	}
        else if (init[0] == '@') {
		TRACE_LUA_FUNC_END();
                return dofile(L, init+1);
	} else
                dostring(L, init, "=" LUA_INIT);
	
	TRACE_LUA_FUNC_END();
        return 0;
}
Beispiel #16
0
static int runargs(lua_State *L, char **argv, int n)
{
    int i;

    for (i = 1; i < n; i++)
    {
        lua_assert(argv[i][0] == '-');

        switch (argv[i][1]) /* option */
        {
        case 'e': {
            const char *chunk = argv[i] + 2;
            if (*chunk == '\0')
                chunk = argv[++i];

            lua_assert(chunk != NULL);
            if (dostring(L, chunk, "=(command line)") != LUA_OK)
                return 0;

            break;
        }

        case 'l': {
            const char *filename = argv[i] + 2;
            if (*filename == '\0')
                filename = argv[++i];

            lua_assert(filename != NULL);
            if (dolibrary(L, filename) != LUA_OK)
                return 0; /* stop if file fails */

            break;
        }

        default: break;
        }
    }

    return 1;
}
Beispiel #17
0
static int runargs (lua_State *L, char **argv, int n)
{
	int i;
	for (i = 1; i < n; i++) {
		if (argv[i] == NULL) continue;
		lua_assert(argv[i][0] == '-');
		switch (argv[i][1]) {  /* option */
		case 'e': {
			const char *chunk = argv[i] + 2;
			if (*chunk == '\0') chunk = argv[++i];
			lua_assert(chunk != NULL);
			if (dostring(L, chunk, "=(command line)") != 0)
				return 1;
			break;
		}
		case 'm': {
			const char *limit = argv[i] + 2;
			int memlimit=0;
			if (*limit == '\0') limit = argv[++i];
			lua_assert(limit != NULL);
			memlimit = atoi(limit);
			lua_gc(L, LUA_GCSETMEMLIMIT, memlimit);
			break;
		}
		case 'l': {
			const char *filename = argv[i] + 2;
			if (*filename == '\0') filename = argv[++i];
			lua_assert(filename != NULL);
			if (dolibrary(L, filename))
				return 1;  /* stop if file fails */
			break;
		}
		default:
			break;
		}
	}
	return 0;
}
Beispiel #18
0
int main()
{
    lua_State* L = lua_open();
    luaopen_base(L);
#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 501
	 // lua 5.1 or newer
	 luaL_openlibs(L);
#else
	 // lua 5.0.2 or older
    lua_baselibopen(L);
#endif
    luabind::open(L);

    luabind::module(L, "namespace")
    [
        luabind::def("HelloWorld", (void(*)())&HelloWorld),
        luabind::def("HelloWorld", (void(*)(const std::string&))&HelloWorld),
        luabind::def("HelloWorld", (void(*)(const int))&HelloWorld),
        luabind::class_<MyClass>("MyClass")
            .def(luabind::constructor<>())
            .def("Hello", (void(MyClass::*)()) &MyClass::Hello)
            .def("Hello", (void(MyClass::*)(const std::string&)) &MyClass::Hello)
            .scope
            [
                luabind::class_<MyOtherClass>("MyOtherClass")
                    .property("num", &MyOtherClass::num)
            ]
    ];

    lua_register(L, "GetLuabindInfo", &GetLuabindInfo);

    try
    {

        dostring(L,
                 "assert(GetLuabindInfo() == nil)\n"
                 "assert(GetLuabindInfo(1) == nil)\n"
                 "assert(GetLuabindInfo(\"test\") == nil)\n"

                 "testUs = {namespace.HelloWorld, namespace.MyClass, namespace.MyClass.MyOtherClass}\n"
                 "for index, testMe in ipairs(testUs) do\n"
                 " info = GetLuabindInfo(testMe)\n"
                 " if not info then\n"
                 "  print(\"entry \" .. index .. \" is no luabind function/class\")\n"
                 " else\n"
                 "  print(\"entry \" .. index .. \" is a \" .. info.type .. \" called \" .. (info.name or \"\"))\n"
                 "  if info.type == \"function\" then\n"
                 "   print(\"available overloads:\")\n"
                 "   for _, signature in ipairs(info.overloads) do\n"
                 "    print(signature)\n"
                 "   end\n"
                 "  else\n"
                 "   assert(info.type == \"class\")\n"
                 "  end\n"
                 " end\n"
                 " print()\n"
                 "end\n"
                 "namespace.HelloWorld{}\n" //this prints the correct signature in the error
                 );
    }
    catch(std::string err)
    {
        std::cout<<"Caught error: " << err << std::endl;
    }

    lua_close(L);
    return 0;
}
Beispiel #19
0
static int handle_argv (char *argv[], int *interactive) {
  if (argv[1] == NULL) {  /* no more arguments? */
    if (stdin_is_tty()) {
      print_version();
      manual_input();
    }
    else
      file_input(NULL);  /* executes stdin as a file */
  }
  else {  /* other arguments; loop over them */
    int i;
    for (i = 1; argv[i] != NULL; i++) {
      if (argv[i][0] != '-') break;  /* not an option? */
      switch (argv[i][1]) {  /* option */
        case '-': {  /* `--' */
          if (argv[i][2] != '\0') {
            print_usage();
            return 1;
          }
          i++;  /* skip this argument */
          goto endloop;  /* stop handling arguments */
        }
        case '\0': {
          file_input(NULL);  /* executes stdin as a file */
          break;
        }
        case 'i': {
          *interactive = 1;
          break;
        }
        case 'v': {
          print_version();
          break;
        }
        case 'e': {
          const char *chunk = argv[i] + 2;
          if (*chunk == '\0') chunk = argv[++i];
          if (chunk == NULL) {
            print_usage();
            return 1;
          }
          if (dostring(chunk, "=<command line>") != 0)
            return 1;
          break;
        }
        case 'l': {
          const char *filename = argv[i] + 2;
          if (*filename == '\0') filename = argv[++i];
          if (filename == NULL) {
            print_usage();
            return 1;
          }
          if (load_file(filename))
            return 1;  /* stop if file fails */
          break;
        }
        case 'c': {
          l_message(progname, "option `-c' is deprecated");
          break;
        }
        case 's': {
          l_message(progname, "option `-s' is deprecated");
          break;
        }
        default: {
          print_usage();
          return 1;
        }
      }
    } endloop:
    if (argv[i] != NULL) {
      const char *filename = argv[i];
      getargs(argv, i);  /* collect arguments */
      lua_setglobal(L, "arg");
      return file_input(filename);  /* stop scanning arguments */
    }
  }
  return 0;
}
Beispiel #20
0
static int pmain(lua_State *L)
{
  struct Smain *s = (struct Smain *)lua_touserdata(L, 1);
  char **argv = s->argv;
  int script;
  int flags = 0;
  globalL = L;
  if (argv[0] && argv[0][0]) progname = argv[0];
  LUAJIT_VERSION_SYM();  /* linker-enforced version check */
  script = collectargs(argv, &flags);
  if (script < 0) {  /* invalid args? */
    print_usage();
    s->status = 1;
    return 0;
  }
  if ((flags & FLAGS_NOENV)) {
    lua_pushboolean(L, 1);
    lua_setfield(L, LUA_REGISTRYINDEX, "LUA_NOENV");
  }
  lua_gc(L, LUA_GCSTOP, 0);  /* stop collector during initialization */
  luaL_openlibs(L);  /* open libraries */

  /**************/
  lua_pushstring(L, MULTIWIICL_VERSION);
  lua_setglobal(L, "MULTIWIICL_VERSION");

  extern int luaopen_lpeg(lua_State*);
  lua_pushcfunction(L, luaopen_lpeg);
  lua_call(L, 0, 0);

  extern int luaopen_luaserial(lua_State*);
  lua_pushcfunction(L, luaopen_luaserial);
  lua_call(L, 0, 0);

  extern int luaopen_linenoise(lua_State*);
  lua_pushcfunction(L, luaopen_linenoise);
  lua_call(L, 0, 1);

# include "Config.h"
  if (dostring(L, (const char*)Config, "@Config")) {
    printf("%s\n", lua_tostring(L, -1));
    lua_pop(L, 1);
  }

# include "MultiWii.h"
  if (dostring(L, (const char*)MultiWii, "@MultiWii")) {
    printf("%s\n", lua_tostring(L, -1));
    lua_pop(L, 1);
  }

  /**************/

  lua_gc(L, LUA_GCRESTART, -1);
  if (!(flags & FLAGS_NOENV)) {
    s->status = handle_luainit(L);
    if (s->status != 0) return 0;
  }

  s->status = runargs(L, argv, (script > 0) ? script : s->argc);
  if (s->status != 0) return 0;
  if (script) {
    s->status = handle_script(L, argv, script);
    if (s->status != 0) return 0;
  }

  if ((flags & FLAGS_INTERACTIVE) || (script == 0 && !(flags & (FLAGS_EXEC|FLAGS_VERSION)))) {
#   include "Prompt.h"
    if (dostring(L, (const char*)Prompt, "@Prompt")) {
      printf("%s\n", lua_tostring(L, -1));
      lua_pop(L, 1);
    }
  }

//  if ((flags & FLAGS_VERSION)) print_version();
//  s->status = runargs(L, argv, (script > 0) ? script : s->argc);
//  if (s->status != 0) return 0;
//  if (script) {
//    s->status = handle_script(L, argv, script);
//    if (s->status != 0) return 0;
//  }
//  if ((flags & FLAGS_INTERACTIVE)) {
//    print_jit_status(L);
//    dotty(L);
//  } else if (script == 0 && !(flags & (FLAGS_EXEC|FLAGS_VERSION))) {
//    if (lua_stdin_is_tty()) {
//      print_version();
//      print_jit_status(L);
//      dotty(L);
//    } else {
//      dofile(L, NULL);  /* executes stdin as a file */
//    }
//  }
  return 0;
}
Beispiel #21
0
static int handle_argv (lua_State *L, int argc, char **argv, int *interactive) {
  if (argv[1] == NULL) {  /* no arguments? */
    *interactive = 0;
    if (lua_stdin_is_tty())
      dotty(L);
    else
      dofile(L, NULL);  /* executes stdin as a file */
  }
  else {  /* other arguments; loop over them */
    int i;
    for (i = 1; argv[i] != NULL; i++) {
      if (argv[i][0] != '-') break;  /* not an option? */
      switch (argv[i][1]) {  /* option */
        case '-': {  /* `--' */
          if (argv[i][2] != '\0') {
            print_usage();
            return 1;
          }
          i++;  /* skip this argument */
          goto endloop;  /* stop handling arguments */
        }
        case '\0': {
          clearinteractive(interactive);
          dofile(L, NULL);  /* executes stdin as a file */
          break;
        }
        case 'i': {
          *interactive = 2;  /* force interactive mode after arguments */
          break;
        }
        case 'v': {
          clearinteractive(interactive);
          print_version();
          break;
        }
        case 'e': {
          const char *chunk = argv[i] + 2;
          clearinteractive(interactive);
          if (*chunk == '\0') chunk = argv[++i];
          if (chunk == NULL) {
            print_usage();
            return 1;
          }
          if (dostring(L, chunk, "=(command line)") != 0)
            return 1;
          break;
        }
        case 'l': {
          const char *filename = argv[i] + 2;
          if (*filename == '\0') filename = argv[++i];
          if (filename == NULL) {
            print_usage();
            return 1;
          }
          if (dolibrary(L, filename))
            return 1;  /* stop if file fails */
          break;
        }
        default: {
          clearinteractive(interactive);
          print_usage();
          return 1;
        }
      }
    } endloop:
    if (argv[i] != NULL) {
      int status;
      const char *filename = argv[i];
      int narg = getargs(L, argc, argv, i);  /* collect arguments */
      lua_setglobal(L, "arg");
      clearinteractive(interactive);
      status = luaL_loadfile(L, filename);
      lua_insert(L, -(narg+1));
      if (status == 0)
        status = docall(L, narg, 0);
      else
        lua_pop(L, narg);      
      return report(L, status);
    }
  }
  return 0;
}
Beispiel #22
0
/* the driver */
int
main(int argc, char *argv[])
{
    int status = 1;
    int i;
    lua_State *L = NULL;

    if (QDP_initialize(&argc, &argv)) {
        fprintf(stderr, "QDP initialization failed\n");
        return 1;
    }
    QDP_profcontrol(0);
    double node = QDP_this_node;
    QMP_min_double(&node);
    qlua_master_node = node;

    L = lua_open();
    if (L == NULL) {
        message("can not create Lua state");
        goto end;
    }
    qlua_init(L, argc, argv);  /* open libraries */

    if (argc < 2) {
        message("QLUA component versions:\n");
        for (i = 0; versions[i].name; i++)
            message(" %10s: %s\n", versions[i].name, versions[i].value);
    } else {

                for (i = 1; i < argc; i++) {
                        char *source;
                        if(strcmp(argv[i],"-e")==0) { // process command
                                const char *chunk = argv[i] + 2;
                                if (*chunk == '\0') {
                                        if (++i >= argc) {
                                                message("missing argument to -e");
                                                goto end;
                                        }
                                        chunk = argv[i];
                                }
                                QLUA_ASSERT(chunk != NULL);
                                status = dostring(L, chunk);
                                source = "=(command line)";
                        } else {
                                status = dofile(L, argv[i]);
                                source = argv[i];
                        }
                        report(L, source, status);
                        if (status) {
                                fflush(stdout);
                                fflush(stderr);
                                QDP_abort(1);
                                break;
                        }
                }
    }
    qlua_fini(L);
    lua_close(L);
end:
    QDP_finalize();
    return status;
}