@@ -189,6 +189,8 @@ void CScriptEngine::reinit()
189189 file_header = file_header_new;
190190 else
191191 file_header = file_header_old;
192+ scriptBufferSize = 1024 *1024 ;
193+ scriptBuffer = xr_alloc<char >(scriptBufferSize);
192194}
193195
194196int CScriptEngine::vscript_log (LuaMessageType luaMessageType, LPCSTR caFormat, va_list marker)
@@ -338,38 +340,14 @@ bool CScriptEngine::load_buffer(lua_State *L, LPCSTR caBuffer, size_t tSize, LPC
338340 xr_sprintf (insert, header, caNameSpaceName, a, b);
339341 u32 str_len = xr_strlen (insert);
340342 u32 const total_size = str_len+tSize;
341- LPSTR script = nullptr ;
342- bool dynamic_allocation = false ;
343- __try
343+ if (total_size>=scriptBufferSize)
344344 {
345- if (total_size < 768 * 1024 )
346- script = (LPSTR)_alloca (total_size);
347- else
348- {
349- #ifdef DEBUG
350- script = (LPSTR)Memory.mem_alloc (total_size, " lua script file" );
351- #else
352- script = (LPSTR)Memory.mem_alloc (total_size);
353- #endif
354- dynamic_allocation = true ;
355- }
356- }
357- __except (GetExceptionCode ()==STATUS_STACK_OVERFLOW)
358- {
359- int errcode = _resetstkoflw ();
360- R_ASSERT2 (errcode, " Could not reset the stack after \" Stack overflow\" exception!" );
361- #ifdef DEBUG
362- script = (LPSTR)Memory.mem_alloc (total_size, " lua script file (after exception)" );
363- #else
364- script = (LPSTR)Memory.mem_alloc (total_size);
365- #endif
366- dynamic_allocation = true ;
345+ scriptBufferSize = total_size;
346+ scriptBuffer = (char *)xr_realloc (scriptBuffer, scriptBufferSize);
367347 }
368- xr_strcpy (script, total_size, insert);
369- CopyMemory (script+str_len, caBuffer, u32 (tSize));
370- l_iErrorCode = luaL_loadbuffer (L, script, tSize+str_len, caScriptName);
371- if (dynamic_allocation)
372- xr_free (script);
348+ xr_strcpy (scriptBuffer, total_size, insert);
349+ CopyMemory (scriptBuffer+str_len, caBuffer, u32 (tSize));
350+ l_iErrorCode = luaL_loadbuffer (L, scriptBuffer, tSize+str_len, caScriptName);
373351 }
374352 else
375353 l_iErrorCode = luaL_loadbuffer (L, caBuffer, tSize, caScriptName);
@@ -808,6 +786,8 @@ CScriptEngine::~CScriptEngine()
808786 disconnect_from_debugger ();
809787#endif
810788#endif
789+ if (scriptBuffer)
790+ xr_free (scriptBuffer);
811791}
812792
813793void CScriptEngine::unload ()
0 commit comments