-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathpython.js
More file actions
12035 lines (11034 loc) · 357 KB
/
Copy pathpython.js
File metadata and controls
12035 lines (11034 loc) · 357 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
(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
(function webpackUniversalModuleDefinition(root, factory) {
if(typeof exports === 'object' && typeof module === 'object')
module.exports = factory();
else if(typeof define === 'function' && define.amd)
define([], factory);
else if(typeof exports === 'object')
exports["filbert"] = factory();
else
root["filbert"] = factory();
})(this, function() {
return /******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId])
/******/ return installedModules[moduleId].exports;
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ exports: {},
/******/ id: moduleId,
/******/ loaded: false
/******/ };
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/ // Flag the module as loaded
/******/ module.loaded = true;
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/ // Load entry module and return exports
/******/ return __webpack_require__(0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ function(module, exports, __webpack_require__) {
'use strict';
var Sk = __webpack_require__(1);
var transform = __webpack_require__(2);
var improveError = __webpack_require__(3);
var defaultOptions = {
locations: true,
ranges: true,
sippets: true,
filename: 'file.py',
useLet: false,
friendlyErrors: true
};
function rangeToLoc(x, offsets) {
var best = -1;
for ( var i = 0; i < offsets.length; ++i ) {
if ( offsets[i] > x ) break;
best = i;
}
var off = best >= 0 ? offsets[best] : 0;
return {line: best+2, column: x - off, pos: x };
}
function locToRange(line, col, offsets) {
var loff = 0;
if ( line > 2 && (line-2) < offsets.length ) loff = offsets[line-2];
return loff + col;
}
function decorate(n, code, offsets, options) {
var numrange = locToRange(n.lineno, n.col_offset, offsets);
var range = [
numrange === numrange ? numrange : Infinity,
numrange === numrange ? numrange : -Infinity
];
if ( n.value ) range[1] += (n.value.length);
if ( n.children )
for ( var i = 0; i < n.children.length; ++i ) {
var r = decorate(n.children[i], code, offsets, options);
range[0] = Math.min(range[0], r[0]);
range[1] = Math.max(range[1], r[1]);
}
if ( options.ranges ) n.range = range;
if ( options.locations ) {
n.loc = {
start: rangeToLoc(range[0], offsets),
end: rangeToLoc(range[1], offsets),
};
}
if ( options.snippets ) n.str = code.substring(range[0], range[1]);
return range;
}
function parser(code, options) {
var lineOffsets = [];
var idx = -1;
var parse, ast;
options = options || {};
for ( var opt in defaultOptions ) {
if ( !(opt in options) ) options[opt] = defaultOptions[opt];
}
while ( true ) {
idx = code.indexOf("\n", idx+1);
if ( idx < 0 ) break;
lineOffsets.push(idx+1);
}
try {
parse = Sk.parse(options.filename, code);
decorate(parse.cst, code, lineOffsets, options);
parse.flags = parse.flags | Sk.Parser.CO_FUTURE_UNICODE_LITERALS; //Enable future unicode literals
ast = Sk.astFromParse(parse.cst, options.filename, parse.flags);
} catch ( e ) {
if ( e.extra && e.extra.node ) decorate(e.extra.node, code, lineOffsets, options);
improveError(e, options, code);
if ( e.loc ) {
e.pos = locToRange(e.loc.line, e.loc.column, lineOffsets);
}
throw e;
}
//console.log(JSON.stringify(ast, null, " "));
var ctx = {varType: (options.useLet ? 'let' : 'var')};
var js = transform(ast, ctx);
return js;
}
module.exports = {
parse: parser,
pythonRuntime: __webpack_require__(4),
defaultOptions: {runtimeParamName: '__pythonRuntime'}
};
/***/ },
/* 1 */
/***/ function(module, exports) {
/* WEBPACK VAR INJECTION */(function(global) {
/* ---- /Users/rob/skulpty/lib/preamble.js ---- */
var goog = {
global: global
};
var COMPILED = false;
goog.exportSymbol = function() {};
goog.require = function() {};
goog.inherits = function(childCtor, parentCtor) {
if ( !parentCtor ) throw new Error("Cant inherit from undefined?");
/** @constructor */
function tempCtor() {};
tempCtor.prototype = parentCtor.prototype;
childCtor.superClass_ = parentCtor.prototype;
childCtor.prototype = new tempCtor();
/** @override */
childCtor.prototype.constructor = childCtor;
};
goog.asserts = {
assert: function(what, why) {
if ( !what ) throw new Error("AssertionFailed:" + why);
}
};
var Sk = Sk || {};
Sk.builtin = Sk.builtin || {};
Sk.builtin.bool = Boolean;
Sk.builtin.int_ = function Int(x) {
this.v = x;
};
Sk.builtin.int_.prototype.threshold$ = Infinity;
Sk.builtin.int_.prototype.valueOf = function() { return this.v.valueOf(); };
Sk.builtin.tuple = function(x) {
this.v = Array.prototype.slice.call(x, 0);
};
Sk.builtin.tuple.prototype.sq$length = function() { return this.length; };
Sk.builtin.float_ = Number;
Sk.builtin.long = Number;
Sk.builtin.func = function(fx) { return fx; };
/* ---- /Users/rob/skulpty/lib/../node_modules/skulpt/src/type.js ---- */
if(Sk.builtin === undefined) {
Sk.builtin = {};
}
/**
* Maps Python dunder names to the Skulpt Javascript function names that
* implement them.
*
* Note: __add__, __mul__, and __rmul__ can be used for either numeric or
* sequence types. Here, they default to the numeric versions (i.e. nb$add,
* nb$multiply, and nb$reflected_multiply). This works because Sk.abstr.binary_op_
* checks for the numeric shortcuts and not the sequence shortcuts when computing
* a binary operation.
*
* Because many of these functions are used in contexts in which Skulpt does not
* [yet] handle suspensions, the assumption is that they must not suspend. However,
* some of these built-in functions are acquiring 'canSuspend' arguments to signal
* where this is not the case. These need to be spliced out of the argument list before
* it is passed to python. Array values in this map contain [dunderName, argumentIdx],
* where argumentIdx specifies the index of the 'canSuspend' boolean argument.
*
* @type {Object}
*/
Sk.dunderToSkulpt = {
"__eq__": "ob$eq",
"__ne__": "ob$ne",
"__lt__": "ob$lt",
"__le__": "ob$le",
"__gt__": "ob$gt",
"__ge__": "ob$ge",
"__hash__": "tp$hash",
"__abs__": "nb$abs",
"__neg__": "nb$negative",
"__pos__": "nb$positive",
"__int__": "nb$int_",
"__long__": "nb$lng",
"__float__": "nb$float_",
"__add__": "nb$add",
"__radd__": "nb$reflected_add",
"__sub__": "nb$subtract",
"__rsub__": "nb$reflected_subtract",
"__mul__": "nb$multiply",
"__rmul__": "nb$reflected_multiply",
"__div__": "nb$divide",
"__rdiv__": "nb$reflected_divide",
"__floordiv__": "nb$floor_divide",
"__rfloordiv__": "nb$reflected_floor_divide",
"__mod__": "nb$remainder",
"__rmod__": "nb$reflected_remainder",
"__divmod__": "nb$divmod",
"__rdivmod__": "nb$reflected_divmod",
"__pow__": "nb$power",
"__rpow__": "nb$reflected_power",
"__contains__": "sq$contains",
"__len__": ["sq$length", 0]
};
/**
*
* @constructor
*
* @param {*} name name or object to get type of, if only one arg
*
* @param {Sk.builtin.tuple=} bases
*
* @param {Object=} dict
*
*
* This type represents the type of `type'. *Calling* an instance of
* this builtin type named "type" creates class objects. The resulting
* class objects will have various tp$xyz attributes on them that allow
* for the various operations on that object.
*
* calling the type or calling an instance of the type? or both?
*/
Sk.builtin.type = function (name, bases, dict) {
var mro;
var obj;
var klass;
var v;
if (bases === undefined && dict === undefined) {
// 1 arg version of type()
// the argument is an object, not a name and returns a type object
obj = name;
return obj.ob$type;
} else {
// argument dict must be of type dict
if(dict.tp$name !== "dict") {
throw new Sk.builtin.TypeError("type() argument 3 must be dict, not " + Sk.abstr.typeName(dict));
}
// checks if name must be string
if(!Sk.builtin.checkString(name)) {
throw new Sk.builtin.TypeError("type() argument 1 must be str, not " + Sk.abstr.typeName(name));
}
// argument bases must be of type tuple
if(bases.tp$name !== "tuple") {
throw new Sk.builtin.TypeError("type() argument 2 must be tuple, not " + Sk.abstr.typeName(bases));
}
// type building version of type
// dict is the result of running the classes code object
// (basically the dict of functions). those become the prototype
// object of the class).
/**
* @constructor
*/
klass = function (kwdict, varargseq, kws, args, canSuspend) {
var init;
var self = this;
var s;
var args_copy;
if (!(this instanceof klass)) {
return new klass(kwdict, varargseq, kws, args, canSuspend);
}
args = args || [];
self["$d"] = new Sk.builtin.dict([]);
self["$d"].mp$ass_subscript(new Sk.builtin.str("__dict__"), self["$d"]);
if (klass.prototype.tp$base !== undefined) {
if (klass.prototype.tp$base.sk$klass) {
klass.prototype.tp$base.call(this, kwdict, varargseq, kws, args.slice(), canSuspend);
} else {
// Call super constructor if subclass of a builtin
args_copy = args.slice();
args_copy.unshift(klass, this);
Sk.abstr.superConstructor.apply(undefined, args_copy);
}
}
init = Sk.builtin.type.typeLookup(self.ob$type, "__init__");
if (init !== undefined) {
// return should be None or throw a TypeError otherwise
args.unshift(self);
s = Sk.misceval.applyOrSuspend(init, kwdict, varargseq, kws, args);
return (function doSusp(s) {
if (s instanceof Sk.misceval.Suspension) {
// TODO I (Meredydd) don't know whether we are ever called
// from anywhere except Sk.misceval.applyOrSuspend().
// If we're not, we don't need a canSuspend parameter at all.
if (canSuspend) {
return new Sk.misceval.Suspension(doSusp, s);
} else {
return Sk.misceval.retryOptionalSuspensionOrThrow(s);
}
} else {
return self;
}
})(s);
}
return self;
};
var _name = Sk.ffi.remapToJs(name); // unwrap name string to js for latter use
var inheritsFromObject = false, inheritsBuiltin = false;
if (bases.v.length === 0 && Sk.python3) {
// new style class, inherits from object by default
inheritsFromObject = true;
Sk.abstr.setUpInheritance(_name, klass, Sk.builtin.object);
}
var parent, it, firstAncestor, builtin_bases = [];
// Set up inheritance from any builtins
for (it = bases.tp$iter(), parent = it.tp$iternext(); parent !== undefined; parent = it.tp$iternext()) {
if (firstAncestor === undefined) {
firstAncestor = parent;
}
if (parent.prototype instanceof Sk.builtin.object || parent === Sk.builtin.object) {
while (parent.sk$klass && parent.prototype.tp$base) {
parent = parent.prototype.tp$base;
}
if (!parent.sk$klass && builtin_bases.indexOf(parent) < 0) {
builtin_bases.push(parent);
}
// This class inherits from Sk.builtin.object at some level
inheritsFromObject = true;
}
}
if (builtin_bases.length > 1) {
throw new Sk.builtin.TypeError("Multiple inheritance with more than one builtin type is unsupported");
}
// Javascript does not support multiple inheritance, so only the first
// base (if any) will directly inherit in Javascript
if (firstAncestor !== undefined) {
goog.inherits(klass, firstAncestor);
if (firstAncestor.prototype instanceof Sk.builtin.object || firstAncestor === Sk.builtin.object) {
klass.prototype.tp$base = firstAncestor;
}
}
klass.prototype.tp$name = _name;
klass.prototype.ob$type = Sk.builtin.type.makeIntoTypeObj(_name, klass);
if (!inheritsFromObject) {
// old style class, does not inherit from object
klass.prototype.tp$getattr = Sk.builtin.object.prototype.GenericGetAttr;
klass.prototype.tp$setattr = Sk.builtin.object.prototype.GenericSetAttr;
}
// set __module__ if not present (required by direct type(name, bases, dict) calls)
var module_lk = new Sk.builtin.str("__module__");
if(dict.mp$lookup(module_lk) === undefined) {
dict.mp$ass_subscript(module_lk, Sk.globals["__name__"]);
}
// copy properties into our klass object
// uses python iter methods
var k;
for (it = dict.tp$iter(), k = it.tp$iternext(); k !== undefined; k = it.tp$iternext()) {
v = dict.mp$subscript(k);
if (v === undefined) {
v = null;
}
klass.prototype[k.v] = v;
klass[k.v] = v;
}
klass["__class__"] = klass;
klass["__name__"] = name;
klass.sk$klass = true;
klass.prototype.tp$descr_get = function () {
goog.asserts.fail("in type tp$descr_get");
};
klass.prototype["$r"] = function () {
var cname;
var mod;
// TODO use Sk.abstr.gattr() here so __repr__ can be dynamically provided (eg by __getattr__())
var reprf = this.tp$getattr("__repr__");
if (reprf !== undefined && reprf.im_func !== Sk.builtin.object.prototype["__repr__"]) {
return Sk.misceval.apply(reprf, undefined, undefined, undefined, []);
}
if ((klass.prototype.tp$base !== undefined) &&
(klass.prototype.tp$base !== Sk.builtin.object) &&
(klass.prototype.tp$base.prototype["$r"] !== undefined)) {
// If subclass of a builtin which is not object, use that class' repr
return klass.prototype.tp$base.prototype["$r"].call(this);
} else {
// Else, use default repr for a user-defined class instance
mod = dict.mp$subscript(module_lk); // lookup __module__
cname = "";
if (mod) {
cname = mod.v + ".";
}
return new Sk.builtin.str("<" + cname + _name + " object>");
}
};
klass.prototype.tp$str = function () {
// TODO use Sk.abstr.gattr() here so __str__ can be dynamically provided (eg by __getattr__())
var strf = this.tp$getattr("__str__");
if (strf !== undefined && strf.im_func !== Sk.builtin.object.prototype["__str__"]) {
return Sk.misceval.apply(strf, undefined, undefined, undefined, []);
}
if ((klass.prototype.tp$base !== undefined) &&
(klass.prototype.tp$base !== Sk.builtin.object) &&
(klass.prototype.tp$base.prototype.tp$str !== undefined)) {
// If subclass of a builtin which is not object, use that class' repr
return klass.prototype.tp$base.prototype.tp$str.call(this);
}
return this["$r"]();
};
klass.prototype.tp$length = function (canSuspend) {
var r = Sk.misceval.chain(Sk.abstr.gattr(this, "__len__", canSuspend), function(lenf) {
return Sk.misceval.applyOrSuspend(lenf, undefined, undefined, undefined, []);
});
return canSuspend ? r : Sk.misceval.retryOptionalSuspensionOrThrow(r);
};
klass.prototype.tp$call = function (args, kw) {
return Sk.misceval.chain(Sk.abstr.gattr(this, "__call__", true), function(callf) {
return Sk.misceval.applyOrSuspend(callf, undefined, undefined, kw, args);
});
};
klass.prototype.tp$iter = function () {
var iterf = Sk.abstr.gattr(this, "__iter__", false);
return Sk.misceval.callsim(iterf);
};
klass.prototype.tp$iternext = function (canSuspend) {
var self = this;
var r = Sk.misceval.chain(
Sk.misceval.tryCatch(function() {
return Sk.abstr.gattr(self, "next", canSuspend);
}, function(e) {
if (e instanceof Sk.builtin.AttributeError) {
throw new Sk.builtin.TypeError("'" + Sk.abstr.typeName(self) + "' object is not iterable");
} else {
throw e;
}
}),
function(/** {Object} */ iternextf) {
return Sk.misceval.tryCatch(function() {
return Sk.misceval.callsimOrSuspend(iternextf);
}, function(e) {
if (e instanceof Sk.builtin.StopIteration) {
return undefined;
} else {
throw e;
}
});
});
return canSuspend ? r : Sk.misceval.retryOptionalSuspensionOrThrow(r);
};
klass.prototype.tp$getitem = function (key, canSuspend) {
var getf = Sk.abstr.gattr(this, "__getitem__", canSuspend), r;
if (getf !== undefined) {
r = Sk.misceval.applyOrSuspend(getf, undefined, undefined, undefined, [key]);
return canSuspend ? r : Sk.misceval.retryOptionalSuspensionOrThrow(r);
}
throw new Sk.builtin.TypeError("'" + Sk.abstr.typeName(this) + "' object does not support indexing");
};
klass.prototype.tp$setitem = function (key, value, canSuspend) {
var setf = Sk.abstr.gattr(this, "__setitem__", canSuspend), r;
if (setf !== undefined) {
r = Sk.misceval.applyOrSuspend(setf, undefined, undefined, undefined, [key, value]);
return canSuspend ? r : Sk.misceval.retryOptionalSuspensionOrThrow(r);
}
throw new Sk.builtin.TypeError("'" + Sk.abstr.typeName(this) + "' object does not support item assignment");
};
if (bases) {
//print("building mro for", name);
//for (var i = 0; i < bases.length; ++i)
//print("base[" + i + "]=" + bases[i].tp$name);
klass["$d"] = new Sk.builtin.dict([]);
klass["$d"].mp$ass_subscript(Sk.builtin.type.basesStr_, bases);
mro = Sk.builtin.type.buildMRO(klass);
klass["$d"].mp$ass_subscript(Sk.builtin.type.mroStr_, mro);
klass.tp$mro = mro;
//print("mro result", Sk.builtin.repr(mro).v);
}
// fix for class attributes
klass.tp$setattr = Sk.builtin.type.prototype.tp$setattr;
var shortcutDunder = function (skulpt_name, magic_name, magic_func, canSuspendIdx) {
klass.prototype[skulpt_name] = function () {
var args = Array.prototype.slice.call(arguments), canSuspend;
args.unshift(magic_func, this);
if (canSuspendIdx) {
canSuspend = args[canSuspendIdx+1];
args.splice(canSuspendIdx+1, 1);
if (canSuspend) {
return Sk.misceval.callsimOrSuspend.apply(undefined, args);
}
}
return Sk.misceval.callsim.apply(undefined, args);
};
};
// Register skulpt shortcuts to magic methods defined by this class.
// TODO: This is somewhat problematic, as it means that dynamically defined
// methods (eg those returned by __getattr__()) cannot be used by these magic
// functions.
var dunder, skulpt_name, canSuspendIdx;
for (dunder in Sk.dunderToSkulpt) {
skulpt_name = Sk.dunderToSkulpt[dunder];
if (typeof(skulpt_name) === "string") {
canSuspendIdx = null;
} else {
canSuspendIdx = skulpt_name[1];
skulpt_name = skulpt_name[0];
}
if (klass[dunder]) {
// scope workaround
shortcutDunder(skulpt_name, dunder, klass[dunder], canSuspendIdx);
}
}
return klass;
}
};
/**
*
*/
Sk.builtin.type.makeTypeObj = function (name, newedInstanceOfType) {
Sk.builtin.type.makeIntoTypeObj(name, newedInstanceOfType);
return newedInstanceOfType;
};
Sk.builtin.type.makeIntoTypeObj = function (name, t) {
goog.asserts.assert(name !== undefined);
goog.asserts.assert(t !== undefined);
t.ob$type = Sk.builtin.type;
t.tp$name = name;
t["$r"] = function () {
var ctype;
var mod = t.__module__;
var cname = "";
if (mod) {
cname = mod.v + ".";
}
ctype = "class";
if (!mod && !t.sk$klass && !Sk.python3) {
ctype = "type";
}
return new Sk.builtin.str("<" + ctype + " '" + cname + t.tp$name + "'>");
};
t.tp$str = undefined;
t.tp$getattr = Sk.builtin.type.prototype.tp$getattr;
t.tp$setattr = Sk.builtin.object.prototype.GenericSetAttr;
t.tp$richcompare = Sk.builtin.type.prototype.tp$richcompare;
t.sk$type = true;
return t;
};
Sk.builtin.type.ob$type = Sk.builtin.type;
Sk.builtin.type.tp$name = "type";
Sk.builtin.type["$r"] = function () {
if(Sk.python3) {
return new Sk.builtin.str("<class 'type'>");
} else {
return new Sk.builtin.str("<type 'type'>");
}
};
//Sk.builtin.type.prototype.tp$descr_get = function() { print("in type descr_get"); };
//Sk.builtin.type.prototype.tp$name = "type";
// basically the same as GenericGetAttr except looks in the proto instead
Sk.builtin.type.prototype.tp$getattr = function (name) {
var res;
var tp = this;
var descr;
var f;
if (this["$d"]) {
res = this["$d"].mp$lookup(new Sk.builtin.str(name));
if (res !== undefined) {
return res;
}
}
descr = Sk.builtin.type.typeLookup(tp, name);
//print("type.tpgetattr descr", descr, descr.tp$name, descr.func_code, name);
if (descr !== undefined && descr !== null && descr.ob$type !== undefined) {
f = descr.ob$type.tp$descr_get;
// todo;if (f && descr.tp$descr_set) // is a data descriptor if it has a set
// return f.call(descr, this, this.ob$type);
}
if (f) {
// non-data descriptor
return f.call(descr, null, tp);
}
if (descr !== undefined) {
return descr;
}
return undefined;
};
Sk.builtin.type.prototype.tp$setattr = function (name, value) {
// class attributes are direct properties of the object
this[name] = value;
};
Sk.builtin.type.typeLookup = function (type, name) {
var mro = type.tp$mro;
var pyname = new Sk.builtin.str(name);
var base;
var res;
var i;
// todo; probably should fix this, used for builtin types to get stuff
// from prototype
if (!mro) {
if (type.prototype) {
return type.prototype[name];
}
return undefined;
}
for (i = 0; i < mro.v.length; ++i) {
base = mro.v[i];
if (base.hasOwnProperty(name)) {
return base[name];
}
res = base["$d"].mp$lookup(pyname);
if (res !== undefined) {
return res;
}
if (base.prototype && base.prototype[name] !== undefined) {
return base.prototype[name];
}
}
return undefined;
};
Sk.builtin.type.mroMerge_ = function (seqs) {
/*
var tmp = [];
for (var i = 0; i < seqs.length; ++i)
{
tmp.push(new Sk.builtin.list(seqs[i]));
}
print(Sk.builtin.repr(new Sk.builtin.list(tmp)).v);
*/
var seq;
var i;
var next;
var k;
var sseq;
var j;
var cand;
var cands;
var res = [];
for (; ;) {
for (i = 0; i < seqs.length; ++i) {
seq = seqs[i];
if (seq.length !== 0) {
break;
}
}
if (i === seqs.length) { // all empty
return res;
}
cands = [];
for (i = 0; i < seqs.length; ++i) {
seq = seqs[i];
//print("XXX", Sk.builtin.repr(new Sk.builtin.list(seq)).v);
if (seq.length !== 0) {
cand = seq[0];
//print("CAND", Sk.builtin.repr(cand).v);
OUTER:
for (j = 0; j < seqs.length; ++j) {
sseq = seqs[j];
for (k = 1; k < sseq.length; ++k) {
if (sseq[k] === cand) {
break OUTER;
}
}
}
// cand is not in any sequences' tail -> constraint-free
if (j === seqs.length) {
cands.push(cand);
}
}
}
if (cands.length === 0) {
throw new Sk.builtin.TypeError("Inconsistent precedences in type hierarchy");
}
next = cands[0];
// append next to result and remove from sequences
res.push(next);
for (i = 0; i < seqs.length; ++i) {
seq = seqs[i];
if (seq.length > 0 && seq[0] === next) {
seq.splice(0, 1);
}
}
}
};
Sk.builtin.type.buildMRO_ = function (klass) {
// MERGE(klass + mro(bases) + bases)
var i;
var bases;
var all = [
[klass]
];
//Sk.debugout("buildMRO for", klass.tp$name);
var kbases = klass["$d"].mp$subscript(Sk.builtin.type.basesStr_);
for (i = 0; i < kbases.v.length; ++i) {
all.push(Sk.builtin.type.buildMRO_(kbases.v[i]));
}
bases = [];
for (i = 0; i < kbases.v.length; ++i) {
bases.push(kbases.v[i]);
}
all.push(bases);
return Sk.builtin.type.mroMerge_(all);
};
/*
* C3 MRO (aka CPL) linearization. Figures out which order to search through
* base classes to determine what should override what. C3 does the "right
* thing", and it's what Python has used since 2.3.
*
* Kind of complicated to explain, but not really that complicated in
* implementation. Explanations:
*
* http://people.csail.mit.edu/jrb/goo/manual.43/goomanual_55.html
* http://www.python.org/download/releases/2.3/mro/
* http://192.220.96.201/dylan/linearization-oopsla96.html
*
* This implementation is based on a post by Samuele Pedroni on python-dev
* (http://mail.python.org/pipermail/python-dev/2002-October/029176.html) when
* discussing its addition to Python.
*/
Sk.builtin.type.buildMRO = function (klass) {
return new Sk.builtin.tuple(Sk.builtin.type.buildMRO_(klass));
};
Sk.builtin.type.prototype.tp$richcompare = function (other, op) {
var r2;
var r1;
if (other.ob$type != Sk.builtin.type) {
return undefined;
}
if (!this["$r"] || !other["$r"]) {
return undefined;
}
r1 = new Sk.builtin.str(this["$r"]().v.slice(1,6));
r2 = new Sk.builtin.str(other["$r"]().v.slice(1,6));
if (this["$r"]().v.slice(1,6) !== "class") {
r1 = this["$r"]();
r2 = other["$r"]();
}
return r1.tp$richcompare(r2, op);
};
/* ---- /Users/rob/skulpty/lib/../node_modules/skulpt/src/abstract.js ---- */
/**
* @namespace Sk.abstr
*
*/
Sk.abstr = {};
//
// Number
//
Sk.abstr.typeName = function (v) {
var vtypename;
if (v.tp$name !== undefined) {
vtypename = v.tp$name;
} else {
vtypename = "<invalid type>";
}
return vtypename;
};
Sk.abstr.binop_type_error = function (v, w, name) {
var vtypename = Sk.abstr.typeName(v),
wtypename = Sk.abstr.typeName(w);
throw new Sk.builtin.TypeError("unsupported operand type(s) for " + name + ": '" + vtypename + "' and '" + wtypename + "'");
};
Sk.abstr.unop_type_error = function (v, name) {
var vtypename = Sk.abstr.typeName(v),
uop = {
"UAdd" : "+",
"USub" : "-",
"Invert": "~"
}[name];
throw new Sk.builtin.TypeError("bad operand type for unary " + uop + ": '" + vtypename + "'");
};
/**
* lookup and return the LHS object slot function method. This coudl be either a builtin slot function or a dunder method defined by the user.
* @param obj
* @param name
* @returns {Object|null|undefined}
* @private
*/
Sk.abstr.boNameToSlotFuncLhs_ = function (obj, name) {
if (obj === null) {
return undefined;
}
switch (name) {
case "Add":
return obj.nb$add ? obj.nb$add : obj["__add__"];
case "Sub":
return obj.nb$subtract ? obj.nb$subtract : obj["__sub__"];
case "Mult":
return obj.nb$multiply ? obj.nb$multiply : obj["__mul__"];
case "Div":
return obj.nb$divide ? obj.nb$divide : obj["__div__"];
case "FloorDiv":
return obj.nb$floor_divide ? obj.nb$floor_divide : obj["__floordiv__"];
case "Mod":
return obj.nb$remainder ? obj.nb$remainder : obj["__mod__"];
case "DivMod":
return obj.nb$divmod ? obj.nb$divmod : obj["__divmod__"];
case "Pow":
return obj.nb$power ? obj.nb$power : obj["__pow__"];
case "LShift":
return obj.nb$lshift ? obj.nb$lshift : obj["__lshift__"];
case "RShift":
return obj.nb$rshift ? obj.nb$rshift : obj["__rshift__"];
case "BitAnd":
return obj.nb$and ? obj.nb$and : obj["__and__"];
case "BitXor":
return obj.nb$xor ? obj.nb$xor : obj["__xor__"];
case "BitOr":
return obj.nb$or ? obj.nb$or : obj["__or__"];
}
};
Sk.abstr.boNameToSlotFuncRhs_ = function (obj, name) {
if (obj === null) {
return undefined;
}
switch (name) {
case "Add":
return obj.nb$reflected_add ? obj.nb$reflected_add : obj["__radd__"];
case "Sub":
return obj.nb$reflected_subtract ? obj.nb$reflected_subtract : obj["__rsub__"];
case "Mult":
return obj.nb$reflected_multiply ? obj.nb$reflected_multiply : obj["__rmul__"];
case "Div":
return obj.nb$reflected_divide ? obj.nb$reflected_divide : obj["__rdiv__"];
case "FloorDiv":
return obj.nb$reflected_floor_divide ? obj.nb$reflected_floor_divide : obj["__rfloordiv__"];
case "Mod":
return obj.nb$reflected_remainder ? obj.nb$reflected_remainder : obj["__rmod__"];
case "DivMod":
return obj.nb$reflected_divmod ? obj.nb$reflected_divmod : obj["__rdivmod__"];
case "Pow":
return obj.nb$reflected_power ? obj.nb$reflected_power : obj["__rpow__"];
case "LShift":
return obj.nb$reflected_lshift ? obj.nb$reflected_lshift : obj["__rlshift__"];
case "RShift":
return obj.nb$reflected_rshift ? obj.nb$reflected_rshift : obj["__rrshift__"];
case "BitAnd":
return obj.nb$reflected_and ? obj.nb$reflected_and : obj["__rand__"];
case "BitXor":
return obj.nb$reflected_xor ? obj.nb$reflected_xor : obj["__rxor__"];
case "BitOr":
return obj.nb$reflected_or ? obj.nb$reflected_or : obj["__ror__"];
}
};
Sk.abstr.iboNameToSlotFunc_ = function (obj, name) {
switch (name) {
case "Add":
return obj.nb$inplace_add ? obj.nb$inplace_add : obj["__iadd__"];
case "Sub":
return obj.nb$inplace_subtract ? obj.nb$inplace_subtract : obj["__isub__"];
case "Mult":
return obj.nb$inplace_multiply ? obj.nb$inplace_multiply : obj["__imul__"];
case "Div":
return obj.nb$inplace_divide ? obj.nb$inplace_divide : obj["__idiv__"];
case "FloorDiv":
return obj.nb$inplace_floor_divide ? obj.nb$inplace_floor_divide : obj["__ifloordiv__"];
case "Mod":
return obj.nb$inplace_remainder;
case "Pow":
return obj.nb$inplace_power;
case "LShift":
return obj.nb$inplace_lshift ? obj.nb$inplace_lshift : obj["__ilshift__"];
case "RShift":
return obj.nb$inplace_rshift ? obj.nb$inplace_rshift : obj["__irshift__"];
case "BitAnd":
return obj.nb$inplace_and;
case "BitOr":
return obj.nb$inplace_or;