Repository navigation
Error handling in environment - #659
Conversation
Codecov Report
@@ Coverage Diff @@
## master #659 +/- ##
==========================================
- Coverage 59.97% 59.80% -0.17%
==========================================
Files 166 167 +1
Lines 10851 10982 +131
==========================================
+ Hits 6508 6568 +60
- Misses 4343 4414 +71
Continue to review full report at Codecov.
|
HalidOdat
left a comment
There was a problem hiding this comment.
This looks good! we should add some test cases where a panic was being caused.
|
@HalidOdat I think it would be better to add tests after there's support for strict mode added (After that, some implementation in exec(Assignment Operator) needs to be fixed).
Current behaviour: Assignments without prior declarations are added to the function scope. >> function f () {
x = 12;
}
>> f()
>> x // Must be visible in global
Uncaught: "ReferenceError": "x is not defined"Correct behaviour: >> function f () {
x = 12;
}
>> f()
undefined
>> x
12
// Ok>> function f () {
'use strict';
x = 12;
}
>> f() // Can't do this in strict mode
Uncaught ReferenceError: assignment to undeclared variable x
let x = 12;
const x = 12;What do you think? |
|
I'm taking a re-look at this. |
|
Hi! It seems this requires a rebase :) what is the state of the PR? |
There was a problem hiding this comment.
This is looking very good. The CI error is unerlated, and seems to be related to the new Test262 conformance comments.
It should be writing this comment, but it's failing:
Test262 conformance changes:
| Test result | master count | PR count | difference |
|---|---|---|---|
| Total | 78,493 | 78,493 | 0 |
| Passed | 19,596 | 19,600 | +4 |
| Ignored | 15,585 | 15,585 | 0 |
| Failed | 43,312 | 43,308 | -4 |
| Panics | 447 | 413 | -34 |
| Conformance | 24.97 | 24.97 | +0.01% |
| ReferenceError(String), | ||
| TypeError(String), |
There was a problem hiding this comment.
I think we could use a Box<str> here to reduce a bit the memory footprint.
There was a problem hiding this comment.
The Error messages are however dynamic (depend on variable names, etc.) which is why we had used a String earlier. Could you please elaborate a bit more?
There was a problem hiding this comment.
Yep, a String contains a pointer, a length and a capacity. The capacity indicates how many bytes are allocated for the string, while the length indicates how many are in use. This is useful to grow the string without needing to allocate, but once we know that the String will not change anymore, it's better to drop the excess capacity and have just a pointer with length, a Box<str>.
This can be constructed using String::into_boxed_str() you can find more information on the standard library documentation :).
There was a problem hiding this comment.
@Razican Finally, when we are ready to construct the actual Error object, the API currently expects a template which can convert to a String as an argument (for the message).
pub fn construct_reference_error<M> (&mut self, msg: M) -> Value
where
M: Into<String> {
...
}Would a Box<str> be useful in this case?
There was a problem hiding this comment.
Most (if not all) of the things that can be converted to a String can be converted to a Box<str>, so changing it to Into<Box<str>> could make the trick.
|
I would spect this to be able to remove some of the panics listed in #817. Namely:
Example:
|
|
Hi @54k1, did you have the chance to review why are those environment panics still happening? |
Razican
left a comment
There was a problem hiding this comment.
This looks good to me. Only needs a cargo fmt.
This PR introduces error handling in
environment.This Pull Request fixes/closes #622.
()orValue.ErrorKindenum gives a hint to the other modules about the Error. According to the spec, there only areReferenceErrorandTypeErrorgenerated from environment. And so there only are two fields in theErrorKindenum.ErrorKindto typeError. This is achieved usingto_errormethod. For instance,env.get_this_binding().map_err(|e| e.to_error(ctx))?;panic!s andassert!s that are present in some environment methods correspond to assert statements in the spec. Some things have to be guaranteed (such as prior existence of a binding) and if they fail, they may lead topanic!.