swiftwasm / swift

WebAssembly support for the Swift programming language

Home Page:https://swiftwasm.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

SwiftWASM REPL - allow import of WebAssembly.Memory

kerimcharfi opened this issue · comments

Motivation
In order to make SwiftWasm more debuggable a SwiftWasm REPL is needed. This would also be very helpful for debug tools.
Solution
My Idea to make this happen with as little effort as possible for the swiftwasm maintainers while still having the maximum of flexibility for debugging tools is the following:

prerequisit: there is a paused (dummy or real) SwiftWasm main module inside of a wasm runtime xy

  1. A REPL prompt text is submitted
  2. analyse the current callframe of the main module and extract variable names and their pointers from DWARF
  3. swift codegen: wrap REPL prompt in a exported function with raw pointers to all reachable variables as function parameters
  4. compile the wrapped REPL prompt with SwiftWASM into a new wasm module. Lets call it "side module"
  5. load side module into runtime
  6. import WebAssembly.Memory from paused main module into the compiled side module
  7. call the exported generated repl wrapper function from side module and pass pointers of the reachable variables into the function
  8. add newly declared variables (found by dwarf in generated side module) to some kind of REPL session
  9. go back to step 0

Im currently blocked by step 5: importing a external WebAssembly.Memory into a Module compiled by SwiftWasm

I'm having problems with runtime initialization of the sidemodule crashing.

Whats needed

What I need to proceed is something similar to emscripten side modules.
Ideally theres also no swift runtime linked inside of a side module to improve performance by reducing parsing time and no call to the runtime initialization, which would f*** up the state of the runtime of the main module.

Questions

Are there already flags to avoid linking everything?
Can someone explain what happens inside __wasm_call_ctors?

Update

step 5 is not an issue anymore

▿ repl.astruct
  - myinti: 9
  - myfloat: 42.0
  ▿ mystruct: repl.aNestedStruct
    - a: 5

This is the first successful dump of a nested struct from a "sidemodule".
However, I need something like AssemblyScripts --memoryBase linker flag in order to proceed with my hacky prototype.