annotate src/function.cpp @ 160:56181fb5fc7e

Added a function.is_python property indicating whether the js function is implemented in python.
author Atul Varma <varmaa@toolness.com>
date Sun, 30 Aug 2009 15:47:23 -0700
parents 5ec81091cb89
children dd32a92f6b4f
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
154
5ec81091cb89 Added filename/line-number metadata to functions. Wanted to just add a 'script' property but this proved problematic--see the TODO in the code for more information.
Atul Varma <varmaa@toolness.com>
parents: 152
diff changeset
40 #include "jsdbgapi.h"
152
80463c8c7930 Added a 'name' property to function obects. Fixed a TODO.
Atul Varma <varmaa@toolness.com>
parents: 131
diff changeset
41 #include "structmember.h"
80463c8c7930 Added a 'name' property to function obects. Fixed a TODO.
Atul Varma <varmaa@toolness.com>
parents: 131
diff changeset
42
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
43 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
44 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
45 {
152
80463c8c7930 Added a 'name' property to function obects. Fixed a TODO.
Atul Varma <varmaa@toolness.com>
parents: 131
diff changeset
46 self->fun = NULL;
154
5ec81091cb89 Added filename/line-number metadata to functions. Wanted to just add a 'script' property but this proved problematic--see the TODO in the code for more information.
Atul Varma <varmaa@toolness.com>
parents: 152
diff changeset
47 self->filename = NULL;
152
80463c8c7930 Added a 'name' property to function obects. Fixed a TODO.
Atul Varma <varmaa@toolness.com>
parents: 131
diff changeset
48
80463c8c7930 Added a 'name' property to function obects. Fixed a TODO.
Atul Varma <varmaa@toolness.com>
parents: 131
diff changeset
49 if (self->name) {
80463c8c7930 Added a 'name' property to function obects. Fixed a TODO.
Atul Varma <varmaa@toolness.com>
parents: 131
diff changeset
50 Py_DECREF(self->name);
80463c8c7930 Added a 'name' property to function obects. Fixed a TODO.
Atul Varma <varmaa@toolness.com>
parents: 131
diff changeset
51 self->name = NULL;
80463c8c7930 Added a 'name' property to function obects. Fixed a TODO.
Atul Varma <varmaa@toolness.com>
parents: 131
diff changeset
52 }
80463c8c7930 Added a 'name' property to function obects. Fixed a TODO.
Atul Varma <varmaa@toolness.com>
parents: 131
diff changeset
53
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 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
55 }
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
56
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 static JSBool
65
f89a582c1276 A few function renamings for consistency.
Atul Varma <varmaa@toolness.com>
parents: 61
diff changeset
58 PYM_dispatchJSFunctionToPython(JSContext *cx,
f89a582c1276 A few function renamings for consistency.
Atul Varma <varmaa@toolness.com>
parents: 61
diff changeset
59 JSObject *obj,
f89a582c1276 A few function renamings for consistency.
Atul Varma <varmaa@toolness.com>
parents: 61
diff changeset
60 uintN argc,
f89a582c1276 A few function renamings for consistency.
Atul Varma <varmaa@toolness.com>
parents: 61
diff changeset
61 jsval *argv,
f89a582c1276 A few function renamings for consistency.
Atul Varma <varmaa@toolness.com>
parents: 61
diff changeset
62 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
63 {
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
64 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
65 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
66 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
67 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
68 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
69
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
70 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
71 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
72 &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
73 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
74
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
75 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
76 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
77 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
78 }
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
79
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
80 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
81 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
82
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 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
84 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
85 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
86 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
87 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
88 }
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
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 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
91 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
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 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
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 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
98 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
99 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
100 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
101 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
102 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
103 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
104 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
105 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
106 }
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
107 }
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
108
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
109 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
110 (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
111 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
112 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
113 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
114 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
115 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
116 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
117 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
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
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
120 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
121 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
122 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
123 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
124 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
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
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
127 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
128 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
129
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
130 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
131 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
132 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
133 }
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
134
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
135 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
136 }
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
137
152
80463c8c7930 Added a 'name' property to function obects. Fixed a TODO.
Atul Varma <varmaa@toolness.com>
parents: 131
diff changeset
138 static PyMemberDef PYM_members[] = {
80463c8c7930 Added a 'name' property to function obects. Fixed a TODO.
Atul Varma <varmaa@toolness.com>
parents: 131
diff changeset
139 {"name", T_OBJECT, offsetof(PYM_JSFunction, name), READONLY,
80463c8c7930 Added a 'name' property to function obects. Fixed a TODO.
Atul Varma <varmaa@toolness.com>
parents: 131
diff changeset
140 "Name of the function."},
154
5ec81091cb89 Added filename/line-number metadata to functions. Wanted to just add a 'script' property but this proved problematic--see the TODO in the code for more information.
Atul Varma <varmaa@toolness.com>
parents: 152
diff changeset
141 {"filename", T_STRING, offsetof(PYM_JSFunction, filename), READONLY,
5ec81091cb89 Added filename/line-number metadata to functions. Wanted to just add a 'script' property but this proved problematic--see the TODO in the code for more information.
Atul Varma <varmaa@toolness.com>
parents: 152
diff changeset
142 "Filename of function's source code."},
5ec81091cb89 Added filename/line-number metadata to functions. Wanted to just add a 'script' property but this proved problematic--see the TODO in the code for more information.
Atul Varma <varmaa@toolness.com>
parents: 152
diff changeset
143 {"base_lineno", T_UINT, offsetof(PYM_JSFunction, baseLineno), READONLY,
5ec81091cb89 Added filename/line-number metadata to functions. Wanted to just add a 'script' property but this proved problematic--see the TODO in the code for more information.
Atul Varma <varmaa@toolness.com>
parents: 152
diff changeset
144 "Base line number of function's source code."},
5ec81091cb89 Added filename/line-number metadata to functions. Wanted to just add a 'script' property but this proved problematic--see the TODO in the code for more information.
Atul Varma <varmaa@toolness.com>
parents: 152
diff changeset
145 {"line_extent", T_UINT, offsetof(PYM_JSFunction, lineExtent), READONLY,
5ec81091cb89 Added filename/line-number metadata to functions. Wanted to just add a 'script' property but this proved problematic--see the TODO in the code for more information.
Atul Varma <varmaa@toolness.com>
parents: 152
diff changeset
146 "Line extent of function's source code."},
160
56181fb5fc7e Added a function.is_python property indicating whether the js function is implemented in python.
Atul Varma <varmaa@toolness.com>
parents: 154
diff changeset
147 {"is_python", T_BYTE, offsetof(PYM_JSFunction, isPython), READONLY,
56181fb5fc7e Added a function.is_python property indicating whether the js function is implemented in python.
Atul Varma <varmaa@toolness.com>
parents: 154
diff changeset
148 "Whether or not the function is implemented in Python."},
152
80463c8c7930 Added a 'name' property to function obects. Fixed a TODO.
Atul Varma <varmaa@toolness.com>
parents: 131
diff changeset
149 {NULL, NULL, NULL, NULL, NULL}
80463c8c7930 Added a 'name' property to function obects. Fixed a TODO.
Atul Varma <varmaa@toolness.com>
parents: 131
diff changeset
150 };
80463c8c7930 Added a 'name' property to function obects. Fixed a TODO.
Atul Varma <varmaa@toolness.com>
parents: 131
diff changeset
151
39
9103afca7386 Moved some code around.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
152 PyTypeObject PYM_JSFunctionType = {
9103afca7386 Moved some code around.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
153 PyObject_HEAD_INIT(NULL)
9103afca7386 Moved some code around.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
154 0, /*ob_size*/
9103afca7386 Moved some code around.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
155 "pymonkey.Function", /*tp_name*/
9103afca7386 Moved some code around.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
156 sizeof(PYM_JSFunction), /*tp_basicsize*/
9103afca7386 Moved some code around.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
157 0, /*tp_itemsize*/
131
f956a6dea16c Minor housekeeping: replaced tabs w/ spaces, got rid of outdated TODOs, etc.
Atul Varma <varmaa@toolness.com>
parents: 117
diff changeset
158 /*tp_dealloc*/
39
9103afca7386 Moved some code around.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
159 (destructor) PYM_JSFunctionDealloc,
9103afca7386 Moved some code around.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
160 0, /*tp_print*/
9103afca7386 Moved some code around.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
161 0, /*tp_getattr*/
9103afca7386 Moved some code around.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
162 0, /*tp_setattr*/
9103afca7386 Moved some code around.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
163 0, /*tp_compare*/
9103afca7386 Moved some code around.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
164 0, /*tp_repr*/
9103afca7386 Moved some code around.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
165 0, /*tp_as_number*/
9103afca7386 Moved some code around.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
166 0, /*tp_as_sequence*/
9103afca7386 Moved some code around.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
167 0, /*tp_as_mapping*/
9103afca7386 Moved some code around.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
168 0, /*tp_hash */
9103afca7386 Moved some code around.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
169 0, /*tp_call*/
9103afca7386 Moved some code around.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
170 0, /*tp_str*/
9103afca7386 Moved some code around.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
171 0, /*tp_getattro*/
9103afca7386 Moved some code around.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
172 0, /*tp_setattro*/
9103afca7386 Moved some code around.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
173 0, /*tp_as_buffer*/
131
f956a6dea16c Minor housekeeping: replaced tabs w/ spaces, got rid of outdated TODOs, etc.
Atul Varma <varmaa@toolness.com>
parents: 117
diff changeset
174 /*tp_flags*/
39
9103afca7386 Moved some code around.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
175 Py_TPFLAGS_DEFAULT,
131
f956a6dea16c Minor housekeeping: replaced tabs w/ spaces, got rid of outdated TODOs, etc.
Atul Varma <varmaa@toolness.com>
parents: 117
diff changeset
176 /* tp_doc */
39
9103afca7386 Moved some code around.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
177 "JavaScript Function.",
131
f956a6dea16c Minor housekeeping: replaced tabs w/ spaces, got rid of outdated TODOs, etc.
Atul Varma <varmaa@toolness.com>
parents: 117
diff changeset
178 0, /* tp_traverse */
f956a6dea16c Minor housekeeping: replaced tabs w/ spaces, got rid of outdated TODOs, etc.
Atul Varma <varmaa@toolness.com>
parents: 117
diff changeset
179 0, /* tp_clear */
f956a6dea16c Minor housekeeping: replaced tabs w/ spaces, got rid of outdated TODOs, etc.
Atul Varma <varmaa@toolness.com>
parents: 117
diff changeset
180 0, /* tp_richcompare */
39
9103afca7386 Moved some code around.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
181 0, /* tp_weaklistoffset */
131
f956a6dea16c Minor housekeeping: replaced tabs w/ spaces, got rid of outdated TODOs, etc.
Atul Varma <varmaa@toolness.com>
parents: 117
diff changeset
182 0, /* tp_iter */
f956a6dea16c Minor housekeeping: replaced tabs w/ spaces, got rid of outdated TODOs, etc.
Atul Varma <varmaa@toolness.com>
parents: 117
diff changeset
183 0, /* tp_iternext */
39
9103afca7386 Moved some code around.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
184 0, /* tp_methods */
152
80463c8c7930 Added a 'name' property to function obects. Fixed a TODO.
Atul Varma <varmaa@toolness.com>
parents: 131
diff changeset
185 PYM_members, /* tp_members */
39
9103afca7386 Moved some code around.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
186 0, /* tp_getset */
9103afca7386 Moved some code around.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
187 0, /* tp_base */
9103afca7386 Moved some code around.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
188 0, /* tp_dict */
9103afca7386 Moved some code around.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
189 0, /* tp_descr_get */
9103afca7386 Moved some code around.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
190 0, /* tp_descr_set */
9103afca7386 Moved some code around.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
191 0, /* tp_dictoffset */
9103afca7386 Moved some code around.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
192 0, /* tp_init */
9103afca7386 Moved some code around.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
193 0, /* tp_alloc */
9103afca7386 Moved some code around.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
194 0, /* tp_new */
9103afca7386 Moved some code around.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
195 };
9103afca7386 Moved some code around.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
196
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
197 PYM_JSFunction *
152
80463c8c7930 Added a 'name' property to function obects. Fixed a TODO.
Atul Varma <varmaa@toolness.com>
parents: 131
diff changeset
198 PYM_newJSFunction(PYM_JSContextObject *context,
80463c8c7930 Added a 'name' property to function obects. Fixed a TODO.
Atul Varma <varmaa@toolness.com>
parents: 131
diff changeset
199 JSFunction *function)
80463c8c7930 Added a 'name' property to function obects. Fixed a TODO.
Atul Varma <varmaa@toolness.com>
parents: 131
diff changeset
200 {
80463c8c7930 Added a 'name' property to function obects. Fixed a TODO.
Atul Varma <varmaa@toolness.com>
parents: 131
diff changeset
201 PYM_JSFunction *jsFunction = PyObject_New(PYM_JSFunction,
80463c8c7930 Added a 'name' property to function obects. Fixed a TODO.
Atul Varma <varmaa@toolness.com>
parents: 131
diff changeset
202 &PYM_JSFunctionType);
80463c8c7930 Added a 'name' property to function obects. Fixed a TODO.
Atul Varma <varmaa@toolness.com>
parents: 131
diff changeset
203 if (jsFunction == NULL)
80463c8c7930 Added a 'name' property to function obects. Fixed a TODO.
Atul Varma <varmaa@toolness.com>
parents: 131
diff changeset
204 return NULL;
80463c8c7930 Added a 'name' property to function obects. Fixed a TODO.
Atul Varma <varmaa@toolness.com>
parents: 131
diff changeset
205
80463c8c7930 Added a 'name' property to function obects. Fixed a TODO.
Atul Varma <varmaa@toolness.com>
parents: 131
diff changeset
206 jsFunction->fun = function;
154
5ec81091cb89 Added filename/line-number metadata to functions. Wanted to just add a 'script' property but this proved problematic--see the TODO in the code for more information.
Atul Varma <varmaa@toolness.com>
parents: 152
diff changeset
207 jsFunction->name = NULL;
5ec81091cb89 Added filename/line-number metadata to functions. Wanted to just add a 'script' property but this proved problematic--see the TODO in the code for more information.
Atul Varma <varmaa@toolness.com>
parents: 152
diff changeset
208 jsFunction->filename = NULL;
5ec81091cb89 Added filename/line-number metadata to functions. Wanted to just add a 'script' property but this proved problematic--see the TODO in the code for more information.
Atul Varma <varmaa@toolness.com>
parents: 152
diff changeset
209 jsFunction->baseLineno = 0;
5ec81091cb89 Added filename/line-number metadata to functions. Wanted to just add a 'script' property but this proved problematic--see the TODO in the code for more information.
Atul Varma <varmaa@toolness.com>
parents: 152
diff changeset
210 jsFunction->lineExtent = 0;
160
56181fb5fc7e Added a function.is_python property indicating whether the js function is implemented in python.
Atul Varma <varmaa@toolness.com>
parents: 154
diff changeset
211 jsFunction->isPython = 0;
152
80463c8c7930 Added a 'name' property to function obects. Fixed a TODO.
Atul Varma <varmaa@toolness.com>
parents: 131
diff changeset
212
80463c8c7930 Added a 'name' property to function obects. Fixed a TODO.
Atul Varma <varmaa@toolness.com>
parents: 131
diff changeset
213 JSString *name = JS_GetFunctionId(jsFunction->fun);
154
5ec81091cb89 Added filename/line-number metadata to functions. Wanted to just add a 'script' property but this proved problematic--see the TODO in the code for more information.
Atul Varma <varmaa@toolness.com>
parents: 152
diff changeset
214 if (name != NULL) {
5ec81091cb89 Added filename/line-number metadata to functions. Wanted to just add a 'script' property but this proved problematic--see the TODO in the code for more information.
Atul Varma <varmaa@toolness.com>
parents: 152
diff changeset
215 // It's not an anonymous function.
152
80463c8c7930 Added a 'name' property to function obects. Fixed a TODO.
Atul Varma <varmaa@toolness.com>
parents: 131
diff changeset
216 jsFunction->name = PYM_jsvalToPyObject(context,
80463c8c7930 Added a 'name' property to function obects. Fixed a TODO.
Atul Varma <varmaa@toolness.com>
parents: 131
diff changeset
217 STRING_TO_JSVAL(name));
80463c8c7930 Added a 'name' property to function obects. Fixed a TODO.
Atul Varma <varmaa@toolness.com>
parents: 131
diff changeset
218 if (jsFunction->name == NULL) {
80463c8c7930 Added a 'name' property to function obects. Fixed a TODO.
Atul Varma <varmaa@toolness.com>
parents: 131
diff changeset
219 Py_DECREF((PyObject *) jsFunction);
80463c8c7930 Added a 'name' property to function obects. Fixed a TODO.
Atul Varma <varmaa@toolness.com>
parents: 131
diff changeset
220 return NULL;
80463c8c7930 Added a 'name' property to function obects. Fixed a TODO.
Atul Varma <varmaa@toolness.com>
parents: 131
diff changeset
221 }
80463c8c7930 Added a 'name' property to function obects. Fixed a TODO.
Atul Varma <varmaa@toolness.com>
parents: 131
diff changeset
222 }
80463c8c7930 Added a 'name' property to function obects. Fixed a TODO.
Atul Varma <varmaa@toolness.com>
parents: 131
diff changeset
223
154
5ec81091cb89 Added filename/line-number metadata to functions. Wanted to just add a 'script' property but this proved problematic--see the TODO in the code for more information.
Atul Varma <varmaa@toolness.com>
parents: 152
diff changeset
224 JSScript *script = JS_GetFunctionScript(context->cx, jsFunction->fun);
5ec81091cb89 Added filename/line-number metadata to functions. Wanted to just add a 'script' property but this proved problematic--see the TODO in the code for more information.
Atul Varma <varmaa@toolness.com>
parents: 152
diff changeset
225
5ec81091cb89 Added filename/line-number metadata to functions. Wanted to just add a 'script' property but this proved problematic--see the TODO in the code for more information.
Atul Varma <varmaa@toolness.com>
parents: 152
diff changeset
226 // TODO: Ideally, we'd convert the script to an object and set it as
5ec81091cb89 Added filename/line-number metadata to functions. Wanted to just add a 'script' property but this proved problematic--see the TODO in the code for more information.
Atul Varma <varmaa@toolness.com>
parents: 152
diff changeset
227 // an attribute of the function, but this results in strange segfaults,
5ec81091cb89 Added filename/line-number metadata to functions. Wanted to just add a 'script' property but this proved problematic--see the TODO in the code for more information.
Atul Varma <varmaa@toolness.com>
parents: 152
diff changeset
228 // perhaps because JS functions destroy their scripts on finalization
5ec81091cb89 Added filename/line-number metadata to functions. Wanted to just add a 'script' property but this proved problematic--see the TODO in the code for more information.
Atul Varma <varmaa@toolness.com>
parents: 152
diff changeset
229 // while creating an object from a script makes it subject to GC.
5ec81091cb89 Added filename/line-number metadata to functions. Wanted to just add a 'script' property but this proved problematic--see the TODO in the code for more information.
Atul Varma <varmaa@toolness.com>
parents: 152
diff changeset
230 if (script) {
5ec81091cb89 Added filename/line-number metadata to functions. Wanted to just add a 'script' property but this proved problematic--see the TODO in the code for more information.
Atul Varma <varmaa@toolness.com>
parents: 152
diff changeset
231 // It's an interpreted function.
5ec81091cb89 Added filename/line-number metadata to functions. Wanted to just add a 'script' property but this proved problematic--see the TODO in the code for more information.
Atul Varma <varmaa@toolness.com>
parents: 152
diff changeset
232 jsFunction->filename = JS_GetScriptFilename(context->cx, script);
5ec81091cb89 Added filename/line-number metadata to functions. Wanted to just add a 'script' property but this proved problematic--see the TODO in the code for more information.
Atul Varma <varmaa@toolness.com>
parents: 152
diff changeset
233 jsFunction->baseLineno = JS_GetScriptBaseLineNumber(context->cx, script);
5ec81091cb89 Added filename/line-number metadata to functions. Wanted to just add a 'script' property but this proved problematic--see the TODO in the code for more information.
Atul Varma <varmaa@toolness.com>
parents: 152
diff changeset
234 jsFunction->lineExtent = JS_GetScriptLineExtent(context->cx, script);
5ec81091cb89 Added filename/line-number metadata to functions. Wanted to just add a 'script' property but this proved problematic--see the TODO in the code for more information.
Atul Varma <varmaa@toolness.com>
parents: 152
diff changeset
235 }
5ec81091cb89 Added filename/line-number metadata to functions. Wanted to just add a 'script' property but this proved problematic--see the TODO in the code for more information.
Atul Varma <varmaa@toolness.com>
parents: 152
diff changeset
236
152
80463c8c7930 Added a 'name' property to function obects. Fixed a TODO.
Atul Varma <varmaa@toolness.com>
parents: 131
diff changeset
237 return jsFunction;
80463c8c7930 Added a 'name' property to function obects. Fixed a TODO.
Atul Varma <varmaa@toolness.com>
parents: 131
diff changeset
238 }
80463c8c7930 Added a 'name' property to function obects. Fixed a TODO.
Atul Varma <varmaa@toolness.com>
parents: 131
diff changeset
239
80463c8c7930 Added a 'name' property to function obects. Fixed a TODO.
Atul Varma <varmaa@toolness.com>
parents: 131
diff changeset
240 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
241 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
242 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
243 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
244 {
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
245 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
246 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
247 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
248 }
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
249
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
250 JSFunction *func = JS_NewFunction(context->cx,
65
f89a582c1276 A few function renamings for consistency.
Atul Varma <varmaa@toolness.com>
parents: 61
diff changeset
251 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
252 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
253
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
254 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
255 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
256 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
257 }
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
258
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
259 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
260
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
261 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
262 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
263 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
264 }
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
265
152
80463c8c7930 Added a 'name' property to function obects. Fixed a TODO.
Atul Varma <varmaa@toolness.com>
parents: 131
diff changeset
266 PYM_JSFunction *object = PYM_newJSFunction(context, func);
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
267 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
268 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
269
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
270 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
271 (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
272 // 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
273 // 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
274 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
275
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
276 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
277 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
278 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
279 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
280 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
281 }
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
282
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
283 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
284 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
285 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
286 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
287 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
288 }
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
289
160
56181fb5fc7e Added a function.is_python property indicating whether the js function is implemented in python.
Atul Varma <varmaa@toolness.com>
parents: 154
diff changeset
290 object->isPython = 1;
56181fb5fc7e Added a function.is_python property indicating whether the js function is implemented in python.
Atul Varma <varmaa@toolness.com>
parents: 154
diff changeset
291
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
292 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
293 }