mirror of
https://github.com/ccxvii/mujs.git
synced 2026-02-05 17:29:43 +08:00
Issue #135: Expose type of value as an enum with js_type().
This matches the values used by the 'typeof' operator.
This commit is contained in:
19
jsrun.c
19
jsrun.c
@@ -249,6 +249,25 @@ const char *js_typeof(js_State *J, int idx)
|
||||
}
|
||||
}
|
||||
|
||||
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));
|
||||
|
||||
12
mujs.h
12
mujs.h
@@ -85,6 +85,17 @@ enum {
|
||||
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_equal(js_State *J);
|
||||
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);
|
||||
|
||||
Reference in New Issue
Block a user