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

Support Google Closure Compiler #440

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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: 25 additions & 4 deletions effekt/shared/src/main/scala/effekt/generator/js/Transformer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,31 @@ trait Transformer {
// const $getOp = "get$1234"
// const $putOp = "put$7554"
def generateStateAccessors: List[js.Stmt] = {
val getter = Const(JSName("$getOp"), JsString(nameDef(symbols.builtins.TState.get).name))
val setter = Const(JSName("$putOp"), JsString(nameDef(symbols.builtins.TState.put).name))

List(getter, setter)
val getOp = nameDef(symbols.builtins.TState.get).name
val putOp = nameDef(symbols.builtins.TState.put).name

js.RawStmt(
s""" // TODO maybe use weak refs (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakRef)
| function Cell(init) {
| var _value = init;
| const cell = ({
| backup: function() {
| var _backup = _value
| var cell = this;
| return () => { _value = _backup; return cell }
| }
| });
| cell.${getOp} = function() {
| return _value
| };
| cell.${putOp} = function(v) {
| _value = v;
| return $$effekt.unit;
| };
| return cell
| }
|
|""".stripMargin) :: Nil
}

// Names
Expand Down
4 changes: 1 addition & 3 deletions libraries/js/effekt_builtins.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,4 @@ function compare$impl(obj1, obj2) {
function println$impl(obj) {
//return $effekt.delayed(() => { console.log(show(obj)); return $effekt.unit; });
console.log(show$impl(obj)); return $effekt.unit;
}

$effekt.unit = { __unit: true }
}
79 changes: 0 additions & 79 deletions libraries/js/effekt_matching.js

This file was deleted.

153 changes: 48 additions & 105 deletions libraries/js/effekt_runtime.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,9 @@
const $runtime = (function() {
(function() {

// Common Runtime
// --------------

// Regions
// TODO maybe use weak refs (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakRef)
function Cell(init) {
var _value = init;
const cell = ({
backup: function() {
var _backup = _value
var cell = this;
return () => { _value = _backup; return cell }
}
});
// $getOp and $putOp are auto generated from the compiler
cell[$getOp] = function() {
return _value
};
cell[$putOp] = function(v) {
_value = v;
return $effekt.unit;
};
return cell
}

function Arena() {
return {
Expand Down Expand Up @@ -170,19 +150,6 @@ const $runtime = (function() {
})
}

function withStateIn(prompt, init, f) {
const cell = Cell(init)

if (prompt === toplevel) {
return f(cell)
} else {
return Control(k => {
allocateInto(k, prompt, cell);
return Step(f(cell), k)
})
}
}

// Delimited Control
function Control(apply) {
const self = {
Expand All @@ -209,20 +176,6 @@ const $runtime = (function() {
return f(a => trampoline(apply(k, a)))
})

const abort = Control(k => $effekt.unit)


const capture = f => {
// [abort; f
const action = () => f($effekt.unit).then(() => abort)
return shift(toplevel)(k =>
k({
shouldRun: false,
cont : () => k({ shouldRun: true, cont: action })
})).then(a => a.shouldRun ? a.cont() : $effekt.pure(a.cont))
}

//const reset = (p, c => Control(k => Step(c, Stack(Nil, Arena(), p, k)))

function handleMonadic(body) {
const p = _prompt++;
Expand Down Expand Up @@ -321,65 +274,55 @@ const $runtime = (function() {
return leftRegion
}

return {
// Common API
// -----------
constructor: (_, tag) => function() {
return { __tag: tag, __data: Array.from(arguments) }
},

hole: function() { throw "Implementation missing" },

// Monadic API
// -----------
pure: pure,
callcc: callcc,
capture: capture,
delayed: delayed,
handleMonadic: handleMonadic,
ref: Cell,
state: withState,
shift: shift,
_if: (c, thn, els) => c ? thn() : els(),
withRegion: withRegion,


// Direct style API
// ----------------
fresh: function(init) {
return currentRegion.fresh(init)
},

freshPrompt: function() { return ++_prompt; },

suspend: function(prompt, body) {
_stacksize = 0;
throw new Suspension(prompt, body, Nil, Empty)
},
suspend_bidirectional: function(prompt, caps, body) {
throw new Suspension(prompt, body, Cons(thunk => thunk.apply(null, caps), Nil), Empty)
},

// suspension: the raised exception.
push: function(suspension, frame) {
if (!(suspension instanceof Suspension)) throw suspension;
// Assuming `suspension` is a value or variable you want to return
throw new Suspension(suspension.prompt, suspension.body,
Cons(frame, suspension.frames), suspension.cont);
},

handle: function(prompt, s) {
return handleOrRethrow(prompt, s, Nil)
},

freshRegion: function() {
return enterRegion(new Arena)
},
// Common API
// -----------
$effekt.constructor = (_, tag) => function() {
return { __tag: tag, __data: Array.from(arguments) }
}

leaveRegion: leaveRegion,
$effekt.hole = function() { throw "Implementation missing" }

$effekt.unit = { __unit: true }

// Monadic API
// -----------
$effekt.pure = pure
$effekt.callcc = callcc
$effekt.delayed = delayed
$effekt.handleMonadic = handleMonadic
$effekt.ref = Cell
$effekt.state = withState
$effekt.shift = shift
$effekt._if = (c, thn, els) => c ? thn() : els()
$effekt.withRegion = withRegion

// Direct style API
// ----------------
$effekt.fresh = function(init) {
return currentRegion.fresh(init)
}
$effekt.freshPrompt = function() { return ++_prompt; }
$effekt.suspend = function(prompt, body) {
throw new Suspension(prompt, body, Nil, Empty)
}
$effekt.suspend_bidirectional = function(prompt, caps, body) {
throw new Suspension(prompt, body, Cons(thunk => thunk.apply(null, caps), Nil), Empty)
}

global: global
// suspension: the raised exception.
$effekt.push = function(suspension, frame) {
if (!(suspension instanceof Suspension)) throw suspension;
// Assuming `suspension` is a value or variable you want to return
throw new Suspension(suspension.prompt, suspension.body,
Cons(frame, suspension.frames), suspension.cont);
}
$effekt.handle = function(prompt, s) {
return handleOrRethrow(prompt, s, Nil)
}
$effekt.freshRegion = function() {
return enterRegion(Arena())
}
$effekt.leaveRegion = leaveRegion
$effekt.global = global
})()

Object.assign($effekt, $runtime);
10 changes: 9 additions & 1 deletion libraries/js/text/string.effekt
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,16 @@ def toInt(str: String): Option[Int] =
extern pure def unsafeCharAt(str: String, n: Int): String =
"${str}[${n}]"

extern """
function parseIntUndefined(str) {
const result = parseInt(str, 10)
if (Number.isNaN(result)) return undefined;
else return result;
}
"""

extern pure def unsafeToInt(str: String): Int =
"(Number.isNaN(parseInt(${str})) ? undefined : parseInt(${str}))"
"parseIntUndefined(${str})"

def indexOf(str: String, sub: String): Option[Int] = {
val index = str.unsafeIndexOf(sub)
Expand Down
Loading