/
jslib_js_of_ocaml.js
137 lines (130 loc) · 4.72 KB
/
jslib_js_of_ocaml.js
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
// Js_of_ocaml library
// http://www.ocsigen.org/js_of_ocaml/
// Copyright (C) 2010 Jérôme Vouillon
// 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.
///////////// Jslib: code specific to Js_of_ocaml
//Provides: caml_js_from_bool const
function caml_js_from_bool(x) { return !!x; }
//Provides: caml_js_to_bool const
function caml_js_to_bool(x) { return +x; }
//Provides: caml_js_from_float const
function caml_js_from_float(x) { return x; }
//Provides: caml_js_to_float const
function caml_js_to_float(x) { return x; }
//Provides: caml_js_from_string mutable
//Requires: MlString
function caml_js_from_string(s) { return s.toString(); }
//Provides: caml_js_from_array mutable
//Requires: raw_array_sub
function caml_js_from_array(a) { return raw_array_sub(a,1,a.length-1); }
//Provides: caml_js_to_array mutable
//Requires: raw_array_cons
function caml_js_to_array(a) { return raw_array_cons(a,0); }
//Provides: caml_js_var mutable
//Requires: js_print_stderr
//Requires: MlString
function caml_js_var(x) {
var x = x.toString();
//Checks that x has the form ident[.ident]*
if(!x.match(/^[a-zA-Z_$][a-zA-Z_$0-9]*(\.[a-zA-Z_$][a-zA-Z_$0-9]*)*$/)){
js_print_stderr("caml_js_var: \"" + x + "\" is not a valid JavaScript variable. continuing ..");
//joo_global_object.console.error("Js.Unsafe.eval_string")
}
return eval(x);
}
//Provides: caml_js_call
//Requires: caml_js_from_array
function caml_js_call(f, o, args) { return f.apply(o, caml_js_from_array(args)); }
//Provides: caml_js_fun_call
//Requires: caml_js_from_array
function caml_js_fun_call(f, args) { return f.apply(null, caml_js_from_array(args)); }
//Provides: caml_js_meth_call
//Requires: MlString
//Requires: caml_js_from_array
function caml_js_meth_call(o, f, args) {
return o[f.toString()].apply(o, caml_js_from_array(args));
}
//Provides: caml_js_new
//Requires: caml_js_from_array
function caml_js_new(c, a) {
switch (a.length) {
case 1: return new c;
case 2: return new c (a[1]);
case 3: return new c (a[1],a[2]);
case 4: return new c (a[1],a[2],a[3]);
case 5: return new c (a[1],a[2],a[3],a[4]);
case 6: return new c (a[1],a[2],a[3],a[4],a[5]);
case 7: return new c (a[1],a[2],a[3],a[4],a[5],a[6]);
case 8: return new c (a[1],a[2],a[3],a[4],a[5],a[6], a[7]);
}
function F() { return c.apply(this, caml_js_from_array(a)); }
F.prototype = c.prototype;
return new F;
}
//Provides: caml_js_wrap_callback const
//Requires: caml_call_gen,raw_array_copy
function caml_js_wrap_callback(f) {
return function () {
if(arguments.length > 0){
return caml_call_gen(f, raw_array_copy(arguments));
} else {
return caml_call_gen(f, [undefined]);
}
}
}
//Provides: caml_js_wrap_meth_callback const
//Requires: caml_call_gen,raw_array_cons
function caml_js_wrap_meth_callback(f) {
return function () {
return caml_call_gen(f,raw_array_cons(arguments,this));
}
}
//Provides: caml_js_wrap_meth_callback_unsafe const
//Requires: caml_call_gen,raw_array_cons
function caml_js_wrap_meth_callback_unsafe(f) {
return function () { f.apply(null, raw_array_cons(arguments,this)); }
}
//Provides: caml_js_equals mutable
function caml_js_equals (x, y) { return +(x == y); }
//Provides: caml_js_to_byte_string const
//Requires: caml_new_string
function caml_js_to_byte_string (s) {return caml_new_string (s);}
//Provides: caml_js_eval_string
//Requires: MlString
function caml_js_eval_string (s) {return eval(s.toString());}
//Provides: caml_js_expr
//Requires: js_print_stderr
//Requires: MlString
function caml_js_expr(s) {
js_print_stderr("caml_js_expr: fallback to runtime evaluation");
return eval(s.toString());}
//Provides: caml_pure_js_expr const
//Requires: js_print_stderr
//Requires: MlString
function caml_pure_js_expr (s){
js_print_stderr("caml_pure_js_expr: fallback to runtime evaluation");
return eval(s.toString());}
//Provides: caml_js_object
//Requires: MlString
function caml_js_object (a) {
var o = {};
for (var i = 1; i < a.length; i++) {
var p = a[i];
o[p[1].toString()] = p[2];
}
return o;
}