nv-constexpr-angle-exec 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,84 @@
1
+ // tst-proto.js
2
+ const vm = require("vm");
3
+
4
+ // 原始对象
5
+ var obj = {
6
+ bigint: 9007199254740993n,
7
+ num: 123.456,
8
+ str: 'hello\\nworld',
9
+ bool: true,
10
+ nil: null,
11
+ undef: undefined
12
+ }
13
+
14
+ console.log("=== 原始对象 ===");
15
+ console.log("obj:", obj);
16
+ console.log("Object.getPrototypeOf(obj) === Object.prototype:", Object.getPrototypeOf(obj) === Object.prototype);
17
+ console.log("Object.getPrototypeOf(obj):", Object.getPrototypeOf(obj));
18
+ console.log("Object.prototype:", Object.prototype);
19
+ console.log("");
20
+
21
+ // 创建 context
22
+ const context = { input: obj };
23
+ vm.createContext(context);
24
+
25
+ // 在 vm 中执行代码,直接返回对象
26
+ vm.runInContext(`result = input;`, context);
27
+
28
+ console.log("=== vm 执行后 ===");
29
+ console.log("context.result:", context.result);
30
+ console.log("Object.getPrototypeOf(context.result) === Object.prototype:", Object.getPrototypeOf(context.result) === Object.prototype);
31
+ console.log("Object.getPrototypeOf(context.result):", Object.getPrototypeOf(context.result));
32
+ console.log("");
33
+
34
+ // 实验 2:在 vm 中创建新对象
35
+ vm.runInContext(`newObj = { a: 1, b: 2 };`, context);
36
+
37
+ console.log("=== vm 中创建的新对象 ===");
38
+ console.log("context.newObj:", context.newObj);
39
+ console.log("Object.getPrototypeOf(context.newObj) === Object.prototype:", Object.getPrototypeOf(context.newObj) === Object.prototype);
40
+ console.log("Object.getPrototypeOf(context.newObj):", Object.getPrototypeOf(context.newObj));
41
+ console.log("");
42
+
43
+ // 实验 3:检查 vm context 本身的原型
44
+ console.log("=== vm context 原型 ===");
45
+ console.log("Object.getPrototypeOf(context):", Object.getPrototypeOf(context));
46
+ console.log("Object.getPrototypeOf(context) === Object.prototype:", Object.getPrototypeOf(context) === Object.prototype);
47
+ console.log("");
48
+
49
+ // 实验 4:检查 vm context 的属性描述符
50
+ console.log("=== vm context 属性描述符 ===");
51
+ console.log("Object.getOwnPropertyDescriptor(context, 'result'):", Object.getOwnPropertyDescriptor(context, 'result'));
52
+ console.log("");
53
+
54
+ // 实验 5:尝试用 Object.create(null) 在 vm 中创建对象
55
+ vm.runInContext(`nullProtoObj = Object.create(null); nullProtoObj.x = 1;`, context);
56
+
57
+ console.log("=== vm 中 Object.create(null) 创建的对象 ===");
58
+ console.log("context.nullProtoObj:", context.nullProtoObj);
59
+ console.log("Object.getPrototypeOf(context.nullProtoObj):", Object.getPrototypeOf(context.nullProtoObj));
60
+ console.log("");
61
+
62
+
63
+ // 实验 7:检查隐藏的 [[Prototype]]
64
+ console.log("=== 深入检查原型 ===");
65
+ const resultProto = Object.getPrototypeOf(context.result);
66
+ console.log("resultProto === Object.prototype:", resultProto === Object.prototype);
67
+ console.log("resultProto == Object.prototype:", resultProto == Object.prototype); // 宽松相等
68
+ console.log("resultProto instanceof Object:", resultProto instanceof Object);
69
+ console.log("Object.prototype instanceof Object:", Object.prototype instanceof Object);
70
+ console.log("");
71
+
72
+ // 实验 8:检查属性是否可枚举
73
+ console.log("=== 检查属性枚举 ===");
74
+ console.log("Object.keys(context.result):", Object.keys(context.result));
75
+ console.log("Object.getOwnPropertyNames(context.result):", Object.getOwnPropertyNames(context.result));
76
+ console.log("");
77
+
78
+ // 实验 9:尝试修改原型
79
+ try {
80
+ Object.setPrototypeOf(context.result, null);
81
+ console.log("成功将 context.result 的原型设为 null");
82
+ } catch (e) {
83
+ console.log("无法修改 context.result 的原型:", e.message);
84
+ }
@@ -0,0 +1,38 @@
1
+ const vm = require("vm");
2
+
3
+ // 外部环境的 Object.prototype
4
+ console.log("=== 外部环境 ===");
5
+ console.log("Object.prototype:", Object.prototype);
6
+ console.log("Object.prototype === Object.prototype:", Object.prototype === Object.prototype);
7
+
8
+ // 创建 vm context
9
+ const context = {};
10
+ vm.createContext(context);
11
+
12
+ // 在 vm 中获取 Object.prototype
13
+ vm.runInContext(`
14
+ vmObjectPrototype = Object.prototype;
15
+ vmGlobalObject = globalThis;
16
+ `, context);
17
+
18
+ console.log("\n=== vm 环境 ===");
19
+ console.log("context.vmObjectPrototype:", context.vmObjectPrototype);
20
+ console.log("context.vmGlobalObject:", context.vmGlobalObject);
21
+ console.log("");
22
+
23
+ // 比较
24
+ console.log("=== 比较 ===");
25
+ console.log("Object.prototype === context.vmObjectPrototype:", Object.prototype === context.vmObjectPrototype);
26
+ console.log("Object.prototype == context.vmObjectPrototype:", Object.prototype == context.vmObjectPrototype);
27
+
28
+ // 检查 vm 中创建的对象原型
29
+ vm.runInContext(`
30
+ vmObj = { a: 1 };
31
+ vmObjProto = Object.getPrototypeOf(vmObj);
32
+ `, context);
33
+
34
+ console.log("\n=== vm 中创建的对象 ===");
35
+ console.log("context.vmObj:", context.vmObj);
36
+ console.log("context.vmObjProto:", context.vmObjProto);
37
+ console.log("context.vmObjProto === Object.prototype:", context.vmObjProto === Object.prototype);
38
+ console.log("context.vmObjProto === context.vmObjectPrototype:", context.vmObjProto === context.vmObjectPrototype);
package/TEST/tst.js ADDED
@@ -0,0 +1,220 @@
1
+ const run = require('../index');
2
+ const codify = require('nv-constexpr-simple-codify');
3
+
4
+ console.log("========== AngleExec 测试 ==========\n");
5
+
6
+ // ============ 测试 1: 基础支持类型 ============
7
+ console.log("测试 1: 基础支持类型");
8
+ const basicEnv = {
9
+ bigint: 9007199254740993n,
10
+ num: 123.456,
11
+ str: "hello\\nworld",
12
+ bool: true,
13
+ nil: null,
14
+ undef: undefined
15
+ };
16
+
17
+ const [ok1, result1] = run(basicEnv, `({ bigint, num, str, bool, nil, undef })`);
18
+ console.log("Result:", ok1, result1);
19
+ console.log("");
20
+
21
+ // ============ 测试 2: TypedArrays ============
22
+ console.log("测试 2: TypedArrays");
23
+ const typedArrayEnv = {
24
+ uint8: new Uint8Array([1, 2, 255]),
25
+ int32: new Int32Array([-2147483648, 2147483647]),
26
+ float64: new Float64Array([1.7976931348623157e+308, -Infinity, NaN])
27
+ };
28
+
29
+ const [ok2, result2] = run(typedArrayEnv, `({ uint8, int32, float64 })`);
30
+ console.log("Result:", ok2, result2);
31
+ console.log("");
32
+
33
+ // ============ 测试 3: Date 和 RegExp ============
34
+ console.log("测试 3: Date 和 RegExp");
35
+ const dateRegexEnv = {
36
+ date: new Date('2024-01-15T12:30:45.123Z'),
37
+ regex: /test\/pattern/gi
38
+ };
39
+
40
+ const [ok3, result3] = run(dateRegexEnv, `({ date, regex })`);
41
+ console.log("Result:", ok3, result3);
42
+ console.log("");
43
+
44
+ // ============ 测试 4: 嵌套结构 ============
45
+ console.log("测试 4: 嵌套结构");
46
+ const nestedEnv = {
47
+ nested: {
48
+ level2: {
49
+ value: 789n,
50
+ arr: [1n, 2n, 3n]
51
+ }
52
+ }
53
+ };
54
+
55
+ const [ok4, result4] = run(nestedEnv, `({ nested })`);
56
+ console.log("Result:", ok4, result4);
57
+ console.log("");
58
+
59
+
60
+ // ============ 测试 16: 不支持的类型 - BigInt too large for int64 (testing copy v8 bigint.cc scenario) ============
61
+ console.log("测试 16: BigInt 超出 int64 范围 (理论上支持,但需要 v8 bigint.cc)");
62
+ const hugeBigIntEnv = { huge: 1844674407370955161612345678901234567890n };
63
+
64
+ const [ok16, result16] = run(hugeBigIntEnv, `huge`);
65
+ console.log("Result:", ok16, result16);
66
+ console.log("Note: This is supported by codify, but C++ may need v8 bigint.cc for such large values");
67
+ console.log("");
68
+
69
+ // ============ 测试 17: 复杂嵌套 + 多种类型 ============
70
+ console.log("测试 17: 复杂嵌套 + 多种类型");
71
+ const complexEnv = {
72
+ meta: {
73
+ created: new Date(),
74
+ version: 1.0,
75
+ enabled: true
76
+ },
77
+ data: {
78
+ buffer: new ArrayBuffer(8),
79
+ view: new DataView(new ArrayBuffer(16)),
80
+ numbers: new Float32Array([1.5, -2.5, 0.0])
81
+ },
82
+ items: [
83
+ { id: 1, value: 10n },
84
+ { id: 2, value: 20n }
85
+ ]
86
+ };
87
+
88
+ // 填充 buffer
89
+ new Uint8Array(complexEnv.data.buffer).set([0, 32, 64, 96, 128, 160, 192, 224]);
90
+ new Uint8Array(complexEnv.data.view.buffer).set(new Array(16).fill(0));
91
+
92
+ const [ok17, result17] = run(complexEnv, `({ meta, data, items })`);
93
+ console.log("Result:", ok17, result17);
94
+ console.log("");
95
+
96
+ // ============ 测试 18: 空数组和空对象 ============
97
+ console.log("测试 18: 空数组和空对象");
98
+ const emptyEnv = {
99
+ emptyArr: [],
100
+ emptyObj: {}
101
+ };
102
+
103
+ const [ok18, result18] = run(emptyEnv, `({ emptyArr, emptyObj })`);
104
+ console.log("Result:", ok18, result18);
105
+ console.log("");
106
+
107
+ // ============ 测试 19: 只有 undefined 和 null ============
108
+ console.log("测试 19: 只有 undefined 和 null");
109
+ const nullishEnv = {
110
+ undef: undefined,
111
+ nil: null
112
+ };
113
+
114
+ const [ok19, result19] = run(nullishEnv, `({ undef, nil })`);
115
+ console.log("Result:", ok19, result19);
116
+ console.log("");
117
+
118
+
119
+
120
+
121
+
122
+ // ============ 测试 5: 循环引用 ============
123
+ console.log("测试 5: 循环引用");
124
+ const circObj = { a: 1 };
125
+ circObj.self = circObj;
126
+ const circEnv = { circ: circObj };
127
+
128
+ console.log("is_supported:", codify.is_supported(circEnv))
129
+
130
+
131
+ const [ok5, result5] = run(circEnv, `circ`);
132
+ console.log("Result:", ok5, result5);
133
+ console.log("");
134
+
135
+ // ============ 测试 6: 不支持的类型 - Set ============
136
+ console.log("测试 6: 不支持的类型 - Set");
137
+ const setEnv = { mySet: new Set([1, 2, 3]) };
138
+
139
+ const [ok6, result6] = run(setEnv, `mySet`);
140
+ console.log("Result:", ok6, result6);
141
+ console.log("");
142
+
143
+ // ============ 测试 7: 不支持的类型 - Map ============
144
+ console.log("测试 7: 不支持的类型 - Map");
145
+ const mapEnv = { myMap: new Map([['a', 1], ['b', 2]]) };
146
+
147
+ const [ok7, result7] = run(mapEnv, `myMap`);
148
+ console.log("Result:", ok7, result7);
149
+ console.log("");
150
+
151
+ // ============ 测试 8: 不支持的类型 - Promise ============
152
+ console.log("测试 8: 不支持的类型 - Promise");
153
+ const promiseEnv = { myPrms: Promise.resolve(42) };
154
+
155
+ const [ok8, result8] = run(promiseEnv, `myPrms`);
156
+ console.log("Result:", ok8, result8);
157
+ console.log("");
158
+
159
+ // ============ 测试 9: 不支持的类型 - Function ============
160
+ console.log("测试 9: 不支持的类型 - Function");
161
+ const funcEnv = { myFunc: function() { return 1; } };
162
+
163
+ const [ok9, result9] = run(funcEnv, `myFunc`);
164
+ console.log("Result:", ok9, result9);
165
+ console.log("");
166
+
167
+ // ============ 测试 10: 不支持的类型 - Symbol ============
168
+ console.log("测试 10: 不支持的类型 - Symbol");
169
+ const symbolEnv = { mySym: Symbol("test") };
170
+
171
+ const [ok10, result10] = run(symbolEnv, `mySym`);
172
+ console.log("Result:", ok10, result10);
173
+ console.log("");
174
+
175
+ // ============ 测试 11: 不支持的类型 - Generator ============
176
+ console.log("测试 11: 不支持的类型 - Generator");
177
+ function* gen() { yield 1; }
178
+ const genEnv = { myGen: gen() };
179
+
180
+ const [ok11, result11] = run(genEnv, `myGen`);
181
+ console.log("Result:", ok11, result11);
182
+ console.log("");
183
+
184
+ // ============ 测试 12: 不支持的类型 - WeakMap ============
185
+ console.log("测试 12: 不支持的类型 - WeakMap");
186
+ const weakMapEnv = { myWeakMap: new WeakMap() };
187
+
188
+ const [ok12, result12] = run(weakMapEnv, `myWeakMap`);
189
+ console.log("Result:", ok12, result12);
190
+ console.log("");
191
+
192
+ // ============ 测试 13: 不支持的类型 - WeakSet ============
193
+ console.log("测试 13: 不支持的类型 - WeakSet");
194
+ const weakSetEnv = { myWeakSet: new WeakSet() };
195
+
196
+ const [ok13, result13] = run(weakSetEnv, `myWeakSet`);
197
+ console.log("Result:", ok13, result13);
198
+ console.log("");
199
+
200
+ // ============ 测试 14: 不支持的类型 - WeakRef ============
201
+ console.log("测试 14: 不支持的类型 - WeakRef");
202
+ const weakRefEnv = { myWeakRef: new WeakRef({}) };
203
+
204
+ const [ok14, result14] = run(weakRefEnv, `myWeakRef`);
205
+ console.log("Result:", ok14, result14);
206
+ console.log("");
207
+
208
+ // ============ 测试 15: 不支持的类型 - Iterator ============
209
+ console.log("测试 15: 不支持的类型 - Iterator");
210
+ const iterEnv = { myIter: [1, 2, 3][Symbol.iterator]() };
211
+
212
+ const [ok15, result15] = run(iterEnv, `myIter`);
213
+ console.log("Result:", ok15, result15);
214
+ console.log("");
215
+
216
+
217
+ console.log("========== 测试完成 ==========");
218
+
219
+
220
+
package/index.js ADDED
@@ -0,0 +1,33 @@
1
+ const vm = require("vm");
2
+ const codify = require("nv-constexpr-simple-codify");
3
+
4
+
5
+ class AngleExec {
6
+ context = { ___recv___:null};
7
+ constructor(env) {
8
+ Object.assign(this.context,env);
9
+ vm.createContext(this.context);
10
+ }
11
+ run(code) {
12
+ this.context.___recv___ = null;
13
+ vm.runInContext(`___recv___ = ${code};`, this.context);
14
+ var r = this.context.___recv___;
15
+ var [cond,reason] = codify.is_supported(r,true);
16
+ if(cond) {
17
+ return [true, codify(r)]
18
+ } else {
19
+ return [false,reason]
20
+ }
21
+ }
22
+ }
23
+
24
+ const creat = (env) => new AngleExec(env)
25
+ const run = (env,code)=>{
26
+ var ctx = creat(env);
27
+ return ctx.run(code);
28
+ }
29
+ module.exports = run;
30
+ module.exports.creat = creat;
31
+ module.exports.AngleExec = AngleExec;
32
+
33
+
package/package.json ADDED
@@ -0,0 +1,14 @@
1
+ {
2
+ "name": "nv-constexpr-angle-exec",
3
+ "version": "1.0.0",
4
+ "main": "index.js",
5
+ "scripts": {
6
+ "test": "echo \"Error: no test specified\" && exit 1"
7
+ },
8
+ "author": "",
9
+ "license": "ISC",
10
+ "description": "",
11
+ "dependencies": {
12
+ "nv-constexpr-simple-codify": "^1.0.5"
13
+ }
14
+ }