mirror of
https://github.com/nim-lang/Nim.git
synced 2026-02-17 16:38:33 +00:00
x11 example compiles again; updated python wrapper; compiler supports variables in DLLs
This commit is contained in:
@@ -418,9 +418,20 @@ proc assignLocalVar(p: BProc, s: PSym) =
|
||||
|
||||
include ccgthreadvars
|
||||
|
||||
proc VarInDynamicLib(m: BModule, sym: PSym)
|
||||
proc mangleDynLibProc(sym: PSym): PRope
|
||||
|
||||
proc assignGlobalVar(p: BProc, s: PSym) =
|
||||
if s.loc.k == locNone:
|
||||
fillLoc(s.loc, locGlobalVar, s.typ, mangleName(s), OnHeap)
|
||||
|
||||
if lfDynamicLib in s.loc.flags:
|
||||
var q = findPendingModule(p.module, s)
|
||||
if q != nil and not ContainsOrIncl(q.declaredThings, s.id):
|
||||
VarInDynamicLib(q, s)
|
||||
else:
|
||||
s.loc.r = mangleDynLibProc(s)
|
||||
return
|
||||
useHeader(p.module, s)
|
||||
if lfNoDecl in s.loc.flags: return
|
||||
if sfThread in s.flags:
|
||||
@@ -537,6 +548,21 @@ proc SymInDynamicLib(m: BModule, sym: PSym) =
|
||||
"$1 = linkonce global $2 zeroinitializer$n",
|
||||
[sym.loc.r, getTypeDesc(m, sym.loc.t)])
|
||||
|
||||
proc VarInDynamicLib(m: BModule, sym: PSym) =
|
||||
var lib = sym.annex
|
||||
var extname = sym.loc.r
|
||||
loadDynamicLib(m, lib)
|
||||
incl(sym.loc.flags, lfIndirect)
|
||||
var tmp = mangleDynLibProc(sym)
|
||||
sym.loc.r = tmp # from now on we only need the internal name
|
||||
inc(m.labels, 2)
|
||||
appcg(m, m.s[cfsDynLibInit],
|
||||
"$1 = ($2*) #nimGetProcAddr($3, $4);$n",
|
||||
[tmp, getTypeDesc(m, sym.typ),
|
||||
lib.name, cstringLit(m, m.s[cfsDynLibInit], ropeToStr(extname))])
|
||||
appf(m.s[cfsVars], "$2* $1;$n",
|
||||
[sym.loc.r, getTypeDesc(m, sym.loc.t)])
|
||||
|
||||
proc SymInDynamicLibPartial(m: BModule, sym: PSym) =
|
||||
sym.loc.r = mangleDynLibProc(sym)
|
||||
sym.typ.sym = nil # generate a new name
|
||||
@@ -751,6 +777,7 @@ proc genVarPrototype(m: BModule, sym: PSym) =
|
||||
else:
|
||||
app(m.s[cfsVars], "extern ")
|
||||
app(m.s[cfsVars], getTypeDesc(m, sym.loc.t))
|
||||
if lfDynamicLib in sym.loc.flags: app(m.s[cfsVars], "*")
|
||||
if sfRegister in sym.flags: app(m.s[cfsVars], " register")
|
||||
if sfVolatile in sym.flags: app(m.s[cfsVars], " volatile")
|
||||
appf(m.s[cfsVars], " $1;$n", [sym.loc.r])
|
||||
|
||||
@@ -3589,7 +3589,7 @@ strings automatically:
|
||||
|
||||
Dynlib pragma for import
|
||||
------------------------
|
||||
With the `dynlib`:idx: pragma a procedure can be imported from
|
||||
With the `dynlib`:idx: pragma a procedure or a variable can be imported from
|
||||
a dynamic library (``.dll`` files for Windows, ``lib*.so`` files for UNIX).
|
||||
The non-optional argument has to be the name of the dynamic library:
|
||||
|
||||
|
||||
@@ -9,4 +9,3 @@ Py_Initialize()
|
||||
discard PyRun_SimpleString("from time import time,ctime\L" &
|
||||
"print 'Today is',ctime(time())\L")
|
||||
Py_Finalize()
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ const
|
||||
WINDOW_HEIGHT = 300
|
||||
|
||||
var
|
||||
width, height: cint
|
||||
width, height: cuint
|
||||
display: PDisplay
|
||||
screen: cint
|
||||
depth: int
|
||||
@@ -29,10 +29,10 @@ proc create_window =
|
||||
XBlackPixel(display, screen),
|
||||
XWhitePixel(display, screen))
|
||||
size_hints.flags = PSize or PMinSize or PMaxSize
|
||||
size_hints.min_width = width
|
||||
size_hints.max_width = width
|
||||
size_hints.min_height = height
|
||||
size_hints.max_height = height
|
||||
size_hints.min_width = width.cint
|
||||
size_hints.max_width = width.cint
|
||||
size_hints.min_height = height.cint
|
||||
size_hints.max_height = height.cint
|
||||
discard XSetStandardProperties(display, win, "Simple Window", "window",
|
||||
0, nil, 0, addr(size_hints))
|
||||
discard XSelectInput(display, win, ButtonPressMask or KeyPressMask or
|
||||
|
||||
@@ -60,12 +60,14 @@ import
|
||||
|
||||
|
||||
when defined(windows):
|
||||
const dllname = "python(26|25|24|23|22|21|20|16|15).dll"
|
||||
const dllname = "python(27|26|25|24|23|22|21|20|16|15).dll"
|
||||
elif defined(macosx):
|
||||
const dllname = "libpython(2.6|2.5|2.4|2.3|2.2|2.1|2.0|1.6|1.5).dylib"
|
||||
const dllname = "libpython(2.7|2.6|2.5|2.4|2.3|2.2|2.1|2.0|1.6|1.5).dylib"
|
||||
else:
|
||||
const dllver = ".1"
|
||||
const dllname = "libpython(2.6|2.5|2.4|2.3|2.2|2.1|2.0|1.6|1.5).so" & dllver
|
||||
const dllname = "libpython(2.7|2.6|2.5|2.4|2.3|2.2|2.1|2.0|1.6|1.5).so" &
|
||||
dllver
|
||||
|
||||
|
||||
const
|
||||
PYT_METHOD_BUFFER_INCREASE* = 10
|
||||
@@ -1545,26 +1547,28 @@ proc init(lib: TLibHandle) =
|
||||
PyEnum_Type = cast[PPyTypeObject](symAddr(lib, "PyEnum_Type"))
|
||||
|
||||
# Unfortunately we have to duplicate the loading mechanism here, because Nimrod
|
||||
# does not support variables from dynamic libraries. Well designed API's don't
|
||||
# require this anyway. Python is an exception.
|
||||
# used to not support variables from dynamic libraries. Well designed API's
|
||||
# don't require this anyway. Python is an exception.
|
||||
|
||||
var
|
||||
lib: TLibHandle
|
||||
|
||||
when defined(windows):
|
||||
when defined(windows):
|
||||
const
|
||||
LibNames = ["python26.dll", "python25.dll",
|
||||
"python24.dll", "python23.dll", "python22.dll", "python21.dll",
|
||||
LibNames = ["python27.dll", "python26.dll", "python25.dll",
|
||||
"python24.dll", "python23.dll", "python22.dll", "python21.dll",
|
||||
"python20.dll", "python16.dll", "python15.dll"]
|
||||
elif defined(macosx):
|
||||
const
|
||||
LibNames = ["libpython2.6.dylib", "libpython2.5.dylib",
|
||||
"libpython2.4.dylib", "libpython2.3.dylib", "libpython2.2.dylib",
|
||||
"libpython2.1.dylib", "libpython2.0.dylib",
|
||||
LibNames = ["libpython2.7.dylib", "libpython2.6.dylib",
|
||||
"libpython2.5.dylib", "libpython2.4.dylib", "libpython2.3.dylib",
|
||||
"libpython2.2.dylib", "libpython2.1.dylib", "libpython2.0.dylib",
|
||||
"libpython1.6.dylib", "libpython1.5.dylib"]
|
||||
else:
|
||||
else:
|
||||
const
|
||||
LibNames = ["libpython2.6.so" & dllver,
|
||||
LibNames = [
|
||||
"libpython2.7.so" & dllver,
|
||||
"libpython2.6.so" & dllver,
|
||||
"libpython2.5.so" & dllver,
|
||||
"libpython2.4.so" & dllver,
|
||||
"libpython2.3.so" & dllver,
|
||||
|
||||
1
todo.txt
1
todo.txt
@@ -1,7 +1,6 @@
|
||||
version 0.9.0
|
||||
=============
|
||||
|
||||
- implement ``dynlib`` for variables
|
||||
- implicit deref for parameter matching
|
||||
- deprecate ``var x, y = 0`` as it's confusing for tuple consistency
|
||||
- test sequence of closures; especially that the GC does not leak for those!
|
||||
|
||||
@@ -112,6 +112,7 @@ Compiler Additions
|
||||
via the ``doc2`` command. This new generator uses all of the semantic passes
|
||||
of the compiler and can thus generate documentation for symbols hiding in
|
||||
macros.
|
||||
- The compiler now supports the ``dynlib`` pragma for variables.
|
||||
|
||||
|
||||
Language Additions
|
||||
|
||||
Reference in New Issue
Block a user