add Tone namespace + allow functions

This commit is contained in:
Felix Roos 2022-02-16 20:10:35 +01:00
parent ef9527d691
commit 6babfa3435

View File

@ -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?' : '.'));