Thanks to visit codestin.com
Credit goes to github.com

Skip to content

More minification. #4931

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 4 commits into from
Mar 16, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -186,13 +186,13 @@ private[emitter] final class ClassEmitter(sjsGen: SJSGen) {

val chainProtoWithGlobals = superClass match {
case None =>
WithGlobals.nil
WithGlobals(setPrototypeVar(ctorVar))

case Some(_) if shouldExtendJSError(className, superClass) =>
globalRef("Error").map(chainPrototypeWithLocalCtor(className, ctorVar, _))
globalRef("Error").map(chainPrototypeWithLocalCtor(className, ctorVar, _, localDeclPrototypeVar = false))

case Some(parentIdent) =>
WithGlobals(List(ctorVar.prototype := js.New(globalVar(VarField.h, parentIdent.name), Nil)))
WithGlobals(List(genAssignPrototype(ctorVar, js.New(globalVar(VarField.h, parentIdent.name), Nil))))
}

for {
Expand All @@ -208,12 +208,12 @@ private[emitter] final class ClassEmitter(sjsGen: SJSGen) {
js.JSDocConstructor(realCtorDef.head) ::
realCtorDef.tail :::
chainProto :::
(genIdentBracketSelect(ctorVar.prototype, "constructor") := ctorVar) ::
(genIdentBracketSelect(prototypeFor(ctorVar), "constructor") := ctorVar) ::

// Inheritable constructor
js.JSDocConstructor(inheritableCtorDef.head) ::
inheritableCtorDef.tail :::
(globalVar(VarField.h, className).prototype := ctorVar.prototype) :: Nil
(globalVar(VarField.h, className).prototype := prototypeFor(ctorVar)) :: Nil
)
}
}
Expand Down Expand Up @@ -243,8 +243,8 @@ private[emitter] final class ClassEmitter(sjsGen: SJSGen) {
val ctorVar = fileLevelVar(VarField.b, genName(className))

js.JSDocConstructor(ctorVar := ctorFun) ::
chainPrototypeWithLocalCtor(className, ctorVar, superCtor) :::
(genIdentBracketSelect(ctorVar.prototype, "constructor") := ctorVar) :: Nil
chainPrototypeWithLocalCtor(className, ctorVar, superCtor, localDeclPrototypeVar = true) :::
(genIdentBracketSelect(prototypeFor(ctorVar), "constructor") := ctorVar) :: Nil
}
}
}
Expand Down Expand Up @@ -333,15 +333,15 @@ private[emitter] final class ClassEmitter(sjsGen: SJSGen) {
}

private def chainPrototypeWithLocalCtor(className: ClassName, ctorVar: js.Tree,
superCtor: js.Tree)(implicit pos: Position): List[js.Tree] = {
superCtor: js.Tree, localDeclPrototypeVar: Boolean)(implicit pos: Position): List[js.Tree] = {
import TreeDSL._

val dummyCtor = fileLevelVar(VarField.hh, genName(className))

List(
js.JSDocConstructor(genConst(dummyCtor.ident, js.Function(false, Nil, None, js.Skip()))),
dummyCtor.prototype := superCtor.prototype,
ctorVar.prototype := js.New(dummyCtor, Nil)
genAssignPrototype(ctorVar, js.New(dummyCtor, Nil), localDeclPrototypeVar)
)
}

Expand Down Expand Up @@ -638,7 +638,7 @@ private[emitter] final class ClassEmitter(sjsGen: SJSGen) {
else globalVar(VarField.c, className)

if (namespace.isStatic) classVarRef
else classVarRef.prototype
else prototypeFor(classVarRef)
}

def genMemberNameTree(name: Tree)(
Expand Down Expand Up @@ -826,7 +826,7 @@ private[emitter] final class ClassEmitter(sjsGen: SJSGen) {

def genTypeData(className: ClassName, kind: ClassKind,
superClass: Option[ClassIdent], ancestors: List[ClassName],
jsNativeLoadSpec: Option[JSNativeLoadSpec])(
jsNativeLoadSpec: Option[JSNativeLoadSpec], hasInstances: Boolean)(
implicit moduleContext: ModuleContext,
globalKnowledge: GlobalKnowledge, pos: Position): WithGlobals[List[js.Tree]] = {
import TreeDSL._
Expand All @@ -836,21 +836,35 @@ private[emitter] final class ClassEmitter(sjsGen: SJSGen) {
val isJSType =
kind.isJSType

val isJSTypeParam =
if (isJSType) js.BooleanLiteral(true)
else js.Undefined()
/* The `kindOrCtor` param is either:
* - an int: 1 means isInterface; 2 means isJSType; 0 otherwise
* - a Scala class constructor: means 0 + assign `kindOrCtor.prototype.$classData = <this TypeData>;`
*
* We must only assign the `$classData` if the class is a regular
* (non-hijacked) Scala class, and if it has instances. Otherwise there is
* no Scala class constructor for the class at all.
*/
val kindOrCtorParam = {
if (isJSType) js.IntLiteral(2)
else if (kind == ClassKind.Interface) js.IntLiteral(1)
else if (kind.isClass && hasInstances) globalVar(VarField.c, className)
else js.IntLiteral(0)
}

val parentData = if (globalKnowledge.isParentDataAccessed) {
superClass.fold[js.Tree] {
val parentDataOpt = if (globalKnowledge.isParentDataAccessed) {
val parentData = superClass.fold[js.Tree] {
if (isObjectClass) js.Null()
else js.Undefined()
} { parent =>
globalVar(VarField.d, parent.name)
}
parentData :: Nil
} else {
js.Undefined()
Nil
}

assert(ancestors.headOption.contains(className),
s"The ancestors of ${className.nameString} do not start with itself: $ancestors")
val ancestorsRecord = js.ObjectConstr(
ancestors.withFilter(_ != ObjectClass).map(ancestor => (genAncestorIdent(ancestor), js.IntLiteral(1)))
)
Expand Down Expand Up @@ -902,15 +916,11 @@ private[emitter] final class ClassEmitter(sjsGen: SJSGen) {

isInstanceFunWithGlobals.flatMap { isInstanceFun =>
val allParams = List(
js.ObjectConstr(List(genAncestorIdent(className) -> js.IntLiteral(0))),
js.BooleanLiteral(kind == ClassKind.Interface),
kindOrCtorParam,
js.StringLiteral(RuntimeClassNameMapperImpl.map(
semantics.runtimeClassNameMapper, className.nameString)),
ancestorsRecord,
isJSTypeParam,
parentData,
isInstanceFun
)
ancestorsRecord
) ::: parentDataOpt ::: isInstanceFun :: Nil

val prunedParams =
allParams.reverse.dropWhile(_.isInstanceOf[js.Undefined]).reverse
Expand All @@ -922,14 +932,6 @@ private[emitter] final class ClassEmitter(sjsGen: SJSGen) {
}
}

def genSetTypeData(className: ClassName)(
implicit moduleContext: ModuleContext,
globalKnowledge: GlobalKnowledge, pos: Position): js.Tree = {
import TreeDSL._

globalVar(VarField.c, className).prototype DOT cpn.classData := globalVar(VarField.d, className)
}

def genModuleAccessor(className: ClassName, isJSClass: Boolean)(
implicit moduleContext: ModuleContext,
globalKnowledge: GlobalKnowledge, pos: Position): WithGlobals[List[js.Tree]] = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ private[emitter] object CoreJSLib {
defineLinkingInfo() :::
defineJSBuiltinsSnapshotsAndPolyfills() :::
declareCachedL0() :::
definePropertyName() :::
defineCharClass() :::
defineRuntimeFunctions() :::
defineObjectGetClassFunctions() :::
Expand Down Expand Up @@ -526,23 +525,6 @@ private[emitter] object CoreJSLib {
))
}

private def definePropertyName(): List[Tree] = {
/* Encodes a property name for runtime manipulation.
*
* Usage:
* env.propertyName({someProp:0})
* Returns:
* "someProp"
* Useful when the property is renamed by a global optimizer (like
* Closure) but we must still get hold of a string of that name for
* runtime reflection.
*/
defineFunction1(VarField.propertyName) { obj =>
val prop = varRef("prop")
ForIn(genEmptyImmutableLet(prop.ident), obj, Return(prop))
}
}

private def defineCharClass(): List[Tree] = {
val ctor = {
val c = varRef("c")
Expand All @@ -562,6 +544,7 @@ private[emitter] object CoreJSLib {
extractWithGlobals(globalClassDef(VarField.Char, CoreVar, None, ctor :: toStr :: Nil))
} else {
defineFunction(VarField.Char, ctor.args, ctor.body) :::
setPrototypeVar(globalVar(VarField.Char, CoreVar)) :::
assignES5ClassMembers(globalVar(VarField.Char, CoreVar), List(toStr))
}
}
Expand Down Expand Up @@ -1528,16 +1511,16 @@ private[emitter] object CoreJSLib {
val clsDef = {
extractWithGlobals(globalFunctionDef(VarField.ac, componentTypeRef,
ctor.args, ctor.restParam, ctor.body)) :::
(ArrayClass.prototype := New(globalVar(VarField.h, ObjectClass), Nil)) ::
(ArrayClass.prototype DOT "constructor" := ArrayClass) ::
genAssignPrototype(ArrayClass, New(globalVar(VarField.h, ObjectClass), Nil)) ::
(prototypeFor(ArrayClass) DOT "constructor" := ArrayClass) ::
assignES5ClassMembers(ArrayClass, members)
}

componentTypeRef match {
case _: ClassRef =>
clsDef :::
extractWithGlobals(globalFunctionDef(VarField.ah, ObjectClass, Nil, None, Skip())) :::
(globalVar(VarField.ah, ObjectClass).prototype := ArrayClass.prototype) :: Nil
(globalVar(VarField.ah, ObjectClass).prototype := prototypeFor(ArrayClass)) :: Nil
case _: PrimRef =>
clsDef
}
Expand Down Expand Up @@ -1651,23 +1634,34 @@ private[emitter] object CoreJSLib {
}

val initClass = {
val internalNameObj = varRef("internalNameObj")
val isInterface = varRef("isInterface")
/* This is either:
* - an int: 1 means isInterface; 2 means isJSType; 0 otherwise
* - a Scala class constructor: means 0 + assign `kindOrCtor.prototype.$classData = this;`
*/
val kindOrCtor = varRef("kindOrCtor")

val hasParentData = globalKnowledge.isParentDataAccessed

val fullName = varRef("fullName")
val ancestors = varRef("ancestors")
val isJSType = varRef("isJSType")
val parentData = varRef("parentData")
val isInstance = varRef("isInstance")
val internalName = varRef("internalName")
val that = varRef("that")
val depth = varRef("depth")
val obj = varRef("obj")
MethodDef(static = false, Ident(cpn.initClass),
paramList(internalNameObj, isInterface, fullName, ancestors,
isJSType, parentData, isInstance), None, {
val params =
if (hasParentData) paramList(kindOrCtor, fullName, ancestors, parentData, isInstance)
else paramList(kindOrCtor, fullName, ancestors, isInstance)
MethodDef(static = false, Ident(cpn.initClass), params, None, {
Block(
const(internalName, genCallHelper(VarField.propertyName, internalNameObj)),
if (globalKnowledge.isParentDataAccessed)
/* Extract the internalName, which is the first property of ancestors.
* We use `getOwnPropertyNames()`, which since ES 2015 guarantees
* to return non-integer string keys in creation order.
*/
const(internalName,
BracketSelect(Apply(genIdentBracketSelect(ObjectRef, "getOwnPropertyNames"), List(ancestors)), 0)),
if (hasParentData)
privateFieldSet(cpn.parentData, parentData)
else
Skip(),
Expand All @@ -1678,15 +1672,18 @@ private[emitter] object CoreJSLib {
Return(!(!(BracketSelect(that DOT cpn.ancestors, internalName))))
})
}),
privateFieldSet(cpn.isJSType, !(!isJSType)),
privateFieldSet(cpn.isJSType, kindOrCtor === 2),
publicFieldSet(cpn.name, fullName),
publicFieldSet(cpn.isInterface, isInterface),
publicFieldSet(cpn.isInterface, kindOrCtor === 1),
publicFieldSet(cpn.isInstance, isInstance || {
genArrowFunction(paramList(obj), {
Return(!(!(obj && (obj DOT classData) &&
BracketSelect(obj DOT classData DOT cpn.ancestors, internalName))))
})
}),
If(typeof(kindOrCtor) !== str("number"), {
kindOrCtor.prototype DOT cpn.classData := This()
}),
Return(This())
)
})
Expand All @@ -1697,7 +1694,6 @@ private[emitter] object CoreJSLib {
val name = varRef("name")

Block(
arrayClass.prototype DOT classData := This(),
const(name, str("[") + (componentData DOT cpn.arrayEncodedName)),
privateFieldSet(cpn.constr, arrayClass),
if (globalKnowledge.isParentDataAccessed)
Expand Down Expand Up @@ -1729,6 +1725,7 @@ private[emitter] object CoreJSLib {
MethodDef(static = false, Ident(cpn.initSpecializedArray),
paramList(componentData, arrayClass, typedArrayClass, isAssignableFromFun), None, {
Block(
arrayClass.prototype DOT classData := This(),
initArrayCommonBody(arrayClass, componentData, componentData, 1),
const(self, This()), // capture `this` for use in arrow fun
privateFieldSet(cpn.isAssignableFromFun, isAssignableFromFun || {
Expand Down Expand Up @@ -1833,14 +1830,19 @@ private[emitter] object CoreJSLib {
val members = set ::: copyTo ::: clone :: Nil

if (useClassesForRegularClasses) {
ClassDef(Some(ArrayClass.ident), Some(globalVar(VarField.ac, ObjectClass)),
ctor :: members)
Block(
ClassDef(Some(ArrayClass.ident), Some(globalVar(VarField.ac, ObjectClass)),
ctor :: members),
ArrayClass.prototype DOT cpn.classData := This()
)
} else {
Block(
FunctionDef(ArrayClass.ident, ctor.args, ctor.restParam, ctor.body) ::
(ArrayClass.prototype := New(globalVar(VarField.ah, ObjectClass), Nil)) ::
(ArrayClass.prototype DOT "constructor" := ArrayClass) ::
assignES5ClassMembers(ArrayClass, members)
genAssignPrototype(ArrayClass, New(globalVar(VarField.ah, ObjectClass), Nil), localDecl = true) ::
(prototypeFor(ArrayClass) DOT "constructor" := ArrayClass) ::
assignES5ClassMembers(ArrayClass, members) :::
(prototypeFor(ArrayClass) DOT cpn.classData := This()) ::
Nil
)
}
}
Expand Down Expand Up @@ -2000,6 +2002,7 @@ private[emitter] object CoreJSLib {
extractWithGlobals(globalClassDef(VarField.TypeData, CoreVar, None, ctor :: members))
} else {
defineFunction(VarField.TypeData, ctor.args, ctor.body) :::
setPrototypeVar(globalVar(VarField.TypeData, CoreVar)) :::
assignES5ClassMembers(globalVar(VarField.TypeData, CoreVar), members)
}
}
Expand Down Expand Up @@ -2159,7 +2162,7 @@ private[emitter] object CoreJSLib {
for {
MethodDef(static, name, args, restParam, body) <- members
} yield {
val target = if (static) classRef else classRef.prototype
val target = if (static) classRef else prototypeFor(classRef)
genPropSelect(target, name) := Function(arrow = false, args, restParam, body)
}
}
Expand Down
Loading