Remove Result, add plugin.nim, forceBuild
This commit is contained in:
parent
f7a77b7ab9
commit
89ff8c0cc0
6 changed files with 29 additions and 25 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
8
nimterop/plugin.nim
Normal file
8
nimterop/plugin.nim
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
import macros
|
||||
|
||||
type
|
||||
Symbol* = object
|
||||
name*: string
|
||||
kind*: NimSymKind
|
||||
|
||||
onSymbolType* = proc(sym: var Symbol) {.cdecl.}
|
||||
|
|
@ -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")
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue