This repository was archived by the owner on May 16, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweak.js
129 lines (114 loc) · 3.5 KB
/
weak.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
/*---------------------------------------------------------------------------------------------
* Copyright (c) No one - this is OSS. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
const EventEmitter = require('events');
const binary = require('node-pre-gyp');
const path = require('path')
const binding_path = binary.find(path.resolve(path.join(__dirname,'./package.json')));
const { WeakTag, ObjectInfo } = require(binding_path);
Object.setPrototypeOf(ObjectInfo, EventEmitter);
Object.setPrototypeOf(ObjectInfo.prototype, EventEmitter.prototype);
const kInfo = Symbol('kInfo');
const map = new WeakMap();
function weak(obj, callback) {
const info = new ObjectInfo(obj, onGarbageCollect);
const tag = new WeakTag(info);
const list = map.get(obj);
if (list === undefined)
map.set(obj, [tag]);
else
list.push(tag);
if (typeof callback === 'function')
info.on('dead', callback);
return makeProxy(info);
}
weak.create = weak;
module.exports = weak;
function onGarbageCollect() {
this.emit('dead');
}
weak.get = function(ref) {
return ref[kInfo].target;
};
weak.isDead = function(ref) {
return weak.get(ref) === undefined;
};
weak.isNearDeath = function(ref) {
return false;
};
weak.isWeakRef = function(ref) {
return !!ref[kInfo];
};
weak.addCallback = function(ref, callback) {
ref[kInfo].addListener('dead', callback);
};
weak.removeCallback = function(ref, callback) {
ref[kInfo].removeListener('dead', callback);
};
weak.removeCallbacks = function(ref) {
ref[kInfo].removeAllListeners('dead');
};
weak.callbacks = function(ref) {
return ref[kInfo].listeners('dead');
};
const proxyHandler = {
getPrototypeOf(info) {
const target = info.target;
return target === undefined ? null : Object.getPrototypeOf(target);
},
setPrototypeOf(info, proto) {
const target = info.target;
return target === undefined ? false : Object.setPrototypeOf(target, proto);
},
isExtensible(info) {
return Object.isExtensible(info); // For invariant keeping
},
preventExtensions(info) {
const target = info.target;
if (target !== undefined) {
Object.preventExtensions(target);
}
Object.preventExtensions(info); // For invariant keeping
return true;
},
getOwnPropertyDescriptor(info, key) {
const target = info.target;
return target === undefined ? undefined :
Object.getOwnPropertyDescriptor(target, key);
},
defineProperty(info, key, descriptor) {
const target = info.target;
return target === undefined ? false :
Object.defineProperty(target, key, descriptor);
},
has(info, key) {
const target = info.target;
return target === undefined ? false : key in target;
},
get(info, key) {
if (key === kInfo) return info;
const target = info.target;
return target === undefined ? undefined : target[key];
},
set(info, key, value) {
const target = info.target;
if (target !== undefined) target[key] = value;
return true;
},
deleteProperty(info, key) {
const target = info.target;
if (target !== undefined) delete target[key];
return true;
},
ownKeys(info) {
const target = info.target;
return target === undefined ? [] :
Object.getOwnPropertyNames(target).concat(
Object.getOwnPropertySymbols(target));
}
};
function makeProxy(info) {
return new Proxy(info, proxyHandler);
}