2012-06-10 16:44:50 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
{
|
|
|
|
if (typeof Components != "undefined") {
|
2012-08-13 18:54:42 -07:00
|
|
|
var EXPORTED_SYMBOLS = ["OS"];
|
|
|
|
Components.utils.import("resource://gre/modules/ctypes.jsm");
|
|
|
|
Components.classes["@mozilla.org/net/osfileconstantsservice;1"].
|
|
|
|
getService(Components.interfaces.nsIOSFileConstantsService).init();
|
2012-06-10 16:44:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
(function(exports) {
|
|
|
|
"use strict";
|
|
|
|
/*
|
|
|
|
* This block defines |OS.Shared.Type|. However, |OS| can exist already
|
|
|
|
* (in particular, if this code is executed in a worker thread, it is
|
|
|
|
* defined).
|
|
|
|
*/
|
|
|
|
if (!exports.OS) {
|
|
|
|
exports.OS = {};
|
|
|
|
}
|
|
|
|
if (!exports.OS.Shared) {
|
|
|
|
exports.OS.Shared = {};
|
|
|
|
}
|
|
|
|
if (exports.OS.Shared.Type) {
|
|
|
|
return; // Avoid double-initialization
|
|
|
|
}
|
|
|
|
|
|
|
|
let LOG;
|
|
|
|
if (typeof console != "undefined" && console.log) {
|
|
|
|
LOG = console.log.bind(console, "OS");
|
|
|
|
} else {
|
|
|
|
LOG = function() {
|
|
|
|
let text = "OS";
|
|
|
|
for (let i = 0; i < arguments.length; ++i) {
|
|
|
|
text += (" " + arguments[i]);
|
|
|
|
}
|
|
|
|
dump(text + "\n");
|
|
|
|
};
|
|
|
|
}
|
|
|
|
exports.OS.Shared.LOG = LOG;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An OS error.
|
|
|
|
*
|
|
|
|
* This class is provided mostly for type-matching. If you need more
|
|
|
|
* details about an error, you should use the platform-specific error
|
|
|
|
* codes provided by subclasses of |OS.Shared.Error|.
|
|
|
|
*
|
|
|
|
* @param {string} operation The operation that failed.
|
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
function OSError(operation) {
|
|
|
|
Error.call(this);
|
|
|
|
this.operation = operation;
|
|
|
|
}
|
|
|
|
exports.OS.Shared.Error = OSError;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Abstraction above js-ctypes types.
|
|
|
|
*
|
|
|
|
* Use values of this type to register FFI functions. In addition to the
|
|
|
|
* usual features of js-ctypes, values of this type perform the necessary
|
|
|
|
* transformations to ensure that C errors are handled nicely, to connect
|
|
|
|
* resources with their finalizer, etc.
|
|
|
|
*
|
|
|
|
* @param {string} name The name of the type. Must be unique.
|
2012-08-09 12:13:46 -07:00
|
|
|
* @param {CType} implementation The js-ctypes implementation of the type.
|
2012-06-10 16:44:50 -07:00
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
*/
|
2012-08-09 12:13:46 -07:00
|
|
|
function Type(name, implementation) {
|
2012-06-10 16:44:50 -07:00
|
|
|
if (!(typeof name == "string")) {
|
|
|
|
throw new TypeError("Type expects as first argument a name, got: "
|
|
|
|
+ name);
|
|
|
|
}
|
|
|
|
if (!(implementation instanceof ctypes.CType)) {
|
|
|
|
throw new TypeError("Type expects as second argument a ctypes.CType"+
|
|
|
|
", got: " + implementation);
|
|
|
|
}
|
2012-08-09 12:13:46 -07:00
|
|
|
Object.defineProperty(this, "name", { value: name });
|
|
|
|
Object.defineProperty(this, "implementation", { value: implementation });
|
2012-06-10 16:44:50 -07:00
|
|
|
}
|
|
|
|
Type.prototype = {
|
2012-08-13 18:54:42 -07:00
|
|
|
/**
|
|
|
|
* Serialize a value of |this| |Type| into a format that can
|
|
|
|
* be transmitted as a message (not necessarily a string).
|
|
|
|
*
|
|
|
|
* In the default implementation, the method returns the
|
|
|
|
* value unchanged.
|
|
|
|
*/
|
|
|
|
toMsg: function default_toMsg(value) {
|
|
|
|
return value;
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Deserialize a message to a value of |this| |Type|.
|
|
|
|
*
|
|
|
|
* In the default implementation, the method returns the
|
|
|
|
* message unchanged.
|
|
|
|
*/
|
|
|
|
fromMsg: function default_fromMsg(msg) {
|
|
|
|
return msg;
|
|
|
|
},
|
2012-08-09 12:13:46 -07:00
|
|
|
/**
|
|
|
|
* Import a value from C.
|
|
|
|
*
|
|
|
|
* In this default implementation, return the value
|
|
|
|
* unchanged.
|
|
|
|
*/
|
|
|
|
importFromC: function default_importFromC(value) {
|
2012-06-10 16:44:50 -07:00
|
|
|
return value;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A pointer/array used to pass data to the foreign function.
|
|
|
|
*/
|
|
|
|
get in_ptr() {
|
|
|
|
delete this.in_ptr;
|
2012-08-09 12:13:46 -07:00
|
|
|
let ptr_t = new PtrType("[in] " + this.name + "*",
|
2012-06-10 16:44:50 -07:00
|
|
|
this.implementation.ptr);
|
|
|
|
Object.defineProperty(this, "in_ptr",
|
|
|
|
{
|
|
|
|
get: function() {
|
|
|
|
return ptr_t;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return ptr_t;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A pointer/array used to receive data from the foreign function.
|
|
|
|
*/
|
|
|
|
get out_ptr() {
|
|
|
|
delete this.out_ptr;
|
2012-08-09 12:13:46 -07:00
|
|
|
let ptr_t = new PtrType("[out] " + this.name + "*",
|
2012-06-10 16:44:50 -07:00
|
|
|
this.implementation.ptr);
|
|
|
|
Object.defineProperty(this, "out_ptr",
|
|
|
|
{
|
|
|
|
get: function() {
|
|
|
|
return ptr_t;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return ptr_t;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A pointer/array used to both pass data to the foreign function
|
|
|
|
* and receive data from the foreign function.
|
|
|
|
*
|
|
|
|
* Whenever possible, prefer using |in_ptr| or |out_ptr|, which
|
|
|
|
* are generally faster.
|
|
|
|
*/
|
|
|
|
get inout_ptr() {
|
|
|
|
delete this.inout_ptr;
|
2012-08-09 12:13:46 -07:00
|
|
|
let ptr_t = new PtrType("[inout] " + this.name + "*",
|
2012-06-10 16:44:50 -07:00
|
|
|
this.implementation.ptr);
|
|
|
|
Object.defineProperty(this, "inout_ptr",
|
|
|
|
{
|
|
|
|
get: function() {
|
|
|
|
return ptr_t;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return ptr_t;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Attach a finalizer to a type.
|
|
|
|
*/
|
2012-07-21 10:44:58 -07:00
|
|
|
releaseWith: function releaseWith(finalizer) {
|
2012-06-10 16:44:50 -07:00
|
|
|
let parent = this;
|
2012-08-09 12:13:46 -07:00
|
|
|
let type = this.withName("[auto " + this.name + ", " + finalizer + "] ");
|
|
|
|
type.importFromC = function importFromC(value, operation) {
|
|
|
|
return ctypes.CDataFinalizer(
|
|
|
|
parent.importFromC(value, operation),
|
|
|
|
finalizer);
|
|
|
|
};
|
2012-06-10 16:44:50 -07:00
|
|
|
return type;
|
2012-07-21 10:44:58 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return an alias to a type with a different name.
|
|
|
|
*/
|
|
|
|
withName: function withName(name) {
|
|
|
|
return Object.create(this, {name: {value: name}});
|
2012-08-09 12:13:46 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cast a C value to |this| type.
|
|
|
|
*
|
|
|
|
* Throw an error if the value cannot be casted.
|
|
|
|
*/
|
|
|
|
cast: function cast(value) {
|
|
|
|
return ctypes.cast(value, this.implementation);
|
|
|
|
}
|
2012-06-10 16:44:50 -07:00
|
|
|
};
|
|
|
|
|
2012-08-09 12:13:46 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A |Type| of pointers.
|
|
|
|
*
|
|
|
|
* @param {string} name The name of this type.
|
|
|
|
* @param {CType} implementation The type of this pointer.
|
|
|
|
*/
|
|
|
|
function PtrType(name, implementation) {
|
|
|
|
Type.call(this, name, implementation);
|
|
|
|
}
|
|
|
|
PtrType.prototype = Object.create(Type.prototype);
|
|
|
|
|
2012-08-13 18:54:42 -07:00
|
|
|
/**
|
|
|
|
* Convert a value to a pointer.
|
|
|
|
*
|
|
|
|
* Protocol:
|
|
|
|
* - |null| returns |null|
|
|
|
|
* - a string returns |{string: value}|
|
|
|
|
* - an ArrayBuffer returns |{ptr: address_of_buffer}|
|
|
|
|
* - a C array returns |{ptr: address_of_buffer}|
|
|
|
|
* everything else raises an error
|
|
|
|
*/
|
|
|
|
PtrType.prototype.toMsg = function ptr_toMsg(value) {
|
|
|
|
if (value == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (typeof value == "string") {
|
|
|
|
return { string: value };
|
|
|
|
}
|
|
|
|
let normalized;
|
|
|
|
if ("byteLength" in value) { // ArrayBuffer
|
|
|
|
normalized = Types.uint8_t.in_ptr.implementation(value);
|
|
|
|
} else if ("addressOfElement" in value) { // C array
|
|
|
|
normalized = value.addressOfElement(0);
|
|
|
|
} else if ("isNull" in value) { // C pointer
|
|
|
|
normalized = value;
|
|
|
|
} else {
|
|
|
|
throw new TypeError("Value " + value +
|
|
|
|
" cannot be converted to a pointer");
|
|
|
|
}
|
|
|
|
let cast = Types.uintptr_t.cast(normalized);
|
|
|
|
return {ptr: cast.value.toString()};
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert a message back to a pointer.
|
|
|
|
*/
|
|
|
|
PtrType.prototype.fromMsg = function ptr_fromMsg(msg) {
|
|
|
|
if (msg == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if ("string" in msg) {
|
|
|
|
return msg.string;
|
|
|
|
}
|
|
|
|
if ("ptr" in msg) {
|
|
|
|
let address = ctypes.uintptr_t(msg.ptr);
|
|
|
|
return this.cast(address);
|
|
|
|
}
|
|
|
|
throw new TypeError("Message " + msg.toSource() +
|
|
|
|
" does not represent a pointer");
|
|
|
|
};
|
|
|
|
|
2012-06-10 16:44:50 -07:00
|
|
|
exports.OS.Shared.Type = Type;
|
|
|
|
let Types = Type;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Some values are large integers on 64 bit platforms. Unfortunately,
|
|
|
|
* in practice, 64 bit integers cannot be manipulated in JS. We
|
|
|
|
* therefore project them to regular numbers whenever possible.
|
|
|
|
*/
|
|
|
|
|
|
|
|
let projectLargeInt = function projectLargeInt(x) {
|
2012-06-15 08:22:50 -07:00
|
|
|
return parseInt(x.toString(), 10);
|
2012-06-10 16:44:50 -07:00
|
|
|
};
|
|
|
|
let projectLargeUInt = function projectLargeUInt(x) {
|
2012-07-21 10:44:58 -07:00
|
|
|
return parseInt(x.toString(), 10);
|
2012-06-10 16:44:50 -07:00
|
|
|
};
|
|
|
|
let projectValue = function projectValue(x) {
|
|
|
|
if (!(x instanceof ctypes.CData)) {
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
if (!("value" in x)) { // Sanity check
|
|
|
|
throw new TypeError("Number " + x.toSource() + " has no field |value|");
|
|
|
|
}
|
|
|
|
return x.value;
|
|
|
|
};
|
|
|
|
|
|
|
|
function projector(type, signed) {
|
2012-08-09 12:13:46 -07:00
|
|
|
LOG("Determining best projection for", type,
|
|
|
|
"(size: ", type.size, ")", signed?"signed":"unsigned");
|
|
|
|
if (type instanceof Type) {
|
|
|
|
type = type.implementation;
|
|
|
|
}
|
2012-06-10 16:44:50 -07:00
|
|
|
if (!type.size) {
|
|
|
|
throw new TypeError("Argument is not a proper C type");
|
|
|
|
}
|
|
|
|
// Determine if type is projected to Int64/Uint64
|
|
|
|
if (type.size == 8 // Usual case
|
2012-08-09 12:13:46 -07:00
|
|
|
// The following cases have special treatment in js-ctypes
|
|
|
|
// Regardless of their size, the value getter returns
|
|
|
|
// a Int64/Uint64
|
2012-06-10 16:44:50 -07:00
|
|
|
|| type == ctypes.size_t // Special cases
|
|
|
|
|| type == ctypes.ssize_t
|
|
|
|
|| type == ctypes.intptr_t
|
|
|
|
|| type == ctypes.uintptr_t
|
|
|
|
|| type == ctypes.off_t){
|
|
|
|
if (signed) {
|
|
|
|
LOG("Projected as a large signed integer");
|
|
|
|
return projectLargeInt;
|
|
|
|
} else {
|
|
|
|
LOG("Projected as a large unsigned integer");
|
|
|
|
return projectLargeUInt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
LOG("Projected as a regular number");
|
|
|
|
return projectValue;
|
|
|
|
};
|
2012-06-27 17:15:33 -07:00
|
|
|
exports.OS.Shared.projectValue = projectValue;
|
|
|
|
|
2012-06-10 16:44:50 -07:00
|
|
|
|
|
|
|
|
2012-06-27 17:15:33 -07:00
|
|
|
/**
|
|
|
|
* Get the appropriate type for an unsigned int of the given size.
|
|
|
|
*
|
|
|
|
* This function is useful to define types such as |mode_t| whose
|
|
|
|
* actual width depends on the OS/platform.
|
|
|
|
*
|
|
|
|
* @param {number} size The number of bytes requested.
|
|
|
|
*/
|
|
|
|
Types.uintn_t = function uintn_t(size) {
|
|
|
|
switch (size) {
|
|
|
|
case 1: return Types.uint8_t;
|
|
|
|
case 2: return Types.uint16_t;
|
|
|
|
case 4: return Types.uint32_t;
|
|
|
|
case 8: return Types.uint64_t;
|
|
|
|
default:
|
|
|
|
throw new Error("Cannot represent unsigned integers of " + size + " bytes");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the appropriate type for an signed int of the given size.
|
|
|
|
*
|
|
|
|
* This function is useful to define types such as |mode_t| whose
|
|
|
|
* actual width depends on the OS/platform.
|
|
|
|
*
|
|
|
|
* @param {number} size The number of bytes requested.
|
|
|
|
*/
|
|
|
|
Types.intn_t = function intn_t(size) {
|
|
|
|
switch (size) {
|
|
|
|
case 1: return Types.int8_t;
|
|
|
|
case 2: return Types.int16_t;
|
|
|
|
case 4: return Types.int32_t;
|
|
|
|
case 8: return Types.int64_t;
|
|
|
|
default:
|
|
|
|
throw new Error("Cannot represent integers of " + size + " bytes");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-06-10 16:44:50 -07:00
|
|
|
/**
|
|
|
|
* Actual implementation of common C types.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The void value.
|
|
|
|
*/
|
|
|
|
Types.void_t =
|
|
|
|
new Type("void",
|
|
|
|
ctypes.void_t);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Shortcut for |void*|.
|
|
|
|
*/
|
|
|
|
Types.voidptr_t =
|
2012-08-09 12:13:46 -07:00
|
|
|
new PtrType("void*",
|
2012-06-10 16:44:50 -07:00
|
|
|
ctypes.voidptr_t);
|
|
|
|
|
|
|
|
// void* is a special case as we can cast any pointer to/from it
|
|
|
|
// so we have to shortcut |in_ptr|/|out_ptr|/|inout_ptr| and
|
|
|
|
// ensure that js-ctypes' casting mechanism is invoked directly
|
|
|
|
["in_ptr", "out_ptr", "inout_ptr"].forEach(function(key) {
|
|
|
|
Object.defineProperty(Types.void_t, key,
|
|
|
|
{
|
2012-08-09 12:13:46 -07:00
|
|
|
value: Types.voidptr_t
|
2012-06-10 16:44:50 -07:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2012-08-09 12:13:46 -07:00
|
|
|
/**
|
|
|
|
* A Type of integers.
|
|
|
|
*
|
|
|
|
* @param {string} name The name of this type.
|
|
|
|
* @param {CType} implementation The underlying js-ctypes implementation.
|
|
|
|
* @param {bool} signed |true| if this is a type of signed integers,
|
|
|
|
* |false| otherwise.
|
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
function IntType(name, implementation, signed) {
|
|
|
|
Type.call(this, name, implementation);
|
|
|
|
this.importFromC = projector(implementation, signed);
|
|
|
|
};
|
|
|
|
IntType.prototype = Object.create(Type.prototype);
|
|
|
|
|
2012-06-10 16:44:50 -07:00
|
|
|
/**
|
|
|
|
* A C char (one byte)
|
|
|
|
*/
|
|
|
|
Types.char =
|
|
|
|
new Type("char",
|
|
|
|
ctypes.char);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A C wide char (two bytes)
|
|
|
|
*/
|
|
|
|
Types.jschar =
|
|
|
|
new Type("jschar",
|
|
|
|
ctypes.jschar);
|
|
|
|
|
2012-08-10 08:52:55 -07:00
|
|
|
/**
|
|
|
|
* Base string types.
|
|
|
|
*/
|
|
|
|
Types.cstring = Types.char.in_ptr.withName("[in] C string");
|
|
|
|
Types.wstring = Types.jschar.in_ptr.withName("[in] wide string");
|
|
|
|
Types.out_cstring = Types.char.out_ptr.withName("[out] C string");
|
|
|
|
Types.out_wstring = Types.jschar.out_ptr.withName("[out] wide string");
|
|
|
|
|
2012-06-27 17:15:33 -07:00
|
|
|
/**
|
|
|
|
* A C integer (8-bits).
|
|
|
|
*/
|
|
|
|
Types.int8_t =
|
2012-08-09 12:13:46 -07:00
|
|
|
new IntType("int8_t", ctypes.int8_t, true);
|
2012-06-27 17:15:33 -07:00
|
|
|
|
|
|
|
Types.uint8_t =
|
2012-08-09 12:13:46 -07:00
|
|
|
new IntType("uint8_t", ctypes.uint8_t, false);
|
2012-06-27 17:15:33 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A C integer (16-bits).
|
|
|
|
*
|
|
|
|
* Also known as WORD under Windows.
|
|
|
|
*/
|
|
|
|
Types.int16_t =
|
2012-08-09 12:13:46 -07:00
|
|
|
new IntType("int16_t", ctypes.int16_t, true);
|
2012-06-27 17:15:33 -07:00
|
|
|
|
|
|
|
Types.uint16_t =
|
2012-08-09 12:13:46 -07:00
|
|
|
new IntType("uint16_t", ctypes.uint16_t, false);
|
2012-06-27 17:15:33 -07:00
|
|
|
|
2012-06-10 16:44:50 -07:00
|
|
|
/**
|
|
|
|
* A C integer (32-bits).
|
|
|
|
*
|
|
|
|
* Also known as DWORD under Windows.
|
|
|
|
*/
|
|
|
|
Types.int32_t =
|
2012-08-09 12:13:46 -07:00
|
|
|
new IntType("int32_t", ctypes.int32_t, true);
|
2012-06-10 16:44:50 -07:00
|
|
|
|
|
|
|
Types.uint32_t =
|
2012-08-09 12:13:46 -07:00
|
|
|
new IntType("uint32_t", ctypes.uint32_t, false);
|
2012-06-10 16:44:50 -07:00
|
|
|
|
2012-06-22 17:24:27 -07:00
|
|
|
/**
|
|
|
|
* A C integer (64-bits).
|
|
|
|
*/
|
|
|
|
Types.int64_t =
|
2012-08-09 12:13:46 -07:00
|
|
|
new IntType("int64_t", ctypes.int64_t, true);
|
2012-06-22 17:24:27 -07:00
|
|
|
|
|
|
|
Types.uint64_t =
|
2012-08-09 12:13:46 -07:00
|
|
|
new IntType("uint64_t", ctypes.uint64_t, false);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A C integer
|
|
|
|
*
|
|
|
|
* Size depends on the platform.
|
|
|
|
*/
|
|
|
|
Types.int = Types.intn_t(ctypes.int.size).
|
|
|
|
withName("int");
|
|
|
|
|
|
|
|
Types.unsigned_int = Types.intn_t(ctypes.unsigned_int.size).
|
|
|
|
withName("unsigned int");
|
2012-06-22 17:24:27 -07:00
|
|
|
|
2012-06-10 16:44:50 -07:00
|
|
|
/**
|
2012-08-09 12:13:46 -07:00
|
|
|
* A C long integer.
|
|
|
|
*
|
2012-06-10 16:44:50 -07:00
|
|
|
* Size depends on the platform.
|
|
|
|
*/
|
|
|
|
Types.long =
|
2012-08-09 12:13:46 -07:00
|
|
|
Types.intn_t(ctypes.long.size).withName("long");
|
|
|
|
|
|
|
|
Types.unsigned_long =
|
|
|
|
Types.intn_t(ctypes.unsigned_long.size).withName("unsigned long");
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An unsigned integer with the same size as a pointer.
|
|
|
|
*
|
|
|
|
* Used to cast a pointer to an integer, whenever necessary.
|
|
|
|
*/
|
|
|
|
Types.uintptr_t =
|
|
|
|
Types.uintn_t(ctypes.uintptr_t.size).withName("uintptr_t");
|
2012-06-10 16:44:50 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A boolean.
|
|
|
|
* Implemented as a C integer.
|
|
|
|
*/
|
2012-08-09 12:13:46 -07:00
|
|
|
Types.bool = Types.int.withName("bool");
|
|
|
|
Types.bool.importFromC = function projectBool(x) {
|
|
|
|
return !!(x.value);
|
|
|
|
};
|
2012-06-10 16:44:50 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A user identifier.
|
2012-08-09 12:13:46 -07:00
|
|
|
*
|
2012-06-10 16:44:50 -07:00
|
|
|
* Implemented as a C integer.
|
|
|
|
*/
|
|
|
|
Types.uid_t =
|
2012-08-09 12:13:46 -07:00
|
|
|
Types.int.withName("uid_t");
|
2012-06-10 16:44:50 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A group identifier.
|
2012-08-09 12:13:46 -07:00
|
|
|
*
|
2012-06-10 16:44:50 -07:00
|
|
|
* Implemented as a C integer.
|
|
|
|
*/
|
|
|
|
Types.gid_t =
|
2012-08-09 12:13:46 -07:00
|
|
|
Types.int.withName("gid_t");
|
2012-06-10 16:44:50 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* An offset (positive or negative).
|
2012-08-09 12:13:46 -07:00
|
|
|
*
|
2012-06-10 16:44:50 -07:00
|
|
|
* Implemented as a C integer.
|
|
|
|
*/
|
|
|
|
Types.off_t =
|
2012-08-09 12:13:46 -07:00
|
|
|
new IntType("off_t", ctypes.off_t, true);
|
2012-06-10 16:44:50 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A size (positive).
|
2012-08-09 12:13:46 -07:00
|
|
|
*
|
2012-06-10 16:44:50 -07:00
|
|
|
* Implemented as a C size_t.
|
|
|
|
*/
|
|
|
|
Types.size_t =
|
2012-08-09 12:13:46 -07:00
|
|
|
new IntType("size_t", ctypes.size_t, false);
|
2012-06-10 16:44:50 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* An offset (positive or negative).
|
|
|
|
* Implemented as a C integer.
|
|
|
|
*/
|
|
|
|
Types.ssize_t =
|
2012-08-09 12:13:46 -07:00
|
|
|
new IntType("ssize_t", ctypes.ssize_t, true);
|
2012-06-10 16:44:50 -07:00
|
|
|
|
2012-08-10 08:52:55 -07:00
|
|
|
/**
|
|
|
|
* Encoding/decoding strings
|
|
|
|
*/
|
|
|
|
Types.uencoder =
|
|
|
|
new Type("uencoder", ctypes.StructType("uencoder"));
|
|
|
|
Types.udecoder =
|
|
|
|
new Type("udecoder", ctypes.StructType("udecoder"));
|
2012-07-05 01:20:07 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Utility class, used to build a |struct| type
|
|
|
|
* from a set of field names, types and offsets.
|
|
|
|
*
|
|
|
|
* @param {string} name The name of the |struct| type.
|
|
|
|
* @param {number} size The total size of the |struct| type in bytes.
|
|
|
|
*/
|
|
|
|
function HollowStructure(name, size) {
|
|
|
|
if (!name) {
|
|
|
|
throw new TypeError("HollowStructure expects a name");
|
|
|
|
}
|
|
|
|
if (!size || size < 0) {
|
|
|
|
throw new TypeError("HollowStructure expects a (positive) size");
|
|
|
|
}
|
|
|
|
|
|
|
|
// A mapping from offsets in the struct to name/type pairs
|
|
|
|
// (or nothing if no field starts at that offset).
|
|
|
|
this.offset_to_field_info = [];
|
|
|
|
|
|
|
|
// The name of the struct
|
|
|
|
this.name = name;
|
|
|
|
|
|
|
|
// The size of the struct, in bytes
|
|
|
|
this.size = size;
|
|
|
|
|
|
|
|
// The number of paddings inserted so far.
|
|
|
|
// Used to give distinct names to padding fields.
|
|
|
|
this._paddings = 0;
|
|
|
|
}
|
|
|
|
HollowStructure.prototype = {
|
|
|
|
/**
|
|
|
|
* Add a field at a given offset.
|
|
|
|
*
|
|
|
|
* @param {number} offset The offset at which to insert the field.
|
|
|
|
* @param {string} name The name of the field.
|
|
|
|
* @param {CType|Type} type The type of the field.
|
|
|
|
*/
|
|
|
|
add_field_at: function add_field_at(offset, name, type) {
|
2012-07-21 10:44:58 -07:00
|
|
|
if (offset == null) {
|
2012-07-05 01:20:07 -07:00
|
|
|
throw new TypeError("add_field_at requires a non-null offset");
|
|
|
|
}
|
|
|
|
if (!name) {
|
|
|
|
throw new TypeError("add_field_at requires a non-null name");
|
|
|
|
}
|
|
|
|
if (!type) {
|
|
|
|
throw new TypeError("add_field_at requires a non-null type");
|
|
|
|
}
|
|
|
|
if (type instanceof Type) {
|
|
|
|
type = type.implementation;
|
|
|
|
}
|
|
|
|
if (this.offset_to_field_info[offset]) {
|
|
|
|
throw new Error("HollowStructure " + this.name +
|
|
|
|
" already has a field at offset " + offset);
|
|
|
|
}
|
|
|
|
if (offset + type.size > this.size) {
|
|
|
|
throw new Error("HollowStructure " + this.name +
|
|
|
|
" cannot place a value of type " + type +
|
|
|
|
" at offset " + offset +
|
|
|
|
" without exceeding its size of " + this.size);
|
|
|
|
}
|
|
|
|
let field = {name: name, type:type};
|
|
|
|
this.offset_to_field_info[offset] = field;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a pseudo-field that will only serve as padding.
|
|
|
|
*
|
|
|
|
* @param {number} size The number of bytes in the field.
|
|
|
|
* @return {Object} An association field-name => field-type,
|
|
|
|
* as expected by |ctypes.StructType|.
|
|
|
|
*/
|
|
|
|
_makePaddingField: function makePaddingField(size) {
|
|
|
|
let field = ({});
|
|
|
|
field["padding_" + this._paddings] =
|
|
|
|
ctypes.ArrayType(ctypes.uint8_t, size);
|
|
|
|
this._paddings++;
|
|
|
|
return field;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert this |HollowStructure| into a |Type|.
|
|
|
|
*/
|
|
|
|
getType: function getType() {
|
|
|
|
// Contents of the structure, in the format expected
|
|
|
|
// by ctypes.StructType.
|
|
|
|
let struct = [];
|
|
|
|
|
|
|
|
let i = 0;
|
|
|
|
while (i < this.size) {
|
|
|
|
let currentField = this.offset_to_field_info[i];
|
|
|
|
if (!currentField) {
|
|
|
|
// No field was specified at this offset, we need to
|
|
|
|
// introduce some padding.
|
|
|
|
|
|
|
|
// Firstly, determine how many bytes of padding
|
|
|
|
let padding_length = 1;
|
|
|
|
while (i + padding_length < this.size
|
|
|
|
&& !this.offset_to_field_info[i + padding_length]) {
|
|
|
|
++padding_length;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Then add the padding
|
|
|
|
struct.push(this._makePaddingField(padding_length));
|
|
|
|
|
|
|
|
// And proceed
|
|
|
|
i += padding_length;
|
|
|
|
} else {
|
|
|
|
// We have a field at this offset.
|
|
|
|
|
|
|
|
// Firstly, ensure that we do not have two overlapping fields
|
|
|
|
for (let j = 1; j < currentField.type.size; ++j) {
|
|
|
|
let candidateField = this.offset_to_field_info[i + j];
|
|
|
|
if (candidateField) {
|
|
|
|
throw new Error("Fields " + currentField.name +
|
|
|
|
" and " + candidateField.name +
|
|
|
|
" overlap at position " + (i + j));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Then add the field
|
|
|
|
let field = ({});
|
|
|
|
field[currentField.name] = currentField.type;
|
|
|
|
struct.push(field);
|
|
|
|
|
|
|
|
// And proceed
|
|
|
|
i += currentField.type.size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let result = new Type(this.name, ctypes.StructType(this.name, struct));
|
|
|
|
if (result.implementation.size != this.size) {
|
|
|
|
throw new Error("Wrong size for type " + this.name +
|
|
|
|
": expected " + this.size +
|
|
|
|
", found " + result.implementation.size +
|
|
|
|
" (" + result.implementation.toSource() + ")");
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
exports.OS.Shared.HollowStructure = HollowStructure;
|
|
|
|
|
2012-06-10 16:44:50 -07:00
|
|
|
/**
|
|
|
|
* Declare a function through js-ctypes
|
|
|
|
*
|
|
|
|
* @param {ctypes.library} lib The ctypes library holding the function.
|
|
|
|
* @param {string} symbol The name of the function, as defined in the
|
|
|
|
* library.
|
|
|
|
* @param {ctypes.abi} abi The abi to use, or |null| for default.
|
|
|
|
* @param {Type} returnType The type of values returned by the function.
|
|
|
|
* @param {...Type} argTypes The type of arguments to the function.
|
|
|
|
*
|
|
|
|
* @return null if the function could not be defined (generally because
|
|
|
|
* it does not exist), or a JavaScript wrapper performing the call to C
|
|
|
|
* and any type conversion required.
|
|
|
|
*/// Note: Future versions will use a different implementation of this
|
|
|
|
// function on the main thread, osfile worker thread and regular worker
|
|
|
|
// thread
|
|
|
|
let declareFFI = function declareFFI(lib, symbol, abi,
|
|
|
|
returnType /*, argTypes ...*/) {
|
|
|
|
LOG("Attempting to declare FFI ", symbol);
|
|
|
|
// We guard agressively, to avoid any late surprise
|
|
|
|
if (typeof symbol != "string") {
|
|
|
|
throw new TypeError("declareFFI expects as first argument a string");
|
|
|
|
}
|
|
|
|
abi = abi || ctypes.default_abi;
|
|
|
|
if (Object.prototype.toString.call(abi) != "[object CABI]") {
|
|
|
|
// Note: This is the only known manner of checking whether an object
|
|
|
|
// is an abi.
|
|
|
|
throw new TypeError("declareFFI expects as second argument an abi or null");
|
|
|
|
}
|
2012-08-09 12:13:46 -07:00
|
|
|
if (!returnType.importFromC) {
|
|
|
|
throw new TypeError("declareFFI expects as third argument an instance of Type");
|
|
|
|
}
|
2012-06-10 16:44:50 -07:00
|
|
|
let signature = [symbol, abi];
|
|
|
|
let argtypes = [];
|
|
|
|
for (let i = 3; i < arguments.length; ++i) {
|
|
|
|
let current = arguments[i];
|
2012-07-05 01:21:09 -07:00
|
|
|
if (!current) {
|
|
|
|
throw new TypeError("Missing type for argument " + ( i - 3 ) +
|
|
|
|
" of symbol " + symbol);
|
|
|
|
}
|
2012-06-10 16:44:50 -07:00
|
|
|
if (!current.implementation) {
|
|
|
|
throw new TypeError("Missing implementation for argument " + (i - 3)
|
|
|
|
+ " of symbol " + symbol
|
|
|
|
+ " ( " + current.name + " )" );
|
|
|
|
}
|
|
|
|
signature.push(current.implementation);
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
let fun = lib.declare.apply(lib, signature);
|
|
|
|
let result = function ffi(/*arguments*/) {
|
|
|
|
let result = fun.apply(fun, arguments);
|
2012-08-09 12:13:46 -07:00
|
|
|
return returnType.importFromC(result, symbol);
|
2012-06-10 16:44:50 -07:00
|
|
|
};
|
|
|
|
if (exports.OS.Shared.DEBUG) {
|
|
|
|
result.fun = fun; // Also return the raw FFI function.
|
|
|
|
}
|
|
|
|
LOG("Function", symbol, "declared");
|
|
|
|
return result;
|
|
|
|
} catch (x) {
|
|
|
|
// Note: Not being able to declare a function is normal.
|
|
|
|
// Some functions are OS (or OS version)-specific.
|
|
|
|
LOG("Could not declare function " + symbol, x);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
exports.OS.Shared.declareFFI = declareFFI;
|
2012-06-15 08:22:50 -07:00
|
|
|
|
|
|
|
|
2012-08-10 08:52:55 -07:00
|
|
|
/**
|
|
|
|
* Libxul-based utilities, shared by all back-ends.
|
|
|
|
*/
|
|
|
|
|
|
|
|
let libxul = ctypes.open(OS.Constants.Path.libxul);
|
|
|
|
exports.OS.Shared.libxul = libxul;
|
|
|
|
|
|
|
|
exports.OS.Shared.Utils = {};
|
|
|
|
|
|
|
|
let Strings = exports.OS.Shared.Utils.Strings = {};
|
|
|
|
let Pointers = exports.OS.Shared.Utils.Pointers = {};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Import a wide string (e.g. a |jschar.ptr|) as a string.
|
|
|
|
*
|
|
|
|
* @param {CData} wstring The C representation of a widechar string
|
|
|
|
* (can be jschar* or a jschar[]).
|
|
|
|
* @return {string} The same string, as a JavaScript String.
|
|
|
|
*/
|
|
|
|
Strings.importWString = function importWString(wstring) {
|
|
|
|
return wstring.readString();
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
let NS_Free = libxul.declare("osfile_ns_free", ctypes.default_abi,
|
|
|
|
/*return*/ Types.void_t.implementation,
|
|
|
|
/*ptr*/ Types.voidptr_t.implementation);
|
|
|
|
|
|
|
|
let wstrdup = declareFFI(libxul, "osfile_wstrdup", ctypes.default_abi,
|
|
|
|
/*return*/ Types.out_wstring.releaseWith(NS_Free),
|
|
|
|
/*ptr*/ Types.wstring);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Export a string as a wide string (e.g. a |jschar.ptr|).
|
|
|
|
*
|
|
|
|
* @param {string} string A JavaScript String.
|
|
|
|
* @return {CData} The C representation of that string, as a |jschar*|.
|
|
|
|
* This value will be automatically garbage-collected once it is
|
|
|
|
* not referenced anymore.
|
|
|
|
*/
|
|
|
|
Strings.exportWString = function exportWString(string) {
|
|
|
|
return wstrdup(string);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Encodings
|
|
|
|
|
|
|
|
Strings.encodeAll = declareFFI(libxul, "osfile_EncodeAll",
|
|
|
|
ctypes.default_abi,
|
|
|
|
/*return*/ Types.void_t.out_ptr.releaseWith(NS_Free),
|
|
|
|
/*encoding*/ Types.cstring,
|
|
|
|
/*source*/ Types.wstring,
|
|
|
|
/*bytes*/ Types.uint32_t.out_ptr);
|
|
|
|
|
|
|
|
let _decodeAll = declareFFI(libxul, "osfile_DecodeAll",
|
|
|
|
ctypes.default_abi,
|
|
|
|
/*return*/ Types.out_wstring.releaseWith(NS_Free),
|
|
|
|
/*encoding*/ Types.cstring,
|
|
|
|
/*source*/ Types.void_t.in_ptr,
|
|
|
|
/*bytes*/ Types.uint32_t);
|
|
|
|
|
|
|
|
Strings.decodeAll = function decodeAll(encoding, source, bytes) {
|
|
|
|
let decoded = _decodeAll(encoding, source, bytes);
|
|
|
|
if (!decoded) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return Strings.importWString(decoded);
|
|
|
|
};
|
|
|
|
|
2012-08-13 18:54:42 -07:00
|
|
|
exports.OS.Shared.Utils = { Strings: Strings };
|
|
|
|
|
|
|
|
|
2012-06-15 08:22:50 -07:00
|
|
|
/**
|
|
|
|
* Specific tools that don't really fit anywhere.
|
|
|
|
*/
|
|
|
|
let _aux = {};
|
|
|
|
exports.OS.Shared._aux = _aux;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Utility function shared by implementations of |OS.File.open|:
|
|
|
|
* extract read/write/trunc/create/existing flags from a |mode|
|
|
|
|
* object.
|
|
|
|
*
|
|
|
|
* @param {*=} mode An object that may contain fields |read|,
|
|
|
|
* |write|, |truncate|, |create|, |existing|. These fields
|
|
|
|
* are interpreted only if true-ish.
|
|
|
|
* @return {{read:bool, write:bool, trunc:bool, create:bool,
|
|
|
|
* existing:bool}} an object recapitulating the options set
|
|
|
|
* by |mode|.
|
|
|
|
* @throws {TypeError} If |mode| contains other fields, or
|
|
|
|
* if it contains both |create| and |truncate|, or |create|
|
|
|
|
* and |existing|.
|
|
|
|
*/
|
|
|
|
_aux.normalizeOpenMode = function normalizeOpenMode(mode) {
|
|
|
|
let result = {
|
|
|
|
read: false,
|
|
|
|
write: false,
|
|
|
|
trunc: false,
|
|
|
|
create: false,
|
|
|
|
existing: false
|
|
|
|
};
|
|
|
|
for (let key in mode) {
|
|
|
|
if (!mode[key]) continue; // Only interpret true-ish keys
|
|
|
|
switch (key) {
|
|
|
|
case "read":
|
|
|
|
result.read = true;
|
|
|
|
break;
|
|
|
|
case "write":
|
|
|
|
result.write = true;
|
|
|
|
break;
|
|
|
|
case "truncate": // fallthrough
|
|
|
|
case "trunc":
|
|
|
|
result.trunc = true;
|
|
|
|
result.write = true;
|
|
|
|
break;
|
|
|
|
case "create":
|
|
|
|
result.create = true;
|
|
|
|
result.write = true;
|
|
|
|
break;
|
|
|
|
case "existing": // fallthrough
|
|
|
|
case "exist":
|
|
|
|
result.existing = true;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new TypeError("Mode " + key + " not understood");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Reject opposite modes
|
|
|
|
if (result.existing && result.create) {
|
|
|
|
throw new TypeError("Cannot specify both existing:true and create:true");
|
|
|
|
}
|
|
|
|
if (result.trunc && result.create) {
|
|
|
|
throw new TypeError("Cannot specify both trunc:true and create:true");
|
|
|
|
}
|
|
|
|
// Handle read/write
|
|
|
|
if (!result.write) {
|
|
|
|
result.read = true;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
};
|
2012-06-10 16:44:50 -07:00
|
|
|
})(this);
|
|
|
|
}
|