njs icon indicating copy to clipboard operation
njs copied to clipboard

Support for ES6 optional function parameter

Open xbb123 opened this issue 6 years ago • 11 comments

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Default_parameters

xbb123 avatar Aug 24 '19 19:08 xbb123

just want to note, that we need an additional scope, if default parameters are present:

.editor
// Entering editor mode (^D to finish, ^C to cancel)
var x = function(a = 1, b = 1, c = () => a + b) {
    var a = 2, b = 2;
    return c();
};

var y = function(a, b, c) {
    a = 1;
    b = 1;
    c = () => a + b;
    var a = 2, b = 2;
    return c();
};

console.log(x(), y());


2 4
undefined

drsm avatar Aug 25 '19 19:08 drsm

Hello, I would like to get this feature too.

Is there some kind of ETA when the feature gets available?

sly-roar avatar Aug 20 '21 12:08 sly-roar

@sly-roar

The feature is in progress, is planned for 0.7.0.

xeioex avatar Sep 24 '21 15:09 xeioex

@sly-roar @drsm @xbb123 @xeioex

Patch.

lexborisov avatar Sep 24 '21 16:09 lexborisov

Known bugs to fix:

>> var x = function(a = 1, b = 1, c = () => a + b) {
    var a = 2, b = 2;
    return c();
};
>> x();
4
// FAIL
>> function f(a=()=>b, b=1) {return a}; f()()
Thrown:
ReferenceError: cannot access variable before initialization
    at anonymous (t2.js:1)
    at console.log (native)
    at main (t2.js:3)
// FAIL
>> ((x = 42) => {}).length
1
// FAIL
>> ((a,b=1) => {}).length
2 
// FAIL

>> var f = function(x = arguments[2], y = arguments[3], z) { return y; };
>> f(undefined, undefined, 'third', 'fourth');
'third'
// FAIL

xeioex avatar Sep 24 '21 17:09 xeioex

@lexborisov @xeioex

and some more tests:

>> var x = function(a = 1, b = 1, c = () => a + b) { let a = 2, b = 2; return c(); }
Thrown:
SyntaxError: "a" has already been declared in shell:1
// OK

>> var x = function(a = () => 1, b = () => 1, c = () => a() + b()) { function a() { return 2; }; return c(); }
undefined
>> x()
2
// OK

>> var x = function(a = () => 1, b = () => 1, c = () => a() + b()) { var a = () => 2; return c(); }
undefined
>> x()
3
// FAILED

>> var x = function(a = () => 1, b = () => 1, c = () => a() + b()) { function a() { return 2; } return c() + a(); }
undefined
>> x()
3
// FAILED

drsm avatar Sep 24 '21 17:09 drsm

maybe unrelated:

$ build/njs -q
>> var x = () => (x = a) => x; let a = 2; x()();
2
// OK
>> 
$ build/njs -q
>> var x = () => (x = a) => x; 
undefined
>> var a = 2;
undefined
>> x()()
2
// OK
>> 
$ build/njs -q
>> var x = () => (x = a) => x; 
undefined
>> let a = 2;
undefined
>> x()()
Thrown:
ReferenceError: "a" is not defined
    at anonymous (:1)
    at main (:1)
// FAIL
>>

drsm avatar Sep 24 '21 20:09 drsm

>> var x = async function(inner = () => Promise.reject('reject'), next = await inner()) {};
undefined
// vs.
> var x = async function(inner = () => Promise.reject('reject'), next = await inner()) {};
var x = async function(inner = () => Promise.reject('reject'), next = await inner()) {};
                                                                      ^^^^^

Uncaught SyntaxError: Illegal await-expression in formal parameters of async function

drsm avatar Sep 25 '21 20:09 drsm

@drsm

Unfortunately, in the current architecture, we cannot solve closures problem:

function x(a = 1, b = 1, c = () => a + b) {
    console.log(a, b);

    let f = () => {return a + b}
    var a = 2, b = 3;

    console.log(f());

    return c();
};

console.log(x());

It was decided to postpone this task. We will return to it after the release. Thank for testing!

lexborisov avatar Sep 28 '21 19:09 lexborisov

Unfortunately, in the current architecture, we cannot solve closures problem

BTW, I didn't ever see that someone use that "feature" in the wild.

drsm avatar Sep 29 '21 11:09 drsm

@drsm We decided to postpone this feature because of non-trivial limitations. In the current architecture we cannot guarantee correct execution (and catching all possible unsupported cases) when there are function values as a default arguments.

After 0.7.0 @lexborisov is going to start working on the frames refactoring, when C-like frames are removed implementing this feature correctly will be much simpler. Removing C-like frames will also require implementing garbage collector.

xeioex avatar Oct 13 '21 17:10 xeioex