Skip to content

Commit

Permalink
Add test file
Browse files Browse the repository at this point in the history
  • Loading branch information
borkdude committed Dec 1, 2023
1 parent ff1aa50 commit 6f6c313
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ aoc_3.cljs
aoc_3.mjs
libtests
tmp
!test-resources/js_api.mjs
23 changes: 23 additions & 0 deletions test-resources/js_api.mjs
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)));

0 comments on commit 6f6c313

Please sign in to comment.