diff --git a/README.md b/README.md
index 423c1545..743ad4e1 100644
--- a/README.md
+++ b/README.md
@@ -5,17 +5,15 @@
[](https://travis-ci.org/AssemblyScript/assemblyscript)
[](https://build.snapcraft.io/user/AssemblyScript/assemblyscript)
-**AssemblyScript** compiles strictly typed [TypeScript](http://www.typescriptlang.org) to [WebAssembly](http://webassembly.org) using [Binaryen](https://github.com/WebAssembly/binaryen). It generates lean and mean WebAssembly modules while being just an `npm install` away.
+**AssemblyScript** compiles strictly typed [TypeScript](http://www.typescriptlang.org) (basically JavaScript with types) to [WebAssembly](http://webassembly.org) using [Binaryen](https://github.com/WebAssembly/binaryen). It generates lean and mean WebAssembly modules while being just an `npm install` away.
-See [the AssemblyScript wiki](https://github.com/AssemblyScript/assemblyscript/wiki) for further instructions and documentation. You can also try it out in [WebAssembly Studio](https://webassembly.studio)!
+See [the AssemblyScript wiki](https://github.com/AssemblyScript/assemblyscript/wiki) for instructions and documentation. You can also try it out in [WebAssembly Studio](https://webassembly.studio)!
Examples
--------
-A few early examples to get an idea:
-
* **[Conway's Game of Life](./examples/game-of-life)**
- Continuously updates the cellular automaton and visualizes its state on a canvas.
+ Continuously updates the cellular automaton and visualizes its state on a canvas. There's also [this WebAssembly Studio fiddle](https://webassembly.studio/?f=gvuw4enb3qk) of it.
* **[i64 polyfill](./examples/i64-polyfill)**
Exposes WebAssembly's i64 operations to JavaScript using 32-bit integers (low and high bits).
diff --git a/dist/asc.js b/dist/asc.js
index 0d444ab9..ab8cac58 100644
--- a/dist/asc.js
+++ b/dist/asc.js
@@ -1,2 +1,2 @@
-!function(e,n){"object"==typeof exports&&"object"==typeof module?module.exports=n(function(){try{return require("assemblyscript")}catch(e){}}()):"function"==typeof define&&define.amd?define(["assemblyscript"],n):"object"==typeof exports?exports.asc=n(function(){try{return require("assemblyscript")}catch(e){}}()):e.asc=n(e.assemblyscript)}("undefined"!=typeof self?self:this,function(__WEBPACK_EXTERNAL_MODULE__6__){return function(e){var n={};function t(r){if(n[r])return n[r].exports;var i=n[r]={i:r,l:!1,exports:{}};return e[r].call(i.exports,i,i.exports,t),i.l=!0,i.exports}return t.m=e,t.c=n,t.d=function(e,n,r){t.o(e,n)||Object.defineProperty(e,n,{configurable:!1,enumerable:!0,get:r})},t.r=function(e){Object.defineProperty(e,"__esModule",{value:!0})},t.n=function(e){var n=e&&e.__esModule?function(){return e.default}:function(){return e};return t.d(n,"a",n),n},t.o=function(e,n){return Object.prototype.hasOwnProperty.call(e,n)},t.p="",t(t.s=10)}([function(e,n,t){var r,i;n.nextTick=function(e){setTimeout(e,0)},n.platform=n.arch=n.execPath=n.title="browser",n.pid=1,n.browser=!0,n.env={},n.argv=[],n.binding=function(e){throw new Error("No such module. (Possibly not yet loaded)")},i="/",n.cwd=function(){return i},n.chdir=function(e){r||(r=t(2)),i=r.resolve(e,i)},n.exit=n.kill=n.umask=n.dlopen=n.uptime=n.memoryUsage=n.uvCounters=function(){},n.features={}},function(e,n){var t;t=function(){return this}();try{t=t||Function("return this")()||(0,eval)("this")}catch(e){"object"==typeof window&&(t=window)}e.exports=t},function(e,n,t){(function(e){function t(e,n){for(var t=0,r=e.length-1;r>=0;r--){var i=e[r];"."===i?e.splice(r,1):".."===i?(e.splice(r,1),t++):t&&(e.splice(r,1),t--)}if(n)for(;t--;t)e.unshift("..");return e}var r=/^(\/?|)([\s\S]*?)((?:\.{1,2}|[^\/]+?|)(\.[^.\/]*|))(?:[\/]*)$/,i=function(e){return r.exec(e).slice(1)};function s(e,n){if(e.filter)return e.filter(n);for(var t=[],r=0;r=-1&&!r;i--){var a=i>=0?arguments[i]:e.cwd();if("string"!=typeof a)throw new TypeError("Arguments to path.resolve must be strings");a&&(n=a+"/"+n,r="/"===a.charAt(0))}return n=t(s(n.split("/"),function(e){return!!e}),!r).join("/"),(r?"/":"")+n||"."},n.normalize=function(e){var r=n.isAbsolute(e),i="/"===a(e,-1);return(e=t(s(e.split("/"),function(e){return!!e}),!r).join("/"))||r||(e="."),e&&i&&(e+="/"),(r?"/":"")+e},n.isAbsolute=function(e){return"/"===e.charAt(0)},n.join=function(){var e=Array.prototype.slice.call(arguments,0);return n.normalize(s(e,function(e,n){if("string"!=typeof e)throw new TypeError("Arguments to path.join must be strings");return e}).join("/"))},n.relative=function(e,t){function r(e){for(var n=0;n=0&&""===e[t];t--);return n>t?[]:e.slice(n,t-n+1)}e=n.resolve(e).substr(1),t=n.resolve(t).substr(1);for(var i=r(e.split("/")),s=r(t.split("/")),a=Math.min(i.length,s.length),o=a,l=0;l191&&r<224?s[a++]=(31&r)<<6|63&e[n++]:r>239&&r<365?(r=((7&r)<<18|(63&e[n++])<<12|(63&e[n++])<<6|63&e[n++])-65536,s[a++]=55296+(r>>10),s[a++]=56320+(1023&r)):s[a++]=(15&r)<<12|(63&e[n++])<<6|63&e[n++],a>8191&&((i||(i=[])).push(String.fromCharCode.apply(String,s)),a=0);return i?(a&&i.push(String.fromCharCode.apply(String,s.slice(0,a))),i.join("")):String.fromCharCode.apply(String,s.slice(0,a))},r.write=function(e,n,t){for(var r,i,s=t,a=0;a>6|192,n[t++]=63&r|128):55296==(64512&r)&&56320==(64512&(i=e.charCodeAt(a+1)))?(r=65536+((1023&r)<<10)+(1023&i),++a,n[t++]=r>>18|240,n[t++]=r>>12&63|128,n[t++]=r>>6&63|128,n[t++]=63&r|128):(n[t++]=r>>12|224,n[t++]=r>>6&63|128,n[t++]=63&r|128);return t-s}},function(e,n){},function(module,exports,__webpack_require__){(function(process,global){const fs=__webpack_require__(8),path=__webpack_require__(2),utf8=__webpack_require__(7),EOL="win32"===process.platform?"\r\n":"\n";var assemblyscript,isDev;function parseArguments(e){const n={};return Object.keys(exports.options).forEach(e=>{const t=exports.options[e];t.aliases&&((n.alias||(n.alias={}))[e]=t.aliases),void 0!==t.default&&((n.default||(n.default={}))[e]=t.default),"string"===t.type?(n.string||(n.string=[])).push(e):"boolean"===t.type&&(n.boolean||(n.boolean=[])).push(e)}),__webpack_require__(4)(e,n)}function checkDiagnostics(e,n){for(var t,r=!1;null!=(t=assemblyscript.nextDiagnostic(e));)n.write(assemblyscript.formatDiagnostic(t,n.isTTY,!0)+EOL+EOL),assemblyscript.isError(t)&&(r=!0);return r}function createStats(){return{readTime:0,readCount:0,writeTime:0,writeCount:0,parseTime:0,parseCount:0,compileTime:0,compileCount:0,emitTime:0,emitCount:0,validateTime:0,validateCount:0,optimizeTime:0,optimizeCount:0}}function measure(e){const n=process.hrtime();e();const t=process.hrtime(n);return 1e9*t[0]+t[1]}function formatTime(e){return e?(e/1e6).toFixed(3)+" ms":"N/A"}function printStats(e,n){function t(e,n){return formatTime(e)}(n||process.stdout).write(["I/O Read : "+t(e.readTime,e.readCount),"I/O Write : "+t(e.writeTime,e.writeCount),"Parse : "+t(e.parseTime,e.parseCount),"Compile : "+t(e.compileTime,e.compileCount),"Emit : "+t(e.emitTime,e.emitCount),"Validate : "+t(e.validateTime,e.validateCount),"Optimize : "+t(e.optimizeTime,e.optimizeCount)].join(EOL)+EOL)}(()=>{try{assemblyscript=__webpack_require__(6),isDev=!1;try{__webpack_require__(!function(){var e=new Error('Cannot find module "source-map-support"');throw e.code="MODULE_NOT_FOUND",e}()).install()}catch(e){}}catch(e){try{__webpack_require__(!function(){var e=new Error('Cannot find module "ts-node"');throw e.code="MODULE_NOT_FOUND",e}()).register({project:path.join(".","..","src","tsconfig.json")}),__webpack_require__(!function(){var e=new Error('Cannot find module "../src/glue/js"');throw e.code="MODULE_NOT_FOUND",e}()),assemblyscript=__webpack_require__(!function(){var e=new Error('Cannot find module "../src"');throw e.code="MODULE_NOT_FOUND",e}()),isDev=!0}catch(e){assemblyscript=eval("require('./assemblyscript')"),isDev=!1}}})(),exports.isBundle=!0,exports.isDev=isDev,exports.version=exports.isBundle?"0.5.0":__webpack_require__(!function(){var e=new Error('Cannot find module "../package.json"');throw e.code="MODULE_NOT_FOUND",e}()).version,exports.options=__webpack_require__(5),exports.sourceMapRoot="assemblyscript:///",exports.libraryPrefix=assemblyscript.LIBRARY_PREFIX,exports.defaultOptimizeLevel=2,exports.defaultShrinkLevel=1,exports.libraryFiles=exports.isBundle?Object({"allocator/arena":'/**\n * Arena Memory Allocator\n *\n * Provides a `reset_memory` function to reset the heap to its initial state. A user has to make\n * sure that there are no more references to cleared memory afterwards. Always aligns to 8 bytes.\n *\n * @module std/assembly/allocator/arena\n *//***/\n\nimport { AL_MASK, MAX_SIZE_32 } from "../internal/allocator";\n\nvar startOffset: usize = (HEAP_BASE + AL_MASK) & ~AL_MASK;\nvar offset: usize = startOffset;\n\n@global\nexport function allocate_memory(size: usize): usize {\n if (size) {\n if (size > MAX_SIZE_32) unreachable();\n let ptr = offset;\n let newPtr = (ptr + size + AL_MASK) & ~AL_MASK;\n let pagesBefore = current_memory();\n if (newPtr > pagesBefore << 16) {\n let pagesNeeded = ((newPtr - ptr + 0xffff) & ~0xffff) >>> 16;\n let pagesWanted = max(pagesBefore, pagesNeeded); // double memory\n if (grow_memory(pagesWanted) < 0) {\n if (grow_memory(pagesNeeded) < 0) {\n unreachable(); // out of memory\n }\n }\n }\n offset = newPtr;\n return ptr;\n }\n return 0;\n}\n\n@global\nexport function free_memory(ptr: usize): void {\n // nop\n}\n\n@global\nexport function reset_memory(): void {\n offset = startOffset;\n}\n',"allocator/buddy":'/**\n * Buddy Memory Allocator.\n * @module std/assembly/allocator/buddy\n *//***/\n\n/*\n Copyright 2018 Evan Wallace\n\n Permission is hereby granted, free of charge, to any person obtaining a copy\n of this software and associated documentation files (the "Software"), to deal\n in the Software without restriction, including without limitation the rights\n to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n copies of the Software, and to permit persons to whom the Software is\n furnished to do so, subject to the following conditions:\n\n The above copyright notice and this permission notice shall be included in all\n copies or substantial portions of the Software.\n\n THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n SOFTWARE.\n\n*/// see: https://github.com/evanw/buddy-malloc\n\n/*\n * This file implements a buddy memory allocator, which is an allocator that\n * allocates memory within a fixed linear address range. It spans the address\n * range with a binary tree that tracks free space. Both "malloc" and "free"\n * are O(log N) time where N is the maximum possible number of allocations.\n *\n * The "buddy" term comes from how the tree is used. When memory is allocated,\n * nodes in the tree are split recursively until a node of the appropriate size\n * is reached. Every split results in two child nodes, each of which is the\n * buddy of the other. When a node is freed, the node and its buddy can be\n * merged again if the buddy is also free. This makes the memory available\n * for larger allocations again.\n */\n\n/*\n * Every allocation needs an 8-byte header to store the allocation size while\n * staying 8-byte aligned. The address returned by "malloc" is the address\n * right after this header (i.e. the size occupies the 8 bytes before the\n * returned address).\n */\nconst HEADER_SIZE: usize = 8;\n\n/*\n * The minimum allocation size is 16 bytes because we have an 8-byte header and\n * we need to stay 8-byte aligned.\n */\nconst MIN_ALLOC_LOG2: usize = 4;\nconst MIN_ALLOC: usize = 1 << MIN_ALLOC_LOG2;\n\n/*\n * The maximum allocation size is currently set to 2gb. This is the total size\n * of the heap. It\'s technically also the maximum allocation size because the\n * heap could consist of a single allocation of this size. But of course real\n * heaps will have multiple allocations, so the real maximum allocation limit\n * is at most 1gb.\n */\nconst MAX_ALLOC_LOG2: usize = 30; // 31;\nconst MAX_ALLOC: usize = 1 << MAX_ALLOC_LOG2;\n\n/*\n * Allocations are done in powers of two starting from MIN_ALLOC and ending at\n * MAX_ALLOC inclusive. Each allocation size has a bucket that stores the free\n * list for that allocation size.\n *\n * Given a bucket index, the size of the allocations in that bucket can be\n * found with "(size_t)1 << (MAX_ALLOC_LOG2 - bucket)".\n */\nconst BUCKET_COUNT: usize = MAX_ALLOC_LOG2 - MIN_ALLOC_LOG2 + 1;\n\n/*\n * Free lists are stored as circular doubly-linked lists. Every possible\n * allocation size has an associated free list that is threaded through all\n * currently free blocks of that size. That means MIN_ALLOC must be at least\n * "sizeof(list_t)". MIN_ALLOC is currently 16 bytes, so this will be true for\n * both 32-bit and 64-bit.\n */\n@unmanaged\nclass List {\n prev: List;\n next: List;\n static readonly SIZE: usize = 2 * sizeof();\n}\n\n/*\n * Each bucket corresponds to a certain allocation size and stores a free list\n * for that size. The bucket at index 0 corresponds to an allocation size of\n * MAX_ALLOC (i.e. the whole address space).\n */\nvar BUCKETS_START: usize = HEAP_BASE;\nvar BUCKETS_END: usize = BUCKETS_START + BUCKET_COUNT * List.SIZE;\n\nfunction buckets$get(index: usize): List {\n assert(index < BUCKET_COUNT);\n return changetype(BUCKETS_START + index * List.SIZE);\n}\n\n/*\n * We could initialize the allocator by giving it one free block the size of\n * the entire address space. However, this would cause us to instantly reserve\n * half of the entire address space on the first allocation, since the first\n * split would store a free list entry at the start of the right child of the\n * root. Instead, we have the tree start out small and grow the size of the\n * tree as we use more memory. The size of the tree is tracked by this value.\n */\nvar bucket_limit: usize;\n\n/*\n * This array represents a linearized binary tree of bits. Every possible\n * allocation larger than MIN_ALLOC has a node in this tree (and therefore a\n * bit in this array).\n *\n * Given the index for a node, lineraized binary trees allow you to traverse to\n * the parent node or the child nodes just by doing simple arithmetic on the\n * index:\n *\n * - Move to parent: index = (index - 1) / 2;\n * - Move to left child: index = index * 2 + 1;\n * - Move to right child: index = index * 2 + 2;\n * - Move to sibling: index = ((index - 1) ^ 1) + 1;\n *\n * Each node in this tree can be in one of several states:\n *\n * - UNUSED (both children are UNUSED)\n * - SPLIT (one child is UNUSED and the other child isn\'t)\n * - USED (neither children are UNUSED)\n *\n * These states take two bits to store. However, it turns out we have enough\n * information to distinguish between UNUSED and USED from context, so we only\n * need to store SPLIT or not, which only takes a single bit.\n *\n * Note that we don\'t need to store any nodes for allocations of size MIN_ALLOC\n * since we only ever care about parent nodes.\n */\nconst SPLIT_COUNT: usize = (1 << (BUCKET_COUNT - 1)) / 8;\nvar NODE_IS_SPLIT_START: usize = BUCKETS_END;\nvar NODE_IS_SPLIT_END: usize = NODE_IS_SPLIT_START + SPLIT_COUNT * sizeof();\n\nfunction node_is_split$get(index: usize): i32 {\n assert(index < SPLIT_COUNT);\n return load(NODE_IS_SPLIT_START + index);\n}\n\nfunction node_is_split$set(index: usize, state: i32): void {\n assert(index < SPLIT_COUNT);\n store(NODE_IS_SPLIT_START + index, state);\n}\n\n/*\n * This is the starting address of the address range for this allocator. Every\n * returned allocation will be an offset of this pointer from 0 to MAX_ALLOC.\n */\nvar base_ptr: usize;\n\n/*\n * This is the maximum address that has ever been used by the allocator. It\'s\n * used to know when to call "brk" to request more memory from the kernel.\n */\nvar max_ptr: usize;\n\n/*\n * Make sure all addresses before "new_value" are valid and can be used. Memory\n * is allocated in a 2gb address range but that memory is not reserved up\n * front. It\'s only reserved when it\'s needed by calling this function. This\n * will return false if the memory could not be reserved.\n */\nfunction update_max_ptr(new_value: usize): i32 {\n if (new_value > max_ptr) {\n // if (brk(new_value)) {\n // return 0;\n // }\n let oldPages = current_memory();\n let newPages = (((new_value + 0xffff) & ~0xffff) >> 16);\n assert(newPages > oldPages);\n if (grow_memory(newPages - oldPages) < 0) {\n return 0;\n }\n // max_ptr = new_value;\n max_ptr = newPages << 16;\n }\n return 1;\n}\n\n/*\n * Initialize a list to empty. Because these are circular lists, an "empty"\n * list is an entry where both links point to itself. This makes insertion\n * and removal simpler because they don\'t need any branches.\n */\nfunction list_init(list: List): void {\n list.prev = list;\n list.next = list;\n}\n\n/*\n * Append the provided entry to the end of the list. This assumes the entry\n * isn\'t in a list already because it overwrites the linked list pointers.\n */\nfunction list_push(list: List, entry: List): void {\n var prev = list.prev;\n entry.prev = prev;\n entry.next = list;\n prev.next = entry;\n list.prev = entry;\n}\n\n/*\n * Remove the provided entry from whichever list it\'s currently in. This\n * assumes that the entry is in a list. You don\'t need to provide the list\n * because the lists are circular, so the list\'s pointers will automatically\n * be updated if the first or last entries are removed.\n */\nfunction list_remove(entry: List): void {\n var prev = entry.prev;\n var next = entry.next;\n prev.next = next;\n next.prev = prev;\n}\n\n/*\n * Remove and return the first entry in the list or NULL if the list is empty.\n */\nfunction list_pop(list: List): List | null {\n var back = list.prev;\n if (back == list) return null;\n list_remove(back);\n return back;\n}\n\n/*\n * This maps from the index of a node to the address of memory that node\n * represents. The bucket can be derived from the index using a loop but is\n * required to be provided here since having them means we can avoid the loop\n * and have this function return in constant time.\n */\nfunction ptr_for_node(index: usize, bucket: usize): usize {\n return base_ptr + ((index - (1 << bucket) + 1) << (MAX_ALLOC_LOG2 - bucket));\n}\n\n/*\n * This maps from an address of memory to the node that represents that\n * address. There are often many nodes that all map to the same address, so\n * the bucket is needed to uniquely identify a node.\n */\nfunction node_for_ptr(ptr: usize, bucket: usize): usize {\n return ((ptr - base_ptr) >> (MAX_ALLOC_LOG2 - bucket)) + (1 << bucket) - 1;\n}\n\n/*\n * Given the index of a node, this returns the "is split" flag of the parent.\n */\nfunction parent_is_split(index: usize): i32 {\n index = (index - 1) / 2;\n return (node_is_split$get(index / 8) >>> (index % 8)) & 1;\n}\n\n/*\n * Given the index of a node, this flips the "is split" flag of the parent.\n */\nfunction flip_parent_is_split(index: usize): void {\n index = (index - 1) / 2;\n var indexDiv8 = index / 8;\n node_is_split$set(indexDiv8,\n node_is_split$get(indexDiv8) ^ (1 << (index % 8))\n );\n}\n\n/*\n * Given the requested size passed to "malloc", this function returns the index\n * of the smallest bucket that can fit that size.\n */\nfunction bucket_for_request(request: usize): usize {\n var bucket = BUCKET_COUNT - 1;\n var size = MIN_ALLOC;\n\n while (size < request) {\n bucket--;\n size *= 2;\n }\n\n return bucket;\n}\n\n/*\n * The tree is always rooted at the current bucket limit. This call grows the\n * tree by repeatedly doubling it in size until the root lies at the provided\n * bucket index. Each doubling lowers the bucket limit by 1.\n */\nfunction lower_bucket_limit(bucket: usize): u32 {\n while (bucket < bucket_limit) {\n let root = node_for_ptr(base_ptr, bucket_limit);\n let right_child: usize;\n\n /*\n * If the parent isn\'t SPLIT, that means the node at the current bucket\n * limit is UNUSED and our address space is entirely free. In that case,\n * clear the root free list, increase the bucket limit, and add a single\n * block with the newly-expanded address space to the new root free list.\n */\n if (!parent_is_split(root)) {\n list_remove(changetype(base_ptr));\n list_init(buckets$get(--bucket_limit));\n list_push(buckets$get(bucket_limit), changetype(base_ptr));\n continue;\n }\n\n /*\n * Otherwise, the tree is currently in use. Create a parent node for the\n * current root node in the SPLIT state with a right child on the free\n * list. Make sure to reserve the memory for the free list entry before\n * writing to it. Note that we do not need to flip the "is split" flag for\n * our current parent because it\'s already on (we know because we just\n * checked it above).\n */\n right_child = ptr_for_node(root + 1, bucket_limit);\n if (!update_max_ptr(right_child + List.SIZE)) {\n return 0;\n }\n list_push(buckets$get(bucket_limit), changetype(right_child));\n list_init(buckets$get(--bucket_limit));\n\n /*\n * Set the grandparent\'s SPLIT flag so if we need to lower the bucket limit\n * again, we\'ll know that the new root node we just added is in use.\n */\n root = (root - 1) / 2;\n if (root != 0) {\n flip_parent_is_split(root);\n }\n }\n\n return 1;\n}\n\n@global\nexport function allocate_memory(request: usize): usize {\n var original_bucket: usize, bucket: usize;\n\n /*\n * Make sure it\'s possible for an allocation of this size to succeed. There\'s\n * a hard-coded limit on the maximum allocation size because of the way this\n * allocator works.\n */\n if (request > MAX_ALLOC - HEADER_SIZE) unreachable();\n\n /*\n * Initialize our global state if this is the first call to "malloc". At the\n * beginning, the tree has a single node that represents the smallest\n * possible allocation size. More memory will be reserved later as needed.\n */\n if (base_ptr == 0) {\n // base_ptr = max_ptr = (uint8_t *)sbrk(0);\n base_ptr = (NODE_IS_SPLIT_END + 7) & ~7; // must be aligned\n max_ptr = current_memory() << 16; // must grow first\n bucket_limit = BUCKET_COUNT - 1;\n if (!update_max_ptr(base_ptr + List.SIZE)) {\n return 0;\n }\n list_init(buckets$get(BUCKET_COUNT - 1));\n list_push(buckets$get(BUCKET_COUNT - 1), changetype(base_ptr));\n }\n\n /*\n * Find the smallest bucket that will fit this request. This doesn\'t check\n * that there\'s space for the request yet.\n */\n bucket = bucket_for_request(request + HEADER_SIZE);\n original_bucket = bucket;\n\n /*\n * Search for a bucket with a non-empty free list that\'s as large or larger\n * than what we need. If there isn\'t an exact match, we\'ll need to split a\n * larger one to get a match.\n */\n while (bucket + 1 != 0) {\n let size: usize, bytes_needed: usize, i: usize;\n let ptr: usize;\n\n /*\n * We may need to grow the tree to be able to fit an allocation of this\n * size. Try to grow the tree and stop here if we can\'t.\n */\n if (!lower_bucket_limit(bucket)) {\n return 0;\n }\n\n /*\n * Try to pop a block off the free list for this bucket. If the free list\n * is empty, we\'re going to have to split a larger block instead.\n */\n ptr = changetype(list_pop(buckets$get(bucket)));\n if (!ptr) {\n /*\n * If we\'re not at the root of the tree or it\'s impossible to grow the\n * tree any more, continue on to the next bucket.\n */\n if (bucket != bucket_limit || bucket == 0) {\n bucket--;\n continue;\n }\n\n /*\n * Otherwise, grow the tree one more level and then pop a block off the\n * free list again. Since we know the root of the tree is used (because\n * the free list was empty), this will add a parent above this node in\n * the SPLIT state and then add the new right child node to the free list\n * for this bucket. Popping the free list will give us this right child.\n */\n if (!lower_bucket_limit(bucket - 1)) {\n return 0;\n }\n ptr = changetype(list_pop(buckets$get(bucket)));\n }\n\n /*\n * Try to expand the address space first before going any further. If we\n * have run out of space, put this block back on the free list and fail.\n */\n size = 1 << (MAX_ALLOC_LOG2 - bucket);\n bytes_needed = bucket < original_bucket ? size / 2 + List.SIZE : size;\n if (!update_max_ptr(ptr + bytes_needed)) {\n list_push(buckets$get(bucket), changetype(ptr));\n return 0;\n }\n\n /*\n * If we got a node off the free list, change the node from UNUSED to USED.\n * This involves flipping our parent\'s "is split" bit because that bit is\n * the exclusive-or of the UNUSED flags of both children, and our UNUSED\n * flag (which isn\'t ever stored explicitly) has just changed.\n *\n * Note that we shouldn\'t ever need to flip the "is split" bit of our\n * grandparent because we know our buddy is USED so it\'s impossible for our\n * grandparent to be UNUSED (if our buddy chunk was UNUSED, our parent\n * wouldn\'t ever have been split in the first place).\n */\n i = node_for_ptr(ptr, bucket);\n if (i != 0) {\n flip_parent_is_split(i);\n }\n\n /*\n * If the node we got is larger than we need, split it down to the correct\n * size and put the new unused child nodes on the free list in the\n * corresponding bucket. This is done by repeatedly moving to the left\n * child, splitting the parent, and then adding the right child to the free\n * list.\n */\n while (bucket < original_bucket) {\n i = i * 2 + 1;\n bucket++;\n flip_parent_is_split(i);\n list_push(\n buckets$get(bucket),\n changetype(ptr_for_node(i + 1, bucket))\n );\n }\n\n /*\n * Now that we have a memory address, write the block header (just the size\n * of the allocation) and return the address immediately after the header.\n */\n store(ptr, request);\n return ptr + HEADER_SIZE;\n }\n\n return 0;\n}\n\n@global\nexport function free_memory(ptr: usize): void {\n var bucket: usize, i: usize;\n\n /*\n * Ignore any attempts to free a NULL pointer.\n */\n if (!ptr) {\n return;\n }\n\n /*\n * We were given the address returned by "malloc" so get back to the actual\n * address of the node by subtracting off the size of the block header. Then\n * look up the index of the node corresponding to this address.\n */\n ptr = ptr - HEADER_SIZE;\n bucket = bucket_for_request(load(ptr) + HEADER_SIZE);\n i = node_for_ptr(ptr, bucket);\n\n /*\n * Traverse up to the root node, flipping USED blocks to UNUSED and merging\n * UNUSED buddies together into a single UNUSED parent.\n */\n while (i != 0) {\n /*\n * Change this node from UNUSED to USED. This involves flipping our\n * parent\'s "is split" bit because that bit is the exclusive-or of the\n * UNUSED flags of both children, and our UNUSED flag (which isn\'t ever\n * stored explicitly) has just changed.\n */\n flip_parent_is_split(i);\n\n /*\n * If the parent is now SPLIT, that means our buddy is USED, so don\'t merge\n * with it. Instead, stop the iteration here and add ourselves to the free\n * list for our bucket.\n *\n * Also stop here if we\'re at the current root node, even if that root node\n * is now UNUSED. Root nodes don\'t have a buddy so we can\'t merge with one.\n */\n if (parent_is_split(i) || bucket == bucket_limit) {\n break;\n }\n\n /*\n * If we get here, we know our buddy is UNUSED. In this case we should\n * merge with that buddy and continue traversing up to the root node. We\n * need to remove the buddy from its free list here but we don\'t need to\n * add the merged parent to its free list yet. That will be done once after\n * this loop is finished.\n */\n list_remove(changetype(ptr_for_node(((i - 1) ^ 1) + 1, bucket)));\n i = (i - 1) / 2;\n bucket--;\n }\n\n /*\n * Add ourselves to the free list for our bucket. We add to the back of the\n * list because "malloc" takes from the back of the list and we want a "free"\n * followed by a "malloc" of the same size to ideally use the same address\n * for better memory locality.\n */\n list_push(buckets$get(bucket), changetype(ptr_for_node(i, bucket)));\n}\n\n@global\nexport function reset_memory(): void {\n unreachable();\n}\n',"allocator/emscripten":"/**\n * Emscripten Memory Allocator.\n *\n * Uses Emscripten's exported _malloc and _free implementations, i.e., when linking with\n * Emscripten-compiled programs that already provide these. Differs from 'system' in that their\n * names are prefixed with an underscore.\n *\n * @module std/assembly/allocator/emscripten\n *//***/\n\ndeclare function _malloc(size: usize): usize;\ndeclare function _free(ptr: usize): void;\n\n@global\nexport function allocate_memory(size: usize): usize {\n return _malloc(size);\n}\n\n@global\nexport function free_memory(ptr: usize): void {\n _free(ptr);\n}\n\n@global\nexport function reset_memory(): void {\n unreachable();\n}\n","allocator/system":"/**\n * System Memory Allocator.\n *\n * Uses the environment's malloc and free implementations, i.e., when linking with other C-like\n * programs that already provide these.\n *\n * @module std/assembly/allocator/system\n *//***/\n\ndeclare function malloc(size: usize): usize;\ndeclare function free(ptr: usize): void;\n\n@global\nexport function allocate_memory(size: usize): usize {\n return malloc(size);\n}\n\n@global\nexport function free_memory(ptr: usize): void {\n free(ptr);\n}\n\n@global\nexport function reset_memory(): void {\n unreachable();\n}\n","allocator/tlsf":"/**\n * Two-Level Segregate Fit Memory Allocator.\n *\n * A general purpose dynamic memory allocator specifically designed to meet real-time requirements.\n * Always aligns to 8 bytes.\n *\n * @module std/assembly/allocator/tlsf\n *//***/\n\n// ╒══════════════ Block size interpretation (32-bit) ═════════════╕\n// 3 2 1\n// 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 bits\n// ├─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┼─┴─┴─┴─┴─╫─┴─┴─┤\n// │ | FL │ SB = SL + AL │ ◄─ usize\n// └───────────────────────────────────────────────┴─────────╨─────┘\n// FL: first level, SL: second level, AL: alignment, SB: small block\n\nimport {\n AL_BITS,\n AL_SIZE,\n AL_MASK\n} from \"../internal/allocator\";\n\nconst SL_BITS: u32 = 5;\nconst SL_SIZE: usize = 1 << SL_BITS;\n\nconst SB_BITS: usize = (SL_BITS + AL_BITS);\nconst SB_SIZE: usize = 1 << SB_BITS;\n\nconst FL_BITS: u32 = (sizeof() == sizeof()\n ? 30 // ^= up to 1GB per block\n : 32 // ^= up to 4GB per block\n) - SB_BITS;\n\n// ╒════════════════ Block structure layout (32-bit) ══════════════╕\n// 3 2 1\n// 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 bits\n// ├─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┼─┼─┤\n// │ size │L│F│ ◄─┐ info\n// ╞═══════════════════════════════════════════════════════════╧═╧═╡ │ ┐\n// │ if free: ◄ prev │ ◄─┤ usize\n// ├───────────────────────────────────────────────────────────────┤ │\n// │ if free: next ► │ ◄─┤\n// ├───────────────────────────────────────────────────────────────┤ │\n// │ ... unused free space >= 0 ... │ │ = 0\n// ├ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┤ │\n// │ if free: jump ▲ │ ◄─┘\n// └───────────────────────────────────────────────────────────────┘ MIN SIZE ┘\n// F: FREE, L: LEFT_FREE\n\n/** Tag indicating that this block is free. */\nconst FREE: usize = 1 << 0;\n/** Tag indicating that this block's left block is free. */\nconst LEFT_FREE: usize = 1 << 1;\n/** Mask to obtain all tags. */\nconst TAGS: usize = FREE | LEFT_FREE;\n\n/** Block structure. */\n@unmanaged\nclass Block {\n\n /** Info field holding this block's size and tags. */\n info: usize;\n\n /** End offset of the {@link Block#info} field. User data starts here. */\n static readonly INFO: usize = (sizeof() + AL_MASK) & ~AL_MASK;\n\n /** Previous free block, if any. Only valid if free. */\n prev: Block | null;\n /** Next free block, if any. Only valid if free. */\n next: Block | null;\n\n /** Minimum size of a block, excluding {@link Block#info}. */\n static readonly MIN_SIZE: usize = (3 * sizeof() + AL_MASK) & ~AL_MASK;// prev + next + jump\n\n /** Maximum size of a used block, excluding {@link Block#info}. */\n static readonly MAX_SIZE: usize = 1 << (FL_BITS + SB_BITS);\n\n /** Gets this block's left (free) block in memory. */\n get left(): Block {\n assert(this.info & LEFT_FREE); // must be free to contain a jump\n return assert(\n load(changetype(this) - sizeof())\n ); // can't be null\n }\n\n /** Gets this block's right block in memory. */\n get right(): Block {\n assert(this.info & ~TAGS); // can't skip beyond the tail block\n return assert(\n changetype(\n changetype(this) + Block.INFO + (this.info & ~TAGS)\n )\n ); // can't be null\n }\n}\n\n// ╒════════════════ Root structure layout (32-bit) ═══════════════╕\n// 3 2 1\n// 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 bits\n// ├─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┤ ┐\n// │ 0 | flMap S│ ◄────┐\n// ╞═══════════════════════════════════════════════════════════════╡ │\n// │ slMap[0] S │ ◄─┐ │\n// ├───────────────────────────────────────────────────────────────┤ │ │\n// │ slMap[1] │ ◄─┤ │\n// ├───────────────────────────────────────────────────────────────┤ u32 │\n// │ ... │ ◄─┤ │\n// ├───────────────────────────────────────────────────────────────┤ │ │\n// │ slMap[22] P │ ◄─┘ │\n// ╞═══════════════════════════════════════════════════════════════╡ usize\n// │ head[0] │ ◄────┤\n// ├───────────────────────────────────────────────────────────────┤ │\n// │ ... │ ◄────┤\n// ├───────────────────────────────────────────────────────────────┤ │\n// │ head[736] │ ◄────┤\n// ╞═══════════════════════════════════════════════════════════════╡ │\n// │ tailRef │ ◄────┘\n// └───────────────────────────────────────────────────────────────┘ SIZE ┘\n// S: Small blocks map, P: Possibly padded if 64-bit\n\nassert((1 << SL_BITS) <= 32); // second level must fit into 32 bits\n\n/** Root structure. */\n@unmanaged\nclass Root {\n\n /** First level bitmap. */\n flMap: usize = 0;\n\n /** Start offset of second level maps. */\n private static readonly SL_START: usize = sizeof();\n\n // Using *one* SL map per *FL bit*\n\n /** Gets the second level map for the specified first level. */\n getSLMap(fl: usize): u32 {\n assert(fl < FL_BITS); // fl out of range\n return load(changetype(this) + fl * 4, Root.SL_START);\n }\n\n /** Sets the second level map for the specified first level. */\n setSLMap(fl: usize, value: u32): void {\n assert(fl < FL_BITS); // fl out of range\n store(changetype(this) + fl * 4, value, Root.SL_START);\n }\n\n /** End offset of second level maps. */\n private static readonly SL_END: usize = Root.SL_START + FL_BITS * 4;\n\n // Using *number bits per SL* heads per *FL bit*\n\n /** Start offset of FL/SL heads. */\n private static readonly HL_START: usize = (Root.SL_END + AL_MASK) & ~AL_MASK;\n\n /** Gets the head of the specified first and second level index. */\n getHead(fl: usize, sl: u32): Block | null {\n assert(fl < FL_BITS); // fl out of range\n assert(sl < SL_SIZE); // sl out of range\n return changetype(load(\n changetype(this) + (fl * SL_SIZE + sl) * sizeof()\n , Root.HL_START));\n }\n\n /** Sets the head of the specified first and second level index. */\n setHead(fl: usize, sl: u32, value: Block | null): void {\n assert(fl < FL_BITS); // fl out of range\n assert(sl < SL_SIZE); // sl out of range\n store(\n changetype(this) + (fl * SL_SIZE + sl) * sizeof()\n , changetype(value)\n , Root.HL_START);\n }\n\n /** End offset of FL/SL heads. */\n private static readonly HL_END: usize = (\n Root.HL_START + FL_BITS * SL_SIZE * sizeof()\n );\n\n get tailRef(): usize { return load(0, Root.HL_END); }\n set tailRef(value: usize) { store(0, value, Root.HL_END); }\n\n /** Total size of the {@link Root} structure. */\n static readonly SIZE: usize = Root.HL_END + sizeof();\n\n /** Inserts a previously used block back into the free list. */\n insert(block: Block): void {\n // check as much as possible here to prevent invalid free blocks\n assert(block); // cannot be null\n var blockInfo = block.info;\n assert(blockInfo & FREE); // must be free\n var size: usize;\n assert(\n (size = block.info & ~TAGS) >= Block.MIN_SIZE && size < Block.MAX_SIZE\n ); // must be valid, not necessary to compute yet if noAssert=true\n\n var right: Block = assert(block.right); // can't be null\n var rightInfo = right.info;\n\n // merge with right block if also free\n if (rightInfo & FREE) {\n this.remove(right);\n block.info = (blockInfo += Block.INFO + (rightInfo & ~TAGS));\n right = block.right;\n rightInfo = right.info;\n // jump is set below\n }\n\n // merge with left block if also free\n if (blockInfo & LEFT_FREE) {\n let left: Block = assert(block.left); // can't be null\n let leftInfo = left.info;\n assert(leftInfo & FREE); // must be free according to tags\n this.remove(left);\n left.info = (leftInfo += Block.INFO + (blockInfo & ~TAGS));\n block = left;\n blockInfo = leftInfo;\n // jump is set below\n }\n\n right.info = rightInfo | LEFT_FREE;\n this.setJump(block, right);\n // right is no longer used now, hence rightInfo is not synced\n\n size = blockInfo & ~TAGS;\n assert(size >= Block.MIN_SIZE && size < Block.MAX_SIZE); // must be valid\n\n // mapping_insert\n var fl: usize, sl: u32;\n if (size < SB_SIZE) {\n fl = 0;\n sl = (size / AL_SIZE);\n } else {\n fl = fls(size);\n sl = ((size >> (fl - SL_BITS)) ^ (1 << SL_BITS));\n fl -= SB_BITS - 1;\n }\n\n // perform insertion\n var head = this.getHead(fl, sl);\n block.prev = null;\n block.next = head;\n if (head) head.prev = block;\n this.setHead(fl, sl, block);\n\n // update first and second level maps\n this.flMap |= (1 << fl);\n this.setSLMap(fl, this.getSLMap(fl) | (1 << sl));\n }\n\n /**\n * Removes a free block from FL/SL maps. Does not alter left/jump because it\n * is likely that splitting is performed afterwards, invalidating any changes\n * again.\n */\n private remove(block: Block): void {\n var blockInfo = block.info;\n assert(blockInfo & FREE); // must be free\n var size = blockInfo & ~TAGS;\n assert(size >= Block.MIN_SIZE && size < Block.MAX_SIZE); // must be valid\n\n // mapping_insert\n var fl: usize, sl: u32;\n if (size < SB_SIZE) {\n fl = 0;\n sl = (size / AL_SIZE);\n } else {\n fl = fls(size);\n sl = ((size >> (fl - SL_BITS)) ^ (1 << SL_BITS));\n fl -= SB_BITS - 1;\n }\n\n // link previous and next free block\n var prev = block.prev;\n var next = block.next;\n if (prev) prev.next = next;\n if (next) next.prev = prev;\n\n // update head if we are removing it\n if (block == this.getHead(fl, sl)) {\n this.setHead(fl, sl, next);\n\n // clear second level map if head is empty now\n if (!next) {\n let slMap = this.getSLMap(fl);\n this.setSLMap(fl, slMap &= ~(1 << sl));\n\n // clear first level map if second level is empty now\n if (!slMap) this.flMap &= ~(1 << fl);\n }\n }\n }\n\n /** Searches for a free block of at least the specified size. */\n search(size: usize): Block | null {\n assert(size >= Block.MIN_SIZE && size < Block.MAX_SIZE);\n\n // mapping_search\n var fl: usize, sl: u32;\n if (size < SB_SIZE) {\n fl = 0;\n sl = (size / AL_SIZE);\n } else {\n // (*) size += (1 << (fls(size) - SL_BITS)) - 1;\n fl = fls(size);\n sl = ((size >> (fl - SL_BITS)) ^ (1 << SL_BITS));\n fl -= SB_BITS - 1;\n // (*) instead of rounding up, use next second level list for better fit\n if (sl < SL_SIZE - 1) ++sl;\n else ++fl, sl = 0;\n }\n\n // search second level\n var slMap = this.getSLMap(fl) & (~0 << sl);\n var head: Block | null;\n if (!slMap) {\n // search next larger first level\n let flMap = this.flMap & (~0 << (fl + 1));\n if (!flMap) {\n head = null;\n } else {\n fl = ffs(flMap);\n slMap = assert(this.getSLMap(fl)); // can't be zero if fl points here\n head = this.getHead(fl, ffs(slMap));\n }\n } else {\n head = this.getHead(fl, ffs(slMap));\n }\n return head;\n }\n\n /** Links a free left with its right block in memory. */\n private setJump(left: Block, right: Block): void {\n assert(left.info & FREE); // must be free\n assert(left.right == right); // right block must match\n assert(right.info & LEFT_FREE); // right block must be tagged as LEFT_FREE\n store(\n changetype(right) - sizeof()\n , left); // last word in left block's (free) data region\n }\n\n /**\n * Uses the specified free block, removing it from internal maps and\n * splitting it if possible, and returns its data pointer.\n */\n use(block: Block, size: usize): usize {\n var blockInfo = block.info;\n assert(blockInfo & FREE); // must be free so we can use it\n assert(size >= Block.MIN_SIZE && size < Block.MAX_SIZE); // must be valid\n assert(!(size & AL_MASK)); // size must be aligned so the new block is\n\n this.remove(block);\n\n // split if the block can hold another MIN_SIZE block\n var remaining = (blockInfo & ~TAGS) - size;\n if (remaining >= Block.INFO + Block.MIN_SIZE) {\n block.info = size | (blockInfo & LEFT_FREE); // also discards FREE\n\n let spare = changetype(\n changetype(block) + Block.INFO + size\n );\n spare.info = (remaining - Block.INFO) | FREE; // not LEFT_FREE\n this.insert(spare); // also sets jump\n\n // otherwise tag block as no longer FREE and right as no longer LEFT_FREE\n } else {\n block.info = blockInfo & ~FREE;\n let right: Block = assert(block.right); // can't be null (tail)\n right.info &= ~LEFT_FREE;\n }\n\n return changetype(block) + Block.INFO;\n }\n\n /** Adds more memory to the pool. */\n addMemory(start: usize, end: usize): bool {\n assert(start <= end);\n assert(!(start & AL_MASK)); // must be aligned\n assert(!(end & AL_MASK)); // must be aligned\n\n var tailRef = this.tailRef;\n var tailInfo: usize = 0;\n if (tailRef) {\n assert(start >= tailRef + sizeof()); // starts after tail\n\n // merge with current tail if adjacent\n if (start - Block.INFO == tailRef) {\n start -= Block.INFO;\n tailInfo = changetype(tailRef).info;\n }\n\n } else {\n assert(start >= changetype(this) + Root.SIZE); // starts after root\n }\n\n // check if size is large enough for a free block and the tail block\n var size = end - start;\n if (size < Block.INFO + Block.MIN_SIZE + Block.INFO) {\n return false;\n }\n\n // left size is total minus its own and the zero-length tail's header\n var leftSize = size - 2 * Block.INFO;\n var left = changetype(start);\n left.info = leftSize | FREE | (tailInfo & LEFT_FREE);\n left.prev = null;\n left.next = null;\n\n // tail is a zero-length used block\n var tail = changetype(start + size - Block.INFO);\n tail.info = 0 | LEFT_FREE;\n this.tailRef = changetype(tail);\n\n this.insert(left); // also merges with free left before tail / sets jump\n\n return true;\n }\n}\n\n/** Determines the first (LSB to MSB) set bit's index of a word. */\nfunction ffs(word: T): T {\n assert(word != 0); // word cannot be 0\n return ctz(word); // differs from ffs only for 0\n}\n\n/** Determines the last (LSB to MSB) set bit's index of a word. */\nfunction fls(word: T): T {\n assert(word != 0); // word cannot be 0\n const inv: T = (sizeof() << 3) - 1;\n return inv - clz(word);\n}\n\n/** Reference to the initialized {@link Root} structure, once initialized. */\nvar ROOT: Root = changetype(0);\n\n// External interface\n\n/** Allocates a chunk of memory. */\n@global\nexport function allocate_memory(size: usize): usize {\n\n // initialize if necessary\n var root = ROOT;\n if (!root) {\n let rootOffset = (HEAP_BASE + AL_MASK) & ~AL_MASK;\n ROOT = root = changetype(rootOffset);\n root.tailRef = 0;\n root.flMap = 0;\n for (let fl: usize = 0; fl < FL_BITS; ++fl) {\n root.setSLMap(fl, 0);\n for (let sl: u32 = 0; sl < SL_SIZE; ++sl) {\n root.setHead(fl, sl, null);\n }\n }\n root.addMemory((rootOffset + Root.SIZE + AL_MASK) & ~AL_MASK, current_memory() << 16);\n }\n\n // search for a suitable block\n var data: usize = 0;\n if (size) {\n if (size > Block.MAX_SIZE) unreachable();\n // 32-bit MAX_SIZE is 1 << 30 and itself aligned, hence the following can't overflow MAX_SIZE\n size = max((size + AL_MASK) & ~AL_MASK, Block.MIN_SIZE);\n\n let block = root.search(size);\n if (!block) {\n\n // request more memory\n let pagesBefore = current_memory();\n let pagesNeeded = ((size + 0xffff) & ~0xffff) >>> 16;\n let pagesWanted = max(pagesBefore, pagesNeeded); // double memory\n if (grow_memory(pagesWanted) < 0) {\n if (grow_memory(pagesNeeded) < 0) {\n unreachable(); // out of memory\n }\n }\n let pagesAfter = current_memory();\n root.addMemory(pagesBefore << 16, pagesAfter << 16);\n block = assert(root.search(size)); // must be found now\n }\n\n assert((block.info & ~TAGS) >= size);\n data = root.use(block, size);\n }\n\n return data;\n}\n\n/** Frees the chunk of memory at the specified address. */\n@global\nexport function free_memory(data: usize): void {\n if (data) {\n let root = ROOT;\n if (root) {\n let block = changetype(data - Block.INFO);\n let blockInfo = block.info;\n assert(!(blockInfo & FREE)); // must be used\n block.info = blockInfo | FREE;\n root.insert(changetype(data - Block.INFO));\n }\n }\n}\n\n@global\nexport function reset_memory(): void {\n unreachable();\n}\n",array:'import {\n MAX_BLENGTH,\n HEADER_SIZE as HEADER_SIZE_AB,\n allocUnsafe,\n reallocUnsafe,\n loadUnsafe,\n storeUnsafe\n} from "./internal/arraybuffer";\n\nimport {\n defaultComparator,\n insertionSort,\n weakHeapSort\n} from "./internal/array";\n\nexport class Array {\n\n /* @internal */ buffer_: ArrayBuffer;\n /* @internal */ length_: i32;\n\n constructor(length: i32 = 0) {\n const MAX_LENGTH = MAX_BLENGTH >>> alignof();\n if (length > MAX_LENGTH) throw new RangeError("Invalid array length");\n this.buffer_ = allocUnsafe(length << alignof());\n this.length_ = length;\n }\n\n get length(): i32 {\n return this.length_;\n }\n\n set length(length: i32) {\n var buffer = this.buffer_;\n var capacity = buffer.byteLength >>> alignof();\n if (length > capacity) {\n const MAX_LENGTH = MAX_BLENGTH >>> alignof();\n if (length > MAX_LENGTH) throw new RangeError("Invalid array length");\n buffer = reallocUnsafe(buffer, length << alignof());\n this.buffer_ = buffer;\n }\n this.length_ = length;\n }\n\n every(callbackfn: (element: T, index: i32, array: Array) => bool): bool {\n var buffer = this.buffer_;\n for (let index = 0, toIndex = this.length_; index < toIndex && index < this.length_; ++index) {\n if (!callbackfn(loadUnsafe(buffer, index), index, this)) return false;\n }\n return true;\n }\n\n findIndex(predicate: (element: T, index: i32, array: Array) => bool): i32 {\n var buffer = this.buffer_;\n for (let index = 0, toIndex = this.length_; index < toIndex && index < this.length_; ++index) {\n if (predicate(loadUnsafe(buffer, index), index, this)) return index;\n }\n return -1;\n }\n\n @operator("[]")\n private __get(index: i32): T {\n var buffer = this.buffer_;\n var capacity = buffer.byteLength >>> alignof();\n if (index >= capacity) throw new Error("Index out of bounds");\n // return load(changetype(buffer) + (index << alignof()), HEADER_SIZE_AB);\n return loadUnsafe(buffer, index);\n }\n\n @operator("[]=")\n private __set(index: i32, value: T): void {\n var buffer = this.buffer_;\n var capacity = buffer.byteLength >>> alignof();\n if (index >= capacity) {\n const MAX_LENGTH = MAX_BLENGTH >>> alignof();\n if (index >= MAX_LENGTH) throw new Error("Invalid array length");\n buffer = reallocUnsafe(buffer, (index + 1) << alignof());\n this.buffer_ = buffer;\n this.length_ = index + 1;\n }\n // store(changetype(buffer) + (index << alignof()), value, HEADER_SIZE_AB);\n storeUnsafe(buffer, index, value);\n }\n\n includes(searchElement: T, fromIndex: i32 = 0): bool {\n var length = this.length_;\n if (length == 0 || fromIndex >= length) return false;\n if (fromIndex < 0) fromIndex = max(length + fromIndex, 0);\n var buffer = this.buffer_;\n while (fromIndex < length) {\n if (loadUnsafe(buffer, fromIndex) == searchElement) return true;\n ++fromIndex;\n }\n return false;\n }\n\n indexOf(searchElement: T, fromIndex: i32 = 0): i32 {\n var length = this.length_;\n if (length == 0 || fromIndex >= length) return -1;\n if (fromIndex < 0) fromIndex = max(length + fromIndex, 0);\n var buffer = this.buffer_;\n while (fromIndex < length) {\n if (loadUnsafe(buffer, fromIndex) == searchElement) return fromIndex;\n ++fromIndex;\n }\n return -1;\n }\n\n lastIndexOf(searchElement: T, fromIndex: i32 = this.length_): i32 {\n var length = this.length_;\n if (length == 0) return -1;\n if (fromIndex < 0) fromIndex = length + fromIndex; // no need to clamp\n else if (fromIndex >= length) fromIndex = length - 1;\n var buffer = this.buffer_;\n while (fromIndex >= 0) { // ^\n if (loadUnsafe(buffer, fromIndex) == searchElement) return fromIndex;\n --fromIndex;\n }\n return -1;\n }\n\n push(element: T): i32 {\n var length = this.length_;\n var buffer = this.buffer_;\n var capacity = buffer.byteLength >>> alignof();\n var newLength = length + 1; // safe only if length is checked\n if (length >= capacity) {\n const MAX_LENGTH = MAX_BLENGTH >>> alignof();\n if (length >= MAX_LENGTH) throw new Error("Invalid array length");\n buffer = reallocUnsafe(buffer, newLength << alignof());\n this.buffer_ = buffer;\n }\n this.length_ = newLength;\n storeUnsafe(buffer, length, element);\n return newLength;\n }\n\n pop(): T {\n var length = this.length_;\n if (length < 1) throw new RangeError("Array is empty");\n var element = loadUnsafe(this.buffer_, --length);\n this.length_ = length;\n return element;\n }\n\n reduce(\n callbackfn: (previousValue: U, currentValue: T, currentIndex: i32, array: Array) => U,\n initialValue: U\n ): U {\n var accum = initialValue;\n var buffer = this.buffer_;\n for (let index = 0, toIndex = this.length_; index < toIndex && index < this.length_; ++index) {\n accum = callbackfn(accum, loadUnsafe(buffer, index), index, this);\n }\n return accum;\n }\n\n shift(): T {\n var length = this.length_;\n if (length < 1) throw new RangeError("Array is empty");\n var buffer = this.buffer_;\n var element = loadUnsafe(buffer, 0);\n var lastIndex = length - 1;\n move_memory(\n changetype(buffer) + HEADER_SIZE_AB,\n changetype(buffer) + HEADER_SIZE_AB + sizeof(),\n lastIndex << alignof()\n );\n storeUnsafe(buffer, lastIndex, isReference() ? null : 0);\n this.length_ = lastIndex;\n return element;\n }\n\n some(callbackfn: (element: T, index: i32, array: Array) => bool): bool {\n var buffer = this.buffer_;\n for (let index = 0, toIndex = this.length_; index < toIndex && index < this.length_; ++index) {\n if (callbackfn(loadUnsafe(buffer, index), index, this)) return true;\n }\n return false;\n }\n\n unshift(element: T): i32 {\n var buffer = this.buffer_;\n var capacity = buffer.byteLength >>> alignof();\n var length = this.length_;\n var newLength = length + 1; // safe only if length is checked\n if (length >= capacity) {\n const MAX_LENGTH = MAX_BLENGTH >>> alignof();\n if (length >= MAX_LENGTH) throw new Error("Invalid array length");\n buffer = reallocUnsafe(buffer, newLength << alignof());\n capacity = buffer.byteLength >>> alignof();\n this.buffer_ = buffer;\n }\n move_memory(\n changetype(buffer) + HEADER_SIZE_AB + sizeof(),\n changetype(buffer) + HEADER_SIZE_AB,\n (capacity - 1) << alignof()\n );\n storeUnsafe(buffer, 0, element);\n this.length_ = newLength;\n return newLength;\n }\n\n slice(begin: i32 = 0, end: i32 = i32.MAX_VALUE): Array {\n var length = this.length_;\n if (begin < 0) begin = max(length + begin, 0);\n else if (begin > length) begin = length;\n if (end < 0) end = length + end; // no need to clamp\n else if (end > length) end = length;\n if (end < begin) end = begin; // ^\n var newLength = end - begin;\n assert(newLength >= 0);\n var sliced = new Array(newLength);\n if (newLength) {\n move_memory(\n changetype(sliced.buffer_) + HEADER_SIZE_AB,\n changetype(this.buffer_) + HEADER_SIZE_AB + (begin << alignof()),\n newLength << alignof()\n );\n }\n return sliced;\n }\n\n splice(start: i32, deleteCount: i32 = i32.MAX_VALUE): void {\n if (deleteCount < 1) return;\n var length = this.length_;\n if (start < 0) start = max(length + start, 0);\n if (start >= length) return;\n deleteCount = min(deleteCount, length - start);\n var buffer = this.buffer_;\n move_memory(\n changetype(buffer) + HEADER_SIZE_AB + (start << alignof()),\n changetype(buffer) + HEADER_SIZE_AB + ((start + deleteCount) << alignof()),\n deleteCount << alignof()\n );\n this.length_ = length - deleteCount;\n }\n\n reverse(): Array {\n var buffer = this.buffer_;\n for (let front = 0, back = this.length_ - 1; front < back; ++front, --back) {\n let temp = loadUnsafe(buffer, front);\n storeUnsafe(buffer, front, loadUnsafe(buffer, back));\n storeUnsafe(buffer, back, temp);\n }\n return this;\n }\n\n sort(comparator: (a: T, b: T) => i32 = defaultComparator()): Array {\n var length = this.length_;\n if (length <= 1) return this;\n var buffer = this.buffer_;\n if (length == 2) {\n let a = loadUnsafe(buffer, 1); // a = arr[1]\n let b = loadUnsafe(buffer, 0); // b = arr[0]\n if (comparator(a, b) < 0) {\n storeUnsafe(buffer, 1, b); // arr[1] = b;\n storeUnsafe(buffer, 0, a); // arr[0] = a;\n }\n return this;\n }\n return length < 256\n ? insertionSort(this, comparator)\n : weakHeapSort(this, comparator);\n }\n}\n',arraybuffer:'import {\n HEADER_SIZE,\n MAX_BLENGTH,\n allocUnsafe\n} from "./internal/arraybuffer";\n\n@sealed\nexport class ArrayBuffer {\n\n readonly byteLength: i32; // capped to [0, MAX_LENGTH]\n\n constructor(length: i32) {\n if (length > MAX_BLENGTH) throw new RangeError("Invalid array buffer length");\n var buffer = allocUnsafe(length);\n set_memory(changetype(buffer) + HEADER_SIZE, 0, length);\n return buffer;\n }\n\n slice(begin: i32 = 0, end: i32 = MAX_BLENGTH): ArrayBuffer {\n var len = this.byteLength;\n if (begin < 0) begin = max(len + begin, 0);\n else begin = min(begin, len);\n if (end < 0) end = max(len + end, 0);\n else end = min(end, len);\n var newLen = max(end - begin, 0);\n var buffer = allocUnsafe(newLen);\n move_memory(changetype(buffer) + HEADER_SIZE, changetype(this) + HEADER_SIZE + begin, newLen);\n return buffer;\n }\n}\n\nexport declare interface FastArray {}\n\nexport declare interface ArrayBufferView {\n readonly buffer: ArrayBuffer;\n readonly byteOffset: i32;\n readonly byteLength: i32;\n readonly length: i32;\n}\n',builtins:"export declare function isInteger(value?: T): bool;\n\nexport declare function isFloat(value?: T): bool;\n\nexport declare function isReference(value?: T): bool;\n\nexport declare function isString(value?: T): bool;\n\nexport declare function isArray(value?: T): bool;\n\nexport const NaN: f64 = 0 / 0;\n\nexport const Infinity: f64 = 1 / 0;\n\nexport function isNaN(value: T): bool {\n return isFloat(value)\n ? sizeof() == 32\n ? (reinterpret(value) & -1 >>> 1) > 0xFF << 23\n : (reinterpret(value) & -1 >>> 1) > 0x7FF << 52\n : false;\n}\n\nexport function isFinite(value: T): bool {\n return isFloat(value)\n ? sizeof() == 32\n ? (reinterpret(value) & -1 >>> 1) < 0xFF << 23\n : (reinterpret(value) & -1 >>> 1) < 0x7FF << 52\n : true;\n}\n\nexport declare function clz(value: T): T;\n\nexport declare function ctz(value: T): T;\n\nexport declare function popcnt(value: T): T;\n\nexport declare function rotl(value: T, shift: T): T;\n\nexport declare function rotr(value: T, shift: T): T;\n\nexport declare function abs(value: T): T;\n\nexport declare function max(left: T, right: T): T;\n\nexport declare function min(left: T, right: T): T;\n\nexport declare function ceil(value: T): T;\n\nexport declare function floor(value: T): T;\n\nexport declare function copysign(left: T, right: T): T;\n\nexport declare function nearest(value: T): T;\n\nexport declare function reinterpret(value: void): T;\n\nexport declare function sqrt(value: T): T;\n\nexport declare function trunc(value: T): T;\n\nexport declare function load(offset: usize, constantOffset?: usize): T;\n\nexport declare function store(offset: usize, value: void, constantOffset?: usize): T;\n\nexport declare function sizeof(): usize; // | u32 / u64\n\nexport declare function alignof(): usize; // | u32 / u64\n\nexport declare function offsetof(fieldName?: string): usize; // | u32 / u64\n\nexport declare function select(ifTrue: T, ifFalse: T, condition: bool): T;\n\nexport declare function unreachable(): void;\n\nexport declare function current_memory(): i32;\n\nexport declare function grow_memory(pages: i32): i32;\n\n// export declare function move_memory(dest: usize, src: usize: n: usize): void;\n\n// export declare function set_memory(dest: usize, value: u32, n: usize): void;\n\nexport declare function changetype(value: void): T;\n\nexport declare function assert(isTrueish: T, message?: string): T;\n\nexport declare function abort(\n message?: string | null,\n fileName?: string | null,\n lineNumber?: u32,\n columnNumber?: u32\n): void;\n\nexport declare function i8(value: void): i8;\nexport namespace i8 {\n export const MIN_VALUE: i8 = -128;\n export const MAX_VALUE: i8 = 127;\n}\n\nexport declare function i16(value: void): i16;\nexport namespace i16 {\n export const MIN_VALUE: i16 = -32768;\n export const MAX_VALUE: i16 = 32767;\n}\n\nexport declare function i32(value: void): i32;\nexport namespace i32 {\n export const MIN_VALUE: i32 = -2147483648;\n export const MAX_VALUE: i32 = 2147483647;\n}\n\nexport declare function i64(value: void): i64;\nexport namespace i64 {\n export const MIN_VALUE: i64 = -9223372036854775808;\n export const MAX_VALUE: i64 = 9223372036854775807;\n}\n\nexport declare function isize(value: void): isize;\nexport namespace isize {\n export const MIN_VALUE: isize = sizeof() == sizeof()\n ? -2147483648\n : -9223372036854775808;\n export const MAX_VALUE: isize = sizeof() == sizeof()\n ? 2147483647\n : 9223372036854775807;\n}\n\nexport declare function u8(value: void): u8;\nexport namespace u8 {\n export const MIN_VALUE: u8 = 0;\n export const MAX_VALUE: u8 = 255;\n}\n\nexport declare function u16(value: void): u16;\nexport namespace u16 {\n export const MIN_VALUE: u16 = 0;\n export const MAX_VALUE: u16 = 65535;\n}\n\nexport declare function u32(value: void): u32;\nexport namespace u32 {\n export const MIN_VALUE: u32 = 0;\n export const MAX_VALUE: u32 = 4294967295;\n}\n\nexport declare function u64(value: void): u64;\nexport namespace u64 {\n export const MIN_VALUE: u64 = 0;\n export const MAX_VALUE: u64 = 18446744073709551615;\n}\n\nexport declare function usize(value: void): usize;\nexport namespace usize {\n export const MIN_VALUE: usize = 0;\n export const MAX_VALUE: usize = sizeof() == sizeof()\n ? 4294967295\n : 18446744073709551615;\n}\n\nexport declare function bool(value: void): bool;\nexport namespace bool {\n export const MIN_VALUE: bool = false;\n export const MAX_VALUE: bool = true;\n}\n\nexport declare function f32(value: void): f32;\nexport namespace f32 {\n export const MIN_VALUE = reinterpret