From 8c0949281e8102d4e208869aac50267bd3fa25a9 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Wed, 24 May 2017 14:55:51 +1000 Subject: src/lstate.js: Create lua_State given global_State (not vice-versa) --- src/lstate.js | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/lstate.js b/src/lstate.js index 124322d..38c9b09 100644 --- a/src/lstate.js +++ b/src/lstate.js @@ -44,8 +44,9 @@ class CallInfo { class lua_State { - constructor() { - this.id = null; + constructor(g) { + this.id = g.id_counter++; + this.base_ci = new CallInfo(); // Will be populated later this.top = 0; this.ci = null; @@ -63,10 +64,10 @@ class lua_State { class global_State { - constructor(L) { + constructor() { this.id_counter = 0; /* used to give objects unique ids */ - this.mainthread = L; + this.mainthread = null; this.l_registry = new lobject.TValue(CT.LUA_TNIL, null); this.panic = null; this.atnativeerror = null; @@ -135,7 +136,6 @@ const f_luaopen = function(L) { }; const preinit_thread = function(L, g) { - L.id = g.id_counter++; L.l_G = g; L.stack = null; L.ci = null; @@ -154,7 +154,7 @@ const preinit_thread = function(L, g) { const lua_newthread = function(L) { let g = L.l_G; - let L1 = new lua_State(); + let L1 = new lua_State(g); L.stack[L.top++] = new lobject.TValue(CT.LUA_TTHREAD, L1); assert(L.top <= L.ci.top, "stack overflow"); preinit_thread(L1, g); @@ -172,10 +172,11 @@ const luaE_freethread = function(L, L1) { }; const lua_newstate = function() { - let L = new lua_State(); - let g = new global_State(L); + let g = new global_State(); + let L = new lua_State(g); preinit_thread(L, g); + g.mainthread = L; if (ldo.luaD_rawrunprotected(L, f_luaopen, null) !== TS.LUA_OK) { L = null; -- cgit v1.2.3-54-g00ecf