aboutsummaryrefslogtreecommitdiff
path: root/src/state.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/state.cpp')
-rw-r--r--src/state.cpp103
1 files changed, 54 insertions, 49 deletions
diff --git a/src/state.cpp b/src/state.cpp
index 55540c8..4a5f995 100644
--- a/src/state.cpp
+++ b/src/state.cpp
@@ -1,5 +1,5 @@
1/* 1/*
2* STATE.C 2* STATE.CPP
3* 3*
4* Lua tools to support Lanes. 4* Lua tools to support Lanes.
5*/ 5*/
@@ -8,7 +8,7 @@
8=============================================================================== 8===============================================================================
9 9
10Copyright (C) 2002-10 Asko Kauppi <akauppi@gmail.com> 10Copyright (C) 2002-10 Asko Kauppi <akauppi@gmail.com>
112011-21 benoit Germain <bnt.germain@gmail.com> 112011-24 benoit Germain <bnt.germain@gmail.com>
12 12
13Permission is hereby granted, free of charge, to any person obtaining a copy 13Permission is hereby granted, free of charge, to any person obtaining a copy
14of this software and associated documentation files (the "Software"), to deal 14of this software and associated documentation files (the "Software"), to deal
@@ -31,20 +31,11 @@ THE SOFTWARE.
31=============================================================================== 31===============================================================================
32*/ 32*/
33 33
34#include <stdio.h> 34#include "state.h"
35#include <assert.h> 35
36#include <string.h>
37#include <ctype.h>
38#include <stdlib.h>
39#if !defined(__APPLE__)
40#include <malloc.h>
41#endif // __APPLE__
42
43#include "compat.h"
44#include "macros_and_utils.h"
45#include "universe.h"
46#include "tools.h"
47#include "lanes.h" 36#include "lanes.h"
37#include "tools.h"
38#include "universe.h"
48 39
49// ################################################################################################ 40// ################################################################################################
50 41
@@ -58,7 +49,7 @@ THE SOFTWARE.
58// 49//
59// Upvalues: [1]: original 'require' function 50// Upvalues: [1]: original 'require' function
60// 51//
61static int luaG_new_require( lua_State* L) 52[[nodiscard]] static int luaG_new_require(lua_State* L)
62{ 53{
63 int rc; 54 int rc;
64 int const args = lua_gettop( L); // args 55 int const args = lua_gettop( L); // args
@@ -88,6 +79,8 @@ static int luaG_new_require( lua_State* L)
88 return lua_gettop(L); // result(s) 79 return lua_gettop(L); // result(s)
89} 80}
90 81
82// #################################################################################################
83
91/* 84/*
92* Serialize calls to 'require', if it exists 85* Serialize calls to 'require', if it exists
93*/ 86*/
@@ -119,15 +112,16 @@ void serialize_require(DEBUGSPEW_PARAM_COMMA( Universe* U) lua_State* L)
119 112
120/*---=== luaG_newstate ===---*/ 113/*---=== luaG_newstate ===---*/
121 114
122static int require_lanes_core( lua_State* L) 115[[nodiscard]] static int require_lanes_core(lua_State* L)
123{ 116{
124 // leaves a copy of 'lanes.core' module table on the stack 117 // leaves a copy of 'lanes.core' module table on the stack
125 luaL_requiref( L, "lanes.core", luaopen_lanes_core, 0); 118 luaL_requiref( L, "lanes.core", luaopen_lanes_core, 0);
126 return 1; 119 return 1;
127} 120}
128 121
122// #################################################################################################
129 123
130static const luaL_Reg libs[] = 124static luaL_Reg const libs[] =
131{ 125{
132 { LUA_LOADLIBNAME, luaopen_package}, 126 { LUA_LOADLIBNAME, luaopen_package},
133 { LUA_TABLIBNAME, luaopen_table}, 127 { LUA_TABLIBNAME, luaopen_table},
@@ -163,7 +157,9 @@ static const luaL_Reg libs[] =
163 { nullptr, nullptr } 157 { nullptr, nullptr }
164}; 158};
165 159
166static void open1lib( DEBUGSPEW_PARAM_COMMA( Universe* U) lua_State* L, char const* name_, size_t len_) 160// #################################################################################################
161
162static void open1lib(DEBUGSPEW_PARAM_COMMA(Universe* U) lua_State* L, char const* name_, size_t len_)
167{ 163{
168 int i; 164 int i;
169 for( i = 0; libs[i].name; ++ i) 165 for( i = 0; libs[i].name; ++ i)
@@ -192,30 +188,33 @@ static void open1lib( DEBUGSPEW_PARAM_COMMA( Universe* U) lua_State* L, char con
192 } 188 }
193} 189}
194 190
191// #################################################################################################
195 192
196// just like lua_xmove, args are (from, to) 193// just like lua_xmove, args are (from, to)
197static void copy_one_time_settings( Universe* U, lua_State* L, lua_State* L2) 194static void copy_one_time_settings(Universe* U, Source L, Dest L2)
198{ 195{
199 STACK_GROW( L, 2); 196 STACK_GROW(L, 2);
200 STACK_CHECK_START_REL(L, 0); 197 STACK_CHECK_START_REL(L, 0);
201 STACK_CHECK_START_REL(L2, 0); 198 STACK_CHECK_START_REL(L2, 0);
202 199
203 DEBUGSPEW_CODE( fprintf( stderr, INDENT_BEGIN "copy_one_time_settings()\n" INDENT_END)); 200 DEBUGSPEW_CODE(fprintf( stderr, INDENT_BEGIN "copy_one_time_settings()\n" INDENT_END));
204 DEBUGSPEW_CODE( ++ U->debugspew_indent_depth); 201 DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_add(1, std::memory_order_relaxed));
205 202
206 CONFIG_REGKEY.pushValue(L); // config 203 CONFIG_REGKEY.pushValue(L); // config
207 // copy settings from from source to destination registry 204 // copy settings from from source to destination registry
208 if( luaG_inter_move( U, L, L2, 1, LookupMode::LaneBody) < 0) // // config 205 if (luaG_inter_move(U, L, L2, 1, LookupMode::LaneBody) != InterCopyResult::Success) // // config
209 { 206 {
210 (void) luaL_error( L, "failed to copy settings when loading lanes.core"); 207 luaL_error( L, "failed to copy settings when loading lanes.core"); // doesn't return
211 } 208 }
212 // set L2:_R[CONFIG_REGKEY] = settings 209 // set L2:_R[CONFIG_REGKEY] = settings
213 CONFIG_REGKEY.setValue(L2, [](lua_State* L) { lua_insert(L, -2); }); // config 210 CONFIG_REGKEY.setValue(L2, [](lua_State* L) { lua_insert(L, -2); }); // config
214 STACK_CHECK( L2, 0); 211 STACK_CHECK(L2, 0);
215 STACK_CHECK( L, 0); 212 STACK_CHECK(L, 0);
216 DEBUGSPEW_CODE( -- U->debugspew_indent_depth); 213 DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_sub(1, std::memory_order_relaxed));
217} 214}
218 215
216// #################################################################################################
217
219void initialize_on_state_create( Universe* U, lua_State* L) 218void initialize_on_state_create( Universe* U, lua_State* L)
220{ 219{
221 STACK_CHECK_START_REL(L, 1); // settings 220 STACK_CHECK_START_REL(L, 1); // settings
@@ -247,7 +246,9 @@ void initialize_on_state_create( Universe* U, lua_State* L)
247 STACK_CHECK(L, 1); 246 STACK_CHECK(L, 1);
248} 247}
249 248
250lua_State* create_state( Universe* U, lua_State* from_) 249// #################################################################################################
250
251lua_State* create_state(Universe* U, lua_State* from_)
251{ 252{
252 lua_State* L; 253 lua_State* L;
253#if LUAJIT_FLAVOR() == 64 254#if LUAJIT_FLAVOR() == 64
@@ -273,11 +274,13 @@ lua_State* create_state( Universe* U, lua_State* from_)
273 274
274 if (L == nullptr) 275 if (L == nullptr)
275 { 276 {
276 std::ignore = luaL_error( from_, "luaG_newstate() failed while creating state; out of memory"); 277 luaL_error(from_, "luaG_newstate() failed while creating state; out of memory"); // doesn't return
277 } 278 }
278 return L; 279 return L;
279} 280}
280 281
282// #################################################################################################
283
281void call_on_state_create(Universe* U, lua_State* L, lua_State* from_, LookupMode mode_) 284void call_on_state_create(Universe* U, lua_State* L, lua_State* from_, LookupMode mode_)
282{ 285{
283 if (U->on_state_create_func != nullptr) 286 if (U->on_state_create_func != nullptr)
@@ -313,6 +316,8 @@ void call_on_state_create(Universe* U, lua_State* L, lua_State* from_, LookupMod
313 } 316 }
314} 317}
315 318
319// #################################################################################################
320
316/* 321/*
317* Like 'luaL_openlibs()' but allows the set of libraries be selected 322* Like 'luaL_openlibs()' but allows the set of libraries be selected
318* 323*
@@ -326,11 +331,11 @@ void call_on_state_create(Universe* U, lua_State* L, lua_State* from_, LookupMod
326* *NOT* called for keeper states! 331* *NOT* called for keeper states!
327* 332*
328*/ 333*/
329lua_State* luaG_newstate( Universe* U, lua_State* from_, char const* libs_) 334lua_State* luaG_newstate(Universe* U, Source from_, char const* libs_)
330{ 335{
331 lua_State* L = create_state( U, from_); 336 Dest const L{ create_state(U, from_) };
332 337
333 STACK_GROW( L, 2); 338 STACK_GROW(L, 2);
334 STACK_CHECK_START_ABS(L, 0); 339 STACK_CHECK_START_ABS(L, 0);
335 340
336 // copy the universe as a light userdata (only the master state holds the full userdata) 341 // copy the universe as a light userdata (only the master state holds the full userdata)
@@ -349,8 +354,8 @@ lua_State* luaG_newstate( Universe* U, lua_State* from_, char const* libs_)
349 return L; 354 return L;
350 } 355 }
351 356
352 DEBUGSPEW_CODE( fprintf( stderr, INDENT_BEGIN "luaG_newstate()\n" INDENT_END)); 357 DEBUGSPEW_CODE(fprintf( stderr, INDENT_BEGIN "luaG_newstate()\n" INDENT_END));
353 DEBUGSPEW_CODE( ++ U->debugspew_indent_depth); 358 DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_add(1, std::memory_order_relaxed));
354 359
355 // copy settings (for example because it may contain a Lua on_state_create function) 360 // copy settings (for example because it may contain a Lua on_state_create function)
356 copy_one_time_settings( U, from_, L); 361 copy_one_time_settings( U, from_, L);
@@ -423,22 +428,22 @@ lua_State* luaG_newstate( Universe* U, lua_State* from_, char const* libs_)
423 428
424#if 0 && USE_DEBUG_SPEW() 429#if 0 && USE_DEBUG_SPEW()
425 // dump the lookup database contents 430 // dump the lookup database contents
426 lua_getfield( L, LUA_REGISTRYINDEX, LOOKUP_REGKEY); // {} 431 lua_getfield(L, LUA_REGISTRYINDEX, LOOKUP_REGKEY); // {}
427 lua_pushnil( L); // {} nil 432 lua_pushnil(L); // {} nil
428 while( lua_next( L, -2)) // {} k v 433 while (lua_next(L, -2)) // {} k v
429 { 434 {
430 lua_getglobal( L, "print"); // {} k v print 435 lua_getglobal(L, "print"); // {} k v print
431 lua_pushlstring( L, debugspew_indent, U->debugspew_indent_depth); // {} k v print " " 436 lua_pushlstring(L, debugspew_indent, U->debugspew_indent_depth.load(std::memory_order_relaxed)); // {} k v print " "
432 lua_pushvalue( L, -4); // {} k v print " " k 437 lua_pushvalue(L, -4); // {} k v print " " k
433 lua_pushvalue( L, -4); // {} k v print " " k v 438 lua_pushvalue(L, -4); // {} k v print " " k v
434 lua_call( L, 3, 0); // {} k v 439 lua_call(L, 3, 0); // {} k v
435 lua_pop( L, 1); // {} k 440 lua_pop(L, 1); // {} k
436 } 441 }
437 lua_pop( L, 1); // {} 442 lua_pop(L, 1); // {}
438#endif // USE_DEBUG_SPEW() 443#endif // USE_DEBUG_SPEW()
439 444
440 lua_pop( L, 1); 445 lua_pop(L, 1);
441 STACK_CHECK(L, 0); 446 STACK_CHECK(L, 0);
442 DEBUGSPEW_CODE(--U->debugspew_indent_depth); 447 DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_sub(1, std::memory_order_relaxed));
443 return L; 448 return L;
444} 449}