add extend to eval to control scope

This commit is contained in:
Felix Roos 2022-03-25 23:28:30 +01:00
parent 54d41410fb
commit 2f13e425c6
2 changed files with 35 additions and 48 deletions

View File

@ -1,53 +1,13 @@
import shapeshifter from './shapeshifter.mjs';
import * as strudel from '@strudel/core/strudel.mjs';
import '@strudel/tone/tone.mjs';
import '@strudel/midi/midi.mjs';
import '@strudel/tonal/voicings.mjs';
import '@strudel/tonal/tonal.mjs';
import '@strudel/xen/xen.mjs';
import '@strudel/xen/tune.mjs';
import '@strudel/core/euclid.mjs';
import euclid from '@strudel/core/euclid.mjs';
import '@strudel/tone/pianoroll.mjs';
import '@strudel/tone/draw.mjs';
import * as uiHelpers from '@strudel/tone/ui.mjs';
import * as drawHelpers from '@strudel/tone/draw.mjs';
import gist from '@strudel/core/gist.js';
import { mini } from '@strudel/mini/mini.mjs';
import { Tone } from '@strudel/tone';
import * as toneHelpers from '@strudel/tone/tone.mjs';
import * as voicingHelpers from '@strudel/tonal/voicings.mjs';
// this will add all methods from definedMethod to strudel + connect all the partial application stuff
const bootstrapped = { ...strudel, ...strudel.Pattern.prototype.bootstrap() };
// console.log('bootstrapped',bootstrapped.transpose(2).transpose);
function hackLiteral(literal, names, func) {
names.forEach((name) => {
Object.defineProperty(literal.prototype, name, {
get: function () {
return func(String(this));
},
});
});
}
// with this, you can do 'c2 [eb2 g2]'.mini.fast(2) or 'c2 [eb2 g2]'.m.fast(2),
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
// this will add everything to global scope, which is accessed by eval
Object.assign(globalThis, Tone, bootstrapped, toneHelpers, voicingHelpers, drawHelpers, uiHelpers, {
gist,
euclid,
mini,
Tone,
});
export const extend = (...args) => {
// TODO: find a way to make args available to eval without adding it to global scope...
// sadly, "with" does not work in strict mode
Object.assign(window, ...args);
};
export const evaluate = async (code) => {
const shapeshifted = shapeshifter(code); // transform syntactically correct js code to semantically usable code
drawHelpers.cleanup();
uiHelpers.cleanup();
let evaluated = await eval(shapeshifted);
if (!isPattern(evaluated)) {
console.log('evaluated', evaluated);

View File

@ -1,16 +1,41 @@
import React, { useCallback, useLayoutEffect, useRef, useState } from 'react';
import CodeMirror, { markEvent, markParens } from './CodeMirror';
import cx from './cx';
import { evaluate } from '@strudel/eval';
import { evaluate, extend } from '@strudel/eval';
import logo from './logo.svg';
import playStatic from './static.mjs';
import { defaultSynth } from '@strudel/tone';
import * as tunes from './tunes.mjs';
import useRepl from './useRepl.mjs';
import { useWebMidi } from './useWebMidi';
import './App.css'
import './App.css';
// eval stuff start
import * as strudel from '@strudel/core/strudel.mjs';
import gist from '@strudel/core/gist.js';
import { mini } from '@strudel/mini/mini.mjs';
import { Tone } from '@strudel/tone';
import * as toneHelpers from '@strudel/tone/tone.mjs';
import * as voicingHelpers from '@strudel/tonal/voicings.mjs';
import * as uiHelpers from '@strudel/tone/ui.mjs';
import * as drawHelpers from '@strudel/tone/draw.mjs';
import euclid from '@strudel/core/euclid.mjs';
import '@strudel/tone/tone.mjs';
import '@strudel/midi/midi.mjs';
import '@strudel/tonal/voicings.mjs';
import '@strudel/tonal/tonal.mjs';
import '@strudel/xen/xen.mjs';
import '@strudel/xen/tune.mjs';
import '@strudel/core/euclid.mjs';
import '@strudel/tone/pianoroll.mjs';
import '@strudel/tone/draw.mjs';
// TODO: use https://www.npmjs.com/package/@monaco-editor/react
extend(Tone, strudel, strudel.Pattern.prototype.bootstrap(), toneHelpers, voicingHelpers, drawHelpers, uiHelpers, {
gist,
euclid,
mini,
Tone,
});
// eval stuff end
const [_, codeParam] = window.location.href.split('#');
let decoded;
@ -118,6 +143,8 @@ function App() {
const _code = getRandomTune();
console.log('tune', _code); // uncomment this to debug when random code fails
setCode(_code);
drawHelpers.cleanup();
uiHelpers.cleanup();
const parsed = await evaluate(_code);
setPattern(parsed.pattern);
}}