efront 3.36.5 → 3.36.7

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.
@@ -17,5 +17,16 @@ var Symbol = this.Symbol || function () {
17
17
  };
18
18
 
19
19
  Symbol.prototype = prototype;
20
+ Symbol.iterator = Symbol('iterator');
21
+ Symbol.asyncIterator = Symbol('asyncIterator');
22
+ Array.prototype[Symbol.iterator] = function () {
23
+ var arr = this, cx = 0, dx = arr.length;
24
+ return {
25
+ next() {
26
+ if (cx < dx) return { value: arr[cx++], done: false };
27
+ return { value: undefined, done: true };
28
+ }
29
+ }
30
+ };
20
31
  return Symbol;
21
32
  }();
@@ -0,0 +1,35 @@
1
+ class Generator {
2
+ constructor(f) {
3
+ this.state = "suspended";
4
+ this.exec = f;
5
+ };
6
+ throw(e) {
7
+ delete this.exec;
8
+ this.state = "closed";
9
+ throw e;
10
+ }
11
+ return(value) {
12
+ delete this.exec;
13
+ this.state = "closed";
14
+ return { value: undefined, done: true };
15
+ }
16
+ next() {
17
+ if (this.exec) {
18
+ var exec = this.exec;
19
+ delete this.exec;
20
+ delete this.value;
21
+ exec(this.return, this.throw, (value, exec) => {
22
+ this.value = value;
23
+ this.exec = exec;
24
+ });
25
+ }
26
+ return { value: this.value, done: !this.exec };
27
+ }
28
+ }
29
+ Generator.prototype[Symbol.iterator] = function () {
30
+ return this;
31
+ };
32
+
33
+ function aster_() {
34
+ return new Generator(exec_.bind(this, arguments));
35
+ }
@@ -0,0 +1,50 @@
1
+ class AsyncGenerator {
2
+ constructor(f) {
3
+ this.state = "suspended";
4
+ this.exec = f;
5
+ };
6
+ throw(e) {
7
+ delete this.exec;
8
+ this.state = "closed";
9
+ return Promise.reject(e);
10
+ }
11
+ return(value) {
12
+ delete this.exec;
13
+ this.state = "closed";
14
+ return Promise.resolve({ value: undefined, done: true });
15
+ }
16
+ next() {
17
+ if (this.exec) {
18
+ var exec = this.exec;
19
+ delete this.exec;
20
+ delete this.value;
21
+ return this.promise = new Promise(function (ok, oh) {
22
+ exec(function (result) {
23
+ delete this.promise;
24
+ ok(result);
25
+ this.return(result);
26
+ }, function (error) {
27
+ delete this.promise;
28
+ oh(error);
29
+ this.throw(error);
30
+ }, (value, exec) => {
31
+ delete this.promise;
32
+ ok({ value: value, done: !this.exec })
33
+ this.value = value;
34
+ this.exec = exec;
35
+ });
36
+ })
37
+ }
38
+ else if (this.promise) {
39
+ return this.promise.then(this.next, this.next);
40
+ }
41
+ return Promise.resolve({ value: undefined, done: true });
42
+ }
43
+
44
+ }
45
+ Generator.prototype[Symbol.asyncIterator] = function () {
46
+ return this;
47
+ }
48
+ function asyncAster_() {
49
+ return new AsyncGenerator(exec_.bind(this, arguments));
50
+ }
@@ -0,0 +1,4 @@
1
+
2
+ function async_() {
3
+ return new Promise(exec_.bind(this, arguments));
4
+ }
@@ -0,0 +1,11 @@
1
+ var test = async function (queue, expect) {
2
+ var res = await async_.apply(null, queue);
3
+ assert(res, expect);
4
+ }
5
+ test([function () {
6
+ return [1, 0]
7
+ }, function () {
8
+ return [, 2];
9
+ }], 2);
10
+
11
+ test(compile$unstruct(compile$scanner2(`1+2`)), 3);
@@ -0,0 +1,62 @@
1
+ var exec = function (args, ok, oh, int) {
2
+ var p = null, index = 0, r, finished = false, t = this;
3
+ var next = function (arg) {
4
+ p = arg;
5
+ run();
6
+ };
7
+ var catches = [];
8
+ var thro = function (err) {
9
+ if (catches.length) {
10
+ [index, p] = catches[catches.length - 1]
11
+ index += p >>> 16;
12
+ p = err;
13
+ next();
14
+ }
15
+ else {
16
+ oh(err);
17
+ }
18
+ };
19
+ var retn = function (p) {
20
+ r = p;
21
+ finished = true;
22
+ if (catches.length) fina();
23
+ else ok(r);
24
+ };
25
+ var fina = function () {
26
+ // 仅在try或catch未结束时使用
27
+ [index, p] = catches[catches.length - 1];
28
+ index += (p >>> 16) + (p & 0xffff);
29
+ next();
30
+ };
31
+ var fine = function () {
32
+ catches.pop();
33
+ next();
34
+ }
35
+ var run = function () {
36
+ var args_length = args.length, i;
37
+ if (finished && !catches.length || index > args_length) return ok(r);
38
+ while (index < args_length) {
39
+ try {
40
+ [p, i] = args[index].call(t, p) || [1, 0];
41
+ } catch (e) {
42
+ p = null;
43
+ thro(e);
44
+ break;
45
+ }
46
+ switch (i) {
47
+ case 0: index += p; break; // reflow
48
+ case 1:
49
+ index++; // await p;
50
+ if (p && isFunction(p.then)) return p.then(next, thro);
51
+ else return next(p);
52
+ case 2: return finished = true, retn(p); // return p;
53
+ case 3: return index++, int(p, next); // yield p;
54
+ case 7: index++; catches.push([index, p]); break; // try start
55
+ case 9: return p ? fine() : fina(); // finally
56
+ default: throw "代码异常!";
57
+ }
58
+ }
59
+ retn();
60
+ };
61
+ next();
62
+ };
@@ -33,7 +33,7 @@ function,continue,debugger
33
33
  instanceof`.trim().split(/[,\s]+/);
34
34
  class Javascript extends Program {
35
35
  straps = straps;
36
- value_reg = /^(false|true|null|Infinity|NaN|undefined|arguments|this|eval|super)$/
36
+ value_reg = /^(false|true|null|Infinity|NaN|undefined)$/
37
37
  transive_reg = /^(new|var|let|const|yield|void|in|of|typeof|delete|case|return|await|export|default|instanceof|throw|extends|import|from)$/
38
38
  strapexp_reg = /^(new|void|typeof|delete|class|function|await)/;
39
39
  forceend_reg = /^(return|yield|break|continue|debugger)$/;
@@ -222,7 +222,7 @@ class Program {
222
222
  };
223
223
  var row = 1, colstart = -1;
224
224
  var save = (type) => {
225
- if (lasttype === STAMP && type === STAMP && !/[,;\:\?]/.test(m)) {
225
+ if (lasttype === STAMP && type === STAMP && !/[,;\:]/.test(m)) {
226
226
  var scope = queue[queue.length - 1];
227
227
  if (/=>$/i.test(scope.text) ||
228
228
  /[=>]$/.test(scope.text) && /[^>=]/.test(m) ||
@@ -101,8 +101,11 @@ var skipAssignment = function (o, cx) {
101
101
  next();
102
102
  break;
103
103
  }
104
- case VALUE:
105
104
  case QUOTED:
105
+ if (needpunc && /^`/.test(o.text || o.entry)) {
106
+ needpunc = false;
107
+ }
108
+ case VALUE:
106
109
  if (needpunc) break loop;
107
110
  needpunc = true;
108
111
  next();
@@ -146,6 +149,11 @@ var skipAssignment = function (o, cx) {
146
149
  needpunc = true;
147
150
  break;
148
151
  }
152
+ else if (o.text === 'debugger') {
153
+ next();
154
+ needpunc = false;
155
+ break loop;
156
+ }
149
157
  else {
150
158
  next();
151
159
  needpunc = false;
@@ -0,0 +1,20 @@
1
+ var { createExpressList, createString } = common;
2
+ var test = function (text) {
3
+ var code = scanner2(text);
4
+ var exps = createExpressList(code);
5
+ console.log(code.isExpressQueue(),exps.map(createString))
6
+ };
7
+ test('a')
8
+ test('a+b=c')
9
+ test('if(a)a+b=c')
10
+ test('if(a)b=c; else c=d+a')
11
+ test('if(a)b=c; else if(m)c=d+a;else aa')
12
+ test('with(a)b=c; if(m)c=d+a;else aa')
13
+ test('try{}catch(){}finally{} if(m)c=d+a;else aa')
14
+ test('try{}catch{}finally{} if(m)c=d+a;else aa')
15
+ test('try{}catch{}finally{} if(m)c=d+a;else aa debugger\r\n a')
16
+ test('try{}catch{}\r\nfinally{}\r\n if(m)c=d+a;else aa debugger\r\n a')
17
+ test('var a')
18
+ test('var a,b,c=1;')
19
+ test('a:')
20
+ test('a`b`,a`aa{s}`,c;')
@@ -1,7 +1,7 @@
1
1
  var scanner2 = require("./scanner2");
2
2
  var strings = require("../basic/strings");
3
3
  var Program = scanner2.Program;
4
- var { STAMP, SCOPED, STRAP, EXPRESS, COMMENT, SPACE, PROPERTY, VALUE, QUOTED, number_reg, rename, getDeclared, skipAssignment, createString } = require("./common");
4
+ var { STAMP, SCOPED, STRAP, EXPRESS, COMMENT, SPACE, PROPERTY, VALUE, QUOTED, rename, getDeclared, skipAssignment, createString, relink, createExpressList } = require("./common");
5
5
  var link = function (a, b) {
6
6
  if (a) a.next = b;
7
7
  if (b) b.prev = a;
@@ -394,7 +394,7 @@ var killcls = function (body, o, getname_) {
394
394
  while (next && !next.isClass) next = next.next;
395
395
  base = createString(splice1(body, o, o = next));
396
396
  }
397
- if (base === 'Array') base = 'Array2';
397
+ // if (base === 'Array') base = 'Array2'; 降级时不做填充的工作
398
398
  var index = 0;
399
399
  while (o && o.isClass) {
400
400
  var m = o.first;
@@ -615,6 +615,9 @@ var killobj = function (body, getobjname, getname_, setsolid, deep = 0) {
615
615
  if (o && o.type === SCOPED) o = o.next;// ()
616
616
  if (o && o.type === SCOPED) o = o.next;// {}
617
617
  break;
618
+ case "async":
619
+ splice1(body, o, o = o.next);
620
+ break;
618
621
  default:
619
622
  o = o.next;
620
623
  }
@@ -661,15 +664,86 @@ var killobj = function (body, getobjname, getname_, setsolid, deep = 0) {
661
664
  o = o.next;
662
665
  }
663
666
  };
664
- var import_ = function () { };
665
- var export_ = function () { };
666
667
 
667
668
  // 字面量 false|true|null|Infinity|NaN|undefined|arguments|this|eval|super
668
- var unfalse = function () { };
669
- var unyield = function () { };
670
- var unawait = function (body, awaits, getname_) {
669
+ var power_map = {};
670
+ [
671
+ '=,+=,-=,*=,/=,%=,|=,&=,^=,**=,~=',
672
+ '=>', '?,:', '&&,||', '&,|,^',
673
+ 'instanceof,in,==,>=,<=,>,<,!=,!==,===,!in,!instanceof',
674
+ '>>,>>>,<<', '+,-', '*,/,%', '**',
675
+ 'typeof,await,yield,!,~', '++,--'
676
+ ].forEach((pp, i) => {
677
+ pp.split(",").forEach(p => {
678
+ power_map[p] = i + 1;
679
+ })
680
+ });
681
+ var unawait = function (body, getname, argname) {
682
+ return unstruct(body, function () {
683
+ return getname("_");
684
+ }, argname);
671
685
  };
672
- var unforof = function (o, gettempname_, getnextname_) {
686
+ var getsync = function (m) {
687
+ if (m.type === SCOPED && m.await) return null;
688
+ var n = skipAssignment(m);
689
+ while (m !== n) {
690
+ if (m.await || m.type === STRAP && /^(yield|await)$/.test(m.text)) return null;
691
+ m = m.next;
692
+ }
693
+ };
694
+ var ises3 = function (o) {
695
+ if (o && o.type === SCOPED && o.entry === "{") {
696
+ if (!o.await) return false;
697
+ }
698
+ else while (o) {
699
+ o = getsync(o);
700
+ if (o === null) return true;
701
+ if (!o || o.type !== STAMP || o.text !== ',') {
702
+ break;
703
+ }
704
+ o = o.next;
705
+ }
706
+ return false;
707
+ }
708
+ var unforin = function (o, body, getnewname_) {
709
+ // 仅处理有 await 或 yield 的代码
710
+ if (!o.await) {
711
+ if (!ises3(o.next)) return;
712
+ }
713
+ var m = o.first;
714
+ var hasdeclare = false;
715
+ if (m.type === STRAP) {
716
+ m = m.next;
717
+ hasdeclare = true;
718
+ }
719
+ var n = m.next;
720
+ if (n.type !== STRAP || n.text !== 'in') {
721
+ return;
722
+ }
723
+ n = n.next;
724
+ var tname = getnewname_();
725
+ var sname = getnewname_();
726
+ var kname = getnewname_();
727
+ var s = scanner2(`${sname}=`);
728
+ insert1(s, null, ...splice1(o, n));
729
+ insert1(s, null,
730
+ ...scanner2(`,${tname}=[];for(${kname} in ${sname})${tname}.push(${kname});`)
731
+ );
732
+ insert1(body, o.prev, ...s);
733
+ splice1(o, m.next);
734
+ splice1(o, m);
735
+ insert1(o, o.first, ...scanner2(`${kname}=0;${kname}<${tname}.length`));
736
+ var c = scanner2(`(=${tname}[${kname}]);${kname}++`);
737
+ insert1(c[0], c[0].first, m);
738
+ insert1(o, null, ...c);
739
+ };
740
+
741
+ var unforof = function (o, getnewname, used) {
742
+ var hasawait = false;
743
+ if (o.type === STRAP && o.text === 'await') {
744
+ hasawait = true;
745
+ o = o.next;
746
+ }
673
747
  var m = o.first;
674
748
  var hasdeclare = false;
675
749
  if (m.type === STRAP) {
@@ -687,24 +761,21 @@ var unforof = function (o, gettempname_, getnextname_) {
687
761
  var [d] = getDeclared(p);
688
762
  insert1(o, m, ...scanner2(d.map(a => a.text).join(",")));
689
763
  }
690
- splice1(o, m);
691
- var iname = gettempname_();
692
- insert1(o, null, ...scanner2(`${iname}=0`));
764
+ var iname = getnewname();
765
+ var gname = getnewname();
693
766
  var oname;
694
- if (f.type === EXPRESS && !/\./.test(oname)) {
767
+ if (!f.next && f.type === EXPRESS && !/\./.test(f.text) && used[f.text].length === 1) {
768
+ splice1(o, m);
695
769
  oname = f.text;
696
770
  }
697
771
  else {
698
- oname = getnextname_();
772
+ oname = getnewname();
773
+ splice1(o, n, f);
774
+ var mo = splice1(o, f);
699
775
  insert1(o, null, ...scanner2(`,${oname}=`));
700
- insert1(o, null, ...splice1(o, f));
776
+ insert1(o, null, ...mo);
701
777
  }
702
- insert1(o, null, ...scanner2(`;${iname}<${oname}.length&&`));
703
- var q = scanner2(`(=${oname}[${iname}],true)`)[0];
704
- insert1(q, q[0], p);
705
- insert1(o, null, q);
706
- insert1(o, null, ...scanner2(`;${iname}++`));
707
-
778
+ insert1(o, null, ...scanner2(`${gname}=${hasawait ? `${oname}[Symbol.asyncGenerator]||${oname}[Symbol.generator]` : `${oname}[Symbol.generator]||${oname}[Symbol.asyncGenerator]`}||Array.prototype[Symbol.asyncGenerator].bind(${oname}),${gname}=${gname}(),${iname}=${hasawait ? "await " : ''}${gname}.next();${p.text}=${iname}.value,!${iname}.done;${iname}=${gname}.next();`));
708
779
  };
709
780
  var unarrow = function (body, o, killobj, getname_) {
710
781
  var p = o.prev;
@@ -853,7 +924,7 @@ var down = function (scoped) {
853
924
  argcodes.push(`var ${an}=arguments;`);
854
925
  }
855
926
  var fordeep = 0;
856
- var kill = function (scoped) {
927
+ var kill = function (scoped, _, body) {
857
928
  if (scoped.isfunc) return down(scoped);
858
929
  killlet(scoped);
859
930
  var saveddeep = fordeep;
@@ -865,7 +936,9 @@ var down = function (scoped) {
865
936
  var hp = scoped.head.prev;
866
937
  if (hp && hp.type === STRAP) {
867
938
  if (hp.text === 'for') {
868
- unforof(scoped.head, getdeepname, getdeepname);
939
+ unforof(scoped.head, getdeepname, scoped.used);
940
+ unforin(scoped.head, body, getdeepname);
941
+ // unforcx(scoped.head, getdeepname);
869
942
  }
870
943
  else if (hp.text === 'catch') {
871
944
  killarg(scoped.head, scoped.body, _getname);
@@ -884,11 +957,24 @@ var down = function (scoped) {
884
957
  if (argcodes.length) precode(argcodes.join(";") + ";");
885
958
  if (scoped.body) scoped.body.keeplet = false, killobj(scoped.body, gettmpname, _getname, setsolid);
886
959
  scoped.forEach(kill);
960
+ if (scoped.async || scoped.yield) {
961
+ var argname = _getname("_");
962
+ var code = unawait(scoped.body, _getname, argname);
963
+ var body = scanner2(`return ${[, "aster_", "async_", "asyncAster_"][scoped.async << 1 | scoped.yield]}()`);
964
+ code.forEach(function (c) {
965
+ var f = scanner2(`function(${body[2].length ? argname : ''}){}`);
966
+ if (!c.length) insert1(f[2], null, ...scanner2('return []'));
967
+ else insert1(f[2], null, ...c);
968
+ if (body[2].length) insert1(body[2], null, { type: STAMP, text: "," });
969
+ insert1(body[2], null, ...f);
970
+ });
971
+ scoped.body.splice(0, scoped.body.length);
972
+ insert1(scoped.body, null, ...body);
973
+ }
887
974
  }
888
975
  else {
889
976
  kill(scoped);
890
977
  }
891
- Program.prototype.relink(scoped.body);
892
978
  };
893
979
  /**
894
980
  * @param {Program} code
@@ -58,7 +58,7 @@ assert(downLevel(`={a:1,[a]:1}`), `= (_ = { a: 1 }, _[a] = 1, _)`);
58
58
  assert(downLevel(`={a,[a]:1}`), `= (_ = {}, _.a = a, _[a] = 1, _)`);
59
59
  assert(downLevel(`={[a]:{[b]:1}}`), `= (_ = {}, _[a] = (_0 = {}, _0[b] = 1, _0), _)`);
60
60
  assert(downLevel(`={[a]:{[b]:{[c]:1}}}`), `= (_ = {}, _[a] = (_0 = {}, _0[b] = (_1 = {}, _1[c] = 1, _1), _0), _)`);
61
- assert(downLevel(`={[a]:{[b]:{[c]:1}},[b]:{[a]:1})}`), `= (_ = {}, _[a] = (_0 = {}, _0[b] = (_1 = {}, _1[c] = 1, _1), _0), _[b] = (_0 = {}, _0[a] = 1, _0), _)`);
61
+ assert(downLevel(`={[a]:{[b]:{[c]:1}},[b]:{[a]:1}}`), `= (_ = {}, _[a] = (_0 = {}, _0[b] = (_1 = {}, _1[c] = 1, _1), _0), _[b] = (_0 = {}, _0[a] = 1, _0), _)`);
62
62
  // 对象展开
63
63
  assert(downLevel(`={...a}`), `= extend({}, a)`);
64
64
  assert(downLevel(`={...{a:1}}`), `= extend({}, { a: 1 })`);
@@ -95,6 +95,7 @@ assert(downLevel(`function (a,...b,b){}`), "function (a, b) { b = arguments.leng
95
95
  assert(downLevel(`(a)=>k`), "function (a) { return k }")
96
96
  assert(downLevel(`(...a) => k`), "function () { var a = Array.prototype.slice.call(arguments, 0); return k }")
97
97
  assert(downLevel(`for(var o of os)`), "for (var _ = 0; _ < os.length && (o = os[_], true); _++)")
98
+ assert(downLevel(`for(var o of o)`), "for (var _ = 0, _0 = o; _ < _0.length && (o = _0[_], true); _++)")
98
99
  assert(downLevel(`for(var [a] of os)`), "for (var _ = 0; _ < os.length && (a = os[_][0], true); _++)")
99
100
  assert(downLevel(`for(var [a,b] of os)`), "for (var _ = 0; _ < os.length && (_0 = os[_], a = _0[0], b = _0[1], true); _++)")
100
101
  assert(downLevel(`[...a]=a`), "a = Array.prototype.slice.call(a, 0)")
@@ -104,8 +105,11 @@ assert(downLevel(`[...a,c]=a`), "_ = a, a = Array.prototype.slice.call(a, 0, -1)
104
105
  assert(downLevel(`{...a,c}=a`), `c = a.c, a = rest_(a, ["c"])`)
105
106
  assert(downLevel(`{c,...a}=a`), `c = a.c, a = rest_(a, ["c"])`)
106
107
  assert(downLevel(`{c,[c]:b,...a}=a`), `c = a.c, b = a[c], a = rest_(a, ["c", c])`)
107
- assert(downLevel(`async function(){}`), `function(){return Promise.resolve()}`)
108
- assert(downLevel(`async function(a){await a}`), `function(a){return Promise.resolve(a)}`)
109
- assert(downLevel(`async function(a){await a,await b}`), `function(a){return async_(function(){return a},function(){return b}})`)
110
- assert(downLevel(`async function(){await a,await b}`), `function(){return async_(function(){return a},function(){return b}})`)
111
- assert(downLevel(`async function(a){ if(c)await a}`), `function(){return Promise.resolve(a).then(b)}`)
108
+ assert(downLevel(`async function(){}`), `function () { return async_() }`)
109
+ assert(downLevel(`async function(){return 1}`), `function () { return async_(function () { _0 = 1; return [_0, 2] }) }`)
110
+ assert(downLevel(`async function(a){await a}`), `function (a) { return async_(function () { _0 = a; return [_0, 1] }) }`)
111
+ assert(downLevel(`async function(a){return await a}`), `function (a) { return async_(function () { _0 = a; return [_0, 1] }, function (_) { _0 = _; return [_0, 2] }) }`)
112
+ assert(downLevel(`async function(a){await a,await b}`), `function (a) { return async_(function () { _0 = a; return [_0, 1] }, function (_) { _0 = b; return [_0, 1] }) }`)
113
+ assert(downLevel(`async function(){await a,await b}`), `function () { return async_(function () { _0 = a; return [_0, 1] }, function (_) { _0 = b; return [_0, 1] }) }`)
114
+ assert(downLevel(`async function(a){ if(c)await a,await b;else if(s) await c;}`), `function (a) { return async_(function () { if (!c) return [3, 0]; _0 = a; return [_0, 1] }, function (_) { _0 = b; return [_0, 1] }, function (_) { return [2, 0] }, function (_) { if (!s) return [1, 0]; _0 = c; return [_0, 1] }) }`)
115
+ assert(downLevel(`async function(a){ for(i=1;i<2;i++) await 1 }`), `function (a) { return async_(function () { i = 1; return [1, 0] }, function (_) { _0 = i < 2; if (!_0) return [3, 0] }, function (_) { _0 = 1; return [_0, 1] }, function (_) { _0 = i++; return [-2, 0] }) }`)
@@ -0,0 +1,12 @@
1
+ var powermap = {};
2
+ [
3
+ '=,+=,-=,*=,/=,%=,|=,&=,^=,**=,~=,?,:,=>'/* 1 */,
4
+ '&&,||,??'/* 4 */, '&,|,^'/* 5 */,
5
+ 'instanceof,in,==,>=,<=,>,<,!=,!==,===,!in,!instanceof'/* 6 */,
6
+ '>>,>>>,<<'/* 7 */, '+,-'/* 8 */, '*,/,%'/* 9 */, '**'/* 10 */,
7
+ 'typeof,await,yield,new,delete,!,~'/* 11 */, '++,--'/* 12 */
8
+ ].forEach((pp, i) => {
9
+ pp.split(",").forEach(p => {
10
+ powermap[p] = i + 1;
11
+ })
12
+ });