Skip to content
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

(feat) Impl function missing, #170 #171

Merged
merged 7 commits into from
Oct 11, 2019
Merged

(feat) Impl function missing, #170 #171

merged 7 commits into from
Oct 11, 2019

Conversation

killme2008
Copy link
Owner

实现 FunctionMissing 扩展接口:

public interface FunctionMissing {
  /**
   * Called when function not found, return the invocation result.
   *
   * @param name function name
   * @param env invocation env
   * @param args invocation arguments.
   * @return The invocation result.
   */
  AviatorObject onFunctionMissing(String name, Map<String, Object> env, AviatorObject... args);
}

当函数未找到的情况下将调用设置的 onFunctionMissing 处理器,用户可以自定义函数查找和调用逻辑。 #170

提供了一个 JavaMethodReflectionFunctionMissing 实现,采用反射方式调用第一个函数参数的实例方法:

 private AviatorEvaluatorInstance instance;

  @Before
  public void setup() {
    this.instance = AviatorEvaluator.newInstance();
    this.instance.setFunctionMissing(JavaMethodReflectionFunctionMissing.getInstance());
  }

那么就可以直接调用一些 java 方法了:

    assertEquals(2, this.instance.execute("indexOf('hello','l')"));
    assertEquals("heeeo", this.instance.execute("replaceAll('hello','l','e')"));
    assertEquals("hello", this.instance.execute("trim(' hello ')"));
    assertEquals(1, this.instance.execute("signum(3M)"));
    assertEquals(101L, this.instance.execute("execute(__instance__, '1+100')"));

@killme2008 killme2008 added this to the 4.2.5 milestone Oct 10, 2019
@killme2008 killme2008 merged commit 5652c8c into master Oct 11, 2019
@killme2008 killme2008 deleted the v4.2.5-dev branch October 11, 2019 06:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant