Skip to content

Commit

Permalink
fixes to indexing
Browse files Browse the repository at this point in the history
  • Loading branch information
Richard authored and Richard committed Dec 28, 2020
1 parent 4509217 commit 8956a22
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 26 deletions.
2 changes: 1 addition & 1 deletion crates/cargo-webassembly/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cargo-webassembly"
version = "0.0.17"
version = "0.0.18"
authors = ["Richard Anaya"]
edition = "2018"
license = "MIT OR Apache-2.0"
Expand Down
40 changes: 20 additions & 20 deletions crates/cargo-webassembly/src/template/js-wasm.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ class Index {
}

toNum() {
return ((this.generation & 0xffffffff) << 32) | (this.index & 0xffffffff);
return Number(BigInt(this.generation) << BigInt(32) | BigInt(this.index));
}
}

Index.fromNum = function(n) {
let i = ((n & 0xffffffff00000000) >> 32) & 0xffffffff;
Index.fromNum = function (n) {
let i = Number(((BigInt(n) & BigInt(0xffffffff00000000)) >> BigInt(32)) & BigInt(0xffffffff));
let g = n & 0xffffffff;
return new Index(Number(g), Number(i));
return new Index(g, i);
};

class GenerationalArena {
Expand Down Expand Up @@ -121,7 +121,7 @@ window.JsWasm = {
arena.insert(typeof document != "undefined" ? document.body : null);
let context = {
functions: [
function(){
function () {
debugger;
}
],
Expand All @@ -130,15 +130,15 @@ window.JsWasm = {
utf8enc: new TextEncoder("utf-8"),
utf16dec: new TextDecoder("utf-16"),
utf16enc: new TextEncoder("utf-16"),
toCallbackArg: function(arg) {
toCallbackArg: function (arg) {
if (typeof arg === "object") {
return context.storeObject(arg);
}
return arg;
},
createCallback: function(cb) {
createCallback: function (cb) {
let fnHandleCallback = this.module.instance.exports.handle_callback;
return function() {
return function () {
const arg = arguments;
fnHandleCallback(
cb,
Expand All @@ -155,7 +155,7 @@ window.JsWasm = {
);
};
},
readCStringFromMemory: function(start) {
readCStringFromMemory: function (start) {
const data = new Uint8Array(this.module.instance.exports.memory.buffer);
const str = [];
let i = start;
Expand All @@ -175,14 +175,14 @@ window.JsWasm = {
memory.set(bytes, start);
return start;
},
readUtf8FromMemory: function(start, len) {
readUtf8FromMemory: function (start, len) {
const memory = new Uint8Array(
this.module.instance.exports.memory.buffer
);
const text = this.utf8dec.decode(memory.subarray(start, start + len));
return text;
},
writeUtf8ToMemory: function(str) {
writeUtf8ToMemory: function (str) {
const bytes = utf8enc.encode(str);
const len = bytes.length;
const start = this.module.instance.exports.malloc(len);
Expand All @@ -192,14 +192,14 @@ window.JsWasm = {
memory.set(bytes, start);
return start;
},
readUtf16FromMemory: function(start, len) {
readUtf16FromMemory: function (start, len) {
const memory = new Uint8Array(
this.module.instance.exports.memory.buffer
);
const text = this.utf16dec.decode(memory.subarray(start, start + len));
return text;
},
writeUtf16ToMemory: function(str) {
writeUtf16ToMemory: function (str) {
const bytes = utf16enc.encode(str);
const len = bytes.length;
const start = this.module.instance.exports.malloc(len);
Expand All @@ -218,28 +218,28 @@ window.JsWasm = {
let b = mem.slice(ptr + 4, ptr + 4 + length);
return new Uint8Array(b);
},
storeObject: function(obj) {
storeObject: function (obj) {
const index = this.objects.insert(obj);
return index.toNum();
},
getObject: function(handle) {
getObject: function (handle) {
return this.objects.get(Index.fromNum(handle));
},
releaseObject: function(handle) {
releaseObject: function (handle) {
this.objects.remove(Index.fromNum(handle));
}
};
return {
context,
abort() {
throw new Error("WebAssembly module aborted");
throw new Error("WebAssembly module aborted");
},
js_release(obj) {
context.releaseObject(obj);
},
js_register_function(start, len, utfByteLen) {
let functionBody;
if(utfByteLen === 16) {
if (utfByteLen === 16) {
functionBody = context.readUtf16FromMemory(start, len);
} else {
functionBody = context.readUtf8FromMemory(start, len);
Expand Down Expand Up @@ -280,7 +280,7 @@ window.JsWasm = {
}
};

document.addEventListener("DOMContentLoaded", function() {
document.addEventListener("DOMContentLoaded", function () {
const wasmScripts = document.querySelectorAll(
"script[type='application/wasm']"
);
Expand All @@ -295,7 +295,7 @@ document.addEventListener("DOMContentLoaded", function() {
});

if (window.WasmScriptComponents) {
window.WasmScriptComponents["js-wasm"] = function(e) {
window.WasmScriptComponents["js-wasm"] = function (e) {
return {
...e,
...JsWasm.createEnvironment()
Expand Down
Binary file modified examples/snake/example.wasm
Binary file not shown.
2 changes: 1 addition & 1 deletion examples/snake/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ impl Game {
});

request_animation_loop(|delta| match Game::instance().run(delta) {
Err(e) => error(&e.to_string()),
Err(e) => console_error(&e.to_string()),
_ => (),
});
}
Expand Down
6 changes: 3 additions & 3 deletions js-wasm.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ class Index {
}

toNum() {
return ((this.generation & 0xffffffff) << 32) | (this.index & 0xffffffff);
return Number(BigInt(this.generation) << BigInt(32) | BigInt(this.index));
}
}

Index.fromNum = function(n) {
let i = ((n & 0xffffffff00000000) >> 32) & 0xffffffff;
let i = Number(((BigInt(n) & BigInt(0xffffffff00000000)) >> BigInt(32)) & BigInt(0xffffffff));
let g = n & 0xffffffff;
return new Index(Number(g), Number(i));
return new Index(g, i);
};

class GenerationalArena {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "js-wasm",
"version": "0.2.14",
"version": "0.2.15",
"description": "Call JavaScript from WebAssembly ",
"main": "js-wasm.js",
"directories": {
Expand Down

0 comments on commit 8956a22

Please sign in to comment.