Skip to main content

Phase 07: Mochi extern emitter

Status: Planned

Deliverables

  1. package3/kotlin/extern/emit.goEmit(surface *APIObject, wrapperDir string, outputPath string) error — write shim.mochi from the type-mapped APIObject and the libwrap.h entry points.
  2. package3/kotlin/extern/names.go — naming rules: Kotlin FooBar.bazQux → Mochi foo_bar_baz_qux, collision avoidance (suffix _2 if a name is taken).
  3. package3/kotlin/extern/format.goshim.mochi formatting: one extern type per Kotlin class, one extern fn per function, sorted by class then by function name.
  4. package3/kotlin/extern/custom.go — preserve custom overrides: if an existing shim.mochi has extern fn ... custom, do not overwrite those lines on mochi pkg sync kotlin.

shim.mochi format

// Code generated by mochi pkg lock. DO NOT EDIT.
// Artifact: com.squareup.okhttp3:[email protected]
// Wrapper: kotlin_wrap/okhttp/libwrap.so (sha256: a7c3d5...)
// Metadata: schema-version 9

extern type OkHttpClient
extern type Request
extern type RequestBuilder
extern type Response
extern type ResponseBody
extern type Call

extern fn okhttp_client_new(): OkHttpClient from kotlin "com.squareup.okhttp3.OkHttpClient.<init>"
extern fn okhttp_request_builder_new(): RequestBuilder from kotlin "com.squareup.okhttp3.Request.Builder.<init>"
extern fn okhttp_request_builder_url(b: RequestBuilder, url: string): RequestBuilder from kotlin "com.squareup.okhttp3.Request.Builder.url"
extern fn okhttp_request_builder_build(b: RequestBuilder): Request from kotlin "com.squareup.okhttp3.Request.Builder.build"
extern fn okhttp_client_new_call(c: OkHttpClient, req: Request): Call from kotlin "com.squareup.okhttp3.OkHttpClient.newCall"
extern fn okhttp_call_execute(call: Call): Response from kotlin "com.squareup.okhttp3.Call.execute"
extern fn okhttp_response_body(resp: Response): ResponseBody from kotlin "com.squareup.okhttp3.Response.body"
extern fn okhttp_response_body_string(body: ResponseBody): string from kotlin "com.squareup.okhttp3.ResponseBody.string"

The from kotlin "..." annotation is used by the Mochi linker to resolve the extern fn to the compiled libwrap.so entry point.

Gate

Emit shim.mochi for [email protected] and verify:

  1. The Mochi parser accepts all emitted shim.mochi files without parse errors.
  2. Every extern fn in the shim has a corresponding symbol in libwrap.h.
  3. Every extern fn name is unique within the shim (no collisions).
  4. mochi pkg sync kotlin on an existing shim with one extern fn ... custom line preserves that line unchanged.
  5. The generated comment header contains the correct artifact coordinates, SHA-256, and metadata schema version.