From 32c0c6c4103e8753ac8ad8ca581bec9c87362ca0 Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Sun, 19 Jan 2014 14:55:19 +0100 Subject: [PATCH] Rename jsR_numbertostring to jsR_stringfromnumber. Add js_tointeger, js_toint32, js_touint32. --- jsbnumber.c | 2 +- jsbuiltin.c | 2 +- jsrun.c | 15 +++++++++++++++ jsrun.h | 9 ++++++--- jsvalue.c | 8 ++++---- 5 files changed, 27 insertions(+), 9 deletions(-) diff --git a/jsbnumber.c b/jsbnumber.c index ee704d7..d69d310 100644 --- a/jsbnumber.c +++ b/jsbnumber.c @@ -27,7 +27,7 @@ static int Np_toString(js_State *J, int n) { js_Object *self = js_toobject(J, 0); if (self->type != JS_CNUMBER) jsR_error(J, "TypeError"); - js_pushliteral(J, jsR_numbertostring(J, self->u.number)); + js_pushliteral(J, jsR_stringfromnumber(J, self->u.number)); return 1; } diff --git a/jsbuiltin.c b/jsbuiltin.c index 38d51e5..cbe3646 100644 --- a/jsbuiltin.c +++ b/jsbuiltin.c @@ -49,7 +49,7 @@ static int jsB_parseInt(js_State *J, int argc) static int jsB_parseFloat(js_State *J, int argc) { const char *s = js_tostring(J, 1); - js_pushnumber(J, jsR_stringtonumber(J, s)); + js_pushnumber(J, jsR_numberfromstring(J, s)); return 1; } diff --git a/jsrun.c b/jsrun.c index 036f2e5..47e359f 100644 --- a/jsrun.c +++ b/jsrun.c @@ -155,6 +155,21 @@ double js_tonumber(js_State *J, int idx) return jsR_tonumber(J, stackidx(J, idx)); } +double js_tointeger(js_State *J, int idx) +{ + return tointeger(jsR_tonumber(J, stackidx(J, idx))); +} + +int js_toint32(js_State *J, int idx) +{ + return toint32(jsR_tonumber(J, stackidx(J, idx))); +} + +unsigned int js_touint32(js_State *J, int idx) +{ + return touint32(jsR_tonumber(J, stackidx(J, idx))); +} + const char *js_tostring(js_State *J, int idx) { return jsR_tostring(J, stackidx(J, idx)); diff --git a/jsrun.h b/jsrun.h index e018652..b365d52 100644 --- a/jsrun.h +++ b/jsrun.h @@ -28,8 +28,8 @@ int jsR_compare(js_State *J); int jsR_equal(js_State *J); int jsR_strictequal(js_State *J); -const char *jsR_numbertostring(js_State *J, double number); -double jsR_stringtonumber(js_State *J, const char *string); +const char *jsR_stringfromnumber(js_State *J, double number); +double jsR_numberfromstring(js_State *J, const char *string); /* public */ @@ -69,9 +69,12 @@ int js_iscallable(js_State *J, int idx); int js_toboolean(js_State *J, int idx); double js_tonumber(js_State *J, int idx); -double js_tointeger(js_State *J, int idx); const char *js_tostring(js_State *J, int idx); +double js_tointeger(js_State *J, int idx); +int js_toint32(js_State *J, int idx); +unsigned int js_touint32(js_State *J, int idx); + void js_pop(js_State *J, int n); void js_dup(js_State *J); void js_copy(js_State *J, int idx); diff --git a/jsvalue.c b/jsvalue.c index b275e37..75f21e3 100644 --- a/jsvalue.c +++ b/jsvalue.c @@ -2,7 +2,7 @@ #include "jsobject.h" #include "jsrun.h" -const char *jsR_numbertostring(js_State *J, double n) +const char *jsR_stringfromnumber(js_State *J, double n) { char buf[32]; if (isnan(n)) return "NaN"; @@ -12,7 +12,7 @@ const char *jsR_numbertostring(js_State *J, double n) return js_intern(J, buf); } -double jsR_stringtonumber(js_State *J, const char *s) +double jsR_numberfromstring(js_State *J, const char *s) { /* TODO: use lexer to parse string grammar */ return strtod(s, NULL); @@ -99,7 +99,7 @@ double jsR_tonumber(js_State *J, const js_Value *v) case JS_TNULL: return 0; case JS_TBOOLEAN: return v->u.boolean; case JS_TNUMBER: return v->u.number; - case JS_TSTRING: return jsR_stringtonumber(J, v->u.string); + case JS_TSTRING: return jsR_numberfromstring(J, v->u.string); case JS_TOBJECT: { js_Value vv = jsR_toprimitive(J, v, JS_HNUMBER); @@ -115,7 +115,7 @@ const char *jsR_tostring(js_State *J, const js_Value *v) case JS_TUNDEFINED: return "undefined"; case JS_TNULL: return "null"; case JS_TBOOLEAN: return v->u.boolean ? "true" : "false"; - case JS_TNUMBER: return jsR_numbertostring(J, v->u.number); + case JS_TNUMBER: return jsR_stringfromnumber(J, v->u.number); case JS_TSTRING: return v->u.string; case JS_TOBJECT: {