Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix this capture for mixins in globalThis #169

Merged
merged 2 commits into from
Jun 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 15 additions & 14 deletions shared/src/main/scala/mlscript/JSBackend.scala
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,19 @@ class JSBackend(allowUnresolvedSymbols: Boolean) {
}
else Nil


protected def addNuTypeToGlobalThis(typeDef: NuTypeDef, moduleName: Str) = {
import JSCodeHelpers._
typeDef match {
case NuTypeDef(Mxn, TypeName(nme), _, _, _, _, _, _, _, _) =>
JSAssignExpr(id("globalThis").member(nme), JSArrowFn(param("base") :: Nil, L(
JSInvoke(id(moduleName).member(nme), id("base") :: Nil)
))).stmt
case NuTypeDef(_, TypeName(nme), _, _, _, _, _, _, _, _) =>
JSAssignExpr(id("globalThis").member(nme), id(moduleName).member(nme)).stmt
}
}

protected def translateLocalNewType(typeDef: NuTypeDef)(implicit scope: Scope): JSConstDecl = {
// TODO: support traitSymbols
val (traitSymbols, classSymbols, mixinSymbols, moduleSymbols) = declareNewTypeDefs(typeDef :: Nil, false)
Expand Down Expand Up @@ -1118,13 +1131,7 @@ class JSWebBackend extends JSBackend(allowUnresolvedSymbols = true) {
val insDecl =
JSConstDecl(moduleIns.runtimeName, JSNew(JSIdent(topModule.runtimeName)))

def include(typeName: Str, moduleName: Str) =
JSExprStmt(JSAssignExpr(JSField(JSIdent("globalThis"), typeName), JSField(JSIdent(moduleName), typeName)))
val includes =
typeDefs.filter(!_.isDecl).map {
case nu: NuTypeDef =>
include(nu.nme.name, moduleIns.runtimeName)
}
val includes = typeDefs.filter(!_.isDecl).map(addNuTypeToGlobalThis(_, moduleIns.runtimeName))

val resultsIdent = JSIdent(resultsName)
val resultNames = ListBuffer[Str]()
Expand Down Expand Up @@ -1316,13 +1323,7 @@ class JSTestBackend extends JSBackend(allowUnresolvedSymbols = false) {
val insDecl =
JSConstDecl(moduleIns.runtimeName, JSNew(JSIdent(topModule.runtimeName)))

def include(typeName: Str, moduleName: Str) =
JSExprStmt(JSAssignExpr(JSField(JSIdent("globalThis"), typeName), JSField(JSIdent(moduleName), typeName)))
val includes =
typeDefs.filter(!_.isDecl).map {
case nu: NuTypeDef =>
include(nu.nme.name, moduleIns.runtimeName)
}
val includes = typeDefs.filter(!_.isDecl).map(addNuTypeToGlobalThis(_, moduleIns.runtimeName))

val zeroWidthSpace = JSLit("\"\\u200B\"")
val catchClause = JSCatchClause(
Expand Down
22 changes: 11 additions & 11 deletions shared/src/test/diff/codegen/Mixin.mls
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ mixin EvalBase {
//│ }
//│ }
//│ const typing_unit1 = new TypingUnit1;
//│ globalThis.EvalBase = typing_unit1.EvalBase;
//│ globalThis.EvalBase = ((base) => typing_unit1.EvalBase(base));
//│ // End of generated code
//│ ┌ Block at Mixin.mls:107
//│ ├─┬ Prelude
Expand All @@ -164,9 +164,9 @@ mixin EvalBase {
//│ │ │ }
//│ │ │ }
//│ │ │ const typing_unit1 = new TypingUnit1;
//│ │ │ globalThis.EvalBase = typing_unit1.EvalBase;
//│ │ │ globalThis.EvalBase = ((base) => typing_unit1.EvalBase(base));
//│ │ └── Reply
//│ │ [Function: EvalBase]
//│ │ [Function (anonymous)]
//│ └── No queries

:js
Expand Down Expand Up @@ -258,7 +258,7 @@ mixin EvalNeg {
//│ }
//│ }
//│ const typing_unit3 = new TypingUnit3;
//│ globalThis.EvalNeg = typing_unit3.EvalNeg;
//│ globalThis.EvalNeg = ((base) => typing_unit3.EvalNeg(base));
//│ // End of generated code
//│ ┌ Block at Mixin.mls:231
//│ ├─┬ Prelude
Expand All @@ -282,9 +282,9 @@ mixin EvalNeg {
//│ │ │ }
//│ │ │ }
//│ │ │ const typing_unit3 = new TypingUnit3;
//│ │ │ globalThis.EvalNeg = typing_unit3.EvalNeg;
//│ │ │ globalThis.EvalNeg = ((base) => typing_unit3.EvalNeg(base));
//│ │ └── Reply
//│ │ [Function: EvalNeg]
//│ │ [Function (anonymous)]
//│ └── No queries

:js
Expand Down Expand Up @@ -319,7 +319,7 @@ mixin EvalNegNeg {
//│ }
//│ }
//│ const typing_unit4 = new TypingUnit4;
//│ globalThis.EvalNegNeg = typing_unit4.EvalNegNeg;
//│ globalThis.EvalNegNeg = ((base) => typing_unit4.EvalNegNeg(base));
//│ // End of generated code
//│ ┌ Block at Mixin.mls:292
//│ ├─┬ Prelude
Expand All @@ -343,9 +343,9 @@ mixin EvalNegNeg {
//│ │ │ }
//│ │ │ }
//│ │ │ const typing_unit4 = new TypingUnit4;
//│ │ │ globalThis.EvalNegNeg = typing_unit4.EvalNegNeg;
//│ │ │ globalThis.EvalNegNeg = ((base) => typing_unit4.EvalNegNeg(base));
//│ │ └── Reply
//│ │ [Function: EvalNegNeg]
//│ │ [Function (anonymous)]
//│ └── No queries

:js
Expand Down Expand Up @@ -510,7 +510,7 @@ mixin Fooo(x: Int) { fun f = [x, this.x] }
//│ }
//│ }
//│ const typing_unit14 = new TypingUnit14;
//│ globalThis.Fooo = typing_unit14.Fooo;
//│ globalThis.Fooo = ((base) => typing_unit14.Fooo(base));
//│ // End of generated code

:js
Expand All @@ -533,7 +533,7 @@ mixin Bazz(y: Int)
//│ }
//│ }
//│ const typing_unit15 = new TypingUnit15;
//│ globalThis.Bazz = typing_unit15.Bazz;
//│ globalThis.Bazz = ((base) => typing_unit15.Bazz(base));
//│ // End of generated code

:js
Expand Down
67 changes: 67 additions & 0 deletions shared/src/test/diff/codegen/MixinCapture.mls
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
:NewDefs

:js
class Lit(n: Int)
mixin EvalAddLit {
fun eval(e) =
if e is
Lit(n) then n
}
//│ class Lit(n: Int)
//│ mixin EvalAddLit() {
//│ fun eval: Lit -> Int
//│ }
//│ // Prelude
//│ let res;
//│ class TypingUnit {
//│ #Lit;
//│ constructor() {
//│ }
//│ EvalAddLit(base) {
//│ const outer = this;
//│ return (class EvalAddLit extends base {
//│ constructor(...rest) {
//│ super(...rest);
//│ }
//│ eval(e) {
//│ return ((() => {
//│ let a;
//│ return (a = e, a instanceof outer.Lit.class ? ((n) => n)(e.n) : (() => {
//│ throw new Error("non-exhaustive case expression");
//│ })());
//│ })());
//│ }
//│ });
//│ }
//│ get Lit() {
//│ const outer = this;
//│ if (this.#Lit === undefined) {
//│ class Lit {
//│ #n;
//│ get n() { return this.#n; }
//│ constructor(n) {
//│ this.#n = n;
//│ }
//│ };
//│ this.#Lit = ((n) => Object.freeze(new Lit(n)));
//│ this.#Lit.class = Lit;
//│ }
//│ return this.#Lit;
//│ }
//│ }
//│ const typing_unit = new TypingUnit;
//│ globalThis.Lit = typing_unit.Lit;
//│ globalThis.EvalAddLit = ((base) => typing_unit.EvalAddLit(base));
//│ // End of generated code

module TestLang extends EvalAddLit
//│ module TestLang {
//│ fun eval: Lit -> Int
//│ }

TestLang.eval(Lit(0))
//│ Int
//│ res
//│ = 0
LPTK marked this conversation as resolved.
Show resolved Hide resolved


2 changes: 1 addition & 1 deletion shared/src/test/diff/codegen/Nested.mls
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ mixin C() {
//│ }
//│ }
//│ const typing_unit3 = new TypingUnit3;
//│ globalThis.C = typing_unit3.C;
//│ globalThis.C = ((base) => typing_unit3.C(base));
//│ // End of generated code

:js
Expand Down
6 changes: 3 additions & 3 deletions shared/src/test/diff/codegen/Super.mls
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ mixin Foo0 {
//│ }
//│ }
//│ const typing_unit = new TypingUnit;
//│ globalThis.Foo0 = typing_unit.Foo0;
//│ globalThis.Foo0 = ((base) => typing_unit.Foo0(base));
//│ // End of generated code

:js
Expand Down Expand Up @@ -63,7 +63,7 @@ mixin Foo1 {
//│ }
//│ }
//│ const typing_unit1 = new TypingUnit1;
//│ globalThis.Foo1 = typing_unit1.Foo1;
//│ globalThis.Foo1 = ((base) => typing_unit1.Foo1(base));
//│ // End of generated code

module Test0 extends Foo0, Foo1
Expand Down Expand Up @@ -108,7 +108,7 @@ mixin Foo2 {
//│ }
//│ }
//│ const typing_unit4 = new TypingUnit4;
//│ globalThis.Foo2 = typing_unit4.Foo2;
//│ globalThis.Foo2 = ((base) => typing_unit4.Foo2(base));
//│ // End of generated code
//│ Syntax error:
//│ 'super' keyword unexpected here
Expand Down
4 changes: 2 additions & 2 deletions shared/src/test/diff/nu/BadMixins.mls
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ M0
//│ mixin M0()
//│ error
//│ res
//│ = [Function: M0]
//│ = [Function (anonymous)]

:e
M0
Expand All @@ -19,5 +19,5 @@ M0
//│ ╙── ^^
//│ error
//│ res
//│ = [Function: M0]
//│ = [Function (anonymous)]

Loading