| 986 | } |
| 987 | |
| 988 | lua_State *create_lua_state(bool use_contract) |
| 989 | { |
| 990 | lua_State *L = luaL_newstate(); |
| 991 | luaL_openlibs(L); |
| 992 | // run init lua code here, eg. init storage api, load some modules |
| 993 | add_global_c_function(L, "debugger", &enter_lua_debugger); |
| 994 | add_global_c_function(L, "exit_debugger", &exit_lua_debugger); |
| 995 | lua_createtable(L, 0, 0); |
| 996 | lua_setglobal(L, "last_return"); // 函数的最后返回值记录到这个全局变量 |
| 997 | add_global_c_function(L, "Array", &glua_core_lib_Array); |
| 998 | add_global_c_function(L, "Map", &glua_core_lib_Hashmap); |
| 999 | |
| 1000 | luaL_newmetatable(L, "GluaByteStream_metatable"); |
| 1001 | lua_pushcfunction(L, &glua_core_lib_Stream_size); |
| 1002 | lua_setfield(L, -2, "size"); |
| 1003 | lua_pushcfunction(L, &glua_core_lib_Stream_eof); |
| 1004 | lua_setfield(L, -2, "eof"); |
| 1005 | lua_pushcfunction(L, &glua_core_lib_Stream_current); |
| 1006 | lua_setfield(L, -2, "current"); |
| 1007 | lua_pushcfunction(L, &glua_core_lib_Stream_pos); |
| 1008 | lua_setfield(L, -2, "pos"); |
| 1009 | lua_pushcfunction(L, &glua_core_lib_Stream_next); |
| 1010 | lua_setfield(L, -2, "next"); |
| 1011 | lua_pushcfunction(L, &glua_core_lib_Stream_push); |
| 1012 | lua_setfield(L, -2, "push"); |
| 1013 | lua_pushcfunction(L, &glua_core_lib_Stream_push_string); |
| 1014 | lua_setfield(L, -2, "push_string"); |
| 1015 | lua_pushcfunction(L, &glua_core_lib_Stream_reset_pos); |
| 1016 | lua_setfield(L, -2, "reset_pos"); |
| 1017 | |
| 1018 | lua_pushvalue(L, -1); |
| 1019 | lua_setfield(L, -2, "__index"); |
| 1020 | lua_pop(L, 1); |
| 1021 | |
| 1022 | add_global_c_function(L, "Stream", &glua_core_lib_Stream); |
| 1023 | |
| 1024 | add_global_c_function(L, "glua_core_lib_pairs_by_keys_func_loader", &glua_core_lib_pairs_by_keys_func_loader); |
| 1025 | |
| 1026 | lua_createtable(L, 0, 0); |
| 1027 | lua_pushcfunction(L, &glua_core_lib_storage_metatable_index); |
| 1028 | lua_setfield(L, -2, "__index"); |
| 1029 | lua_pushcfunction(L, &glua_core_lib_storage_metatable_new_index); |
| 1030 | lua_setfield(L, -2, "__newindex"); |
| 1031 | lua_getglobal(L, "thinkyoung"); |
| 1032 | lua_pushvalue(L, -2); |
| 1033 | lua_setfield(L, -2, "storage_mt"); |
| 1034 | lua_pop(L, 2); |
| 1035 | |
| 1036 | /* |
| 1037 | contract_mt = { |
| 1038 | __index = glua_core_lib_contract_metatable_index, |
| 1039 | __newindex = glua_core_lib_contract_metatable_newindex |
| 1040 | } |
| 1041 | */ |
| 1042 | lua_createtable(L, 0, 0); |
| 1043 | lua_pushcfunction(L, &glua_core_lib_contract_metatable_index); |
| 1044 | lua_setfield(L, -2, "__index"); |
| 1045 | lua_pushcfunction(L, &glua_core_lib_contract_metatable_newindex); |
no test coverage detected