Skip to content

Commit e3f9e89

Browse files
committed
Make result of :sjs command exclude sanity checks
Showing the sanitized result is usually not useful or desired and obscures the result. Command :ssjs can be used to see the checks, if enabled.
1 parent 626b759 commit e3f9e89

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+379
-672
lines changed

hkmc2/jvm/src/test/scala/hkmc2/JSBackendDiffMaker.scala

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@ import codegen.Block
1313
import codegen.js.Scope
1414
import hkmc2.syntax.Tree.Ident
1515
import hkmc2.codegen.Path
16+
import hkmc2.Diagnostic.Source
1617

1718
abstract class JSBackendDiffMaker extends MLsDiffMaker:
1819

1920
val debugLowering = NullaryCommand("dl")
2021
val js = NullaryCommand("js")
21-
val sjs = NullaryCommand("sjs")
22+
val showSanitizedJS = NullaryCommand("ssjs")
23+
val showJS = NullaryCommand("sjs")
2224
val showRepl = NullaryCommand("showRepl")
2325
val silent = NullaryCommand("silent")
2426
val noSanityCheck = NullaryCommand("noSanityCheck")
@@ -49,7 +51,29 @@ abstract class JSBackendDiffMaker extends MLsDiffMaker:
4951

5052
override def processTerm(blk: semantics.Term.Blk, inImport: Bool)(using Raise): Unit =
5153
super.processTerm(blk, inImport)
52-
if js.isSet then
54+
val outerRaise: Raise = summon
55+
var showingJSYieldedCompileError = false
56+
if showJS.isSet then
57+
given Raise =
58+
case d @ ErrorReport(source = Source.Compilation) =>
59+
showingJSYieldedCompileError = true
60+
outerRaise(d)
61+
case d => outerRaise(d)
62+
val low = ltl.givenIn:
63+
new codegen.Lowering
64+
with codegen.LoweringSelSanityChecks(instrument = false)
65+
with codegen.LoweringTraceLog(instrument = false)
66+
given Elaborator.Ctx = curCtx
67+
val jsb = new JSBuilder
68+
with JSBuilderArgNumSanityChecks(instrument = false)
69+
val le = low.program(blk)
70+
val nestedScp = baseScp.nest
71+
val je = nestedScp.givenIn:
72+
jsb.program(le, N, wd)
73+
val jsStr = je.stripBreaks.mkString(100)
74+
output(s"JS (unsanitized):")
75+
output(jsStr)
76+
if js.isSet && !showingJSYieldedCompileError then
5377
val low = ltl.givenIn:
5478
new codegen.Lowering
5579
with codegen.LoweringSelSanityChecks(noSanityCheck.isUnset)
@@ -71,7 +95,7 @@ abstract class JSBackendDiffMaker extends MLsDiffMaker:
7195
val je = nestedScp.givenIn:
7296
jsb.program(le, N, wd)
7397
val jsStr = je.stripBreaks.mkString(100)
74-
if sjs.isSet then
98+
if showSanitizedJS.isSet then
7599
output(s"JS:")
76100
output(jsStr)
77101
def mkQuery(prefix: Str, jsStr: Str) =

hkmc2/shared/src/main/scala/hkmc2/semantics/Elaborator.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,8 @@ extends Importer:
144144
cls.definedSymbols.get(nme.name) match
145145
case s @ S(clsSym) => s
146146
case N =>
147-
raise(ErrorReport(msg"Module '${cls.symbol.nme}' does not contain member '${nme.name}'" -> srcTree.toLoc :: Nil))
147+
raise(ErrorReport(msg"${cls.k.desc.capitalize} '${cls.symbol.nme
148+
}' does not contain member '${nme.name}'" -> srcTree.toLoc :: Nil))
148149
N
149150
case N =>
150151
N

hkmc2/shared/src/test/mlscript/basics/Classes.mls

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class Foo(x: Int) { log("Hello!") }
3333
//│ ╔══[ERROR] Expected a valid definition head, found block instead
3434
//│ ║ l.32: class Foo(x: Int) { log("Hello!") }
3535
//│ ╙── ^^^^^^^^^^^^^
36-
//│ JS:
36+
//│ JS (unsanitized):
3737
//│ this.<error> = class <error> { constructor() { } toString() { return "<error>"; } }; null
3838
//│ > try { this.<error> = class <error> { constructor() { } toString() { return "<error>"; } }; null } catch (e) { console.log('\u200B' + e + '\u200B'); }
3939
//│ > ^

hkmc2/shared/src/test/mlscript/basics/MultiParamLists.mls

Lines changed: 17 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -5,50 +5,28 @@
55

66

77
fun f(n1: Int): Int = n1
8-
//│ JS:
9-
//│ function f(...args) {
10-
//│ globalThis.Predef.checkArgs("f", 1, true, args.length);
11-
//│ let n1 = args[0];
12-
//│ return n1;
13-
//│ }
14-
//│ null
8+
//│ JS (unsanitized):
9+
//│ function f(n1) { return n1; } null
1510

1611
f(42)
17-
//│ JS:
12+
//│ JS (unsanitized):
1813
//│ this.f(42)
1914
//│ = 42
2015

2116
fun f(n1: Int)(n2: Int): Int = (10 * n1 + n2)
22-
//│ JS:
23-
//│ function f(...args) {
24-
//│ globalThis.Predef.checkArgs("f", 1, true, args.length);
25-
//│ let n1 = args[0];
26-
//│ return (...args1) => {
27-
//│ globalThis.Predef.checkArgs("", 1, true, args1.length);
28-
//│ let n2 = args1[0];
29-
//│ let tmp;
30-
//│ tmp = 10 * n1;
31-
//│ return tmp + n2;
32-
//│ };
33-
//│ }
34-
//│ null
17+
//│ JS (unsanitized):
18+
//│ function f(n1) { return (n2) => { let tmp; tmp = 10 * n1; return tmp + n2; }; } null
3519

3620
f(4)(2)
37-
//│ JS:
21+
//│ JS (unsanitized):
3822
//│ let tmp; tmp = this.f(4); tmp(2) ?? null
3923
//│ = 42
4024

4125
fun f(n1: Int)(n2: Int)(n3: Int): Int = 10 * (10 * n1 + n2) + n3
42-
//│ JS:
43-
//│ function f(...args) {
44-
//│ globalThis.Predef.checkArgs("f", 1, true, args.length);
45-
//│ let n1 = args[0];
46-
//│ return (...args1) => {
47-
//│ globalThis.Predef.checkArgs("", 1, true, args1.length);
48-
//│ let n2 = args1[0];
49-
//│ return (...args2) => {
50-
//│ globalThis.Predef.checkArgs("", 1, true, args2.length);
51-
//│ let n3 = args2[0];
26+
//│ JS (unsanitized):
27+
//│ function f(n1) {
28+
//│ return (n2) => {
29+
//│ return (n3) => {
5230
//│ let tmp, tmp1, tmp2;
5331
//│ tmp = 10 * n1;
5432
//│ tmp1 = tmp + n2;
@@ -60,24 +38,16 @@ fun f(n1: Int)(n2: Int)(n3: Int): Int = 10 * (10 * n1 + n2) + n3
6038
//│ null
6139

6240
f(4)(2)(0)
63-
//│ JS:
41+
//│ JS (unsanitized):
6442
//│ let tmp, tmp1; tmp = this.f(4); tmp1 = tmp(2) ?? null; tmp1(0) ?? null
6543
//│ = 420
6644

6745
fun f(n1: Int)(n2: Int)(n3: Int)(n4: Int): Int = 10 * (10 * (10 * n1 + n2) + n3) + n4
68-
//│ JS:
69-
//│ function f(...args) {
70-
//│ globalThis.Predef.checkArgs("f", 1, true, args.length);
71-
//│ let n1 = args[0];
72-
//│ return (...args1) => {
73-
//│ globalThis.Predef.checkArgs("", 1, true, args1.length);
74-
//│ let n2 = args1[0];
75-
//│ return (...args2) => {
76-
//│ globalThis.Predef.checkArgs("", 1, true, args2.length);
77-
//│ let n3 = args2[0];
78-
//│ return (...args3) => {
79-
//│ globalThis.Predef.checkArgs("", 1, true, args3.length);
80-
//│ let n4 = args3[0];
46+
//│ JS (unsanitized):
47+
//│ function f(n1) {
48+
//│ return (n2) => {
49+
//│ return (n3) => {
50+
//│ return (n4) => {
8151
//│ let tmp, tmp1, tmp2, tmp3, tmp4;
8252
//│ tmp = 10 * n1;
8353
//│ tmp1 = tmp + n2;
@@ -92,7 +62,7 @@ fun f(n1: Int)(n2: Int)(n3: Int)(n4: Int): Int = 10 * (10 * (10 * n1 + n2) + n3)
9262
//│ null
9363

9464
f(3)(0)(3)(1)
95-
//│ JS:
65+
//│ JS (unsanitized):
9666
//│ let tmp, tmp1, tmp2; tmp = this.f(3); tmp1 = tmp(0) ?? null; tmp2 = tmp1(3) ?? null; tmp2(1) ?? null
9767
//│ = 3031
9868

hkmc2/shared/src/test/mlscript/codegen/BadInit.mls

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,13 @@ module Bar with
88
val x = 1
99
module Baz with
1010
val a = Bar.x
11-
//│ JS:
11+
//│ JS (unsanitized):
1212
//│ const Bar$class = class Bar {
1313
//│ constructor() {
1414
//│ this.x = 1;
1515
//│ const Baz$class = class Baz {
1616
//│ constructor() {
17-
//│ let selRes, tmp;
18-
//│ selRes = globalThis.Bar.x;
19-
//│ if (selRes === undefined) {
20-
//│ throw new globalThis.Error("Access to required field 'x' yielded 'undefined'");
21-
//│ } else {
22-
//│ tmp = selRes;
23-
//│ }
24-
//│ this.a = tmp;
17+
//│ this.a = globalThis.Bar.x;
2518
//│ }
2619
//│ toString() { return "Baz"; }
2720
//│ };

hkmc2/shared/src/test/mlscript/codegen/BadNew.mls

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@ new 2
1515
:ge
1616
new 2 + 2
1717
//│ ═══[COMPILATION ERROR] Illegal reference to builtin symbol '+'
18-
//│ JS:
18+
//│ JS (unsanitized):
1919
//│ new (()=>{throw globalThis.Error("Illegal reference to builtin symbol '+'")})()(2, 2)
20-
//│ ═══[RUNTIME ERROR] TypeError: (intermediate value) is not a constructor
2120

2221
:re
2322
new()
@@ -40,7 +39,7 @@ new { x = 1 }
4039
//│ lhs = Ident of "x"
4140
//│ rhs = IntLit of 1
4241
//│ ╔══[ERROR] Name not found: x
43-
//│ ║ l.36: new { x = 1 }
42+
//│ ║ l.35: new { x = 1 }
4443
//│ ╙── ^
4544

4645

hkmc2/shared/src/test/mlscript/codegen/BadOpen.mls

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ open Foo { y }
2323

2424
:sjs
2525
y
26-
//│ JS:
26+
//│ JS (unsanitized):
2727
//│ this.Foo.y
2828

2929

hkmc2/shared/src/test/mlscript/codegen/BasicTerms.mls

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66

77
1
8-
//│ JS:
8+
//│ JS (unsanitized):
99
//│ 1
1010
//│ = 1
1111

@@ -16,27 +16,32 @@
1616
//│ ╔══[WARNING] Pure expression in statement position
1717
//│ ║ l.14: 1
1818
//│ ╙── ^
19+
//│ JS (unsanitized):
20+
//│ 2
21+
//│ ╔══[WARNING] Pure expression in statement position
22+
//│ ║ l.14: 1
23+
//│ ╙── ^
1924
//│ Lowered:
2025
//│ Program:
2126
//│ imports = Nil
2227
//│ main = Return:
2328
//│ res = Lit of IntLit of 2
2429
//│ implct = true
25-
//│ JS:
26-
//│ 2
2730
//│ = 2
2831

2932

3033
:fixme // should be a "linking" error
3134
log("Hi")
32-
//│ JS:
35+
//│ JS (unsanitized):
3336
//│ this.log("Hi") ?? null
3437
//│ ═══[RUNTIME ERROR] TypeError: this.log is not a function
3538

3639
:fixme
3740
:lot
3841
log("Hi")
3942
2
43+
//│ JS (unsanitized):
44+
//│ let tmp; tmp = this.log("Hi") ?? null; 2
4045
//│ Lowered:
4146
//│ Program:
4247
//│ imports = Nil
@@ -53,14 +58,12 @@ log("Hi")
5358
//│ rest = Return: \
5459
//│ res = Lit of IntLit of 2
5560
//│ implct = true
56-
//│ JS:
57-
//│ let tmp; tmp = this.log("Hi") ?? null; 2
5861
//│ ═══[RUNTIME ERROR] TypeError: this.log is not a function
5962

6063

6164
:re
6265
2(2)
63-
//│ JS:
66+
//│ JS (unsanitized):
6467
//│ 2(2) ?? null
6568
//│ ═══[RUNTIME ERROR] TypeError: 2 is not a function
6669

hkmc2/shared/src/test/mlscript/codegen/BuiltinOps.mls

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@
33

44
:sjs
55
2 + 2
6-
//│ JS:
6+
//│ JS (unsanitized):
77
//│ 2 + 2
88
//│ = 4
99

1010
:sjs
1111
2 +. 2
12-
//│ JS:
12+
//│ JS (unsanitized):
1313
//│ 2 + 2
1414
//│ = 4
1515

1616
:sjs
1717
+2
18-
//│ JS:
18+
//│ JS (unsanitized):
1919
//│ + 2
2020
//│ = 2
2121

@@ -24,7 +24,7 @@ fun (+) lol(a, b) = [a, b]
2424

2525
:sjs
2626
1 + 2
27-
//│ JS:
27+
//│ JS (unsanitized):
2828
//│ this.lol(1, 2)
2929
//│ = [ 1, 2 ]
3030

hkmc2/shared/src/test/mlscript/codegen/CaseOfCase.mls

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,8 @@ fun test(x) =
1717
) is
1818
Some(v) then log(v)
1919
None then log("none")
20-
//│ JS:
21-
//│ function test(...args) {
22-
//│ globalThis.Predef.checkArgs("test", 1, true, args.length);
23-
//│ let x = args[0];
20+
//│ JS (unsanitized):
21+
//│ function test(x) {
2422
//│ let param0, v, scrut, param01, v1, tmp, tmp1;
2523
//│ if (x instanceof globalThis.Some.class) {
2624
//│ param0 = x.value;

0 commit comments

Comments
 (0)