-
Notifications
You must be signed in to change notification settings - Fork 465
/
Copy pathcaml_sys.ts
148 lines (115 loc) · 4.12 KB
/
caml_sys.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Js_of_ocaml runtime support
// http://www.ocsigen.org/js_of_ocaml/
// Copyright (C) 2014 Jérôme Vouillon, Hugo Heuzard, Andy Ray
// Laboratoire PPS - CNRS Université Paris Diderot
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, with linking exception;
// either version 2.1 of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
// Copyright (c) 2015 Bloomberg LP. All rights reserved.
// Hongbo Zhang (hzhang295@bloomberg.net)
'use strict';
import { caml_array_sub} from './caml_array'
import {caml_raise_not_found, caml_invalid_argument} from './caml_exceptions'
function caml_sys_getenv (n : string) {
//nodejs env
if(typeof process !== 'undefined'
&& process.env
&& process.env[n] != undefined)
return process.env[n];
caml_raise_not_found ();
}
/**
* @define
* @type {boolean}
*/
var IS_NODE_JS : boolean = true ;
/**
*
* @param {number} code
*/
function caml_sys_exit (code) {
// process resides in global variable of
// nodejs
if(IS_NODE_JS && typeof process !== 'undefined'){
process["exit"](code);
}
caml_invalid_argument("Function 'exit' not implemented");
}
var caml_initial_time = +new Date() * 0.001;
/**
*
* @returns {number}
*/
function caml_sys_time () {
return +new Date() * 0.001 - caml_initial_time;
}
function caml_sys_get_config () {
// TODO: shall we inline
return [0, "Unix", 32, 0];
}
/**
* external random_seed : unit -> int array = "caml_sys_random_seed"
* stdlib/random.ml
* Another common usage of the unary + operator is to coerce a Date object into a number, because the result is the Unix timestamp (milliseconds elapsed since 1 January 1970 00:00:00 UTC) representation of the date/time value:
*
* var d = new Date( "Mon, 18 Aug 2014 08:53:06 CDT" );
*
* +d; // 1408369986000
* The most common usage of this idiom is to get the current now moment as a timestamp, such as:
*
* var timestamp = +new Date();
*/
function caml_sys_random_seed () : [number] {
return [(+new Date()^0xffffffff*Math.random())];
}
function caml_sys_const_big_endian () { return 0; }
function caml_sys_const_word_size () { return 32; }
function caml_sys_const_int_size () { return 32; }
//Provides: caml_sys_const_max_wosize const
// max_int / 4 so that the following does not overflow
//let max_string_length = word_size / 8 * max_array_length - 1;;
function caml_sys_const_max_wosize () { return (0x7FFFFFFF/4) | 0;}
function caml_sys_const_ostype_cygwin () { return 0; }
function caml_sys_const_ostype_unix () { return 1; }
function caml_sys_const_ostype_win32 () { return 0; }
function caml_sys_system_command(_cmd){
return 127;
}
// Put it here: Uncaught ReferenceError: process is not defined
// Make typescript compiler happy
declare var process : any ;
// external get_argv : unit -> string * string array = "caml_sys_get_argv"
function caml_sys_get_argv (_:void) {
var main = "a.out";
var args = []
if((typeof process !== 'undefined')
&& process["argv"]
&& process["argv"]["length"] > 0) {
var argv = process.argv
//nodejs
main = argv[1];
args = caml_array_sub(argv,2,argv.length - 2);
}
var p = main;
var args2 = [0, p];
for(var i = 0; i < args.length; i++)
args2.push(args[i]);
return [0, p, args2];
}
export {
caml_sys_time,
caml_sys_random_seed,
caml_sys_system_command,
caml_sys_getenv
}