-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathjsproxy.c
More file actions
4912 lines (4435 loc) · 135 KB
/
jsproxy.c
File metadata and controls
4912 lines (4435 loc) · 135 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
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* JsProxy Class
*
* The root JsProxy class is a simple class that wraps a JsRef. We define
* overloads for getattr, setattr, delattr, repr, bool, and comparison operators
* on the base class.
*
* We define a wide variety of subclasses on the fly with different operator
* overloads depending on the functionality detected on the wrapped js object.
* This is pretty much an identical strategy to the one used in PyProxy.
*
* Most of the overloads do not require any extra space which is convenient
* because multiple inheritance does not work well with different sized C
* structs. The Callable subclass and the Buffer subclass both need some extra
* space. Currently we use the maximum paranoia approach: JsProxy always
* allocates the extra 12 bytes needed for a Callable, and that way if an object
* ever comes around that is a Buffer and also is Callable, we've got it
* covered.
*
* We create the dynamic types as heap types with PyType_FromSpecWithBases. It's
* a good idea to consult the source for PyType_FromSpecWithBases in
* typeobject.c before modifying since the behavior doesn't exactly match the
* documentation.
*
* We don't currently have any way to define a new heap type
* without leaking the dynamically allocated methods array, but this is fine
* because we never free the dynamic types we construct. (it'd probably be
* possible by subclassing PyType with a different tp_dealloc method).
*/
#define PY_SSIZE_T_CLEAN
#include "Python.h"
#include "python_unexposed.h"
#include "docstring.h"
#include "error_handling.h"
#include "js2python.h"
#include "jsbind.h"
#include "jslib.h"
#include "jsmemops.h"
#include "jsproxy.h"
#include "jsproxy_call.h"
#include "pyproxy.h"
#include "python2js.h"
#include "structmember.h"
// clang-format off
#define IS_ITERABLE (1 << 0)
#define IS_ITERATOR (1 << 1)
#define HAS_LENGTH (1 << 2)
#define HAS_GET (1 << 3)
#define HAS_SET (1 << 4)
#define HAS_HAS (1 << 5)
#define HAS_INCLUDES (1 << 6)
#define HAS_DISPOSE (1 << 7)
#define HAS_ASYNC_DISPOSE (1 << 8)
#define IS_AWAITABLE (1 << 9)
#define IS_BUFFER (1 << 10)
#define IS_CALLABLE (1 << 11)
#define IS_ARRAY (1 << 12)
#define IS_ARRAY_LIKE (1 << 13)
#define IS_TYPEDARRAY (1 << 14)
#define IS_DOUBLE_PROXY (1 << 15)
#define IS_OBJECT_MAP (1 << 16)
#define IS_ASYNC_ITERABLE (1 << 17)
#define IS_GENERATOR (1 << 18)
#define IS_ASYNC_GENERATOR (1 << 19)
#define IS_ASYNC_ITERATOR (1 << 20)
#define IS_ERROR (1 << 21)
#define IS_PY_JSON_DICT (1 << 22)
#define IS_PY_JSON_SEQUENCE (1 << 23)
// clang-format on
_Py_IDENTIFIER(get_event_loop);
_Py_IDENTIFIER(ensure_future);
_Py_IDENTIFIER(create_future);
_Py_IDENTIFIER(set_exception);
_Py_IDENTIFIER(set_result);
_Py_IDENTIFIER(__await__);
_Py_IDENTIFIER(__dir__);
_Py_IDENTIFIER(KeysView);
_Py_IDENTIFIER(ItemsView);
_Py_IDENTIFIER(ValuesView);
_Py_IDENTIFIER(popitem);
_Py_IDENTIFIER(clear);
_Py_IDENTIFIER(update);
_Py_IDENTIFIER(_js_type_flags);
Js_IDENTIFIER(then);
Js_IDENTIFIER(finally);
Js_IDENTIFIER(has);
Js_IDENTIFIER(set);
Js_IDENTIFIER(delete);
Js_IDENTIFIER(includes);
Js_IDENTIFIER(next);
Js_IDENTIFIER(return);
Js_IDENTIFIER(throw);
_Py_IDENTIFIER(fileno);
_Py_IDENTIFIER(register);
static PyObject* collections_abc;
static PyObject* typing;
static PyObject* MutableMapping;
static PyObject* JsProxy_metaclass;
static PyObject* asyncio_mod;
static PyObject* MutableSequence;
static PyObject* Sequence;
static PyObject* MutableMapping;
static PyObject* Mapping;
static PyObject* future_helper_mod;
Js_static_string(PYPROXY_DESTROYED_AT_END_OF_FUNCTION_CALL,
"This borrowed proxy was automatically destroyed at the "
"end of a function call. Try using "
"create_proxy or create_once_callable.");
////////////////////////////////////////////////////////////
// JsProxy
//
// This is a Python object that provides idiomatic access to a JavaScript
// object.
struct BufferFields
{
Py_ssize_t byteLength;
char* format;
Py_ssize_t itemsize;
bool check_assignments;
};
struct MethodFields
{
JsRef this_;
vectorcallfunc vectorcall;
};
struct ExceptionFields
{
PyObject* args;
PyObject* notes;
PyObject* traceback;
PyObject* context;
PyObject* cause;
char suppress_context;
};
struct ObjectMapFields
{
bool hereditary;
};
// clang-format off
// dict and js fields always needs to be in the same place.
// dict field is part of PyBaseExceptionObject, so it should go up top.
// The js field has to come after ExceptionFields so we get the memory layout
// right so we put it at the end
// In between we have a union with the extra fields that are used by just by one
// of JsBuffer, JsCallable, and JsException
typedef struct
{
PyObject_HEAD
PyObject* dict;
union {
struct BufferFields bf;
struct MethodFields mf;
struct ExceptionFields ef;
struct ObjectMapFields omf;
} tf;
JsRef js;
PyObject* signature;
} JsProxy;
// clang-format on
// Layout of dict and ExceptionFields needs to exactly match the layout of the
// same-name fields of BaseException. Otherwise bad things will happen. Check it
// with static asserts!
_Static_assert(offsetof(PyBaseExceptionObject, dict) == offsetof(JsProxy, dict),
"dict layout conflict between JsProxy and PyExc_BaseException");
#define CHECK_EXC_FIELD(field) \
_Static_assert( \
offsetof(PyBaseExceptionObject, field) == \
offsetof(JsProxy, tf) + offsetof(struct ExceptionFields, field), \
"'" #field "' layout conflict between JsProxy and PyExc_BaseException");
CHECK_EXC_FIELD(args);
CHECK_EXC_FIELD(notes);
CHECK_EXC_FIELD(traceback);
CHECK_EXC_FIELD(context);
CHECK_EXC_FIELD(cause);
CHECK_EXC_FIELD(suppress_context);
#undef CHEC_EXC_FIELD
#define FIELD_SIZE(type, field) sizeof(((type*)0)->field)
_Static_assert(sizeof(PyBaseExceptionObject) ==
sizeof(PyObject) + FIELD_SIZE(JsProxy, dict) +
sizeof(struct ExceptionFields),
"size conflict between JsProxy and PyExc_BaseException");
#undef FIELD_SIZE
#define JsProxy_REF(x) ((JsProxy*)x)->js
#define JsProxy_VAL(x) hiwire_get(JsProxy_REF(x))
#define JsProxy_DICT(x) (((JsProxy*)x)->dict)
#define JsProxy_SIG(x) (((JsProxy*)x)->signature)
#define JsMethod_THIS_REF(x) ((JsProxy*)x)->tf.mf.this_
#define JsMethod_THIS(x) JsRef_toVal(JsMethod_THIS_REF(x))
#define JsMethod_VECTORCALL(x) (((JsProxy*)x)->tf.mf.vectorcall)
#define JsException_ARGS(x) (((JsProxy*)x)->tf.ef.args)
#define JsBuffer_FORMAT(x) (((JsProxy*)x)->tf.bf.format)
#define JsBuffer_BYTE_LENGTH(x) (((JsProxy*)x)->tf.bf.byteLength)
#define JsBuffer_ITEMSIZE(x) (((JsProxy*)x)->tf.bf.itemsize)
#define JsBuffer_CHECK_ASSIGNMENTS(x) (((JsProxy*)x)->tf.bf.check_assignments)
#define JsObjMap_HEREDITARY(x) (((JsProxy*)x)->tf.omf.hereditary)
int
JsProxy_getflags(PyObject* self)
{
PyObject* pyflags =
_PyObject_GetAttrId((PyObject*)Py_TYPE(self), &PyId__js_type_flags);
if (pyflags == NULL) {
return -1;
}
int result = PyLong_AsLong(pyflags);
Py_CLEAR(pyflags);
return result;
}
#define OBJMAP_HEREDITARY 1
#define OBJMAP_PY_JSON 2
static int
JsProxy_get_objmap_flags(PyObject* self)
{
int flags = JsProxy_getflags(self);
bool py_json = !!(flags & (IS_PY_JSON_DICT | IS_PY_JSON_SEQUENCE));
bool objmap_hereditary = (flags & IS_OBJECT_MAP) && JsObjMap_HEREDITARY(self);
int result = 0;
if (py_json) {
result |= OBJMAP_PY_JSON;
}
if (objmap_hereditary) {
result |= OBJMAP_HEREDITARY;
}
return result;
}
int
JsProxy_is_py_json(PyObject* self)
{
return !!(JsProxy_getflags(self) & (IS_PY_JSON_DICT | IS_PY_JSON_SEQUENCE));
}
static PyObject*
js2python_objmap(JsVal jsval, int flags)
{
PyObject* result = NULL;
result = js2python_immutable(jsval);
if (result != NULL) {
return result;
}
return JsProxy_create_objmap(jsval, flags);
}
PyObject*
js2python_as_py_json(JsVal jsval)
{
return js2python_objmap(jsval, OBJMAP_PY_JSON);
}
#define INCLUDE_OBJMAP_METHODS(flags) \
!((flags) & (IS_ARRAY | IS_TYPEDARRAY | IS_ARRAY_LIKE | IS_BUFFER | \
IS_DOUBLE_PROXY | IS_ITERATOR | IS_CALLABLE | IS_ERROR))
static int
JsProxy_clear(PyObject* self)
{
int flags = JsProxy_getflags(self);
if (flags == -1) {
return -1;
}
if ((flags & IS_CALLABLE) && (JsMethod_THIS_REF(self) != NULL)) {
JsVal this = hiwire_pop(JsMethod_THIS_REF(self));
if (pyproxy_Check(this)) {
destroy_proxy(this, NULL);
}
}
Py_CLEAR(JsProxy_DICT(self));
Py_CLEAR(JsProxy_SIG(self));
#ifdef DEBUG_F
extern bool tracerefs;
if (tracerefs) {
printf("jsproxy clear %zd, %zd\n", (long)self, (long)JsProxy_REF(self));
}
#endif
if (flags & IS_ERROR) {
if (((PyTypeObject*)PyExc_Exception)->tp_clear(self)) {
return -1;
}
}
hiwire_CLEAR(JsProxy_REF(self));
return 0;
}
static void
JsProxy_dealloc(PyObject* self)
{
FAIL_IF_MINUS_ONE(JsProxy_clear(self));
Py_TYPE(self)->tp_free(self);
return;
finally:
printf("Internal Pyodide error Unraiseable error in JsProxy_dealloc:\n");
PyErr_Print();
}
// attach a signature to a copy of the JsProxy.
// js_id stays the same.
PyObject*
JsProxy_bind_sig(PyObject* self, PyObject* sig)
{
return JsProxy_create_with_this(
JsProxy_VAL(self), JsMethod_THIS(self), sig, JsProxy_is_py_json(self));
}
static PyMethodDef JsProxy_bind_sig_MethodDef = {
"bind_sig",
(PyCFunction)JsProxy_bind_sig,
METH_O,
};
PyObject*
JsProxy_bind_class(PyObject* self, PyObject* sig)
{
PyObject* result = NULL;
// Call `sig = jsbind.bind_class_sig(sig)` and then delegate to
// JsProxy_bind_sig.
// bind_class_sig takes sig and returns type[sig].
_Py_IDENTIFIER(bind_class_sig);
PyObject* class_sig =
_PyObject_CallMethodIdOneArg(jsbind, &PyId_bind_class_sig, sig);
FAIL_IF_NULL(class_sig);
result = JsProxy_bind_sig(self, class_sig);
finally:
Py_CLEAR(class_sig);
return result;
}
static PyMethodDef JsProxy_bind_class_MethodDef = {
"bind_class",
(PyCFunction)JsProxy_bind_class,
METH_O,
};
/**
* repr overload, does `obj.toString()` which produces a low-quality repr.
*/
static PyObject*
JsProxy_Repr(PyObject* self)
{
JsVal repr = JsvObject_toString(JsProxy_VAL(self));
if (JsvError_Check(repr)) {
return NULL;
}
return js2python(repr);
}
/**
* typeof getter, returns `typeof(obj)`.
*/
static PyObject*
JsProxy_typeof(PyObject* self, void* _unused)
{
return js2python(Jsv_typeof(JsProxy_VAL(self)));
}
static PyObject*
JsProxy_js_id(PyObject* self, void* _unused)
{
PyObject* result = NULL;
JsRef idval = JsProxy_REF(self);
int x[2] = { (int)Py_TYPE(self), (int)idval };
Py_hash_t result_c = Py_HashBuffer(x, 8);
FAIL_IF_MINUS_ONE(result_c);
result = PyLong_FromLong(result_c);
finally:
return result;
}
EM_JS_VAL(JsVal, JsProxy_GetAttr_js, (JsVal jsobj, const char* ptrkey), {
const jskey = normalizeReservedWords(UTF8ToString(ptrkey));
const result = jsobj[jskey];
// clang-format off
if (result === undefined && !(jskey in jsobj)) {
// clang-format on
return Module.error;
}
return result;
});
// JsMethodCallSingleton is a special structure which we return from
// JsProxy_GetMethod. The purpose of it is to optimize method calls
// `jsproxy.f()`. When we execute JsProxy_GetMethod(jsproxy, f_unicode), we
// stuff the JS function `jsproxy.f`, the JS object `jsproxy`, and the method
// signature into one of these structs and return it. Then the call is routed to
// this struct which avoids making a JsProxy.
//
// As an additional optimization, we observe that the pattern is always:
//
// method = PyObject_GetMethod(obj, method_name);
// result = PyObject_Call(method, obj, ... other args)
// Py_DECREF(method);
//
// In other words, the return value of `_PyObject_GetMethod` is used exactly
// once. To save on allocations, we make a global called method_call_singleton
// and reuse it if the reference count is 1 (since then the only reference to it
// is our reference). Otherwise we allocate a new one. We shouldn't have to
// allocate a new `method_call_singleton` except when third party code uses
// `_PyObject_GetMethod`.
typedef struct
{
PyObject_HEAD;
JsRef func;
JsRef this_;
PyObject* signature;
vectorcallfunc vectorcall;
} JsMethodCallSingleton;
static PyTypeObject JsMethodCallSingletonType;
static JsMethodCallSingleton* method_call_singleton;
static PyObject*
JsMethodCallSingleton_Vectorcall(PyObject* o,
PyObject* const* pyargs,
size_t nargsf,
PyObject* kwnames)
{
JsMethodCallSingleton* self = (JsMethodCallSingleton*)o;
if (self->func == NULL) {
PyErr_SetString(PyExc_SystemError, "Expected self->func not to be NULL");
return NULL;
}
JsVal func = hiwire_get(self->func);
JsVal this_ = hiwire_get(self->this_);
PyObject* sig = self->signature;
return JsMethod_Vectorcall_impl(func, this_, sig, pyargs, nargsf, kwnames);
}
static JsMethodCallSingleton*
make_method_call_singleton()
{
JsMethodCallSingleton* result =
(JsMethodCallSingleton*)JsMethodCallSingletonType.tp_alloc(
&JsMethodCallSingletonType, 0);
if (result == NULL) {
return NULL;
}
result->vectorcall = JsMethodCallSingleton_Vectorcall;
result->func = NULL;
result->this_ = NULL;
result->signature = NULL;
return result;
}
static int
JsMethodCallSingleton_clear(JsMethodCallSingleton* o)
{
JsMethodCallSingleton* self = (JsMethodCallSingleton*)o;
hiwire_CLEAR(self->func);
hiwire_CLEAR(self->this_);
Py_CLEAR(self->signature);
return 0;
}
// This isn't static so we can call it from conftest.py to prevent leak check
// false positives
EMSCRIPTEN_KEEPALIVE void
clear_method_call_singleton(void)
{
if (Py_REFCNT(method_call_singleton) == 1) {
// We hold the only reference count so we can reuse it.
// Clear it out first.
JsMethodCallSingleton_clear(method_call_singleton);
} else {
// Oops, someone held on to the previous method_call_singleton or otherwise
// used it in an unexpected way. Make another!
// This should never happen except when third party code uses
// `_PyObject_GetMethod`.
Py_SETREF(method_call_singleton, make_method_call_singleton());
}
}
static void
JsMethodCallSingleton_dealloc(PyObject* self)
{
JsMethodCallSingleton_clear((JsMethodCallSingleton*)self);
Py_TYPE(self)->tp_free(self);
}
static PyTypeObject JsMethodCallSingletonType = {
.tp_name = "_pyodide.JsMethodCallSingleton",
.tp_basicsize = sizeof(JsMethodCallSingleton),
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_VECTORCALL,
.tp_vectorcall_offset = offsetof(JsMethodCallSingleton, vectorcall),
.tp_call = (PyCFunctionWithKeywords)PyObject_Vectorcall,
.tp_doc = "A hacky type to avoid temporaries",
.tp_dealloc = JsMethodCallSingleton_dealloc,
};
static PyObject*
JsProxy_GetAttr_helper(PyObject* self, PyObject* attr, bool is_method);
PyObject*
JsProxy_GetMethod(PyObject* self, PyObject* attr)
{
return JsProxy_GetAttr_helper(self, attr, true);
}
PyObject*
JsProxy_GetAttr(PyObject* self, PyObject* attr)
{
return JsProxy_GetAttr_helper(self, attr, false);
}
/**
* getattr overload, first checks whether the attribute exists in the JsProxy
* dict, and if so returns that. Otherwise, it attempts lookup on the wrapped
* object.
*/
static PyObject*
JsProxy_GetAttr_helper(PyObject* self, PyObject* attr, bool is_method)
{
PyObject* result = _PyObject_GenericGetAttrWithDict(self, attr, NULL, 1);
if (result != NULL || PyErr_Occurred()) {
return result;
}
bool success = false;
JsVal jsresult = JS_ERROR;
PyObject* get_attr_sig_res = NULL;
PyObject* attr_sig = NULL;
// result:
PyObject* pyresult = NULL;
const char* key = PyUnicode_AsUTF8(attr);
FAIL_IF_NULL(key);
if (strcmp(key, "keys") == 0 && JsvArray_Check(JsProxy_VAL(self))) {
// Sometimes Python APIs test for the existence of a "keys" function
// to decide whether something should be treated like a dict.
// This mixes badly with the javascript Array.keys API, so pretend that it
// doesn't exist. (Array.keys isn't very useful anyways so hopefully this
// won't confuse too many people...)
PyErr_SetString(PyExc_AttributeError, key);
FAIL();
}
jsresult = JsProxy_GetAttr_js(JsProxy_VAL(self), key);
if (JsvError_Check(jsresult)) {
if (!PyErr_Occurred()) {
PyErr_SetString(PyExc_AttributeError, key);
}
FAIL();
}
if (JsProxy_SIG(self) != NULL) {
_Py_IDENTIFIER(get_attr_sig);
get_attr_sig_res = _PyObject_CallMethodIdObjArgs(
jsbind, &PyId_get_attr_sig, JsProxy_SIG(self), attr, NULL);
FAIL_IF_NULL(get_attr_sig_res);
bool got_converter;
PyObject* sig;
if (!PyArg_ParseTuple(get_attr_sig_res, "pO", &got_converter, &sig)) {
FAIL();
}
if (got_converter) {
pyresult = Js2PyConverter_convert(sig, jsresult, Jsv_null);
goto success;
}
if (!Py_IsNone(sig)) {
attr_sig = Py_XNewRef(sig);
}
}
// attr_sig might contain the result sig or it might be NULL.
// TODO: maybe allow being strict and requiring that we get a sig?
if (pyproxy_Check(jsresult)) {
pyresult = js2python(jsresult);
FAIL_IF_NULL(pyresult);
goto success;
}
if (is_method) {
if (!JsvFunction_Check(jsresult)) {
// Not callable, this should be an error...
PyErr_SetString(PyExc_TypeError, "Expected callable");
FAIL();
}
clear_method_call_singleton();
pyresult = Py_NewRef(method_call_singleton);
method_call_singleton->func = hiwire_new(jsresult);
method_call_singleton->this_ = JsProxy_REF(self);
hiwire_incref(method_call_singleton->this_);
method_call_singleton->signature = Py_NewRef(attr_sig);
goto success;
}
if (JsvFunction_Check(jsresult)) {
pyresult =
JsProxy_create_with_this(jsresult, JsProxy_VAL(self), attr_sig, false);
} else if (attr_sig) {
pyresult =
JsProxy_create_with_this(jsresult, Jsv_undefined, attr_sig, false);
} else {
pyresult = js2python(jsresult);
}
FAIL_IF_NULL(pyresult);
success:
success = true;
finally:
Py_CLEAR(attr_sig);
Py_CLEAR(get_attr_sig_res);
if (!success) {
Py_CLEAR(pyresult);
}
return pyresult;
}
// clang-format off
EM_JS_NUM(errcode,
JsProxy_SetAttr_js,
(JsVal jsobj, const char* ptrkey, JsVal jsval),
{
let jskey = normalizeReservedWords(UTF8ToString(ptrkey));
jsobj[jskey] = jsval;
});
// clang-format on
EM_JS_NUM(errcode, JsProxy_DelAttr_js, (JsVal jsobj, const char* ptrkey), {
let jskey = normalizeReservedWords(UTF8ToString(ptrkey));
delete jsobj[jskey];
});
/**
* setattr / delttr overload. TODO: Raise an error if the attribute exists on
* the proxy.
*/
static int
JsProxy_SetAttr(PyObject* self, PyObject* attr, PyObject* pyvalue)
{
bool success = false;
const char* key = PyUnicode_AsUTF8(attr);
FAIL_IF_NULL(key);
if (strncmp(key, "__", 2) == 0) {
// Avoid creating reference loops between Python and JavaScript with js
// modules. Such reference loops make it hard to avoid leaking memory.
if (strcmp(key, "__loader__") == 0 || strcmp(key, "__name__") == 0 ||
strcmp(key, "__package__") == 0 || strcmp(key, "__path__") == 0 ||
strcmp(key, "__spec__") == 0) {
return PyObject_GenericSetAttr(self, attr, pyvalue);
}
}
if (pyvalue == NULL) {
FAIL_IF_MINUS_ONE(JsProxy_DelAttr_js(JsProxy_VAL(self), key));
} else {
JsVal jsvalue = python2js(pyvalue);
FAIL_IF_MINUS_ONE(JsProxy_SetAttr_js(JsProxy_VAL(self), key, jsvalue));
}
success = true;
finally:
return success ? 0 : -1;
}
static PyObject*
JsProxy_RichCompare(PyObject* a, PyObject* b, int op)
{
if (!JsProxy_Check(b)) {
switch (op) {
case Py_EQ:
Py_RETURN_FALSE;
case Py_NE:
Py_RETURN_TRUE;
default:
return Py_NotImplemented;
}
}
int result;
JsVal jsa = JsProxy_VAL(a);
JsVal jsb = JsProxy_VAL(b);
switch (op) {
case Py_LT:
result = Jsv_less_than(jsa, jsb);
break;
case Py_LE:
result = Jsv_less_than_equal(jsa, jsb);
break;
case Py_EQ:
result = Jsv_equal(jsa, jsb);
break;
case Py_NE:
result = Jsv_not_equal(jsa, jsb);
break;
case Py_GT:
result = Jsv_greater_than(jsa, jsb);
break;
case Py_GE:
result = Jsv_greater_than_equal(jsa, jsb);
break;
}
if (result) {
Py_RETURN_TRUE;
} else {
Py_RETURN_FALSE;
}
}
EM_JS_VAL(JsVal, JsProxy_GetIter_js, (JsVal obj), {
return obj[Symbol.iterator]();
});
/**
* iter overload. Present if IS_ITERABLE but not IS_ITERATOR (if the IS_ITERATOR
* flag is present we use PyObject_SelfIter). Does `obj[Symbol.iterator]()`.
*/
static PyObject*
JsProxy_GetIter(PyObject* self)
{
JsVal iter = JsProxy_GetIter_js(JsProxy_VAL(self));
FAIL_IF_JS_ERROR(iter);
return js2python_objmap(iter, JsProxy_get_objmap_flags(self));
finally:
return NULL;
}
// clang-format off
EM_JS_NUM(
JsVal,
handle_next_result_js,
(JsVal res, int* done, char** msg),
{
let errmsg;
if(typeof res !== "object") {
errmsg = `Result should have type "object" not "${typeof res}"`;
} else if(typeof res.done === "undefined") {
if (typeof res.then === "function") {
errmsg = `Result was a promise, use anext() / asend() / athrow() instead.`;
} else {
errmsg = `Result has no "done" field.`;
}
}
if (errmsg) {
DEREF_U32(msg, 0) = stringToNewUTF8(errmsg);
DEREF_U32(done, 0) = -1;
}
DEREF_U32(done, 0) = res.done;
return res.value;
});
PySendResult
handle_next_result(JsVal next_res, PyObject** result, int objmap_flags){
PySendResult res = PYGEN_ERROR;
char* msg = NULL;
*result = NULL;
int done;
JsVal jsresult = handle_next_result_js(next_res, &done, &msg);
// done:
// 1 ==> finished
// 0 ==> not finished
// -1 ==> error (if msg is set, we set the error flag to a TypeError with
// msg otherwise the error flag must already be set)
if (msg) {
PyErr_SetString(PyExc_TypeError, msg);
free(msg);
FAIL();
}
FAIL_IF_MINUS_ONE(done);
// If there was no "value", "idresult" will be jsundefined
// so pyvalue will be set to Py_None.
*result = js2python_immutable(jsresult);
if (!*result) {
*result = JsProxy_create_objmap(jsresult, objmap_flags);
}
FAIL_IF_NULL(*result);
if(pyproxy_Check(jsresult)) {
Js_static_string(msg, "This borrowed proxy was automatically destroyed at the end"
" of a generator");
destroy_proxy(jsresult, &msg);
}
res = done ? PYGEN_RETURN : PYGEN_NEXT;
finally:
return res;
}
// clang-format on
PySendResult
JsProxy_am_send(PyObject* self, PyObject* arg, PyObject** result)
{
*result = NULL;
PySendResult ret = PYGEN_ERROR;
JsVal proxies = JsvArray_New();
JsVal jsarg = Jsv_undefined;
if (arg) {
jsarg = python2js_track_proxies(arg, proxies, true);
FAIL_IF_JS_ERROR(jsarg);
}
JsVal next_res =
JsvObject_CallMethodId_OneArg(JsProxy_VAL(self), &JsId_next, jsarg);
FAIL_IF_JS_ERROR(next_res);
ret = handle_next_result(next_res, result, JsProxy_get_objmap_flags(self));
finally:
if (arg) {
destroy_proxies(proxies, &PYPROXY_DESTROYED_AT_END_OF_FUNCTION_CALL);
}
return ret;
}
PyObject*
JsProxy_IterNext(PyObject* self)
{
PyObject* result;
if (JsProxy_am_send(self, NULL, &result) == PYGEN_RETURN) {
// The Python docs for tp_iternext say "When the iterator is exhausted, it
// must return NULL; a StopIteration exception may or may not be set."
// So if the result is None, we can just leave error flag unset.
if (!Py_IsNone(result)) {
_PyGen_SetStopIterationValue(result);
}
Py_CLEAR(result);
}
return result;
}
PyObject*
JsGenerator_send(PyObject* self, PyObject* arg)
{
PyObject* result;
if (JsProxy_am_send(self, arg, &result) == PYGEN_RETURN) {
if (Py_IsNone(result)) {
PyErr_SetNone(PyExc_StopIteration);
} else {
_PyGen_SetStopIterationValue(result);
}
Py_CLEAR(result);
}
return result;
}
static PyMethodDef JsGenerator_send_MethodDef = {
"send",
(PyCFunction)JsGenerator_send,
METH_O,
};
static PyObject* JsException;
static PyObject*
JsException_reduce(PyObject* self, PyObject* Py_UNUSED(ignored))
{
// Record name, message, and stack.
// See _core_docs.JsException._new_exc where the unpickling will happen.
PyObject* res = NULL;
PyObject* args = NULL;
PyObject* name = NULL;
PyObject* message = NULL;
PyObject* stack = NULL;
name = PyObject_GetAttrString(self, "name");
FAIL_IF_NULL(name);
message = PyObject_GetAttrString(self, "message");
FAIL_IF_NULL(message);
stack = PyObject_GetAttrString(self, "stack");
FAIL_IF_NULL(stack);
args = PyTuple_Pack(3, name, message, stack);
FAIL_IF_NULL(args);
PyObject* dict = JsProxy_DICT(self);
if (dict) {
res = PyTuple_Pack(3, Py_TYPE(self), args, dict);
} else {
res = PyTuple_Pack(2, Py_TYPE(self), args);
}
finally:
Py_CLEAR(args);
Py_CLEAR(name);
Py_CLEAR(message);
Py_CLEAR(stack);
return res;
}
static PyMethodDef JsException_reduce_MethodDef = {
"__reduce__",
(PyCFunction)JsException_reduce,
METH_NOARGS
};
PyObject*
JsException_js_error_getter(PyObject* self, void* closure)
{
Py_INCREF(self);
return self;
}
// clang-format off
EM_JS_VAL(JsVal,
JsException_new_helper,
(char* name_ptr, char* message_ptr, char* stack_ptr),
{
let name = UTF8ToString(name_ptr);
let message = UTF8ToString(message_ptr);
let stack = UTF8ToString(stack_ptr);
return API.deserializeError(name, message, stack);
});
// clang-format on
// We use this to unpickle JsException objects.
static PyObject*
JsException_new(PyTypeObject* subtype, PyObject* args, PyObject* kwds)
{
static char* kwlist[] = { "name", "message", "stack", 0 };
char* name;
char* message = "";
char* stack = "";
if (!PyArg_ParseTupleAndKeywords(
args, kwds, "s|ss:__new__", kwlist, &name, &message, &stack)) {
return NULL;
}
JsVal result = JsException_new_helper(name, message, stack);
FAIL_IF_JS_ERROR(result);
return js2python(result);
finally:
return NULL;
}
static int
JsException_init(PyBaseExceptionObject* self, PyObject* args, PyObject* kwds)
{
return 0;
}
/**
* Shared logic between throw and async throw.
*
* Possibly "typ" is an exception instance and val and tb are null. Otherwise,
* it's an old style call "typ" should be an exception type, "val" an instance,
* and tb an optional traceback. Figure out which is the case and get an
* exception object.
*
* Then if the exception object is PyExc_GeneratorExit, call jsobj.return().
* Otherwise, convert it to js and call jsobj.throw(jsexc). Return the result of
* whichever of these two calls we make (or set the error flag and return NULL
* if something goes wrong).
*/
JsVal
process_throw_args(PyObject* self, PyObject* typ, PyObject* val, PyObject* tb)
{
if (Py_IsNone(tb)) {
tb = NULL;
} else if (tb != NULL && !PyTraceBack_Check(tb)) {
PyErr_SetString(PyExc_TypeError,
"throw() third argument must be a traceback object");
return JS_ERROR;
}
Py_INCREF(typ);
Py_XINCREF(val);
Py_XINCREF(tb);
if (PyExceptionClass_Check(typ)) {
PyErr_NormalizeException(&typ, &val, &tb);
if (tb != NULL) {
PyException_SetTraceback(val, tb);
}
} else if (PyExceptionInstance_Check(typ)) {
/* Raising an instance. The value should be a dummy. */
if (val && !Py_IsNone(val)) {
PyErr_SetString(PyExc_TypeError,
"instance exception may not have a separate value");
goto failed_throw;
} else {
/* Normalize to raise <class>, <instance> */