weird string replacement experiments

This commit is contained in:
2023-08-19 23:38:31 +02:00
parent 63958a572d
commit c8090b4137
2 changed files with 43 additions and 28 deletions

View File

@ -55,7 +55,7 @@ export class UserAPI {
*/ */
private variables: { [key: string]: any } = {}; private variables: { [key: string]: any } = {};
private iterators: { [key: string]: any } = {}; private counters: { [key: string]: any } = {};
private _drunk: DrunkWalk = new DrunkWalk(-100, 100, false); private _drunk: DrunkWalk = new DrunkWalk(-100, 100, false);
MidiConnection: MidiConnection = new MidiConnection(); MidiConnection: MidiConnection = new MidiConnection();
@ -298,56 +298,60 @@ export class UserAPI {
}; };
// ============================================================= // =============================================================
// Iterator related functions // Counter related functions
// ============================================================= // =============================================================
public iterator = (name: string, limit?: number, step?: number): number => { public counter = (
name: string | number,
limit?: number,
step?: number
): number => {
/** /**
* Returns the current value of an iterator, and increments it by the step value. * Returns the current value of a counter, and increments it by the step value.
* *
* @param name - The name of the iterator * @param name - The name of the counter
* @param limit - The upper limit of the iterator * @param limit - The upper limit of the counter
* @param step - The step value of the iterator * @param step - The step value of the counter
* @returns The current value of the iterator * @returns The current value of the counter
*/ */
if (!(name in this.iterators)) { if (!(name in this.counters)) {
// Create new iterator with default step of 1 // Create new counter with default step of 1
this.iterators[name] = { this.counters[name] = {
value: 0, value: 0,
step: step ?? 1, step: step ?? 1,
limit, limit,
}; };
} else { } else {
// Check if limit has changed // Check if limit has changed
if (this.iterators[name].limit !== limit) { if (this.counters[name].limit !== limit) {
// Reset value to 0 and update limit // Reset value to 0 and update limit
this.iterators[name].value = 0; this.counters[name].value = 0;
this.iterators[name].limit = limit; this.counters[name].limit = limit;
} }
// Check if step has changed // Check if step has changed
if (this.iterators[name].step !== step) { if (this.counters[name].step !== step) {
// Update step // Update step
this.iterators[name].step = step ?? this.iterators[name].step; this.counters[name].step = step ?? this.counters[name].step;
} }
// Increment existing iterator by step value // Increment existing iterator by step value
this.iterators[name].value += this.iterators[name].step; this.counters[name].value += this.counters[name].step;
// Check for limit overshoot // Check for limit overshoot
if ( if (
this.iterators[name].limit !== undefined && this.counters[name].limit !== undefined &&
this.iterators[name].value > this.iterators[name].limit this.counters[name].value > this.counters[name].limit
) { ) {
this.iterators[name].value = 0; this.counters[name].value = 0;
} }
} }
// Return current iterator value // Return current iterator value
return this.iterators[name].value; return this.counters[name].value;
}; };
$ = this.iterator; $ = this.counter;
// ============================================================= // =============================================================
// Drunk mechanism // Drunk mechanism
@ -910,7 +914,7 @@ export class UserAPI {
// Rythmic generators // Rythmic generators
// ============================================================= // =============================================================
euclid = ( public euclid = (
iterator: number, iterator: number,
pulses: number, pulses: number,
length: number, length: number,
@ -927,6 +931,7 @@ export class UserAPI {
*/ */
return this._euclidean_cycle(pulses, length, rotate)[iterator % length]; return this._euclidean_cycle(pulses, length, rotate)[iterator % length];
}; };
ec = this.euclid;
_euclidean_cycle( _euclidean_cycle(
pulses: number, pulses: number,
@ -1119,7 +1124,7 @@ export class UserAPI {
sound = (sound: string) => { sound = (sound: string) => {
return new Sound(sound, this.app); return new Sound(sound, this.app);
}; };
snd = this.sound;
samples = samples; samples = samples;
log = console.log; log = console.log;

View File

@ -6,6 +6,10 @@ const delay = (ms: number) =>
setTimeout(() => reject(new Error("Operation took too long")), ms) setTimeout(() => reject(new Error("Operation took too long")), ms)
); );
const codeReplace = (code: string): string => {
let new_code = code.replace(/->/g, "&&").replace(/::/g, "&&");
return new_code;
};
const tryCatchWrapper = ( const tryCatchWrapper = (
application: Editor, application: Editor,
@ -13,7 +17,9 @@ const tryCatchWrapper = (
): Promise<boolean> => { ): Promise<boolean> => {
return new Promise((resolve, _) => { return new Promise((resolve, _) => {
try { try {
Function(`"use strict";try{${code}} catch (e) {console.log(e)};`).call(application.api); Function(
`"use strict";try{${codeReplace(code)}} catch (e) {console.log(e)};`
).call(application.api);
resolve(true); resolve(true);
} catch (error) { } catch (error) {
console.log(error); console.log(error);
@ -31,7 +37,7 @@ const addFunctionToCache = (code: string, fn: Function) => {
cache.delete(cache.keys().next().value); cache.delete(cache.keys().next().value);
} }
cache.set(code, fn); cache.set(code, fn);
} };
export const tryEvaluate = async ( export const tryEvaluate = async (
application: Editor, application: Editor,
@ -54,7 +60,11 @@ export const tryEvaluate = async (
]); ]);
if (isCodeValid) { if (isCodeValid) {
code.committed = code.candidate; code.committed = code.candidate;
const newFunction = new Function(`"use strict";try{${wrappedCode}} catch (e) {console.log(e)};`); const newFunction = new Function(
`"use strict";try{${codeReplace(
wrappedCode
)}} catch (e) {console.log(e)};`
);
addFunctionToCache(candidateCode, newFunction); addFunctionToCache(candidateCode, newFunction);
} else { } else {
await evaluate(application, code, timeout); await evaluate(application, code, timeout);