forked from Distributive-Network/PythonMonkey
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSObjectProxy.cc
More file actions
667 lines (558 loc) · 19.8 KB
/
JSObjectProxy.cc
File metadata and controls
667 lines (558 loc) · 19.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
/**
* @file JSObjectProxy.cc
* @author Caleb Aikens (caleb@distributive.network), Tom Tang (xmader@distributive.network) and Philippe Laporte (philippe@distributive.network)
* @brief JSObjectProxy is a custom C-implemented python type that derives from dict. It acts as a proxy for JSObjects from Spidermonkey, and behaves like a dict would.
* @version 0.1
* @date 2023-06-26
*
* Copyright (c) 2023-2024 Distributive Corp.
*
*/
#include "include/JSObjectProxy.hh"
#include "include/JSObjectIterProxy.hh"
#include "include/JSObjectKeysProxy.hh"
#include "include/JSObjectValuesProxy.hh"
#include "include/JSObjectItemsProxy.hh"
#include "include/modules/pythonmonkey/pythonmonkey.hh"
#include "include/jsTypeFactory.hh"
#include "include/pyTypeFactory.hh"
#include "include/PyBaseProxyHandler.hh"
#include <jsapi.h>
#include <jsfriendapi.h>
#include <Python.h>
#include <object.h>
JSContext *GLOBAL_CX; /**< pointer to PythonMonkey's JSContext */
bool keyToId(PyObject *key, JS::MutableHandleId idp) {
if (PyUnicode_Check(key)) { // key is str type
JS::RootedString idString(GLOBAL_CX);
const char *keyStr = PyUnicode_AsUTF8(key);
JS::ConstUTF8CharsZ utf8Chars(keyStr, strlen(keyStr));
idString.set(JS_NewStringCopyUTF8Z(GLOBAL_CX, utf8Chars));
return JS_StringToId(GLOBAL_CX, idString, idp);
} else if (PyLong_Check(key)) { // key is int type
uint32_t keyAsInt = PyLong_AsUnsignedLong(key); // raise OverflowError if the value of pylong is out of range for a unsigned long
return JS_IndexToId(GLOBAL_CX, keyAsInt, idp);
} else {
return false; // fail
}
}
void JSObjectProxyMethodDefinitions::JSObjectProxy_dealloc(JSObjectProxy *self)
{
// TODO (Caleb Aikens): intentional override of PyDict_Type's tp_dealloc. Probably results in leaking dict memory
self->jsObject.set(nullptr);
PyObject_GC_UnTrack(self);
Py_TYPE(self)->tp_free((PyObject *)self);
}
PyObject *JSObjectProxyMethodDefinitions::JSObjectProxy_new(PyTypeObject *subtype, PyObject *args, PyObject *kwds)
{
return PyDict_Type.tp_new(subtype, args, kwds);
}
int JSObjectProxyMethodDefinitions::JSObjectProxy_init(JSObjectProxy *self, PyObject *args, PyObject *kwds)
{
if (PyDict_Type.tp_init((PyObject *)self, args, kwds) < 0) {
return -1;
}
return 0;
}
Py_ssize_t JSObjectProxyMethodDefinitions::JSObjectProxy_length(JSObjectProxy *self)
{
JS::RootedIdVector props(GLOBAL_CX);
if (!js::GetPropertyKeys(GLOBAL_CX, self->jsObject, JSITER_OWNONLY, &props))
{
// @TODO (Caleb Aikens) raise exception here
return -1;
}
return props.length();
}
PyObject *JSObjectProxyMethodDefinitions::JSObjectProxy_get(JSObjectProxy *self, PyObject *key)
{
JS::RootedId id(GLOBAL_CX);
if (!keyToId(key, &id)) {
// TODO (Caleb Aikens): raise exception here
return NULL; // key is not a str or int
}
// look through the methods for dispatch
for (size_t index = 0;; index++) {
const char *methodName = JSObjectProxyType.tp_methods[index].ml_name;
if (methodName == NULL || !PyUnicode_Check(key)) {
JS::RootedValue *value = new JS::RootedValue(GLOBAL_CX);
JS_GetPropertyById(GLOBAL_CX, self->jsObject, id, value);
JS::RootedObject *thisObj = new JS::RootedObject(GLOBAL_CX, self->jsObject);
return pyTypeFactory(GLOBAL_CX, thisObj, value)->getPyObject();
}
else {
if (strcmp(methodName, PyUnicode_AsUTF8(key)) == 0) {
return PyObject_GenericGetAttr((PyObject *)self, key);
}
}
}
}
int JSObjectProxyMethodDefinitions::JSObjectProxy_contains(JSObjectProxy *self, PyObject *key)
{
JS::RootedId id(GLOBAL_CX);
if (!keyToId(key, &id)) {
// TODO (Caleb Aikens): raise exception here
return -1; // key is not a str or int
}
JS::RootedValue *value = new JS::RootedValue(GLOBAL_CX);
JS_GetPropertyById(GLOBAL_CX, self->jsObject, id, value);
return value->isUndefined() ? 0 : 1;
}
int JSObjectProxyMethodDefinitions::JSObjectProxy_assign(JSObjectProxy *self, PyObject *key, PyObject *value)
{
JS::RootedId id(GLOBAL_CX);
if (!keyToId(key, &id)) { // invalid key
// TODO (Caleb Aikens): raise exception here
return -1;
}
if (value) { // we are setting a value
JS::RootedValue jValue(GLOBAL_CX, jsTypeFactory(GLOBAL_CX, value));
JS_SetPropertyById(GLOBAL_CX, self->jsObject, id, jValue);
} else { // we are deleting a value
JS::ObjectOpResult ignoredResult;
JS_DeletePropertyById(GLOBAL_CX, self->jsObject, id, ignoredResult);
}
return 0;
}
PyObject *JSObjectProxyMethodDefinitions::JSObjectProxy_richcompare(JSObjectProxy *self, PyObject *other, int op)
{
if (op != Py_EQ && op != Py_NE) {
Py_RETURN_NOTIMPLEMENTED;
}
std::unordered_map<PyObject *, PyObject *> visited;
bool isEqual = JSObjectProxy_richcompare_helper(self, other, visited);
switch (op)
{
case Py_EQ:
return PyBool_FromLong(isEqual);
case Py_NE:
return PyBool_FromLong(!isEqual);
default:
return NULL;
}
}
bool JSObjectProxyMethodDefinitions::JSObjectProxy_richcompare_helper(JSObjectProxy *self, PyObject *other, std::unordered_map<PyObject *, PyObject *> &visited)
{
if (!(Py_TYPE(other)->tp_iter) && !PySequence_Check(other) & !PyMapping_Check(other)) { // if other is not a container
return false;
}
if ((visited.find((PyObject *)self) != visited.end() && visited[(PyObject *)self] == other) ||
(visited.find((PyObject *)other) != visited.end() && visited[other] == (PyObject *)self)
) // if we've already compared these before, skip
{
return true;
}
visited.insert({{(PyObject *)self, other}});
if (Py_TYPE((PyObject *)self) == Py_TYPE(other)) {
JS::RootedValue selfVal(GLOBAL_CX, JS::ObjectValue(*self->jsObject));
JS::RootedValue otherVal(GLOBAL_CX, JS::ObjectValue(*(*(JSObjectProxy *)other).jsObject));
if (selfVal.asRawBits() == otherVal.asRawBits()) {
return true;
}
bool *isEqual;
}
JS::RootedIdVector props(GLOBAL_CX);
if (!js::GetPropertyKeys(GLOBAL_CX, self->jsObject, JSITER_OWNONLY, &props))
{
// @TODO (Caleb Aikens) raise exception here
return NULL;
}
// iterate recursively through members of self and check for equality
size_t length = props.length();
for (size_t i = 0; i < length; i++)
{
JS::HandleId id = props[i];
JS::RootedValue *key = new JS::RootedValue(GLOBAL_CX);
key->setString(id.toString());
JS::RootedObject *global = new JS::RootedObject(GLOBAL_CX, JS::GetNonCCWObjectGlobal(self->jsObject));
PyObject *pyKey = pyTypeFactory(GLOBAL_CX, global, key)->getPyObject();
PyObject *pyVal1 = PyObject_GetItem((PyObject *)self, pyKey);
PyObject *pyVal2 = PyObject_GetItem((PyObject *)other, pyKey);
if (!pyVal2) { // if other.key is NULL then not equal
return false;
}
if (pyVal1 && Py_TYPE(pyVal1) == &JSObjectProxyType) { // if either subvalue is a JSObjectProxy, we need to pass around our visited map
if (!JSObjectProxy_richcompare_helper((JSObjectProxy *)pyVal1, pyVal2, visited))
{
return false;
}
}
else if (pyVal2 && Py_TYPE(pyVal2) == &JSObjectProxyType) {
if (!JSObjectProxy_richcompare_helper((JSObjectProxy *)pyVal2, pyVal1, visited))
{
return false;
}
}
else if (PyObject_RichCompare(pyVal1, pyVal2, Py_EQ) == Py_False) {
return false;
}
}
return true;
}
PyObject *JSObjectProxyMethodDefinitions::JSObjectProxy_iter(JSObjectProxy *self) {
// key iteration
JSObjectIterProxy *iterator = PyObject_GC_New(JSObjectIterProxy, &JSObjectIterProxyType);
if (iterator == NULL) {
return NULL;
}
iterator->it.it_index = 0;
iterator->it.reversed = false;
iterator->it.kind = KIND_KEYS;
Py_INCREF(self);
iterator->it.di_dict = (PyDictObject *)self;
iterator->it.props = new JS::RootedIdVector(GLOBAL_CX);
// Get **enumerable** own properties
if (!js::GetPropertyKeys(GLOBAL_CX, self->jsObject, JSITER_OWNONLY, iterator->it.props)) {
return NULL;
}
PyObject_GC_Track(iterator);
return (PyObject *)iterator;
}
PyObject *JSObjectProxyMethodDefinitions::JSObjectProxy_repr(JSObjectProxy *self) {
Py_ssize_t i = Py_ReprEnter((PyObject *)self);
if (i != 0) {
return i > 0 ? PyUnicode_FromString("{...}") : NULL;
}
Py_ssize_t selfLength = JSObjectProxy_length(self);
if (selfLength == 0) {
Py_ReprLeave((PyObject *)self);
return PyUnicode_FromString("{}");
}
_PyUnicodeWriter writer;
_PyUnicodeWriter_Init(&writer);
writer.overallocate = 1;
/* "{" + "1: 2" + ", 3: 4" * (len - 1) + "}" */
writer.min_length = 1 + 4 + (2 + 4) * (selfLength - 1) + 1;
PyObject *key = NULL, *value = NULL;
JS::RootedObject *global = new JS::RootedObject(GLOBAL_CX, JS::GetNonCCWObjectGlobal(self->jsObject));
JS::RootedIdVector props(GLOBAL_CX);
if (_PyUnicodeWriter_WriteChar(&writer, '{') < 0) {
goto error;
}
/* Do repr() on each key+value pair, and insert ": " between them. Note that repr may mutate the dict. */
// Get **enumerable** own properties
if (!js::GetPropertyKeys(GLOBAL_CX, self->jsObject, JSITER_OWNONLY, &props)) {
return NULL;
}
for (Py_ssize_t index = 0; index < selfLength; index++) {
if (index > 0) {
if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0) {
goto error;
}
}
JS::HandleId id = props[index];
key = idToKey(GLOBAL_CX, id);
// Prevent repr from deleting key or value during key format.
Py_INCREF(key);
PyObject *s = PyObject_Repr(key);
if (s == NULL) {
Py_DECREF(s);
goto error;
}
int res = _PyUnicodeWriter_WriteStr(&writer, s);
if (res < 0) {
goto error;
}
if (_PyUnicodeWriter_WriteASCIIString(&writer, ": ", 2) < 0) {
goto error;
}
JS::RootedValue *elementVal = new JS::RootedValue(GLOBAL_CX);
JS_GetPropertyById(GLOBAL_CX, self->jsObject, id, elementVal);
if (&elementVal->toObject() == self->jsObject.get()) {
value = (PyObject *)self;
} else {
value = pyTypeFactory(GLOBAL_CX, global, elementVal)->getPyObject();
}
Py_INCREF(value);
s = PyObject_Repr(value);
if (s == NULL) {
Py_DECREF(s);
goto error;
}
res = _PyUnicodeWriter_WriteStr(&writer, s);
Py_DECREF(s);
if (res < 0) {
goto error;
}
Py_CLEAR(key);
Py_CLEAR(value);
}
writer.overallocate = 0;
if (_PyUnicodeWriter_WriteChar(&writer, '}') < 0) {
goto error;
}
Py_ReprLeave((PyObject *)self);
return _PyUnicodeWriter_Finish(&writer);
error:
Py_ReprLeave((PyObject *)self);
_PyUnicodeWriter_Dealloc(&writer);
Py_XDECREF(key);
Py_XDECREF(value);
return NULL;
}
// private
static int mergeFromSeq2(JSObjectProxy *self, PyObject *seq2) {
PyObject *it; /* iter(seq2) */
Py_ssize_t i; /* index into seq2 of current element */
PyObject *item; /* seq2[i] */
PyObject *fast; /* item as a 2-tuple or 2-list */
it = PyObject_GetIter(seq2);
if (it == NULL)
return -1;
for (i = 0;; ++i) {
PyObject *key, *value;
Py_ssize_t n;
fast = NULL;
item = PyIter_Next(it);
if (item == NULL) {
if (PyErr_Occurred())
goto Fail;
break;
}
/* Convert item to sequence, and verify length 2. */
fast = PySequence_Fast(item, "");
if (fast == NULL) {
if (PyErr_ExceptionMatches(PyExc_TypeError))
PyErr_Format(PyExc_TypeError,
"cannot convert dictionary update "
"sequence element #%zd to a sequence",
i);
goto Fail;
}
n = PySequence_Fast_GET_SIZE(fast);
if (n != 2) {
PyErr_Format(PyExc_ValueError,
"dictionary update sequence element #%zd "
"has length %zd; 2 is required",
i, n);
goto Fail;
}
/* Update/merge with this (key, value) pair. */
key = PySequence_Fast_GET_ITEM(fast, 0);
value = PySequence_Fast_GET_ITEM(fast, 1);
Py_INCREF(key);
Py_INCREF(value);
if (JSObjectProxyMethodDefinitions::JSObjectProxy_assign(self, key, value) < 0) {
Py_DECREF(key);
Py_DECREF(value);
goto Fail;
}
Py_DECREF(key);
Py_DECREF(value);
Py_DECREF(fast);
Py_DECREF(item);
}
i = 0;
goto Return;
Fail:
Py_XDECREF(item);
Py_XDECREF(fast);
i = -1;
Return:
Py_DECREF(it);
return Py_SAFE_DOWNCAST(i, Py_ssize_t, int);
}
PyObject *JSObjectProxyMethodDefinitions::JSObjectProxy_or(JSObjectProxy *self, PyObject *other) {
#if PY_VERSION_HEX < 0x03090000
// | is not supported on dicts in python3.8 or less, so only allow if both
// operands are JSObjectProxy
if (!PyObject_TypeCheck(self, &JSObjectProxyType) || !PyObject_TypeCheck(other, &JSObjectProxyType)) {
Py_RETURN_NOTIMPLEMENTED;
}
#endif
if (!PyDict_Check(self) || !PyDict_Check(other)) {
Py_RETURN_NOTIMPLEMENTED;
}
if (!PyObject_TypeCheck(self, &JSObjectProxyType) && PyObject_TypeCheck(other, &JSObjectProxyType)) {
return PyDict_Type.tp_as_number->nb_or((PyObject *)&(self->dict), other);
}
else {
JS::Rooted<JS::ValueArray<3>> args(GLOBAL_CX);
args[0].setObjectOrNull(JS_NewPlainObject(GLOBAL_CX));
args[1].setObjectOrNull(self->jsObject); // this is null is left operand is real dict
JS::RootedValue jValueOther(GLOBAL_CX, jsTypeFactory(GLOBAL_CX, other));
args[2].setObject(jValueOther.toObject());
JS::RootedObject *global = new JS::RootedObject(GLOBAL_CX, JS::GetNonCCWObjectGlobal(self->jsObject));
// call Object.assign
JS::RootedValue Object(GLOBAL_CX);
if (!JS_GetProperty(GLOBAL_CX, *global, "Object", &Object)) {
PyErr_Format(PyExc_SystemError, "%s JSAPI call failed", JSObjectProxyType.tp_name);
return NULL;
}
JS::RootedObject rootedObject(GLOBAL_CX, Object.toObjectOrNull());
JS::RootedValue *ret = new JS::RootedValue(GLOBAL_CX);
if (!JS_CallFunctionName(GLOBAL_CX, rootedObject, "assign", args, ret)) {
PyErr_Format(PyExc_SystemError, "%s JSAPI call failed", JSObjectProxyType.tp_name);
return NULL;
}
return pyTypeFactory(GLOBAL_CX, global, ret)->getPyObject();
}
}
PyObject *JSObjectProxyMethodDefinitions::JSObjectProxy_ior(JSObjectProxy *self, PyObject *other) {
if (PyDict_Check(other)) {
JS::Rooted<JS::ValueArray<2>> args(GLOBAL_CX);
args[0].setObjectOrNull(self->jsObject);
JS::RootedValue jValueOther(GLOBAL_CX, jsTypeFactory(GLOBAL_CX, other));
args[1].setObject(jValueOther.toObject());
JS::RootedObject *global = new JS::RootedObject(GLOBAL_CX, JS::GetNonCCWObjectGlobal(self->jsObject));
// call Object.assign
JS::RootedValue Object(GLOBAL_CX);
if (!JS_GetProperty(GLOBAL_CX, *global, "Object", &Object)) {
PyErr_Format(PyExc_SystemError, "%s JSAPI call failed", JSObjectProxyType.tp_name);
return NULL;
}
JS::RootedObject rootedObject(GLOBAL_CX, Object.toObjectOrNull());
JS::RootedValue ret(GLOBAL_CX);
if (!JS_CallFunctionName(GLOBAL_CX, rootedObject, "assign", args, &ret)) {
PyErr_Format(PyExc_SystemError, "%s JSAPI call failed", JSObjectProxyType.tp_name);
return NULL;
}
}
else {
if (mergeFromSeq2(self, other) < 0) {
return NULL;
}
}
Py_INCREF(self);
return (PyObject *)self;
}
PyObject *JSObjectProxyMethodDefinitions::JSObjectProxy_get_method(JSObjectProxy *self, PyObject *const *args, Py_ssize_t nargs) {
PyObject *key;
PyObject *default_value = Py_None;
if (!_PyArg_CheckPositional("get", nargs, 1, 2)) {
return NULL;
}
key = args[0];
if (nargs < 2) {
goto skip_optional;
}
default_value = args[1];
skip_optional:
PyObject *value = JSObjectProxy_get(self, key);
if (value == Py_None) {
value = default_value;
}
Py_XINCREF(value);
return value;
}
PyObject *JSObjectProxyMethodDefinitions::JSObjectProxy_setdefault_method(JSObjectProxy *self, PyObject *const *args, Py_ssize_t nargs) {
PyObject *key;
PyObject *default_value = Py_None;
if (!_PyArg_CheckPositional("setdefault", nargs, 1, 2)) {
return NULL;
}
key = args[0];
if (nargs < 2) {
goto skip_optional;
}
default_value = args[1];
skip_optional:
PyObject* value = JSObjectProxy_get(self, key);
if (value == Py_None) {
JSObjectProxy_assign(self, key, default_value);
Py_XINCREF(default_value);
return default_value;
}
Py_XINCREF(value);
return value;
}
PyObject *JSObjectProxyMethodDefinitions::JSObjectProxy_pop_method(JSObjectProxy *self, PyObject *const *args, Py_ssize_t nargs) {
PyObject *key;
PyObject *default_value = NULL;
if (!_PyArg_CheckPositional("pop", nargs, 1, 2)) {
return NULL;
}
key = args[0];
if (nargs < 2) {
goto skip_optional;
}
default_value = args[1];
skip_optional:
JS::RootedId id(GLOBAL_CX);
if (!keyToId(key, &id)) {
// TODO (Caleb Aikens): raise exception here PyObject *seq = PyTuple_New(length);
return NULL;
}
JS::RootedValue *value = new JS::RootedValue(GLOBAL_CX);
JS_GetPropertyById(GLOBAL_CX, self->jsObject, id, value);
if (value->isUndefined()) {
if (default_value != NULL) {
Py_INCREF(default_value);
return default_value;
}
_PyErr_SetKeyError(key);
return NULL;
}
else {
JS::ObjectOpResult ignoredResult;
JS_DeletePropertyById(GLOBAL_CX, self->jsObject, id, ignoredResult);
JS::RootedObject *global = new JS::RootedObject(GLOBAL_CX, JS::GetNonCCWObjectGlobal(self->jsObject));
return pyTypeFactory(GLOBAL_CX, global, value)->getPyObject();
}
}
PyObject *JSObjectProxyMethodDefinitions::JSObjectProxy_clear_method(JSObjectProxy *self) {
JS::RootedIdVector props(GLOBAL_CX);
if (!js::GetPropertyKeys(GLOBAL_CX, self->jsObject, JSITER_OWNONLY, &props))
{
// @TODO (Caleb Aikens) raise exception here
return NULL;
}
JS::ObjectOpResult ignoredResult;
size_t length = props.length();
for (size_t index = 0; index < length; index++) {
JS_DeletePropertyById(GLOBAL_CX, self->jsObject, props[index], ignoredResult);
}
Py_RETURN_NONE;
}
PyObject *JSObjectProxyMethodDefinitions::JSObjectProxy_copy_method(JSObjectProxy *self) {
JS::Rooted<JS::ValueArray<2>> args(GLOBAL_CX);
args[0].setObjectOrNull(JS_NewPlainObject(GLOBAL_CX));
args[1].setObjectOrNull(self->jsObject);
JS::RootedObject *global = new JS::RootedObject(GLOBAL_CX, JS::GetNonCCWObjectGlobal(self->jsObject));
// call Object.assign
JS::RootedValue Object(GLOBAL_CX);
if (!JS_GetProperty(GLOBAL_CX, *global, "Object", &Object)) {
PyErr_Format(PyExc_SystemError, "%s JSAPI call failed", JSObjectProxyType.tp_name);
return NULL;
}
JS::RootedObject rootedObject(GLOBAL_CX, Object.toObjectOrNull());
JS::RootedValue *ret = new JS::RootedValue(GLOBAL_CX);
if (!JS_CallFunctionName(GLOBAL_CX, rootedObject, "assign", args, ret)) {
PyErr_Format(PyExc_SystemError, "%s JSAPI call failed", JSObjectProxyType.tp_name);
return NULL;
}
return pyTypeFactory(GLOBAL_CX, global, ret)->getPyObject();
}
PyObject *JSObjectProxyMethodDefinitions::JSObjectProxy_update_method(JSObjectProxy *self, PyObject *args, PyObject *kwds) {
PyObject *arg = NULL;
int result = 0;
if (!PyArg_UnpackTuple(args, "update", 0, 1, &arg)) {
return NULL;
}
else if (arg != NULL) {
if (PyDict_CheckExact(arg) || PyObject_TypeCheck(arg, &JSObjectProxyType)) {
JSObjectProxyMethodDefinitions::JSObjectProxy_ior((JSObjectProxy *)self, arg);
result = 0;
} else { // iterable
result = mergeFromSeq2((JSObjectProxy *)self, arg);
if (result < 0) {
return NULL;
}
}
}
if (result == 0 && kwds != NULL) {
if (PyArg_ValidateKeywordArguments(kwds)) {
JSObjectProxyMethodDefinitions::JSObjectProxy_ior((JSObjectProxy *)self, kwds);
}
}
Py_RETURN_NONE;
}
PyObject *JSObjectProxyMethodDefinitions::JSObjectProxy_keys_method(JSObjectProxy *self) {
return _PyDictView_New((PyObject *)self, &JSObjectKeysProxyType);
}
PyObject *JSObjectProxyMethodDefinitions::JSObjectProxy_values_method(JSObjectProxy *self) {
return _PyDictView_New((PyObject *)self, &JSObjectValuesProxyType);
}
PyObject *JSObjectProxyMethodDefinitions::JSObjectProxy_items_method(JSObjectProxy *self) {
return _PyDictView_New((PyObject *)self, &JSObjectItemsProxyType);
}