Skip to content
This repository has been archived by the owner on Jul 12, 2024. It is now read-only.

Clean up identifier names. #139

Merged
merged 3 commits into from
May 22, 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

Large diffs are not rendered by default.

1,274 changes: 620 additions & 654 deletions wasm/src/main/scala/org/scalajs/linker/backend/wasmemitter/CoreWasmLib.scala

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@ final class Emitter(config: Emitter.Config) {

val fb =
new FunctionBuilder(ctx.moduleBuilder, genFunctionID.start, OriginalName("start"), pos)
val instrs: fb.type = fb

// Initialize itables
for (clazz <- sortedClasses if clazz.kind.isClass && clazz.hasDirectInstances) {
Expand All @@ -154,34 +153,34 @@ final class Emitter(config: Emitter.Config) {

interfaces.foreach { iface =>
val idx = ctx.getItableIdx(iface)
instrs += wa.GlobalGet(genGlobalID.forITable(className))
instrs += wa.I32Const(idx)
fb += wa.GlobalGet(genGlobalID.forITable(className))
fb += wa.I32Const(idx)

for (method <- iface.tableEntries)
instrs += ctx.refFuncWithDeclaration(resolvedMethodInfos(method).tableEntryName)
instrs += wa.StructNew(genTypeID.forITable(iface.name))
instrs += wa.ArraySet(genTypeID.itables)
fb += ctx.refFuncWithDeclaration(resolvedMethodInfos(method).tableEntryID)
fb += wa.StructNew(genTypeID.forITable(iface.name))
fb += wa.ArraySet(genTypeID.itables)
}
}
}

locally {
// For array classes, resolve methods in jl.Object
val globalName = genGlobalID.arrayClassITable
val globalID = genGlobalID.arrayClassITable
val resolvedMethodInfos = ctx.getClassInfo(ObjectClass).resolvedMethodInfos

for {
interfaceName <- List(SerializableClass, CloneableClass)
// Use getClassInfoOption in case the reachability analysis got rid of those interfaces
interfaceInfo <- ctx.getClassInfoOption(interfaceName)
} {
instrs += wa.GlobalGet(globalName)
instrs += wa.I32Const(ctx.getItableIdx(interfaceInfo))
fb += wa.GlobalGet(globalID)
fb += wa.I32Const(ctx.getItableIdx(interfaceInfo))

for (method <- interfaceInfo.tableEntries)
instrs += ctx.refFuncWithDeclaration(resolvedMethodInfos(method).tableEntryName)
instrs += wa.StructNew(genTypeID.forITable(interfaceName))
instrs += wa.ArraySet(genTypeID.itables)
fb += ctx.refFuncWithDeclaration(resolvedMethodInfos(method).tableEntryID)
fb += wa.StructNew(genTypeID.forITable(interfaceName))
fb += wa.ArraySet(genTypeID.itables)
}
}

Expand All @@ -191,8 +190,8 @@ final class Emitter(config: Emitter.Config) {
for (fieldDef <- clazz.fields) {
fieldDef match {
case FieldDef(flags, name, _, _) if !flags.namespace.isStatic =>
instrs += wa.Call(genFunctionID.newSymbol)
instrs += wa.GlobalSet(genGlobalID.forJSPrivateField(name.name))
fb += wa.Call(genFunctionID.newSymbol)
fb += wa.GlobalSet(genGlobalID.forJSPrivateField(name.name))
case _ =>
()
}
Expand All @@ -202,12 +201,12 @@ final class Emitter(config: Emitter.Config) {
// Emit the static initializers

for (clazz <- sortedClasses if clazz.hasStaticInitializer) {
val funcName = genFunctionID.forMethod(
val funcID = genFunctionID.forMethod(
MemberNamespace.StaticConstructor,
clazz.className,
StaticInitializerName
)
instrs += wa.Call(funcName)
fb += wa.Call(funcID)
}

// Initialize the top-level exports that require it
Expand All @@ -216,56 +215,55 @@ final class Emitter(config: Emitter.Config) {
// Load the (initial) exported value on the stack
tle.tree match {
case TopLevelJSClassExportDef(_, exportName) =>
instrs += wa.Call(genFunctionID.loadJSClass(tle.owningClass))
fb += wa.Call(genFunctionID.loadJSClass(tle.owningClass))
case TopLevelModuleExportDef(_, exportName) =>
instrs += wa.Call(genFunctionID.loadModule(tle.owningClass))
fb += wa.Call(genFunctionID.loadModule(tle.owningClass))
case TopLevelMethodExportDef(_, methodDef) =>
instrs += ctx.refFuncWithDeclaration(genFunctionID.forExport(tle.exportName))
fb += ctx.refFuncWithDeclaration(genFunctionID.forExport(tle.exportName))
if (methodDef.restParam.isDefined) {
instrs += wa.I32Const(methodDef.args.size)
instrs += wa.Call(genFunctionID.makeExportedDefRest)
fb += wa.I32Const(methodDef.args.size)
fb += wa.Call(genFunctionID.makeExportedDefRest)
} else {
instrs += wa.Call(genFunctionID.makeExportedDef)
fb += wa.Call(genFunctionID.makeExportedDef)
}
case TopLevelFieldExportDef(_, _, fieldIdent) =>
/* Usually redundant, but necessary if the static field is never
* explicitly set and keeps its default (zero) value instead. In that
* case this initial call is required to publish that zero value (as
* opposed to the default `undefined` value of the JS `let`).
*/
instrs += wa.GlobalGet(genGlobalID.forStaticField(fieldIdent.name))
fb += wa.GlobalGet(genGlobalID.forStaticField(fieldIdent.name))
}

// Call the export setter
instrs += wa.Call(genFunctionID.forTopLevelExportSetter(tle.exportName))
fb += wa.Call(genFunctionID.forTopLevelExportSetter(tle.exportName))
}

// Emit the module initializers

moduleInitializers.foreach { init =>
def genCallStatic(className: ClassName, methodName: MethodName): Unit = {
val functionName =
genFunctionID.forMethod(MemberNamespace.PublicStatic, className, methodName)
instrs += wa.Call(functionName)
val funcID = genFunctionID.forMethod(MemberNamespace.PublicStatic, className, methodName)
fb += wa.Call(funcID)
}

ModuleInitializerImpl.fromInitializer(init) match {
case ModuleInitializerImpl.MainMethodWithArgs(className, encodedMainMethodName, args) =>
// vtable of Array[String]
instrs += wa.GlobalGet(genGlobalID.forVTable(BoxedStringClass))
instrs += wa.I32Const(1)
instrs += wa.Call(genFunctionID.arrayTypeData)
fb += wa.GlobalGet(genGlobalID.forVTable(BoxedStringClass))
fb += wa.I32Const(1)
fb += wa.Call(genFunctionID.arrayTypeData)

// itable of Array[String]
instrs += wa.GlobalGet(genGlobalID.arrayClassITable)
fb += wa.GlobalGet(genGlobalID.arrayClassITable)

// underlying array of args
args.foreach(arg => instrs ++= ctx.getConstantStringInstr(arg))
instrs += wa.ArrayNewFixed(genTypeID.anyArray, args.size)
args.foreach(arg => fb ++= ctx.getConstantStringInstr(arg))
fb += wa.ArrayNewFixed(genTypeID.anyArray, args.size)

// array object
val stringArrayTypeRef = ArrayTypeRef(ClassRef(BoxedStringClass), 1)
instrs += wa.StructNew(genTypeID.forArrayClass(stringArrayTypeRef))
fb += wa.StructNew(genTypeID.forArrayClass(stringArrayTypeRef))

// call
genCallStatic(className, encodedMainMethodName)
Expand Down Expand Up @@ -295,8 +293,8 @@ final class Emitter(config: Emitter.Config) {
* Element section with the declarative mode is the recommended way to
* introduce these declarations.
*/
val exprs = funcDeclarations.map { name =>
wa.Expr(List(wa.RefFunc(name)))
val exprs = funcDeclarations.map { funcID =>
wa.Expr(List(wa.RefFunc(funcID)))
}
ctx.moduleBuilder.addElement(
wamod.Element(watpe.RefType.funcref, exprs, wamod.Element.Mode.Declarative)
Expand Down
Loading