8000 Allocate all emitter var field strings statically by gzm0 · Pull Request #4911 · scala-js/scala-js · GitHub
[go: up one dir, main page]

Skip to content

Allocate all emitter var field strings statically #4911

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ private[emitter] class FunctionEmitter(sjsGen: SJSGen) {

private def newSyntheticVar()(implicit pos: Position): js.Ident = {
syntheticVarCounter += 1
fileLevelVarIdent("$x" + syntheticVarCounter)
fileLevelVarIdent(VarField.x, syntheticVarCounter.toString())
}

@inline
Expand Down Expand Up @@ -482,7 +482,7 @@ private[emitter] class FunctionEmitter(sjsGen: SJSGen) {
implicit pos: Position): WithGlobals[js.Function] = {

performOptimisticThenPessimisticRuns {
val thisIdent = fileLevelVarIdent("thiz", thisOriginalName)
val thisIdent = fileLevelVarIdent(VarField.thiz, thisOriginalName)
val env = env0.withExplicitThis()
val js.Function(jsArrow, jsParams, restParam, jsBody) =
desugarToFunctionInternal(arrow = false, params, None, body, isStat, env)
Expand Down Expand Up @@ -667,7 +667,7 @@ private[emitter] class FunctionEmitter(sjsGen: SJSGen) {
unnest(superClass, qualifier, item, rhs) {
(newSuperClass, newQualifier, newItem, newRhs, env0) =>
implicit val env = env0
genCallHelper("superSet", transformExprNoChar(newSuperClass),
genCallHelper(VarField.superSet, transformExprNoChar(newSuperClass),
transformExprNoChar(newQualifier), transformExprNoChar(item),
transformExprNoChar(rhs))
}
Expand All @@ -678,7 +678,7 @@ private[emitter] class FunctionEmitter(sjsGen: SJSGen) {
if (needToUseGloballyMutableVarSetter(scope)) {
unnest(rhs) { (rhs, env0) =>
implicit val env = env0
js.Apply(globalVar("u", scope), transformExpr(rhs, lhs.tpe) :: Nil)
js.Apply(globalVar(VarField.u, scope), transformExpr(rhs, lhs.tpe) :: Nil)
}
} else {
// Assign normally.
Expand All @@ -692,7 +692,7 @@ private[emitter] class FunctionEmitter(sjsGen: SJSGen) {
case StoreModule(className, value) =>
unnest(value) { (newValue, env0) =>
implicit val env = env0
js.Assign(globalVar("n", className), transformExprNoChar(newValue))
js.Assign(globalVar(VarField.n, className), transformExprNoChar(newValue))
}

case While(cond, body) =>
Expand Down Expand Up @@ -773,7 +773,7 @@ private[emitter] class FunctionEmitter(sjsGen: SJSGen) {
} else {
val superCtor = {
if (globalKnowledge.hasStoredSuperClass(enclosingClassName)) {
fileLevelVar("superClass")
fileLevelVar(VarField.superClass)
} else {
val superClass =
globalKnowledge.getSuperClassOfJSClass(enclosingClassName)
Expand Down Expand Up @@ -879,9 +879,9 @@ private[emitter] class FunctionEmitter(sjsGen: SJSGen) {
case (PrimArray(srcPrimRef), PrimArray(destPrimRef)) if srcPrimRef == destPrimRef =>
genUncheckedArraycopy(jsArgs)
case (RefArray(), RefArray()) =>
genCallHelper("systemArraycopyRefs", jsArgs: _*)
genCallHelper(VarField.systemArraycopyRefs, jsArgs: _*)
case _ =>
genCallHelper("systemArraycopyFull", jsArgs: _*)
genCallHelper(VarField.systemArraycopyFull, jsArgs: _*)
}
}
}
Expand Down Expand Up @@ -2209,7 +2209,7 @@ private[emitter] class FunctionEmitter(sjsGen: SJSGen) {
genSelect(transformExprNoChar(checkNotNull(qualifier)), className, field)

case SelectStatic(className, item) =>
globalVar("t", (className, item.name))
globalVar(VarField.t, (className, item.name))

case SelectJSNativeMember(className, member) =>
val jsNativeLoadSpec =
Expand Down Expand Up @@ -2247,10 +2247,10 @@ private[emitter] class FunctionEmitter(sjsGen: SJSGen) {
js.Apply(newReceiver(false) DOT transformMethodIdent(method), newArgs)

def genDispatchApply(): js.Tree =
genCallHelper("dp_" + genName(methodName), newReceiver(false) :: newArgs: _*)
js.Apply(globalVar(VarField.dp, methodName), newReceiver(false) :: newArgs)

def genHijackedMethodApply(className: ClassName): js.Tree =
genApplyStaticLike("f", className, method, newReceiver(className == BoxedCharacterClass) :: newArgs)
genApplyStaticLike(VarField.f, className, method, newReceiver(className == BoxedCharacterClass) :: newArgs)

if (isMaybeHijackedClass(receiver.tpe) &&
!methodName.isReflectiveProxy) {
Expand Down Expand Up @@ -2300,20 +2300,20 @@ private[emitter] class FunctionEmitter(sjsGen: SJSGen) {
val transformedArgs = newReceiver :: newArgs

if (flags.isConstructor) {
genApplyStaticLike("ct", className, method, transformedArgs)
genApplyStaticLike(VarField.ct, className, method, transformedArgs)
} else if (flags.isPrivate) {
genApplyStaticLike("p", className, method, transformedArgs)
genApplyStaticLike(VarField.p, className, method, transformedArgs)
} else if (globalKnowledge.isInterface(className)) {
genApplyStaticLike("f", className, method, transformedArgs)
genApplyStaticLike(VarField.f, className, method, transformedArgs)
} else {
val fun =
globalVar("c", className).prototype DOT transformMethodIdent(method)
globalVar(VarField.c, className).prototype DOT transformMethodIdent(method)
js.Apply(fun DOT "call", transformedArgs)
}

case ApplyStatic(flags, className, method, args) =>
genApplyStaticLike(
if (flags.isPrivate) "ps" else "s",
if (flags.isPrivate) VarField.ps else VarField.s,
className,
method,
transformTypedArgs(method.name, args))
Expand Down Expand Up @@ -2354,7 +2354,7 @@ private[emitter] class FunctionEmitter(sjsGen: SJSGen) {
else
genLongMethodApply(newLhs, LongImpl.toInt)
case DoubleToInt =>
genCallHelper("doubleToInt", newLhs)
genCallHelper(VarField.doubleToInt, newLhs)
case DoubleToFloat =>
genFround(newLhs)

Expand All @@ -2366,14 +2366,14 @@ private[emitter] class FunctionEmitter(sjsGen: SJSGen) {
genLongMethodApply(newLhs, LongImpl.toDouble)
case DoubleToLong =>
if (useBigIntForLongs)
genCallHelper("doubleToLong", newLhs)
genCallHelper(VarField.doubleToLong, newLhs)
else
genLongModuleApply(LongImpl.fromDouble, newLhs)

// Long -> Float (neither widening nor narrowing)
case LongToFloat =>
if (useBigIntForLongs)
genCallHelper("longToFloat", newLhs)
genCallHelper(VarField.longToFloat, newLhs)
else
genLongMethodApply(newLhs, LongImpl.toFloat)

Expand Down Expand Up @@ -2458,14 +2458,14 @@ private[emitter] class FunctionEmitter(sjsGen: SJSGen) {
case IntLiteral(r) if r != 0 =>
or0(js.BinaryOp(JSBinaryOp./, newLhs, newRhs))
case _ =>
genCallHelper("intDiv", newLhs, newRhs)
genCallHelper(VarField.intDiv, newLhs, newRhs)
}
case Int_% =>
rhs match {
case IntLiteral(r) if r != 0 =>
or0(js.BinaryOp(JSBinaryOp.%, newLhs, newRhs))
case _ =>
genCallHelper("intMod", newLhs, newRhs)
genCallHelper(VarField.intMod, newLhs, newRhs)
}

case Int_| => js.BinaryOp(JSBinaryOp.|, newLhs, newRhs)
Expand Down Expand Up @@ -2513,7 +2513,7 @@ private[emitter] class FunctionEmitter(sjsGen: SJSGen) {
case LongLiteral(r) if r != 0L =>
wrapBigInt64(js.BinaryOp(JSBinaryOp./, newLhs, newRhs))
case _ =>
genCallHelper("longDiv", newLhs, newRhs)
genCallHelper(VarField.longDiv, newLhs, newRhs)
}
} else {
genLongMethodApply(newLhs, LongImpl./, newRhs)
Expand All @@ -2524,7 +2524,7 @@ private[emitter] class FunctionEmitter(sjsGen: SJSGen) {
case LongLiteral(r) if r != 0L =>
wrapBigInt64(js.BinaryOp(JSBinaryOp.%, newLhs, newRhs))
case _ =>
genCallHelper("longMod", newLhs, newRhs)
genCallHelper(VarField.longMod, newLhs, newRhs)
}
} else {
genLongMethodApply(newLhs, LongImpl.%, newRhs)
Expand Down Expand Up @@ -2631,7 +2631,7 @@ private[emitter] class FunctionEmitter(sjsGen: SJSGen) {
case String_charAt =>
semantics.stringIndexOutOfBounds match {
case CheckedBehavior.Compliant | CheckedBehavior.Fatal =>
genCallHelper("charAt", newLhs, newRhs)
genCallHelper(VarField.charAt, newLhs, newRhs)
case CheckedBehavior.Unchecked =>
js.Apply(genIdentBracketSelect(newLhs, "charCodeAt"), List(newRhs))
}
Expand Down Expand Up @@ -2672,7 +2672,7 @@ private[emitter] class FunctionEmitter(sjsGen: SJSGen) {
extractWithGlobals(genAsInstanceOf(transformExprNoChar(expr), tpe))

case GetClass(expr) =>
genCallHelper("objectGetClass", transformExprNoChar(expr))
genCallHelper(VarField.objectGetClass, transformExprNoChar(expr))

case Clone(expr) =>
val newExpr = transformExprNoChar(checkNotNull(expr))
Expand Down Expand Up @@ -2700,15 +2700,15 @@ private[emitter] class FunctionEmitter(sjsGen: SJSGen) {
*/
case ClassType(CloneableClass) | ClassType(SerializableClass) |
ClassType(ObjectClass) | AnyType =>
genCallHelper("objectOrArrayClone", newExpr)
genCallHelper(VarField.objectOrArrayClone, newExpr)

// Otherwise, it is known not to be an array.
case _ =>
genCallHelper("objectClone", newExpr)
genCallHelper(VarField.objectClone, newExpr)
}

case IdentityHashCode(expr) =>
genCallHelper("systemIdentityHashCode", transformExprNoChar(expr))
genCallHelper(VarField.systemIdentityHashCode, transformExprNoChar(expr))

case WrapAsThrowable(expr) =>
val newExpr = transformExprNoChar(expr).asInstanceOf[js.VarRef]
Expand All @@ -2727,7 +2727,7 @@ private[emitter] class FunctionEmitter(sjsGen: SJSGen) {
// Transients

case Transient(CheckNotNull(obj)) =>
genCallHelper("n", transformExpr(obj, preserveChar = true))
genCallHelper(VarField.n, transformExpr(obj, preserveChar = true))
case Transient(AssumeNotNull(obj)) =>
transformExpr(obj, preserveChar = true)

Expand All @@ -2753,7 +2753,7 @@ private[emitter] class FunctionEmitter(sjsGen: SJSGen) {
}

case Transient(ObjectClassName(obj)) =>
genCallHelper("objectClassName", transformExprNoChar(obj))
genCallHelper(VarField.objectClassName, transformExprNoChar(obj))

case Transient(ArrayToTypedArray(expr, primRef)) =>
val value = transformExprNoChar(checkNotNull(expr))
Expand Down Expand Up @@ -2800,7 +2800,7 @@ private[emitter] class FunctionEmitter(sjsGen: SJSGen) {

case Transient(JSNewVararg(constr, argsArray)) =>
assert(!es2015, s"generated a JSNewVargs with ES 2015+ at ${tree.pos}")
genCallHelper("newJSObjectWithVarargs",
genCallHelper(VarField.newJSObjectWithVarargs,
transformExprNoChar(constr), transformExprNoChar(argsArray))

case JSPrivateSelect(qualifier, className, field) =>
Expand Down Expand Up @@ -2840,7 +2840,7 @@ private[emitter] class FunctionEmitter(sjsGen: SJSGen) {
transformExprNoChar(method)), args.map(transformJSArg))

case JSSuperSelect(superClass, qualifier, item) =>
genCallHelper("superGet", transformExprNoChar(superClass),
genCallHelper(VarField.superGet, transformExprNoChar(superClass),
transformExprNoChar(qualifier), transformExprNoChar(item))

case JSImportCall(arg) =>
Expand Down Expand Up @@ -2902,7 +2902,7 @@ private[emitter] class FunctionEmitter(sjsGen: SJSGen) {
js.UnaryOp(JSUnaryOp.typeof, transformExprNoChar(globalRef))

case JSLinkingInfo() =>
globalVar("linkingInfo", CoreVar)
globalVar(VarField.linkingInfo, CoreVar)

// Literals

Expand Down Expand Up @@ -2943,18 +2943,18 @@ private[emitter] class FunctionEmitter(sjsGen: SJSGen) {
js.This()

case VarKind.ExplicitThisAlias =>
fileLevelVar("thiz")
fileLevelVar(VarField.thiz)

case VarKind.ClassCapture =>
fileLevelVar("cc", genName(name.name))
fileLevelVar(VarField.cc, genName(name.name))
}

case Transient(JSVarRef(name, _)) =>
js.VarRef(name)

case This() =>
if (env.hasExplicitThis)
fileLevelVar("thiz")
fileLevelVar(VarField.thiz)
else
js.This()

Expand All @@ -2971,7 +2971,7 @@ private[emitter] class FunctionEmitter(sjsGen: SJSGen) {
for ((value, expectedType) <- captureValues.zip(expectedTypes))
yield transformExpr(value, expectedType)
}
js.Apply(globalVar("a", className), transformedArgs)
js.Apply(globalVar(VarField.a, className), transformedArgs)

// Invalid trees

Expand All @@ -2984,7 +2984,7 @@ private[emitter] class FunctionEmitter(sjsGen: SJSGen) {
if (preserveChar || tree.tpe != CharType)
baseResult
else
genCallHelper("bC", baseResult)
genCallHelper(VarField.bC, baseResult)
}

private def transformApplyDynamicImport(tree: ApplyDynamicImport)(
Expand Down Expand Up @@ -3012,7 +3012,7 @@ private[emitter] class FunctionEmitter(sjsGen: SJSGen) {
}

val innerCall = extractWithGlobals {
withDynamicGlobalVar("s", (className, method.name)) { v =>
withDynamicGlobalVar(VarField.s, (className, method.name)) { v =>
js.Apply(v, newArgs)
}
}
Expand Down Expand Up @@ -3232,7 +3232,7 @@ private[emitter] class FunctionEmitter(sjsGen: SJSGen) {
keepOnlyDangerousVarNames = false)
}

private def genApplyStaticLike(field: String, className: ClassName,
private def genApplyStaticLike(field: VarField, className: ClassName,
method: MethodIdent, args: List[js.Tree])(
implicit pos: Position): js.Tree = {
js.Apply(globalVar(field, (className, method.name)), args)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ package org.scalajs.linker.backend.emitter
import org.scalajs.linker.interface.ESVersion

private[emitter] sealed abstract class PolyfillableBuiltin(
val builtinName: String, val availableInESVersion: ESVersion)
val polyfillField: VarField, val availableInESVersion: ESVersion)

private[emitter] object PolyfillableBuiltin {
lazy val All: List[PolyfillableBuiltin] = List(
Expand All @@ -27,18 +27,18 @@ private[emitter] object PolyfillableBuiltin {
)

sealed abstract class GlobalVarBuiltin(val globalVar: String,
builtinName: String, availableInESVersion: ESVersion)
extends PolyfillableBuiltin(builtinName, availableInESVersion)
polyfillField: VarField, availableInESVersion: ESVersion)
extends PolyfillableBuiltin(polyfillField, availableInESVersion)

sealed abstract class NamespacedBuiltin(val namespaceGlobalVar: String,
builtinName: String, availableInESVersion: ESVersion)
extends PolyfillableBuiltin(builtinName, availableInESVersion)
val builtinName: String, polyfillField: VarField, availableInESVersion: ESVersion)
extends PolyfillableBuiltin(polyfillField, availableInESVersion)

case object ObjectIsBuiltin extends NamespacedBuiltin("Object", "is", ESVersion.ES2015)
case object ImulBuiltin extends NamespacedBuiltin("Math", "imul", ESVersion.ES2015)
case object FroundBuiltin extends NamespacedBuiltin("Math", "fround", ESVersion.ES2015)
case object ObjectIsBuiltin extends NamespacedBuiltin("Object", "is", VarField.is, ESVersion.ES2015)
case object ImulBuiltin extends NamespacedBuiltin("Math", "imul", VarField.imul, ESVersion.ES2015)
case object FroundBuiltin extends NamespacedBuiltin("Math", "fround", VarField.fround, ESVersion.ES2015)
case object PrivateSymbolBuiltin
extends GlobalVarBuiltin("Symbol", "privateJSFieldSymbol", ESVersion.ES2015)
case object GetOwnPropertyDescriptorsBuiltin
extends NamespacedBuiltin("Object", "getOwnPropertyDescriptors", ESVersion.ES2017)
extends GlobalVarBuiltin("Symbol", VarField.privateJSFieldSymbol, ESVersion.ES2015)
case object GetOwnPropertyDescriptorsBuiltin extends NamespacedBuiltin("Object",
"getOwnPropertyDescriptors", VarField.getOwnPropertyDescriptors, ESVersion.ES2017)
}
Loading
0