Skip to content

Commit

Permalink
Improved support for BigInts
Browse files Browse the repository at this point in the history
  • Loading branch information
Qwokka committed Sep 6, 2019
1 parent 56a1747 commit c02f59f
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 80 deletions.
77 changes: 11 additions & 66 deletions content/cetus.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ class Cetus {
}

setSpeedhackMultiplier(multiplier) {
if (isNaN(multiplier)) {
if (bigintIsNaN(multiplier)) {
return;
}

Expand Down Expand Up @@ -265,7 +265,7 @@ class Cetus {
this._resolveFunctions();
}

if (!isNaN(funcIndex) && typeof this._functions[funcIndex] === "object") {
if (!bigintIsNaN(funcIndex) && typeof this._functions[funcIndex] === "object") {
return this._functions[funcIndex];
}
}
Expand Down Expand Up @@ -346,69 +346,14 @@ const colorLog = function(msg) {
"color: #ff2424; background: #fff; padding:5px 0;");
};

const isValidMemType = function(memType) {
switch (memType) {
case "i8":
case "i16":
case "i32":
case "f32":
case "i64":
case "f64":
return true;
default:
return false;
}
};

const indexToRealAddress = function(memIndex, memType) {
const memSize = getElementSize(memType);

return memIndex * memSize;
};

const realAddressToIndex = function(memAddr, memType) {
const memSize = getElementSize(memType);

return Math.floor(memAddr / memSize);
};

const getElementSize = function(type) {
let indexSize;

switch (type) {
case "i8":
indexSize = 1;
break;
case "i16":
indexSize = 2;
break;
case "i32":
indexSize = 4;
break;
case "i64":
indexSize = 8;
break;
case "f32":
indexSize = 4;
break;
case "f64":
indexSize = 8;
break;
default:
throw new Error("Invalid memory type " + type + " in getElementSize()");
}

return indexSize;
};

// Event will be captured by content.js and passed along to the extension
const sendExtensionMessage = function(type, msg) {
const msgBody = {
type: type,
body: msg
};

const evt = new CustomEvent("cetusMsgIn", { detail: JSON.stringify(msgBody) } );
const evt = new CustomEvent("cetusMsgIn", { detail: bigintJsonStringify(msgBody) } );

window.dispatchEvent(evt);
};
Expand All @@ -419,7 +364,7 @@ window.addEventListener("cetusMsgOut", function(msgRaw) {
return;
}

const msg = JSON.parse(msgRaw.detail);
const msg = bigintJsonParse(msgRaw.detail);

const msgType = msg.type;
const msgBody = msg.body;
Expand Down Expand Up @@ -498,7 +443,7 @@ window.addEventListener("cetusMsgOut", function(msgRaw) {
const modifyValue = msgBody.memValue;
const modifyMemType = msgBody.memType;

if (isNaN(modifyIndex) || isNaN(modifyValue) || !isValidMemType(modifyMemType)) {
if (bigintIsNaN(modifyIndex) || bigintIsNaN(modifyValue) || !isValidMemType(modifyMemType)) {
return;
}

Expand All @@ -516,11 +461,11 @@ window.addEventListener("cetusMsgOut", function(msgRaw) {
const watchSize = msgBody.size;
const watchFlags = msgBody.flags;

if (isNaN(watchIndex) ||
isNaN(watchAddr) ||
isNaN(watchValue) ||
isNaN(watchSize) ||
isNaN(watchFlags)) {
if (bigintIsNaN(watchIndex) ||
bigintIsNaN(watchAddr) ||
bigintIsNaN(watchValue) ||
bigintIsNaN(watchSize) ||
bigintIsNaN(watchFlags)) {
return;
}

Expand Down Expand Up @@ -549,7 +494,7 @@ window.addEventListener("cetusMsgOut", function(msgRaw) {
case "shEnable":
const shMultiplier = msgBody.multiplier;

if (isNaN(shMultiplier)) {
if (bigintIsNaN(shMultiplier)) {
return;
}

Expand Down
3 changes: 2 additions & 1 deletion content/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ limitations under the License.

// Listen for messages from web page and pass them along to extension
window.addEventListener("cetusMsgIn", function(msg) {
chrome.runtime.sendMessage(JSON.parse(msg.detail));
chrome.runtime.sendMessage(msg.detail);
}, false);

// Listn for messages from extension and pass them along to web page
Expand Down Expand Up @@ -91,6 +91,7 @@ storageGet("savedPatches", function(result) {
}
}

injectScript("/shared/utils.js");
injectScript("/shared/wail.min.js/wail.min.js");
injectScript("/content/thirdparty/stacktrace/stacktrace.min.js");
injectScript("/content/cetus.js");
Expand Down
20 changes: 14 additions & 6 deletions extension/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ class BackgroundExtension {

// TODO Actually handle port closed error
try {
this._popupChannel.postMessage(msg);
const msgStr = bigintJsonStringify(msg);

this._popupChannel.postMessage(msgStr);
}
catch (err) {
return;
Expand All @@ -111,7 +113,9 @@ class BackgroundExtension {

passthruPopupMessage(msg) {
if (this._popupChannel !== null) {
this._popupChannel.postMessage(msg);
const msgStr = bigintJsonStringify(msg);

this._popupChannel.postMessage(msgStr);
}
}

Expand Down Expand Up @@ -348,7 +352,7 @@ const popupMessageListener = function(msg) {
case "removeBookmark":
const removeMemAddr = msgBody.memAddr;

if (isNaN(removeMemAddr)) {
if (bigintIsNaN(removeMemAddr)) {
return;
}

Expand Down Expand Up @@ -402,7 +406,7 @@ const popupMessageListener = function(msg) {
const funcIndex = msgBody.index;
const lineNum = msgBody.lineNum;

if (typeof funcIndex !== "string" || isNaN(funcIndex) || funcIndex === "") {
if (typeof funcIndex !== "string" || bigintIsNaN(funcIndex) || funcIndex === "") {
return;
}

Expand Down Expand Up @@ -454,7 +458,11 @@ bgExtension = new BackgroundExtension();

// This listener receives commands directly from the page
// As such, all inputs should be treated as untrusted
chrome.runtime.onMessage.addListener(function(msg) {
chrome.runtime.onMessage.addListener(function(msgRaw) {
const msg = bigintJsonParse(msgRaw);

console.log(msg);

const msgType = msg.type;
const msgBody = msg.body;

Expand Down Expand Up @@ -527,7 +535,7 @@ chrome.runtime.onMessage.addListener(function(msg) {

// All keys in resultObject should be numeric. If not, toss the whole thing
for (let entry in resultObject) {
if (isNaN(entry)) {
if (bigintIsNaN(entry)) {
return;
}
}
Expand Down
4 changes: 3 additions & 1 deletion extension/extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,9 @@ class PopupExtension {
}
}

const bgMessageListener = function(msg) {
const bgMessageListener = function(msgRaw) {
const msg = bigintJsonParse(msgRaw);

const type = msg.type;
const msgBody = msg.body;

Expand Down
12 changes: 6 additions & 6 deletions extension/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ document.getElementById('searchForm').onsubmit = function(e) {
let lowerAddr = form.lower.value;
let upperAddr = form.upper.value;

if (lowerAddr == '' || isNaN(lowerAddr)) {
if (lowerAddr == '' || bigintIsNaN(lowerAddr)) {
lowerAddr = 0;
}

if (upperAddr == '' || isNaN(upperAddr)) {
if (upperAddr == '' || bigintIsNaN(upperAddr)) {
upperAddr = 0xffffffff;
}

Expand All @@ -66,7 +66,7 @@ document.getElementById('searchForm').onsubmit = function(e) {

extension.searchMemType = memType;

if (isNaN(param) || param == '') {
if (bigintIsNaN(param) || param == '') {
param = null;
}

Expand Down Expand Up @@ -486,7 +486,7 @@ const updateSearchResults = function(resultCount, resultObject, resultMemType) {
const value = resultObject[index];

// Validate both index and value are numeric
if (isNaN(index) || isNaN(value)) {
if (bigintIsNaN(index) || bigintIsNaN(value)) {
continue;
}

Expand Down Expand Up @@ -1040,7 +1040,7 @@ importPatchButton.addEventListener('click', importPatch);
const updateSpeedhackGauge = function() {
const range = document.getElementById("shRange");

if (isNaN(range.value)) {
if (bigintIsNaN(range.value)) {
return;
}

Expand Down Expand Up @@ -1100,7 +1100,7 @@ document.getElementById('toggleSpeedhack').onclick = function(e) {
const range = document.getElementById('shRange');
const multiplier = range.value;

if (isNaN(multiplier)) {
if (bigintIsNaN(multiplier)) {
return;
}

Expand Down
1 change: 1 addition & 0 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"content/cetus.js",
"content/init.js",
"shared/wail.min.js/wail.min.js",
"shared/utils.js",
"content/thirdparty/stacktrace/stacktrace.min.js"
],
"browser_action": {
Expand Down
30 changes: 30 additions & 0 deletions shared/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,33 @@ const downloadText = function(filename, text) {

document.body.removeChild(anchorElement);
};


// BigInt capable JSON handlers from
// https://golb.hplar.ch/2018/09/javascript-bigint.html
const bigintJsonParse = function(jsonString) {
return JSON.parse(jsonString, (key, value) => {
if (typeof value === 'string' && /^\d+n$/.test(value)) {
return BigInt(value.slice(0, -1));
}
return value;
});
}

const bigintJsonStringify = function(jsonObject) {
return JSON.stringify(jsonObject, (key, value) => {
if (typeof value === 'bigint') {
return value.toString() + 'n';
} else {
return value;
}
});
}

const bigintIsNaN = function(value) {
if (typeof value === "bigint") {
return false;
}

return isNaN(value);
}

0 comments on commit c02f59f

Please sign in to comment.