Repository navigation
Expand file tree
/
Copy pathPHPVariable.ts
More file actions
240 lines (194 loc) · 7.21 KB
/
Copy pathPHPVariable.ts
File metadata and controls
240 lines (194 loc) · 7.21 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import type { PHPContext } from "../PHPContext.js";
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 abstract class PHPReference {
public static [Symbol.hasInstance](value: unknown): boolean {
if (Function.prototype[Symbol.hasInstance].call(this, value)) return true;
return this === PHPReference
&& value !== null
&& (typeof value === "object" || typeof value === "function")
&& (value as Record<symbol, unknown>)[SYMBOL_PHP_REFERENCE] === true;
}
public abstract get(): Promise<any>;
public abstract set(value: any): Promise<any>;
public async setFrom(otherref: PHPReference): Promise<any> {
return await this.set(await otherref.get());
}
public async isset(): Promise<boolean> {
const value = await this.get();
return value !== null && value !== undefined;
}
public async unset(): Promise<void> {
await this.set(undefined);
}
}
export function isPHPReference(value: any): value is PHPReference {
return value instanceof PHPReference
|| (value !== null && (typeof value === "object" || typeof value === "function") && value[SYMBOL_PHP_REFERENCE] === true);
}
export function isPHPVariable(value: any): boolean {
return value instanceof PHPVariable
|| (value !== null && (typeof value === "object" || typeof value === "function") && value[SYMBOL_PHP_VARIABLE] === true);
}
export class PHPLiteral extends PHPReference {
private readonly value: any;
constructor(value: any) {
super();
this.value = value;
}
public async get(): Promise<any> {
return this.value;
}
public async set(_value: any): Promise<never> {
throw new PHPFatalError("Literals cannot be changed");
}
public toString(): string {
return this.value === null || this.value === undefined ? "" : String(this.value);
}
public valueOf(): any {
return this.value;
}
public [Symbol.toPrimitive](hint: string): any {
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 {
constructor(
private readonly ctx: PHPContext,
private readonly obj: any,
private readonly prop: string,
private readonly phpName = prop.startsWith("$") ? prop.slice(1) : prop
) {
super();
}
public async get(): Promise<any> {
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);
}
public async set(value: any): Promise<any> {
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);
}
public async isset(): Promise<boolean> {
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;
}
public async unset(): Promise<void> {
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 {
constructor(
private readonly container: any,
private readonly key: any
) {
super();
}
public async get(): Promise<any> {
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;
}
public async set(value: any): Promise<any> {
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 {
private value: any;
constructor(initialValue: any = undefined) {
super();
this.value = initialValue;
}
public async get(): Promise<any> {
return this.value;
}
public async set(value: any): Promise<any> {
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 as any)[key] = actual[key];
}
delete (copy as any).__ptr;
this.value = copy;
return copy;
}
if (typeof actual === "object" && actual !== null && !actual[SYMBOL_PHP_NAME] && actual.constructor === Object) {
const copy = { ...actual };
delete (copy as any).__ptr;
this.value = copy;
return copy;
}
this.value = actual;
return actual;
}
public setFrom(otherref: PHPReference): Promise<any> {
if (otherref instanceof PHPVariable) return this.set(otherref.value);
return super.setFrom(otherref);
}
public toString(): string {
return this.value === null || this.value === undefined ? "" : String(this.value);
}
public valueOf(): any {
return this.value;
}
public [Symbol.toPrimitive](hint: string): any {
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 },
});