nginx / njs

A subset of JavaScript language to use in nginx
http://nginx.org/en/docs/njs/
BSD 2-Clause "Simplified" License
1.02k stars 147 forks source link

implicit var declarations. #212

Open xeioex opened 5 years ago

xeioex commented 5 years ago

To support expressions like: https://github.com/v8/v8/blob/master/benchmarks/crypto.js#L1668 and https://github.com/v8/v8/blob/master/benchmarks/earley-boyer.js#L4677.

hongzhidao commented 5 years ago

@xeioex @drsm

What's the difference between these two lines and the below?

"use strict";
a = "b"; // throw ReferrenceError 

Need to be more specific.

drsm commented 5 years ago

@hongzhidao @xeioex

Actually we have a bug (global scope in accumulative mode): 1.

// njs
>> var get = () => a;
undefined
>> var set = () => a = 1;
undefined
>> a
ReferenceError: "a" is not defined in shell:1
    at main (native)

>> get() // ok
ReferenceError: "a" is not defined in shell:1
    at anonymous (native)
    at main (native)

>> set() // ok
ReferenceError: "a" is not defined in shell:1
    at anonymous (native)
    at main (native)

>> var a;
undefined
>> a
undefined
>> get() // fail
ReferenceError: "a" is not defined in shell:1
    at anonymous (native)
    at main (native)

>> set() // fail
ReferenceError: "a" is not defined in shell:1
    at anonymous (native)
    at main (native)

2.

//  node --use-strict
> var get = () => a;
undefined
> var set = () => a = 1;
undefined
> a
Thrown:
ReferenceError: a is not defined
> get()
Thrown:
ReferenceError: a is not defined
    at get (repl:1:17)
> set()
Thrown:
ReferenceError: a is not defined
    at set (repl:1:19)
> var a;
undefined
> get()
undefined
> set()
1
> get()
1
  1. implicit vars (should be optional IMO)
    // node
    > var get = () => a;
    undefined
    > var set = () => a = 1;
    undefined
    > a
    Thrown:
    ReferenceError: a is not defined
    > get()
    Thrown:
    ReferenceError: a is not defined
    at get (repl:1:17)
    > set()
    1
    > a
    1
    > get()
    1
    > 

    closely related to #132