attempt to make 0.19 compatible

workaround for nim/macro/typedesc issue

work-around for nim-lang/Nim#7375

fixed more isNil usages

fixed new name of setStackBottom
This commit is contained in:
Fabian Keller 2018-12-30 12:40:10 +01:00 • committed by Ruslan Mustakov
commit 3a935b9308
6 changed files with 94 additions and 100 deletions

View file

@ -1,6 +1,6 @@
# Copyright 2018 Xored Software, Inc.
import macros, tables, typetraits
import macros, tables, typetraits, strutils, sets, sequtils, options
import godotinternal, internal.godotvariants
import godotnim, core.variants
@ -10,8 +10,8 @@ type
typ: NimNode
defaultValue: NimNode
isNoGodot: bool
hint: string
hintStr: string
hint: Option[string]
hintStr: Option[string]
usage: NimNode
isExported: bool
@ -111,12 +111,12 @@ proc removePragma(statement: NimNode, pname: string): bool =
return true
proc removeStrPragma(statement: NimNode,
pname: string): string {.compileTime.} =
pname: string): Option[string] {.compileTime.} =
## Removes the pragma from the node and returns value of the pragma
## Works for routine nodes or nnkPragmaExpr
let node = removePragmaNode(statement, pname)
result = if node.isNil: nil
else: $node
result = if node.isNil: none(string)
else: some($node)
proc isExported(node: NimNode): bool {.compileTime.} =
if node.kind == nnkPragmaExpr:
@ -197,7 +197,7 @@ proc parseType(definition, callSite: NimNode): ObjectDecl =
parseError(option, "valid type specifier expected")
result.isTool = isTool
if result.parentName.isNil: result.parentName = "Object"
if result.parentName == "": result.parentName = "Object"
for statement in body:
case statement.kind:
of nnkVarSection:
@ -424,9 +424,6 @@ template registerGodotField(classNameLit, classNameIdent, propNameLit,
unsafeAddr attr, setFunc, getFunc)
hintStrGodot.deinit()
static:
import strutils, sets, sequtils
proc toGodotStyle(s: string): string {.compileTime.} =
result = newStringOfCap(s.len + 10)
for c in s:
@ -447,7 +444,7 @@ proc genType(obj: ObjectDecl): NimNode {.compileTime.} =
let objTy = newNimNode(nnkObjectTy)
typeDef.add(newNimNode(nnkRefTy).add(objTy))
objTy.add(newEmptyNode())
if obj.parentName.isNil:
if obj.parentName == "":
objTy.add(newEmptyNode())
else:
objTy.add(newNimNode(nnkOfInherit).add(ident(obj.parentName)))
@ -506,23 +503,25 @@ proc genType(obj: ObjectDecl): NimNode {.compileTime.} =
result.add(meth.nimNode)
# Register Godot object
let parentName = if obj.parentName.isNil: newStrLitNode("Object")
let parentName = if obj.parentName == "": newStrLitNode("Object")
else: newStrLitNode(obj.parentName)
let classNameLit = newStrLitNode(obj.name)
let classNameIdent = ident(obj.name)
let isRef: bool = if obj.parentName.isNil: false
let isRef: bool = if obj.parentName == "": false
else: obj.parentName in refClasses
# Wrapping bools with a newLit is required as a temporary workaround for
# https://github.com/nim-lang/Nim/issues/7375
result.add(getAst(
registerGodotClass(classNameIdent, classNameLit, isRef, parentName,
genSym(nskProc, "createFunc"), obj.isTool)))
registerGodotClass(classNameIdent, classNameLit, newLit(isRef), parentName,
genSym(nskProc, "createFunc"), newLit(obj.isTool))))
# Register fields (properties)
for field in obj.fields:
if field.isNoGodot: continue
let hintStr = if field.hintStr.isNil: "NIM"
else: field.hintStr
let hint = if field.hint.isNil: "NIM"
else: field.hint
let hintStr = if field.hintStr.isNone: "NIM"
else: field.hintStr.get
let hint = if field.hint.isNone: "NIM"
else: field.hint.get
let usage =
if field.usage.isNil:
newLit(ord(GodotPropertyUsage.Default) or
@ -546,7 +545,7 @@ proc genType(obj: ObjectDecl): NimNode {.compileTime.} =
methodNameLit, minArgs, maxArgs,
argTypes, methFuncIdent, hasReturnValue) =
{.emit: """/*TYPESECTION*/
N_NOINLINE(void, setStackBottom)(void* thestackbottom);
N_NOINLINE(void, nimGC_setStackBottom)(void* thestackbottom);
""".}
proc methFuncIdent(obj: ptr GodotObject, methodData: pointer,
userData: pointer, numArgs: cint,
@ -554,7 +553,7 @@ N_NOINLINE(void, setStackBottom)(void* thestackbottom);
GodotVariant {.noconv.} =
var stackBottom {.volatile.}: pointer
{.emit: """
setStackBottom((void*)(&`stackBottom`));
nimGC_setStackBottom((void*)(&`stackBottom`));
""".}
let self = cast[classNameIdent](userData)
const isStaticCall = methodNameLit == cstring"_ready" or

View file

@ -1,6 +1,6 @@
# Copyright 2018 Xored Software, Inc.
import tables, typetraits, macros, unicode
import tables, typetraits, macros, unicode, strutils, sets
import gdnativeapi
import core.godotcoretypes, core.godotbase
import core.vector2, core.rect2,
@ -101,8 +101,6 @@ var classRegistryStatic* {.compileTime.}: TableRef[FNV1Hash, ObjectInfo]
static:
classRegistryStatic = newTable[FNV1Hash, ObjectInfo]()
static:
import sets, strutils
var nativeClasses {.compileTime.} = newSeq[string]()
var refClasses* {.compileTime.} = newSeq[string]()
@ -204,7 +202,7 @@ macro baseNativeType(T: typedesc): cstring =
if typeName == "NimGodotObject":
break
t = getType(t[1][1])
if baseT.isNil:
if baseT == "":
result = newNilLit()
else:
let rStr = newNimNode(nnkRStrLit)
@ -345,26 +343,20 @@ proc newRStrLit(s: string): NimNode {.compileTime.} =
result = newNimNode(nnkRStrLit)
result.strVal = s
static:
import strutils
macro toGodotName(T: typedesc): string =
var godotName: string
proc toGodotName(T: typedesc): string =
if T is GodotString or T is string:
godotName = "String"
"String"
elif T is SomeFloat:
godotName = "float"
"float"
elif T is SomeUnsignedInt or T is SomeSignedInt:
godotName = "int"
if godotName.isNil or godotName.len == 0:
"int"
else:
let nameStr = (($T.getType()[1][1].symbol).split(':')[0])
godotName = case nameStr:
of "File", "Directory", "Thread", "Mutex", "Semaphore":
"_" & nameStr
else:
nameStr
result = newLit(godotName)
case nameStr:
of "File", "Directory", "Thread", "Mutex", "Semaphore":
"_" & nameStr
else:
nameStr
macro asCString(s: static[string]): cstring =
result = newNimNode(nnkCallStrLit).add(
@ -717,7 +709,7 @@ proc fromVariant*(s: var string, val: Variant): ConversionResult =
if val.getType() == VariantType.String:
s = val.asString()
elif val.getType() == VariantType.Nil:
s = nil
s = ""
else:
result = ConversionResult.TypeError
@ -839,7 +831,7 @@ proc fromVariant*[T: Table or TableRef or OrderedTable or OrderedTableRef](t: va
{.emit: """/*TYPESECTION*/
N_LIB_EXPORT N_CDECL(void, NimMain)(void);
N_NOINLINE(void, setStackBottom)(void* thestackbottom);
N_NOINLINE(void, nimGC_setStackBottom)(void* thestackbottom);
""".}
var nativeLibHandle: pointer
@ -855,7 +847,7 @@ proc godot_nativescript_init(handle: pointer) {.
var stackBottom {.volatile.}: pointer
{.emit: """
NimMain();
setStackBottom((void*)(&`stackBottom`));
nimGC_setStackBottom((void*)(&`stackBottom`));
""".}
GC_fullCollect()
GC_disable()
@ -893,7 +885,7 @@ proc registerFrameCallback*(cb: proc () {.closure.}) =
proc godot_nativescript_frame() {.cdecl, exportc, dynlib.} =
var stackBottom {.volatile.}: pointer
{.emit: """
setStackBottom((void*)(&`stackBottom`));
nimGC_setStackBottom((void*)(&`stackBottom`));
""".}
for cb in idleCallbacks:
cb()