/
bslClosure.js
151 lines (129 loc) · 3.71 KB
/
bslClosure.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
Copyright © 2011 MLstate
This file is part of OPA.
OPA is free software: you can redistribute it and/or modify it under the
terms of the GNU Affero General Public License, version 3, as published by
the Free Software Foundation.
OPA 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 Affero General Public License for
more details.
You should have received a copy of the GNU Affero General Public License
along with OPA. If not, see <http://www.gnu.org/licenses/>.
*/
/* module byClosure
@author Sebastien Briais
@author David Rajchenbach-Teller (made the code more JS idiomatic)
*/
##extern-type Closure.t
##extern-type Closure.args
var global = this;
/**
* <!> Part of the JsInterface (used by funaction serialization)
**/
##register on_distant : string -> bool
##args(str)
{
return (str in global) && global[str].distant || false
}
function set_distant(str)
{
var l = str.split(",")
for (var i = l.length; i--;) {
(l[i] in global) && (global[l[i]].distant = true)
}
}
function get_local_unsafe(str) {
return global[str];
}
##register get_local : string -> option(Closure.t)
##args(str)
{
return (str in global) ? js_some(global[str]) : js_none;
}
##module Args
##register create : int -> Closure.args
##args(n)
{
return new Array(n);
}
##register length : Closure.args -> int
##args(a)
{
return a.length;
}
##register set : Closure.args, int, 'a -> void
##args(a,i,x)
{
a[i] = x;
}
##register get : Closure.args, int -> 'a
##args(a,i)
{
return a[i];
}
##endmodule
##register create_anyarray : 'impl, int, 'ident -> Closure.t
##args(f,n,identifier)
{
return function() { return f.call(null,arguments) }
}
##register [cps-bypass] create_anyarray_cps \ `create_anyarray_cps` : 'impl, int, 'ident, continuation(Closure.t) -> void
function create_anyarray_cps(f,n,identifier,k)
{
/*
var any_cps = function(args){
var k = args.pop();
return f(args, k);
}
return %BslClosure.create%(any_cps, n+1, identifier);
*/
error("TODO create_any_array_cps")
}
/**
* Part of JsInterface (funaction)
**/
function args_apply(closure,args) {
return closure.apply(null,args)
}
##register apply \ `args_apply` : Closure.t, Closure.args -> 'a
##register [cps-bypass] apply_cps : Closure.t, Closure.args, continuation('a) -> void
##args(closure, args, k)
{
error("TODO: apply_cps")
/*if(closure.arity - 1 == args.length + closure.args.length){
args.push(k);
}
return %BslClosure.apply%(closure, args);*/
}
function get_closure_name(closure) {
return closure.toString().match(/function *([^(]*)/)[1]
}
##register get_identifier : 'c -> option('a)
##args(closure)
{
if ('identifier' in closure) return closure.identifier;
var name = get_closure_name(closure);
return (global[name] == closure) ? js_some(make_onefield_record(static_field_of_name("closure_name"),name)) : js_none;
}
##register set_identifier : Closure.t, 'a -> void
##args(closure, identifier)
{
closure.identifier = js_some(identifier);
}
##register is_empty : 'b -> bool
##args(closure)
{
var name = get_closure_name(closure)
return (global[name] == closure) // toplevel function are the only opa closures that are empty
}
##register import \ `clos_import` : 'a, int -> Closure.t
##register export \ `clos_export` : Closure.t -> 'a
function clos_import(clos,_arity) { return clos }
function clos_export(clos) { return clos }
/** Should print closure-related info. Ignored for the moment.*/
##register closure_debug_from_opa: string,string -> void
##args(topic, message)
{
return ;
}