using loop from java (directly intepreting a string using Executable)

35 views
Skip to first unread message

ses

unread,
Jul 6, 2012, 9:02:34 AM7/6/12
to loop...@googlegroups.com
I'm trying to do something which could be an unintended use of the language, but I feel loop may be a good fit for some of the logic I have in an existing Java application.

I want to compile and interpret a String of loop code within Java. Essentially I would like out of it a map of Objects to Methods I can call from Java. But I'm experiencing the following error:

java.lang.VerifyError: (class: _default, method: declarativeLogic signature: (Ljava/lang/Object;)Ljava/lang/Object;) Unable to pop operand off an empty stack

The error is caused by the following, very raw, test code:

Executable ex = new Executable(
new StringReader("require `"+EditForm.class.getName()+"` \n" +
"declarativeLogic(editForm) -> { \n" +
"'blah': @(editForm) -> { \n" +
"editForm.editButton.disabled, \n" +
"editForm.newButton.disabled, \n" +
"editForm.cascade \n" +
"}, \n" +
"State: @(editForm) -> { \n" +
  "editForm.cascade \n" +
"} \n" +
"}"));
ex.compile();
Method method = ex.getCompiled().getDeclaredMethod("declarativeLogic", EditForm.class);

Any assistance would be gratefully appreciated.

Dhanji R. Prasanna

unread,
Jul 6, 2012, 6:45:03 PM7/6/12
to loop...@googlegroups.com
Hi,

This is possible, but there are two different ways to achieve it easier than the one you're trying:

Loop.compile("...")  - returns a Class instance which you can call reflectively.
Loop.implement(...) - returns an impl of a java interface backed by a loop module.

Other than that you should remove the braces from your declarativeLogic (only closures need the braces). Plz also note that uppercase identifiers are not allowed (State -> will resolve to a type, not a variable). 

Dhanji.

ses

unread,
Jul 23, 2012, 3:53:55 PM7/23/12
to loop...@googlegroups.com
Thanks for your detailed response, I've resolved the issues with the Loop code and got it working using 'Loop.compile'. I've made a very minor modification to the Loop class to support the option of directly compiling Strings and sent a pull request on github, hope this is OK. Let me know if you think it is a good idea.
Reply all
Reply to author
Forward
0 new messages