Mercurial > pymonkey
comparison function.c @ 65:f89a582c1276
A few function renamings for consistency.
author | Atul Varma <varmaa@toolness.com> |
---|---|
date | Sun, 26 Jul 2009 13:09:58 -0700 |
parents | 1506350991d4 |
children | b49180c39d0a |
comparison
equal
deleted
inserted
replaced
64:fb7e11dec538 | 65:f89a582c1276 |
---|---|
36 | 36 |
37 #include "function.h" | 37 #include "function.h" |
38 #include "utils.h" | 38 #include "utils.h" |
39 | 39 |
40 static JSBool | 40 static JSBool |
41 getHeldFunction(JSContext *cx, JSObject *obj, PyObject **callable) | 41 PYM_getHeldFunction(JSContext *cx, JSObject *obj, PyObject **callable) |
42 { | 42 { |
43 jsval jsCallable; | 43 jsval jsCallable; |
44 if (!JS_GetReservedSlot(cx, obj, 0, &jsCallable)) { | 44 if (!JS_GetReservedSlot(cx, obj, 0, &jsCallable)) { |
45 JS_ReportError(cx, "JS_GetReservedSlot() failed."); | 45 JS_ReportError(cx, "JS_GetReservedSlot() failed."); |
46 return JS_FALSE; | 46 return JS_FALSE; |
48 *callable = (PyObject *) JSVAL_TO_PRIVATE(jsCallable); | 48 *callable = (PyObject *) JSVAL_TO_PRIVATE(jsCallable); |
49 return JS_TRUE; | 49 return JS_TRUE; |
50 } | 50 } |
51 | 51 |
52 static void | 52 static void |
53 finalizeFunctionHolder(JSContext *cx, JSObject *obj) | 53 PYM_finalizeFunctionHolder(JSContext *cx, JSObject *obj) |
54 { | 54 { |
55 PyObject *callable; | 55 PyObject *callable; |
56 if (getHeldFunction(cx, obj, &callable)) | 56 if (PYM_getHeldFunction(cx, obj, &callable)) |
57 Py_DECREF(callable); | 57 Py_DECREF(callable); |
58 } | 58 } |
59 | 59 |
60 JSClass PYM_JS_FunctionHolderClass = { | 60 static JSClass PYM_JS_FunctionHolderClass = { |
61 "PymonkeyFunctionHolder", JSCLASS_HAS_RESERVED_SLOTS(1), | 61 "PymonkeyFunctionHolder", JSCLASS_HAS_RESERVED_SLOTS(1), |
62 JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, | 62 JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, |
63 JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, | 63 JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, |
64 finalizeFunctionHolder, | 64 PYM_finalizeFunctionHolder, |
65 JSCLASS_NO_OPTIONAL_MEMBERS | 65 JSCLASS_NO_OPTIONAL_MEMBERS |
66 }; | 66 }; |
67 | 67 |
68 static JSObject * | 68 static JSObject * |
69 newFunctionHolder(JSContext *cx, PyObject *callable) | 69 PYM_newFunctionHolder(JSContext *cx, PyObject *callable) |
70 { | 70 { |
71 JSObject *obj = JS_NewObject(cx, &PYM_JS_FunctionHolderClass, NULL, NULL); | 71 JSObject *obj = JS_NewObject(cx, &PYM_JS_FunctionHolderClass, NULL, NULL); |
72 if (obj) { | 72 if (obj) { |
73 if (JS_SetReservedSlot(cx, obj, 0, PRIVATE_TO_JSVAL(callable))) | 73 if (JS_SetReservedSlot(cx, obj, 0, PRIVATE_TO_JSVAL(callable))) |
74 Py_INCREF(callable); | 74 Py_INCREF(callable); |
87 { | 87 { |
88 PYM_JSObjectType.tp_dealloc((PyObject *) self); | 88 PYM_JSObjectType.tp_dealloc((PyObject *) self); |
89 } | 89 } |
90 | 90 |
91 static JSBool | 91 static JSBool |
92 dispatchJSFunctionToPython(JSContext *cx, | 92 PYM_dispatchJSFunctionToPython(JSContext *cx, |
93 JSObject *obj, | 93 JSObject *obj, |
94 uintN argc, | 94 uintN argc, |
95 jsval *argv, | 95 jsval *argv, |
96 jsval *rval) | 96 jsval *rval) |
97 { | 97 { |
98 jsval callee = JS_ARGV_CALLEE(argv); | 98 jsval callee = JS_ARGV_CALLEE(argv); |
99 jsval functionHolder; | 99 jsval functionHolder; |
100 if (!JS_GetReservedSlot(cx, JSVAL_TO_OBJECT(callee), 0, &functionHolder)) { | 100 if (!JS_GetReservedSlot(cx, JSVAL_TO_OBJECT(callee), 0, &functionHolder)) { |
101 JS_ReportError(cx, "JS_GetReservedSlot() failed."); | 101 JS_ReportError(cx, "JS_GetReservedSlot() failed."); |
102 return JS_FALSE; | 102 return JS_FALSE; |
103 } | 103 } |
104 | 104 |
105 PyObject *callable; | 105 PyObject *callable; |
106 if (!getHeldFunction(cx, JSVAL_TO_OBJECT(functionHolder), &callable)) | 106 if (!PYM_getHeldFunction(cx, JSVAL_TO_OBJECT(functionHolder), &callable)) |
107 return JS_FALSE; | 107 return JS_FALSE; |
108 | 108 |
109 PYM_JSContextObject *context = (PYM_JSContextObject *) | 109 PYM_JSContextObject *context = (PYM_JSContextObject *) |
110 JS_GetContextPrivate(cx); | 110 JS_GetContextPrivate(cx); |
111 | 111 |
218 PyErr_SetString(PyExc_TypeError, "Callable must be callable"); | 218 PyErr_SetString(PyExc_TypeError, "Callable must be callable"); |
219 return NULL; | 219 return NULL; |
220 } | 220 } |
221 | 221 |
222 JSFunction *func = JS_NewFunction(context->cx, | 222 JSFunction *func = JS_NewFunction(context->cx, |
223 dispatchJSFunctionToPython, 0, | 223 PYM_dispatchJSFunctionToPython, 0, |
224 0, NULL, name); | 224 0, NULL, name); |
225 | 225 |
226 if (func == NULL) { | 226 if (func == NULL) { |
227 PyErr_SetString(PYM_error, "JS_DefineFunction() failed"); | 227 PyErr_SetString(PYM_error, "JS_DefineFunction() failed"); |
228 return NULL; | 228 return NULL; |
244 (PYM_JSObject *) object) == NULL) | 244 (PYM_JSObject *) object) == NULL) |
245 // Note that our object's reference count will have already | 245 // Note that our object's reference count will have already |
246 // been decremented by PYM_newJSObject(). | 246 // been decremented by PYM_newJSObject(). |
247 return NULL; | 247 return NULL; |
248 | 248 |
249 JSObject *functionHolder = newFunctionHolder(context->cx, callable); | 249 JSObject *functionHolder = PYM_newFunctionHolder(context->cx, callable); |
250 if (functionHolder == NULL) { | 250 if (functionHolder == NULL) { |
251 Py_DECREF((PyObject *) object); | 251 Py_DECREF((PyObject *) object); |
252 return NULL; | 252 return NULL; |
253 } | 253 } |
254 | 254 |