Skip to content

Commit

Permalink
With a bit of cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Namaneo committed Sep 11, 2023
1 parent ab68ea8 commit c8fabb3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 23 deletions.
29 changes: 17 additions & 12 deletions ui/sources/services/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ export default class Core {
/** @type {Graphics} */
#graphics = null;

/** @type {(variables: Variable[]) => void} */
#on_variables = null;

/**
* @param {string} name
* @param {WebAssembly.Memory} memory
Expand All @@ -55,23 +58,17 @@ export default class Core {
Core.#running = new Promise(resolve => Core.#stop = resolve);

this.#graphics = new Graphics(canvas);
this.#on_variables = on_variables

const origin = location.origin + location.pathname.substring(0, location.pathname.lastIndexOf('/'));
const config = { core: this.#name, system, rom, origin, memory: this.#memory };
const script = await (await fetch('worker.js')).text();

const handler = async message => {
switch (message.data.type) {
case 'thread':
const thread = new Parallel(Interop, false, handler);
const core = await thread.create(this.#name, script);
await core.init(Parallel.instrument(this), await Files.clone(), { ...config, ...message.data });
this.#threads.push(thread);
break;
case 'variables':
on_variables(message.data.variables);
break;
}
const thread = new Parallel(Interop, false, handler);
const core = await thread.create(this.#name, script);
await core.init(Parallel.instrument(this), await Files.clone(), { ...config, ...message.data });
this.#threads.push(thread);
}

this.#parallel = new Parallel(Interop, false, handler);
Expand Down Expand Up @@ -126,13 +123,21 @@ export default class Core {

/**
* @param {Audio} audio
* @returns
* @returns {void}
*/
play(audio) {
const audio_view = new Float32Array(this.#memory.buffer, audio.data, audio.frames * 2);
AudioPlayer.queue(audio_view.slice(), audio.rate);
}

/**
* @param {Variable[]} variables
* @returns {void}
*/
variables(variables) {
this.#on_variables(variables);
}

/** @param {Settings} settings @returns {Promise<void>} */
async settings(settings) { await this.#interop?.variables(settings.variables); }

Expand Down
17 changes: 6 additions & 11 deletions ui/sources/services/interop.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,31 +127,26 @@ export default class Interop {

const junie_interop_video = (video_c) => {
const video = Video.parse(config.memory, video_c);
if (!video.data)
return;

this.#core.draw(video);
if (video.data)
this.#core.draw(video);
};

const junie_interop_audio = (audio_c) => {
const audio = Audio.parse(config.memory, audio_c);
if (!audio.frames)
return;

this.#core.play(audio);
if (audio.frames)
this.#core.play(audio);
};

const junie_interop_variables = (variables_c) => {
const variables = Variable.parse(this.#instance, variables_c);
postMessage({ type: 'variables', variables });
this.#core.variables(Variable.parse(this.#instance, variables_c));
}

const source = await WebAssembly.instantiateStreaming(fetch(`${config.origin}/modules/${config.core}.wasm`), {
env: { memory: config.memory, junie_interop_video, junie_interop_audio, junie_interop_variables },
wasi_snapshot_preview1: this.#wasi.environment,
wasi: { 'thread-spawn': (start_arg) => {
const id = filesystem.id();
postMessage({ type: 'thread', id, fds: this.#wasi.fds, start_arg });
postMessage({ id, fds: this.#wasi.fds, start_arg });
return id;
}},
});
Expand Down

0 comments on commit c8fabb3

Please sign in to comment.