forked from TypeScriptToLua/TypeScriptToLua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSetDescriptor.ts
More file actions
72 lines (61 loc) · 2.26 KB
/
SetDescriptor.ts
File metadata and controls
72 lines (61 loc) · 2.26 KB
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
function ____descriptorIndex(this: any, key: string): void {
const value = rawget(this, key);
if (value !== null) {
return value;
}
let metatable = getmetatable(this);
while (metatable) {
const rawResult = rawget(metatable, key);
if (rawResult !== undefined) {
return rawResult;
}
const descriptors = rawget(metatable, "_descriptors");
if (descriptors) {
const descriptor: PropertyDescriptor = descriptors[key];
if (descriptor) {
if (descriptor.get) {
return descriptor.get.call(this);
}
return descriptor.value;
}
}
metatable = getmetatable(metatable);
}
}
function ____descriptorNewindex(this: any, key: string, value: any): void {
let metatable = getmetatable(this);
while (metatable) {
const descriptors = rawget(metatable, "_descriptors");
if (descriptors) {
const descriptor: PropertyDescriptor = descriptors[key];
if (descriptor) {
if (descriptor.set) {
descriptor.set.call(this, value);
} else {
if (descriptor.writable === false) {
throw `Cannot assign to read only property '${key}' of object '${this}'`;
}
descriptor.value = value;
}
return;
}
}
metatable = getmetatable(metatable);
}
rawset(this, key, value);
}
// It's also used directly in class transform to add descriptors to the prototype
function __TS__SetDescriptor(this: void, target: any, key: any, desc: PropertyDescriptor, isPrototype = false): void {
let metatable = isPrototype ? target : getmetatable(target);
if (!metatable) {
metatable = {};
setmetatable(target, metatable);
}
const value = rawget(target, key);
if (value !== undefined) rawset(target, key, undefined);
if (!rawget(metatable, "_descriptors")) metatable._descriptors = {};
const descriptor = __TS__CloneDescriptor(desc);
metatable._descriptors[key] = descriptor;
metatable.__index = ____descriptorIndex;
metatable.__newindex = ____descriptorNewindex;
}