From 4c7732b08043dc63ff195b0bc646cc184cefde41 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Tue, 16 May 2017 16:32:32 +1000 Subject: src/lfunc.js: Fix luaF_findupval, it needs to insert the upvalue into correct place in linked list, not the start --- src/lfunc.js | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-) (limited to 'src/lfunc.js') diff --git a/src/lfunc.js b/src/lfunc.js index 7e324da..663f614 100644 --- a/src/lfunc.js +++ b/src/lfunc.js @@ -54,28 +54,26 @@ const luaF_newLclosure = function(L, n) { const luaF_findupval = function(L, level) { - let pp = L.openupval; - let p = pp; - while (pp !== null && pp.v >= level) { - p = pp; + let prevp; + let p = L.openupval; + while (p !== null && p.v >= level) { assert(p.isopen()); - if (p.v === level) - return p; - - pp = p.open_next; + if (p.v === level) /* found a corresponding upvalue? */ + return p; /* return it */ + prevp = p; + p = p.open_next; } - + /* not found: create a new upvalue */ let uv = new UpVal(); - - if (p) { /* The openupval list is not empty */ - uv.open_next = p; /* link it to list of open upvalues */ - } - - L.openupval = uv; - + /* link it to list of open upvalues */ + uv.open_next = p; + if (prevp) + prevp.open_next = uv; + else + L.openupval = uv; + /* current value lives in the stack */ uv.L = L; - uv.v = level; /* current value lives in the stack */ - + uv.v = level; return uv; }; -- cgit v1.2.3-70-g09d2