From 4b275c2861c0bbe43bd5c8e1b990ab523dec78fb Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Wed, 25 Mar 2009 13:05:34 -0500 Subject: [PATCH 01/16] make add-library normalize-path, use libfactor-ffi-tests for testing ffi --- basis/compiler/tests/alien.factor | 23 +++++++++++++++++++---- core/alien/alien.factor | 5 +++-- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/basis/compiler/tests/alien.factor b/basis/compiler/tests/alien.factor index f3c2deb2d8..2aed251676 100644 --- a/basis/compiler/tests/alien.factor +++ b/basis/compiler/tests/alien.factor @@ -6,6 +6,21 @@ continuations effects namespaces.private io io.streams.string memory system threads tools.test math accessors combinators specialized-arrays.float ; +<< +: libfactor-ffi-tests-path ( -- string ) + { + { [ os winnt? ] [ "resource:libfactor-ffi-test.dll" ] } + { [ os macosx? ] [ "resource:libfactor-ffi-test.dylib" ] } + { [ os unix? ] [ "resource:libfactor-ffi-test.so" ] } + } cond ; + +"f-cdecl" libfactor-ffi-tests-path "cdecl" add-library + +"f-stdcall" libfactor-ffi-tests-path "stdcall" add-library +>> + +LIBRARY: f-cdecl + FUNCTION: void ffi_test_0 ; [ ] [ ffi_test_0 ] unit-test @@ -107,9 +122,9 @@ unit-test "int" { "int" "int" "int" "int" } "stdcall" alien-indirect gc ; -<< "f-stdcall" f "stdcall" add-library >> +LIBRARY: f-stdcall -[ f ] [ "f-stdcall" load-library ] unit-test +[ f ] [ "f-stdcall" load-library f = ] unit-test [ "stdcall" ] [ "f-stdcall" library abi>> ] unit-test : ffi_test_18 ( w x y z -- int ) @@ -149,7 +164,7 @@ FUNCTION: void ffi_test_20 double x1, double x2, double x3, : ffi_test_31 ( a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a -- result y ) "int" - f "ffi_test_31" + "f-stdcall" "ffi_test_31" { "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" } alien-invoke gc 3 ; @@ -157,7 +172,7 @@ FUNCTION: void ffi_test_20 double x1, double x2, double x3, : ffi_test_31_point_5 ( a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a -- result ) "float" - f "ffi_test_31_point_5" + "f-stdcall" "ffi_test_31_point_5" { "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" } alien-invoke ; diff --git a/core/alien/alien.factor b/core/alien/alien.factor index 83665778f1..8e48ac770c 100644 --- a/core/alien/alien.factor +++ b/core/alien/alien.factor @@ -1,7 +1,7 @@ ! Copyright (C) 2004, 2009 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. USING: accessors assocs kernel math namespaces sequences system -kernel.private byte-arrays arrays init ; +kernel.private byte-arrays arrays init io.backend ; IN: alien ! Some predicate classes used by the compiler for optimization @@ -64,6 +64,7 @@ TUPLE: library path abi dll ; library dup [ dll>> ] when ; : add-library ( name path abi -- ) + [ dup [ normalize-path ] when ] dip swap libraries get set-at ; ERROR: alien-callback-error ; @@ -99,4 +100,4 @@ PRIVATE> : initialize-alien ( symbol quot -- ) swap dup get-global dup recompute-value? [ drop [ call dup 31337 expiry-check boa ] dip set-global ] - [ 2nip object>> ] if ; inline \ No newline at end of file + [ 2nip object>> ] if ; inline From 97283b9857da3aff4e3e827f88ef2b1f6a6c8836 Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Wed, 25 Mar 2009 13:06:20 -0500 Subject: [PATCH 02/16] compile an ffi test library, reduce factor runtime by 6kb! --- Makefile | 43 ++++++++++++++++++++++++------------------- vm/Config.macosx | 2 ++ vm/Config.unix | 1 + vm/Config.windows | 1 + 4 files changed, 28 insertions(+), 19 deletions(-) diff --git a/Makefile b/Makefile index 234b8a2c88..0c426ca5bc 100644 --- a/Makefile +++ b/Makefile @@ -4,6 +4,7 @@ LD = ld EXECUTABLE = factor CONSOLE_EXECUTABLE = factor-console +TEST_LIBRARY = factor-ffi-test VERSION = 0.92 IMAGE = factor.image @@ -35,7 +36,6 @@ DLL_OBJS = $(PLAF_DLL_OBJS) \ vm/debug.o \ vm/errors.o \ vm/factor.o \ - vm/ffi_test.o \ vm/image.o \ vm/io.o \ vm/math.o \ @@ -48,6 +48,8 @@ DLL_OBJS = $(PLAF_DLL_OBJS) \ EXE_OBJS = $(PLAF_EXE_OBJS) +TEST_OBJS = vm/ffi_test.o + default: $(MAKE) `./build-support/factor.sh make-target` @@ -81,60 +83,60 @@ help: @echo "X11=1 force link with X11 libraries instead of Cocoa (only on Mac OS X)" openbsd-x86-32: - $(MAKE) $(EXECUTABLE) CONFIG=vm/Config.openbsd.x86.32 + $(MAKE) $(EXECUTABLE) $(TEST_LIBRARY) CONFIG=vm/Config.openbsd.x86.32 openbsd-x86-64: - $(MAKE) $(EXECUTABLE) CONFIG=vm/Config.openbsd.x86.64 + $(MAKE) $(EXECUTABLE) $(TEST_LIBRARY) CONFIG=vm/Config.openbsd.x86.64 freebsd-x86-32: - $(MAKE) $(EXECUTABLE) CONFIG=vm/Config.freebsd.x86.32 + $(MAKE) $(EXECUTABLE) $(TEST_LIBRARY) CONFIG=vm/Config.freebsd.x86.32 freebsd-x86-64: - $(MAKE) $(EXECUTABLE) CONFIG=vm/Config.freebsd.x86.64 + $(MAKE) $(EXECUTABLE) $(TEST_LIBRARY) CONFIG=vm/Config.freebsd.x86.64 netbsd-x86-32: - $(MAKE) $(EXECUTABLE) CONFIG=vm/Config.netbsd.x86.32 + $(MAKE) $(EXECUTABLE) $(TEST_LIBRARY) CONFIG=vm/Config.netbsd.x86.32 netbsd-x86-64: - $(MAKE) $(EXECUTABLE) CONFIG=vm/Config.netbsd.x86.64 + $(MAKE) $(EXECUTABLE) $(TEST_LIBRARY) CONFIG=vm/Config.netbsd.x86.64 macosx-ppc: - $(MAKE) $(EXECUTABLE) macosx.app CONFIG=vm/Config.macosx.ppc + $(MAKE) $(EXECUTABLE) $(TEST_LIBRARY) macosx.app CONFIG=vm/Config.macosx.ppc macosx-x86-32: - $(MAKE) $(EXECUTABLE) macosx.app CONFIG=vm/Config.macosx.x86.32 + $(MAKE) $(EXECUTABLE) $(TEST_LIBRARY) macosx.app CONFIG=vm/Config.macosx.x86.32 macosx-x86-64: - $(MAKE) $(EXECUTABLE) macosx.app CONFIG=vm/Config.macosx.x86.64 + $(MAKE) $(EXECUTABLE) $(TEST_LIBRARY) macosx.app CONFIG=vm/Config.macosx.x86.64 linux-x86-32: - $(MAKE) $(EXECUTABLE) CONFIG=vm/Config.linux.x86.32 + $(MAKE) $(EXECUTABLE) $(TEST_LIBRARY) CONFIG=vm/Config.linux.x86.32 linux-x86-64: - $(MAKE) $(EXECUTABLE) CONFIG=vm/Config.linux.x86.64 + $(MAKE) $(EXECUTABLE) $(TEST_LIBRARY) CONFIG=vm/Config.linux.x86.64 linux-ppc: - $(MAKE) $(EXECUTABLE) CONFIG=vm/Config.linux.ppc + $(MAKE) $(EXECUTABLE) $(TEST_LIBRARY) CONFIG=vm/Config.linux.ppc linux-arm: - $(MAKE) $(EXECUTABLE) CONFIG=vm/Config.linux.arm + $(MAKE) $(EXECUTABLE) $(TEST_LIBRARY) CONFIG=vm/Config.linux.arm solaris-x86-32: - $(MAKE) $(EXECUTABLE) CONFIG=vm/Config.solaris.x86.32 + $(MAKE) $(EXECUTABLE) $(TEST_LIBRARY) CONFIG=vm/Config.solaris.x86.32 solaris-x86-64: - $(MAKE) $(EXECUTABLE) CONFIG=vm/Config.solaris.x86.64 + $(MAKE) $(EXECUTABLE) $(TEST_LIBRARY) CONFIG=vm/Config.solaris.x86.64 winnt-x86-32: - $(MAKE) $(EXECUTABLE) CONFIG=vm/Config.windows.nt.x86.32 + $(MAKE) $(EXECUTABLE) $(TEST_LIBRARY) CONFIG=vm/Config.windows.nt.x86.32 $(MAKE) $(CONSOLE_EXECUTABLE) CONFIG=vm/Config.windows.nt.x86.32 winnt-x86-64: - $(MAKE) $(EXECUTABLE) CONFIG=vm/Config.windows.nt.x86.64 + $(MAKE) $(EXECUTABLE) $(TEST_LIBRARY) CONFIG=vm/Config.windows.nt.x86.64 $(MAKE) $(CONSOLE_EXECUTABLE) CONFIG=vm/Config.windows.nt.x86.64 wince-arm: - $(MAKE) $(EXECUTABLE) CONFIG=vm/Config.windows.ce.arm + $(MAKE) $(EXECUTABLE) $(TEST_LIBRARY) CONFIG=vm/Config.windows.ce.arm macosx.app: factor mkdir -p $(BUNDLE)/Contents/MacOS @@ -157,6 +159,9 @@ factor-console: $(DLL_OBJS) $(EXE_OBJS) $(CC) $(LIBS) $(LIBPATH) -L. $(LINK_WITH_ENGINE) \ $(CFLAGS) $(CFLAGS_CONSOLE) -o factor$(EXE_SUFFIX)$(CONSOLE_EXTENSION) $(EXE_OBJS) +factor-ffi-test: $(TEST_OBJS) + $(CC) $(LIBPATH) $(CFLAGS) $(SHARED_FLAG) -o libfactor-ffi-test$(DLL_EXTENSION) $(TEST_OBJS) + clean: rm -f vm/*.o rm -f factor*.dll libfactor.{a,so,dylib} diff --git a/vm/Config.macosx b/vm/Config.macosx index 42334a0524..6655d548b7 100644 --- a/vm/Config.macosx +++ b/vm/Config.macosx @@ -5,6 +5,8 @@ PLAF_DLL_OBJS += vm/os-macosx.o vm/mach_signal.o DLL_EXTENSION = .dylib +SHARED_FLAG = -dynamiclib + ifdef X11 LIBS = -lm -framework Cocoa -L/opt/local/lib $(X11_UI_LIBS) -Wl,-dylib_file,/System/Library/Frameworks/OpenGL.framework/Versions/A/Libraries/libGL.dylib:/System/Library/Frameworks/OpenGL.framework/Versions/A/Libraries/libGL.dylib else diff --git a/vm/Config.unix b/vm/Config.unix index e8cb877249..8f2f140247 100644 --- a/vm/Config.unix +++ b/vm/Config.unix @@ -6,6 +6,7 @@ EXE_SUFFIX = DLL_PREFIX = lib DLL_EXTENSION = .a # DLL_EXTENSION = .so +SHARED_FLAG = -shared PLAF_DLL_OBJS = vm/os-unix.o PLAF_EXE_OBJS += vm/main-unix.o diff --git a/vm/Config.windows b/vm/Config.windows index 45d2f0cb98..75452a9bb4 100644 --- a/vm/Config.windows +++ b/vm/Config.windows @@ -1,6 +1,7 @@ CFLAGS += -DWINDOWS -mno-cygwin LIBS = -lm PLAF_DLL_OBJS += vm/os-windows.o +SHARED_FLAG = -shared EXE_EXTENSION=.exe CONSOLE_EXTENSION=.com DLL_EXTENSION=.dll From f4948ec9701cdda02a28e5ad6620db96666026ba Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Wed, 25 Mar 2009 22:52:52 -0500 Subject: [PATCH 03/16] Merge tools.test.ui into ui.gadgets.debug --- basis/tools/test/ui/authors.txt | 1 - basis/tools/test/ui/ui.factor | 16 ---------------- basis/ui/gadgets/debug/debug.factor | 16 +++++++++++++++- basis/ui/gadgets/editors/editors-tests.factor | 2 +- basis/ui/gadgets/panes/panes-tests.factor | 2 +- .../ui/gadgets/scrollers/scrollers-tests.factor | 2 +- basis/ui/tools/browser/browser-tests.factor | 2 +- basis/ui/tools/listener/listener-tests.factor | 2 +- 8 files changed, 20 insertions(+), 23 deletions(-) delete mode 100755 basis/tools/test/ui/authors.txt delete mode 100644 basis/tools/test/ui/ui.factor diff --git a/basis/tools/test/ui/authors.txt b/basis/tools/test/ui/authors.txt deleted file mode 100755 index 1901f27a24..0000000000 --- a/basis/tools/test/ui/authors.txt +++ /dev/null @@ -1 +0,0 @@ -Slava Pestov diff --git a/basis/tools/test/ui/ui.factor b/basis/tools/test/ui/ui.factor deleted file mode 100644 index c37e7799cb..0000000000 --- a/basis/tools/test/ui/ui.factor +++ /dev/null @@ -1,16 +0,0 @@ -USING: dlists ui.gadgets ui.gadgets.private -kernel ui namespaces io.streams.string io ; -IN: tools.test.ui - -! We can't print to output-stream here because that might be a pane -! stream, and our graft-queue rebinding here would be captured -! by code adding children to the pane... -: with-grafted-gadget ( gadget quot -- ) - [ - \ graft-queue [ - over - graft notify-queued - dip - ungraft notify-queued - ] with-variable - ] with-string-writer print ; diff --git a/basis/ui/gadgets/debug/debug.factor b/basis/ui/gadgets/debug/debug.factor index 076c772f97..f8d496c1fc 100644 --- a/basis/ui/gadgets/debug/debug.factor +++ b/basis/ui/gadgets/debug/debug.factor @@ -2,9 +2,23 @@ ! See http://factorcode.org/license.txt for BSD license. USING: accessors arrays colors.constants combinators kernel opengl sequences ui ui.baseline-alignment ui.gadgets -ui.gadgets.buttons ui.gadgets.labels ui.pens ui.render ui.text ; +ui.gadgets.buttons ui.gadgets.labels ui.pens ui.render ui.text +ui.gadgets.private dlists namespaces io.streams.string io ; IN: ui.gadgets.debug +! We can't print to output-stream here because that might be a pane +! stream, and our graft-queue rebinding here would be captured +! by code adding children to the pane... +: with-grafted-gadget ( gadget quot -- ) + [ + \ graft-queue set + \ layout-queue set + over + graft notify-queued + dip + ungraft notify-queued + ] with-string-writer print ; inline + TUPLE: baseline-gadget < gadget baseline cap-height ; M: baseline-gadget baseline baseline>> ; diff --git a/basis/ui/gadgets/editors/editors-tests.factor b/basis/ui/gadgets/editors/editors-tests.factor index f8dc5b91c9..bd610ba53b 100644 --- a/basis/ui/gadgets/editors/editors-tests.factor +++ b/basis/ui/gadgets/editors/editors-tests.factor @@ -1,6 +1,6 @@ USING: accessors ui.gadgets.editors tools.test kernel io io.streams.plain definitions namespaces ui.gadgets -ui.gadgets.grids prettyprint documents ui.gestures tools.test.ui +ui.gadgets.grids prettyprint documents ui.gestures ui.gadgets.debug models documents.elements ui.gadgets.scrollers ui.gadgets.line-support sequences ; IN: ui.gadgets.editors.tests diff --git a/basis/ui/gadgets/panes/panes-tests.factor b/basis/ui/gadgets/panes/panes-tests.factor index 0c47af0214..0529437a76 100644 --- a/basis/ui/gadgets/panes/panes-tests.factor +++ b/basis/ui/gadgets/panes/panes-tests.factor @@ -1,7 +1,7 @@ USING: alien ui.gadgets.panes ui.gadgets namespaces kernel sequences io io.styles io.streams.string tools.test prettyprint definitions help help.syntax help.markup -help.stylesheet splitting tools.test.ui models math summary +help.stylesheet splitting ui.gadgets.debug models math summary inspector accessors help.topics see ; IN: ui.gadgets.panes.tests diff --git a/basis/ui/gadgets/scrollers/scrollers-tests.factor b/basis/ui/gadgets/scrollers/scrollers-tests.factor index 4e61c9b1ed..d4cdc95daf 100644 --- a/basis/ui/gadgets/scrollers/scrollers-tests.factor +++ b/basis/ui/gadgets/scrollers/scrollers-tests.factor @@ -1,7 +1,7 @@ USING: ui.gadgets ui.gadgets.scrollers namespaces tools.test kernel models models.product models.range ui.gadgets.viewports ui.gadgets.labels ui.gadgets.grids ui.gadgets.sliders math -math.vectors arrays sequences tools.test.ui math.rectangles +math.vectors arrays sequences ui.gadgets.debug math.rectangles accessors ui.gadgets.buttons ui.gadgets.packs ui.gadgets.scrollers.private ; IN: ui.gadgets.scrollers.tests diff --git a/basis/ui/tools/browser/browser-tests.factor b/basis/ui/tools/browser/browser-tests.factor index 7477edbe6a..3757f392c4 100644 --- a/basis/ui/tools/browser/browser-tests.factor +++ b/basis/ui/tools/browser/browser-tests.factor @@ -1,5 +1,5 @@ IN: ui.tools.browser.tests -USING: tools.test tools.test.ui ui.tools.browser math ; +USING: tools.test ui.gadgets.debug ui.tools.browser math ; \ must-infer [ ] [ \ + [ ] with-grafted-gadget ] unit-test diff --git a/basis/ui/tools/listener/listener-tests.factor b/basis/ui/tools/listener/listener-tests.factor index 63df55b71a..986e1270eb 100644 --- a/basis/ui/tools/listener/listener-tests.factor +++ b/basis/ui/tools/listener/listener-tests.factor @@ -1,7 +1,7 @@ USING: continuations documents ui.tools.listener hashtables kernel namespaces parser sequences tools.test ui.commands ui.gadgets ui.gadgets.editors -ui.gadgets.panes vocabs words tools.test.ui slots.private +ui.gadgets.panes vocabs words ui.gadgets.debug slots.private threads arrays generic threads accessors listener math calendar concurrency.promises io ui.tools.common ; IN: ui.tools.listener.tests From ff07c8f35203a3bc0c6677c18c943996f426b0f1 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Wed, 25 Mar 2009 22:53:03 -0500 Subject: [PATCH 04/16] Fix compiler.tree.debugger tests --- basis/compiler/tree/debugger/debugger-tests.factor | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/basis/compiler/tree/debugger/debugger-tests.factor b/basis/compiler/tree/debugger/debugger-tests.factor index 4d1c5c824d..9b4a6da12a 100644 --- a/basis/compiler/tree/debugger/debugger-tests.factor +++ b/basis/compiler/tree/debugger/debugger-tests.factor @@ -1,5 +1,5 @@ IN: compiler.tree.debugger.tests -USING: compiler.tree.debugger tools.test sorting sequences io ; +USING: compiler.tree.debugger tools.test sorting sequences io math.order ; \ optimized. must-infer \ optimizer-report. must-infer From 6984c143e9a8ddea73fc67aa4afd4ac561bba84a Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Wed, 25 Mar 2009 22:54:31 -0500 Subject: [PATCH 05/16] Fix hang when running ui tests --- basis/ui/gadgets/gadgets.factor | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/basis/ui/gadgets/gadgets.factor b/basis/ui/gadgets/gadgets.factor index a4bedf1ef7..adcfdfb00d 100644 --- a/basis/ui/gadgets/gadgets.factor +++ b/basis/ui/gadgets/gadgets.factor @@ -135,9 +135,7 @@ SYMBOL: ui-notify-flag : forget-pref-dim ( gadget -- ) f >>pref-dim drop ; -: ui-state ( symbol -- value ) get [ "UI not running" throw ] unless* ; - -: layout-queue ( -- queue ) \ layout-queue ui-state ; +: layout-queue ( -- queue ) \ layout-queue get ; : layout-later ( gadget -- ) #! When unit testing gadgets without the UI running, the @@ -216,7 +214,8 @@ M: gadget ungraft* drop ; > graft-queue delete-node ] From a2147ccb27269cfe7d81b85f12da0388df827691 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Wed, 25 Mar 2009 22:54:53 -0500 Subject: [PATCH 06/16] Slides from talk at PyCon VM summit, Chicago 2009 --- extra/chicago-talk/authors.txt | 1 + extra/chicago-talk/chicago-talk.factor | 65 ++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 extra/chicago-talk/authors.txt create mode 100644 extra/chicago-talk/chicago-talk.factor diff --git a/extra/chicago-talk/authors.txt b/extra/chicago-talk/authors.txt new file mode 100644 index 0000000000..d4f5d6b3ae --- /dev/null +++ b/extra/chicago-talk/authors.txt @@ -0,0 +1 @@ +Slava Pestov \ No newline at end of file diff --git a/extra/chicago-talk/chicago-talk.factor b/extra/chicago-talk/chicago-talk.factor new file mode 100644 index 0000000000..9d42ff13f2 --- /dev/null +++ b/extra/chicago-talk/chicago-talk.factor @@ -0,0 +1,65 @@ +! Copyright (C) 2009 Slava Pestov. +! See http://factorcode.org/license.txt for BSD license. +USING: slides help.markup ; +IN: chicago-talk + +CONSTANT: chicago-slides +{ +{ $slide "factor" + +{ $url "http://factorcode.org" } + +} +{ $slide "goals" + +"high level language" +"expressive and extensible" +"reasonable performance" +"interactive development with arbitrary redefinition" +"standalone app deployment (strip out compiler and REPL)" + +} +{ $slide "challenges" + +"higher-order functions" +"dynamic typing" +"memory allocation" +"float boxing/unboxing" +"integer overflow checks" +"user-defined abstractions" + +} +{ $slide "implementation" + +"VM: 12 kloc of C, library: >100 kloc of Factor" +"generational copying garbage collection, card marking write barrier" +"full continuations, tail calls" +"simple non-optimizing “bootstrap” compiler" +"optimizing compiler" + +} +{ $slide "optimizing compiler" + +"about 12,000 lines of Factor code" +"targets x86-32, x86-64, PowerPC" +"factor code ⇒ high-level SSA ⇒ low-level SSA ⇒ machine code" + +} +{ $slide "high-level optimizer" + +"macro expansion, defunctionalization" +"type and interval inference, sparse conditional constant propagation, method inlining" +"escape analysis and tuple unboxing" + +} +{ $slide "low-level optimizer" + +"alias analysis, value numbering, write barrier elimination" +"linear scan register allocation" + +} +} + +: chicago-talk ( -- ) chicago-slides slides-window ; + +MAIN: chicago-talk \ No newline at end of file From 3c1b716bf99a5c5432d8b3e0b04ba3d0af192be2 Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Wed, 25 Mar 2009 23:00:02 -0500 Subject: [PATCH 07/16] move a lot of words to alien.libraries --- basis/alien/libraries/authors.txt | 1 + basis/alien/libraries/libraries-docs.factor | 60 +++++++++++++++++++++ basis/alien/libraries/libraries.factor | 22 ++++++++ 3 files changed, 83 insertions(+) create mode 100644 basis/alien/libraries/authors.txt create mode 100644 basis/alien/libraries/libraries-docs.factor create mode 100644 basis/alien/libraries/libraries.factor diff --git a/basis/alien/libraries/authors.txt b/basis/alien/libraries/authors.txt new file mode 100644 index 0000000000..1901f27a24 --- /dev/null +++ b/basis/alien/libraries/authors.txt @@ -0,0 +1 @@ +Slava Pestov diff --git a/basis/alien/libraries/libraries-docs.factor b/basis/alien/libraries/libraries-docs.factor new file mode 100644 index 0000000000..3b9c56c8fb --- /dev/null +++ b/basis/alien/libraries/libraries-docs.factor @@ -0,0 +1,60 @@ +! Copyright (C) 2009 Slava Pestov. +! See http://factorcode.org/license.txt for BSD license. +USING: accessors alien alien.syntax assocs help.markup +help.syntax io.backend kernel namespaces ; +IN: alien.libraries + +HELP: +{ $values + { "path" "a pathname string" } { "abi" "the ABI used by the library, either " { $snippet "cdecl" } " or " { $snippet "stdcall" } } + { "library" library } } +{ $description "Opens a C library using the path and ABI parameters and outputs a library tuple." } +{ $notes "User code should use " { $link add-library } " so that the opened library is added to a global hashtable, " { $link libraries } "." } ; + +HELP: libraries +{ $description "A global hashtable that keeps a list of open libraries. Use the " { $link add-library } " word to construct a library and add it with a single call." } ; + +HELP: library +{ $values { "name" "a string" } { "library" "a hashtable" } } +{ $description "Looks up a library by its logical name. The library object is a hashtable with the following keys:" + { $list + { { $snippet "name" } " - the full path of the C library binary" } + { { $snippet "abi" } " - the ABI used by the library, either " { $snippet "cdecl" } " or " { $snippet "stdcall" } } + { { $snippet "dll" } " - an instance of the " { $link dll } " class; only set if the library is loaded" } + } +} ; + +HELP: dlopen ( path -- dll ) +{ $values { "path" "a pathname string" } { "dll" "a DLL handle" } } +{ $description "Opens a native library and outputs a handle which may be passed to " { $link dlsym } " or " { $link dlclose } "." } +{ $errors "Throws an error if the library could not be found, or if loading fails for some other reason." } +{ $notes "This is the low-level facility used to implement " { $link load-library } ". Use the latter instead." } ; + +HELP: dlsym ( name dll -- alien ) +{ $values { "name" "a C symbol name" } { "dll" "a DLL handle" } { "alien" "an alien pointer" } } +{ $description "Looks up a symbol in a native library. If " { $snippet "dll" } " is " { $link f } " looks for the symbol in the runtime executable." } +{ $errors "Throws an error if the symbol could not be found." } ; + +HELP: dlclose ( dll -- ) +{ $values { "dll" "a DLL handle" } } +{ $description "Closes a DLL handle created by " { $link dlopen } ". This word might not be implemented on all platforms." } ; + +HELP: load-library +{ $values { "name" "a string" } { "dll" "a DLL handle" } } +{ $description "Loads a library by logical name and outputs a handle which may be passed to " { $link dlsym } " or " { $link dlclose } ". If the library is already loaded, returns the existing handle." } ; + +HELP: add-library +{ $values { "name" "a string" } { "path" "a string" } { "abi" "one of " { $snippet "\"cdecl\"" } " or " { $snippet "\"stdcall\"" } } } +{ $description "Defines a new logical library named " { $snippet "name" } " located in the file system at " { $snippet "path" } "and the specified ABI." } +{ $notes "Because the entire source file is parsed before top-level forms are executed, " { $link add-library } " cannot be used in the same file as " { $link POSTPONE: FUNCTION: } " definitions from that library. The " { $link add-library } " call will happen too late, after compilation, and the alien calls will not work." +$nl +"Instead, " { $link add-library } " calls must either be placed in different source files from those that use that library, or alternatively, " { $link "syntax-immediate" } " can be used to load the library before compilation." } +{ $examples "Here is a typical usage of " { $link add-library } ":" +{ $code + "<< \"freetype\" {" + " { [ os macosx? ] [ \"libfreetype.6.dylib\" \"cdecl\" add-library ] }" + " { [ os windows? ] [ \"freetype6.dll\" \"cdecl\" add-library ] }" + " [ drop ]" + "} cond >>" +} +"Note the parse time evaluation with " { $link POSTPONE: << } "." } ; diff --git a/basis/alien/libraries/libraries.factor b/basis/alien/libraries/libraries.factor new file mode 100644 index 0000000000..adb9eeb1bb --- /dev/null +++ b/basis/alien/libraries/libraries.factor @@ -0,0 +1,22 @@ +! Copyright (C) 2009 Slava Pestov. +! See http://factorcode.org/license.txt for BSD license. +USING: accessors alien assocs io.backend kernel namespaces ; +IN: alien.libraries + +SYMBOL: libraries + +libraries [ H{ } clone ] initialize + +TUPLE: library path abi dll ; + +: library ( name -- library ) libraries get at ; + +: ( path abi -- library ) + over dup [ dlopen ] when \ library boa ; + +: load-library ( name -- dll ) + library dup [ dll>> ] when ; + +: add-library ( name path abi -- ) + [ dup [ normalize-path ] when ] dip + swap libraries get set-at ; From 8b7fcce399a5fff507f2868fbe094e0b2ed681fa Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Wed, 25 Mar 2009 23:00:19 -0500 Subject: [PATCH 08/16] update code for usages of add-library --- basis/alien/syntax/syntax.factor | 2 +- basis/cairo/ffi/ffi.factor | 3 +- basis/compiler/codegen/codegen.factor | 2 +- basis/compression/zlib/ffi/ffi.factor | 2 +- basis/core-text/utilities/utilities.factor | 3 +- basis/db/postgresql/ffi/ffi.factor | 2 +- basis/db/sqlite/ffi/ffi.factor | 2 +- basis/debugger/debugger-docs.factor | 3 +- basis/glib/glib.factor | 3 +- basis/opengl/gl/macosx/macosx.factor | 2 +- basis/openssl/libcrypto/libcrypto.factor | 3 +- basis/openssl/libssl/libssl.factor | 3 +- basis/pango/pango.factor | 4 +- basis/stack-checker/alien/alien.factor | 2 +- .../known-words/known-words.factor | 1 + basis/tools/disassembler/udis/udis.factor | 2 +- basis/ui/tools/profiler/profiler.factor | 2 +- basis/windows/ce/ce.factor | 2 +- basis/windows/dinput/dinput.factor | 6 -- basis/windows/nt/nt.factor | 3 +- core/alien/alien-docs.factor | 82 ++++--------------- core/alien/alien-tests.factor | 6 +- core/alien/alien.factor | 20 +---- core/bootstrap/primitives.factor | 7 +- extra/curses/ffi/ffi.factor | 3 +- extra/freetype/freetype.factor | 3 +- 26 files changed, 53 insertions(+), 120 deletions(-) diff --git a/basis/alien/syntax/syntax.factor b/basis/alien/syntax/syntax.factor index 5406970364..6a1bf7f635 100644 --- a/basis/alien/syntax/syntax.factor +++ b/basis/alien/syntax/syntax.factor @@ -4,7 +4,7 @@ USING: accessors arrays alien alien.c-types alien.structs alien.arrays alien.strings kernel math namespaces parser sequences words quotations math.parser splitting grouping effects assocs combinators lexer strings.parser alien.parser -fry vocabs.parser words.constant ; +fry vocabs.parser words.constant alien.libraries ; IN: alien.syntax SYNTAX: DLL" lexer get skip-blank parse-string dlopen parsed ; diff --git a/basis/cairo/ffi/ffi.factor b/basis/cairo/ffi/ffi.factor index e7c0a17660..2930843ad7 100644 --- a/basis/cairo/ffi/ffi.factor +++ b/basis/cairo/ffi/ffi.factor @@ -5,7 +5,8 @@ ! License: http://factorcode.org/license.txt USING: system combinators alien alien.syntax alien.c-types -alien.destructors kernel accessors sequences arrays ui.gadgets ; +alien.destructors kernel accessors sequences arrays ui.gadgets +alien.libraries ; IN: cairo.ffi << { diff --git a/basis/compiler/codegen/codegen.factor b/basis/compiler/codegen/codegen.factor index 4ddd0c0300..7df80c6b6e 100755 --- a/basis/compiler/codegen/codegen.factor +++ b/basis/compiler/codegen/codegen.factor @@ -3,7 +3,7 @@ USING: namespaces make math math.order math.parser sequences accessors kernel kernel.private layouts assocs words summary arrays combinators classes.algebra alien alien.c-types alien.structs -alien.strings alien.arrays alien.complex sets libc +alien.strings alien.arrays alien.complex sets libc alien.libraries continuations.private fry cpu.architecture compiler.errors compiler.alien diff --git a/basis/compression/zlib/ffi/ffi.factor b/basis/compression/zlib/ffi/ffi.factor index d369c22e4c..a472f9a2fe 100755 --- a/basis/compression/zlib/ffi/ffi.factor +++ b/basis/compression/zlib/ffi/ffi.factor @@ -1,6 +1,6 @@ ! Copyright (C) 2009 Doug Coleman. ! See http://factorcode.org/license.txt for BSD license. -USING: alien alien.syntax combinators system ; +USING: alien alien.syntax combinators system alien.libraries ; IN: compression.zlib.ffi << "zlib" { diff --git a/basis/core-text/utilities/utilities.factor b/basis/core-text/utilities/utilities.factor index 7de601c433..d3b1352750 100644 --- a/basis/core-text/utilities/utilities.factor +++ b/basis/core-text/utilities/utilities.factor @@ -1,6 +1,7 @@ ! Copyright (C) 2009 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. -USING: words parser alien alien.c-types kernel fry accessors ; +USING: words parser alien alien.c-types kernel fry accessors +alien.libraries ; IN: core-text.utilities SYNTAX: C-GLOBAL: diff --git a/basis/db/postgresql/ffi/ffi.factor b/basis/db/postgresql/ffi/ffi.factor index fc407b06bd..93f93c9a13 100644 --- a/basis/db/postgresql/ffi/ffi.factor +++ b/basis/db/postgresql/ffi/ffi.factor @@ -1,7 +1,7 @@ ! Copyright (C) 2007, 2008 Doug Coleman. ! See http://factorcode.org/license.txt for BSD license. ! tested on debian linux with postgresql 8.1 -USING: alien alien.syntax combinators system ; +USING: alien alien.syntax combinators system alien.libraries ; IN: db.postgresql.ffi << "postgresql" { diff --git a/basis/db/sqlite/ffi/ffi.factor b/basis/db/sqlite/ffi/ffi.factor index 341995634e..61394391a0 100644 --- a/basis/db/sqlite/ffi/ffi.factor +++ b/basis/db/sqlite/ffi/ffi.factor @@ -3,7 +3,7 @@ ! An interface to the sqlite database. Tested against sqlite v3.1.3. ! Not all functions have been wrapped. USING: alien compiler kernel math namespaces sequences strings alien.syntax - system combinators alien.c-types ; +system combinators alien.c-types alien.libraries ; IN: db.sqlite.ffi << "sqlite" { diff --git a/basis/debugger/debugger-docs.factor b/basis/debugger/debugger-docs.factor index 30c9fd37ab..ff5869efab 100644 --- a/basis/debugger/debugger-docs.factor +++ b/basis/debugger/debugger-docs.factor @@ -1,6 +1,7 @@ USING: alien arrays generic generic.math help.markup help.syntax kernel math memory strings sbufs vectors io io.files classes -help generic.standard continuations io.files.private listener ; +help generic.standard continuations io.files.private listener +alien.libraries ; IN: debugger ARTICLE: "debugger" "The debugger" diff --git a/basis/glib/glib.factor b/basis/glib/glib.factor index 1805f4bff9..ca481cb900 100755 --- a/basis/glib/glib.factor +++ b/basis/glib/glib.factor @@ -1,7 +1,8 @@ ! Copyright (C) 2008 Matthew Willis. ! Copyright (C) 2009 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license -USING: alien alien.syntax alien.destructors combinators system ; +USING: alien alien.syntax alien.destructors combinators system +alien.libraries ; IN: glib << diff --git a/basis/opengl/gl/macosx/macosx.factor b/basis/opengl/gl/macosx/macosx.factor index eb8dda5e33..9e095a62e6 100644 --- a/basis/opengl/gl/macosx/macosx.factor +++ b/basis/opengl/gl/macosx/macosx.factor @@ -1,4 +1,4 @@ -USING: kernel alien ; +USING: kernel alien alien.libraries ; IN: opengl.gl.macosx : gl-function-context ( -- context ) 0 ; inline diff --git a/basis/openssl/libcrypto/libcrypto.factor b/basis/openssl/libcrypto/libcrypto.factor index 1a25b4d019..b9e00b6c8d 100644 --- a/basis/openssl/libcrypto/libcrypto.factor +++ b/basis/openssl/libcrypto/libcrypto.factor @@ -5,7 +5,8 @@ ! ! export LD_LIBRARY_PATH=/opt/local/lib -USING: alien alien.syntax combinators kernel system ; +USING: alien alien.syntax combinators kernel system +alien.libraries ; IN: openssl.libcrypto diff --git a/basis/openssl/libssl/libssl.factor b/basis/openssl/libssl/libssl.factor index 8ed15a4e5e..21f712fdc8 100644 --- a/basis/openssl/libssl/libssl.factor +++ b/basis/openssl/libssl/libssl.factor @@ -2,7 +2,8 @@ ! Portions copyright (C) 2008 Slava Pestov ! See http://factorcode.org/license.txt for BSD license. USING: alien alien.syntax combinators kernel system namespaces -assocs parser lexer sequences words quotations math.bitwise ; +assocs parser lexer sequences words quotations math.bitwise +alien.libraries ; IN: openssl.libssl diff --git a/basis/pango/pango.factor b/basis/pango/pango.factor index 540d1cb9f7..ec5afa3c3d 100644 --- a/basis/pango/pango.factor +++ b/basis/pango/pango.factor @@ -2,7 +2,7 @@ ! Copyright (C) 2009 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license USING: arrays system alien.destructors alien.c-types alien.syntax alien -combinators math.rectangles kernel math ; +combinators math.rectangles kernel math alien.libraries ; IN: pango ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! @@ -34,4 +34,4 @@ C-STRUCT: PangoRectangle : PangoRectangle>rect ( PangoRectangle -- rect ) [ [ PangoRectangle-x pango>float ] [ PangoRectangle-y pango>float ] bi 2array ] [ [ PangoRectangle-width pango>float ] [ PangoRectangle-height pango>float ] bi 2array ] bi - ; \ No newline at end of file + ; diff --git a/basis/stack-checker/alien/alien.factor b/basis/stack-checker/alien/alien.factor index f52632040d..0b135319ff 100644 --- a/basis/stack-checker/alien/alien.factor +++ b/basis/stack-checker/alien/alien.factor @@ -1,7 +1,7 @@ ! Copyright (C) 2008 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. USING: kernel sequences accessors combinators math namespaces -init sets words +init sets words alien.libraries alien alien.c-types stack-checker.backend stack-checker.errors stack-checker.visitor ; IN: stack-checker.alien diff --git a/basis/stack-checker/known-words/known-words.factor b/basis/stack-checker/known-words/known-words.factor index a7f348d36b..a38bb42c7e 100644 --- a/basis/stack-checker/known-words/known-words.factor +++ b/basis/stack-checker/known-words/known-words.factor @@ -12,6 +12,7 @@ classes.tuple.private vectors vectors.private words definitions words.private assocs summary compiler.units system.private combinators locals locals.backend locals.types words.private quotations.private combinators.private stack-checker.values +alien.libraries stack-checker.alien stack-checker.state stack-checker.errors diff --git a/basis/tools/disassembler/udis/udis.factor b/basis/tools/disassembler/udis/udis.factor index 304595f41c..51e399c1c3 100755 --- a/basis/tools/disassembler/udis/udis.factor +++ b/basis/tools/disassembler/udis/udis.factor @@ -2,7 +2,7 @@ ! See http://factorcode.org/license.txt for BSD license. USING: tools.disassembler namespaces combinators alien alien.syntax alien.c-types lexer parser kernel -sequences layouts math math.order +sequences layouts math math.order alien.libraries math.parser system make fry arrays ; IN: tools.disassembler.udis diff --git a/basis/ui/tools/profiler/profiler.factor b/basis/ui/tools/profiler/profiler.factor index 6bca4b40c4..1c2318a35e 100644 --- a/basis/ui/tools/profiler/profiler.factor +++ b/basis/ui/tools/profiler/profiler.factor @@ -210,4 +210,4 @@ profiler-gadget "toolbar" f { : com-profile ( quot -- ) profile profiler-window ; -MAIN: profiler-window \ No newline at end of file +MAIN: profiler-window diff --git a/basis/windows/ce/ce.factor b/basis/windows/ce/ce.factor index 18c48a0139..ff6a9ad4fc 100644 --- a/basis/windows/ce/ce.factor +++ b/basis/windows/ce/ce.factor @@ -1,4 +1,4 @@ -USING: alien sequences ; +USING: alien sequences alien.libraries ; { { "advapi32" "\\windows\\coredll.dll" "stdcall" } { "gdi32" "\\windows\\coredll.dll" "stdcall" } diff --git a/basis/windows/dinput/dinput.factor b/basis/windows/dinput/dinput.factor index dc544858b6..20a54dff98 100755 --- a/basis/windows/dinput/dinput.factor +++ b/basis/windows/dinput/dinput.factor @@ -2,12 +2,6 @@ USING: windows.kernel32 windows.ole32 windows.com windows.com.syntax alien alien.c-types alien.syntax kernel system namespaces math ; IN: windows.dinput -<< - os windows? - [ "dinput" "dinput8.dll" "stdcall" add-library ] - when ->> - LIBRARY: dinput TYPEDEF: void* LPDIENUMDEVICESCALLBACKW diff --git a/basis/windows/nt/nt.factor b/basis/windows/nt/nt.factor index 24d0032c5b..e05a409e67 100644 --- a/basis/windows/nt/nt.factor +++ b/basis/windows/nt/nt.factor @@ -1,6 +1,7 @@ -USING: alien sequences ; +USING: alien sequences alien.libraries ; { { "advapi32" "advapi32.dll" "stdcall" } + { "dinput" "dinput8.dll" "stdcall" } { "gdi32" "gdi32.dll" "stdcall" } { "user32" "user32.dll" "stdcall" } { "kernel32" "kernel32.dll" "stdcall" } diff --git a/core/alien/alien-docs.factor b/core/alien/alien-docs.factor index edac8c09cc..398d04e4dd 100644 --- a/core/alien/alien-docs.factor +++ b/core/alien/alien-docs.factor @@ -1,7 +1,7 @@ USING: byte-arrays arrays help.syntax help.markup -alien.syntax compiler definitions math libc -debugger parser io io.backend system -alien.accessors eval ; +alien.syntax compiler definitions math libc eval +debugger parser io io.backend system alien.accessors +alien.libraries ; IN: alien HELP: alien @@ -22,16 +22,6 @@ HELP: { $values { "alien" c-ptr } } { $description "Constructs an invalid alien pointer that has expired." } ; -HELP: -{ $values - { "path" "a pathname string" } { "abi" "the ABI used by the library, either " { $snippet "cdecl" } " or " { $snippet "stdcall" } } - { "library" library } } -{ $description "Opens a C library using the path and ABI parameters and outputs a library tuple." } -{ $notes "User code should use " { $link add-library } " so that the opened library is added to a global hashtable, " { $link libraries } "." } ; - -HELP: libraries -{ $description "A global hashtable that keeps a list of open libraries. Use the " { $link add-library } " word to construct a library and add it with a single call." } ; - HELP: ( displacement c-ptr -- alien ) { $values { "displacement" "an integer" } { "c-ptr" c-ptr } { "alien" "a new alien" } } { $description "Creates a new alien address object, wrapping a raw memory address. The alien points to a location in memory which is offset by " { $snippet "displacement" } " from the address of " { $snippet "c-ptr" } "." } @@ -54,61 +44,6 @@ HELP: HELP: c-ptr { $class-description "Class of objects consisting of aliens, byte arrays and " { $link f } ". These objects can convert to pointer C types, which are all aliases of " { $snippet "void*" } "." } ; -HELP: library -{ $values { "name" "a string" } { "library" "a hashtable" } } -{ $description "Looks up a library by its logical name. The library object is a hashtable with the following keys:" - { $list - { { $snippet "name" } " - the full path of the C library binary" } - { { $snippet "abi" } " - the ABI used by the library, either " { $snippet "cdecl" } " or " { $snippet "stdcall" } } - { { $snippet "dll" } " - an instance of the " { $link dll } " class; only set if the library is loaded" } - } -} ; - -HELP: dlopen ( path -- dll ) -{ $values { "path" "a pathname string" } { "dll" "a DLL handle" } } -{ $description "Opens a native library and outputs a handle which may be passed to " { $link dlsym } " or " { $link dlclose } "." } -{ $errors "Throws an error if the library could not be found, or if loading fails for some other reason." } -{ $notes "This is the low-level facility used to implement " { $link load-library } ". Use the latter instead." } ; - -HELP: dlsym ( name dll -- alien ) -{ $values { "name" "a C symbol name" } { "dll" "a DLL handle" } { "alien" "an alien pointer" } } -{ $description "Looks up a symbol in a native library. If " { $snippet "dll" } " is " { $link f } " looks for the symbol in the runtime executable." } -{ $errors "Throws an error if the symbol could not be found." } ; - -HELP: dlclose ( dll -- ) -{ $values { "dll" "a DLL handle" } } -{ $description "Closes a DLL handle created by " { $link dlopen } ". This word might not be implemented on all platforms." } ; - -HELP: load-library -{ $values { "name" "a string" } { "dll" "a DLL handle" } } -{ $description "Loads a library by logical name and outputs a handle which may be passed to " { $link dlsym } " or " { $link dlclose } ". If the library is already loaded, returns the existing handle." } ; - -HELP: add-library -{ $values { "name" "a string" } { "path" "a string" } { "abi" "one of " { $snippet "\"cdecl\"" } " or " { $snippet "\"stdcall\"" } } } -{ $description "Defines a new logical library named " { $snippet "name" } " located in the file system at " { $snippet "path" } "and the specified ABI." } -{ $notes "Because the entire source file is parsed before top-level forms are executed, " { $link add-library } " cannot be used in the same file as " { $link POSTPONE: FUNCTION: } " definitions from that library. The " { $link add-library } " call will happen too late, after compilation, and the alien calls will not work." -$nl -"Instead, " { $link add-library } " calls must either be placed in different source files from those that use that library, or alternatively, " { $link "syntax-immediate" } " can be used to load the library before compilation." } -{ $examples "Here is a typical usage of " { $link add-library } ":" -{ $code - "<< \"freetype\" {" - " { [ os macosx? ] [ \"libfreetype.6.dylib\" \"cdecl\" add-library ] }" - " { [ os windows? ] [ \"freetype6.dll\" \"cdecl\" add-library ] }" - " [ drop ]" - "} cond >>" -} -"Note the parse time evaluation with " { $link POSTPONE: << } "." } ; - -HELP: alien-invoke-error -{ $error-description "Thrown if the word calling " { $link alien-invoke } " was not compiled with the optimizing compiler. This may be a result of one of several failure conditions:" - { $list - { "This can happen when experimenting with " { $link alien-invoke } " in this listener. To fix the problem, place the " { $link alien-invoke } " call in a word; word definitions are automatically compiled with the optimizing compiler." } - { "The return type or parameter list references an unknown C type." } - { "The symbol or library could not be found." } - { "One of the four inputs to " { $link alien-invoke } " is not a literal value. To call functions which are not known at compile-time, use " { $link alien-indirect } "." } - } -} ; - HELP: alien-invoke { $values { "..." "zero or more objects passed to the C function" } { "return" "a C return type" } { "library" "a logical library name" } { "function" "a C function name" } { "parameters" "a sequence of C parameter types" } } { $description "Calls a C library function with the given name. Input parameters are taken from the data stack, and the return value is pushed on the data stack after the function returns. A return type of " { $snippet "\"void\"" } " indicates that no value is to be expected." } @@ -226,6 +161,16 @@ ARTICLE: "alien-invoke" "Calling C from Factor" { $subsection alien-indirect } "There are some details concerning the conversion of Factor objects to C values, and vice versa. See " { $link "c-data" } "." ; +HELP: alien-invoke-error +{ $error-description "Thrown if the word calling " { $link alien-invoke } " was not compiled with the optimizing compiler. This may be a result of one of several failure conditions:" + { $list + { "This can happen when experimenting with " { $link alien-invoke } " in this listener. To fix the problem, place the " { $link alien-invoke } " call in a word; word definitions are automatically compiled with the optimizing compiler." } + { "The return type or parameter list references an unknown C type." } + { "The symbol or library could not be found." } + { "One of the four inputs to " { $link alien-invoke } " is not a literal value. To call functions which are not known at compile-time, use " { $link alien-indirect } "." } + } +} ; + ARTICLE: "alien-callback-gc" "Callbacks and code GC" "A callback consits of two parts; the callback word, which pushes the address of the callback on the stack when executed, and the callback body itself. If the callback word is redefined, removed from the dictionary using " { $link forget } ", or recompiled, the callback body will not be reclaimed by the garbage collector, since potentially C code may be holding a reference to the callback body." $nl @@ -321,6 +266,7 @@ $nl { $warning "C does not perform runtime type checking, automatic memory management or array bounds checks. Incorrect usage of C library functions can lead to crashes, data corruption, and security exploits." } { $subsection "loading-libs" } { $subsection "aliens" } +{ $subsection "alien.libraries" } { $subsection "alien-invoke" } { $subsection "alien-callback" } { $subsection "c-data" } diff --git a/core/alien/alien-tests.factor b/core/alien/alien-tests.factor index e4063b733c..57dc298c00 100644 --- a/core/alien/alien-tests.factor +++ b/core/alien/alien-tests.factor @@ -1,7 +1,7 @@ -IN: alien.tests USING: accessors alien alien.accessors alien.syntax byte-arrays arrays kernel kernel.private namespaces tools.test sequences libc math -system prettyprint layouts ; +system prettyprint layouts alien.libraries ; +IN: alien.tests [ t ] [ -1 alien-address 0 > ] unit-test @@ -85,4 +85,4 @@ f initialize-test set-global [ ] [ initialize-test get BAD-ALIEN >>alien drop ] unit-test -[ 7575 ] [ initialize-test [ 7575 ] initialize-alien ] unit-test \ No newline at end of file +[ 7575 ] [ initialize-test [ 7575 ] initialize-alien ] unit-test diff --git a/core/alien/alien.factor b/core/alien/alien.factor index 8e48ac770c..ea0cb9208e 100644 --- a/core/alien/alien.factor +++ b/core/alien/alien.factor @@ -1,7 +1,7 @@ ! Copyright (C) 2004, 2009 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. USING: accessors assocs kernel math namespaces sequences system -kernel.private byte-arrays arrays init io.backend ; +kernel.private byte-arrays arrays init ; IN: alien ! Some predicate classes used by the compiler for optimization @@ -49,24 +49,6 @@ M: alien equal? 2drop f ] if ; -SYMBOL: libraries - -libraries [ H{ } clone ] initialize - -TUPLE: library path abi dll ; - -: library ( name -- library ) libraries get at ; - -: ( path abi -- library ) - over dup [ dlopen ] when \ library boa ; - -: load-library ( name -- dll ) - library dup [ dll>> ] when ; - -: add-library ( name path abi -- ) - [ dup [ normalize-path ] when ] dip - swap libraries get set-at ; - ERROR: alien-callback-error ; : alien-callback ( return parameters abi quot -- alien ) diff --git a/core/bootstrap/primitives.factor b/core/bootstrap/primitives.factor index ed64571582..e3803f2150 100644 --- a/core/bootstrap/primitives.factor +++ b/core/bootstrap/primitives.factor @@ -62,6 +62,7 @@ bootstrapping? on { "alien" "alien.accessors" + "alien.libraries" "arrays" "byte-arrays" "classes.private" @@ -458,9 +459,9 @@ tuple { "code-room" "memory" (( -- code-free code-total )) } { "micros" "system" (( -- us )) } { "modify-code-heap" "compiler.units" (( alist -- )) } - { "dlopen" "alien" (( path -- dll )) } - { "dlsym" "alien" (( name dll -- alien )) } - { "dlclose" "alien" (( dll -- )) } + { "dlopen" "alien.libraries" (( path -- dll )) } + { "dlsym" "alien.libraries" (( name dll -- alien )) } + { "dlclose" "alien.libraries" (( dll -- )) } { "" "byte-arrays" (( n -- byte-array )) } { "(byte-array)" "byte-arrays" (( n -- byte-array )) } { "" "alien" (( displacement c-ptr -- alien )) } diff --git a/extra/curses/ffi/ffi.factor b/extra/curses/ffi/ffi.factor index b1c481a576..3ff9404bff 100644 --- a/extra/curses/ffi/ffi.factor +++ b/extra/curses/ffi/ffi.factor @@ -1,6 +1,7 @@ ! Copyright (C) 2008 Doug Coleman. ! See http://factorcode.org/license.txt for BSD license. -USING: accessors alien alien.syntax combinators kernel system ; +USING: accessors alien alien.syntax combinators kernel system +alien.libraries ; IN: curses.ffi << "curses" { diff --git a/extra/freetype/freetype.factor b/extra/freetype/freetype.factor index 06c875b2fa..c45475cefa 100644 --- a/extra/freetype/freetype.factor +++ b/extra/freetype/freetype.factor @@ -1,6 +1,7 @@ ! Copyright (C) 2005, 2007 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. -USING: alien alien.syntax kernel system combinators ; +USING: alien alien.syntax kernel system combinators +alien.libraries ; IN: freetype << "freetype" { From 77fccb27825a751ac49f54fe6ac6cd31cbc95a71 Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Thu, 26 Mar 2009 10:38:00 -0500 Subject: [PATCH 09/16] build with -fPIC on linux64 --- vm/Config.linux.x86.64 | 1 + 1 file changed, 1 insertion(+) diff --git a/vm/Config.linux.x86.64 b/vm/Config.linux.x86.64 index bfd1222496..7771ebdbbd 100644 --- a/vm/Config.linux.x86.64 +++ b/vm/Config.linux.x86.64 @@ -1,3 +1,4 @@ include vm/Config.linux include vm/Config.x86.64 LIBPATH = -L/usr/X11R6/lib64 -L/usr/X11R6/lib +CFLAGS = -fPIC From 4e33df153df2c64927dd2404fe2c1812502b8df9 Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Thu, 26 Mar 2009 10:43:54 -0500 Subject: [PATCH 10/16] add using --- basis/pango/cairo/cairo.factor | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/basis/pango/cairo/cairo.factor b/basis/pango/cairo/cairo.factor index 7683b78890..45b7a9cb31 100644 --- a/basis/pango/cairo/cairo.factor +++ b/basis/pango/cairo/cairo.factor @@ -3,7 +3,8 @@ ! See http://factorcode.org/license.txt for BSD license. ! ! pangocairo bindings, from pango/pangocairo.h -USING: alien alien.syntax combinators system cairo.ffi ; +USING: alien alien.syntax combinators system cairo.ffi +alien.libraries ; IN: pango.cairo << { @@ -85,4 +86,4 @@ FUNCTION: void pango_cairo_layout_path ( cairo_t* cr, PangoLayout* layout ) ; FUNCTION: void -pango_cairo_error_underline_path ( cairo_t* cr, double x, double y, double width, double height ) ; \ No newline at end of file +pango_cairo_error_underline_path ( cairo_t* cr, double x, double y, double width, double height ) ; From b771758ff7d32676c18c6b7e2e041fb24f4526a4 Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Thu, 26 Mar 2009 10:45:52 -0500 Subject: [PATCH 11/16] fix load errors, help lint --- basis/alien/fortran/fortran.factor | 2 +- basis/compiler/tests/alien.factor | 2 +- basis/math/blas/ffi/ffi.factor | 3 ++- core/alien/alien-docs.factor | 1 - 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/basis/alien/fortran/fortran.factor b/basis/alien/fortran/fortran.factor index 83d56bf9e2..71efa1aa24 100644 --- a/basis/alien/fortran/fortran.factor +++ b/basis/alien/fortran/fortran.factor @@ -5,7 +5,7 @@ byte-arrays combinators combinators.short-circuit fry generalizations kernel lexer macros math math.parser namespaces parser sequences splitting stack-checker vectors vocabs.parser words locals io.encodings.ascii io.encodings.string shuffle effects math.ranges -math.order sorting strings system ; +math.order sorting strings system alien.libraries ; IN: alien.fortran SINGLETONS: f2c-abi gfortran-abi intel-unix-abi intel-windows-abi ; diff --git a/basis/compiler/tests/alien.factor b/basis/compiler/tests/alien.factor index 2aed251676..aa9346f788 100644 --- a/basis/compiler/tests/alien.factor +++ b/basis/compiler/tests/alien.factor @@ -4,7 +4,7 @@ namespaces namespaces tools.test sequences stack-checker stack-checker.errors words arrays parser quotations continuations effects namespaces.private io io.streams.string memory system threads tools.test math accessors combinators -specialized-arrays.float ; +specialized-arrays.float alien.libraries ; << : libfactor-ffi-tests-path ( -- string ) diff --git a/basis/math/blas/ffi/ffi.factor b/basis/math/blas/ffi/ffi.factor index 1749103ce4..5466ad2161 100644 --- a/basis/math/blas/ffi/ffi.factor +++ b/basis/math/blas/ffi/ffi.factor @@ -1,4 +1,5 @@ -USING: alien alien.fortran kernel system combinators ; +USING: alien alien.fortran kernel system combinators +alien.libraries ; IN: math.blas.ffi << diff --git a/core/alien/alien-docs.factor b/core/alien/alien-docs.factor index 398d04e4dd..6bd1d2f53a 100644 --- a/core/alien/alien-docs.factor +++ b/core/alien/alien-docs.factor @@ -266,7 +266,6 @@ $nl { $warning "C does not perform runtime type checking, automatic memory management or array bounds checks. Incorrect usage of C library functions can lead to crashes, data corruption, and security exploits." } { $subsection "loading-libs" } { $subsection "aliens" } -{ $subsection "alien.libraries" } { $subsection "alien-invoke" } { $subsection "alien-callback" } { $subsection "c-data" } From 785e3bcfd01db9e2bdf3e48a4cdfffc41bd0fbea Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Thu, 26 Mar 2009 10:47:25 -0500 Subject: [PATCH 12/16] only use -fPIC on the ffi library --- Makefile | 2 +- vm/Config.linux.x86.64 | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 0c426ca5bc..bfaaa3eee4 100644 --- a/Makefile +++ b/Makefile @@ -160,7 +160,7 @@ factor-console: $(DLL_OBJS) $(EXE_OBJS) $(CFLAGS) $(CFLAGS_CONSOLE) -o factor$(EXE_SUFFIX)$(CONSOLE_EXTENSION) $(EXE_OBJS) factor-ffi-test: $(TEST_OBJS) - $(CC) $(LIBPATH) $(CFLAGS) $(SHARED_FLAG) -o libfactor-ffi-test$(DLL_EXTENSION) $(TEST_OBJS) + $(CC) $(LIBPATH) $(CFLAGS) $(FFI_TEST_CFLAGS) $(SHARED_FLAG) -o libfactor-ffi-test$(DLL_EXTENSION) $(TEST_OBJS) clean: rm -f vm/*.o diff --git a/vm/Config.linux.x86.64 b/vm/Config.linux.x86.64 index 7771ebdbbd..a02fcb4d6d 100644 --- a/vm/Config.linux.x86.64 +++ b/vm/Config.linux.x86.64 @@ -1,4 +1,4 @@ include vm/Config.linux include vm/Config.x86.64 LIBPATH = -L/usr/X11R6/lib64 -L/usr/X11R6/lib -CFLAGS = -fPIC +FFI_TEST_CFLAGS = -fPIC From 63e06589bddfa387fdbe41cc0abeaeac83f7b205 Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Thu, 26 Mar 2009 18:56:10 -0500 Subject: [PATCH 13/16] don't normalize-path in add-library --- basis/alien/libraries/libraries.factor | 1 - basis/compiler/tests/alien.factor | 11 ++++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/basis/alien/libraries/libraries.factor b/basis/alien/libraries/libraries.factor index adb9eeb1bb..3fcc15974c 100644 --- a/basis/alien/libraries/libraries.factor +++ b/basis/alien/libraries/libraries.factor @@ -18,5 +18,4 @@ TUPLE: library path abi dll ; library dup [ dll>> ] when ; : add-library ( name path abi -- ) - [ dup [ normalize-path ] when ] dip swap libraries get set-at ; diff --git a/basis/compiler/tests/alien.factor b/basis/compiler/tests/alien.factor index aa9346f788..b26abb561c 100644 --- a/basis/compiler/tests/alien.factor +++ b/basis/compiler/tests/alien.factor @@ -1,18 +1,19 @@ -IN: compiler.tests USING: alien alien.c-types alien.syntax compiler kernel namespaces namespaces tools.test sequences stack-checker stack-checker.errors words arrays parser quotations continuations effects namespaces.private io io.streams.string memory system threads tools.test math accessors combinators specialized-arrays.float alien.libraries ; +IN: compiler.tests << : libfactor-ffi-tests-path ( -- string ) + "resource:" normalize-path { - { [ os winnt? ] [ "resource:libfactor-ffi-test.dll" ] } - { [ os macosx? ] [ "resource:libfactor-ffi-test.dylib" ] } - { [ os unix? ] [ "resource:libfactor-ffi-test.so" ] } - } cond ; + { [ os winnt? ] [ "libfactor-ffi-test.dll" ] } + { [ os macosx? ] [ "libfactor-ffi-test.dylib" ] } + { [ os unix? ] [ "libfactor-ffi-test.so" ] } + } cond append-path ; "f-cdecl" libfactor-ffi-tests-path "cdecl" add-library From 73b8bf1193deb75a4d147c979fc05c88df7cebb6 Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Thu, 26 Mar 2009 19:58:23 -0500 Subject: [PATCH 14/16] fix using list --- basis/compiler/tests/alien.factor | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/basis/compiler/tests/alien.factor b/basis/compiler/tests/alien.factor index b26abb561c..7d65ea7103 100644 --- a/basis/compiler/tests/alien.factor +++ b/basis/compiler/tests/alien.factor @@ -3,7 +3,8 @@ namespaces namespaces tools.test sequences stack-checker stack-checker.errors words arrays parser quotations continuations effects namespaces.private io io.streams.string memory system threads tools.test math accessors combinators -specialized-arrays.float alien.libraries ; +specialized-arrays.float alien.libraries io.pathnames +io.backend ; IN: compiler.tests << From 9daf3d27715d427b49a0907379889717e9f1fd9f Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Thu, 26 Mar 2009 20:19:45 -0500 Subject: [PATCH 15/16] fix the makefile for linux64 ffi_test --- Makefile | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index bfaaa3eee4..3f385ec496 100644 --- a/Makefile +++ b/Makefile @@ -159,7 +159,7 @@ factor-console: $(DLL_OBJS) $(EXE_OBJS) $(CC) $(LIBS) $(LIBPATH) -L. $(LINK_WITH_ENGINE) \ $(CFLAGS) $(CFLAGS_CONSOLE) -o factor$(EXE_SUFFIX)$(CONSOLE_EXTENSION) $(EXE_OBJS) -factor-ffi-test: $(TEST_OBJS) +factor-ffi-test: vm/ffi_test.o $(CC) $(LIBPATH) $(CFLAGS) $(FFI_TEST_CFLAGS) $(SHARED_FLAG) -o libfactor-ffi-test$(DLL_EXTENSION) $(TEST_OBJS) clean: @@ -169,6 +169,9 @@ clean: vm/resources.o: $(WINDRES) vm/factor.rs vm/resources.o +vm/ffi_test.o: vm/ffi_test.c + $(CC) -c $(CFLAGS) $(FFI_TEST_CFLAGS) -o $@ $< + .c.o: $(CC) -c $(CFLAGS) -o $@ $< From e04efe5f5acedd74d82298ac1b0269de52b5082f Mon Sep 17 00:00:00 2001 From: sheeple Date: Fri, 27 Mar 2009 10:41:24 -0500 Subject: [PATCH 16/16] use .a instead of .so on unix --- basis/compiler/tests/alien.factor | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/basis/compiler/tests/alien.factor b/basis/compiler/tests/alien.factor index 7d65ea7103..c65fed55e2 100644 --- a/basis/compiler/tests/alien.factor +++ b/basis/compiler/tests/alien.factor @@ -13,7 +13,7 @@ IN: compiler.tests { { [ os winnt? ] [ "libfactor-ffi-test.dll" ] } { [ os macosx? ] [ "libfactor-ffi-test.dylib" ] } - { [ os unix? ] [ "libfactor-ffi-test.so" ] } + { [ os unix? ] [ "libfactor-ffi-test.a" ] } } cond append-path ; "f-cdecl" libfactor-ffi-tests-path "cdecl" add-library