beef331 / nimscripter

Quick and easy Nim <-> Nimscript interop
MIT License
149 stars 8 forks source link

VM Conversion doesnt properly handle distinct types. #15

Closed doongjohn closed 2 years ago

doongjohn commented 2 years ago

I'm trying to make something similar to AutoHotkey by combining wAuto and nimscripter but I can't export the functions

import nimscripter
import wAuto

proc sendToActiveWindow(
  text: string,
  raw = false,
  window = InvalidWindow,
  attach = false,
  restoreCapslock = false
) =
  send(text, raw, window, attach, restoreCapslock)

exportTo(
  autonim,
  sendToActiveWindow,
)

const
  scriptProcs = implNimScriptModule(autonim)
  ourScript = NimScriptPath"./example/mynimscript.nims"

let intr = loadScript(ourScript, scriptProcs)

error:

/home/---/projects/autonim/src/main.nim(27, 36) template/generic instantiation of `implNimscriptModule` from here
/home/---/.nimble/pkgs/nimscripter-1.0.14/nimscripter/expose.nim(94, 26) template/generic instantiation of `fromVm` from here
/home/---/.nimble/pkgs/nimscripter-1.0.14/nimscripter/vmconversion.nim(91, 73) Error: type mismatch: got <typedesc[HWND]>
but expected one of:
proc fromVm(t: typedesc[SomeOrdinal or char]; node: PNode): t:type
  first type mismatch at position: 2
  missing parameter: node
10 other mismatching symbols have been suppressed; compile with --showAllMismatches:on to see them
beef331 commented 2 years ago

What is the procedure type of autonim and it's parameters? I do not know what the issue would be without that information.

doongjohn commented 2 years ago

Isn't autonim a moduleName?

macro exportTo*(moduleName: untyped, procDefs: varargs[untyped]): untyped =
  ## Takes a module name and symbols, adding them to the proper table internally.
  result = newStmtList()
  var moduleName = $moduleName
  for pDef in procDefs:
    result.add newCall("addToCache", pdef, newLit(modulename))

also this is a function signature of send is\ https://github.com/khchen/wAuto/blob/fa8e642f4d78822bff4b7fad9d33d5ef3be81f34/wAuto/keyboard.nim#L337

proc send*(text: string, raw = false, window = InvalidWindow, attach = false,
    restoreCapslock = false) =
  ...

InvalidWindow is type of Window* = distinct HWND and HWND is just an int

beef331 commented 2 years ago

It is i forgot how my API worked. :smile: It errors about HWND so i guess it's the invalid window, you will probably need a better wrapping. It might be the default parameters aswell.

doongjohn commented 2 years ago

amazing I just changed the default parameter to primitive type and it worked!

proc sendToActiveWindow(
  text: string,
  raw = false,
  window = 0,
  attach = false,
  restoreCapslock = false
) =
  send(text, raw, Window window, attach, restoreCapslock)

Also I guess I found a minimal example of this issue?

type DistInt = distinct int

proc yay(a: DistInt) = discard

exportTo(
  moduleName,
  yay,
)

const scriptProcs = implNimScriptModule(moduleName)
beef331 commented 2 years ago

Yea this is odd since I do have procedures for T: distinct https://github.com/beef331/nimscripter/blob/master/src/nimscripter/vmconversion.nim#L40

beef331 commented 2 years ago

Oh it looks like i forgot to pass the argument, so yea I guess fix coming soon.