forked from squint-cljs/squint
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
24 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,3 +36,4 @@ aoc_3.cljs | |
aoc_3.mjs | ||
libtests | ||
tmp | ||
!test-resources/js_api.mjs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { compileStringEx } from 'squint-cljs/index.js'; | ||
|
||
// repl = output suitable for REPL | ||
// async = start in async mode (allows top level awaits) | ||
// context: 'return' = start in return context since we're going to wrap the result in a self-calling function | ||
// elide-exports: do not emit exports, since they cannot be evaluated by `eval` | ||
const opts = {repl: true, async: true, context: 'return', "elide-exports": true}; | ||
let state = null; | ||
state = compileStringEx('(ns foo) (def x 1) x', opts, state); | ||
|
||
// we evaluate REPL output in an async function since it may contain top level awaits | ||
function wrappedInAsyncFn(s) { | ||
return `(async function() {\n${s}\n})()`; | ||
} | ||
|
||
// the following gives 1 like expected | ||
console.log(await eval(wrappedInAsyncFn(state.javascript))); | ||
|
||
// pass res1 which contains compiler state | ||
state = compileStringEx('x', opts, state); | ||
// since we're evaluating x in the same namespace it was defined in, evaluating | ||
// the returned javascript gives 1 | ||
console.log(await eval(wrappedInAsyncFn(state.javascript))); |