File: weak.js

package info (click to toggle)
obrowser 1.1%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 3,216 kB
  • ctags: 3,498
  • sloc: ml: 13,505; makefile: 343; sh: 11
file content (55 lines) | stat: -rw-r--r-- 1,609 bytes parent folder | download
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
/***********************************************************************/
/*                              O'Browser                              */
/*                                                                     */
/*  Copyright 2008 Benjamin Canou. This file is distributed under the  */
/*  terms of the GNU Library General Public License described in file  */
/*  ../LICENSE.                                                        */
/*                                                                     */
/***********************************************************************/

// Idea : use timers to clear weak values at regular intervals.
// Gorgeous.
// But I don not think there is any other option.

var registered_weaks = null;
function register_weak (a) {
    registered_weaks = {
	car : a,
	cdr : registered_weaks
    };
}

// Caml name: create
// Type:      int -> 'a t
RT["caml_weak_create"] = function (size) {
    var a = mk_block (size, ABSTRACT_TAG);
    for (var i = 0;i < size;i++)
	a.set (i, 0);
    register_weak (a);
    return a;
}

// Caml name: set
// Type:      'a t -> int -> 'a option -> unit
RT["caml_weak_set"] = function (a, i, v) {
    a.set (i, v);
    return UNIT;
}

// Caml name: get
// Type:      'a t -> int -> 'a option
RT["caml_weak_get"] = function (a, i) {
    return a.get (i);
}

// Caml name: get_copy
// Type:      'a t -> int -> 'a option
RT["caml_weak_get_copy"] = function (a, i) {
    return a.get (i);
}

// Caml name: check
// Type:      'a t -> int -> bool
RT["caml_weak_check"] = function (a, i) {
    return mk_bool (is_long (a.get (i)));
}