@@ -1423,6 +1423,71 @@ static tb_void_t xm_engine_bind_to_lua(lua_State* lua, xm_engine_t* engine)
14231423 lua_setglobal (lua , "__global_engine" );
14241424}
14251425
1426+ // load and execute the main script
1427+ static tb_bool_t xm_engine_load_main_script (xm_engine_t * engine , tb_char_t const * mainfile )
1428+ {
1429+ #ifdef TB_CONFIG_OS_WINDOWS
1430+ // use tb_file_init to support unicode file path on windows
1431+ tb_bool_t ok = tb_false ;
1432+ tb_file_ref_t file = tb_null ;
1433+ tb_byte_t * data = tb_null ;
1434+
1435+ do
1436+ {
1437+ // open file
1438+ file = tb_file_init (mainfile , TB_FILE_MODE_RO );
1439+ if (!file )
1440+ {
1441+ tb_printf ("error: cannot open file: %s\n" , mainfile );
1442+ break ;
1443+ }
1444+
1445+ // get file size
1446+ tb_size_t size = (tb_size_t )tb_file_size (file );
1447+ tb_assert_and_check_break (size );
1448+
1449+ // allocate buffer
1450+ data = (tb_byte_t * )tb_malloc (size );
1451+ tb_assert_and_check_break (data );
1452+
1453+ // read file content
1454+ if (!tb_file_read (file , data , size ))
1455+ {
1456+ tb_printf ("error: cannot read file: %s\n" , mainfile );
1457+ break ;
1458+ }
1459+
1460+ // load lua buffer
1461+ if (luaL_loadbuffer (engine -> lua , (tb_char_t const * )data , size , mainfile ))
1462+ {
1463+ tb_printf ("error: %s\n" , lua_tostring (engine -> lua , -1 ));
1464+ break ;
1465+ }
1466+
1467+ // execute lua script
1468+ if (lua_pcall (engine -> lua , 0 , LUA_MULTRET , 0 ))
1469+ {
1470+ tb_printf ("error: %s\n" , lua_tostring (engine -> lua , -1 ));
1471+ break ;
1472+ }
1473+
1474+ ok = tb_true ;
1475+
1476+ } while (0 );
1477+
1478+ if (data ) tb_free (data );
1479+ if (file ) tb_file_exit (file );
1480+ return ok ;
1481+ #else
1482+ if (luaL_dofile (engine -> lua , mainfile ))
1483+ {
1484+ tb_printf ("error: %s\n" , lua_tostring (engine -> lua , -1 ));
1485+ return tb_false ;
1486+ }
1487+ return tb_true ;
1488+ #endif
1489+ }
1490+
14261491/* //////////////////////////////////////////////////////////////////////////////////////
14271492 * implementation
14281493 */
@@ -1671,11 +1736,8 @@ tb_int_t xm_engine_main(xm_engine_ref_t self, tb_int_t argc, tb_char_t** argv, t
16711736 tb_trace_d ("main: %s" , path );
16721737
16731738 // load and execute the main script
1674- if (luaL_dofile (engine -> lua , path ))
1675- {
1676- tb_printf ("error: %s\n" , lua_tostring (engine -> lua , -1 ));
1739+ if (!xm_engine_load_main_script (engine , path ))
16771740 return -1 ;
1678- }
16791741
16801742 // set the error function
16811743 lua_getglobal (engine -> lua , "debug" );
0 commit comments