ref: 759acbd4ddaa706d2f457616d4e5ea32ea4127f6
parent: 40a12fba0dd5bef0dfcdf3067ea021d3a221faf4
author: Tor Andersson <tor@ccxvii.net>
date: Mon Jan 20 11:18:49 EST 2014
Add enum-to-string functions for syntax tree and opcodes.
--- a/jscompile.h
+++ b/jscompile.h
@@ -113,7 +113,7 @@
};
js_Function *jsC_compile(js_State *J, js_Ast *prog);
-
+const char *jsC_opcodestring(int opcode);
void jsC_dumpfunction(js_State *J, js_Function *fun);
#endif
--- a/jsdump.c
+++ b/jsdump.c
@@ -5,6 +5,8 @@
#include <assert.h>
+#define nelem(a) (sizeof (a) / sizeof (a)[0])
+
static const char *astname[] = {
"list", "fundec", "ident", "number", "string", "regexp", "undef",
"null", "true", "false", "this", "fun", "array", "object", "prop_val",
@@ -24,6 +26,20 @@
static const char *opname[] = {
#include "opnames.h"
};
+
+const char *jsP_aststring(js_AstType type)
+{
+ if (type < 0 || type > nelem(astname))
+ return "<unknown>";
+ return astname[type];
+}
+
+const char *jsC_opcodestring(int type)
+{
+ if (type < 0 || type > nelem(opname))
+ return "<unknown>";
+ return opname[type];
+}
static inline void pc(int c)
{
--- a/jsparse.h
+++ b/jsparse.h
@@ -128,6 +128,7 @@
void jsP_optimize(js_State *J, js_Ast *prog);
void jsP_freeparse(js_State *J);
+const char *jsP_aststring(js_AstType type);
void jsP_dumpsyntax(js_State *J, js_Ast *prog);
void jsP_dumplist(js_State *J, js_Ast *prog);
--- a/jsrun.c
+++ b/jsrun.c
@@ -808,7 +808,7 @@
return;
default:
- js_error(J, "illegal instruction: %d (pc=%d)", opcode, (int)(pc - F->code - 1));
+ js_error(J, "illegal instruction: %s (pc=%d)", jsC_opcodestring(opcode), (int)(pc - F->code - 1));
}
}
}