chakra-core / ChakraCore

ChakraCore is an open source Javascript engine with a C API.
MIT License
9.13k stars 1.2k forks source link

Error constructor with more than one argument #5443

Closed gfablima closed 3 years ago

gfablima commented 6 years ago

Hi everyone,

I found an inconsistence on Error() with more than one argument. According to ES6 Error constructor the Error is called with only one argument (message). The other engines returns the first argument, chakra returns the second argument.

Steps to reproduce:

try{
    throw new Error(null)
}catch (e){
    print(e)
}

try{
    throw new Error(null, 'test failed', 'another argument')
}catch (e){
    print(e)
}

Actual results: Error: null Error: test failed

Expected results: Error: null Error: null

V8, SpiderMonkey and JSC works as expected.

OS: Ubuntu 16.04 x86 chakra: 1.11.0.0-beta

rhuanjl commented 6 years ago

Is this used for some internal error handling features?

https://github.com/Microsoft/ChakraCore/blob/bcc695afbcba34a53cbaad294cb1018b6715bb03/lib/Runtime/Library/JavascriptError.cpp#L97-L115

dilijev commented 6 years ago

Confirmed incorrect behavior.

>eshost --tags jsvu -is E:\work\bugs\rs5bugs\_curiosity\issue5443\test.js
## Source
try{
    throw new Error(null)
}catch (e){
    print(e.message)
}

try{
    throw new Error('a')
}catch (e){
    print(e.message)
}

try{
    throw new Error(null, 'test failed', 'another argument')
}catch (e){
    print(e.message)
}

try{
    throw new Error('b', 'test failed', 'another argument')
}catch (e){
    print(e.message)
}

#### jsvu-ch
null
a
test failed
test failed

#### jsvu-jsc, jsvu-sm, jsvu-v8, jsvu-xs
null
a
null
b
igor-simoes commented 6 years ago

cinfuzz

rhuanjl commented 6 years ago

Looking at this again my comment above may be unclear - what i saw by looking at the code is that ChakraCore intentionally implements a non-standard Error constructor it:

a) if called with 1 argument behaves per spec BUT b) if called with 2 arguments it assumes that argument 1 = error number/code and argument 2 = error message

This behaviour for 2 arguments is not per spec BUT looks very intentional hence my question above of it it's used for something specific (or more than one such thing) if so fixing it could break things.