Mercurial > pymonkey
annotate object.c @ 22:988a8998c75f
JS objects reflected into Python are now identity-preserving, though the implementation for this is pretty bad right now.
author | Atul Varma <varmaa@toolness.com> |
---|---|
date | Sun, 28 Jun 2009 21:49:07 -0700 |
parents | fc04e5f1c675 |
children | 951ad1b15587 |
rev | line source |
---|---|
13 | 1 #include "object.h" |
21
fc04e5f1c675
Changed object constructor to take a context instead of a runtime.
Atul Varma <varmaa@toolness.com>
parents:
18
diff
changeset
|
2 #include "runtime.h" |
22
988a8998c75f
JS objects reflected into Python are now identity-preserving, though the implementation for this is pretty bad right now.
Atul Varma <varmaa@toolness.com>
parents:
21
diff
changeset
|
3 #include "utils.h" |
13 | 4 |
5 JSClass PYM_JS_ObjectClass = { | |
6 "PymonkeyObject", JSCLASS_GLOBAL_FLAGS, | |
7 JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, | |
8 JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub, | |
9 JSCLASS_NO_OPTIONAL_MEMBERS | |
10 }; | |
11 | |
12 static void | |
13 PYM_JSObjectDealloc(PYM_JSObject *self) | |
14 { | |
22
988a8998c75f
JS objects reflected into Python are now identity-preserving, though the implementation for this is pretty bad right now.
Atul Varma <varmaa@toolness.com>
parents:
21
diff
changeset
|
15 if (self->weakrefList) |
988a8998c75f
JS objects reflected into Python are now identity-preserving, though the implementation for this is pretty bad right now.
Atul Varma <varmaa@toolness.com>
parents:
21
diff
changeset
|
16 PyObject_ClearWeakRefs((PyObject *) self); |
13 | 17 |
15
baa4cb961330
JS objects in python-land are now rooted while in python-land so that they're not gc'd while in python-land.
Atul Varma <varmaa@toolness.com>
parents:
13
diff
changeset
|
18 if (self->obj) { |
22
988a8998c75f
JS objects reflected into Python are now identity-preserving, though the implementation for this is pretty bad right now.
Atul Varma <varmaa@toolness.com>
parents:
21
diff
changeset
|
19 if (PyDict_DelItem(self->runtime->objects, |
988a8998c75f
JS objects reflected into Python are now identity-preserving, though the implementation for this is pretty bad right now.
Atul Varma <varmaa@toolness.com>
parents:
21
diff
changeset
|
20 self->uniqueId) == -1) |
988a8998c75f
JS objects reflected into Python are now identity-preserving, though the implementation for this is pretty bad right now.
Atul Varma <varmaa@toolness.com>
parents:
21
diff
changeset
|
21 PySys_WriteStderr("WARNING: PyDict_DelItem() failed.\n"); |
988a8998c75f
JS objects reflected into Python are now identity-preserving, though the implementation for this is pretty bad right now.
Atul Varma <varmaa@toolness.com>
parents:
21
diff
changeset
|
22 Py_DECREF(self->uniqueId); |
988a8998c75f
JS objects reflected into Python are now identity-preserving, though the implementation for this is pretty bad right now.
Atul Varma <varmaa@toolness.com>
parents:
21
diff
changeset
|
23 self->uniqueId = NULL; |
988a8998c75f
JS objects reflected into Python are now identity-preserving, though the implementation for this is pretty bad right now.
Atul Varma <varmaa@toolness.com>
parents:
21
diff
changeset
|
24 |
988a8998c75f
JS objects reflected into Python are now identity-preserving, though the implementation for this is pretty bad right now.
Atul Varma <varmaa@toolness.com>
parents:
21
diff
changeset
|
25 // JS_RemoveRoot() always returns JS_TRUE, so don't |
988a8998c75f
JS objects reflected into Python are now identity-preserving, though the implementation for this is pretty bad right now.
Atul Varma <varmaa@toolness.com>
parents:
21
diff
changeset
|
26 // bother checking its return value. |
15
baa4cb961330
JS objects in python-land are now rooted while in python-land so that they're not gc'd while in python-land.
Atul Varma <varmaa@toolness.com>
parents:
13
diff
changeset
|
27 JS_RemoveRootRT(self->runtime->rt, &self->obj); |
baa4cb961330
JS objects in python-land are now rooted while in python-land so that they're not gc'd while in python-land.
Atul Varma <varmaa@toolness.com>
parents:
13
diff
changeset
|
28 self->obj = NULL; |
baa4cb961330
JS objects in python-land are now rooted while in python-land so that they're not gc'd while in python-land.
Atul Varma <varmaa@toolness.com>
parents:
13
diff
changeset
|
29 } |
baa4cb961330
JS objects in python-land are now rooted while in python-land so that they're not gc'd while in python-land.
Atul Varma <varmaa@toolness.com>
parents:
13
diff
changeset
|
30 |
22
988a8998c75f
JS objects reflected into Python are now identity-preserving, though the implementation for this is pretty bad right now.
Atul Varma <varmaa@toolness.com>
parents:
21
diff
changeset
|
31 if (self->runtime) { |
988a8998c75f
JS objects reflected into Python are now identity-preserving, though the implementation for this is pretty bad right now.
Atul Varma <varmaa@toolness.com>
parents:
21
diff
changeset
|
32 Py_DECREF(self->runtime); |
988a8998c75f
JS objects reflected into Python are now identity-preserving, though the implementation for this is pretty bad right now.
Atul Varma <varmaa@toolness.com>
parents:
21
diff
changeset
|
33 self->runtime = NULL; |
988a8998c75f
JS objects reflected into Python are now identity-preserving, though the implementation for this is pretty bad right now.
Atul Varma <varmaa@toolness.com>
parents:
21
diff
changeset
|
34 } |
13 | 35 } |
36 | |
37 PyTypeObject PYM_JSObjectType = { | |
38 PyObject_HEAD_INIT(NULL) | |
39 0, /*ob_size*/ | |
40 "pymonkey.Object", /*tp_name*/ | |
41 sizeof(PYM_JSObject), /*tp_basicsize*/ | |
42 0, /*tp_itemsize*/ | |
43 /*tp_dealloc*/ | |
44 (destructor) PYM_JSObjectDealloc, | |
45 0, /*tp_print*/ | |
46 0, /*tp_getattr*/ | |
47 0, /*tp_setattr*/ | |
48 0, /*tp_compare*/ | |
49 0, /*tp_repr*/ | |
50 0, /*tp_as_number*/ | |
51 0, /*tp_as_sequence*/ | |
52 0, /*tp_as_mapping*/ | |
53 0, /*tp_hash */ | |
54 0, /*tp_call*/ | |
55 0, /*tp_str*/ | |
56 0, /*tp_getattro*/ | |
57 0, /*tp_setattro*/ | |
58 0, /*tp_as_buffer*/ | |
59 Py_TPFLAGS_DEFAULT, /*tp_flags*/ | |
60 /* tp_doc */ | |
61 "JavaScript Object.", | |
62 0, /* tp_traverse */ | |
63 0, /* tp_clear */ | |
64 0, /* tp_richcompare */ | |
22
988a8998c75f
JS objects reflected into Python are now identity-preserving, though the implementation for this is pretty bad right now.
Atul Varma <varmaa@toolness.com>
parents:
21
diff
changeset
|
65 /* tp_weaklistoffset */ |
988a8998c75f
JS objects reflected into Python are now identity-preserving, though the implementation for this is pretty bad right now.
Atul Varma <varmaa@toolness.com>
parents:
21
diff
changeset
|
66 offsetof(PYM_JSObject, weakrefList), |
13 | 67 0, /* tp_iter */ |
68 0, /* tp_iternext */ | |
69 0, /* tp_methods */ | |
70 0, /* tp_members */ | |
71 0, /* tp_getset */ | |
72 0, /* tp_base */ | |
73 0, /* tp_dict */ | |
74 0, /* tp_descr_get */ | |
75 0, /* tp_descr_set */ | |
76 0, /* tp_dictoffset */ | |
77 0, /* tp_init */ | |
78 0, /* tp_alloc */ | |
79 0, /* tp_new */ | |
80 }; | |
18
f3223debd70b
Refactoring; moved PYM_JSObject creation code into a new public function in object.c.
Atul Varma <varmaa@toolness.com>
parents:
15
diff
changeset
|
81 |
21
fc04e5f1c675
Changed object constructor to take a context instead of a runtime.
Atul Varma <varmaa@toolness.com>
parents:
18
diff
changeset
|
82 PYM_JSObject *PYM_newJSObject(PYM_JSContextObject *context, |
18
f3223debd70b
Refactoring; moved PYM_JSObject creation code into a new public function in object.c.
Atul Varma <varmaa@toolness.com>
parents:
15
diff
changeset
|
83 JSObject *obj) { |
22
988a8998c75f
JS objects reflected into Python are now identity-preserving, though the implementation for this is pretty bad right now.
Atul Varma <varmaa@toolness.com>
parents:
21
diff
changeset
|
84 jsid uniqueId; |
988a8998c75f
JS objects reflected into Python are now identity-preserving, though the implementation for this is pretty bad right now.
Atul Varma <varmaa@toolness.com>
parents:
21
diff
changeset
|
85 if (!JS_GetObjectId(context->cx, obj, &uniqueId)) { |
988a8998c75f
JS objects reflected into Python are now identity-preserving, though the implementation for this is pretty bad right now.
Atul Varma <varmaa@toolness.com>
parents:
21
diff
changeset
|
86 PyErr_SetString(PYM_error, "JS_GetObjectId() failed"); |
988a8998c75f
JS objects reflected into Python are now identity-preserving, though the implementation for this is pretty bad right now.
Atul Varma <varmaa@toolness.com>
parents:
21
diff
changeset
|
87 return NULL; |
988a8998c75f
JS objects reflected into Python are now identity-preserving, though the implementation for this is pretty bad right now.
Atul Varma <varmaa@toolness.com>
parents:
21
diff
changeset
|
88 } |
988a8998c75f
JS objects reflected into Python are now identity-preserving, though the implementation for this is pretty bad right now.
Atul Varma <varmaa@toolness.com>
parents:
21
diff
changeset
|
89 PyObject *pyUniqueId = PyLong_FromLong(uniqueId); |
988a8998c75f
JS objects reflected into Python are now identity-preserving, though the implementation for this is pretty bad right now.
Atul Varma <varmaa@toolness.com>
parents:
21
diff
changeset
|
90 if (pyUniqueId == NULL) |
988a8998c75f
JS objects reflected into Python are now identity-preserving, though the implementation for this is pretty bad right now.
Atul Varma <varmaa@toolness.com>
parents:
21
diff
changeset
|
91 return NULL; |
988a8998c75f
JS objects reflected into Python are now identity-preserving, though the implementation for this is pretty bad right now.
Atul Varma <varmaa@toolness.com>
parents:
21
diff
changeset
|
92 |
988a8998c75f
JS objects reflected into Python are now identity-preserving, though the implementation for this is pretty bad right now.
Atul Varma <varmaa@toolness.com>
parents:
21
diff
changeset
|
93 PYM_JSRuntimeObject *runtime = context->runtime; |
988a8998c75f
JS objects reflected into Python are now identity-preserving, though the implementation for this is pretty bad right now.
Atul Varma <varmaa@toolness.com>
parents:
21
diff
changeset
|
94 PyObject *cachedObject = PyDict_GetItem(runtime->objects, |
988a8998c75f
JS objects reflected into Python are now identity-preserving, though the implementation for this is pretty bad right now.
Atul Varma <varmaa@toolness.com>
parents:
21
diff
changeset
|
95 pyUniqueId); |
988a8998c75f
JS objects reflected into Python are now identity-preserving, though the implementation for this is pretty bad right now.
Atul Varma <varmaa@toolness.com>
parents:
21
diff
changeset
|
96 if (cachedObject) { |
988a8998c75f
JS objects reflected into Python are now identity-preserving, though the implementation for this is pretty bad right now.
Atul Varma <varmaa@toolness.com>
parents:
21
diff
changeset
|
97 cachedObject = PyWeakref_GetObject(cachedObject); |
988a8998c75f
JS objects reflected into Python are now identity-preserving, though the implementation for this is pretty bad right now.
Atul Varma <varmaa@toolness.com>
parents:
21
diff
changeset
|
98 Py_INCREF(cachedObject); |
988a8998c75f
JS objects reflected into Python are now identity-preserving, though the implementation for this is pretty bad right now.
Atul Varma <varmaa@toolness.com>
parents:
21
diff
changeset
|
99 Py_DECREF(pyUniqueId); |
988a8998c75f
JS objects reflected into Python are now identity-preserving, though the implementation for this is pretty bad right now.
Atul Varma <varmaa@toolness.com>
parents:
21
diff
changeset
|
100 return (PYM_JSObject *) cachedObject; |
988a8998c75f
JS objects reflected into Python are now identity-preserving, though the implementation for this is pretty bad right now.
Atul Varma <varmaa@toolness.com>
parents:
21
diff
changeset
|
101 } |
988a8998c75f
JS objects reflected into Python are now identity-preserving, though the implementation for this is pretty bad right now.
Atul Varma <varmaa@toolness.com>
parents:
21
diff
changeset
|
102 |
18
f3223debd70b
Refactoring; moved PYM_JSObject creation code into a new public function in object.c.
Atul Varma <varmaa@toolness.com>
parents:
15
diff
changeset
|
103 PYM_JSObject *object = PyObject_New(PYM_JSObject, |
f3223debd70b
Refactoring; moved PYM_JSObject creation code into a new public function in object.c.
Atul Varma <varmaa@toolness.com>
parents:
15
diff
changeset
|
104 &PYM_JSObjectType); |
22
988a8998c75f
JS objects reflected into Python are now identity-preserving, though the implementation for this is pretty bad right now.
Atul Varma <varmaa@toolness.com>
parents:
21
diff
changeset
|
105 if (object == NULL) { |
988a8998c75f
JS objects reflected into Python are now identity-preserving, though the implementation for this is pretty bad right now.
Atul Varma <varmaa@toolness.com>
parents:
21
diff
changeset
|
106 Py_DECREF(pyUniqueId); |
18
f3223debd70b
Refactoring; moved PYM_JSObject creation code into a new public function in object.c.
Atul Varma <varmaa@toolness.com>
parents:
15
diff
changeset
|
107 return NULL; |
22
988a8998c75f
JS objects reflected into Python are now identity-preserving, though the implementation for this is pretty bad right now.
Atul Varma <varmaa@toolness.com>
parents:
21
diff
changeset
|
108 } |
988a8998c75f
JS objects reflected into Python are now identity-preserving, though the implementation for this is pretty bad right now.
Atul Varma <varmaa@toolness.com>
parents:
21
diff
changeset
|
109 |
988a8998c75f
JS objects reflected into Python are now identity-preserving, though the implementation for this is pretty bad right now.
Atul Varma <varmaa@toolness.com>
parents:
21
diff
changeset
|
110 object->runtime = NULL; |
988a8998c75f
JS objects reflected into Python are now identity-preserving, though the implementation for this is pretty bad right now.
Atul Varma <varmaa@toolness.com>
parents:
21
diff
changeset
|
111 object->obj = NULL; |
988a8998c75f
JS objects reflected into Python are now identity-preserving, though the implementation for this is pretty bad right now.
Atul Varma <varmaa@toolness.com>
parents:
21
diff
changeset
|
112 object->uniqueId = NULL; |
988a8998c75f
JS objects reflected into Python are now identity-preserving, though the implementation for this is pretty bad right now.
Atul Varma <varmaa@toolness.com>
parents:
21
diff
changeset
|
113 object->weakrefList = NULL; |
988a8998c75f
JS objects reflected into Python are now identity-preserving, though the implementation for this is pretty bad right now.
Atul Varma <varmaa@toolness.com>
parents:
21
diff
changeset
|
114 |
988a8998c75f
JS objects reflected into Python are now identity-preserving, though the implementation for this is pretty bad right now.
Atul Varma <varmaa@toolness.com>
parents:
21
diff
changeset
|
115 PyObject *weakref = PyWeakref_NewRef((PyObject *) object, NULL); |
988a8998c75f
JS objects reflected into Python are now identity-preserving, though the implementation for this is pretty bad right now.
Atul Varma <varmaa@toolness.com>
parents:
21
diff
changeset
|
116 if (weakref == NULL) { |
988a8998c75f
JS objects reflected into Python are now identity-preserving, though the implementation for this is pretty bad right now.
Atul Varma <varmaa@toolness.com>
parents:
21
diff
changeset
|
117 Py_DECREF(object); |
988a8998c75f
JS objects reflected into Python are now identity-preserving, though the implementation for this is pretty bad right now.
Atul Varma <varmaa@toolness.com>
parents:
21
diff
changeset
|
118 Py_DECREF(pyUniqueId); |
988a8998c75f
JS objects reflected into Python are now identity-preserving, though the implementation for this is pretty bad right now.
Atul Varma <varmaa@toolness.com>
parents:
21
diff
changeset
|
119 return NULL; |
988a8998c75f
JS objects reflected into Python are now identity-preserving, though the implementation for this is pretty bad right now.
Atul Varma <varmaa@toolness.com>
parents:
21
diff
changeset
|
120 } |
988a8998c75f
JS objects reflected into Python are now identity-preserving, though the implementation for this is pretty bad right now.
Atul Varma <varmaa@toolness.com>
parents:
21
diff
changeset
|
121 |
988a8998c75f
JS objects reflected into Python are now identity-preserving, though the implementation for this is pretty bad right now.
Atul Varma <varmaa@toolness.com>
parents:
21
diff
changeset
|
122 if (PyDict_SetItem(runtime->objects, pyUniqueId, weakref) == -1) { |
988a8998c75f
JS objects reflected into Python are now identity-preserving, though the implementation for this is pretty bad right now.
Atul Varma <varmaa@toolness.com>
parents:
21
diff
changeset
|
123 Py_DECREF(weakref); |
988a8998c75f
JS objects reflected into Python are now identity-preserving, though the implementation for this is pretty bad right now.
Atul Varma <varmaa@toolness.com>
parents:
21
diff
changeset
|
124 Py_DECREF(object); |
988a8998c75f
JS objects reflected into Python are now identity-preserving, though the implementation for this is pretty bad right now.
Atul Varma <varmaa@toolness.com>
parents:
21
diff
changeset
|
125 Py_DECREF(pyUniqueId); |
988a8998c75f
JS objects reflected into Python are now identity-preserving, though the implementation for this is pretty bad right now.
Atul Varma <varmaa@toolness.com>
parents:
21
diff
changeset
|
126 return NULL; |
988a8998c75f
JS objects reflected into Python are now identity-preserving, though the implementation for this is pretty bad right now.
Atul Varma <varmaa@toolness.com>
parents:
21
diff
changeset
|
127 } |
18
f3223debd70b
Refactoring; moved PYM_JSObject creation code into a new public function in object.c.
Atul Varma <varmaa@toolness.com>
parents:
15
diff
changeset
|
128 |
21
fc04e5f1c675
Changed object constructor to take a context instead of a runtime.
Atul Varma <varmaa@toolness.com>
parents:
18
diff
changeset
|
129 object->runtime = context->runtime; |
18
f3223debd70b
Refactoring; moved PYM_JSObject creation code into a new public function in object.c.
Atul Varma <varmaa@toolness.com>
parents:
15
diff
changeset
|
130 Py_INCREF(object->runtime); |
f3223debd70b
Refactoring; moved PYM_JSObject creation code into a new public function in object.c.
Atul Varma <varmaa@toolness.com>
parents:
15
diff
changeset
|
131 |
f3223debd70b
Refactoring; moved PYM_JSObject creation code into a new public function in object.c.
Atul Varma <varmaa@toolness.com>
parents:
15
diff
changeset
|
132 object->obj = obj; |
22
988a8998c75f
JS objects reflected into Python are now identity-preserving, though the implementation for this is pretty bad right now.
Atul Varma <varmaa@toolness.com>
parents:
21
diff
changeset
|
133 object->uniqueId = pyUniqueId; |
18
f3223debd70b
Refactoring; moved PYM_JSObject creation code into a new public function in object.c.
Atul Varma <varmaa@toolness.com>
parents:
15
diff
changeset
|
134 |
f3223debd70b
Refactoring; moved PYM_JSObject creation code into a new public function in object.c.
Atul Varma <varmaa@toolness.com>
parents:
15
diff
changeset
|
135 JS_AddNamedRootRT(object->runtime->rt, &object->obj, |
f3223debd70b
Refactoring; moved PYM_JSObject creation code into a new public function in object.c.
Atul Varma <varmaa@toolness.com>
parents:
15
diff
changeset
|
136 "Pymonkey-Generated Object"); |
f3223debd70b
Refactoring; moved PYM_JSObject creation code into a new public function in object.c.
Atul Varma <varmaa@toolness.com>
parents:
15
diff
changeset
|
137 |
f3223debd70b
Refactoring; moved PYM_JSObject creation code into a new public function in object.c.
Atul Varma <varmaa@toolness.com>
parents:
15
diff
changeset
|
138 return object; |
f3223debd70b
Refactoring; moved PYM_JSObject creation code into a new public function in object.c.
Atul Varma <varmaa@toolness.com>
parents:
15
diff
changeset
|
139 } |