-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
5,406 additions
and
5,405 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
Oops, something went wrong.