1 /* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
2 /* vim: set sts=2 sw=2 et tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 /* exported ArrayBuffer_transfer, Library, SubprocessConstants */
10 // ctypes is either already available in the chrome worker scope, or defined
11 // in scope via loadSubScript.
15 * Returns a new ArrayBuffer whose contents have been taken from the `buffer`'s
16 * data and then is either truncated or zero-extended by `size`. If `size` is
17 * undefined, the `byteLength` of the `buffer` is used. This operation leaves
18 * `buffer` in a detached state.
20 * @param {ArrayBuffer} buffer
21 * @param {integer} [size = buffer.byteLength]
22 * @returns {ArrayBuffer}
24 var ArrayBuffer_transfer = function (buffer, size = buffer.byteLength) {
25 let u8out = new Uint8Array(size);
26 let u8buffer = new Uint8Array(buffer, 0, Math.min(size, buffer.byteLength));
33 var Library = class Library {
34 constructor(name, names, definitions) {
35 if (name in libraries) {
36 return libraries[name];
39 for (let name of names) {
42 this.library = ctypes.open(name);
45 // Ignore errors until we've tried all the options.
49 throw new Error("Could not load libc");
52 libraries[name] = this;
54 for (let symbol of Object.keys(definitions)) {
55 this.declare(symbol, ...definitions[symbol]);
59 declare(name, ...args) {
60 Object.defineProperty(this, name, {
63 Object.defineProperty(this, name, {
65 value: this.library.declare(name, ...args),
75 * Holds constants which apply to various Subprocess operations.
80 var SubprocessConstants = {
82 * @property {integer} ERROR_END_OF_FILE
83 * The operation failed because the end of the file was reached.
86 ERROR_END_OF_FILE: 0xff7a0001,
88 * @property {integer} ERROR_INVALID_JSON
89 * The operation failed because an invalid JSON was encountered.
92 ERROR_INVALID_JSON: 0xff7a0002,
94 * @property {integer} ERROR_BAD_EXECUTABLE
95 * The operation failed because the given file did not exist, or
96 * could not be executed.
99 ERROR_BAD_EXECUTABLE: 0xff7a0003,
101 * @property {integer} ERROR_INVALID_OPTION
102 * The operation failed because an invalid option was provided.
105 ERROR_INVALID_OPTION: 0xff7a0004,
108 Object.freeze(SubprocessConstants);