Skip to content

Commit

Permalink
Fixed issue where ~9 MB sbml file loading caused runtime memory excep…
Browse files Browse the repository at this point in the history
…tion

Final check in before first release.
  • Loading branch information
bartjuw committed Apr 18, 2023
1 parent 489db89 commit 401a32e
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 27 deletions.
81 changes: 55 additions & 26 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,18 @@
var getSBMLInfoMessages;// "
var getSBMLWarnings; // "
var freeAll; // "

var jsFree; // emscripten function
//var jsMalloc; // " Not needed
var jsAllocateUTF8; // "
var jsUTF8ToString; // "

// Load library functions (asynchronous call):
try {
libantimony().then((libantimony) => {
// Format: libantimony.cwrap( function name, return type, input param array of types).
loadString = libantimony.cwrap('loadString', 'number', ['string']);
loadAntimonyString = libantimony.cwrap('loadAntimonyString', 'number', ['string']);
loadSBMLString = libantimony.cwrap('loadSBMLString', 'number', ['string']);
loadString = libantimony.cwrap('loadString', 'number', ['number']);
loadAntimonyString = libantimony.cwrap('loadAntimonyString', 'number', ['number']);
loadSBMLString = libantimony.cwrap('loadSBMLString', 'number', ['number']);
getSBMLString = libantimony.cwrap('getSBMLString', 'string', ['null']);
getAntimonyString = libantimony.cwrap('getAntimonyString', 'string', ['null']);
getCompSBMLString = libantimony.cwrap('getCompSBMLString', 'string', ['string']);
Expand All @@ -46,15 +49,22 @@
getSBMLInfoMessages = libantimony.cwrap('getSBMLInfoMessages', 'string', ['string']);
getSBMLWarnings = libantimony.cwrap('getSBMLWarnings', 'string', ['string']);
freeAll = libantimony.cwrap('freeAll', 'null', ['null']);
// Emscripten funcs (direct calls):
jsFree = (strPtr) => libantimony._free(strPtr);
jsAllocateUTF8 = (newStr) => libantimony.allocateUTF8(newStr);
// Do not need?:
jsUTF8ToString = (strPtr) => libantimony.UTF8ToString(strPtr);
//jsMalloc = (strLenPlus1) => libantimony._malloc(strLenPlus1); // Not needed, use jsAllocateUTF8 to reserve space on Emscripten Heap for string.
});
}
catch(err) {
console.log('Load libantimony error: ', err);
}
// Test functionality:
function runAntimonyCheck() {
var intResult = loadString(antString); // TEST loadString()
var intResult = loadString( jsAllocateUTF8(antString) ); // TEST loadString()
var antResult;
console.log('** TEST: loadString():');
if(intResult < 0) {
console.log('loadString() returned error:' ,intResult);
console.log(getLastError());
Expand All @@ -66,26 +76,41 @@
clearPreviousLoads();

sbmlResult = 'none'; // TEST loadAntimonyString()
intResult = loadAntimonyString(antString);
intResult = loadAntimonyString( jsAllocateUTF8(antString) );
sbmlResult = getSBMLString();
console.log('load ant again, return int:' ,intResult);
console.log('** TEST: loadAntimonyString(), return int:' ,intResult);
console.log('SBML again: ' ,sbmlResult);

clearPreviousLoads(); // TEST loadSBMLString()
intResult = loadSBMLString(sbmlResult);
console.log('getWarnings: ', getWarnings());
intResult = loadSBMLString( jsAllocateUTF8(sbmlResult) );
antResult = getAntimonyString();
console.log('load SBML string, return int:' ,intResult);
console.log('** TEST: loadSBMLString(), return int:' ,intResult);
console.log('Antimony string: ' ,antResult);
console.log('getWarnings: ', getWarnings());

clearPreviousLoads(); // TEST getLastError()
clearPreviousLoads(); // TEST getLastError(), SBML error
var errSBMLStr = sbmlResult.replace('id="S1"', 'id="S5"');
intResult = loadSBMLString(errSBMLStr);
console.log('load bad SBML model, return int:' ,intResult);
console.log('getLastError(): ', getLastError());
console.log('getSBMLInfoMessages(): ', getSBMLInfoMessages());// none expected
console.log('getSBMLWarnings(): ', getSBMLWarnings('example1')); // none expected
intResult = loadSBMLString( jsAllocateUTF8(errSBMLStr) );
console.log('** TEST: load bad SBML model, return int:' ,intResult);
console.log('bad SBML: getLastError(): ', getLastError());
console.log('bad SBML: getSBMLInfoMessages(): ', getSBMLInfoMessages());// none expected
console.log('bad SBML: getSBMLWarnings(): ', getSBMLWarnings('example1')); // none expected

clearPreviousLoads(); // TEST getLastError(), Antimony error
var errAntStr = "model example1; S1 -> S2; k1*S*S5 S1 > S5*550; S2 = 0; k1 = 0.1; end";
intResult = loadAntimonyString( jsAllocateUTF8(errAntStr) );
console.log('** TEST: load Bad Antimony model, return int:' ,intResult);
console.log('Bad Antimony: getLastError(): ', getLastError()); // none expected
console.log('Bad Antimony: getSBMLInfoMessages(): ', getSBMLInfoMessages());// none expected
console.log('Bad Antimony: getSBMLWarnings(): ', getSBMLWarnings('example1'));

clearPreviousLoads(); // TEST getSBMLWarnings(), Antimony error
var errAntStr = "model example1; S1 -> S2; k1*S*S5 ; S1 > S5*550; S2 = 0; k1 = 0.1; end";
intResult = loadAntimonyString( jsAllocateUTF8(errAntStr) );
console.log('** TEST: load Warnings Antimony model, return int:' ,intResult);
console.log('Warnings Antimony: getLastError(): ', getLastError()); // none expected
console.log(' Warnings Antimony: getSBMLInfoMessages(): ', getSBMLInfoMessages());// none expected
console.log(' Warnings Antimony: getSBMLWarnings(): ', getSBMLWarnings('example1'));
return sbmlResult;
}
</script>
Expand All @@ -96,23 +121,26 @@
antCode = document.getElementById("antimonycode").value;
clearPreviousLoads();
//console.log("*** Antimony code: ",antCode);
var load_int= loadAntimonyString(antCode);
var ptrAntCode = jsAllocateUTF8(antCode);
var load_int= loadAntimonyString(ptrAntCode);
console.log("processAntimony: int returned: ", load_int);
if (load_int > 0) {
sbmlResult = getSBMLString();
document.getElementById("sbmlcode").value = sbmlResult;
document.getElementById("procSBMLBtn").disabled = false;

}
else {
var errStr = getLastError();
window.alert(errStr); }

jsFree(ptrAntCode);
}

function processSBML() {
sbmlCode = document.getElementById("sbmlcode").value;
clearPreviousLoads();
var load_int= loadSBMLString(sbmlCode);
var ptrSBMLCode = jsAllocateUTF8(sbmlCode);
var load_int= loadSBMLString(ptrSBMLCode);
console.log("processSBML: int returned: ", load_int);
if (load_int > 0) {
antResult = getAntimonyString();
Expand All @@ -123,19 +151,19 @@
var errStr = getLastError();
window.alert(errStr);
}

jsFree(ptrSBMLCode);

}

function processFile(fileStr) {
console.log('Loaded Model str: ', fileStr);
//console.log('Loaded Model str: ', fileStr);
try {
if(loadAntimonyString(fileStr) > 0) {
//if(loadSBMLString(fileStr) > 0) {
var ptrFileStr = jsAllocateUTF8(fileStr);
if(loadAntimonyString(ptrFileStr) > 0) {
document.getElementById("antimonycode").value = fileStr;
document.getElementById("procSBMLBtn").disabled = true;
document.getElementById("sbmlcode").value = "[SBML code here.]";}
else if (loadSBMLString(fileStr) > 0){
// if (loadSBMLString(fileStr) > 0){
document.getElementById("sbmlcode").value = "[SBML code here.]";}
else if (loadSBMLString(ptrFileStr) > 0){
document.getElementById("sbmlcode").value = fileStr;
document.getElementById("procAntimonyBtn").disabled = true;
document.getElementById("antimonycode").value = "[Antimony code here.]";
Expand All @@ -150,6 +178,7 @@
console.log('processing file error: :', err);
window.alert(err);
}
jsFree(ptrFileStr);
}

</script>
Expand Down
2 changes: 1 addition & 1 deletion docs/libantimony.js

Large diffs are not rendered by default.

Binary file modified docs/libantimony.wasm
Binary file not shown.
14 changes: 14 additions & 0 deletions release/current/ReleaseInfo.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
libAntimonyjs release 1.0.0 info:
Date: 2023apr18
libSBML source: 5.19.0
Antimony source: 2.13.3
Emscripten SDK version: 3.1.20

Following Methods are available:

From Antimony library:
_loadString, _loadAntimonyString, _loadSBMLString, _clearPreviousLoads, _getAntimonyString, _freeAll,
_getSBMLString, _getCompSBMLString, _getLastError, _getWarnings, _getSBMLInfoMessages, _getSBMLWarnings,

From Emscripten:
_malloc, _free, ccall, cwrap, allocateUTF8, UTF8ToString
21 changes: 21 additions & 0 deletions release/current/libantimony.js

Large diffs are not rendered by default.

Binary file added release/current/libantimony.wasm
Binary file not shown.

0 comments on commit 401a32e

Please sign in to comment.