Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/python-wasm
Path: blob/main/python/pylang/src/baselib/internal.py
1398 views
1
# vim:fileencoding=utf-8
2
# License: BSD
3
4
# globals: exports, console, ρσ_iterator_symbol, ρσ_kwargs_symbol, ρσ_arraylike, ρσ_list_contains, ρσ_list_constructor, ρσ_str, ρσ_int, ρσ_float
5
6
def ρσ_eslice(arr, step, start, end):
7
if jstype(arr) is 'string' or v'arr instanceof String':
8
is_string = True
9
arr = arr.split('')
10
11
if step < 0:
12
step = -step
13
arr = arr.slice().reverse()
14
if jstype(start) is not "undefined": start = arr.length - start - 1
15
if jstype(end) is not "undefined": end = arr.length - end - 1
16
if jstype(start) is "undefined": start = 0
17
if jstype(end) is "undefined": end = arr.length
18
19
arr = arr.slice(start, end).filter(def(e, i): return i % step is 0;)
20
if is_string:
21
arr = arr.join('')
22
return arr
23
24
def ρσ_delslice(arr, step, start, end):
25
if jstype(arr) is 'string' or v'arr instanceof String':
26
is_string = True
27
arr = arr.split('')
28
if step < 0:
29
if jstype(start) is "undefined": start = arr.length
30
if jstype(end) is "undefined": end = 0
31
start, end, step = end, start, -step
32
if jstype(start) is "undefined": start = 0
33
if jstype(end) is "undefined": end = arr.length
34
35
if step is 1:
36
arr.splice(start, end - start)
37
else:
38
if end > start:
39
indices = v'[]'
40
for v'var i = start; i < end; i += step':
41
indices.push(i)
42
for v'var i = indices.length - 1; i >= 0; i--':
43
arr.splice(indices[i], 1)
44
45
if is_string:
46
arr = arr.join('')
47
return arr
48
49
def ρσ_flatten(arr):
50
ans = []
51
for v'var i=0; i < arr.length; i++':
52
value = arr[i] # noqa:undef
53
if Array.isArray(value):
54
ans = ans.concat(ρσ_flatten(value))
55
else:
56
ans.push(value)
57
return ans
58
59
def ρσ_unpack_asarray(num, iterable):
60
if ρσ_arraylike(iterable):
61
return iterable
62
ans = v'[]'
63
if jstype(iterable[ρσ_iterator_symbol]) is 'function':
64
iterator = iterable.keys() if jstype(Map) is 'function' and v'iterable instanceof Map' else iterable[ρσ_iterator_symbol]()
65
result = iterator.next()
66
while not result.done and ans.length < num:
67
ans.push(result.value)
68
result = iterator.next()
69
return ans
70
71
def ρσ_extends(child, parent):
72
child.prototype = Object.create(parent.prototype)
73
child.prototype.constructor = child
74
75
ρσ_in = (def ():
76
if jstype(Map) is 'function' and jstype(Set) is 'function':
77
return def(val, arr):
78
if jstype(arr) is 'string':
79
return arr.indexOf(val) is not -1
80
if jstype(arr.__contains__) is 'function':
81
return arr.__contains__(val)
82
if v'arr instanceof Map || arr instanceof Set':
83
return arr.has(val)
84
if ρσ_arraylike(arr):
85
return ρσ_list_contains.call(arr, val)
86
return Object.prototype.hasOwnProperty.call(arr, val)
87
return def(val, arr):
88
if jstype(arr) is 'string':
89
return arr.indexOf(val) is not -1
90
if jstype(arr.__contains__) is 'function':
91
return arr.__contains__(val)
92
if ρσ_arraylike(arr):
93
return ρσ_list_contains.call(arr, val)
94
return Object.prototype.hasOwnProperty.call(arr, val)
95
)()
96
97
def ρσ_Iterable(iterable):
98
# Once ES6 is mature, change AST_ForIn to use the iterator protocol and get
99
# rid of this function entirely
100
if ρσ_arraylike(iterable):
101
return iterable
102
if jstype(iterable[ρσ_iterator_symbol]) is 'function':
103
iterator = iterable.keys() if jstype(Map) is 'function' and v'iterable instanceof Map' else iterable[ρσ_iterator_symbol]()
104
ans = []
105
result = iterator.next()
106
while not result.done:
107
ans.push(result.value)
108
result = iterator.next()
109
return ans
110
# so we can use 'for ... in' syntax with objects, as we would with dicts in python
111
return Object.keys(iterable)
112
113
ρσ_desugar_kwargs = (def ():
114
if jstype(Object.assign) is 'function':
115
return def():
116
ans = Object.create(None)
117
ans[ρσ_kwargs_symbol] = True
118
for v'var i = 0; i < arguments.length; i++':
119
Object.assign(ans, arguments[i])
120
return ans
121
return def():
122
ans = Object.create(None)
123
ans[ρσ_kwargs_symbol] = True
124
for v'var i = 0; i < arguments.length; i++':
125
keys = Object.keys(arguments[i])
126
for v'var j = 0; j < keys.length; j++':
127
ans[keys[j]] = arguments[i][keys[j]]
128
return ans
129
)()
130
131
def ρσ_interpolate_kwargs(f, supplied_args):
132
if not f.__argnames__:
133
return f.apply(this, supplied_args)
134
has_prop = Object.prototype.hasOwnProperty
135
kwobj = supplied_args.pop()
136
if f.__handles_kwarg_interpolation__:
137
args = Array(Math.max(supplied_args.length, f.__argnames__.length) + 1)
138
args[-1] = kwobj
139
for v'var i = 0; i < args.length - 1; i++':
140
if i < f.__argnames__.length:
141
prop = f.__argnames__[i]
142
if has_prop.call(kwobj, prop):
143
args[i] = kwobj[prop]
144
v'delete kwobj[prop]'
145
elif i < supplied_args.length:
146
args[i] = supplied_args[i]
147
else:
148
args[i] = supplied_args[i]
149
return f.apply(this, args)
150
151
for v'var i = 0; i < f.__argnames__.length; i++':
152
prop = f.__argnames__[i]
153
if has_prop.call(kwobj, prop):
154
supplied_args[i] = kwobj[prop]
155
return f.apply(this, supplied_args)
156
157
def ρσ_interpolate_kwargs_constructor(apply, f, supplied_args):
158
if apply:
159
f.apply(this, supplied_args)
160
else:
161
ρσ_interpolate_kwargs.call(this, f, supplied_args)
162
return this
163
164
def ρσ_getitem(obj, key):
165
if obj.__getitem__:
166
return obj.__getitem__(key)
167
if jstype(key) is 'number' and key < 0:
168
key += obj.length
169
return obj[key]
170
171
def ρσ_setitem(obj, key, val):
172
if obj.__setitem__:
173
obj.__setitem__(key, val)
174
else:
175
if jstype(key) is 'number' and key < 0:
176
key += obj.length
177
obj[key] = val
178
179
def ρσ_delitem(obj, key):
180
if obj.__delitem__:
181
obj.__delitem__(key)
182
elif jstype(obj.splice) is 'function':
183
obj.splice(key, 1)
184
else:
185
if jstype(key) is 'number' and key < 0:
186
key += obj.length
187
v'delete obj[key]'
188
189
def ρσ_bound_index(idx, arr):
190
if jstype(idx) is 'number' and idx < 0:
191
idx += arr.length
192
return idx
193
194
def ρσ_splice(arr, val, start, end):
195
start = start or 0
196
if start < 0:
197
start += arr.length
198
if end is undefined:
199
end = arr.length
200
if end < 0:
201
end += arr.length
202
Array.prototype.splice.apply(arr, v'[start, end - start].concat(val)')
203
204
ρσ_exists = {
205
'n': def(expr):
206
return expr is not undefined and expr is not None
207
,'d': def(expr):
208
if expr is undefined or expr is None:
209
return Object.create(None)
210
return expr
211
,'c': def(expr):
212
if jstype(expr) is 'function':
213
return expr
214
return def():
215
return undefined
216
,'g': def(expr):
217
if expr is undefined or expr is None or jstype(expr.__getitem__) is not 'function':
218
return {'__getitem__': def(): return undefined;}
219
,'e': def(expr, alt):
220
return alt if expr is undefined or expr is None else expr
221
}
222
223
def ρσ_mixin():
224
# Implement a depth-first left-to-right method resolution order This is not
225
# the same as python's MRO, but I really dont feel like implementing the C3
226
# linearization right now, if that is even possible with prototypical
227
# inheritance.
228
seen = Object.create(None)
229
# Ensure the following special properties are never copied
230
seen.__argnames__ = seen.__handles_kwarg_interpolation__ = seen.__init__ = seen.__annotations__ = seen.__doc__ = seen.__bind_methods__ = seen.__bases__ = seen.constructor = seen.__class__ = True
231
resolved_props = {}
232
p = target = arguments[0].prototype
233
while p and p is not Object.prototype:
234
props = Object.getOwnPropertyNames(p)
235
for v'var i = 0; i < props.length; i++':
236
seen[props[i]] = True
237
p = Object.getPrototypeOf(p)
238
for v'var c = 1; c < arguments.length; c++':
239
p = arguments[c].prototype
240
while p and p is not Object.prototype:
241
props = Object.getOwnPropertyNames(p)
242
for v'var i = 0; i < props.length; i++':
243
name = props[i]
244
if seen[name]:
245
continue
246
seen[name] = True
247
resolved_props[name] = Object.getOwnPropertyDescriptor(p, name)
248
p = Object.getPrototypeOf(p)
249
Object.defineProperties(target, resolved_props)
250
251
def ρσ_instanceof():
252
obj = arguments[0]
253
bases = ''
254
if obj and obj.constructor and obj.constructor.prototype:
255
bases = obj.constructor.prototype.__bases__ or ''
256
for v'var i = 1; i < arguments.length; i++':
257
q = arguments[i]
258
if v'obj instanceof q':
259
return True
260
if (q is Array or q is ρσ_list_constructor) and Array.isArray(obj):
261
return True
262
if q is ρσ_str and (jstype(obj) is 'string' or v'obj instanceof String'):
263
return True
264
if q is ρσ_int and (jstype(obj) is 'number' and Number.isInteger(obj)):
265
return True
266
if q is ρσ_float and (jstype(obj) is 'number' and not Number.isInteger(obj)):
267
return True
268
if bases.length > 1:
269
for v'var c = 1; c < bases.length; c++':
270
cls = bases[c]
271
while cls:
272
if q is cls:
273
return True
274
p = Object.getPrototypeOf(cls.prototype)
275
if not p:
276
break
277
cls = p.constructor
278
return False
279
280