I have a Hello World WebAssembly and I tried to add some code to show the time.
The following line appears to kill the function and it returns nothing (no text, no error)
let dt = Utc::now();
If I comment out the line the function runs as before and returns a string.
Is this happening to anyone else?
I have the 2 lines below at the top of my rs file:
extern crate chrono;
use chrono::{Duration, Utc};
I have the following in the dependencies in the toml file:
chrono = "0.4"
To be used in WASM,
chrono
must be compiled withwasmbind
feature.I wasn't able to find this in documentation, however. This feature was referenced in source code:
Also, there's an open issue for documenting this behavior.
There are several reasons for the behavior you see.
chrono
calls theget_time
function from thetime
crate, which then can delegate to thelibc
or something, depending on the target. However, when you compile to WASM, standard library is rather limited - there are no system calls, for example, and so a bunch of functionality must be provided in some other ways; in particular, aforementionedget_time
function is explicitly unimplemented for this target, so that any call to it will panic.js-sys
) is necessary only for some targets, it will be hidden behind feature flag, so that users of library (here,chrono
) on e.g. Windows or Linux will not pull it unnecessarily. That's why you need to enable the feature explicitly, even if without it the library will be unusable on your target.console_error_panic_hook
crate, which, if added to your project, would show you the "not yet implemented" error in the console window.