Javascript function hoisting bug

Talk about code development, features, specific bugs, enhancements, patches, and similar things.
Forum rules
Please keep everything here strictly on-topic.
This board is meant for Pale Moon source code development related subjects only like code snippets, patches, specific bugs, git, the repositories, etc.

This is not for tech support! Please do not post tech support questions in the "Development" board!
Please make sure not to use this board for support questions. Please post issues with specific websites, extensions, etc. in the relevant boards for those topics.

Please keep things on-topic as this forum will be used for reference for Pale Moon development. Expect topics that aren't relevant as such to be moved or deleted.
Yetanfou

Javascript function hoisting bug

Unread post by Yetanfou » 2017-03-24, 23:28

LS,

I got a bug report for one of my apps related to Palemoon. The bug report concerns the following code snippet:

Code: Select all

document.onreadystatechange = function () {  

    if (document.readyState == "complete") {

        var type=decodeURIComponent(getUrlParameter('type')),
            file=decodeURIComponent(getUrlParameter('file')),
            options = {},
            $session = $('.session');

// unrelated code elided

        // why is there no standard library function for this? 
        function getUrlParameter (param) {
            var pattern = new RegExp('[?&]'+param+'((=([^&]*))|(?=(&|$)))','i');
            var m = window.location.search.match(pattern);
            return m && ( typeof(m[3])==='undefined' ? '' : m[3] );
        }
    }
};
This code works in all other browsers it has been thrown at, but fails in Palemoon with

Code: Select all

ReferenceError: getUrlParameter is not defined ready.js:5:12
Given Palemoon/Goanna's heritage in Gecko this is an odd bug to encounter, function hoisting is a standard feature of Javascript since its inception. So, what's up?

GMforker

Re: Javascript function hoisting bug

Unread post by GMforker » 2017-03-25, 05:49

See #900

User avatar
Moonchild
Pale Moon guru
Pale Moon guru
Posts: 35602
Joined: 2011-08-28, 17:27
Location: Motala, SE
Contact:

Re: Javascript function hoisting bug

Unread post by Moonchild » 2017-03-25, 14:52

Yetanfou wrote:function hoisting is a standard feature of Javascript since its inception. So, what's up?
It's a standard feature for script-global function scopes, but not for { } block scopes (that's new in ES6).
Since you're defining a new function inside the if() block scope, it won't get hoisted to the top of that block. To work around this, define your functions first inside the block before calling them.
"Sometimes, the best way to get what you want is to be a good person." -- Louis Rossmann
"Seek wisdom, not knowledge. Knowledge is of the past; wisdom is of the future." -- Native American proverb
"Linux makes everything difficult." -- Lyceus Anubite

Yetanfou

Re: Javascript function hoisting bug

Unread post by Yetanfou » 2017-03-25, 19:46

OK, that works so the (lone?) Palemoon user can enjoy this app for a while longer. The next version won't work, again, this time due to the use of 'classes' in the bitjs unarchiver. Any sight on those being supported in PM? Like classes or not, they're out there in the wild and they're not going away...

User avatar
Moonchild
Pale Moon guru
Pale Moon guru
Posts: 35602
Joined: 2011-08-28, 17:27
Location: Motala, SE
Contact:

Re: Javascript function hoisting bug

Unread post by Moonchild » 2017-03-26, 01:32

Everything is out there on "some" sites and not going away.
Why?
Because people love their shinies more than keeping their service accessible, and people have transformed javascript into something of a hybrid monstrosity of event-driven and C-like instead of sticking to its true nature.

JS is actually a point of much contention right now, and it's killing the internet. It's one of the main reasons why independent browsers are struggling, because the giants are both writing the standards and providing the software to "prove" that those standards are wanted, and pimping every new feature they come up with to millions so it's always in use "somewhere". The result is an unfair playing field.
"Sometimes, the best way to get what you want is to be a good person." -- Louis Rossmann
"Seek wisdom, not knowledge. Knowledge is of the past; wisdom is of the future." -- Native American proverb
"Linux makes everything difficult." -- Lyceus Anubite

Yetanfou

Re: Javascript function hoisting bug

Unread post by Yetanfou » 2017-03-26, 23:36

While there is some truth in your statement about Javascript (and CSS and whatever version of HTML is en vogue this year) being in a constant state of flux because, well, because 'we can', really, that does not negate the fact that the average user wants the browser to be able to navigate such sites as they happen upon, whatever dialect of markup or styling or active content they might contain. Had the whole web been written in, say, Postscript or its visually impaired parent Forth none of this might have happened but that bridge has been passed and burned a long time ago. The web is, and browsers are generally made to be used on the web. When in Rome, act as the Romans do. They're a fickle bunch, wont to fads and eager to adopt foreign ways.

I realise that chasing a moving target is not easy nor enjoyable but I also think Palemoon has a moment of opportunity coming up. Mozilla has turned the Chrome-clone switch to 11 and is getting ready to drop those things which make Gecko-based browsers preferable over Blink/Webkit-based ones: deep extensibility. With web extensions being the only recourse for future versions of Firefox, lots of disgruntled soon-to-be-former Firefox users will be looking for an alternative to keep on using their TreeStyleTabs et al. That alternative could be Palemoon, but if it fails to render their shiny they'll just keep on looking.

That idea about using the stand-alone version of Spidermonkey might be worth considering.

Locked