rapydscript-ns 0.8.4 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (132) hide show
  1. package/.agignore +1 -1
  2. package/.github/workflows/ci.yml +38 -38
  3. package/=template.pyj +5 -5
  4. package/CHANGELOG.md +18 -0
  5. package/HACKING.md +103 -103
  6. package/LICENSE +24 -24
  7. package/README.md +715 -169
  8. package/TODO.md +9 -2
  9. package/add-toc-to-readme +2 -2
  10. package/bin/export +75 -75
  11. package/bin/rapydscript +70 -70
  12. package/bin/web-repl-export +102 -102
  13. package/build +2 -2
  14. package/language-service/index.js +36 -27
  15. package/package.json +1 -1
  16. package/publish.py +37 -37
  17. package/release/baselib-plain-pretty.js +2358 -168
  18. package/release/baselib-plain-ugly.js +73 -3
  19. package/release/compiler.js +6282 -3092
  20. package/release/signatures.json +31 -30
  21. package/session.vim +4 -4
  22. package/setup.cfg +2 -2
  23. package/src/ast.pyj +1 -0
  24. package/src/baselib-builtins.pyj +340 -2
  25. package/src/baselib-bytes.pyj +664 -0
  26. package/src/baselib-errors.pyj +1 -1
  27. package/src/baselib-internal.pyj +267 -60
  28. package/src/baselib-itertools.pyj +110 -97
  29. package/src/baselib-str.pyj +22 -4
  30. package/src/compiler.pyj +36 -36
  31. package/src/errors.pyj +30 -30
  32. package/src/lib/abc.pyj +317 -0
  33. package/src/lib/aes.pyj +646 -646
  34. package/src/lib/copy.pyj +120 -120
  35. package/src/lib/dataclasses.pyj +532 -0
  36. package/src/lib/elementmaker.pyj +83 -83
  37. package/src/lib/encodings.pyj +126 -126
  38. package/src/lib/enum.pyj +125 -0
  39. package/src/lib/gettext.pyj +569 -569
  40. package/src/lib/itertools.pyj +580 -580
  41. package/src/lib/math.pyj +193 -193
  42. package/src/lib/operator.pyj +11 -11
  43. package/src/lib/pythonize.pyj +20 -20
  44. package/src/lib/random.pyj +118 -118
  45. package/src/lib/re.pyj +504 -470
  46. package/src/lib/react.pyj +74 -74
  47. package/src/lib/traceback.pyj +63 -63
  48. package/src/lib/typing.pyj +577 -0
  49. package/src/lib/uuid.pyj +77 -77
  50. package/src/monaco-language-service/builtins.js +14 -4
  51. package/src/monaco-language-service/diagnostics.js +19 -20
  52. package/src/monaco-language-service/dts.js +550 -550
  53. package/src/output/classes.pyj +62 -26
  54. package/src/output/comments.pyj +45 -45
  55. package/src/output/exceptions.pyj +201 -201
  56. package/src/output/functions.pyj +78 -5
  57. package/src/output/jsx.pyj +164 -164
  58. package/src/output/loops.pyj +5 -2
  59. package/src/output/operators.pyj +100 -34
  60. package/src/output/treeshake.pyj +182 -182
  61. package/src/output/utils.pyj +72 -72
  62. package/src/parse.pyj +80 -16
  63. package/src/string_interpolation.pyj +72 -72
  64. package/src/tokenizer.pyj +9 -4
  65. package/src/unicode_aliases.pyj +576 -576
  66. package/src/utils.pyj +192 -192
  67. package/test/_import_one.pyj +37 -37
  68. package/test/_import_two/__init__.pyj +11 -11
  69. package/test/_import_two/level2/deep.pyj +4 -4
  70. package/test/_import_two/other.pyj +6 -6
  71. package/test/_import_two/sub.pyj +13 -13
  72. package/test/abc.pyj +291 -0
  73. package/test/aes_vectors.pyj +421 -421
  74. package/test/annotations.pyj +80 -80
  75. package/test/arithmetic_nostrict.pyj +88 -0
  76. package/test/arithmetic_types.pyj +169 -0
  77. package/test/baselib.pyj +91 -0
  78. package/test/bytes.pyj +467 -0
  79. package/test/classes.pyj +1 -0
  80. package/test/comparison_ops.pyj +173 -0
  81. package/test/dataclasses.pyj +253 -0
  82. package/test/decorators.pyj +77 -77
  83. package/test/docstrings.pyj +39 -39
  84. package/test/elementmaker_test.pyj +45 -45
  85. package/test/enum.pyj +134 -0
  86. package/test/eval_exec.pyj +56 -0
  87. package/test/format.pyj +148 -0
  88. package/test/functions.pyj +151 -151
  89. package/test/generators.pyj +41 -41
  90. package/test/generic.pyj +370 -370
  91. package/test/imports.pyj +72 -72
  92. package/test/internationalization.pyj +73 -73
  93. package/test/lint.pyj +164 -164
  94. package/test/loops.pyj +85 -85
  95. package/test/numpy.pyj +734 -734
  96. package/test/object.pyj +64 -0
  97. package/test/omit_function_metadata.pyj +20 -20
  98. package/test/python_compat.pyj +17 -15
  99. package/test/python_features.pyj +70 -15
  100. package/test/regexp.pyj +83 -55
  101. package/test/repl.pyj +121 -121
  102. package/test/scoped_flags.pyj +76 -76
  103. package/test/tuples.pyj +96 -0
  104. package/test/typing.pyj +469 -0
  105. package/test/unit/index.js +116 -7
  106. package/test/unit/language-service-dts.js +543 -543
  107. package/test/unit/language-service-hover.js +455 -455
  108. package/test/unit/language-service.js +84 -0
  109. package/test/unit/web-repl.js +804 -1
  110. package/test/vars_locals_globals.pyj +94 -0
  111. package/tools/cli.js +558 -547
  112. package/tools/compile.js +224 -219
  113. package/tools/completer.js +131 -131
  114. package/tools/embedded_compiler.js +262 -251
  115. package/tools/gettext.js +185 -185
  116. package/tools/ini.js +65 -65
  117. package/tools/lint.js +16 -19
  118. package/tools/msgfmt.js +187 -187
  119. package/tools/repl.js +223 -223
  120. package/tools/test.js +118 -118
  121. package/tools/utils.js +128 -128
  122. package/tools/web_repl.js +95 -95
  123. package/try +41 -41
  124. package/web-repl/env.js +196 -196
  125. package/web-repl/index.html +163 -163
  126. package/web-repl/main.js +252 -252
  127. package/web-repl/prism.css +139 -139
  128. package/web-repl/prism.js +113 -113
  129. package/web-repl/rapydscript.js +224 -224
  130. package/web-repl/sha1.js +25 -25
  131. package/PYTHON_DIFFERENCES_REPORT.md +0 -291
  132. package/PYTHON_FEATURE_COVERAGE.md +0 -200
@@ -798,7 +798,7 @@ var TESTS = [
798
798
  var repl = RS.web_repl();
799
799
  var js = bundle_compile(repl, [
800
800
  "from react import forwardRef",
801
- "def render(props, ref): return props.x",
801
+ "def render(props, ref): return props['x']",
802
802
  "FancyInput = forwardRef(render)",
803
803
  "assrt.equal(jstype(FancyInput), 'function')",
804
804
  "assrt.equal(FancyInput({'x': 99}), 99)",
@@ -864,6 +864,136 @@ var TESTS = [
864
864
  },
865
865
  },
866
866
 
867
+ // ── bytes / bytearray ────────────────────────────────────────────────────
868
+
869
+ {
870
+ name: "bundle_bytes_basic",
871
+ description: "bytes() construction and basic operations work in bundled baselib",
872
+ run: function () {
873
+ var repl = RS.web_repl();
874
+ var js = bundle_compile(repl, [
875
+ "b = bytes([72, 101, 108, 108, 111])",
876
+ "assrt.equal(len(b), 5)",
877
+ "assrt.equal(b[0], 72)",
878
+ "assrt.equal(b[-1], 111)",
879
+ "assrt.ok(isinstance(b, bytes))",
880
+ ].join("\n"));
881
+ run_js(js);
882
+ },
883
+ },
884
+
885
+ {
886
+ name: "bundle_bytes_from_string",
887
+ description: "bytes(str, encoding) encodes a string to UTF-8 bytes in bundled baselib",
888
+ run: function () {
889
+ var repl = RS.web_repl();
890
+ var js = bundle_compile(repl, [
891
+ "b = bytes('Hello', 'utf-8')",
892
+ "assrt.equal(len(b), 5)",
893
+ "assrt.equal(b[0], 72)",
894
+ "assrt.equal(b.decode('utf-8'), 'Hello')",
895
+ ].join("\n"));
896
+ run_js(js);
897
+ },
898
+ },
899
+
900
+ {
901
+ name: "bundle_bytes_hex",
902
+ description: "bytes.hex() and bytes.fromhex() round-trip in bundled baselib",
903
+ run: function () {
904
+ var repl = RS.web_repl();
905
+ var js = bundle_compile(repl, [
906
+ "b = bytes([0, 15, 255])",
907
+ "assrt.equal(b.hex(), '000fff')",
908
+ "b2 = bytes.fromhex('000fff')",
909
+ "assrt.ok(b == b2)",
910
+ ].join("\n"));
911
+ run_js(js);
912
+ },
913
+ },
914
+
915
+ {
916
+ name: "bundle_bytes_slice",
917
+ description: "bytes slice returns bytes in bundled baselib",
918
+ run: function () {
919
+ var repl = RS.web_repl();
920
+ var js = bundle_compile(repl, [
921
+ "b = bytes([10, 20, 30, 40, 50])",
922
+ "s = b[1:4]",
923
+ "assrt.ok(isinstance(s, bytes))",
924
+ "assrt.equal(len(s), 3)",
925
+ "assrt.equal(s[0], 20)",
926
+ "assrt.equal(s[2], 40)",
927
+ ].join("\n"));
928
+ run_js(js);
929
+ },
930
+ },
931
+
932
+ {
933
+ name: "bundle_bytearray_mutation",
934
+ description: "bytearray append/extend/pop work in bundled baselib",
935
+ run: function () {
936
+ var repl = RS.web_repl();
937
+ var js = bundle_compile(repl, [
938
+ "ba = bytearray([1, 2, 3])",
939
+ "assrt.ok(isinstance(ba, bytearray))",
940
+ "ba.append(4)",
941
+ "assrt.equal(len(ba), 4)",
942
+ "assrt.equal(ba[3], 4)",
943
+ "p = ba.pop()",
944
+ "assrt.equal(p, 4)",
945
+ "assrt.equal(len(ba), 3)",
946
+ ].join("\n"));
947
+ run_js(js);
948
+ },
949
+ },
950
+
951
+ {
952
+ name: "bundle_bytes_repr",
953
+ description: "repr(bytes(...)) returns b'...' notation in bundled baselib",
954
+ run: function () {
955
+ var repl = RS.web_repl();
956
+ var js = bundle_compile(repl, [
957
+ "b = bytes([72, 101, 108, 108, 111])",
958
+ "assrt.equal(repr(b), \"b'Hello'\")",
959
+ ].join("\n"));
960
+ run_js(js);
961
+ },
962
+ },
963
+
964
+ {
965
+ name: "bundle_bytes_literal_syntax",
966
+ description: "b'...' bytes literal compiles and runs correctly in bundled compiler",
967
+ run: function () {
968
+ var repl = RS.web_repl();
969
+ var js = bundle_compile(repl, [
970
+ "b = b'Hello'",
971
+ "assrt.ok(isinstance(b, bytes))",
972
+ "assrt.equal(len(b), 5)",
973
+ "assrt.equal(b[0], 72)",
974
+ "assrt.equal(b[4], 111)",
975
+ "assrt.ok(b == bytes([72, 101, 108, 108, 111]))",
976
+ "# hex escapes",
977
+ "b2 = b'\\x48\\x65\\x6c\\x6c\\x6f'",
978
+ "assrt.ok(b2 == b)",
979
+ "# empty literal",
980
+ "e = b''",
981
+ "assrt.equal(len(e), 0)",
982
+ "# adjacent literal concatenation",
983
+ "c = b'foo' b'bar'",
984
+ "assrt.equal(len(c), 6)",
985
+ "assrt.ok(c == bytes([102, 111, 111, 98, 97, 114]))",
986
+ "# uppercase B prefix",
987
+ "u = B'ABC'",
988
+ "assrt.ok(isinstance(u, bytes))",
989
+ "assrt.equal(u[0], 65)",
990
+ "# repr shows b'...' notation",
991
+ "assrt.equal(repr(b'Hi'), \"b'Hi'\")",
992
+ ].join("\n"));
993
+ run_js(js);
994
+ },
995
+ },
996
+
867
997
  {
868
998
  name: "bundle___import__-error",
869
999
  description: "__import__ raises ModuleNotFoundError for unknown module in web-repl",
@@ -882,6 +1012,679 @@ var TESTS = [
882
1012
  },
883
1013
  },
884
1014
 
1015
+ // ── Enum ─────────────────────────────────────────────────────────────────
1016
+
1017
+ {
1018
+ name: "bundle_enum_basic",
1019
+ description: "Enum subclass .name and .value attributes work in bundled baselib",
1020
+ run: function () {
1021
+ var repl = RS.web_repl();
1022
+ var js = bundle_compile(repl, [
1023
+ "from enum import Enum",
1024
+ "class Color(Enum):",
1025
+ " RED = 1",
1026
+ " GREEN = 2",
1027
+ " BLUE = 3",
1028
+ "assrt.equal(Color.RED.name, 'RED')",
1029
+ "assrt.equal(Color.RED.value, 1)",
1030
+ "assrt.equal(Color.GREEN.name, 'GREEN')",
1031
+ "assrt.equal(Color.GREEN.value, 2)",
1032
+ "assrt.equal(Color.BLUE.name, 'BLUE')",
1033
+ "assrt.equal(Color.BLUE.value, 3)",
1034
+ ].join("\n"));
1035
+ run_js(js);
1036
+ },
1037
+ },
1038
+
1039
+ {
1040
+ name: "bundle_enum_identity",
1041
+ description: "Enum members are singletons — Color.RED is Color.RED",
1042
+ run: function () {
1043
+ var repl = RS.web_repl();
1044
+ var js = bundle_compile(repl, [
1045
+ "from enum import Enum",
1046
+ "class Color(Enum):",
1047
+ " RED = 1",
1048
+ " GREEN = 2",
1049
+ "assrt.ok(Color.RED is Color.RED)",
1050
+ "assrt.ok(Color.RED is not Color.GREEN)",
1051
+ ].join("\n"));
1052
+ run_js(js);
1053
+ },
1054
+ },
1055
+
1056
+ {
1057
+ name: "bundle_enum_iteration",
1058
+ description: "list(Color) and for-loop over an Enum class yield all members in order",
1059
+ run: function () {
1060
+ var repl = RS.web_repl();
1061
+ var js = bundle_compile(repl, [
1062
+ "from enum import Enum",
1063
+ "class Color(Enum):",
1064
+ " RED = 1",
1065
+ " GREEN = 2",
1066
+ " BLUE = 3",
1067
+ "members = list(Color)",
1068
+ "assrt.equal(len(members), 3)",
1069
+ "assrt.equal(members[0].name, 'RED')",
1070
+ "assrt.equal(members[1].name, 'GREEN')",
1071
+ "assrt.equal(members[2].name, 'BLUE')",
1072
+ "names = []",
1073
+ "for m in Color:",
1074
+ " names.push(m.name)",
1075
+ "assrt.deepEqual(names, ['RED', 'GREEN', 'BLUE'])",
1076
+ ].join("\n"));
1077
+ run_js(js);
1078
+ },
1079
+ },
1080
+
1081
+ {
1082
+ name: "bundle_enum_isinstance",
1083
+ description: "isinstance(Color.RED, Color) and isinstance(Color.RED, Enum) both return True",
1084
+ run: function () {
1085
+ var repl = RS.web_repl();
1086
+ var js = bundle_compile(repl, [
1087
+ "from enum import Enum",
1088
+ "class Color(Enum):",
1089
+ " RED = 1",
1090
+ "assrt.ok(isinstance(Color.RED, Color))",
1091
+ "assrt.ok(isinstance(Color.RED, Enum))",
1092
+ "assrt.ok(not isinstance(1, Color))",
1093
+ ].join("\n"));
1094
+ run_js(js);
1095
+ },
1096
+ },
1097
+
1098
+ {
1099
+ name: "bundle_enum_repr_str",
1100
+ description: "repr() and str() of an enum member produce the expected strings",
1101
+ run: function () {
1102
+ var repl = RS.web_repl();
1103
+ var js = bundle_compile(repl, [
1104
+ "from enum import Enum",
1105
+ "class Color(Enum):",
1106
+ " RED = 1",
1107
+ "assrt.equal(repr(Color.RED), '<Color.RED: 1>')",
1108
+ "assrt.equal(str(Color.RED), 'Color.RED')",
1109
+ ].join("\n"));
1110
+ run_js(js);
1111
+ },
1112
+ },
1113
+
1114
+ {
1115
+ name: "bundle_dataclasses_basic",
1116
+ description: "@dataclass generates __init__, __repr__, __eq__ in bundled baselib",
1117
+ run: function () {
1118
+ var repl = RS.web_repl();
1119
+ var js = bundle_compile(repl, [
1120
+ "from dataclasses import dataclass, field, is_dataclass",
1121
+ "@dataclass",
1122
+ "class Point:",
1123
+ " x: int",
1124
+ " y: int = 0",
1125
+ "p = Point(3, 4)",
1126
+ "assrt.equal(p.x, 3)",
1127
+ "assrt.equal(p.y, 4)",
1128
+ "assrt.ok(p == Point(3, 4))",
1129
+ "assrt.ok(p is not Point(3, 4))",
1130
+ "assrt.equal(repr(p), 'Point(x=3, y=4)')",
1131
+ "assrt.ok(is_dataclass(p))",
1132
+ "assrt.ok(is_dataclass(Point))",
1133
+ ].join("\n"));
1134
+ run_js(js);
1135
+ },
1136
+ },
1137
+
1138
+ {
1139
+ name: "bundle_dataclasses_field_factory",
1140
+ description: "field(default_factory=...) gives each instance its own mutable default",
1141
+ run: function () {
1142
+ var repl = RS.web_repl();
1143
+ var js = bundle_compile(repl, [
1144
+ "from dataclasses import dataclass, field",
1145
+ "@dataclass",
1146
+ "class Container:",
1147
+ " items: list = field(default_factory=list)",
1148
+ "a = Container()",
1149
+ "b = Container()",
1150
+ "a.items.push(1)",
1151
+ "assrt.equal(a.items.length, 1)",
1152
+ "assrt.equal(b.items.length, 0)",
1153
+ ].join("\n"));
1154
+ run_js(js);
1155
+ },
1156
+ },
1157
+
1158
+ {
1159
+ name: "bundle_dataclasses_asdict",
1160
+ description: "asdict() recursively converts a dataclass instance to a plain dict",
1161
+ run: function () {
1162
+ var repl = RS.web_repl();
1163
+ var js = bundle_compile(repl, [
1164
+ "from dataclasses import dataclass, asdict",
1165
+ "@dataclass",
1166
+ "class Inner:",
1167
+ " value: int",
1168
+ "@dataclass",
1169
+ "class Outer:",
1170
+ " inner: object",
1171
+ " tag: str",
1172
+ "d = asdict(Outer(Inner(42), 'hello'))",
1173
+ "assrt.equal(d['tag'], 'hello')",
1174
+ "assrt.equal(d['inner']['value'], 42)",
1175
+ ].join("\n"));
1176
+ run_js(js);
1177
+ },
1178
+ },
1179
+
1180
+ {
1181
+ name: "bundle_dataclasses_frozen",
1182
+ description: "frozen=True makes fields read-only (writable:false, assignment silently ignored outside strict mode)",
1183
+ run: function () {
1184
+ var repl = RS.web_repl();
1185
+ var js = bundle_compile(repl, [
1186
+ "from dataclasses import dataclass",
1187
+ "@dataclass(frozen=True)",
1188
+ "class FP:",
1189
+ " x: int",
1190
+ " y: int",
1191
+ "fp = FP(1, 2)",
1192
+ "assrt.equal(fp.x, 1)",
1193
+ "assrt.equal(fp.y, 2)",
1194
+ "fp.x = 99",
1195
+ // In non-strict mode, writable:false assignment is silently ignored
1196
+ "assrt.equal(fp.x, 1)",
1197
+ ].join("\n"));
1198
+ run_js(js);
1199
+ },
1200
+ },
1201
+
1202
+ {
1203
+ name: "bundle_format_dunder",
1204
+ description: "__format__ dunder dispatches from format(), str.format(), and f-strings in web-repl bundle",
1205
+ run: function () {
1206
+ var repl = RS.web_repl();
1207
+ var js = bundle_compile(repl, [
1208
+ "class Money:",
1209
+ " def __init__(self, amount):",
1210
+ " self.amount = amount",
1211
+ " def __str__(self):",
1212
+ " return str(self.amount)",
1213
+ " def __format__(self, spec):",
1214
+ " if spec == 'usd':",
1215
+ " return '$' + str(self.amount)",
1216
+ " return format(self.amount, spec)",
1217
+ "m = Money(42)",
1218
+ "assrt.equal(format(m, 'usd'), '$42')",
1219
+ "assrt.equal(str.format('{:usd}', m), '$42')",
1220
+ "assrt.equal(f'{m:usd}', '$42')",
1221
+ "assrt.equal(format(m, '.2f'), '42.00')",
1222
+ "assrt.equal(f'{m:.2f}', '42.00')",
1223
+ // Default __format__ (no custom __format__ defined)
1224
+ "class Plain:",
1225
+ " def __str__(self):",
1226
+ " return 'plain'",
1227
+ "p = Plain()",
1228
+ "assrt.equal(format(p), 'plain')",
1229
+ "assrt.equal(str.format('{}', p), 'plain')",
1230
+ "assrt.equal(f'{p}', 'plain')",
1231
+ ].join("\n"));
1232
+ run_js(js);
1233
+ },
1234
+ },
1235
+
1236
+ // ── object() builtin ──────────────────────────────────────────────────────
1237
+
1238
+ {
1239
+ name: "bundle_object_sentinel",
1240
+ description: "object() returns distinct instances usable as sentinels",
1241
+ run: function () {
1242
+ var repl = RS.web_repl();
1243
+ var js = bundle_compile(repl, [
1244
+ "s1 = object()",
1245
+ "s2 = object()",
1246
+ "assrt.ok(s1 is not s2)",
1247
+ "assrt.ok(isinstance(s1, object))",
1248
+ "assrt.ok(isinstance(s2, object))",
1249
+ "h1 = hash(s1)",
1250
+ "h2 = hash(s2)",
1251
+ "assrt.ok(h1 is not h2)",
1252
+ "assrt.equal(hash(s1), h1)",
1253
+ ].join("\n"));
1254
+ run_js(js);
1255
+ },
1256
+ },
1257
+
1258
+ {
1259
+ name: "bundle_object_subclass",
1260
+ description: "class Foo(object) works and isinstance checks succeed",
1261
+ run: function () {
1262
+ var repl = RS.web_repl();
1263
+ var js = bundle_compile(repl, [
1264
+ "class Node(object):",
1265
+ " def __init__(self, val):",
1266
+ " self.val = val",
1267
+ "n = Node(42)",
1268
+ "assrt.ok(isinstance(n, Node))",
1269
+ "assrt.ok(isinstance(n, object))",
1270
+ "assrt.equal(n.val, 42)",
1271
+ ].join("\n"));
1272
+ run_js(js);
1273
+ },
1274
+ },
1275
+
1276
+ // ── float.is_integer() ───────────────────────────────────────────────────
1277
+
1278
+ {
1279
+ name: "bundle_float_is_integer",
1280
+ description: "float.is_integer() returns True for whole numbers and False for fractions/inf/nan",
1281
+ run: function () {
1282
+ var repl = RS.web_repl();
1283
+ var js = bundle_compile(repl, [
1284
+ "assrt.equal((1.0).is_integer(), True)",
1285
+ "assrt.equal((1.5).is_integer(), False)",
1286
+ "assrt.equal((0.0).is_integer(), True)",
1287
+ "assrt.equal((-2.0).is_integer(), True)",
1288
+ "assrt.equal((-2.5).is_integer(), False)",
1289
+ "assrt.equal((1e10).is_integer(), True)",
1290
+ "assrt.equal(v'Infinity'.is_integer(), False)",
1291
+ "assrt.equal(v'NaN'.is_integer(), False)",
1292
+ ].join("\n"));
1293
+ run_js(js);
1294
+ },
1295
+ },
1296
+
1297
+ // ── int.bit_length() ─────────────────────────────────────────────────────
1298
+
1299
+ {
1300
+ name: "bundle_int_bit_length",
1301
+ description: "int.bit_length() returns the number of bits needed to represent the integer",
1302
+ run: function () {
1303
+ var repl = RS.web_repl();
1304
+ var js = bundle_compile(repl, [
1305
+ "assrt.equal((0).bit_length(), 0)",
1306
+ "assrt.equal((1).bit_length(), 1)",
1307
+ "assrt.equal((255).bit_length(), 8)",
1308
+ "assrt.equal((256).bit_length(), 9)",
1309
+ "assrt.equal((1023).bit_length(), 10)",
1310
+ "assrt.equal((1024).bit_length(), 11)",
1311
+ "assrt.equal((-1).bit_length(), 1)",
1312
+ "assrt.equal((-5).bit_length(), 3)",
1313
+ "assrt.equal((-255).bit_length(), 8)",
1314
+ ].join("\n"));
1315
+ run_js(js);
1316
+ },
1317
+ },
1318
+
1319
+ // ── abc module ───────────────────────────────────────────────────────────
1320
+
1321
+ {
1322
+ name: "bundle_abc_basic",
1323
+ description: "ABC + @abstractmethod raises TypeError when abstract class is instantiated",
1324
+ run: function () {
1325
+ var repl = RS.web_repl();
1326
+ var js = bundle_compile(repl, [
1327
+ "from abc import ABC, abstractmethod",
1328
+ "class Shape(ABC):",
1329
+ " @abstractmethod",
1330
+ " def area(self): pass",
1331
+ "raised = False",
1332
+ "try:",
1333
+ " Shape()",
1334
+ "except TypeError:",
1335
+ " raised = True",
1336
+ "assrt.ok(raised)",
1337
+ "assrt.ok('area' in Shape.__abstractmethods__)",
1338
+ "assrt.equal(len(Shape.__abstractmethods__), 1)",
1339
+ ].join("\n"));
1340
+ run_js(js);
1341
+ },
1342
+ },
1343
+
1344
+ {
1345
+ name: "bundle_abc_concrete",
1346
+ description: "Concrete ABC subclass can be instantiated; isinstance sees the full chain",
1347
+ run: function () {
1348
+ var repl = RS.web_repl();
1349
+ var js = bundle_compile(repl, [
1350
+ "from abc import ABC, abstractmethod",
1351
+ "class Shape(ABC):",
1352
+ " @abstractmethod",
1353
+ " def area(self): pass",
1354
+ "class Circle(Shape):",
1355
+ " def __init__(self, r):",
1356
+ " self.r = r",
1357
+ " def area(self):",
1358
+ " return 3.14159 * self.r * self.r",
1359
+ "c = Circle(5)",
1360
+ "assrt.ok(isinstance(c, Circle))",
1361
+ "assrt.ok(isinstance(c, Shape))",
1362
+ "assrt.ok(isinstance(c, ABC))",
1363
+ "assrt.equal(c.r, 5)",
1364
+ "assrt.ok(abs(c.area() - 78.53975) < 0.001)",
1365
+ "assrt.equal(len(Circle.__abstractmethods__), 0)",
1366
+ ].join("\n"));
1367
+ run_js(js);
1368
+ },
1369
+ },
1370
+
1371
+ {
1372
+ name: "bundle_abc_register",
1373
+ description: "ABC.register() makes isinstance return True for virtual subclasses",
1374
+ run: function () {
1375
+ var repl = RS.web_repl();
1376
+ var js = bundle_compile(repl, [
1377
+ "from abc import ABC, abstractmethod",
1378
+ "class MyABC(ABC):",
1379
+ " @abstractmethod",
1380
+ " def do_it(self): pass",
1381
+ "class External:",
1382
+ " def do_it(self): return 42",
1383
+ "MyABC.register(External)",
1384
+ "assrt.ok(isinstance(External(), MyABC))",
1385
+ "class Unrelated:",
1386
+ " def do_it(self): return 99",
1387
+ "assrt.ok(not isinstance(Unrelated(), MyABC))",
1388
+ ].join("\n"));
1389
+ run_js(js);
1390
+ },
1391
+ },
1392
+
1393
+ {
1394
+ name: "bundle_abc_protocol",
1395
+ description: "@runtime_checkable Protocol enables structural isinstance() checks",
1396
+ run: function () {
1397
+ var repl = RS.web_repl();
1398
+ var js = bundle_compile(repl, [
1399
+ "from abc import Protocol, runtime_checkable",
1400
+ "@runtime_checkable",
1401
+ "class Drawable(Protocol):",
1402
+ " def draw(self): pass",
1403
+ "class Canvas:",
1404
+ " def draw(self):",
1405
+ " return 'painting'",
1406
+ "class NotDrawable:",
1407
+ " def paint(self): pass",
1408
+ "assrt.ok(isinstance(Canvas(), Drawable))",
1409
+ "assrt.ok(not isinstance(NotDrawable(), Drawable))",
1410
+ "assrt.ok(not isinstance(42, Drawable))",
1411
+ "assrt.ok('draw' in Drawable.__protocol_attrs__)",
1412
+ ].join("\n"));
1413
+ run_js(js);
1414
+ },
1415
+ },
1416
+
1417
+ // ── eval / exec ───────────────────────────────────────────────────────
1418
+
1419
+ {
1420
+ name: "bundle_zip_strict",
1421
+ description: "zip(strict=True) raises ValueError on length mismatch in the web-repl bundle",
1422
+ run: function () {
1423
+ var repl = RS.web_repl();
1424
+ var js = bundle_compile(repl, [
1425
+ // equal-length: no error
1426
+ "assrt.deepEqual(list(zip([1,2], [3,4], strict=True)), [[1,3],[2,4]])",
1427
+ // zero iterables with strict: empty result, no error
1428
+ "assrt.deepEqual(list(zip(strict=True)), [])",
1429
+ // first longer than second
1430
+ "err1 = False",
1431
+ "try:",
1432
+ " list(zip([1,2], [3], strict=True))",
1433
+ "except ValueError:",
1434
+ " err1 = True",
1435
+ "assrt.ok(err1)",
1436
+ // second longer than first
1437
+ "err2 = False",
1438
+ "try:",
1439
+ " list(zip([1], [3,4], strict=True))",
1440
+ "except ValueError:",
1441
+ " err2 = True",
1442
+ "assrt.ok(err2)",
1443
+ ].join("\n"));
1444
+ run_js(js);
1445
+ },
1446
+ },
1447
+
1448
+ {
1449
+ name: "bundle_eval_exec",
1450
+ description: "eval and exec builtins work in the web-repl bundle",
1451
+ run: function () {
1452
+ var repl = RS.web_repl();
1453
+ var js = bundle_compile(repl, [
1454
+ // eval — basic expression
1455
+ "assrt.equal(eval('1 + 2'), 3)",
1456
+ "assrt.equal(eval('10 * 5'), 50)",
1457
+ "assrt.equal(eval('True'), True)",
1458
+ // eval with globals dict
1459
+ "assrt.equal(eval('x + y', {'x': 10, 'y': 5}), 15)",
1460
+ "assrt.equal(eval('a * b', {'a': 3, 'b': 4}), 12)",
1461
+ // eval with locals overriding globals
1462
+ "assrt.equal(eval('x', {'x': 1}, {'x': 99}), 99)",
1463
+ // exec returns None (null in JS)
1464
+ "assrt.equal(exec('1 + 2'), None)",
1465
+ // exec side-effects via mutable object in globals
1466
+ "log = []",
1467
+ "exec(\"log.push('hello')\", {'log': log})",
1468
+ "assrt.equal(log[0], 'hello')",
1469
+ "exec('log.push(1 + 2)', {'log': log})",
1470
+ "assrt.equal(log[1], 3)",
1471
+ // exec with function in globals
1472
+ "out = []",
1473
+ "def _add(a, b): out.push(a + b);",
1474
+ "exec('fn(10, 7)', {'fn': _add, 'out': out})",
1475
+ "assrt.equal(out[0], 17)",
1476
+ ].join("\n"));
1477
+ run_js(js);
1478
+ },
1479
+ },
1480
+
1481
+ // ── complex numbers ───────────────────────────────────────────────────────
1482
+
1483
+ {
1484
+ name: "bundle_complex_constructor",
1485
+ description: "complex() constructor and .real/.imag attributes work in bundled baselib",
1486
+ run: function () {
1487
+ var repl = RS.web_repl();
1488
+ var js = bundle_compile(repl, [
1489
+ "c = complex(3, 4)",
1490
+ "assrt.equal(c.real, 3)",
1491
+ "assrt.equal(c.imag, 4)",
1492
+ "c0 = complex()",
1493
+ "assrt.equal(c0.real, 0)",
1494
+ "assrt.equal(c0.imag, 0)",
1495
+ "c1 = complex(5)",
1496
+ "assrt.equal(c1.real, 5)",
1497
+ "assrt.equal(c1.imag, 0)",
1498
+ ].join("\n"));
1499
+ run_js(js);
1500
+ },
1501
+ },
1502
+
1503
+ {
1504
+ name: "bundle_complex_j_literal",
1505
+ description: "j-suffix imaginary literal compiles and runs correctly in bundled compiler",
1506
+ run: function () {
1507
+ var repl = RS.web_repl();
1508
+ var js = bundle_compile(repl, [
1509
+ "c = 4j",
1510
+ "assrt.equal(c.real, 0)",
1511
+ "assrt.equal(c.imag, 4)",
1512
+ "c2 = 3+4j",
1513
+ "assrt.equal(c2.real, 3)",
1514
+ "assrt.equal(c2.imag, 4)",
1515
+ "assrt.ok(isinstance(c, complex))",
1516
+ ].join("\n"));
1517
+ run_js(js);
1518
+ },
1519
+ },
1520
+
1521
+ {
1522
+ name: "bundle_complex_arithmetic",
1523
+ description: "complex arithmetic (+, -, *, /) and abs() work in bundled baselib",
1524
+ run: function () {
1525
+ var repl = RS.web_repl();
1526
+ var js = bundle_compile(repl, [
1527
+ "a = complex(1, 2)",
1528
+ "b = complex(3, 4)",
1529
+ "assrt.ok(a + b == complex(4, 6))",
1530
+ "assrt.ok(b - a == complex(2, 2))",
1531
+ "assrt.ok(a * b == complex(-5, 10))",
1532
+ "assrt.ok(b / complex(1, 0) == b)",
1533
+ "assrt.equal(abs(complex(3, 4)), 5)",
1534
+ "assrt.ok(-complex(3, 4) == complex(-3, -4))",
1535
+ "assrt.ok(complex(3, 4).conjugate() == complex(3, -4))",
1536
+ ].join("\n"));
1537
+ run_js(js);
1538
+ },
1539
+ },
1540
+
1541
+ {
1542
+ name: "bundle_complex_repr",
1543
+ description: "repr() and str() of complex produce Python-style notation in bundled baselib",
1544
+ run: function () {
1545
+ var repl = RS.web_repl();
1546
+ var js = bundle_compile(repl, [
1547
+ "assrt.equal(repr(complex(0, 0)), '0j')",
1548
+ "assrt.equal(repr(complex(1, 0)), '(1+0j)')",
1549
+ "assrt.equal(repr(complex(0, 1)), '1j')",
1550
+ "assrt.equal(repr(complex(3, 4)), '(3+4j)')",
1551
+ "assrt.equal(repr(complex(3, -4)), '(3-4j)')",
1552
+ "assrt.equal(str(complex(3, 4)), '(3+4j)')",
1553
+ ].join("\n"));
1554
+ run_js(js);
1555
+ },
1556
+ },
1557
+
1558
+ {
1559
+ name: "bundle_vars_locals_globals",
1560
+ description: "vars(), locals(), globals() work in bundled baselib",
1561
+ run: function () {
1562
+ var repl = RS.web_repl();
1563
+ var js = bundle_compile(repl, [
1564
+ "from __python__ import overload_getitem",
1565
+ "class Point:",
1566
+ " def __init__(self, x, y):",
1567
+ " self.x = x",
1568
+ " self.y = y",
1569
+ "p = Point(7, 9)",
1570
+ "d = vars(p)",
1571
+ "assrt.equal(d['x'], 7)",
1572
+ "assrt.equal(d['y'], 9)",
1573
+ // snapshot — mutation does not affect original
1574
+ "d['x'] = 0",
1575
+ "assrt.equal(p.x, 7)",
1576
+ // vars() with no arg returns empty dict
1577
+ "v = vars()",
1578
+ "assrt.ok(v is not None)",
1579
+ "assrt.ok(isinstance(v, dict))",
1580
+ // locals() returns empty dict
1581
+ "loc = locals()",
1582
+ "assrt.ok(isinstance(loc, dict))",
1583
+ // globals() returns dict
1584
+ "g = globals()",
1585
+ "assrt.ok(isinstance(g, dict))",
1586
+ // vars(obj) returns dict instance
1587
+ "assrt.ok(isinstance(vars(Point(1, 2)), dict))",
1588
+ ].join("\n"));
1589
+ run_js(js);
1590
+ },
1591
+ },
1592
+
1593
+ // -------------------------------------------------------------------------
1594
+ // Context-persistence tests: compile WITHOUT keep_baselib, run in the repl
1595
+ // vm context. These catch "ReferenceError: ρσ_X is not defined" bugs that
1596
+ // only surface when baselib symbols are `let`-declared (not `var` / function
1597
+ // declarations) and therefore don't persist between vm.runInContext calls.
1598
+ // -------------------------------------------------------------------------
1599
+
1600
+ {
1601
+ name: "repl_in_operator_persistence",
1602
+ description: "ρσ_in accessible after baselib init — 'in' operator regression in web-repl context",
1603
+ run: function () {
1604
+ var repl = RS.web_repl();
1605
+ // Compile WITHOUT keep_baselib so the output does not include the
1606
+ // baselib — the compiled code references ρσ_in from the persistent ctx.
1607
+ var js = repl.compile([
1608
+ "assert 'x' in ['x', 'y', 'z'], \"'x' in list should be True\"",
1609
+ "assert 'a' not in ['x', 'y', 'z'], \"'a' not in list should be True\"",
1610
+ "assert 2 in {1: 'one', 2: 'two'}, '2 in dict should be True'",
1611
+ // Original failing case: 'Math' in globals() must not throw
1612
+ "g = globals()",
1613
+ "result = 'Math' in g", // just must not throw ReferenceError
1614
+ ].join("\n"), {export_main: true, tree_shake: false});
1615
+ repl.runjs(js);
1616
+ },
1617
+ },
1618
+
1619
+ {
1620
+ name: "repl_kwargs_persistence",
1621
+ description: "ρσ_desugar_kwargs accessible after baselib init — **kwargs in web-repl context",
1622
+ run: function () {
1623
+ var repl = RS.web_repl();
1624
+ var js = repl.compile([
1625
+ "def greet(name, greeting='Hello'):",
1626
+ " return greeting + ', ' + name + '!'",
1627
+ "result = greet('World', greeting='Hi')",
1628
+ "assert result == 'Hi, World!', 'kwargs call should produce Hi, World!'",
1629
+ ].join("\n"), {export_main: true, tree_shake: false});
1630
+ repl.runjs(js);
1631
+ },
1632
+ },
1633
+
1634
+ {
1635
+ name: "repl_nameerror_persistence",
1636
+ description: "NameError accessible after baselib init — except NameError in web-repl context",
1637
+ run: function () {
1638
+ var repl = RS.web_repl();
1639
+ var js = repl.compile([
1640
+ "caught = False",
1641
+ "try:",
1642
+ " raise NameError('test error')",
1643
+ "except NameError:",
1644
+ " caught = True",
1645
+ "assert caught, 'NameError should be catchable in except clause'",
1646
+ ].join("\n"), {export_main: true, tree_shake: false});
1647
+ repl.runjs(js);
1648
+ },
1649
+ },
1650
+
1651
+ {
1652
+ name: "repl_getattr_persistence",
1653
+ description: "ρσ_JS_Proxy/ρσ_attr_proxy_handler accessible after baselib init — __getattr__ in web-repl context",
1654
+ run: function () {
1655
+ var repl = RS.web_repl();
1656
+ var js = repl.compile([
1657
+ "class Magic:",
1658
+ " def __getattr__(self, name):",
1659
+ " return name + '_value'",
1660
+ "m = Magic()",
1661
+ "assert m.foo == 'foo_value', '__getattr__ should return foo_value'",
1662
+ "assert m.bar == 'bar_value', '__getattr__ should return bar_value'",
1663
+ ].join("\n"), {export_main: true, tree_shake: false});
1664
+ repl.runjs(js);
1665
+ },
1666
+ },
1667
+
1668
+ {
1669
+ name: "repl_exists_persistence",
1670
+ description: "ρσ_exists accessible after baselib init — existential operator on non-SymbolRef in web-repl context",
1671
+ run: function () {
1672
+ var repl = RS.web_repl();
1673
+ var js = repl.compile([
1674
+ // fns['key'] is a subscript (not SymbolRef) — emits ρσ_exists.c(fns['key'])()
1675
+ "def get_val(): return 42",
1676
+ "fns = {'key': get_val}",
1677
+ "result = fns['key']?()",
1678
+ "assert result == 42, 'existential call via subscript should return 42'",
1679
+ // None value: dict lookup miss returns None, existential function call returns undefined
1680
+ "missing = fns.get('nokey')",
1681
+ "result2 = missing?()",
1682
+ "assert result2 is undefined, 'existential call on None should return undefined'",
1683
+ ].join("\n"), {export_main: true, tree_shake: false});
1684
+ repl.runjs(js);
1685
+ },
1686
+ },
1687
+
885
1688
  ];
886
1689
 
887
1690
  // ---------------------------------------------------------------------------