Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small JSHint fixes #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 53 additions & 52 deletions loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,23 @@ var Module = null;
}

function img(src) {
var img = new Image();
img.src = src;
return img;
var _img = new Image();
_img.src = src;
return _img;
}

// yea, this is a hack
if (/archive\.org$/.test(document.location.hostname)) {
var images = { ia: img("/images/ialogo.png"),
mame: img("/images/mame.png"),
mess: img("/images/mess.png"),
dosbox: img("/images/dosbox.png")
};
} else {
images = { ia: img("other_logos/ia-logo-150x150.png"),
mame: img("other_logos/mame.png"),
mess: img("other_logos/mess.png"),
dosbox: img("other_logos/dosbox.png")
};
}
var images = (/archive\.org$/.test(document.location.hostname)) ?
{ ia: img("/images/ialogo.png"),
mame: img("/images/mame.png"),
mess: img("/images/mess.png"),
dosbox: img("/images/dosbox.png")
} : {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't merge that; the indentation is terrible. The colon of the tertiary operator must line up with the question mark.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I'm used to a different kind of indentation. I'll get it fixed.

ia: img("other_logos/ia-logo-150x150.png"),
mame: img("other_logos/mame.png"),
mess: img("other_logos/mess.png"),
dosbox: img("other_logos/dosbox.png")
};
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, the closing brace must line up with the opening brace.


function updateLogo() {
if (emulator_logo) {
Expand All @@ -57,7 +55,7 @@ var Module = null;
if (!audio_ctx) {
return false;
}
var sample = new audio_ctx;
var sample = new audio_ctx();
return sample.sampleRate.toString();
}());

Expand Down Expand Up @@ -97,7 +95,7 @@ var Module = null;
modulecfg = JSON.parse(data);
var mame = modulecfg &&
'arcade' in modulecfg &&
parseInt(modulecfg['arcade'], 10);
parseInt(modulecfg.arcade, 10);
var get_files;

if (module && module.indexOf("dosbox") === 0) {
Expand All @@ -120,7 +118,7 @@ var Module = null;
throw new Error("Unknown module type "+ module +"; cannot configure the emulator.");
}

var nr = modulecfg['native_resolution'];
var nr = modulecfg.native_resolution;
config_args = [cfgr.emulatorJS(get_js_url(modulecfg.js_filename)),
cfgr.locateAdditionalEmulatorJS(locateAdditionalJS),
cfgr.fileSystemKey(game),
Expand Down Expand Up @@ -181,21 +179,22 @@ var Module = null;
// first get the urls
var urls = [], files = [];
var len = metadata.documentElement.childNodes.length, i;
var node;
for (i = 0; i < len; i++) {
var node = metadata.documentElement.childNodes[i];
node = metadata.documentElement.childNodes[i];
var m = node.nodeName.match(/^dosbox_drive_[a-zA-Z]$/);
if (m) {
urls.push(node);
}
}

// and a count, then fetch them in
var len = urls.length;
len = urls.length;
for (i = 0; i < len; i++) {
var node = urls[i],
drive = node.nodeName.split('_')[2],
title = 'Game File ('+ (i+1) +' of '+ (game ? len+1 : len) +')',
url = get_zip_url(node.textContent);
node = urls[i],
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, that converts some of these into global variables.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, but that variable was getting redefined on the same scope. If it's getting used only in that scope, maybe it should be deleted before defining it again.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In a perfect world we'd use let instead of var, but we can move the var node out of the loop and still have a var on the others.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't want to use let because as it's ES6 only, it'd break compatibiity with older browsers.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly.

On the other hand, compatibility with older browsers is already rather spotty...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, and most current browsers already implement let correctly, maybe we can use it...

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nah, let's avoid making it worse.

drive = node.nodeName.split('_')[2],
title = 'Game File ('+ (i+1) +' of '+ (game ? len+1 : len) +')',
url = get_zip_url(node.textContent);
files.push(cfgr.mountZip(drive, cfgr.fetchFile(title, url)));
}

Expand All @@ -211,7 +210,7 @@ var Module = null;

function get_mess_files(cfgr, metadata, modulecfg) {
var files = [],
bios_files = modulecfg['bios_filenames'];
bios_files = modulecfg.bios_filenames;
bios_files.forEach(function (fname, i) {
if (fname) {
var title = "Bios File ("+ (i+1) +" of "+ bios_files.length +")";
Expand All @@ -223,15 +222,15 @@ var Module = null;
files.push(cfgr.mountFile('/'+ get_game_name(game),
cfgr.fetchFile("Game File",
get_zip_url(game))));
files.push(cfgr.mountFile('/'+ modulecfg['driver'] + '.cfg',
files.push(cfgr.mountFile('/'+ modulecfg.driver + '.cfg',
cfgr.fetchOptionalFile("CFG File",
get_other_emulator_config_url(module))));
return files;
}

function get_mame_files(cfgr, metadata, modulecfg) {
var files = [],
bios_files = modulecfg['bios_filenames'];
bios_files = modulecfg.bios_filenames;
bios_files.forEach(function (fname, i) {
if (fname) {
var title = "Bios File ("+ (i+1) +" of "+ bios_files.length +")";
Expand All @@ -243,7 +242,7 @@ var Module = null;
files.push(cfgr.mountFile('/'+ get_game_name(game),
cfgr.fetchFile("Game File",
get_zip_url(game))));
files.push(cfgr.mountFile('/'+ modulecfg['driver'] + '.cfg',
files.push(cfgr.mountFile('/'+ modulecfg.driver + '.cfg',
cfgr.fetchOptionalFile("CFG File",
get_other_emulator_config_url(module))));
return files;
Expand Down Expand Up @@ -299,9 +298,9 @@ var Module = null;
/**
* BaseLoader
*/
function BaseLoader() {
var BaseLoader = function() {
return Array.prototype.reduce.call(arguments, extend);
}
};
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should probably move away from the "var = function {};" style


BaseLoader.canvas = function (id) {
var elem = id instanceof Element ? id : document.getElementById(id);
Expand Down Expand Up @@ -365,11 +364,11 @@ var Module = null;
return { title: title, data: data };
};

function DosBoxLoader() {
var DosBoxLoader = function() {
var config = Array.prototype.reduce.call(arguments, extend);
config.emulator_arguments = build_dosbox_arguments(config.emulatorStart, config.files);
return config;
}
};
DosBoxLoader.__proto__ = BaseLoader;

DosBoxLoader.startExe = function (path) {
Expand Down Expand Up @@ -404,14 +403,15 @@ var Module = null;
/**
* JSMAMELoader
*/
function JSMAMELoader() {
var JSMAMELoader = function() {
var config = Array.prototype.reduce.call(arguments, extend);
config.emulator_arguments = build_mame_arguments(config.muted, config.mess_driver,
config.nativeResolution, config.sample_rate,
config.extra_mess_args);
config.needs_jsmess_webaudio = true;
return config;
}
};

JSMAMELoader.__proto__ = BaseLoader;

JSMAMELoader.driver = function (driver) {
Expand Down Expand Up @@ -871,7 +871,7 @@ var Module = null;
resolve();
}
};
};
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rather than mixing and matching.


var resizeCanvas = function (canvas, scale, resolution, aspectRatio) {
if (scale && resolution) {
Expand Down Expand Up @@ -1021,7 +1021,7 @@ var Module = null;
} else if ('onwebkitfullscreenchange' in document) {
document.addEventListener('webkitfullscreenchange', fullScreenChangeHandler);
}
};
}

this.requestFullScreen = function () {
Module.requestFullScreen(1, 0);
Expand Down Expand Up @@ -1054,14 +1054,14 @@ var Module = null;
}
});
}
};
}

/**
* misc
*/
function BFSOpenZip(loadedData) {
return new BrowserFS.FileSystem.ZipFS(loadedData);
};
}

// This is such a hack. We're not calling the BrowserFS api
// "correctly", so we have to synthesize these flags ourselves
Expand Down Expand Up @@ -1119,7 +1119,7 @@ var Module = null;
fs.readFileSync(dosboxConfPath, null, flag_r),
null, flag_w, 0x1a4);
}
};
}

function extend(a, b) {
if (a === null)
Expand Down Expand Up @@ -1170,7 +1170,7 @@ var Module = null;
gain_node = context.createGain();
gain_node.gain.value = 1.0;
gain_node.connect(context.destination);
};
}

function set_mastervolume (
// even though it's 'attenuation' the value is negative, so...
Expand All @@ -1193,7 +1193,7 @@ var Module = null;
gain_web_audio = +1;

gain_node.gain.value = gain_web_audio;
};
}

function update_audio_stream (
pBuffer, // pointer into emscripten heap. int16 samples
Expand Down Expand Up @@ -1236,7 +1236,7 @@ var Module = null;
pending_buffers.push(buffer);

tick();
};
}

function tick () {
// Note: this is the time the web audio mixer has mixed up to,
Expand All @@ -1261,20 +1261,20 @@ var Module = null;
// chunk of prebuffered audio. At that point it seems like
// JSMESS never catches up and our sound glitches forever.

var insert_point = (buffer_insert_point === null)
? now
: buffer_insert_point;
var insert_point = (buffer_insert_point === null) ?
now :
buffer_insert_point;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not an improvement

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not, but most hinters mark it as an error.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The general rule:

foo ? bar
    : baz

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe I could add a .jshintrc file in order to disable this and other unnecessary warnings.


if (pending_buffers.length) {
for (var i = 0, l = pending_buffers.length; i < l; i++) {
var buffer = pending_buffers[i];
for (var _i = 0, _l = pending_buffers.length; _i < _l; _i++) {
var _buffer = pending_buffers[_i];
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes the warning go away, but is really ugly.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, maybe it could use more descriptive variable names.


var source_node = context.createBufferSource();
source_node.buffer = buffer;
source_node.buffer = _buffer;
source_node.connect(gain_node);
source_node.start(insert_point);

insert_point += buffer.duration;
insert_point += _buffer.duration;
}

pending_buffers.length = 0;
Expand All @@ -1283,10 +1283,11 @@ var Module = null;
if (buffer_insert_point <= now)
buffer_insert_point = now;
}
};
}

function get_context() {
return context;
};
}

return {
set_mastervolume: set_mastervolume,
Expand Down