Mercurial > pymonkey
comparison function.c @ 52:427b01954b22
The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
author | Atul Varma <varmaa@toolness.com> |
---|---|
date | Wed, 08 Jul 2009 18:45:38 -0700 |
parents | bc4263c6ae82 |
children | 1506350991d4 |
comparison
equal
deleted
inserted
replaced
51:fabd3f2271fa | 52:427b01954b22 |
---|---|
62 JS_ReportError(cx, "JS_GetReservedSlot() failed."); | 62 JS_ReportError(cx, "JS_GetReservedSlot() failed."); |
63 return JS_FALSE; | 63 return JS_FALSE; |
64 } | 64 } |
65 PyObject *callable = (PyObject *) JSVAL_TO_PRIVATE(jsCallable); | 65 PyObject *callable = (PyObject *) JSVAL_TO_PRIVATE(jsCallable); |
66 | 66 |
67 // TODO: Convert args and 'this' parameter. | 67 PYM_JSContextObject *context = (PYM_JSContextObject *) |
68 PyObject *args = PyTuple_New(1); | 68 JS_GetContextPrivate(cx); |
69 | |
70 jsval thisArg = OBJECT_TO_JSVAL(obj); | |
71 PyObject *pyThisArg = PYM_jsvalToPyObject(context, thisArg); | |
72 if (pyThisArg == NULL) { | |
73 PYM_pythonExceptionToJs(context); | |
74 return JS_FALSE; | |
75 } | |
76 | |
77 PyObject *funcArgs = PyTuple_New(argc); | |
78 if (funcArgs == NULL) { | |
79 Py_DECREF(pyThisArg); | |
80 JS_ReportOutOfMemory(cx); | |
81 return JS_FALSE; | |
82 } | |
83 | |
84 for (unsigned int i = 0; i < argc; i++) { | |
85 PyObject *arg = PYM_jsvalToPyObject(context, argv[i]); | |
86 if (arg == NULL || PyTuple_SetItem(funcArgs, i, arg)) { | |
87 if (arg) | |
88 Py_DECREF(arg); | |
89 Py_DECREF(funcArgs); | |
90 Py_DECREF(pyThisArg); | |
91 PYM_pythonExceptionToJs(context); | |
92 return JS_FALSE; | |
93 } | |
94 } | |
95 | |
96 PyObject *args = PyTuple_Pack(3, | |
97 (PyObject *) context, | |
98 pyThisArg, | |
99 funcArgs); | |
100 Py_DECREF(pyThisArg); | |
101 Py_DECREF(funcArgs); | |
69 if (args == NULL) { | 102 if (args == NULL) { |
70 JS_ReportOutOfMemory(cx); | 103 JS_ReportOutOfMemory(cx); |
71 return JS_FALSE; | 104 return JS_FALSE; |
72 } | 105 } |
73 | 106 |
74 PYM_JSContextObject *context = (PYM_JSContextObject *) | |
75 JS_GetContextPrivate(cx); | |
76 Py_INCREF(context); | |
77 PyTuple_SET_ITEM(args, 0, (PyObject *) context); | |
78 | |
79 PyObject *result = PyObject_Call(callable, args, NULL); | 107 PyObject *result = PyObject_Call(callable, args, NULL); |
108 Py_DECREF(args); | |
80 if (result == NULL) { | 109 if (result == NULL) { |
81 PYM_pythonExceptionToJs(context); | 110 PYM_pythonExceptionToJs(context); |
82 return JS_FALSE; | 111 return JS_FALSE; |
83 } | 112 } |
84 | 113 |
87 | 116 |
88 if (error) { | 117 if (error) { |
89 PYM_pythonExceptionToJs(context); | 118 PYM_pythonExceptionToJs(context); |
90 return JS_FALSE; | 119 return JS_FALSE; |
91 } | 120 } |
121 | |
92 return JS_TRUE; | 122 return JS_TRUE; |
93 } | 123 } |
94 | 124 |
95 PyTypeObject PYM_JSFunctionType = { | 125 PyTypeObject PYM_JSFunctionType = { |
96 PyObject_HEAD_INIT(NULL) | 126 PyObject_HEAD_INIT(NULL) |