Commit a976a99
committed
fix(hir/codegen): cross-module class + array-method dispatch + Date instanceof
While porting Mango (a Perry app) to use the pure-TypeScript
`@perryts/mongodb` driver, several long-latent dispatch holes surfaced
once a real cross-module class graph was being compiled. Each is fixed
at the level it manifests rather than worked around in user code.
- compile.rs: honour a `"perry"` exports condition in `package.json`
so packages can ship TS source (`./src/index.ts`) for Perry-native
compilation alongside a pre-built JS dist for Node/Bun consumers.
- compile.rs + codegen.rs: imported classes now carry
`static_method_names`, populated from the source HIR's
`class.static_methods`. Codegen registers each entry in
`method_names` keyed by `(effective_name, sm)` so
`MyClass.connect(...)` on an imported class resolves to the
source-side `_perry_static_<src>__<MyClass>__<connect>` symbol
instead of falling through to `0.0`. Matching no-op
`__perry_extern_closure_<Class>` wrappers are emitted for class
names so referencing the class as a value (instanceof, eq) doesn't
fail at link time.
- lower.rs (`StaticMethodCall` candidate detection): treat any
uppercase imported identifier as a candidate class so
`Foo.connect(...)` on a cross-module class lowers to
`Expr::StaticMethodCall` and reaches the `method_names` table above.
Without this, the same call lowered to a `Call(PropertyGet(...))`
shape that read garbage off the static `ClosureHeader` global and
crashed.
- lower.rs (array-method fast path): add a class-instance / unknown
receiver guard so `coll.find(filter)`, `xs.map(fn)`, etc. on a
user-class instance no longer collapse to `Expr::ArrayFind` /
`Expr::ArrayMap` and dispatch to `js_array_<method>` on a class
handle — the runtime returned `0` and the smoke ended up calling
`0.toArray()`. An overlapping-method list (find / map / filter /
forEach / reduce / some / every / join …) is the trigger; receivers
whose static type is `Type::Any` plus one of those names now skip
the array fast-path and fall through to method-dispatch resolution.
- js_transform.rs: cross-module pass propagates native-instance
tagging through plain ident-rebinds (`let sock: Socket = plainSock`)
via a fixed-point scan. Without this, an explicit type annotation
re-erased the `(net, Socket)` tag set on the source variable and
the next `sock.on('data', cb)` skipped the native dispatcher and
segfaulted reading `[handle - 8]`.
- date.rs + object.rs: `instanceof Date` previously returned `true`
for every finite f64. Add a thread-local `DATE_REGISTRY` of f64
bit patterns produced by `js_date_new*` and consult it from the
`js_instanceof` Date arm. Restores the typed BSON-encoder
dispatch that was mis-classifying `batchSize: 100` as a Date and
rejecting the wire find command.
destructuring.rs: comment-only — points readers at the cross-module
js_transform pass that subsumes the variable-to-variable native
instance propagation that used to live here.
Tested end to end against MongoDB 7.0 from a perry-native binary:
connect, ping, listDatabases, insertOne, find().toArray(), findOne,
updateOne, deleteOne all roundtrip with correct ObjectId bytes.
Mango (92 modules) still compiles to a 9.4 MB native binary.1 parent bc4462e commit a976a99
7 files changed
Lines changed: 388 additions & 137 deletions
File tree
- crates
- perry-codegen/src
- perry-hir/src
- perry-runtime/src
- perry/src/commands
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
167 | 167 | | |
168 | 168 | | |
169 | 169 | | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
170 | 175 | | |
171 | 176 | | |
172 | 177 | | |
| |||
1009 | 1014 | | |
1010 | 1015 | | |
1011 | 1016 | | |
| 1017 | + | |
| 1018 | + | |
| 1019 | + | |
| 1020 | + | |
| 1021 | + | |
| 1022 | + | |
| 1023 | + | |
| 1024 | + | |
| 1025 | + | |
| 1026 | + | |
| 1027 | + | |
| 1028 | + | |
| 1029 | + | |
| 1030 | + | |
| 1031 | + | |
| 1032 | + | |
| 1033 | + | |
| 1034 | + | |
| 1035 | + | |
| 1036 | + | |
| 1037 | + | |
1012 | 1038 | | |
1013 | 1039 | | |
1014 | 1040 | | |
| |||
1381 | 1407 | | |
1382 | 1408 | | |
1383 | 1409 | | |
| 1410 | + | |
| 1411 | + | |
| 1412 | + | |
| 1413 | + | |
| 1414 | + | |
| 1415 | + | |
| 1416 | + | |
| 1417 | + | |
| 1418 | + | |
| 1419 | + | |
| 1420 | + | |
| 1421 | + | |
| 1422 | + | |
| 1423 | + | |
1384 | 1424 | | |
1385 | 1425 | | |
1386 | 1426 | | |
1387 | 1427 | | |
1388 | 1428 | | |
| 1429 | + | |
1389 | 1430 | | |
1390 | 1431 | | |
1391 | 1432 | | |
1392 | 1433 | | |
1393 | 1434 | | |
| 1435 | + | |
| 1436 | + | |
| 1437 | + | |
| 1438 | + | |
| 1439 | + | |
| 1440 | + | |
| 1441 | + | |
| 1442 | + | |
| 1443 | + | |
| 1444 | + | |
| 1445 | + | |
| 1446 | + | |
| 1447 | + | |
| 1448 | + | |
| 1449 | + | |
| 1450 | + | |
| 1451 | + | |
| 1452 | + | |
| 1453 | + | |
| 1454 | + | |
| 1455 | + | |
| 1456 | + | |
| 1457 | + | |
| 1458 | + | |
| 1459 | + | |
| 1460 | + | |
| 1461 | + | |
1394 | 1462 | | |
1395 | 1463 | | |
1396 | 1464 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1376 | 1376 | | |
1377 | 1377 | | |
1378 | 1378 | | |
| 1379 | + | |
| 1380 | + | |
| 1381 | + | |
| 1382 | + | |
1379 | 1383 | | |
1380 | 1384 | | |
1381 | 1385 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
976 | 976 | | |
977 | 977 | | |
978 | 978 | | |
| 979 | + | |
| 980 | + | |
| 981 | + | |
| 982 | + | |
| 983 | + | |
| 984 | + | |
| 985 | + | |
| 986 | + | |
| 987 | + | |
| 988 | + | |
| 989 | + | |
| 990 | + | |
| 991 | + | |
| 992 | + | |
| 993 | + | |
| 994 | + | |
| 995 | + | |
| 996 | + | |
| 997 | + | |
| 998 | + | |
| 999 | + | |
| 1000 | + | |
| 1001 | + | |
| 1002 | + | |
| 1003 | + | |
| 1004 | + | |
| 1005 | + | |
| 1006 | + | |
| 1007 | + | |
| 1008 | + | |
| 1009 | + | |
| 1010 | + | |
| 1011 | + | |
| 1012 | + | |
| 1013 | + | |
| 1014 | + | |
| 1015 | + | |
| 1016 | + | |
| 1017 | + | |
| 1018 | + | |
| 1019 | + | |
| 1020 | + | |
| 1021 | + | |
| 1022 | + | |
| 1023 | + | |
| 1024 | + | |
| 1025 | + | |
| 1026 | + | |
979 | 1027 | | |
980 | 1028 | | |
981 | 1029 | | |
| |||
1093 | 1141 | | |
1094 | 1142 | | |
1095 | 1143 | | |
| 1144 | + | |
| 1145 | + | |
| 1146 | + | |
| 1147 | + | |
| 1148 | + | |
| 1149 | + | |
| 1150 | + | |
| 1151 | + | |
| 1152 | + | |
| 1153 | + | |
| 1154 | + | |
| 1155 | + | |
| 1156 | + | |
| 1157 | + | |
| 1158 | + | |
| 1159 | + | |
| 1160 | + | |
| 1161 | + | |
| 1162 | + | |
| 1163 | + | |
| 1164 | + | |
| 1165 | + | |
| 1166 | + | |
| 1167 | + | |
| 1168 | + | |
| 1169 | + | |
| 1170 | + | |
| 1171 | + | |
| 1172 | + | |
| 1173 | + | |
| 1174 | + | |
| 1175 | + | |
| 1176 | + | |
| 1177 | + | |
| 1178 | + | |
| 1179 | + | |
| 1180 | + | |
| 1181 | + | |
| 1182 | + | |
| 1183 | + | |
| 1184 | + | |
| 1185 | + | |
| 1186 | + | |
| 1187 | + | |
| 1188 | + | |
| 1189 | + | |
| 1190 | + | |
| 1191 | + | |
| 1192 | + | |
| 1193 | + | |
| 1194 | + | |
| 1195 | + | |
| 1196 | + | |
| 1197 | + | |
| 1198 | + | |
| 1199 | + | |
| 1200 | + | |
| 1201 | + | |
| 1202 | + | |
| 1203 | + | |
| 1204 | + | |
| 1205 | + | |
| 1206 | + | |
| 1207 | + | |
| 1208 | + | |
| 1209 | + | |
| 1210 | + | |
| 1211 | + | |
| 1212 | + | |
| 1213 | + | |
| 1214 | + | |
| 1215 | + | |
| 1216 | + | |
| 1217 | + | |
| 1218 | + | |
| 1219 | + | |
| 1220 | + | |
| 1221 | + | |
| 1222 | + | |
| 1223 | + | |
| 1224 | + | |
| 1225 | + | |
| 1226 | + | |
| 1227 | + | |
| 1228 | + | |
| 1229 | + | |
| 1230 | + | |
| 1231 | + | |
| 1232 | + | |
| 1233 | + | |
| 1234 | + | |
| 1235 | + | |
| 1236 | + | |
| 1237 | + | |
| 1238 | + | |
| 1239 | + | |
| 1240 | + | |
| 1241 | + | |
1096 | 1242 | | |
1097 | 1243 | | |
1098 | 1244 | | |
| |||
0 commit comments