-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfext.js
executable file
·1731 lines (1415 loc) · 51.9 KB
/
fext.js
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*global mret mdecl meth mfun methD mfunD console Map print global
exports __fext_debug_all*/
/*
fext.js: Fast Explicit Tail Calls
Mutual recursion is supported.
By G. Lathoud, June 2018 and later.
Boost license, see file ./LICENSE
API:
Use `mfun` to declare a function, `meth` to declare a method, and
wrap the inner tail calls with `mret`.
Examples: ./test/fext_unittest_es6.js and ./test/fext_unittest.js
Documentation: see ./README.md (on GitHub:
https://github.com/glathoud)
Write-up with a speed test: see ./index.html (live instance:
http://glat.info/fext)
Implementation notes:
(1) fext.js *itself* is implemented with mostly "old-fashioned"
syntax to fit old build systems. Arrow functions are supported,
on older JavaScript engines put them in a string.
(2) Lousy regexps, no parser, and thus no parser maintenance. It
might seem "bad" at first (what is "bad"?), but also means that
fext.js should work in the future versions of JavaScript
with little or no maintenance.
(3) Function decompilation is used! Let the flame wars start!!
More seriously, you can pass a code string wherever preferred.
ES6's backticks come in handy here.
*/
var global, exports
, __fext_debug_all // ?boolean?
;
(function (global) {
'use strict';
// ---------- API
global.mret = mret;
global.mfun = mfun;
global.meth = meth;
global.mself = mself;
// Debugging tools
var mfunD = global.mfunD = mfun.bind( { debug : true } );
var methD = global.methD = meth.bind( { debug : 'method' } );
// Alternative: single namespace
// `fext` itself is a shortcut for `mfun`
// https://github.com/glathoud/fext/issues/13
var fext = global.fext = mfun;
fext.mret = mret;
fext.mfun = mfun;
fext.meth = meth;
fext.mfunD = mfunD;
fext.methD = methD;
fext.mself = mself;
// ---------- API: convenience tool
mfun.log_to = log_to;
// ---------- API implementation
function mself()
{
throw new Error
( 'I am just a symbol, not supposed to be called.' );
}
var _debugging = false
// Detect Nashorn. Reason: limit expansion for better Nashorn
// support:
//
// https://github.com/glathoud/fext/issues/14 (regression)
// https://github.com/glathoud/fext/issues/8
// https://bugs.java.com/bugdatabase/view_bug.do?bug_id=JDK-8205600
, _usingNashorn = typeof document === 'undefined' && typeof window === 'undefined'
// https://stackoverflow.com/questions/37718661/how-can-i-determine-which-javascript-engine-rhino-or-nashorn-is-running-my-code
&& typeof Java !== "undefined" && Java && typeof Java.type === "function"
;
function mret()
{
if (_debugging)
{
var this_ =
'object' === typeof _debugging && _debugging
|| null
, arg0 = arguments[ 0 ]
, f = arg0 === mself || !arg0
? mself._current_dbg_f
: arg0
, arg_rest = [].slice.call( arguments, 1 )
;
return f
.apply( this_ // methD case
, ( this_
, this_
? [ this_ ] // methD case
: [] // mfunD case
)
.concat( arg_rest )
);
}
else
{
throw new Error
(
'fext: (1) wrap your function with `mfun`. '
+ ' (2) Do not forget `return` in tail calls,'
+ ' as in `return mret(...)`'
+ ' See https://github.com/glathoud/fext and '
+ 'http://glat.info/fext for examples.'
);
}
}
// Various constants
var LABEL_MAIN_LOOP = '__fext_main_loop__'
, LABEL_INLINE_LOOP = function ( i )
{
(isFinite(i) && i).toPrecision.call.a;
return '__fext_inline_loop_' + i + '__';
}
, UNDEFINED = 'undefined'
, V_CASE_I = '__fext_case_i__'
, V_CASE_I_RETURN = '-1'
, V_RET = '__fext_ret__'
, V_THAT = 'that'
;
function mfun( a, b, c )
/*
Returns a function that will lazily create the optimized
implementation, cache it, and call it.
API: missing args allowed, but order fixed:
var f = mfun( [groupkey,] [name,] f_or_s );
`f_or_s` is mandatory, the other two optional.
`f_or_s`: string|function
`name`: string
`groupkey` is any common object that will be used as a key
to group mutually recursive functions.
`groupkey` will NOT be modified, only used as a key to
determine which functions know each other (a.k.a. group).
--- Variants --
(More examples in ./README.md)
(1) Self-recursion
var my_function = mfun( anonymous_function_or_string );
(2) Mutual recursion with named functions
Custom `groupkey`:
var groupkey = {} // whatever object
, first_fun = mfun( groupkey, named_func_or_str )
, second_fun = mfun( groupkey, named_func_or_str )
, third_fun = mfun( groupkey, named_func_or_str )
;
Default `groupkey` (=== `first_fun`):
var first_fun = mfun( named_func_or_str )
, second_fun = mfun( first_fun, named_func_or_str )
, third_fun = mfun( first_fun, named_func_or_str )
;
(3) Mutual recursion with anonymous function (e.g. arrow
functions)
Custom `groupkey`:
var groupkey = {} // whatever object
, first_fun = mfun( groupkey, name, anonymous_f_or_s )
, second_fun = mfun( groupkey, name, anonymous_f_or_s )
, third_fun = mfun( groupkey, name, anonymous_f_or_s )
;
Default `groupkey` (=== `first_fun`):
var first_fun = mfun( name, anonymous_func_or_str )
, second_fun = mfun( first_fun, name, anonymous_func_or_str )
, third_fun = mfun( first_fun, name, anonymous_func_or_str )
;
*/
{
/*
Read the arguments, missing args allowed, but order fixed:
[groupkey], [name], f_or_s
`f_or_s` is mandatory, the other two optional.
*/
var arr =
[ a, b, c ]
, groupkey, name, f_or_s
;
for (var i = arr.length; i--;) // read right-to-left
{
var x = arr[ i ];
if (x == null)
continue;
var tyx = typeof x;
if (!f_or_s)
{
if ('function' === tyx || 'string' === tyx)
{
f_or_s = x;
continue;
}
}
else
{
if ('string' === tyx)
{
name = x;
continue;
}
else
{
groupkey = x;
break;
}
}
log_to( 'error', 'fext: invalid argument', x );
null.invalid_argument;
}
// -- Options
function dflt( x, fallback )
{
return x != null ? x : fallback;
}
// ?boolean?
var debug = dflt( this && this.debug
, !!__fext_debug_all
);
// ?boolean?
var inline_body_me = dflt( this && this.inline_body
, true
);
// ?integer>0?: number of expansion levels
var expansion_me = dflt( this && this.expansion
, _usingNashorn ? 2 : 4
);
// ?function?: Mostly for internal use (e.g. `meth()`)
var prepro_arg_arr = this && this.preprocess_argname_arr
|| null;
// ?boolean?: Mostly for internal use (e.g. `meth()`)
var mret_that_dot = dflt( this && this.mret_that_dot
, false
);
// ?boolean?: Destructure implementation (no intermediary variables)
// https://github.com/glathoud/fext/issues/18
var destructure = dflt( this && this.destructure
, false // deactivated for now because slow-down as of 2019-09, see #18. To activate: replace `false` with `supports_destructure()`
);
// --- Debug support
var ret = debug ? fext_wrapper_dbg : fext_wrapper;
// --- Defaults parameters
groupkey || (groupkey = ret);
if (!name)
{
// Function.name Not supported by IE, but then...
if ('function' === typeof f_or_s)
name = f_or_s.name;
if (!name) // ...fallback (in the IE case, too)
{
name = ( /^\s*function\s+(\w+)/.exec( ''+f_or_s )
|| []
)[ 1 ] || null;
}
}
// Support anonymous functions: `return mret( mself, ...)`
name || (name = '__fext_anonymous_' + (
mfun.__fext_anon_id__ = 1 + (mfun.__fext_anon_id__ | 0)
) + '__');
// --- Check
(f_or_s && name && groupkey) || null.missing_arg;
(f_or_s.bind || f_or_s.substring).call.a;
name.substring.call.a;
// --- Extract the code
// if a function, this calls decompilation (sin!)
var f_code = '' + f_or_s;
// --- Rock it
var mo_fun = f_code.match(
/^\s*function[^\(]*\(([^\)]*)\)([\s\S]*)$/
)
, mo_arrow = !mo_fun && f_code.match(
/^\s*\(?([^\)=]+)\)?\s*=>([\s\S]*)$/
)
, s_param = (mo_fun ? mo_fun[ 1 ]
: mo_arrow[ 1 ]
).trim()
, s_body = (mo_fun ? mo_fun[ 2 ]
: 'return ' + mo_arrow[ 2 ] + ';'
).trim()
// Prevent issues in the `inline_body` context
, has_var = /\bvar\s/.test( s_body )
// Scan the head
, argname_arr_0 = split_args( s_param )
, argname_arr = prepro_arg_arr
? prepro_arg_arr( argname_arr_0 ) // mostly for `meth`
: argname_arr_0
, narg = argname_arr.length
// `argname_csv` might include comments like `/*string*/`
, argname_csv = argname_arr.join( ', ' )
;
// `argname_arr` does not include comments anymore
argname_arr = argname_arr.map( function ( s ) {
return white_out_comments( s ).trim();
})
// Scan the body for tail calls
var tc_arr = find_tail_calls
.call(
{ mret_that_dot : mret_that_dot
, destructure : destructure
}
, name, s_body
)
, tc_len = tc_arr.length
;
if (tc_len < 1 && 'function' === typeof f_or_s)
{
log_to( 'warn'
, 'fext.js: no tail calls found in function '
+ name + ':'
, f_or_s
);
return f_or_s; // nothing to transform
}
// Build a list of unique names
// me first (see `V_CASE_I` init below)
var piece_arr = [ name ]
, piece_i_of_name = {}
, tmp = {}
// Things we'll do later (when all `mfun` declared)
, depgraph_complete = false
, all_argname_arr = argname_arr.slice() // finished later
, all_argname_set = argname_arr.reduce(
function ( o, s ) {
o[ s ] = true;
return o;
}
, {}
)
, other_argname_arr = [] // built later
;
piece_i_of_name[ name ] = 0;
tc_arr.forEach(
function ( o ) { o.name_arr.forEach( mark_name ); }
);
function mark_name( a_name )
{
if (a_name !== name && !(a_name in tmp))
{
// "unique names": each name only once
tmp[ a_name ] = true;
piece_i_of_name[ a_name ] = piece_arr.length;
piece_arr.push( a_name );
}
}
// Open the access to other functions
// (for mutual recursion)
var topmap = mfun.__fext_topmap__
|| (mfun.__fext_topmap__ = new Map)
, space =
(
topmap.has( groupkey )
? topmap
: (topmap.set( groupkey, {} ), topmap)
)
.get( groupkey )
;
space[ name ] && null.already_defined;
space[ name ] = Object.freeze( {
name : name
, argname_arr : argname_arr
, narg : narg
, callcode_gen : callcode_gen
, piececode_gen : piececode_gen
, piece_arr_0 : piece_arr.slice()
// Used by `inline_body`
, new_body_gen : new_body_gen
} );
// `debug` support: Leave the source code untouched for
// easier debugging.
//
// Note that here as well we convert arrow functions back to
// the `function` notation, for better support of older JS
// engines (e.g. nashorn as of 2018-07) and thus better
// integration into a variety of new and old JS build
// systems. https://github.com/glathoud/fext/issues/14
//
var dbg_f = 'string' === typeof f_or_s
? (new Function( 'return (function '+name+'( '+argname_csv+') {'+s_body+'});' ))()
: f_or_s
;
// Convenience (esp. for `meth`)
ret.getImpl = debug ? null : getImpl;
ret.getEvalImpl = debug
? function () { return ret; }
: function () { return '(' + getImpl() + ')'; }
;
if (debug)
{
// To have access to the generated code (simply to
// understand what is going on).
ret._tf_get_dbg_f_code = function () { return '' + dbg_f; };
}
else
{
// Support transfun's inlineable interface
// https://github.com/glathoud/fext/issues/16
// https://github.com/glathoud/transfun/issues/7
ret._tf_get_argname_arr = _tf_get_argname_arr;
ret._tf_get_bodycode = function () { return getImpl()._tf_get_bodycode(); };
}
return ret;
// --- Details
function _tf_get_argname_arr() { return argname_arr.slice(); }
var impl;
function fext_wrapper( /*...arguments...*/ )
{
if (_debugging)
return fext_wrapper_dbg.apply( this, arguments );
/*
Create the implementation at the latest moment:
when all dependencies have been defined
(depgraph_complete).
*/
return (impl || (impl = getImpl()))
.apply( this, arguments );
}
function getImpl()
{
if (!impl)
{
var head = argname_csv
, body = master_bodycode_gen()
;
try
{
/*
Nicety: make sure the returned function has a
meaningful name. Reason: simply doing `new
Function(head,body)` would return an anonymous
function).
*/
var tmp = 'return ' + name + ';\n'
+ 'function ' + name + '( ' + head + ' )\n'
+ '{\n'
+ body
+ '}\n'
;
impl = new Function( tmp )();
if (!debug)
{
// Support transfun's inlineable interface
// https://github.com/glathoud/fext/issues/16
// https://github.com/glathoud/transfun/issues/7
impl._tf_get_argname_arr = _tf_get_argname_arr;
impl._tf_get_bodycode = function () { return body; };
}
}
catch( e )
{
log_to(
'error'
, 'fext: caught error while compiling '
+ 'the implementation: ' + e
);
throw e;
}
}
return impl;
}
function fext_wrapper_dbg()
{
var old_debugging = _debugging;
var old_dbg_f = mself._current_dbg_f;
if (!debug)
{
throw new Error(
'fext.js: to debug, please set mfunD/methD'
+ ' for "' + name + '" as well.'
);
}
_debugging = debug;
mself._current_dbg_f = dbg_f;
try
{
var ret = dbg_f.apply( this, arguments );
}
finally
{
mself._current_dbg_f = old_dbg_f;
_debugging = old_debugging;
}
return ret;
}
function getImpl_dbg()
{
return dbg_f;
}
function master_bodycode_gen()
/*
Tail call elimination: replace each tail call with an
integer assignment.
An integer value >= 0 says which function will be run next
*after* the current one returns.
An integer value -1 says: final return.
*/
{
ensure_depgraph_complete();
var is_last_inline = inline_body_me
&& !expansion_me
, gen_cfg = {
inline_body : inline_body_me
, is_last_inline : is_last_inline
, expansion : expansion_me
, inline_loop : { ind : 0 } // will be unique, esp. for D #10
}
;
return [
'"use strict";'
, 'var ' +
(
[
V_THAT + ' = this'
, V_CASE_I + ' = 0'
, V_RET
]
.concat( other_argname_arr )
.concat( destructure ? []
: all_argname_arr
.map( _fext_argname )
)
).join( ', ' )
, ';'
, LABEL_MAIN_LOOP + ': for (;;)'
, '{'
, ' switch( ' + V_CASE_I + ')'
, ' {'
]
.concat( piece_arr.map(
function ( a_name, i )
{
(a_name === name) === (i === 0)
|| null.bug;
return 'case ' + i + ': '
+ space[ a_name ].callcode_gen(
piece_i_of_name
, null
, gen_cfg
)
+ ' continue ' + LABEL_MAIN_LOOP
+ ';\n';
}
) )
.concat([
' default: return ' + V_RET + ';'
, ' }'
, '}'
, ''
])
.concat(
inline_body_me
? []
: piece_arr.map(
function ( a_name, i )
{
(a_name === name) === (i === 0)
|| null.bug;
return space[ a_name ]
.piececode_gen(
piece_i_of_name
, gen_cfg
);
}
)
)
.join( '\n' )
}
function ensure_depgraph_complete()
{
if (!depgraph_complete)
{
/*
We assume that all necessary mfun's have been
registered to the "group" identified by
groupkey.
We can now complete the graph of dependencies.
And we use that opportunity to build
`other_argname_arr`.
*/
for (var added = true, n_last = 1; added; )
{
added = false;
var new_n_last = piece_arr.length;
for (var i = n_last; i < new_n_last; ++i)
{
var i_name = piece_arr[ i ];
(i_name || null).substring.call.a;
var i_o = space[ i_name ];
if (!i_o)
{
throw new Error
('fext: did you forget to declare/name'
+ ' "' + i_name + '"?'
);
}
var i_name_arr = i_o.piece_arr_0;
for (var j = i_name_arr.length; j--;)
{
var j_name = i_name_arr[ j ];
(j_name || null).substring.call.a;
if (!(j_name in piece_i_of_name))
{
piece_i_of_name[ j_name ] =
piece_arr.length;
piece_arr.push( j_name );
added = true;
}
}
// We use that opportunity to build
// `other_argname_arr`
var o_argname_arr = i_o.argname_arr;
for (var j = o_argname_arr.length; j--;)
{
var an = o_argname_arr[ j ];
if (!(an in all_argname_set))
{
all_argname_set[ an ] = true;
all_argname_arr .push( an );
other_argname_arr.push( an );
}
}
}
n_last = new_n_last;
}
depgraph_complete = true;
}
}
function callcode_gen( piece_i_of_name, remaining_ex, cfg )
{
cfg || null.mandatory;
remaining_ex == null
&& (remaining_ex = cfg.expansion);
var ret_arr = [
cfg.inline_body
? new_body_gen( piece_i_of_name, cfg )
: name + '_fext();'
];
callcode_gen_expand( remaining_ex );
return ret_arr.join( '\n' );
function callcode_gen_expand( ex )
{
if (ex-- > 0)
{
ret_arr.push(
'if (' + V_CASE_I + ' === '
+ V_CASE_I_RETURN
+ ') return ' + V_RET + ';'
);
piece_arr[ 0 ] === name || null.bug;
var only_self = piece_arr.length === 1;
for (var j_begin = 0
, j_end = piece_arr.length
, j_last = j_end - 1
, j = j_begin;
j < j_end;
j++
)
{
var a_name = piece_arr[ j ]
, a_pi = piece_i_of_name[ a_name ]
;
if (!only_self)
{
tmp = j === j_end - 1
? 'else {'
:
((j_begin < j ? 'else if' : 'if')
+ ' ('
+ V_CASE_I + ' === ' + a_pi
+ ') {'
)
;
ret_arr.push( tmp );
}
var cfg2 = cfg;
if (cfg.inline_body)
{
cfg2 = Object.create( cfg2 );
cfg2.is_last_inline = ex === 0;
}
ret_arr.push(
space[ a_name ].callcode_gen(
piece_i_of_name, ex, cfg2
)
);
if (!only_self)
ret_arr.push( '}' );
}
}
}
}
function piececode_gen(
/*uint[string]*/piece_i_of_name
, /*object*/cfg
)
{
cfg || null.mandatory;
var p_head = 'function ' + name + '_fext()'
, new_body = new_body_gen( piece_i_of_name, cfg )
;
return [
p_head
, '{'
, new_body
, ' ;'
]
.concat(
cfg.inline_body
? [ 'return;' ]
: [ V_CASE_I + ' = ' + V_CASE_I_RETURN + ';'
, V_RET + ' = ' + UNDEFINED + ';'
]
)
.concat( [ '}' ] )
.join( '\n' );
}
function new_body_gen( /*uint[string]*/piece_i_of_name
, /*object*/cfg )
{
cfg || null.mandatory;
var inline_body = cfg.inline_body
, is_last_inline = cfg.is_last_inline
;
if (inline_body && has_var)
{
throw new Error(
'fext (inline_body: true): function "' + name
+ '" contains `var`, which is forbidden.'
+ ' Please use `let` or `const` instead.'
);
}
if (inline_body)
{
cfg.inline_loop.ind.toPrecision.call.a;
// Common index to enforce unicity, esp. for D #10
cfg.inline_loop.ind++;
}
var new_body = s_body;
// Decreasing order important here
for (var i = tc_len; i--;)
{
new_body = tc_arr[ i ]
.change_body( new_body, piece_i_of_name, space
, cfg );
}
return inline_body && !is_last_inline
? LABEL_INLINE_LOOP( cfg.inline_loop.ind )
+ ': for(;;) {\n' + new_body + '\n}\n'
: new_body;
}
}
// ---------- API implementation: Variant to declare methods.
function meth( a, b )
/*
API: 2 variants, but in all cases `name` is mandatory AND must
be the same as the method name.
(1) Anonymous functions (self-recursion or mutual recursion).
o.<name> = meth( <name>, <anonymous_function_or_string> )
(2) Named functions (self-)2nd and 3rd for mutual recursion
o.<name> = meth( <named_function_or_string> )
IMPORTANT
For performance reasons (*), the method declarations must take
an extra input parameter `that`, and use it instead of `this`.
All `mret` calls must use `that.`, as in `mret( that.mself,
...)`.
(*) as of June 2018: issues in Firefox with .bind(this).
NOTE
This extra input parameter `that` also made possible to write
the debugging tool `methD`.
Example:
var o = {
factorial : meth( 'factorial'
, (that, n, acc) =>
acc == null ? mret( that.mself, n, 1 )
: n > 1 ? mret( that.mself, n-1, acc*n )
: acc)
};
More examples in ./README.md
*/
{
var name = b
? a
: (('' + a).match(
/^\s*function\s+([^\(\s]+)/) || []
)[ 1 ]
;
(name || null).required;
name.substring.call.a;
var debug = __fext_debug_all || this && this.debug;
var destructure = this && this.destructure;
if (debug)
{
var f_or_s = b || a
, dbg_f = 'string' === typeof f_or_s
? (new Function( 'return (' + f_or_s + ');' ))()
: f_or_s
;
meth_wrapper_dbg.getImpl = null;
meth_wrapper_dbg.getEvalImpl = function () {
return meth_wrapper_dbg;
};
return meth_wrapper_dbg;
}
// Use case: mutual recursion (or named self-recursion)
/*
3-step strategy required to support mutual recursion
between methods:
(1) the API user declares all methods: ==> `meth_wrapper2`
created.