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

Commit 7465e39

Browse files
authored
Merge pull request #112 from sjrd/constant-strings-for-typedata-name
Store the name data of classes along with constant strings.
2 parents a2bc825 + b4ca991 commit 7465e39

File tree

5 files changed

+55
-35
lines changed

5 files changed

+55
-35
lines changed

wasm/src/main/scala/ir2wasm/HelperFunctions.scala

+14-8
Original file line numberDiff line numberDiff line change
@@ -264,13 +264,17 @@ object HelperFunctions {
264264
instrs += CALL(WasmFunctionName.stringConcat)
265265
} {
266266
// it is not an array; its name is stored in nameData
267-
instrs += LOCAL_GET(typeDataParam)
268-
instrs += STRUCT_GET(
269-
WasmStructTypeName.typeData,
270-
WasmFieldIdx.typeData.nameDataIdx
271-
)
272-
instrs += REF_AS_NOT_NULL
273-
instrs += CALL(WasmFunctionName.createStringFromData)
267+
for (
268+
idx <- List(
269+
WasmFieldIdx.typeData.nameOffsetIdx,
270+
WasmFieldIdx.typeData.nameSizeIdx,
271+
WasmFieldIdx.typeData.nameStringIndexIdx
272+
)
273+
) {
274+
instrs += LOCAL_GET(typeDataParam)
275+
instrs += STRUCT_GET(WasmStructTypeName.typeData, idx)
276+
}
277+
instrs += CALL(WasmFunctionName.stringLiteral)
274278
}
275279

276280
// typeData.name := <top of stack> ; leave it on the stack
@@ -483,7 +487,9 @@ object HelperFunctions {
483487
instrs += LOCAL_GET(typeDataParam)
484488

485489
// typeData := new typeData(...)
486-
instrs += REF_NULL(WasmHeapType.None) // nameData
490+
instrs += I32_CONST(0) // nameOffset
491+
instrs += I32_CONST(0) // nameSize
492+
instrs += I32_CONST(0) // nameStringIndex
487493
instrs += I32_CONST(KindArray) // kind = KindArray
488494
instrs += I32_CONST(0) // specialInstanceTypes = 0
489495

wasm/src/main/scala/ir2wasm/WasmBuilder.scala

+1-7
Original file line numberDiff line numberDiff line change
@@ -281,13 +281,7 @@ class WasmBuilder(coreSpec: CoreSpec) {
281281
)
282282
}
283283

284-
val nameDataValueItems = nameStr.toList.map(c => I32_CONST(c.toInt))
285-
val nameDataValueArrayNew =
286-
ARRAY_NEW_FIXED(
287-
WasmTypeName.WasmArrayTypeName.i16Array,
288-
nameDataValueItems.size
289-
)
290-
val nameDataValue: List[WasmInstr] = nameDataValueItems :+ nameDataValueArrayNew
284+
val nameDataValue: List[WasmInstr] = ctx.getConstantStringDataInstr(nameStr)
291285

292286
val strictAncestorsValue: List[WasmInstr] = {
293287
typeRef match {

wasm/src/main/scala/wasm4s/Names.scala

+23-15
Original file line numberDiff line numberDiff line change
@@ -313,12 +313,18 @@ object Names {
313313
// Fields of the typeData structs
314314
object typeData {
315315

316-
/** The name data as `(ref null (array u16))` so that it can be initialized as a constant.
316+
/** The name data as the 3 arguments to `stringLiteral`.
317317
*
318-
* It is non-null for primitives and for classes. It is null for array types, as array types
319-
* compute their `name` from the `name` of their component type.
318+
* It is only meaningful for primitives and for classes. For array types, they are all 0, as
319+
* array types compute their `name` from the `name` of their component type.
320320
*/
321-
val nameData = new WasmFieldName("nameData")
321+
val nameOffset = new WasmFieldName("nameOffset")
322+
323+
/** See `nameOffset`. */
324+
val nameSize = new WasmFieldName("nameSize")
325+
326+
/** See `nameOffset`. */
327+
val nameStringIndex = new WasmFieldName("nameStringIndex")
322328

323329
/** The kind of type data, an `i32`.
324330
*
@@ -410,17 +416,19 @@ object Names {
410416
val uniqueRegularField = WasmFieldIdx(2)
411417

412418
object typeData {
413-
val nameDataIdx = WasmFieldIdx(0)
414-
val kindIdx = WasmFieldIdx(1)
415-
val specialInstanceTypesIdx = WasmFieldIdx(2)
416-
val strictAncestorsIdx = WasmFieldIdx(3)
417-
val componentTypeIdx = WasmFieldIdx(4)
418-
val nameIdx = WasmFieldIdx(5)
419-
val classOfIdx = WasmFieldIdx(6)
420-
val arrayOfIdx = WasmFieldIdx(7)
421-
val cloneFunctionIdx = WasmFieldIdx(8)
422-
val isJSClassInstanceIdx = WasmFieldIdx(9)
423-
val reflectiveProxiesIdx = WasmFieldIdx(10)
419+
val nameOffsetIdx = WasmFieldIdx(0)
420+
val nameSizeIdx = WasmFieldIdx(1)
421+
val nameStringIndexIdx = WasmFieldIdx(2)
422+
val kindIdx = WasmFieldIdx(3)
423+
val specialInstanceTypesIdx = WasmFieldIdx(4)
424+
val strictAncestorsIdx = WasmFieldIdx(5)
425+
val componentTypeIdx = WasmFieldIdx(6)
426+
val nameIdx = WasmFieldIdx(7)
427+
val classOfIdx = WasmFieldIdx(8)
428+
val arrayOfIdx = WasmFieldIdx(9)
429+
val cloneFunctionIdx = WasmFieldIdx(10)
430+
val isJSClassInstanceIdx = WasmFieldIdx(11)
431+
val reflectiveProxiesIdx = WasmFieldIdx(12)
424432
}
425433

426434
object reflectiveProxy {

wasm/src/main/scala/wasm4s/Wasm.scala

+12-2
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,18 @@ object WasmStructType {
135135
def typeData(implicit ctx: ReadOnlyWasmContext): WasmStructType = WasmStructType(
136136
List(
137137
WasmStructField(
138-
WasmFieldName.typeData.nameData,
139-
WasmRefType.nullable(WasmArrayTypeName.i16Array),
138+
WasmFieldName.typeData.nameOffset,
139+
WasmInt32,
140+
isMutable = false
141+
),
142+
WasmStructField(
143+
WasmFieldName.typeData.nameSize,
144+
WasmInt32,
145+
isMutable = false
146+
),
147+
WasmStructField(
148+
WasmFieldName.typeData.nameStringIndex,
149+
WasmInt32,
140150
isMutable = false
141151
),
142152
WasmStructField(

wasm/src/main/scala/wasm4s/WasmContext.scala

+5-3
Original file line numberDiff line numberDiff line change
@@ -200,16 +200,18 @@ abstract class TypeDefinableWasmContext extends ReadOnlyWasmContext { this: Wasm
200200
}
201201
}
202202

203-
def getConstantStringInstr(str: String): List[WasmInstr] = {
203+
def getConstantStringInstr(str: String): List[WasmInstr] =
204+
getConstantStringDataInstr(str) :+ WasmInstr.CALL(WasmFunctionName.stringLiteral)
205+
206+
def getConstantStringDataInstr(str: String): List[WasmInstr.I32_CONST] = {
204207
val data = addConstantStringGlobal(str)
205208
List(
206209
WasmInstr.I32_CONST(data.offset),
207210
// Assuming that the stringLiteral method will instantiate the
208211
// constant string from the data section using "array.newData $i16Array ..."
209212
// The length of the array should be equal to the length of the WTF-16 encoded string
210213
WasmInstr.I32_CONST(str.length()),
211-
WasmInstr.I32_CONST(data.constantStringIndex),
212-
WasmInstr.CALL(WasmFunctionName.stringLiteral)
214+
WasmInstr.I32_CONST(data.constantStringIndex)
213215
)
214216
}
215217

0 commit comments

Comments
 (0)