From 6babfa3435538928acd25566b12135777b4345bc Mon Sep 17 00:00:00 2001 From: Felix Roos Date: Wed, 16 Feb 2022 20:10:35 +0100 Subject: [PATCH] add Tone namespace + allow functions --- repl/src/evaluate.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/repl/src/evaluate.ts b/repl/src/evaluate.ts index abdb63a0..712ba780 100644 --- a/repl/src/evaluate.ts +++ b/repl/src/evaluate.ts @@ -6,6 +6,8 @@ import './tonal'; import './groove'; import shapeshifter from './shapeshifter'; import { minify } from './parse'; +import * as Tone from 'tone'; +import * as toneHelpers from './tone'; // this will add all methods from definedMethod to strudel + connect all the partial application stuff const bootstrapped: any = { ...strudel, ...strudel.Pattern.prototype.bootstrap() }; @@ -25,11 +27,16 @@ function hackLiteral(literal, names, func) { hackLiteral(String, ['mini', 'm'], bootstrapped.mini); // comment out this line if you panic hackLiteral(String, ['pure', 'p'], bootstrapped.pure); // comment out this line if you panic -Object.assign(globalThis, bootstrapped); // this will add contents of bootstrapped to scope (used by eval) +// this will add everything to global scope, which is accessed by eval +Object.assign(globalThis, bootstrapped, Tone, toneHelpers); export const evaluate: any = (code: string) => { const shapeshifted = shapeshifter(code); // transform syntactically correct js code to semantically usable code - const pattern = minify(eval(shapeshifted)); // eval and minify (if user entered a string) + let evaluated = eval(shapeshifted); + if (typeof evaluated === 'function') { + evaluated = evaluated(); + } + const pattern = minify(evaluated); // eval and minify (if user entered a string) if (pattern?.constructor?.name !== 'Pattern') { const message = `got "${typeof pattern}" instead of pattern`; throw new Error(message + (typeof pattern === 'function' ? ', did you forget to call a function?' : '.'));