nv-constexpr-paren-exec 1.0.1 → 1.1.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 CHANGED
@@ -1,4 +1,2 @@
1
- function() {
2
1
  this.stt = 888;
3
2
  ++this.uuu;
4
- }
package/TEST/tst.js CHANGED
@@ -1,5 +1,7 @@
1
1
  const run = require("../index");
2
2
 
3
+ console.log(run(`./func.js`))
3
4
 
4
-
5
- console.log(run({},`act:= path="./func.js"`))
5
+ console.log(run(`./func.js`,0));
6
+ console.log(run(`./func.js`,1));
7
+ console.log(run(`./func.js`,2));
package/index.js CHANGED
@@ -1,143 +1,52 @@
1
- const _fs = require("fs");
2
- const _path = require("path");
3
- const vm = require("vm");
1
+ const _fs = require("fs");
2
+ const _path = require("path");
4
3
 
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
- // `indent :=<depth> ... `
13
- const parse = (s)=>{
14
- var idx = s.indexOf(":=");
15
- if(idx >=0) {
16
- var type = s.slice(0,idx).trim().toLowerCase();
17
- var ison_str = s.slice(idx+2).trim();
18
- if(type === "name" || type === "nm") {
19
- return {
20
- type:"name",
21
- expr:ison_str
22
- }
23
- } else if(type === "action" || type === "act") {
24
- ison_str = `{${ison_str}}`;
25
- var ison = parse_from_str(ison_str);
26
- // .cmt Same As .desc
27
- // .path
28
- // .code
29
- // .code 优先级高于 .path
30
- if(!Array.isArray(ison) && ison !== null && typeof(ison) === "object") {
31
- var d = {type:"action"}
32
- if(ison.cmt !== undefined) {
33
- d.cmt = String(ison.cmt);
34
- } else if(ison.desc !== undefined) {
35
- d.cmt = String(ison.desc)
36
- } else {}
37
- if(ison.code) {
38
- d.code = String(ison.code)
39
- } else if(ison.path) {
40
- var code = _fs.readFileSync(_path.resolve(ison.path)).toString();
41
- if(code.includes("`")) {throw('code can NOT includes "`"');} else {}
42
- code = code.replace(/[\r\n\t ]+/g," ");
43
- code = code.trim();
44
- var rgx0 = /\{(.*)\}$/;
45
- var r0 = rgx0.exec(code);
46
- if(r0) {
47
- d.code = r0[1].trim();
48
- } else {
49
- d.code = code;
50
- }
51
- } else {}
52
- if(d.code) {
53
- return d;
54
- } else {
55
- throw(`action content MUST have .path OR .code`);
56
- }
57
- } else {
58
- throw(`action content MUST be object`);
59
- }
60
- } else if(type === "indent") {
61
- var d = {type:"indent"};
62
- var rgx = /^([0-9]+)(.*)$/;
63
- var r = rgx.exec(ison_str);
64
- var depth = parseInt(r[1]);
65
- if(!Object.is(depth,NaN)) {
66
- var code = r[2];
67
- if(code.includes("`")) {throw('code can NOT includes "`"');} else {}
68
- code = code.replace(/[\t ]+/g," ");
69
- code = code.replace(/[\r\n]+/g,"\n");
70
- var lns = code.splice("\n");
71
- lns = lns.map(ln=>" ".repeat(depth)+ln);
72
- d.code = "\n" +lns.join("\n") + "\n";
73
- return d;
74
- } else {
75
- throw(`indent :=<int> ${depth} is MUST be int`)
76
- }
77
- } else {
78
- throw(`type can ONLY be : name | action |nm | act | indent`);
79
- }
80
- } else {
81
- throw(`MUST have type(name | action |nm | act): <type>:=... `)
4
+ const topl = (p)=> {
5
+ p = _path.resolve(p);
6
+ var pl = [];
7
+ while(true) {
8
+ var desc = _path.parse(p);
9
+ if(desc.dir !== p) {
10
+ pl.push(desc.name);
11
+ p = desc.dir;
12
+ } else {
13
+ break;
14
+ }
82
15
  }
16
+ return pl;
83
17
  }
84
18
 
85
- class ParenExec {
86
- context = { ___recv___:null};
87
- constructor(env) {
88
- Object.assign(this.context,env);
89
- vm.createContext(this.context);
90
- }
91
- run(raw,keep_cmt=true) {
92
- try {
93
- var d = parse(raw);
94
- this.context.___recv___ = null;
95
- if(d.type === "name") {
96
- var expr = d.expr;
97
- this.context.___recv___ = null;
98
- vm.runInContext(`___recv___ = ${expr};`, this.context);
99
- var r = this.context.___recv___;
100
- var [cond,reason] = codify.is_supported(r,true);
101
- if(cond) {
102
- if(typeof(r) === "string") {
103
- r= unesc(r);
104
- if(is_jsid_str(r)) {
105
- return [true,r];
106
- } else {
107
- return [false,`${r} NOT valid identifier`];
108
- }
109
- } else {
110
- return [false,"name MUST be string"];
111
- }
112
- } else {
113
- return [false,reason]
114
- }
115
- } else if(d.type === "action"){
116
- var cmt = d.cmt;
117
- var code = d.code;
118
- if(cmt !== undefined) {
119
- return [true,`${code}/*${cmt}*/`];
120
- } else {
121
- return [true,`${code}`];
122
- }
123
- } else {
124
- //d.type === "indent"
125
- return [true, d.code];
126
- }
127
- } catch(e) {
128
- return [false,e.message]
129
- }
19
+ const run = (path, fn_as_cmt_depth=1)=>{
20
+ path = _path.resolve(path);
21
+ if(_fs.existsSync(path)) {
22
+ var stat = _fs.lstatSync(path);
23
+ if(stat.isFile()) {
24
+ var code = _fs.readFileSync(path).toString();
25
+ if(code.includes("`")) {
26
+ return [false, "macro code can NOT includes '`' "];
27
+ } else {
28
+ code = code.trim();
29
+ code = code.replace(/[\t ]+/g, " ");
30
+ code = code.replace(/[\n\r]+/g, "\n");
31
+ var pl = topl(path);
32
+ var cnt = pl.length;
33
+ pl = pl.slice(0,fn_as_cmt_depth);
34
+ pl.reverse();
35
+ var cmt = pl.join("/");
36
+ if(cmt!== "") {
37
+ code = `${code}/*${cmt}*/`;
38
+ } else {
39
+ }
40
+ return [true,code];
41
+ }
42
+ } else {
43
+ return [false,`${path} NOT file`]
44
+ }
45
+ } else {
46
+ return [false,`${path} NOT exists`]
130
47
  }
131
48
  }
132
49
 
133
- const creat = (env) => new ParenExec(env)
134
- const run = (env,code,keep_cmt=true)=>{
135
- var ctx = creat(env);
136
- return ctx.run(code);
137
- }
50
+ run.topl = topl;
138
51
  module.exports = run;
139
- module.exports.parse = parse;
140
- module.exports.creat = creat;
141
- module.exports.ParenExec = ParenExec;
142
-
143
52
 
package/package.json CHANGED
@@ -1,26 +1,7 @@
1
1
  {
2
2
  "name": "nv-constexpr-paren-exec",
3
- "version": "1.0.1",
3
+ "version": "1.1.0",
4
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
5
  "scripts": {
25
6
  "test": "echo \"Error: no test specified\" && exit 1"
26
7
  },