Class ScriptInstanceBuilder

java.lang.Object
com.blackrook.rookscript.ScriptInstanceBuilder

public final class ScriptInstanceBuilder extends Object
A script instance builder for new script instances and such.
Author:
Matthew Tropiano
  • Method Details

    • withSource

      public ScriptInstanceBuilder withSource(String sourceData)
      Attaches script source to compile on instantiation. Replaces an existing script source, if set before.
      Parameters:
      sourceData - the source data.
      Returns:
      the builder, for chained calls.
    • withSource

      public ScriptInstanceBuilder withSource(String sourcePath, String sourceData)
      Attaches script source to compile on instantiation. Replaces an existing script source, if set before.
      Parameters:
      sourcePath - the path name of this string data (for include path parent).
      sourceData - the source data.
      Returns:
      the builder, for chained calls.
    • withSource

      public ScriptInstanceBuilder withSource(File sourceFile)
      Attaches script source to compile on instantiation. Replaces an existing script source, if set before.
      Parameters:
      sourceFile - the source file.
      Returns:
      the builder, for chained calls.
    • withSource

      public ScriptInstanceBuilder withSource(String streamPath, InputStream sourceStream)
      Attaches script input stream source to compile on instantiation. Replaces an existing script source, if set before.
      Parameters:
      streamPath - the stream path (used for errors and include directives).
      sourceStream - the source stream.
      Returns:
      the builder, for chained calls.
    • withSource

      public ScriptInstanceBuilder withSource(String streamPath, Reader sourceReader)
      Attaches script source to compile on instantiation. Replaces an existing script source, if set before.
      Parameters:
      streamPath - the stream path (used for errors and include directives).
      sourceReader - the source reader.
      Returns:
      the builder, for chained calls.
    • usingReaderIncluder

      public ScriptInstanceBuilder usingReaderIncluder(ScriptReaderIncluder includer)
      Adds the optional reader includer to use for compiling code.
      Parameters:
      includer - the reader includer to use.
      Returns:
      the builder, for chained calls.
    • usingReaderOptions

      public ScriptInstanceBuilder usingReaderOptions(ScriptReaderOptions options)
      Adds the optional reader options to use for compiling code.
      Parameters:
      options - the reader options to use.
      Returns:
      the builder, for chained calls.
    • withScript

      public ScriptInstanceBuilder withScript(Script script)
      Attaches the script used for this instance. Removes an existing script source, if set before.
      Parameters:
      script - the script to use.
      Returns:
      the builder, for chained calls.
    • withScriptStack

      public ScriptInstanceBuilder withScriptStack(int activationDepth, int valueStackDepth)
      Attaches a new script instance stack used for this instance. Each instance created will create a new stack instance.
      Parameters:
      activationDepth - the activation stack depth.
      valueStackDepth - the value stack depth.
      Returns:
      the builder, for chained calls.
    • withScriptStack

      public ScriptInstanceBuilder withScriptStack(ScriptInstanceStack stack)
      Attaches a script instance stack for this instance. Each instance created will reuse the provided stack instance.
      Parameters:
      stack - the stack to use.
      Returns:
      the builder, for chained calls.
    • withFunctionResolver

      public ScriptInstanceBuilder withFunctionResolver(ScriptFunctionResolver resolver)
      Adds a function resolver to this builder to be used in the script, clearing all resolvers first. Each instance created will use the resolvers added for compiling and runtime.
      Parameters:
      resolver - the resolver to add.
      Returns:
      the builder, for chained calls.
      See Also:
    • withFunctionResolver

      public ScriptInstanceBuilder withFunctionResolver(String namespace, ScriptFunctionResolver resolver)
      Adds a function resolver to this builder to be used in the script, clearing all resolvers first. Each instance created will use the resolvers added for compiling and runtime.
      Parameters:
      namespace - the namespace to use for the resolver.
      resolver - the resolver to add.
      Returns:
      the builder, for chained calls.
      See Also:
    • andFunctionResolver

      public ScriptInstanceBuilder andFunctionResolver(ScriptFunctionResolver resolver)
      Adds a function resolver to this builder to be used in the script. Each instance created will use the resolvers added for compiling and runtime.
      Parameters:
      resolver - the resolver to add.
      Returns:
      the builder, for chained calls.
      See Also:
    • andFunctionResolver

      public ScriptInstanceBuilder andFunctionResolver(String namespace, ScriptFunctionResolver resolver)
      Adds a function resolver to this builder to be used in the script. Each instance created will use the resolvers added for compiling and runtime.
      Parameters:
      namespace - the namespace to use for the resolver.
      resolver - the resolver to add.
      Returns:
      the builder, for chained calls.
      See Also:
    • withScope

      public ScriptInstanceBuilder withScope(String name, ScriptVariableResolver resolver)
      Adds a scope to be used in the script, clearing the set of scopes first. Each instance created will be provided with the added scopes at runtime.
      Parameters:
      name - the scope name.
      resolver - the scope resolver to add.
      Returns:
      the builder, for chained calls.
      See Also:
    • andScope

      public ScriptInstanceBuilder andScope(String name, ScriptVariableResolver resolver)
      Adds a scope to be used in the script. Each instance created will be provided with the added scopes at runtime.
      Parameters:
      name - the scope name.
      resolver - the scope resolver to add.
      Returns:
      the builder, for chained calls.
      See Also:
    • withWaitHandler

      public ScriptInstanceBuilder withWaitHandler(ScriptWaitHandler waitHandler)
      Sets the wait handler for the script instance. Each instance created will use this wait handler when a script is forced into a ScriptInstance.State.WAITING state.
      Parameters:
      waitHandler - the waiting handler to attach.
      Returns:
      the builder, for chained calls.
      See Also:
    • withEnvironment

      public ScriptInstanceBuilder withEnvironment(ScriptEnvironment environment)
      Sets the script environment for this script. Each instance created will use this environment.
      Parameters:
      environment - the environment to use.
      Returns:
      the builder, for chained calls.
    • withRunawayLimit

      public ScriptInstanceBuilder withRunawayLimit(int runawayLimit)
      Sets the runaway limit for the instance. Each instance created will use this limit.
      Parameters:
      runawayLimit - the limit. 0 or less is no limit.
      Returns:
      the builder, for chained calls.
    • createInstance

      public ScriptInstance createInstance()
      Gets the instance built from the set components. The script is compiled through this call. Any parsing errors are thrown as ScriptBuilderException with the parser error as the cause.
      Returns:
      a new ScriptInstance created from the characteristics set.
      Throws:
      ScriptInstanceBuilder.BuilderException - if the instance can't be built.
    • createFactory

      public ScriptInstanceFactory createFactory()
      Gets the instance built from the set components. The script is compiled through this call. Any parsing errors are thrown as ScriptBuilderException with the parser error as the cause.
      Returns:
      a new ScriptInstance created from the characteristics set.
      Throws:
      ScriptInstanceBuilder.BuilderException - if the instance can't be built.
    • call

      public ScriptInstance call(String entryName, Object... parameters)
      Creates the instance using the set components, then calls an entry point with a set of parameters. The script is compiled through this call. Any parsing errors are thrown as ScriptBuilderException with the parser error as the cause.
      Parameters:
      entryName - the entry point name.
      parameters - the starting parameters to push onto the stack.
      Returns:
      a new ScriptInstance created from the characteristics set.
      Throws:
      ScriptInstanceBuilder.BuilderException - if the instance can't be built.
      ScriptExecutionException - if the provided amount of parameters do not match the amount of parameters that the script requires, or the provided entry point does not exist.
      See Also:
    • callAndReturnAs

      public <T> T callAndReturnAs(Class<T> returnType, String entryName, Object... parameters)
      Creates the instance using the set components, then calls an entry point with a set of parameters. The script is compiled through this call. Any parsing errors are thrown as ScriptBuilderException with the parser error as the cause.

      The script instance is lost from this call - only use this if you want to execute and discard the execution instance.

      Type Parameters:
      T - the return type.
      Parameters:
      returnType - the return type to get from the script.
      entryName - the entry point name.
      parameters - the starting parameters to push onto the stack.
      Returns:
      a new ScriptInstance created from the characteristics set.
      Throws:
      ScriptInstanceBuilder.BuilderException - if the instance can't be built.
      ScriptExecutionException - if the provided amount of parameters do not match the amount of parameters that the script requires, or the provided entry point does not exist.
      See Also: