Bug 1232067 - Ensure the libc free() is used if the a.out free() is not available. r=yoric

--HG--
extra : rebase_source : 9545ddec5807d63fc2a002480db02c81f2324a72
This commit is contained in:
aleth 2016-06-09 01:15:57 +02:00
parent e93ec93a47
commit 5f700307ff
2 changed files with 47 additions and 19 deletions

View File

@ -1050,6 +1050,42 @@ Library.prototype = Object.freeze({
});
},
/**
* Define a js-ctypes function lazily using ctypes method declare,
* with a fallback library to use if this library can't be opened
* or the function cannot be declared.
*
* @param {fallbacklibrary} The fallback Library object.
* @param {object} The object containing the function as a field.
* @param {string} The name of the field containing 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 {ctypes.CType} returnType The type of values returned by the function.
* @param {...ctypes.CType} argTypes The type of arguments to the function.
*/
declareLazyWithFallback: function(fallbacklibrary, object, field, ...args) {
let lib = this;
Object.defineProperty(object, field, {
get: function() {
delete this[field];
try {
let ffi = lib.library.declare(...args);
if (ffi) {
return this[field] = ffi;
}
} catch (ex) {
// Use the fallback library and get the symbol from there.
fallbacklibrary.declareLazy(object, field, ...args);
return object[field];
}
return undefined;
},
configurable: true,
enumerable: true
});
},
toString: function() {
return "[Library " + this.name + "]";
}

View File

@ -264,25 +264,17 @@
// We override the definition of free() on several platforms.
let default_lib = new SharedAll.Library("default_lib",
"a.out");
try {
// On platforms for which we override free(), nspr defines
// a special library name "a.out" that will resolve to the
// correct implementation free().
// If it turns out we don't have an a.out library or a.out
// doesn't contain free, use the ordinary libc free.
default_lib.declareLazy(SysFile, "free",
default_lib.declareLazyWithFallback(libc, SysFile, "free",
"free", ctypes.default_abi,
/*return*/ ctypes.void_t,
/*ptr*/ ctypes.voidptr_t);
} catch (ex) {
// We don't have an a.out library or a.out doesn't contain free.
// Either way, use the ordinary libc free.
libc.declareLazy(SysFile, "free",
"free", ctypes.default_abi,
/*return*/ ctypes.void_t,
/*ptr*/ ctypes.voidptr_t);
}
}