comparison context.c @ 41:71ab5e977dd3

Added a context.call_function() method.
author Atul Varma <varmaa@toolness.com>
date Fri, 03 Jul 2009 20:57:58 -0700
parents 8a7abd0bb48d
children e62b1801f9af
comparison
equal deleted inserted replaced
40:8a7abd0bb48d 41:71ab5e977dd3
146 PyErr_SetString(PYM_error, "JS_DefineProperty() failed"); 146 PyErr_SetString(PYM_error, "JS_DefineProperty() failed");
147 return NULL; 147 return NULL;
148 } 148 }
149 149
150 Py_RETURN_NONE; 150 Py_RETURN_NONE;
151 }
152
153 static PyObject *
154 PYM_callFunction(PYM_JSContextObject *self, PyObject *args)
155 {
156 PYM_JSObject *obj;
157 PYM_JSFunction *fun;
158 PyObject *funcArgs;
159
160 if (!PyArg_ParseTuple(args, "O!O!O!", &PYM_JSObjectType, &obj,
161 &PYM_JSFunctionType, &fun,
162 &PyTuple_Type, &funcArgs))
163 return NULL;
164
165 uintN argc = PyTuple_Size(funcArgs);
166 jsval argv[argc];
167 jsval *currArg = argv;
168
169 for (unsigned int i = 0; i < argc; i++) {
170 PyObject *item = PyTuple_GET_ITEM(funcArgs, i);
171 if (item == NULL ||
172 PYM_pyObjectToJsval(self->cx, item, currArg) == -1)
173 return NULL;
174 currArg++;
175 }
176
177 jsval rval;
178
179 // TODO: This assumes that a JSFunction * is actually a subclass of
180 // a JSObject *, which may or may not be regarded as an implementation
181 // detail.
182 if (!JS_CallFunction(self->cx, obj->obj, (JSFunction *) fun->base.obj,
183 argc, argv, &rval)) {
184 // TODO: There's a pending exception on the JS context, should we
185 // do something about it?
186 PyErr_SetString(PYM_error, "Function failed");
187 return NULL;
188 }
189
190 return PYM_jsvalToPyObject(self, rval);
151 } 191 }
152 192
153 static PyObject * 193 static PyObject *
154 PYM_newFunction(PYM_JSContextObject *self, PyObject *args) 194 PYM_newFunction(PYM_JSContextObject *self, PyObject *args)
155 { 195 {
173 {"evaluate_script", 213 {"evaluate_script",
174 (PyCFunction) PYM_evaluateScript, METH_VARARGS, 214 (PyCFunction) PYM_evaluateScript, METH_VARARGS,
175 "Evaluate the given JavaScript code in the context of the given " 215 "Evaluate the given JavaScript code in the context of the given "
176 "global object, using the given filename" 216 "global object, using the given filename"
177 "and line number information."}, 217 "and line number information."},
218 {"call_function",
219 (PyCFunction) PYM_callFunction, METH_VARARGS,
220 "Calls a JS function."},
178 {"new_function", 221 {"new_function",
179 (PyCFunction) PYM_newFunction, METH_VARARGS, 222 (PyCFunction) PYM_newFunction, METH_VARARGS,
180 "Creates a new function callable from JS."}, 223 "Creates a new function callable from JS."},
181 {"define_property", 224 {"define_property",
182 (PyCFunction) PYM_defineProperty, METH_VARARGS, 225 (PyCFunction) PYM_defineProperty, METH_VARARGS,