Skip to content

Commit b5ea196

Browse files
authored
unifying the messaging for type errors and adding type checks for builtin types (#10474)
* surface better type errors in pxsim * cleanup * make messages consistent * add type checking for refaction * move function * fix action typecheck and null/undefined type string * add errors for get/set/delete
1 parent c5cef11 commit b5ea196

File tree

4 files changed

+166
-29
lines changed

4 files changed

+166
-29
lines changed

pxtcompiler/emitter/backjs.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -493,16 +493,19 @@ function ${id}(s) {
493493
}
494494
}
495495

496+
function vTableRef(info: ClassInfo) {
497+
return `${info.id}_VT`
498+
}
499+
496500
function checkSubtype(info: ClassInfo, r0 = "r0") {
497-
const vt = `${info.id}_VT`
498-
return `checkSubtype(${r0}, ${vt})`
501+
return `checkSubtype(${r0}, ${vTableRef(info)})`
499502
}
500503

501504
function emitInstanceOf(info: ClassInfo, tp: string, r0 = "r0") {
502505
if (tp == "bool")
503506
write(`r0 = ${checkSubtype(info)};`)
504507
else if (tp == "validate") {
505-
write(`if (!${checkSubtype(info, r0)}) failedCast(${r0});`)
508+
write(`if (!${checkSubtype(info, r0)}) failedCast(${r0}, ${vTableRef(info)});`)
506509
} else {
507510
U.oops()
508511
}

pxtsim/langsupport.ts

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,9 @@ namespace pxsim {
531531
}
532532

533533
export function mapGetByString(map: RefMap, key: string) {
534+
if (!map) {
535+
throwFailedPropertyAccessError(map, key);
536+
}
534537
key += ""
535538
if (map instanceof RefRecord) {
536539
let r = map as RefRecord
@@ -544,8 +547,14 @@ namespace pxsim {
544547
}
545548

546549
export function mapDeleteByString(map: RefMap, key: string) {
547-
if (!(map instanceof RefMap))
548-
pxtrt.panic(923)
550+
if (map === undefined || map === null) {
551+
throwNullUndefinedAsObjectError();
552+
}
553+
554+
if (!(map instanceof RefMap)) {
555+
throwFailedCastError(map, "object");
556+
}
557+
549558
let i = map.findIdx(key);
550559
if (i >= 0)
551560
map.data.splice(i, 1)
@@ -556,6 +565,9 @@ namespace pxsim {
556565
export const mapGetGeneric = mapGetByString
557566

558567
export function mapSetByString(map: RefMap, key: string, val: any) {
568+
if (!map) {
569+
throwFailedPropertyAccessError(map, key);
570+
}
559571
key += ""
560572
if (map instanceof RefRecord) {
561573
let r = map as RefRecord
@@ -574,6 +586,9 @@ namespace pxsim {
574586
}
575587

576588
export function keysOf(v: RefMap) {
589+
if (v === undefined || v === null) {
590+
throwNullUndefinedAsObjectError();
591+
}
577592
let r = new RefCollection()
578593
if (v instanceof RefMap)
579594
for (let k of v.data) {
@@ -653,6 +668,7 @@ namespace pxsim {
653668
}
654669

655670
export function runInBackground(a: RefAction) {
671+
typeCheck(a);
656672
runtime.runFiberAsync(a);
657673
}
658674

@@ -662,10 +678,14 @@ namespace pxsim {
662678
.then(() => U.delay(20))
663679
.then(loop);
664680
}
665-
pxtrt.nullCheck(a)
681+
typeCheck(a);
666682
loop()
667683
}
668-
}
669-
670684

685+
export function typeCheck(a: RefAction) {
686+
if (!a || !(a instanceof RefAction) && !(a as any).info) {
687+
throwFailedCastError(a, "function");
688+
}
689+
}
690+
}
671691
}

0 commit comments

Comments
 (0)