diff options
author | Benoit Giannangeli <giann008@gmail.com> | 2017-05-14 00:09:23 +0200 |
---|---|---|
committer | Benoit Giannangeli <giann008@gmail.com> | 2017-05-14 00:09:23 +0200 |
commit | 4cc641ef7394d4aa07889d633068350eb26f893a (patch) | |
tree | dbe2b6e266f96a0fee5bba5bc1aeb2d4c7df07ee /src/lobject.js | |
parent | 00026860d3fe14b12cd539ee76ee791b0d46f7a4 (diff) | |
download | fengari-4cc641ef7394d4aa07889d633068350eb26f893a.tar.gz fengari-4cc641ef7394d4aa07889d633068350eb26f893a.tar.bz2 fengari-4cc641ef7394d4aa07889d633068350eb26f893a.zip |
lua_arith, moved luaO_arith to lobject.js
Diffstat (limited to 'src/lobject.js')
-rw-r--r-- | src/lobject.js | 53 |
1 files changed, 50 insertions, 3 deletions
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; |