File: weak.js

package info (click to toggle)
js-of-ocaml 6.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 37,932 kB
  • sloc: ml: 135,957; javascript: 58,364; ansic: 437; makefile: 422; sh: 12; perl: 4
file content (240 lines) | stat: -rw-r--r-- 6,774 bytes parent folder | download | duplicates (2)
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// Js_of_ocaml runtime support
// 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.

// Weak API

//Provides: caml_ephe_key_offset
var caml_ephe_key_offset = 3;

//Provides: caml_ephe_data_offset
var caml_ephe_data_offset = 2;

//Provides: caml_ephe_none
var caml_ephe_none = { caml_ephe_none: 0 };

//Provides: caml_ephe_set_key
//Requires: caml_ephe_key_offset
//Requires: caml_ephe_get_data
//Requires: caml_ephe_set_data_opt
function caml_ephe_set_key(x, i, v) {
  var old = caml_ephe_get_data(x);
  if (globalThis.WeakRef && v instanceof Object) v = new globalThis.WeakRef(v);
  x[caml_ephe_key_offset + i] = v;
  caml_ephe_set_data_opt(x, old);
  return 0;
}

//Provides: caml_ephe_unset_key
//Requires: caml_ephe_key_offset
//Requires: caml_ephe_get_data
//Requires: caml_ephe_set_data_opt
//Requires: caml_ephe_none
function caml_ephe_unset_key(x, i) {
  var old = caml_ephe_get_data(x);
  x[caml_ephe_key_offset + i] = caml_ephe_none;
  caml_ephe_set_data_opt(x, old);
  return 0;
}

//Provides: caml_ephe_create
//Requires: caml_weak_create
function caml_ephe_create(n) {
  return caml_weak_create(n);
}

//Provides: caml_weak_create
//Requires: caml_ephe_key_offset
//Requires: caml_ephe_none
function caml_weak_create(n) {
  var alen = caml_ephe_key_offset + n;
  var x = new Array(alen);
  x[0] = 251;
  x[1] = "caml_ephe_list_head";
  for (var i = 2; i < alen; i++) {
    x[i] = caml_ephe_none;
  }
  return x;
}

//Provides: caml_weak_set
//Requires: caml_ephe_set_key, caml_ephe_unset_key
function caml_weak_set(x, i, v) {
  if (v === 0) caml_ephe_unset_key(x, i);
  else caml_ephe_set_key(x, i, v[1]);
  return 0;
}
//Provides: caml_ephe_get_key
//Requires: caml_ephe_key_offset, caml_ephe_data_offset
//Requires: caml_ephe_none
//Alias: caml_weak_get

function caml_ephe_get_key(x, i) {
  var weak = x[caml_ephe_key_offset + i];
  if (weak === caml_ephe_none) return 0;
  if (globalThis.WeakRef && weak instanceof globalThis.WeakRef) {
    weak = weak.deref();
    if (weak === undefined) {
      x[caml_ephe_key_offset + i] = caml_ephe_none;
      x[caml_ephe_data_offset] = caml_ephe_none;
      return 0;
    }
  }
  return [0, weak];
}
//Provides: caml_ephe_get_key_copy
//Requires: caml_ephe_get_key,caml_ephe_key_offset
//Requires: caml_obj_dup
//Alias: caml_weak_get_copy
function caml_ephe_get_key_copy(x, i) {
  var y = caml_ephe_get_key(x, i);
  if (y === 0) return y;
  var z = y[1];
  if (Array.isArray(z)) return [0, caml_obj_dup(z)];
  return y;
}

//Provides: caml_ephe_check_key mutable
//Requires: caml_ephe_key_offset, caml_ephe_data_offset
//Requires: caml_ephe_none
//Alias: caml_weak_check
function caml_ephe_check_key(x, i) {
  var weak = x[caml_ephe_key_offset + i];
  if (weak === caml_ephe_none) return 0;
  if (globalThis.WeakRef && weak instanceof globalThis.WeakRef) {
    weak = weak.deref();
    if (weak === undefined) {
      x[caml_ephe_key_offset + i] = caml_ephe_none;
      x[caml_ephe_data_offset] = caml_ephe_none;
      return 0;
    }
  }
  return 1;
}

//Provides: caml_ephe_blit_key
//Requires: caml_array_blit
//Requires: caml_ephe_key_offset
//Requires: caml_ephe_get_data
//Requires: caml_ephe_set_data_opt
//Alias: caml_weak_blit
function caml_ephe_blit_key(a1, i1, a2, i2, len) {
  var old = caml_ephe_get_data(a1);
  // minus one because caml_array_blit works on ocaml array
  caml_array_blit(
    a1,
    caml_ephe_key_offset + i1 - 1,
    a2,
    caml_ephe_key_offset + i2 - 1,
    len,
  );
  caml_ephe_set_data_opt(a2, old);
  return 0;
}

//Provides: caml_ephe_blit_data
//Requires: caml_ephe_get_data, caml_ephe_set_data_opt
function caml_ephe_blit_data(src, dst) {
  var old = caml_ephe_get_data(src);
  caml_ephe_set_data_opt(dst, old);
  return 0;
}

//Provides: caml_ephe_get_data
//Requires: caml_ephe_data_offset, caml_ephe_key_offset
//Requires: caml_ephe_none
function caml_ephe_get_data(x) {
  var data = x[caml_ephe_data_offset];
  if (data === caml_ephe_none) return 0;
  for (var i = caml_ephe_key_offset; i < x.length; i++) {
    var k = x[i];
    if (globalThis.WeakRef && k instanceof globalThis.WeakRef) {
      var d = k.deref();
      if (d === undefined) {
        x[i] = caml_ephe_none;
        x[caml_ephe_data_offset] = caml_ephe_none;
        return 0;
      }
      if (globalThis.WeakMap) {
        data = data.get(k);
        if (data === undefined) {
          x[caml_ephe_data_offset] = caml_ephe_none;
          return 0;
        }
      }
    }
  }
  return [0, data];
}

//Provides: caml_ephe_get_data_copy
//Requires: caml_ephe_get_data
//Requires: caml_obj_dup
function caml_ephe_get_data_copy(x) {
  var r = caml_ephe_get_data(x);
  if (r === 0) return 0;
  var z = r[1];
  if (Array.isArray(z)) return [0, caml_obj_dup(z)];
  return r;
}

//Provides: caml_ephe_set_data
//Requires: caml_ephe_data_offset, caml_ephe_key_offset
//Requires: caml_ephe_none
function caml_ephe_set_data(x, data) {
  for (var i = x.length - 1; i >= caml_ephe_key_offset; i--) {
    var k = x[i];
    if (globalThis.WeakRef && k instanceof globalThis.WeakRef) {
      var d = k.deref();
      if (d === undefined) {
        x[i] = caml_ephe_none;
        continue;
      }
      if (globalThis.WeakMap) {
        data = new globalThis.WeakMap().set(k, data);
      }
    }
  }
  x[caml_ephe_data_offset] = data;
  return 0;
}

//Provides: caml_ephe_set_data_opt
//Requires: caml_ephe_set_data
//Requires: caml_ephe_unset_data
function caml_ephe_set_data_opt(x, data_opt) {
  if (data_opt === 0) caml_ephe_unset_data(x);
  else caml_ephe_set_data(x, data_opt[1]);
  return 0;
}

//Provides: caml_ephe_unset_data
//Requires: caml_ephe_data_offset
//Requires: caml_ephe_none
function caml_ephe_unset_data(x) {
  x[caml_ephe_data_offset] = caml_ephe_none;
  return 0;
}

//Provides: caml_ephe_check_data
//Requires: caml_ephe_get_data
function caml_ephe_check_data(x) {
  var data = caml_ephe_get_data(x);
  if (data === 0) return 0;
  else return 1;
}