MCPcopy Create free account
hub / github.com/defold/defold / SetScript

Function SetScript

engine/gui/src/gui.cpp:4783–4843  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

4781 }
4782
4783 Result SetScript(HScript script, dmLuaDDF::LuaSource *source)
4784 {
4785 lua_State* L = script->m_Context->m_LuaState;
4786 int top = lua_gettop(L);
4787 (void) top;
4788
4789 Result res = RESULT_OK;
4790
4791 int ret = dmScript::LuaLoad(L, source);
4792 if (ret != 0)
4793 {
4794 dmLogError("Error compiling script: %s", lua_tostring(L,-1));
4795 lua_pop(L, 1);
4796 res = RESULT_SYNTAX_ERROR;
4797 goto bail;
4798 }
4799
4800 lua_rawgeti(L, LUA_REGISTRYINDEX, script->m_InstanceReference);
4801 dmScript::SetInstance(L);
4802
4803 ret = dmScript::PCall(L, 0, 0);
4804
4805 lua_pushnil(L);
4806 dmScript::SetInstance(L);
4807
4808 if (ret != 0)
4809 {
4810 res = RESULT_SCRIPT_ERROR;
4811 goto bail;
4812 }
4813
4814 for (uint32_t i = 0; i < MAX_SCRIPT_FUNCTION_COUNT; ++i)
4815 {
4816 if (script->m_FunctionReferences[i] != LUA_NOREF)
4817 {
4818 dmScript::Unref(L, LUA_REGISTRYINDEX, script->m_FunctionReferences[i]);
4819 script->m_FunctionReferences[i] = LUA_NOREF;
4820 }
4821
4822 lua_getglobal(L, SCRIPT_FUNCTION_NAMES[i]);
4823 if (lua_type(L, -1) == LUA_TFUNCTION)
4824 {
4825 script->m_FunctionReferences[i] = dmScript::Ref(L, LUA_REGISTRYINDEX);
4826 }
4827 else
4828 {
4829 if (lua_isnil(L, -1) == 0)
4830 dmLogWarning("'%s' is not a function (%s)", SCRIPT_FUNCTION_NAMES[i], source->m_Filename);
4831 lua_pop(L, 1);
4832 }
4833
4834 lua_pushnil(L);
4835 lua_setglobal(L, SCRIPT_FUNCTION_NAMES[i]);
4836 }
4837
4838 free((void*)script->m_SourceFileName);
4839 script->m_SourceFileName = strdup(source->m_Filename);
4840bail:

Callers 2

ResCreateGuiScriptFunction · 0.50
ResRecreateGuiScriptFunction · 0.50

Calls 11

lua_gettopFunction · 0.85
lua_rawgetiFunction · 0.85
SetInstanceFunction · 0.85
PCallFunction · 0.85
lua_pushnilFunction · 0.85
UnrefFunction · 0.85
lua_typeFunction · 0.85
RefFunction · 0.85
freeFunction · 0.85
LuaLoadFunction · 0.50
assertFunction · 0.50

Tested by

no test coverage detected