diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/headlessCanvas.js | 1 | ||||
-rw-r--r-- | src/jsifier.js | 46 | ||||
-rw-r--r-- | src/library.js | 3 | ||||
-rw-r--r-- | src/library_fs.js | 181 | ||||
-rw-r--r-- | src/library_html5.js | 19 | ||||
-rw-r--r-- | src/library_idbfs.js | 324 | ||||
-rw-r--r-- | src/library_sdl.js | 27 | ||||
-rw-r--r-- | src/settings.js | 14 | ||||
-rw-r--r-- | src/struct_info.json | 4 |
9 files changed, 392 insertions, 227 deletions
diff --git a/src/headlessCanvas.js b/src/headlessCanvas.js index 6b0f9d47..4bd17a7b 100644 --- a/src/headlessCanvas.js +++ b/src/headlessCanvas.js @@ -446,6 +446,7 @@ function headlessCanvas() { case /* GL_MAX_FRAGMENT_UNIFORM_VECTORS */ 0x8DFD: return 4096; case /* GL_MAX_VARYING_VECTORS */ 0x8DFC: return 32; case /* GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS */ 0x8B4D: return 32; + case /* GL_ARRAY_BUFFER_BINDING */ 0x8894: return 0; default: console.log('getParameter ' + pname + '?'); return 0; } }, diff --git a/src/jsifier.js b/src/jsifier.js index ab5440f7..ab10f455 100644 --- a/src/jsifier.js +++ b/src/jsifier.js @@ -224,6 +224,7 @@ function JSify(data, functionsOnly) { // globalVariable function globalVariableHandler(item) { + function needsPostSet(value) { if (typeof value !== 'string') return false; // (' is ok, as it is something we can indexize later into a concrete int: ('{{ FI_ ... @@ -274,7 +275,9 @@ function JSify(data, functionsOnly) { constant = Runtime.alignMemory(calcAllocatedSize(item.type)); } else { if (item.external) { - if (Runtime.isNumberType(item.type) || isPointerType(item.type)) { + if (LibraryManager.library[item.ident.slice(1)]) { + constant = LibraryManager.library[item.ident.slice(1)]; + } else if (Runtime.isNumberType(item.type) || isPointerType(item.type)) { constant = zeros(Runtime.getNativeFieldSize(item.type)); } else { constant = makeEmptyStruct(item.type); @@ -282,22 +285,23 @@ function JSify(data, functionsOnly) { } else { constant = parseConst(item.value, item.type, item.ident); } - assert(typeof constant === 'object');//, [typeof constant, JSON.stringify(constant), item.external]); // This is a flattened object. We need to find its idents, so they can be assigned to later - var structTypes = null; - constant.forEach(function(value, i) { - if (needsPostSet(value)) { // ident, or expression containing an ident - if (!structTypes) structTypes = generateStructTypes(item.type); - itemsDict.GlobalVariablePostSet.push({ - intertype: 'GlobalVariablePostSet', - JS: makeSetValue(makeGlobalUse(item.ident), i, value, structTypes[i], false, true) + ';' // ignore=true, since e.g. rtti and statics cause lots of safe_heap errors - }); - constant[i] = '0'; - } else { - if (typeof value === 'string') constant[i] = deParenCarefully(value); - } - }); + if (typeof constant === 'object') { + var structTypes = null; + constant.forEach(function(value, i) { + if (needsPostSet(value)) { // ident, or expression containing an ident + if (!structTypes) structTypes = generateStructTypes(item.type); + itemsDict.GlobalVariablePostSet.push({ + intertype: 'GlobalVariablePostSet', + JS: makeSetValue(makeGlobalUse(item.ident), i, value, structTypes[i], false, true) + ';' // ignore=true, since e.g. rtti and statics cause lots of safe_heap errors + }); + constant[i] = '0'; + } else { + if (typeof value === 'string') constant[i] = deParenCarefully(value); + } + }); + } if (item.external) { // External variables in shared libraries should not be declared as @@ -312,14 +316,18 @@ function JSify(data, functionsOnly) { } // ensure alignment - var extra = Runtime.alignMemory(constant.length) - constant.length; - if (item.ident.substr(0, 5) == '__ZTV') extra += Runtime.alignMemory(QUANTUM_SIZE); - while (extra-- > 0) constant.push(0); + if (typeof constant === 'object') { + var extra = Runtime.alignMemory(constant.length) - constant.length; + if (item.ident.substr(0, 5) == '__ZTV') extra += Runtime.alignMemory(QUANTUM_SIZE); + while (extra-- > 0) constant.push(0); + } } // NOTE: This is the only place that could potentially create static // allocations in a shared library. - constant = makePointer(constant, null, allocator, item.type, index); + if (typeof constant === 'object') { + constant = makePointer(constant, null, allocator, item.type, index); + } var js = (index !== null ? '' : item.ident + '=') + constant; if (js) js += ';'; diff --git a/src/library.js b/src/library.js index 30453137..26ce8457 100644 --- a/src/library.js +++ b/src/library.js @@ -1641,6 +1641,7 @@ LibraryManager.library = { for (var i = 0; i < maxx; i++) { next = get(); {{{ makeSetValue('argPtr++', 0, 'next', 'i8') }}}; + if (next === 0) return i > 0 ? fields : fields-1; // we failed to read the full length of this field } formatIndex += nextC - formatIndex + 1; continue; @@ -3457,7 +3458,7 @@ LibraryManager.library = { rand_r: function(seedp) { seedp = seedp|0; var val = 0; - val = (Math_imul({{{ makeGetValueAsm('seedp', 0, 'i32') }}}, 31010991)|0) + 0x676e6177 | 0; + val = ((Math_imul({{{ makeGetValueAsm('seedp', 0, 'i32') }}}, 31010991)|0) + 0x676e6177 ) & {{{ cDefine('RAND_MAX') }}}; // assumes RAND_MAX is in bit mask form (power of 2 minus 1) {{{ makeSetValueAsm('seedp', 0, 'val', 'i32') }}}; return val|0; }, diff --git a/src/library_fs.js b/src/library_fs.js index e6b060f6..c30876f5 100644 --- a/src/library_fs.js +++ b/src/library_fs.js @@ -40,7 +40,17 @@ mergeInto(LibraryManager.library, { // lookupPath: function(path, opts) { path = PATH.resolve(FS.cwd(), path); - opts = opts || { recurse_count: 0 }; + opts = opts || {}; + + var defaults = { + follow_mount: true, + recurse_count: 0 + }; + for (var key in defaults) { + if (opts[key] === undefined) { + opts[key] = defaults[key]; + } + } if (opts.recurse_count > 8) { // max recursive lookup of 8 throw new FS.ErrnoError(ERRNO_CODES.ELOOP); @@ -67,10 +77,11 @@ mergeInto(LibraryManager.library, { // jump to the mount's root node if this is a mountpoint if (FS.isMountpoint(current)) { - current = current.mount.root; + if (!islast || (islast && opts.follow_mount)) { + current = current.mounted.root; + } } - // follow symlinks // by default, lookupPath will not follow a symlink if it is the final path component. // setting opts.follow = true will override this behavior. if (!islast || opts.follow) { @@ -163,28 +174,26 @@ mergeInto(LibraryManager.library, { createNode: function(parent, name, mode, rdev) { if (!FS.FSNode) { FS.FSNode = function(parent, name, mode, rdev) { + if (!parent) { + parent = this; // root node sets parent to itself + } + this.parent = parent; + this.mount = parent.mount; + this.mounted = null; this.id = FS.nextInode++; this.name = name; this.mode = mode; this.node_ops = {}; this.stream_ops = {}; this.rdev = rdev; - this.parent = null; - this.mount = null; - if (!parent) { - parent = this; // root node sets parent to itself - } - this.parent = parent; - this.mount = parent.mount; - FS.hashAddNode(this); }; + FS.FSNode.prototype = {}; + // compatibility var readMode = {{{ cDefine('S_IRUGO') }}} | {{{ cDefine('S_IXUGO') }}}; var writeMode = {{{ cDefine('S_IWUGO') }}}; - FS.FSNode.prototype = {}; - // NOTE we must use Object.defineProperties instead of individual calls to // Object.defineProperty in order to make closure compiler happy Object.defineProperties(FS.FSNode.prototype, { @@ -204,7 +213,12 @@ mergeInto(LibraryManager.library, { }, }); } - return new FS.FSNode(parent, name, mode, rdev); + + var node = new FS.FSNode(parent, name, mode, rdev); + + FS.hashAddNode(node); + + return node; }, destroyNode: function(node) { FS.hashRemoveNode(node); @@ -213,7 +227,7 @@ mergeInto(LibraryManager.library, { return node === node.parent; }, isMountpoint: function(node) { - return node.mounted; + return !!node.mounted; }, isFile: function(mode) { return (mode & {{{ cDefine('S_IFMT') }}}) === {{{ cDefine('S_IFREG') }}}; @@ -441,61 +455,131 @@ mergeInto(LibraryManager.library, { // // core // + getMounts: function(mount) { + var mounts = []; + var check = [mount]; + + while (check.length) { + var m = check.pop(); + + mounts.push(m); + + check.push.apply(check, m.mounts); + } + + return mounts; + }, syncfs: function(populate, callback) { if (typeof(populate) === 'function') { callback = populate; populate = false; } + var mounts = FS.getMounts(FS.root.mount); var completed = 0; - var total = FS.mounts.length; + function done(err) { if (err) { - return callback(err); + if (!done.errored) { + done.errored = true; + return callback(err); + } + return; } - if (++completed >= total) { + if (++completed >= mounts.length) { callback(null); } }; // sync all mounts - for (var i = 0; i < FS.mounts.length; i++) { - var mount = FS.mounts[i]; + mounts.forEach(function (mount) { if (!mount.type.syncfs) { - done(null); - continue; + return done(null); } mount.type.syncfs(mount, populate, done); - } + }); }, mount: function(type, opts, mountpoint) { - var lookup; - if (mountpoint) { - lookup = FS.lookupPath(mountpoint, { follow: false }); + var root = mountpoint === '/'; + var pseudo = !mountpoint; + var node; + + if (root && FS.root) { + throw new FS.ErrnoError(ERRNO_CODES.EBUSY); + } else if (!root && !pseudo) { + var lookup = FS.lookupPath(mountpoint, { follow_mount: false }); + mountpoint = lookup.path; // use the absolute path + node = lookup.node; + + if (FS.isMountpoint(node)) { + throw new FS.ErrnoError(ERRNO_CODES.EBUSY); + } + + if (!FS.isDir(node.mode)) { + throw new FS.ErrnoError(ERRNO_CODES.ENOTDIR); + } } + var mount = { type: type, opts: opts, mountpoint: mountpoint, - root: null + mounts: [] }; + // create a root node for the fs - var root = type.mount(mount); - root.mount = mount; - mount.root = root; - // assign the mount info to the mountpoint's node - if (lookup) { - lookup.node.mount = mount; - lookup.node.mounted = true; - // compatibility update FS.root if we mount to / - if (mountpoint === '/') { - FS.root = mount.root; + var mountRoot = type.mount(mount); + mountRoot.mount = mount; + mount.root = mountRoot; + + if (root) { + FS.root = mountRoot; + } else if (node) { + // set as a mountpoint + node.mounted = mount; + + // add the new mount to the current mount's children + if (node.mount) { + node.mount.mounts.push(mount); } } - // add to our cached list of mounts - FS.mounts.push(mount); - return root; + + return mountRoot; + }, + unmount: function (mountpoint) { + var lookup = FS.lookupPath(mountpoint, { follow_mount: false }); + + if (!FS.isMountpoint(lookup.node)) { + throw new FS.ErrnoError(ERRNO_CODES.EINVAL); + } + + // destroy the nodes for this mount, and all its child mounts + var node = lookup.node; + var mount = node.mounted; + var mounts = FS.getMounts(mount); + + Object.keys(FS.nameTable).forEach(function (hash) { + var current = FS.nameTable[hash]; + + while (current) { + var next = current.name_next; + + if (mounts.indexOf(current.mount) !== -1) { + FS.destroyNode(current); + } + + current = next; + } + }); + + // no longer a mountpoint + node.mounted = null; + + // remove this mount from the child mounts + var idx = node.mount.mounts.indexOf(mount); + assert(idx !== -1); + node.mount.mounts.splice(idx, 1); }, lookup: function(parent, name) { return parent.node_ops.lookup(parent, name); @@ -677,7 +761,7 @@ mergeInto(LibraryManager.library, { FS.destroyNode(node); }, readlink: function(path) { - var lookup = FS.lookupPath(path, { follow: false }); + var lookup = FS.lookupPath(path); var link = lookup.node; if (!link.node_ops.readlink) { throw new FS.ErrnoError(ERRNO_CODES.EINVAL); @@ -975,6 +1059,9 @@ mergeInto(LibraryManager.library, { opts = opts || {}; opts.flags = opts.flags || 'r'; opts.encoding = opts.encoding || 'binary'; + if (opts.encoding !== 'utf8' && opts.encoding !== 'binary') { + throw new Error('Invalid encoding type "' + opts.encoding + '"'); + } var ret; var stream = FS.open(path, opts.flags); var stat = FS.stat(path); @@ -989,8 +1076,6 @@ mergeInto(LibraryManager.library, { } } else if (opts.encoding === 'binary') { ret = buf; - } else { - throw new Error('Invalid encoding type "' + opts.encoding + '"'); } FS.close(stream); return ret; @@ -999,15 +1084,16 @@ mergeInto(LibraryManager.library, { opts = opts || {}; opts.flags = opts.flags || 'w'; opts.encoding = opts.encoding || 'utf8'; + if (opts.encoding !== 'utf8' && opts.encoding !== 'binary') { + throw new Error('Invalid encoding type "' + opts.encoding + '"'); + } var stream = FS.open(path, opts.flags, opts.mode); if (opts.encoding === 'utf8') { var utf8 = new Runtime.UTF8Processor(); var buf = new Uint8Array(utf8.processJSString(data)); - FS.write(stream, buf, 0, buf.length, 0); + FS.write(stream, buf, 0, buf.length, 0, opts.canOwn); } else if (opts.encoding === 'binary') { - FS.write(stream, data, 0, data.length, 0); - } else { - throw new Error('Invalid encoding type "' + opts.encoding + '"'); + FS.write(stream, data, 0, data.length, 0, opts.canOwn); } FS.close(stream); }, @@ -1119,7 +1205,6 @@ mergeInto(LibraryManager.library, { FS.nameTable = new Array(4096); - FS.root = FS.createNode(null, '/', {{{ cDefine('S_IFDIR') }}} | 0777, 0); FS.mount(MEMFS, {}, '/'); FS.createDefaultDirectories(); diff --git a/src/library_html5.js b/src/library_html5.js index 703f9a74..a38c2390 100644 --- a/src/library_html5.js +++ b/src/library_html5.js @@ -709,7 +709,9 @@ var LibraryJSEvents = { var eventHandler = { target: JSEvents.findEventTarget(target), - allowsDeferredCalls: true, + allowsDeferredCalls: false, // XXX Currently disabled, see bug https://bugzilla.mozilla.org/show_bug.cgi?id=966493 + // Once the above bug is resolved, enable the following condition if possible: + // allowsDeferredCalls: eventTypeString == 'touchstart', eventTypeString: eventTypeString, callbackfunc: callbackfunc, handlerFunc: handlerFunc, @@ -980,18 +982,23 @@ var LibraryJSEvents = { if (allowedOrientations & 2) orientations.push("portrait-secondary"); if (allowedOrientations & 4) orientations.push("landscape-primary"); if (allowedOrientations & 8) orientations.push("landscape-secondary"); + var succeeded; if (window.screen.lockOrientation) { - window.screen.lockOrientation(orientations); + succeeded = window.screen.lockOrientation(orientations); } else if (window.screen.mozLockOrientation) { - window.screen.mozLockOrientation(orientations); + succeeded = window.screen.mozLockOrientation(orientations); } else if (window.screen.webkitLockOrientation) { - window.screen.webkitLockOrientation(orientations); + succeeded = window.screen.webkitLockOrientation(orientations); } else if (window.screen.msLockOrientation) { - window.screen.msLockOrientation(orientations); + succeeded = window.screen.msLockOrientation(orientations); } else { return {{{ cDefine('EMSCRIPTEN_RESULT_NOT_SUPPORTED') }}}; } - return {{{ cDefine('EMSCRIPTEN_RESULT_SUCCESS') }}}; + if (succeeded) { + return {{{ cDefine('EMSCRIPTEN_RESULT_SUCCESS') }}}; + } else { + return {{{ cDefine('EMSCRIPTEN_RESULT_FAILED') }}}; + } }, emscripten_unlock_orientation: function() { diff --git a/src/library_idbfs.js b/src/library_idbfs.js index 7f50f17e..91015e77 100644 --- a/src/library_idbfs.js +++ b/src/library_idbfs.js @@ -5,14 +5,12 @@ mergeInto(LibraryManager.library, { indexedDB: function() { return window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB; }, - DB_VERSION: 20, + DB_VERSION: 21, DB_STORE_NAME: 'FILE_DATA', - // reuse all of the core MEMFS functionality mount: function(mount) { + // reuse all of the core MEMFS functionality return MEMFS.mount.apply(null, arguments); }, - // the only custom function IDBFS implements is to handle - // synchronizing the wrapped MEMFS with a backing IDB instance syncfs: function(mount, populate, callback) { IDBFS.getLocalSet(mount, function(err, local) { if (err) return callback(err); @@ -27,103 +25,46 @@ mergeInto(LibraryManager.library, { }); }); }, - reconcile: function(src, dst, callback) { - var total = 0; - - var create = {}; - for (var key in src.files) { - if (!src.files.hasOwnProperty(key)) continue; - var e = src.files[key]; - var e2 = dst.files[key]; - if (!e2 || e.timestamp > e2.timestamp) { - create[key] = e; - total++; - } - } - - var remove = {}; - for (var key in dst.files) { - if (!dst.files.hasOwnProperty(key)) continue; - var e = dst.files[key]; - var e2 = src.files[key]; - if (!e2) { - remove[key] = e; - total++; - } + getDB: function(name, callback) { + // check the cache first + var db = IDBFS.dbs[name]; + if (db) { + return callback(null, db); } - if (!total) { - // early out - return callback(null); + var req; + try { + req = IDBFS.indexedDB().open(name, IDBFS.DB_VERSION); + } catch (e) { + return callback(e); } + req.onupgradeneeded = function(e) { + var db = e.target.result; + var transaction = e.target.transaction; - var completed = 0; - function done(err) { - if (err) return callback(err); - if (++completed >= total) { - return callback(null); - } - }; - - // create a single transaction to handle and IDB reads / writes we'll need to do - var db = src.type === 'remote' ? src.db : dst.db; - var transaction = db.transaction([IDBFS.DB_STORE_NAME], 'readwrite'); - transaction.onerror = function transaction_onerror() { callback(this.error); }; - var store = transaction.objectStore(IDBFS.DB_STORE_NAME); - - for (var path in create) { - if (!create.hasOwnProperty(path)) continue; - var entry = create[path]; + var fileStore; - if (dst.type === 'local') { - // save file to local - try { - if (FS.isDir(entry.mode)) { - FS.mkdir(path, entry.mode); - } else if (FS.isFile(entry.mode)) { - var stream = FS.open(path, 'w+', 0666); - FS.write(stream, entry.contents, 0, entry.contents.length, 0, true /* canOwn */); - FS.close(stream); - } - done(null); - } catch (e) { - return done(e); - } + if (db.objectStoreNames.contains(IDBFS.DB_STORE_NAME)) { + fileStore = transaction.objectStore(IDBFS.DB_STORE_NAME); } else { - // save file to IDB - var req = store.put(entry, path); - req.onsuccess = function req_onsuccess() { done(null); }; - req.onerror = function req_onerror() { done(this.error); }; + fileStore = db.createObjectStore(IDBFS.DB_STORE_NAME); } - } - for (var path in remove) { - if (!remove.hasOwnProperty(path)) continue; - var entry = remove[path]; + fileStore.createIndex('timestamp', 'timestamp', { unique: false }); + }; + req.onsuccess = function() { + db = req.result; - if (dst.type === 'local') { - // delete file from local - try { - if (FS.isDir(entry.mode)) { - // TODO recursive delete? - FS.rmdir(path); - } else if (FS.isFile(entry.mode)) { - FS.unlink(path); - } - done(null); - } catch (e) { - return done(e); - } - } else { - // delete file from IDB - var req = store.delete(path); - req.onsuccess = function req_onsuccess() { done(null); }; - req.onerror = function req_onerror() { done(this.error); }; - } - } + // add to the cache + IDBFS.dbs[name] = db; + callback(null, db); + }; + req.onerror = function() { + callback(this.error); + }; }, getLocalSet: function(mount, callback) { - var files = {}; + var entries = {}; function isRealDir(p) { return p !== '.' && p !== '..'; @@ -134,83 +75,192 @@ mergeInto(LibraryManager.library, { } }; - var check = FS.readdir(mount.mountpoint) - .filter(isRealDir) - .map(toAbsolute(mount.mountpoint)); + var check = FS.readdir(mount.mountpoint).filter(isRealDir).map(toAbsolute(mount.mountpoint)); while (check.length) { var path = check.pop(); - var stat, node; + var stat; try { - var lookup = FS.lookupPath(path); - node = lookup.node; stat = FS.stat(path); } catch (e) { return callback(e); } if (FS.isDir(stat.mode)) { - check.push.apply(check, FS.readdir(path) - .filter(isRealDir) - .map(toAbsolute(path))); - - files[path] = { mode: stat.mode, timestamp: stat.mtime }; - } else if (FS.isFile(stat.mode)) { - files[path] = { contents: node.contents, mode: stat.mode, timestamp: stat.mtime }; - } else { - return callback(new Error('node type not supported')); + check.push.apply(check, FS.readdir(path).filter(isRealDir).map(toAbsolute(path))); } - } - return callback(null, { type: 'local', files: files }); - }, - getDB: function(name, callback) { - // look it up in the cache - var db = IDBFS.dbs[name]; - if (db) { - return callback(null, db); - } - var req; - try { - req = IDBFS.indexedDB().open(name, IDBFS.DB_VERSION); - } catch (e) { - return onerror(e); + entries[path] = { timestamp: stat.mtime }; } - req.onupgradeneeded = function req_onupgradeneeded() { - db = req.result; - db.createObjectStore(IDBFS.DB_STORE_NAME); - }; - req.onsuccess = function req_onsuccess() { - db = req.result; - // add to the cache - IDBFS.dbs[name] = db; - callback(null, db); - }; - req.onerror = function req_onerror() { - callback(this.error); - }; + + return callback(null, { type: 'local', entries: entries }); }, getRemoteSet: function(mount, callback) { - var files = {}; + var entries = {}; IDBFS.getDB(mount.mountpoint, function(err, db) { if (err) return callback(err); var transaction = db.transaction([IDBFS.DB_STORE_NAME], 'readonly'); - transaction.onerror = function transaction_onerror() { callback(this.error); }; + transaction.onerror = function() { callback(this.error); }; var store = transaction.objectStore(IDBFS.DB_STORE_NAME); - store.openCursor().onsuccess = function store_openCursor_onsuccess(event) { + var index = store.index('timestamp'); + + index.openKeyCursor().onsuccess = function(event) { var cursor = event.target.result; + if (!cursor) { - return callback(null, { type: 'remote', db: db, files: files }); + return callback(null, { type: 'remote', db: db, entries: entries }); } - files[cursor.key] = cursor.value; + entries[cursor.primaryKey] = { timestamp: cursor.key }; + cursor.continue(); }; }); + }, + loadLocalEntry: function(path, callback) { + var stat, node; + + try { + var lookup = FS.lookupPath(path); + node = lookup.node; + stat = FS.stat(path); + } catch (e) { + return callback(e); + } + + if (FS.isDir(stat.mode)) { + return callback(null, { timestamp: stat.mtime, mode: stat.mode }); + } else if (FS.isFile(stat.mode)) { + return callback(null, { timestamp: stat.mtime, mode: stat.mode, contents: node.contents }); + } else { + return callback(new Error('node type not supported')); + } + }, + storeLocalEntry: function(path, entry, callback) { + try { + if (FS.isDir(entry.mode)) { + FS.mkdir(path, entry.mode); + } else if (FS.isFile(entry.mode)) { + FS.writeFile(path, entry.contents, { encoding: 'binary', canOwn: true }); + } else { + return callback(new Error('node type not supported')); + } + + FS.utime(path, entry.timestamp, entry.timestamp); + } catch (e) { + return callback(e); + } + + callback(null); + }, + removeLocalEntry: function(path, callback) { + try { + var lookup = FS.lookupPath(path); + var stat = FS.stat(path); + + if (FS.isDir(stat.mode)) { + FS.rmdir(path); + } else if (FS.isFile(stat.mode)) { + FS.unlink(path); + } + } catch (e) { + return callback(e); + } + + callback(null); + }, + loadRemoteEntry: function(store, path, callback) { + var req = store.get(path); + req.onsuccess = function(event) { callback(null, event.target.result); }; + req.onerror = function() { callback(this.error); }; + }, + storeRemoteEntry: function(store, path, entry, callback) { + var req = store.put(entry, path); + req.onsuccess = function() { callback(null); }; + req.onerror = function() { callback(this.error); }; + }, + removeRemoteEntry: function(store, path, callback) { + var req = store.delete(path); + req.onsuccess = function() { callback(null); }; + req.onerror = function() { callback(this.error); }; + }, + reconcile: function(src, dst, callback) { + var total = 0; + + var create = []; + Object.keys(src.entries).forEach(function (key) { + var e = src.entries[key]; + var e2 = dst.entries[key]; + if (!e2 || e.timestamp > e2.timestamp) { + create.push(key); + total++; + } + }); + + var remove = []; + Object.keys(dst.entries).forEach(function (key) { + var e = dst.entries[key]; + var e2 = src.entries[key]; + if (!e2) { + remove.push(key); + total++; + } + }); + + if (!total) { + return callback(null); + } + + var errored = false; + var completed = 0; + var db = src.type === 'remote' ? src.db : dst.db; + var transaction = db.transaction([IDBFS.DB_STORE_NAME], 'readwrite'); + var store = transaction.objectStore(IDBFS.DB_STORE_NAME); + + function done(err) { + if (err) { + if (!done.errored) { + done.errored = true; + return callback(err); + } + return; + } + if (++completed >= total) { + return callback(null); + } + }; + + transaction.onerror = function() { done(this.error); }; + + // sort paths in ascending order so directory entries are created + // before the files inside them + create.sort().forEach(function (path) { + if (dst.type === 'local') { + IDBFS.loadRemoteEntry(store, path, function (err, entry) { + if (err) return done(err); + IDBFS.storeLocalEntry(path, entry, done); + }); + } else { + IDBFS.loadLocalEntry(path, function (err, entry) { + if (err) return done(err); + IDBFS.storeRemoteEntry(store, path, entry, done); + }); + } + }); + + // sort paths in descending order so files are deleted before their + // parent directories + remove.sort().reverse().forEach(function(path) { + if (dst.type === 'local') { + IDBFS.removeLocalEntry(path, done); + } else { + IDBFS.removeRemoteEntry(store, path, done); + } + }); } } }); diff --git a/src/library_sdl.js b/src/library_sdl.js index 80734d95..04e8fc18 100644 --- a/src/library_sdl.js +++ b/src/library_sdl.js @@ -215,7 +215,6 @@ var LibrarySDL = { makeSurface: function(width, height, flags, usePageCanvas, source, rmask, gmask, bmask, amask) { flags = flags || 0; var surf = _malloc({{{ C_STRUCTS.SDL_Surface.__size__ }}}); // SDL_Surface has 15 fields of quantum size - var buffer = _malloc(width*height*4); // TODO: only allocate when locked the first time var pixelFormat = _malloc({{{ C_STRUCTS.SDL_PixelFormat.__size__ }}}); flags |= 1; // SDL_HWSURFACE - this tells SDL_MUSTLOCK that this needs to be locked @@ -229,7 +228,7 @@ var LibrarySDL = { {{{ makeSetValue('surf', C_STRUCTS.SDL_Surface.h, 'height', 'i32') }}}; // SDL_Surface.h {{{ makeSetValue('surf', C_STRUCTS.SDL_Surface.pitch, 'width * bpp', 'i32') }}}; // SDL_Surface.pitch, assuming RGBA or indexed for now, // since that is what ImageData gives us in browsers - {{{ makeSetValue('surf', C_STRUCTS.SDL_Surface.pixels, 'buffer', 'void*') }}}; // SDL_Surface.pixels + {{{ makeSetValue('surf', C_STRUCTS.SDL_Surface.pixels, '0', 'void*') }}}; // SDL_Surface.pixels, lazily initialized inside of SDL_LockSurface {{{ makeSetValue('surf', C_STRUCTS.SDL_Surface.clip_rect, '0', 'i32*') }}}; // SDL_Surface.offset {{{ makeSetValue('surf', C_STRUCTS.SDL_Surface.refcount, '1', 'i32') }}}; @@ -274,7 +273,7 @@ var LibrarySDL = { canvas: canvas, ctx: ctx, surf: surf, - buffer: buffer, + buffer: 0, pixelFormat: pixelFormat, alpha: 255, flags: flags, @@ -337,7 +336,7 @@ var LibrarySDL = { var info = SDL.surfaces[surf]; if (!info.usePageCanvas && info.canvas) SDL.canvasPool.push(info.canvas); - _free(info.buffer); + if (info.buffer) _free(info.buffer); _free(info.pixelFormat); _free(surf); SDL.surfaces[surf] = null; @@ -986,6 +985,11 @@ var LibrarySDL = { surfData.locked++; if (surfData.locked > 1) return 0; + if (!surfData.buffer) { + surfData.buffer = _malloc(surfData.width * surfData.height * 4); + {{{ makeSetValue('surf', C_STRUCTS.SDL_Surface.pixels, 'surfData.buffer', 'void*') }}}; + } + // Mark in C/C++-accessible SDL structure // SDL_Surface has the following fields: Uint32 flags, SDL_PixelFormat *format; int w, h; Uint16 pitch; void *pixels; ... // So we have fields all of the same size, and 5 of them before us. @@ -1723,6 +1727,7 @@ var LibrarySDL = { SDL.audio.pushAudio=function(ptr,sizeBytes) { try { --SDL.audio.numAudioTimersPending; + if (SDL.audio.paused) return; var sizeSamples = sizeBytes / SDL.audio.bytesPerSample; // How many samples fit in the callback buffer? var sizeSamplesPerChannel = sizeSamples / SDL.audio.channels; // How many samples per a single channel fit in the cb buffer? @@ -1773,18 +1778,18 @@ var LibrarySDL = { SDL.audio.soundSource[SDL.audio.nextSoundSource]['start'](playtime); var buffer_duration = sizeSamplesPerChannel / SDL.audio.freq; SDL.audio.nextPlayTime = playtime + buffer_duration; - SDL.audio.nextSoundSource = (SDL.audio.nextSoundSource + 1) % 4; + // Timer will be scheduled before the buffer completed playing. + // Extra buffers are needed to avoid disturbing playing buffer. + SDL.audio.nextSoundSource = (SDL.audio.nextSoundSource + 1) % (SDL.audio.numSimultaneouslyQueuedBuffers + 2); var secsUntilNextCall = playtime-curtime; // Queue the next audio frame push to be performed when the previously queued buffer has finished playing. - if (SDL.audio.numAudioTimersPending == 0) { - var preemptBufferFeedMSecs = buffer_duration/2.0; - SDL.audio.timer = Browser.safeSetTimeout(SDL.audio.caller, Math.max(0.0, 1000.0*secsUntilNextCall-preemptBufferFeedMSecs)); - ++SDL.audio.numAudioTimersPending; - } + var preemptBufferFeedMSecs = 1000*buffer_duration/2.0; + SDL.audio.timer = Browser.safeSetTimeout(SDL.audio.caller, Math.max(0.0, 1000.0*secsUntilNextCall-preemptBufferFeedMSecs)); + ++SDL.audio.numAudioTimersPending; // If we are risking starving, immediately queue extra buffers. - if (secsUntilNextCall <= buffer_duration && SDL.audio.numAudioTimersPending < SDL.audio.numSimultaneouslyQueuedBuffers) { + if (SDL.audio.numAudioTimersPending < SDL.audio.numSimultaneouslyQueuedBuffers) { |