From 4cc641ef7394d4aa07889d633068350eb26f893a Mon Sep 17 00:00:00 2001 From: Benoit Giannangeli Date: Sun, 14 May 2017 00:09:23 +0200 Subject: lua_arith, moved luaO_arith to lobject.js --- src/lobject.js | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 50 insertions(+), 3 deletions(-) (limited to 'src/lobject.js') diff --git a/src/lobject.js b/src/lobject.js index f926e85..9790d23 100644 --- a/src/lobject.js +++ b/src/lobject.js @@ -10,6 +10,7 @@ const lstring = require('./lstring.js'); const luaconf = require('./luaconf.js'); const lvm = require('./lvm.js'); const llimit = require('./llimit.js'); +const ltm = require('./ltm.js'); const CT = defs.constant_types; const char = defs.char; @@ -547,14 +548,60 @@ const numarith = function(L, op, v1, v2) { } }; -module.exports.LUA_TPROTO = LUA_TPROTO; -module.exports.LUA_TDEADKEY = LUA_TDEADKEY; +const luaO_arith = function(L, op, p1, p2, res) { + switch (op) { + case defs.LUA_OPBAND: case defs.LUA_OPBOR: case defs.LUA_OPBXOR: + case defs.LUA_OPSHL: case defs.LUA_OPSHR: + case defs.LUA_OPBNOT: { /* operate only on integers */ + let i1 = lvm.tointeger(p1); + let i2 = lvm.tointeger(p2); + if (i1 !== false && i2 !== false) { + res.type = CT.LUA_TNUMINT; + res.value = intarith(L, op, i1, i2); + return; + } + else break; /* go to the end */ + } + case defs.LUA_OPDIV: case defs.LUA_OPPOW: { /* operate only on floats */ + let n1 = lvm.tonumber(p1); + let n2 = lvm.tonumber(p2); + if (n1 !== false && n2 !== false) { + res.type = CT.LUA_TNUMFLT; + res.value = numarith(L, op, n1, n2); + return; + } + else break; /* go to the end */ + } + default: { /* other operations */ + let n1 = lvm.tonumber(p1); + let n2 = lvm.tonumber(p2); + if (p1.ttisinteger() && p2.ttisinteger()) { + res.type = CT.LUA_TNUMINT; + res.value = intarith(L, op, p1.value, p2.value); + return; + } + else if (n1 !== false && n2 !== false) { + res.type = CT.LUA_TNUMFLT; + res.value = numarith(L, op, n1, n2); + return; + } + else break; /* go to the end */ + } + } + /* could not perform raw operation; try metamethod */ + assert(L !== null); /* should not fail when folding (compile time) */ + ltm.luaT_trybinTM(L, p1, p2, res, (op - defs.LUA_OPADD) + ltm.TMS.TM_ADD); +}; + + module.exports.CClosure = CClosure; module.exports.LClosure = LClosure; +module.exports.LUA_TDEADKEY = LUA_TDEADKEY; +module.exports.LUA_TPROTO = LUA_TPROTO; module.exports.LocVar = LocVar; module.exports.TValue = TValue; module.exports.UTF8BUFFSZ = UTF8BUFFSZ; -module.exports.intarith = intarith; +module.exports.luaO_arith = luaO_arith; module.exports.luaO_chunkid = luaO_chunkid; module.exports.luaO_hexavalue = luaO_hexavalue; module.exports.luaO_int2fb = luaO_int2fb; -- cgit v1.2.3-54-g00ecf