shithub: libmujs

Download patch

ref: 3d29cd2f2f48db7510567e1f6d00483cf9b04a06
parent: 33ffe6efebf477ee1f73c27670af460ffd6cc08d
author: Tor Andersson <tor.andersson@artifex.com>
date: Fri Mar 26 11:47:23 EDT 2021

Issue #135: Expose type of value as an enum with js_type().

This matches the values used by the 'typeof' operator.

--- a/jsrun.c
+++ b/jsrun.c
@@ -249,6 +249,25 @@
 	}
 }
 
+int js_type(js_State *J, int idx)
+{
+	js_Value *v = stackidx(J, idx);
+	switch (v->type) {
+	default:
+	case JS_TSHRSTR: return JS_ISSTRING;
+	case JS_TUNDEFINED: return JS_ISUNDEFINED;
+	case JS_TNULL: return JS_ISNULL;
+	case JS_TBOOLEAN: return JS_ISBOOLEAN;
+	case JS_TNUMBER: return JS_ISNUMBER;
+	case JS_TLITSTR: return JS_ISSTRING;
+	case JS_TMEMSTR: return JS_ISSTRING;
+	case JS_TOBJECT:
+		if (v->u.object->type == JS_CFUNCTION || v->u.object->type == JS_CCFUNCTION)
+			return JS_ISFUNCTION;
+		return JS_ISOBJECT;
+	}
+}
+
 int js_toboolean(js_State *J, int idx)
 {
 	return jsV_toboolean(J, stackidx(J, idx));
--- a/mujs.h
+++ b/mujs.h
@@ -85,6 +85,17 @@
 	JS_DONTCONF = 4,
 };
 
+/* enum for js_type() */
+enum {
+	JS_ISUNDEFINED,
+	JS_ISNULL,
+	JS_ISBOOLEAN,
+	JS_ISNUMBER,
+	JS_ISSTRING,
+	JS_ISFUNCTION,
+	JS_ISOBJECT
+};
+
 void js_report(js_State *J, const char *message);
 
 void js_newerror(js_State *J, const char *message);
@@ -217,6 +228,7 @@
 int js_strictequal(js_State *J);
 int js_instanceof(js_State *J);
 const char *js_typeof(js_State *J, int idx);
+int js_type(js_State *J, int idx);
 
 void js_repr(js_State *J, int idx);
 const char *js_torepr(js_State *J, int idx);