annotate src/function.cpp @ 123:08a012e96f62

Added another test and fixed a bug it revealed.
author Atul Varma <varmaa@toolness.com>
date Wed, 19 Aug 2009 01:00:35 -0700
parents ac8ca0ee7760
children f956a6dea16c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
48
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
1 /* ***** BEGIN LICENSE BLOCK *****
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
3 *
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
4 * The contents of this file are subject to the Mozilla Public License Version
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
5 * 1.1 (the "License"); you may not use this file except in compliance with
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
6 * the License. You may obtain a copy of the License at
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
7 * http://www.mozilla.org/MPL/
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
8 *
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
9 * Software distributed under the License is distributed on an "AS IS" basis,
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
11 * for the specific language governing rights and limitations under the
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
12 * License.
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
13 *
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
14 * The Original Code is Pymonkey.
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
15 *
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
16 * The Initial Developer of the Original Code is Mozilla.
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
17 * Portions created by the Initial Developer are Copyright (C) 2007
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
18 * the Initial Developer. All Rights Reserved.
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
19 *
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
20 * Contributor(s):
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
21 * Atul Varma <atul@mozilla.com>
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
22 *
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
23 * Alternatively, the contents of this file may be used under the terms of
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
24 * either the GNU General Public License Version 2 or later (the "GPL"), or
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
25 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
26 * in which case the provisions of the GPL or the LGPL are applicable instead
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
27 * of those above. If you wish to allow use of your version of this file only
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
28 * under the terms of either the GPL or the LGPL, and not to allow others to
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
29 * use your version of this file under the terms of the MPL, indicate your
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
30 * decision by deleting the provisions above and replace them with the notice
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
31 * and other provisions required by the GPL or the LGPL. If you do not delete
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
32 * the provisions above, a recipient may use your version of this file under
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
33 * the terms of any one of the MPL, the GPL or the LGPL.
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
34 *
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
35 * ***** END LICENSE BLOCK ***** */
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
36
37
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
37 #include "function.h"
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
38 #include "utils.h"
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
39
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
40 static void
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
41 PYM_JSFunctionDealloc(PYM_JSFunction *self)
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
42 {
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
43 PYM_JSObjectType.tp_dealloc((PyObject *) self);
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
44 }
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
45
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
46 static JSBool
65
f89a582c1276 A few function renamings for consistency.
Atul Varma <varmaa@toolness.com>
parents: 61
diff changeset
47 PYM_dispatchJSFunctionToPython(JSContext *cx,
f89a582c1276 A few function renamings for consistency.
Atul Varma <varmaa@toolness.com>
parents: 61
diff changeset
48 JSObject *obj,
f89a582c1276 A few function renamings for consistency.
Atul Varma <varmaa@toolness.com>
parents: 61
diff changeset
49 uintN argc,
f89a582c1276 A few function renamings for consistency.
Atul Varma <varmaa@toolness.com>
parents: 61
diff changeset
50 jsval *argv,
f89a582c1276 A few function renamings for consistency.
Atul Varma <varmaa@toolness.com>
parents: 61
diff changeset
51 jsval *rval)
37
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
52 {
66
b49180c39d0a Pymonkey now handles the GIL properly so that Python code can run while JS code does.
Atul Varma <varmaa@toolness.com>
parents: 65
diff changeset
53 PYM_PyAutoEnsureGIL gil;
37
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
54 jsval callee = JS_ARGV_CALLEE(argv);
61
1506350991d4 JS Wrapped python functions are now only GC'd by python once they've first been GC'd by JS.
Atul Varma <varmaa@toolness.com>
parents: 52
diff changeset
55 jsval functionHolder;
70
b0360c0ed546 Factored the JS FunctionHolder class into the JS PymonkeyObject class, so that any PymonkeyObject can contain a private Python object if it needs to.
Atul Varma <varmaa@toolness.com>
parents: 68
diff changeset
56 if (!JS_GetReservedSlot(cx, JSVAL_TO_OBJECT(callee), 0, &functionHolder))
37
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
57 return JS_FALSE;
61
1506350991d4 JS Wrapped python functions are now only GC'd by python once they've first been GC'd by JS.
Atul Varma <varmaa@toolness.com>
parents: 52
diff changeset
58
1506350991d4 JS Wrapped python functions are now only GC'd by python once they've first been GC'd by JS.
Atul Varma <varmaa@toolness.com>
parents: 52
diff changeset
59 PyObject *callable;
70
b0360c0ed546 Factored the JS FunctionHolder class into the JS PymonkeyObject class, so that any PymonkeyObject can contain a private Python object if it needs to.
Atul Varma <varmaa@toolness.com>
parents: 68
diff changeset
60 if (!PYM_JS_getPrivatePyObject(cx, JSVAL_TO_OBJECT(functionHolder),
b0360c0ed546 Factored the JS FunctionHolder class into the JS PymonkeyObject class, so that any PymonkeyObject can contain a private Python object if it needs to.
Atul Varma <varmaa@toolness.com>
parents: 68
diff changeset
61 &callable))
61
1506350991d4 JS Wrapped python functions are now only GC'd by python once they've first been GC'd by JS.
Atul Varma <varmaa@toolness.com>
parents: 52
diff changeset
62 return JS_FALSE;
37
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
63
86
16a3e99e9b77 JS-wrapped Python functions that have had their Python functions cleared by context.clear_object_private() now throw appropriate JS exceptions when called.
Atul Varma <varmaa@toolness.com>
parents: 70
diff changeset
64 if (callable == Py_None) {
16a3e99e9b77 JS-wrapped Python functions that have had their Python functions cleared by context.clear_object_private() now throw appropriate JS exceptions when called.
Atul Varma <varmaa@toolness.com>
parents: 70
diff changeset
65 JS_ReportError(cx, "Wrapped Python function no longer exists");
16a3e99e9b77 JS-wrapped Python functions that have had their Python functions cleared by context.clear_object_private() now throw appropriate JS exceptions when called.
Atul Varma <varmaa@toolness.com>
parents: 70
diff changeset
66 return JS_FALSE;
16a3e99e9b77 JS-wrapped Python functions that have had their Python functions cleared by context.clear_object_private() now throw appropriate JS exceptions when called.
Atul Varma <varmaa@toolness.com>
parents: 70
diff changeset
67 }
16a3e99e9b77 JS-wrapped Python functions that have had their Python functions cleared by context.clear_object_private() now throw appropriate JS exceptions when called.
Atul Varma <varmaa@toolness.com>
parents: 70
diff changeset
68
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.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
69 PYM_JSContextObject *context = (PYM_JSContextObject *)
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
70 JS_GetContextPrivate(cx);
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
71
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
72 jsval thisArg = OBJECT_TO_JSVAL(obj);
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
73 PyObject *pyThisArg = PYM_jsvalToPyObject(context, thisArg);
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
74 if (pyThisArg == NULL) {
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
75 PYM_pythonExceptionToJs(context);
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
76 return JS_FALSE;
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
77 }
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
78
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
79 PyObject *funcArgs = PyTuple_New(argc);
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
80 if (funcArgs == NULL) {
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
81 Py_DECREF(pyThisArg);
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
82 JS_ReportOutOfMemory(cx);
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
83 return JS_FALSE;
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
84 }
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
85
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
86 for (unsigned int i = 0; i < argc; i++) {
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
87 PyObject *arg = PYM_jsvalToPyObject(context, argv[i]);
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
88 if (arg == NULL || PyTuple_SetItem(funcArgs, i, arg)) {
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
89 if (arg)
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
90 Py_DECREF(arg);
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
91 Py_DECREF(funcArgs);
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
92 Py_DECREF(pyThisArg);
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
93 PYM_pythonExceptionToJs(context);
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
94 return JS_FALSE;
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
95 }
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
96 }
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
97
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
98 PyObject *args = PyTuple_Pack(3,
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
99 (PyObject *) context,
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
100 pyThisArg,
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
101 funcArgs);
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
102 Py_DECREF(pyThisArg);
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
103 Py_DECREF(funcArgs);
37
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
104 if (args == NULL) {
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
105 JS_ReportOutOfMemory(cx);
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
106 return JS_FALSE;
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
107 }
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
108
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
109 PyObject *result = PyObject_Call(callable, args, NULL);
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.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
110 Py_DECREF(args);
37
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
111 if (result == NULL) {
46
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 44
diff changeset
112 PYM_pythonExceptionToJs(context);
37
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
113 return JS_FALSE;
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
114 }
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
115
44
0b9a316ce4ef Changed function signature of PYM_pyObjectToJsval() to be consistent w/ the rest of the API.
Atul Varma <varmaa@toolness.com>
parents: 43
diff changeset
116 int error = PYM_pyObjectToJsval(context, result, rval);
37
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
117 Py_DECREF(result);
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
118
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
119 if (error) {
46
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 44
diff changeset
120 PYM_pythonExceptionToJs(context);
37
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
121 return JS_FALSE;
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
122 }
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.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
123
37
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
124 return JS_TRUE;
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
125 }
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
126
39
9103afca7386 Moved some code around.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
127 PyTypeObject PYM_JSFunctionType = {
9103afca7386 Moved some code around.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
128 PyObject_HEAD_INIT(NULL)
9103afca7386 Moved some code around.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
129 0, /*ob_size*/
9103afca7386 Moved some code around.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
130 "pymonkey.Function", /*tp_name*/
9103afca7386 Moved some code around.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
131 sizeof(PYM_JSFunction), /*tp_basicsize*/
9103afca7386 Moved some code around.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
132 0, /*tp_itemsize*/
9103afca7386 Moved some code around.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
133 /*tp_dealloc*/
9103afca7386 Moved some code around.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
134 (destructor) PYM_JSFunctionDealloc,
9103afca7386 Moved some code around.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
135 0, /*tp_print*/
9103afca7386 Moved some code around.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
136 0, /*tp_getattr*/
9103afca7386 Moved some code around.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
137 0, /*tp_setattr*/
9103afca7386 Moved some code around.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
138 0, /*tp_compare*/
9103afca7386 Moved some code around.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
139 0, /*tp_repr*/
9103afca7386 Moved some code around.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
140 0, /*tp_as_number*/
9103afca7386 Moved some code around.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
141 0, /*tp_as_sequence*/
9103afca7386 Moved some code around.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
142 0, /*tp_as_mapping*/
9103afca7386 Moved some code around.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
143 0, /*tp_hash */
9103afca7386 Moved some code around.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
144 0, /*tp_call*/
9103afca7386 Moved some code around.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
145 0, /*tp_str*/
9103afca7386 Moved some code around.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
146 0, /*tp_getattro*/
9103afca7386 Moved some code around.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
147 0, /*tp_setattro*/
9103afca7386 Moved some code around.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
148 0, /*tp_as_buffer*/
9103afca7386 Moved some code around.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
149 /*tp_flags*/
9103afca7386 Moved some code around.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
150 Py_TPFLAGS_DEFAULT,
9103afca7386 Moved some code around.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
151 /* tp_doc */
9103afca7386 Moved some code around.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
152 "JavaScript Function.",
9103afca7386 Moved some code around.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
153 0, /* tp_traverse */
9103afca7386 Moved some code around.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
154 0, /* tp_clear */
9103afca7386 Moved some code around.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
155 0, /* tp_richcompare */
9103afca7386 Moved some code around.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
156 0, /* tp_weaklistoffset */
9103afca7386 Moved some code around.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
157 0, /* tp_iter */
9103afca7386 Moved some code around.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
158 0, /* tp_iternext */
9103afca7386 Moved some code around.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
159 0, /* tp_methods */
9103afca7386 Moved some code around.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
160 0, /* tp_members */
9103afca7386 Moved some code around.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
161 0, /* tp_getset */
9103afca7386 Moved some code around.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
162 0, /* tp_base */
9103afca7386 Moved some code around.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
163 0, /* tp_dict */
9103afca7386 Moved some code around.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
164 0, /* tp_descr_get */
9103afca7386 Moved some code around.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
165 0, /* tp_descr_set */
9103afca7386 Moved some code around.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
166 0, /* tp_dictoffset */
9103afca7386 Moved some code around.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
167 0, /* tp_init */
9103afca7386 Moved some code around.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
168 0, /* tp_alloc */
9103afca7386 Moved some code around.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
169 0, /* tp_new */
9103afca7386 Moved some code around.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
170 };
9103afca7386 Moved some code around.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
171
37
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
172 PYM_JSFunction *
40
8a7abd0bb48d Renamed PYM_newJSFunction() to PYM_newJSFunctionFromCallable(). PYM_newJSObject() now returns objects of type PYM_JSFunctionType as needed.
Atul Varma <varmaa@toolness.com>
parents: 39
diff changeset
173 PYM_newJSFunctionFromCallable(PYM_JSContextObject *context,
8a7abd0bb48d Renamed PYM_newJSFunction() to PYM_newJSFunctionFromCallable(). PYM_newJSObject() now returns objects of type PYM_JSFunctionType as needed.
Atul Varma <varmaa@toolness.com>
parents: 39
diff changeset
174 PyObject *callable,
8a7abd0bb48d Renamed PYM_newJSFunction() to PYM_newJSFunctionFromCallable(). PYM_newJSObject() now returns objects of type PYM_JSFunctionType as needed.
Atul Varma <varmaa@toolness.com>
parents: 39
diff changeset
175 const char *name)
37
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
176 {
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
177 if (!PyCallable_Check(callable)) {
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
178 PyErr_SetString(PyExc_TypeError, "Callable must be callable");
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
179 return NULL;
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
180 }
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
181
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
182 JSFunction *func = JS_NewFunction(context->cx,
65
f89a582c1276 A few function renamings for consistency.
Atul Varma <varmaa@toolness.com>
parents: 61
diff changeset
183 PYM_dispatchJSFunctionToPython, 0,
37
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
184 0, NULL, name);
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
185
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
186 if (func == NULL) {
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
187 PyErr_SetString(PYM_error, "JS_DefineFunction() failed");
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
188 return NULL;
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
189 }
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
190
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
191 JSObject *funcObj = JS_GetFunctionObject(func);
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
192
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
193 if (funcObj == NULL) {
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
194 PyErr_SetString(PYM_error, "JS_GetFunctionObject() failed");
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
195 return NULL;
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
196 }
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
197
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
198 PYM_JSFunction *object = PyObject_New(PYM_JSFunction,
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
199 &PYM_JSFunctionType);
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
200 if (object == NULL)
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
201 return NULL;
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
202
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
203 if (PYM_newJSObject(context, funcObj,
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
204 (PYM_JSObject *) object) == NULL)
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
205 // Note that our object's reference count will have already
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
206 // been decremented by PYM_newJSObject().
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
207 return NULL;
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
208
70
b0360c0ed546 Factored the JS FunctionHolder class into the JS PymonkeyObject class, so that any PymonkeyObject can contain a private Python object if it needs to.
Atul Varma <varmaa@toolness.com>
parents: 68
diff changeset
209 JSObject *functionHolder = PYM_JS_newObject(context->cx, callable);
61
1506350991d4 JS Wrapped python functions are now only GC'd by python once they've first been GC'd by JS.
Atul Varma <varmaa@toolness.com>
parents: 52
diff changeset
210 if (functionHolder == NULL) {
1506350991d4 JS Wrapped python functions are now only GC'd by python once they've first been GC'd by JS.
Atul Varma <varmaa@toolness.com>
parents: 52
diff changeset
211 Py_DECREF((PyObject *) object);
70
b0360c0ed546 Factored the JS FunctionHolder class into the JS PymonkeyObject class, so that any PymonkeyObject can contain a private Python object if it needs to.
Atul Varma <varmaa@toolness.com>
parents: 68
diff changeset
212 PyErr_SetString(PYM_error, "PYM_JS_newObject() failed");
61
1506350991d4 JS Wrapped python functions are now only GC'd by python once they've first been GC'd by JS.
Atul Varma <varmaa@toolness.com>
parents: 52
diff changeset
213 return NULL;
1506350991d4 JS Wrapped python functions are now only GC'd by python once they've first been GC'd by JS.
Atul Varma <varmaa@toolness.com>
parents: 52
diff changeset
214 }
1506350991d4 JS Wrapped python functions are now only GC'd by python once they've first been GC'd by JS.
Atul Varma <varmaa@toolness.com>
parents: 52
diff changeset
215
1506350991d4 JS Wrapped python functions are now only GC'd by python once they've first been GC'd by JS.
Atul Varma <varmaa@toolness.com>
parents: 52
diff changeset
216 if (!JS_SetReservedSlot(context->cx, funcObj, 0,
1506350991d4 JS Wrapped python functions are now only GC'd by python once they've first been GC'd by JS.
Atul Varma <varmaa@toolness.com>
parents: 52
diff changeset
217 OBJECT_TO_JSVAL(functionHolder))) {
1506350991d4 JS Wrapped python functions are now only GC'd by python once they've first been GC'd by JS.
Atul Varma <varmaa@toolness.com>
parents: 52
diff changeset
218 Py_DECREF((PyObject *) object);
1506350991d4 JS Wrapped python functions are now only GC'd by python once they've first been GC'd by JS.
Atul Varma <varmaa@toolness.com>
parents: 52
diff changeset
219 PyErr_SetString(PYM_error, "JS_SetReservedSlot() failed");
1506350991d4 JS Wrapped python functions are now only GC'd by python once they've first been GC'd by JS.
Atul Varma <varmaa@toolness.com>
parents: 52
diff changeset
220 return NULL;
1506350991d4 JS Wrapped python functions are now only GC'd by python once they've first been GC'd by JS.
Atul Varma <varmaa@toolness.com>
parents: 52
diff changeset
221 }
37
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
222
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
223 return object;
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
224 }