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
|
## .. 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
|
## `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
|
## characters like leading/trailing `_` or rename symbols that would clash due to Nim's style
|
||||||
## and suffixes. The symbol name and type is provided to the callback and the name can be
|
## insensitivity. It can also be used to remove prefixes and suffixes like `SDL_`. The symbol
|
||||||
## modified. Symbol types can be any of the following:
|
## 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
|
## - `nskConst` for constants
|
||||||
## - `nskType` for type identifiers, including primitive
|
## - `nskType` for type identifiers, including primitive
|
||||||
## - `nskParam` for param names
|
## - `nskParam` for param names
|
||||||
|
|
@ -196,16 +198,16 @@ macro cPlugin*(body): untyped =
|
||||||
cPlugin:
|
cPlugin:
|
||||||
import strutils
|
import strutils
|
||||||
|
|
||||||
proc onSymbol*(sym: var Symbol): Result {.exportc, dynlib.} =
|
proc onSymbol*(sym: var Symbol) {.exportc, dynlib.} =
|
||||||
sym.name = sym.name.strip(chars={'_'})
|
sym.name = sym.name.strip(chars={'_'})
|
||||||
|
|
||||||
let
|
let
|
||||||
data = "import nimterop/cimport\n\n" & body.repr
|
data = "import nimterop/plugin\n\n" & body.repr
|
||||||
hash = data.hash()
|
hash = data.hash()
|
||||||
phash = if hash<0: -hash else: hash
|
phash = if hash<0: -hash else: hash
|
||||||
path = getTempDir() / "nimterop_" & $phash & ".nim"
|
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)
|
writeFile(path, data)
|
||||||
|
|
||||||
doAssert fileExists(path), "Unable to write plugin file: " & path
|
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 regex
|
||||||
|
|
||||||
import "."/[git, globals, treesitter/runtime]
|
import "."/[git, globals, plugin, treesitter/runtime]
|
||||||
|
|
||||||
const gReserved = """
|
const gReserved = """
|
||||||
addr and as asm
|
addr and as asm
|
||||||
|
|
@ -88,7 +88,8 @@ proc getType*(str: string): string =
|
||||||
result = gTypeMap[result]
|
result = gTypeMap[result]
|
||||||
|
|
||||||
template checkUnderscores(str, errmsg: string): untyped =
|
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 =
|
proc getIdentifier*(str: string, kind: NimSymKind): string =
|
||||||
doAssert str.len != 0, "Blank identifier error"
|
doAssert str.len != 0, "Blank identifier error"
|
||||||
|
|
@ -96,9 +97,7 @@ proc getIdentifier*(str: string, kind: NimSymKind): string =
|
||||||
if gStateRT.onSymbol != nil:
|
if gStateRT.onSymbol != nil:
|
||||||
var
|
var
|
||||||
sym = Symbol(name: str, kind: kind)
|
sym = Symbol(name: str, kind: kind)
|
||||||
res = gStateRT.onSymbol(sym)
|
gStateRT.onSymbol(sym)
|
||||||
|
|
||||||
doAssert res.error == 0, res.message
|
|
||||||
|
|
||||||
result = sym.name
|
result = sym.name
|
||||||
checkUnderscores(result, &"Identifier '{str}' still contains leading/trailing underscores '_' after 'cPlugin:onSymbol()': result '{result}'")
|
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)
|
let lib = loadLib(pdll)
|
||||||
doAssert lib != nil, "Plugin $1 compiled to $2 failed to load" % [fullpath, 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
|
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 regex
|
||||||
|
|
||||||
|
import "."/plugin
|
||||||
|
|
||||||
when not declared(CIMPORT):
|
when not declared(CIMPORT):
|
||||||
import "."/treesitter/runtime
|
import "."/treesitter/runtime
|
||||||
|
|
||||||
|
|
@ -45,14 +47,6 @@ type
|
||||||
tonim*: proc (ast: ref Ast, node: TSNode)
|
tonim*: proc (ast: ref Ast, node: TSNode)
|
||||||
regex*: Regex
|
regex*: Regex
|
||||||
|
|
||||||
Symbol* = object
|
|
||||||
name*: string
|
|
||||||
kind*: NimSymKind
|
|
||||||
|
|
||||||
Result* = object
|
|
||||||
error*: int
|
|
||||||
message*: string
|
|
||||||
|
|
||||||
State = object
|
State = object
|
||||||
compile*, defines*, headers*, includeDirs*, searchDirs*, symOverride*: seq[string]
|
compile*, defines*, headers*, includeDirs*, searchDirs*, symOverride*: seq[string]
|
||||||
|
|
||||||
|
|
@ -67,7 +61,8 @@ type
|
||||||
when not declared(CIMPORT):
|
when not declared(CIMPORT):
|
||||||
grammar*: seq[tuple[grammar: string, call: proc(ast: ref Ast, node: TSNode) {.nimcall.}]]
|
grammar*: seq[tuple[grammar: string, call: proc(ast: ref Ast, node: TSNode) {.nimcall.}]]
|
||||||
|
|
||||||
onSymbol*: proc(sym: var Symbol): Result {.cdecl.}
|
onSymbol*: onSymbolType
|
||||||
|
|
||||||
var
|
var
|
||||||
gStateCT {.compiletime, used.}: State
|
gStateCT {.compiletime, used.}: State
|
||||||
gStateRT {.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:
|
cPlugin:
|
||||||
import strutils
|
import strutils
|
||||||
|
|
||||||
proc onSymbol*(sym: var Symbol): Result {.exportc, dynlib.} =
|
proc onSymbol*(sym: var Symbol) {.exportc, dynlib.} =
|
||||||
sym.name = sym.name.strip(chars={'_'})
|
sym.name = sym.name.strip(chars={'_'})
|
||||||
|
|
||||||
cImport cSearchPath("math.h")
|
cImport cSearchPath("math.h")
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,7 @@ cCompile cSearchPath("test.c")
|
||||||
cPlugin:
|
cPlugin:
|
||||||
import strutils
|
import strutils
|
||||||
|
|
||||||
proc onSymbol*(sym: var Symbol): Result {.exportc, dynlib.} =
|
proc onSymbol*(sym: var Symbol) {.exportc, dynlib.} =
|
||||||
sym.name = sym.name.strip(chars={'_'})
|
sym.name = sym.name.strip(chars={'_'})
|
||||||
|
|
||||||
cImport cSearchPath "test.h"
|
cImport cSearchPath "test.h"
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue