Code generation server side ?

1,507 views
Skip to first unread message

Matthieu Nantern

unread,
Jul 21, 2014, 4:19:22 AM7/21/14
to blo...@googlegroups.com
Hi !

I find Blockly very interesting but is there a way to generate the code server side ?

I want to allow users to create code that I will execute later server-side (in JS).

I've read the documentation but didn't see a way to do it. Is it possible ?

Thanks for help !


phil cleaver

unread,
Jul 21, 2014, 11:11:50 AM7/21/14
to blo...@googlegroups.com
Hey Matthieu,

Not a 100% sure what you are asking, could you please elaborate a little bit, and tell us why you can't generate it on the client that is running Blockly..

If you are asking if you can generate code on some other machine other than the clients then, as Blockly is writtten in JavaScript, I'm guessing it should be possible to run a node.js server that could be loaded with Blockly executables and your block definitions.  You could then send it your workspace XML, have it evaluate the code and send it back to you.

If you are asking if you can generate JavaScript code that could be run on a server (maybe node.js again), then absolutely.  You can generate any JavaScript code you like, and run it on whatever machine you like.

Are you familiar with node?

Phil

Matthieu Nantern

unread,
Jul 21, 2014, 2:49:54 PM7/21/14
to blo...@googlegroups.com
Hey Phil, thanks for your response !

I don't want to generate the JS on the client side because that code can be easily changed. So I chose to pass the XML to the server and generate the JS server-side. I want to be able to run the code when I want (even without the client).

Actually I tried to execute the js server side on the JVM with Nashorn (http://winterbe.com/posts/2014/04/05/java8-nashorn-tutorial/) but I got a lot of error because of "window" which is undefined server-side.

I was thinking about calling:
  1. Function textToDom to convert my XML string to a DOM: first issue, DOMParser is undefined into Nashorn
  2. Changing workspaceToCode to accept a parameter (the DOM)

What you think about that ? Is there an easier way  ?


Thanks for you help !

phil cleaver

unread,
Jul 22, 2014, 12:28:31 AM7/22/14
to blo...@googlegroups.com
Hey Matthieu,

Please forgive me if this is irrelevant, but I'm curious about your working model.  You say you don't want to generate JS on the client side because that code can easily be changed.  Are you concerned about security and don't want the user to be able to change the JavaScript output from Blockly?  Could the XML you are sending not just as easily be changed? If this is the case then there is no security benefit to sending XML as opposed to JS.

From my (limited) knowledge of Blockly I think that it should be possible to generate code from XML outside of a browser.  It's an idea I've been toying with myself, but have yet to start on.  

As a thought, did you try to create a dummy "window" object in your nashorn environment that is just there to stop errors being thrown.  That way, there should be minimal changes needed to the Blockly core files, meaning that, as Blockly is certainly very actively developed, there should be less patching involved for new code releases.

Blockly looks to me to be extemely well written, so I'm sure that Neil and the Blockly team ahve written it in such a way that "window" is not needed for code generation.

I am unfamiliar with nashorn, but think node should be a better option as there are alot more users out there to help you. 

Sorry I don't have all the answers, but will help wherever I can.

Phil

Matthieu Nantern

unread,
Jul 22, 2014, 2:54:49 AM7/22/14
to blo...@googlegroups.com
Exactly, I don't want that the user be able to change the JS output from blockly. The XML can also be changed but in that case, I will not be able to convert it into JS.

Thanks for your help !

Blake Bourque

unread,
Dec 4, 2014, 2:34:14 PM12/4/14
to blo...@googlegroups.com
All,

Has any progress been made on this front? I would really like to run blockly headless on the server with nodeJS has anyone attempted this and had success or not?

I wonder if Neil could weigh in as to whether it should be technically possible? 

Thanks,
Blake

phil cleaver

unread,
Dec 7, 2014, 11:02:53 PM12/7/14
to blo...@googlegroups.com
Hi Blake,

If that question was aimed at me then I'm afraid I haven't progressed any further on this, but am absolutely interested in achieving what you are proposing.  I'll keep you updated.

Phil

Neil Fraser

unread,
Dec 20, 2014, 3:26:12 AM12/20/14
to blo...@googlegroups.com
I have just managed to get a snippet of XML to build as headless blocks and then generate as code.  There's still a bit more testing I need to do (the diff is over 3000 lines long), but server-side generation will happen in time for christmas.

--
You received this message because you are subscribed to the Google Groups "Blockly" group.
To unsubscribe from this group and stop receiving emails from it, send an email to blockly+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--

Matthieu Nantern

unread,
Dec 22, 2014, 3:16:29 AM12/22/14
to blo...@googlegroups.com, ro...@neil.fraser.name
That's a great gift !

Thank you

Eric Berry

unread,
Jan 16, 2015, 11:41:54 PM1/16/15
to blo...@googlegroups.com, ro...@neil.fraser.name
Did you happen to make any progress on this?  I'm looking to do the same.

Blake

unread,
Jan 16, 2015, 11:55:21 PM1/16/15
to blo...@googlegroups.com
Eric,

Lots of progress has been made on the serverside code generation front. (Also known as headless)

Here is a diff which shows the changes I made to build.py to get headless generation working. 


Hope that helps

Eric Berry

unread,
Jan 21, 2015, 7:44:07 PM1/21/15
to blo...@googlegroups.com
Awesome!  Definitely helps. Thanks Blake.

--
You received this message because you are subscribed to a topic in the Google Groups "Blockly" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/blockly/N70CBxwENDg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to blockly+u...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages