-
Couldn't load subscription status.
- Fork 189
Javascript: generate public functions for all externals #258
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
Converting to draft as an invalid reference to the |
|
Ok, above issue addressed. A public function is now generated for each external, and the |
|
We could also merge the code from Code minimisation or an optimising JS implementation may well do something equivalent, but it was easy to do in the Snowball compiler for Go and Rust at least so may be worthwhile doing here too. Do you have a use case for multiple externals BTW? It certainly good to have them actually usable, but also useful to know if people actually have a current use for them. |
compiler/generator_js.c
Outdated
| w(g, "~Mreturn this.getCurrent();~N"); | ||
| w(g, "~-~M}~N"); | ||
|
|
||
| if (!strcmp((const char *) q->s, "stem")) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
q->s isn't nul-terminated so use the same test as the other generators which special-case stem have:
if (SIZE(q->s) == 4 && memcmp(q->s, "stem", 4) == 0) {
(Your version probably worked in testing because newly allocated memory is typically zeroed by the OS so as not to reveal data from other processes, but relying on that is undefined behaviour.)
Yeah, I am writing a stemmer for a minority language, but snowball is great so I'm also using it for other string processing tools. Phonemiser, tokeniser, conjugating verbs. Also since (as I understand it) externals can only take one argument I'll likely have multiple externals for setting options, like regional variants for the phonemiser. Will get to changes today, thanks for the review |
|
If you're mostly needing getters/setters for variables, we could provide a way to generate those (you can write them yourself in Snowball for strings, but it's harder to for integers and booleans; even for strings, we can probably generate slightly more efficient ones for some target languages). There already is a way to do this with the C generator (
It'd be cleaner for the program to specify what to expose - e.g. Where This would also mean the compiler would know not to try to localise these variables (there's work in progress to automatically detect Snowball variables which can be made local to one function in the generated target language code). |
FYI, this variable localisation is now merged.
Did you do this? I don't see a push since. If it's just the single change I suggested and that looks good to you, I can just fold that in. |
|
I haven't yet sorry, and there's a bug in my |
|
Ok I've corrected the string comparison for "stem". I looked at changing the way externals are generated based on whether they are called internally, but it was more complicated to implement than in Rust or Go since in JS the externals return a different type, and they require additional code at the beginning and end of the function. Complicating the code didn't seem worth it to save the overhead of a single function call. |
Seems reasonable. If the code is minimised it'll probably eliminate that anyway, and an optimising JS interpreter may be able to. It happens for every word stemmed, so may be worth profiling at some point just to check if it's worth trying harder to change, but really the key thing is it should be no worse than before your patch (since |
Adds a public function to the JS class for each external routine, which does the same getting & setting of the current word as
stemWord.I've leftstemWorduntouched for backwards compatibility - not sure if that's the right thing to do, it creates a reference to a non-existent#stemfunction if the program you're writing isn't a stemmer, although that's not a problem in practice.The
stemWordfunction is now added only if there is an external with the exact namestem.Addresses one of the minor points in #244