All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.snapscript.tree.dispatch.ValueDispatcher Maven / Gradle / Ivy

There is a newer version: 1.4.6
Show newest version
package org.snapscript.tree.dispatch;

import java.util.concurrent.Callable;

import org.snapscript.core.Context;
import org.snapscript.core.InternalStateException;
import org.snapscript.core.Module;
import org.snapscript.core.Scope;
import org.snapscript.core.Value;
import org.snapscript.core.bind.FunctionBinder;

public class ValueDispatcher implements InvocationDispatcher {
   
   private final Value value;
   private final Scope scope;      
   
   public ValueDispatcher(Scope scope, Object value) {
      this.value = (Value)value;
      this.scope = scope;
   }

   @Override
   public Value dispatch(String name, Object... arguments) throws Exception {
      Module module = scope.getModule();
      Context context = module.getContext();
      FunctionBinder binder = context.getBinder();
      Callable closure = binder.bind(value, arguments); // function variable
      
      if(closure == null) {
         throw new InternalStateException("Method '" + name + "' not found in scope");
      }
      return closure.call();   
   }
   
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy