Procházet zdrojové kódy

change: luat_main改为主动搜索main的路径,然后dofile,这样更快也更节省内存

Wendal Chen před 4 roky
rodič
revize
06fb1c9133
2 změnil soubory, kde provedl 29 přidání a 13 odebrání
  1. 14 4
      lua/src/loadlib.c
  2. 15 9
      luat/modules/luat_main.c

+ 14 - 4
lua/src/loadlib.c

@@ -505,9 +505,7 @@ static const char* search_paths[] = {
   "",
 };
 
-static int searcher_Lua (lua_State *L) {
-  char filename[32];
-  const char *name = luaL_checkstring(L, 1);
+int luat_search_module(const char* name, char* filename) {
   int index = 0;
   while (1) {
     if (strlen(search_paths[index]) == 0)
@@ -520,12 +518,21 @@ static int searcher_Lua (lua_State *L) {
   if (filename[0] == 0x00) {
     return -1;
   }
-  int re = checkload(L, (luaL_loadfile(L, filename) == LUA_OK), filename);
+  return 0;
+}
+
+static int searcher_Lua (lua_State *L) {
+  char filename[32] = {0};
+  const char *name = luaL_checkstring(L, 1);
+  int re = luat_search_module(name, filename);
+  if (re == 0)
+    re = checkload(L, (luaL_loadfile(L, filename) == LUA_OK), filename);
   //rt_kprintf("searcher_Lua name=%s re=%d\n", name, re);
   return re;
 }
 
 
+
 /*
 ** Try to find a load function for module 'modname' at file 'filename'.
 ** First, change '.' to '_' in 'modname'; then, if 'modname' has
@@ -635,9 +642,12 @@ static int ll_require (lua_State *L) {
   
   // add by wendal, 替换原有的逻辑
   lua_pushstring(L, name);
+  //luat_os_print_heapinfo("go-loadfile");
   if (searcher_Lua(L) == 2) {
+    //luat_os_print_heapinfo("go-call");
     lua_pushstring(L, name);
     lua_call(L, 2, 1);
+    //luat_os_print_heapinfo("after-call");
   }
   else {
     luaL_error(L, "module '%s' not found", name);

+ 15 - 9
luat/modules/luat_main.c

@@ -30,8 +30,10 @@ lua_State * luat_get_state() {
   return L;
 }
 
+int luat_search_module(const char* name, char* filename);
+
 static int pmain(lua_State *L) {
-    int re = 0;
+    int re = -2;
 
     #ifdef LUA_32BITS
     //LLOGD("Lua complied with LUA_32BITS");
@@ -51,16 +53,20 @@ static int pmain(lua_State *L) {
       if (slen > 4 && !strcmp(".lua", win32_argv[1] + (slen - 4)))
         re = luaL_dofile(L, win32_argv[1]);
     }
-    else
-        re = luaL_dostring(L, "require(\"main\")");
+    #endif
+    if (re == -2) {
+      char filename[32] = {0};
+      if (luat_search_module("main", filename) == 0) {
+        re = luaL_dofile(L, filename);
+      }
+      else {
+        re = -1;
+        luaL_error(L, "module '%s' not found", "main");
+      }
+    }
+        
     report(L, re);
     lua_pushboolean(L, re == LUA_OK);  /* signal no errors */
-    #else
-    re = luaL_dostring(L, "require(\"main\")");
-
-    report(L, re);
-    lua_pushboolean(L, 1);  /* signal no errors */
-    #endif
     return 1;
 }