json-tck/src/main/java/net/java/html/js/tests/Receiver.java
author Jaroslav Tulach <jtulach@netbeans.org>
Thu, 18 Dec 2014 03:25:54 +0100
branchweakfx
changeset 915 15af7ebf1d0e
permissions -rw-r--r--
Bringing the !keepAlive approach to javax.script presenter as well
jtulach@915
     1
/**
jtulach@915
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jtulach@915
     3
 *
jtulach@915
     4
 * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
jtulach@915
     5
 *
jtulach@915
     6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
jtulach@915
     7
 * Other names may be trademarks of their respective owners.
jtulach@915
     8
 *
jtulach@915
     9
 * The contents of this file are subject to the terms of either the GNU
jtulach@915
    10
 * General Public License Version 2 only ("GPL") or the Common
jtulach@915
    11
 * Development and Distribution License("CDDL") (collectively, the
jtulach@915
    12
 * "License"). You may not use this file except in compliance with the
jtulach@915
    13
 * License. You can obtain a copy of the License at
jtulach@915
    14
 * http://www.netbeans.org/cddl-gplv2.html
jtulach@915
    15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
jtulach@915
    16
 * specific language governing permissions and limitations under the
jtulach@915
    17
 * License.  When distributing the software, include this License Header
jtulach@915
    18
 * Notice in each file and include the License file at
jtulach@915
    19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
jtulach@915
    20
 * particular file as subject to the "Classpath" exception as provided
jtulach@915
    21
 * by Oracle in the GPL Version 2 section of the License file that
jtulach@915
    22
 * accompanied this code. If applicable, add the following below the
jtulach@915
    23
 * License Header, with the fields enclosed by brackets [] replaced by
jtulach@915
    24
 * your own identifying information:
jtulach@915
    25
 * "Portions Copyrighted [year] [name of copyright owner]"
jtulach@915
    26
 *
jtulach@915
    27
 * Contributor(s):
jtulach@915
    28
 *
jtulach@915
    29
 * The Original Software is NetBeans. The Initial Developer of the Original
jtulach@915
    30
 * Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
jtulach@915
    31
 *
jtulach@915
    32
 * If you wish your version of this file to be governed by only the CDDL
jtulach@915
    33
 * or only the GPL Version 2, indicate your decision by adding
jtulach@915
    34
 * "[Contributor] elects to include this software in this distribution
jtulach@915
    35
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
jtulach@915
    36
 * single choice of license, a recipient has the option to distribute
jtulach@915
    37
 * your version of this file under either the CDDL, the GPL Version 2 or
jtulach@915
    38
 * to extend the choice of license to its licensees as provided above.
jtulach@915
    39
 * However, if you add GPL Version 2 code and therefore, elected the GPL
jtulach@915
    40
 * Version 2 license, then the option applies only if the new code is
jtulach@915
    41
 * made subject to such option by the copyright holder.
jtulach@915
    42
 */
jtulach@915
    43
package net.java.html.js.tests;
jtulach@915
    44
jtulach@915
    45
import java.lang.ref.Reference;
jtulach@915
    46
import java.lang.ref.WeakReference;
jtulach@915
    47
import net.java.html.js.JavaScriptBody;
jtulach@915
    48
jtulach@915
    49
/**
jtulach@915
    50
 */
jtulach@915
    51
public final class Receiver {
jtulach@915
    52
    private final Object fn;
jtulach@915
    53
    Object value;
jtulach@915
    54
    final Reference<Object> ref;
jtulach@915
    55
jtulach@915
    56
    public Receiver(Object v) {
jtulach@915
    57
        this.fn = initFn(v);
jtulach@915
    58
        this.ref = new WeakReference<Object>(v);
jtulach@915
    59
        this.value = this;
jtulach@915
    60
    }
jtulach@915
    61
    
jtulach@915
    62
    public void apply() {
jtulach@915
    63
        fnApply(fn, this);
jtulach@915
    64
    }
jtulach@915
    65
    
jtulach@915
    66
    void set(Object v) {
jtulach@915
    67
        value = v;
jtulach@915
    68
    }
jtulach@915
    69
    
jtulach@915
    70
    @JavaScriptBody(args = { "v" }, keepAlive = false, javacall = true, 
jtulach@915
    71
        body = "return function(rec) {\n"
jtulach@915
    72
        + "  rec.@net.java.html.js.tests.Receiver::set(Ljava/lang/Object;)(v);\n"
jtulach@915
    73
        + "};\n")
jtulach@915
    74
    private static native Object initFn(Object v);
jtulach@915
    75
    
jtulach@915
    76
    @JavaScriptBody(args = { "fn", "thiz" }, body =
jtulach@915
    77
        "fn(thiz);"
jtulach@915
    78
    )
jtulach@915
    79
    private static native void fnApply(Object fn, Receiver thiz);
jtulach@915
    80
}