Skip to content

Commit

Permalink
check
Browse files Browse the repository at this point in the history
  • Loading branch information
oligamiq committed Sep 23, 2024
1 parent c982ce2 commit 8e5189c
Show file tree
Hide file tree
Showing 21 changed files with 5,406 additions and 5,405 deletions.
2,288 changes: 1,144 additions & 1,144 deletions src/wasi_farm/animals.ts

Large diffs are not rendered by default.

184 changes: 92 additions & 92 deletions src/wasi_farm/farm.ts
Original file line number Diff line number Diff line change
@@ -1,111 +1,111 @@
import { debug } from "../debug.js";
import { Fd } from "../fd.js";
import { Options } from "../wasi.js";
import { WASIFarmPark } from "./park.js";
import { WASIFarmRefObject } from "./ref.js";
import type { Fd } from "../fd.js";
import type { Options } from "../wasi.js";
import type { WASIFarmPark } from "./park.js";
import type { WASIFarmRefObject } from "./ref.js";
import { WASIFarmParkUseArrayBuffer } from "./shared_array_buffer/index.js";

export class WASIFarm {
private fds: Array<Fd>;
private park: WASIFarmPark;
private fds: Array<Fd>;
private park: WASIFarmPark;

private can_array_buffer;
private can_array_buffer;

constructor(
stdin?: Fd,
stdout?: Fd,
stderr?: Fd,
fds: Array<Fd> = [],
options: Options & {
allocator_size?: number;
} = {},
) {
debug.enable(options.debug);
constructor(
stdin?: Fd,
stdout?: Fd,
stderr?: Fd,
fds: Array<Fd> = [],
options: Options & {
allocator_size?: number;
} = {},
) {
debug.enable(options.debug);

const new_fds = [];
let stdin_ = undefined;
let stdout_ = undefined;
let stderr_ = undefined;
if (stdin) {
new_fds.push(stdin);
stdin_ = new_fds.length - 1;
}
if (stdout) {
new_fds.push(stdout);
stdout_ = new_fds.length - 1;
}
if (stderr) {
new_fds.push(stderr);
stderr_ = new_fds.length - 1;
}
new_fds.push(...fds);
const new_fds = [];
let stdin_ = undefined;
let stdout_ = undefined;
let stderr_ = undefined;
if (stdin) {
new_fds.push(stdin);
stdin_ = new_fds.length - 1;
}
if (stdout) {
new_fds.push(stdout);
stdout_ = new_fds.length - 1;
}
if (stderr) {
new_fds.push(stderr);
stderr_ = new_fds.length - 1;
}
new_fds.push(...fds);

const default_allow_fds = [];
for (let i = 0; i < new_fds.length; i++) {
default_allow_fds.push(i);
}
const default_allow_fds = [];
for (let i = 0; i < new_fds.length; i++) {
default_allow_fds.push(i);
}

this.fds = new_fds;
this.fds = new_fds;

// WebAssembly.Memory can be used to create a SharedArrayBuffer, but it cannot be transferred by postMessage.
// Uncaught (in promise) DataCloneError:
// Failed to execute 'postMessage' on 'Worker':
// SharedArrayBuffer transfer requires self.crossOriginIsolated.
try {
new SharedArrayBuffer(4);
this.can_array_buffer = true;
} catch (e) {
this.can_array_buffer = false;
console.warn("SharedArrayBuffer is not supported:", e);
// WebAssembly.Memory can be used to create a SharedArrayBuffer, but it cannot be transferred by postMessage.
// Uncaught (in promise) DataCloneError:
// Failed to execute 'postMessage' on 'Worker':
// SharedArrayBuffer transfer requires self.crossOriginIsolated.
try {
new SharedArrayBuffer(4);
this.can_array_buffer = true;
} catch (e) {
this.can_array_buffer = false;
console.warn("SharedArrayBuffer is not supported:", e);

if (!crossOriginIsolated) {
console.warn(
"SharedArrayBuffer is not supported because crossOriginIsolated is not enabled.",
);
}
}
if (!crossOriginIsolated) {
console.warn(
"SharedArrayBuffer is not supported because crossOriginIsolated is not enabled.",
);
}
}

if (this.can_array_buffer) {
this.park = new WASIFarmParkUseArrayBuffer(
this.fds_ref(),
stdin_,
stdout_,
stderr_,
default_allow_fds,
options?.allocator_size,
);
} else {
throw new Error("Non SharedArrayBuffer is not supported yet");
}
if (this.can_array_buffer) {
this.park = new WASIFarmParkUseArrayBuffer(
this.fds_ref(),
stdin_,
stdout_,
stderr_,
default_allow_fds,
options?.allocator_size,
);
} else {
throw new Error("Non SharedArrayBuffer is not supported yet");
}

this.park.listen();
}
this.park.listen();
}

private fds_ref(): Array<Fd> {
const fds = new Proxy([] as Array<Fd>, {
get: (_, prop) => {
// console.log("fds", prop);
private fds_ref(): Array<Fd> {
const fds = new Proxy([] as Array<Fd>, {
get: (_, prop) => {
// console.log("fds", prop);

if (prop === "push") {
return (fd: Fd) => {
const len = this.fds.push(fd);
return len;
};
}
return this.fds[prop];
},
if (prop === "push") {
return (fd: Fd) => {
const len = this.fds.push(fd);
return len;
};
}
return this.fds[prop];
},

set: (_, prop, value) => {
// console.log("fds", prop, value);
this.fds[prop] = value;
return true;
},
});
set: (_, prop, value) => {
// console.log("fds", prop, value);
this.fds[prop] = value;
return true;
},
});

return fds;
}
return fds;
}

get_ref(): WASIFarmRefObject {
return this.park.get_ref();
}
get_ref(): WASIFarmRefObject {
return this.park.get_ref();
}
}
2 changes: 1 addition & 1 deletion src/wasi_farm/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { WASIFarmAnimal } from "./animals.js";
import { WASIFarm } from "./farm.js";
import { WASIFarmRef } from "./ref.js";
import { WASIFarmAnimal } from "./animals.js";
import { thread_spawn_on_worker } from "./shared_array_buffer/index.js";
export { WASIFarm, WASIFarmRef, WASIFarmAnimal, thread_spawn_on_worker };
Loading

0 comments on commit 8e5189c

Please sign in to comment.