diff --git a/package-lock.json b/package-lock.json index 20480cb..9e9dee2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -29,6 +29,9 @@ }, "engines": { "node": ">=14.0.0" + }, + "peerDependencies": { + "@fink/js-interop": ">2.1" } }, "node_modules/@babel/code-frame": { @@ -580,9 +583,10 @@ } }, "node_modules/@fink/js-interop": { - "version": "2.0.1", - "resolved": "https://registry.npmjs.org/@fink/js-interop/-/js-interop-2.0.1.tgz", - "integrity": "sha512-WDjgBWf6hSS3aAP9lMM+OYJOQTOaWoPvWDgN+7IakSacplsX2omk6bdpE1sYBwSLEqfAQ0ZuyybfdGv83WM/WA==", + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/@fink/js-interop/-/js-interop-2.1.0.tgz", + "integrity": "sha512-6yxsatYmTb/exBvItoZZYIWv0uIqyuvKW5AaOu/Un94uL5GNRVD6vJYbCX7V1xp4xG+asRTn6uQnzoIn7fTN+A==", + "license": "MIT", "engines": { "node": ">=14.0.0" } @@ -601,9 +605,9 @@ } }, "node_modules/@fink/loxia": { - "version": "18.2.0", - "resolved": "https://registry.npmjs.org/@fink/loxia/-/loxia-18.2.0.tgz", - "integrity": "sha512-MGdfJwGFOtUqbs23/TMqS6PLWjPdo9P0hIcCZ1E2bkS/r6GKCrIBR8+ytq5cngTVkWbw2u3Nd7Qc4trLKBA0Iw==", + "version": "18.3.0", + "resolved": "https://registry.npmjs.org/@fink/loxia/-/loxia-18.3.0.tgz", + "integrity": "sha512-kiMZ334cYMw7zAJLapWjb9MC3/u+mYz7UD9biD4P9YTdoHLQf+2JSZ4+PuAXjiE24RYrzU2nfZTkZx1kcfCtKg==", "dev": true, "dependencies": { "@babel/core": "^7.10.5", @@ -18366,9 +18370,9 @@ } }, "@fink/js-interop": { - "version": "2.0.1", - "resolved": "https://registry.npmjs.org/@fink/js-interop/-/js-interop-2.0.1.tgz", - "integrity": "sha512-WDjgBWf6hSS3aAP9lMM+OYJOQTOaWoPvWDgN+7IakSacplsX2omk6bdpE1sYBwSLEqfAQ0ZuyybfdGv83WM/WA==" + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/@fink/js-interop/-/js-interop-2.1.0.tgz", + "integrity": "sha512-6yxsatYmTb/exBvItoZZYIWv0uIqyuvKW5AaOu/Un94uL5GNRVD6vJYbCX7V1xp4xG+asRTn6uQnzoIn7fTN+A==" }, "@fink/larix": { "version": "15.2.0", @@ -18381,9 +18385,9 @@ } }, "@fink/loxia": { - "version": "18.2.0", - "resolved": "https://registry.npmjs.org/@fink/loxia/-/loxia-18.2.0.tgz", - "integrity": "sha512-MGdfJwGFOtUqbs23/TMqS6PLWjPdo9P0hIcCZ1E2bkS/r6GKCrIBR8+ytq5cngTVkWbw2u3Nd7Qc4trLKBA0Iw==", + "version": "18.3.0", + "resolved": "https://registry.npmjs.org/@fink/loxia/-/loxia-18.3.0.tgz", + "integrity": "sha512-kiMZ334cYMw7zAJLapWjb9MC3/u+mYz7UD9biD4P9YTdoHLQf+2JSZ4+PuAXjiE24RYrzU2nfZTkZx1kcfCtKg==", "dev": true, "requires": { "@babel/core": "^7.10.5", diff --git a/package.json b/package.json index 2a12d9c..7eb8d4a 100644 --- a/package.json +++ b/package.json @@ -55,6 +55,9 @@ "npx-run": "^2.1.2", "semantic-release": "^17.2.1" }, + "peerDependencies": { + "@fink/js-interop": ">2.1" + }, "dependencies": { "@babel/core": "^7.10.5", "@babel/traverse": "^7.10.5", diff --git a/src/lang/async/init.test.fnk.snap b/src/lang/async/init.test.fnk.snap index e60d225..bc7f543 100644 --- a/src/lang/async/init.test.fnk.snap +++ b/src/lang/async/init.test.fnk.snap @@ -1,73 +1,39 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP exports[`await compiles 1`] = ` -"export const task1 = async (foo) => -(await foo); +"import { _unfold_ } from \\"@fink/js-interop/runtime.js\\"; +export const task1 = async (foo) => -(await foo); export const task2 = async (foo) => await (foo + 4); export const task3 = async (foo) => { const bar = await foo(); const spam = await foo(); return bar + 123; }; -export const a_gen = function unfold(ˆinitial_5) { - return { - async *[Symbol.asyncIterator]() { - let ˆitem_1 = ˆinitial_5; +export const a_gen = _unfold_(async (curr = 0) => { + const ˆvalue_1 = shrub; - while (true) { - const curr = ˆitem_1 === undefined ? 0 : ˆitem_1; - - let _do_result; - - ˆmatch_4: { - const ˆvalue_3 = shrub; - - if (ˆvalue_3 === spam) { - _do_result = await ni(curr); - break ˆmatch_4; - } - - { - _do_result = curr + 1; - break ˆmatch_4; - } - } - - const ˆresult_2 = _do_result; - _do_result = undefined; - yield ˆresult_2; - ˆitem_1 = ˆresult_2; - } - } + if (ˆvalue_1 === spam) { + return await ni(curr); + } - }; -}; + { + return curr + 1; + } +}, 1, false, false); await ni;" `; exports[`await handles awaiting async iterables 1`] = ` -"let _do_result; +"import { _unfold_ } from \\"@fink/js-interop/runtime.js\\"; + +let _do_result; { let _do_result2; { let ˆpipe_result_3 = undefined; - - _do_result2 = ˆpipe_result_3 = function unfold(ˆinitial_6) { - return { - async *[Symbol.asyncIterator]() { - let ˆitem_4 = ˆinitial_6; - - while (true) { - const cntr = ˆitem_4 === undefined ? 0 : ˆitem_4; - const ˆresult_5 = (await cntr) + 1; - yield ˆresult_5; - ˆitem_4 = ˆresult_5; - } - } - - }; - }(ˆpipe_result_3); + _do_result2 = ˆpipe_result_3 = _unfold_(async (cntr = 0) => (await cntr) + 1, 1, false, false)(ˆpipe_result_3); } const ˆitems_1 = _do_result2; _do_result2 = undefined; diff --git a/src/lang/call/pipe.test.fnk.snap b/src/lang/call/pipe.test.fnk.snap index 9fe5ee3..5da6c09 100644 --- a/src/lang/call/pipe.test.fnk.snap +++ b/src/lang/call/pipe.test.fnk.snap @@ -1,7 +1,8 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP exports[`pipe compiles 1`] = ` -"{ +"import { _map_ } from \\"@fink/js-interop/runtime.js\\"; +{ let ˆpipe_result_1 = undefined; ˆpipe_result_1 = foo(ˆpipe_result_1); @@ -16,19 +17,7 @@ exports[`pipe compiles 1`] = ` } { let ˆpipe_result_2 = [1, 2, 3]; - - ˆpipe_result_2 = function map(ˆitems_5) { - return { - *[Symbol.iterator]() { - for (const ˆitem_3 of ˆitems_5) { - const item = ˆitem_3; - const ˆresult_4 = item * 2; - yield ˆresult_4; - } - } - - }; - }(ˆpipe_result_2); + ˆpipe_result_2 = _map_((item) => item * 2, 1, false, false)(ˆpipe_result_2); }" `; diff --git a/src/lang/context.fnk b/src/lang/context.fnk index 6560e14..4fbaaeb 100644 --- a/src/lang/context.fnk +++ b/src/lang/context.fnk @@ -15,7 +15,7 @@ add = fn type, op, transformer: fn {transformers, ...ctx}: -add_with_builtin = fn type, op, impl_uri, transformer: fn ctx: +add_with_runtime = fn type, op, transformer, impl_uri: fn ctx: built_in_name = '_${op}_' pipe ctx: add_runtime_fn built_in_name, ? diff --git a/src/lang/init.fnk b/src/lang/init.fnk index 46af664..46f80c1 100644 --- a/src/lang/init.fnk +++ b/src/lang/init.fnk @@ -17,7 +17,7 @@ {add_arithmitic} = import './arithmitic/init.fnk' {add_block} = import './block/init.fnk' {add_partial} = import './partial/init.fnk' -{init_builtins} = import './runtime.fnk' +{init_runtimes} = import './runtime.fnk' @@ -60,7 +60,7 @@ init_ctx = fn code, filename, options: ignoreable_imports: [] pipe ctx: - init_builtins + init_runtimes add_transformers diff --git a/src/lang/iterable/common.fnk b/src/lang/iterable/common.fnk index b4265f6..9db3b3d 100644 --- a/src/lang/iterable/common.fnk +++ b/src/lang/iterable/common.fnk @@ -13,14 +13,32 @@ babe_types = import '@babel/types' transform_with_runtime_lib = fn iter_fn, node, ctx: args_len = length node.args - [use_spread, exprs] = match node.exprs: - [..., {type: 'spread'}]: + [use_spread, exprs] = match node: + {op: not ? in ['map', 'unfold']}: + [[], node.exprs] + {exprs: [..., {type: 'spread'}]}: spread = {type: 'literal', value: 'true', loc: node.loc} [...exprs, last] = node.exprs - [spread, [...exprs, last.right]] + [[spread], [...exprs, last.right]] else: spread = {type: 'literal', value: 'false', loc: node.loc} - [spread, node.exprs] + [[spread], node.exprs] + + [is_async, args] = match node.args: + [{op: 'await'}]: + async = {type: 'literal', value: 'true', loc: node.loc} + [{right}, ...rest] = node.args + [async, [right, ...rest]] + else: + async = {type: 'literal', value: 'false', loc: node.loc} + [async, node.args] + + [fold_initial, final_args] = match node: + {op: 'fold', args: [, {type: 'assign'}]}: + [first, {left, right}, ...rest] = args + [[right], [first, left, ...rest]] + else: + [[], args] [result, end_ctx] = transform rec: @@ -29,13 +47,13 @@ transform_with_runtime_lib = fn iter_fn, node, ctx: args: seq: rec: type: 'fn' - args: node.args + args: final_args exprs: exprs loc: node.loc {type: 'number', value: '${args_len}', loc: node.loc} - {type: 'literal', value: 'false', loc: node.loc} - use_spread - + is_async + ...use_spread + ...fold_initial loc: node.loc ctx [result, end_ctx] diff --git a/src/lang/iterable/filter.test.fnk b/src/lang/iterable/filter.test.fnk index 8b8ad3d..04e4b6e 100644 --- a/src/lang/iterable/filter.test.fnk +++ b/src/lang/iterable/filter.test.fnk @@ -3,11 +3,10 @@ -describe 'gen 2', fn: - it 'compiles', fn: +describe 'filter', fn: + it 'compiles with default runtime', fn: expect fink2js ' - {_filter_} = import "@fink/js-interop/iter.js" filter item: item % 2 == 0 ' @@ -15,18 +14,35 @@ describe 'gen 2', fn: expect fink2js ' - {_filter_} = import "@fink/js-interop/iter.js" filter item, cntr=0: [item < 1 and cntr % 2 == 0, cntr + 1] ' to_match_snapshot + it 'compiles with custom runtime', fn: + expect + fink2js ' + {_filter_} = import "./filter.fnk" + filter item: + item % 2 == 0 + ' + to_match_snapshot -describe 'filter', fn: + expect + fink2js ' + filter item, cntr=0: + [item < 1 and cntr % 2 == 0, cntr + 1] + ' + to_match_snapshot + + + +describe 'filter legacy', fn: it 'compiles', fn: expect fink2js ' + {_filter_} = import "@fink/loxia:iter:legacy" filter item: item % 2 == 0 @@ -43,6 +59,7 @@ describe 'filter', fn: it 'compiles with accu', fn: expect fink2js ' + {_filter_} = import "@fink/loxia:iter:legacy" filter item, cntr=0: [item < 1 and cntr % 2 == 0, cntr + 1] ' @@ -50,6 +67,7 @@ describe 'filter', fn: expect fink2js ' + {_filter_} = import "@fink/loxia:iter:legacy" filter _, is_even=false: [is_even, not is_even] ' @@ -57,6 +75,7 @@ describe 'filter', fn: expect fink2js ' + {_filter_} = import "@fink/loxia:iter:legacy" filter , is_even=false: [is_even, not is_even] ' @@ -68,6 +87,7 @@ describe 'filter async', fn: it 'compiles', fn: expect fink2js ' + {_filter_} = import "@fink/loxia:iter:legacy" filter item: await item % 2 == 0 ' diff --git a/src/lang/iterable/filter.test.fnk.snap b/src/lang/iterable/filter.test.fnk.snap index 240e5fc..e9b45d4 100644 --- a/src/lang/iterable/filter.test.fnk.snap +++ b/src/lang/iterable/filter.test.fnk.snap @@ -15,7 +15,31 @@ exports[`filter async compiles 1`] = ` });" `; -exports[`filter compiles 1`] = ` +exports[`filter compiles with custom runtime 1`] = ` +"import { _filter_ } from \\"./filter.js\\"; + +_filter_((item) => item % 2 === 0, 1, false, false);" +`; + +exports[`filter compiles with custom runtime 2`] = ` +"import { _filter_ } from \\"@fink/js-interop/runtime.js\\"; + +_filter_((item, cntr = 0) => [item < 1 && cntr % 2 === 0, cntr + 1], 2, false, false);" +`; + +exports[`filter compiles with default runtime 1`] = ` +"import { _filter_ } from \\"@fink/js-interop/runtime.js\\"; + +_filter_((item) => item % 2 === 0, 1, false, false);" +`; + +exports[`filter compiles with default runtime 2`] = ` +"import { _filter_ } from \\"@fink/js-interop/runtime.js\\"; + +_filter_((item, cntr = 0) => [item < 1 && cntr % 2 === 0, cntr + 1], 2, false, false);" +`; + +exports[`filter legacy compiles 1`] = ` "(function filter(ˆitems_3) { return { *[Symbol.iterator]() { @@ -64,7 +88,7 @@ exports[`filter compiles 1`] = ` });" `; -exports[`filter compiles with accu 1`] = ` +exports[`filter legacy compiles with accu 1`] = ` "(function filter(ˆitems_5) { return { *[Symbol.iterator]() { @@ -83,7 +107,7 @@ exports[`filter compiles with accu 1`] = ` });" `; -exports[`filter compiles with accu 2`] = ` +exports[`filter legacy compiles with accu 2`] = ` "(function filter(ˆitems_5) { return { *[Symbol.iterator]() { @@ -101,7 +125,7 @@ exports[`filter compiles with accu 2`] = ` });" `; -exports[`filter compiles with accu 3`] = ` +exports[`filter legacy compiles with accu 3`] = ` "(function filter(ˆitems_5) { return { *[Symbol.iterator]() { @@ -118,15 +142,3 @@ exports[`filter compiles with accu 3`] = ` }; });" `; - -exports[`gen 2 compiles 1`] = ` -"import { _filter_ } from \\"@fink/js-interop/iter.js\\"; - -_filter_((item) => item % 2 === 0, 1, false, false);" -`; - -exports[`gen 2 compiles 2`] = ` -"import { _filter_ } from \\"@fink/js-interop/iter.js\\"; - -_filter_((item, cntr = 0) => [item < 1 && cntr % 2 === 0, cntr + 1], 2, false, false);" -`; diff --git a/src/lang/iterable/fold.test.fnk b/src/lang/iterable/fold.test.fnk index f9be4bb..306f3d9 100644 --- a/src/lang/iterable/fold.test.fnk +++ b/src/lang/iterable/fold.test.fnk @@ -3,11 +3,10 @@ -describe 'gen 2', fn: - it 'compiles', fn: +describe 'fold', fn: + it 'compiles with default runtime', fn: expect fink2js ' - {_fold_} = import "@fink/js-interop/iter.js" fold item, prev=0: ni = item + prev item * ni @@ -16,7 +15,6 @@ describe 'gen 2', fn: expect fink2js ' - {_fold_} = import "@fink/js-interop/iter.js" fold item, prev=0, acc=1: [item * acc + prev, acc + 1] ' @@ -24,19 +22,42 @@ describe 'gen 2', fn: expect fink2js ' - {_fold_} = import "@fink/js-interop/iter.js" + fold item, prev=0, acc=1, shared_acc: + [item * acc + prev, acc + 1, shared_acc + 1] + ' + to_match_snapshot + + expect + fink2js ' fold item: item ' to_match_snapshot + expect + fink2js ' + fold: false + ' + to_match_snapshot + + it 'compiles with custom runtime', fn: + expect + fink2js ' + {_fold_} = import "./fold.fnk" + fold item, prev=0: + ni = item + prev + item * ni + ' + to_match_snapshot -describe 'fold', fn: + +describe 'fold legacy', fn: it 'compiles', fn: expect fink2js ' + {_fold_} = import "@fink/loxia:iter:legacy" fold item, prev=0: ni = item + prev item * ni @@ -47,6 +68,7 @@ describe 'fold', fn: it 'compiles with accu', fn: expect fink2js ' + {_fold_} = import "@fink/loxia:iter:legacy" fold item, prev=0, acc=1: [item * acc + prev, acc + 1] ' @@ -56,6 +78,7 @@ describe 'fold', fn: it 'compiles without using prev result', fn: expect fink2js ' + {_fold_} = import "@fink/loxia:iter:legacy" fold item: item ' @@ -65,6 +88,7 @@ describe 'fold', fn: it 'destructuring item', fn: expect fink2js ' + {_fold_} = import "@fink/loxia:iter:legacy" fold [foo, ...bar], prev=[]: [[foo, bar], ...prev] @@ -77,6 +101,7 @@ describe 'fold', fn: it 'destructuring prev result', fn: expect fink2js ' + {_fold_} = import "@fink/loxia:iter:legacy" fold item, [foo, ...bar]=[]: [item, bar, foo] @@ -91,8 +116,10 @@ describe 'fold async', fn: it 'compiles', fn: expect fink2js ' + {_fold_} = import "@fink/loxia:iter:legacy" fold item, acc=0: ni = await item + acc item * acc ' to_match_snapshot + diff --git a/src/lang/iterable/fold.test.fnk.snap b/src/lang/iterable/fold.test.fnk.snap index 70267d9..62907ee 100644 --- a/src/lang/iterable/fold.test.fnk.snap +++ b/src/lang/iterable/fold.test.fnk.snap @@ -16,7 +16,49 @@ exports[`fold async compiles 1`] = ` };" `; -exports[`fold compiles 1`] = ` +exports[`fold compiles with custom runtime 1`] = ` +"import { _fold_ } from \\"./fold.js\\"; + +_fold_((item, prev = 0) => { + const ni = item + prev; + return item * ni; +}, 2, false, false);" +`; + +exports[`fold compiles with default runtime 1`] = ` +"import { _fold_ } from \\"@fink/js-interop/runtime.js\\"; + +_fold_((item, prev = 0) => { + const ni = item + prev; + return item * ni; +}, 2, false, false);" +`; + +exports[`fold compiles with default runtime 2`] = ` +"import { _fold_ } from \\"@fink/js-interop/runtime.js\\"; + +_fold_((item, prev = 0, acc = 1) => [item * acc + prev, acc + 1], 3, false, false);" +`; + +exports[`fold compiles with default runtime 3`] = ` +"import { _fold_ } from \\"@fink/js-interop/runtime.js\\"; + +_fold_((item, prev = 0, acc = 1, shared_acc) => [item * acc + prev, acc + 1, shared_acc + 1], 4, false, false);" +`; + +exports[`fold compiles with default runtime 4`] = ` +"import { _fold_ } from \\"@fink/js-interop/runtime.js\\"; + +_fold_((item) => item, 1, false, false);" +`; + +exports[`fold compiles with default runtime 5`] = ` +"import { _fold_ } from \\"@fink/js-interop/runtime.js\\"; + +_fold_(() => false, 0, false, false);" +`; + +exports[`fold legacy compiles 1`] = ` "ˆitems_3 => { let ˆfold_result_4 = 0; @@ -32,7 +74,7 @@ exports[`fold compiles 1`] = ` };" `; -exports[`fold compiles with accu 1`] = ` +exports[`fold legacy compiles with accu 1`] = ` "ˆitems_5 => { let ˆaccu_2 = 1; let ˆfold_result_6 = 0; @@ -50,7 +92,7 @@ exports[`fold compiles with accu 1`] = ` };" `; -exports[`fold compiles without using prev result 1`] = ` +exports[`fold legacy compiles without using prev result 1`] = ` "ˆitems_3 => { let ˆfold_result_4; @@ -64,7 +106,7 @@ exports[`fold compiles without using prev result 1`] = ` };" `; -exports[`fold destructuring item 1`] = ` +exports[`fold legacy destructuring item 1`] = ` "ˆitems_3 => { let ˆfold_result_4 = []; @@ -99,7 +141,7 @@ exports[`fold destructuring item 1`] = ` };" `; -exports[`fold destructuring prev result 1`] = ` +exports[`fold legacy destructuring prev result 1`] = ` "ˆitems_3 => { let ˆfold_result_4 = []; @@ -134,24 +176,3 @@ exports[`fold destructuring prev result 1`] = ` return ˆfold_result_8; };" `; - -exports[`gen 2 compiles 1`] = ` -"import { _fold_ } from \\"@fink/js-interop/iter.js\\"; - -_fold_((item, prev = 0) => { - const ni = item + prev; - return item * ni; -}, 2, false, false);" -`; - -exports[`gen 2 compiles 2`] = ` -"import { _fold_ } from \\"@fink/js-interop/iter.js\\"; - -_fold_((item, prev = 0, acc = 1) => [item * acc + prev, acc + 1], 3, false, false);" -`; - -exports[`gen 2 compiles 3`] = ` -"import { _fold_ } from \\"@fink/js-interop/iter.js\\"; - -_fold_((item) => item, 1, false, false);" -`; diff --git a/src/lang/iterable/init.fnk b/src/lang/iterable/init.fnk index 9207bde..575ae9c 100644 --- a/src/lang/iterable/init.fnk +++ b/src/lang/iterable/init.fnk @@ -1,4 +1,4 @@ -{add_with_builtin, any} = import '../context.fnk' +{add_with_runtime, any} = import '../context.fnk' {transform_fold} = import './fold.fnk' {transform_unfold} = import './unfold.fnk' @@ -31,7 +31,7 @@ with_legacy = fn legacy_transform: fn node, ctx: add = fn op, legacy_transform: fn ctx: pipe ctx: - add_with_builtin any, op, legacy_uri, with_legacy legacy_transform + add_with_runtime any, op, with_legacy legacy_transform diff --git a/src/lang/iterable/map.test.fnk b/src/lang/iterable/map.test.fnk index 42b184f..85e33c0 100644 --- a/src/lang/iterable/map.test.fnk +++ b/src/lang/iterable/map.test.fnk @@ -3,53 +3,45 @@ {fink2js} = import '../../testing/generate.fnk' -describe 'gen 2', fn: - it 'compiles', fn: - expect - fink2js ' - {_map_} = import "@fink/js-interop/iter.js" - map item: - item * 2 - ' - to_match_snapshot +describe 'map', fn: + + it 'uses default runtime', fn: expect fink2js ' - {_map_} = import "@fink/js-interop/iter.js" - map item, acc=0: - [item * 2 * acc, acc + 1] - ' - to_match_snapshot + pipe [1, 2, 3]: + map item: + item * 2 + map item, acc=0: + [item + acc, acc + 1] - it 'uses custom iterable impl.', fn: - expect - fink2js ' - {_map_} = import "@fink/js-interop/iter.js" - map item: - ...[item, item * 2] + map item, acc=0, shared_acc=0: + [item + acc + shared_acc, acc + 1, shared_acc + 1] + + map item: + ...item ' to_match_snapshot - - it 'uses multiple custom iterable impl.', fn: + it 'handles await', fn: expect fink2js ' - {_map_} = import "./iter.fnk" - {_filter_} = import "./iter-filter.fnk" pipe [1, 2, 3]: - map item: item * 2 - map item: item / 2 - filter item: item % 2 == 0 + map await item: + item * 2 ' to_match_snapshot - it 'uses legacy iterables', fn: +describe 'custom runtime', fn: + + it 'uses multiple custom runtimes', fn: expect fink2js ' - {_map_, _filter_} = import "@fink/loxia:iter:legacy" + {_map_} = import "./iter.fnk" + {_filter_} = import "./iter-filter.fnk" pipe [1, 2, 3]: map item: item * 2 map item: item / 2 @@ -59,10 +51,11 @@ describe 'gen 2', fn: -describe 'map', fn: +describe 'map legacy', fn: it 'compiles single line', fn: expect fink2js ' + {_map_} = import "@fink/loxia:iter:legacy" map item: item * 2 ' to_match_snapshot @@ -71,6 +64,7 @@ describe 'map', fn: it 'compiles multi line', fn: expect fink2js ' + {_map_} = import "@fink/loxia:iter:legacy" map item: ni = foo item ni + 2 @@ -81,6 +75,7 @@ describe 'map', fn: it 'compiles single line with default value', fn: expect fink2js ' + {_map_} = import "@fink/loxia:iter:legacy" map item=123: item * 2 ' to_match_snapshot @@ -89,6 +84,7 @@ describe 'map', fn: it 'compiles single line with destructured obj', fn: expect fink2js ' + {_map_} = import "@fink/loxia:iter:legacy" map {item}: item * 2 ' to_match_snapshot @@ -97,6 +93,7 @@ describe 'map', fn: it 'compiles single line with destructured list', fn: expect fink2js ' + {_map_} = import "@fink/loxia:iter:legacy" map [x, y]: x + y ' to_match_snapshot @@ -105,6 +102,7 @@ describe 'map', fn: it 'compiles as flat map', fn: expect fink2js ' + {_map_} = import "@fink/loxia:iter:legacy" map [x, y]: ...[x, y] ' @@ -114,6 +112,7 @@ describe 'map', fn: it 'compiles with foo', fn: expect fink2js ' + {_map_} = import "@fink/loxia:iter:legacy" map {foo}: match foo: bar: spam @@ -124,6 +123,7 @@ describe 'map', fn: it 'compiles with acc', fn: expect fink2js ' + {_map_} = import "@fink/loxia:iter:legacy" map {foo}, acc=0: [foo + acc, acc+1] ' @@ -131,6 +131,7 @@ describe 'map', fn: expect fink2js ' + {_map_} = import "@fink/loxia:iter:legacy" map {foo}, acc=0: ...[[acc, foo], acc+1] ' @@ -140,6 +141,7 @@ describe 'map', fn: it 'compiles destructuring', fn: expect fink2js ' + {_map_} = import "@fink/loxia:iter:legacy" map [x, ...rest]: rest @@ -149,12 +151,13 @@ describe 'map', fn: to_match_snapshot - -describe 'map async', fn: - it 'compiles', fn: + it 'compiles async', fn: expect fink2js ' + {_map_} = import "@fink/loxia:iter:legacy" map item: await item * 2 ' - to_match_snapshot \ No newline at end of file + to_match_snapshot + + diff --git a/src/lang/iterable/map.test.fnk.snap b/src/lang/iterable/map.test.fnk.snap index d2e0560..e118942 100644 --- a/src/lang/iterable/map.test.fnk.snap +++ b/src/lang/iterable/map.test.fnk.snap @@ -1,69 +1,6 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`gen 2 compiles 1`] = ` -"import { _map_ } from \\"@fink/js-interop/iter.js\\"; - -_map_((item) => item * 2, 1, false, false);" -`; - -exports[`gen 2 compiles 2`] = ` -"import { _map_ } from \\"@fink/js-interop/iter.js\\"; - -_map_((item, acc = 0) => [item * 2 * acc, acc + 1], 2, false, false);" -`; - -exports[`gen 2 uses custom iterable impl. 1`] = ` -"import { _map_ } from \\"@fink/js-interop/iter.js\\"; - -_map_((item) => [item, item * 2], 1, false, true);" -`; - -exports[`gen 2 uses legacy iterables 1`] = ` -"{ - let ˆpipe_result_1 = [1, 2, 3]; - - ˆpipe_result_1 = function map(ˆitems_4) { - return { - *[Symbol.iterator]() { - for (const ˆitem_2 of ˆitems_4) { - const item = ˆitem_2; - const ˆresult_3 = item * 2; - yield ˆresult_3; - } - } - - }; - }(ˆpipe_result_1); - - ˆpipe_result_1 = function map(ˆitems_7) { - return { - *[Symbol.iterator]() { - for (const ˆitem_5 of ˆitems_7) { - const item = ˆitem_5; - const ˆresult_6 = item / 2; - yield ˆresult_6; - } - } - - }; - }(ˆpipe_result_1); - - ˆpipe_result_1 = function filter(ˆitems_10) { - return { - *[Symbol.iterator]() { - for (const ˆitem_8 of ˆitems_10) { - const item = ˆitem_8; - const ˆresult_9 = item % 2 === 0; - if (ˆresult_9) yield ˆitem_8; - } - } - - }; - }(ˆpipe_result_1); -}" -`; - -exports[`gen 2 uses multiple custom iterable impl. 1`] = ` +exports[`custom runtime uses multiple custom runtimes 1`] = ` "import { _map_ } from \\"./iter.js\\"; import { _filter_ } from \\"./iter-filter.js\\"; { @@ -74,14 +11,22 @@ import { _filter_ } from \\"./iter-filter.js\\"; }" `; -exports[`map async compiles 1`] = ` +exports[`map handles await 1`] = ` +"import { _map_ } from \\"@fink/js-interop/runtime.js\\"; +{ + let ˆpipe_result_1 = [1, 2, 3]; + ˆpipe_result_1 = _map_((item) => item * 2, 1, true, false)(ˆpipe_result_1); +}" +`; + +exports[`map legacy compiles as flat map 1`] = ` "(function map(ˆitems_3) { return { - async *[Symbol.asyncIterator]() { - for await (const ˆitem_1 of ˆitems_3) { - const item = ˆitem_1; - const ˆresult_2 = (await item) * 2; - yield ˆresult_2; + *[Symbol.iterator]() { + for (const ˆitem_1 of ˆitems_3) { + const [x, y] = ˆitem_1; + const ˆresult_2 = [x, y]; + yield* ˆresult_2; } } @@ -89,14 +34,14 @@ exports[`map async compiles 1`] = ` });" `; -exports[`map compiles as flat map 1`] = ` +exports[`map legacy compiles async 1`] = ` "(function map(ˆitems_3) { return { - *[Symbol.iterator]() { - for (const ˆitem_1 of ˆitems_3) { - const [x, y] = ˆitem_1; - const ˆresult_2 = [x, y]; - yield* ˆresult_2; + async *[Symbol.asyncIterator]() { + for await (const ˆitem_1 of ˆitems_3) { + const item = ˆitem_1; + const ˆresult_2 = (await item) * 2; + yield ˆresult_2; } } @@ -104,7 +49,7 @@ exports[`map compiles as flat map 1`] = ` });" `; -exports[`map compiles destructuring 1`] = ` +exports[`map legacy compiles destructuring 1`] = ` "(function map(ˆitems_3) { return { *[Symbol.iterator]() { @@ -139,7 +84,7 @@ exports[`map compiles destructuring 1`] = ` });" `; -exports[`map compiles multi line 1`] = ` +exports[`map legacy compiles multi line 1`] = ` "(function map(ˆitems_3) { return { *[Symbol.iterator]() { @@ -155,7 +100,7 @@ exports[`map compiles multi line 1`] = ` });" `; -exports[`map compiles single line 1`] = ` +exports[`map legacy compiles single line 1`] = ` "(function map(ˆitems_3) { return { *[Symbol.iterator]() { @@ -170,7 +115,7 @@ exports[`map compiles single line 1`] = ` });" `; -exports[`map compiles single line with default value 1`] = ` +exports[`map legacy compiles single line with default value 1`] = ` "(function map(ˆitems_3) { return { *[Symbol.iterator]() { @@ -185,7 +130,7 @@ exports[`map compiles single line with default value 1`] = ` });" `; -exports[`map compiles single line with destructured list 1`] = ` +exports[`map legacy compiles single line with destructured list 1`] = ` "(function map(ˆitems_3) { return { *[Symbol.iterator]() { @@ -200,7 +145,7 @@ exports[`map compiles single line with destructured list 1`] = ` });" `; -exports[`map compiles single line with destructured obj 1`] = ` +exports[`map legacy compiles single line with destructured obj 1`] = ` "(function map(ˆitems_3) { return { *[Symbol.iterator]() { @@ -217,7 +162,7 @@ exports[`map compiles single line with destructured obj 1`] = ` });" `; -exports[`map compiles with acc 1`] = ` +exports[`map legacy compiles with acc 1`] = ` "(function map(ˆitems_5) { return { *[Symbol.iterator]() { @@ -238,7 +183,7 @@ exports[`map compiles with acc 1`] = ` });" `; -exports[`map compiles with acc 2`] = ` +exports[`map legacy compiles with acc 2`] = ` "(function map(ˆitems_5) { return { *[Symbol.iterator]() { @@ -259,7 +204,7 @@ exports[`map compiles with acc 2`] = ` });" `; -exports[`map compiles with foo 1`] = ` +exports[`map legacy compiles with foo 1`] = ` "(function map(ˆitems_5) { return { *[Symbol.iterator]() { @@ -288,3 +233,14 @@ exports[`map compiles with foo 1`] = ` }; });" `; + +exports[`map uses default runtime 1`] = ` +"import { _map_ } from \\"@fink/js-interop/runtime.js\\"; +{ + let ˆpipe_result_1 = [1, 2, 3]; + ˆpipe_result_1 = _map_((item) => item * 2, 1, false, false)(ˆpipe_result_1); + ˆpipe_result_1 = _map_((item, acc = 0) => [item + acc, acc + 1], 2, false, false)(ˆpipe_result_1); + ˆpipe_result_1 = _map_((item, acc = 0, shared_acc = 0) => [item + acc + shared_acc, acc + 1, shared_acc + 1], 3, false, false)(ˆpipe_result_1); + ˆpipe_result_1 = _map_((item) => item, 1, false, true)(ˆpipe_result_1); +}" +`; diff --git a/src/lang/iterable/unfold.test.fnk b/src/lang/iterable/unfold.test.fnk index 95a8571..f33d34d 100644 --- a/src/lang/iterable/unfold.test.fnk +++ b/src/lang/iterable/unfold.test.fnk @@ -3,11 +3,17 @@ -describe 'gen 2', fn: - it 'compiles', fn: +describe 'unfold', fn: + it 'compiles with default runtime', fn: + expect + fink2js ' + unfold : + 123 + ' + to_match_snapshot + expect fink2js ' - {_unfold_} = import "@fink/js-interop/iter.js" unfold prev: prev + 1 ' @@ -15,7 +21,6 @@ describe 'gen 2', fn: expect fink2js ' - {_unfold_} = import "@fink/js-interop/iter.js" unfold prev=1: prev + 1 ' @@ -23,21 +28,37 @@ describe 'gen 2', fn: expect fink2js ' - {_unfold_} = import "@fink/js-interop/iter.js" unfold prev=1, acc=1: [prev * accu, accu + 1] ' to_match_snapshot + expect + fink2js ' + unfold prev=1, acc=1, shared_accu: + [prev * accu, accu + 1, shared_accu + 1] + ' + to_match_snapshot + it 'compiles with custom runtime', fn: + expect + fink2js ' + {_unfold_} = import "./iter.fnk" + unfold prev: + prev + 1 + ' + to_match_snapshot -describe 'unfold', fn: + + +describe 'unfold legacy', fn: it 'compiles with previous', fn: expect fink2js ' + {_unfold_} = import "@fink/loxia:iter:legacy" unfold prev: prev + 1 ' @@ -47,6 +68,7 @@ describe 'unfold', fn: it 'compiles with default', fn: expect fink2js ' + {_unfold_} = import "@fink/loxia:iter:legacy" unfold prev=1: prev + 1 ' @@ -56,6 +78,7 @@ describe 'unfold', fn: it 'compiles with default and accu', fn: expect fink2js ' + {_unfold_} = import "@fink/loxia:iter:legacy" unfold prev=1, acc=1: [prev * accu, accu + 1] ' @@ -65,6 +88,7 @@ describe 'unfold', fn: it 'compiles without args', fn: expect fink2js ' + {_unfold_} = import "@fink/loxia:iter:legacy" unfold: 1234 ' @@ -74,6 +98,7 @@ describe 'unfold', fn: it 'compiles with spread', fn: expect fink2js ' + {_unfold_} = import "@fink/loxia:iter:legacy" unfold: ...[1, 2, 3] ' diff --git a/src/lang/iterable/unfold.test.fnk.snap b/src/lang/iterable/unfold.test.fnk.snap index 2a00b7f..e56dee1 100644 --- a/src/lang/iterable/unfold.test.fnk.snap +++ b/src/lang/iterable/unfold.test.fnk.snap @@ -1,24 +1,42 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`gen 2 compiles 1`] = ` -"import { _unfold_ } from \\"@fink/js-interop/iter.js\\"; +exports[`unfold compiles with custom runtime 1`] = ` +"import { _unfold_ } from \\"./iter.js\\"; _unfold_((prev) => prev + 1, 1, false, false);" `; -exports[`gen 2 compiles 2`] = ` -"import { _unfold_ } from \\"@fink/js-interop/iter.js\\"; +exports[`unfold compiles with default runtime 1`] = ` +"import { _unfold_ } from \\"@fink/js-interop/runtime.js\\"; + +_unfold_(() => 123, 0, false, false);" +`; + +exports[`unfold compiles with default runtime 2`] = ` +"import { _unfold_ } from \\"@fink/js-interop/runtime.js\\"; + +_unfold_((prev) => prev + 1, 1, false, false);" +`; + +exports[`unfold compiles with default runtime 3`] = ` +"import { _unfold_ } from \\"@fink/js-interop/runtime.js\\"; _unfold_((prev = 1) => prev + 1, 1, false, false);" `; -exports[`gen 2 compiles 3`] = ` -"import { _unfold_ } from \\"@fink/js-interop/iter.js\\"; +exports[`unfold compiles with default runtime 4`] = ` +"import { _unfold_ } from \\"@fink/js-interop/runtime.js\\"; _unfold_((prev = 1, acc = 1) => [prev * accu, accu + 1], 2, false, false);" `; -exports[`unfold compiles with default 1`] = ` +exports[`unfold compiles with default runtime 5`] = ` +"import { _unfold_ } from \\"@fink/js-interop/runtime.js\\"; + +_unfold_((prev = 1, acc = 1, shared_accu) => [prev * accu, accu + 1, shared_accu + 1], 3, false, false);" +`; + +exports[`unfold legacy compiles with default 1`] = ` "(function unfold(ˆinitial_3) { return { *[Symbol.iterator]() { @@ -36,7 +54,7 @@ exports[`unfold compiles with default 1`] = ` });" `; -exports[`unfold compiles with default and accu 1`] = ` +exports[`unfold legacy compiles with default and accu 1`] = ` "(function unfold(ˆinitial_5) { return { *[Symbol.iterator]() { @@ -57,7 +75,7 @@ exports[`unfold compiles with default and accu 1`] = ` });" `; -exports[`unfold compiles with previous 1`] = ` +exports[`unfold legacy compiles with previous 1`] = ` "(function unfold(ˆinitial_3) { return { *[Symbol.iterator]() { @@ -75,7 +93,7 @@ exports[`unfold compiles with previous 1`] = ` });" `; -exports[`unfold compiles with spread 1`] = ` +exports[`unfold legacy compiles with spread 1`] = ` "(function unfold(ˆinitial_3) { return { *[Symbol.iterator]() { @@ -92,7 +110,7 @@ exports[`unfold compiles with spread 1`] = ` });" `; -exports[`unfold compiles without args 1`] = ` +exports[`unfold legacy compiles without args 1`] = ` "(function unfold(ˆinitial_3) { return { *[Symbol.iterator]() { diff --git a/src/lang/iterable/until.test.fnk b/src/lang/iterable/until.test.fnk index 6416139..6f324d6 100644 --- a/src/lang/iterable/until.test.fnk +++ b/src/lang/iterable/until.test.fnk @@ -3,11 +3,10 @@ -describe 'gen 2', fn: - it 'compiles', fn: +describe 'until', fn: + it 'compiles with default runtime', fn: expect fink2js ' - {_until_} = import "@fink/js-interop/iter.js" until item: item < 10 ' @@ -15,18 +14,34 @@ describe 'gen 2', fn: expect fink2js ' - {_until_} = import "@fink/js-interop/iter.js" until item, cntr=0: [cntr > 2, cntr + 1] ' to_match_snapshot + expect + fink2js ' + until item, cntr=0, shared_accu: + [cntr > 2, cntr + 1, shared_accu + 1] + ' + to_match_snapshot -describe 'until', fn: + it 'compiles with custom runtime', fn: + fink2js ' + {_until_} = import "./iter.fnk" + until item: + item < 10 + ' + to_match_snapshot + + + +describe 'until legacy', fn: it 'compiles', fn: expect fink2js ' + {_until_} = import "@fink/loxia:iter:legacy" until item: item < 10 ' @@ -36,6 +51,7 @@ describe 'until', fn: it 'compiles with accu', fn: expect fink2js ' + {_until_} = import "@fink/loxia:iter:legacy" until _, cntr=0: [cntr > 2, cntr + 1] @@ -48,6 +64,7 @@ describe 'until', fn: it 'compiles destructuring', fn: expect fink2js ' + {_until_} = import "@fink/loxia:iter:legacy" until [foo, ...bar]: foo_alone = foo and is_empty bar foo_alone @@ -63,6 +80,7 @@ describe 'until async', fn: it 'compiles', fn: expect fink2js ' + {_until_} = import "@fink/loxia:iter:legacy" until item: await item < 10 ' diff --git a/src/lang/iterable/until.test.fnk.snap b/src/lang/iterable/until.test.fnk.snap index 5e220b9..506b7f5 100644 --- a/src/lang/iterable/until.test.fnk.snap +++ b/src/lang/iterable/until.test.fnk.snap @@ -1,17 +1,5 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`gen 2 compiles 1`] = ` -"import { _until_ } from \\"@fink/js-interop/iter.js\\"; - -_until_((item) => item < 10, 1, false, false);" -`; - -exports[`gen 2 compiles 2`] = ` -"import { _until_ } from \\"@fink/js-interop/iter.js\\"; - -_until_((item, cntr = 0) => [cntr > 2, cntr + 1], 2, false, false);" -`; - exports[`until async compiles 1`] = ` "(function filter_until(ˆitems_3) { return { @@ -28,7 +16,25 @@ exports[`until async compiles 1`] = ` });" `; -exports[`until compiles 1`] = ` +exports[`until compiles with default runtime 1`] = ` +"import { _until_ } from \\"@fink/js-interop/runtime.js\\"; + +_until_((item) => item < 10, 1, false, false);" +`; + +exports[`until compiles with default runtime 2`] = ` +"import { _until_ } from \\"@fink/js-interop/runtime.js\\"; + +_until_((item, cntr = 0) => [cntr > 2, cntr + 1], 2, false, false);" +`; + +exports[`until compiles with default runtime 3`] = ` +"import { _until_ } from \\"@fink/js-interop/runtime.js\\"; + +_until_((item, cntr = 0, shared_accu) => [cntr > 2, cntr + 1, shared_accu + 1], 3, false, false);" +`; + +exports[`until legacy compiles 1`] = ` "(function filter_until(ˆitems_3) { return { *[Symbol.iterator]() { @@ -44,7 +50,7 @@ exports[`until compiles 1`] = ` });" `; -exports[`until compiles destructuring 1`] = ` +exports[`until legacy compiles destructuring 1`] = ` "(function filter_until(ˆitems_3) { return { *[Symbol.iterator]() { @@ -83,7 +89,7 @@ exports[`until compiles destructuring 1`] = ` });" `; -exports[`until compiles with accu 1`] = ` +exports[`until legacy compiles with accu 1`] = ` "(function filter_until(ˆitems_5) { return { *[Symbol.iterator]() { diff --git a/src/lang/iterable/while.test.fnk b/src/lang/iterable/while.test.fnk index 94559d9..6eeafa7 100644 --- a/src/lang/iterable/while.test.fnk +++ b/src/lang/iterable/while.test.fnk @@ -3,11 +3,10 @@ -describe 'gen 2', fn: - it 'compiles', fn: +describe 'while', fn: + it 'compiles with default runtime', fn: expect fink2js ' - {_while_} = import "@fink/js-interop/iter.js" while item: item < 10 ' @@ -15,19 +14,34 @@ describe 'gen 2', fn: expect fink2js ' - {_while_} = import "@fink/js-interop/iter.js" while item, cntr=0: [cntr > 2, cntr + 1] ' to_match_snapshot + expect + fink2js ' + while item, cntr=0, shared_accu: + [cntr > 2, cntr + 1, shared_accu + 1] + ' + to_match_snapshot + it 'compiles with custom runtime', fn: + fink2js ' + {_while_} = import "./iter.fnk" + while item: + item < 10 + ' + to_match_snapshot -describe 'while', fn: + + +describe 'while legacy', fn: it 'compiles', fn: expect fink2js ' + {_while_} = import "@fink/loxia:iter:legacy" while item: item < 10 ' @@ -37,6 +51,8 @@ describe 'while', fn: it 'compiles with accu', fn: expect fink2js ' + {_while_} = import "@fink/loxia:iter:legacy" + while _, cntr=0: [cntr < 2, cntr + 1] @@ -52,6 +68,7 @@ describe 'while', fn: it 'compiles destructuring', fn: expect fink2js ' + {_while_} = import "@fink/loxia:iter:legacy" while [foo, ...bar]: foo_alone = foo and is_empty bar foo_alone @@ -67,6 +84,7 @@ describe 'while async', fn: it 'compiles', fn: expect fink2js ' + {_while_} = import "@fink/loxia:iter:legacy" while item: await item < 10 ' diff --git a/src/lang/iterable/while.test.fnk.snap b/src/lang/iterable/while.test.fnk.snap index e43dcea..a75c6f8 100644 --- a/src/lang/iterable/while.test.fnk.snap +++ b/src/lang/iterable/while.test.fnk.snap @@ -1,17 +1,5 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`gen 2 compiles 1`] = ` -"import { _while_ } from \\"@fink/js-interop/iter.js\\"; - -_while_((item) => item < 10, 1, false, false);" -`; - -exports[`gen 2 compiles 2`] = ` -"import { _while_ } from \\"@fink/js-interop/iter.js\\"; - -_while_((item, cntr = 0) => [cntr > 2, cntr + 1], 2, false, false);" -`; - exports[`while async compiles 1`] = ` "(function filter_while(ˆitems_3) { return { @@ -28,7 +16,25 @@ exports[`while async compiles 1`] = ` });" `; -exports[`while compiles 1`] = ` +exports[`while compiles with default runtime 1`] = ` +"import { _while_ } from \\"@fink/js-interop/runtime.js\\"; + +_while_((item) => item < 10, 1, false, false);" +`; + +exports[`while compiles with default runtime 2`] = ` +"import { _while_ } from \\"@fink/js-interop/runtime.js\\"; + +_while_((item, cntr = 0) => [cntr > 2, cntr + 1], 2, false, false);" +`; + +exports[`while compiles with default runtime 3`] = ` +"import { _while_ } from \\"@fink/js-interop/runtime.js\\"; + +_while_((item, cntr = 0, shared_accu) => [cntr > 2, cntr + 1, shared_accu + 1], 3, false, false);" +`; + +exports[`while legacy compiles 1`] = ` "(function filter_while(ˆitems_3) { return { *[Symbol.iterator]() { @@ -44,7 +50,7 @@ exports[`while compiles 1`] = ` });" `; -exports[`while compiles destructuring 1`] = ` +exports[`while legacy compiles destructuring 1`] = ` "(function filter_while(ˆitems_3) { return { *[Symbol.iterator]() { @@ -82,7 +88,7 @@ exports[`while compiles destructuring 1`] = ` });" `; -exports[`while compiles with accu 1`] = ` +exports[`while legacy compiles with accu 1`] = ` "(function filter_while(ˆitems_5) { return { *[Symbol.iterator]() { diff --git a/src/lang/logical/in.fnk b/src/lang/logical/in.fnk index cda5f43..9021b54 100644 --- a/src/lang/logical/in.fnk +++ b/src/lang/logical/in.fnk @@ -10,8 +10,6 @@ babel_types = import '@babel/types' legacy_uri = '@fink/loxia:iter:legacy' -default_in_impl_uri = legacy_uri - transform_legacy = fn node, ctx: @@ -82,6 +80,7 @@ transform_legacy = fn node, ctx: transform_with_runtime_lib = fn runtime_fn, node, ctx: + # TODO: if right hand side is a literal, we could optimize as legacy did [right, next_ctx] = transform node.right, ctx [left, end_ctx] = transform node.left, next_ctx @@ -100,7 +99,6 @@ transform_in = fn node, ctx: match next_ctx: legacy_uri == runtime_impl runtime_fn, ?: transform_legacy node, next_ctx - else: transform_with_runtime_lib runtime_fn, node, next_ctx diff --git a/src/lang/logical/in.test.fnk b/src/lang/logical/in.test.fnk index abbca1c..f06cbe7 100644 --- a/src/lang/logical/in.test.fnk +++ b/src/lang/logical/in.test.fnk @@ -3,7 +3,7 @@ describe 'in', fn: - it 'shortcuts known with knwon right hand side', fn: + it 'compiles with default runtime', fn: expect fink2js " foo = 1 in [1, 2, 3] @@ -14,10 +14,10 @@ describe 'in', fn: to_match_snapshot - it 'compiles with explicit legacy impl', fn: + it 'compiles with custom runtime', fn: expect fink2js " - {_in_} = import '@fink/loxia:iter:legacy' + {_in_} = import './foo.fnk' foo = 1 in [1, 2, 3] bar = '2' in '1234' spam = key in {foo: bar} @@ -26,13 +26,16 @@ describe 'in', fn: to_match_snapshot - it 'compiles with custom builin lib', fn: + it 'compiles with legacy', fn: expect fink2js " - {_in_} = import './foo.fnk' + {_in_} = import '@fink/loxia:iter:legacy' foo = 1 in [1, 2, 3] bar = '2' in '1234' spam = key in {foo: bar} shrub = item in ni " to_match_snapshot + + + diff --git a/src/lang/logical/in.test.fnk.snap b/src/lang/logical/in.test.fnk.snap index 48b5591..b1a3854 100644 --- a/src/lang/logical/in.test.fnk.snap +++ b/src/lang/logical/in.test.fnk.snap @@ -1,6 +1,6 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`in compiles with custom builin lib 1`] = ` +exports[`in compiles with custom runtime 1`] = ` "import { _in_ } from \\"./foo.js\\"; export const foo = _in_(1, [1, 2, 3]); export const bar = _in_(\`2\`, \`1234\`); @@ -10,16 +10,17 @@ export const spam = _in_(key, { export const shrub = _in_(item, ni);" `; -exports[`in compiles with explicit legacy impl 1`] = ` -"export const foo = [1, 2, 3].includes(1); -export const bar = \`1234\`.includes(\`2\`); -export const spam = { +exports[`in compiles with default runtime 1`] = ` +"import { _in_ } from \\"@fink/js-interop/runtime.js\\"; +export const foo = _in_(1, [1, 2, 3]); +export const bar = _in_(\`2\`, \`1234\`); +export const spam = _in_(key, { foo: bar -}.hasOwnProperty(key); -export const shrub = ni?.includes?.(item) ?? ni?.has?.(item) ?? ni?.hasOwnProperty?.(item) ?? false;" +}); +export const shrub = _in_(item, ni);" `; -exports[`in shortcuts known with knwon right hand side 1`] = ` +exports[`in compiles with legacy 1`] = ` "export const foo = [1, 2, 3].includes(1); export const bar = \`1234\`.includes(\`2\`); export const spam = { diff --git a/src/lang/logical/init.fnk b/src/lang/logical/init.fnk index 3ea5636..db7979e 100644 --- a/src/lang/logical/init.fnk +++ b/src/lang/logical/init.fnk @@ -1,10 +1,10 @@ babel_types = import '@babel/types' {logicalExpression, unaryExpression} = babel_types -{add, add_with_builtin, any} = import '../context.fnk' +{add, add_with_runtime, any} = import '../context.fnk' {transform} = import '../transform.fnk' -{transform_in, default_in_impl_uri} = import './in.fnk' +{transform_in} = import './in.fnk' @@ -40,4 +40,4 @@ add_logical = fn ctx: add any, 'or', transform_logical add any, 'not', transform_not # TODO: should this live in iterables? - add_with_builtin any, 'in', default_in_impl_uri, transform_in + add_with_runtime any, 'in', transform_in diff --git a/src/lang/runtime.fnk b/src/lang/runtime.fnk index 0406f80..465990f 100644 --- a/src/lang/runtime.fnk +++ b/src/lang/runtime.fnk @@ -1,6 +1,8 @@ {is_str} = import '@fink/std-lib/str.fnk' +default_in_runtime = '@fink/js-interop/runtime.js' + add_runtime_fn = fn name, ctx: {runtime} = ctx @@ -12,7 +14,7 @@ add_runtime_fn = fn name, ctx: -set_runtime_impl = fn name, uri, ctx: +set_runtime_impl = fn name, uri=default_in_runtime, ctx: {runtime} = ctx overrides = match runtime: @@ -82,7 +84,7 @@ get_runtime_imports = fn ctx: -init_builtins = fn ctx: +init_runtimes = fn ctx: rec: ...ctx runtime: rec: