Repository navigation
Expand file tree
/
Copy pathPHPVariable.js
More file actions
207 lines (207 loc) · 7.23 KB
/
Copy pathPHPVariable.js
File metadata and controls
207 lines (207 loc) · 7.23 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
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
import { PHPFatalError } from "./PHPError.js";
import { SYMBOL_PHP_NAME } from "./Reflection.js";
export const SYMBOL_PHP_REFERENCE = Symbol.for("php.reference");
export const SYMBOL_PHP_VARIABLE = Symbol.for("php.variable");
export class PHPReference {
static [Symbol.hasInstance](value) {
if (Function.prototype[Symbol.hasInstance].call(this, value))
return true;
return this === PHPReference
&& value !== null
&& (typeof value === "object" || typeof value === "function")
&& value[SYMBOL_PHP_REFERENCE] === true;
}
async setFrom(otherref) {
return await this.set(await otherref.get());
}
async isset() {
const value = await this.get();
return value !== null && value !== undefined;
}
async unset() {
await this.set(undefined);
}
}
export function isPHPReference(value) {
return value instanceof PHPReference
|| (value !== null && (typeof value === "object" || typeof value === "function") && value[SYMBOL_PHP_REFERENCE] === true);
}
export function isPHPVariable(value) {
return value instanceof PHPVariable
|| (value !== null && (typeof value === "object" || typeof value === "function") && value[SYMBOL_PHP_VARIABLE] === true);
}
export class PHPLiteral extends PHPReference {
value;
constructor(value) {
super();
this.value = value;
}
async get() {
return this.value;
}
async set(_value) {
throw new PHPFatalError("Literals cannot be changed");
}
toString() {
return this.value === null || this.value === undefined ? "" : String(this.value);
}
valueOf() {
return this.value;
}
[Symbol.toPrimitive](hint) {
if (hint === "number")
return Number(this.value) || 0;
if (hint === "string")
return this.value === null || this.value === undefined ? "" : String(this.value);
return this.value;
}
}
export class PHPPropertyReference extends PHPReference {
ctx;
obj;
prop;
phpName;
constructor(ctx, obj, prop, phpName = prop.startsWith("$") ? prop.slice(1) : prop) {
super();
this.ctx = ctx;
this.obj = obj;
this.prop = prop;
this.phpName = phpName;
}
async get() {
const target = isPHPReference(this.obj) ? await this.obj.get() : this.obj;
if (target === null || target === undefined) {
return this.ctx.getProperty(target, this.prop);
}
const direct = target[this.prop];
if (direct !== undefined) {
return isPHPReference(direct) ? await direct.get() : direct;
}
if (typeof target.__get === "function") {
return await target.__get(this.ctx, new PHPLiteral(this.phpName));
}
return await this.ctx.getProperty(target, this.prop);
}
async set(value) {
const target = isPHPReference(this.obj) ? await this.obj.get() : this.obj;
if (target === null || target === undefined) {
return this.ctx.setProperty(target, this.prop, value);
}
const direct = target[this.prop];
if (isPHPReference(direct))
return await direct.set(value);
if (direct !== undefined) {
target[this.prop] = value;
return value;
}
if (typeof target.__set === "function") {
return await target.__set(this.ctx, new PHPLiteral(this.phpName), new PHPLiteral(value));
}
return await this.ctx.setProperty(target, this.prop, value);
}
async isset() {
const target = isPHPReference(this.obj) ? await this.obj.get() : this.obj;
if (target === null || target === undefined)
return false;
const direct = target[this.prop];
if (direct !== undefined) {
const value = isPHPReference(direct) ? await direct.get() : direct;
return value !== null && value !== undefined;
}
if (typeof target.__isset === "function") {
const result = await target.__isset(this.ctx, new PHPLiteral(this.phpName));
return this.ctx.isTruthy(isPHPReference(result) ? await result.get() : result);
}
return false;
}
async unset() {
const target = isPHPReference(this.obj) ? await this.obj.get() : this.obj;
if (target === null || target === undefined)
return;
if (target[this.prop] === undefined && typeof target.__unset === "function") {
await target.__unset(this.ctx, new PHPLiteral(this.phpName));
return;
}
delete target[this.prop];
}
}
export class PHPArrayOffsetReference extends PHPReference {
container;
key;
constructor(container, key) {
super();
this.container = container;
this.key = key;
}
async get() {
const target = isPHPReference(this.container) ? await this.container.get() : this.container;
if (target === null || (typeof target !== "object" && typeof target !== "function"))
return undefined;
const value = target[this.key];
return isPHPReference(value) ? await value.get() : value;
}
async set(value) {
const target = isPHPReference(this.container) ? await this.container.get() : this.container;
const actual = isPHPReference(value) ? await value.get() : value;
if (target !== null && (typeof target === "object" || typeof target === "function")) {
target[this.key] = actual;
}
return actual;
}
}
export class PHPVariable extends PHPReference {
value;
constructor(initialValue = undefined) {
super();
this.value = initialValue;
}
async get() {
return this.value;
}
async set(value) {
const actual = isPHPReference(value) ? await value.get() : value;
if (Array.isArray(actual)) {
const copy = [...actual];
for (const key of Object.keys(actual)) {
if (isNaN(Number(key)))
copy[key] = actual[key];
}
delete copy.__ptr;
this.value = copy;
return copy;
}
if (typeof actual === "object" && actual !== null && !actual[SYMBOL_PHP_NAME] && actual.constructor === Object) {
const copy = { ...actual };
delete copy.__ptr;
this.value = copy;
return copy;
}
this.value = actual;
return actual;
}
setFrom(otherref) {
if (otherref instanceof PHPVariable)
return this.set(otherref.value);
return super.setFrom(otherref);
}
toString() {
return this.value === null || this.value === undefined ? "" : String(this.value);
}
valueOf() {
return this.value;
}
[Symbol.toPrimitive](hint) {
if (hint === "number")
return Number(this.value) || 0;
if (hint === "string")
return this.value === null || this.value === undefined ? "" : String(this.value);
return this.value;
}
}
Object.defineProperties(PHPReference.prototype, {
[SYMBOL_PHP_REFERENCE]: { value: true },
});
Object.defineProperties(PHPVariable.prototype, {
[SYMBOL_PHP_VARIABLE]: { value: true },
});
//# sourceMappingURL=PHPVariable.js.map