aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/analyzer.js128
-rw-r--r--src/compiler.js4
-rw-r--r--src/experimental/stringCache.diff147
-rw-r--r--src/intertyper.js14
-rw-r--r--src/jsifier.js52
-rw-r--r--src/library.js316
-rw-r--r--src/library_browser.js343
-rw-r--r--src/library_egl.js6
-rw-r--r--src/library_gc.js167
-rw-r--r--src/library_gl.js1939
-rw-r--r--src/library_glut.js26
-rw-r--r--src/library_sdl.js745
-rw-r--r--src/modules.js5
-rw-r--r--src/parseTools.js72
-rw-r--r--src/postamble.js49
-rw-r--r--src/preamble.js76
-rw-r--r--src/runtime.js71
-rw-r--r--src/settings.js19
-rw-r--r--src/shell.html57
-rw-r--r--src/shell.js18
-rw-r--r--src/utility.js4
21 files changed, 3520 insertions, 738 deletions
diff --git a/src/analyzer.js b/src/analyzer.js
index 8ded86f1..4bf2255e 100644
--- a/src/analyzer.js
+++ b/src/analyzer.js
@@ -9,7 +9,7 @@ var VAR_NATIVIZED = 'nativized';
var VAR_EMULATED = 'emulated';
var ENTRY_IDENT = toNiceIdent('%0');
-var ENTRY_IDENTS = set(toNiceIdent('%0'), toNiceIdent('%1'));
+var ENTRY_IDENT_IDS = set(0, 1); // XXX
function recomputeLines(func) {
func.lines = func.labels.map(function(label) { return label.lines }).reduce(concatenator, []);
@@ -544,6 +544,10 @@ function analyzer(data, sidePass) {
params: [(signed && j + whole > sourceElements.length) ? signedKeepAlive : null],
type: 'i32',
};
+ if (j == 0 && isUnsignedOp(value.op) && sourceBits < 32) {
+ // zext sign correction
+ result.ident = makeSignOp(result.ident, 'i' + sourceBits, 'un', 1, 1);
+ }
if (fraction != 0) {
var other = {
intertype: 'value',
@@ -1176,7 +1180,9 @@ function analyzer(data, sidePass) {
func.labelIdsInverse = {};
func.labelIds[toNiceIdent('%0')] = 0;
func.labelIdsInverse[0] = toNiceIdent('%0');
- func.labelIdCounter = 1;
+ func.labelIds[toNiceIdent('%1')] = 1;
+ func.labelIdsInverse[1] = toNiceIdent('%1');
+ func.labelIdCounter = 2;
func.labels.forEach(function(label) {
func.labelIds[label.ident] = func.labelIdCounter++;
func.labelIdsInverse[func.labelIdCounter-1] = label.ident;
@@ -1202,6 +1208,7 @@ function analyzer(data, sidePass) {
if (phi.intertype == 'phi') {
for (var i = 0; i < phi.params.length; i++) {
phi.params[i].label = func.labelIds[phi.params[i].label];
+ if (!phi.params[i].label) warn('phi refers to nonexistent label on line ' + phi.lineNum);
}
}
});
@@ -1217,9 +1224,10 @@ function analyzer(data, sidePass) {
// So we need to handle that in a special way here.
function getActualLabelId(labelId) {
if (func.labelsDict[labelId]) return labelId;
- if (labelId in ENTRY_IDENTS) {
- assert(func.labelsDict[ENTRY_IDENT]);
- return ENTRY_IDENT;
+ if (labelId in ENTRY_IDENT_IDS) {
+ labelId = func.labelIds[ENTRY_IDENT];
+ assert(func.labelsDict[labelId]);
+ return labelId;
}
return null;
}
@@ -1270,88 +1278,44 @@ function analyzer(data, sidePass) {
recomputeLines(func);
}
- if (!MICRO_OPTS) {
- // 'Emulate' phis, by doing an if where the phi appears in the .ll. For this
- // we need __lastLabel__.
- func.needsLastLabel = false;
- func.labels.forEach(function(label) {
- var phis = [];
- label.lines.forEach(function(phi) {
- if (phi.intertype == 'phi') {
- for (var i = 0; i < phi.params.length; i++) {
- var sourceLabelId = getActualLabelId(phi.params[i].label);
- if (sourceLabelId) {
- var sourceLabel = func.labelsDict[sourceLabelId];
- var lastLine = sourceLabel.lines.slice(-1)[0];
- assert(lastLine.intertype in LLVM.PHI_REACHERS, 'Only some can lead to labels with phis:' + [func.ident, label.ident, lastLine.intertype]);
- lastLine.currLabelId = sourceLabelId;
- }
- }
- phis.push(phi);
- func.needsLastLabel = true;
- }
- });
+ // Properly implement phis, by pushing them back into the branch
+ // that leads to here. We will only have the |var| definition in this location.
- if (phis.length >= 2) {
- // Multiple phis have the semantics that they all occur 'in parallel', i.e., changes to
- // a variable that is the result of a phi should *not* affect the other results. We must
- // therefore be careful!
- phis[phis.length-1].postSet = '; /* post-phi: */';
- for (var i = 0; i < phis.length-1; i++) {
- var ident = phis[i].assignTo;
- var phid = ident+'$phi'
- phis[phis.length-1].postSet += ident + '=' + phid + ';';
- phis[i].assignTo = phid;
- func.variables[phid] = {
- ident: phid,
- type: func.variables[ident].type,
- origin: func.variables[ident].origin,
- lineNum: func.variables[ident].lineNum,
- uses: 1,
- impl: VAR_EMULATED
- };
- }
- }
- });
- } else {
- // MICRO_OPTS == 1: Properly implement phis, by pushing them back into the branch
- // that leads to here. We will only have the |var| definition in this location.
-
- // First, push phis back
- func.labels.forEach(function(label) {
- label.lines.forEach(function(phi) {
- if (phi.intertype == 'phi') {
- for (var i = 0; i < phi.params.length; i++) {
- var param = phi.params[i];
- var sourceLabelId = getActualLabelId(param.label);
- if (sourceLabelId) {
- var sourceLabel = func.labelsDict[sourceLabelId];
- var lastLine = sourceLabel.lines.slice(-1)[0];
- assert(lastLine.intertype in LLVM.PHI_REACHERS, 'Only some can lead to labels with phis:' + [func.ident, label.ident, lastLine.intertype]);
- if (!lastLine.phi) {
- lastLine.phi = true;
- assert(!lastLine.dependent);
- lastLine.dependent = {
- intertype: 'phiassigns',
- params: []
- };
+ // First, push phis back
+ func.labels.forEach(function(label) {
+ label.lines.forEach(function(phi) {
+ if (phi.intertype == 'phi') {
+ for (var i = 0; i < phi.params.length; i++) {
+ var param = phi.params[i];
+ if (!param.label) warn('phi refers to nonexistent label on line ' + phi.lineNum);
+ var sourceLabelId = getActualLabelId(param.label);
+ if (sourceLabelId) {
+ var sourceLabel = func.labelsDict[sourceLabelId];
+ var lastLine = sourceLabel.lines.slice(-1)[0];
+ assert(lastLine.intertype in LLVM.PHI_REACHERS, 'Only some can lead to labels with phis:' + [func.ident, label.ident, lastLine.intertype]);
+ if (!lastLine.phi) {
+ lastLine.phi = true;
+ assert(!lastLine.dependent);
+ lastLine.dependent = {
+ intertype: 'phiassigns',
+ params: []
};
- lastLine.dependent.params.push({
- intertype: 'phiassign',
- ident: phi.assignTo,
- value: param.value,
- targetLabel: label.ident
- });
- }
+ };
+ lastLine.dependent.params.push({
+ intertype: 'phiassign',
+ ident: phi.assignTo,
+ value: param.value,
+ targetLabel: label.ident
+ });
}
- // The assign to phi is now just a var
- phi.intertype = 'var';
- phi.ident = phi.assignTo;
- phi.assignTo = null;
}
- });
+ // The assign to phi is now just a var
+ phi.intertype = 'var';
+ phi.ident = phi.assignTo;
+ phi.assignTo = null;
+ }
});
- }
+ });
});
this.forwardItem(item, 'StackAnalyzer');
}
diff --git a/src/compiler.js b/src/compiler.js
index 29ae47dd..89da32d5 100644
--- a/src/compiler.js
+++ b/src/compiler.js
@@ -114,6 +114,7 @@ load('settings.js');
var settings_file = arguments_[0];
var ll_file = arguments_[1];
+additionalLibraries = Array.prototype.slice.call(arguments_, 2);
if (settings_file) {
var settings = JSON.parse(read(settings_file));
@@ -122,6 +123,7 @@ if (settings_file) {
}
}
+
if (CORRECT_SIGNS >= 2) {
CORRECT_SIGNS_LINES = set(CORRECT_SIGNS_LINES); // for fast checking
}
@@ -144,6 +146,8 @@ if (PGO) { // by default, correct everything during PGO
EXPORTED_FUNCTIONS = set(EXPORTED_FUNCTIONS);
EXPORTED_GLOBALS = set(EXPORTED_GLOBALS);
+RUNTIME_DEBUG = LIBRARY_DEBUG || GL_DEBUG;
+
// Settings sanity checks
assert(!(USE_TYPED_ARRAYS === 2 && QUANTUM_SIZE !== 4), 'For USE_TYPED_ARRAYS == 2, must have normal QUANTUM_SIZE of 4');
diff --git a/src/experimental/stringCache.diff b/src/experimental/stringCache.diff
new file mode 100644
index 00000000..26cbc68c
--- /dev/null
+++ b/src/experimental/stringCache.diff
@@ -0,0 +1,147 @@
+diff --git a/src/library_gl.js b/src/library_gl.js
+index 7471578..9228964 100644
+--- a/src/library_gl.js
++++ b/src/library_gl.js
+@@ -1256,28 +1256,28 @@ var LibraryGL = {
+
+ setClientAttribute: function(name, size, type, stride, pointer) {
+ var attrib = this.clientAttributes[GL.immediate.ATTRIBUTE_BY_NAME[name]];
+ attrib.size = size;
+ attrib.type = type;
+ attrib.stride = stride;
+ attrib.pointer = pointer;
+- attrib.name = name + size;
++ attrib.name = Runtime.getStringConcat(name, size);
+ },
+
+ // Renderers
+ addRendererComponent: function(component) {
+ if (this.rendererComponents[component]) return;
+ this.rendererComponents[component] = 1;
+- this.renderer += component;
++ this.renderer = Runtime.getStringConcat(this.renderer, component);
+ },
+
+ setRenderer: function(renderer) {
+ var name = renderer;
+ if (GL.currProgram && renderer[0] != 'U') {
+- name = 'UD' + GL.currProgram + '|' + renderer; // user-defined program renderer
++ name = Runtime.getStringConcat(Runtime.getStringConcat('UD', GL.currProgram), Runtime.getStringConcat('|', renderer)); // user-defined program renderer
+ }
+ this.renderer = name;
+ if (this.renderers[name]) return this.renderers[name];
+ this.renderers[name] = this.createRenderer(renderer);
+ return this.renderers[name];
+ },
+
+@@ -1300,15 +1300,18 @@ var LibraryGL = {
+ }
+ vertexSize += size * 4; // XXX assuming float
+ } else if (which == 'N') {
+ vertexSize += 4; // 1 char, + alignment
+ } else if (which == 'C') {
+ vertexSize += 4; // Up to 4 chars, + alignment
+ } else {
+- console.log('Warning: Ignoring renderer attribute ' + which);
++#if ASSERTIONS
++ console.log('Warning: Ignoring renderer attribute');
++ console.log(which);
++#endif
+ size = parseInt(renderer[i+1]);
+ vertexSize += size * 4; // XXX assuming float
+ }
+ }
+ assert(positionSize > 0);
+ // TODO: verify vertexSize is equal to the stride in enabled client arrays
+ var useCurrProgram = !!GL.currProgram;
+@@ -1465,30 +1468,30 @@ var LibraryGL = {
+ var renderer = '', bytes = 0;
+ for (var i = 0; i < attributes.length; i++) {
+ var attribute = attributes[i];
+ if (!attribute) break;
+ attribute.offset = attribute.pointer - start;
+ if (attribute.offset > bytes) { // ensure we start where we should
+ assert((attribute.offset - bytes)%4 == 0); // XXX assuming 4-alignment
+- renderer += '?' + ((attribute.offset - bytes)/4);
++ renderer = Runtime.getStringConcat(renderer, Runtime.getStringConcat('?', ((attribute.offset - bytes)/4)));
+ bytes += attribute.offset - bytes;
+ }
+- renderer += attribute.name;
++ renderer = Runtime.getStringConcat(renderer, attribute.name);
+ bytes += attribute.size * GL.immediate.byteSizeByType[attribute.type];
+ if (bytes % 4 != 0) bytes += 4 - (bytes % 4); // XXX assuming 4-alignment
+ #if ASSERTIONS
+ assert(0 <= attribute.offset && attribute.offset < stride); // must all be in the same buffer
+ #endif
+ }
+
+ assert(stride == 0 || bytes <= stride);
+
+ if (bytes < stride) { // ensure the size is that of the stride
+ assert((stride - bytes)%4 == 0); // assuming float
+- renderer += '?' + ((stride-bytes)/4);
++ renderer = Runtime.getStringConcat(renderer, Runtime.getStringConcat('?', ((stride-bytes)/4)));
+ bytes = stride;
+ }
+
+ bytes *= count;
+ if (!GL.currArrayBuffer) {
+ GL.immediate.vertexData = {{{ makeHEAPView('F32', 'start', 'start + bytes') }}}; // XXX assuming float
+ }
+@@ -1671,15 +1674,15 @@ var LibraryGL = {
+ },
+
+ glVertexPointer__deps: ['$GLEmulation'], // if any pointers are used, glVertexPointer must be, and if it is, then we need emulation
+ glVertexPointer: function(size, type, stride, pointer) {
+ GL.immediate.setClientAttribute('V', size, type, stride, pointer);
+ },
+ glTexCoordPointer: function(size, type, stride, pointer) {
+- GL.immediate.setClientAttribute('T' + GL.immediate.clientActiveTexture, size, type, stride, pointer);
++ GL.immediate.setClientAttribute(Runtime.getStringConcat('T', GL.immediate.clientActiveTexture), size, type, stride, pointer);
+ },
+ glNormalPointer: function(type, stride, pointer) {
+ GL.immediate.setClientAttribute('N', 1, type, stride, pointer);
+ },
+ glColorPointer: function(size, type, stride, pointer) {
+ GL.immediate.setClientAttribute('C', size, type, stride, pointer);
+ },
+diff --git a/src/runtime.js b/src/runtime.js
+index 6a251c4..012a66d 100644
+--- a/src/runtime.js
++++ b/src/runtime.js
+@@ -319,25 +319,34 @@ var Runtime = {
+ if (!Runtime.warnOnce.shown) Runtime.warnOnce.shown = {};
+ if (!Runtime.warnOnce.shown[text]) {
+ Runtime.warnOnce.shown[text] = 1;
+ Module.printErr(text);
+ }
+ },
+
++ // Cache for JS function wrappers for C functions in FUNCTION_TABLE
+ funcWrappers: {},
+-
+ getFuncWrapper: function(func) {
+ if (!Runtime.funcWrappers[func]) {
+ Runtime.funcWrappers[func] = function() {
+ FUNCTION_TABLE[func].apply(null, arguments);
+ };
+ }
+ return Runtime.funcWrappers[func];
+ },
+
++ // Cache for small recurring strings generated by concatenating other
++ // strings, use this to avoid needless allocation and collection
++ stringCache: {},
++ getStringConcat: function(a, b) {
++ var cacheItem = Runtime.stringCache[a];
++ if (!cacheItem) cacheItem = Runtime.stringCache[a] = {};
++ return cacheItem[b] || (cacheItem[b] = a + b);
++ },
++
+ #if RUNTIME_DEBUG
+ debug: true, // Switch to false at runtime to disable logging at the right times
+
+ printObjectList: [],
+
+ prettyPrint: function(arg) {
+ if (typeof arg == 'undefined') return '!UNDEFINED!';
diff --git a/src/intertyper.js b/src/intertyper.js
index 0f9ce659..fbad353a 100644
--- a/src/intertyper.js
+++ b/src/intertyper.js
@@ -69,12 +69,12 @@ function intertyper(data, sidePass, baseLineNums) {
if (mainPass && (line[0] == '%' || line[0] == '@')) {
// If this isn't a type, it's a global variable, make a note of the information now, we will need it later
- var testType = /[@%\w\d\.\" $]+ = type .*/.exec(line);
+ var testType = /[@%\w\d\.\" $-]+ = type .*/.exec(line);
if (!testType) {
- var global = /([@%\w\d\.\" $]+) = .*/.exec(line);
+ var global = /([@%\w\d\.\" $-]+) = .*/.exec(line);
var globalIdent = toNiceIdent(global[1]);
- var testAlias = /[@%\w\d\.\" $]+ = alias .*/.exec(line);
- var testString = /^[^"]+c\"[^"]+"/.exec(line);
+ var testAlias = /[@%\w\d\.\" $-]+ = alias .*/.exec(line);
+ var testString = /[@%\w\d\.\" $-]+ = [\w ]+ \[\d+ x i8] c".*/.exec(line);
Variables.globals[globalIdent] = {
name: globalIdent,
alias: !!testAlias,
@@ -350,6 +350,10 @@ function intertyper(data, sidePass, baseLineNums) {
return 'FuncHeader';
if (tokensLength >= 1 && token0Text == '}')
return 'FuncEnd';
+ if (token0Text == 'module' && token1Text == 'asm') {
+ warn('Ignoring module asm: ' + item.tokens[2].text);
+ return '/dev/null';
+ }
}
if (tokensLength >= 3 && (token0Text == 'call' || token1Text == 'call'))
return 'Call';
@@ -790,7 +794,7 @@ function intertyper(data, sidePass, baseLineNums) {
value: parseLLVMSegment(typeToken.concat(subSegments[0]))
};
return ret;
- });
+ }).filter(function(param) { return param.value && param.value.ident != 'undef' });
this.forwardItem(item, 'Reintegrator');
}
});
diff --git a/src/jsifier.js b/src/jsifier.js
index 904517e1..01ecd7d3 100644
--- a/src/jsifier.js
+++ b/src/jsifier.js
@@ -71,7 +71,7 @@ function JSify(data, functionsOnly, givenFunctions) {
}
}
} else {
- libFuncsToInclude = ['memcpy', 'memset', 'malloc', 'free'];
+ libFuncsToInclude = ['memcpy', 'memset', 'malloc', 'free', '$Browser'];
}
libFuncsToInclude.forEach(function(ident) {
data.functionStubs.push({
@@ -283,7 +283,7 @@ function JSify(data, functionsOnly, givenFunctions) {
var val = LibraryManager.library[shortident];
var padding;
if (Runtime.isNumberType(item.type) || isPointerType(item.type)) {
- padding = [item.type].concat(zeros(Runtime.getNativeFieldSize(item.type)));
+ padding = [item.type].concat(zeros(Runtime.getNativeFieldSize(item.type)-1));
} else {
padding = makeEmptyStruct(item.type);
}
@@ -408,8 +408,8 @@ function JSify(data, functionsOnly, givenFunctions) {
// name the function; overwrite if it's already named
snippet = snippet.replace(/function(?:\s+([^(]+))?\s*\(/, 'function _' + ident + '(');
if (LIBRARY_DEBUG) {
- snippet = snippet.replace('{', '{ var ret = (function() {Module.printErr("[library call:' + ident + ': " + Array.prototype.slice.call(arguments) + "]"); ');
- snippet = snippet.substr(0, snippet.length-1) + '}).apply(this, arguments); Module.printErr(" [ return:" + ret); return ret; }';
+ snippet = snippet.replace('{', '{ var ret = (function() { if (Runtime.debug) Module.printErr("[library call:' + ident + ': " + Array.prototype.slice.call(arguments).map(Runtime.prettyPrint) + "]"); ');
+ snippet = snippet.substr(0, snippet.length-1) + '}).apply(this, arguments); if (Runtime.debug && typeof ret !== "undefined") Module.printErr(" [ return:" + Runtime.prettyPrint(ret)); return ret; }';
}
}
@@ -432,7 +432,7 @@ function JSify(data, functionsOnly, givenFunctions) {
} else {
ident = '_' + ident;
}
- var text = (deps ? '\n' + deps.map(addFromLibrary).join('\n') : '');
+ var text = (deps ? '\n' + deps.map(addFromLibrary).filter(function(x) { return x != '' }).join('\n') : '');
text += isFunction ? snippet : 'var ' + ident + '=' + snippet + ';';
if (ident in EXPORTED_FUNCTIONS) {
text += '\nModule["' + ident + '"] = ' + ident + ';';
@@ -450,6 +450,9 @@ function JSify(data, functionsOnly, givenFunctions) {
item.JS = addFromLibrary(shortident);
} else {
item.JS = 'var ' + item.ident + '; // stub for ' + item.ident;
+ if (WARN_ON_UNDEFINED_SYMBOLS) {
+ warn('Unresolved symbol: ' + item.ident);
+ }
}
return ret;
}
@@ -555,9 +558,6 @@ function JSify(data, functionsOnly, givenFunctions) {
if (CLOSURE_ANNOTATIONS) func.JS += '/** @type {number} */';
func.JS += ' var __label__;\n';
}
- if (func.needsLastLabel) {
- func.JS += ' var __lastLabel__ = null;\n';
- }
// Walk function blocks and generate JS
function walkBlock(block, indent) {
@@ -748,7 +748,7 @@ function JSify(data, functionsOnly, givenFunctions) {
makeFuncLineActor('noop', function(item) {
return ';';
});
- makeFuncLineActor('var', function(item) { // assigns into phis become simple vars when MICRO_OPTS
+ makeFuncLineActor('var', function(item) { // assigns into phis become simple vars
return 'var ' + item.ident + ';';
});
makeFuncLineActor('store', function(item) {
@@ -762,6 +762,10 @@ function JSify(data, functionsOnly, givenFunctions) {
}
switch (impl) {
case VAR_NATIVIZED:
+ if (isNumber(item.ident)) {
+ // Direct write to a memory address; this may be an intentional segfault, if not, it is a bug in the source
+ return 'throw "fault on write to ' + item.ident + '";';
+ }
return item.ident + '=' + value + ';'; // We have the actual value here
break;
case VAR_EMULATED:
@@ -789,11 +793,8 @@ function JSify(data, functionsOnly, givenFunctions) {
return label;
}
- function makeBranch(label, lastLabel, labelIsVariable) {
+ function makeBranch(label, lastLabel, labelIsVariable) { // lastLabel is deprecated
var pre = '';
- if (!MICRO_OPTS && lastLabel) {
- pre = '__lastLabel__ = ' + getLabelId(lastLabel) + '; ';
- }
if (label[0] == 'B') {
assert(!labelIsVariable, 'Cannot handle branches to variables with special branching options');
var parts = label.split('|');
@@ -1030,7 +1031,16 @@ function JSify(data, functionsOnly, givenFunctions) {
makeFuncLineActor('extractvalue', function(item) {
assert(item.indexes.length == 1); // TODO: use getelementptr parsing stuff, for depth. For now, we assume that LLVM aggregates are flat,
// and we emulate them using simple JS objects { f1: , f2: , } etc., for speed
- return item.ident + '.f' + item.indexes[0][0].text;
+ var index = item.indexes[0][0].text;
+ var valueType = Types.types[item.type].fields[index];
+ if (USE_TYPED_ARRAYS != 2 || valueType != 'i64') {
+ return item.ident + '.f' + index;
+ } else {
+ var assignTo = item.assignTo;
+ item.assignTo = null;
+ return 'var ' + assignTo + '$0 = ' + item.ident + '.f' + index + '[0];' +
+ 'var ' + assignTo + '$1 = ' + item.ident + '.f' + index + '[1];';
+ }
});
makeFuncLineActor('insertvalue', function(item) {
assert(item.indexes.length == 1); // TODO: see extractvalue
@@ -1052,20 +1062,6 @@ function JSify(data, functionsOnly, givenFunctions) {
return RuntimeGenerator.stackAlloc(getFastValue(calcAllocatedSize(item.allocatedType), '*', item.allocatedNum));
}
});
- makeFuncLineActor('phi', function(item) {
- var params = item.params;
- assert(!MICRO_OPTS);
- function makeOne(i) {
- if (i === params.length-1) {
- return finalizeLLVMParameter(params[i].value);
- }
- return '__lastLabel__ == ' + getLabelId(params[i].label) + ' ? ' +
- finalizeLLVMParameter(params[i].value) + ' : (' + makeOne(i+1) + ')';
- }
- var ret = makeOne(0);
- if (item.postSet) ret += item.postSet;
- return ret;
- });
makeFuncLineActor('mathop', processMathop);
diff --git a/src/library.js b/src/library.js
index 5b85c56f..bb73c48a 100644
--- a/src/library.js
+++ b/src/library.js
@@ -28,7 +28,14 @@ LibraryManager.library = {
$FS__deps: ['$ERRNO_CODES', '__setErrNo', 'stdin', 'stdout', 'stderr', '_impure_ptr'],
$FS__postset: '__ATINIT__.unshift({ func: function() { if (!Module["noFSInit"] && !FS.init.initialized) FS.init() } });' +
'__ATMAIN__.push({ func: function() { FS.ignorePermissions = false } });' +
- '__ATEXIT__.push({ func: function() { FS.quit() } });',
+ '__ATEXIT__.push({ func: function() { FS.quit() } });' +
+ // export some names through closure
+ 'Module["FS_createFolder"] = FS.createFolder;' +
+ 'Module["FS_createPath"] = FS.createPath;' +
+ 'Module["FS_createDataFile"] = FS.createDataFile;' +
+ 'Module["FS_createLazyFile"] = FS.createLazyFile;' +
+ 'Module["FS_createLink"] = FS.createLink;' +
+ 'Module["FS_createDevice"] = FS.createDevice;',
$FS: {
// The path to the current folder.
currentPath: '/',
@@ -90,18 +97,18 @@ LibraryManager.library = {
#if FS_LOG
var inputPath = path;
function log() {
- print('FS.analyzePath("' + inputPath + '", ' +
- dontResolveLastLink + ', ' +
- linksVisited + ') => {' +
- 'isRoot: ' + ret.isRoot + ', ' +
- 'exists: ' + ret.exists + ', ' +
- 'error: ' + ret.error + ', ' +
- 'name: "' + ret.name + '", ' +
- 'path: "' + ret.path + '", ' +
- 'object: ' + ret.object + ', ' +
- 'parentExists: ' + ret.parentExists + ', ' +
- 'parentPath: "' + ret.parentPath + '", ' +
- 'parentObject: ' + ret.parentObject + '}');
+ Module['print']('FS.analyzePath("' + inputPath + '", ' +
+ dontResolveLastLink + ', ' +
+ linksVisited + ') => {' +
+ 'isRoot: ' + ret.isRoot + ', ' +
+ 'exists: ' + ret.exists + ', ' +
+ 'error: ' + ret.error + ', ' +
+ 'name: "' + ret.name + '", ' +
+ 'path: "' + ret.path + '", ' +
+ 'object: ' + ret.object + ', ' +
+ 'parentExists: ' + ret.parentExists + ', ' +
+ 'parentPath: "' + ret.parentPath + '", ' +
+ 'parentObject: ' + ret.parentObject + '}');
}
#endif
path = FS.absolutePath(path);
@@ -177,11 +184,11 @@ LibraryManager.library = {
// Creates a file system record: file, link, device or folder.
createObject: function(parent, name, properties, canRead, canWrite) {
#if FS_LOG
- print('FS.createObject("' + parent + '", ' +
- '"' + name + '", ' +
- JSON.stringify(properties) + ', ' +
- canRead + ', ' +
- canWrite + ')');
+ Module['print']('FS.createObject("' + parent + '", ' +
+ '"' + name + '", ' +
+ JSON.stringify(properties) + ', ' +
+ canRead + ', ' +
+ canWrite + ')');
#endif
if (!parent) parent = '/';
if (typeof parent === 'string') parent = FS.findObject(parent);
@@ -257,11 +264,20 @@ LibraryManager.library = {
var properties = {isDevice: false, contents: data};
return FS.createFile(parent, name, properties, canRead, canWrite);
},
- // Creates a file record for lazy-loading from a URL.
+ // Creates a file record for lazy-loading from a URL. XXX This requires a synchronous
+ // XHR, which is not possible in browsers except in a web worker! Use preloading,
+ // either --preload-file in emcc or FS.createPreloadedFile
createLazyFile: function(parent, name, url, canRead, canWrite) {
var properties = {isDevice: false, url: url};
return FS.createFile(parent, name, properties, canRead, canWrite);
},
+ // Preloads a file asynchronously. You can call this before run, for example in
+ // preRun. run will be delayed until this file arrives and is set up.
+ createPreloadedFile: function(parent, name, url, canRead, canWrite) {
+ Browser.asyncLoad(url, function(data) {
+ FS.createDataFile(parent, name, data, canRead, canWrite);
+ });
+ },
// Creates a link to a sepcific local path.
createLink: function(parent, name, target, canRead, canWrite) {
var properties = {isDevice: false, link: target};
@@ -340,6 +356,7 @@ LibraryManager.library = {
typeof window.prompt == 'function') {
// Browser.
result = window.prompt('Input: ');
+ if (result === null) result = String.fromCharCode(0); // cancel ==> EOF
} else if (typeof readline == 'function') {
// Command line.
result = readline();
@@ -371,8 +388,10 @@ LibraryManager.library = {
if (!error.printer) error.printer = Module['print'];
if (!error.buffer) error.buffer = [];
- // Create the temporary folder.
- FS.createFolder('/', 'tmp', true, true);
+ // Create the temporary folder, if not already created
+ try {
+ FS.createFolder('/', 'tmp', true, true);
+ } catch(e) {}
// Create the I/O devices.
var devFolder = FS.createFolder('/', 'dev', true, true);
@@ -1476,7 +1495,7 @@ LibraryManager.library = {
lseek: function(fildes, offset, whence) {
// off_t lseek(int fildes, off_t offset, int whence);
// http://pubs.opengroup.org/onlinepubs/000095399/functions/lseek.html
- if (FS.streams[fildes] && !FS.streams[fildes].isDevice) {
+ if (FS.streams[fildes] && !FS.streams[fildes].object.isDevice) {
var stream = FS.streams[fildes];
var position = offset;
if (whence === 1) { // SEEK_CUR.
@@ -1867,7 +1886,7 @@ LibraryManager.library = {
#if CATCH_EXIT_CODE
throw new ExitStatus();
-#else
+#else
throw 'exit(' + status + ') called, at ' + new Error().stack;
#endif
},
@@ -2217,6 +2236,9 @@ LibraryManager.library = {
if (!self.called) {
STATICTOP = alignMemoryPage(STATICTOP); // make sure we start out aligned
self.called = true;
+#if GC_SUPPORT
+ _sbrk.DYNAMIC_START = STATICTOP;
+#endif
}
var ret = STATICTOP;
if (bytes != 0) Runtime.staticAlloc(bytes);
@@ -2242,6 +2264,15 @@ LibraryManager.library = {
// TODO: Document.
_scanString__deps: ['_isFloat'],
_scanString: function(format, get, unget, varargs) {
+ if (!__scanString.whiteSpace) {
+ __scanString.whiteSpace = {};
+ __scanString.whiteSpace[' '.charCodeAt(0)] = 1;
+ __scanString.whiteSpace['\t'.charCodeAt(0)] = 1;
+ __scanString.whiteSpace['\n'.charCodeAt(0)] = 1;
+ __scanString.whiteSpace[' '] = 1;
+ __scanString.whiteSpace['\t'] = 1;
+ __scanString.whiteSpace['\n'] = 1;
+ }
// Supports %x, %4x, %d.%d, %s, %f, %lf.
// TODO: Support all format specifiers.
format = Pointer_stringify(format);
@@ -2249,6 +2280,7 @@ LibraryManager.library = {
var argsi = 0;
var fields = 0;
var argIndex = 0;
+ var next;
for (var formatIndex = 0; formatIndex < format.length; formatIndex++) {
if (next <= 0) return fields;
var next = get();
@@ -2264,11 +2296,14 @@ LibraryManager.library = {
if (formatIndex != maxSpecifierStart) {
max_ = parseInt(format.slice(maxSpecifierStart, formatIndex), 10);
}
- // TODO: Handle type size modifier.
var long_ = false;
+ var half = false;
if (format[formatIndex] == 'l') {
long_ = true;
formatIndex++;
+ } else if (format[formatIndex] == 'h') {
+ half = true;
+ formatIndex++;
}
var type = format[formatIndex];
formatIndex++;
@@ -2288,13 +2323,18 @@ LibraryManager.library = {
buffer.pop();
unget();
}
+ unget();
+ next = get();
} else {
+ var first = true;
while ((curr < max_ || isNaN(max_)) && next > 0) {
- if ((type === 'd' && next >= '0'.charCodeAt(0) && next <= '9'.charCodeAt(0)) ||
- (type === 'x' && (next >= '0'.charCodeAt(0) && next <= '9'.charCodeAt(0) ||
- next >= 'a'.charCodeAt(0) && next <= 'f'.charCodeAt(0) ||
- next >= 'A'.charCodeAt(0) && next <= 'F'.charCodeAt(0))) ||
- (type === 's') &&
+ if (!(next in __scanString.whiteSpace) && // stop on whitespace
+ (type == 's' ||
+ ((type === 'd' || type == 'u') && ((next >= '0'.charCodeAt(0) && next <= '9'.charCodeAt(0)) ||
+ (first && next == '-'.charCodeAt(0)))) ||
+ (type === 'x' && (next >= '0'.charCodeAt(0) && next <= '9'.charCodeAt(0) |