ref: 0d595bd6cf4efb2aa32837cd542a13d1ebbdb150
parent: 060dbbb03ca18062b4d6c3b0666b5924c592350b
author: Tor Andersson <[email protected]>
date: Fri Jan 17 16:09:53 EST 2014
Define some global functions and values.
--- a/jsbuiltin.c
+++ b/jsbuiltin.c
@@ -94,6 +94,35 @@
js_setglobal(J, "String");
}
+static int jsB_parseInt(js_State *J, int argc)
+{
+ const char *s = js_tostring(J, 1);
+ double radix = argc > 1 ? js_tonumber(J, 2) : 10;
+ js_pushnumber(J, strtol(s, NULL, radix == 0 ? 10 : radix));
+ return 1;
+}
+
+static int jsB_parseFloat(js_State *J, int argc)
+{
+ const char *s = js_tostring(J, 1);
+ js_pushnumber(J, strtod(s, NULL));
+ return 1;
+}
+
+static int jsB_isNaN(js_State *J, int argc)
+{
+ double n = js_tonumber(J, 1);
+ js_pushboolean(J, isnan(n));
+ return 1;
+}
+
+static int jsB_isFinite(js_State *J, int argc)
+{
+ double n = js_tonumber(J, 1);
+ js_pushboolean(J, isfinite(n));
+ return 1;
+}
+
static void jsB_register(js_State *J, const char *name, js_CFunction cfun)
{
js_pushcfunction(J, cfun);
@@ -109,6 +138,20 @@
jsB_initnumber(J);
jsB_initstring(J);
+ js_pushnumber(J, NAN);
+ js_setglobal(J, "NaN");
+
+ js_pushnumber(J, INFINITY);
+ js_setglobal(J, "Infinity");
+
+ js_pushundefined(J);
+ js_setglobal(J, "undefined");
+
jsB_register(J, "eval", jsB_eval);
+ jsB_register(J, "parseInt", jsB_parseInt);
+ jsB_register(J, "parseFloat", jsB_parseFloat);
+ jsB_register(J, "isNaN", jsB_isNaN);
+ jsB_register(J, "isFinite", jsB_isFinite);
+
jsB_register(J, "print", jsB_print);
}
--- a/jsrun.h
+++ b/jsrun.h
@@ -7,10 +7,14 @@
js_Object *variables;
};
+/* private */
void jsB_init(js_State *J);
js_Environment *jsR_newenvironment(js_State *J, js_Object *variables, js_Environment *outer);
int jsR_loadscript(js_State *J, const char *filename, const char *source);
void jsR_error(js_State *J, const char *fmt, ...);
+void js_pushobject(js_State *J, js_Object *v);
+
+/* public */
void js_call(js_State *J, int n);