Skip to content
Navigation Menu
{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathjavascriptcore.pyx
More file actions
1316 lines (1049 loc) · 44.7 KB
/
Copy pathjavascriptcore.pyx
File metadata and controls
1316 lines (1049 loc) · 44.7 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
# This file is part of PyJavaScriptCore, a binding between CPython and
# WebKit's JavaScriptCore.
#
# Copyright (C) 2009, Martin Soto <soto@freedesktop.org>
# Copyright (C) 2009, john paul janecek (see README file)
#
# PyJavaScriptCore is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public License
# as published by the Free Software Foundation; either version 2 of
# the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
"""
Two-way binding between CPython and WebKit's JavaScriptCore.
"""
import sys
import types
import collections
import weakref
cdef:
ctypedef unsigned short bool
include "stdlib.pyi"
include "python.pyi"
include "jsbase.pyi"
include "jscontextref.pyi"
include "jsstringref.pyi"
include "jsvalueref.pyi"
include "jsobjectref.pyi"
#
# Null Singleton
#
class NullType(object):
"""A singleton type to represent JavaScript's `null` value in
Python."""
def __init__(self):
# Make this a singleton class.
if Null is not None:
raise TypeError("cannot create '%s' instances"
% self.__class__.__name__)
def __nonzero__(self):
# As in javascript, the Null value has a false boolean value.
return False
# Create the actual Null singleton.
Null = None
Null = NullType()
#
# Value conversion
#
# Wrapper cache. All attempts at wrapping a single object should
# return the same wrapper. This way, there is a one-to-one
# relationship between wrappers and their wrapped objects. The
# following dictionaries maintain this relationship:
# A dictionary associating wrapped JavaScript objects to their Python
# wrappers. Keys are pointers to JavaScript objects cast to long int
# (and converted to Python integers to store them in the
# dictionary). Values are (weak references to) the corresponding
# wrappers. Notice that casting to the long type is necessary because
# pointers are larger than plain ints in 64-bit platforms.
cdef object _pyWrappedJSObjs = weakref.WeakValueDictionary()
# A dictionary associating wrapped Python objects to their JavaScript
# wrappers. Keys are ids of Python objects, as returned by the id()
# function. Values are pointers to the corresponding JavaScript
# wrappers enclosed in PyCObject instances. Wrappers are deleted from
# this dictionary when they get garbage collected (see pyObjFinalize).
cdef object _pyWrappedPyObjs = {}
cdef object wrapJSObject(JSContextRef jsCtx, JSValueRef jsValue):
cdef object wrapper
try:
return _pyWrappedJSObjs[<long>jsValue]
except KeyError:
pass
if JSObjectIsFunction(jsCtx, jsValue):
wrapper = makeJSFunction(jsCtx, jsValue)
else:
wrapper = makeJSObject(jsCtx, jsValue)
_pyWrappedJSObjs[<long>jsValue] = wrapper
return wrapper
cdef object jsToPython(JSContextRef jsCtx, JSValueRef jsValue):
"""Convert a JavaScript value into a Python value."""
cdef int jsType = JSValueGetType(jsCtx, jsValue)
cdef JSStringRef jsStr
cdef double doubleVal
cdef long intVal
if jsType == kJSTypeUndefined:
return None
if jsType == kJSTypeNull:
return Null
elif jsType == kJSTypeBoolean:
return types.BooleanType(JSValueToBoolean(jsCtx, jsValue))
elif jsType == kJSTypeNumber:
# If the value is actually an integer, return it is an
# instance of Python's int type.
doubleVal = JSValueToNumber(jsCtx, jsValue, NULL)
intVal = <long>doubleVal
if intVal == doubleVal:
return intVal
else:
return doubleVal
elif jsType == kJSTypeString:
jsStr = JSValueToStringCopy(jsCtx, jsValue, NULL)
try:
return PyUnicode_DecodeUTF16(<Py_UNICODE*>JSStringGetCharactersPtr(jsStr),
JSStringGetLength(jsStr) * 2,
NULL, 0)
finally:
JSStringRelease(jsStr)
elif JSValueIsObjectOfClass(jsCtx, jsValue, pyObjectClass):
# This is a wrapped Python object. Just unwrap it.
return <object>JSObjectGetPrivate(jsValue)
else:
return wrapJSObject(jsCtx, jsValue)
return None
class JSException(Exception):
"""Python exception class to encapsulate JavaScript exceptions."""
def __init__(self, pyWrapped):
"""Create a JavaScript exception object.#
The parameter is the original exception object thrown by the
JavaScript code, wrapped as a Python object."""
self.pyWrapped = pyWrapped
try:
self.name = pyWrapped.name
except AttributeError:
self.name = '<Unknown error>'
try:
self.message = pyWrapped.message
except AttributeError:
self.message = '<no message>'
def __str__(self):
return self.message
cdef object jsExceptionToPython(JSContextRef jsCtx, JSValueRef jsException):
"""Factory function for creating exception objects."""
return JSException(jsToPython(jsCtx, jsException))
cdef object pyStringFromJS(JSStringRef jsString):
return PyUnicode_DecodeUTF16(<Py_UNICODE *>JSStringGetCharactersPtr(jsString),
JSStringGetLength(jsString) * 2,
NULL, 0)
cdef JSStringRef createJSStringFromPython(object pyStr):
"""Create a ``JSString`` from a Python object.
This is a create function. Ownership of the result is transferred
to the caller."""
pyStr = unicode(pyStr).encode('utf-8')
return JSStringCreateWithUTF8CString(pyStr)
cdef JSObjectRef wrapPyObject(JSContextRef jsCtx, object pyValue):
cdef JSObjectRef wrapper
try:
return <JSObjectRef>PyCObject_AsVoidPtr(_pyWrappedPyObjs[id(pyValue)])
except KeyError:
pass
wrapper = makePyObject(jsCtx, pyValue)
_pyWrappedPyObjs[id(pyValue)] = PyCObject_FromVoidPtr(wrapper, NULL)
return wrapper
cdef JSValueRef pythonToJS(JSContextRef jsCtx, object pyValue):
"""Convert a Python value into a JavaScript value.
The returned value belongs to the specified context, and must be
protected if it is going to be permanently stored (e.g., inside an
object)."""
if isinstance(pyValue, types.NoneType):
return JSValueMakeUndefined(jsCtx)
elif isinstance(pyValue, NullType):
return JSValueMakeNull(jsCtx)
elif isinstance(pyValue, types.BooleanType):
return JSValueMakeBoolean(jsCtx, pyValue)
elif isinstance(pyValue, (types.IntType, types.FloatType)):
return JSValueMakeNumber(jsCtx, pyValue)
elif isinstance(pyValue, types.StringTypes):
return JSValueMakeString(jsCtx, createJSStringFromPython(pyValue))
elif isinstance(pyValue, _JSBaseObject):
# This is a wrapped JavaScript object, just unwrap it.
return (<_JSObject>pyValue).jsObject
else:
# Wrap all other Python objects into a generic wrapper.
return wrapPyObject(jsCtx, pyValue)
#
# Python Wrappers for JavaScript objects
#
# The name of the length array property.
cdef JSStringRef jsLengthName = JSStringCreateWithUTF8CString("length")
cdef class _JSBaseObject:
"""Base class for all Python wrappers for JavaScript objects.
This class only manages object allocation and deallocation.
"""
# Make it possible to have weak references to this object.
cdef object __weakref__
cdef JSContextRef jsCtx
cdef JSObjectRef jsObject
def __init__(self):
self.jsCtx = NULL
self.jsObject = NULL
cdef setup(self, JSContextRef jsCtx, JSObjectRef jsObject):
# We claim ownership of objects here and release them in
# __dealloc__. Notice that we also need to own a reference to
# the context, because it may otherwise disappear while this
# object still exists.
self.jsCtx = jsCtx
JSGlobalContextRetain(self.jsCtx)
self.jsObject = jsObject
JSValueProtect(self.jsCtx, self.jsObject)
def __dealloc__(self):
JSValueUnprotect(self.jsCtx, self.jsObject)
JSGlobalContextRelease(self.jsCtx)
cdef class _JSSequence(_JSBaseObject):
"""A Python sequence view on a JavaScript object.
See the ``asSeq`` function in this module and the
``_JSObject.__asSeq__`` method for more details.
Since Cython extension classes cannot inherit from Python classes,
we first define class ``_JSSequence`` and then define
``JSSequence`` as a standard Python class that mixes in
``MutableMapping`` from the ``collections`` module."""
cdef int getLength(self) except -1:
cdef JSValueRef jsException = NULL
cdef JSValueRef jsResult
cdef int result
jsResult = JSObjectGetProperty(self.jsCtx, self.jsObject,
jsLengthName, &jsException)
if jsException != NULL or JSValueIsUndefined(self.jsCtx, jsResult):
raise TypeError, "not an array or array-like JavaScript object"
result = <int>JSValueToNumber(self.jsCtx, jsResult, &jsException)
if jsException != NULL:
raise TypeError, "not an array or array-like JavaScript object"
return result
cdef int setLength(self, int length) except -1:
cdef JSValueRef jsException = NULL
cdef JSValueRef jsLength = JSValueMakeNumber(self.jsCtx, length)
JSObjectSetProperty(self.jsCtx, self.jsObject, jsLengthName,
jsLength, kJSPropertyAttributeNone, &jsException)
if jsException != NULL:
raise TypeError, "not a mutable array or array-like " \
"JavaScript object"
return 0
cdef JSValueRef getItem(self, int index) except NULL:
cdef JSValueRef jsException = NULL
cdef JSValueRef jsResult
jsResult = JSObjectGetPropertyAtIndex(self.jsCtx, self.jsObject,
index, &jsException)
if jsException != NULL:
raise jsExceptionToPython(self.jsCtx, jsException)
return jsResult
cdef int setItem(self, int index, JSValueRef jsValue) except -1:
cdef JSValueRef jsException = NULL
JSObjectSetPropertyAtIndex(self.jsCtx, self.jsObject, index, jsValue,
&jsException)
if jsException != NULL:
raise jsExceptionToPython(self.jsCtx, jsException)
return 0
cdef int copyBlock(self, int start, int end, int dest) except -1:
"""Copy the elements of ``[start, end]`` to position ``dest``.
Both ``start`` and ``end`` must be non-negative and ``start
must be lower than ``end``."""
cdef int count = end - start
cdef int i
if start < dest:
for i in range(count - 1, -1, -1):
self.setItem(dest + i, self.getItem(start + i))
elif start > dest:
for i in range(count):
self.setItem(dest + i, self.getItem(start + i))
return 0
def __contains__(self, pyItem):
cdef JSValueRef jsItem = pythonToJS(self.jsCtx, pyItem)
cdef JSValueRef jsElem
for i in range(self.getLength()):
jsElem = self.getItem(i)
if JSValueIsObjectOfClass(self.jsCtx, jsElem, pyObjectClass):
# This is a wrapped Python object, compare according
# to Python rules.
if <object>JSObjectGetPrivate(jsElem) == pyItem:
return True
else:
# Compare according to JavaScript rules.
if JSValueIsStrictEqual(self.jsCtx, jsItem, jsElem):
return True
return False
def __len__(self):
return self.getLength()
def __iter__(self):
return _JSSeqIterator(self)
def __getitem__(self, pyIndex):
cdef int index
cdef int length = self.getLength()
cdef int i
if isinstance(pyIndex, int) or isinstance(pyIndex, long):
index = pyIndex
# Handle negative indexes.
if index < 0:
index += length
# Exclude out-of-range indexes.
if index < 0 or index >= length:
raise IndexError, "list index out of range"
return jsToPython(self.jsCtx, self.getItem(index))
elif isinstance(pyIndex, slice):
# Don't know how efficient this is, but it looks cool
# anyway.
return [jsToPython(self.jsCtx, self.getItem(i))
for i in xrange(*pyIndex.indices(length))]
else:
raise TypeError, "list indices must be integers, not %s" % \
pyIndex.__class__.__name__
def __setitem__(self, pyIndex, pyValue):
cdef int index
cdef int length = self.getLength()
cdef int start, end, step
cdef int valueLength
cdef int sliceSize
cdef int i
if isinstance(pyIndex, int) or isinstance(pyIndex, long):
index = pyIndex
# Handle negative indexes.
if index < 0:
index += length
# Exclude out-of-range indexes.
if index < 0 or index >= length:
raise IndexError, "list index out of range"
self.setItem(index, pythonToJS(self.jsCtx, pyValue))
elif isinstance(pyIndex, slice):
start, end, step = pyIndex.indices(length)
pyValueList = list(pyValue)
valueLength = len(pyValueList)
if step == 1:
# Move the elements after the slice to their final
# position.
self.copyBlock(end, length, start + valueLength)
if end - start > valueLength:
# Truncate the list to its new length.
self.setLength(length - (end - start) + valueLength)
else:
# Calculate the size of the extended slice.
sliceSize = (end - start) / step
if (end - start) % step > 0:
sliceSize += 1
if sliceSize != valueLength:
raise ValueError, "attempt to assign sequence of size" \
" %d to extended slice of size %d" % \
(valueLength, sliceSize)
# Copy the elements to their destination.
i = start
for pyElem in pyValueList:
self.setItem(i, pythonToJS(self.jsCtx, pyElem))
i += step
else:
raise TypeError, "list indices must be integers, not %s" % \
pyIndex.__class__.__name__
def __delitem__(self, pyIndex):
cdef int index
cdef int length = self.getLength()
cdef int start, end, step
cdef int frm, dest, nextDel
if isinstance(pyIndex, int) or isinstance(pyIndex, long):
index = pyIndex
# Handle negative indexes.
if index < 0:
index += length
# Exclude out-of-range indexes.
if index < 0 or index >= length:
raise IndexError, "list index out of range"
self.copyBlock(index + 1, length, index)
self.setLength(length - 1)
elif isinstance(pyIndex, slice):
start, end, step = pyIndex.indices(length)
if step == 1:
# Move the elements after the slice to their final
# position.
self.copyBlock(end, length, start)
self.setLength(length - (end - start))
else:
# Copy the elements to their final positions. Elements
# are copied from frm to dest. nextDel marks the
# position of the next element that must be deleted
# (-1 if no more elements have to be deleted).
nextDel = start
if nextDel >= end:
nextDel = -1
dest = start
for frm in range(start, length):
if frm == nextDel:
nextDel += step
if nextDel >= end:
nextDel = -1
else:
self.setItem(dest, self.getItem(frm))
dest += 1
# Truncate the list.
self.setLength(dest)
else:
raise TypeError, "list indices must be integers, not %s" % \
pyIndex.__class__.__name__
def insert(self, pyIndex, pyValue):
cdef int index
cdef int length = self.getLength()
if not isinstance(pyIndex, int) and not isinstance(pyIndex, long):
raise TypeError, "list indices must be integers, not %s" % \
pyIndex.__class__.__name__
index = pyIndex
# The insert method is special in how indexes are handled:
# Handle negative indexes.
if index < 0:
index += length
# Handle out-of-range indexes.
if index < 0:
index = 0
elif index > length:
index = length
self.copyBlock(index, length, index + 1)
self.setItem(index, pythonToJS(self.jsCtx, pyValue))
class JSSequence(_JSSequence, collections.MutableSequence):
"""Mix ``_JSSequence`` and ``collections.MutableSequence``."""
__slots__ = ()
cdef class _JSSeqIterator:
"""Iterator class for JavaScript array-like objects."""
cdef _JSSequence pySeq
cdef int index
def __init__(self, pySeq):
self.pySeq = pySeq
self.index = 0
def __iter__(self):
return self
def __next__(self):
if self.index < self.pySeq.getLength():
value = self.pySeq[self.index]
self.index += 1
return value
else:
raise StopIteration
def next(self):
"""Wrap the ``__next__`` method for backwards compatibility.
"""
return self.__next__()
cdef class _JSObject(_JSBaseObject):
"""Wrapper class to make JavaScript objects accessible from
Python.
Since JavaScript objects can be interchangeably used as objects
and as associative data structures, wrapped JavaScript objects
always implement Python's mapping protocol. For those JavaScript
objects that behave like sequences, the ``asSeq`` function (or
``__asSeq__`` method ) can be invoked to obtain a view of the
object that behaves as a Python sequence.
Also, since Cython extension classes cannot inherit from Python
classes, we first define class ``_JSObject`` and then define
``JSObject`` as a standard Python class that mixes in
``MutableSequence`` from the ``collections`` module."""
# Sequence view of this object.
cdef _JSSequence seqView
def __init__(self):
_JSBaseObject.__init__(self)
self.seqView = None
def __getattr__(self, pyName):
cdef JSStringRef jsName
cdef JSValueRef jsException = NULL
cdef JSValueRef jsResult
jsName = createJSStringFromPython(pyName)
try:
jsResult = JSObjectGetProperty(self.jsCtx, self.jsObject,
jsName, &jsException)
if jsException != NULL:
raise jsExceptionToPython(self.jsCtx, jsException)
if JSValueIsUndefined(self.jsCtx, jsResult):
# This may be a property with an undefined value, or
# no property at all.
if JSObjectHasProperty(self.jsCtx, self.jsObject, jsName):
return jsToPython(self.jsCtx, jsResult)
else:
# For inexisting properties, we use Python
# behavior.
raise AttributeError, \
"JavaScript object has no property '%s'" % pyName
elif not JSValueIsObjectOfClass(self.jsCtx, jsResult,
pyObjectClass) and \
JSValueIsObject(self.jsCtx, jsResult) and \
JSObjectIsFunction(self.jsCtx, jsResult):
# This is a native JavaScript function, we mimic
# Python's behavior and return it bound to this
# object.
return makeJSBoundMethod(self.jsCtx, jsResult,
self.jsObject)
else:
return jsToPython(self.jsCtx, jsResult)
finally:
JSStringRelease(jsName)
def __setattr__(self, pyName, pyValue):
cdef JSStringRef jsName
cdef JSValueRef jsException = NULL
jsName = createJSStringFromPython(pyName)
try:
JSObjectSetProperty(self.jsCtx, self.jsObject, jsName,
pythonToJS(self.jsCtx, pyValue),
kJSPropertyAttributeNone, &jsException)
if jsException != NULL:
raise jsExceptionToPython(self.jsCtx, jsException)
finally:
JSStringRelease(jsName)
def __delattr__(self, pyName):
cdef JSStringRef jsName
cdef JSValueRef jsException = NULL
jsName = createJSStringFromPython(pyName)
try:
if not JSObjectHasProperty(self.jsCtx, self.jsObject, jsName):
# Use Python behavior for inexisting properties.
raise AttributeError, \
"JavaScript object has no property '%s'" % pyName
if not JSObjectDeleteProperty(self.jsCtx, self.jsObject,
jsName, &jsException):
raise AttributeError, \
"property '%s' of JavaScript object cannot " \
"be deleted" % pyName
if jsException != NULL:
raise jsExceptionToPython(self.jsCtx, jsException)
finally:
JSStringRelease(jsName)
def __asSeq__(self):
"""Return the sequence view of this object.
Since it is impossible to reliably distinguish between
JavaScript arrays and array-like objects and other types of
objects, we cannot decide automatically when a Python wrapper
should behave as a sequence. This method retrieves a view of
the current object (a proxy object) that implements the
mutable sequence protocol.
Some of the operations depend in the view depend on the
presence of a 'length' property in the original object, and
will fail with a ``TypeError`` when it isn't present.
This method should normally be called through the ``asSeq``
function in this module.
"""
if self.seqView is None:
self.seqView = JSSequence()
self.seqView.setup(self.jsCtx, self.jsObject)
return self.seqView
#
# Methods implementing the mutable mapping protocol
#
def __len__(self):
cdef JSPropertyNameArrayRef nameArray
nameArray = JSObjectCopyPropertyNames(self.jsCtx, self.jsObject)
try:
return JSPropertyNameArrayGetCount(nameArray)
finally:
JSPropertyNameArrayRelease(nameArray)
def __iter__(self):
return _JSObjectIterator(self)
def __contains__(self, pyKey):
cdef JSStringRef jsKey
jsKey = createJSStringFromPython(pyKey)
try:
return JSObjectHasProperty(self.jsCtx, self.jsObject,
jsKey) != 0
finally:
JSStringRelease(jsKey)
def __getitem__(self, pyKey):
cdef JSStringRef jsKey
cdef JSValueRef jsException = NULL
cdef JSValueRef jsResult
jsKey = createJSStringFromPython(pyKey)
try:
jsResult = JSObjectGetProperty(self.jsCtx, self.jsObject,
jsKey, &jsException)
if jsException != NULL:
raise jsExceptionToPython(self.jsCtx, jsException)
if JSValueIsUndefined(self.jsCtx, jsResult):
# This may be a property with an undefined value, or
# no property at all.
if JSObjectHasProperty(self.jsCtx, self.jsObject, jsKey):
return jsToPython(self.jsCtx, jsResult)
else:
# For inexisting properties, we use Python
# behavior.
raise KeyError, \
"JavaScript object has no property '%s'" % pyKey
else:
return jsToPython(self.jsCtx, jsResult)
finally:
JSStringRelease(jsKey)
def __setitem__(self, pyKey, pyValue):
cdef JSStringRef jsKey
cdef JSValueRef jsException = NULL
jsKey = createJSStringFromPython(pyKey)
try:
JSObjectSetProperty(self.jsCtx, self.jsObject, jsKey,
pythonToJS(self.jsCtx, pyValue),
kJSPropertyAttributeNone, &jsException)
if jsException != NULL:
raise jsExceptionToPython(self.jsCtx, jsException)
finally:
JSStringRelease(jsKey)
def __delitem__(self, pyKey):
cdef JSStringRef jsKey
cdef JSValueRef jsException = NULL
jsKey = createJSStringFromPython(pyKey)
try:
if not JSObjectHasProperty(self.jsCtx, self.jsObject, jsKey):
# Use Python behavior for inexisting properties.
raise KeyError, \
"JavaScript object has no property '%s'" % pyKey
if not JSObjectDeleteProperty(self.jsCtx, self.jsObject,
jsKey, &jsException):
raise KeyError, \
"property '%s' of JavaScript object cannot " \
"be deleted" % pyKey
if jsException != NULL:
raise jsExceptionToPython(self.jsCtx, jsException)
finally:
JSStringRelease(jsKey)
class JSObject(_JSObject, collections.MutableMapping):
"""Mix ``_JSObject`` and ``collections.MutableMapping``."""
__slots__ = ()
cdef class _JSObjectIterator:
"""Iterator class for JavaScript objects.
This provides mapping-style iteration on JavaScript objects."""
cdef JSPropertyNameArrayRef nameArray
cdef int index
def __init__(self, _JSObject pyObject):
self.nameArray = JSObjectCopyPropertyNames(pyObject.jsCtx,
pyObject.jsObject)
self.index = 0
def __iter__(self):
return self
def __next__(self):
if self.index < JSPropertyNameArrayGetCount(self.nameArray):
pyPropName = pyStringFromJS(JSPropertyNameArrayGetNameAtIndex(
self.nameArray, self.index))
self.index += 1
return pyPropName
else:
raise StopIteration
def next(self):
"""Wrap the ``__next__`` method for backwards compatibility.
"""
return self.__next__()
def __dealloc__(self):
JSPropertyNameArrayRelease(self.nameArray)
def asSeq(pyObject):
"""Return the sequence view of ``pyObject``."""
return pyObject.__asSeq__()
cdef makeJSObject(JSContextRef jsCtx, JSObjectRef jsObject):
"""Factory function for 'JSObject' instances."""
cdef _JSObject obj = JSObject()
obj.setup(jsCtx, jsObject)
return obj
cdef class _JSFunction(_JSObject):
"""Specialized wrapper class to make JavaScript functions callable
from Python.
This class gets mixed with ``collections.MutableMapping in the
``JSFunction`` class."""
def __call__(self, *args):
cdef JSValueRef *jsArgs
cdef JSValueRef result
cdef JSObjectRef jsThisObject
cdef JSValueRef jsError = NULL
if len(args):
jsArgs = <JSValueRef *>malloc(len(args) * sizeof(JSValueRef))
else:
jsArgs = NULL
for i, arg in enumerate(args):
jsArgs[i] = pythonToJS(self.jsCtx, arg)
result = JSObjectCallAsFunction(self.jsCtx, self.jsObject,
NULL, len(args), jsArgs,
&jsError)
free(jsArgs)
if jsError != NULL:
raise jsExceptionToPython(self.jsCtx, jsError)
return jsToPython(self.jsCtx, result)
class JSFunction(_JSFunction, collections.MutableMapping):
"""Mix ``_JSFunction`` and ``collections.MutableMapping``."""
__slots__ = ()
cdef makeJSFunction(JSContextRef jsCtx, JSObjectRef jsObject):
"""Factory function for 'JSFunction' instances."""
cdef _JSFunction obj = JSFunction()
obj.setup(jsCtx, jsObject)
return obj
cdef class _JSBoundMethod(_JSObject):
"""A JavaScript bound method.
Instances of this class operate in a similar way to Python bound
methods, but they encapsulate a JavaScript object and
function. When they are called, the function is called with the
object as 'this' object.
This class gets mixed with ``collections.MutableMapping in the
``JSBoundMethod`` class."""
cdef JSObjectRef jsThisObj
cdef setup2(self, JSContextRef jsCtx, JSObjectRef jsObject,
JSObjectRef jsThisObj):
_JSObject.setup(self, jsCtx, jsObject)
# __dealloc__ unprotects jsThisObj, so that it's guaranteed to
# exist as long as this object exists.
JSValueProtect(jsCtx, jsThisObj)
self.jsThisObj = jsThisObj
def __call__(self, *args):
cdef JSValueRef *jsArgs
cdef JSValueRef result
cdef JSValueRef jsError = NULL
if len(args):
jsArgs = <JSValueRef *>malloc(len(args) * sizeof(JSValueRef))
else:
jsArgs = NULL
for i, arg in enumerate(args):
jsArgs[i] = pythonToJS(self.jsCtx, arg)
result = JSObjectCallAsFunction(self.jsCtx, self.jsObject,
self.jsThisObj, len(args), jsArgs,
&jsError)
free(jsArgs)
if jsError != NULL:
raise jsExceptionToPython(self.jsCtx, jsError)
return jsToPython(self.jsCtx, result)
def __dealloc__(self):
JSValueUnprotect(self.jsCtx, self.jsThisObj)
class JSBoundMethod(_JSBoundMethod, collections.MutableMapping):
"""Mix ``_JSBoundMethod`` and ``collections.MutableMapping``."""
__slots__ = ()
cdef makeJSBoundMethod(JSContextRef jsCtx, JSObjectRef jsObject,
JSObjectRef thisObj):
"""Factory function for 'JSBoundMethod' instances."""
cdef _JSBoundMethod obj = JSBoundMethod()
obj.setup2(jsCtx, jsObject, thisObj)
return obj
cdef class JSContext:
"""Wrapper class for JavaScriptCore context objects.
Call the constructor without arguments to obtain a new default
context that can be used to execute JavaScript but that will not
provide any access to a DOM or any other browser-specific objects.
A context obtained from another object (e.g. a WebKit browser
component can also be passed to the constructor in order to gain
full access to it from Python.
"""
cdef JSContextRef jsCtx
cdef object pyCtxExtern
def __cinit__(self, pyCtxExtern=None):
if pyCtxExtern is None:
# Create a new context.
self.jsCtx = JSGlobalContextCreate(NULL)
self.pyCtxExtern = None
else:
# Extract the actual context object.
self.jsCtx = <JSContextRef>PyCObject_AsVoidPtr(pyCtxExtern)
JSGlobalContextRetain(self.jsCtx)
self.pyCtxExtern = pyCtxExtern
def __init__(self, pyCtxExtern=None):
pass
property globalObject:
"""Global object for this context."""
def __get__(self):
return jsToPython(self.jsCtx,
JSContextGetGlobalObject(self.jsCtx))
def evaluateScript(self, script, thisObject=None, sourceURL=None,
startingLineNumber=1):
cdef JSValueRef jsException = NULL
cdef JSValueRef jsValue
cdef JSStringRef jsScript = createJSStringFromPython(script)
try:
jsValue = JSEvaluateScript(self.jsCtx, jsScript,
<JSObjectRef>NULL,
<JSStringRef>NULL,
startingLineNumber,
&jsException)
if jsException != NULL:
raise jsExceptionToPython(self.jsCtx, jsException)
finally:
JSStringRelease(jsScript)
return jsToPython(self.jsCtx, jsValue)
def getCtx(self):
return self.pyCtxExtern
def __dealloc__(self):
JSGlobalContextRelease(self.jsCtx)
#
# JavaScript Wrappers for Python Objects
#
# Helper functions:
cdef JSValueRef pyExceptionToJS(JSContextRef jsCtx, object exc):
"""Make a JavaScript exception object from a Python exception
object."""
cdef JSStringRef jsMsgStr
cdef JSValueRef jsMsg
# Make a string from the exception object (the unicode conversion
# in createJSStringFromPython takes care of extracting the
# message).
jsMsgStr = createJSStringFromPython(exc)
jsMsg = JSValueMakeString(jsCtx, jsMsgStr)
JSStringRelease(jsMsgStr)
return JSObjectMakeError(jsCtx, 1, &jsMsg, NULL)
# PythonObject: Generic JavaScript wrapper for Python objects.
cdef void pyObjInitialize(JSContextRef ctx,
JSObjectRef jsObj) with gil:
cdef object pyObj = <object>JSObjectGetPrivate(jsObj)
# Keep a reference to the wrapped Python object during the
# lifetime of the JavaScript wrapper. This reference is released
# in the finalize method.
Py_INCREF(pyObj)
You can’t perform that action at this time.
