Skip to content
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

Why the function returns null when second invoking on IE browser only [Dart2JS] #36206

Closed
jumperchen opened this Issue Mar 14, 2019 · 4 comments

Comments

Projects
None yet
5 participants
@jumperchen
Copy link

jumperchen commented Mar 14, 2019

Dart version: 2.1.0
Browser: IE 11 (version: 11.648.17134.0)

import 'dart:math';
getRandom() {
  var rand;
  print('before try');
  try {
    rand = new Random.secure();
  } catch (e) {
    rand = new Random();
  }
  print('after try $rand');
  return rand;
}
main() {
  print('result: ${getRandom() == null}');
  print('result: ${getRandom() == null}');
}

The result:

before try
after try Instance of 'minified:bg'
result: false
before try
after try null
result: true

@rakudrama rakudrama self-assigned this Mar 14, 2019

@sigmundch

This comment has been minimized.

Copy link
Member

sigmundch commented Mar 14, 2019

Not sure why I can't see @lrhn reponse here in the bug, but I got a notification email with his clever insight:

I'll dare a guess: The Random.secure constructor reads its result from a (lazy) static variable with an initializer which throws. This throw is caught the first time. The second time such a lazy variable is read, after a failed initialization, it evaluates to null.

So, the implementation should probably make sure to throw each time Random.secure is called, not just the first time.

@rakudrama is working on a CL to fix how Random.secure is written to ensure that the error is thrown every time.

@kevmoo

This comment has been minimized.

@rakudrama

This comment has been minimized.

Copy link
Member

rakudrama commented Mar 15, 2019

Fixed at 9b18505

@jumperchen This will take some time to make its way into a released SDK. In the meantime, you can work around the bug by arranging to call getRandom() just once, something like:

Random _random;
Random myGetRandom() => _random ??= getRandom();

Under the covers, Random() and Random.secure() each return a singleton in the js_runtime implementation, so this will not change the behaviour.

@rakudrama rakudrama closed this Mar 15, 2019

@jumperchen

This comment has been minimized.

Copy link
Author

jumperchen commented Mar 16, 2019

@rakudrama Okay, thanks for the quick fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.