boot/src/main/java/org/netbeans/html/boot/spi/Fn.java
branchgc
changeset 900 2ee22312e414
parent 838 bdc3d696dd4a
child 902 5c65f811cf55
     1.1 --- a/boot/src/main/java/org/netbeans/html/boot/spi/Fn.java	Tue Aug 26 18:13:30 2014 +0200
     1.2 +++ b/boot/src/main/java/org/netbeans/html/boot/spi/Fn.java	Fri Dec 12 11:22:40 2014 +0100
     1.3 @@ -115,8 +115,39 @@
     1.4       * @since 0.7
     1.5       */
     1.6      public static Fn define(Class<?> caller, String code, String... names) {
     1.7 +        return define(caller, false, code, names);
     1.8 +    }
     1.9 +
    1.10 +    /** Helper method to find current presenter and ask it to define new
    1.11 +     * function.
    1.12 +     * 
    1.13 +     * @param caller the class who wishes to define the function
    1.14 +     * @param keepParametersAlive whether Java parameters should survive in JavaScript
    1.15 +     *   after the method invocation is over
    1.16 +     * @param code the body of the function (can reference <code>this</code> and <code>names</code> variables)
    1.17 +     * @param names names of individual parameters
    1.18 +     * @return the function object that can be {@link Fn#invoke(java.lang.Object, java.lang.Object...) invoked}
    1.19 +     *    - can return <code>null</code> if there is {@link #activePresenter() no presenter}
    1.20 +     * @since 1.1
    1.21 +     */
    1.22 +    public static Fn define(Class<?> caller, boolean keepParametersAlive, String code, String... names) {
    1.23          final Presenter p = FnContext.currentPresenter(false);
    1.24 -        return p == null ? null : p.defineFn(code, names);
    1.25 +        if (p == null) {
    1.26 +            return null;
    1.27 +        }
    1.28 +        if (p instanceof KeepAlive) {
    1.29 +            boolean[] arr;
    1.30 +            if (!keepParametersAlive) {
    1.31 +                arr = new boolean[names.length];
    1.32 +                for (int i = 0; i < arr.length; i++) {
    1.33 +                    arr[i] = false;
    1.34 +                }
    1.35 +            } else {
    1.36 +                arr = null;
    1.37 +            }
    1.38 +            return ((KeepAlive)p).defineFn(code, names, arr);
    1.39 +        }
    1.40 +        return p.defineFn(code, names);
    1.41      }
    1.42      
    1.43      private static final Map<String,Set<Presenter>> LOADED = new HashMap<String, Set<Presenter>>();
    1.44 @@ -244,7 +275,7 @@
    1.45      protected final Presenter presenter() {
    1.46          return presenter;
    1.47      }
    1.48 -
    1.49 +    
    1.50      /** The representation of a <em>presenter</em> - usually a browser window.
    1.51       * Should be provided by a library included in the application and registered
    1.52       * in <code>META-INF/services</code>, for example with
    1.53 @@ -329,4 +360,29 @@
    1.54           */
    1.55          public Object toJava(Object js);
    1.56      }
    1.57 +
    1.58 +    /** Additional interface to {@link Presenter} to control more precisely
    1.59 +     * garbage collection behavior of individual parameters. See 
    1.60 +     * {@link JavaScriptBody#keepAlive()} attribute for description of the
    1.61 +     * actual behavior of the interface.
    1.62 +     * 
    1.63 +     * @since 1.1
    1.64 +     */
    1.65 +    public interface KeepAlive {
    1.66 +        /** Creates new function with given parameter names and provided body.
    1.67 +         * 
    1.68 +         * @param code the body of the function. Can refer to variables named
    1.69 +         *   as <code>names</code>
    1.70 +         * @param names names of parameters of the function - these will be 
    1.71 +         *   available when the <code>code</code> body executes
    1.72 +         * @param keepAlive array of booleans describing for each parameter
    1.73 +         *   whether it should be kept alive or not. Length of the array
    1.74 +         *   must be the same as length of <code>names</code> array. The
    1.75 +         *   array may be <code>null</code> to signal that all parameters
    1.76 +         *   should be <em>kept alive</em>.
    1.77 +         * 
    1.78 +         * @return function that can be later invoked
    1.79 +         */
    1.80 +        public Fn defineFn(String code, String[] names, boolean[] keepAlive);
    1.81 +    }
    1.82  }