Under GHCJS, how does getCurrentTime
work? In the time
library itself, this is implemented using the FFI, calling out to functions provided by the operating system. However, there aren't any lines in time
that start with:
foreign import javascript ...
I've checked the shims
repository that GHCJS using to patch libraries. It patches the timezone getting function but has no mention of getCurrentTime
. The only thing I found that was remotely close was in ghcjs-boot
, where old-time
is patched with an:
#ifdef ghcjs_HOST_OS
type CTimeVal = ()
type CTimeZone = ()
...
foreign import ccall unsafe "HsTime.h __hscore_gettimeofday"
gettimeofday :: Ptr CTimeVal -> Ptr CTimeZone -> IO CInt
...
But there are two problems with this. One is that it's not the right library (old-time
instead of time
). The other is that it's still using the C FFI. I don't understand how the using the C FFI can work when you are compiling with GHCJS.
So, where does getCurrentTime
get shimmed in for GHCJS?
In response to the comment about grepping ghcjs source, if I search for getTime
(which I believe would be the javascript function used) in GHCJS's source, I get basically nothing. However, by grepping an all.js
file produced by GHCJS for a project that uses getCurrentTime
, I get this:
ag '\bgetTime\b' all.js
20948: h$log((("elapsed time: " + (h$RTS_597.getTime() - h$RTS_595.getTime())) + "ms"));
22863: var atime = goog.math.Long.fromNumber(fs.atime.getTime());
22864: var mtime = goog.math.Long.fromNumber(fs.mtime.getTime());
22865: var ctime = goog.math.Long.fromNumber(fs.ctime.getTime());
The latter three are from some kind of filesystem shim.
I found this in the generated javascript:
function h$gettimeofday(tv_v,tv_o,tz_v,tz_o) {
var now = Date.now();
tv_v.dv.setInt32(tv_o, (now / 1000)|0, true);
tv_v.dv.setInt32(tv_o + 4, ((now % 1000) * 1000)|0, true);
if(tv_v.len >= tv_o + 12) {
tv_v.dv.setInt32(tv_o + 8, ((now % 1000) * 1000)|0, true);
}
return 0;
}
But the question of how this got linked in remains.