3030 JSObj * {.inheritable , pure .} = object
3131 o* : JSRef
3232 JSString * = object of JSObj
33- JSExternRef * {.importc : " __externref_t" , noinit .} = object
34- JSExternObj * [T] {.noinit .} = distinct JSExternRef
33+ JSExternObjBase {.inheritable , pure , noinit , importc : " __externref_t" , bycopy .} = object
34+
35+ JSExternRef * = JSExternObjBase
36+
37+ macro parent (t: typedesc ): untyped =
38+ let n = t.getType ()[1 ]
39+ if sameType (n, bindSym " JSObj" ):
40+ return bindSym " JSExternRef"
41+ else :
42+ let imp = n.getTypeImpl ()
43+ imp.expectKind (nnkObjectTy)
44+ let base = imp[1 ][0 ]
45+ result = newTree (nnkObjectTy,
46+ newEmptyNode (),
47+ newTree (nnkOfInherit,
48+ newTree (nnkBracketExpr, ident " JSExternObj" , base)),
49+ newEmptyNode ())
50+
51+ type
52+ JSExternObj * [T] {.noinit , importc : " __externref_t" , bycopy .} = parent (T)
3553
3654type ExternRefTable {.importc .} = object
3755var globalRefs {.codegendecl : " static __externref_t $2[0]" .}: ExternRefTable
@@ -40,8 +58,6 @@ proc wasmTableSet(t: ExternRefTable, i: cint, e: JSExternRef) {.importc: "__buil
4058proc wasmTableGet (t: ExternRefTable , i: cint ): JSExternRef {.importc : " __builtin_wasm_table_get" , nodecl .}
4159
4260proc nullExternRef (): JSExternRef {.importc : " __builtin_wasm_ref_null_extern" , nodecl .}
43- # proc blabla(): JSExternRef {.importc, constructor: "blabla()".} =
44- # discard
4561
4662proc globalRefsTab (): int =
4763 proc initGlobalRefsTab (): int =
@@ -132,7 +148,7 @@ template toWasmWrapperType(t: typedesc): typedesc =
132148 elif t is JSRef :
133149 JSExternRef
134150 elif t is JSObj :
135- JSExternObj [t]
151+ JSExternObj [t] | t
136152 else :
137153 t
138154
@@ -377,10 +393,13 @@ proc `==`*(n: typeof(nil), e: JSExternObj): bool {.inline, enforceNoRaises.} = i
377393converter toJSRef * (e: JSExternRef ): JSRef =
378394 createJSRef (e)
379395
380- converter toJSExternObj * [T: JSObj ](o: T): JSExternObj [T] =
396+ converter toJSExternObj * [T: JSObj ](o: T): JSExternObj [T] {. inline .} =
381397 JSExternObj [T](wasmTableGet (globalRefs, cast [int32 ](o.o)))
382398
383- converter toJSObj * [T: JSObj ](e: JSExternObj [T]): T =
399+ converter toJSObj * [T: JSObj ](e: JSExternObj [T]): T {.inline .} =
400+ T (o: cast [JSRef ](storeRef (JSExternRef (e))))
401+
402+ proc to * [T: JSObj , F](e: JSExternObj [F]): T {.inline .} =
384403 T (o: cast [JSRef ](storeRef (JSExternRef (e))))
385404
386405proc toFuncExternRef (p: pointer ): JSExternFuncRef {.importc , codegendecl : codegenDeclStr (" \\ 06\\ 01_nimm.exports.__indirect_function_table.get" ).}
0 commit comments