Skip to content

Refactor emscripten py - method extraction#5137

Merged
jgravelle-google merged 52 commits intoemscripten-core:incomingfrom
jgravelle-google:refactor_emscripten_py
Apr 17, 2017
Merged

Refactor emscripten py - method extraction#5137
jgravelle-google merged 52 commits intoemscripten-core:incomingfrom
jgravelle-google:refactor_emscripten_py

Conversation

@jgravelle-google
Copy link
Contributor

Still working on this, but wanted to post what I've got so far.

Goal: Make emscripten.py easier to understand.

At the moment I've been mainly decomposing the four big sub-functions that make up emscript into smaller functions with more explicit input/output. Haven't touched yet but am planning to: finalize_output and emscript_wasm_backend. function_tables_and_exports still needs work too.

Looking for feedback on:

  • general direction
  • obvious things I'm missing
  • style

in roughly that order.

Been testing by running the core tests.

@kripken
Copy link
Member

kripken commented Apr 13, 2017

Overall this looks nicer and cleaner to me.

Only style thing that feels odd is the double newlines, is that a standard convention?

@jgravelle-google
Copy link
Contributor Author

Yep, in PEP8: https://www.python.org/dev/peps/pep-0008/#blank-lines
Although I personally think it looks weird to have two blank lines around one-liner functions. But for longer functions it makes sense, and it's even weirder to do it inconsistently. Although by adding double newline spacing to functions I'm introducing here but not fixing them elsewhere I'm in fact introducing inconsistency. Although I think my plan was to eventually reformat this whole file?

I guess what I'm getting at is I don't much care either way really, but figured if I'm prettifying things I might as well do the python-standard thing by default.

@kripken
Copy link
Member

kripken commented Apr 14, 2017

Cool, yeah, sounds good to do it that way.

'coerced_access': coercer(access),
}
getter = '''
function get{name}(ptr) {{
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this is interesting.

I'm using '{a} {b}'.format(a=1, b=2) instead of '%s %s' % (1, 2) here so I can give the parameters more explanatory names.
But then because we're writing a javascript function, the string contains {s, which we then have to escape as {{s, which was pretty non-obvious.
On top of that I pass the format dictionary with **kwargs, which I feel is one of the less obvious bits of python syntax.

I personally like the way this looks, but is this sufficiently non-obvious that %ss would be better?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could \{ escape here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can't, that would be much more immediately readable as an escape sequence
The problem is the .format method interpreting the brace as part of its control, whereas \{ would help if it were a language-level escaping issue, this is a library-level interpretation issue, so no dice.
https://docs.python.org/2/library/string.html#format-string-syntax

I'm inclined to leave it, because I suspect it's a lot less weird looking if the reader is familiar with the format function. It's also still valid JS with the extra braces, so if it ever gets naively changed to %s-style in the future it'll still work. And if someone thinks "we don't need these I can remove a brace" they'll hit the KeyError I ran in to (because format then interprets the entire function body as the key). So it's ultimately pretty harmless.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, sounds good to me too.

('F32', float_coerce, 2), # TODO: fround when present
('F64', float_coerce, 3),
]
first_in_asm += ''.join([make_get_set(*args) for args in get_set_types]) + '\n'
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto here, I feel the *args is maybe a bit too cute. But I don't want to repeat the variable names because I feel that actually obscures the understanding here, which is "call the function with these sets of arguments"

Which of the following is best?

  • make_get_set(*args) for args in get_set_types
  • make_get_set(a, b, c) for a, b, c in get_set_types
  • make_get_set(name, coercer, shift) for name, coercer, shift in get_set_types

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like *args personally.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, I agree
Thanks

@jgravelle-google
Copy link
Contributor Author

We should probably merge this before it gets too big or long-lived. Running the other tests now, anything else I should do before merging? Any objections?

@jgravelle-google
Copy link
Contributor Author

other tests came back clean

@kripken
Copy link
Member

kripken commented Apr 14, 2017

I'd say this needs all the tests passing before merge - I'd run the main test suite, other that you already did, and browser as well.

@jgravelle-google
Copy link
Contributor Author

Sounds right. I'll try the browser tests, but I'm away from my desktop and they tend to not like running properly over SSH for some reason, so I'll merge this on Monday if everything passes.

@jgravelle-google
Copy link
Contributor Author

browser tests passed, merging

@jgravelle-google jgravelle-google merged commit ae66b6c into emscripten-core:incoming Apr 17, 2017
@jgravelle-google jgravelle-google deleted the refactor_emscripten_py branch April 17, 2017 16:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants