Skip to content

Commit 8990e3c

Browse files
committed
Generator scheduler
1 parent a21197b commit 8990e3c

File tree

17 files changed

+392
-273
lines changed

17 files changed

+392
-273
lines changed

lib/elixir_script/passes/output/js_module.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ defmodule ElixirScript.Output.JSModule do
1717
end
1818

1919
def start do
20-
start_process_call = Helpers.call(
20+
start_process_call = Helpers.call_sync(
2121
J.member_expression(
2222
Helpers.process_system(),
2323
J.identifier("spawn")

lib/elixir_script/passes/translate/forms/try.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ defmodule ElixirScript.Translate.Forms.Try do
7070
Helpers.call(
7171
JS.member_expression(
7272
Helpers.patterns(),
73-
JS.identifier("defmatchAsync")
73+
JS.identifier("defmatchGen")
7474
),
7575
processed_clauses
7676
)

lib/elixir_script/passes/translate/function.ex

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ defmodule ElixirScript.Translate.Function do
9898
match_or_default_call = Helpers.call(
9999
J.member_expression(
100100
Helpers.patterns(),
101-
J.identifier("match_or_default_async")
101+
J.identifier("match_or_default_gen")
102102
),
103103
[J.array_expression(patterns), J.identifier("__function_args__"), guards]
104104
)
@@ -160,8 +160,7 @@ defmodule ElixirScript.Translate.Function do
160160
J.identifier("null")
161161
{:__block__, _, block_body} ->
162162
{list, _} = Enum.map_reduce(block_body, state, fn(x, acc) ->
163-
{ast, acc} = Form.compile(x, acc)
164-
{[pause(), ast], acc}
163+
Form.compile(x, acc)
165164
end)
166165
List.flatten(list)
167166
_ ->
@@ -171,17 +170,7 @@ defmodule ElixirScript.Translate.Function do
171170
{ast, state}
172171
end
173172

174-
defp pause() do
175-
Helpers.call(
176-
J.member_expression(
177-
Helpers.process_system,
178-
J.identifier("pause")
179-
),
180-
[]
181-
)
182-
end
183-
184-
defp update_last_call(clause_body, %{function: {name, _}, anonymous_fn: anonymous?}) do
173+
def update_last_call(clause_body, %{function: {name, _}, anonymous_fn: anonymous?}) do
185174
last_item = List.last(clause_body)
186175
function_name = ElixirScript.Translate.Identifier.make_function_name(name)
187176

lib/elixir_script/passes/translate/helpers.ex

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,24 @@ defmodule ElixirScript.Translate.Helpers do
2727
end
2828

2929
def call(callee, arguments) do
30-
call_sync(callee, arguments)
31-
|> J.await_expression()
30+
J.call_expression(
31+
J.member_expression(process_system(), J.identifier("run")),
32+
[
33+
callee,
34+
J.array_expression(arguments)
35+
]
36+
)
37+
|> J.yield_expression(true)
3238
end
3339

3440
def arrow_function(params, body) do
35-
J.arrow_function_expression(
41+
J.function_expression(
3642
params,
3743
[],
3844
body,
45+
true,
3946
false,
40-
false,
41-
true
47+
false
4248
)
4349
end
4450

@@ -48,9 +54,9 @@ defmodule ElixirScript.Translate.Helpers do
4854
params,
4955
[],
5056
body,
57+
true,
5158
false,
52-
false,
53-
true
59+
false
5460
)
5561
end
5662

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"erlang-types": "^1.0.1",
2424
"grapheme-splitter": "^1.0.2",
2525
"rollup-plugin-commonjs": "^8.2.1",
26-
"tailored": "^2.7.2"
26+
"tailored": "file:/Users/bryanjos/projects/basstype/tailored"
2727
},
2828
"devDependencies": {
2929
"@std/esm": "^0.8.3",

rollup.config.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ export default {
1414
commonjs(),
1515
babel({
1616
babelrc: false
17-
}),
18-
minify({
19-
keepFnName: true,
20-
keepClassName: true
2117
})
18+
//minify({
19+
// keepFnName: true,
20+
// keepClassName: true
21+
//})
2222
],
2323
output: [{ file: 'priv/build/iife/ElixirScript.Core.js', format: 'iife' }]
2424
};

src/javascript/lib/core/erlang_compat/lists.js

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ function reverse(list) {
55
return [...list].reverse();
66
}
77

8-
async function foreach(fun, list) {
8+
function* foreach(fun, list) {
99
for (const x of list) {
10-
await fun(x);
10+
yield* fun(x);
1111
}
1212

1313
return Symbol.for('ok');
@@ -35,17 +35,17 @@ function flatten(deepList, tail = []) {
3535
return val.concat(tail);
3636
}
3737

38-
async function foldl(fun, acc0, list) {
38+
function* foldl(fun, acc0, list) {
3939
let acc = acc0;
4040

4141
for (const value of list) {
42-
acc = await fun(value, acc);
42+
acc = yield* fun(value, acc);
4343
}
4444

4545
return acc;
4646
}
4747

48-
async function foldr(fun, acc0, list) {
48+
function foldr(fun, acc0, list) {
4949
return foldl(fun, acc0, reverse(list));
5050
}
5151

@@ -126,18 +126,22 @@ function keytake(key, n, tupleList) {
126126
const result = keyfind(key, n, tupleList);
127127

128128
if (result !== false) {
129-
return new ErlangTypes.Tuple(result.get(n - 1), result, keydelete(key, n, tupleList));
129+
return new ErlangTypes.Tuple(
130+
result.get(n - 1),
131+
result,
132+
keydelete(key, n, tupleList)
133+
);
130134
}
131135

132136
return false;
133137
}
134138

135-
async function mapfoldl(fun, acc0, list1) {
139+
function* mapfoldl(fun, acc0, list1) {
136140
const listResult = [];
137141
let accResult = acc0;
138142

139143
for (const item of list1) {
140-
const tuple = await fun(item, accResult);
144+
const tuple = yield* fun(item, accResult);
141145
listResult.push(tuple.get(0));
142146
accResult = tuple.get(1);
143147
}
@@ -149,22 +153,22 @@ function concat(things) {
149153
return things.map(v => v.toString()).join();
150154
}
151155

152-
async function map(fun, list) {
156+
function* map(fun, list) {
153157
const reList = [];
154158

155159
for (const value of list) {
156-
const result = await fun(value);
160+
const result = yield* fun(value);
157161
reList.push(result);
158162
}
159163

160164
return reList;
161165
}
162166

163-
async function filter(pred, list1) {
167+
function* filter(pred, list1) {
164168
const reList = [];
165169

166170
for (const value of list1) {
167-
const result = await pred(value);
171+
const result = yield* pred(value);
168172
if (result === true) {
169173
reList.push(value);
170174
}
@@ -173,11 +177,11 @@ async function filter(pred, list1) {
173177
return reList;
174178
}
175179

176-
async function filtermap(fun, list1) {
180+
function* filtermap(fun, list1) {
177181
const list2 = [];
178182

179183
for (const item of list1) {
180-
const value = await fun(item);
184+
const value = yield* fun(item);
181185

182186
if (value === true) {
183187
list2.push(item);
@@ -199,35 +203,35 @@ function member(elem, list) {
199203
return false;
200204
}
201205

202-
async function all(pred, list) {
206+
function* all(pred, list) {
203207
for (const item of list) {
204-
if ((await pred(item)) === false) {
208+
if ((yield* pred(item)) === false) {
205209
return false;
206210
}
207211
}
208212

209213
return true;
210214
}
211215

212-
async function any(pred, list) {
216+
function* any(pred, list) {
213217
for (const item of list) {
214-
if ((await pred(item)) === true) {
218+
if ((yield* pred(item)) === true) {
215219
return true;
216220
}
217221
}
218222

219223
return false;
220224
}
221225

222-
async function splitwith(pred, list) {
226+
function* splitwith(pred, list) {
223227
let switchToList2 = false;
224228
const list1 = [];
225229
const list2 = [];
226230

227231
for (const item of list) {
228232
if (switchToList2 === true) {
229233
list2.push(item);
230-
} else if ((await pred(item)) === true) {
234+
} else if ((yield* pred(item)) === true) {
231235
list1.push(item);
232236
} else {
233237
switchToList2 = true;
@@ -238,7 +242,7 @@ async function splitwith(pred, list) {
238242
return new ErlangTypes.Tuple(list1, list2);
239243
}
240244

241-
async function sort(...args) {
245+
function* sort(...args) {
242246
if (args.length === 1) {
243247
const list2 = [...args[0]];
244248
return list2.sort();
@@ -247,8 +251,8 @@ async function sort(...args) {
247251
const fun = args[0];
248252
const list2 = [...args[1]];
249253

250-
const result = list2.sort(async (a, b) => {
251-
const sortResult = await fun(a, b);
254+
const result = list2.sort(function*(a, b) {
255+
const sortResult = yield* fun(a, b);
252256

253257
if (sortResult === true) {
254258
return -1;
@@ -283,5 +287,5 @@ export default {
283287
all,
284288
any,
285289
splitwith,
286-
sort,
290+
sort
287291
};

src/javascript/lib/core/erlang_compat/maps.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ function find(key, map) {
2121
return ERROR;
2222
}
2323

24-
async function fold(fun, init, map) {
24+
function* fold(fun, init, map) {
2525
let acc = init;
2626

2727
for (const [key, value] of map.entries()) {
28-
acc = await fun(key, value, acc);
28+
acc = yield* fun(key, value, acc);
2929
}
3030

3131
return acc;
@@ -169,5 +169,5 @@ export default {
169169
merge,
170170
update,
171171
get,
172-
take,
172+
take
173173
};

0 commit comments

Comments
 (0)