mirror of
https://github.com/ccxvii/mujs.git
synced 2026-02-06 01:41:37 +08:00
Change C functions to not have a return value.
They should always push a return value on the JS stack.
This commit is contained in:
2
js.h
2
js.h
@@ -26,7 +26,7 @@
|
||||
|
||||
typedef struct js_State js_State;
|
||||
|
||||
typedef int (*js_CFunction)(js_State *J, unsigned int argc);
|
||||
typedef void (*js_CFunction)(js_State *J, unsigned int argc);
|
||||
|
||||
/* Basic functions */
|
||||
js_State *js_newstate(void);
|
||||
|
||||
92
jsarray.c
92
jsarray.c
@@ -45,7 +45,7 @@ void js_delindex(js_State *J, int idx, unsigned int i)
|
||||
js_delproperty(J, idx, buf);
|
||||
}
|
||||
|
||||
static int jsB_new_Array(js_State *J, unsigned int argc)
|
||||
static void jsB_new_Array(js_State *J, unsigned int argc)
|
||||
{
|
||||
unsigned int i;
|
||||
|
||||
@@ -65,11 +65,9 @@ static int jsB_new_Array(js_State *J, unsigned int argc)
|
||||
js_setindex(J, -2, i - 1);
|
||||
}
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Ap_concat(js_State *J, unsigned int argc)
|
||||
static void Ap_concat(js_State *J, unsigned int argc)
|
||||
{
|
||||
unsigned int n, len, i;
|
||||
|
||||
@@ -95,11 +93,9 @@ static int Ap_concat(js_State *J, unsigned int argc)
|
||||
++n;
|
||||
}
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Ap_join(js_State *J, unsigned int argc)
|
||||
static void Ap_join(js_State *J, unsigned int argc)
|
||||
{
|
||||
char * volatile out = NULL;
|
||||
const char *sep;
|
||||
@@ -119,7 +115,7 @@ static int Ap_join(js_State *J, unsigned int argc)
|
||||
|
||||
if (len == 0) {
|
||||
js_pushliteral(J, "");
|
||||
return 1;
|
||||
return;
|
||||
}
|
||||
|
||||
if (js_try(J)) {
|
||||
@@ -152,10 +148,9 @@ static int Ap_join(js_State *J, unsigned int argc)
|
||||
js_pushstring(J, out);
|
||||
js_endtry(J);
|
||||
free(out);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Ap_pop(js_State *J, unsigned int argc)
|
||||
static void Ap_pop(js_State *J, unsigned int argc)
|
||||
{
|
||||
unsigned int n;
|
||||
|
||||
@@ -169,11 +164,9 @@ static int Ap_pop(js_State *J, unsigned int argc)
|
||||
js_setlength(J, 0, 0);
|
||||
js_pushundefined(J);
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Ap_push(js_State *J, unsigned int argc)
|
||||
static void Ap_push(js_State *J, unsigned int argc)
|
||||
{
|
||||
unsigned int i, n;
|
||||
|
||||
@@ -187,10 +180,9 @@ static int Ap_push(js_State *J, unsigned int argc)
|
||||
js_setlength(J, 0, n);
|
||||
|
||||
js_pushnumber(J, n);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Ap_reverse(js_State *J, unsigned int argc)
|
||||
static void Ap_reverse(js_State *J, unsigned int argc)
|
||||
{
|
||||
unsigned int len, middle, lower;
|
||||
|
||||
@@ -216,10 +208,9 @@ static int Ap_reverse(js_State *J, unsigned int argc)
|
||||
}
|
||||
|
||||
js_copy(J, 0);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Ap_shift(js_State *J, unsigned int argc)
|
||||
static void Ap_shift(js_State *J, unsigned int argc)
|
||||
{
|
||||
unsigned int k, len;
|
||||
|
||||
@@ -228,7 +219,7 @@ static int Ap_shift(js_State *J, unsigned int argc)
|
||||
if (len == 0) {
|
||||
js_setlength(J, 0, 0);
|
||||
js_pushundefined(J);
|
||||
return 1;
|
||||
return;
|
||||
}
|
||||
|
||||
js_getindex(J, 0, 0);
|
||||
@@ -242,11 +233,9 @@ static int Ap_shift(js_State *J, unsigned int argc)
|
||||
|
||||
js_delindex(J, 0, len - 1);
|
||||
js_setlength(J, 0, len - 1);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Ap_slice(js_State *J, unsigned int argc)
|
||||
static void Ap_slice(js_State *J, unsigned int argc)
|
||||
{
|
||||
unsigned int len, s, e, n;
|
||||
double sv, ev;
|
||||
@@ -266,8 +255,6 @@ static int Ap_slice(js_State *J, unsigned int argc)
|
||||
for (n = 0; s < e; ++s, ++n)
|
||||
if (js_hasindex(J, 0, s))
|
||||
js_setindex(J, -2, n);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int compare(js_State *J, unsigned int x, unsigned int y, int *hasx, int *hasy, int hasfn)
|
||||
@@ -306,7 +293,7 @@ static int compare(js_State *J, unsigned int x, unsigned int y, int *hasx, int *
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int Ap_sort(js_State *J, unsigned int argc)
|
||||
static void Ap_sort(js_State *J, unsigned int argc)
|
||||
{
|
||||
unsigned int len, i, k;
|
||||
int hasx, hasy, hasfn;
|
||||
@@ -333,10 +320,9 @@ static int Ap_sort(js_State *J, unsigned int argc)
|
||||
}
|
||||
|
||||
js_copy(J, 0);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Ap_splice(js_State *J, unsigned int argc)
|
||||
static void Ap_splice(js_State *J, unsigned int argc)
|
||||
{
|
||||
unsigned int len, start, del, add, k;
|
||||
double f;
|
||||
@@ -384,11 +370,9 @@ static int Ap_splice(js_State *J, unsigned int argc)
|
||||
}
|
||||
|
||||
js_setlength(J, 0, len - del + add);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Ap_unshift(js_State *J, unsigned int argc)
|
||||
static void Ap_unshift(js_State *J, unsigned int argc)
|
||||
{
|
||||
unsigned int i, k, len;
|
||||
|
||||
@@ -411,16 +395,15 @@ static int Ap_unshift(js_State *J, unsigned int argc)
|
||||
js_setlength(J, 0, len + argc);
|
||||
|
||||
js_pushnumber(J, len + argc);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Ap_toString(js_State *J, unsigned int argc)
|
||||
static void Ap_toString(js_State *J, unsigned int argc)
|
||||
{
|
||||
js_pop(J, argc);
|
||||
return Ap_join(J, 0);
|
||||
}
|
||||
|
||||
static int Ap_indexOf(js_State *J, unsigned int argc)
|
||||
static void Ap_indexOf(js_State *J, unsigned int argc)
|
||||
{
|
||||
int k, len, from;
|
||||
|
||||
@@ -434,17 +417,16 @@ static int Ap_indexOf(js_State *J, unsigned int argc)
|
||||
if (js_hasindex(J, 0, k)) {
|
||||
if (js_strictequal(J)) {
|
||||
js_pushnumber(J, k);
|
||||
return 1;
|
||||
return;
|
||||
}
|
||||
js_pop(J, 1);
|
||||
}
|
||||
}
|
||||
|
||||
js_pushnumber(J, -1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Ap_lastIndexOf(js_State *J, unsigned int argc)
|
||||
static void Ap_lastIndexOf(js_State *J, unsigned int argc)
|
||||
{
|
||||
int k, len, from;
|
||||
|
||||
@@ -458,17 +440,16 @@ static int Ap_lastIndexOf(js_State *J, unsigned int argc)
|
||||
if (js_hasindex(J, 0, k)) {
|
||||
if (js_strictequal(J)) {
|
||||
js_pushnumber(J, k);
|
||||
return 1;
|
||||
return;
|
||||
}
|
||||
js_pop(J, 1);
|
||||
}
|
||||
}
|
||||
|
||||
js_pushnumber(J, -1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Ap_every(js_State *J, unsigned int argc)
|
||||
static void Ap_every(js_State *J, unsigned int argc)
|
||||
{
|
||||
int k, len;
|
||||
|
||||
@@ -488,16 +469,15 @@ static int Ap_every(js_State *J, unsigned int argc)
|
||||
js_copy(J, 0);
|
||||
js_call(J, 3);
|
||||
if (!js_toboolean(J, -1))
|
||||
return 1;
|
||||
return;
|
||||
js_pop(J, 2);
|
||||
}
|
||||
}
|
||||
|
||||
js_pushboolean(J, 1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Ap_some(js_State *J, unsigned int argc)
|
||||
static void Ap_some(js_State *J, unsigned int argc)
|
||||
{
|
||||
int k, len;
|
||||
|
||||
@@ -517,16 +497,15 @@ static int Ap_some(js_State *J, unsigned int argc)
|
||||
js_copy(J, 0);
|
||||
js_call(J, 3);
|
||||
if (js_toboolean(J, -1))
|
||||
return 1;
|
||||
return;
|
||||
js_pop(J, 2);
|
||||
}
|
||||
}
|
||||
|
||||
js_pushboolean(J, 0);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Ap_forEach(js_State *J, unsigned int argc)
|
||||
static void Ap_forEach(js_State *J, unsigned int argc)
|
||||
{
|
||||
int k, len;
|
||||
|
||||
@@ -549,10 +528,10 @@ static int Ap_forEach(js_State *J, unsigned int argc)
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
js_pushundefined(J);
|
||||
}
|
||||
|
||||
static int Ap_map(js_State *J, unsigned int argc)
|
||||
static void Ap_map(js_State *J, unsigned int argc)
|
||||
{
|
||||
int k, len;
|
||||
|
||||
@@ -577,11 +556,9 @@ static int Ap_map(js_State *J, unsigned int argc)
|
||||
js_pop(J, 1);
|
||||
}
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Ap_filter(js_State *J, unsigned int argc)
|
||||
static void Ap_filter(js_State *J, unsigned int argc)
|
||||
{
|
||||
int k, to, len;
|
||||
|
||||
@@ -611,11 +588,9 @@ static int Ap_filter(js_State *J, unsigned int argc)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Ap_reduce(js_State *J, unsigned int argc)
|
||||
static void Ap_reduce(js_State *J, unsigned int argc)
|
||||
{
|
||||
int k, len;
|
||||
|
||||
@@ -652,10 +627,10 @@ static int Ap_reduce(js_State *J, unsigned int argc)
|
||||
++k;
|
||||
}
|
||||
|
||||
return 1; /* return accumulator */
|
||||
/* return accumulator */
|
||||
}
|
||||
|
||||
static int Ap_reduceRight(js_State *J, unsigned int argc)
|
||||
static void Ap_reduceRight(js_State *J, unsigned int argc)
|
||||
{
|
||||
int k, len;
|
||||
|
||||
@@ -692,18 +667,17 @@ static int Ap_reduceRight(js_State *J, unsigned int argc)
|
||||
--k;
|
||||
}
|
||||
|
||||
return 1; /* return accumulator */
|
||||
/* return accumulator */
|
||||
}
|
||||
|
||||
static int A_isArray(js_State *J, unsigned int argc)
|
||||
static void A_isArray(js_State *J, unsigned int argc)
|
||||
{
|
||||
if (js_isobject(J, 1)) {
|
||||
js_Object *T = js_toobject(J, 1);
|
||||
js_pushboolean(J, T->type == JS_CARRAY);
|
||||
return 1;
|
||||
} else {
|
||||
js_pushboolean(J, 0);
|
||||
}
|
||||
js_pushboolean(J, 0);
|
||||
return 1;
|
||||
}
|
||||
|
||||
void jsB_initarray(js_State *J)
|
||||
|
||||
12
jsboolean.c
12
jsboolean.c
@@ -2,32 +2,28 @@
|
||||
#include "jsvalue.h"
|
||||
#include "jsbuiltin.h"
|
||||
|
||||
static int jsB_new_Boolean(js_State *J, unsigned int argc)
|
||||
static void jsB_new_Boolean(js_State *J, unsigned int argc)
|
||||
{
|
||||
js_newboolean(J, js_toboolean(J, 1));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int jsB_Boolean(js_State *J, unsigned int argc)
|
||||
static void jsB_Boolean(js_State *J, unsigned int argc)
|
||||
{
|
||||
js_pushboolean(J, js_toboolean(J, 1));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Bp_toString(js_State *J, unsigned int argc)
|
||||
static void Bp_toString(js_State *J, unsigned int argc)
|
||||
{
|
||||
js_Object *self = js_toobject(J, 0);
|
||||
if (self->type != JS_CBOOLEAN) js_typeerror(J, "not a boolean");
|
||||
js_pushliteral(J, self->u.boolean ? "true" : "false");
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Bp_valueOf(js_State *J, unsigned int argc)
|
||||
static void Bp_valueOf(js_State *J, unsigned int argc)
|
||||
{
|
||||
js_Object *self = js_toobject(J, 0);
|
||||
if (self->type != JS_CBOOLEAN) js_typeerror(J, "not a boolean");
|
||||
js_pushboolean(J, self->u.boolean);
|
||||
return 1;
|
||||
}
|
||||
|
||||
void jsB_initboolean(js_State *J)
|
||||
|
||||
39
jsbuiltin.c
39
jsbuiltin.c
@@ -28,50 +28,45 @@ void jsB_props(js_State *J, const char *name, const char *string)
|
||||
js_defproperty(J, -2, name, JS_DONTENUM);
|
||||
}
|
||||
|
||||
static int jsB_eval(js_State *J, unsigned int argc)
|
||||
static void jsB_eval(js_State *J, unsigned int argc)
|
||||
{
|
||||
if (!js_isstring(J, -1)) {
|
||||
js_copy(J, 1);
|
||||
return 1;
|
||||
return;
|
||||
}
|
||||
js_loadstring(J, "(eval)", js_tostring(J, -1));
|
||||
js_pushglobal(J);
|
||||
js_call(J, 0);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int jsB_parseInt(js_State *J, unsigned int argc)
|
||||
static void jsB_parseInt(js_State *J, unsigned int argc)
|
||||
{
|
||||
const char *s = js_tostring(J, 1);
|
||||
double radix = js_isdefined(J, 2) ? js_tonumber(J, 2) : 10;
|
||||
while (jsY_iswhite(*s) || jsY_isnewline(*s)) ++s;
|
||||
js_pushnumber(J, strtol(s, NULL, radix == 0 ? 10 : radix));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int jsB_parseFloat(js_State *J, unsigned int argc)
|
||||
static void jsB_parseFloat(js_State *J, unsigned int argc)
|
||||
{
|
||||
const char *s = js_tostring(J, 1);
|
||||
while (jsY_iswhite(*s) || jsY_isnewline(*s)) ++s;
|
||||
js_pushnumber(J, strtod(s, NULL));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int jsB_isNaN(js_State *J, unsigned int argc)
|
||||
static void jsB_isNaN(js_State *J, unsigned int argc)
|
||||
{
|
||||
double n = js_tonumber(J, 1);
|
||||
js_pushboolean(J, isnan(n));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int jsB_isFinite(js_State *J, unsigned int argc)
|
||||
static void jsB_isFinite(js_State *J, unsigned int argc)
|
||||
{
|
||||
double n = js_tonumber(J, 1);
|
||||
js_pushboolean(J, isfinite(n));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Encode(js_State *J, const char *str, const char *unescaped)
|
||||
static void Encode(js_State *J, const char *str, const char *unescaped)
|
||||
{
|
||||
js_Buffer *sb = NULL;
|
||||
|
||||
@@ -96,10 +91,9 @@ static int Encode(js_State *J, const char *str, const char *unescaped)
|
||||
js_pushstring(J, sb ? sb->s : "");
|
||||
js_endtry(J);
|
||||
free(sb);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Decode(js_State *J, const char *str, const char *reserved)
|
||||
static void Decode(js_State *J, const char *str, const char *reserved)
|
||||
{
|
||||
js_Buffer *sb = NULL;
|
||||
int a, b;
|
||||
@@ -134,7 +128,6 @@ static int Decode(js_State *J, const char *str, const char *reserved)
|
||||
js_pushstring(J, sb ? sb->s : "");
|
||||
js_endtry(J);
|
||||
free(sb);
|
||||
return 1;
|
||||
}
|
||||
|
||||
#define URIRESERVED ";/?:@&=+$,"
|
||||
@@ -143,24 +136,24 @@ static int Decode(js_State *J, const char *str, const char *reserved)
|
||||
#define URIMARK "-_.!~*`()"
|
||||
#define URIUNESCAPED URIALPHA URIDIGIT URIMARK
|
||||
|
||||
static int jsB_decodeURI(js_State *J, unsigned int argc)
|
||||
static void jsB_decodeURI(js_State *J, unsigned int argc)
|
||||
{
|
||||
return Decode(J, js_tostring(J, 1), URIRESERVED "#");
|
||||
Decode(J, js_tostring(J, 1), URIRESERVED "#");
|
||||
}
|
||||
|
||||
static int jsB_decodeURIComponent(js_State *J, unsigned int argc)
|
||||
static void jsB_decodeURIComponent(js_State *J, unsigned int argc)
|
||||
{
|
||||
return Decode(J, js_tostring(J, 1), "");
|
||||
Decode(J, js_tostring(J, 1), "");
|
||||
}
|
||||
|
||||
static int jsB_encodeURI(js_State *J, unsigned int argc)
|
||||
static void jsB_encodeURI(js_State *J, unsigned int argc)
|
||||
{
|
||||
return Encode(J, js_tostring(J, 1), URIUNESCAPED URIRESERVED "#");
|
||||
Encode(J, js_tostring(J, 1), URIUNESCAPED URIRESERVED "#");
|
||||
}
|
||||
|
||||
static int jsB_encodeURIComponent(js_State *J, unsigned int argc)
|
||||
static void jsB_encodeURIComponent(js_State *J, unsigned int argc)
|
||||
{
|
||||
return Encode(J, js_tostring(J, 1), URIUNESCAPED);
|
||||
Encode(J, js_tostring(J, 1), URIUNESCAPED);
|
||||
}
|
||||
|
||||
void jsB_init(js_State *J)
|
||||
|
||||
152
jsdate.c
152
jsdate.c
@@ -253,24 +253,22 @@ static double js_todate(js_State *J, int idx)
|
||||
return self->u.number;
|
||||
}
|
||||
|
||||
static int js_setdate(js_State *J, int idx, double t)
|
||||
static void js_setdate(js_State *J, int idx, double t)
|
||||
{
|
||||
js_Object *self = js_toobject(J, idx);
|
||||
if (self->type != JS_CDATE)
|
||||
js_typeerror(J, "not a date");
|
||||
self->u.number = TimeClip(t);
|
||||
js_pushnumber(J, self->u.number);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int D_parse(js_State *J, unsigned int argc)
|
||||
static void D_parse(js_State *J, unsigned int argc)
|
||||
{
|
||||
double t = parseDate(js_tostring(J, 1));
|
||||
js_pushnumber(J, t);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int D_UTC(js_State *J, unsigned int argc)
|
||||
static void D_UTC(js_State *J, unsigned int argc)
|
||||
{
|
||||
double y, m, d, H, M, S, ms, t;
|
||||
y = js_tonumber(J, 1);
|
||||
@@ -284,22 +282,19 @@ static int D_UTC(js_State *J, unsigned int argc)
|
||||
t = MakeDate(MakeDay(y, m, d), MakeTime(H, M, S, ms));
|
||||
t = TimeClip(t);
|
||||
js_pushnumber(J, t);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int D_now(js_State *J, unsigned int argc)
|
||||
static void D_now(js_State *J, unsigned int argc)
|
||||
{
|
||||
js_pushnumber(J, Now());
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int jsB_Date(js_State *J, unsigned int argc)
|
||||
static void jsB_Date(js_State *J, unsigned int argc)
|
||||
{
|
||||
js_pushstring(J, fmtlocal(FMT_DATETIME, Now()));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int jsB_new_Date(js_State *J, unsigned int argc)
|
||||
static void jsB_new_Date(js_State *J, unsigned int argc)
|
||||
{
|
||||
js_Object *obj;
|
||||
js_Value v;
|
||||
@@ -331,316 +326,292 @@ static int jsB_new_Date(js_State *J, unsigned int argc)
|
||||
obj->u.number = t;
|
||||
|
||||
js_pushobject(J, obj);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Dp_valueOf(js_State *J, unsigned int argc)
|
||||
static void Dp_valueOf(js_State *J, unsigned int argc)
|
||||
{
|
||||
double t = js_todate(J, 0);
|
||||
js_pushnumber(J, t);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Dp_toString(js_State *J, unsigned int argc)
|
||||
static void Dp_toString(js_State *J, unsigned int argc)
|
||||
{
|
||||
double t = js_todate(J, 0);
|
||||
js_pushstring(J, fmtlocal(FMT_DATETIME, t));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Dp_toDateString(js_State *J, unsigned int argc)
|
||||
static void Dp_toDateString(js_State *J, unsigned int argc)
|
||||
{
|
||||
double t = js_todate(J, 0);
|
||||
js_pushstring(J, fmtlocal(FMT_DATE, t));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Dp_toTimeString(js_State *J, unsigned int argc)
|
||||
static void Dp_toTimeString(js_State *J, unsigned int argc)
|
||||
{
|
||||
double t = js_todate(J, 0);
|
||||
js_pushstring(J, fmtlocal(FMT_TIME, t));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Dp_toUTCString(js_State *J, unsigned int argc)
|
||||
static void Dp_toUTCString(js_State *J, unsigned int argc)
|
||||
{
|
||||
double t = js_todate(J, 0);
|
||||
js_pushstring(J, fmtutc(FMT_DATETIME, t));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Dp_toISOString(js_State *J, unsigned int argc)
|
||||
static void Dp_toISOString(js_State *J, unsigned int argc)
|
||||
{
|
||||
double t = js_todate(J, 0);
|
||||
js_pushstring(J, fmtutc(FMT_DATETIME_ISO, t));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Dp_getFullYear(js_State *J, unsigned int argc)
|
||||
static void Dp_getFullYear(js_State *J, unsigned int argc)
|
||||
{
|
||||
double t = js_todate(J, 0);
|
||||
js_pushnumber(J, YearFromTime(LocalTime(t)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Dp_getMonth(js_State *J, unsigned int argc)
|
||||
static void Dp_getMonth(js_State *J, unsigned int argc)
|
||||
{
|
||||
double t = js_todate(J, 0);
|
||||
js_pushnumber(J, MonthFromTime(LocalTime(t)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Dp_getDate(js_State *J, unsigned int argc)
|
||||
static void Dp_getDate(js_State *J, unsigned int argc)
|
||||
{
|
||||
double t = js_todate(J, 0);
|
||||
js_pushnumber(J, DateFromTime(LocalTime(t)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Dp_getDay(js_State *J, unsigned int argc)
|
||||
static void Dp_getDay(js_State *J, unsigned int argc)
|
||||
{
|
||||
double t = js_todate(J, 0);
|
||||
js_pushnumber(J, WeekDay(LocalTime(t)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Dp_getHours(js_State *J, unsigned int argc)
|
||||
static void Dp_getHours(js_State *J, unsigned int argc)
|
||||
{
|
||||
double t = js_todate(J, 0);
|
||||
js_pushnumber(J, HourFromTime(LocalTime(t)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Dp_getMinutes(js_State *J, unsigned int argc)
|
||||
static void Dp_getMinutes(js_State *J, unsigned int argc)
|
||||
{
|
||||
double t = js_todate(J, 0);
|
||||
js_pushnumber(J, MinFromTime(LocalTime(t)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Dp_getSeconds(js_State *J, unsigned int argc)
|
||||
static void Dp_getSeconds(js_State *J, unsigned int argc)
|
||||
{
|
||||
double t = js_todate(J, 0);
|
||||
js_pushnumber(J, SecFromTime(LocalTime(t)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Dp_getMilliseconds(js_State *J, unsigned int argc)
|
||||
static void Dp_getMilliseconds(js_State *J, unsigned int argc)
|
||||
{
|
||||
double t = js_todate(J, 0);
|
||||
js_pushnumber(J, msFromTime(LocalTime(t)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Dp_getUTCFullYear(js_State *J, unsigned int argc)
|
||||
static void Dp_getUTCFullYear(js_State *J, unsigned int argc)
|
||||
{
|
||||
double t = js_todate(J, 0);
|
||||
js_pushnumber(J, YearFromTime(t));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Dp_getUTCMonth(js_State *J, unsigned int argc)
|
||||
static void Dp_getUTCMonth(js_State *J, unsigned int argc)
|
||||
{
|
||||
double t = js_todate(J, 0);
|
||||
js_pushnumber(J, MonthFromTime(t));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Dp_getUTCDate(js_State *J, unsigned int argc)
|
||||
static void Dp_getUTCDate(js_State *J, unsigned int argc)
|
||||
{
|
||||
double t = js_todate(J, 0);
|
||||
js_pushnumber(J, DateFromTime(t));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Dp_getUTCDay(js_State *J, unsigned int argc)
|
||||
static void Dp_getUTCDay(js_State *J, unsigned int argc)
|
||||
{
|
||||
double t = js_todate(J, 0);
|
||||
js_pushnumber(J, WeekDay(t));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Dp_getUTCHours(js_State *J, unsigned int argc)
|
||||
static void Dp_getUTCHours(js_State *J, unsigned int argc)
|
||||
{
|
||||
double t = js_todate(J, 0);
|
||||
js_pushnumber(J, HourFromTime(t));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Dp_getUTCMinutes(js_State *J, unsigned int argc)
|
||||
static void Dp_getUTCMinutes(js_State *J, unsigned int argc)
|
||||
{
|
||||
double t = js_todate(J, 0);
|
||||
js_pushnumber(J, MinFromTime(t));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Dp_getUTCSeconds(js_State *J, unsigned int argc)
|
||||
static void Dp_getUTCSeconds(js_State *J, unsigned int argc)
|
||||
{
|
||||
double t = js_todate(J, 0);
|
||||
js_pushnumber(J, SecFromTime(t));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Dp_getUTCMilliseconds(js_State *J, unsigned int argc)
|
||||
static void Dp_getUTCMilliseconds(js_State *J, unsigned int argc)
|
||||
{
|
||||
double t = js_todate(J, 0);
|
||||
js_pushnumber(J, msFromTime(t));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Dp_getTimezoneOffset(js_State *J, unsigned int argc)
|
||||
static void Dp_getTimezoneOffset(js_State *J, unsigned int argc)
|
||||
{
|
||||
double t = js_todate(J, 0);
|
||||
js_pushnumber(J, (t - LocalTime(t)) / msPerMinute);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Dp_setTime(js_State *J, unsigned int argc)
|
||||
static void Dp_setTime(js_State *J, unsigned int argc)
|
||||
{
|
||||
return js_setdate(J, 0, js_tonumber(J, 1));
|
||||
js_setdate(J, 0, js_tonumber(J, 1));
|
||||
}
|
||||
|
||||
static int Dp_setMilliseconds(js_State *J, unsigned int argc)
|
||||
static void Dp_setMilliseconds(js_State *J, unsigned int argc)
|
||||
{
|
||||
double t = LocalTime(js_todate(J, 0));
|
||||
double h = HourFromTime(t);
|
||||
double m = MinFromTime(t);
|
||||
double s = SecFromTime(t);
|
||||
double ms = js_tonumber(J, 1);
|
||||
return js_setdate(J, 0, UTC(MakeDate(Day(t), MakeTime(h, m, s, ms))));
|
||||
js_setdate(J, 0, UTC(MakeDate(Day(t), MakeTime(h, m, s, ms))));
|
||||
}
|
||||
|
||||
static int Dp_setSeconds(js_State *J, unsigned int argc)
|
||||
static void Dp_setSeconds(js_State *J, unsigned int argc)
|
||||
{
|
||||
double t = LocalTime(js_todate(J, 0));
|
||||
double h = HourFromTime(t);
|
||||
double m = MinFromTime(t);
|
||||
double s = js_tonumber(J, 1);
|
||||
double ms = argc > 1 ? js_tonumber(J, 2) : msFromTime(t);
|
||||
return js_setdate(J, 0, UTC(MakeDate(Day(t), MakeTime(h, m, s, ms))));
|
||||
js_setdate(J, 0, UTC(MakeDate(Day(t), MakeTime(h, m, s, ms))));
|
||||
}
|
||||
|
||||
static int Dp_setMinutes(js_State *J, unsigned int argc)
|
||||
static void Dp_setMinutes(js_State *J, unsigned int argc)
|
||||
{
|
||||
double t = LocalTime(js_todate(J, 0));
|
||||
double h = HourFromTime(t);
|
||||
double m = js_tonumber(J, 1);
|
||||
double s = argc > 1 ? js_tonumber(J, 2) : SecFromTime(t);
|
||||
double ms = argc > 2 ? js_tonumber(J, 3) : msFromTime(t);
|
||||
return js_setdate(J, 0, UTC(MakeDate(Day(t), MakeTime(h, m, s, ms))));
|
||||
js_setdate(J, 0, UTC(MakeDate(Day(t), MakeTime(h, m, s, ms))));
|
||||
}
|
||||
|
||||
static int Dp_setHours(js_State *J, unsigned int argc)
|
||||
static void Dp_setHours(js_State *J, unsigned int argc)
|
||||
{
|
||||
double t = LocalTime(js_todate(J, 0));
|
||||
double h = js_tonumber(J, 1);
|
||||
double m = argc > 1 ? js_tonumber(J, 2) : HourFromTime(t);
|
||||
double s = argc > 2 ? js_tonumber(J, 3) : SecFromTime(t);
|
||||
double ms = argc > 3 ? js_tonumber(J, 4) : msFromTime(t);
|
||||
return js_setdate(J, 0, UTC(MakeDate(Day(t), MakeTime(h, m, s, ms))));
|
||||
js_setdate(J, 0, UTC(MakeDate(Day(t), MakeTime(h, m, s, ms))));
|
||||
}
|
||||
|
||||
static int Dp_setDate(js_State *J, unsigned int argc)
|
||||
static void Dp_setDate(js_State *J, unsigned int argc)
|
||||
{
|
||||
double t = LocalTime(js_todate(J, 0));
|
||||
double y = YearFromTime(t);
|
||||
double m = MonthFromTime(t);
|
||||
double d = js_tonumber(J, 1);
|
||||
return js_setdate(J, 0, UTC(MakeDate(MakeDay(y, m, d), TimeWithinDay(t))));
|
||||
js_setdate(J, 0, UTC(MakeDate(MakeDay(y, m, d), TimeWithinDay(t))));
|
||||
}
|
||||
|
||||
static int Dp_setMonth(js_State *J, unsigned int argc)
|
||||
static void Dp_setMonth(js_State *J, unsigned int argc)
|
||||
{
|
||||
double t = LocalTime(js_todate(J, 0));
|
||||
double y = YearFromTime(t);
|
||||
double m = js_tonumber(J, 1);
|
||||
double d = argc > 1 ? js_tonumber(J, 3) : DateFromTime(t);
|
||||
return js_setdate(J, 0, UTC(MakeDate(MakeDay(y, m, d), TimeWithinDay(t))));
|
||||
js_setdate(J, 0, UTC(MakeDate(MakeDay(y, m, d), TimeWithinDay(t))));
|
||||
}
|
||||
|
||||
static int Dp_setFullYear(js_State *J, unsigned int argc)
|
||||
static void Dp_setFullYear(js_State *J, unsigned int argc)
|
||||
{
|
||||
double t = LocalTime(js_todate(J, 0));
|
||||
double y = js_tonumber(J, 1);
|
||||
double m = argc > 1 ? js_tonumber(J, 2) : MonthFromTime(t);
|
||||
double d = argc > 2 ? js_tonumber(J, 3) : DateFromTime(t);
|
||||
return js_setdate(J, 0, UTC(MakeDate(MakeDay(y, m, d), TimeWithinDay(t))));
|
||||
js_setdate(J, 0, UTC(MakeDate(MakeDay(y, m, d), TimeWithinDay(t))));
|
||||
}
|
||||
|
||||
static int Dp_setUTCMilliseconds(js_State *J, unsigned int argc)
|
||||
static void Dp_setUTCMilliseconds(js_State *J, unsigned int argc)
|
||||
{
|
||||
double t = js_todate(J, 0);
|
||||
double h = HourFromTime(t);
|
||||
double m = MinFromTime(t);
|
||||
double s = SecFromTime(t);
|
||||
double ms = js_tonumber(J, 1);
|
||||
return js_setdate(J, 0, MakeDate(Day(t), MakeTime(h, m, s, ms)));
|
||||
js_setdate(J, 0, MakeDate(Day(t), MakeTime(h, m, s, ms)));
|
||||
}
|
||||
|
||||
static int Dp_setUTCSeconds(js_State *J, unsigned int argc)
|
||||
static void Dp_setUTCSeconds(js_State *J, unsigned int argc)
|
||||
{
|
||||
double t = js_todate(J, 0);
|
||||
double h = HourFromTime(t);
|
||||
double m = MinFromTime(t);
|
||||
double s = js_tonumber(J, 1);
|
||||
double ms = argc > 1 ? js_tonumber(J, 2) : msFromTime(t);
|
||||
return js_setdate(J, 0, MakeDate(Day(t), MakeTime(h, m, s, ms)));
|
||||
js_setdate(J, 0, MakeDate(Day(t), MakeTime(h, m, s, ms)));
|
||||
}
|
||||
|
||||
static int Dp_setUTCMinutes(js_State *J, unsigned int argc)
|
||||
static void Dp_setUTCMinutes(js_State *J, unsigned int argc)
|
||||
{
|
||||
double t = js_todate(J, 0);
|
||||
double h = HourFromTime(t);
|
||||
double m = js_tonumber(J, 1);
|
||||
double s = argc > 1 ? js_tonumber(J, 2) : SecFromTime(t);
|
||||
double ms = argc > 2 ? js_tonumber(J, 3) : msFromTime(t);
|
||||
return js_setdate(J, 0, MakeDate(Day(t), MakeTime(h, m, s, ms)));
|
||||
js_setdate(J, 0, MakeDate(Day(t), MakeTime(h, m, s, ms)));
|
||||
}
|
||||
|
||||
static int Dp_setUTCHours(js_State *J, unsigned int argc)
|
||||
static void Dp_setUTCHours(js_State *J, unsigned int argc)
|
||||
{
|
||||
double t = js_todate(J, 0);
|
||||
double h = js_tonumber(J, 1);
|
||||
double m = argc > 1 ? js_tonumber(J, 2) : HourFromTime(t);
|
||||
double s = argc > 2 ? js_tonumber(J, 3) : SecFromTime(t);
|
||||
double ms = argc > 3 ? js_tonumber(J, 4) : msFromTime(t);
|
||||
return js_setdate(J, 0, MakeDate(Day(t), MakeTime(h, m, s, ms)));
|
||||
js_setdate(J, 0, MakeDate(Day(t), MakeTime(h, m, s, ms)));
|
||||
}
|
||||
|
||||
static int Dp_setUTCDate(js_State *J, unsigned int argc)
|
||||
static void Dp_setUTCDate(js_State *J, unsigned int argc)
|
||||
{
|
||||
double t = js_todate(J, 0);
|
||||
double y = YearFromTime(t);
|
||||
double m = MonthFromTime(t);
|
||||
double d = js_tonumber(J, 1);
|
||||
return js_setdate(J, 0, MakeDate(MakeDay(y, m, d), TimeWithinDay(t)));
|
||||
js_setdate(J, 0, MakeDate(MakeDay(y, m, d), TimeWithinDay(t)));
|
||||
}
|
||||
|
||||
static int Dp_setUTCMonth(js_State *J, unsigned int argc)
|
||||
static void Dp_setUTCMonth(js_State *J, unsigned int argc)
|
||||
{
|
||||
double t = js_todate(J, 0);
|
||||
double y = YearFromTime(t);
|
||||
double m = js_tonumber(J, 1);
|
||||
double d = argc > 1 ? js_tonumber(J, 3) : DateFromTime(t);
|
||||
return js_setdate(J, 0, MakeDate(MakeDay(y, m, d), TimeWithinDay(t)));
|
||||
js_setdate(J, 0, MakeDate(MakeDay(y, m, d), TimeWithinDay(t)));
|
||||
}
|
||||
|
||||
static int Dp_setUTCFullYear(js_State *J, unsigned int argc)
|
||||
static void Dp_setUTCFullYear(js_State *J, unsigned int argc)
|
||||
{
|
||||
double t = js_todate(J, 0);
|
||||
double y = js_tonumber(J, 1);
|
||||
double m = argc > 1 ? js_tonumber(J, 2) : MonthFromTime(t);
|
||||
double d = argc > 2 ? js_tonumber(J, 3) : DateFromTime(t);
|
||||
return js_setdate(J, 0, MakeDate(MakeDay(y, m, d), TimeWithinDay(t)));
|
||||
js_setdate(J, 0, MakeDate(MakeDay(y, m, d), TimeWithinDay(t)));
|
||||
}
|
||||
|
||||
static int Dp_toJSON(js_State *J, unsigned int argc)
|
||||
static void Dp_toJSON(js_State *J, unsigned int argc)
|
||||
{
|
||||
js_Object *obj = js_toobject(J, 0);
|
||||
js_Value tv = js_toprimitive(J, 0, JS_HNUMBER);
|
||||
if (tv.type == JS_TNULL && !isfinite(tv.u.number)) {
|
||||
js_pushnull(J);
|
||||
return 1;
|
||||
return;
|
||||
}
|
||||
js_pushobject(J, obj);
|
||||
js_getproperty(J, -1, "toISOString");
|
||||
@@ -648,7 +619,6 @@ static int Dp_toJSON(js_State *J, unsigned int argc)
|
||||
js_typeerror(J, "Date.prototype.toJSON: this.toISOString not a function");
|
||||
js_pushobject(J, obj);
|
||||
js_call(J, 0);
|
||||
return 1;
|
||||
}
|
||||
|
||||
void jsB_initdate(js_State *J)
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
#define QQ(X) #X
|
||||
#define Q(X) QQ(X)
|
||||
|
||||
static int Ep_toString(js_State *J, unsigned int argc)
|
||||
static void Ep_toString(js_State *J, unsigned int argc)
|
||||
{
|
||||
const char *name = "Error";
|
||||
const char *message = "";
|
||||
@@ -34,7 +34,6 @@ static int Ep_toString(js_State *J, unsigned int argc)
|
||||
js_pushliteral(J, message);
|
||||
js_concat(J);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int jsB_ErrorX(js_State *J, unsigned int argc, js_Object *prototype)
|
||||
@@ -55,8 +54,8 @@ static void js_newerrorx(js_State *J, const char *message, js_Object *prototype)
|
||||
}
|
||||
|
||||
#define DERROR(name, Name) \
|
||||
static int jsB_##Name(js_State *J, unsigned int argc) { \
|
||||
return jsB_ErrorX(J, argc, J->Name##_prototype); \
|
||||
static void jsB_##Name(js_State *J, unsigned int argc) { \
|
||||
jsB_ErrorX(J, argc, J->Name##_prototype); \
|
||||
} \
|
||||
void js_new##name(js_State *J, const char *s) { \
|
||||
js_newerrorx(J, s, J->Name##_prototype); \
|
||||
|
||||
16
jsfunction.c
16
jsfunction.c
@@ -4,7 +4,7 @@
|
||||
#include "jsvalue.h"
|
||||
#include "jsbuiltin.h"
|
||||
|
||||
static int jsB_Function(js_State *J, unsigned int argc)
|
||||
static void jsB_Function(js_State *J, unsigned int argc)
|
||||
{
|
||||
const char *source;
|
||||
js_Buffer *sb;
|
||||
@@ -40,16 +40,14 @@ static int jsB_Function(js_State *J, unsigned int argc)
|
||||
jsP_freeparse(J);
|
||||
|
||||
js_newfunction(J, fun, J->GE);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int jsB_Function_prototype(js_State *J, unsigned int argc)
|
||||
static void jsB_Function_prototype(js_State *J, unsigned int argc)
|
||||
{
|
||||
js_pushundefined(J);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Fp_toString(js_State *J, unsigned int argc)
|
||||
static void Fp_toString(js_State *J, unsigned int argc)
|
||||
{
|
||||
js_Object *self = js_toobject(J, 0);
|
||||
char *s;
|
||||
@@ -78,11 +76,9 @@ static int Fp_toString(js_State *J, unsigned int argc)
|
||||
} else {
|
||||
js_pushliteral(J, "function () { ... }");
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Fp_apply(js_State *J, unsigned int argc)
|
||||
static void Fp_apply(js_State *J, unsigned int argc)
|
||||
{
|
||||
int i, n;
|
||||
char name[20];
|
||||
@@ -103,10 +99,9 @@ static int Fp_apply(js_State *J, unsigned int argc)
|
||||
}
|
||||
|
||||
js_call(J, n);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Fp_call(js_State *J, unsigned int argc)
|
||||
static void Fp_call(js_State *J, unsigned int argc)
|
||||
{
|
||||
unsigned int i;
|
||||
|
||||
@@ -119,7 +114,6 @@ static int Fp_call(js_State *J, unsigned int argc)
|
||||
js_copy(J, i);
|
||||
|
||||
js_call(J, argc - 1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
void jsB_initfunction(js_State *J)
|
||||
|
||||
2
jsi.h
2
jsi.h
@@ -53,7 +53,7 @@ void js_rot3pop2(js_State *J);
|
||||
void js_dup1rot3(js_State *J);
|
||||
void js_dup1rot4(js_State *J);
|
||||
|
||||
int js_RegExp_prototype_exec(js_State *J, js_Regexp *re, const char *text);
|
||||
void js_RegExp_prototype_exec(js_State *J, js_Regexp *re, const char *text);
|
||||
|
||||
/* Exception handling */
|
||||
|
||||
|
||||
54
jsmath.c
54
jsmath.c
@@ -2,103 +2,87 @@
|
||||
#include "jsvalue.h"
|
||||
#include "jsbuiltin.h"
|
||||
|
||||
static int Math_abs(js_State *J, unsigned int argc)
|
||||
static void Math_abs(js_State *J, unsigned int argc)
|
||||
{
|
||||
js_pushnumber(J, abs(js_tonumber(J, 1)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Math_acos(js_State *J, unsigned int argc)
|
||||
static void Math_acos(js_State *J, unsigned int argc)
|
||||
{
|
||||
js_pushnumber(J, acos(js_tonumber(J, 1)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Math_asin(js_State *J, unsigned int argc)
|
||||
static void Math_asin(js_State *J, unsigned int argc)
|
||||
{
|
||||
js_pushnumber(J, asin(js_tonumber(J, 1)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Math_atan(js_State *J, unsigned int argc)
|
||||
static void Math_atan(js_State *J, unsigned int argc)
|
||||
{
|
||||
js_pushnumber(J, atan(js_tonumber(J, 1)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Math_atan2(js_State *J, unsigned int argc)
|
||||
static void Math_atan2(js_State *J, unsigned int argc)
|
||||
{
|
||||
js_pushnumber(J, atan2(js_tonumber(J, 1), js_tonumber(J, 2)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Math_ceil(js_State *J, unsigned int argc)
|
||||
static void Math_ceil(js_State *J, unsigned int argc)
|
||||
{
|
||||
js_pushnumber(J, ceil(js_tonumber(J, 1)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Math_cos(js_State *J, unsigned int argc)
|
||||
static void Math_cos(js_State *J, unsigned int argc)
|
||||
{
|
||||
js_pushnumber(J, cos(js_tonumber(J, 1)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Math_exp(js_State *J, unsigned int argc)
|
||||
static void Math_exp(js_State *J, unsigned int argc)
|
||||
{
|
||||
js_pushnumber(J, exp(js_tonumber(J, 1)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Math_floor(js_State *J, unsigned int argc)
|
||||
static void Math_floor(js_State *J, unsigned int argc)
|
||||
{
|
||||
js_pushnumber(J, floor(js_tonumber(J, 1)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Math_log(js_State *J, unsigned int argc)
|
||||
static void Math_log(js_State *J, unsigned int argc)
|
||||
{
|
||||
js_pushnumber(J, log(js_tonumber(J, 1)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Math_pow(js_State *J, unsigned int argc)
|
||||
static void Math_pow(js_State *J, unsigned int argc)
|
||||
{
|
||||
js_pushnumber(J, pow(js_tonumber(J, 1), js_tonumber(J, 2)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Math_random(js_State *J, unsigned int argc)
|
||||
static void Math_random(js_State *J, unsigned int argc)
|
||||
{
|
||||
js_pushnumber(J, (double)rand() / (RAND_MAX - 1));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Math_round(js_State *J, unsigned int argc)
|
||||
static void Math_round(js_State *J, unsigned int argc)
|
||||
{
|
||||
js_pushnumber(J, round(js_tonumber(J, 1)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Math_sin(js_State *J, unsigned int argc)
|
||||
static void Math_sin(js_State *J, unsigned int argc)
|
||||
{
|
||||
js_pushnumber(J, sin(js_tonumber(J, 1)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Math_sqrt(js_State *J, unsigned int argc)
|
||||
static void Math_sqrt(js_State *J, unsigned int argc)
|
||||
{
|
||||
js_pushnumber(J, sqrt(js_tonumber(J, 1)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Math_tan(js_State *J, unsigned int argc)
|
||||
static void Math_tan(js_State *J, unsigned int argc)
|
||||
{
|
||||
js_pushnumber(J, tan(js_tonumber(J, 1)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Math_max(js_State *J, unsigned int argc)
|
||||
static void Math_max(js_State *J, unsigned int argc)
|
||||
{
|
||||
double n = js_tonumber(J, 1);
|
||||
unsigned int i;
|
||||
@@ -107,10 +91,9 @@ static int Math_max(js_State *J, unsigned int argc)
|
||||
n = n > m ? n : m;
|
||||
}
|
||||
js_pushnumber(J, n);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Math_min(js_State *J, unsigned int argc)
|
||||
static void Math_min(js_State *J, unsigned int argc)
|
||||
{
|
||||
double n = js_tonumber(J, 1);
|
||||
unsigned int i;
|
||||
@@ -119,7 +102,6 @@ static int Math_min(js_State *J, unsigned int argc)
|
||||
n = n < m ? n : m;
|
||||
}
|
||||
js_pushnumber(J, n);
|
||||
return 1;
|
||||
}
|
||||
|
||||
void jsB_initmath(js_State *J)
|
||||
|
||||
21
jsnumber.c
21
jsnumber.c
@@ -2,35 +2,31 @@
|
||||
#include "jsvalue.h"
|
||||
#include "jsbuiltin.h"
|
||||
|
||||
static int jsB_new_Number(js_State *J, unsigned int argc)
|
||||
static void jsB_new_Number(js_State *J, unsigned int argc)
|
||||
{
|
||||
js_newnumber(J, js_isdefined(J, 1) ? js_tonumber(J, 1) : 0);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int jsB_Number(js_State *J, unsigned int argc)
|
||||
static void jsB_Number(js_State *J, unsigned int argc)
|
||||
{
|
||||
js_pushnumber(J, js_isdefined(J, 1) ? js_tonumber(J, 1) : 0);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Np_valueOf(js_State *J, unsigned int argc)
|
||||
static void Np_valueOf(js_State *J, unsigned int argc)
|
||||
{
|
||||
js_Object *self = js_toobject(J, 0);
|
||||
if (self->type != JS_CNUMBER) js_typeerror(J, "not a number");
|
||||
js_pushnumber(J, self->u.number);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Np_toString(js_State *J, unsigned int argc)
|
||||
static void Np_toString(js_State *J, unsigned int argc)
|
||||
{
|
||||
js_Object *self = js_toobject(J, 0);
|
||||
if (self->type != JS_CNUMBER) js_typeerror(J, "not a number");
|
||||
js_pushliteral(J, jsV_numbertostring(J, self->u.number));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Np_toFixed(js_State *J, unsigned int argc)
|
||||
static void Np_toFixed(js_State *J, unsigned int argc)
|
||||
{
|
||||
char buf[40];
|
||||
js_Object *self = js_toobject(J, 0);
|
||||
@@ -38,10 +34,9 @@ static int Np_toFixed(js_State *J, unsigned int argc)
|
||||
if (self->type != JS_CNUMBER) js_typeerror(J, "not a number");
|
||||
sprintf(buf, "%.*f", width, self->u.number);
|
||||
js_pushstring(J, buf);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Np_toExponential(js_State *J, unsigned int argc)
|
||||
static void Np_toExponential(js_State *J, unsigned int argc)
|
||||
{
|
||||
char buf[40];
|
||||
js_Object *self = js_toobject(J, 0);
|
||||
@@ -49,10 +44,9 @@ static int Np_toExponential(js_State *J, unsigned int argc)
|
||||
if (self->type != JS_CNUMBER) js_typeerror(J, "not a number");
|
||||
sprintf(buf, "%.*e", width, self->u.number);
|
||||
js_pushstring(J, buf);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Np_toPrecision(js_State *J, unsigned int argc)
|
||||
static void Np_toPrecision(js_State *J, unsigned int argc)
|
||||
{
|
||||
char buf[40];
|
||||
js_Object *self = js_toobject(J, 0);
|
||||
@@ -60,7 +54,6 @@ static int Np_toPrecision(js_State *J, unsigned int argc)
|
||||
if (self->type != JS_CNUMBER) js_typeerror(J, "not a number");
|
||||
sprintf(buf, "%.*g", width, self->u.number);
|
||||
js_pushstring(J, buf);
|
||||
return 1;
|
||||
}
|
||||
|
||||
void jsB_initnumber(js_State *J)
|
||||
|
||||
73
jsobject.c
73
jsobject.c
@@ -2,25 +2,23 @@
|
||||
#include "jsvalue.h"
|
||||
#include "jsbuiltin.h"
|
||||
|
||||
static int jsB_new_Object(js_State *J, unsigned int argc)
|
||||
static void jsB_new_Object(js_State *J, unsigned int argc)
|
||||
{
|
||||
if (argc == 0 || js_isundefined(J, 1) || js_isnull(J, 1))
|
||||
js_newobject(J);
|
||||
else
|
||||
js_pushobject(J, js_toobject(J, 1));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int jsB_Object(js_State *J, unsigned int argc)
|
||||
static void jsB_Object(js_State *J, unsigned int argc)
|
||||
{
|
||||
if (argc == 0 || js_isundefined(J, 1) || js_isnull(J, 1))
|
||||
js_newobject(J);
|
||||
else
|
||||
js_pushobject(J, js_toobject(J, 1));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Op_toString(js_State *J, unsigned int argc)
|
||||
static void Op_toString(js_State *J, unsigned int argc)
|
||||
{
|
||||
js_Object *self = js_toobject(J, 0);
|
||||
switch (self->type) {
|
||||
@@ -46,25 +44,22 @@ static int Op_toString(js_State *J, unsigned int argc)
|
||||
js_concat(J);
|
||||
break;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Op_valueOf(js_State *J, unsigned int argc)
|
||||
static void Op_valueOf(js_State *J, unsigned int argc)
|
||||
{
|
||||
js_copy(J, 0);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Op_hasOwnProperty(js_State *J, unsigned int argc)
|
||||
static void Op_hasOwnProperty(js_State *J, unsigned int argc)
|
||||
{
|
||||
js_Object *self = js_toobject(J, 0);
|
||||
const char *name = js_tostring(J, 1);
|
||||
js_Property *ref = jsV_getownproperty(J, self, name);
|
||||
js_pushboolean(J, ref != NULL);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Op_isPrototypeOf(js_State *J, unsigned int argc)
|
||||
static void Op_isPrototypeOf(js_State *J, unsigned int argc)
|
||||
{
|
||||
js_Object *self = js_toobject(J, 0);
|
||||
if (js_isobject(J, 1)) {
|
||||
@@ -73,24 +68,22 @@ static int Op_isPrototypeOf(js_State *J, unsigned int argc)
|
||||
V = V->prototype;
|
||||
if (V == self) {
|
||||
js_pushboolean(J, 1);
|
||||
return 1;
|
||||
return;
|
||||
}
|
||||
} while (V);
|
||||
}
|
||||
js_pushboolean(J, 0);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Op_propertyIsEnumerable(js_State *J, unsigned int argc)
|
||||
static void Op_propertyIsEnumerable(js_State *J, unsigned int argc)
|
||||
{
|
||||
js_Object *self = js_toobject(J, 0);
|
||||
const char *name = js_tostring(J, 1);
|
||||
js_Property *ref = jsV_getownproperty(J, self, name);
|
||||
js_pushboolean(J, ref && !(ref->atts & JS_DONTENUM));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int O_getPrototypeOf(js_State *J, unsigned int argc)
|
||||
static void O_getPrototypeOf(js_State *J, unsigned int argc)
|
||||
{
|
||||
js_Object *obj;
|
||||
if (!js_isobject(J, 1))
|
||||
@@ -100,10 +93,9 @@ static int O_getPrototypeOf(js_State *J, unsigned int argc)
|
||||
js_pushobject(J, obj->prototype);
|
||||
else
|
||||
js_pushnull(J);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int O_getOwnPropertyDescriptor(js_State *J, unsigned int argc)
|
||||
static void O_getOwnPropertyDescriptor(js_State *J, unsigned int argc)
|
||||
{
|
||||
js_Object *obj;
|
||||
js_Property *ref;
|
||||
@@ -137,10 +129,9 @@ static int O_getOwnPropertyDescriptor(js_State *J, unsigned int argc)
|
||||
js_pushboolean(J, !(ref->atts & JS_DONTCONF));
|
||||
js_setproperty(J, -2, "configurable");
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int O_getOwnPropertyNames(js_State *J, unsigned int argc)
|
||||
static void O_getOwnPropertyNames(js_State *J, unsigned int argc)
|
||||
{
|
||||
js_Object *obj;
|
||||
js_Property *ref;
|
||||
@@ -185,8 +176,6 @@ static int O_getOwnPropertyNames(js_State *J, unsigned int argc)
|
||||
js_pushliteral(J, "lastIndex");
|
||||
js_setindex(J, -2, i++);
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void ToPropertyDescriptor(js_State *J, js_Object *obj, const char *name, js_Object *desc)
|
||||
@@ -242,16 +231,15 @@ static void ToPropertyDescriptor(js_State *J, js_Object *obj, const char *name,
|
||||
js_pop(J, 2);
|
||||
}
|
||||
|
||||
static int O_defineProperty(js_State *J, unsigned int argc)
|
||||
static void O_defineProperty(js_State *J, unsigned int argc)
|
||||
{
|
||||
if (!js_isobject(J, 1)) js_typeerror(J, "not an object");
|
||||
if (!js_isobject(J, 3)) js_typeerror(J, "not an object");
|
||||
ToPropertyDescriptor(J, js_toobject(J, 1), js_tostring(J, 2), js_toobject(J, 3));
|
||||
js_copy(J, 1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int O_defineProperties(js_State *J, unsigned int argc)
|
||||
static void O_defineProperties(js_State *J, unsigned int argc)
|
||||
{
|
||||
js_Object *props;
|
||||
js_Property *ref;
|
||||
@@ -269,10 +257,9 @@ static int O_defineProperties(js_State *J, unsigned int argc)
|
||||
}
|
||||
|
||||
js_copy(J, 1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int O_create(js_State *J, unsigned int argc)
|
||||
static void O_create(js_State *J, unsigned int argc)
|
||||
{
|
||||
js_Object *obj;
|
||||
js_Object *proto;
|
||||
@@ -299,11 +286,9 @@ static int O_create(js_State *J, unsigned int argc)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int O_keys(js_State *J, unsigned int argc)
|
||||
static void O_keys(js_State *J, unsigned int argc)
|
||||
{
|
||||
js_Object *obj;
|
||||
js_Property *ref;
|
||||
@@ -330,28 +315,24 @@ static int O_keys(js_State *J, unsigned int argc)
|
||||
js_setindex(J, -2, i++);
|
||||
}
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int O_preventExtensions(js_State *J, unsigned int argc)
|
||||
static void O_preventExtensions(js_State *J, unsigned int argc)
|
||||
{
|
||||
if (!js_isobject(J, 1))
|
||||
js_typeerror(J, "not an object");
|
||||
js_toobject(J, 1)->extensible = 0;
|
||||
js_copy(J, 1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int O_isExtensible(js_State *J, unsigned int argc)
|
||||
static void O_isExtensible(js_State *J, unsigned int argc)
|
||||
{
|
||||
if (!js_isobject(J, 1))
|
||||
js_typeerror(J, "not an object");
|
||||
js_pushboolean(J, js_toobject(J, 1)->extensible);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int O_seal(js_State *J, unsigned int argc)
|
||||
static void O_seal(js_State *J, unsigned int argc)
|
||||
{
|
||||
js_Object *obj;
|
||||
js_Property *ref;
|
||||
@@ -366,10 +347,9 @@ static int O_seal(js_State *J, unsigned int argc)
|
||||
ref->atts |= JS_DONTCONF;
|
||||
|
||||
js_copy(J, 1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int O_isSealed(js_State *J, unsigned int argc)
|
||||
static void O_isSealed(js_State *J, unsigned int argc)
|
||||
{
|
||||
js_Object *obj;
|
||||
js_Property *ref;
|
||||
@@ -380,21 +360,20 @@ static int O_isSealed(js_State *J, unsigned int argc)
|
||||
obj = js_toobject(J, 1);
|
||||
if (obj->extensible) {
|
||||
js_pushboolean(J, 0);
|
||||
return 1;
|
||||
return;
|
||||
}
|
||||
|
||||
for (ref = obj->head; ref; ref = ref->next) {
|
||||
if (!(ref->atts & JS_DONTCONF)) {
|
||||
js_pushboolean(J, 0);
|
||||
return 1;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
js_pushboolean(J, 1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int O_freeze(js_State *J, unsigned int argc)
|
||||
static void O_freeze(js_State *J, unsigned int argc)
|
||||
{
|
||||
js_Object *obj;
|
||||
js_Property *ref;
|
||||
@@ -409,10 +388,9 @@ static int O_freeze(js_State *J, unsigned int argc)
|
||||
ref->atts |= JS_READONLY | JS_DONTCONF;
|
||||
|
||||
js_copy(J, 1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int O_isFrozen(js_State *J, unsigned int argc)
|
||||
static void O_isFrozen(js_State *J, unsigned int argc)
|
||||
{
|
||||
js_Object *obj;
|
||||
js_Property *ref;
|
||||
@@ -423,18 +401,17 @@ static int O_isFrozen(js_State *J, unsigned int argc)
|
||||
obj = js_toobject(J, 1);
|
||||
if (obj->extensible) {
|
||||
js_pushboolean(J, 0);
|
||||
return 1;
|
||||
return;
|
||||
}
|
||||
|
||||
for (ref = obj->head; ref; ref = ref->next) {
|
||||
if (!(ref->atts & (JS_READONLY | JS_DONTCONF))) {
|
||||
js_pushboolean(J, 0);
|
||||
return 1;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
js_pushboolean(J, 1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
void jsB_initobject(js_State *J)
|
||||
|
||||
9
json.c
9
json.c
@@ -92,14 +92,13 @@ static void jsonvalue(js_State *J)
|
||||
}
|
||||
}
|
||||
|
||||
static int JSON_parse(js_State *J, unsigned int argc)
|
||||
static void JSON_parse(js_State *J, unsigned int argc)
|
||||
{
|
||||
const char *source = js_tostring(J, 1);
|
||||
jsY_initlex(J, "JSON", source);
|
||||
jsonnext(J);
|
||||
jsonvalue(J);
|
||||
// TODO: reviver Walk()
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void fmtnum(js_Buffer **sb, double n)
|
||||
@@ -237,7 +236,7 @@ static int fmtvalue(js_State *J, js_Buffer **sb, const char *key)
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int JSON_stringify(js_State *J, unsigned int argc)
|
||||
static void JSON_stringify(js_State *J, unsigned int argc)
|
||||
{
|
||||
js_Buffer *sb = NULL;
|
||||
if (argc > 0) {
|
||||
@@ -251,10 +250,10 @@ static int JSON_stringify(js_State *J, unsigned int argc)
|
||||
js_pushstring(J, sb ? sb->s : "");
|
||||
js_endtry(J);
|
||||
free(sb);
|
||||
return 1;
|
||||
}
|
||||
} else {
|
||||
js_pushundefined(J);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void jsB_initjson(js_State *J)
|
||||
|
||||
26
jsregexp.c
26
jsregexp.c
@@ -29,7 +29,7 @@ void js_newregexp(js_State *J, const char *pattern, int flags)
|
||||
js_pushobject(J, obj);
|
||||
}
|
||||
|
||||
int js_RegExp_prototype_exec(js_State *J, js_Regexp *re, const char *text)
|
||||
void js_RegExp_prototype_exec(js_State *J, js_Regexp *re, const char *text)
|
||||
{
|
||||
Resub m[10];
|
||||
unsigned int i;
|
||||
@@ -40,7 +40,7 @@ int js_RegExp_prototype_exec(js_State *J, js_Regexp *re, const char *text)
|
||||
if (re->last > strlen(text)) {
|
||||
re->last = 0;
|
||||
js_pushnull(J);
|
||||
return 1;
|
||||
return;
|
||||
}
|
||||
if (re->last > 0) {
|
||||
text += re->last;
|
||||
@@ -56,17 +56,16 @@ int js_RegExp_prototype_exec(js_State *J, js_Regexp *re, const char *text)
|
||||
}
|
||||
if (re->flags & JS_REGEXP_G)
|
||||
re->last = re->last + (m[0].ep - text);
|
||||
return 1;
|
||||
return;
|
||||
}
|
||||
|
||||
if (re->flags & JS_REGEXP_G)
|
||||
re->last = 0;
|
||||
|
||||
js_pushnull(J);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Rp_test(js_State *J, unsigned int argc)
|
||||
static void Rp_test(js_State *J, unsigned int argc)
|
||||
{
|
||||
js_Regexp *re;
|
||||
const char *text;
|
||||
@@ -81,7 +80,7 @@ static int Rp_test(js_State *J, unsigned int argc)
|
||||
if (re->last > strlen(text)) {
|
||||
re->last = 0;
|
||||
js_pushboolean(J, 0);
|
||||
return 1;
|
||||
return;
|
||||
}
|
||||
if (re->last > 0) {
|
||||
text += re->last;
|
||||
@@ -93,17 +92,16 @@ static int Rp_test(js_State *J, unsigned int argc)
|
||||
if (re->flags & JS_REGEXP_G)
|
||||
re->last = re->last + (m[0].ep - text);
|
||||
js_pushboolean(J, 1);
|
||||
return 1;
|
||||
return;
|
||||
}
|
||||
|
||||
if (re->flags & JS_REGEXP_G)
|
||||
re->last = 0;
|
||||
|
||||
js_pushboolean(J, 0);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int jsB_new_RegExp(js_State *J, unsigned int argc)
|
||||
static void jsB_new_RegExp(js_State *J, unsigned int argc)
|
||||
{
|
||||
js_Regexp *old;
|
||||
const char *pattern;
|
||||
@@ -142,17 +140,16 @@ static int jsB_new_RegExp(js_State *J, unsigned int argc)
|
||||
}
|
||||
|
||||
js_newregexp(J, pattern, flags);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int jsB_RegExp(js_State *J, unsigned int argc)
|
||||
static void jsB_RegExp(js_State *J, unsigned int argc)
|
||||
{
|
||||
if (js_isregexp(J, 1) && argc == 1)
|
||||
return 1;
|
||||
return;
|
||||
return jsB_new_RegExp(J, argc);
|
||||
}
|
||||
|
||||
static int Rp_toString(js_State *J, unsigned int argc)
|
||||
static void Rp_toString(js_State *J, unsigned int argc)
|
||||
{
|
||||
js_Regexp *re;
|
||||
char *out;
|
||||
@@ -175,10 +172,9 @@ static int Rp_toString(js_State *J, unsigned int argc)
|
||||
js_pushstring(J, out);
|
||||
js_endtry(J);
|
||||
free(out);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Rp_exec(js_State *J, unsigned int argc)
|
||||
static void Rp_exec(js_State *J, unsigned int argc)
|
||||
{
|
||||
return js_RegExp_prototype_exec(J, js_toregexp(J, 0), js_tostring(J, 1));
|
||||
}
|
||||
|
||||
18
jsrun.c
18
jsrun.c
@@ -840,22 +840,18 @@ static void jsR_callscript(js_State *J, unsigned int n, js_Function *F)
|
||||
js_pushvalue(J, v);
|
||||
}
|
||||
|
||||
static void jsR_callcfunction(js_State *J, unsigned int n, int min, js_CFunction F)
|
||||
static void jsR_callcfunction(js_State *J, unsigned int n, unsigned int min, js_CFunction F)
|
||||
{
|
||||
int rv, i;
|
||||
unsigned int i;
|
||||
js_Value v;
|
||||
|
||||
for (i = n; i < min; ++i)
|
||||
js_pushundefined(J);
|
||||
|
||||
rv = F(J, n);
|
||||
if (rv) {
|
||||
js_Value v = js_tovalue(J, -1);
|
||||
TOP = --BOT; /* clear stack */
|
||||
js_pushvalue(J, v);
|
||||
} else {
|
||||
TOP = --BOT; /* clear stack */
|
||||
js_pushundefined(J);
|
||||
}
|
||||
F(J, n);
|
||||
v = js_tovalue(J, -1);
|
||||
TOP = --BOT; /* clear stack */
|
||||
js_pushvalue(J, v);
|
||||
}
|
||||
|
||||
void js_call(js_State *J, unsigned int n)
|
||||
|
||||
103
jsstring.c
103
jsstring.c
@@ -50,35 +50,31 @@ int js_utfptrtoidx(const char *s, const char *p)
|
||||
return i;
|
||||
}
|
||||
|
||||
static int jsB_new_String(js_State *J, unsigned int argc)
|
||||
static void jsB_new_String(js_State *J, unsigned int argc)
|
||||
{
|
||||
js_newstring(J, js_isdefined(J, 1) ? js_tostring(J, 1) : "");
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int jsB_String(js_State *J, unsigned int argc)
|
||||
static void jsB_String(js_State *J, unsigned int argc)
|
||||
{
|
||||
js_pushliteral(J, js_isdefined(J, 1) ? js_tostring(J, 1) : "");
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Sp_toString(js_State *J, unsigned int argc)
|
||||
static void Sp_toString(js_State *J, unsigned int argc)
|
||||
{
|
||||
js_Object *self = js_toobject(J, 0);
|
||||
if (self->type != JS_CSTRING) js_typeerror(J, "not a string");
|
||||
js_pushliteral(J, self->u.s.string);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Sp_valueOf(js_State *J, unsigned int argc)
|
||||
static void Sp_valueOf(js_State *J, unsigned int argc)
|
||||
{
|
||||
js_Object *self = js_toobject(J, 0);
|
||||
if (self->type != JS_CSTRING) js_typeerror(J, "not a string");
|
||||
js_pushliteral(J, self->u.s.string);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Sp_charAt(js_State *J, unsigned int argc)
|
||||
static void Sp_charAt(js_State *J, unsigned int argc)
|
||||
{
|
||||
char buf[UTFmax + 1];
|
||||
const char *s = js_tostring(J, 0);
|
||||
@@ -90,10 +86,9 @@ static int Sp_charAt(js_State *J, unsigned int argc)
|
||||
} else {
|
||||
js_pushliteral(J, "");
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Sp_charCodeAt(js_State *J, unsigned int argc)
|
||||
static void Sp_charCodeAt(js_State *J, unsigned int argc)
|
||||
{
|
||||
const char *s = js_tostring(J, 0);
|
||||
int pos = js_tointeger(J, 1);
|
||||
@@ -102,17 +97,16 @@ static int Sp_charCodeAt(js_State *J, unsigned int argc)
|
||||
js_pushnumber(J, rune);
|
||||
else
|
||||
js_pushnumber(J, NAN);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Sp_concat(js_State *J, unsigned int argc)
|
||||
static void Sp_concat(js_State *J, unsigned int argc)
|
||||
{
|
||||
char * volatile out;
|
||||
const char *s;
|
||||
unsigned int i, n;
|
||||
|
||||
if (argc == 0)
|
||||
return 1;
|
||||
return;
|
||||
|
||||
s = js_tostring(J, 0);
|
||||
n = strlen(s);
|
||||
@@ -134,10 +128,9 @@ static int Sp_concat(js_State *J, unsigned int argc)
|
||||
js_pushstring(J, out);
|
||||
js_endtry(J);
|
||||
free(out);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Sp_indexOf(js_State *J, unsigned int argc)
|
||||
static void Sp_indexOf(js_State *J, unsigned int argc)
|
||||
{
|
||||
const char *haystack = js_tostring(J, 0);
|
||||
const char *needle = js_tostring(J, 1);
|
||||
@@ -148,16 +141,15 @@ static int Sp_indexOf(js_State *J, unsigned int argc)
|
||||
while (*haystack) {
|
||||
if (k >= pos && !strncmp(haystack, needle, len)) {
|
||||
js_pushnumber(J, k);
|
||||
return 1;
|
||||
return;
|
||||
}
|
||||
haystack += chartorune(&rune, haystack);
|
||||
++k;
|
||||
}
|
||||
js_pushnumber(J, -1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Sp_lastIndexOf(js_State *J, unsigned int argc)
|
||||
static void Sp_lastIndexOf(js_State *J, unsigned int argc)
|
||||
{
|
||||
const char *haystack = js_tostring(J, 0);
|
||||
const char *needle = js_tostring(J, 1);
|
||||
@@ -172,17 +164,16 @@ static int Sp_lastIndexOf(js_State *J, unsigned int argc)
|
||||
++k;
|
||||
}
|
||||
js_pushnumber(J, last);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Sp_localeCompare(js_State *J, unsigned int argc)
|
||||
static void Sp_localeCompare(js_State *J, unsigned int argc)
|
||||
{
|
||||
const char *a = js_tostring(J, 0);
|
||||
const char *b = js_tostring(J, 1);
|
||||
return strcmp(a, b);
|
||||
js_pushnumber(J, strcmp(a, b));
|
||||
}
|
||||
|
||||
static int Sp_slice(js_State *J, unsigned int argc)
|
||||
static void Sp_slice(js_State *J, unsigned int argc)
|
||||
{
|
||||
const char *str = js_tostring(J, 0);
|
||||
const char *ss, *ee;
|
||||
@@ -205,10 +196,9 @@ static int Sp_slice(js_State *J, unsigned int argc)
|
||||
}
|
||||
|
||||
js_pushlstring(J, ss, ee - ss);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Sp_substring(js_State *J, unsigned int argc)
|
||||
static void Sp_substring(js_State *J, unsigned int argc)
|
||||
{
|
||||
const char *str = js_tostring(J, 0);
|
||||
const char *ss, *ee;
|
||||
@@ -228,10 +218,9 @@ static int Sp_substring(js_State *J, unsigned int argc)
|
||||
}
|
||||
|
||||
js_pushlstring(J, ss, ee - ss);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Sp_toLowerCase(js_State *J, unsigned int argc)
|
||||
static void Sp_toLowerCase(js_State *J, unsigned int argc)
|
||||
{
|
||||
const char *src = js_tostring(J, 0);
|
||||
char *dst = malloc(UTFmax * strlen(src) + 1);
|
||||
@@ -250,10 +239,9 @@ static int Sp_toLowerCase(js_State *J, unsigned int argc)
|
||||
js_pushstring(J, dst);
|
||||
js_endtry(J);
|
||||
free(dst);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Sp_toUpperCase(js_State *J, unsigned int argc)
|
||||
static void Sp_toUpperCase(js_State *J, unsigned int argc)
|
||||
{
|
||||
const char *src = js_tostring(J, 0);
|
||||
char *dst = malloc(UTFmax * strlen(src) + 1);
|
||||
@@ -272,7 +260,6 @@ static int Sp_toUpperCase(js_State *J, unsigned int argc)
|
||||
js_pushstring(J, dst);
|
||||
js_endtry(J);
|
||||
free(dst);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int istrim(int c)
|
||||
@@ -281,7 +268,7 @@ static int istrim(int c)
|
||||
c == 0xA || c == 0xD || c == 0x2028 || c == 0x2029;
|
||||
}
|
||||
|
||||
static int Sp_trim(js_State *J, unsigned int argc)
|
||||
static void Sp_trim(js_State *J, unsigned int argc)
|
||||
{
|
||||
const char *s, *e;
|
||||
s = js_tostring(J, 0);
|
||||
@@ -291,10 +278,9 @@ static int Sp_trim(js_State *J, unsigned int argc)
|
||||
while (e > s && istrim(e[-1]))
|
||||
--e;
|
||||
js_pushlstring(J, s, e - s);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int S_fromCharCode(js_State *J, unsigned int argc)
|
||||
static void S_fromCharCode(js_State *J, unsigned int argc)
|
||||
{
|
||||
unsigned int i;
|
||||
Rune c;
|
||||
@@ -316,10 +302,9 @@ static int S_fromCharCode(js_State *J, unsigned int argc)
|
||||
|
||||
js_endtry(J);
|
||||
free(s);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Sp_match(js_State *J, unsigned int argc)
|
||||
static void Sp_match(js_State *J, unsigned int argc)
|
||||
{
|
||||
js_Regexp *re;
|
||||
Resub m[10];
|
||||
@@ -361,11 +346,9 @@ static int Sp_match(js_State *J, unsigned int argc)
|
||||
if (c - b == 0)
|
||||
++a;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Sp_search(js_State *J, unsigned int argc)
|
||||
static void Sp_search(js_State *J, unsigned int argc)
|
||||
{
|
||||
js_Regexp *re;
|
||||
Resub m[10];
|
||||
@@ -386,11 +369,9 @@ static int Sp_search(js_State *J, unsigned int argc)
|
||||
js_pushnumber(J, js_utfptrtoidx(text, m[0].sp));
|
||||
else
|
||||
js_pushnumber(J, -1);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Sp_replace_regexp(js_State *J, unsigned int argc)
|
||||
static void Sp_replace_regexp(js_State *J, unsigned int argc)
|
||||
{
|
||||
js_Regexp *re;
|
||||
Resub m[10];
|
||||
@@ -403,7 +384,7 @@ static int Sp_replace_regexp(js_State *J, unsigned int argc)
|
||||
|
||||
if (js_regexec(re->prog, source, nelem(m), m, 0)) {
|
||||
js_copy(J, 0);
|
||||
return 1;
|
||||
return;
|
||||
}
|
||||
|
||||
re->last = 0;
|
||||
@@ -481,10 +462,9 @@ end:
|
||||
js_pushstring(J, sb ? sb->s : "");
|
||||
js_endtry(J);
|
||||
free(sb);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Sp_replace_string(js_State *J, unsigned int argc)
|
||||
static void Sp_replace_string(js_State *J, unsigned int argc)
|
||||
{
|
||||
const char *source, *needle, *s, *r;
|
||||
js_Buffer *sb = NULL;
|
||||
@@ -496,7 +476,7 @@ static int Sp_replace_string(js_State *J, unsigned int argc)
|
||||
s = strstr(source, needle);
|
||||
if (!s) {
|
||||
js_copy(J, 0);
|
||||
return 1;
|
||||
return;
|
||||
}
|
||||
n = strlen(needle);
|
||||
|
||||
@@ -541,17 +521,16 @@ static int Sp_replace_string(js_State *J, unsigned int argc)
|
||||
js_pushstring(J, sb ? sb->s : "");
|
||||
js_endtry(J);
|
||||
free(sb);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Sp_replace(js_State *J, unsigned int argc)
|
||||
static void Sp_replace(js_State *J, unsigned int argc)
|
||||
{
|
||||
if (js_isregexp(J, 1))
|
||||
return Sp_replace_regexp(J, argc);
|
||||
return Sp_replace_string(J, argc);
|
||||
}
|
||||
|
||||
static int Sp_split_regexp(js_State *J, unsigned int argc)
|
||||
static void Sp_split_regexp(js_State *J, unsigned int argc)
|
||||
{
|
||||
js_Regexp *re;
|
||||
Resub m[10];
|
||||
@@ -571,11 +550,11 @@ static int Sp_split_regexp(js_State *J, unsigned int argc)
|
||||
/* splitting the empty string */
|
||||
if (e == 0) {
|
||||
if (js_regexec(re->prog, text, nelem(m), m, 0)) {
|
||||
if (len == limit) return 1;
|
||||
if (len == limit) return;
|
||||
js_pushliteral(J, "");
|
||||
js_setindex(J, -2, 0);
|
||||
}
|
||||
return 1;
|
||||
return;
|
||||
}
|
||||
|
||||
p = a = text;
|
||||
@@ -592,12 +571,12 @@ static int Sp_split_regexp(js_State *J, unsigned int argc)
|
||||
continue;
|
||||
}
|
||||
|
||||
if (len == limit) return 1;
|
||||
if (len == limit) return;
|
||||
js_pushlstring(J, p, b - p);
|
||||
js_setindex(J, -2, len++);
|
||||
|
||||
for (k = 1; k < nelem(m) && m[k].sp; ++k) {
|
||||
if (len == limit) return 1;
|
||||
if (len == limit) return;
|
||||
js_pushlstring(J, m[k].sp, m[k].ep - m[k].sp);
|
||||
js_setindex(J, -2, len++);
|
||||
}
|
||||
@@ -605,14 +584,12 @@ static int Sp_split_regexp(js_State *J, unsigned int argc)
|
||||
a = p = c;
|
||||
}
|
||||
|
||||
if (len == limit) return 1;
|
||||
if (len == limit) return;
|
||||
js_pushstring(J, p);
|
||||
js_setindex(J, -2, len);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Sp_split_string(js_State *J, unsigned int argc)
|
||||
static void Sp_split_string(js_State *J, unsigned int argc)
|
||||
{
|
||||
const char *str = js_tostring(J, 0);
|
||||
const char *sep = js_tostring(J, 1);
|
||||
@@ -632,7 +609,7 @@ static int Sp_split_string(js_State *J, unsigned int argc)
|
||||
js_setindex(J, -2, i);
|
||||
str += n;
|
||||
}
|
||||
return 1;
|
||||
return;
|
||||
}
|
||||
|
||||
for (i = 0; str && i < limit; ++i) {
|
||||
@@ -647,21 +624,19 @@ static int Sp_split_string(js_State *J, unsigned int argc)
|
||||
str = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int Sp_split(js_State *J, unsigned int argc)
|
||||
static void Sp_split(js_State *J, unsigned int argc)
|
||||
{
|
||||
if (js_isundefined(J, 1)) {
|
||||
js_newarray(J);
|
||||
js_copy(J, 0);
|
||||
js_setindex(J, -2, 0);
|
||||
return 1;
|
||||
} else if (js_isregexp(J, 1)) {
|
||||
Sp_split_regexp(J, argc);
|
||||
} else {
|
||||
Sp_split_string(J, argc);
|
||||
}
|
||||
if (js_isregexp(J, 1))
|
||||
return Sp_split_regexp(J, argc);
|
||||
return Sp_split_string(J, argc);
|
||||
}
|
||||
|
||||
void jsB_initstring(js_State *J)
|
||||
|
||||
11
main.c
11
main.c
@@ -4,22 +4,21 @@
|
||||
|
||||
#define PS1 "> "
|
||||
|
||||
static int jsB_gc(js_State *J, unsigned int argc)
|
||||
static void jsB_gc(js_State *J, unsigned int argc)
|
||||
{
|
||||
int report = js_toboolean(J, 1);
|
||||
js_gc(J, report);
|
||||
return 0;
|
||||
js_pushundefined(J);
|
||||
}
|
||||
|
||||
static int jsB_load(js_State *J, unsigned int argc)
|
||||
static void jsB_load(js_State *J, unsigned int argc)
|
||||
{
|
||||
const char *filename = js_tostring(J, 1);
|
||||
int rv = js_dofile(J, filename);
|
||||
js_pushboolean(J, !rv);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int jsB_print(js_State *J, unsigned int argc)
|
||||
static void jsB_print(js_State *J, unsigned int argc)
|
||||
{
|
||||
unsigned int i;
|
||||
for (i = 1; i <= argc; ++i) {
|
||||
@@ -28,7 +27,7 @@ static int jsB_print(js_State *J, unsigned int argc)
|
||||
fputs(s, stdout);
|
||||
}
|
||||
putchar('\n');
|
||||
return 0;
|
||||
js_pushundefined(J);
|
||||
}
|
||||
|
||||
int
|
||||
|
||||
Reference in New Issue
Block a user