Skip to content

Commit e638cb0

Browse files
committed
Reduce size of generated Javascript by putting more features in make_function_infos(), make_async() and enter_frame()
1 parent 66297cb commit e638cb0

File tree

7 files changed

+110
-107
lines changed

7 files changed

+110
-107
lines changed

www/src/ast_to_js.js

Lines changed: 27 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1590,9 +1590,7 @@ $B.ast.ClassDef.prototype.to_js = function(scopes){
15901590
`if(resolved_bases !== bases){\nlocals.__orig_bases__ = bases}\n` +
15911591
`locals.__doc__ = ${docstring}\n` +
15921592
`var frame = [name, locals, module, ${globals_name}]\n` +
1593-
`frame.__file__ = __file__\n` +
1594-
`frame.$lineno = ${this.lineno}\n` +
1595-
`frame.$f_trace = $B.enter_frame(frame)\n` +
1593+
`$B.enter_frame(frame, __file__, ${this.lineno})\n` +
15961594
`var _frame_obj = $B.frame_obj\n` +
15971595
`if(frame.$f_trace !== _b_.None){\n$B.trace_line()}\n`
15981596

@@ -2412,9 +2410,7 @@ function type_param_in_def(tp, ref, scopes){
24122410
js += `function BOUND_OF_${name}(){\n` +
24132411
`var current_frame = $B.frame_obj.frame,\n` +
24142412
`frame = ['BOUND_OF_${name}', {}, '${gname}', ${globals_name}]\n` +
2415-
`frame.$f_trace = $B.enter_frame(frame)\n` +
2416-
`frame.__file__ = '${scopes.filename}'\n` +
2417-
`frame.$lineno = ${tp.bound.lineno}\n` +
2413+
`$B.enter_frame(frame, __file__, ${tp.bound.lineno})\n` +
24182414
`try{\n` +
24192415
`var res = ${tp.bound.to_js(scopes)}\n` +
24202416
`$B.leave_frame()\nreturn res\n` +
@@ -2511,8 +2507,7 @@ $B.ast.FunctionDef.prototype.to_js = function(scopes){
25112507
`locals = locals_${type_params_ref},\n` +
25122508
`frame = ['${type_params_ref}', locals, '${gname}', ${globals_name}],\n` +
25132509
`type_params = []\n` +
2514-
`frame.$f_trace = $B.enter_frame(frame)\n` +
2515-
`frame.__file__ = '${scopes.filename}'\n`
2510+
`$B.enter_frame(frame, '${scopes.filename}', ${this.lineno})\n`
25162511
for(var item of this.type_params){
25172512
type_params += type_param_in_def(item, type_params_ref, scopes)
25182513
}
@@ -2591,19 +2586,14 @@ $B.ast.FunctionDef.prototype.to_js = function(scopes){
25912586

25922587
js += `var frame = ["${this.$is_lambda ? '<lambda>': this.name}", ` +
25932588
`locals, "${gname}", ${globals_name}, ${name2}]
2594-
if(locals.$has_generators){
2595-
frame.$has_generators = true
2596-
}
2597-
frame.__file__ = __file__
2598-
frame.$lineno = ${this.lineno}
2599-
frame.$f_trace = $B.enter_frame(frame)\n`
2589+
$B.enter_frame(frame, __file__, ${this.lineno})\n`
26002590

26012591
if(func_scope.needs_stack_length){
26022592
js += `var stack_length = $B.count_frames()\n`
26032593
}
26042594

26052595
if(func_scope.needs_frames || is_async){
2606-
js += `var _frame_obj = $B.frame_obj\n` +
2596+
js += `var _frame_obj = $B.frame_obj,\n` +
26072597
`_linenums = $B.make_linenums()\n`
26082598
}
26092599

@@ -2674,11 +2664,19 @@ $B.ast.FunctionDef.prototype.to_js = function(scopes){
26742664
this.name
26752665

26762666
// Flags
2677-
var flags = 3
2678-
if(this.args.vararg){flags |= 4}
2679-
if(this.args.kwarg){flags |= 8}
2680-
if(is_generator){flags |= 32}
2681-
if(is_async){flags |= 128}
2667+
var flags = $B.COMPILER_FLAGS.OPTIMIZED | $B.COMPILER_FLAGS.NEWLOCALS
2668+
if(this.args.vararg){
2669+
flags |= $B.COMPILER_FLAGS.VARARGS
2670+
}
2671+
if(this.args.kwarg){
2672+
flags |= $B.COMPILER_FLAGS.VARKEYWORDS
2673+
}
2674+
if(is_generator){
2675+
flags |= $B.COMPILER_FLAGS.GENERATOR
2676+
}
2677+
if(is_async){
2678+
flags |= $B.COMPILER_FLAGS.COROUTINE
2679+
}
26822680

26832681
var parameters = [],
26842682
locals = [],
@@ -2703,9 +2701,7 @@ $B.ast.FunctionDef.prototype.to_js = function(scopes){
27032701
if(in_class){
27042702
js += `${name2}.$is_method = true\n`
27052703
}
2706-
if(is_async){
2707-
js += `${name2}.$is_async = true\n`
2708-
}
2704+
27092705
// Set admin infos
27102706
js += `$B.make_function_infos(${name2}, ` +
27112707
`'${gname}', ` +
@@ -2723,7 +2719,6 @@ $B.ast.FunctionDef.prototype.to_js = function(scopes){
27232719
`[${free_vars}], ` +
27242720
`${this.args.kwonlyargs.length}, ` +
27252721
`'${this.$is_lambda ? '<lambda>': this.name}', ` +
2726-
`${varnames.length}, ` +
27272722
`${this.args.posonlyargs.length}, ` +
27282723
`'${this.$is_lambda ? '<lambda>': qualname}', ` +
27292724
`[${varnames}])\n`;
@@ -2732,9 +2727,6 @@ $B.ast.FunctionDef.prototype.to_js = function(scopes){
27322727
js += `${name2} = $B.make_async(${name2})\n`
27332728
}
27342729

2735-
js += `${name2}.$args_parser = $B.make_args_parser_and_parse\n`;
2736-
2737-
27382730
var mangled = mangle(scopes, func_name_scope, this.name),
27392731
func_ref = `${make_scope_name(scopes, func_name_scope)}.${mangled}`
27402732

@@ -2864,10 +2856,10 @@ $B.ast.GeneratorExp.prototype.to_js = function(scopes){
28642856

28652857
// special case for first generator
28662858
var first = this.generators[0]
2867-
var js = `$B.enter_frame(frame)\n` +
2859+
var js = `$B.enter_frame(frame, __file__, ${this.lineno})\n` +
28682860
`var next_func_${id} = $B.make_js_iterator(expr, frame, ${this.lineno})\n` +
28692861
`for(var next_${id} of next_func_${id}){\n` +
2870-
`frame.$f_trace = $B.enter_frame(frame)\n`
2862+
`$B.enter_frame(frame, __file__, ${this.lineno})\n`
28712863
// assign result of iteration to target
28722864
var name = new $B.ast.Name(`next_${id}`, new $B.ast.Load())
28732865
copy_position(name, first_for.iter)
@@ -3028,7 +3020,7 @@ $B.ast.Interactive.prototype.to_js = function(scopes){
30283020
`locals = ${global_name},\n` +
30293021
`frame = ["${module_id}", locals, "${module_id}", locals]`
30303022

3031-
js += `\nvar __file__ = frame.__file__ = '${scopes.filename ?? "<string>"}'\n` +
3023+
js += `\nvar __file__ = '${scopes.filename ?? "<string>"}'\n` +
30323024
`locals.__name__ = '${name}'\n` +
30333025
`locals.__doc__ = ${extract_docstring(this, scopes)}\n`
30343026

@@ -3040,9 +3032,8 @@ $B.ast.Interactive.prototype.to_js = function(scopes){
30403032
// for exec(), frame is put on top of the stack inside
30413033
// py_builtin_functions.js / $$eval()
30423034

3043-
js += `frame.$f_trace = $B.enter_frame(frame)\n`
3044-
js += `$B.set_lineno(frame, 1)\n` +
3045-
'\nvar _frame_obj = $B.frame_obj\n'
3035+
js += `$B.enter_frame(frame, __file__, 1)\n`
3036+
js += '\nvar _frame_obj = $B.frame_obj\n'
30463037
js += 'var stack_length = $B.count_frames()\n'
30473038

30483039
js += `try{\n` +
@@ -3394,7 +3385,7 @@ $B.ast.Module.prototype.to_js = function(scopes){
33943385
}
33953386
}
33963387

3397-
js += `\nvar __file__ = frame.__file__ = '${scopes.filename ?? "<string>"}'\n` +
3388+
js += `\nvar __file__ = '${scopes.filename ?? "<string>"}'\n` +
33983389
`locals.__name__ = '${name}'\n` +
33993390
`locals.__doc__ = ${extract_docstring(this, scopes)}\n`
34003391

@@ -3406,9 +3397,8 @@ $B.ast.Module.prototype.to_js = function(scopes){
34063397
// for exec(), frame is put on top of the stack inside
34073398
// py_builtin_functions.js / $$eval()
34083399
if(! namespaces){
3409-
js += `frame.$f_trace = $B.enter_frame(frame)\n`
3410-
js += `$B.set_lineno(frame, 1)\n` +
3411-
'\nvar _frame_obj = $B.frame_obj\n'
3400+
js += `$B.enter_frame(frame, __file__, 1)\n`
3401+
js += '\nvar _frame_obj = $B.frame_obj\n'
34123402
}
34133403
js += 'var stack_length = $B.count_frames()\n'
34143404

www/src/async.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ $B.make_async = func => {
6666
f.$infos = func.$infos
6767
f.$is_func = true
6868
f.$is_async = true
69+
f.$args_parser = func.$args_parser
6970
return f
7071
}
7172

0 commit comments

Comments
 (0)