|
| 1 | +/* |
| 2 | + * Copyright 2025 The Closure Compiler Authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.javascript.jscomp; |
| 18 | + |
| 19 | +import static com.google.common.base.Preconditions.checkArgument; |
| 20 | +import static com.google.common.collect.ImmutableMap.toImmutableMap; |
| 21 | +import static java.util.function.Function.identity; |
| 22 | + |
| 23 | +import com.google.common.collect.ImmutableMap; |
| 24 | +import com.google.javascript.jscomp.js.RuntimeJsLibManager; |
| 25 | +import com.google.javascript.jscomp.js.RuntimeJsLibManager.ExternedField; |
| 26 | +import com.google.javascript.rhino.IR; |
| 27 | +import com.google.javascript.rhino.Node; |
| 28 | +import java.util.LinkedHashSet; |
| 29 | +import java.util.List; |
| 30 | +import java.util.Set; |
| 31 | + |
| 32 | +/** |
| 33 | + * Takes the set of runtime libraries and fields used in a @closureUnaware nested compilation, and |
| 34 | + * then 1) ensures the library definitions are injected in the main AST and 2) passes all specific |
| 35 | + * $jscomp.* fields used into the @closureUnaware shadow AST as an argument. |
| 36 | + * |
| 37 | + * <p>Before: |
| 38 | + * |
| 39 | + * <pre> |
| 40 | + * /** @closureUnaware * / |
| 41 | + * (function() { |
| 42 | + * use($jscomp_someField$$); |
| 43 | + * }).call(undefined); |
| 44 | + * </pre> |
| 45 | + * |
| 46 | + * <p>After: |
| 47 | + * |
| 48 | + * <pre> |
| 49 | + * /** @closureUnaware * / |
| 50 | + * (function($jscomp_someField$$) { |
| 51 | + * use($jscomp_someField$$); |
| 52 | + * }).call(undefined, $jscomp.someField); |
| 53 | + * </pre> |
| 54 | + */ |
| 55 | +final class InjectClosureUnawareRuntimeLibraries implements CompilerPass { |
| 56 | + private final AbstractCompiler mainCompiler; |
| 57 | + private final AbstractCompiler shadowCompiler; |
| 58 | + private final List<ClosureUnawareCallSite> closureUnawareCalls; |
| 59 | + |
| 60 | + record ClosureUnawareCallSite(Node callNode, Node closureUnawareFunction) { |
| 61 | + ClosureUnawareCallSite { |
| 62 | + checkArgument(callNode.isCall(), callNode); |
| 63 | + checkArgument(closureUnawareFunction.isFunction(), closureUnawareFunction); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + InjectClosureUnawareRuntimeLibraries( |
| 68 | + AbstractCompiler mainCompiler, |
| 69 | + AbstractCompiler shadowCompiler, |
| 70 | + List<ClosureUnawareCallSite> closureUnawareCalls) { |
| 71 | + this.mainCompiler = mainCompiler; |
| 72 | + this.shadowCompiler = shadowCompiler; |
| 73 | + this.closureUnawareCalls = closureUnawareCalls; |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public void process(Node externs, Node root) { |
| 78 | + injectLibrariesIntoMainAst(); |
| 79 | + injectFieldsIntoCallSites(); |
| 80 | + } |
| 81 | + |
| 82 | + private void injectLibrariesIntoMainAst() { |
| 83 | + RuntimeJsLibManager mainManager = mainCompiler.getRuntimeJsLibManager(); |
| 84 | + RuntimeJsLibManager shadowManager = shadowCompiler.getRuntimeJsLibManager(); |
| 85 | + for (String runtimeLib : shadowManager.getInjectedLibraries()) { |
| 86 | + mainManager.ensureLibraryInjected(runtimeLib, /* force= */ false); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + private void injectFieldsIntoCallSites() { |
| 91 | + var fieldsByName = |
| 92 | + shadowCompiler.getRuntimeJsLibManager().getExternedFields().stream() |
| 93 | + .collect(toImmutableMap(ExternedField::qualifiedName, identity())); |
| 94 | + |
| 95 | + for (ClosureUnawareCallSite callSite : closureUnawareCalls) { |
| 96 | + Set<ExternedField> toInject = |
| 97 | + gatherRuntimeFields(callSite.closureUnawareFunction(), fieldsByName); |
| 98 | + injectAll(callSite, toInject); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + private void injectAll(ClosureUnawareCallSite callSite, Set<ExternedField> toInject) { |
| 103 | + if (toInject.isEmpty()) { |
| 104 | + return; |
| 105 | + } |
| 106 | + Node parameterParent = NodeUtil.getFunctionParameters(callSite.closureUnawareFunction()); |
| 107 | + Node argumentParent = callSite.callNode(); |
| 108 | + |
| 109 | + if (parameterParent.hasChildren() && parameterParent.getLastChild().isRest()) { |
| 110 | + throw new IllegalStateException( |
| 111 | + "rest parameters not allowed in closureUnaware function parameters, found " |
| 112 | + + parameterParent.getLastChild()); |
| 113 | + } |
| 114 | + |
| 115 | + for (var field : toInject) { |
| 116 | + Node parameter = IR.name(field.qualifiedName()).srcref(parameterParent); |
| 117 | + Node argument = |
| 118 | + NodeUtil.newQName(mainCompiler, field.uncompiledName()).srcrefTree(argumentParent); |
| 119 | + |
| 120 | + parameterParent.addChildToBack(parameter); |
| 121 | + shadowCompiler.reportChangeToEnclosingScope(parameterParent); |
| 122 | + |
| 123 | + argumentParent.addChildToBack(argument); |
| 124 | + mainCompiler.reportChangeToEnclosingScope(argumentParent); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Returns the subset of {@code allInjectedFields} that are actually referenced in the given |
| 130 | + * {@code shadowAst}. |
| 131 | + */ |
| 132 | + private static Set<ExternedField> gatherRuntimeFields( |
| 133 | + Node root, ImmutableMap<String, ExternedField> allKnownFields) { |
| 134 | + Set<ExternedField> seen = new LinkedHashSet<>(); |
| 135 | + NodeUtil.Visitor visitor = |
| 136 | + (Node n) -> { |
| 137 | + if (!n.isName()) { |
| 138 | + return; |
| 139 | + } |
| 140 | + var field = allKnownFields.get(n.getString()); |
| 141 | + if (field != null) { |
| 142 | + seen.add(field); |
| 143 | + } |
| 144 | + }; |
| 145 | + NodeUtil.visitPreOrder(root, visitor); |
| 146 | + return seen; |
| 147 | + } |
| 148 | +} |
0 commit comments