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

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Wasm: Implement checked nullPointers.
  • Loading branch information
sjrd committed Aug 29, 2024
commit 58af468534ea2373d1b85fb9f6e803c40d1e18ad
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,6 @@ final class WebAssemblyLinkerBackend(config: LinkerBackendImpl.Config)
coreSpec.moduleKind == ModuleKind.ESModule,
s"The WebAssembly backend only supports ES modules; was ${coreSpec.moduleKind}."
)
require(
coreSpec.semantics.nullPointers == CheckedBehavior.Unchecked,
"The WebAssembly backend currently only supports CheckedBehavior.Unchecked semantics; " +
s"was ${coreSpec.semantics}."
)
require(
coreSpec.semantics.strictFloats,
"The WebAssembly backend only supports strict float semantics."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,10 @@ final class CoreWasmLib(coreSpec: CoreSpec) {
genCheckedStringCharAt()
}

if (semantics.nullPointers != CheckedBehavior.Unchecked) {
genThrowNullPointerException()
}

if (semantics.moduleInit == CheckedBehavior.Fatal) {
genThrowModuleInitError()
}
Expand Down Expand Up @@ -1361,6 +1365,26 @@ final class CoreWasmLib(coreSpec: CoreSpec) {
fb.buildAndAddToModule()
}

/** `throwNullPointerException: void -> void`.
*
* This function always throws. It should be followed by an `unreachable`
* statement.
*/
private def genThrowNullPointerException()(implicit ctx: WasmContext): Unit = {
val typeDataType = RefType(genTypeID.typeData)

val fb = newFunctionBuilder(genFunctionID.throwNullPointerException)

maybeWrapInUBE(fb, semantics.nullPointers) {
genNewScalaClass(fb, NullPointerExceptionClass, NoArgConstructorName) {
}
}
fb += ExternConvertAny
fb += Throw(genTagID.exception)

fb.buildAndAddToModule()
}

/** Generates the `arrayGet.x` functions. */
private def genArrayGets()(implicit ctx: WasmContext): Unit = {
for (baseRef <- arrayBaseRefs)
Expand Down Expand Up @@ -2248,10 +2272,21 @@ final class CoreWasmLib(coreSpec: CoreSpec) {
val valueParam = fb.addParam("value", RefType.any)
fb.setResultType(RefType.any)

fb += LocalGet(valueParam)
fb += Call(genFunctionID.anyGetTypeData)
fb += RefAsNonNull // NPE for null.getName()
fb += ReturnCall(genFunctionID.typeDataName)
if (semantics.nullPointers == CheckedBehavior.Unchecked) {
fb += LocalGet(valueParam)
fb += Call(genFunctionID.anyGetTypeData)
fb += RefAsNonNull // NPE for null.getName()
fb += ReturnCall(genFunctionID.typeDataName)
} else {
fb.block() { npeLabel =>
fb += LocalGet(valueParam)
fb += Call(genFunctionID.anyGetTypeData)
fb += BrOnNull(npeLabel) // NPE for null.getName()
fb += ReturnCall(genFunctionID.typeDataName)
}
fb += Call(genFunctionID.throwNullPointerException)
fb += Unreachable
}

fb.buildAndAddToModule()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,10 @@ object Emitter {
StringArgConstructorName)
},

cond(nullPointers != Unchecked) {
instantiateClass(NullPointerExceptionClass, NoArgConstructorName)
},

cond(stringIndexOutOfBounds != Unchecked) {
instantiateClass(StringIndexOutOfBoundsExceptionClass,
IntArgConstructorName)
Expand Down
Loading