annotate context.c @ 66:b49180c39d0a

Pymonkey now handles the GIL properly so that Python code can run while JS code does.
author Atul Varma <varmaa@toolness.com>
date Sun, 26 Jul 2009 15:06:19 -0700
parents fb7e11dec538
children b5160c82be65
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: 47
diff changeset
1 /* ***** BEGIN LICENSE BLOCK *****
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 47
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: 47
diff changeset
3 *
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 47
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: 47
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: 47
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: 47
diff changeset
7 * http://www.mozilla.org/MPL/
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
8 *
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 47
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: 47
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: 47
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: 47
diff changeset
12 * License.
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
13 *
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
14 * The Original Code is Pymonkey.
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
15 *
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 47
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: 47
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: 47
diff changeset
18 * the Initial Developer. All Rights Reserved.
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
19 *
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
20 * Contributor(s):
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
21 * Atul Varma <atul@mozilla.com>
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
22 *
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 47
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: 47
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: 47
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: 47
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: 47
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: 47
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: 47
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: 47
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: 47
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: 47
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: 47
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: 47
diff changeset
34 *
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
35 * ***** END LICENSE BLOCK ***** */
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
36
11
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
37 #include "context.h"
13
ca17531e8c81 Added an object class.
Atul Varma <varmaa@toolness.com>
parents: 12
diff changeset
38 #include "object.h"
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: 33
diff changeset
39 #include "function.h"
13
ca17531e8c81 Added an object class.
Atul Varma <varmaa@toolness.com>
parents: 12
diff changeset
40 #include "utils.h"
11
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
41
64
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 60
diff changeset
42 static JSBool
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 60
diff changeset
43 PYM_operationCallback(JSContext *cx)
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 60
diff changeset
44 {
66
b49180c39d0a Pymonkey now handles the GIL properly so that Python code can run while JS code does.
Atul Varma <varmaa@toolness.com>
parents: 64
diff changeset
45 PYM_PyAutoEnsureGIL gil;
64
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 60
diff changeset
46 PYM_JSContextObject *context = (PYM_JSContextObject *)
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 60
diff changeset
47 JS_GetContextPrivate(cx);
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 60
diff changeset
48
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 60
diff changeset
49 PyObject *callable = context->opCallback;
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 60
diff changeset
50 PyObject *args = PyTuple_Pack(1, (PyObject *) context);
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 60
diff changeset
51 if (args == NULL) {
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 60
diff changeset
52 JS_ReportOutOfMemory(cx);
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 60
diff changeset
53 return JS_FALSE;
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 60
diff changeset
54 }
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 60
diff changeset
55 PyObject *result = PyObject_Call(callable, args, NULL);
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 60
diff changeset
56 Py_DECREF(args);
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 60
diff changeset
57 if (result == NULL) {
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 60
diff changeset
58 PYM_pythonExceptionToJs(context);
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 60
diff changeset
59 return JS_FALSE;
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 60
diff changeset
60 }
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 60
diff changeset
61
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 60
diff changeset
62 Py_DECREF(result);
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 60
diff changeset
63 return JS_TRUE;
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 60
diff changeset
64 }
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 60
diff changeset
65
48
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
66 // This is the default JSErrorReporter for pymonkey-owned JS contexts.
46
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 44
diff changeset
67 static void
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 44
diff changeset
68 PYM_reportError(JSContext *cx, const char *message, JSErrorReport *report)
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 44
diff changeset
69 {
66
b49180c39d0a Pymonkey now handles the GIL properly so that Python code can run while JS code does.
Atul Varma <varmaa@toolness.com>
parents: 64
diff changeset
70 PYM_PyAutoEnsureGIL gil;
b49180c39d0a Pymonkey now handles the GIL properly so that Python code can run while JS code does.
Atul Varma <varmaa@toolness.com>
parents: 64
diff changeset
71
47
3f4982759e55 Converting JS exceptions into Python exceptions is now doable, albeit not yet implemented, thanks to the discovery of JSOPTION_DONT_REPORT_UNCAUGHT. Also, JS warnings are now converted into Python warnings.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
72 // Convert JS warnings into Python warnings.
3f4982759e55 Converting JS exceptions into Python exceptions is now doable, albeit not yet implemented, thanks to the discovery of JSOPTION_DONT_REPORT_UNCAUGHT. Also, JS warnings are now converted into Python warnings.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
73 if (JSREPORT_IS_WARNING(report->flags)) {
3f4982759e55 Converting JS exceptions into Python exceptions is now doable, albeit not yet implemented, thanks to the discovery of JSOPTION_DONT_REPORT_UNCAUGHT. Also, JS warnings are now converted into Python warnings.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
74 if (report->filename)
3f4982759e55 Converting JS exceptions into Python exceptions is now doable, albeit not yet implemented, thanks to the discovery of JSOPTION_DONT_REPORT_UNCAUGHT. Also, JS warnings are now converted into Python warnings.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
75 PyErr_WarnExplicit(NULL, message, report->filename, report->lineno,
3f4982759e55 Converting JS exceptions into Python exceptions is now doable, albeit not yet implemented, thanks to the discovery of JSOPTION_DONT_REPORT_UNCAUGHT. Also, JS warnings are now converted into Python warnings.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
76 NULL, NULL);
3f4982759e55 Converting JS exceptions into Python exceptions is now doable, albeit not yet implemented, thanks to the discovery of JSOPTION_DONT_REPORT_UNCAUGHT. Also, JS warnings are now converted into Python warnings.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
77 else
3f4982759e55 Converting JS exceptions into Python exceptions is now doable, albeit not yet implemented, thanks to the discovery of JSOPTION_DONT_REPORT_UNCAUGHT. Also, JS warnings are now converted into Python warnings.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
78 PyErr_Warn(NULL, message);
3f4982759e55 Converting JS exceptions into Python exceptions is now doable, albeit not yet implemented, thanks to the discovery of JSOPTION_DONT_REPORT_UNCAUGHT. Also, JS warnings are now converted into Python warnings.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
79 } else
3f4982759e55 Converting JS exceptions into Python exceptions is now doable, albeit not yet implemented, thanks to the discovery of JSOPTION_DONT_REPORT_UNCAUGHT. Also, JS warnings are now converted into Python warnings.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
80 // TODO: Not sure if this will ever get called, but we should know
3f4982759e55 Converting JS exceptions into Python exceptions is now doable, albeit not yet implemented, thanks to the discovery of JSOPTION_DONT_REPORT_UNCAUGHT. Also, JS warnings are now converted into Python warnings.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
81 // if it is.
3f4982759e55 Converting JS exceptions into Python exceptions is now doable, albeit not yet implemented, thanks to the discovery of JSOPTION_DONT_REPORT_UNCAUGHT. Also, JS warnings are now converted into Python warnings.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
82 PyErr_Warn(NULL, "A JS error was reported.");
46
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 44
diff changeset
83 }
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 44
diff changeset
84
11
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
85 static void
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
86 PYM_JSContextDealloc(PYM_JSContextObject *self)
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
87 {
64
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 60
diff changeset
88 if (self->opCallback) {
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 60
diff changeset
89 Py_DECREF(self->opCallback);
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 60
diff changeset
90 self->opCallback = NULL;
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 60
diff changeset
91 }
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 60
diff changeset
92
11
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
93 if (self->cx) {
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
94 JS_DestroyContext(self->cx);
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
95 self->cx = NULL;
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
96 }
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
97
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
98 Py_DECREF(self->runtime);
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
99 self->runtime = NULL;
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
100
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
101 self->ob_type->tp_free((PyObject *) self);
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
102 }
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
103
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
104 static PyObject *
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
105 PYM_getRuntime(PYM_JSContextObject *self, PyObject *args)
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
106 {
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
107 Py_INCREF(self->runtime);
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
108 return (PyObject *) self->runtime;
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
109 }
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
110
13
ca17531e8c81 Added an object class.
Atul Varma <varmaa@toolness.com>
parents: 12
diff changeset
111 static PyObject *
ca17531e8c81 Added an object class.
Atul Varma <varmaa@toolness.com>
parents: 12
diff changeset
112 PYM_newObject(PYM_JSContextObject *self, PyObject *args)
ca17531e8c81 Added an object class.
Atul Varma <varmaa@toolness.com>
parents: 12
diff changeset
113 {
18
f3223debd70b Refactoring; moved PYM_JSObject creation code into a new public function in object.c.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
114 JSObject *obj = JS_NewObject(self->cx, &PYM_JS_ObjectClass, NULL, NULL);
f3223debd70b Refactoring; moved PYM_JSObject creation code into a new public function in object.c.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
115 if (obj == NULL) {
13
ca17531e8c81 Added an object class.
Atul Varma <varmaa@toolness.com>
parents: 12
diff changeset
116 PyErr_SetString(PYM_error, "JS_NewObject() failed");
ca17531e8c81 Added an object class.
Atul Varma <varmaa@toolness.com>
parents: 12
diff changeset
117 return NULL;
ca17531e8c81 Added an object class.
Atul Varma <varmaa@toolness.com>
parents: 12
diff changeset
118 }
ca17531e8c81 Added an object class.
Atul Varma <varmaa@toolness.com>
parents: 12
diff changeset
119
19
fbb9a61fa030 Moved context creation code into its own public function in context.c.
Atul Varma <varmaa@toolness.com>
parents: 18
diff changeset
120 // If this fails, we don't need to worry about cleaning up
fbb9a61fa030 Moved context creation code into its own public function in context.c.
Atul Varma <varmaa@toolness.com>
parents: 18
diff changeset
121 // obj because it'll get cleaned up at the next GC.
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: 33
diff changeset
122 return (PyObject *) PYM_newJSObject(self, obj, NULL);
13
ca17531e8c81 Added an object class.
Atul Varma <varmaa@toolness.com>
parents: 12
diff changeset
123 }
ca17531e8c81 Added an object class.
Atul Varma <varmaa@toolness.com>
parents: 12
diff changeset
124
14
9edcdb4ab12d added an init_standard_classes() method to context objects.
Atul Varma <varmaa@toolness.com>
parents: 13
diff changeset
125 static PyObject *
17
0812422ec99e Added a context.get_property() method.
Atul Varma <varmaa@toolness.com>
parents: 16
diff changeset
126 PYM_getProperty(PYM_JSContextObject *self, PyObject *args)
0812422ec99e Added a context.get_property() method.
Atul Varma <varmaa@toolness.com>
parents: 16
diff changeset
127 {
29
608d086d12e3 Added a new PYM_pyObjectToJsval() function that only supports unicode for the moment. Also, whereever we're assuming that Py_UNICODE is UCS-2, we're surrounding such code with #ifndef Py_UNICODE_WIDE.
Atul Varma <varmaa@toolness.com>
parents: 27
diff changeset
128 #ifndef Py_UNICODE_WIDE
17
0812422ec99e Added a context.get_property() method.
Atul Varma <varmaa@toolness.com>
parents: 16
diff changeset
129 PYM_JSObject *object;
27
21045074139f Based on my new understanding of JSString */jschar * thanks to folks on #jsapi, I've removed the requirement that SpiderMonkey be in UTF-8 mode to translate strings between Python and SpiderMonkey.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
130 Py_UNICODE *string;
17
0812422ec99e Added a context.get_property() method.
Atul Varma <varmaa@toolness.com>
parents: 16
diff changeset
131
27
21045074139f Based on my new understanding of JSString */jschar * thanks to folks on #jsapi, I've removed the requirement that SpiderMonkey be in UTF-8 mode to translate strings between Python and SpiderMonkey.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
132 if (!PyArg_ParseTuple(args, "O!u", &PYM_JSObjectType, &object,
21045074139f Based on my new understanding of JSString */jschar * thanks to folks on #jsapi, I've removed the requirement that SpiderMonkey be in UTF-8 mode to translate strings between Python and SpiderMonkey.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
133 &string))
17
0812422ec99e Added a context.get_property() method.
Atul Varma <varmaa@toolness.com>
parents: 16
diff changeset
134 return NULL;
0812422ec99e Added a context.get_property() method.
Atul Varma <varmaa@toolness.com>
parents: 16
diff changeset
135
27
21045074139f Based on my new understanding of JSString */jschar * thanks to folks on #jsapi, I've removed the requirement that SpiderMonkey be in UTF-8 mode to translate strings between Python and SpiderMonkey.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
136 JSString *jsString = JS_NewUCStringCopyZ(self->cx,
21045074139f Based on my new understanding of JSString */jschar * thanks to folks on #jsapi, I've removed the requirement that SpiderMonkey be in UTF-8 mode to translate strings between Python and SpiderMonkey.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
137 (const jschar *) string);
17
0812422ec99e Added a context.get_property() method.
Atul Varma <varmaa@toolness.com>
parents: 16
diff changeset
138 if (jsString == NULL) {
0812422ec99e Added a context.get_property() method.
Atul Varma <varmaa@toolness.com>
parents: 16
diff changeset
139 PyErr_SetString(PYM_error, "JS_NewStringCopyZ() failed");
0812422ec99e Added a context.get_property() method.
Atul Varma <varmaa@toolness.com>
parents: 16
diff changeset
140 return NULL;
0812422ec99e Added a context.get_property() method.
Atul Varma <varmaa@toolness.com>
parents: 16
diff changeset
141 }
0812422ec99e Added a context.get_property() method.
Atul Varma <varmaa@toolness.com>
parents: 16
diff changeset
142
0812422ec99e Added a context.get_property() method.
Atul Varma <varmaa@toolness.com>
parents: 16
diff changeset
143 jsval val;
66
b49180c39d0a Pymonkey now handles the GIL properly so that Python code can run while JS code does.
Atul Varma <varmaa@toolness.com>
parents: 64
diff changeset
144 JSBool result;
b49180c39d0a Pymonkey now handles the GIL properly so that Python code can run while JS code does.
Atul Varma <varmaa@toolness.com>
parents: 64
diff changeset
145 Py_BEGIN_ALLOW_THREADS;
b49180c39d0a Pymonkey now handles the GIL properly so that Python code can run while JS code does.
Atul Varma <varmaa@toolness.com>
parents: 64
diff changeset
146 result = JS_GetUCProperty(self->cx, object->obj,
b49180c39d0a Pymonkey now handles the GIL properly so that Python code can run while JS code does.
Atul Varma <varmaa@toolness.com>
parents: 64
diff changeset
147 JS_GetStringChars(jsString),
b49180c39d0a Pymonkey now handles the GIL properly so that Python code can run while JS code does.
Atul Varma <varmaa@toolness.com>
parents: 64
diff changeset
148 JS_GetStringLength(jsString), &val);
b49180c39d0a Pymonkey now handles the GIL properly so that Python code can run while JS code does.
Atul Varma <varmaa@toolness.com>
parents: 64
diff changeset
149 Py_END_ALLOW_THREADS;
b49180c39d0a Pymonkey now handles the GIL properly so that Python code can run while JS code does.
Atul Varma <varmaa@toolness.com>
parents: 64
diff changeset
150
b49180c39d0a Pymonkey now handles the GIL properly so that Python code can run while JS code does.
Atul Varma <varmaa@toolness.com>
parents: 64
diff changeset
151 if (!result) {
17
0812422ec99e Added a context.get_property() method.
Atul Varma <varmaa@toolness.com>
parents: 16
diff changeset
152 // TODO: Get the actual JS exception. Any exception that exists
0812422ec99e Added a context.get_property() method.
Atul Varma <varmaa@toolness.com>
parents: 16
diff changeset
153 // here will probably still be pending on the JS context.
0812422ec99e Added a context.get_property() method.
Atul Varma <varmaa@toolness.com>
parents: 16
diff changeset
154 PyErr_SetString(PYM_error, "Getting property failed.");
0812422ec99e Added a context.get_property() method.
Atul Varma <varmaa@toolness.com>
parents: 16
diff changeset
155 return NULL;
0812422ec99e Added a context.get_property() method.
Atul Varma <varmaa@toolness.com>
parents: 16
diff changeset
156 }
0812422ec99e Added a context.get_property() method.
Atul Varma <varmaa@toolness.com>
parents: 16
diff changeset
157
21
fc04e5f1c675 Changed object constructor to take a context instead of a runtime.
Atul Varma <varmaa@toolness.com>
parents: 20
diff changeset
158 return PYM_jsvalToPyObject(self, val);
29
608d086d12e3 Added a new PYM_pyObjectToJsval() function that only supports unicode for the moment. Also, whereever we're assuming that Py_UNICODE is UCS-2, we're surrounding such code with #ifndef Py_UNICODE_WIDE.
Atul Varma <varmaa@toolness.com>
parents: 27
diff changeset
159 #else
608d086d12e3 Added a new PYM_pyObjectToJsval() function that only supports unicode for the moment. Also, whereever we're assuming that Py_UNICODE is UCS-2, we're surrounding such code with #ifndef Py_UNICODE_WIDE.
Atul Varma <varmaa@toolness.com>
parents: 27
diff changeset
160 PyErr_SetString(PyExc_NotImplementedError,
608d086d12e3 Added a new PYM_pyObjectToJsval() function that only supports unicode for the moment. Also, whereever we're assuming that Py_UNICODE is UCS-2, we're surrounding such code with #ifndef Py_UNICODE_WIDE.
Atul Varma <varmaa@toolness.com>
parents: 27
diff changeset
161 "This function is not yet implemented for wide "
608d086d12e3 Added a new PYM_pyObjectToJsval() function that only supports unicode for the moment. Also, whereever we're assuming that Py_UNICODE is UCS-2, we're surrounding such code with #ifndef Py_UNICODE_WIDE.
Atul Varma <varmaa@toolness.com>
parents: 27
diff changeset
162 "unicode builds of Python.");
608d086d12e3 Added a new PYM_pyObjectToJsval() function that only supports unicode for the moment. Also, whereever we're assuming that Py_UNICODE is UCS-2, we're surrounding such code with #ifndef Py_UNICODE_WIDE.
Atul Varma <varmaa@toolness.com>
parents: 27
diff changeset
163 return NULL;
608d086d12e3 Added a new PYM_pyObjectToJsval() function that only supports unicode for the moment. Also, whereever we're assuming that Py_UNICODE is UCS-2, we're surrounding such code with #ifndef Py_UNICODE_WIDE.
Atul Varma <varmaa@toolness.com>
parents: 27
diff changeset
164 #endif
17
0812422ec99e Added a context.get_property() method.
Atul Varma <varmaa@toolness.com>
parents: 16
diff changeset
165 }
0812422ec99e Added a context.get_property() method.
Atul Varma <varmaa@toolness.com>
parents: 16
diff changeset
166
0812422ec99e Added a context.get_property() method.
Atul Varma <varmaa@toolness.com>
parents: 16
diff changeset
167 static PyObject *
60
e557d84318a7 Added a gc() method to the context object which performs garbage collection.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
168 PYM_gc(PYM_JSContextObject *self, PyObject *args)
e557d84318a7 Added a gc() method to the context object which performs garbage collection.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
169 {
e557d84318a7 Added a gc() method to the context object which performs garbage collection.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
170 JS_GC(self->cx);
e557d84318a7 Added a gc() method to the context object which performs garbage collection.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
171 Py_RETURN_NONE;
e557d84318a7 Added a gc() method to the context object which performs garbage collection.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
172 }
e557d84318a7 Added a gc() method to the context object which performs garbage collection.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
173
e557d84318a7 Added a gc() method to the context object which performs garbage collection.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
174 static PyObject *
14
9edcdb4ab12d added an init_standard_classes() method to context objects.
Atul Varma <varmaa@toolness.com>
parents: 13
diff changeset
175 PYM_initStandardClasses(PYM_JSContextObject *self, PyObject *args)
9edcdb4ab12d added an init_standard_classes() method to context objects.
Atul Varma <varmaa@toolness.com>
parents: 13
diff changeset
176 {
9edcdb4ab12d added an init_standard_classes() method to context objects.
Atul Varma <varmaa@toolness.com>
parents: 13
diff changeset
177 PYM_JSObject *object;
9edcdb4ab12d added an init_standard_classes() method to context objects.
Atul Varma <varmaa@toolness.com>
parents: 13
diff changeset
178
9edcdb4ab12d added an init_standard_classes() method to context objects.
Atul Varma <varmaa@toolness.com>
parents: 13
diff changeset
179 if (!PyArg_ParseTuple(args, "O!", &PYM_JSObjectType, &object))
9edcdb4ab12d added an init_standard_classes() method to context objects.
Atul Varma <varmaa@toolness.com>
parents: 13
diff changeset
180 return NULL;
9edcdb4ab12d added an init_standard_classes() method to context objects.
Atul Varma <varmaa@toolness.com>
parents: 13
diff changeset
181
9edcdb4ab12d added an init_standard_classes() method to context objects.
Atul Varma <varmaa@toolness.com>
parents: 13
diff changeset
182 if (!JS_InitStandardClasses(self->cx, object->obj)) {
9edcdb4ab12d added an init_standard_classes() method to context objects.
Atul Varma <varmaa@toolness.com>
parents: 13
diff changeset
183 PyErr_SetString(PYM_error, "JS_InitStandardClasses() failed");
9edcdb4ab12d added an init_standard_classes() method to context objects.
Atul Varma <varmaa@toolness.com>
parents: 13
diff changeset
184 return NULL;
9edcdb4ab12d added an init_standard_classes() method to context objects.
Atul Varma <varmaa@toolness.com>
parents: 13
diff changeset
185 }
9edcdb4ab12d added an init_standard_classes() method to context objects.
Atul Varma <varmaa@toolness.com>
parents: 13
diff changeset
186
9edcdb4ab12d added an init_standard_classes() method to context objects.
Atul Varma <varmaa@toolness.com>
parents: 13
diff changeset
187 Py_RETURN_NONE;
9edcdb4ab12d added an init_standard_classes() method to context objects.
Atul Varma <varmaa@toolness.com>
parents: 13
diff changeset
188 }
9edcdb4ab12d added an init_standard_classes() method to context objects.
Atul Varma <varmaa@toolness.com>
parents: 13
diff changeset
189
16
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 15
diff changeset
190 static PyObject *
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 15
diff changeset
191 PYM_evaluateScript(PYM_JSContextObject *self, PyObject *args)
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 15
diff changeset
192 {
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 15
diff changeset
193 PYM_JSObject *object;
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 15
diff changeset
194 const char *source;
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 15
diff changeset
195 int sourceLen;
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 15
diff changeset
196 const char *filename;
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 15
diff changeset
197 int lineNo;
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 15
diff changeset
198
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 15
diff changeset
199 if (!PyArg_ParseTuple(args, "O!s#si", &PYM_JSObjectType, &object,
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 15
diff changeset
200 &source, &sourceLen, &filename, &lineNo))
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 15
diff changeset
201 return NULL;
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 15
diff changeset
202
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 15
diff changeset
203 JS_BeginRequest(self->cx);
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 15
diff changeset
204
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 15
diff changeset
205 jsval rval;
66
b49180c39d0a Pymonkey now handles the GIL properly so that Python code can run while JS code does.
Atul Varma <varmaa@toolness.com>
parents: 64
diff changeset
206 JSBool result;
b49180c39d0a Pymonkey now handles the GIL properly so that Python code can run while JS code does.
Atul Varma <varmaa@toolness.com>
parents: 64
diff changeset
207 Py_BEGIN_ALLOW_THREADS;
b49180c39d0a Pymonkey now handles the GIL properly so that Python code can run while JS code does.
Atul Varma <varmaa@toolness.com>
parents: 64
diff changeset
208 result = JS_EvaluateScript(self->cx, object->obj, source, sourceLen,
b49180c39d0a Pymonkey now handles the GIL properly so that Python code can run while JS code does.
Atul Varma <varmaa@toolness.com>
parents: 64
diff changeset
209 filename, lineNo, &rval);
b49180c39d0a Pymonkey now handles the GIL properly so that Python code can run while JS code does.
Atul Varma <varmaa@toolness.com>
parents: 64
diff changeset
210 Py_END_ALLOW_THREADS;
b49180c39d0a Pymonkey now handles the GIL properly so that Python code can run while JS code does.
Atul Varma <varmaa@toolness.com>
parents: 64
diff changeset
211
b49180c39d0a Pymonkey now handles the GIL properly so that Python code can run while JS code does.
Atul Varma <varmaa@toolness.com>
parents: 64
diff changeset
212 if (!result) {
46
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 44
diff changeset
213 PYM_jsExceptionToPython(self);
16
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 15
diff changeset
214 JS_EndRequest(self->cx);
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 15
diff changeset
215 return NULL;
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 15
diff changeset
216 }
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 15
diff changeset
217
21
fc04e5f1c675 Changed object constructor to take a context instead of a runtime.
Atul Varma <varmaa@toolness.com>
parents: 20
diff changeset
218 PyObject *pyRval = PYM_jsvalToPyObject(self, rval);
16
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 15
diff changeset
219
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 15
diff changeset
220 JS_EndRequest(self->cx);
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 15
diff changeset
221
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 15
diff changeset
222 return pyRval;
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 15
diff changeset
223 }
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 15
diff changeset
224
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: 33
diff changeset
225 static PyObject *
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: 33
diff changeset
226 PYM_defineProperty(PYM_JSContextObject *self, PyObject *args)
24
74b7ad049542 Added very primitive support for calling python functions from JS.
Atul Varma <varmaa@toolness.com>
parents: 21
diff changeset
227 {
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: 33
diff changeset
228 PYM_JSObject *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: 33
diff changeset
229 PyObject *value;
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: 33
diff changeset
230 const char *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: 33
diff changeset
231
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: 33
diff changeset
232 if (!PyArg_ParseTuple(args, "O!sO", &PYM_JSObjectType, &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: 33
diff changeset
233 &name, &value))
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: 33
diff changeset
234 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: 33
diff changeset
235
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: 33
diff changeset
236 jsval jsValue;
24
74b7ad049542 Added very primitive support for calling python functions from JS.
Atul Varma <varmaa@toolness.com>
parents: 21
diff changeset
237
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
238 if (PYM_pyObjectToJsval(self, value, &jsValue) == -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: 33
diff changeset
239 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: 33
diff changeset
240
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: 33
diff changeset
241 // TODO: Support unicode naming.
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: 33
diff changeset
242 if (!JS_DefineProperty(self->cx, object->obj, name, jsValue,
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: 33
diff changeset
243 NULL, NULL, JSPROP_ENUMERATE)) {
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: 33
diff changeset
244 // TODO: There's probably an exception pending on self->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: 33
diff changeset
245 // what should we do about it?
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: 33
diff changeset
246 PyErr_SetString(PYM_error, "JS_DefineProperty() 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: 33
diff changeset
247 return NULL;
24
74b7ad049542 Added very primitive support for calling python functions from JS.
Atul Varma <varmaa@toolness.com>
parents: 21
diff changeset
248 }
74b7ad049542 Added very primitive support for calling python functions from JS.
Atul Varma <varmaa@toolness.com>
parents: 21
diff changeset
249
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: 33
diff changeset
250 Py_RETURN_NONE;
24
74b7ad049542 Added very primitive support for calling python functions from JS.
Atul Varma <varmaa@toolness.com>
parents: 21
diff changeset
251 }
74b7ad049542 Added very primitive support for calling python functions from JS.
Atul Varma <varmaa@toolness.com>
parents: 21
diff changeset
252
74b7ad049542 Added very primitive support for calling python functions from JS.
Atul Varma <varmaa@toolness.com>
parents: 21
diff changeset
253 static PyObject *
41
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
254 PYM_callFunction(PYM_JSContextObject *self, PyObject *args)
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
255 {
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
256 PYM_JSObject *obj;
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
257 PYM_JSFunction *fun;
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
258 PyObject *funcArgs;
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
259
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
260 if (!PyArg_ParseTuple(args, "O!O!O!", &PYM_JSObjectType, &obj,
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
261 &PYM_JSFunctionType, &fun,
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
262 &PyTuple_Type, &funcArgs))
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
263 return NULL;
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
264
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
265 uintN argc = PyTuple_Size(funcArgs);
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
266 jsval argv[argc];
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
267 jsval *currArg = argv;
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
268
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
269 for (unsigned int i = 0; i < argc; i++) {
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
270 PyObject *item = PyTuple_GET_ITEM(funcArgs, i);
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
271 if (item == NULL ||
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
272 PYM_pyObjectToJsval(self, item, currArg) == -1)
41
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
273 return NULL;
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
274 currArg++;
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
275 }
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
276
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
277 jsval rval;
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
278
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
279 // TODO: This assumes that a JSFunction * is actually a subclass of
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
280 // a JSObject *, which may or may not be regarded as an implementation
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
281 // detail.
66
b49180c39d0a Pymonkey now handles the GIL properly so that Python code can run while JS code does.
Atul Varma <varmaa@toolness.com>
parents: 64
diff changeset
282 JSBool result;
b49180c39d0a Pymonkey now handles the GIL properly so that Python code can run while JS code does.
Atul Varma <varmaa@toolness.com>
parents: 64
diff changeset
283 Py_BEGIN_ALLOW_THREADS;
b49180c39d0a Pymonkey now handles the GIL properly so that Python code can run while JS code does.
Atul Varma <varmaa@toolness.com>
parents: 64
diff changeset
284 result = JS_CallFunction(self->cx, obj->obj,
b49180c39d0a Pymonkey now handles the GIL properly so that Python code can run while JS code does.
Atul Varma <varmaa@toolness.com>
parents: 64
diff changeset
285 (JSFunction *) fun->base.obj,
b49180c39d0a Pymonkey now handles the GIL properly so that Python code can run while JS code does.
Atul Varma <varmaa@toolness.com>
parents: 64
diff changeset
286 argc, argv, &rval);
b49180c39d0a Pymonkey now handles the GIL properly so that Python code can run while JS code does.
Atul Varma <varmaa@toolness.com>
parents: 64
diff changeset
287 Py_END_ALLOW_THREADS;
b49180c39d0a Pymonkey now handles the GIL properly so that Python code can run while JS code does.
Atul Varma <varmaa@toolness.com>
parents: 64
diff changeset
288
b49180c39d0a Pymonkey now handles the GIL properly so that Python code can run while JS code does.
Atul Varma <varmaa@toolness.com>
parents: 64
diff changeset
289 if (!result) {
46
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 44
diff changeset
290 PYM_jsExceptionToPython(self);
41
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
291 return NULL;
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
292 }
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
293
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
294 return PYM_jsvalToPyObject(self, rval);
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
295 }
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
296
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
297 static PyObject *
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: 33
diff changeset
298 PYM_newFunction(PYM_JSContextObject *self, PyObject *args)
24
74b7ad049542 Added very primitive support for calling python functions from JS.
Atul Varma <varmaa@toolness.com>
parents: 21
diff changeset
299 {
74b7ad049542 Added very primitive support for calling python functions from JS.
Atul Varma <varmaa@toolness.com>
parents: 21
diff changeset
300 PyObject *callable;
74b7ad049542 Added very primitive support for calling python functions from JS.
Atul Varma <varmaa@toolness.com>
parents: 21
diff changeset
301 const char *name;
74b7ad049542 Added very primitive support for calling python functions from JS.
Atul Varma <varmaa@toolness.com>
parents: 21
diff changeset
302
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: 33
diff changeset
303 if (!PyArg_ParseTuple(args, "Os", &callable, &name))
24
74b7ad049542 Added very primitive support for calling python functions from JS.
Atul Varma <varmaa@toolness.com>
parents: 21
diff changeset
304 return NULL;
74b7ad049542 Added very primitive support for calling python functions from JS.
Atul Varma <varmaa@toolness.com>
parents: 21
diff changeset
305
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: 38
diff changeset
306 return (PyObject *) PYM_newJSFunctionFromCallable(self, callable, name);
24
74b7ad049542 Added very primitive support for calling python functions from JS.
Atul Varma <varmaa@toolness.com>
parents: 21
diff changeset
307 }
74b7ad049542 Added very primitive support for calling python functions from JS.
Atul Varma <varmaa@toolness.com>
parents: 21
diff changeset
308
64
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 60
diff changeset
309 static PyObject *
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 60
diff changeset
310 PYM_setOperationCallback(PYM_JSContextObject *self, PyObject *args)
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 60
diff changeset
311 {
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 60
diff changeset
312 PyObject *callable;
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 60
diff changeset
313
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 60
diff changeset
314 if (!PyArg_ParseTuple(args, "O", &callable))
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 60
diff changeset
315 return NULL;
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 60
diff changeset
316
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 60
diff changeset
317 if (!PyCallable_Check(callable)) {
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 60
diff changeset
318 PyErr_SetString(PyExc_TypeError, "Callable must be callable");
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 60
diff changeset
319 return NULL;
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 60
diff changeset
320 }
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 60
diff changeset
321
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 60
diff changeset
322 JS_SetOperationCallback(self->cx, PYM_operationCallback);
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 60
diff changeset
323
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 60
diff changeset
324 Py_INCREF(callable);
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 60
diff changeset
325 if (self->opCallback)
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 60
diff changeset
326 Py_DECREF(self->opCallback);
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 60
diff changeset
327 self->opCallback = callable;
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 60
diff changeset
328
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 60
diff changeset
329 Py_RETURN_NONE;
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 60
diff changeset
330 }
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 60
diff changeset
331
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 60
diff changeset
332 static PyObject *
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 60
diff changeset
333 PYM_triggerOperationCallback(PYM_JSContextObject *self, PyObject *args)
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 60
diff changeset
334 {
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 60
diff changeset
335 JS_TriggerOperationCallback(self->cx);
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 60
diff changeset
336 Py_RETURN_NONE;
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 60
diff changeset
337 }
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 60
diff changeset
338
11
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
339 static PyMethodDef PYM_JSContextMethods[] = {
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
340 {"get_runtime", (PyCFunction) PYM_getRuntime, METH_VARARGS,
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
341 "Get the JavaScript runtime associated with this context."},
13
ca17531e8c81 Added an object class.
Atul Varma <varmaa@toolness.com>
parents: 12
diff changeset
342 {"new_object", (PyCFunction) PYM_newObject, METH_VARARGS,
ca17531e8c81 Added an object class.
Atul Varma <varmaa@toolness.com>
parents: 12
diff changeset
343 "Create a new JavaScript object."},
14
9edcdb4ab12d added an init_standard_classes() method to context objects.
Atul Varma <varmaa@toolness.com>
parents: 13
diff changeset
344 {"init_standard_classes",
9edcdb4ab12d added an init_standard_classes() method to context objects.
Atul Varma <varmaa@toolness.com>
parents: 13
diff changeset
345 (PyCFunction) PYM_initStandardClasses, METH_VARARGS,
9edcdb4ab12d added an init_standard_classes() method to context objects.
Atul Varma <varmaa@toolness.com>
parents: 13
diff changeset
346 "Add standard classes and functions to the given object."},
16
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 15
diff changeset
347 {"evaluate_script",
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 15
diff changeset
348 (PyCFunction) PYM_evaluateScript, METH_VARARGS,
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 15
diff changeset
349 "Evaluate the given JavaScript code in the context of the given "
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 15
diff changeset
350 "global object, using the given filename"
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 15
diff changeset
351 "and line number information."},
41
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
352 {"call_function",
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
353 (PyCFunction) PYM_callFunction, METH_VARARGS,
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
354 "Calls a JS function."},
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: 33
diff changeset
355 {"new_function",
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: 33
diff changeset
356 (PyCFunction) PYM_newFunction, METH_VARARGS,
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: 33
diff changeset
357 "Creates a new function callable from JS."},
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: 33
diff changeset
358 {"define_property",
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: 33
diff changeset
359 (PyCFunction) PYM_defineProperty, METH_VARARGS,
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: 33
diff changeset
360 "Defines a property on an object."},
17
0812422ec99e Added a context.get_property() method.
Atul Varma <varmaa@toolness.com>
parents: 16
diff changeset
361 {"get_property", (PyCFunction) PYM_getProperty, METH_VARARGS,
0812422ec99e Added a context.get_property() method.
Atul Varma <varmaa@toolness.com>
parents: 16
diff changeset
362 "Gets the given property for the given JavaScript object."},
60
e557d84318a7 Added a gc() method to the context object which performs garbage collection.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
363 {"gc", (PyCFunction) PYM_gc, METH_VARARGS,
e557d84318a7 Added a gc() method to the context object which performs garbage collection.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
364 "Performs garbage collection on the context's runtime."},
64
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 60
diff changeset
365 {"set_operation_callback", (PyCFunction) PYM_setOperationCallback,
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 60
diff changeset
366 METH_VARARGS,
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 60
diff changeset
367 "Sets the operation callback for the context."},
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 60
diff changeset
368 {"trigger_operation_callback", (PyCFunction) PYM_triggerOperationCallback,
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 60
diff changeset
369 METH_VARARGS,
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 60
diff changeset
370 "Triggers the operation callback for the context."},
11
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
371 {NULL, NULL, 0, NULL}
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
372 };
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
373
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
374 PyTypeObject PYM_JSContextType = {
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
375 PyObject_HEAD_INIT(NULL)
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
376 0, /*ob_size*/
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
377 "pymonkey.Context", /*tp_name*/
12
6d95cfaa1e0b Fixed a code typo.
Atul Varma <varmaa@toolness.com>
parents: 11
diff changeset
378 sizeof(PYM_JSContextObject), /*tp_basicsize*/
11
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
379 0, /*tp_itemsize*/
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
380 /*tp_dealloc*/
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
381 (destructor) PYM_JSContextDealloc,
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
382 0, /*tp_print*/
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
383 0, /*tp_getattr*/
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
384 0, /*tp_setattr*/
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
385 0, /*tp_compare*/
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
386 0, /*tp_repr*/
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
387 0, /*tp_as_number*/
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
388 0, /*tp_as_sequence*/
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
389 0, /*tp_as_mapping*/
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
390 0, /*tp_hash */
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
391 0, /*tp_call*/
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
392 0, /*tp_str*/
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
393 0, /*tp_getattro*/
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
394 0, /*tp_setattro*/
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
395 0, /*tp_as_buffer*/
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
396 Py_TPFLAGS_DEFAULT, /*tp_flags*/
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
397 /* tp_doc */
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
398 "JavaScript Context.",
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
399 0, /* tp_traverse */
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
400 0, /* tp_clear */
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
401 0, /* tp_richcompare */
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
402 0, /* tp_weaklistoffset */
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
403 0, /* tp_iter */
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
404 0, /* tp_iternext */
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
405 PYM_JSContextMethods, /* tp_methods */
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
406 0, /* tp_members */
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
407 0, /* tp_getset */
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
408 0, /* tp_base */
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
409 0, /* tp_dict */
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
410 0, /* tp_descr_get */
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
411 0, /* tp_descr_set */
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
412 0, /* tp_dictoffset */
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
413 0, /* tp_init */
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
414 0, /* tp_alloc */
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
415 0, /* tp_new */
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
416 };
19
fbb9a61fa030 Moved context creation code into its own public function in context.c.
Atul Varma <varmaa@toolness.com>
parents: 18
diff changeset
417
fbb9a61fa030 Moved context creation code into its own public function in context.c.
Atul Varma <varmaa@toolness.com>
parents: 18
diff changeset
418 extern PYM_JSContextObject *
fbb9a61fa030 Moved context creation code into its own public function in context.c.
Atul Varma <varmaa@toolness.com>
parents: 18
diff changeset
419 PYM_newJSContextObject(PYM_JSRuntimeObject *runtime, JSContext *cx)
fbb9a61fa030 Moved context creation code into its own public function in context.c.
Atul Varma <varmaa@toolness.com>
parents: 18
diff changeset
420 {
fbb9a61fa030 Moved context creation code into its own public function in context.c.
Atul Varma <varmaa@toolness.com>
parents: 18
diff changeset
421 PYM_JSContextObject *context = PyObject_New(PYM_JSContextObject,
fbb9a61fa030 Moved context creation code into its own public function in context.c.
Atul Varma <varmaa@toolness.com>
parents: 18
diff changeset
422 &PYM_JSContextType);
fbb9a61fa030 Moved context creation code into its own public function in context.c.
Atul Varma <varmaa@toolness.com>
parents: 18
diff changeset
423 if (context == NULL)
fbb9a61fa030 Moved context creation code into its own public function in context.c.
Atul Varma <varmaa@toolness.com>
parents: 18
diff changeset
424 return NULL;
fbb9a61fa030 Moved context creation code into its own public function in context.c.
Atul Varma <varmaa@toolness.com>
parents: 18
diff changeset
425
64
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 60
diff changeset
426 context->opCallback = NULL;
19
fbb9a61fa030 Moved context creation code into its own public function in context.c.
Atul Varma <varmaa@toolness.com>
parents: 18
diff changeset
427 context->runtime = runtime;
fbb9a61fa030 Moved context creation code into its own public function in context.c.
Atul Varma <varmaa@toolness.com>
parents: 18
diff changeset
428 Py_INCREF(runtime);
fbb9a61fa030 Moved context creation code into its own public function in context.c.
Atul Varma <varmaa@toolness.com>
parents: 18
diff changeset
429
fbb9a61fa030 Moved context creation code into its own public function in context.c.
Atul Varma <varmaa@toolness.com>
parents: 18
diff changeset
430 context->cx = cx;
43
5727675b1bcb JS-wrapped python functions now take a context object as their first parameter.
Atul Varma <varmaa@toolness.com>
parents: 42
diff changeset
431 JS_SetContextPrivate(cx, context);
46
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 44
diff changeset
432 JS_SetErrorReporter(cx, PYM_reportError);
19
fbb9a61fa030 Moved context creation code into its own public function in context.c.
Atul Varma <varmaa@toolness.com>
parents: 18
diff changeset
433
38
6cd870a2b81e If the JS engine supports GC zeal setting, we enable it automatically to help find any memory management bugs.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
434 #ifdef JS_GC_ZEAL
6cd870a2b81e If the JS engine supports GC zeal setting, we enable it automatically to help find any memory management bugs.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
435 // TODO: Consider exposing JS_SetGCZeal() to Python instead of
6cd870a2b81e If the JS engine supports GC zeal setting, we enable it automatically to help find any memory management bugs.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
436 // hard-coding it here.
6cd870a2b81e If the JS engine supports GC zeal setting, we enable it automatically to help find any memory management bugs.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
437 JS_SetGCZeal(cx, 2);
6cd870a2b81e If the JS engine supports GC zeal setting, we enable it automatically to help find any memory management bugs.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
438 #endif
6cd870a2b81e If the JS engine supports GC zeal setting, we enable it automatically to help find any memory management bugs.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
439
19
fbb9a61fa030 Moved context creation code into its own public function in context.c.
Atul Varma <varmaa@toolness.com>
parents: 18
diff changeset
440 return context;
fbb9a61fa030 Moved context creation code into its own public function in context.c.
Atul Varma <varmaa@toolness.com>
parents: 18
diff changeset
441 }