-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WASM] Implement modular string literals.
The bundler now needs to instantiate a frontend to be able to construct AST to synthesize methods. In contrast to the exports generator that can run concurrently with transpilation (since it only needs the class jars), to synthesize getters the bundler relies on transpilation to produce summaries. PiperOrigin-RevId: 590001386
- Loading branch information
1 parent
c4c1052
commit 070e29d
Showing
17 changed files
with
339 additions
and
196 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
transpiler/java/com/google/j2cl/transpiler/ast/StringLiteralGettersCreator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/* | ||
* Copyright 2021 Google Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
* use this file except in compliance with the License. You may obtain a copy of | ||
* the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
package com.google.j2cl.transpiler.ast; | ||
|
||
import com.google.common.base.Ascii; | ||
import com.google.common.collect.HashMultiset; | ||
import com.google.common.collect.Multiset; | ||
import com.google.j2cl.transpiler.ast.MethodDescriptor.MethodOrigin; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** Implements lazy initialization of String literals. */ | ||
public class StringLiteralGettersCreator { | ||
|
||
private final Map<String, MethodDescriptor> literalMethodByString = new HashMap<>(); | ||
|
||
public Map<String, MethodDescriptor> getLiteralMethodByString() { | ||
return literalMethodByString; | ||
} | ||
|
||
/** | ||
* Returns the descriptor for the getter of {@code stringLiteral}, creating it if it did not | ||
* exist. | ||
*/ | ||
public MethodDescriptor getOrCreateLiteralMethod( | ||
Type type, StringLiteral stringLiteral, boolean synthesizeMethod) { | ||
String value = stringLiteral.getValue(); | ||
if (literalMethodByString.containsKey(value)) { | ||
return literalMethodByString.get(value); | ||
} | ||
|
||
String snippet = createSnippet(type, value); | ||
MethodDescriptor getLiteralMethod = | ||
getLazyStringLiteralGettterMethodDescriptor( | ||
type.getTypeDescriptor(), "$getString_" + snippet); | ||
|
||
if (synthesizeMethod) { | ||
type.synthesizeLazilyInitializedField( | ||
"$string_" + snippet, synthesizeStringCreation(stringLiteral), getLiteralMethod); | ||
} | ||
|
||
literalMethodByString.put(value, getLiteralMethod); | ||
|
||
return getLiteralMethod; | ||
} | ||
|
||
/** Returns the descriptor for the getter of the string literal. */ | ||
private static MethodDescriptor getLazyStringLiteralGettterMethodDescriptor( | ||
DeclaredTypeDescriptor enclosingTypeDescriptor, String name) { | ||
return MethodDescriptor.newBuilder() | ||
.setName(name) | ||
.setReturnTypeDescriptor(TypeDescriptors.get().javaLangString) | ||
.setEnclosingTypeDescriptor(enclosingTypeDescriptor) | ||
.setOrigin(MethodOrigin.SYNTHETIC_STRING_LITERAL_GETTER) | ||
.setStatic(true) | ||
.setSynthetic(true) | ||
.setSideEffectFree(true) | ||
.build(); | ||
} | ||
|
||
private final Map<Type, Multiset<String>> snippetsByType = new HashMap<>(); | ||
|
||
private String createSnippet(Type type, String value) { | ||
// Take the first few characters of the string and remove invalid identifier characters. | ||
String prefix = Ascii.truncate(value, 15, "...").replaceAll("[^A-Za-z0-9.]", "_"); | ||
|
||
var typeSnippets = snippetsByType.computeIfAbsent(type, t -> HashMultiset.create()); | ||
int occurrences = typeSnippets.count(prefix); | ||
typeSnippets.add(prefix); | ||
if (occurrences > 0) { | ||
return String.format("|%s|_%d", prefix, occurrences); | ||
} else { | ||
return String.format("|%s|", prefix); | ||
} | ||
} | ||
|
||
/** | ||
* Converts the StringLiteral into a call to the runtime to initialize create a String from a char | ||
* array. | ||
*/ | ||
private static Expression synthesizeStringCreation(StringLiteral stringLiteral) { | ||
ArrayTypeDescriptor charArrayDescriptor = | ||
ArrayTypeDescriptor.newBuilder().setComponentTypeDescriptor(PrimitiveTypes.CHAR).build(); | ||
MethodDescriptor fromInternalArray = | ||
TypeDescriptors.get() | ||
.javaLangString | ||
.getMethodDescriptor("fromInternalArray", charArrayDescriptor); | ||
if (fromInternalArray != null) { | ||
// TODO(b/272381112): Remove after non-stringref experiment. | ||
// This is the non-stringref j.l.String. | ||
return MethodCall.Builder.from(fromInternalArray) | ||
.setArguments(new ArrayLiteral(charArrayDescriptor, stringLiteral.toCharLiterals())) | ||
.build(); | ||
} | ||
return RuntimeMethods.createStringFromJsStringMethodCall(stringLiteral); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.