ref: f8871d072c5ea287958300f0420fb8156ef16cbf
parent: e65764ecf9cb58621d78d9ac378008d076016e1b
author: Tor Andersson <tor@ccxvii.net>
date: Thu Feb 6 09:56:52 EST 2014
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.
--- a/js.h
+++ b/js.h
@@ -95,6 +95,7 @@
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);
@@ -101,6 +102,7 @@
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);
--- a/jsrun.c
+++ b/jsrun.c
@@ -108,6 +108,13 @@
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 @@
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;
@@ -601,6 +610,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)