Skip to content

Commit 1777a5b

Browse files
committed
No longer set imported modules as properties of this
1 parent 27eeade commit 1777a5b

File tree

12 files changed

+102
-101
lines changed

12 files changed

+102
-101
lines changed

hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -762,9 +762,9 @@ trait LoweringTraceLog(instrument: Bool)(using TL, Raise, State)
762762
extension (k: Block => Block)
763763
def |>: (b: Block): Block = k(b)
764764

765-
private val traceLogFn = selFromGlobalThis("Predef", "TraceLogger", "log")
766-
private val traceLogIndentFn = selFromGlobalThis("Predef", "TraceLogger", "indent")
767-
private val traceLogResetFn = selFromGlobalThis("Predef", "TraceLogger", "resetIndent")
765+
private val traceLogFn = Value.Ref(State.runtimeSymbol).selSN("TraceLogger").selSN("log")
766+
private val traceLogIndentFn = Value.Ref(State.runtimeSymbol).selSN("TraceLogger").selSN("indent")
767+
private val traceLogResetFn = Value.Ref(State.runtimeSymbol).selSN("TraceLogger").selSN("resetIndent")
768768
private val strConcatFn = selFromGlobalThis("String", "prototype", "concat", "call")
769769
private val inspectFn = selFromGlobalThis("util", "inspect")
770770

hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -468,17 +468,14 @@ class JSBuilder(using TL, State, Ctx) extends CodeBuilder:
468468

469469
def worksheet(p: Program)(using Raise, Scope): (Document, Document) =
470470
reserveNames(p)
471-
p.imports.foreach: i =>
472-
i._1 -> scope.allocateName(i._1)
473-
val imps = p.imports.map: i =>
474-
val v = doc"this.${getVar(i._1)}"
475-
doc"""$v = await import("${i._2.toString
476-
}"); # if ($v.default !== undefined) $v = $v.default;"""
477-
blockPreamble(p.main) -> (imps.mkDocument(doc" # ") :/: returningTerm(p.main, endSemi = false).stripBreaks)
471+
lazy val imps = p.imports.map: i =>
472+
doc"""${getVar(i._1)} = await import("${i._2.toString}").then(m => m.default ?? m);"""
473+
blockPreamble(p.imports.map(_._1).toSeq ++ p.main.definedVars.toSeq) ->
474+
(imps.mkDocument(doc" # ") :/: returningTerm(p.main, endSemi = false).stripBreaks)
478475

479-
def blockPreamble(t: Block)(using Raise, Scope): Document =
476+
def blockPreamble(ss: Iterable[Symbol])(using Raise, Scope): Document =
480477
// TODO document: mutable var assnts require the lookup
481-
val vars = t.definedVars.toSeq.filter(scope.lookup(_).isEmpty).sortBy(_.uid).iterator.map(l =>
478+
val vars = ss.toSeq.filter(scope.lookup(_).isEmpty).sortBy(_.uid).iterator.map(l =>
482479
l -> scope.allocateName(l))
483480
if vars.isEmpty then doc"" else
484481
doc" # let " :: vars.map: (_, nme) =>
@@ -487,7 +484,7 @@ class JSBuilder(using TL, State, Ctx) extends CodeBuilder:
487484
:: doc";"
488485

489486
def block(t: Block, endSemi: Bool)(using Raise, Scope): Document =
490-
blockPreamble(t) :: returningTerm(t, endSemi)
487+
blockPreamble(t.definedVars) :: returningTerm(t, endSemi)
491488

492489
def body(t: Block, endSemi: Bool)(using Raise, Scope): Document = scope.nest givenIn:
493490
block(t, endSemi)

hkmc2/shared/src/test/mlscript-compile/Predef.mjs

Lines changed: 3 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -15,50 +15,6 @@ let Predef1;
1515
this.render = Rendering.render;
1616
this.assert = globalThis.console.assert;
1717
this.foldl = Predef.fold;
18-
(class TraceLogger {
19-
static {
20-
Predef.TraceLogger = TraceLogger;
21-
this.enabled = false;
22-
this.indentLvl = 0;
23-
}
24-
static indent() {
25-
let scrut, prev, tmp;
26-
scrut = TraceLogger.enabled;
27-
if (scrut === true) {
28-
prev = TraceLogger.indentLvl;
29-
tmp = prev + 1;
30-
TraceLogger.indentLvl = tmp;
31-
return prev
32-
} else {
33-
return runtime.Unit
34-
}
35-
}
36-
static resetIndent(n) {
37-
let scrut;
38-
scrut = TraceLogger.enabled;
39-
if (scrut === true) {
40-
TraceLogger.indentLvl = n;
41-
return runtime.Unit
42-
} else {
43-
return runtime.Unit
44-
}
45-
}
46-
static log(msg) {
47-
let scrut, tmp, tmp1, tmp2, tmp3, tmp4;
48-
scrut = TraceLogger.enabled;
49-
if (scrut === true) {
50-
tmp = runtime.safeCall("| ".repeat(TraceLogger.indentLvl));
51-
tmp1 = runtime.safeCall(" ".repeat(TraceLogger.indentLvl));
52-
tmp2 = "\n" + tmp1;
53-
tmp3 = msg.replaceAll("\n", tmp2);
54-
tmp4 = tmp + tmp3;
55-
return runtime.safeCall(globalThis.console.log(tmp4))
56-
} else {
57-
return runtime.Unit
58-
}
59-
}
60-
static toString() { return "TraceLogger"; }
61-
});
6218
}
6319
static id(x) {
6420
return x
@@ -119,11 +75,6 @@ let Predef1;
11975
tmp1 = runtime.safeCall(tmp(...xs));
12076
return runtime.safeCall(globalThis.console.log(...tmp1))
12177
}
122-
static printRaw(x6) {
123-
let tmp;
124-
tmp = runtime.safeCall(Predef.render(x6));
125-
return runtime.safeCall(globalThis.console.log(tmp))
126-
}
12778
static renderAsStr(arg) {
12879
if (typeof arg === 'string') {
12980
return arg
@@ -175,15 +126,15 @@ let Predef1;
175126
}
176127
static mkStr(...xs2) {
177128
let tmp, tmp1, lambda;
178-
lambda = (undefined, function (acc, x7) {
129+
lambda = (undefined, function (acc, x6) {
179130
let tmp2, tmp3, tmp4;
180-
if (typeof x7 === 'string') {
131+
if (typeof x6 === 'string') {
181132
tmp2 = true;
182133
} else {
183134
tmp2 = false;
184135
}
185136
tmp3 = runtime.safeCall(Predef.assert(tmp2));
186-
tmp4 = acc + x7;
137+
tmp4 = acc + x6;
187138
return (tmp3 , tmp4)
188139
});
189140
tmp = lambda;

hkmc2/shared/src/test/mlscript-compile/Predef.mls

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ val render = Rendering.render
3636
fun print(...xs) =
3737
console.log(...map(renderAsStr)(...xs))
3838

39-
fun printRaw(x) =
40-
console.log(render(x))
4139

4240
fun renderAsStr(arg) =
4341
if arg is Str then arg else render(arg)
@@ -71,23 +69,6 @@ fun mkStr(...xs) =
7169

7270

7371

74-
module TraceLogger with
75-
mut val enabled = false
76-
mut val indentLvl = 0
77-
fun indent() =
78-
if enabled then
79-
let prev = indentLvl
80-
set indentLvl = prev + 1
81-
prev
82-
else ()
83-
fun resetIndent(n) =
84-
if enabled then
85-
set indentLvl = n
86-
else ()
87-
fun log(msg) =
88-
if enabled then
89-
console.log("| ".repeat(indentLvl) + msg.replaceAll("\n", "\n" + " ".repeat(indentLvl)))
90-
else ()
9172

9273

9374

hkmc2/shared/src/test/mlscript-compile/Runtime.mjs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,50 @@ let Runtime1;
9898
static toString() { return "Str"; }
9999
});
100100
this.render = Rendering.render;
101+
(class TraceLogger {
102+
static {
103+
Runtime.TraceLogger = TraceLogger;
104+
this.enabled = false;
105+
this.indentLvl = 0;
106+
}
107+
static indent() {
108+
let scrut, prev, tmp;
109+
scrut = TraceLogger.enabled;
110+
if (scrut === true) {
111+
prev = TraceLogger.indentLvl;
112+
tmp = prev + 1;
113+
TraceLogger.indentLvl = tmp;
114+
return prev
115+
} else {
116+
return runtime.Unit
117+
}
118+
}
119+
static resetIndent(n) {
120+
let scrut;
121+
scrut = TraceLogger.enabled;
122+
if (scrut === true) {
123+
TraceLogger.indentLvl = n;
124+
return runtime.Unit
125+
} else {
126+
return runtime.Unit
127+
}
128+
}
129+
static log(msg) {
130+
let scrut, tmp, tmp1, tmp2, tmp3, tmp4;
131+
scrut = TraceLogger.enabled;
132+
if (scrut === true) {
133+
tmp = runtime.safeCall("| ".repeat(TraceLogger.indentLvl));
134+
tmp1 = runtime.safeCall(" ".repeat(TraceLogger.indentLvl));
135+
tmp2 = "\n" + tmp1;
136+
tmp3 = msg.replaceAll("\n", tmp2);
137+
tmp4 = tmp + tmp3;
138+
return runtime.safeCall(globalThis.console.log(tmp4))
139+
} else {
140+
return runtime.Unit
141+
}
142+
}
143+
static toString() { return "TraceLogger"; }
144+
});
101145
const FatalEffect$class = class FatalEffect {
102146
constructor() {}
103147
toString() { return "FatalEffect"; }
@@ -272,6 +316,11 @@ let Runtime1;
272316
return res
273317
}
274318
}
319+
static printRaw(x2) {
320+
let tmp;
321+
tmp = runtime.safeCall(Runtime.render(x2));
322+
return runtime.safeCall(globalThis.console.log(tmp))
323+
}
275324
static raisePrintStackEffect(showLocals) {
276325
return Runtime.mkEffect(Runtime.PrintStackEffect, showLocals)
277326
}

hkmc2/shared/src/test/mlscript-compile/Runtime.mls

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,28 @@ module Str with
8181
// Re-export rendering functions
8282
val render = Rendering.render
8383

84+
fun printRaw(x) = console.log(render(x))
85+
86+
// TraceLogger
87+
88+
module TraceLogger with
89+
mut val enabled = false
90+
mut val indentLvl = 0
91+
fun indent() =
92+
if enabled then
93+
let prev = indentLvl
94+
set indentLvl = prev + 1
95+
prev
96+
else ()
97+
fun resetIndent(n) =
98+
if enabled then
99+
set indentLvl = n
100+
else ()
101+
fun log(msg) =
102+
if enabled then
103+
console.log("| ".repeat(indentLvl) + msg.replaceAll("\n", "\n" + " ".repeat(indentLvl)))
104+
else ()
105+
84106
// Private definitions for algebraic effects
85107

86108
object FatalEffect

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ fun test2(y) = y + 1
9494
//│ > undefined
9595
//│ REPL> Parsed:
9696
//│ > undefined
97-
//│ REPL> Sending: try { runtime.checkCall(this.Predef.printRaw(block$res18)) } catch (e) { console.log('\u200B' + e.stack + '\u200B'); }
97+
//│ REPL> Sending: try { runtime.checkCall(runtime.printRaw(block$res18)) } catch (e) { console.log('\u200B' + e.stack + '\u200B'); }
9898
//│ REPL> Collected:
9999
//│ > undefined
100100
//│ > Unit { class: [class Unit] }

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,17 @@ let c = C
2121
new c("Bob")
2222
//│ = [object Object]
2323

24+
:re
2425
globalThis.MyClass
25-
//│ = class MyClass { constructor(name) { this.name = name; } greet() { console.log(`Hello from ${this.name}!`); }}
26+
//│ ═══[RUNTIME ERROR] Error: Access to required field 'MyClass' yielded 'undefined'
2627

2728

2829
:ge
2930
fun foo() =
3031
import "../../js/MyClass.mjs"
3132
()
3233
//│ ╔══[COMPILATION ERROR] Imports must be at the top level
33-
//│ ║ l.30: import "../../js/MyClass.mjs"
34+
//│ ║ l.31: import "../../js/MyClass.mjs"
3435
//│ ╙── ^^^^^^^^^^^^^^^^^^^^^^
3536

3637

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ print(12)
3535
//│ > 42
3636

3737

38-
globalThis.Predef.print(123)
38+
Predef.print(123)
3939
//│ > 123
4040

4141

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ fun res() = 1
1515
//│ > undefined
1616
//│ REPL> Parsed:
1717
//│ > undefined
18-
//│ REPL> Sending: try { runtime.checkCall(this.Predef.printRaw(block$res3)) } catch (e) { console.log('\u200B' + e.stack + '\u200B'); }
18+
//│ REPL> Sending: try { runtime.checkCall(runtime.printRaw(block$res3)) } catch (e) { console.log('\u200B' + e.stack + '\u200B'); }
1919
//│ REPL> Collected:
2020
//│ > undefined
2121
//│ > Unit { class: [class Unit] }
@@ -33,7 +33,7 @@ fun res() = 1
3333
//│ > undefined
3434
//│ REPL> Parsed:
3535
//│ > undefined
36-
//│ REPL> Sending: try { runtime.checkCall(this.Predef.printRaw(block$res4)) } catch (e) { console.log('\u200B' + e.stack + '\u200B'); }
36+
//│ REPL> Sending: try { runtime.checkCall(runtime.printRaw(block$res4)) } catch (e) { console.log('\u200B' + e.stack + '\u200B'); }
3737
//│ REPL> Collected:
3838
//│ > 4
3939
//│ > Unit { class: [class Unit] }
@@ -52,7 +52,7 @@ res
5252
//│ > undefined
5353
//│ REPL> Parsed:
5454
//│ > undefined
55-
//│ REPL> Sending: try { runtime.checkCall(this.Predef.printRaw(block$res5)) } catch (e) { console.log('\u200B' + e.stack + '\u200B'); }
55+
//│ REPL> Sending: try { runtime.checkCall(runtime.printRaw(block$res5)) } catch (e) { console.log('\u200B' + e.stack + '\u200B'); }
5656
//│ REPL> Collected:
5757
//│ > [function res]
5858
//│ > Unit { class: [class Unit] }
@@ -75,15 +75,15 @@ let x = 1, print(x), x
7575
//│ > 1
7676
//│ > undefined
7777
//│ > 1
78-
//│ REPL> Sending: try { runtime.checkCall(this.Predef.printRaw(block$res6)) } catch (e) { console.log('\u200B' + e.stack + '\u200B'); }
78+
//│ REPL> Sending: try { runtime.checkCall(runtime.printRaw(block$res6)) } catch (e) { console.log('\u200B' + e.stack + '\u200B'); }
7979
//│ REPL> Collected:
8080
//│ > 1
8181
//│ > Unit { class: [class Unit] }
8282
//│ REPL> Parsed:
8383
//│ > 1
8484
//│ > Unit { class: [class Unit] }
8585
//│ = 1
86-
//│ REPL> Sending: try { runtime.checkCall(this.Predef.printRaw(x)) } catch (e) { console.log('\u200B' + e.stack + '\u200B'); }
86+
//│ REPL> Sending: try { runtime.checkCall(runtime.printRaw(x)) } catch (e) { console.log('\u200B' + e.stack + '\u200B'); }
8787
//│ REPL> Collected:
8888
//│ > 1
8989
//│ > Unit { class: [class Unit] }

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ f(1,2)(3)
6161
:showRepl
6262
:traceJS
6363
f(1,2)(-3)
64-
//│ REPL> Sending: globalThis.Predef.TraceLogger.enabled = true; globalThis.Predef.TraceLogger.resetIndent(0)
64+
//│ REPL> Sending: runtime.TraceLogger.enabled = true; runtime.TraceLogger.resetIndent(0)
6565
//│ REPL> Collected:
6666
//│ > Unit { class: [class Unit] }
6767
//│ REPL> Sending: block$res9 = undefined
@@ -90,10 +90,10 @@ f(1,2)(-3)
9090
//│ > | | => 42
9191
//│ > | => 42
9292
//│ > => 42
93-
//│ REPL> Sending: globalThis.Predef.TraceLogger.enabled = false
93+
//│ REPL> Sending: runtime.TraceLogger.enabled = false
9494
//│ REPL> Collected:
9595
//│ > false
96-
//│ REPL> Sending: try { runtime.checkCall(this.Predef.printRaw(block$res9)) } catch (e) { console.log('\u200B' + e.stack + '\u200B'); }
96+
//│ REPL> Sending: try { runtime.checkCall(runtime.printRaw(block$res9)) } catch (e) { console.log('\u200B' + e.stack + '\u200B'); }
9797
//│ REPL> Collected:
9898
//│ > 45
9999
//│ > Unit { class: [class Unit] }
@@ -112,7 +112,7 @@ f(1,2)(-4)
112112
//│ > undefined
113113
//│ REPL> Parsed:
114114
//│ > undefined
115-
//│ REPL> Sending: try { runtime.checkCall(this.Predef.printRaw(block$res10)) } catch (e) { console.log('\u200B' + e.stack + '\u200B'); }
115+
//│ REPL> Sending: try { runtime.checkCall(runtime.printRaw(block$res10)) } catch (e) { console.log('\u200B' + e.stack + '\u200B'); }
116116
//│ REPL> Collected:
117117
//│ > 45
118118
//│ > Unit { class: [class Unit] }

hkmc2DiffTests/src/test/scala/hkmc2/JSBackendDiffMaker.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,8 @@ abstract class JSBackendDiffMaker extends MLsDiffMaker:
148148

149149
if traceJS.isSet then
150150
host.execute(
151-
"globalThis.Predef.TraceLogger.enabled = true; " +
152-
"globalThis.Predef.TraceLogger.resetIndent(0)")
151+
s"$runtimeNme.TraceLogger.enabled = true; " +
152+
s"$runtimeNme.TraceLogger.resetIndent(0)")
153153

154154
// * Sometimes the JS block won't execute due to a syntax or runtime error so we always set this first
155155
host.execute(s"$resNme = undefined")
@@ -159,7 +159,7 @@ abstract class JSBackendDiffMaker extends MLsDiffMaker:
159159
.foreach: line =>
160160
output(s"> ${line}")
161161
if traceJS.isSet then
162-
host.execute("globalThis.Predef.TraceLogger.enabled = false")
162+
host.execute(s"$runtimeNme.TraceLogger.enabled = false")
163163

164164
if silent.isUnset then
165165
import Elaborator.Ctx.*
@@ -178,7 +178,7 @@ abstract class JSBackendDiffMaker extends MLsDiffMaker:
178178
import codegen.*
179179
Return(
180180
Call(
181-
Value.Ref(Elaborator.State.globalThisSymbol).selSN("Predef").selSN("printRaw"),
181+
Value.Ref(Elaborator.State.runtimeSymbol).selSN("printRaw"),
182182
Arg(false, Value.Ref(sym)) :: Nil)(true, false),
183183
implct = true)
184184
val je = nestedScp.givenIn:

0 commit comments

Comments
 (0)