diff options
| author | Ganesh Viswanathan <dev@genotrance.com> | 2019-01-27 15:45:31 -0600 |
|---|---|---|
| committer | genotrance <dev@genotrance.com> | 2019-01-27 22:05:54 -0600 |
| commit | 89ff8c0cc00a3d9e7d93613d38f2df13bdf31e2d (patch) | |
| tree | 2574c2234ccc149e96581050bc02c832bfa010cb | |
| parent | f7a77b7ab9c7cae36014e1991245e0d4c1da7099 (diff) | |
| download | nimterop-89ff8c0cc00a3d9e7d93613d38f2df13bdf31e2d.tar.gz nimterop-89ff8c0cc00a3d9e7d93613d38f2df13bdf31e2d.zip | |
Remove Result, add plugin.nim, forceBuild
| -rw-r--r-- | nimterop/cimport.nim | 16 | ||||
| -rw-r--r-- | nimterop/getters.nim | 11 | ||||
| -rw-r--r-- | nimterop/globals.nim | 15 | ||||
| -rw-r--r-- | nimterop/plugin.nim | 8 | ||||
| -rw-r--r-- | tests/tmath.nim | 2 | ||||
| -rw-r--r-- | tests/tnimterop_c.nim | 2 |
6 files changed, 29 insertions, 25 deletions
diff --git a/nimterop/cimport.nim b/nimterop/cimport.nim index 0a4a138..cc8f44d 100644 --- a/nimterop/cimport.nim +++ b/nimterop/cimport.nim @@ -180,12 +180,14 @@ macro cPlugin*(body): untyped = ## ## .. code-block:: nim ## - ## proc onSymbol(sym: var Symbol): Result {.exportc, dynlib.} + ## proc onSymbol(sym: var Symbol) {.exportc, dynlib.} ## ## `onSymbol()` can be used to handle symbol name modifications required due to invalid - ## characters like `_` or to rename duplicate types. It can also be used to remove prefixes - ## and suffixes. The symbol name and type is provided to the callback and the name can be - ## modified. Symbol types can be any of the following: + ## characters like leading/trailing `_` or rename symbols that would clash due to Nim's style + ## insensitivity. It can also be used to remove prefixes and suffixes like `SDL_`. The symbol + ## name and type is provided to the callback and the name can be modified. + ## + ## Symbol types can be any of the following: ## - `nskConst` for constants ## - `nskType` for type identifiers, including primitive ## - `nskParam` for param names @@ -196,16 +198,16 @@ macro cPlugin*(body): untyped = cPlugin: import strutils - proc onSymbol*(sym: var Symbol): Result {.exportc, dynlib.} = + proc onSymbol*(sym: var Symbol) {.exportc, dynlib.} = sym.name = sym.name.strip(chars={'_'}) let - data = "import nimterop/cimport\n\n" & body.repr + data = "import nimterop/plugin\n\n" & body.repr hash = data.hash() phash = if hash<0: -hash else: hash path = getTempDir() / "nimterop_" & $phash & ".nim" - if not fileExists(path) or gStateCT.nocache: + if not fileExists(path) or gStateCT.nocache or compileOption("forceBuild"): writeFile(path, data) doAssert fileExists(path), "Unable to write plugin file: " & path diff --git a/nimterop/getters.nim b/nimterop/getters.nim index e2cfbe9..688ca23 100644 --- a/nimterop/getters.nim +++ b/nimterop/getters.nim @@ -2,7 +2,7 @@ import dynlib, macros, os, sequtils, sets, strformat, strutils, tables, times import regex -import "."/[git, globals, treesitter/runtime] +import "."/[git, globals, plugin, treesitter/runtime] const gReserved = """ addr and as asm @@ -88,7 +88,8 @@ proc getType*(str: string): string = result = gTypeMap[result] template checkUnderscores(str, errmsg: string): untyped = - doAssert str[0] != '_' and str[^1] != '_', errmsg + if str.len != 0: + doAssert str[0] != '_' and str[^1] != '_', errmsg proc getIdentifier*(str: string, kind: NimSymKind): string = doAssert str.len != 0, "Blank identifier error" @@ -96,9 +97,7 @@ proc getIdentifier*(str: string, kind: NimSymKind): string = if gStateRT.onSymbol != nil: var sym = Symbol(name: str, kind: kind) - res = gStateRT.onSymbol(sym) - - doAssert res.error == 0, res.message + gStateRT.onSymbol(sym) result = sym.name checkUnderscores(result, &"Identifier '{str}' still contains leading/trailing underscores '_' after 'cPlugin:onSymbol()': result '{result}'") @@ -343,5 +342,5 @@ proc loadPlugin*(fullpath: string) = let lib = loadLib(pdll) doAssert lib != nil, "Plugin $1 compiled to $2 failed to load" % [fullpath, pdll] - gStateRT.onSymbol = cast[type(gStateRT.onSymbol)](lib.symAddr("onSymbol")) + gStateRT.onSymbol = cast[onSymbolType](lib.symAddr("onSymbol")) doAssert gStateRT.onSymbol != nil, "onSymbol() load failed from " & pdll diff --git a/nimterop/globals.nim b/nimterop/globals.nim index dac31e5..57b9a81 100644 --- a/nimterop/globals.nim +++ b/nimterop/globals.nim @@ -1,7 +1,9 @@ -import macros, sequtils, sets, tables +import sequtils, sets, tables import regex +import "."/plugin + when not declared(CIMPORT): import "."/treesitter/runtime @@ -45,14 +47,6 @@ type tonim*: proc (ast: ref Ast, node: TSNode) regex*: Regex - Symbol* = object - name*: string - kind*: NimSymKind - - Result* = object - error*: int - message*: string - State = object compile*, defines*, headers*, includeDirs*, searchDirs*, symOverride*: seq[string] @@ -67,7 +61,8 @@ type when not declared(CIMPORT): grammar*: seq[tuple[grammar: string, call: proc(ast: ref Ast, node: TSNode) {.nimcall.}]] - onSymbol*: proc(sym: var Symbol): Result {.cdecl.} + onSymbol*: onSymbolType + var gStateCT {.compiletime, used.}: State gStateRT {.used.}: State diff --git a/nimterop/plugin.nim b/nimterop/plugin.nim new file mode 100644 index 0000000..909643c --- /dev/null +++ b/nimterop/plugin.nim @@ -0,0 +1,8 @@ +import macros + +type + Symbol* = object + name*: string + kind*: NimSymKind + + onSymbolType* = proc(sym: var Symbol) {.cdecl.}
\ No newline at end of file diff --git a/tests/tmath.nim b/tests/tmath.nim index a561251..63f1786 100644 --- a/tests/tmath.nim +++ b/tests/tmath.nim @@ -14,7 +14,7 @@ cAddStdDir() cPlugin: import strutils - proc onSymbol*(sym: var Symbol): Result {.exportc, dynlib.} = + proc onSymbol*(sym: var Symbol) {.exportc, dynlib.} = sym.name = sym.name.strip(chars={'_'}) cImport cSearchPath("math.h") diff --git a/tests/tnimterop_c.nim b/tests/tnimterop_c.nim index c9b4667..457db7b 100644 --- a/tests/tnimterop_c.nim +++ b/tests/tnimterop_c.nim @@ -12,7 +12,7 @@ cCompile cSearchPath("test.c") cPlugin: import strutils - proc onSymbol*(sym: var Symbol): Result {.exportc, dynlib.} = + proc onSymbol*(sym: var Symbol) {.exportc, dynlib.} = sym.name = sym.name.strip(chars={'_'}) cImport cSearchPath "test.h" |
