nv-constexpr-paren-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.
package/TEST/func.js ADDED
@@ -0,0 +1,4 @@
1
+ function() {
2
+ this.stt = 888;
3
+ ++this.uuu;
4
+ }
package/TEST/tst.js ADDED
@@ -0,0 +1,5 @@
1
+ const run = require("../index");
2
+
3
+
4
+
5
+ console.log(run({},`act:= path="./func.js"`))
package/index.js ADDED
@@ -0,0 +1,125 @@
1
+ const _fs = require("fs");
2
+ const _path = require("path");
3
+ const vm = require("vm");
4
+
5
+ const {parse_from_str} = require("nvison-bw");
6
+ const {is_jsid_str} = require("nv-char-jsid2");
7
+ const unesc = require("nv-string-unesc");
8
+ const codify = require("nv-constexpr-simple-codify");
9
+
10
+ // `name:= "a" +"b"` -> "ab"
11
+ // `act:= path="./func.js" ` -> body of func.js
12
+
13
+
14
+ const parse = (s)=>{
15
+ var idx = s.indexOf(":=");
16
+ if(idx >=0) {
17
+ var type = s.slice(0,idx).trim().toLowerCase();
18
+ var ison_str = s.slice(idx+2).trim();
19
+ if(type === "name" || type === "nm") {
20
+ return {
21
+ type:"name",
22
+ expr:ison_str
23
+ }
24
+ } else if(type === "action" || type === "act") {
25
+ ison_str = `{${ison_str}}`;
26
+ var ison = parse_from_str(ison_str);
27
+ // .cmt Same As .desc
28
+ // .path
29
+ // .code
30
+ // .code 优先级高于 .path
31
+ if(!Array.isArray(ison) && ison !== null && typeof(ison) === "object") {
32
+ var d = {type:"action"}
33
+ if(ison.cmt !== undefined) {
34
+ d.cmt = String(ison.cmt);
35
+ } else if(ison.desc !== undefined) {
36
+ d.cmt = String(ison.desc)
37
+ } else {}
38
+ if(ison.code) {
39
+ d.code = String(ison.code)
40
+ } else if(ison.path) {
41
+ var code = _fs.readFileSync(_path.resolve(ison.path)).toString();
42
+ if(code.includes("`")) {throw('code can NOT includes "`"');} else {}
43
+ code = code.replace(/[\r\n\t ]+/g," ");
44
+ code = code.trim();
45
+ var rgx0 = /\{(.*)\}$/;
46
+ var r0 = rgx0.exec(code);
47
+ if(r0) {
48
+ d.code = r0[1].trim();
49
+ } else {
50
+ d.code = code;
51
+ }
52
+ } else {}
53
+ if(d.code) {
54
+ return d;
55
+ } else {
56
+ throw(`action content MUST have .path OR .code`);
57
+ }
58
+ } else {
59
+ throw(`action content MUST be object`);
60
+ }
61
+ } else {
62
+ throw(`type can ONLY be : name | action |nm | act`);
63
+ }
64
+ } else {
65
+ throw(`MUST have type(name | action |nm | act): <type>:=... `)
66
+ }
67
+ }
68
+
69
+ class ParenExec {
70
+ context = { ___recv___:null};
71
+ constructor(env) {
72
+ Object.assign(this.context,env);
73
+ vm.createContext(this.context);
74
+ }
75
+ run(raw,keep_cmt=true) {
76
+ try {
77
+ var d = parse(raw);
78
+ this.context.___recv___ = null;
79
+ if(d.type === "name") {
80
+ var expr = d.expr;
81
+ this.context.___recv___ = null;
82
+ vm.runInContext(`___recv___ = ${expr};`, this.context);
83
+ var r = this.context.___recv___;
84
+ var [cond,reason] = codify.is_supported(r,true);
85
+ if(cond) {
86
+ if(typeof(r) === "string") {
87
+ r= unesc(r);
88
+ if(is_jsid_str(r)) {
89
+ return [true,r];
90
+ } else {
91
+ return [false,`${r} NOT valid identifier`];
92
+ }
93
+ } else {
94
+ return [false,"name MUST be string"];
95
+ }
96
+ } else {
97
+ return [false,reason]
98
+ }
99
+ } else {
100
+ //d.type === "action"
101
+ var cmt = d.cmt;
102
+ var code = d.code;
103
+ if(cmt !== undefined) {
104
+ return `${code}/*${cmt}*/`;
105
+ } else {
106
+ return `${code}`;
107
+ }
108
+ }
109
+ } catch(e) {
110
+ return [false,e.message]
111
+ }
112
+ }
113
+ }
114
+
115
+ const creat = (env) => new ParenExec(env)
116
+ const run = (env,code,keep_cmt=true)=>{
117
+ var ctx = creat(env);
118
+ return ctx.run(code);
119
+ }
120
+ module.exports = run;
121
+ module.exports.parse = parse;
122
+ module.exports.creat = creat;
123
+ module.exports.ParenExec = ParenExec;
124
+
125
+
package/package.json ADDED
@@ -0,0 +1,30 @@
1
+ {
2
+ "name": "nv-constexpr-paren-exec",
3
+ "version": "1.0.0",
4
+ "main": "index.js",
5
+ "dependencies": {
6
+ "nv-char-basic": "^1.0.3",
7
+ "nv-char-jsid2": "^1.0.8",
8
+ "nv-char-whitespace": "^1.0.7",
9
+ "nv-constexpr-simple-codify": "^1.0.5",
10
+ "nv-facutil-csutil": "^1.0.21",
11
+ "nv-facutil-untf": "^1.0.2",
12
+ "nv-string-unesc": "^1.0.0",
13
+ "nvison-bw": "^1.0.0",
14
+ "nvison-cfg": "^1.1.0",
15
+ "nvison-obj-class": "^1.2.2",
16
+ "nvison-obj-typedef": "^1.1.0",
17
+ "nvison-parse-d": "^1.2.1",
18
+ "nvison-parse-engine": "^1.2.1",
19
+ "nvison-parse-internal-bw": "^1.2.2",
20
+ "nvison-parse-scope": "^1.1.1",
21
+ "nvison-parse-state": "^1.0.12"
22
+ },
23
+ "devDependencies": {},
24
+ "scripts": {
25
+ "test": "echo \"Error: no test specified\" && exit 1"
26
+ },
27
+ "author": "",
28
+ "license": "ISC",
29
+ "description": ""
30
+ }