-
-
Notifications
You must be signed in to change notification settings - Fork 185
Expand file tree
/
Copy pathSetDescriptor.ts
More file actions
47 lines (40 loc) · 1.72 KB
/
SetDescriptor.ts
File metadata and controls
47 lines (40 loc) · 1.72 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
import { __TS__CloneDescriptor } from "./CloneDescriptor";
import { __TS__DescriptorGet } from "./DescriptorGet";
import { __TS__DescriptorSet } from "./DescriptorSet";
const getmetatable = _G.getmetatable;
function descriptorIndex(this: any, key: string): void {
return __TS__DescriptorGet.call(this, getmetatable(this), key);
}
function descriptorNewIndex(this: any, key: string, value: any): void {
return __TS__DescriptorSet.call(this, getmetatable(this), key, value);
}
// It's also used directly in class transform to add descriptors to the prototype
export 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);
}
// When setting a descriptor on an instance (not a prototype), ensure it has
// its own metatable so descriptors are not shared across instances.
// See: https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1625
if (!isPrototype && !rawget(metatable, "_isOwnDescriptorMetatable")) {
const instanceMetatable: any = {};
instanceMetatable._isOwnDescriptorMetatable = true;
setmetatable(instanceMetatable, metatable);
setmetatable(target, instanceMetatable);
metatable = instanceMetatable;
}
const value = rawget(target, key);
if (value !== undefined) rawset(target, key, undefined);
if (!rawget(metatable, "_descriptors")) metatable._descriptors = {};
metatable._descriptors[key] = __TS__CloneDescriptor(desc);
metatable.__index = descriptorIndex;
metatable.__newindex = descriptorNewIndex;
}