Add functions to let C bindings define setters and getters.

Also add js_currentfunction to allow C bindings to access
properties defined on the currently executing function.
This commit is contained in:
Tor Andersson
2014-02-06 16:03:44 +01:00
parent e65764ecf9
commit f8871d072c
2 changed files with 17 additions and 0 deletions
+2
View File
@@ -95,12 +95,14 @@ 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);
void js_defaccessor(js_State *J, int idx, const char *name);
int js_hasindex(js_State *J, int idx, unsigned int i);
void js_getindex(js_State *J, int idx, unsigned int i);
void js_setindex(js_State *J, int idx, unsigned int i);
void js_delindex(js_State *J, int idx, unsigned int i);
void js_currentfunction(js_State *J);
void js_pushglobal(js_State *J);
void js_pushundefined(js_State *J);
void js_pushnull(js_State *J);
+15
View File
@@ -108,6 +108,13 @@ void js_pushglobal(js_State *J)
js_pushobject(J, J->G);
}
void js_currentfunction(js_State *J)
{
CHECKSTACK(1);
STACK[TOP] = STACK[BOT-1];
++TOP;
}
/* Read values from stack */
static const js_Value *stackidx(js_State *J, int idx)
@@ -245,6 +252,8 @@ void *js_touserdata(js_State *J, const char *tag, int idx)
static js_Object *jsR_tofunction(js_State *J, int idx)
{
const js_Value *v = stackidx(J, idx);
if (v->type == JS_TNULL)
return NULL;
if (v->type == JS_TOBJECT)
if (v->u.object->type == JS_CFUNCTION || v->u.object->type == JS_CCFUNCTION)
return v->u.object;
@@ -603,6 +612,12 @@ void js_delproperty(js_State *J, int idx, const char *name)
jsR_delproperty(J, js_toobject(J, idx), name);
}
void js_defaccessor(js_State *J, int idx, const char *name)
{
jsR_defproperty(J, js_toobject(J, idx), name, 0, NULL, jsR_tofunction(J, -2), jsR_tofunction(J, -1));
js_pop(J, 2);
}
int js_hasproperty(js_State *J, int idx, const char *name)
{
return jsR_hasproperty(J, js_toobject(J, idx), name);