Skip to content

Commit

Permalink
Make result of :sjs command exclude sanity checks
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
LPTK committed Dec 11, 2024
1 parent 626b759 commit e3f9e89
Show file tree
Hide file tree
Showing 51 changed files with 379 additions and 672 deletions.
30 changes: 27 additions & 3 deletions hkmc2/jvm/src/test/scala/hkmc2/JSBackendDiffMaker.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ import codegen.Block
import codegen.js.Scope
import hkmc2.syntax.Tree.Ident
import hkmc2.codegen.Path
import hkmc2.Diagnostic.Source

abstract class JSBackendDiffMaker extends MLsDiffMaker:

val debugLowering = NullaryCommand("dl")
val js = NullaryCommand("js")
val sjs = NullaryCommand("sjs")
val showSanitizedJS = NullaryCommand("ssjs")
val showJS = NullaryCommand("sjs")
val showRepl = NullaryCommand("showRepl")
val silent = NullaryCommand("silent")
val noSanityCheck = NullaryCommand("noSanityCheck")
Expand Down Expand Up @@ -49,7 +51,29 @@ abstract class JSBackendDiffMaker extends MLsDiffMaker:

override def processTerm(blk: semantics.Term.Blk, inImport: Bool)(using Raise): Unit =
super.processTerm(blk, inImport)
if js.isSet then
val outerRaise: Raise = summon
var showingJSYieldedCompileError = false
if showJS.isSet then
given Raise =
case d @ ErrorReport(source = Source.Compilation) =>
showingJSYieldedCompileError = true
outerRaise(d)
case d => outerRaise(d)
val low = ltl.givenIn:
new codegen.Lowering
with codegen.LoweringSelSanityChecks(instrument = false)
with codegen.LoweringTraceLog(instrument = false)
given Elaborator.Ctx = curCtx
val jsb = new JSBuilder
with JSBuilderArgNumSanityChecks(instrument = false)
val le = low.program(blk)
val nestedScp = baseScp.nest
val je = nestedScp.givenIn:
jsb.program(le, N, wd)
val jsStr = je.stripBreaks.mkString(100)
output(s"JS (unsanitized):")
output(jsStr)
if js.isSet && !showingJSYieldedCompileError then
val low = ltl.givenIn:
new codegen.Lowering
with codegen.LoweringSelSanityChecks(noSanityCheck.isUnset)
Expand All @@ -71,7 +95,7 @@ abstract class JSBackendDiffMaker extends MLsDiffMaker:
val je = nestedScp.givenIn:
jsb.program(le, N, wd)
val jsStr = je.stripBreaks.mkString(100)
if sjs.isSet then
if showSanitizedJS.isSet then
output(s"JS:")
output(jsStr)
def mkQuery(prefix: Str, jsStr: Str) =
Expand Down
3 changes: 2 additions & 1 deletion hkmc2/shared/src/main/scala/hkmc2/semantics/Elaborator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@ extends Importer:
cls.definedSymbols.get(nme.name) match
case s @ S(clsSym) => s
case N =>
raise(ErrorReport(msg"Module '${cls.symbol.nme}' does not contain member '${nme.name}'" -> srcTree.toLoc :: Nil))
raise(ErrorReport(msg"${cls.k.desc.capitalize} '${cls.symbol.nme
}' does not contain member '${nme.name}'" -> srcTree.toLoc :: Nil))
N
case N =>
N
Expand Down
2 changes: 1 addition & 1 deletion hkmc2/shared/src/test/mlscript/basics/Classes.mls
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Foo(x: Int) { log("Hello!") }
//│ ╔══[ERROR] Expected a valid definition head, found block instead
//│ ║ l.32: class Foo(x: Int) { log("Hello!") }
//│ ╙── ^^^^^^^^^^^^^
//│ JS:
//│ JS (unsanitized):
//│ this.<error> = class <error> { constructor() { } toString() { return "<error>"; } }; null
//│ > try { this.<error> = class <error> { constructor() { } toString() { return "<error>"; } }; null } catch (e) { console.log('\u200B' + e + '\u200B'); }
//│ > ^
Expand Down
64 changes: 17 additions & 47 deletions hkmc2/shared/src/test/mlscript/basics/MultiParamLists.mls
Original file line number Diff line number Diff line change
Expand Up @@ -5,50 +5,28 @@


fun f(n1: Int): Int = n1
//│ JS:
//│ function f(...args) {
//│ globalThis.Predef.checkArgs("f", 1, true, args.length);
//│ let n1 = args[0];
//│ return n1;
//│ }
//│ null
//│ JS (unsanitized):
//│ function f(n1) { return n1; } null

f(42)
//│ JS:
//│ JS (unsanitized):
//│ this.f(42)
//│ = 42

fun f(n1: Int)(n2: Int): Int = (10 * n1 + n2)
//│ JS:
//│ function f(...args) {
//│ globalThis.Predef.checkArgs("f", 1, true, args.length);
//│ let n1 = args[0];
//│ return (...args1) => {
//│ globalThis.Predef.checkArgs("", 1, true, args1.length);
//│ let n2 = args1[0];
//│ let tmp;
//│ tmp = 10 * n1;
//│ return tmp + n2;
//│ };
//│ }
//│ null
//│ JS (unsanitized):
//│ function f(n1) { return (n2) => { let tmp; tmp = 10 * n1; return tmp + n2; }; } null

f(4)(2)
//│ JS:
//│ JS (unsanitized):
//│ let tmp; tmp = this.f(4); tmp(2) ?? null
//│ = 42

fun f(n1: Int)(n2: Int)(n3: Int): Int = 10 * (10 * n1 + n2) + n3
//│ JS:
//│ function f(...args) {
//│ globalThis.Predef.checkArgs("f", 1, true, args.length);
//│ let n1 = args[0];
//│ return (...args1) => {
//│ globalThis.Predef.checkArgs("", 1, true, args1.length);
//│ let n2 = args1[0];
//│ return (...args2) => {
//│ globalThis.Predef.checkArgs("", 1, true, args2.length);
//│ let n3 = args2[0];
//│ JS (unsanitized):
//│ function f(n1) {
//│ return (n2) => {
//│ return (n3) => {
//│ let tmp, tmp1, tmp2;
//│ tmp = 10 * n1;
//│ tmp1 = tmp + n2;
Expand All @@ -60,24 +38,16 @@ fun f(n1: Int)(n2: Int)(n3: Int): Int = 10 * (10 * n1 + n2) + n3
//│ null

f(4)(2)(0)
//│ JS:
//│ JS (unsanitized):
//│ let tmp, tmp1; tmp = this.f(4); tmp1 = tmp(2) ?? null; tmp1(0) ?? null
//│ = 420

fun f(n1: Int)(n2: Int)(n3: Int)(n4: Int): Int = 10 * (10 * (10 * n1 + n2) + n3) + n4
//│ JS:
//│ function f(...args) {
//│ globalThis.Predef.checkArgs("f", 1, true, args.length);
//│ let n1 = args[0];
//│ return (...args1) => {
//│ globalThis.Predef.checkArgs("", 1, true, args1.length);
//│ let n2 = args1[0];
//│ return (...args2) => {
//│ globalThis.Predef.checkArgs("", 1, true, args2.length);
//│ let n3 = args2[0];
//│ return (...args3) => {
//│ globalThis.Predef.checkArgs("", 1, true, args3.length);
//│ let n4 = args3[0];
//│ JS (unsanitized):
//│ function f(n1) {
//│ return (n2) => {
//│ return (n3) => {
//│ return (n4) => {
//│ let tmp, tmp1, tmp2, tmp3, tmp4;
//│ tmp = 10 * n1;
//│ tmp1 = tmp + n2;
Expand All @@ -92,7 +62,7 @@ fun f(n1: Int)(n2: Int)(n3: Int)(n4: Int): Int = 10 * (10 * (10 * n1 + n2) + n3)
//│ null

f(3)(0)(3)(1)
//│ JS:
//│ JS (unsanitized):
//│ let tmp, tmp1, tmp2; tmp = this.f(3); tmp1 = tmp(0) ?? null; tmp2 = tmp1(3) ?? null; tmp2(1) ?? null
//│ = 3031

Expand Down
11 changes: 2 additions & 9 deletions hkmc2/shared/src/test/mlscript/codegen/BadInit.mls
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,13 @@ module Bar with
val x = 1
module Baz with
val a = Bar.x
//│ JS:
//│ JS (unsanitized):
//│ const Bar$class = class Bar {
//│ constructor() {
//│ this.x = 1;
//│ const Baz$class = class Baz {
//│ constructor() {
//│ let selRes, tmp;
//│ selRes = globalThis.Bar.x;
//│ if (selRes === undefined) {
//│ throw new globalThis.Error("Access to required field 'x' yielded 'undefined'");
//│ } else {
//│ tmp = selRes;
//│ }
//│ this.a = tmp;
//│ this.a = globalThis.Bar.x;
//│ }
//│ toString() { return "Baz"; }
//│ };
Expand Down
5 changes: 2 additions & 3 deletions hkmc2/shared/src/test/mlscript/codegen/BadNew.mls
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ new 2
:ge
new 2 + 2
//│ ═══[COMPILATION ERROR] Illegal reference to builtin symbol '+'
//│ JS:
//│ JS (unsanitized):
//│ new (()=>{throw globalThis.Error("Illegal reference to builtin symbol '+'")})()(2, 2)
//│ ═══[RUNTIME ERROR] TypeError: (intermediate value) is not a constructor

:re
new()
Expand All @@ -40,7 +39,7 @@ new { x = 1 }
//│ lhs = Ident of "x"
//│ rhs = IntLit of 1
//│ ╔══[ERROR] Name not found: x
//│ ║ l.36: new { x = 1 }
//│ ║ l.35: new { x = 1 }
//│ ╙── ^


2 changes: 1 addition & 1 deletion hkmc2/shared/src/test/mlscript/codegen/BadOpen.mls
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ open Foo { y }

:sjs
y
//│ JS:
//│ JS (unsanitized):
//│ this.Foo.y


Expand Down
17 changes: 10 additions & 7 deletions hkmc2/shared/src/test/mlscript/codegen/BasicTerms.mls
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@


1
//│ JS:
//│ JS (unsanitized):
//│ 1
//│ = 1

Expand All @@ -16,27 +16,32 @@
//│ ╔══[WARNING] Pure expression in statement position
//│ ║ l.14: 1
//│ ╙── ^
//│ JS (unsanitized):
//│ 2
//│ ╔══[WARNING] Pure expression in statement position
//│ ║ l.14: 1
//│ ╙── ^
//│ Lowered:
//│ Program:
//│ imports = Nil
//│ main = Return:
//│ res = Lit of IntLit of 2
//│ implct = true
//│ JS:
//│ 2
//│ = 2


:fixme // should be a "linking" error
log("Hi")
//│ JS:
//│ JS (unsanitized):
//│ this.log("Hi") ?? null
//│ ═══[RUNTIME ERROR] TypeError: this.log is not a function

:fixme
:lot
log("Hi")
2
//│ JS (unsanitized):
//│ let tmp; tmp = this.log("Hi") ?? null; 2
//│ Lowered:
//│ Program:
//│ imports = Nil
Expand All @@ -53,14 +58,12 @@ log("Hi")
//│ rest = Return: \
//│ res = Lit of IntLit of 2
//│ implct = true
//│ JS:
//│ let tmp; tmp = this.log("Hi") ?? null; 2
//│ ═══[RUNTIME ERROR] TypeError: this.log is not a function


:re
2(2)
//│ JS:
//│ JS (unsanitized):
//│ 2(2) ?? null
//│ ═══[RUNTIME ERROR] TypeError: 2 is not a function

Expand Down
8 changes: 4 additions & 4 deletions hkmc2/shared/src/test/mlscript/codegen/BuiltinOps.mls
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@

:sjs
2 + 2
//│ JS:
//│ JS (unsanitized):
//│ 2 + 2
//│ = 4

:sjs
2 +. 2
//│ JS:
//│ JS (unsanitized):
//│ 2 + 2
//│ = 4

:sjs
+2
//│ JS:
//│ JS (unsanitized):
//│ + 2
//│ = 2

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

:sjs
1 + 2
//│ JS:
//│ JS (unsanitized):
//│ this.lol(1, 2)
//│ = [ 1, 2 ]

Expand Down
6 changes: 2 additions & 4 deletions hkmc2/shared/src/test/mlscript/codegen/CaseOfCase.mls
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@ fun test(x) =
) is
Some(v) then log(v)
None then log("none")
//│ JS:
//│ function test(...args) {
//│ globalThis.Predef.checkArgs("test", 1, true, args.length);
//│ let x = args[0];
//│ JS (unsanitized):
//│ function test(x) {
//│ let param0, v, scrut, param01, v1, tmp, tmp1;
//│ if (x instanceof globalThis.Some.class) {
//│ param0 = x.value;
Expand Down
Loading

0 comments on commit e3f9e89

Please sign in to comment.