From fac3f501e441f946f9d90eb566439920f5fe1bfd Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Sat, 25 Jan 2014 22:56:16 +0100 Subject: [PATCH] Add js_hasproperty function. --- js.h | 1 + jsfunction.c | 4 ++-- jsgc.c | 2 +- jsrun.c | 8 ++++++-- 4 files changed, 10 insertions(+), 5 deletions(-) diff --git a/js.h b/js.h index 10299a9..d78a795 100644 --- a/js.h +++ b/js.h @@ -94,6 +94,7 @@ void js_getproperty(js_State *J, int idx, const char *name); void js_setproperty(js_State *J, int idx, const char *name); void js_defproperty(js_State *J, int idx, const char *name, int atts); void js_delproperty(js_State *J, int idx, const char *name); +int js_hasproperty(js_State *J, int idx, const char *name); void js_pushglobal(js_State *J); void js_pushundefined(js_State *J); diff --git a/jsfunction.c b/jsfunction.c index bd6b3d9..bb8f4b9 100644 --- a/jsfunction.c +++ b/jsfunction.c @@ -27,13 +27,13 @@ static int Fp_toString(js_State *J, int argc) js_Function *F = self->u.f.function; n = strlen("function () { ... }"); n += strlen(F->name); - for (i = 0; i < F->numparams; i++) + for (i = 0; i < F->numparams; ++i) n += strlen(F->params[i]) + 1; s = malloc(n); strcpy(s, "function "); strcat(s, F->name); strcat(s, "("); - for (i = 0; i < F->numparams; i++) { + for (i = 0; i < F->numparams; ++i) { if (i > 0) strcat(s, ","); strcat(s, F->params[i]); } diff --git a/jsgc.c b/jsgc.c index 679ed58..9a662b5 100644 --- a/jsgc.c +++ b/jsgc.c @@ -49,7 +49,7 @@ static void jsG_markfunction(js_State *J, int mark, js_Function *fun) { int i; fun->gcmark = mark; - for (i = 0; i < fun->funlen; i++) + for (i = 0; i < fun->funlen; ++i) if (fun->funtab[i]->gcmark != mark) jsG_markfunction(J, mark, fun->funtab[i]); } diff --git a/jsrun.c b/jsrun.c index ae762e9..ce45232 100644 --- a/jsrun.c +++ b/jsrun.c @@ -430,6 +430,11 @@ void js_delproperty(js_State *J, int idx, const char *name) jsR_delproperty(J, js_toobject(J, idx), name); } +int js_hasproperty(js_State *J, int idx, const char *name) +{ + return !!jsV_getproperty(J, js_toobject(J, idx), name); +} + /* Environment records */ js_Environment *jsR_newenvironment(js_State *J, js_Object *vars, js_Environment *outer) @@ -759,8 +764,7 @@ static void jsR_run(js_State *J, js_Function *F) case OP_IN: str = js_tostring(J, -2); - obj = js_toobject(J, -1); - b = jsV_getproperty(J, obj, str) != NULL; + b = js_hasproperty(J, -1, str); js_pop(J, 2); js_pushboolean(J, b); break;