Skip to content

Commit

Permalink
fix: auto import runtime funcs for in
Browse files Browse the repository at this point in the history
  • Loading branch information
kollhof committed Sep 19, 2021
1 parent 12d3369 commit a39dfce
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/js/conditionals/match.test.fnk.snap
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,8 @@ export const ;"
`;
exports[`match records matches value assertions 1`] = `
"let ret_0;
"import { _in_ } from \\"@fink/js-interop/runtime.js\\";
let ret_0;
ret_0: {
const key_1 = \`spam\`;
Expand Down
2 changes: 2 additions & 0 deletions src/js/context.fnk
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ get_js = fn id, {values}:
identifier id


add_runtime_requirement = fn func, {runtime, ...ctx}:
{...ctx, runtime: {...runtime, (func): true}}


get_js_literal = fn id, ctx:
Expand Down
2 changes: 1 addition & 1 deletion src/js/init.fnk
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ transform_ir = fn input_exprs, options:
exprs = replace_idents input_exprs
rev_expr = reverse exprs

ctx = pipe {trasformers: {}, values: {}, ...options}:
ctx = pipe {runtime: {}, trasformers: {}, values: {}, ...options}:
init_ctx exprs, ?
prepare_rets rev_expr, ?
prepare_imports rev_expr, ?
Expand Down
6 changes: 4 additions & 2 deletions src/js/logical/in.fnk
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
types = import '@babel/types'
{identifier, callExpression} = types

{add, get_js, set_js} = import '../context.fnk'
{add, get_js, set_js, add_runtime_requirement} = import '../context.fnk'



Expand All @@ -14,7 +14,9 @@ transform_in = fn expr, ctx:
get_js left_id, ctx
get_js right_id, ctx

set_js result_id, js, ctx
pipe ctx:
set_js result_id, js, ?
add_runtime_requirement 'in', ?



Expand Down
4 changes: 3 additions & 1 deletion src/js/logical/in.test.fnk.snap
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ export const shrub = _in_(item, ni);"
exports[`in compiles with custom runtime import 1`] = `"import { _in_ } from \\"./foo.js\\";"`;

exports[`in compiles with default runtime 1`] = `
"const foo_0 = _in_(1, [1, x, 3]);
"import { _in_ } from \\"@fink/js-interop/runtime.js\\";
const foo_0 = _in_(1, [1, x, 3]);
const bar_0 = _in_(\`2\`, \`1234\`);
Expand Down
17 changes: 16 additions & 1 deletion src/js/module/import.fnk
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ types = import '@babel/types'




resolve_ext = fn import_url, options:
match import_url:
not ends_with ?, '.fnk':
Expand All @@ -30,6 +29,22 @@ resolve_ext = fn import_url, options:



get_runtime_imports = fn ctx:
specs = match ctx:
{runtime.in: true}:
list:
importSpecifier
identifier '_in_'
identifier '_in_'

else: []

match specs:
[?]: [importDeclaration specs, stringLiteral '@fink/js-interop/runtime.js']
else: []




transform_import = fn expr, ctx:
[ , [uri_id, key_id=false], [res_id=false]] = expr
Expand Down
6 changes: 5 additions & 1 deletion src/js/module/init.fnk
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ types = import '@babel/types'
{add, get_js, set_js, get_js_literal} = import '../context.fnk'
{transform_exprs} = import '../transform.fnk'
{escape_ident} = import '../identifier/init.fnk'
{get_runtime_imports} = import './import.fnk'


[no_result] = []
Expand Down Expand Up @@ -97,6 +98,7 @@ transform_mod_exports = fn expr, ctx:




transfrom_module = fn exprs, ctx:
[body, next_ctx] = transform_exprs exprs, ctx
[..., last] = exprs
Expand All @@ -111,9 +113,11 @@ transfrom_module = fn exprs, ctx:
else:
[]

runtime_imports = get_runtime_imports next_ctx

js = file
program
body
[...runtime_imports, ...body]
[]
'module'
...directives
Expand Down

0 comments on commit a39dfce

Please sign in to comment.