AllocationStrategy.java revision 1017:1f2fa7bd6d95
1/*
2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25package jdk.nashorn.internal.runtime;
26
27import static jdk.nashorn.internal.lookup.Lookup.MH;
28
29import java.io.Serializable;
30import java.lang.invoke.MethodHandle;
31import java.lang.invoke.MethodHandles;
32import jdk.nashorn.internal.codegen.CompilerConstants;
33import jdk.nashorn.internal.codegen.ObjectClassGenerator.AllocatorDescriptor;
34
35/**
36 * Encapsulates the allocation strategy for a function when used as a constructor. Basically the same as
37 * {@link AllocatorDescriptor}, but with an additionally cached resolved method handle. There is also a
38 * canonical default allocation strategy for functions that don't assign any "this" properties (vast majority
39 * of all functions), therefore saving some storage space in {@link RecompilableScriptFunctionData} that would
40 * otherwise be lost to identical tuples of (map, className, handle) fields.
41 */
42final class AllocationStrategy implements Serializable {
43    private static final long serialVersionUID = 1L;
44
45    private static final MethodHandles.Lookup LOOKUP = MethodHandles.lookup();
46
47    private static final AllocationStrategy DEFAULT_STRATEGY = new AllocationStrategy(new AllocatorDescriptor(0));
48
49    /** Allocator map from allocator descriptor */
50    private final PropertyMap allocatorMap;
51
52    /** Name of class where allocator function resides */
53    private final String allocatorClassName;
54
55    /** lazily generated allocator */
56    private transient MethodHandle allocator;
57
58    private AllocationStrategy(final AllocatorDescriptor desc) {
59        this.allocatorMap = desc.getAllocatorMap();
60        // These classes get loaded, so an interned variant of their name is most likely around anyway.
61        this.allocatorClassName = desc.getAllocatorClassName().intern();
62    }
63
64    private boolean matches(final AllocatorDescriptor desc) {
65        return desc.getAllocatorMap().size() == allocatorMap.size() &&
66                desc.getAllocatorClassName().equals(allocatorClassName);
67    }
68
69    static AllocationStrategy get(final AllocatorDescriptor desc) {
70        return DEFAULT_STRATEGY.matches(desc) ? DEFAULT_STRATEGY : new AllocationStrategy(desc);
71    }
72
73    PropertyMap getAllocatorMap() {
74        return allocatorMap;
75    }
76
77    ScriptObject allocate(final PropertyMap map) {
78        try {
79            if (allocator == null) {
80                allocator = MH.findStatic(LOOKUP, Context.forStructureClass(allocatorClassName),
81                        CompilerConstants.ALLOCATE.symbolName(), MH.type(ScriptObject.class, PropertyMap.class));
82            }
83            return (ScriptObject)allocator.invokeExact(map);
84        } catch (final RuntimeException | Error e) {
85            throw e;
86        } catch (final Throwable t) {
87            throw new RuntimeException(t);
88        }
89    }
90
91    private Object readResolve() {
92        if(allocatorMap.size() == DEFAULT_STRATEGY.allocatorMap.size() &&
93                allocatorClassName.equals(DEFAULT_STRATEGY.allocatorClassName)) {
94            return DEFAULT_STRATEGY;
95        }
96        return this;
97    }
98
99    @Override
100    public String toString() {
101        return "AllocationStrategy[allocatorClassName=" + allocatorClassName + ", allocatorMap.size=" +
102                allocatorMap.size() + "]";
103    }
104}
105