Skip to content

String.prototype.substr() support #307

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 25, 2018
Merged

Conversation

elpiel
Copy link
Contributor

@elpiel elpiel commented Jun 24, 2018

I wanted to open this PR as well as ask some questions. Not sure how the whole binding works, but I do know some stuff currently are not supported as the optional parameters( as well as NaN support). But I also wanted to raise the question: What about all the special cases for specific handling?, for example I can give substr:

If length is undefined, substr() extracts characters to the end of the string.
If length is a negative number, it is treated as 0.

For both start and length, NaN is treated as 0.

How can these cases be handled as a whole and what should we do about them until we can actually handle them?

src/js.rs Outdated
/// the start index and a number of characters after it.
///
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substr
/// TODO: Add `NaN` and `undefined` support
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added todo as we don't yet have that support(or at least not that I know of).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The NaN behavior is on the JS engine implementation side, not our side, so we don't need to worry about it.

assert.equal(wasm.create_substr(aString, -1, 1), "a");
assert.equal(wasm.create_substr(aString, 1, -1), "");
// TODO: Uncomment and test these assertions, once we have support for optional parameters
// assert.equal(wasm.create_substr(aString, -3), "lla");
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did add those tests though(for the optional parameter) and do want to know what should be added as a todo if needed at all.

Copy link
Member

@fitzgen fitzgen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! Thanks :)

src/js.rs Outdated
/// the start index and a number of characters after it.
///
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substr
/// TODO: Add `NaN` and `undefined` support
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The NaN behavior is on the JS engine implementation side, not our side, so we don't need to worry about it.

@elpiel
Copy link
Contributor Author

elpiel commented Jun 25, 2018

Should I remove the todos then? And how should we handle the Optional parameters - again with todos or you want another way of doing it?

@fitzgen
Copy link
Member

fitzgen commented Jun 25, 2018

For optional parameters, it is often best to just require their presence. It is a judgement call on a case-to-case basis though.

@elpiel
Copy link
Contributor Author

elpiel commented Jun 25, 2018

I removed the TODO, don't know if I should leave the TODO.

@fitzgen fitzgen merged commit 76fcbf3 into rustwasm:master Jun 25, 2018
@elpiel elpiel deleted the string-substr branch June 25, 2018 20:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants