Mercurial > pymonkey
annotate function.c @ 49:7401af070bb8
Moved README to Sphinx documentation. Keeping the rendered docs in the repository so that they can be viewed via HG and locally by people who don't have Sphinx installed.
author | Atul Varma <varmaa@toolness.com> |
---|---|
date | Tue, 07 Jul 2009 09:26:36 -0700 |
parents | bc4263c6ae82 |
children | 427b01954b22 |
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 // TODO: What if there's still a reference to the callable in |
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 // JS-land? |
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 if (self->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
|
46 Py_DECREF(self->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
|
47 self->callable = 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
|
48 } |
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
|
49 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
|
50 } |
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
|
51 |
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 static JSBool |
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
|
53 dispatchJSFunctionToPython(JSContext *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
|
54 JSObject *obj, |
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 uintN argc, |
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 jsval *argv, |
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 jsval *rval) |
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
|
58 { |
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
|
59 jsval callee = JS_ARGV_CALLEE(argv); |
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
|
60 jsval jsCallable; |
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
|
61 if (!JS_GetReservedSlot(cx, JSVAL_TO_OBJECT(callee), 0, &jsCallable)) { |
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
|
62 JS_ReportError(cx, "JS_GetReservedSlot() 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
|
63 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
|
64 } |
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 PyObject *callable = (PyObject *) JSVAL_TO_PRIVATE(jsCallable); |
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
|
66 |
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
|
67 // TODO: Convert args and 'this' parameter. |
43
5727675b1bcb
JS-wrapped python functions now take a context object as their first parameter.
Atul Varma <varmaa@toolness.com>
parents:
40
diff
changeset
|
68 PyObject *args = PyTuple_New(1); |
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
|
69 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
|
70 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
|
71 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
|
72 } |
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
|
73 |
43
5727675b1bcb
JS-wrapped python functions now take a context object as their first parameter.
Atul Varma <varmaa@toolness.com>
parents:
40
diff
changeset
|
74 PYM_JSContextObject *context = (PYM_JSContextObject *) |
5727675b1bcb
JS-wrapped python functions now take a context object as their first parameter.
Atul Varma <varmaa@toolness.com>
parents:
40
diff
changeset
|
75 JS_GetContextPrivate(cx); |
5727675b1bcb
JS-wrapped python functions now take a context object as their first parameter.
Atul Varma <varmaa@toolness.com>
parents:
40
diff
changeset
|
76 Py_INCREF(context); |
5727675b1bcb
JS-wrapped python functions now take a context object as their first parameter.
Atul Varma <varmaa@toolness.com>
parents:
40
diff
changeset
|
77 PyTuple_SET_ITEM(args, 0, (PyObject *) context); |
5727675b1bcb
JS-wrapped python functions now take a context object as their first parameter.
Atul Varma <varmaa@toolness.com>
parents:
40
diff
changeset
|
78 |
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
|
79 PyObject *result = PyObject_Call(callable, 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
|
80 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
|
81 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
|
82 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
|
83 } |
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
|
84 |
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
|
85 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
|
86 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
|
87 |
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
|
88 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
|
89 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
|
90 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
|
91 } |
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
|
92 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
|
93 } |
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
|
94 |
39 | 95 PyTypeObject PYM_JSFunctionType = { |
96 PyObject_HEAD_INIT(NULL) | |
97 0, /*ob_size*/ | |
98 "pymonkey.Function", /*tp_name*/ | |
99 sizeof(PYM_JSFunction), /*tp_basicsize*/ | |
100 0, /*tp_itemsize*/ | |
101 /*tp_dealloc*/ | |
102 (destructor) PYM_JSFunctionDealloc, | |
103 0, /*tp_print*/ | |
104 0, /*tp_getattr*/ | |
105 0, /*tp_setattr*/ | |
106 0, /*tp_compare*/ | |
107 0, /*tp_repr*/ | |
108 0, /*tp_as_number*/ | |
109 0, /*tp_as_sequence*/ | |
110 0, /*tp_as_mapping*/ | |
111 0, /*tp_hash */ | |
112 0, /*tp_call*/ | |
113 0, /*tp_str*/ | |
114 0, /*tp_getattro*/ | |
115 0, /*tp_setattro*/ | |
116 0, /*tp_as_buffer*/ | |
117 /*tp_flags*/ | |
118 Py_TPFLAGS_DEFAULT, | |
119 /* tp_doc */ | |
120 "JavaScript Function.", | |
121 0, /* tp_traverse */ | |
122 0, /* tp_clear */ | |
123 0, /* tp_richcompare */ | |
124 0, /* tp_weaklistoffset */ | |
125 0, /* tp_iter */ | |
126 0, /* tp_iternext */ | |
127 0, /* tp_methods */ | |
128 0, /* tp_members */ | |
129 0, /* tp_getset */ | |
130 0, /* tp_base */ | |
131 0, /* tp_dict */ | |
132 0, /* tp_descr_get */ | |
133 0, /* tp_descr_set */ | |
134 0, /* tp_dictoffset */ | |
135 0, /* tp_init */ | |
136 0, /* tp_alloc */ | |
137 0, /* tp_new */ | |
138 }; | |
139 | |
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
|
140 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
|
141 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
|
142 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
|
143 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
|
144 { |
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
|
145 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
|
146 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
|
147 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
|
148 } |
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
|
149 |
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
|
150 JSFunction *func = JS_NewFunction(context->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
|
151 dispatchJSFunctionToPython, 0, |
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
|
152 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
|
153 |
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
|
154 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
|
155 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
|
156 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
|
157 } |
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
|
158 |
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
|
159 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
|
160 |
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
|
161 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
|
162 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
|
163 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
|
164 } |
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
|
165 |
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
|
166 if (!JS_SetReservedSlot(context->cx, funcObj, 0, |
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
|
167 PRIVATE_TO_JSVAL(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
|
168 PyErr_SetString(PYM_error, "JS_SetReservedSlot() 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
|
169 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
|
170 } |
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
|
171 |
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 *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
|
173 &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
|
174 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
|
175 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
|
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 object->callable = 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
|
178 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
|
179 (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
|
180 // 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
|
181 // 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
|
182 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
|
183 |
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 object->callable = 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
|
185 Py_INCREF(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
|
186 |
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 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
|
188 } |