Skip to content

Commit

Permalink
Commit files to repo for distribution. *shrug*
Browse files Browse the repository at this point in the history
  • Loading branch information
kitlith committed Dec 9, 2019
1 parent 659bc3c commit 50dc6bc
Show file tree
Hide file tree
Showing 7 changed files with 240 additions and 5 deletions.
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ console_error_panic_hook = { version = "0.1.1", optional = true }
# Unfortunately, `wee_alloc` requires nightly Rust when targeting wasm for now.
wee_alloc = { version = "0.4.2", optional = true }

faerie = { path = "../faerie" }
target-lexicon = "0.9.0"
goblin = "0.1"
scroll = "0.10"
Expand All @@ -37,6 +36,10 @@ serde_derive = "^1.0.59"
version = "^0.2"
features = ["serde-serialize"]

[dependencies.faerie]
git = "https://github.com/kitlith/faerie"
branch = "mcorrupt_test"

[dev-dependencies]
wasm-bindgen-test = "0.2"

Expand Down
5 changes: 2 additions & 3 deletions pkg/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
*
!.gitignore
!*.user.js
README.md
package.json
2 changes: 1 addition & 1 deletion pkg/mcorrupt.user.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// @namespace Kitlith
// @match https://*.microcorruption.com/cpu/debugger
// @grant GM_getResourceURL
// @version 1.0
// @version 0.0.1
// @author Kitlith
// @require microcorruption_wasm.js
// @resource wasm microcorruption_wasm_bg.wasm
Expand Down
19 changes: 19 additions & 0 deletions pkg/microcorruption_wasm.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/* tslint:disable */
/**
* @param {string} name
* @param {Uint8Array} memory
* @param {any} symbols
* @returns {Uint8Array}
*/
export function gen_elf(name: string, memory: Uint8Array, symbols: any): Uint8Array;

/**
* If `module_or_path` is {RequestInfo}, makes a request and
* for everything else, calls `WebAssembly.instantiate` directly.
*
* @param {RequestInfo | BufferSource | WebAssembly.Module} module_or_path
*
* @returns {Promise<any>}
*/
export default function init (module_or_path: RequestInfo | BufferSource | WebAssembly.Module): Promise<any>;

208 changes: 208 additions & 0 deletions pkg/microcorruption_wasm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
(function() {
const __exports = {};
let wasm;

let WASM_VECTOR_LEN = 0;

let cachedTextEncoder = new TextEncoder('utf-8');

const encodeString = (typeof cachedTextEncoder.encodeInto === 'function'
? function (arg, view) {
return cachedTextEncoder.encodeInto(arg, view);
}
: function (arg, view) {
const buf = cachedTextEncoder.encode(arg);
view.set(buf);
return {
read: arg.length,
written: buf.length
};
});

let cachegetUint8Memory = null;
function getUint8Memory() {
if (cachegetUint8Memory === null || cachegetUint8Memory.buffer !== wasm.memory.buffer) {
cachegetUint8Memory = new Uint8Array(wasm.memory.buffer);
}
return cachegetUint8Memory;
}

function passStringToWasm(arg) {

let len = arg.length;
let ptr = wasm.__wbindgen_malloc(len);

const mem = getUint8Memory();

let offset = 0;

for (; offset < len; offset++) {
const code = arg.charCodeAt(offset);
if (code > 0x7F) break;
mem[ptr + offset] = code;
}

if (offset !== len) {
if (offset !== 0) {
arg = arg.slice(offset);
}
ptr = wasm.__wbindgen_realloc(ptr, len, len = offset + arg.length * 3);
const view = getUint8Memory().subarray(ptr + offset, ptr + len);
const ret = encodeString(arg, view);

offset += ret.written;
}

WASM_VECTOR_LEN = offset;
return ptr;
}

function passArray8ToWasm(arg) {
const ptr = wasm.__wbindgen_malloc(arg.length * 1);
getUint8Memory().set(arg, ptr / 1);
WASM_VECTOR_LEN = arg.length;
return ptr;
}

const heap = new Array(32);

heap.fill(undefined);

heap.push(undefined, null, true, false);

let stack_pointer = 32;

function addBorrowedObject(obj) {
if (stack_pointer == 1) throw new Error('out of js stack');
heap[--stack_pointer] = obj;
return stack_pointer;
}

let cachegetInt32Memory = null;
function getInt32Memory() {
if (cachegetInt32Memory === null || cachegetInt32Memory.buffer !== wasm.memory.buffer) {
cachegetInt32Memory = new Int32Array(wasm.memory.buffer);
}
return cachegetInt32Memory;
}

function getArrayU8FromWasm(ptr, len) {
return getUint8Memory().subarray(ptr / 1, ptr / 1 + len);
}
/**
* @param {string} name
* @param {Uint8Array} memory
* @param {any} symbols
* @returns {Uint8Array}
*/
__exports.gen_elf = function(name, memory, symbols) {
const retptr = 8;
try {
const ret = wasm.gen_elf(retptr, passStringToWasm(name), WASM_VECTOR_LEN, passArray8ToWasm(memory), WASM_VECTOR_LEN, addBorrowedObject(symbols));
const memi32 = getInt32Memory();
const v0 = getArrayU8FromWasm(memi32[retptr / 4 + 0], memi32[retptr / 4 + 1]).slice();
wasm.__wbindgen_free(memi32[retptr / 4 + 0], memi32[retptr / 4 + 1] * 1);
return v0;
} finally {
heap[stack_pointer++] = undefined;
}
};

function getObject(idx) { return heap[idx]; }

let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });

cachedTextDecoder.decode();

function getStringFromWasm(ptr, len) {
return cachedTextDecoder.decode(getUint8Memory().subarray(ptr, ptr + len));
}

let heap_next = heap.length;

function addHeapObject(obj) {
if (heap_next === heap.length) heap.push(heap.length + 1);
const idx = heap_next;
heap_next = heap[idx];

heap[idx] = obj;
return idx;
}

function dropObject(idx) {
if (idx < 36) return;
heap[idx] = heap_next;
heap_next = idx;
}

function takeObject(idx) {
const ret = getObject(idx);
dropObject(idx);
return ret;
}

function init(module) {

let result;
const imports = {};
imports.wbg = {};
imports.wbg.__wbindgen_json_serialize = function(arg0, arg1) {
const obj = getObject(arg1);
const ret = JSON.stringify(obj === undefined ? null : obj);
const ret0 = passStringToWasm(ret);
const ret1 = WASM_VECTOR_LEN;
getInt32Memory()[arg0 / 4 + 0] = ret0;
getInt32Memory()[arg0 / 4 + 1] = ret1;
};
imports.wbg.__wbindgen_string_new = function(arg0, arg1) {
const ret = getStringFromWasm(arg0, arg1);
return addHeapObject(ret);
};
imports.wbg.__wbindgen_rethrow = function(arg0) {
throw takeObject(arg0);
};

if ((typeof URL === 'function' && module instanceof URL) || typeof module === 'string' || (typeof Request === 'function' && module instanceof Request)) {

const response = fetch(module);
if (typeof WebAssembly.instantiateStreaming === 'function') {
result = WebAssembly.instantiateStreaming(response, imports)
.catch(e => {
return response
.then(r => {
if (r.headers.get('Content-Type') != 'application/wasm') {
console.warn("`WebAssembly.instantiateStreaming` failed because your server does not serve wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n", e);
return r.arrayBuffer();
} else {
throw e;
}
})
.then(bytes => WebAssembly.instantiate(bytes, imports));
});
} else {
result = response
.then(r => r.arrayBuffer())
.then(bytes => WebAssembly.instantiate(bytes, imports));
}
} else {

result = WebAssembly.instantiate(module, imports)
.then(result => {
if (result instanceof WebAssembly.Instance) {
return { instance: result, module };
} else {
return result;
}
});
}
return result.then(({instance, module}) => {
wasm = instance.exports;
init.__wbindgen_wasm_module = module;

return wasm;
});
}

self.wasm_bindgen = Object.assign(init, __exports);

})();
6 changes: 6 additions & 0 deletions pkg/microcorruption_wasm_bg.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/* tslint:disable */
export const memory: WebAssembly.Memory;
export function gen_elf(a: number, b: number, c: number, d: number, e: number, f: number): void;
export function __wbindgen_malloc(a: number): number;
export function __wbindgen_realloc(a: number, b: number, c: number): number;
export function __wbindgen_free(a: number, b: number): void;
Binary file added pkg/microcorruption_wasm_bg.wasm
Binary file not shown.

0 comments on commit 50dc6bc

Please sign in to comment.