Merging zn

This commit is contained in:
2023-08-13 20:45:09 +03:00
4 changed files with 991 additions and 946 deletions

View File

@ -62,6 +62,8 @@ To build a standalone browser application using [Tauri](https://tauri.app/), you
- [ ] Give more information the local context of execution of every script - [ ] Give more information the local context of execution of every script
- print/display the internal iterator used in each script - print/display the internal iterator used in each script
- animate code to show which line is currently being executed - animate code to show which line is currently being executed
- [ ] More rhythmic generators
- [ ] Rendering static files (MIDI, MOD, etc...)
## Scheduler ## Scheduler

1857
src/API.ts

File diff suppressed because it is too large Load Diff

View File

@ -1,59 +1,61 @@
import type { Editor } from './main'; import type { Editor } from "./main";
import type { File } from './AppSettings'; import type { File } from "./AppSettings";
function codeInterceptor(code: string): string { const delay = (ms: number) =>
return code new Promise((_, reject) =>
.replace(/->/g, "&&") setTimeout(() => reject(new Error("Operation took too long")), ms)
.replace(/t\[(\d+),(\d+)\]/g, 'mod($1,$2)') );
.replace(/b\[(\d+),(\d+)\]/g, '[$1,$2].includes(beat)')
.replace(/eb\[(\d+),(\d+)\]/g, '[$1,$2].includes(ebeat)')
.replace(/m\[(\d+),(\d+)\]/g, '[$1,$2].includes(bar)');
}
const delay = (ms: number) => new Promise((_, reject) => setTimeout(() => reject(new Error('Operation took too long')), ms)); const tryCatchWrapper = (
application: Editor,
const tryCatchWrapper = (application: Editor, code: string): Promise<boolean> => { code: string
): Promise<boolean> => {
/** /**
* This function wraps a string of code in a try/catch block and returns a promise * This function wraps a string of code in a try/catch block and returns a promise
* that resolves to true if the code is valid and false if the code is invalid after * that resolves to true if the code is valid and false if the code is invalid after
* being evaluated. * being evaluated.
* *
* @param application - The main application instance * @param application - The main application instance
* @param code - The string of code to wrap and evaluate * @param code - The string of code to wrap and evaluate
* @returns A promise that resolves to true if the code is valid and false if the code is invalid * @returns A promise that resolves to true if the code is valid and false if the code is invalid
*/ */
return new Promise((resolve, _) => { return new Promise((resolve, _) => {
try { try {
Function(`with (this) {try{${codeInterceptor(code)}} catch (e) {console.log(e)}};`).call(application.api); Function(`with (this) {try{${code}} catch (e) {console.log(e)}};`).call(
application.api
);
resolve(true); resolve(true);
} catch (error) { } catch (error) {
console.log(error); console.log(error);
resolve(false); resolve(false);
} }
}); });
} };
export const tryEvaluate = async ( export const tryEvaluate = async (
/** /**
* This function attempts to evaluate a string of code in the context of user API. * This function attempts to evaluate a string of code in the context of user API.
* If the code is invalid, it will attempt to evaluate the previous valid code. * If the code is invalid, it will attempt to evaluate the previous valid code.
* *
* @param application - The main application instance * @param application - The main application instance
* @param code - The set of files to evaluate * @param code - The set of files to evaluate
* @param timeout - The timeout in milliseconds * @param timeout - The timeout in milliseconds
* @returns A promise that resolves to void * @returns A promise that resolves to void
* *
*/ */
application: Editor, application: Editor,
code: File, code: File,
timeout = 5000 timeout = 5000
): Promise<void> => { ): Promise<void> => {
try { try {
code.evaluations!++; code.evaluations!++;
const isCodeValid = await Promise.race([tryCatchWrapper( const isCodeValid = await Promise.race([
application, tryCatchWrapper(
`let i = ${code.evaluations};` + codeInterceptor(code.candidate as string), application,
), delay(timeout)]); (`let i = ${code.evaluations};` + code.candidate) as string
),
delay(timeout),
]);
if (isCodeValid) { if (isCodeValid) {
code.committed = code.candidate; code.committed = code.candidate;
@ -63,22 +65,28 @@ export const tryEvaluate = async (
} catch (error) { } catch (error) {
console.log(error); console.log(error);
} }
} };
export const evaluate = async (application: Editor, code: File, timeout = 1000): Promise<void> => { export const evaluate = async (
application: Editor,
code: File,
timeout = 1000
): Promise<void> => {
/** /**
* This function evaluates a string of code in the context of user API. * This function evaluates a string of code in the context of user API.
* *
* @param application - The main application instance * @param application - The main application instance
* @param code - The set of files to evaluate * @param code - The set of files to evaluate
* @param timeout - The timeout in milliseconds * @param timeout - The timeout in milliseconds
* @returns A promise that resolves to void * @returns A promise that resolves to void
*/ */
try { try {
await Promise.race([tryCatchWrapper(application, codeInterceptor(code.committed as string)), delay(timeout)]); await Promise.race([
if (code.evaluations) tryCatchWrapper(application, code.committed as string),
code.evaluations++; delay(timeout),
]);
if (code.evaluations) code.evaluations++;
} catch (error) { } catch (error) {
console.log(error); console.log(error);
} }
} };

View File

@ -1160,7 +1160,11 @@ yaml@^2.1.1:
"zifferjs@https://github.com/amiika/zifferjs.git": "zifferjs@https://github.com/amiika/zifferjs.git":
version "0.0.0" version "0.0.0"
<<<<<<< HEAD
resolved "https://github.com/amiika/zifferjs.git#374ee4edb0b07d9968b635f899f9f88d79ddaa07" resolved "https://github.com/amiika/zifferjs.git#374ee4edb0b07d9968b635f899f9f88d79ddaa07"
=======
resolved "https://github.com/amiika/zifferjs.git#0254770c19db8772a9c3cb6b0571f2b5007593e6"
>>>>>>> 3effb4f456592922e3e97e1e7b16f6f8929a90b4
dependencies: dependencies:
"@types/seedrandom" "^3.0.5" "@types/seedrandom" "^3.0.5"
lru-cache "^10.0.0" lru-cache "^10.0.0"