Njs: Added more object literals support.

Created on 19 Jun 2019  ·  19Comments  ·  Source: nginx/njs

  1. support shorthand methods:
var o = {
  method(parameters) {}
};
// ==
var o = {
  method: function(parameters) {}
};
  1. support [expr] notation called computed property names.
var k = 'abc'.split('');
var o = {
    [k[0]]: 'old-style',
    [k[2]]() { return 'method'; }
};

On it.

BTW, Is this OK? @drsm

{ 'a': 2 }

It passed on chrome, and it's an object. It does not pass in NJS.
So check whether it is an issue, please. @xeioex

ES5.1 feature

Most helpful comment

@hongzhidao

The updated last patch: https://gist.github.com/1462105671135540444a940c534c5144

Added more tests.

All 19 comments

@hongzhidao

chrome and nodejs eval an { whatever } input in REPL a little bit different:

  • try as a block
  • if fails try as an object literal
    (or maybe vice versa, not sure)

njs just evals as a block:

$ build/njs
interactive njs 0.3.3
v.<Tab> -> the properties and prototype methods of v.
type console.help() for more information
>> { 'a': 2 }
SyntaxError: Unexpected token ":" in shell:1
>> var a = { 'a': 2 }
undefined
>> ({ 'a': 2 })
{
 a: 2
}

@drsm

1.

chrome and nodejs eval an { whatever } input in REPL a little bit different:

OK, so we postpone this.

  1. See this https://tc39.es/ecma262/#sec-object-initializer. There are two places I can't understand.
    PropertyDefinitionList[Yield, Await]: what does [Yield, Await] mean here?
    https://tc39.es/ecma262/#prod-CoverInitializedName: can you show an example with this?
  1. See this https://tc39.es/ecma262/#sec-object-initializer. There are two places I can't understand.
    PropertyDefinitionList[Yield, Await]: what does [Yield, Await] mean here?

from 5.1.5 Grammar Notation

A production may be parameterized by a subscripted annotation of the form “[parameters]”, which may appear as a suffix to the nonterminal symbol defined by the production. “parameters” may be either a single name or a comma separated list of names. A parameterized production is shorthand for a set of productions defining all combinations of the parameter names, preceded by an underscore, appended to the parameterized nonterminal symbol.

and https://tc39.es/ecma262/#sec-identifiers-static-semantics-early-errors :

  • It is a Syntax Error if this production has a [Yield] parameter and StringValue of Identifier is "yield".
  • It is a Syntax Error if this production has an [Await] parameter and StringValue of Identifier is "await".

so, it looks like some form of combinators (not sure, the grammar notations are always been a hard part to me).

https://tc39.es/ecma262/#prod-CoverInitializedName: can you show an example with this?

looks like related to https://tc39.es/ecma262/#sec-destructuring-assignment (unsupported and unrelated)

It is a Syntax Error if LeftHandSideExpression is either an ObjectLiteral or an ArrayLiteral and if LeftHandSideExpression is not covering an AssignmentPattern.

var o = {
    a: 1,
    b: 2,
    c: 3
};

var {
    a,
    b,
    c: x
} = o;

console.log(a, b, x);

but also unsure

@xeioex take a look.

I try to get rid of property_token.
https://gist.github.com/hongzhidao/08113ae892415e80a204c581da678328

See this. https://tc39.es/ecma262/#prod-IdentifierReference
As I understand, all the tokens which are name can be called an identifier.
identifier = NJS_TOKEN_NAME + KEYWORDS(all the reserved keywords, so we need a flag, now I named keyword)

I'm on this ticket, but it depends on the design, welcome to suggest.

@drsm

support shorthand methods
support [expr] notation called computed property names.

Here's the patch, welcome to test. Thanks.
https://gist.github.com/hongzhidao/08113ae892415e80a204c581da678328

@xeioex

Getting rid of lexer property and property_token. (done)
https://gist.github.com/hongzhidao/23f562a8f55351e70fac895f77fb44c1

@hongzhidao

the methods looks good, thanks!

computed property names have a minor problem:

>> ({ [{}]() {} })
InternalError: failed conversion of type "object" to string while property initialization
    at main (native)
>> ({ [new Number(12345)]: 1 })
InternalError: failed conversion of type "object number" to string while property initialization
    at main (native)

@drsm good reporting.

computed property names have a minor problem:

Updated.
https://gist.github.com/hongzhidao/28934376c645fe0095596188045f9926

@xeioex
After introducing computed property names, njs_value_to_string need to be introduced.
If OK, I'll split this into two patches, tell me if there is better naming.

BTW, there are two unclear places.

  1. what does ext mean in njs_vm_value_to_ext_string?
  2. njs_vm_value_string_copy is unused?

deleted.

@xeioex @drsm

  1. Introduced njs_vm_value_to_string().
  2. Added Object shorthand methods and computed property names.
    https://gist.github.com/hongzhidao/08113ae892415e80a204c581da678328

@hongzhidao

>> ({ [0]: 1, [-0]: 2 }) == 2
false
>> ({ [0]: 1, [-0]: 2 }) 
{
 0: 1,
 -0: 2
}

https://tc39.es/ecma262/#sec-topropertykey

>> var x = ({ m(a) {} })
undefined
>> new x.m()
{
}
/*---
description: >
    Functions declared as methods may not be used as constructors.
es6id: 14.3.8
---*/

@drsm thanks again.

Updated, welcome to test.
https://gist.github.com/hongzhidao/08113ae892415e80a204c581da678328

@hongzhidao

what does ext mean in njs_vm_value_to_ext_string?

ext - means external (nxt_str_t not, njs_value_t), I think as of now we can remove this prefix.

njs_vm_value_string_copy is unused?

It will be used later.

https://gist.github.com/hongzhidao/08113ae892415e80a204c581da678328

njs_vm_value_to_string()

I think it is better to rename njs_vm_value_to_ext_string() -> njs_vm_value_to_string()
the same with njs_vm_retval_to_ext_string() -> njs_vm_retval_to_string().

where as your njs_vm_value_to_string() -> static njs_vm_value_string() ? // not sure about the last one

- - Passed 8227 tests (30.3%)
- - Failed 18892 tests (69.7%)
+ - Passed 8249 tests (30.4%)
+ - Failed 18870 tests (69.6%)

no degradations.

where as your njs_vm_value_to_string() -> static njs_vm_value_string() ? // not sure about the last one

  1. See the latest patch. I renamed njs_vm_value_string as njs_vm_value_to_string.
  2. I think njs_vm_value_to_string need to be introduced, it'll only do the thing of converting value to string.
  3. Maybe njs_vm_value_to_ext_string can be renamed, it includes njs_vm_backtrace_dump, but now it looks OK.

About -0.

({ [0]: 1, [-0]: 2 }) == 2
false

({ [0]: 1, [-0]: 2 })
{
0: 1,
-0: 2
}

I thought it, in NJS we regard -0 as 0 by default (in njs_vm_value_to_string).
But in the places (njs_vm_retval_to_ext_string and njs_vm_retval_dump) which output it, we need to show its real value.

About -0.

njs.dump should be fixed i think:

>> [ -0, 0 ]
[
 0,
 0
]
>> [ -0, 0 ].map((x) => 1/x)
[
 -Infinity,
 Infinity
]

extracted to #184

@xeioex @drsm take a look.

Updated.https://gist.github.com/hongzhidao/08113ae892415e80a204c581da678328

  1. Introduced njs_function_lambda_alloc().
  2. Introduced njs_vm_value_to_string().
  3. Added Object shorthand methods and computed property names.

@hongzhidao

The updated last patch: https://gist.github.com/1462105671135540444a940c534c5144

Added more tests.

@xeioex

The updated last patch: https://gist.github.com/1462105671135540444a940c534c5144
Added more tests.

Looks good.

Was this page helpful?
0 / 5 - 0 ratings