commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Henri Biestro (JIRA)" <j...@apache.org>
Subject [jira] [Comment Edited] (JEXL-200) Support for dynamic scripting in jexl scripts
Date Mon, 20 Jun 2016 12:53:05 GMT

    [ https://issues.apache.org/jira/browse/JEXL-200?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15339437#comment-15339437
] 

Henri Biestro edited comment on JEXL-200 at 6/20/16 12:52 PM:
--------------------------------------------------------------

Setting aside the dedicated syntax - for which I dont see the necessity -, we can already
achieve what's described:
{code}
    public static class Eval {
        private JexlEngine jexl;

        public JexlScript fn(String src) {
            return jexl.createScript(src);
        }

        void setJexl(JexlEngine je) {
            jexl = je;
        }
    }

    @Test
    public void test200() throws Exception {
        JexlContext jc = new MapContext();
        Map<String, Object> funcs = new HashMap<String, Object>();
        Eval eval = new Eval();
        funcs.put(null, eval);
        JexlEngine jexl = new JexlBuilder().namespaces(funcs).create();
        eval.setJexl(jexl);
        String src = "var f = fn(\'(x)->{x + 42}\'); f(y)";
        JexlScript s200 = jexl.createScript(src, "y");
        Assert.assertEquals(142, s200.execute(jc, 100));
        Assert.assertEquals(52, s200.execute(jc, 10));
    }
{code}


was (Author: henrib):
Setting aside the dedicated syntax - for which I'm dont see the necessity -, we can already
achieve what's described:
{code}
    public static class Eval {
        private JexlEngine jexl;

        public JexlScript fn(String src) {
            return jexl.createScript(src);
        }

        void setJexl(JexlEngine je) {
            jexl = je;
        }
    }

    @Test
    public void test200() throws Exception {
        JexlContext jc = new MapContext();
        Map<String, Object> funcs = new HashMap<String, Object>();
        Eval eval = new Eval();
        funcs.put(null, eval);
        JexlEngine jexl = new JexlBuilder().namespaces(funcs).create();
        eval.setJexl(jexl);
        String src = "var f = fn(\'(x)->{x + 42}\'); f(y)";
        JexlScript s200 = jexl.createScript(src, "y");
        Assert.assertEquals(142, s200.execute(jc, 100));
        Assert.assertEquals(52, s200.execute(jc, 10));
    }
{code}

> Support for dynamic scripting in jexl scripts
> ---------------------------------------------
>
>                 Key: JEXL-200
>                 URL: https://issues.apache.org/jira/browse/JEXL-200
>             Project: Commons JEXL
>          Issue Type: New Feature
>    Affects Versions: 3.0
>            Reporter: Dmitri Blinov
>            Priority: Minor
>
> Allow for dynamically defined functions inside a script where the definition of the function
comes from a string expression, for example:
> {code}
> var x = function(y) => "return y+2";
> {code}
> the defined function should be accessible after its definition is successfully parsed
as ordinary function, and can be evaluated in the current context.
> {code}
> if (x(40) eq 42) {...
> {code}
> I think the idea of dynamic script evaluation is not unusual, and though to some extent
can be implemented via side-added functions like *eval(expr)* a dedicated syntax for this
will benefit from the simplicity of ordinal functions and the ability to control mapping of
the arguments between a caller and the defined function.



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Mime
View raw message