Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timotheecour/5fc8f646d09cf32a85ed820723a41f72 to your computer and use it in GitHub Desktop.
Save timotheecour/5fc8f646d09cf32a85ed820723a41f72 to your computer and use it in GitHub Desktop.
```nim
type
InterfaceVTableAnimal = object
<0>say: proc (this: RootRef): string {.nimcall.}
<1>test2: proc (this: RootRef) {.nimcall.}
type
Animal = Interface[InterfaceVTableAnimal]
proc initVTable(t: var InterfaceVTableAnimal; TIfaceUnpackedThis#: typedesc) =
mixin say
mixin test2
t.`<0>say` = proc (this: RootRef): string =
var ifaceUnpackedThis#: TIfaceUnpackedThis#
unpackObj(this, ifaceUnpackedThis#)
forceReturnValue(string,
checkRequiredMethod(say(ifaceUnpackedThis#), Animal))
t.`<1>test2` = proc (this: RootRef) =
var ifaceUnpackedThis#: TIfaceUnpackedThis#
unpackObj(this, ifaceUnpackedThis#)
forceReturnValue(void,
checkRequiredMethod(test2(ifaceUnpackedThis#), Animal))
proc say(this: Animal): string {.inline, stackTrace: off.} =
this.private_vTable.`<0>say`(this.private_obj)
proc test2(this: Animal) {.inline, stackTrace: off.} =
this.private_vTable.`<1>test2`(this.private_obj)
converter toAnimal[T: ref](a`gensym4: T): Animal {.inline.} =
to(a`gensym4, Animal)
registerInterfaceDecl(Animal):
proc say(): string
proc test2()
do:
t.`<0>say` = proc (this: RootRef): string =
var ifaceUnpackedThis#: TIfaceUnpackedThis#
unpackObj(this, ifaceUnpackedThis#)
forceReturnValue(string,
checkRequiredMethod(say(ifaceUnpackedThis#), Animal))
t.`<1>test2` = proc (this: RootRef) =
var ifaceUnpackedThis#: TIfaceUnpackedThis#
unpackObj(this, ifaceUnpackedThis#)
forceReturnValue(void,
checkRequiredMethod(test2(ifaceUnpackedThis#), Animal))
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment