Repository navigation
Expand file tree
/
Copy pathnodejs.js
More file actions
189 lines (189 loc) · 7.4 KB
/
Copy pathnodejs.js
File metadata and controls
189 lines (189 loc) · 7.4 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
import { createRequire } from "module";
import { PHPExtension } from "../PHPExtension.js";
import { PHPReference } from "../runtime/PHPVariable.js";
import { SYMBOL_PHP_NEW } from "../runtime/Reflection.js";
export const SYMBOL_PHP_NODEJS_PROXY = Symbol.for("php.nodejsProxy");
export const SYMBOL_PHP_NODEJS_VALUE = Symbol.for("php.nodejsValue");
const customRequire = createRequire(import.meta.url);
export function wrapJSValue(val) {
if (val === null || val === undefined)
return val;
if (typeof val === "boolean" || typeof val === "number" || typeof val === "string")
return val;
if (Array.isArray(val)) {
return val.map(wrapJSValue);
}
if (typeof val !== "object" && typeof val !== "function") {
return val;
}
if (val[SYMBOL_PHP_NODEJS_PROXY]) {
return val[SYMBOL_PHP_NODEJS_PROXY];
}
const proxy = new Proxy(val, {
get(target, prop, receiver) {
if (prop === SYMBOL_PHP_NODEJS_PROXY || prop === SYMBOL_PHP_NODEJS_VALUE) {
return target;
}
if (prop === SYMBOL_PHP_NEW) {
return async (ctx, ...args) => {
const unwrappedArgs = await Promise.all(args.map(unwrapPHPValue));
if (typeof target === "function") {
try {
const instance = new target(...unwrappedArgs);
return wrapJSValue(instance);
}
catch {
const instance = target(...unwrappedArgs);
return wrapJSValue(instance);
}
}
return null;
};
}
if (prop === "get" ||
prop === "set" ||
prop === "then" ||
prop === "catch" ||
prop === "finally" ||
typeof prop === "symbol") {
return target[prop];
}
const propStr = typeof prop === "string" ? (prop.startsWith("$") ? prop.slice(1) : prop) : String(prop);
const lowerProp = propStr.toLowerCase();
let targetName = propStr;
if (target && (typeof target === "object" || typeof target === "function")) {
if (!(propStr in target)) {
let curr = target;
while (curr && curr !== Object.prototype) {
for (const p of Object.getOwnPropertyNames(curr)) {
if (p.toLowerCase() === lowerProp) {
targetName = p;
break;
}
}
curr = Object.getPrototypeOf(curr);
}
}
}
const item = target[targetName];
if (typeof item === "function") {
const boundFn = item.bind(target);
return async (ctx, ...args) => {
const unwrappedArgs = await Promise.all(args.map(unwrapPHPValue));
const res = await Promise.resolve(boundFn(...unwrappedArgs));
return wrapJSValue(res);
};
}
return wrapJSValue(item);
},
set(target, prop, value, receiver) {
const propStr = typeof prop === "string" ? (prop.startsWith("$") ? prop.slice(1) : prop) : String(prop);
const lowerProp = propStr.toLowerCase();
let targetName = propStr;
if (target && (typeof target === "object" || typeof target === "function")) {
if (!(propStr in target)) {
let curr = target;
while (curr && curr !== Object.prototype) {
for (const p of Object.getOwnPropertyNames(curr)) {
if (p.toLowerCase() === lowerProp) {
targetName = p;
break;
}
}
curr = Object.getPrototypeOf(curr);
}
}
}
target[targetName] = value;
return true;
},
async apply(target, thisArg, argArray) {
if (typeof target === "function") {
const unwrappedArgs = await Promise.all((argArray || []).slice(1).map(unwrapPHPValue));
const res = await target.apply(thisArg, unwrappedArgs);
return wrapJSValue(res);
}
return undefined;
}
});
try {
Object.defineProperty(val, SYMBOL_PHP_NODEJS_PROXY, {
value: proxy,
writable: false,
configurable: true,
enumerable: false,
});
}
catch { }
return proxy;
}
export async function unwrapPHPValue(val) {
if (val === null || val === undefined)
return val;
const actual = val instanceof PHPReference ? await val.get() : val;
if (actual && (typeof actual === "object" || typeof actual === "function") && actual[SYMBOL_PHP_NODEJS_PROXY]) {
return actual[SYMBOL_PHP_NODEJS_PROXY];
}
if (actual && (typeof actual === "object" || typeof actual === "function") && actual[SYMBOL_PHP_NODEJS_VALUE]) {
return actual[SYMBOL_PHP_NODEJS_VALUE];
}
if (Array.isArray(actual)) {
return await Promise.all(actual.map(unwrapPHPValue));
}
return actual;
}
export class NodeJSExtension extends PHPExtension {
name = "nodejs";
onInit(engine) {
const njs_import = async (ctx, moduleNameArg) => {
const moduleName = String(await moduleNameArg?.get() ?? "");
try {
const mod = customRequire(moduleName);
return wrapJSValue(mod);
}
catch {
return null;
}
};
const njs_global = async (ctx, nameArg) => {
const name = String(await nameArg?.get() ?? "");
const val = globalThis[name];
return wrapJSValue(val);
};
const njs_eval = async (ctx, codeArg) => {
const code = String(await codeArg?.get() ?? "");
const fn = new Function("process", "global", `return (async () => { return (${code}); })();`);
const res = await fn(process, globalThis);
return wrapJSValue(res);
};
const njs_new = async (ctx, classNameArg, ...args) => {
const className = String(await classNameArg?.get() ?? "");
const unwrappedArgs = await Promise.all(args.map(unwrapPHPValue));
let targetClass = globalThis[className];
if (!targetClass) {
try {
targetClass = customRequire(className);
}
catch {
targetClass = null;
}
}
if (typeof targetClass === "function") {
const instance = new targetClass(...unwrappedArgs);
return wrapJSValue(instance);
}
return null;
};
this.functions = {
njs_import,
njs_global,
njs_eval,
njs_new,
nodejs_require: njs_import,
nodejs_global: njs_global,
nodejs_eval: njs_eval,
nodejs_new: njs_new,
};
}
}
//# sourceMappingURL=nodejs.js.map