summaryrefslogtreecommitdiff
path: root/src/lobject.js
diff options
context:
space:
mode:
authordaurnimator <quae@daurnimator.com>2018-01-29 23:45:37 +1100
committerdaurnimator <quae@daurnimator.com>2018-01-29 23:45:37 +1100
commitcee732300058051cdf3ac4df541aeb8ee4e20e22 (patch)
treeb4027dfc64e2d3e9030d8c176648ac26cb6ee1a4 /src/lobject.js
parent7ac9666b8d9c840dfed8105768becaf5e228f84a (diff)
downloadfengari-cee732300058051cdf3ac4df541aeb8ee4e20e22.tar.gz
fengari-cee732300058051cdf3ac4df541aeb8ee4e20e22.tar.bz2
fengari-cee732300058051cdf3ac4df541aeb8ee4e20e22.zip
src/lobject.js: Use a switch/case over `typeof` when getting object ids
Diffstat (limited to 'src/lobject.js')
-rw-r--r--src/lobject.js50
1 files changed, 31 insertions, 19 deletions
diff --git a/src/lobject.js b/src/lobject.js
index 055ccb2..5405b34 100644
--- a/src/lobject.js
+++ b/src/lobject.js
@@ -657,26 +657,38 @@ const luaO_pushvfstring = function(L, fmt, argp) {
v instanceof LClosure ||
v instanceof CClosure) {
pushstr(L, to_luastring("0x"+v.id.toString(16)));
- } else if (v === null) { /* handle null before checking for typeof == object */
- pushstr(L, to_luastring("null"));
- } else if (typeof v === "function" || typeof v === "object") {
- let id = L.l_G.ids.get(v);
- if (!id) {
- id = L.l_G.id_counter++;
- L.l_G.ids.set(v, id);
+ }
+ switch(typeof v) {
+ case "undefined":
+ pushstr(L, to_luastring("undefined"));
+ break;
+ case "number": /* before check object as null is an object */
+ pushstr(L, to_luastring("Number("+v+")"));
+ break;
+ case "string": /* before check object as null is an object */
+ pushstr(L, to_luastring("String("+JSON.stringify(v)+")"));
+ break;
+ case "boolean": /* before check object as null is an object */
+ pushstr(L, to_luastring(v?"Boolean(true)":"Boolean(false)"));
+ break;
+ case "object":
+ if (v === null) { /* null is special */
+ pushstr(L, to_luastring("null"));
+ break;
+ }
+ /* fall through */
+ case "function": {
+ let id = L.l_G.ids.get(v);
+ if (!id) {
+ id = L.l_G.id_counter++;
+ L.l_G.ids.set(v, id);
+ }
+ pushstr(L, to_luastring("0x"+id.toString(16)));
+ break;
}
- pushstr(L, to_luastring("0x"+id.toString(16)));
- } else if (v === void 0) {
- pushstr(L, to_luastring("undefined"));
- } else if (typeof v === "number") { /* before check object as null is an object */
- pushstr(L, to_luastring("Number("+v+")"));
- } else if (typeof v === "string") { /* before check object as null is an object */
- pushstr(L, to_luastring("String("+JSON.stringify(v)+")"));
- } else if (typeof v === "boolean") { /* before check object as null is an object */
- pushstr(L, to_luastring(v?"Boolean(true)":"Boolean(false)"));
- } else {
- /* user provided object. no id available */
- pushstr(L, to_luastring("<id NYI>"));
+ default:
+ /* user provided object. no id available */
+ pushstr(L, to_luastring("<id NYI>"));
}
break;
}