eval in stratifiedjs

54 views
Skip to first unread message

gofrend...@gmail.com

unread,
Nov 24, 2014, 12:43:41 AM11/24/14
to strati...@googlegroups.com
I really need synchronous flow as I made a google-blockly-based application that need to wait for user input before continue everything.
I glad to find stratifiedjs.

However, I've just realize that when I do eval, the script is executed as javascript, not as sjs.

How could I do sjs's eval?

Tim Cuthbertson

unread,
Nov 24, 2014, 2:21:03 AM11/24/14
to strati...@googlegroups.com, gofrend...@gmail.com
Hi,

You're right, Stratifiedjs doesn't override the global `eval` function. It's not that often you really _need_ to use eval, but I don't know much about blocky so I'll assume you have a good reason for it ;)

So anyway, the StratifiedJS eval function lives in the sjs:sys module. You can use it with something like:

    require('sjs:sys').eval("hold(1000); console.log('done!');")

It's a little limited (IIRC it executes in the global scope and doesn't have access to local variables), but if you really do need eval, hopefully it's sufficient.

Cheers,
 - Tim.

gofrend...@gmail.com

unread,
Nov 24, 2014, 6:42:22 PM11/24/14
to strati...@googlegroups.com, gofrend...@gmail.com
Hi Tim, thanks for your reply.
I have try your solution. I also do a trick to let stratifiedjs evaluate necessary functions.
Basically this is what I do:

<script id="my_prerequisites_scripts">
     // write all of my prerequisites javascript here, define functions etc
</script>

<script type="text/sjs">
    var my_code = '// some stratifiedjs script, that use hold etc';
    var my_prefix = document.getElementById('my_prerequisites_scripts').innerText;
    require('sjs:sys').eval(my_prefix + my_code + 'console.log("done")');
</script>


I hope this will help someone with the same problem too.
I glad I found stratifiedjs. It is just elegant and cool. Javascript should be like this :)
Reply all
Reply to author
Forward
0 new messages