-
-
Notifications
You must be signed in to change notification settings - Fork 243
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
Make Blockly OH blocks compatible with the JSScripting add-on #1170
Conversation
This prepends code that makes the `ctx`, `itemRegistry` & `events` objects accessible when the scripts are run with GraalVM, as they are when run with Nashorn. Closes openhab#1169. Signed-off-by: Yannick Schaus <github@schaus.net>
Job #211: Bundle Size — 10.58MB (-0.67%).Changed metrics (5/8)
Changed assets by type (2/7)
|
While experimenting with the JSScripting add-on I discovered that java.lang.Thread.sleep() is not allowed. That I believe will have some impact on this PR. We might need to remove the sleep block for JSScripting or at least radically change it. I tried to use a Promise and await and it did solve the exception that gets thrown but it never returns from the await. |
I doubt we can do this as this will most likely break hundreds of people's rules because I am sure that thread-sleep is used very often. |
A busy wait loop might be a work around. No one like it but I've used it in the past quite successfully in JS Scripting, mostly for unit tests. The problem is two fold:
There is no work around found so far (the usual JavaScript So I would recommend:
Of course, if a work around is found to replace it in the mean time replace the busy wait with that work around. |
Bringing some activity back to this PR discussion …
That is not true. const Thread = Java.type('java.lang.Thread');
console.info("Hello");
Thread.sleep(5000);
console.info("world"); |
Not true any more. It was a problem when I wrote the above though. It would generate the "Multithreaded access no allowed for JS" exception. Quite some time ago it stopped happening. I can't remember when so I couldn't point at what changed to make it work. Of course, if you use a sleep and a Timer and the Timer went off before the sleep was done, you'd be guaranteed to get that exception even now I think. I'm not sure if it'd still be a problem after your recent changes to make timers thread safe. I'm hoping the error won't occur and the timer will just have to wait. |
Thread.sleep and timers that run off do not lead to that exception anymore (at the moment only timers created by |
@florian-h05 Not to promise too much, but I could start testing all blocks based on JSScripting. Can you provide some documentation how I can try it with and then again without JSScripting? This could also help for providing documentation on it which I usually write in terms of blocklies. |
Do you mean how you can switch between JSScripting and Nashorn? For the usage of JS Scripting itself, the addon docs should help. The big question here is: do we just want to make the blocks run on JS Scripting by re-adding access to the raw Java APIs (as done in this PR), or do we want to use the openHAB JavaScript library that comes with JS Scripting?
JS Scripting docs?? Or what do you mean? |
That would be best indeed. The Blockly code generation should strive to present the best code possible to users, so that by switching from the blocks view to the code view, they can learn how they can perform the same task elegantly should they choose to leave Blockly behind and write their own code. With that in mind, the implementations for adding or reading an item's metadata should be radically different when the Blockly code generation is supported by JSScripting (where it's just a openhab-js call) or by Nashorn (where we had to basically implement it by instantiating Java objects and so on). I know this is a debate that we already had (have a fool-proof implementation regardless of how complex or convoluted it gets vs. elegant and straightforward code that could fail). I stick to my guns and think the latter is best. The openhab-js library could provide that "fool-proof layer" so the generated code could remain simple (for instance, sending a command would accept any possible type and let the library deal with the necessary conversions as appropriate). I wouldn't mind having 2 parallel implementations of the code generation for blocks, one would be for Nashorn, and another for JSScripting (which would make use of the openhab-js library). It's just a light refactoring with 2 files and some logic. We could (should?) even think about blocks being available or not based on the script engine. The thing to keep in mind is that for this to work, we'd need stable APIs in the openhab-js library so that the blockly scripts don't break because the library was updated "out-of-band" with npm. |
I don’t know how Blockly exactly works, but that brings me to the another question:
If possible, I would prefer to keep all current blocks. But I think that we can extend the available blocks because openHAB-JS covers large parts of core functionality/APIs.
Generally, we have stable APIs in openHAB-JS and avoid breaking changes, as they are always a problem because the users face them, not other devs. |
Based on openhab#1170. Also-by: Yannick Schaus <github@schaus.net> Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
Based on openhab#1170. Also-by: Yannick Schaus <github@schaus.net> Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
Based on openhab#1170. Also-by: Yannick Schaus <github@schaus.net> Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
Based on openhab#1170. Also-by: Yannick Schaus <github@schaus.net> Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
Based on openhab#1170. Also-by: Yannick Schaus <github@schaus.net> Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
This prepends code that makes the
ctx
,itemRegistry
&events
objects accessible when the scripts are run with GraalVM, as they
are when run with Nashorn.
Closes #1169.
Signed-off-by: Yannick Schaus github@schaus.net