Skip to content

Commit

Permalink
Cleanings in py2js ; restore $B.$profile.return() when leaving a frame
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre Quentel committed Feb 5, 2017
1 parent 07865a0 commit 1393955
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 62 deletions.
29 changes: 11 additions & 18 deletions www/src/brython.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ $B.regexIdentifier=/^(?:[\$A-Z_a-z\xAA\xB5\xBA\xC0-\xD6\xD8-\xF6\xF8-\u02C1\u02C
__BRYTHON__.implementation=[3,3,1,'alpha',0]
__BRYTHON__.__MAGIC__="3.3.1"
__BRYTHON__.version_info=[3,3,0,'alpha',0]
__BRYTHON__.compiled_date="2017-02-05 18:37:22.864657"
__BRYTHON__.compiled_date="2017-02-05 21:43:42.821780"
__BRYTHON__.builtin_module_names=["posix","sys","errno","time","_ajax","_base64","_jsre","_multiprocessing","_posixsubprocess","_profile","_svg","_sys","builtins","dis","hashlib","json","long_int","math","modulefinder","random","_abcoll","_codecs","_collections","_csv","_functools","_imp","_io","_random","_socket","_sre","_string","_struct","_sysconfigdata","_testcapi","_thread","_warnings","_weakref"]

;(function($B){var js,$pos,res,$op
Expand Down Expand Up @@ -1069,9 +1069,8 @@ var new_node=new $Node()
new $NodeJSCtx(new_node,js)
make_args_nodes.push(new_node)
if(this.type=='generator'){
var new_node=new $Node()
new $NodeJSCtx(new_node,'for(var $var in $ns){$locals[$var]=$ns[$var]};')
make_args_nodes.push(new_node)}
js='for(var $var in $ns){$locals[$var]=$ns[$var]};'
make_args_nodes.push($NodeJS(js))}
var only_positional=false
if(this.other_args===null && this.other_kw===null &&
this.after_star.length==0
Expand Down Expand Up @@ -1114,32 +1113,25 @@ new $NodeJSCtx(def_func_node,'')}else{new $NodeJSCtx(def_func_node,'')}
def_func_node.is_def_func=true
def_func_node.module=this.module
var last_instr=node.children[node.children.length-1].C.tree[0]
if(last_instr.type!=='return' && this.type!='generator'){node.add($NodeJS('$B.leave_frame($local_name);return None'))}
if(last_instr.type!=='return' && this.type!='generator'){
node.add($NodeJS('$B.leave_frame($local_name);return None'))}
node.add(def_func_node)
var offset=1
var indent=node.indent
js=name+'.$infos = {'
var name_decl=new $Node()
new $NodeJSCtx(name_decl,js)
node.parent.insert(rank+offset++,name_decl)
node.parent.insert(rank+offset++,$NodeJS(name+'.$infos = {'))
js=' __name__:"'
if(this.scope.ntype=='class'){js+=this.scope.C.tree[0].name+'.'}
js +=this.name+'",'
var name_decl=new $Node()
new $NodeJSCtx(name_decl,js)
node.parent.insert(rank+offset++,name_decl)
node.parent.insert(rank+offset++,$NodeJS(js))
var def_names=[]
for(var i=0;i<this.default_list.length;i++){def_names.push('"'+this.default_list[i]+'"')}
node.parent.insert(rank+offset++,$NodeJS(' __defaults__ : ['+
def_names.join(', ')+'],'))
var module=$get_module(this)
new_node=new $Node()
new $NodeJSCtx(new_node,' __module__ : "'+module.module+'",')
node.parent.insert(rank+offset++,new_node)
js=' __module__ : "'+module.module+'",'
node.parent.insert(rank+offset++,$NodeJS(js))
js=' __doc__: '+(this.doc_string ||'None')+','
new_node=new $Node()
new $NodeJSCtx(new_node,js)
node.parent.insert(rank+offset++,new_node)
node.parent.insert(rank+offset++,$NodeJS(js))
js=' __annotations__: {'+annotations.join(',')+'},'
node.parent.insert(rank+offset++,$NodeJS(js))
for(var attr in $B.bound[this.id]){this.varnames[attr]=true}
Expand Down Expand Up @@ -1172,6 +1164,7 @@ parent.insert(pos+1,try_node)
for(var i=0;i<children.length;i++){if(children[i].is_def_func){for(var j=0;j<children[i].children.length;j++){try_node.add(children[i].children[j])}}else{try_node.add(children[i])}}
parent.children.splice(pos+2,parent.children.length)
var except_node=$NodeJS('catch(err)')
if($B.profile > 0){except_node.add($NodeJS('$B.$profile.return()'))}
except_node.add($NodeJS('$B.leave_frame($local_name);throw err'))
parent.add(except_node)}
this.transformed=true
Expand Down
31 changes: 12 additions & 19 deletions www/src/brython_dist.js

Large diffs are not rendered by default.

66 changes: 43 additions & 23 deletions www/src/py2js.js
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,9 @@ function $AbstractExprCtx(context,with_commas){
this.parent = context
this.tree = []
context.tree[context.tree.length]=this

this.toString = function(){return '(abstract_expr '+with_commas+') '+this.tree}

this.to_js = function(){
this.js_processed=true
if(this.type==='list') return '['+$to_js(this.tree)+']'
Expand Down Expand Up @@ -1969,7 +1971,11 @@ function $DefCtx(context){
pnode = pnode.parent.parent
}

var defaults = [], apos=0, dpos=0,defs1=[],dpos1=0
var defaults = [],
apos=0,
dpos=0,
defs1=[],
dpos1=0
this.argcount = 0
this.kwonlyargcount = 0 // number of args after a star arg
this.varnames = {}
Expand Down Expand Up @@ -2082,6 +2088,29 @@ function $DefCtx(context){
this.argcount+', {'+this.slots.join(', ')+'}, '+
'['+slot_list.join(', ')+'], arguments, '

// Management of default values is complex... It uses a JS object
// called $default, evaluated only once, with the appropriate keys
// and values.
//
// A function like
// def f(x=1):
// ...
//
// is implemented as
//
// $locals["f"] = (function($defaults){
// function f1(){
// ... function body, uses $default to parse arguments ...
// }
// f1.$infos = {
// ... function attributes ...
// }
// return f1
// })({x: 1}) <-- default object is evaluated here
//
// $defaults could be set as an attribute of f1, and be referenced
// inside the function body, but this slows down execution a lot

if(defs1.length){js += '$defaults, '}
else{js += '{}, '}
js += this.other_args+', '+this.other_kw+');'
Expand All @@ -2092,9 +2121,8 @@ function $DefCtx(context){

if(this.type=='generator'){
// For generators, update $locals with the result of $B.args
var new_node = new $Node()
new $NodeJSCtx(new_node,'for(var $var in $ns){$locals[$var]=$ns[$var]};')
make_args_nodes.push(new_node)
js ='for(var $var in $ns){$locals[$var]=$ns[$var]};'
make_args_nodes.push($NodeJS(js))
}

var only_positional = false
Expand Down Expand Up @@ -2176,6 +2204,7 @@ function $DefCtx(context){
// __this__() in module javascript
nodes.push($NodeJS('$B.js_this = this;'))

// remove children of original node
for(var i=nodes.length-1;i>=0;i--){
node.children.splice(0, 0, nodes[i])
}
Expand All @@ -2192,8 +2221,11 @@ function $DefCtx(context){
def_func_node.is_def_func = true
def_func_node.module = this.module

// If the last instruction in the function is not a return,
// add an explicit line "return None".
var last_instr = node.children[node.children.length-1].context.tree[0]
if(last_instr.type!=='return' && this.type!='generator'){
// as always, leave frame before returning
node.add($NodeJS('$B.leave_frame($local_name);return None'))
}

Expand All @@ -2204,25 +2236,16 @@ function $DefCtx(context){

var indent = node.indent

// Set to local
//js = prefix+'='+name
//node.parent.insert(rank+offset++, $NodeJS(js))

// Create attribute $infos for the function
// Adding only one attribute is much faster than adding all the
// keys/values in $infos
js = name+'.$infos = {'
var name_decl = new $Node()
new $NodeJSCtx(name_decl,js)
node.parent.insert(rank+offset++,name_decl)
node.parent.insert(rank+offset++, $NodeJS(name+'.$infos = {'))

// Add attribute __name__
js = ' __name__:"'
if(this.scope.ntype=='class'){js+=this.scope.context.tree[0].name+'.'}
js += this.name+'",'
var name_decl = new $Node()
new $NodeJSCtx(name_decl,js)
node.parent.insert(rank+offset++,name_decl)
node.parent.insert(rank+offset++, $NodeJS(js))

// Add attribute __defaults__
var def_names = []
Expand All @@ -2234,19 +2257,16 @@ function $DefCtx(context){

// Add attribute __module__
var module = $get_module(this)
new_node = new $Node()
new $NodeJSCtx(new_node,' __module__ : "'+module.module+'",')
node.parent.insert(rank+offset++,new_node)
js = ' __module__ : "'+module.module+'",'
node.parent.insert(rank+offset++, $NodeJS(js))

// Add attribute __doc__
js = ' __doc__: '+(this.doc_string || 'None')+','
new_node = new $Node()
new $NodeJSCtx(new_node,js)
node.parent.insert(rank+offset++,new_node)
node.parent.insert(rank+offset++, $NodeJS(js))

// Add attribute __annotations__
js = ' __annotations__: {'+annotations.join(',')+'},'
node.parent.insert(rank+offset++,$NodeJS(js))
node.parent.insert(rank+offset++, $NodeJS(js))


for(var attr in $B.bound[this.id]){this.varnames[attr]=true}
Expand Down Expand Up @@ -2301,8 +2321,8 @@ function $DefCtx(context){
parent.children.splice(pos+2,parent.children.length)

var except_node = $NodeJS('catch(err)')
if($B.profile > 0){except_node.add($NodeJS('$B.$profile.return()'))}
except_node.add($NodeJS('$B.leave_frame($local_name);throw err'))
//if ( $B.profile > 0){finally_node.add($NodeJS('$B.$profile.return()'))}

parent.add(except_node)
}
Expand Down
2 changes: 1 addition & 1 deletion www/src/py_VFS.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion www/src/version_info.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
__BRYTHON__.implementation = [3, 3, 1, 'alpha', 0]
__BRYTHON__.__MAGIC__ = "3.3.1"
__BRYTHON__.version_info = [3, 3, 0, 'alpha', 0]
__BRYTHON__.compiled_date = "2017-02-05 18:37:22.864657"
__BRYTHON__.compiled_date = "2017-02-05 21:43:42.821780"
__BRYTHON__.builtin_module_names = ["posix","sys","errno", "time","_ajax",
"_base64",
"_jsre",
Expand Down

0 comments on commit 1393955

Please sign in to comment.